* buffer.h (FETCH_MULTIBYTE_CHAR): Define as inline.
[emacs.git] / src / xdisp.c
blobcd5f03d34ad3b046f5c248ebcfe61f7be9c1074a
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 #if GLYPH_DEBUG
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
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 xassert (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;
1254 /* Return 1 if position CHARPOS is visible in window W.
1255 CHARPOS < 0 means return info about WINDOW_END position.
1256 If visible, set *X and *Y to pixel coordinates of top left corner.
1257 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1258 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1261 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1262 int *rtop, int *rbot, int *rowh, int *vpos)
1264 struct it it;
1265 void *itdata = bidi_shelve_cache ();
1266 struct text_pos top;
1267 int visible_p = 0;
1268 struct buffer *old_buffer = NULL;
1270 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1271 return visible_p;
1273 if (XBUFFER (w->buffer) != current_buffer)
1275 old_buffer = current_buffer;
1276 set_buffer_internal_1 (XBUFFER (w->buffer));
1279 SET_TEXT_POS_FROM_MARKER (top, w->start);
1280 /* Scrolling a minibuffer window via scroll bar when the echo area
1281 shows long text sometimes resets the minibuffer contents behind
1282 our backs. */
1283 if (CHARPOS (top) > ZV)
1284 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1286 /* Compute exact mode line heights. */
1287 if (WINDOW_WANTS_MODELINE_P (w))
1288 current_mode_line_height
1289 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1290 BVAR (current_buffer, mode_line_format));
1292 if (WINDOW_WANTS_HEADER_LINE_P (w))
1293 current_header_line_height
1294 = display_mode_line (w, HEADER_LINE_FACE_ID,
1295 BVAR (current_buffer, header_line_format));
1297 start_display (&it, w, top);
1298 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1299 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1301 if (charpos >= 0
1302 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1303 && IT_CHARPOS (it) >= charpos)
1304 /* When scanning backwards under bidi iteration, move_it_to
1305 stops at or _before_ CHARPOS, because it stops at or to
1306 the _right_ of the character at CHARPOS. */
1307 || (it.bidi_p && it.bidi_it.scan_dir == -1
1308 && IT_CHARPOS (it) <= charpos)))
1310 /* We have reached CHARPOS, or passed it. How the call to
1311 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1312 or covered by a display property, move_it_to stops at the end
1313 of the invisible text, to the right of CHARPOS. (ii) If
1314 CHARPOS is in a display vector, move_it_to stops on its last
1315 glyph. */
1316 int top_x = it.current_x;
1317 int top_y = it.current_y;
1318 /* Calling line_bottom_y may change it.method, it.position, etc. */
1319 enum it_method it_method = it.method;
1320 int bottom_y = (last_height = 0, line_bottom_y (&it));
1321 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1323 if (top_y < window_top_y)
1324 visible_p = bottom_y > window_top_y;
1325 else if (top_y < it.last_visible_y)
1326 visible_p = 1;
1327 if (bottom_y >= it.last_visible_y
1328 && it.bidi_p && it.bidi_it.scan_dir == -1
1329 && IT_CHARPOS (it) < charpos)
1331 /* When the last line of the window is scanned backwards
1332 under bidi iteration, we could be duped into thinking
1333 that we have passed CHARPOS, when in fact move_it_to
1334 simply stopped short of CHARPOS because it reached
1335 last_visible_y. To see if that's what happened, we call
1336 move_it_to again with a slightly larger vertical limit,
1337 and see if it actually moved vertically; if it did, we
1338 didn't really reach CHARPOS, which is beyond window end. */
1339 struct it save_it = it;
1340 /* Why 10? because we don't know how many canonical lines
1341 will the height of the next line(s) be. So we guess. */
1342 int ten_more_lines =
1343 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1345 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1346 MOVE_TO_POS | MOVE_TO_Y);
1347 if (it.current_y > top_y)
1348 visible_p = 0;
1350 it = save_it;
1352 if (visible_p)
1354 if (it_method == GET_FROM_DISPLAY_VECTOR)
1356 /* We stopped on the last glyph of a display vector.
1357 Try and recompute. Hack alert! */
1358 if (charpos < 2 || top.charpos >= charpos)
1359 top_x = it.glyph_row->x;
1360 else
1362 struct it it2;
1363 start_display (&it2, w, top);
1364 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1365 get_next_display_element (&it2);
1366 PRODUCE_GLYPHS (&it2);
1367 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1368 || it2.current_x > it2.last_visible_x)
1369 top_x = it.glyph_row->x;
1370 else
1372 top_x = it2.current_x;
1373 top_y = it2.current_y;
1377 else if (IT_CHARPOS (it) != charpos)
1379 Lisp_Object cpos = make_number (charpos);
1380 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1381 Lisp_Object string = string_from_display_spec (spec);
1382 int newline_in_string = 0;
1384 if (STRINGP (string))
1386 const char *s = SSDATA (string);
1387 const char *e = s + SBYTES (string);
1388 while (s < e)
1390 if (*s++ == '\n')
1392 newline_in_string = 1;
1393 break;
1397 /* The tricky code below is needed because there's a
1398 discrepancy between move_it_to and how we set cursor
1399 when the display line ends in a newline from a
1400 display string. move_it_to will stop _after_ such
1401 display strings, whereas set_cursor_from_row
1402 conspires with cursor_row_p to place the cursor on
1403 the first glyph produced from the display string. */
1405 /* We have overshoot PT because it is covered by a
1406 display property whose value is a string. If the
1407 string includes embedded newlines, we are also in the
1408 wrong display line. Backtrack to the correct line,
1409 where the display string begins. */
1410 if (newline_in_string)
1412 Lisp_Object startpos, endpos;
1413 EMACS_INT start, end;
1414 struct it it3;
1415 int it3_moved;
1417 /* Find the first and the last buffer positions
1418 covered by the display string. */
1419 endpos =
1420 Fnext_single_char_property_change (cpos, Qdisplay,
1421 Qnil, Qnil);
1422 startpos =
1423 Fprevious_single_char_property_change (endpos, Qdisplay,
1424 Qnil, Qnil);
1425 start = XFASTINT (startpos);
1426 end = XFASTINT (endpos);
1427 /* Move to the last buffer position before the
1428 display property. */
1429 start_display (&it3, w, top);
1430 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1431 /* Move forward one more line if the position before
1432 the display string is a newline or if it is the
1433 rightmost character on a line that is
1434 continued or word-wrapped. */
1435 if (it3.method == GET_FROM_BUFFER
1436 && it3.c == '\n')
1437 move_it_by_lines (&it3, 1);
1438 else if (move_it_in_display_line_to (&it3, -1,
1439 it3.current_x
1440 + it3.pixel_width,
1441 MOVE_TO_X)
1442 == MOVE_LINE_CONTINUED)
1444 move_it_by_lines (&it3, 1);
1445 /* When we are under word-wrap, the #$@%!
1446 move_it_by_lines moves 2 lines, so we need to
1447 fix that up. */
1448 if (it3.line_wrap == WORD_WRAP)
1449 move_it_by_lines (&it3, -1);
1452 /* Record the vertical coordinate of the display
1453 line where we wound up. */
1454 top_y = it3.current_y;
1455 if (it3.bidi_p)
1457 /* When characters are reordered for display,
1458 the character displayed to the left of the
1459 display string could be _after_ the display
1460 property in the logical order. Use the
1461 smallest vertical position of these two. */
1462 start_display (&it3, w, top);
1463 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1464 if (it3.current_y < top_y)
1465 top_y = it3.current_y;
1467 /* Move from the top of the window to the beginning
1468 of the display line where the display string
1469 begins. */
1470 start_display (&it3, w, top);
1471 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1472 /* If it3_moved stays zero after the 'while' loop
1473 below, that means we already were at a newline
1474 before the loop (e.g., the display string begins
1475 with a newline), so we don't need to (and cannot)
1476 inspect the glyphs of it3.glyph_row, because
1477 PRODUCE_GLYPHS will not produce anything for a
1478 newline, and thus it3.glyph_row stays at its
1479 stale content it got at top of the window. */
1480 it3_moved = 0;
1481 /* Finally, advance the iterator until we hit the
1482 first display element whose character position is
1483 CHARPOS, or until the first newline from the
1484 display string, which signals the end of the
1485 display line. */
1486 while (get_next_display_element (&it3))
1488 PRODUCE_GLYPHS (&it3);
1489 if (IT_CHARPOS (it3) == charpos
1490 || ITERATOR_AT_END_OF_LINE_P (&it3))
1491 break;
1492 it3_moved = 1;
1493 set_iterator_to_next (&it3, 0);
1495 top_x = it3.current_x - it3.pixel_width;
1496 /* Normally, we would exit the above loop because we
1497 found the display element whose character
1498 position is CHARPOS. For the contingency that we
1499 didn't, and stopped at the first newline from the
1500 display string, move back over the glyphs
1501 produced from the string, until we find the
1502 rightmost glyph not from the string. */
1503 if (it3_moved
1504 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1506 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1507 + it3.glyph_row->used[TEXT_AREA];
1509 while (EQ ((g - 1)->object, string))
1511 --g;
1512 top_x -= g->pixel_width;
1514 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1515 + it3.glyph_row->used[TEXT_AREA]);
1520 *x = top_x;
1521 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1522 *rtop = max (0, window_top_y - top_y);
1523 *rbot = max (0, bottom_y - it.last_visible_y);
1524 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1525 - max (top_y, window_top_y)));
1526 *vpos = it.vpos;
1529 else
1531 /* We were asked to provide info about WINDOW_END. */
1532 struct it it2;
1533 void *it2data = NULL;
1535 SAVE_IT (it2, it, it2data);
1536 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1537 move_it_by_lines (&it, 1);
1538 if (charpos < IT_CHARPOS (it)
1539 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1541 visible_p = 1;
1542 RESTORE_IT (&it2, &it2, it2data);
1543 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1544 *x = it2.current_x;
1545 *y = it2.current_y + it2.max_ascent - it2.ascent;
1546 *rtop = max (0, -it2.current_y);
1547 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1548 - it.last_visible_y));
1549 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1550 it.last_visible_y)
1551 - max (it2.current_y,
1552 WINDOW_HEADER_LINE_HEIGHT (w))));
1553 *vpos = it2.vpos;
1555 else
1556 bidi_unshelve_cache (it2data, 1);
1558 bidi_unshelve_cache (itdata, 0);
1560 if (old_buffer)
1561 set_buffer_internal_1 (old_buffer);
1563 current_header_line_height = current_mode_line_height = -1;
1565 if (visible_p && XFASTINT (w->hscroll) > 0)
1566 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1568 #if 0
1569 /* Debugging code. */
1570 if (visible_p)
1571 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1572 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1573 else
1574 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1575 #endif
1577 return visible_p;
1581 /* Return the next character from STR. Return in *LEN the length of
1582 the character. This is like STRING_CHAR_AND_LENGTH but never
1583 returns an invalid character. If we find one, we return a `?', but
1584 with the length of the invalid character. */
1586 static inline int
1587 string_char_and_length (const unsigned char *str, int *len)
1589 int c;
1591 c = STRING_CHAR_AND_LENGTH (str, *len);
1592 if (!CHAR_VALID_P (c))
1593 /* We may not change the length here because other places in Emacs
1594 don't use this function, i.e. they silently accept invalid
1595 characters. */
1596 c = '?';
1598 return c;
1603 /* Given a position POS containing a valid character and byte position
1604 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1606 static struct text_pos
1607 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1609 xassert (STRINGP (string) && nchars >= 0);
1611 if (STRING_MULTIBYTE (string))
1613 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1614 int len;
1616 while (nchars--)
1618 string_char_and_length (p, &len);
1619 p += len;
1620 CHARPOS (pos) += 1;
1621 BYTEPOS (pos) += len;
1624 else
1625 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1627 return pos;
1631 /* Value is the text position, i.e. character and byte position,
1632 for character position CHARPOS in STRING. */
1634 static inline struct text_pos
1635 string_pos (ptrdiff_t charpos, Lisp_Object string)
1637 struct text_pos pos;
1638 xassert (STRINGP (string));
1639 xassert (charpos >= 0);
1640 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1641 return pos;
1645 /* Value is a text position, i.e. character and byte position, for
1646 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1647 means recognize multibyte characters. */
1649 static struct text_pos
1650 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1652 struct text_pos pos;
1654 xassert (s != NULL);
1655 xassert (charpos >= 0);
1657 if (multibyte_p)
1659 int len;
1661 SET_TEXT_POS (pos, 0, 0);
1662 while (charpos--)
1664 string_char_and_length ((const unsigned char *) s, &len);
1665 s += len;
1666 CHARPOS (pos) += 1;
1667 BYTEPOS (pos) += len;
1670 else
1671 SET_TEXT_POS (pos, charpos, charpos);
1673 return pos;
1677 /* Value is the number of characters in C string S. MULTIBYTE_P
1678 non-zero means recognize multibyte characters. */
1680 static ptrdiff_t
1681 number_of_chars (const char *s, int multibyte_p)
1683 ptrdiff_t nchars;
1685 if (multibyte_p)
1687 ptrdiff_t rest = strlen (s);
1688 int len;
1689 const unsigned char *p = (const unsigned char *) s;
1691 for (nchars = 0; rest > 0; ++nchars)
1693 string_char_and_length (p, &len);
1694 rest -= len, p += len;
1697 else
1698 nchars = strlen (s);
1700 return nchars;
1704 /* Compute byte position NEWPOS->bytepos corresponding to
1705 NEWPOS->charpos. POS is a known position in string STRING.
1706 NEWPOS->charpos must be >= POS.charpos. */
1708 static void
1709 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1711 xassert (STRINGP (string));
1712 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1714 if (STRING_MULTIBYTE (string))
1715 *newpos = string_pos_nchars_ahead (pos, string,
1716 CHARPOS (*newpos) - CHARPOS (pos));
1717 else
1718 BYTEPOS (*newpos) = CHARPOS (*newpos);
1721 /* EXPORT:
1722 Return an estimation of the pixel height of mode or header lines on
1723 frame F. FACE_ID specifies what line's height to estimate. */
1726 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1728 #ifdef HAVE_WINDOW_SYSTEM
1729 if (FRAME_WINDOW_P (f))
1731 int height = FONT_HEIGHT (FRAME_FONT (f));
1733 /* This function is called so early when Emacs starts that the face
1734 cache and mode line face are not yet initialized. */
1735 if (FRAME_FACE_CACHE (f))
1737 struct face *face = FACE_FROM_ID (f, face_id);
1738 if (face)
1740 if (face->font)
1741 height = FONT_HEIGHT (face->font);
1742 if (face->box_line_width > 0)
1743 height += 2 * face->box_line_width;
1747 return height;
1749 #endif
1751 return 1;
1754 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1755 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1756 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1757 not force the value into range. */
1759 void
1760 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1761 int *x, int *y, NativeRectangle *bounds, int noclip)
1764 #ifdef HAVE_WINDOW_SYSTEM
1765 if (FRAME_WINDOW_P (f))
1767 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1768 even for negative values. */
1769 if (pix_x < 0)
1770 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1771 if (pix_y < 0)
1772 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1774 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1775 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1777 if (bounds)
1778 STORE_NATIVE_RECT (*bounds,
1779 FRAME_COL_TO_PIXEL_X (f, pix_x),
1780 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1781 FRAME_COLUMN_WIDTH (f) - 1,
1782 FRAME_LINE_HEIGHT (f) - 1);
1784 if (!noclip)
1786 if (pix_x < 0)
1787 pix_x = 0;
1788 else if (pix_x > FRAME_TOTAL_COLS (f))
1789 pix_x = FRAME_TOTAL_COLS (f);
1791 if (pix_y < 0)
1792 pix_y = 0;
1793 else if (pix_y > FRAME_LINES (f))
1794 pix_y = FRAME_LINES (f);
1797 #endif
1799 *x = pix_x;
1800 *y = pix_y;
1804 /* Find the glyph under window-relative coordinates X/Y in window W.
1805 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1806 strings. Return in *HPOS and *VPOS the row and column number of
1807 the glyph found. Return in *AREA the glyph area containing X.
1808 Value is a pointer to the glyph found or null if X/Y is not on
1809 text, or we can't tell because W's current matrix is not up to
1810 date. */
1812 static
1813 struct glyph *
1814 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1815 int *dx, int *dy, int *area)
1817 struct glyph *glyph, *end;
1818 struct glyph_row *row = NULL;
1819 int x0, i;
1821 /* Find row containing Y. Give up if some row is not enabled. */
1822 for (i = 0; i < w->current_matrix->nrows; ++i)
1824 row = MATRIX_ROW (w->current_matrix, i);
1825 if (!row->enabled_p)
1826 return NULL;
1827 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1828 break;
1831 *vpos = i;
1832 *hpos = 0;
1834 /* Give up if Y is not in the window. */
1835 if (i == w->current_matrix->nrows)
1836 return NULL;
1838 /* Get the glyph area containing X. */
1839 if (w->pseudo_window_p)
1841 *area = TEXT_AREA;
1842 x0 = 0;
1844 else
1846 if (x < window_box_left_offset (w, TEXT_AREA))
1848 *area = LEFT_MARGIN_AREA;
1849 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1851 else if (x < window_box_right_offset (w, TEXT_AREA))
1853 *area = TEXT_AREA;
1854 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1856 else
1858 *area = RIGHT_MARGIN_AREA;
1859 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1863 /* Find glyph containing X. */
1864 glyph = row->glyphs[*area];
1865 end = glyph + row->used[*area];
1866 x -= x0;
1867 while (glyph < end && x >= glyph->pixel_width)
1869 x -= glyph->pixel_width;
1870 ++glyph;
1873 if (glyph == end)
1874 return NULL;
1876 if (dx)
1878 *dx = x;
1879 *dy = y - (row->y + row->ascent - glyph->ascent);
1882 *hpos = glyph - row->glyphs[*area];
1883 return glyph;
1886 /* Convert frame-relative x/y to coordinates relative to window W.
1887 Takes pseudo-windows into account. */
1889 static void
1890 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1892 if (w->pseudo_window_p)
1894 /* A pseudo-window is always full-width, and starts at the
1895 left edge of the frame, plus a frame border. */
1896 struct frame *f = XFRAME (w->frame);
1897 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1898 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1900 else
1902 *x -= WINDOW_LEFT_EDGE_X (w);
1903 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1907 #ifdef HAVE_WINDOW_SYSTEM
1909 /* EXPORT:
1910 Return in RECTS[] at most N clipping rectangles for glyph string S.
1911 Return the number of stored rectangles. */
1914 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1916 XRectangle r;
1918 if (n <= 0)
1919 return 0;
1921 if (s->row->full_width_p)
1923 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1924 r.x = WINDOW_LEFT_EDGE_X (s->w);
1925 r.width = WINDOW_TOTAL_WIDTH (s->w);
1927 /* Unless displaying a mode or menu bar line, which are always
1928 fully visible, clip to the visible part of the row. */
1929 if (s->w->pseudo_window_p)
1930 r.height = s->row->visible_height;
1931 else
1932 r.height = s->height;
1934 else
1936 /* This is a text line that may be partially visible. */
1937 r.x = window_box_left (s->w, s->area);
1938 r.width = window_box_width (s->w, s->area);
1939 r.height = s->row->visible_height;
1942 if (s->clip_head)
1943 if (r.x < s->clip_head->x)
1945 if (r.width >= s->clip_head->x - r.x)
1946 r.width -= s->clip_head->x - r.x;
1947 else
1948 r.width = 0;
1949 r.x = s->clip_head->x;
1951 if (s->clip_tail)
1952 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1954 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1955 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1956 else
1957 r.width = 0;
1960 /* If S draws overlapping rows, it's sufficient to use the top and
1961 bottom of the window for clipping because this glyph string
1962 intentionally draws over other lines. */
1963 if (s->for_overlaps)
1965 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1966 r.height = window_text_bottom_y (s->w) - r.y;
1968 /* Alas, the above simple strategy does not work for the
1969 environments with anti-aliased text: if the same text is
1970 drawn onto the same place multiple times, it gets thicker.
1971 If the overlap we are processing is for the erased cursor, we
1972 take the intersection with the rectangle of the cursor. */
1973 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1975 XRectangle rc, r_save = r;
1977 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1978 rc.y = s->w->phys_cursor.y;
1979 rc.width = s->w->phys_cursor_width;
1980 rc.height = s->w->phys_cursor_height;
1982 x_intersect_rectangles (&r_save, &rc, &r);
1985 else
1987 /* Don't use S->y for clipping because it doesn't take partially
1988 visible lines into account. For example, it can be negative for
1989 partially visible lines at the top of a window. */
1990 if (!s->row->full_width_p
1991 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 else
1994 r.y = max (0, s->row->y);
1997 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1999 /* If drawing the cursor, don't let glyph draw outside its
2000 advertised boundaries. Cleartype does this under some circumstances. */
2001 if (s->hl == DRAW_CURSOR)
2003 struct glyph *glyph = s->first_glyph;
2004 int height, max_y;
2006 if (s->x > r.x)
2008 r.width -= s->x - r.x;
2009 r.x = s->x;
2011 r.width = min (r.width, glyph->pixel_width);
2013 /* If r.y is below window bottom, ensure that we still see a cursor. */
2014 height = min (glyph->ascent + glyph->descent,
2015 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2016 max_y = window_text_bottom_y (s->w) - height;
2017 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2018 if (s->ybase - glyph->ascent > max_y)
2020 r.y = max_y;
2021 r.height = height;
2023 else
2025 /* Don't draw cursor glyph taller than our actual glyph. */
2026 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2027 if (height < r.height)
2029 max_y = r.y + r.height;
2030 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2031 r.height = min (max_y - r.y, height);
2036 if (s->row->clip)
2038 XRectangle r_save = r;
2040 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2041 r.width = 0;
2044 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2045 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2047 #ifdef CONVERT_FROM_XRECT
2048 CONVERT_FROM_XRECT (r, *rects);
2049 #else
2050 *rects = r;
2051 #endif
2052 return 1;
2054 else
2056 /* If we are processing overlapping and allowed to return
2057 multiple clipping rectangles, we exclude the row of the glyph
2058 string from the clipping rectangle. This is to avoid drawing
2059 the same text on the environment with anti-aliasing. */
2060 #ifdef CONVERT_FROM_XRECT
2061 XRectangle rs[2];
2062 #else
2063 XRectangle *rs = rects;
2064 #endif
2065 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2067 if (s->for_overlaps & OVERLAPS_PRED)
2069 rs[i] = r;
2070 if (r.y + r.height > row_y)
2072 if (r.y < row_y)
2073 rs[i].height = row_y - r.y;
2074 else
2075 rs[i].height = 0;
2077 i++;
2079 if (s->for_overlaps & OVERLAPS_SUCC)
2081 rs[i] = r;
2082 if (r.y < row_y + s->row->visible_height)
2084 if (r.y + r.height > row_y + s->row->visible_height)
2086 rs[i].y = row_y + s->row->visible_height;
2087 rs[i].height = r.y + r.height - rs[i].y;
2089 else
2090 rs[i].height = 0;
2092 i++;
2095 n = i;
2096 #ifdef CONVERT_FROM_XRECT
2097 for (i = 0; i < n; i++)
2098 CONVERT_FROM_XRECT (rs[i], rects[i]);
2099 #endif
2100 return n;
2104 /* EXPORT:
2105 Return in *NR the clipping rectangle for glyph string S. */
2107 void
2108 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2110 get_glyph_string_clip_rects (s, nr, 1);
2114 /* EXPORT:
2115 Return the position and height of the phys cursor in window W.
2116 Set w->phys_cursor_width to width of phys cursor.
2119 void
2120 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2121 struct glyph *glyph, int *xp, int *yp, int *heightp)
2123 struct frame *f = XFRAME (WINDOW_FRAME (w));
2124 int x, y, wd, h, h0, y0;
2126 /* Compute the width of the rectangle to draw. If on a stretch
2127 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2128 rectangle as wide as the glyph, but use a canonical character
2129 width instead. */
2130 wd = glyph->pixel_width - 1;
2131 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2132 wd++; /* Why? */
2133 #endif
2135 x = w->phys_cursor.x;
2136 if (x < 0)
2138 wd += x;
2139 x = 0;
2142 if (glyph->type == STRETCH_GLYPH
2143 && !x_stretch_cursor_p)
2144 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2145 w->phys_cursor_width = wd;
2147 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2149 /* If y is below window bottom, ensure that we still see a cursor. */
2150 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2152 h = max (h0, glyph->ascent + glyph->descent);
2153 h0 = min (h0, glyph->ascent + glyph->descent);
2155 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2156 if (y < y0)
2158 h = max (h - (y0 - y) + 1, h0);
2159 y = y0 - 1;
2161 else
2163 y0 = window_text_bottom_y (w) - h0;
2164 if (y > y0)
2166 h += y - y0;
2167 y = y0;
2171 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2172 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2173 *heightp = h;
2177 * Remember which glyph the mouse is over.
2180 void
2181 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2183 Lisp_Object window;
2184 struct window *w;
2185 struct glyph_row *r, *gr, *end_row;
2186 enum window_part part;
2187 enum glyph_row_area area;
2188 int x, y, width, height;
2190 /* Try to determine frame pixel position and size of the glyph under
2191 frame pixel coordinates X/Y on frame F. */
2193 if (!f->glyphs_initialized_p
2194 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2195 NILP (window)))
2197 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2198 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2199 goto virtual_glyph;
2202 w = XWINDOW (window);
2203 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2204 height = WINDOW_FRAME_LINE_HEIGHT (w);
2206 x = window_relative_x_coord (w, part, gx);
2207 y = gy - WINDOW_TOP_EDGE_Y (w);
2209 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2210 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2212 if (w->pseudo_window_p)
2214 area = TEXT_AREA;
2215 part = ON_MODE_LINE; /* Don't adjust margin. */
2216 goto text_glyph;
2219 switch (part)
2221 case ON_LEFT_MARGIN:
2222 area = LEFT_MARGIN_AREA;
2223 goto text_glyph;
2225 case ON_RIGHT_MARGIN:
2226 area = RIGHT_MARGIN_AREA;
2227 goto text_glyph;
2229 case ON_HEADER_LINE:
2230 case ON_MODE_LINE:
2231 gr = (part == ON_HEADER_LINE
2232 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2233 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2234 gy = gr->y;
2235 area = TEXT_AREA;
2236 goto text_glyph_row_found;
2238 case ON_TEXT:
2239 area = TEXT_AREA;
2241 text_glyph:
2242 gr = 0; gy = 0;
2243 for (; r <= end_row && r->enabled_p; ++r)
2244 if (r->y + r->height > y)
2246 gr = r; gy = r->y;
2247 break;
2250 text_glyph_row_found:
2251 if (gr && gy <= y)
2253 struct glyph *g = gr->glyphs[area];
2254 struct glyph *end = g + gr->used[area];
2256 height = gr->height;
2257 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2258 if (gx + g->pixel_width > x)
2259 break;
2261 if (g < end)
2263 if (g->type == IMAGE_GLYPH)
2265 /* Don't remember when mouse is over image, as
2266 image may have hot-spots. */
2267 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2268 return;
2270 width = g->pixel_width;
2272 else
2274 /* Use nominal char spacing at end of line. */
2275 x -= gx;
2276 gx += (x / width) * width;
2279 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2280 gx += window_box_left_offset (w, area);
2282 else
2284 /* Use nominal line height at end of window. */
2285 gx = (x / width) * width;
2286 y -= gy;
2287 gy += (y / height) * height;
2289 break;
2291 case ON_LEFT_FRINGE:
2292 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2293 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2294 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2295 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2296 goto row_glyph;
2298 case ON_RIGHT_FRINGE:
2299 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2300 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2301 : window_box_right_offset (w, TEXT_AREA));
2302 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2303 goto row_glyph;
2305 case ON_SCROLL_BAR:
2306 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2308 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2309 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2310 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2311 : 0)));
2312 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2314 row_glyph:
2315 gr = 0, gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2319 gr = r; gy = r->y;
2320 break;
2323 if (gr && gy <= y)
2324 height = gr->height;
2325 else
2327 /* Use nominal line height at end of window. */
2328 y -= gy;
2329 gy += (y / height) * height;
2331 break;
2333 default:
2335 virtual_glyph:
2336 /* If there is no glyph under the mouse, then we divide the screen
2337 into a grid of the smallest glyph in the frame, and use that
2338 as our "glyph". */
2340 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2341 round down even for negative values. */
2342 if (gx < 0)
2343 gx -= width - 1;
2344 if (gy < 0)
2345 gy -= height - 1;
2347 gx = (gx / width) * width;
2348 gy = (gy / height) * height;
2350 goto store_rect;
2353 gx += WINDOW_LEFT_EDGE_X (w);
2354 gy += WINDOW_TOP_EDGE_Y (w);
2356 store_rect:
2357 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2359 /* Visible feedback for debugging. */
2360 #if 0
2361 #if HAVE_X_WINDOWS
2362 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2363 f->output_data.x->normal_gc,
2364 gx, gy, width, height);
2365 #endif
2366 #endif
2370 #endif /* HAVE_WINDOW_SYSTEM */
2373 /***********************************************************************
2374 Lisp form evaluation
2375 ***********************************************************************/
2377 /* Error handler for safe_eval and safe_call. */
2379 static Lisp_Object
2380 safe_eval_handler (Lisp_Object arg)
2382 add_to_log ("Error during redisplay: %S", arg, Qnil);
2383 return Qnil;
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2394 Lisp_Object
2395 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2397 Lisp_Object val;
2399 if (inhibit_eval_during_redisplay)
2400 val = Qnil;
2401 else
2403 ptrdiff_t count = SPECPDL_INDEX ();
2404 struct gcpro gcpro1;
2406 GCPRO1 (args[0]);
2407 gcpro1.nvars = nargs;
2408 specbind (Qinhibit_redisplay, Qt);
2409 /* Use Qt to ensure debugger does not run,
2410 so there is no possibility of wanting to redisplay. */
2411 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2412 safe_eval_handler);
2413 UNGCPRO;
2414 val = unbind_to (count, val);
2417 return val;
2421 /* Call function FN with one argument ARG.
2422 Return the result, or nil if something went wrong. */
2424 Lisp_Object
2425 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2427 Lisp_Object args[2];
2428 args[0] = fn;
2429 args[1] = arg;
2430 return safe_call (2, args);
2433 static Lisp_Object Qeval;
2435 Lisp_Object
2436 safe_eval (Lisp_Object sexpr)
2438 return safe_call1 (Qeval, sexpr);
2441 /* Call function FN with one argument ARG.
2442 Return the result, or nil if something went wrong. */
2444 Lisp_Object
2445 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2447 Lisp_Object args[3];
2448 args[0] = fn;
2449 args[1] = arg1;
2450 args[2] = arg2;
2451 return safe_call (3, args);
2456 /***********************************************************************
2457 Debugging
2458 ***********************************************************************/
2460 #if 0
2462 /* Define CHECK_IT to perform sanity checks on iterators.
2463 This is for debugging. It is too slow to do unconditionally. */
2465 static void
2466 check_it (struct it *it)
2468 if (it->method == GET_FROM_STRING)
2470 xassert (STRINGP (it->string));
2471 xassert (IT_STRING_CHARPOS (*it) >= 0);
2473 else
2475 xassert (IT_STRING_CHARPOS (*it) < 0);
2476 if (it->method == GET_FROM_BUFFER)
2478 /* Check that character and byte positions agree. */
2479 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2483 if (it->dpvec)
2484 xassert (it->current.dpvec_index >= 0);
2485 else
2486 xassert (it->current.dpvec_index < 0);
2489 #define CHECK_IT(IT) check_it ((IT))
2491 #else /* not 0 */
2493 #define CHECK_IT(IT) (void) 0
2495 #endif /* not 0 */
2498 #if GLYPH_DEBUG && XASSERTS
2500 /* Check that the window end of window W is what we expect it
2501 to be---the last row in the current matrix displaying text. */
2503 static void
2504 check_window_end (struct window *w)
2506 if (!MINI_WINDOW_P (w)
2507 && !NILP (w->window_end_valid))
2509 struct glyph_row *row;
2510 xassert ((row = MATRIX_ROW (w->current_matrix,
2511 XFASTINT (w->window_end_vpos)),
2512 !row->enabled_p
2513 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2514 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2518 #define CHECK_WINDOW_END(W) check_window_end ((W))
2520 #else
2522 #define CHECK_WINDOW_END(W) (void) 0
2524 #endif
2528 /***********************************************************************
2529 Iterator initialization
2530 ***********************************************************************/
2532 /* Initialize IT for displaying current_buffer in window W, starting
2533 at character position CHARPOS. CHARPOS < 0 means that no buffer
2534 position is specified which is useful when the iterator is assigned
2535 a position later. BYTEPOS is the byte position corresponding to
2536 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2538 If ROW is not null, calls to produce_glyphs with IT as parameter
2539 will produce glyphs in that row.
2541 BASE_FACE_ID is the id of a base face to use. It must be one of
2542 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2543 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2544 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2546 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2547 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2548 will be initialized to use the corresponding mode line glyph row of
2549 the desired matrix of W. */
2551 void
2552 init_iterator (struct it *it, struct window *w,
2553 ptrdiff_t charpos, ptrdiff_t bytepos,
2554 struct glyph_row *row, enum face_id base_face_id)
2556 int highlight_region_p;
2557 enum face_id remapped_base_face_id = base_face_id;
2559 /* Some precondition checks. */
2560 xassert (w != NULL && it != NULL);
2561 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2562 && charpos <= ZV));
2564 /* If face attributes have been changed since the last redisplay,
2565 free realized faces now because they depend on face definitions
2566 that might have changed. Don't free faces while there might be
2567 desired matrices pending which reference these faces. */
2568 if (face_change_count && !inhibit_free_realized_faces)
2570 face_change_count = 0;
2571 free_all_realized_faces (Qnil);
2574 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2575 if (! NILP (Vface_remapping_alist))
2576 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2578 /* Use one of the mode line rows of W's desired matrix if
2579 appropriate. */
2580 if (row == NULL)
2582 if (base_face_id == MODE_LINE_FACE_ID
2583 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2584 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2585 else if (base_face_id == HEADER_LINE_FACE_ID)
2586 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2589 /* Clear IT. */
2590 memset (it, 0, sizeof *it);
2591 it->current.overlay_string_index = -1;
2592 it->current.dpvec_index = -1;
2593 it->base_face_id = remapped_base_face_id;
2594 it->string = Qnil;
2595 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2596 it->paragraph_embedding = L2R;
2597 it->bidi_it.string.lstring = Qnil;
2598 it->bidi_it.string.s = NULL;
2599 it->bidi_it.string.bufpos = 0;
2601 /* The window in which we iterate over current_buffer: */
2602 XSETWINDOW (it->window, w);
2603 it->w = w;
2604 it->f = XFRAME (w->frame);
2606 it->cmp_it.id = -1;
2608 /* Extra space between lines (on window systems only). */
2609 if (base_face_id == DEFAULT_FACE_ID
2610 && FRAME_WINDOW_P (it->f))
2612 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2613 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2614 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2615 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2616 * FRAME_LINE_HEIGHT (it->f));
2617 else if (it->f->extra_line_spacing > 0)
2618 it->extra_line_spacing = it->f->extra_line_spacing;
2619 it->max_extra_line_spacing = 0;
2622 /* If realized faces have been removed, e.g. because of face
2623 attribute changes of named faces, recompute them. When running
2624 in batch mode, the face cache of the initial frame is null. If
2625 we happen to get called, make a dummy face cache. */
2626 if (FRAME_FACE_CACHE (it->f) == NULL)
2627 init_frame_faces (it->f);
2628 if (FRAME_FACE_CACHE (it->f)->used == 0)
2629 recompute_basic_faces (it->f);
2631 /* Current value of the `slice', `space-width', and 'height' properties. */
2632 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2633 it->space_width = Qnil;
2634 it->font_height = Qnil;
2635 it->override_ascent = -1;
2637 /* Are control characters displayed as `^C'? */
2638 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2640 /* -1 means everything between a CR and the following line end
2641 is invisible. >0 means lines indented more than this value are
2642 invisible. */
2643 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2644 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2645 selective_display)),
2646 PTRDIFF_MAX)
2647 : (!NILP (BVAR (current_buffer, selective_display))
2648 ? -1 : 0));
2649 it->selective_display_ellipsis_p
2650 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2652 /* Display table to use. */
2653 it->dp = window_display_table (w);
2655 /* Are multibyte characters enabled in current_buffer? */
2656 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2658 /* Non-zero if we should highlight the region. */
2659 highlight_region_p
2660 = (!NILP (Vtransient_mark_mode)
2661 && !NILP (BVAR (current_buffer, mark_active))
2662 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2664 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2665 start and end of a visible region in window IT->w. Set both to
2666 -1 to indicate no region. */
2667 if (highlight_region_p
2668 /* Maybe highlight only in selected window. */
2669 && (/* Either show region everywhere. */
2670 highlight_nonselected_windows
2671 /* Or show region in the selected window. */
2672 || w == XWINDOW (selected_window)
2673 /* Or show the region if we are in the mini-buffer and W is
2674 the window the mini-buffer refers to. */
2675 || (MINI_WINDOW_P (XWINDOW (selected_window))
2676 && WINDOWP (minibuf_selected_window)
2677 && w == XWINDOW (minibuf_selected_window))))
2679 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2680 it->region_beg_charpos = min (PT, markpos);
2681 it->region_end_charpos = max (PT, markpos);
2683 else
2684 it->region_beg_charpos = it->region_end_charpos = -1;
2686 /* Get the position at which the redisplay_end_trigger hook should
2687 be run, if it is to be run at all. */
2688 if (MARKERP (w->redisplay_end_trigger)
2689 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2690 it->redisplay_end_trigger_charpos
2691 = marker_position (w->redisplay_end_trigger);
2692 else if (INTEGERP (w->redisplay_end_trigger))
2693 it->redisplay_end_trigger_charpos =
2694 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2696 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2698 /* Are lines in the display truncated? */
2699 if (base_face_id != DEFAULT_FACE_ID
2700 || XINT (it->w->hscroll)
2701 || (! WINDOW_FULL_WIDTH_P (it->w)
2702 && ((!NILP (Vtruncate_partial_width_windows)
2703 && !INTEGERP (Vtruncate_partial_width_windows))
2704 || (INTEGERP (Vtruncate_partial_width_windows)
2705 && (WINDOW_TOTAL_COLS (it->w)
2706 < XINT (Vtruncate_partial_width_windows))))))
2707 it->line_wrap = TRUNCATE;
2708 else if (NILP (BVAR (current_buffer, truncate_lines)))
2709 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2710 ? WINDOW_WRAP : WORD_WRAP;
2711 else
2712 it->line_wrap = TRUNCATE;
2714 /* Get dimensions of truncation and continuation glyphs. These are
2715 displayed as fringe bitmaps under X, so we don't need them for such
2716 frames. */
2717 if (!FRAME_WINDOW_P (it->f))
2719 if (it->line_wrap == TRUNCATE)
2721 /* We will need the truncation glyph. */
2722 xassert (it->glyph_row == NULL);
2723 produce_special_glyphs (it, IT_TRUNCATION);
2724 it->truncation_pixel_width = it->pixel_width;
2726 else
2728 /* We will need the continuation glyph. */
2729 xassert (it->glyph_row == NULL);
2730 produce_special_glyphs (it, IT_CONTINUATION);
2731 it->continuation_pixel_width = it->pixel_width;
2734 /* Reset these values to zero because the produce_special_glyphs
2735 above has changed them. */
2736 it->pixel_width = it->ascent = it->descent = 0;
2737 it->phys_ascent = it->phys_descent = 0;
2740 /* Set this after getting the dimensions of truncation and
2741 continuation glyphs, so that we don't produce glyphs when calling
2742 produce_special_glyphs, above. */
2743 it->glyph_row = row;
2744 it->area = TEXT_AREA;
2746 /* Forget any previous info about this row being reversed. */
2747 if (it->glyph_row)
2748 it->glyph_row->reversed_p = 0;
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2760 else
2762 it->first_visible_x
2763 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2788 it->last_visible_y = window_text_bottom_y (w);
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2794 struct face *face;
2796 it->face_id = remapped_base_face_id;
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2809 it->end_charpos = ZV;
2810 IT_CHARPOS (*it) = charpos;
2812 /* We will rely on `reseat' to set this up properly, via
2813 handle_face_prop. */
2814 it->face_id = it->base_face_id;
2816 /* Compute byte position if not specified. */
2817 if (bytepos < charpos)
2818 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2819 else
2820 IT_BYTEPOS (*it) = bytepos;
2822 it->start = it->current;
2823 /* Do we need to reorder bidirectional text? Not if this is a
2824 unibyte buffer: by definition, none of the single-byte
2825 characters are strong R2L, so no reordering is needed. And
2826 bidi.c doesn't support unibyte buffers anyway. Also, don't
2827 reorder while we are loading loadup.el, since the tables of
2828 character properties needed for reordering are not yet
2829 available. */
2830 it->bidi_p =
2831 NILP (Vpurify_flag)
2832 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2833 && it->multibyte_p;
2835 /* If we are to reorder bidirectional text, init the bidi
2836 iterator. */
2837 if (it->bidi_p)
2839 /* Note the paragraph direction that this buffer wants to
2840 use. */
2841 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2842 Qleft_to_right))
2843 it->paragraph_embedding = L2R;
2844 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2845 Qright_to_left))
2846 it->paragraph_embedding = R2L;
2847 else
2848 it->paragraph_embedding = NEUTRAL_DIR;
2849 bidi_unshelve_cache (NULL, 0);
2850 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2851 &it->bidi_it);
2854 /* Compute faces etc. */
2855 reseat (it, it->current.pos, 1);
2858 CHECK_IT (it);
2862 /* Initialize IT for the display of window W with window start POS. */
2864 void
2865 start_display (struct it *it, struct window *w, struct text_pos pos)
2867 struct glyph_row *row;
2868 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2870 row = w->desired_matrix->rows + first_vpos;
2871 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2872 it->first_vpos = first_vpos;
2874 /* Don't reseat to previous visible line start if current start
2875 position is in a string or image. */
2876 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2878 int start_at_line_beg_p;
2879 int first_y = it->current_y;
2881 /* If window start is not at a line start, skip forward to POS to
2882 get the correct continuation lines width. */
2883 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2884 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2885 if (!start_at_line_beg_p)
2887 int new_x;
2889 reseat_at_previous_visible_line_start (it);
2890 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2892 new_x = it->current_x + it->pixel_width;
2894 /* If lines are continued, this line may end in the middle
2895 of a multi-glyph character (e.g. a control character
2896 displayed as \003, or in the middle of an overlay
2897 string). In this case move_it_to above will not have
2898 taken us to the start of the continuation line but to the
2899 end of the continued line. */
2900 if (it->current_x > 0
2901 && it->line_wrap != TRUNCATE /* Lines are continued. */
2902 && (/* And glyph doesn't fit on the line. */
2903 new_x > it->last_visible_x
2904 /* Or it fits exactly and we're on a window
2905 system frame. */
2906 || (new_x == it->last_visible_x
2907 && FRAME_WINDOW_P (it->f))))
2909 if ((it->current.dpvec_index >= 0
2910 || it->current.overlay_string_index >= 0)
2911 /* If we are on a newline from a display vector or
2912 overlay string, then we are already at the end of
2913 a screen line; no need to go to the next line in
2914 that case, as this line is not really continued.
2915 (If we do go to the next line, C-e will not DTRT.) */
2916 && it->c != '\n')
2918 set_iterator_to_next (it, 1);
2919 move_it_in_display_line_to (it, -1, -1, 0);
2922 it->continuation_lines_width += it->current_x;
2924 /* If the character at POS is displayed via a display
2925 vector, move_it_to above stops at the final glyph of
2926 IT->dpvec. To make the caller redisplay that character
2927 again (a.k.a. start at POS), we need to reset the
2928 dpvec_index to the beginning of IT->dpvec. */
2929 else if (it->current.dpvec_index >= 0)
2930 it->current.dpvec_index = 0;
2932 /* We're starting a new display line, not affected by the
2933 height of the continued line, so clear the appropriate
2934 fields in the iterator structure. */
2935 it->max_ascent = it->max_descent = 0;
2936 it->max_phys_ascent = it->max_phys_descent = 0;
2938 it->current_y = first_y;
2939 it->vpos = 0;
2940 it->current_x = it->hpos = 0;
2946 /* Return 1 if POS is a position in ellipses displayed for invisible
2947 text. W is the window we display, for text property lookup. */
2949 static int
2950 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2952 Lisp_Object prop, window;
2953 int ellipses_p = 0;
2954 ptrdiff_t charpos = CHARPOS (pos->pos);
2956 /* If POS specifies a position in a display vector, this might
2957 be for an ellipsis displayed for invisible text. We won't
2958 get the iterator set up for delivering that ellipsis unless
2959 we make sure that it gets aware of the invisible text. */
2960 if (pos->dpvec_index >= 0
2961 && pos->overlay_string_index < 0
2962 && CHARPOS (pos->string_pos) < 0
2963 && charpos > BEGV
2964 && (XSETWINDOW (window, w),
2965 prop = Fget_char_property (make_number (charpos),
2966 Qinvisible, window),
2967 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2969 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2970 window);
2971 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2974 return ellipses_p;
2978 /* Initialize IT for stepping through current_buffer in window W,
2979 starting at position POS that includes overlay string and display
2980 vector/ control character translation position information. Value
2981 is zero if there are overlay strings with newlines at POS. */
2983 static int
2984 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2986 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2987 int i, overlay_strings_with_newlines = 0;
2989 /* If POS specifies a position in a display vector, this might
2990 be for an ellipsis displayed for invisible text. We won't
2991 get the iterator set up for delivering that ellipsis unless
2992 we make sure that it gets aware of the invisible text. */
2993 if (in_ellipses_for_invisible_text_p (pos, w))
2995 --charpos;
2996 bytepos = 0;
2999 /* Keep in mind: the call to reseat in init_iterator skips invisible
3000 text, so we might end up at a position different from POS. This
3001 is only a problem when POS is a row start after a newline and an
3002 overlay starts there with an after-string, and the overlay has an
3003 invisible property. Since we don't skip invisible text in
3004 display_line and elsewhere immediately after consuming the
3005 newline before the row start, such a POS will not be in a string,
3006 but the call to init_iterator below will move us to the
3007 after-string. */
3008 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3010 /* This only scans the current chunk -- it should scan all chunks.
3011 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3012 to 16 in 22.1 to make this a lesser problem. */
3013 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3015 const char *s = SSDATA (it->overlay_strings[i]);
3016 const char *e = s + SBYTES (it->overlay_strings[i]);
3018 while (s < e && *s != '\n')
3019 ++s;
3021 if (s < e)
3023 overlay_strings_with_newlines = 1;
3024 break;
3028 /* If position is within an overlay string, set up IT to the right
3029 overlay string. */
3030 if (pos->overlay_string_index >= 0)
3032 int relative_index;
3034 /* If the first overlay string happens to have a `display'
3035 property for an image, the iterator will be set up for that
3036 image, and we have to undo that setup first before we can
3037 correct the overlay string index. */
3038 if (it->method == GET_FROM_IMAGE)
3039 pop_it (it);
3041 /* We already have the first chunk of overlay strings in
3042 IT->overlay_strings. Load more until the one for
3043 pos->overlay_string_index is in IT->overlay_strings. */
3044 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3046 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3047 it->current.overlay_string_index = 0;
3048 while (n--)
3050 load_overlay_strings (it, 0);
3051 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3055 it->current.overlay_string_index = pos->overlay_string_index;
3056 relative_index = (it->current.overlay_string_index
3057 % OVERLAY_STRING_CHUNK_SIZE);
3058 it->string = it->overlay_strings[relative_index];
3059 xassert (STRINGP (it->string));
3060 it->current.string_pos = pos->string_pos;
3061 it->method = GET_FROM_STRING;
3064 if (CHARPOS (pos->string_pos) >= 0)
3066 /* Recorded position is not in an overlay string, but in another
3067 string. This can only be a string from a `display' property.
3068 IT should already be filled with that string. */
3069 it->current.string_pos = pos->string_pos;
3070 xassert (STRINGP (it->string));
3073 /* Restore position in display vector translations, control
3074 character translations or ellipses. */
3075 if (pos->dpvec_index >= 0)
3077 if (it->dpvec == NULL)
3078 get_next_display_element (it);
3079 xassert (it->dpvec && it->current.dpvec_index == 0);
3080 it->current.dpvec_index = pos->dpvec_index;
3083 CHECK_IT (it);
3084 return !overlay_strings_with_newlines;
3088 /* Initialize IT for stepping through current_buffer in window W
3089 starting at ROW->start. */
3091 static void
3092 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3094 init_from_display_pos (it, w, &row->start);
3095 it->start = row->start;
3096 it->continuation_lines_width = row->continuation_lines_width;
3097 CHECK_IT (it);
3101 /* Initialize IT for stepping through current_buffer in window W
3102 starting in the line following ROW, i.e. starting at ROW->end.
3103 Value is zero if there are overlay strings with newlines at ROW's
3104 end position. */
3106 static int
3107 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3109 int success = 0;
3111 if (init_from_display_pos (it, w, &row->end))
3113 if (row->continued_p)
3114 it->continuation_lines_width
3115 = row->continuation_lines_width + row->pixel_width;
3116 CHECK_IT (it);
3117 success = 1;
3120 return success;
3126 /***********************************************************************
3127 Text properties
3128 ***********************************************************************/
3130 /* Called when IT reaches IT->stop_charpos. Handle text property and
3131 overlay changes. Set IT->stop_charpos to the next position where
3132 to stop. */
3134 static void
3135 handle_stop (struct it *it)
3137 enum prop_handled handled;
3138 int handle_overlay_change_p;
3139 struct props *p;
3141 it->dpvec = NULL;
3142 it->current.dpvec_index = -1;
3143 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3144 it->ignore_overlay_strings_at_pos_p = 0;
3145 it->ellipsis_p = 0;
3147 /* Use face of preceding text for ellipsis (if invisible) */
3148 if (it->selective_display_ellipsis_p)
3149 it->saved_face_id = it->face_id;
3153 handled = HANDLED_NORMALLY;
3155 /* Call text property handlers. */
3156 for (p = it_props; p->handler; ++p)
3158 handled = p->handler (it);
3160 if (handled == HANDLED_RECOMPUTE_PROPS)
3161 break;
3162 else if (handled == HANDLED_RETURN)
3164 /* We still want to show before and after strings from
3165 overlays even if the actual buffer text is replaced. */
3166 if (!handle_overlay_change_p
3167 || it->sp > 1
3168 /* Don't call get_overlay_strings_1 if we already
3169 have overlay strings loaded, because doing so
3170 will load them again and push the iterator state
3171 onto the stack one more time, which is not
3172 expected by the rest of the code that processes
3173 overlay strings. */
3174 || (it->current.overlay_string_index < 0
3175 ? !get_overlay_strings_1 (it, 0, 0)
3176 : 0))
3178 if (it->ellipsis_p)
3179 setup_for_ellipsis (it, 0);
3180 /* When handling a display spec, we might load an
3181 empty string. In that case, discard it here. We
3182 used to discard it in handle_single_display_spec,
3183 but that causes get_overlay_strings_1, above, to
3184 ignore overlay strings that we must check. */
3185 if (STRINGP (it->string) && !SCHARS (it->string))
3186 pop_it (it);
3187 return;
3189 else if (STRINGP (it->string) && !SCHARS (it->string))
3190 pop_it (it);
3191 else
3193 it->ignore_overlay_strings_at_pos_p = 1;
3194 it->string_from_display_prop_p = 0;
3195 it->from_disp_prop_p = 0;
3196 handle_overlay_change_p = 0;
3198 handled = HANDLED_RECOMPUTE_PROPS;
3199 break;
3201 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3202 handle_overlay_change_p = 0;
3205 if (handled != HANDLED_RECOMPUTE_PROPS)
3207 /* Don't check for overlay strings below when set to deliver
3208 characters from a display vector. */
3209 if (it->method == GET_FROM_DISPLAY_VECTOR)
3210 handle_overlay_change_p = 0;
3212 /* Handle overlay changes.
3213 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3214 if it finds overlays. */
3215 if (handle_overlay_change_p)
3216 handled = handle_overlay_change (it);
3219 if (it->ellipsis_p)
3221 setup_for_ellipsis (it, 0);
3222 break;
3225 while (handled == HANDLED_RECOMPUTE_PROPS);
3227 /* Determine where to stop next. */
3228 if (handled == HANDLED_NORMALLY)
3229 compute_stop_pos (it);
3233 /* Compute IT->stop_charpos from text property and overlay change
3234 information for IT's current position. */
3236 static void
3237 compute_stop_pos (struct it *it)
3239 register INTERVAL iv, next_iv;
3240 Lisp_Object object, limit, position;
3241 ptrdiff_t charpos, bytepos;
3243 if (STRINGP (it->string))
3245 /* Strings are usually short, so don't limit the search for
3246 properties. */
3247 it->stop_charpos = it->end_charpos;
3248 object = it->string;
3249 limit = Qnil;
3250 charpos = IT_STRING_CHARPOS (*it);
3251 bytepos = IT_STRING_BYTEPOS (*it);
3253 else
3255 ptrdiff_t pos;
3257 /* If end_charpos is out of range for some reason, such as a
3258 misbehaving display function, rationalize it (Bug#5984). */
3259 if (it->end_charpos > ZV)
3260 it->end_charpos = ZV;
3261 it->stop_charpos = it->end_charpos;
3263 /* If next overlay change is in front of the current stop pos
3264 (which is IT->end_charpos), stop there. Note: value of
3265 next_overlay_change is point-max if no overlay change
3266 follows. */
3267 charpos = IT_CHARPOS (*it);
3268 bytepos = IT_BYTEPOS (*it);
3269 pos = next_overlay_change (charpos);
3270 if (pos < it->stop_charpos)
3271 it->stop_charpos = pos;
3273 /* If showing the region, we have to stop at the region
3274 start or end because the face might change there. */
3275 if (it->region_beg_charpos > 0)
3277 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3278 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3279 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3280 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3283 /* Set up variables for computing the stop position from text
3284 property changes. */
3285 XSETBUFFER (object, current_buffer);
3286 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3289 /* Get the interval containing IT's position. Value is a null
3290 interval if there isn't such an interval. */
3291 position = make_number (charpos);
3292 iv = validate_interval_range (object, &position, &position, 0);
3293 if (!NULL_INTERVAL_P (iv))
3295 Lisp_Object values_here[LAST_PROP_IDX];
3296 struct props *p;
3298 /* Get properties here. */
3299 for (p = it_props; p->handler; ++p)
3300 values_here[p->idx] = textget (iv->plist, *p->name);
3302 /* Look for an interval following iv that has different
3303 properties. */
3304 for (next_iv = next_interval (iv);
3305 (!NULL_INTERVAL_P (next_iv)
3306 && (NILP (limit)
3307 || XFASTINT (limit) > next_iv->position));
3308 next_iv = next_interval (next_iv))
3310 for (p = it_props; p->handler; ++p)
3312 Lisp_Object new_value;
3314 new_value = textget (next_iv->plist, *p->name);
3315 if (!EQ (values_here[p->idx], new_value))
3316 break;
3319 if (p->handler)
3320 break;
3323 if (!NULL_INTERVAL_P (next_iv))
3325 if (INTEGERP (limit)
3326 && next_iv->position >= XFASTINT (limit))
3327 /* No text property change up to limit. */
3328 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3329 else
3330 /* Text properties change in next_iv. */
3331 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3335 if (it->cmp_it.id < 0)
3337 ptrdiff_t stoppos = it->end_charpos;
3339 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3340 stoppos = -1;
3341 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3342 stoppos, it->string);
3345 xassert (STRINGP (it->string)
3346 || (it->stop_charpos >= BEGV
3347 && it->stop_charpos >= IT_CHARPOS (*it)));
3351 /* Return the position of the next overlay change after POS in
3352 current_buffer. Value is point-max if no overlay change
3353 follows. This is like `next-overlay-change' but doesn't use
3354 xmalloc. */
3356 static ptrdiff_t
3357 next_overlay_change (ptrdiff_t pos)
3359 ptrdiff_t i, noverlays;
3360 ptrdiff_t endpos;
3361 Lisp_Object *overlays;
3363 /* Get all overlays at the given position. */
3364 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3366 /* If any of these overlays ends before endpos,
3367 use its ending point instead. */
3368 for (i = 0; i < noverlays; ++i)
3370 Lisp_Object oend;
3371 ptrdiff_t oendpos;
3373 oend = OVERLAY_END (overlays[i]);
3374 oendpos = OVERLAY_POSITION (oend);
3375 endpos = min (endpos, oendpos);
3378 return endpos;
3381 /* How many characters forward to search for a display property or
3382 display string. Searching too far forward makes the bidi display
3383 sluggish, especially in small windows. */
3384 #define MAX_DISP_SCAN 250
3386 /* Return the character position of a display string at or after
3387 position specified by POSITION. If no display string exists at or
3388 after POSITION, return ZV. A display string is either an overlay
3389 with `display' property whose value is a string, or a `display'
3390 text property whose value is a string. STRING is data about the
3391 string to iterate; if STRING->lstring is nil, we are iterating a
3392 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3393 on a GUI frame. DISP_PROP is set to zero if we searched
3394 MAX_DISP_SCAN characters forward without finding any display
3395 strings, non-zero otherwise. It is set to 2 if the display string
3396 uses any kind of `(space ...)' spec that will produce a stretch of
3397 white space in the text area. */
3398 ptrdiff_t
3399 compute_display_string_pos (struct text_pos *position,
3400 struct bidi_string_data *string,
3401 int frame_window_p, int *disp_prop)
3403 /* OBJECT = nil means current buffer. */
3404 Lisp_Object object =
3405 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3406 Lisp_Object pos, spec, limpos;
3407 int string_p = (string && (STRINGP (string->lstring) || string->s));
3408 ptrdiff_t eob = string_p ? string->schars : ZV;
3409 ptrdiff_t begb = string_p ? 0 : BEGV;
3410 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3411 ptrdiff_t lim =
3412 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3413 struct text_pos tpos;
3414 int rv = 0;
3416 *disp_prop = 1;
3418 if (charpos >= eob
3419 /* We don't support display properties whose values are strings
3420 that have display string properties. */
3421 || string->from_disp_str
3422 /* C strings cannot have display properties. */
3423 || (string->s && !STRINGP (object)))
3425 *disp_prop = 0;
3426 return eob;
3429 /* If the character at CHARPOS is where the display string begins,
3430 return CHARPOS. */
3431 pos = make_number (charpos);
3432 if (STRINGP (object))
3433 bufpos = string->bufpos;
3434 else
3435 bufpos = charpos;
3436 tpos = *position;
3437 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3438 && (charpos <= begb
3439 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3440 object),
3441 spec))
3442 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3443 frame_window_p)))
3445 if (rv == 2)
3446 *disp_prop = 2;
3447 return charpos;
3450 /* Look forward for the first character with a `display' property
3451 that will replace the underlying text when displayed. */
3452 limpos = make_number (lim);
3453 do {
3454 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3455 CHARPOS (tpos) = XFASTINT (pos);
3456 if (CHARPOS (tpos) >= lim)
3458 *disp_prop = 0;
3459 break;
3461 if (STRINGP (object))
3462 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3463 else
3464 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3465 spec = Fget_char_property (pos, Qdisplay, object);
3466 if (!STRINGP (object))
3467 bufpos = CHARPOS (tpos);
3468 } while (NILP (spec)
3469 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3470 bufpos, frame_window_p)));
3471 if (rv == 2)
3472 *disp_prop = 2;
3474 return CHARPOS (tpos);
3477 /* Return the character position of the end of the display string that
3478 started at CHARPOS. If there's no display string at CHARPOS,
3479 return -1. A display string is either an overlay with `display'
3480 property whose value is a string or a `display' text property whose
3481 value is a string. */
3482 ptrdiff_t
3483 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3485 /* OBJECT = nil means current buffer. */
3486 Lisp_Object object =
3487 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3488 Lisp_Object pos = make_number (charpos);
3489 ptrdiff_t eob =
3490 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3492 if (charpos >= eob || (string->s && !STRINGP (object)))
3493 return eob;
3495 /* It could happen that the display property or overlay was removed
3496 since we found it in compute_display_string_pos above. One way
3497 this can happen is if JIT font-lock was called (through
3498 handle_fontified_prop), and jit-lock-functions remove text
3499 properties or overlays from the portion of buffer that includes
3500 CHARPOS. Muse mode is known to do that, for example. In this
3501 case, we return -1 to the caller, to signal that no display
3502 string is actually present at CHARPOS. See bidi_fetch_char for
3503 how this is handled.
3505 An alternative would be to never look for display properties past
3506 it->stop_charpos. But neither compute_display_string_pos nor
3507 bidi_fetch_char that calls it know or care where the next
3508 stop_charpos is. */
3509 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3510 return -1;
3512 /* Look forward for the first character where the `display' property
3513 changes. */
3514 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3516 return XFASTINT (pos);
3521 /***********************************************************************
3522 Fontification
3523 ***********************************************************************/
3525 /* Handle changes in the `fontified' property of the current buffer by
3526 calling hook functions from Qfontification_functions to fontify
3527 regions of text. */
3529 static enum prop_handled
3530 handle_fontified_prop (struct it *it)
3532 Lisp_Object prop, pos;
3533 enum prop_handled handled = HANDLED_NORMALLY;
3535 if (!NILP (Vmemory_full))
3536 return handled;
3538 /* Get the value of the `fontified' property at IT's current buffer
3539 position. (The `fontified' property doesn't have a special
3540 meaning in strings.) If the value is nil, call functions from
3541 Qfontification_functions. */
3542 if (!STRINGP (it->string)
3543 && it->s == NULL
3544 && !NILP (Vfontification_functions)
3545 && !NILP (Vrun_hooks)
3546 && (pos = make_number (IT_CHARPOS (*it)),
3547 prop = Fget_char_property (pos, Qfontified, Qnil),
3548 /* Ignore the special cased nil value always present at EOB since
3549 no amount of fontifying will be able to change it. */
3550 NILP (prop) && IT_CHARPOS (*it) < Z))
3552 ptrdiff_t count = SPECPDL_INDEX ();
3553 Lisp_Object val;
3554 struct buffer *obuf = current_buffer;
3555 int begv = BEGV, zv = ZV;
3556 int old_clip_changed = current_buffer->clip_changed;
3558 val = Vfontification_functions;
3559 specbind (Qfontification_functions, Qnil);
3561 xassert (it->end_charpos == ZV);
3563 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3564 safe_call1 (val, pos);
3565 else
3567 Lisp_Object fns, fn;
3568 struct gcpro gcpro1, gcpro2;
3570 fns = Qnil;
3571 GCPRO2 (val, fns);
3573 for (; CONSP (val); val = XCDR (val))
3575 fn = XCAR (val);
3577 if (EQ (fn, Qt))
3579 /* A value of t indicates this hook has a local
3580 binding; it means to run the global binding too.
3581 In a global value, t should not occur. If it
3582 does, we must ignore it to avoid an endless
3583 loop. */
3584 for (fns = Fdefault_value (Qfontification_functions);
3585 CONSP (fns);
3586 fns = XCDR (fns))
3588 fn = XCAR (fns);
3589 if (!EQ (fn, Qt))
3590 safe_call1 (fn, pos);
3593 else
3594 safe_call1 (fn, pos);
3597 UNGCPRO;
3600 unbind_to (count, Qnil);
3602 /* Fontification functions routinely call `save-restriction'.
3603 Normally, this tags clip_changed, which can confuse redisplay
3604 (see discussion in Bug#6671). Since we don't perform any
3605 special handling of fontification changes in the case where
3606 `save-restriction' isn't called, there's no point doing so in
3607 this case either. So, if the buffer's restrictions are
3608 actually left unchanged, reset clip_changed. */
3609 if (obuf == current_buffer)
3611 if (begv == BEGV && zv == ZV)
3612 current_buffer->clip_changed = old_clip_changed;
3614 /* There isn't much we can reasonably do to protect against
3615 misbehaving fontification, but here's a fig leaf. */
3616 else if (!NILP (BVAR (obuf, name)))
3617 set_buffer_internal_1 (obuf);
3619 /* The fontification code may have added/removed text.
3620 It could do even a lot worse, but let's at least protect against
3621 the most obvious case where only the text past `pos' gets changed',
3622 as is/was done in grep.el where some escapes sequences are turned
3623 into face properties (bug#7876). */
3624 it->end_charpos = ZV;
3626 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3627 something. This avoids an endless loop if they failed to
3628 fontify the text for which reason ever. */
3629 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3630 handled = HANDLED_RECOMPUTE_PROPS;
3633 return handled;
3638 /***********************************************************************
3639 Faces
3640 ***********************************************************************/
3642 /* Set up iterator IT from face properties at its current position.
3643 Called from handle_stop. */
3645 static enum prop_handled
3646 handle_face_prop (struct it *it)
3648 int new_face_id;
3649 ptrdiff_t next_stop;
3651 if (!STRINGP (it->string))
3653 new_face_id
3654 = face_at_buffer_position (it->w,
3655 IT_CHARPOS (*it),
3656 it->region_beg_charpos,
3657 it->region_end_charpos,
3658 &next_stop,
3659 (IT_CHARPOS (*it)
3660 + TEXT_PROP_DISTANCE_LIMIT),
3661 0, it->base_face_id);
3663 /* Is this a start of a run of characters with box face?
3664 Caveat: this can be called for a freshly initialized
3665 iterator; face_id is -1 in this case. We know that the new
3666 face will not change until limit, i.e. if the new face has a
3667 box, all characters up to limit will have one. But, as
3668 usual, we don't know whether limit is really the end. */
3669 if (new_face_id != it->face_id)
3671 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3673 /* If new face has a box but old face has not, this is
3674 the start of a run of characters with box, i.e. it has
3675 a shadow on the left side. The value of face_id of the
3676 iterator will be -1 if this is the initial call that gets
3677 the face. In this case, we have to look in front of IT's
3678 position and see whether there is a face != new_face_id. */
3679 it->start_of_box_run_p
3680 = (new_face->box != FACE_NO_BOX
3681 && (it->face_id >= 0
3682 || IT_CHARPOS (*it) == BEG
3683 || new_face_id != face_before_it_pos (it)));
3684 it->face_box_p = new_face->box != FACE_NO_BOX;
3687 else
3689 int base_face_id;
3690 ptrdiff_t bufpos;
3691 int i;
3692 Lisp_Object from_overlay
3693 = (it->current.overlay_string_index >= 0
3694 ? it->string_overlays[it->current.overlay_string_index
3695 % OVERLAY_STRING_CHUNK_SIZE]
3696 : Qnil);
3698 /* See if we got to this string directly or indirectly from
3699 an overlay property. That includes the before-string or
3700 after-string of an overlay, strings in display properties
3701 provided by an overlay, their text properties, etc.
3703 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3704 if (! NILP (from_overlay))
3705 for (i = it->sp - 1; i >= 0; i--)
3707 if (it->stack[i].current.overlay_string_index >= 0)
3708 from_overlay
3709 = it->string_overlays[it->stack[i].current.overlay_string_index
3710 % OVERLAY_STRING_CHUNK_SIZE];
3711 else if (! NILP (it->stack[i].from_overlay))
3712 from_overlay = it->stack[i].from_overlay;
3714 if (!NILP (from_overlay))
3715 break;
3718 if (! NILP (from_overlay))
3720 bufpos = IT_CHARPOS (*it);
3721 /* For a string from an overlay, the base face depends
3722 only on text properties and ignores overlays. */
3723 base_face_id
3724 = face_for_overlay_string (it->w,
3725 IT_CHARPOS (*it),
3726 it->region_beg_charpos,
3727 it->region_end_charpos,
3728 &next_stop,
3729 (IT_CHARPOS (*it)
3730 + TEXT_PROP_DISTANCE_LIMIT),
3732 from_overlay);
3734 else
3736 bufpos = 0;
3738 /* For strings from a `display' property, use the face at
3739 IT's current buffer position as the base face to merge
3740 with, so that overlay strings appear in the same face as
3741 surrounding text, unless they specify their own
3742 faces. */
3743 base_face_id = it->string_from_prefix_prop_p
3744 ? DEFAULT_FACE_ID
3745 : underlying_face_id (it);
3748 new_face_id = face_at_string_position (it->w,
3749 it->string,
3750 IT_STRING_CHARPOS (*it),
3751 bufpos,
3752 it->region_beg_charpos,
3753 it->region_end_charpos,
3754 &next_stop,
3755 base_face_id, 0);
3757 /* Is this a start of a run of characters with box? Caveat:
3758 this can be called for a freshly allocated iterator; face_id
3759 is -1 is this case. We know that the new face will not
3760 change until the next check pos, i.e. if the new face has a
3761 box, all characters up to that position will have a
3762 box. But, as usual, we don't know whether that position
3763 is really the end. */
3764 if (new_face_id != it->face_id)
3766 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3767 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3769 /* If new face has a box but old face hasn't, this is the
3770 start of a run of characters with box, i.e. it has a
3771 shadow on the left side. */
3772 it->start_of_box_run_p
3773 = new_face->box && (old_face == NULL || !old_face->box);
3774 it->face_box_p = new_face->box != FACE_NO_BOX;
3778 it->face_id = new_face_id;
3779 return HANDLED_NORMALLY;
3783 /* Return the ID of the face ``underlying'' IT's current position,
3784 which is in a string. If the iterator is associated with a
3785 buffer, return the face at IT's current buffer position.
3786 Otherwise, use the iterator's base_face_id. */
3788 static int
3789 underlying_face_id (struct it *it)
3791 int face_id = it->base_face_id, i;
3793 xassert (STRINGP (it->string));
3795 for (i = it->sp - 1; i >= 0; --i)
3796 if (NILP (it->stack[i].string))
3797 face_id = it->stack[i].face_id;
3799 return face_id;
3803 /* Compute the face one character before or after the current position
3804 of IT, in the visual order. BEFORE_P non-zero means get the face
3805 in front (to the left in L2R paragraphs, to the right in R2L
3806 paragraphs) of IT's screen position. Value is the ID of the face. */
3808 static int
3809 face_before_or_after_it_pos (struct it *it, int before_p)
3811 int face_id, limit;
3812 ptrdiff_t next_check_charpos;
3813 struct it it_copy;
3814 void *it_copy_data = NULL;
3816 xassert (it->s == NULL);
3818 if (STRINGP (it->string))
3820 ptrdiff_t bufpos, charpos;
3821 int base_face_id;
3823 /* No face change past the end of the string (for the case
3824 we are padding with spaces). No face change before the
3825 string start. */
3826 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3827 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3828 return it->face_id;
3830 if (!it->bidi_p)
3832 /* Set charpos to the position before or after IT's current
3833 position, in the logical order, which in the non-bidi
3834 case is the same as the visual order. */
3835 if (before_p)
3836 charpos = IT_STRING_CHARPOS (*it) - 1;
3837 else if (it->what == IT_COMPOSITION)
3838 /* For composition, we must check the character after the
3839 composition. */
3840 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3841 else
3842 charpos = IT_STRING_CHARPOS (*it) + 1;
3844 else
3846 if (before_p)
3848 /* With bidi iteration, the character before the current
3849 in the visual order cannot be found by simple
3850 iteration, because "reverse" reordering is not
3851 supported. Instead, we need to use the move_it_*
3852 family of functions. */
3853 /* Ignore face changes before the first visible
3854 character on this display line. */
3855 if (it->current_x <= it->first_visible_x)
3856 return it->face_id;
3857 SAVE_IT (it_copy, *it, it_copy_data);
3858 /* Implementation note: Since move_it_in_display_line
3859 works in the iterator geometry, and thinks the first
3860 character is always the leftmost, even in R2L lines,
3861 we don't need to distinguish between the R2L and L2R
3862 cases here. */
3863 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3864 it_copy.current_x - 1, MOVE_TO_X);
3865 charpos = IT_STRING_CHARPOS (it_copy);
3866 RESTORE_IT (it, it, it_copy_data);
3868 else
3870 /* Set charpos to the string position of the character
3871 that comes after IT's current position in the visual
3872 order. */
3873 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3875 it_copy = *it;
3876 while (n--)
3877 bidi_move_to_visually_next (&it_copy.bidi_it);
3879 charpos = it_copy.bidi_it.charpos;
3882 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3884 if (it->current.overlay_string_index >= 0)
3885 bufpos = IT_CHARPOS (*it);
3886 else
3887 bufpos = 0;
3889 base_face_id = underlying_face_id (it);
3891 /* Get the face for ASCII, or unibyte. */
3892 face_id = face_at_string_position (it->w,
3893 it->string,
3894 charpos,
3895 bufpos,
3896 it->region_beg_charpos,
3897 it->region_end_charpos,
3898 &next_check_charpos,
3899 base_face_id, 0);
3901 /* Correct the face for charsets different from ASCII. Do it
3902 for the multibyte case only. The face returned above is
3903 suitable for unibyte text if IT->string is unibyte. */
3904 if (STRING_MULTIBYTE (it->string))
3906 struct text_pos pos1 = string_pos (charpos, it->string);
3907 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3908 int c, len;
3909 struct face *face = FACE_FROM_ID (it->f, face_id);
3911 c = string_char_and_length (p, &len);
3912 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3915 else
3917 struct text_pos pos;
3919 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3920 || (IT_CHARPOS (*it) <= BEGV && before_p))
3921 return it->face_id;
3923 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3924 pos = it->current.pos;
3926 if (!it->bidi_p)
3928 if (before_p)
3929 DEC_TEXT_POS (pos, it->multibyte_p);
3930 else
3932 if (it->what == IT_COMPOSITION)
3934 /* For composition, we must check the position after
3935 the composition. */
3936 pos.charpos += it->cmp_it.nchars;
3937 pos.bytepos += it->len;
3939 else
3940 INC_TEXT_POS (pos, it->multibyte_p);
3943 else
3945 if (before_p)
3947 /* With bidi iteration, the character before the current
3948 in the visual order cannot be found by simple
3949 iteration, because "reverse" reordering is not
3950 supported. Instead, we need to use the move_it_*
3951 family of functions. */
3952 /* Ignore face changes before the first visible
3953 character on this display line. */
3954 if (it->current_x <= it->first_visible_x)
3955 return it->face_id;
3956 SAVE_IT (it_copy, *it, it_copy_data);
3957 /* Implementation note: Since move_it_in_display_line
3958 works in the iterator geometry, and thinks the first
3959 character is always the leftmost, even in R2L lines,
3960 we don't need to distinguish between the R2L and L2R
3961 cases here. */
3962 move_it_in_display_line (&it_copy, ZV,
3963 it_copy.current_x - 1, MOVE_TO_X);
3964 pos = it_copy.current.pos;
3965 RESTORE_IT (it, it, it_copy_data);
3967 else
3969 /* Set charpos to the buffer position of the character
3970 that comes after IT's current position in the visual
3971 order. */
3972 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3974 it_copy = *it;
3975 while (n--)
3976 bidi_move_to_visually_next (&it_copy.bidi_it);
3978 SET_TEXT_POS (pos,
3979 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3982 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3984 /* Determine face for CHARSET_ASCII, or unibyte. */
3985 face_id = face_at_buffer_position (it->w,
3986 CHARPOS (pos),
3987 it->region_beg_charpos,
3988 it->region_end_charpos,
3989 &next_check_charpos,
3990 limit, 0, -1);
3992 /* Correct the face for charsets different from ASCII. Do it
3993 for the multibyte case only. The face returned above is
3994 suitable for unibyte text if current_buffer is unibyte. */
3995 if (it->multibyte_p)
3997 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3998 struct face *face = FACE_FROM_ID (it->f, face_id);
3999 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4003 return face_id;
4008 /***********************************************************************
4009 Invisible text
4010 ***********************************************************************/
4012 /* Set up iterator IT from invisible properties at its current
4013 position. Called from handle_stop. */
4015 static enum prop_handled
4016 handle_invisible_prop (struct it *it)
4018 enum prop_handled handled = HANDLED_NORMALLY;
4020 if (STRINGP (it->string))
4022 Lisp_Object prop, end_charpos, limit, charpos;
4024 /* Get the value of the invisible text property at the
4025 current position. Value will be nil if there is no such
4026 property. */
4027 charpos = make_number (IT_STRING_CHARPOS (*it));
4028 prop = Fget_text_property (charpos, Qinvisible, it->string);
4030 if (!NILP (prop)
4031 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4033 ptrdiff_t endpos;
4035 handled = HANDLED_RECOMPUTE_PROPS;
4037 /* Get the position at which the next change of the
4038 invisible text property can be found in IT->string.
4039 Value will be nil if the property value is the same for
4040 all the rest of IT->string. */
4041 XSETINT (limit, SCHARS (it->string));
4042 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4043 it->string, limit);
4045 /* Text at current position is invisible. The next
4046 change in the property is at position end_charpos.
4047 Move IT's current position to that position. */
4048 if (INTEGERP (end_charpos)
4049 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4051 struct text_pos old;
4052 ptrdiff_t oldpos;
4054 old = it->current.string_pos;
4055 oldpos = CHARPOS (old);
4056 if (it->bidi_p)
4058 if (it->bidi_it.first_elt
4059 && it->bidi_it.charpos < SCHARS (it->string))
4060 bidi_paragraph_init (it->paragraph_embedding,
4061 &it->bidi_it, 1);
4062 /* Bidi-iterate out of the invisible text. */
4065 bidi_move_to_visually_next (&it->bidi_it);
4067 while (oldpos <= it->bidi_it.charpos
4068 && it->bidi_it.charpos < endpos);
4070 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4071 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4072 if (IT_CHARPOS (*it) >= endpos)
4073 it->prev_stop = endpos;
4075 else
4077 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4078 compute_string_pos (&it->current.string_pos, old, it->string);
4081 else
4083 /* The rest of the string is invisible. If this is an
4084 overlay string, proceed with the next overlay string
4085 or whatever comes and return a character from there. */
4086 if (it->current.overlay_string_index >= 0)
4088 next_overlay_string (it);
4089 /* Don't check for overlay strings when we just
4090 finished processing them. */
4091 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4093 else
4095 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4096 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4101 else
4103 int invis_p;
4104 ptrdiff_t newpos, next_stop, start_charpos, tem;
4105 Lisp_Object pos, prop, overlay;
4107 /* First of all, is there invisible text at this position? */
4108 tem = start_charpos = IT_CHARPOS (*it);
4109 pos = make_number (tem);
4110 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4111 &overlay);
4112 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4114 /* If we are on invisible text, skip over it. */
4115 if (invis_p && start_charpos < it->end_charpos)
4117 /* Record whether we have to display an ellipsis for the
4118 invisible text. */
4119 int display_ellipsis_p = invis_p == 2;
4121 handled = HANDLED_RECOMPUTE_PROPS;
4123 /* Loop skipping over invisible text. The loop is left at
4124 ZV or with IT on the first char being visible again. */
4127 /* Try to skip some invisible text. Return value is the
4128 position reached which can be equal to where we start
4129 if there is nothing invisible there. This skips both
4130 over invisible text properties and overlays with
4131 invisible property. */
4132 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4134 /* If we skipped nothing at all we weren't at invisible
4135 text in the first place. If everything to the end of
4136 the buffer was skipped, end the loop. */
4137 if (newpos == tem || newpos >= ZV)
4138 invis_p = 0;
4139 else
4141 /* We skipped some characters but not necessarily
4142 all there are. Check if we ended up on visible
4143 text. Fget_char_property returns the property of
4144 the char before the given position, i.e. if we
4145 get invis_p = 0, this means that the char at
4146 newpos is visible. */
4147 pos = make_number (newpos);
4148 prop = Fget_char_property (pos, Qinvisible, it->window);
4149 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4152 /* If we ended up on invisible text, proceed to
4153 skip starting with next_stop. */
4154 if (invis_p)
4155 tem = next_stop;
4157 /* If there are adjacent invisible texts, don't lose the
4158 second one's ellipsis. */
4159 if (invis_p == 2)
4160 display_ellipsis_p = 1;
4162 while (invis_p);
4164 /* The position newpos is now either ZV or on visible text. */
4165 if (it->bidi_p)
4167 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4168 int on_newline =
4169 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4170 int after_newline =
4171 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4173 /* If the invisible text ends on a newline or on a
4174 character after a newline, we can avoid the costly,
4175 character by character, bidi iteration to NEWPOS, and
4176 instead simply reseat the iterator there. That's
4177 because all bidi reordering information is tossed at
4178 the newline. This is a big win for modes that hide
4179 complete lines, like Outline, Org, etc. */
4180 if (on_newline || after_newline)
4182 struct text_pos tpos;
4183 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4185 SET_TEXT_POS (tpos, newpos, bpos);
4186 reseat_1 (it, tpos, 0);
4187 /* If we reseat on a newline/ZV, we need to prep the
4188 bidi iterator for advancing to the next character
4189 after the newline/EOB, keeping the current paragraph
4190 direction (so that PRODUCE_GLYPHS does TRT wrt
4191 prepending/appending glyphs to a glyph row). */
4192 if (on_newline)
4194 it->bidi_it.first_elt = 0;
4195 it->bidi_it.paragraph_dir = pdir;
4196 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4197 it->bidi_it.nchars = 1;
4198 it->bidi_it.ch_len = 1;
4201 else /* Must use the slow method. */
4203 /* With bidi iteration, the region of invisible text
4204 could start and/or end in the middle of a
4205 non-base embedding level. Therefore, we need to
4206 skip invisible text using the bidi iterator,
4207 starting at IT's current position, until we find
4208 ourselves outside of the invisible text.
4209 Skipping invisible text _after_ bidi iteration
4210 avoids affecting the visual order of the
4211 displayed text when invisible properties are
4212 added or removed. */
4213 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4215 /* If we were `reseat'ed to a new paragraph,
4216 determine the paragraph base direction. We
4217 need to do it now because
4218 next_element_from_buffer may not have a
4219 chance to do it, if we are going to skip any
4220 text at the beginning, which resets the
4221 FIRST_ELT flag. */
4222 bidi_paragraph_init (it->paragraph_embedding,
4223 &it->bidi_it, 1);
4227 bidi_move_to_visually_next (&it->bidi_it);
4229 while (it->stop_charpos <= it->bidi_it.charpos
4230 && it->bidi_it.charpos < newpos);
4231 IT_CHARPOS (*it) = it->bidi_it.charpos;
4232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4233 /* If we overstepped NEWPOS, record its position in
4234 the iterator, so that we skip invisible text if
4235 later the bidi iteration lands us in the
4236 invisible region again. */
4237 if (IT_CHARPOS (*it) >= newpos)
4238 it->prev_stop = newpos;
4241 else
4243 IT_CHARPOS (*it) = newpos;
4244 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4247 /* If there are before-strings at the start of invisible
4248 text, and the text is invisible because of a text
4249 property, arrange to show before-strings because 20.x did
4250 it that way. (If the text is invisible because of an
4251 overlay property instead of a text property, this is
4252 already handled in the overlay code.) */
4253 if (NILP (overlay)
4254 && get_overlay_strings (it, it->stop_charpos))
4256 handled = HANDLED_RECOMPUTE_PROPS;
4257 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4259 else if (display_ellipsis_p)
4261 /* Make sure that the glyphs of the ellipsis will get
4262 correct `charpos' values. If we would not update
4263 it->position here, the glyphs would belong to the
4264 last visible character _before_ the invisible
4265 text, which confuses `set_cursor_from_row'.
4267 We use the last invisible position instead of the
4268 first because this way the cursor is always drawn on
4269 the first "." of the ellipsis, whenever PT is inside
4270 the invisible text. Otherwise the cursor would be
4271 placed _after_ the ellipsis when the point is after the
4272 first invisible character. */
4273 if (!STRINGP (it->object))
4275 it->position.charpos = newpos - 1;
4276 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4278 it->ellipsis_p = 1;
4279 /* Let the ellipsis display before
4280 considering any properties of the following char.
4281 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4282 handled = HANDLED_RETURN;
4287 return handled;
4291 /* Make iterator IT return `...' next.
4292 Replaces LEN characters from buffer. */
4294 static void
4295 setup_for_ellipsis (struct it *it, int len)
4297 /* Use the display table definition for `...'. Invalid glyphs
4298 will be handled by the method returning elements from dpvec. */
4299 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4301 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4302 it->dpvec = v->contents;
4303 it->dpend = v->contents + v->header.size;
4305 else
4307 /* Default `...'. */
4308 it->dpvec = default_invis_vector;
4309 it->dpend = default_invis_vector + 3;
4312 it->dpvec_char_len = len;
4313 it->current.dpvec_index = 0;
4314 it->dpvec_face_id = -1;
4316 /* Remember the current face id in case glyphs specify faces.
4317 IT's face is restored in set_iterator_to_next.
4318 saved_face_id was set to preceding char's face in handle_stop. */
4319 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4320 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4322 it->method = GET_FROM_DISPLAY_VECTOR;
4323 it->ellipsis_p = 1;
4328 /***********************************************************************
4329 'display' property
4330 ***********************************************************************/
4332 /* Set up iterator IT from `display' property at its current position.
4333 Called from handle_stop.
4334 We return HANDLED_RETURN if some part of the display property
4335 overrides the display of the buffer text itself.
4336 Otherwise we return HANDLED_NORMALLY. */
4338 static enum prop_handled
4339 handle_display_prop (struct it *it)
4341 Lisp_Object propval, object, overlay;
4342 struct text_pos *position;
4343 ptrdiff_t bufpos;
4344 /* Nonzero if some property replaces the display of the text itself. */
4345 int display_replaced_p = 0;
4347 if (STRINGP (it->string))
4349 object = it->string;
4350 position = &it->current.string_pos;
4351 bufpos = CHARPOS (it->current.pos);
4353 else
4355 XSETWINDOW (object, it->w);
4356 position = &it->current.pos;
4357 bufpos = CHARPOS (*position);
4360 /* Reset those iterator values set from display property values. */
4361 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4362 it->space_width = Qnil;
4363 it->font_height = Qnil;
4364 it->voffset = 0;
4366 /* We don't support recursive `display' properties, i.e. string
4367 values that have a string `display' property, that have a string
4368 `display' property etc. */
4369 if (!it->string_from_display_prop_p)
4370 it->area = TEXT_AREA;
4372 propval = get_char_property_and_overlay (make_number (position->charpos),
4373 Qdisplay, object, &overlay);
4374 if (NILP (propval))
4375 return HANDLED_NORMALLY;
4376 /* Now OVERLAY is the overlay that gave us this property, or nil
4377 if it was a text property. */
4379 if (!STRINGP (it->string))
4380 object = it->w->buffer;
4382 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4383 position, bufpos,
4384 FRAME_WINDOW_P (it->f));
4386 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4389 /* Subroutine of handle_display_prop. Returns non-zero if the display
4390 specification in SPEC is a replacing specification, i.e. it would
4391 replace the text covered by `display' property with something else,
4392 such as an image or a display string. If SPEC includes any kind or
4393 `(space ...) specification, the value is 2; this is used by
4394 compute_display_string_pos, which see.
4396 See handle_single_display_spec for documentation of arguments.
4397 frame_window_p is non-zero if the window being redisplayed is on a
4398 GUI frame; this argument is used only if IT is NULL, see below.
4400 IT can be NULL, if this is called by the bidi reordering code
4401 through compute_display_string_pos, which see. In that case, this
4402 function only examines SPEC, but does not otherwise "handle" it, in
4403 the sense that it doesn't set up members of IT from the display
4404 spec. */
4405 static int
4406 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4407 Lisp_Object overlay, struct text_pos *position,
4408 ptrdiff_t bufpos, int frame_window_p)
4410 int replacing_p = 0;
4411 int rv;
4413 if (CONSP (spec)
4414 /* Simple specifications. */
4415 && !EQ (XCAR (spec), Qimage)
4416 && !EQ (XCAR (spec), Qspace)
4417 && !EQ (XCAR (spec), Qwhen)
4418 && !EQ (XCAR (spec), Qslice)
4419 && !EQ (XCAR (spec), Qspace_width)
4420 && !EQ (XCAR (spec), Qheight)
4421 && !EQ (XCAR (spec), Qraise)
4422 /* Marginal area specifications. */
4423 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4424 && !EQ (XCAR (spec), Qleft_fringe)
4425 && !EQ (XCAR (spec), Qright_fringe)
4426 && !NILP (XCAR (spec)))
4428 for (; CONSP (spec); spec = XCDR (spec))
4430 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4431 overlay, position, bufpos,
4432 replacing_p, frame_window_p)))
4434 replacing_p = rv;
4435 /* If some text in a string is replaced, `position' no
4436 longer points to the position of `object'. */
4437 if (!it || STRINGP (object))
4438 break;
4442 else if (VECTORP (spec))
4444 ptrdiff_t i;
4445 for (i = 0; i < ASIZE (spec); ++i)
4446 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4447 overlay, position, bufpos,
4448 replacing_p, frame_window_p)))
4450 replacing_p = rv;
4451 /* If some text in a string is replaced, `position' no
4452 longer points to the position of `object'. */
4453 if (!it || STRINGP (object))
4454 break;
4457 else
4459 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4460 position, bufpos, 0,
4461 frame_window_p)))
4462 replacing_p = rv;
4465 return replacing_p;
4468 /* Value is the position of the end of the `display' property starting
4469 at START_POS in OBJECT. */
4471 static struct text_pos
4472 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4474 Lisp_Object end;
4475 struct text_pos end_pos;
4477 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4478 Qdisplay, object, Qnil);
4479 CHARPOS (end_pos) = XFASTINT (end);
4480 if (STRINGP (object))
4481 compute_string_pos (&end_pos, start_pos, it->string);
4482 else
4483 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4485 return end_pos;
4489 /* Set up IT from a single `display' property specification SPEC. OBJECT
4490 is the object in which the `display' property was found. *POSITION
4491 is the position in OBJECT at which the `display' property was found.
4492 BUFPOS is the buffer position of OBJECT (different from POSITION if
4493 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4494 previously saw a display specification which already replaced text
4495 display with something else, for example an image; we ignore such
4496 properties after the first one has been processed.
4498 OVERLAY is the overlay this `display' property came from,
4499 or nil if it was a text property.
4501 If SPEC is a `space' or `image' specification, and in some other
4502 cases too, set *POSITION to the position where the `display'
4503 property ends.
4505 If IT is NULL, only examine the property specification in SPEC, but
4506 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4507 is intended to be displayed in a window on a GUI frame.
4509 Value is non-zero if something was found which replaces the display
4510 of buffer or string text. */
4512 static int
4513 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4514 Lisp_Object overlay, struct text_pos *position,
4515 ptrdiff_t bufpos, int display_replaced_p,
4516 int frame_window_p)
4518 Lisp_Object form;
4519 Lisp_Object location, value;
4520 struct text_pos start_pos = *position;
4521 int valid_p;
4523 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4524 If the result is non-nil, use VALUE instead of SPEC. */
4525 form = Qt;
4526 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4528 spec = XCDR (spec);
4529 if (!CONSP (spec))
4530 return 0;
4531 form = XCAR (spec);
4532 spec = XCDR (spec);
4535 if (!NILP (form) && !EQ (form, Qt))
4537 ptrdiff_t count = SPECPDL_INDEX ();
4538 struct gcpro gcpro1;
4540 /* Bind `object' to the object having the `display' property, a
4541 buffer or string. Bind `position' to the position in the
4542 object where the property was found, and `buffer-position'
4543 to the current position in the buffer. */
4545 if (NILP (object))
4546 XSETBUFFER (object, current_buffer);
4547 specbind (Qobject, object);
4548 specbind (Qposition, make_number (CHARPOS (*position)));
4549 specbind (Qbuffer_position, make_number (bufpos));
4550 GCPRO1 (form);
4551 form = safe_eval (form);
4552 UNGCPRO;
4553 unbind_to (count, Qnil);
4556 if (NILP (form))
4557 return 0;
4559 /* Handle `(height HEIGHT)' specifications. */
4560 if (CONSP (spec)
4561 && EQ (XCAR (spec), Qheight)
4562 && CONSP (XCDR (spec)))
4564 if (it)
4566 if (!FRAME_WINDOW_P (it->f))
4567 return 0;
4569 it->font_height = XCAR (XCDR (spec));
4570 if (!NILP (it->font_height))
4572 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4573 int new_height = -1;
4575 if (CONSP (it->font_height)
4576 && (EQ (XCAR (it->font_height), Qplus)
4577 || EQ (XCAR (it->font_height), Qminus))
4578 && CONSP (XCDR (it->font_height))
4579 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4581 /* `(+ N)' or `(- N)' where N is an integer. */
4582 int steps = XINT (XCAR (XCDR (it->font_height)));
4583 if (EQ (XCAR (it->font_height), Qplus))
4584 steps = - steps;
4585 it->face_id = smaller_face (it->f, it->face_id, steps);
4587 else if (FUNCTIONP (it->font_height))
4589 /* Call function with current height as argument.
4590 Value is the new height. */
4591 Lisp_Object height;
4592 height = safe_call1 (it->font_height,
4593 face->lface[LFACE_HEIGHT_INDEX]);
4594 if (NUMBERP (height))
4595 new_height = XFLOATINT (height);
4597 else if (NUMBERP (it->font_height))
4599 /* Value is a multiple of the canonical char height. */
4600 struct face *f;
4602 f = FACE_FROM_ID (it->f,
4603 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4604 new_height = (XFLOATINT (it->font_height)
4605 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4607 else
4609 /* Evaluate IT->font_height with `height' bound to the
4610 current specified height to get the new height. */
4611 ptrdiff_t count = SPECPDL_INDEX ();
4613 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4614 value = safe_eval (it->font_height);
4615 unbind_to (count, Qnil);
4617 if (NUMBERP (value))
4618 new_height = XFLOATINT (value);
4621 if (new_height > 0)
4622 it->face_id = face_with_height (it->f, it->face_id, new_height);
4626 return 0;
4629 /* Handle `(space-width WIDTH)'. */
4630 if (CONSP (spec)
4631 && EQ (XCAR (spec), Qspace_width)
4632 && CONSP (XCDR (spec)))
4634 if (it)
4636 if (!FRAME_WINDOW_P (it->f))
4637 return 0;
4639 value = XCAR (XCDR (spec));
4640 if (NUMBERP (value) && XFLOATINT (value) > 0)
4641 it->space_width = value;
4644 return 0;
4647 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4648 if (CONSP (spec)
4649 && EQ (XCAR (spec), Qslice))
4651 Lisp_Object tem;
4653 if (it)
4655 if (!FRAME_WINDOW_P (it->f))
4656 return 0;
4658 if (tem = XCDR (spec), CONSP (tem))
4660 it->slice.x = XCAR (tem);
4661 if (tem = XCDR (tem), CONSP (tem))
4663 it->slice.y = XCAR (tem);
4664 if (tem = XCDR (tem), CONSP (tem))
4666 it->slice.width = XCAR (tem);
4667 if (tem = XCDR (tem), CONSP (tem))
4668 it->slice.height = XCAR (tem);
4674 return 0;
4677 /* Handle `(raise FACTOR)'. */
4678 if (CONSP (spec)
4679 && EQ (XCAR (spec), Qraise)
4680 && CONSP (XCDR (spec)))
4682 if (it)
4684 if (!FRAME_WINDOW_P (it->f))
4685 return 0;
4687 #ifdef HAVE_WINDOW_SYSTEM
4688 value = XCAR (XCDR (spec));
4689 if (NUMBERP (value))
4691 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4692 it->voffset = - (XFLOATINT (value)
4693 * (FONT_HEIGHT (face->font)));
4695 #endif /* HAVE_WINDOW_SYSTEM */
4698 return 0;
4701 /* Don't handle the other kinds of display specifications
4702 inside a string that we got from a `display' property. */
4703 if (it && it->string_from_display_prop_p)
4704 return 0;
4706 /* Characters having this form of property are not displayed, so
4707 we have to find the end of the property. */
4708 if (it)
4710 start_pos = *position;
4711 *position = display_prop_end (it, object, start_pos);
4713 value = Qnil;
4715 /* Stop the scan at that end position--we assume that all
4716 text properties change there. */
4717 if (it)
4718 it->stop_charpos = position->charpos;
4720 /* Handle `(left-fringe BITMAP [FACE])'
4721 and `(right-fringe BITMAP [FACE])'. */
4722 if (CONSP (spec)
4723 && (EQ (XCAR (spec), Qleft_fringe)
4724 || EQ (XCAR (spec), Qright_fringe))
4725 && CONSP (XCDR (spec)))
4727 int fringe_bitmap;
4729 if (it)
4731 if (!FRAME_WINDOW_P (it->f))
4732 /* If we return here, POSITION has been advanced
4733 across the text with this property. */
4735 /* Synchronize the bidi iterator with POSITION. This is
4736 needed because we are not going to push the iterator
4737 on behalf of this display property, so there will be
4738 no pop_it call to do this synchronization for us. */
4739 if (it->bidi_p)
4741 it->position = *position;
4742 iterate_out_of_display_property (it);
4743 *position = it->position;
4745 return 1;
4748 else if (!frame_window_p)
4749 return 1;
4751 #ifdef HAVE_WINDOW_SYSTEM
4752 value = XCAR (XCDR (spec));
4753 if (!SYMBOLP (value)
4754 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4755 /* If we return here, POSITION has been advanced
4756 across the text with this property. */
4758 if (it && it->bidi_p)
4760 it->position = *position;
4761 iterate_out_of_display_property (it);
4762 *position = it->position;
4764 return 1;
4767 if (it)
4769 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4771 if (CONSP (XCDR (XCDR (spec))))
4773 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4774 int face_id2 = lookup_derived_face (it->f, face_name,
4775 FRINGE_FACE_ID, 0);
4776 if (face_id2 >= 0)
4777 face_id = face_id2;
4780 /* Save current settings of IT so that we can restore them
4781 when we are finished with the glyph property value. */
4782 push_it (it, position);
4784 it->area = TEXT_AREA;
4785 it->what = IT_IMAGE;
4786 it->image_id = -1; /* no image */
4787 it->position = start_pos;
4788 it->object = NILP (object) ? it->w->buffer : object;
4789 it->method = GET_FROM_IMAGE;
4790 it->from_overlay = Qnil;
4791 it->face_id = face_id;
4792 it->from_disp_prop_p = 1;
4794 /* Say that we haven't consumed the characters with
4795 `display' property yet. The call to pop_it in
4796 set_iterator_to_next will clean this up. */
4797 *position = start_pos;
4799 if (EQ (XCAR (spec), Qleft_fringe))
4801 it->left_user_fringe_bitmap = fringe_bitmap;
4802 it->left_user_fringe_face_id = face_id;
4804 else
4806 it->right_user_fringe_bitmap = fringe_bitmap;
4807 it->right_user_fringe_face_id = face_id;
4810 #endif /* HAVE_WINDOW_SYSTEM */
4811 return 1;
4814 /* Prepare to handle `((margin left-margin) ...)',
4815 `((margin right-margin) ...)' and `((margin nil) ...)'
4816 prefixes for display specifications. */
4817 location = Qunbound;
4818 if (CONSP (spec) && CONSP (XCAR (spec)))
4820 Lisp_Object tem;
4822 value = XCDR (spec);
4823 if (CONSP (value))
4824 value = XCAR (value);
4826 tem = XCAR (spec);
4827 if (EQ (XCAR (tem), Qmargin)
4828 && (tem = XCDR (tem),
4829 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4830 (NILP (tem)
4831 || EQ (tem, Qleft_margin)
4832 || EQ (tem, Qright_margin))))
4833 location = tem;
4836 if (EQ (location, Qunbound))
4838 location = Qnil;
4839 value = spec;
4842 /* After this point, VALUE is the property after any
4843 margin prefix has been stripped. It must be a string,
4844 an image specification, or `(space ...)'.
4846 LOCATION specifies where to display: `left-margin',
4847 `right-margin' or nil. */
4849 valid_p = (STRINGP (value)
4850 #ifdef HAVE_WINDOW_SYSTEM
4851 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4852 && valid_image_p (value))
4853 #endif /* not HAVE_WINDOW_SYSTEM */
4854 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4856 if (valid_p && !display_replaced_p)
4858 int retval = 1;
4860 if (!it)
4862 /* Callers need to know whether the display spec is any kind
4863 of `(space ...)' spec that is about to affect text-area
4864 display. */
4865 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4866 retval = 2;
4867 return retval;
4870 /* Save current settings of IT so that we can restore them
4871 when we are finished with the glyph property value. */
4872 push_it (it, position);
4873 it->from_overlay = overlay;
4874 it->from_disp_prop_p = 1;
4876 if (NILP (location))
4877 it->area = TEXT_AREA;
4878 else if (EQ (location, Qleft_margin))
4879 it->area = LEFT_MARGIN_AREA;
4880 else
4881 it->area = RIGHT_MARGIN_AREA;
4883 if (STRINGP (value))
4885 it->string = value;
4886 it->multibyte_p = STRING_MULTIBYTE (it->string);
4887 it->current.overlay_string_index = -1;
4888 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4889 it->end_charpos = it->string_nchars = SCHARS (it->string);
4890 it->method = GET_FROM_STRING;
4891 it->stop_charpos = 0;
4892 it->prev_stop = 0;
4893 it->base_level_stop = 0;
4894 it->string_from_display_prop_p = 1;
4895 /* Say that we haven't consumed the characters with
4896 `display' property yet. The call to pop_it in
4897 set_iterator_to_next will clean this up. */
4898 if (BUFFERP (object))
4899 *position = start_pos;
4901 /* Force paragraph direction to be that of the parent
4902 object. If the parent object's paragraph direction is
4903 not yet determined, default to L2R. */
4904 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4905 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4906 else
4907 it->paragraph_embedding = L2R;
4909 /* Set up the bidi iterator for this display string. */
4910 if (it->bidi_p)
4912 it->bidi_it.string.lstring = it->string;
4913 it->bidi_it.string.s = NULL;
4914 it->bidi_it.string.schars = it->end_charpos;
4915 it->bidi_it.string.bufpos = bufpos;
4916 it->bidi_it.string.from_disp_str = 1;
4917 it->bidi_it.string.unibyte = !it->multibyte_p;
4918 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4921 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4923 it->method = GET_FROM_STRETCH;
4924 it->object = value;
4925 *position = it->position = start_pos;
4926 retval = 1 + (it->area == TEXT_AREA);
4928 #ifdef HAVE_WINDOW_SYSTEM
4929 else
4931 it->what = IT_IMAGE;
4932 it->image_id = lookup_image (it->f, value);
4933 it->position = start_pos;
4934 it->object = NILP (object) ? it->w->buffer : object;
4935 it->method = GET_FROM_IMAGE;
4937 /* Say that we haven't consumed the characters with
4938 `display' property yet. The call to pop_it in
4939 set_iterator_to_next will clean this up. */
4940 *position = start_pos;
4942 #endif /* HAVE_WINDOW_SYSTEM */
4944 return retval;
4947 /* Invalid property or property not supported. Restore
4948 POSITION to what it was before. */
4949 *position = start_pos;
4950 return 0;
4953 /* Check if PROP is a display property value whose text should be
4954 treated as intangible. OVERLAY is the overlay from which PROP
4955 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4956 specify the buffer position covered by PROP. */
4959 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4960 ptrdiff_t charpos, ptrdiff_t bytepos)
4962 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4963 struct text_pos position;
4965 SET_TEXT_POS (position, charpos, bytepos);
4966 return handle_display_spec (NULL, prop, Qnil, overlay,
4967 &position, charpos, frame_window_p);
4971 /* Return 1 if PROP is a display sub-property value containing STRING.
4973 Implementation note: this and the following function are really
4974 special cases of handle_display_spec and
4975 handle_single_display_spec, and should ideally use the same code.
4976 Until they do, these two pairs must be consistent and must be
4977 modified in sync. */
4979 static int
4980 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4982 if (EQ (string, prop))
4983 return 1;
4985 /* Skip over `when FORM'. */
4986 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4988 prop = XCDR (prop);
4989 if (!CONSP (prop))
4990 return 0;
4991 /* Actually, the condition following `when' should be eval'ed,
4992 like handle_single_display_spec does, and we should return
4993 zero if it evaluates to nil. However, this function is
4994 called only when the buffer was already displayed and some
4995 glyph in the glyph matrix was found to come from a display
4996 string. Therefore, the condition was already evaluated, and
4997 the result was non-nil, otherwise the display string wouldn't
4998 have been displayed and we would have never been called for
4999 this property. Thus, we can skip the evaluation and assume
5000 its result is non-nil. */
5001 prop = XCDR (prop);
5004 if (CONSP (prop))
5005 /* Skip over `margin LOCATION'. */
5006 if (EQ (XCAR (prop), Qmargin))
5008 prop = XCDR (prop);
5009 if (!CONSP (prop))
5010 return 0;
5012 prop = XCDR (prop);
5013 if (!CONSP (prop))
5014 return 0;
5017 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5021 /* Return 1 if STRING appears in the `display' property PROP. */
5023 static int
5024 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5026 if (CONSP (prop)
5027 && !EQ (XCAR (prop), Qwhen)
5028 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5030 /* A list of sub-properties. */
5031 while (CONSP (prop))
5033 if (single_display_spec_string_p (XCAR (prop), string))
5034 return 1;
5035 prop = XCDR (prop);
5038 else if (VECTORP (prop))
5040 /* A vector of sub-properties. */
5041 ptrdiff_t i;
5042 for (i = 0; i < ASIZE (prop); ++i)
5043 if (single_display_spec_string_p (AREF (prop, i), string))
5044 return 1;
5046 else
5047 return single_display_spec_string_p (prop, string);
5049 return 0;
5052 /* Look for STRING in overlays and text properties in the current
5053 buffer, between character positions FROM and TO (excluding TO).
5054 BACK_P non-zero means look back (in this case, TO is supposed to be
5055 less than FROM).
5056 Value is the first character position where STRING was found, or
5057 zero if it wasn't found before hitting TO.
5059 This function may only use code that doesn't eval because it is
5060 called asynchronously from note_mouse_highlight. */
5062 static ptrdiff_t
5063 string_buffer_position_lim (Lisp_Object string,
5064 ptrdiff_t from, ptrdiff_t to, int back_p)
5066 Lisp_Object limit, prop, pos;
5067 int found = 0;
5069 pos = make_number (max (from, BEGV));
5071 if (!back_p) /* looking forward */
5073 limit = make_number (min (to, ZV));
5074 while (!found && !EQ (pos, limit))
5076 prop = Fget_char_property (pos, Qdisplay, Qnil);
5077 if (!NILP (prop) && display_prop_string_p (prop, string))
5078 found = 1;
5079 else
5080 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5081 limit);
5084 else /* looking back */
5086 limit = make_number (max (to, BEGV));
5087 while (!found && !EQ (pos, limit))
5089 prop = Fget_char_property (pos, Qdisplay, Qnil);
5090 if (!NILP (prop) && display_prop_string_p (prop, string))
5091 found = 1;
5092 else
5093 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5094 limit);
5098 return found ? XINT (pos) : 0;
5101 /* Determine which buffer position in current buffer STRING comes from.
5102 AROUND_CHARPOS is an approximate position where it could come from.
5103 Value is the buffer position or 0 if it couldn't be determined.
5105 This function is necessary because we don't record buffer positions
5106 in glyphs generated from strings (to keep struct glyph small).
5107 This function may only use code that doesn't eval because it is
5108 called asynchronously from note_mouse_highlight. */
5110 static ptrdiff_t
5111 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5113 const int MAX_DISTANCE = 1000;
5114 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5115 around_charpos + MAX_DISTANCE,
5118 if (!found)
5119 found = string_buffer_position_lim (string, around_charpos,
5120 around_charpos - MAX_DISTANCE, 1);
5121 return found;
5126 /***********************************************************************
5127 `composition' property
5128 ***********************************************************************/
5130 /* Set up iterator IT from `composition' property at its current
5131 position. Called from handle_stop. */
5133 static enum prop_handled
5134 handle_composition_prop (struct it *it)
5136 Lisp_Object prop, string;
5137 ptrdiff_t pos, pos_byte, start, end;
5139 if (STRINGP (it->string))
5141 unsigned char *s;
5143 pos = IT_STRING_CHARPOS (*it);
5144 pos_byte = IT_STRING_BYTEPOS (*it);
5145 string = it->string;
5146 s = SDATA (string) + pos_byte;
5147 it->c = STRING_CHAR (s);
5149 else
5151 pos = IT_CHARPOS (*it);
5152 pos_byte = IT_BYTEPOS (*it);
5153 string = Qnil;
5154 it->c = FETCH_CHAR (pos_byte);
5157 /* If there's a valid composition and point is not inside of the
5158 composition (in the case that the composition is from the current
5159 buffer), draw a glyph composed from the composition components. */
5160 if (find_composition (pos, -1, &start, &end, &prop, string)
5161 && COMPOSITION_VALID_P (start, end, prop)
5162 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5164 if (start < pos)
5165 /* As we can't handle this situation (perhaps font-lock added
5166 a new composition), we just return here hoping that next
5167 redisplay will detect this composition much earlier. */
5168 return HANDLED_NORMALLY;
5169 if (start != pos)
5171 if (STRINGP (it->string))
5172 pos_byte = string_char_to_byte (it->string, start);
5173 else
5174 pos_byte = CHAR_TO_BYTE (start);
5176 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5177 prop, string);
5179 if (it->cmp_it.id >= 0)
5181 it->cmp_it.ch = -1;
5182 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5183 it->cmp_it.nglyphs = -1;
5187 return HANDLED_NORMALLY;
5192 /***********************************************************************
5193 Overlay strings
5194 ***********************************************************************/
5196 /* The following structure is used to record overlay strings for
5197 later sorting in load_overlay_strings. */
5199 struct overlay_entry
5201 Lisp_Object overlay;
5202 Lisp_Object string;
5203 EMACS_INT priority;
5204 int after_string_p;
5208 /* Set up iterator IT from overlay strings at its current position.
5209 Called from handle_stop. */
5211 static enum prop_handled
5212 handle_overlay_change (struct it *it)
5214 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5215 return HANDLED_RECOMPUTE_PROPS;
5216 else
5217 return HANDLED_NORMALLY;
5221 /* Set up the next overlay string for delivery by IT, if there is an
5222 overlay string to deliver. Called by set_iterator_to_next when the
5223 end of the current overlay string is reached. If there are more
5224 overlay strings to display, IT->string and
5225 IT->current.overlay_string_index are set appropriately here.
5226 Otherwise IT->string is set to nil. */
5228 static void
5229 next_overlay_string (struct it *it)
5231 ++it->current.overlay_string_index;
5232 if (it->current.overlay_string_index == it->n_overlay_strings)
5234 /* No more overlay strings. Restore IT's settings to what
5235 they were before overlay strings were processed, and
5236 continue to deliver from current_buffer. */
5238 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5239 pop_it (it);
5240 xassert (it->sp > 0
5241 || (NILP (it->string)
5242 && it->method == GET_FROM_BUFFER
5243 && it->stop_charpos >= BEGV
5244 && it->stop_charpos <= it->end_charpos));
5245 it->current.overlay_string_index = -1;
5246 it->n_overlay_strings = 0;
5247 it->overlay_strings_charpos = -1;
5248 /* If there's an empty display string on the stack, pop the
5249 stack, to resync the bidi iterator with IT's position. Such
5250 empty strings are pushed onto the stack in
5251 get_overlay_strings_1. */
5252 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5253 pop_it (it);
5255 /* If we're at the end of the buffer, record that we have
5256 processed the overlay strings there already, so that
5257 next_element_from_buffer doesn't try it again. */
5258 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5259 it->overlay_strings_at_end_processed_p = 1;
5261 else
5263 /* There are more overlay strings to process. If
5264 IT->current.overlay_string_index has advanced to a position
5265 where we must load IT->overlay_strings with more strings, do
5266 it. We must load at the IT->overlay_strings_charpos where
5267 IT->n_overlay_strings was originally computed; when invisible
5268 text is present, this might not be IT_CHARPOS (Bug#7016). */
5269 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5271 if (it->current.overlay_string_index && i == 0)
5272 load_overlay_strings (it, it->overlay_strings_charpos);
5274 /* Initialize IT to deliver display elements from the overlay
5275 string. */
5276 it->string = it->overlay_strings[i];
5277 it->multibyte_p = STRING_MULTIBYTE (it->string);
5278 SET_TEXT_POS (it->current.string_pos, 0, 0);
5279 it->method = GET_FROM_STRING;
5280 it->stop_charpos = 0;
5281 if (it->cmp_it.stop_pos >= 0)
5282 it->cmp_it.stop_pos = 0;
5283 it->prev_stop = 0;
5284 it->base_level_stop = 0;
5286 /* Set up the bidi iterator for this overlay string. */
5287 if (it->bidi_p)
5289 it->bidi_it.string.lstring = it->string;
5290 it->bidi_it.string.s = NULL;
5291 it->bidi_it.string.schars = SCHARS (it->string);
5292 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5293 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5294 it->bidi_it.string.unibyte = !it->multibyte_p;
5295 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5299 CHECK_IT (it);
5303 /* Compare two overlay_entry structures E1 and E2. Used as a
5304 comparison function for qsort in load_overlay_strings. Overlay
5305 strings for the same position are sorted so that
5307 1. All after-strings come in front of before-strings, except
5308 when they come from the same overlay.
5310 2. Within after-strings, strings are sorted so that overlay strings
5311 from overlays with higher priorities come first.
5313 2. Within before-strings, strings are sorted so that overlay
5314 strings from overlays with higher priorities come last.
5316 Value is analogous to strcmp. */
5319 static int
5320 compare_overlay_entries (const void *e1, const void *e2)
5322 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5323 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5324 int result;
5326 if (entry1->after_string_p != entry2->after_string_p)
5328 /* Let after-strings appear in front of before-strings if
5329 they come from different overlays. */
5330 if (EQ (entry1->overlay, entry2->overlay))
5331 result = entry1->after_string_p ? 1 : -1;
5332 else
5333 result = entry1->after_string_p ? -1 : 1;
5335 else if (entry1->priority != entry2->priority)
5337 if (entry1->after_string_p)
5338 /* After-strings sorted in order of decreasing priority. */
5339 result = entry2->priority < entry1->priority ? -1 : 1;
5340 else
5341 /* Before-strings sorted in order of increasing priority. */
5342 result = entry1->priority < entry2->priority ? -1 : 1;
5344 else
5345 result = 0;
5347 return result;
5351 /* Load the vector IT->overlay_strings with overlay strings from IT's
5352 current buffer position, or from CHARPOS if that is > 0. Set
5353 IT->n_overlays to the total number of overlay strings found.
5355 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5356 a time. On entry into load_overlay_strings,
5357 IT->current.overlay_string_index gives the number of overlay
5358 strings that have already been loaded by previous calls to this
5359 function.
5361 IT->add_overlay_start contains an additional overlay start
5362 position to consider for taking overlay strings from, if non-zero.
5363 This position comes into play when the overlay has an `invisible'
5364 property, and both before and after-strings. When we've skipped to
5365 the end of the overlay, because of its `invisible' property, we
5366 nevertheless want its before-string to appear.
5367 IT->add_overlay_start will contain the overlay start position
5368 in this case.
5370 Overlay strings are sorted so that after-string strings come in
5371 front of before-string strings. Within before and after-strings,
5372 strings are sorted by overlay priority. See also function
5373 compare_overlay_entries. */
5375 static void
5376 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5378 Lisp_Object overlay, window, str, invisible;
5379 struct Lisp_Overlay *ov;
5380 ptrdiff_t start, end;
5381 ptrdiff_t size = 20;
5382 ptrdiff_t n = 0, i, j;
5383 int invis_p;
5384 struct overlay_entry *entries
5385 = (struct overlay_entry *) alloca (size * sizeof *entries);
5386 USE_SAFE_ALLOCA;
5388 if (charpos <= 0)
5389 charpos = IT_CHARPOS (*it);
5391 /* Append the overlay string STRING of overlay OVERLAY to vector
5392 `entries' which has size `size' and currently contains `n'
5393 elements. AFTER_P non-zero means STRING is an after-string of
5394 OVERLAY. */
5395 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5396 do \
5398 Lisp_Object priority; \
5400 if (n == size) \
5402 struct overlay_entry *old = entries; \
5403 SAFE_NALLOCA (entries, 2, size); \
5404 memcpy (entries, old, size * sizeof *entries); \
5405 size *= 2; \
5408 entries[n].string = (STRING); \
5409 entries[n].overlay = (OVERLAY); \
5410 priority = Foverlay_get ((OVERLAY), Qpriority); \
5411 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5412 entries[n].after_string_p = (AFTER_P); \
5413 ++n; \
5415 while (0)
5417 /* Process overlay before the overlay center. */
5418 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5420 XSETMISC (overlay, ov);
5421 xassert (OVERLAYP (overlay));
5422 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5423 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5425 if (end < charpos)
5426 break;
5428 /* Skip this overlay if it doesn't start or end at IT's current
5429 position. */
5430 if (end != charpos && start != charpos)
5431 continue;
5433 /* Skip this overlay if it doesn't apply to IT->w. */
5434 window = Foverlay_get (overlay, Qwindow);
5435 if (WINDOWP (window) && XWINDOW (window) != it->w)
5436 continue;
5438 /* If the text ``under'' the overlay is invisible, both before-
5439 and after-strings from this overlay are visible; start and
5440 end position are indistinguishable. */
5441 invisible = Foverlay_get (overlay, Qinvisible);
5442 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5444 /* If overlay has a non-empty before-string, record it. */
5445 if ((start == charpos || (end == charpos && invis_p))
5446 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5447 && SCHARS (str))
5448 RECORD_OVERLAY_STRING (overlay, str, 0);
5450 /* If overlay has a non-empty after-string, record it. */
5451 if ((end == charpos || (start == charpos && invis_p))
5452 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5453 && SCHARS (str))
5454 RECORD_OVERLAY_STRING (overlay, str, 1);
5457 /* Process overlays after the overlay center. */
5458 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5460 XSETMISC (overlay, ov);
5461 xassert (OVERLAYP (overlay));
5462 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5463 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5465 if (start > charpos)
5466 break;
5468 /* Skip this overlay if it doesn't start or end at IT's current
5469 position. */
5470 if (end != charpos && start != charpos)
5471 continue;
5473 /* Skip this overlay if it doesn't apply to IT->w. */
5474 window = Foverlay_get (overlay, Qwindow);
5475 if (WINDOWP (window) && XWINDOW (window) != it->w)
5476 continue;
5478 /* If the text ``under'' the overlay is invisible, it has a zero
5479 dimension, and both before- and after-strings apply. */
5480 invisible = Foverlay_get (overlay, Qinvisible);
5481 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5483 /* If overlay has a non-empty before-string, record it. */
5484 if ((start == charpos || (end == charpos && invis_p))
5485 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5486 && SCHARS (str))
5487 RECORD_OVERLAY_STRING (overlay, str, 0);
5489 /* If overlay has a non-empty after-string, record it. */
5490 if ((end == charpos || (start == charpos && invis_p))
5491 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5492 && SCHARS (str))
5493 RECORD_OVERLAY_STRING (overlay, str, 1);
5496 #undef RECORD_OVERLAY_STRING
5498 /* Sort entries. */
5499 if (n > 1)
5500 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5502 /* Record number of overlay strings, and where we computed it. */
5503 it->n_overlay_strings = n;
5504 it->overlay_strings_charpos = charpos;
5506 /* IT->current.overlay_string_index is the number of overlay strings
5507 that have already been consumed by IT. Copy some of the
5508 remaining overlay strings to IT->overlay_strings. */
5509 i = 0;
5510 j = it->current.overlay_string_index;
5511 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5513 it->overlay_strings[i] = entries[j].string;
5514 it->string_overlays[i++] = entries[j++].overlay;
5517 CHECK_IT (it);
5518 SAFE_FREE ();
5522 /* Get the first chunk of overlay strings at IT's current buffer
5523 position, or at CHARPOS if that is > 0. Value is non-zero if at
5524 least one overlay string was found. */
5526 static int
5527 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5529 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5530 process. This fills IT->overlay_strings with strings, and sets
5531 IT->n_overlay_strings to the total number of strings to process.
5532 IT->pos.overlay_string_index has to be set temporarily to zero
5533 because load_overlay_strings needs this; it must be set to -1
5534 when no overlay strings are found because a zero value would
5535 indicate a position in the first overlay string. */
5536 it->current.overlay_string_index = 0;
5537 load_overlay_strings (it, charpos);
5539 /* If we found overlay strings, set up IT to deliver display
5540 elements from the first one. Otherwise set up IT to deliver
5541 from current_buffer. */
5542 if (it->n_overlay_strings)
5544 /* Make sure we know settings in current_buffer, so that we can
5545 restore meaningful values when we're done with the overlay
5546 strings. */
5547 if (compute_stop_p)
5548 compute_stop_pos (it);
5549 xassert (it->face_id >= 0);
5551 /* Save IT's settings. They are restored after all overlay
5552 strings have been processed. */
5553 xassert (!compute_stop_p || it->sp == 0);
5555 /* When called from handle_stop, there might be an empty display
5556 string loaded. In that case, don't bother saving it. But
5557 don't use this optimization with the bidi iterator, since we
5558 need the corresponding pop_it call to resync the bidi
5559 iterator's position with IT's position, after we are done
5560 with the overlay strings. (The corresponding call to pop_it
5561 in case of an empty display string is in
5562 next_overlay_string.) */
5563 if (!(!it->bidi_p
5564 && STRINGP (it->string) && !SCHARS (it->string)))
5565 push_it (it, NULL);
5567 /* Set up IT to deliver display elements from the first overlay
5568 string. */
5569 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5570 it->string = it->overlay_strings[0];
5571 it->from_overlay = Qnil;
5572 it->stop_charpos = 0;
5573 xassert (STRINGP (it->string));
5574 it->end_charpos = SCHARS (it->string);
5575 it->prev_stop = 0;
5576 it->base_level_stop = 0;
5577 it->multibyte_p = STRING_MULTIBYTE (it->string);
5578 it->method = GET_FROM_STRING;
5579 it->from_disp_prop_p = 0;
5581 /* Force paragraph direction to be that of the parent
5582 buffer. */
5583 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5584 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5585 else
5586 it->paragraph_embedding = L2R;
5588 /* Set up the bidi iterator for this overlay string. */
5589 if (it->bidi_p)
5591 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5593 it->bidi_it.string.lstring = it->string;
5594 it->bidi_it.string.s = NULL;
5595 it->bidi_it.string.schars = SCHARS (it->string);
5596 it->bidi_it.string.bufpos = pos;
5597 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5598 it->bidi_it.string.unibyte = !it->multibyte_p;
5599 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5601 return 1;
5604 it->current.overlay_string_index = -1;
5605 return 0;
5608 static int
5609 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5611 it->string = Qnil;
5612 it->method = GET_FROM_BUFFER;
5614 (void) get_overlay_strings_1 (it, charpos, 1);
5616 CHECK_IT (it);
5618 /* Value is non-zero if we found at least one overlay string. */
5619 return STRINGP (it->string);
5624 /***********************************************************************
5625 Saving and restoring state
5626 ***********************************************************************/
5628 /* Save current settings of IT on IT->stack. Called, for example,
5629 before setting up IT for an overlay string, to be able to restore
5630 IT's settings to what they were after the overlay string has been
5631 processed. If POSITION is non-NULL, it is the position to save on
5632 the stack instead of IT->position. */
5634 static void
5635 push_it (struct it *it, struct text_pos *position)
5637 struct iterator_stack_entry *p;
5639 xassert (it->sp < IT_STACK_SIZE);
5640 p = it->stack + it->sp;
5642 p->stop_charpos = it->stop_charpos;
5643 p->prev_stop = it->prev_stop;
5644 p->base_level_stop = it->base_level_stop;
5645 p->cmp_it = it->cmp_it;
5646 xassert (it->face_id >= 0);
5647 p->face_id = it->face_id;
5648 p->string = it->string;
5649 p->method = it->method;
5650 p->from_overlay = it->from_overlay;
5651 switch (p->method)
5653 case GET_FROM_IMAGE:
5654 p->u.image.object = it->object;
5655 p->u.image.image_id = it->image_id;
5656 p->u.image.slice = it->slice;
5657 break;
5658 case GET_FROM_STRETCH:
5659 p->u.stretch.object = it->object;
5660 break;
5662 p->position = position ? *position : it->position;
5663 p->current = it->current;
5664 p->end_charpos = it->end_charpos;
5665 p->string_nchars = it->string_nchars;
5666 p->area = it->area;
5667 p->multibyte_p = it->multibyte_p;
5668 p->avoid_cursor_p = it->avoid_cursor_p;
5669 p->space_width = it->space_width;
5670 p->font_height = it->font_height;
5671 p->voffset = it->voffset;
5672 p->string_from_display_prop_p = it->string_from_display_prop_p;
5673 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5674 p->display_ellipsis_p = 0;
5675 p->line_wrap = it->line_wrap;
5676 p->bidi_p = it->bidi_p;
5677 p->paragraph_embedding = it->paragraph_embedding;
5678 p->from_disp_prop_p = it->from_disp_prop_p;
5679 ++it->sp;
5681 /* Save the state of the bidi iterator as well. */
5682 if (it->bidi_p)
5683 bidi_push_it (&it->bidi_it);
5686 static void
5687 iterate_out_of_display_property (struct it *it)
5689 int buffer_p = !STRINGP (it->string);
5690 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5691 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5693 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5695 /* Maybe initialize paragraph direction. If we are at the beginning
5696 of a new paragraph, next_element_from_buffer may not have a
5697 chance to do that. */
5698 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5699 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5700 /* prev_stop can be zero, so check against BEGV as well. */
5701 while (it->bidi_it.charpos >= bob
5702 && it->prev_stop <= it->bidi_it.charpos
5703 && it->bidi_it.charpos < CHARPOS (it->position)
5704 && it->bidi_it.charpos < eob)
5705 bidi_move_to_visually_next (&it->bidi_it);
5706 /* Record the stop_pos we just crossed, for when we cross it
5707 back, maybe. */
5708 if (it->bidi_it.charpos > CHARPOS (it->position))
5709 it->prev_stop = CHARPOS (it->position);
5710 /* If we ended up not where pop_it put us, resync IT's
5711 positional members with the bidi iterator. */
5712 if (it->bidi_it.charpos != CHARPOS (it->position))
5713 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5714 if (buffer_p)
5715 it->current.pos = it->position;
5716 else
5717 it->current.string_pos = it->position;
5720 /* Restore IT's settings from IT->stack. Called, for example, when no
5721 more overlay strings must be processed, and we return to delivering
5722 display elements from a buffer, or when the end of a string from a
5723 `display' property is reached and we return to delivering display
5724 elements from an overlay string, or from a buffer. */
5726 static void
5727 pop_it (struct it *it)
5729 struct iterator_stack_entry *p;
5730 int from_display_prop = it->from_disp_prop_p;
5732 xassert (it->sp > 0);
5733 --it->sp;
5734 p = it->stack + it->sp;
5735 it->stop_charpos = p->stop_charpos;
5736 it->prev_stop = p->prev_stop;
5737 it->base_level_stop = p->base_level_stop;
5738 it->cmp_it = p->cmp_it;
5739 it->face_id = p->face_id;
5740 it->current = p->current;
5741 it->position = p->position;
5742 it->string = p->string;
5743 it->from_overlay = p->from_overlay;
5744 if (NILP (it->string))
5745 SET_TEXT_POS (it->current.string_pos, -1, -1);
5746 it->method = p->method;
5747 switch (it->method)
5749 case GET_FROM_IMAGE:
5750 it->image_id = p->u.image.image_id;
5751 it->object = p->u.image.object;
5752 it->slice = p->u.image.slice;
5753 break;
5754 case GET_FROM_STRETCH:
5755 it->object = p->u.stretch.object;
5756 break;
5757 case GET_FROM_BUFFER:
5758 it->object = it->w->buffer;
5759 break;
5760 case GET_FROM_STRING:
5761 it->object = it->string;
5762 break;
5763 case GET_FROM_DISPLAY_VECTOR:
5764 if (it->s)
5765 it->method = GET_FROM_C_STRING;
5766 else if (STRINGP (it->string))
5767 it->method = GET_FROM_STRING;
5768 else
5770 it->method = GET_FROM_BUFFER;
5771 it->object = it->w->buffer;
5774 it->end_charpos = p->end_charpos;
5775 it->string_nchars = p->string_nchars;
5776 it->area = p->area;
5777 it->multibyte_p = p->multibyte_p;
5778 it->avoid_cursor_p = p->avoid_cursor_p;
5779 it->space_width = p->space_width;
5780 it->font_height = p->font_height;
5781 it->voffset = p->voffset;
5782 it->string_from_display_prop_p = p->string_from_display_prop_p;
5783 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5784 it->line_wrap = p->line_wrap;
5785 it->bidi_p = p->bidi_p;
5786 it->paragraph_embedding = p->paragraph_embedding;
5787 it->from_disp_prop_p = p->from_disp_prop_p;
5788 if (it->bidi_p)
5790 bidi_pop_it (&it->bidi_it);
5791 /* Bidi-iterate until we get out of the portion of text, if any,
5792 covered by a `display' text property or by an overlay with
5793 `display' property. (We cannot just jump there, because the
5794 internal coherency of the bidi iterator state can not be
5795 preserved across such jumps.) We also must determine the
5796 paragraph base direction if the overlay we just processed is
5797 at the beginning of a new paragraph. */
5798 if (from_display_prop
5799 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5800 iterate_out_of_display_property (it);
5802 xassert ((BUFFERP (it->object)
5803 && IT_CHARPOS (*it) == it->bidi_it.charpos
5804 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5805 || (STRINGP (it->object)
5806 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5807 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5808 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5814 /***********************************************************************
5815 Moving over lines
5816 ***********************************************************************/
5818 /* Set IT's current position to the previous line start. */
5820 static void
5821 back_to_previous_line_start (struct it *it)
5823 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5824 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5828 /* Move IT to the next line start.
5830 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5831 we skipped over part of the text (as opposed to moving the iterator
5832 continuously over the text). Otherwise, don't change the value
5833 of *SKIPPED_P.
5835 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5836 iterator on the newline, if it was found.
5838 Newlines may come from buffer text, overlay strings, or strings
5839 displayed via the `display' property. That's the reason we can't
5840 simply use find_next_newline_no_quit.
5842 Note that this function may not skip over invisible text that is so
5843 because of text properties and immediately follows a newline. If
5844 it would, function reseat_at_next_visible_line_start, when called
5845 from set_iterator_to_next, would effectively make invisible
5846 characters following a newline part of the wrong glyph row, which
5847 leads to wrong cursor motion. */
5849 static int
5850 forward_to_next_line_start (struct it *it, int *skipped_p,
5851 struct bidi_it *bidi_it_prev)
5853 ptrdiff_t old_selective;
5854 int newline_found_p, n;
5855 const int MAX_NEWLINE_DISTANCE = 500;
5857 /* If already on a newline, just consume it to avoid unintended
5858 skipping over invisible text below. */
5859 if (it->what == IT_CHARACTER
5860 && it->c == '\n'
5861 && CHARPOS (it->position) == IT_CHARPOS (*it))
5863 if (it->bidi_p && bidi_it_prev)
5864 *bidi_it_prev = it->bidi_it;
5865 set_iterator_to_next (it, 0);
5866 it->c = 0;
5867 return 1;
5870 /* Don't handle selective display in the following. It's (a)
5871 unnecessary because it's done by the caller, and (b) leads to an
5872 infinite recursion because next_element_from_ellipsis indirectly
5873 calls this function. */
5874 old_selective = it->selective;
5875 it->selective = 0;
5877 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5878 from buffer text. */
5879 for (n = newline_found_p = 0;
5880 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5881 n += STRINGP (it->string) ? 0 : 1)
5883 if (!get_next_display_element (it))
5884 return 0;
5885 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5886 if (newline_found_p && it->bidi_p && bidi_it_prev)
5887 *bidi_it_prev = it->bidi_it;
5888 set_iterator_to_next (it, 0);
5891 /* If we didn't find a newline near enough, see if we can use a
5892 short-cut. */
5893 if (!newline_found_p)
5895 ptrdiff_t start = IT_CHARPOS (*it);
5896 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5897 Lisp_Object pos;
5899 xassert (!STRINGP (it->string));
5901 /* If there isn't any `display' property in sight, and no
5902 overlays, we can just use the position of the newline in
5903 buffer text. */
5904 if (it->stop_charpos >= limit
5905 || ((pos = Fnext_single_property_change (make_number (start),
5906 Qdisplay, Qnil,
5907 make_number (limit)),
5908 NILP (pos))
5909 && next_overlay_change (start) == ZV))
5911 if (!it->bidi_p)
5913 IT_CHARPOS (*it) = limit;
5914 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5916 else
5918 struct bidi_it bprev;
5920 /* Help bidi.c avoid expensive searches for display
5921 properties and overlays, by telling it that there are
5922 none up to `limit'. */
5923 if (it->bidi_it.disp_pos < limit)
5925 it->bidi_it.disp_pos = limit;
5926 it->bidi_it.disp_prop = 0;
5928 do {
5929 bprev = it->bidi_it;
5930 bidi_move_to_visually_next (&it->bidi_it);
5931 } while (it->bidi_it.charpos != limit);
5932 IT_CHARPOS (*it) = limit;
5933 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5934 if (bidi_it_prev)
5935 *bidi_it_prev = bprev;
5937 *skipped_p = newline_found_p = 1;
5939 else
5941 while (get_next_display_element (it)
5942 && !newline_found_p)
5944 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5945 if (newline_found_p && it->bidi_p && bidi_it_prev)
5946 *bidi_it_prev = it->bidi_it;
5947 set_iterator_to_next (it, 0);
5952 it->selective = old_selective;
5953 return newline_found_p;
5957 /* Set IT's current position to the previous visible line start. Skip
5958 invisible text that is so either due to text properties or due to
5959 selective display. Caution: this does not change IT->current_x and
5960 IT->hpos. */
5962 static void
5963 back_to_previous_visible_line_start (struct it *it)
5965 while (IT_CHARPOS (*it) > BEGV)
5967 back_to_previous_line_start (it);
5969 if (IT_CHARPOS (*it) <= BEGV)
5970 break;
5972 /* If selective > 0, then lines indented more than its value are
5973 invisible. */
5974 if (it->selective > 0
5975 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5976 it->selective))
5977 continue;
5979 /* Check the newline before point for invisibility. */
5981 Lisp_Object prop;
5982 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5983 Qinvisible, it->window);
5984 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5985 continue;
5988 if (IT_CHARPOS (*it) <= BEGV)
5989 break;
5992 struct it it2;
5993 void *it2data = NULL;
5994 ptrdiff_t pos;
5995 ptrdiff_t beg, end;
5996 Lisp_Object val, overlay;
5998 SAVE_IT (it2, *it, it2data);
6000 /* If newline is part of a composition, continue from start of composition */
6001 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6002 && beg < IT_CHARPOS (*it))
6003 goto replaced;
6005 /* If newline is replaced by a display property, find start of overlay
6006 or interval and continue search from that point. */
6007 pos = --IT_CHARPOS (it2);
6008 --IT_BYTEPOS (it2);
6009 it2.sp = 0;
6010 bidi_unshelve_cache (NULL, 0);
6011 it2.string_from_display_prop_p = 0;
6012 it2.from_disp_prop_p = 0;
6013 if (handle_display_prop (&it2) == HANDLED_RETURN
6014 && !NILP (val = get_char_property_and_overlay
6015 (make_number (pos), Qdisplay, Qnil, &overlay))
6016 && (OVERLAYP (overlay)
6017 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6018 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6020 RESTORE_IT (it, it, it2data);
6021 goto replaced;
6024 /* Newline is not replaced by anything -- so we are done. */
6025 RESTORE_IT (it, it, it2data);
6026 break;
6028 replaced:
6029 if (beg < BEGV)
6030 beg = BEGV;
6031 IT_CHARPOS (*it) = beg;
6032 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6036 it->continuation_lines_width = 0;
6038 xassert (IT_CHARPOS (*it) >= BEGV);
6039 xassert (IT_CHARPOS (*it) == BEGV
6040 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6041 CHECK_IT (it);
6045 /* Reseat iterator IT at the previous visible line start. Skip
6046 invisible text that is so either due to text properties or due to
6047 selective display. At the end, update IT's overlay information,
6048 face information etc. */
6050 void
6051 reseat_at_previous_visible_line_start (struct it *it)
6053 back_to_previous_visible_line_start (it);
6054 reseat (it, it->current.pos, 1);
6055 CHECK_IT (it);
6059 /* Reseat iterator IT on the next visible line start in the current
6060 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6061 preceding the line start. Skip over invisible text that is so
6062 because of selective display. Compute faces, overlays etc at the
6063 new position. Note that this function does not skip over text that
6064 is invisible because of text properties. */
6066 static void
6067 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6069 int newline_found_p, skipped_p = 0;
6070 struct bidi_it bidi_it_prev;
6072 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6074 /* Skip over lines that are invisible because they are indented
6075 more than the value of IT->selective. */
6076 if (it->selective > 0)
6077 while (IT_CHARPOS (*it) < ZV
6078 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6079 it->selective))
6081 xassert (IT_BYTEPOS (*it) == BEGV
6082 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6083 newline_found_p =
6084 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6087 /* Position on the newline if that's what's requested. */
6088 if (on_newline_p && newline_found_p)
6090 if (STRINGP (it->string))
6092 if (IT_STRING_CHARPOS (*it) > 0)
6094 if (!it->bidi_p)
6096 --IT_STRING_CHARPOS (*it);
6097 --IT_STRING_BYTEPOS (*it);
6099 else
6101 /* We need to restore the bidi iterator to the state
6102 it had on the newline, and resync the IT's
6103 position with that. */
6104 it->bidi_it = bidi_it_prev;
6105 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6106 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6110 else if (IT_CHARPOS (*it) > BEGV)
6112 if (!it->bidi_p)
6114 --IT_CHARPOS (*it);
6115 --IT_BYTEPOS (*it);
6117 else
6119 /* We need to restore the bidi iterator to the state it
6120 had on the newline and resync IT with that. */
6121 it->bidi_it = bidi_it_prev;
6122 IT_CHARPOS (*it) = it->bidi_it.charpos;
6123 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6125 reseat (it, it->current.pos, 0);
6128 else if (skipped_p)
6129 reseat (it, it->current.pos, 0);
6131 CHECK_IT (it);
6136 /***********************************************************************
6137 Changing an iterator's position
6138 ***********************************************************************/
6140 /* Change IT's current position to POS in current_buffer. If FORCE_P
6141 is non-zero, always check for text properties at the new position.
6142 Otherwise, text properties are only looked up if POS >=
6143 IT->check_charpos of a property. */
6145 static void
6146 reseat (struct it *it, struct text_pos pos, int force_p)
6148 ptrdiff_t original_pos = IT_CHARPOS (*it);
6150 reseat_1 (it, pos, 0);
6152 /* Determine where to check text properties. Avoid doing it
6153 where possible because text property lookup is very expensive. */
6154 if (force_p
6155 || CHARPOS (pos) > it->stop_charpos
6156 || CHARPOS (pos) < original_pos)
6158 if (it->bidi_p)
6160 /* For bidi iteration, we need to prime prev_stop and
6161 base_level_stop with our best estimations. */
6162 /* Implementation note: Of course, POS is not necessarily a
6163 stop position, so assigning prev_pos to it is a lie; we
6164 should have called compute_stop_backwards. However, if
6165 the current buffer does not include any R2L characters,
6166 that call would be a waste of cycles, because the
6167 iterator will never move back, and thus never cross this
6168 "fake" stop position. So we delay that backward search
6169 until the time we really need it, in next_element_from_buffer. */
6170 if (CHARPOS (pos) != it->prev_stop)
6171 it->prev_stop = CHARPOS (pos);
6172 if (CHARPOS (pos) < it->base_level_stop)
6173 it->base_level_stop = 0; /* meaning it's unknown */
6174 handle_stop (it);
6176 else
6178 handle_stop (it);
6179 it->prev_stop = it->base_level_stop = 0;
6184 CHECK_IT (it);
6188 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6189 IT->stop_pos to POS, also. */
6191 static void
6192 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6194 /* Don't call this function when scanning a C string. */
6195 xassert (it->s == NULL);
6197 /* POS must be a reasonable value. */
6198 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6200 it->current.pos = it->position = pos;
6201 it->end_charpos = ZV;
6202 it->dpvec = NULL;
6203 it->current.dpvec_index = -1;
6204 it->current.overlay_string_index = -1;
6205 IT_STRING_CHARPOS (*it) = -1;
6206 IT_STRING_BYTEPOS (*it) = -1;
6207 it->string = Qnil;
6208 it->method = GET_FROM_BUFFER;
6209 it->object = it->w->buffer;
6210 it->area = TEXT_AREA;
6211 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6212 it->sp = 0;
6213 it->string_from_display_prop_p = 0;
6214 it->string_from_prefix_prop_p = 0;
6216 it->from_disp_prop_p = 0;
6217 it->face_before_selective_p = 0;
6218 if (it->bidi_p)
6220 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6221 &it->bidi_it);
6222 bidi_unshelve_cache (NULL, 0);
6223 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6224 it->bidi_it.string.s = NULL;
6225 it->bidi_it.string.lstring = Qnil;
6226 it->bidi_it.string.bufpos = 0;
6227 it->bidi_it.string.unibyte = 0;
6230 if (set_stop_p)
6232 it->stop_charpos = CHARPOS (pos);
6233 it->base_level_stop = CHARPOS (pos);
6238 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6239 If S is non-null, it is a C string to iterate over. Otherwise,
6240 STRING gives a Lisp string to iterate over.
6242 If PRECISION > 0, don't return more then PRECISION number of
6243 characters from the string.
6245 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6246 characters have been returned. FIELD_WIDTH < 0 means an infinite
6247 field width.
6249 MULTIBYTE = 0 means disable processing of multibyte characters,
6250 MULTIBYTE > 0 means enable it,
6251 MULTIBYTE < 0 means use IT->multibyte_p.
6253 IT must be initialized via a prior call to init_iterator before
6254 calling this function. */
6256 static void
6257 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6258 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6259 int multibyte)
6261 /* No region in strings. */
6262 it->region_beg_charpos = it->region_end_charpos = -1;
6264 /* No text property checks performed by default, but see below. */
6265 it->stop_charpos = -1;
6267 /* Set iterator position and end position. */
6268 memset (&it->current, 0, sizeof it->current);
6269 it->current.overlay_string_index = -1;
6270 it->current.dpvec_index = -1;
6271 xassert (charpos >= 0);
6273 /* If STRING is specified, use its multibyteness, otherwise use the
6274 setting of MULTIBYTE, if specified. */
6275 if (multibyte >= 0)
6276 it->multibyte_p = multibyte > 0;
6278 /* Bidirectional reordering of strings is controlled by the default
6279 value of bidi-display-reordering. Don't try to reorder while
6280 loading loadup.el, as the necessary character property tables are
6281 not yet available. */
6282 it->bidi_p =
6283 NILP (Vpurify_flag)
6284 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6286 if (s == NULL)
6288 xassert (STRINGP (string));
6289 it->string = string;
6290 it->s = NULL;
6291 it->end_charpos = it->string_nchars = SCHARS (string);
6292 it->method = GET_FROM_STRING;
6293 it->current.string_pos = string_pos (charpos, string);
6295 if (it->bidi_p)
6297 it->bidi_it.string.lstring = string;
6298 it->bidi_it.string.s = NULL;
6299 it->bidi_it.string.schars = it->end_charpos;
6300 it->bidi_it.string.bufpos = 0;
6301 it->bidi_it.string.from_disp_str = 0;
6302 it->bidi_it.string.unibyte = !it->multibyte_p;
6303 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6304 FRAME_WINDOW_P (it->f), &it->bidi_it);
6307 else
6309 it->s = (const unsigned char *) s;
6310 it->string = Qnil;
6312 /* Note that we use IT->current.pos, not it->current.string_pos,
6313 for displaying C strings. */
6314 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6315 if (it->multibyte_p)
6317 it->current.pos = c_string_pos (charpos, s, 1);
6318 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6320 else
6322 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6323 it->end_charpos = it->string_nchars = strlen (s);
6326 if (it->bidi_p)
6328 it->bidi_it.string.lstring = Qnil;
6329 it->bidi_it.string.s = (const unsigned char *) s;
6330 it->bidi_it.string.schars = it->end_charpos;
6331 it->bidi_it.string.bufpos = 0;
6332 it->bidi_it.string.from_disp_str = 0;
6333 it->bidi_it.string.unibyte = !it->multibyte_p;
6334 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6335 &it->bidi_it);
6337 it->method = GET_FROM_C_STRING;
6340 /* PRECISION > 0 means don't return more than PRECISION characters
6341 from the string. */
6342 if (precision > 0 && it->end_charpos - charpos > precision)
6344 it->end_charpos = it->string_nchars = charpos + precision;
6345 if (it->bidi_p)
6346 it->bidi_it.string.schars = it->end_charpos;
6349 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6350 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6351 FIELD_WIDTH < 0 means infinite field width. This is useful for
6352 padding with `-' at the end of a mode line. */
6353 if (field_width < 0)
6354 field_width = INFINITY;
6355 /* Implementation note: We deliberately don't enlarge
6356 it->bidi_it.string.schars here to fit it->end_charpos, because
6357 the bidi iterator cannot produce characters out of thin air. */
6358 if (field_width > it->end_charpos - charpos)
6359 it->end_charpos = charpos + field_width;
6361 /* Use the standard display table for displaying strings. */
6362 if (DISP_TABLE_P (Vstandard_display_table))
6363 it->dp = XCHAR_TABLE (Vstandard_display_table);
6365 it->stop_charpos = charpos;
6366 it->prev_stop = charpos;
6367 it->base_level_stop = 0;
6368 if (it->bidi_p)
6370 it->bidi_it.first_elt = 1;
6371 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6372 it->bidi_it.disp_pos = -1;
6374 if (s == NULL && it->multibyte_p)
6376 ptrdiff_t endpos = SCHARS (it->string);
6377 if (endpos > it->end_charpos)
6378 endpos = it->end_charpos;
6379 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6380 it->string);
6382 CHECK_IT (it);
6387 /***********************************************************************
6388 Iteration
6389 ***********************************************************************/
6391 /* Map enum it_method value to corresponding next_element_from_* function. */
6393 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6395 next_element_from_buffer,
6396 next_element_from_display_vector,
6397 next_element_from_string,
6398 next_element_from_c_string,
6399 next_element_from_image,
6400 next_element_from_stretch
6403 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6406 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6407 (possibly with the following characters). */
6409 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6410 ((IT)->cmp_it.id >= 0 \
6411 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6412 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6413 END_CHARPOS, (IT)->w, \
6414 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6415 (IT)->string)))
6418 /* Lookup the char-table Vglyphless_char_display for character C (-1
6419 if we want information for no-font case), and return the display
6420 method symbol. By side-effect, update it->what and
6421 it->glyphless_method. This function is called from
6422 get_next_display_element for each character element, and from
6423 x_produce_glyphs when no suitable font was found. */
6425 Lisp_Object
6426 lookup_glyphless_char_display (int c, struct it *it)
6428 Lisp_Object glyphless_method = Qnil;
6430 if (CHAR_TABLE_P (Vglyphless_char_display)
6431 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6433 if (c >= 0)
6435 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6436 if (CONSP (glyphless_method))
6437 glyphless_method = FRAME_WINDOW_P (it->f)
6438 ? XCAR (glyphless_method)
6439 : XCDR (glyphless_method);
6441 else
6442 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6445 retry:
6446 if (NILP (glyphless_method))
6448 if (c >= 0)
6449 /* The default is to display the character by a proper font. */
6450 return Qnil;
6451 /* The default for the no-font case is to display an empty box. */
6452 glyphless_method = Qempty_box;
6454 if (EQ (glyphless_method, Qzero_width))
6456 if (c >= 0)
6457 return glyphless_method;
6458 /* This method can't be used for the no-font case. */
6459 glyphless_method = Qempty_box;
6461 if (EQ (glyphless_method, Qthin_space))
6462 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6463 else if (EQ (glyphless_method, Qempty_box))
6464 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6465 else if (EQ (glyphless_method, Qhex_code))
6466 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6467 else if (STRINGP (glyphless_method))
6468 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6469 else
6471 /* Invalid value. We use the default method. */
6472 glyphless_method = Qnil;
6473 goto retry;
6475 it->what = IT_GLYPHLESS;
6476 return glyphless_method;
6479 /* Load IT's display element fields with information about the next
6480 display element from the current position of IT. Value is zero if
6481 end of buffer (or C string) is reached. */
6483 static struct frame *last_escape_glyph_frame = NULL;
6484 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6485 static int last_escape_glyph_merged_face_id = 0;
6487 struct frame *last_glyphless_glyph_frame = NULL;
6488 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6489 int last_glyphless_glyph_merged_face_id = 0;
6491 static int
6492 get_next_display_element (struct it *it)
6494 /* Non-zero means that we found a display element. Zero means that
6495 we hit the end of what we iterate over. Performance note: the
6496 function pointer `method' used here turns out to be faster than
6497 using a sequence of if-statements. */
6498 int success_p;
6500 get_next:
6501 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6503 if (it->what == IT_CHARACTER)
6505 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6506 and only if (a) the resolved directionality of that character
6507 is R..." */
6508 /* FIXME: Do we need an exception for characters from display
6509 tables? */
6510 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6511 it->c = bidi_mirror_char (it->c);
6512 /* Map via display table or translate control characters.
6513 IT->c, IT->len etc. have been set to the next character by
6514 the function call above. If we have a display table, and it
6515 contains an entry for IT->c, translate it. Don't do this if
6516 IT->c itself comes from a display table, otherwise we could
6517 end up in an infinite recursion. (An alternative could be to
6518 count the recursion depth of this function and signal an
6519 error when a certain maximum depth is reached.) Is it worth
6520 it? */
6521 if (success_p && it->dpvec == NULL)
6523 Lisp_Object dv;
6524 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6525 int nonascii_space_p = 0;
6526 int nonascii_hyphen_p = 0;
6527 int c = it->c; /* This is the character to display. */
6529 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6531 xassert (SINGLE_BYTE_CHAR_P (c));
6532 if (unibyte_display_via_language_environment)
6534 c = DECODE_CHAR (unibyte, c);
6535 if (c < 0)
6536 c = BYTE8_TO_CHAR (it->c);
6538 else
6539 c = BYTE8_TO_CHAR (it->c);
6542 if (it->dp
6543 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6544 VECTORP (dv)))
6546 struct Lisp_Vector *v = XVECTOR (dv);
6548 /* Return the first character from the display table
6549 entry, if not empty. If empty, don't display the
6550 current character. */
6551 if (v->header.size)
6553 it->dpvec_char_len = it->len;
6554 it->dpvec = v->contents;
6555 it->dpend = v->contents + v->header.size;
6556 it->current.dpvec_index = 0;
6557 it->dpvec_face_id = -1;
6558 it->saved_face_id = it->face_id;
6559 it->method = GET_FROM_DISPLAY_VECTOR;
6560 it->ellipsis_p = 0;
6562 else
6564 set_iterator_to_next (it, 0);
6566 goto get_next;
6569 if (! NILP (lookup_glyphless_char_display (c, it)))
6571 if (it->what == IT_GLYPHLESS)
6572 goto done;
6573 /* Don't display this character. */
6574 set_iterator_to_next (it, 0);
6575 goto get_next;
6578 /* If `nobreak-char-display' is non-nil, we display
6579 non-ASCII spaces and hyphens specially. */
6580 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6582 if (c == 0xA0)
6583 nonascii_space_p = 1;
6584 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6585 nonascii_hyphen_p = 1;
6588 /* Translate control characters into `\003' or `^C' form.
6589 Control characters coming from a display table entry are
6590 currently not translated because we use IT->dpvec to hold
6591 the translation. This could easily be changed but I
6592 don't believe that it is worth doing.
6594 The characters handled by `nobreak-char-display' must be
6595 translated too.
6597 Non-printable characters and raw-byte characters are also
6598 translated to octal form. */
6599 if (((c < ' ' || c == 127) /* ASCII control chars */
6600 ? (it->area != TEXT_AREA
6601 /* In mode line, treat \n, \t like other crl chars. */
6602 || (c != '\t'
6603 && it->glyph_row
6604 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6605 || (c != '\n' && c != '\t'))
6606 : (nonascii_space_p
6607 || nonascii_hyphen_p
6608 || CHAR_BYTE8_P (c)
6609 || ! CHAR_PRINTABLE_P (c))))
6611 /* C is a control character, non-ASCII space/hyphen,
6612 raw-byte, or a non-printable character which must be
6613 displayed either as '\003' or as `^C' where the '\\'
6614 and '^' can be defined in the display table. Fill
6615 IT->ctl_chars with glyphs for what we have to
6616 display. Then, set IT->dpvec to these glyphs. */
6617 Lisp_Object gc;
6618 int ctl_len;
6619 int face_id;
6620 int lface_id = 0;
6621 int escape_glyph;
6623 /* Handle control characters with ^. */
6625 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6627 int g;
6629 g = '^'; /* default glyph for Control */
6630 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6631 if (it->dp
6632 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6634 g = GLYPH_CODE_CHAR (gc);
6635 lface_id = GLYPH_CODE_FACE (gc);
6637 if (lface_id)
6639 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6641 else if (it->f == last_escape_glyph_frame
6642 && it->face_id == last_escape_glyph_face_id)
6644 face_id = last_escape_glyph_merged_face_id;
6646 else
6648 /* Merge the escape-glyph face into the current face. */
6649 face_id = merge_faces (it->f, Qescape_glyph, 0,
6650 it->face_id);
6651 last_escape_glyph_frame = it->f;
6652 last_escape_glyph_face_id = it->face_id;
6653 last_escape_glyph_merged_face_id = face_id;
6656 XSETINT (it->ctl_chars[0], g);
6657 XSETINT (it->ctl_chars[1], c ^ 0100);
6658 ctl_len = 2;
6659 goto display_control;
6662 /* Handle non-ascii space in the mode where it only gets
6663 highlighting. */
6665 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6667 /* Merge `nobreak-space' into the current face. */
6668 face_id = merge_faces (it->f, Qnobreak_space, 0,
6669 it->face_id);
6670 XSETINT (it->ctl_chars[0], ' ');
6671 ctl_len = 1;
6672 goto display_control;
6675 /* Handle sequences that start with the "escape glyph". */
6677 /* the default escape glyph is \. */
6678 escape_glyph = '\\';
6680 if (it->dp
6681 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6683 escape_glyph = GLYPH_CODE_CHAR (gc);
6684 lface_id = GLYPH_CODE_FACE (gc);
6686 if (lface_id)
6688 /* The display table specified a face.
6689 Merge it into face_id and also into escape_glyph. */
6690 face_id = merge_faces (it->f, Qt, lface_id,
6691 it->face_id);
6693 else if (it->f == last_escape_glyph_frame
6694 && it->face_id == last_escape_glyph_face_id)
6696 face_id = last_escape_glyph_merged_face_id;
6698 else
6700 /* Merge the escape-glyph face into the current face. */
6701 face_id = merge_faces (it->f, Qescape_glyph, 0,
6702 it->face_id);
6703 last_escape_glyph_frame = it->f;
6704 last_escape_glyph_face_id = it->face_id;
6705 last_escape_glyph_merged_face_id = face_id;
6708 /* Draw non-ASCII hyphen with just highlighting: */
6710 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6712 XSETINT (it->ctl_chars[0], '-');
6713 ctl_len = 1;
6714 goto display_control;
6717 /* Draw non-ASCII space/hyphen with escape glyph: */
6719 if (nonascii_space_p || nonascii_hyphen_p)
6721 XSETINT (it->ctl_chars[0], escape_glyph);
6722 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6723 ctl_len = 2;
6724 goto display_control;
6728 char str[10];
6729 int len, i;
6731 if (CHAR_BYTE8_P (c))
6732 /* Display \200 instead of \17777600. */
6733 c = CHAR_TO_BYTE8 (c);
6734 len = sprintf (str, "%03o", c);
6736 XSETINT (it->ctl_chars[0], escape_glyph);
6737 for (i = 0; i < len; i++)
6738 XSETINT (it->ctl_chars[i + 1], str[i]);
6739 ctl_len = len + 1;
6742 display_control:
6743 /* Set up IT->dpvec and return first character from it. */
6744 it->dpvec_char_len = it->len;
6745 it->dpvec = it->ctl_chars;
6746 it->dpend = it->dpvec + ctl_len;
6747 it->current.dpvec_index = 0;
6748 it->dpvec_face_id = face_id;
6749 it->saved_face_id = it->face_id;
6750 it->method = GET_FROM_DISPLAY_VECTOR;
6751 it->ellipsis_p = 0;
6752 goto get_next;
6754 it->char_to_display = c;
6756 else if (success_p)
6758 it->char_to_display = it->c;
6762 /* Adjust face id for a multibyte character. There are no multibyte
6763 character in unibyte text. */
6764 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6765 && it->multibyte_p
6766 && success_p
6767 && FRAME_WINDOW_P (it->f))
6769 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6771 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6773 /* Automatic composition with glyph-string. */
6774 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6776 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6778 else
6780 ptrdiff_t pos = (it->s ? -1
6781 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6782 : IT_CHARPOS (*it));
6783 int c;
6785 if (it->what == IT_CHARACTER)
6786 c = it->char_to_display;
6787 else
6789 struct composition *cmp = composition_table[it->cmp_it.id];
6790 int i;
6792 c = ' ';
6793 for (i = 0; i < cmp->glyph_len; i++)
6794 /* TAB in a composition means display glyphs with
6795 padding space on the left or right. */
6796 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6797 break;
6799 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6803 done:
6804 /* Is this character the last one of a run of characters with
6805 box? If yes, set IT->end_of_box_run_p to 1. */
6806 if (it->face_box_p
6807 && it->s == NULL)
6809 if (it->method == GET_FROM_STRING && it->sp)
6811 int face_id = underlying_face_id (it);
6812 struct face *face = FACE_FROM_ID (it->f, face_id);
6814 if (face)
6816 if (face->box == FACE_NO_BOX)
6818 /* If the box comes from face properties in a
6819 display string, check faces in that string. */
6820 int string_face_id = face_after_it_pos (it);
6821 it->end_of_box_run_p
6822 = (FACE_FROM_ID (it->f, string_face_id)->box
6823 == FACE_NO_BOX);
6825 /* Otherwise, the box comes from the underlying face.
6826 If this is the last string character displayed, check
6827 the next buffer location. */
6828 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6829 && (it->current.overlay_string_index
6830 == it->n_overlay_strings - 1))
6832 ptrdiff_t ignore;
6833 int next_face_id;
6834 struct text_pos pos = it->current.pos;
6835 INC_TEXT_POS (pos, it->multibyte_p);
6837 next_face_id = face_at_buffer_position
6838 (it->w, CHARPOS (pos), it->region_beg_charpos,
6839 it->region_end_charpos, &ignore,
6840 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6841 -1);
6842 it->end_of_box_run_p
6843 = (FACE_FROM_ID (it->f, next_face_id)->box
6844 == FACE_NO_BOX);
6848 else
6850 int face_id = face_after_it_pos (it);
6851 it->end_of_box_run_p
6852 = (face_id != it->face_id
6853 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6856 /* If we reached the end of the object we've been iterating (e.g., a
6857 display string or an overlay string), and there's something on
6858 IT->stack, proceed with what's on the stack. It doesn't make
6859 sense to return zero if there's unprocessed stuff on the stack,
6860 because otherwise that stuff will never be displayed. */
6861 if (!success_p && it->sp > 0)
6863 set_iterator_to_next (it, 0);
6864 success_p = get_next_display_element (it);
6867 /* Value is 0 if end of buffer or string reached. */
6868 return success_p;
6872 /* Move IT to the next display element.
6874 RESEAT_P non-zero means if called on a newline in buffer text,
6875 skip to the next visible line start.
6877 Functions get_next_display_element and set_iterator_to_next are
6878 separate because I find this arrangement easier to handle than a
6879 get_next_display_element function that also increments IT's
6880 position. The way it is we can first look at an iterator's current
6881 display element, decide whether it fits on a line, and if it does,
6882 increment the iterator position. The other way around we probably
6883 would either need a flag indicating whether the iterator has to be
6884 incremented the next time, or we would have to implement a
6885 decrement position function which would not be easy to write. */
6887 void
6888 set_iterator_to_next (struct it *it, int reseat_p)
6890 /* Reset flags indicating start and end of a sequence of characters
6891 with box. Reset them at the start of this function because
6892 moving the iterator to a new position might set them. */
6893 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6895 switch (it->method)
6897 case GET_FROM_BUFFER:
6898 /* The current display element of IT is a character from
6899 current_buffer. Advance in the buffer, and maybe skip over
6900 invisible lines that are so because of selective display. */
6901 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6902 reseat_at_next_visible_line_start (it, 0);
6903 else if (it->cmp_it.id >= 0)
6905 /* We are currently getting glyphs from a composition. */
6906 int i;
6908 if (! it->bidi_p)
6910 IT_CHARPOS (*it) += it->cmp_it.nchars;
6911 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6912 if (it->cmp_it.to < it->cmp_it.nglyphs)
6914 it->cmp_it.from = it->cmp_it.to;
6916 else
6918 it->cmp_it.id = -1;
6919 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6920 IT_BYTEPOS (*it),
6921 it->end_charpos, Qnil);
6924 else if (! it->cmp_it.reversed_p)
6926 /* Composition created while scanning forward. */
6927 /* Update IT's char/byte positions to point to the first
6928 character of the next grapheme cluster, or to the
6929 character visually after the current composition. */
6930 for (i = 0; i < it->cmp_it.nchars; i++)
6931 bidi_move_to_visually_next (&it->bidi_it);
6932 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6933 IT_CHARPOS (*it) = it->bidi_it.charpos;
6935 if (it->cmp_it.to < it->cmp_it.nglyphs)
6937 /* Proceed to the next grapheme cluster. */
6938 it->cmp_it.from = it->cmp_it.to;
6940 else
6942 /* No more grapheme clusters in this composition.
6943 Find the next stop position. */
6944 ptrdiff_t stop = it->end_charpos;
6945 if (it->bidi_it.scan_dir < 0)
6946 /* Now we are scanning backward and don't know
6947 where to stop. */
6948 stop = -1;
6949 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6950 IT_BYTEPOS (*it), stop, Qnil);
6953 else
6955 /* Composition created while scanning backward. */
6956 /* Update IT's char/byte positions to point to the last
6957 character of the previous grapheme cluster, or the
6958 character visually after the current composition. */
6959 for (i = 0; i < it->cmp_it.nchars; i++)
6960 bidi_move_to_visually_next (&it->bidi_it);
6961 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6962 IT_CHARPOS (*it) = it->bidi_it.charpos;
6963 if (it->cmp_it.from > 0)
6965 /* Proceed to the previous grapheme cluster. */
6966 it->cmp_it.to = it->cmp_it.from;
6968 else
6970 /* No more grapheme clusters in this composition.
6971 Find the next stop position. */
6972 ptrdiff_t stop = it->end_charpos;
6973 if (it->bidi_it.scan_dir < 0)
6974 /* Now we are scanning backward and don't know
6975 where to stop. */
6976 stop = -1;
6977 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6978 IT_BYTEPOS (*it), stop, Qnil);
6982 else
6984 xassert (it->len != 0);
6986 if (!it->bidi_p)
6988 IT_BYTEPOS (*it) += it->len;
6989 IT_CHARPOS (*it) += 1;
6991 else
6993 int prev_scan_dir = it->bidi_it.scan_dir;
6994 /* If this is a new paragraph, determine its base
6995 direction (a.k.a. its base embedding level). */
6996 if (it->bidi_it.new_paragraph)
6997 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6998 bidi_move_to_visually_next (&it->bidi_it);
6999 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7000 IT_CHARPOS (*it) = it->bidi_it.charpos;
7001 if (prev_scan_dir != it->bidi_it.scan_dir)
7003 /* As the scan direction was changed, we must
7004 re-compute the stop position for composition. */
7005 ptrdiff_t stop = it->end_charpos;
7006 if (it->bidi_it.scan_dir < 0)
7007 stop = -1;
7008 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7009 IT_BYTEPOS (*it), stop, Qnil);
7012 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7014 break;
7016 case GET_FROM_C_STRING:
7017 /* Current display element of IT is from a C string. */
7018 if (!it->bidi_p
7019 /* If the string position is beyond string's end, it means
7020 next_element_from_c_string is padding the string with
7021 blanks, in which case we bypass the bidi iterator,
7022 because it cannot deal with such virtual characters. */
7023 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7025 IT_BYTEPOS (*it) += it->len;
7026 IT_CHARPOS (*it) += 1;
7028 else
7030 bidi_move_to_visually_next (&it->bidi_it);
7031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7034 break;
7036 case GET_FROM_DISPLAY_VECTOR:
7037 /* Current display element of IT is from a display table entry.
7038 Advance in the display table definition. Reset it to null if
7039 end reached, and continue with characters from buffers/
7040 strings. */
7041 ++it->current.dpvec_index;
7043 /* Restore face of the iterator to what they were before the
7044 display vector entry (these entries may contain faces). */
7045 it->face_id = it->saved_face_id;
7047 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7049 int recheck_faces = it->ellipsis_p;
7051 if (it->s)
7052 it->method = GET_FROM_C_STRING;
7053 else if (STRINGP (it->string))
7054 it->method = GET_FROM_STRING;
7055 else
7057 it->method = GET_FROM_BUFFER;
7058 it->object = it->w->buffer;
7061 it->dpvec = NULL;
7062 it->current.dpvec_index = -1;
7064 /* Skip over characters which were displayed via IT->dpvec. */
7065 if (it->dpvec_char_len < 0)
7066 reseat_at_next_visible_line_start (it, 1);
7067 else if (it->dpvec_char_len > 0)
7069 if (it->method == GET_FROM_STRING
7070 && it->n_overlay_strings > 0)
7071 it->ignore_overlay_strings_at_pos_p = 1;
7072 it->len = it->dpvec_char_len;
7073 set_iterator_to_next (it, reseat_p);
7076 /* Maybe recheck faces after display vector */
7077 if (recheck_faces)
7078 it->stop_charpos = IT_CHARPOS (*it);
7080 break;
7082 case GET_FROM_STRING:
7083 /* Current display element is a character from a Lisp string. */
7084 xassert (it->s == NULL && STRINGP (it->string));
7085 /* Don't advance past string end. These conditions are true
7086 when set_iterator_to_next is called at the end of
7087 get_next_display_element, in which case the Lisp string is
7088 already exhausted, and all we want is pop the iterator
7089 stack. */
7090 if (it->current.overlay_string_index >= 0)
7092 /* This is an overlay string, so there's no padding with
7093 spaces, and the number of characters in the string is
7094 where the string ends. */
7095 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7096 goto consider_string_end;
7098 else
7100 /* Not an overlay string. There could be padding, so test
7101 against it->end_charpos . */
7102 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7103 goto consider_string_end;
7105 if (it->cmp_it.id >= 0)
7107 int i;
7109 if (! it->bidi_p)
7111 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7112 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7113 if (it->cmp_it.to < it->cmp_it.nglyphs)
7114 it->cmp_it.from = it->cmp_it.to;
7115 else
7117 it->cmp_it.id = -1;
7118 composition_compute_stop_pos (&it->cmp_it,
7119 IT_STRING_CHARPOS (*it),
7120 IT_STRING_BYTEPOS (*it),
7121 it->end_charpos, it->string);
7124 else if (! it->cmp_it.reversed_p)
7126 for (i = 0; i < it->cmp_it.nchars; i++)
7127 bidi_move_to_visually_next (&it->bidi_it);
7128 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7129 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7131 if (it->cmp_it.to < it->cmp_it.nglyphs)
7132 it->cmp_it.from = it->cmp_it.to;
7133 else
7135 ptrdiff_t stop = it->end_charpos;
7136 if (it->bidi_it.scan_dir < 0)
7137 stop = -1;
7138 composition_compute_stop_pos (&it->cmp_it,
7139 IT_STRING_CHARPOS (*it),
7140 IT_STRING_BYTEPOS (*it), stop,
7141 it->string);
7144 else
7146 for (i = 0; i < it->cmp_it.nchars; i++)
7147 bidi_move_to_visually_next (&it->bidi_it);
7148 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7149 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7150 if (it->cmp_it.from > 0)
7151 it->cmp_it.to = it->cmp_it.from;
7152 else
7154 ptrdiff_t stop = it->end_charpos;
7155 if (it->bidi_it.scan_dir < 0)
7156 stop = -1;
7157 composition_compute_stop_pos (&it->cmp_it,
7158 IT_STRING_CHARPOS (*it),
7159 IT_STRING_BYTEPOS (*it), stop,
7160 it->string);
7164 else
7166 if (!it->bidi_p
7167 /* If the string position is beyond string's end, it
7168 means next_element_from_string is padding the string
7169 with blanks, in which case we bypass the bidi
7170 iterator, because it cannot deal with such virtual
7171 characters. */
7172 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7174 IT_STRING_BYTEPOS (*it) += it->len;
7175 IT_STRING_CHARPOS (*it) += 1;
7177 else
7179 int prev_scan_dir = it->bidi_it.scan_dir;
7181 bidi_move_to_visually_next (&it->bidi_it);
7182 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7183 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7184 if (prev_scan_dir != it->bidi_it.scan_dir)
7186 ptrdiff_t stop = it->end_charpos;
7188 if (it->bidi_it.scan_dir < 0)
7189 stop = -1;
7190 composition_compute_stop_pos (&it->cmp_it,
7191 IT_STRING_CHARPOS (*it),
7192 IT_STRING_BYTEPOS (*it), stop,
7193 it->string);
7198 consider_string_end:
7200 if (it->current.overlay_string_index >= 0)
7202 /* IT->string is an overlay string. Advance to the
7203 next, if there is one. */
7204 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7206 it->ellipsis_p = 0;
7207 next_overlay_string (it);
7208 if (it->ellipsis_p)
7209 setup_for_ellipsis (it, 0);
7212 else
7214 /* IT->string is not an overlay string. If we reached
7215 its end, and there is something on IT->stack, proceed
7216 with what is on the stack. This can be either another
7217 string, this time an overlay string, or a buffer. */
7218 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7219 && it->sp > 0)
7221 pop_it (it);
7222 if (it->method == GET_FROM_STRING)
7223 goto consider_string_end;
7226 break;
7228 case GET_FROM_IMAGE:
7229 case GET_FROM_STRETCH:
7230 /* The position etc with which we have to proceed are on
7231 the stack. The position may be at the end of a string,
7232 if the `display' property takes up the whole string. */
7233 xassert (it->sp > 0);
7234 pop_it (it);
7235 if (it->method == GET_FROM_STRING)
7236 goto consider_string_end;
7237 break;
7239 default:
7240 /* There are no other methods defined, so this should be a bug. */
7241 abort ();
7244 xassert (it->method != GET_FROM_STRING
7245 || (STRINGP (it->string)
7246 && IT_STRING_CHARPOS (*it) >= 0));
7249 /* Load IT's display element fields with information about the next
7250 display element which comes from a display table entry or from the
7251 result of translating a control character to one of the forms `^C'
7252 or `\003'.
7254 IT->dpvec holds the glyphs to return as characters.
7255 IT->saved_face_id holds the face id before the display vector--it
7256 is restored into IT->face_id in set_iterator_to_next. */
7258 static int
7259 next_element_from_display_vector (struct it *it)
7261 Lisp_Object gc;
7263 /* Precondition. */
7264 xassert (it->dpvec && it->current.dpvec_index >= 0);
7266 it->face_id = it->saved_face_id;
7268 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7269 That seemed totally bogus - so I changed it... */
7270 gc = it->dpvec[it->current.dpvec_index];
7272 if (GLYPH_CODE_P (gc))
7274 it->c = GLYPH_CODE_CHAR (gc);
7275 it->len = CHAR_BYTES (it->c);
7277 /* The entry may contain a face id to use. Such a face id is
7278 the id of a Lisp face, not a realized face. A face id of
7279 zero means no face is specified. */
7280 if (it->dpvec_face_id >= 0)
7281 it->face_id = it->dpvec_face_id;
7282 else
7284 int lface_id = GLYPH_CODE_FACE (gc);
7285 if (lface_id > 0)
7286 it->face_id = merge_faces (it->f, Qt, lface_id,
7287 it->saved_face_id);
7290 else
7291 /* Display table entry is invalid. Return a space. */
7292 it->c = ' ', it->len = 1;
7294 /* Don't change position and object of the iterator here. They are
7295 still the values of the character that had this display table
7296 entry or was translated, and that's what we want. */
7297 it->what = IT_CHARACTER;
7298 return 1;
7301 /* Get the first element of string/buffer in the visual order, after
7302 being reseated to a new position in a string or a buffer. */
7303 static void
7304 get_visually_first_element (struct it *it)
7306 int string_p = STRINGP (it->string) || it->s;
7307 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7308 ptrdiff_t bob = (string_p ? 0 : BEGV);
7310 if (STRINGP (it->string))
7312 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7313 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7315 else
7317 it->bidi_it.charpos = IT_CHARPOS (*it);
7318 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7321 if (it->bidi_it.charpos == eob)
7323 /* Nothing to do, but reset the FIRST_ELT flag, like
7324 bidi_paragraph_init does, because we are not going to
7325 call it. */
7326 it->bidi_it.first_elt = 0;
7328 else if (it->bidi_it.charpos == bob
7329 || (!string_p
7330 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7331 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7333 /* If we are at the beginning of a line/string, we can produce
7334 the next element right away. */
7335 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7336 bidi_move_to_visually_next (&it->bidi_it);
7338 else
7340 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7342 /* We need to prime the bidi iterator starting at the line's or
7343 string's beginning, before we will be able to produce the
7344 next element. */
7345 if (string_p)
7346 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7347 else
7349 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7350 -1);
7351 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7353 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7356 /* Now return to buffer/string position where we were asked
7357 to get the next display element, and produce that. */
7358 bidi_move_to_visually_next (&it->bidi_it);
7360 while (it->bidi_it.bytepos != orig_bytepos
7361 && it->bidi_it.charpos < eob);
7364 /* Adjust IT's position information to where we ended up. */
7365 if (STRINGP (it->string))
7367 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7368 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7370 else
7372 IT_CHARPOS (*it) = it->bidi_it.charpos;
7373 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7376 if (STRINGP (it->string) || !it->s)
7378 ptrdiff_t stop, charpos, bytepos;
7380 if (STRINGP (it->string))
7382 xassert (!it->s);
7383 stop = SCHARS (it->string);
7384 if (stop > it->end_charpos)
7385 stop = it->end_charpos;
7386 charpos = IT_STRING_CHARPOS (*it);
7387 bytepos = IT_STRING_BYTEPOS (*it);
7389 else
7391 stop = it->end_charpos;
7392 charpos = IT_CHARPOS (*it);
7393 bytepos = IT_BYTEPOS (*it);
7395 if (it->bidi_it.scan_dir < 0)
7396 stop = -1;
7397 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7398 it->string);
7402 /* Load IT with the next display element from Lisp string IT->string.
7403 IT->current.string_pos is the current position within the string.
7404 If IT->current.overlay_string_index >= 0, the Lisp string is an
7405 overlay string. */
7407 static int
7408 next_element_from_string (struct it *it)
7410 struct text_pos position;
7412 xassert (STRINGP (it->string));
7413 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7414 xassert (IT_STRING_CHARPOS (*it) >= 0);
7415 position = it->current.string_pos;
7417 /* With bidi reordering, the character to display might not be the
7418 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7419 that we were reseat()ed to a new string, whose paragraph
7420 direction is not known. */
7421 if (it->bidi_p && it->bidi_it.first_elt)
7423 get_visually_first_element (it);
7424 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7427 /* Time to check for invisible text? */
7428 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7430 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7432 if (!(!it->bidi_p
7433 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7434 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7436 /* With bidi non-linear iteration, we could find
7437 ourselves far beyond the last computed stop_charpos,
7438 with several other stop positions in between that we
7439 missed. Scan them all now, in buffer's logical
7440 order, until we find and handle the last stop_charpos
7441 that precedes our current position. */
7442 handle_stop_backwards (it, it->stop_charpos);
7443 return GET_NEXT_DISPLAY_ELEMENT (it);
7445 else
7447 if (it->bidi_p)
7449 /* Take note of the stop position we just moved
7450 across, for when we will move back across it. */
7451 it->prev_stop = it->stop_charpos;
7452 /* If we are at base paragraph embedding level, take
7453 note of the last stop position seen at this
7454 level. */
7455 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7456 it->base_level_stop = it->stop_charpos;
7458 handle_stop (it);
7460 /* Since a handler may have changed IT->method, we must
7461 recurse here. */
7462 return GET_NEXT_DISPLAY_ELEMENT (it);
7465 else if (it->bidi_p
7466 /* If we are before prev_stop, we may have overstepped
7467 on our way backwards a stop_pos, and if so, we need
7468 to handle that stop_pos. */
7469 && IT_STRING_CHARPOS (*it) < it->prev_stop
7470 /* We can sometimes back up for reasons that have nothing
7471 to do with bidi reordering. E.g., compositions. The
7472 code below is only needed when we are above the base
7473 embedding level, so test for that explicitly. */
7474 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7476 /* If we lost track of base_level_stop, we have no better
7477 place for handle_stop_backwards to start from than string
7478 beginning. This happens, e.g., when we were reseated to
7479 the previous screenful of text by vertical-motion. */
7480 if (it->base_level_stop <= 0
7481 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7482 it->base_level_stop = 0;
7483 handle_stop_backwards (it, it->base_level_stop);
7484 return GET_NEXT_DISPLAY_ELEMENT (it);
7488 if (it->current.overlay_string_index >= 0)
7490 /* Get the next character from an overlay string. In overlay
7491 strings, there is no field width or padding with spaces to
7492 do. */
7493 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7495 it->what = IT_EOB;
7496 return 0;
7498 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7499 IT_STRING_BYTEPOS (*it),
7500 it->bidi_it.scan_dir < 0
7501 ? -1
7502 : SCHARS (it->string))
7503 && next_element_from_composition (it))
7505 return 1;
7507 else if (STRING_MULTIBYTE (it->string))
7509 const unsigned char *s = (SDATA (it->string)
7510 + IT_STRING_BYTEPOS (*it));
7511 it->c = string_char_and_length (s, &it->len);
7513 else
7515 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7516 it->len = 1;
7519 else
7521 /* Get the next character from a Lisp string that is not an
7522 overlay string. Such strings come from the mode line, for
7523 example. We may have to pad with spaces, or truncate the
7524 string. See also next_element_from_c_string. */
7525 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7527 it->what = IT_EOB;
7528 return 0;
7530 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7532 /* Pad with spaces. */
7533 it->c = ' ', it->len = 1;
7534 CHARPOS (position) = BYTEPOS (position) = -1;
7536 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7537 IT_STRING_BYTEPOS (*it),
7538 it->bidi_it.scan_dir < 0
7539 ? -1
7540 : it->string_nchars)
7541 && next_element_from_composition (it))
7543 return 1;
7545 else if (STRING_MULTIBYTE (it->string))
7547 const unsigned char *s = (SDATA (it->string)
7548 + IT_STRING_BYTEPOS (*it));
7549 it->c = string_char_and_length (s, &it->len);
7551 else
7553 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7554 it->len = 1;
7558 /* Record what we have and where it came from. */
7559 it->what = IT_CHARACTER;
7560 it->object = it->string;
7561 it->position = position;
7562 return 1;
7566 /* Load IT with next display element from C string IT->s.
7567 IT->string_nchars is the maximum number of characters to return
7568 from the string. IT->end_charpos may be greater than
7569 IT->string_nchars when this function is called, in which case we
7570 may have to return padding spaces. Value is zero if end of string
7571 reached, including padding spaces. */
7573 static int
7574 next_element_from_c_string (struct it *it)
7576 int success_p = 1;
7578 xassert (it->s);
7579 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7580 it->what = IT_CHARACTER;
7581 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7582 it->object = Qnil;
7584 /* With bidi reordering, the character to display might not be the
7585 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7586 we were reseated to a new string, whose paragraph direction is
7587 not known. */
7588 if (it->bidi_p && it->bidi_it.first_elt)
7589 get_visually_first_element (it);
7591 /* IT's position can be greater than IT->string_nchars in case a
7592 field width or precision has been specified when the iterator was
7593 initialized. */
7594 if (IT_CHARPOS (*it) >= it->end_charpos)
7596 /* End of the game. */
7597 it->what = IT_EOB;
7598 success_p = 0;
7600 else if (IT_CHARPOS (*it) >= it->string_nchars)
7602 /* Pad with spaces. */
7603 it->c = ' ', it->len = 1;
7604 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7606 else if (it->multibyte_p)
7607 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7608 else
7609 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7611 return success_p;
7615 /* Set up IT to return characters from an ellipsis, if appropriate.
7616 The definition of the ellipsis glyphs may come from a display table
7617 entry. This function fills IT with the first glyph from the
7618 ellipsis if an ellipsis is to be displayed. */
7620 static int
7621 next_element_from_ellipsis (struct it *it)
7623 if (it->selective_display_ellipsis_p)
7624 setup_for_ellipsis (it, it->len);
7625 else
7627 /* The face at the current position may be different from the
7628 face we find after the invisible text. Remember what it
7629 was in IT->saved_face_id, and signal that it's there by
7630 setting face_before_selective_p. */
7631 it->saved_face_id = it->face_id;
7632 it->method = GET_FROM_BUFFER;
7633 it->object = it->w->buffer;
7634 reseat_at_next_visible_line_start (it, 1);
7635 it->face_before_selective_p = 1;
7638 return GET_NEXT_DISPLAY_ELEMENT (it);
7642 /* Deliver an image display element. The iterator IT is already
7643 filled with image information (done in handle_display_prop). Value
7644 is always 1. */
7647 static int
7648 next_element_from_image (struct it *it)
7650 it->what = IT_IMAGE;
7651 it->ignore_overlay_strings_at_pos_p = 0;
7652 return 1;
7656 /* Fill iterator IT with next display element from a stretch glyph
7657 property. IT->object is the value of the text property. Value is
7658 always 1. */
7660 static int
7661 next_element_from_stretch (struct it *it)
7663 it->what = IT_STRETCH;
7664 return 1;
7667 /* Scan backwards from IT's current position until we find a stop
7668 position, or until BEGV. This is called when we find ourself
7669 before both the last known prev_stop and base_level_stop while
7670 reordering bidirectional text. */
7672 static void
7673 compute_stop_pos_backwards (struct it *it)
7675 const int SCAN_BACK_LIMIT = 1000;
7676 struct text_pos pos;
7677 struct display_pos save_current = it->current;
7678 struct text_pos save_position = it->position;
7679 ptrdiff_t charpos = IT_CHARPOS (*it);
7680 ptrdiff_t where_we_are = charpos;
7681 ptrdiff_t save_stop_pos = it->stop_charpos;
7682 ptrdiff_t save_end_pos = it->end_charpos;
7684 xassert (NILP (it->string) && !it->s);
7685 xassert (it->bidi_p);
7686 it->bidi_p = 0;
7689 it->end_charpos = min (charpos + 1, ZV);
7690 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7691 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7692 reseat_1 (it, pos, 0);
7693 compute_stop_pos (it);
7694 /* We must advance forward, right? */
7695 if (it->stop_charpos <= charpos)
7696 abort ();
7698 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7700 if (it->stop_charpos <= where_we_are)
7701 it->prev_stop = it->stop_charpos;
7702 else
7703 it->prev_stop = BEGV;
7704 it->bidi_p = 1;
7705 it->current = save_current;
7706 it->position = save_position;
7707 it->stop_charpos = save_stop_pos;
7708 it->end_charpos = save_end_pos;
7711 /* Scan forward from CHARPOS in the current buffer/string, until we
7712 find a stop position > current IT's position. Then handle the stop
7713 position before that. This is called when we bump into a stop
7714 position while reordering bidirectional text. CHARPOS should be
7715 the last previously processed stop_pos (or BEGV/0, if none were
7716 processed yet) whose position is less that IT's current
7717 position. */
7719 static void
7720 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7722 int bufp = !STRINGP (it->string);
7723 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7724 struct display_pos save_current = it->current;
7725 struct text_pos save_position = it->position;
7726 struct text_pos pos1;
7727 ptrdiff_t next_stop;
7729 /* Scan in strict logical order. */
7730 xassert (it->bidi_p);
7731 it->bidi_p = 0;
7734 it->prev_stop = charpos;
7735 if (bufp)
7737 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7738 reseat_1 (it, pos1, 0);
7740 else
7741 it->current.string_pos = string_pos (charpos, it->string);
7742 compute_stop_pos (it);
7743 /* We must advance forward, right? */
7744 if (it->stop_charpos <= it->prev_stop)
7745 abort ();
7746 charpos = it->stop_charpos;
7748 while (charpos <= where_we_are);
7750 it->bidi_p = 1;
7751 it->current = save_current;
7752 it->position = save_position;
7753 next_stop = it->stop_charpos;
7754 it->stop_charpos = it->prev_stop;
7755 handle_stop (it);
7756 it->stop_charpos = next_stop;
7759 /* Load IT with the next display element from current_buffer. Value
7760 is zero if end of buffer reached. IT->stop_charpos is the next
7761 position at which to stop and check for text properties or buffer
7762 end. */
7764 static int
7765 next_element_from_buffer (struct it *it)
7767 int success_p = 1;
7769 xassert (IT_CHARPOS (*it) >= BEGV);
7770 xassert (NILP (it->string) && !it->s);
7771 xassert (!it->bidi_p
7772 || (EQ (it->bidi_it.string.lstring, Qnil)
7773 && it->bidi_it.string.s == NULL));
7775 /* With bidi reordering, the character to display might not be the
7776 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7777 we were reseat()ed to a new buffer position, which is potentially
7778 a different paragraph. */
7779 if (it->bidi_p && it->bidi_it.first_elt)
7781 get_visually_first_element (it);
7782 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7785 if (IT_CHARPOS (*it) >= it->stop_charpos)
7787 if (IT_CHARPOS (*it) >= it->end_charpos)
7789 int overlay_strings_follow_p;
7791 /* End of the game, except when overlay strings follow that
7792 haven't been returned yet. */
7793 if (it->overlay_strings_at_end_processed_p)
7794 overlay_strings_follow_p = 0;
7795 else
7797 it->overlay_strings_at_end_processed_p = 1;
7798 overlay_strings_follow_p = get_overlay_strings (it, 0);
7801 if (overlay_strings_follow_p)
7802 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7803 else
7805 it->what = IT_EOB;
7806 it->position = it->current.pos;
7807 success_p = 0;
7810 else if (!(!it->bidi_p
7811 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7812 || IT_CHARPOS (*it) == it->stop_charpos))
7814 /* With bidi non-linear iteration, we could find ourselves
7815 far beyond the last computed stop_charpos, with several
7816 other stop positions in between that we missed. Scan
7817 them all now, in buffer's logical order, until we find
7818 and handle the last stop_charpos that precedes our
7819 current position. */
7820 handle_stop_backwards (it, it->stop_charpos);
7821 return GET_NEXT_DISPLAY_ELEMENT (it);
7823 else
7825 if (it->bidi_p)
7827 /* Take note of the stop position we just moved across,
7828 for when we will move back across it. */
7829 it->prev_stop = it->stop_charpos;
7830 /* If we are at base paragraph embedding level, take
7831 note of the last stop position seen at this
7832 level. */
7833 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7834 it->base_level_stop = it->stop_charpos;
7836 handle_stop (it);
7837 return GET_NEXT_DISPLAY_ELEMENT (it);
7840 else if (it->bidi_p
7841 /* If we are before prev_stop, we may have overstepped on
7842 our way backwards a stop_pos, and if so, we need to
7843 handle that stop_pos. */
7844 && IT_CHARPOS (*it) < it->prev_stop
7845 /* We can sometimes back up for reasons that have nothing
7846 to do with bidi reordering. E.g., compositions. The
7847 code below is only needed when we are above the base
7848 embedding level, so test for that explicitly. */
7849 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7851 if (it->base_level_stop <= 0
7852 || IT_CHARPOS (*it) < it->base_level_stop)
7854 /* If we lost track of base_level_stop, we need to find
7855 prev_stop by looking backwards. This happens, e.g., when
7856 we were reseated to the previous screenful of text by
7857 vertical-motion. */
7858 it->base_level_stop = BEGV;
7859 compute_stop_pos_backwards (it);
7860 handle_stop_backwards (it, it->prev_stop);
7862 else
7863 handle_stop_backwards (it, it->base_level_stop);
7864 return GET_NEXT_DISPLAY_ELEMENT (it);
7866 else
7868 /* No face changes, overlays etc. in sight, so just return a
7869 character from current_buffer. */
7870 unsigned char *p;
7871 ptrdiff_t stop;
7873 /* Maybe run the redisplay end trigger hook. Performance note:
7874 This doesn't seem to cost measurable time. */
7875 if (it->redisplay_end_trigger_charpos
7876 && it->glyph_row
7877 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7878 run_redisplay_end_trigger_hook (it);
7880 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7881 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7882 stop)
7883 && next_element_from_composition (it))
7885 return 1;
7888 /* Get the next character, maybe multibyte. */
7889 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7890 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7891 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7892 else
7893 it->c = *p, it->len = 1;
7895 /* Record what we have and where it came from. */
7896 it->what = IT_CHARACTER;
7897 it->object = it->w->buffer;
7898 it->position = it->current.pos;
7900 /* Normally we return the character found above, except when we
7901 really want to return an ellipsis for selective display. */
7902 if (it->selective)
7904 if (it->c == '\n')
7906 /* A value of selective > 0 means hide lines indented more
7907 than that number of columns. */
7908 if (it->selective > 0
7909 && IT_CHARPOS (*it) + 1 < ZV
7910 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7911 IT_BYTEPOS (*it) + 1,
7912 it->selective))
7914 success_p = next_element_from_ellipsis (it);
7915 it->dpvec_char_len = -1;
7918 else if (it->c == '\r' && it->selective == -1)
7920 /* A value of selective == -1 means that everything from the
7921 CR to the end of the line is invisible, with maybe an
7922 ellipsis displayed for it. */
7923 success_p = next_element_from_ellipsis (it);
7924 it->dpvec_char_len = -1;
7929 /* Value is zero if end of buffer reached. */
7930 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7931 return success_p;
7935 /* Run the redisplay end trigger hook for IT. */
7937 static void
7938 run_redisplay_end_trigger_hook (struct it *it)
7940 Lisp_Object args[3];
7942 /* IT->glyph_row should be non-null, i.e. we should be actually
7943 displaying something, or otherwise we should not run the hook. */
7944 xassert (it->glyph_row);
7946 /* Set up hook arguments. */
7947 args[0] = Qredisplay_end_trigger_functions;
7948 args[1] = it->window;
7949 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7950 it->redisplay_end_trigger_charpos = 0;
7952 /* Since we are *trying* to run these functions, don't try to run
7953 them again, even if they get an error. */
7954 it->w->redisplay_end_trigger = Qnil;
7955 Frun_hook_with_args (3, args);
7957 /* Notice if it changed the face of the character we are on. */
7958 handle_face_prop (it);
7962 /* Deliver a composition display element. Unlike the other
7963 next_element_from_XXX, this function is not registered in the array
7964 get_next_element[]. It is called from next_element_from_buffer and
7965 next_element_from_string when necessary. */
7967 static int
7968 next_element_from_composition (struct it *it)
7970 it->what = IT_COMPOSITION;
7971 it->len = it->cmp_it.nbytes;
7972 if (STRINGP (it->string))
7974 if (it->c < 0)
7976 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7977 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7978 return 0;
7980 it->position = it->current.string_pos;
7981 it->object = it->string;
7982 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7983 IT_STRING_BYTEPOS (*it), it->string);
7985 else
7987 if (it->c < 0)
7989 IT_CHARPOS (*it) += it->cmp_it.nchars;
7990 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7991 if (it->bidi_p)
7993 if (it->bidi_it.new_paragraph)
7994 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7995 /* Resync the bidi iterator with IT's new position.
7996 FIXME: this doesn't support bidirectional text. */
7997 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7998 bidi_move_to_visually_next (&it->bidi_it);
8000 return 0;
8002 it->position = it->current.pos;
8003 it->object = it->w->buffer;
8004 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8005 IT_BYTEPOS (*it), Qnil);
8007 return 1;
8012 /***********************************************************************
8013 Moving an iterator without producing glyphs
8014 ***********************************************************************/
8016 /* Check if iterator is at a position corresponding to a valid buffer
8017 position after some move_it_ call. */
8019 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8020 ((it)->method == GET_FROM_STRING \
8021 ? IT_STRING_CHARPOS (*it) == 0 \
8022 : 1)
8025 /* Move iterator IT to a specified buffer or X position within one
8026 line on the display without producing glyphs.
8028 OP should be a bit mask including some or all of these bits:
8029 MOVE_TO_X: Stop upon reaching x-position TO_X.
8030 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8031 Regardless of OP's value, stop upon reaching the end of the display line.
8033 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8034 This means, in particular, that TO_X includes window's horizontal
8035 scroll amount.
8037 The return value has several possible values that
8038 say what condition caused the scan to stop:
8040 MOVE_POS_MATCH_OR_ZV
8041 - when TO_POS or ZV was reached.
8043 MOVE_X_REACHED
8044 -when TO_X was reached before TO_POS or ZV were reached.
8046 MOVE_LINE_CONTINUED
8047 - when we reached the end of the display area and the line must
8048 be continued.
8050 MOVE_LINE_TRUNCATED
8051 - when we reached the end of the display area and the line is
8052 truncated.
8054 MOVE_NEWLINE_OR_CR
8055 - when we stopped at a line end, i.e. a newline or a CR and selective
8056 display is on. */
8058 static enum move_it_result
8059 move_it_in_display_line_to (struct it *it,
8060 ptrdiff_t to_charpos, int to_x,
8061 enum move_operation_enum op)
8063 enum move_it_result result = MOVE_UNDEFINED;
8064 struct glyph_row *saved_glyph_row;
8065 struct it wrap_it, atpos_it, atx_it, ppos_it;
8066 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8067 void *ppos_data = NULL;
8068 int may_wrap = 0;
8069 enum it_method prev_method = it->method;
8070 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8071 int saw_smaller_pos = prev_pos < to_charpos;
8073 /* Don't produce glyphs in produce_glyphs. */
8074 saved_glyph_row = it->glyph_row;
8075 it->glyph_row = NULL;
8077 /* Use wrap_it to save a copy of IT wherever a word wrap could
8078 occur. Use atpos_it to save a copy of IT at the desired buffer
8079 position, if found, so that we can scan ahead and check if the
8080 word later overshoots the window edge. Use atx_it similarly, for
8081 pixel positions. */
8082 wrap_it.sp = -1;
8083 atpos_it.sp = -1;
8084 atx_it.sp = -1;
8086 /* Use ppos_it under bidi reordering to save a copy of IT for the
8087 position > CHARPOS that is the closest to CHARPOS. We restore
8088 that position in IT when we have scanned the entire display line
8089 without finding a match for CHARPOS and all the character
8090 positions are greater than CHARPOS. */
8091 if (it->bidi_p)
8093 SAVE_IT (ppos_it, *it, ppos_data);
8094 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8095 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8096 SAVE_IT (ppos_it, *it, ppos_data);
8099 #define BUFFER_POS_REACHED_P() \
8100 ((op & MOVE_TO_POS) != 0 \
8101 && BUFFERP (it->object) \
8102 && (IT_CHARPOS (*it) == to_charpos \
8103 || ((!it->bidi_p \
8104 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8105 && IT_CHARPOS (*it) > to_charpos) \
8106 || (it->what == IT_COMPOSITION \
8107 && ((IT_CHARPOS (*it) > to_charpos \
8108 && to_charpos >= it->cmp_it.charpos) \
8109 || (IT_CHARPOS (*it) < to_charpos \
8110 && to_charpos <= it->cmp_it.charpos)))) \
8111 && (it->method == GET_FROM_BUFFER \
8112 || (it->method == GET_FROM_DISPLAY_VECTOR \
8113 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8115 /* If there's a line-/wrap-prefix, handle it. */
8116 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8117 && it->current_y < it->last_visible_y)
8118 handle_line_prefix (it);
8120 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8121 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8123 while (1)
8125 int x, i, ascent = 0, descent = 0;
8127 /* Utility macro to reset an iterator with x, ascent, and descent. */
8128 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8129 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8130 (IT)->max_descent = descent)
8132 /* Stop if we move beyond TO_CHARPOS (after an image or a
8133 display string or stretch glyph). */
8134 if ((op & MOVE_TO_POS) != 0
8135 && BUFFERP (it->object)
8136 && it->method == GET_FROM_BUFFER
8137 && (((!it->bidi_p
8138 /* When the iterator is at base embedding level, we
8139 are guaranteed that characters are delivered for
8140 display in strictly increasing order of their
8141 buffer positions. */
8142 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8143 && IT_CHARPOS (*it) > to_charpos)
8144 || (it->bidi_p
8145 && (prev_method == GET_FROM_IMAGE
8146 || prev_method == GET_FROM_STRETCH
8147 || prev_method == GET_FROM_STRING)
8148 /* Passed TO_CHARPOS from left to right. */
8149 && ((prev_pos < to_charpos
8150 && IT_CHARPOS (*it) > to_charpos)
8151 /* Passed TO_CHARPOS from right to left. */
8152 || (prev_pos > to_charpos
8153 && IT_CHARPOS (*it) < to_charpos)))))
8155 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8157 result = MOVE_POS_MATCH_OR_ZV;
8158 break;
8160 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8161 /* If wrap_it is valid, the current position might be in a
8162 word that is wrapped. So, save the iterator in
8163 atpos_it and continue to see if wrapping happens. */
8164 SAVE_IT (atpos_it, *it, atpos_data);
8167 /* Stop when ZV reached.
8168 We used to stop here when TO_CHARPOS reached as well, but that is
8169 too soon if this glyph does not fit on this line. So we handle it
8170 explicitly below. */
8171 if (!get_next_display_element (it))
8173 result = MOVE_POS_MATCH_OR_ZV;
8174 break;
8177 if (it->line_wrap == TRUNCATE)
8179 if (BUFFER_POS_REACHED_P ())
8181 result = MOVE_POS_MATCH_OR_ZV;
8182 break;
8185 else
8187 if (it->line_wrap == WORD_WRAP)
8189 if (IT_DISPLAYING_WHITESPACE (it))
8190 may_wrap = 1;
8191 else if (may_wrap)
8193 /* We have reached a glyph that follows one or more
8194 whitespace characters. If the position is
8195 already found, we are done. */
8196 if (atpos_it.sp >= 0)
8198 RESTORE_IT (it, &atpos_it, atpos_data);
8199 result = MOVE_POS_MATCH_OR_ZV;
8200 goto done;
8202 if (atx_it.sp >= 0)
8204 RESTORE_IT (it, &atx_it, atx_data);
8205 result = MOVE_X_REACHED;
8206 goto done;
8208 /* Otherwise, we can wrap here. */
8209 SAVE_IT (wrap_it, *it, wrap_data);
8210 may_wrap = 0;
8215 /* Remember the line height for the current line, in case
8216 the next element doesn't fit on the line. */
8217 ascent = it->max_ascent;
8218 descent = it->max_descent;
8220 /* The call to produce_glyphs will get the metrics of the
8221 display element IT is loaded with. Record the x-position
8222 before this display element, in case it doesn't fit on the
8223 line. */
8224 x = it->current_x;
8226 PRODUCE_GLYPHS (it);
8228 if (it->area != TEXT_AREA)
8230 prev_method = it->method;
8231 if (it->method == GET_FROM_BUFFER)
8232 prev_pos = IT_CHARPOS (*it);
8233 set_iterator_to_next (it, 1);
8234 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8235 SET_TEXT_POS (this_line_min_pos,
8236 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8237 if (it->bidi_p
8238 && (op & MOVE_TO_POS)
8239 && IT_CHARPOS (*it) > to_charpos
8240 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8241 SAVE_IT (ppos_it, *it, ppos_data);
8242 continue;
8245 /* The number of glyphs we get back in IT->nglyphs will normally
8246 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8247 character on a terminal frame, or (iii) a line end. For the
8248 second case, IT->nglyphs - 1 padding glyphs will be present.
8249 (On X frames, there is only one glyph produced for a
8250 composite character.)
8252 The behavior implemented below means, for continuation lines,
8253 that as many spaces of a TAB as fit on the current line are
8254 displayed there. For terminal frames, as many glyphs of a
8255 multi-glyph character are displayed in the current line, too.
8256 This is what the old redisplay code did, and we keep it that
8257 way. Under X, the whole shape of a complex character must
8258 fit on the line or it will be completely displayed in the
8259 next line.
8261 Note that both for tabs and padding glyphs, all glyphs have
8262 the same width. */
8263 if (it->nglyphs)
8265 /* More than one glyph or glyph doesn't fit on line. All
8266 glyphs have the same width. */
8267 int single_glyph_width = it->pixel_width / it->nglyphs;
8268 int new_x;
8269 int x_before_this_char = x;
8270 int hpos_before_this_char = it->hpos;
8272 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8274 new_x = x + single_glyph_width;
8276 /* We want to leave anything reaching TO_X to the caller. */
8277 if ((op & MOVE_TO_X) && new_x > to_x)
8279 if (BUFFER_POS_REACHED_P ())
8281 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8282 goto buffer_pos_reached;
8283 if (atpos_it.sp < 0)
8285 SAVE_IT (atpos_it, *it, atpos_data);
8286 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8289 else
8291 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8293 it->current_x = x;
8294 result = MOVE_X_REACHED;
8295 break;
8297 if (atx_it.sp < 0)
8299 SAVE_IT (atx_it, *it, atx_data);
8300 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8305 if (/* Lines are continued. */
8306 it->line_wrap != TRUNCATE
8307 && (/* And glyph doesn't fit on the line. */
8308 new_x > it->last_visible_x
8309 /* Or it fits exactly and we're on a window
8310 system frame. */
8311 || (new_x == it->last_visible_x
8312 && FRAME_WINDOW_P (it->f))))
8314 if (/* IT->hpos == 0 means the very first glyph
8315 doesn't fit on the line, e.g. a wide image. */
8316 it->hpos == 0
8317 || (new_x == it->last_visible_x
8318 && FRAME_WINDOW_P (it->f)))
8320 ++it->hpos;
8321 it->current_x = new_x;
8323 /* The character's last glyph just barely fits
8324 in this row. */
8325 if (i == it->nglyphs - 1)
8327 /* If this is the destination position,
8328 return a position *before* it in this row,
8329 now that we know it fits in this row. */
8330 if (BUFFER_POS_REACHED_P ())
8332 if (it->line_wrap != WORD_WRAP
8333 || wrap_it.sp < 0)
8335 it->hpos = hpos_before_this_char;
8336 it->current_x = x_before_this_char;
8337 result = MOVE_POS_MATCH_OR_ZV;
8338 break;
8340 if (it->line_wrap == WORD_WRAP
8341 && atpos_it.sp < 0)
8343 SAVE_IT (atpos_it, *it, atpos_data);
8344 atpos_it.current_x = x_before_this_char;
8345 atpos_it.hpos = hpos_before_this_char;
8349 prev_method = it->method;
8350 if (it->method == GET_FROM_BUFFER)
8351 prev_pos = IT_CHARPOS (*it);
8352 set_iterator_to_next (it, 1);
8353 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8354 SET_TEXT_POS (this_line_min_pos,
8355 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8356 /* On graphical terminals, newlines may
8357 "overflow" into the fringe if
8358 overflow-newline-into-fringe is non-nil.
8359 On text-only terminals, newlines may
8360 overflow into the last glyph on the
8361 display line.*/
8362 if (!FRAME_WINDOW_P (it->f)
8363 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8365 if (!get_next_display_element (it))
8367 result = MOVE_POS_MATCH_OR_ZV;
8368 break;
8370 if (BUFFER_POS_REACHED_P ())
8372 if (ITERATOR_AT_END_OF_LINE_P (it))
8373 result = MOVE_POS_MATCH_OR_ZV;
8374 else
8375 result = MOVE_LINE_CONTINUED;
8376 break;
8378 if (ITERATOR_AT_END_OF_LINE_P (it))
8380 result = MOVE_NEWLINE_OR_CR;
8381 break;
8386 else
8387 IT_RESET_X_ASCENT_DESCENT (it);
8389 if (wrap_it.sp >= 0)
8391 RESTORE_IT (it, &wrap_it, wrap_data);
8392 atpos_it.sp = -1;
8393 atx_it.sp = -1;
8396 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8397 IT_CHARPOS (*it)));
8398 result = MOVE_LINE_CONTINUED;
8399 break;
8402 if (BUFFER_POS_REACHED_P ())
8404 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8405 goto buffer_pos_reached;
8406 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8408 SAVE_IT (atpos_it, *it, atpos_data);
8409 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8413 if (new_x > it->first_visible_x)
8415 /* Glyph is visible. Increment number of glyphs that
8416 would be displayed. */
8417 ++it->hpos;
8421 if (result != MOVE_UNDEFINED)
8422 break;
8424 else if (BUFFER_POS_REACHED_P ())
8426 buffer_pos_reached:
8427 IT_RESET_X_ASCENT_DESCENT (it);
8428 result = MOVE_POS_MATCH_OR_ZV;
8429 break;
8431 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8433 /* Stop when TO_X specified and reached. This check is
8434 necessary here because of lines consisting of a line end,
8435 only. The line end will not produce any glyphs and we
8436 would never get MOVE_X_REACHED. */
8437 xassert (it->nglyphs == 0);
8438 result = MOVE_X_REACHED;
8439 break;
8442 /* Is this a line end? If yes, we're done. */
8443 if (ITERATOR_AT_END_OF_LINE_P (it))
8445 /* If we are past TO_CHARPOS, but never saw any character
8446 positions smaller than TO_CHARPOS, return
8447 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8448 did. */
8449 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8451 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8453 if (IT_CHARPOS (ppos_it) < ZV)
8455 RESTORE_IT (it, &ppos_it, ppos_data);
8456 result = MOVE_POS_MATCH_OR_ZV;
8458 else
8459 goto buffer_pos_reached;
8461 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8462 && IT_CHARPOS (*it) > to_charpos)
8463 goto buffer_pos_reached;
8464 else
8465 result = MOVE_NEWLINE_OR_CR;
8467 else
8468 result = MOVE_NEWLINE_OR_CR;
8469 break;
8472 prev_method = it->method;
8473 if (it->method == GET_FROM_BUFFER)
8474 prev_pos = IT_CHARPOS (*it);
8475 /* The current display element has been consumed. Advance
8476 to the next. */
8477 set_iterator_to_next (it, 1);
8478 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8479 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8480 if (IT_CHARPOS (*it) < to_charpos)
8481 saw_smaller_pos = 1;
8482 if (it->bidi_p
8483 && (op & MOVE_TO_POS)
8484 && IT_CHARPOS (*it) >= to_charpos
8485 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8486 SAVE_IT (ppos_it, *it, ppos_data);
8488 /* Stop if lines are truncated and IT's current x-position is
8489 past the right edge of the window now. */
8490 if (it->line_wrap == TRUNCATE
8491 && it->current_x >= it->last_visible_x)
8493 if (!FRAME_WINDOW_P (it->f)
8494 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8496 int at_eob_p = 0;
8498 if ((at_eob_p = !get_next_display_element (it))
8499 || BUFFER_POS_REACHED_P ()
8500 /* If we are past TO_CHARPOS, but never saw any
8501 character positions smaller than TO_CHARPOS,
8502 return MOVE_POS_MATCH_OR_ZV, like the
8503 unidirectional display did. */
8504 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8505 && !saw_smaller_pos
8506 && IT_CHARPOS (*it) > to_charpos))
8508 if (it->bidi_p
8509 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8510 RESTORE_IT (it, &ppos_it, ppos_data);
8511 result = MOVE_POS_MATCH_OR_ZV;
8512 break;
8514 if (ITERATOR_AT_END_OF_LINE_P (it))
8516 result = MOVE_NEWLINE_OR_CR;
8517 break;
8520 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8521 && !saw_smaller_pos
8522 && IT_CHARPOS (*it) > to_charpos)
8524 if (IT_CHARPOS (ppos_it) < ZV)
8525 RESTORE_IT (it, &ppos_it, ppos_data);
8526 result = MOVE_POS_MATCH_OR_ZV;
8527 break;
8529 result = MOVE_LINE_TRUNCATED;
8530 break;
8532 #undef IT_RESET_X_ASCENT_DESCENT
8535 #undef BUFFER_POS_REACHED_P
8537 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8538 restore the saved iterator. */
8539 if (atpos_it.sp >= 0)
8540 RESTORE_IT (it, &atpos_it, atpos_data);
8541 else if (atx_it.sp >= 0)
8542 RESTORE_IT (it, &atx_it, atx_data);
8544 done:
8546 if (atpos_data)
8547 bidi_unshelve_cache (atpos_data, 1);
8548 if (atx_data)
8549 bidi_unshelve_cache (atx_data, 1);
8550 if (wrap_data)
8551 bidi_unshelve_cache (wrap_data, 1);
8552 if (ppos_data)
8553 bidi_unshelve_cache (ppos_data, 1);
8555 /* Restore the iterator settings altered at the beginning of this
8556 function. */
8557 it->glyph_row = saved_glyph_row;
8558 return result;
8561 /* For external use. */
8562 void
8563 move_it_in_display_line (struct it *it,
8564 ptrdiff_t to_charpos, int to_x,
8565 enum move_operation_enum op)
8567 if (it->line_wrap == WORD_WRAP
8568 && (op & MOVE_TO_X))
8570 struct it save_it;
8571 void *save_data = NULL;
8572 int skip;
8574 SAVE_IT (save_it, *it, save_data);
8575 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8576 /* When word-wrap is on, TO_X may lie past the end
8577 of a wrapped line. Then it->current is the
8578 character on the next line, so backtrack to the
8579 space before the wrap point. */
8580 if (skip == MOVE_LINE_CONTINUED)
8582 int prev_x = max (it->current_x - 1, 0);
8583 RESTORE_IT (it, &save_it, save_data);
8584 move_it_in_display_line_to
8585 (it, -1, prev_x, MOVE_TO_X);
8587 else
8588 bidi_unshelve_cache (save_data, 1);
8590 else
8591 move_it_in_display_line_to (it, to_charpos, to_x, op);
8595 /* Move IT forward until it satisfies one or more of the criteria in
8596 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8598 OP is a bit-mask that specifies where to stop, and in particular,
8599 which of those four position arguments makes a difference. See the
8600 description of enum move_operation_enum.
8602 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8603 screen line, this function will set IT to the next position that is
8604 displayed to the right of TO_CHARPOS on the screen. */
8606 void
8607 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8609 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8610 int line_height, line_start_x = 0, reached = 0;
8611 void *backup_data = NULL;
8613 for (;;)
8615 if (op & MOVE_TO_VPOS)
8617 /* If no TO_CHARPOS and no TO_X specified, stop at the
8618 start of the line TO_VPOS. */
8619 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8621 if (it->vpos == to_vpos)
8623 reached = 1;
8624 break;
8626 else
8627 skip = move_it_in_display_line_to (it, -1, -1, 0);
8629 else
8631 /* TO_VPOS >= 0 means stop at TO_X in the line at
8632 TO_VPOS, or at TO_POS, whichever comes first. */
8633 if (it->vpos == to_vpos)
8635 reached = 2;
8636 break;
8639 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8641 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8643 reached = 3;
8644 break;
8646 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8648 /* We have reached TO_X but not in the line we want. */
8649 skip = move_it_in_display_line_to (it, to_charpos,
8650 -1, MOVE_TO_POS);
8651 if (skip == MOVE_POS_MATCH_OR_ZV)
8653 reached = 4;
8654 break;
8659 else if (op & MOVE_TO_Y)
8661 struct it it_backup;
8663 if (it->line_wrap == WORD_WRAP)
8664 SAVE_IT (it_backup, *it, backup_data);
8666 /* TO_Y specified means stop at TO_X in the line containing
8667 TO_Y---or at TO_CHARPOS if this is reached first. The
8668 problem is that we can't really tell whether the line
8669 contains TO_Y before we have completely scanned it, and
8670 this may skip past TO_X. What we do is to first scan to
8671 TO_X.
8673 If TO_X is not specified, use a TO_X of zero. The reason
8674 is to make the outcome of this function more predictable.
8675 If we didn't use TO_X == 0, we would stop at the end of
8676 the line which is probably not what a caller would expect
8677 to happen. */
8678 skip = move_it_in_display_line_to
8679 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8680 (MOVE_TO_X | (op & MOVE_TO_POS)));
8682 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8683 if (skip == MOVE_POS_MATCH_OR_ZV)
8684 reached = 5;
8685 else if (skip == MOVE_X_REACHED)
8687 /* If TO_X was reached, we want to know whether TO_Y is
8688 in the line. We know this is the case if the already
8689 scanned glyphs make the line tall enough. Otherwise,
8690 we must check by scanning the rest of the line. */
8691 line_height = it->max_ascent + it->max_descent;
8692 if (to_y >= it->current_y
8693 && to_y < it->current_y + line_height)
8695 reached = 6;
8696 break;
8698 SAVE_IT (it_backup, *it, backup_data);
8699 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8700 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8701 op & MOVE_TO_POS);
8702 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8703 line_height = it->max_ascent + it->max_descent;
8704 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8706 if (to_y >= it->current_y
8707 && to_y < it->current_y + line_height)
8709 /* If TO_Y is in this line and TO_X was reached
8710 above, we scanned too far. We have to restore
8711 IT's settings to the ones before skipping. But
8712 keep the more accurate values of max_ascent and
8713 max_descent we've found while skipping the rest
8714 of the line, for the sake of callers, such as
8715 pos_visible_p, that need to know the line
8716 height. */
8717 int max_ascent = it->max_ascent;
8718 int max_descent = it->max_descent;
8720 RESTORE_IT (it, &it_backup, backup_data);
8721 it->max_ascent = max_ascent;
8722 it->max_descent = max_descent;
8723 reached = 6;
8725 else
8727 skip = skip2;
8728 if (skip == MOVE_POS_MATCH_OR_ZV)
8729 reached = 7;
8732 else
8734 /* Check whether TO_Y is in this line. */
8735 line_height = it->max_ascent + it->max_descent;
8736 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8738 if (to_y >= it->current_y
8739 && to_y < it->current_y + line_height)
8741 /* When word-wrap is on, TO_X may lie past the end
8742 of a wrapped line. Then it->current is the
8743 character on the next line, so backtrack to the
8744 space before the wrap point. */
8745 if (skip == MOVE_LINE_CONTINUED
8746 && it->line_wrap == WORD_WRAP)
8748 int prev_x = max (it->current_x - 1, 0);
8749 RESTORE_IT (it, &it_backup, backup_data);
8750 skip = move_it_in_display_line_to
8751 (it, -1, prev_x, MOVE_TO_X);
8753 reached = 6;
8757 if (reached)
8758 break;
8760 else if (BUFFERP (it->object)
8761 && (it->method == GET_FROM_BUFFER
8762 || it->method == GET_FROM_STRETCH)
8763 && IT_CHARPOS (*it) >= to_charpos
8764 /* Under bidi iteration, a call to set_iterator_to_next
8765 can scan far beyond to_charpos if the initial
8766 portion of the next line needs to be reordered. In
8767 that case, give move_it_in_display_line_to another
8768 chance below. */
8769 && !(it->bidi_p
8770 && it->bidi_it.scan_dir == -1))
8771 skip = MOVE_POS_MATCH_OR_ZV;
8772 else
8773 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8775 switch (skip)
8777 case MOVE_POS_MATCH_OR_ZV:
8778 reached = 8;
8779 goto out;
8781 case MOVE_NEWLINE_OR_CR:
8782 set_iterator_to_next (it, 1);
8783 it->continuation_lines_width = 0;
8784 break;
8786 case MOVE_LINE_TRUNCATED:
8787 it->continuation_lines_width = 0;
8788 reseat_at_next_visible_line_start (it, 0);
8789 if ((op & MOVE_TO_POS) != 0
8790 && IT_CHARPOS (*it) > to_charpos)
8792 reached = 9;
8793 goto out;
8795 break;
8797 case MOVE_LINE_CONTINUED:
8798 /* For continued lines ending in a tab, some of the glyphs
8799 associated with the tab are displayed on the current
8800 line. Since it->current_x does not include these glyphs,
8801 we use it->last_visible_x instead. */
8802 if (it->c == '\t')
8804 it->continuation_lines_width += it->last_visible_x;
8805 /* When moving by vpos, ensure that the iterator really
8806 advances to the next line (bug#847, bug#969). Fixme:
8807 do we need to do this in other circumstances? */
8808 if (it->current_x != it->last_visible_x
8809 && (op & MOVE_TO_VPOS)
8810 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8812 line_start_x = it->current_x + it->pixel_width
8813 - it->last_visible_x;
8814 set_iterator_to_next (it, 0);
8817 else
8818 it->continuation_lines_width += it->current_x;
8819 break;
8821 default:
8822 abort ();
8825 /* Reset/increment for the next run. */
8826 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8827 it->current_x = line_start_x;
8828 line_start_x = 0;
8829 it->hpos = 0;
8830 it->current_y += it->max_ascent + it->max_descent;
8831 ++it->vpos;
8832 last_height = it->max_ascent + it->max_descent;
8833 last_max_ascent = it->max_ascent;
8834 it->max_ascent = it->max_descent = 0;
8837 out:
8839 /* On text terminals, we may stop at the end of a line in the middle
8840 of a multi-character glyph. If the glyph itself is continued,
8841 i.e. it is actually displayed on the next line, don't treat this
8842 stopping point as valid; move to the next line instead (unless
8843 that brings us offscreen). */
8844 if (!FRAME_WINDOW_P (it->f)
8845 && op & MOVE_TO_POS
8846 && IT_CHARPOS (*it) == to_charpos
8847 && it->what == IT_CHARACTER
8848 && it->nglyphs > 1
8849 && it->line_wrap == WINDOW_WRAP
8850 && it->current_x == it->last_visible_x - 1
8851 && it->c != '\n'
8852 && it->c != '\t'
8853 && it->vpos < XFASTINT (it->w->window_end_vpos))
8855 it->continuation_lines_width += it->current_x;
8856 it->current_x = it->hpos = it->max_ascent = it->max_descent = 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;
8863 if (backup_data)
8864 bidi_unshelve_cache (backup_data, 1);
8866 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8870 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8872 If DY > 0, move IT backward at least that many pixels. DY = 0
8873 means move IT backward to the preceding line start or BEGV. This
8874 function may move over more than DY pixels if IT->current_y - DY
8875 ends up in the middle of a line; in this case IT->current_y will be
8876 set to the top of the line moved to. */
8878 void
8879 move_it_vertically_backward (struct it *it, int dy)
8881 int nlines, h;
8882 struct it it2, it3;
8883 void *it2data = NULL, *it3data = NULL;
8884 ptrdiff_t start_pos;
8886 move_further_back:
8887 xassert (dy >= 0);
8889 start_pos = IT_CHARPOS (*it);
8891 /* Estimate how many newlines we must move back. */
8892 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8894 /* Set the iterator's position that many lines back. */
8895 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8896 back_to_previous_visible_line_start (it);
8898 /* Reseat the iterator here. When moving backward, we don't want
8899 reseat to skip forward over invisible text, set up the iterator
8900 to deliver from overlay strings at the new position etc. So,
8901 use reseat_1 here. */
8902 reseat_1 (it, it->current.pos, 1);
8904 /* We are now surely at a line start. */
8905 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8906 reordering is in effect. */
8907 it->continuation_lines_width = 0;
8909 /* Move forward and see what y-distance we moved. First move to the
8910 start of the next line so that we get its height. We need this
8911 height to be able to tell whether we reached the specified
8912 y-distance. */
8913 SAVE_IT (it2, *it, it2data);
8914 it2.max_ascent = it2.max_descent = 0;
8917 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8918 MOVE_TO_POS | MOVE_TO_VPOS);
8920 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8921 /* If we are in a display string which starts at START_POS,
8922 and that display string includes a newline, and we are
8923 right after that newline (i.e. at the beginning of a
8924 display line), exit the loop, because otherwise we will
8925 infloop, since move_it_to will see that it is already at
8926 START_POS and will not move. */
8927 || (it2.method == GET_FROM_STRING
8928 && IT_CHARPOS (it2) == start_pos
8929 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8930 xassert (IT_CHARPOS (*it) >= BEGV);
8931 SAVE_IT (it3, it2, it3data);
8933 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8934 xassert (IT_CHARPOS (*it) >= BEGV);
8935 /* H is the actual vertical distance from the position in *IT
8936 and the starting position. */
8937 h = it2.current_y - it->current_y;
8938 /* NLINES is the distance in number of lines. */
8939 nlines = it2.vpos - it->vpos;
8941 /* Correct IT's y and vpos position
8942 so that they are relative to the starting point. */
8943 it->vpos -= nlines;
8944 it->current_y -= h;
8946 if (dy == 0)
8948 /* DY == 0 means move to the start of the screen line. The
8949 value of nlines is > 0 if continuation lines were involved,
8950 or if the original IT position was at start of a line. */
8951 RESTORE_IT (it, it, it2data);
8952 if (nlines > 0)
8953 move_it_by_lines (it, nlines);
8954 /* The above code moves us to some position NLINES down,
8955 usually to its first glyph (leftmost in an L2R line), but
8956 that's not necessarily the start of the line, under bidi
8957 reordering. We want to get to the character position
8958 that is immediately after the newline of the previous
8959 line. */
8960 if (it->bidi_p
8961 && !it->continuation_lines_width
8962 && !STRINGP (it->string)
8963 && IT_CHARPOS (*it) > BEGV
8964 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8966 ptrdiff_t nl_pos =
8967 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8969 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8971 bidi_unshelve_cache (it3data, 1);
8973 else
8975 /* The y-position we try to reach, relative to *IT.
8976 Note that H has been subtracted in front of the if-statement. */
8977 int target_y = it->current_y + h - dy;
8978 int y0 = it3.current_y;
8979 int y1;
8980 int line_height;
8982 RESTORE_IT (&it3, &it3, it3data);
8983 y1 = line_bottom_y (&it3);
8984 line_height = y1 - y0;
8985 RESTORE_IT (it, it, it2data);
8986 /* If we did not reach target_y, try to move further backward if
8987 we can. If we moved too far backward, try to move forward. */
8988 if (target_y < it->current_y
8989 /* This is heuristic. In a window that's 3 lines high, with
8990 a line height of 13 pixels each, recentering with point
8991 on the bottom line will try to move -39/2 = 19 pixels
8992 backward. Try to avoid moving into the first line. */
8993 && (it->current_y - target_y
8994 > min (window_box_height (it->w), line_height * 2 / 3))
8995 && IT_CHARPOS (*it) > BEGV)
8997 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8998 target_y - it->current_y));
8999 dy = it->current_y - target_y;
9000 goto move_further_back;
9002 else if (target_y >= it->current_y + line_height
9003 && IT_CHARPOS (*it) < ZV)
9005 /* Should move forward by at least one line, maybe more.
9007 Note: Calling move_it_by_lines can be expensive on
9008 terminal frames, where compute_motion is used (via
9009 vmotion) to do the job, when there are very long lines
9010 and truncate-lines is nil. That's the reason for
9011 treating terminal frames specially here. */
9013 if (!FRAME_WINDOW_P (it->f))
9014 move_it_vertically (it, target_y - (it->current_y + line_height));
9015 else
9019 move_it_by_lines (it, 1);
9021 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9028 /* Move IT by a specified amount of pixel lines DY. DY negative means
9029 move backwards. DY = 0 means move to start of screen line. At the
9030 end, IT will be on the start of a screen line. */
9032 void
9033 move_it_vertically (struct it *it, int dy)
9035 if (dy <= 0)
9036 move_it_vertically_backward (it, -dy);
9037 else
9039 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9040 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9041 MOVE_TO_POS | MOVE_TO_Y);
9042 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9044 /* If buffer ends in ZV without a newline, move to the start of
9045 the line to satisfy the post-condition. */
9046 if (IT_CHARPOS (*it) == ZV
9047 && ZV > BEGV
9048 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9049 move_it_by_lines (it, 0);
9054 /* Move iterator IT past the end of the text line it is in. */
9056 void
9057 move_it_past_eol (struct it *it)
9059 enum move_it_result rc;
9061 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9062 if (rc == MOVE_NEWLINE_OR_CR)
9063 set_iterator_to_next (it, 0);
9067 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9068 negative means move up. DVPOS == 0 means move to the start of the
9069 screen line.
9071 Optimization idea: If we would know that IT->f doesn't use
9072 a face with proportional font, we could be faster for
9073 truncate-lines nil. */
9075 void
9076 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9079 /* The commented-out optimization uses vmotion on terminals. This
9080 gives bad results, because elements like it->what, on which
9081 callers such as pos_visible_p rely, aren't updated. */
9082 /* struct position pos;
9083 if (!FRAME_WINDOW_P (it->f))
9085 struct text_pos textpos;
9087 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9088 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9089 reseat (it, textpos, 1);
9090 it->vpos += pos.vpos;
9091 it->current_y += pos.vpos;
9093 else */
9095 if (dvpos == 0)
9097 /* DVPOS == 0 means move to the start of the screen line. */
9098 move_it_vertically_backward (it, 0);
9099 /* Let next call to line_bottom_y calculate real line height */
9100 last_height = 0;
9102 else if (dvpos > 0)
9104 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9105 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9107 /* Only move to the next buffer position if we ended up in a
9108 string from display property, not in an overlay string
9109 (before-string or after-string). That is because the
9110 latter don't conceal the underlying buffer position, so
9111 we can ask to move the iterator to the exact position we
9112 are interested in. Note that, even if we are already at
9113 IT_CHARPOS (*it), the call below is not a no-op, as it
9114 will detect that we are at the end of the string, pop the
9115 iterator, and compute it->current_x and it->hpos
9116 correctly. */
9117 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9118 -1, -1, -1, MOVE_TO_POS);
9121 else
9123 struct it it2;
9124 void *it2data = NULL;
9125 ptrdiff_t start_charpos, i;
9127 /* Start at the beginning of the screen line containing IT's
9128 position. This may actually move vertically backwards,
9129 in case of overlays, so adjust dvpos accordingly. */
9130 dvpos += it->vpos;
9131 move_it_vertically_backward (it, 0);
9132 dvpos -= it->vpos;
9134 /* Go back -DVPOS visible lines and reseat the iterator there. */
9135 start_charpos = IT_CHARPOS (*it);
9136 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9137 back_to_previous_visible_line_start (it);
9138 reseat (it, it->current.pos, 1);
9140 /* Move further back if we end up in a string or an image. */
9141 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9143 /* First try to move to start of display line. */
9144 dvpos += it->vpos;
9145 move_it_vertically_backward (it, 0);
9146 dvpos -= it->vpos;
9147 if (IT_POS_VALID_AFTER_MOVE_P (it))
9148 break;
9149 /* If start of line is still in string or image,
9150 move further back. */
9151 back_to_previous_visible_line_start (it);
9152 reseat (it, it->current.pos, 1);
9153 dvpos--;
9156 it->current_x = it->hpos = 0;
9158 /* Above call may have moved too far if continuation lines
9159 are involved. Scan forward and see if it did. */
9160 SAVE_IT (it2, *it, it2data);
9161 it2.vpos = it2.current_y = 0;
9162 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9163 it->vpos -= it2.vpos;
9164 it->current_y -= it2.current_y;
9165 it->current_x = it->hpos = 0;
9167 /* If we moved too far back, move IT some lines forward. */
9168 if (it2.vpos > -dvpos)
9170 int delta = it2.vpos + dvpos;
9172 RESTORE_IT (&it2, &it2, it2data);
9173 SAVE_IT (it2, *it, it2data);
9174 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9175 /* Move back again if we got too far ahead. */
9176 if (IT_CHARPOS (*it) >= start_charpos)
9177 RESTORE_IT (it, &it2, it2data);
9178 else
9179 bidi_unshelve_cache (it2data, 1);
9181 else
9182 RESTORE_IT (it, it, it2data);
9186 /* Return 1 if IT points into the middle of a display vector. */
9189 in_display_vector_p (struct it *it)
9191 return (it->method == GET_FROM_DISPLAY_VECTOR
9192 && it->current.dpvec_index > 0
9193 && it->dpvec + it->current.dpvec_index != it->dpend);
9197 /***********************************************************************
9198 Messages
9199 ***********************************************************************/
9202 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9203 to *Messages*. */
9205 void
9206 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9208 Lisp_Object args[3];
9209 Lisp_Object msg, fmt;
9210 char *buffer;
9211 ptrdiff_t len;
9212 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9213 USE_SAFE_ALLOCA;
9215 /* Do nothing if called asynchronously. Inserting text into
9216 a buffer may call after-change-functions and alike and
9217 that would means running Lisp asynchronously. */
9218 if (handling_signal)
9219 return;
9221 fmt = msg = Qnil;
9222 GCPRO4 (fmt, msg, arg1, arg2);
9224 args[0] = fmt = build_string (format);
9225 args[1] = arg1;
9226 args[2] = arg2;
9227 msg = Fformat (3, args);
9229 len = SBYTES (msg) + 1;
9230 SAFE_ALLOCA (buffer, char *, len);
9231 memcpy (buffer, SDATA (msg), len);
9233 message_dolog (buffer, len - 1, 1, 0);
9234 SAFE_FREE ();
9236 UNGCPRO;
9240 /* Output a newline in the *Messages* buffer if "needs" one. */
9242 void
9243 message_log_maybe_newline (void)
9245 if (message_log_need_newline)
9246 message_dolog ("", 0, 1, 0);
9250 /* Add a string M of length NBYTES to the message log, optionally
9251 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9252 nonzero, means interpret the contents of M as multibyte. This
9253 function calls low-level routines in order to bypass text property
9254 hooks, etc. which might not be safe to run.
9256 This may GC (insert may run before/after change hooks),
9257 so the buffer M must NOT point to a Lisp string. */
9259 void
9260 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9262 const unsigned char *msg = (const unsigned char *) m;
9264 if (!NILP (Vmemory_full))
9265 return;
9267 if (!NILP (Vmessage_log_max))
9269 struct buffer *oldbuf;
9270 Lisp_Object oldpoint, oldbegv, oldzv;
9271 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9272 ptrdiff_t point_at_end = 0;
9273 ptrdiff_t zv_at_end = 0;
9274 Lisp_Object old_deactivate_mark, tem;
9275 struct gcpro gcpro1;
9277 old_deactivate_mark = Vdeactivate_mark;
9278 oldbuf = current_buffer;
9279 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9280 BVAR (current_buffer, undo_list) = Qt;
9282 oldpoint = message_dolog_marker1;
9283 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9284 oldbegv = message_dolog_marker2;
9285 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9286 oldzv = message_dolog_marker3;
9287 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9288 GCPRO1 (old_deactivate_mark);
9290 if (PT == Z)
9291 point_at_end = 1;
9292 if (ZV == Z)
9293 zv_at_end = 1;
9295 BEGV = BEG;
9296 BEGV_BYTE = BEG_BYTE;
9297 ZV = Z;
9298 ZV_BYTE = Z_BYTE;
9299 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9301 /* Insert the string--maybe converting multibyte to single byte
9302 or vice versa, so that all the text fits the buffer. */
9303 if (multibyte
9304 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9306 ptrdiff_t i;
9307 int c, char_bytes;
9308 char work[1];
9310 /* Convert a multibyte string to single-byte
9311 for the *Message* buffer. */
9312 for (i = 0; i < nbytes; i += char_bytes)
9314 c = string_char_and_length (msg + i, &char_bytes);
9315 work[0] = (ASCII_CHAR_P (c)
9317 : multibyte_char_to_unibyte (c));
9318 insert_1_both (work, 1, 1, 1, 0, 0);
9321 else if (! multibyte
9322 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9324 ptrdiff_t i;
9325 int c, char_bytes;
9326 unsigned char str[MAX_MULTIBYTE_LENGTH];
9327 /* Convert a single-byte string to multibyte
9328 for the *Message* buffer. */
9329 for (i = 0; i < nbytes; i++)
9331 c = msg[i];
9332 MAKE_CHAR_MULTIBYTE (c);
9333 char_bytes = CHAR_STRING (c, str);
9334 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9337 else if (nbytes)
9338 insert_1 (m, nbytes, 1, 0, 0);
9340 if (nlflag)
9342 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9343 printmax_t dups;
9344 insert_1 ("\n", 1, 1, 0, 0);
9346 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9347 this_bol = PT;
9348 this_bol_byte = PT_BYTE;
9350 /* See if this line duplicates the previous one.
9351 If so, combine duplicates. */
9352 if (this_bol > BEG)
9354 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9355 prev_bol = PT;
9356 prev_bol_byte = PT_BYTE;
9358 dups = message_log_check_duplicate (prev_bol_byte,
9359 this_bol_byte);
9360 if (dups)
9362 del_range_both (prev_bol, prev_bol_byte,
9363 this_bol, this_bol_byte, 0);
9364 if (dups > 1)
9366 char dupstr[sizeof " [ times]"
9367 + INT_STRLEN_BOUND (printmax_t)];
9368 int duplen;
9370 /* If you change this format, don't forget to also
9371 change message_log_check_duplicate. */
9372 sprintf (dupstr, " [%"pMd" times]", dups);
9373 duplen = strlen (dupstr);
9374 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9375 insert_1 (dupstr, duplen, 1, 0, 1);
9380 /* If we have more than the desired maximum number of lines
9381 in the *Messages* buffer now, delete the oldest ones.
9382 This is safe because we don't have undo in this buffer. */
9384 if (NATNUMP (Vmessage_log_max))
9386 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9387 -XFASTINT (Vmessage_log_max) - 1, 0);
9388 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9391 BEGV = XMARKER (oldbegv)->charpos;
9392 BEGV_BYTE = marker_byte_position (oldbegv);
9394 if (zv_at_end)
9396 ZV = Z;
9397 ZV_BYTE = Z_BYTE;
9399 else
9401 ZV = XMARKER (oldzv)->charpos;
9402 ZV_BYTE = marker_byte_position (oldzv);
9405 if (point_at_end)
9406 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9407 else
9408 /* We can't do Fgoto_char (oldpoint) because it will run some
9409 Lisp code. */
9410 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9411 XMARKER (oldpoint)->bytepos);
9413 UNGCPRO;
9414 unchain_marker (XMARKER (oldpoint));
9415 unchain_marker (XMARKER (oldbegv));
9416 unchain_marker (XMARKER (oldzv));
9418 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9419 set_buffer_internal (oldbuf);
9420 if (NILP (tem))
9421 windows_or_buffers_changed = old_windows_or_buffers_changed;
9422 message_log_need_newline = !nlflag;
9423 Vdeactivate_mark = old_deactivate_mark;
9428 /* We are at the end of the buffer after just having inserted a newline.
9429 (Note: We depend on the fact we won't be crossing the gap.)
9430 Check to see if the most recent message looks a lot like the previous one.
9431 Return 0 if different, 1 if the new one should just replace it, or a
9432 value N > 1 if we should also append " [N times]". */
9434 static intmax_t
9435 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9437 ptrdiff_t i;
9438 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9439 int seen_dots = 0;
9440 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9441 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9443 for (i = 0; i < len; i++)
9445 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9446 seen_dots = 1;
9447 if (p1[i] != p2[i])
9448 return seen_dots;
9450 p1 += len;
9451 if (*p1 == '\n')
9452 return 2;
9453 if (*p1++ == ' ' && *p1++ == '[')
9455 char *pend;
9456 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9457 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9458 return n+1;
9460 return 0;
9464 /* Display an echo area message M with a specified length of NBYTES
9465 bytes. The string may include null characters. If M is 0, clear
9466 out any existing message, and let the mini-buffer text show
9467 through.
9469 This may GC, so the buffer M must NOT point to a Lisp string. */
9471 void
9472 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9474 /* First flush out any partial line written with print. */
9475 message_log_maybe_newline ();
9476 if (m)
9477 message_dolog (m, nbytes, 1, multibyte);
9478 message2_nolog (m, nbytes, multibyte);
9482 /* The non-logging counterpart of message2. */
9484 void
9485 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9487 struct frame *sf = SELECTED_FRAME ();
9488 message_enable_multibyte = multibyte;
9490 if (FRAME_INITIAL_P (sf))
9492 if (noninteractive_need_newline)
9493 putc ('\n', stderr);
9494 noninteractive_need_newline = 0;
9495 if (m)
9496 fwrite (m, nbytes, 1, stderr);
9497 if (cursor_in_echo_area == 0)
9498 fprintf (stderr, "\n");
9499 fflush (stderr);
9501 /* A null message buffer means that the frame hasn't really been
9502 initialized yet. Error messages get reported properly by
9503 cmd_error, so this must be just an informative message; toss it. */
9504 else if (INTERACTIVE
9505 && sf->glyphs_initialized_p
9506 && FRAME_MESSAGE_BUF (sf))
9508 Lisp_Object mini_window;
9509 struct frame *f;
9511 /* Get the frame containing the mini-buffer
9512 that the selected frame is using. */
9513 mini_window = FRAME_MINIBUF_WINDOW (sf);
9514 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9516 FRAME_SAMPLE_VISIBILITY (f);
9517 if (FRAME_VISIBLE_P (sf)
9518 && ! FRAME_VISIBLE_P (f))
9519 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9521 if (m)
9523 set_message (m, Qnil, nbytes, multibyte);
9524 if (minibuffer_auto_raise)
9525 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9527 else
9528 clear_message (1, 1);
9530 do_pending_window_change (0);
9531 echo_area_display (1);
9532 do_pending_window_change (0);
9533 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9534 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9539 /* Display an echo area message M with a specified length of NBYTES
9540 bytes. The string may include null characters. If M is not a
9541 string, clear out any existing message, and let the mini-buffer
9542 text show through.
9544 This function cancels echoing. */
9546 void
9547 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9549 struct gcpro gcpro1;
9551 GCPRO1 (m);
9552 clear_message (1,1);
9553 cancel_echoing ();
9555 /* First flush out any partial line written with print. */
9556 message_log_maybe_newline ();
9557 if (STRINGP (m))
9559 char *buffer;
9560 USE_SAFE_ALLOCA;
9562 SAFE_ALLOCA (buffer, char *, nbytes);
9563 memcpy (buffer, SDATA (m), nbytes);
9564 message_dolog (buffer, nbytes, 1, multibyte);
9565 SAFE_FREE ();
9567 message3_nolog (m, nbytes, multibyte);
9569 UNGCPRO;
9573 /* The non-logging version of message3.
9574 This does not cancel echoing, because it is used for echoing.
9575 Perhaps we need to make a separate function for echoing
9576 and make this cancel echoing. */
9578 void
9579 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9581 struct frame *sf = SELECTED_FRAME ();
9582 message_enable_multibyte = multibyte;
9584 if (FRAME_INITIAL_P (sf))
9586 if (noninteractive_need_newline)
9587 putc ('\n', stderr);
9588 noninteractive_need_newline = 0;
9589 if (STRINGP (m))
9590 fwrite (SDATA (m), nbytes, 1, stderr);
9591 if (cursor_in_echo_area == 0)
9592 fprintf (stderr, "\n");
9593 fflush (stderr);
9595 /* A null message buffer means that the frame hasn't really been
9596 initialized yet. Error messages get reported properly by
9597 cmd_error, so this must be just an informative message; toss it. */
9598 else if (INTERACTIVE
9599 && sf->glyphs_initialized_p
9600 && FRAME_MESSAGE_BUF (sf))
9602 Lisp_Object mini_window;
9603 Lisp_Object frame;
9604 struct frame *f;
9606 /* Get the frame containing the mini-buffer
9607 that the selected frame is using. */
9608 mini_window = FRAME_MINIBUF_WINDOW (sf);
9609 frame = XWINDOW (mini_window)->frame;
9610 f = XFRAME (frame);
9612 FRAME_SAMPLE_VISIBILITY (f);
9613 if (FRAME_VISIBLE_P (sf)
9614 && !FRAME_VISIBLE_P (f))
9615 Fmake_frame_visible (frame);
9617 if (STRINGP (m) && SCHARS (m) > 0)
9619 set_message (NULL, m, nbytes, multibyte);
9620 if (minibuffer_auto_raise)
9621 Fraise_frame (frame);
9622 /* Assume we are not echoing.
9623 (If we are, echo_now will override this.) */
9624 echo_message_buffer = Qnil;
9626 else
9627 clear_message (1, 1);
9629 do_pending_window_change (0);
9630 echo_area_display (1);
9631 do_pending_window_change (0);
9632 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9633 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9638 /* Display a null-terminated echo area message M. If M is 0, clear
9639 out any existing message, and let the mini-buffer text show through.
9641 The buffer M must continue to exist until after the echo area gets
9642 cleared or some other message gets displayed there. Do not pass
9643 text that is stored in a Lisp string. Do not pass text in a buffer
9644 that was alloca'd. */
9646 void
9647 message1 (const char *m)
9649 message2 (m, (m ? strlen (m) : 0), 0);
9653 /* The non-logging counterpart of message1. */
9655 void
9656 message1_nolog (const char *m)
9658 message2_nolog (m, (m ? strlen (m) : 0), 0);
9661 /* Display a message M which contains a single %s
9662 which gets replaced with STRING. */
9664 void
9665 message_with_string (const char *m, Lisp_Object string, int log)
9667 CHECK_STRING (string);
9669 if (noninteractive)
9671 if (m)
9673 if (noninteractive_need_newline)
9674 putc ('\n', stderr);
9675 noninteractive_need_newline = 0;
9676 fprintf (stderr, m, SDATA (string));
9677 if (!cursor_in_echo_area)
9678 fprintf (stderr, "\n");
9679 fflush (stderr);
9682 else if (INTERACTIVE)
9684 /* The frame whose minibuffer we're going to display the message on.
9685 It may be larger than the selected frame, so we need
9686 to use its buffer, not the selected frame's buffer. */
9687 Lisp_Object mini_window;
9688 struct frame *f, *sf = SELECTED_FRAME ();
9690 /* Get the frame containing the minibuffer
9691 that the selected frame is using. */
9692 mini_window = FRAME_MINIBUF_WINDOW (sf);
9693 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9695 /* A null message buffer means that the frame hasn't really been
9696 initialized yet. Error messages get reported properly by
9697 cmd_error, so this must be just an informative message; toss it. */
9698 if (FRAME_MESSAGE_BUF (f))
9700 Lisp_Object args[2], msg;
9701 struct gcpro gcpro1, gcpro2;
9703 args[0] = build_string (m);
9704 args[1] = msg = string;
9705 GCPRO2 (args[0], msg);
9706 gcpro1.nvars = 2;
9708 msg = Fformat (2, args);
9710 if (log)
9711 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9712 else
9713 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9715 UNGCPRO;
9717 /* Print should start at the beginning of the message
9718 buffer next time. */
9719 message_buf_print = 0;
9725 /* Dump an informative message to the minibuf. If M is 0, clear out
9726 any existing message, and let the mini-buffer text show through. */
9728 static void
9729 vmessage (const char *m, va_list ap)
9731 if (noninteractive)
9733 if (m)
9735 if (noninteractive_need_newline)
9736 putc ('\n', stderr);
9737 noninteractive_need_newline = 0;
9738 vfprintf (stderr, m, ap);
9739 if (cursor_in_echo_area == 0)
9740 fprintf (stderr, "\n");
9741 fflush (stderr);
9744 else if (INTERACTIVE)
9746 /* The frame whose mini-buffer we're going to display the message
9747 on. It may be larger than the selected frame, so we need to
9748 use its buffer, not the selected frame's buffer. */
9749 Lisp_Object mini_window;
9750 struct frame *f, *sf = SELECTED_FRAME ();
9752 /* Get the frame containing the mini-buffer
9753 that the selected frame is using. */
9754 mini_window = FRAME_MINIBUF_WINDOW (sf);
9755 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9757 /* A null message buffer means that the frame hasn't really been
9758 initialized yet. Error messages get reported properly by
9759 cmd_error, so this must be just an informative message; toss
9760 it. */
9761 if (FRAME_MESSAGE_BUF (f))
9763 if (m)
9765 ptrdiff_t len;
9767 len = doprnt (FRAME_MESSAGE_BUF (f),
9768 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9770 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9772 else
9773 message1 (0);
9775 /* Print should start at the beginning of the message
9776 buffer next time. */
9777 message_buf_print = 0;
9782 void
9783 message (const char *m, ...)
9785 va_list ap;
9786 va_start (ap, m);
9787 vmessage (m, ap);
9788 va_end (ap);
9792 #if 0
9793 /* The non-logging version of message. */
9795 void
9796 message_nolog (const char *m, ...)
9798 Lisp_Object old_log_max;
9799 va_list ap;
9800 va_start (ap, m);
9801 old_log_max = Vmessage_log_max;
9802 Vmessage_log_max = Qnil;
9803 vmessage (m, ap);
9804 Vmessage_log_max = old_log_max;
9805 va_end (ap);
9807 #endif
9810 /* Display the current message in the current mini-buffer. This is
9811 only called from error handlers in process.c, and is not time
9812 critical. */
9814 void
9815 update_echo_area (void)
9817 if (!NILP (echo_area_buffer[0]))
9819 Lisp_Object string;
9820 string = Fcurrent_message ();
9821 message3 (string, SBYTES (string),
9822 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9827 /* Make sure echo area buffers in `echo_buffers' are live.
9828 If they aren't, make new ones. */
9830 static void
9831 ensure_echo_area_buffers (void)
9833 int i;
9835 for (i = 0; i < 2; ++i)
9836 if (!BUFFERP (echo_buffer[i])
9837 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9839 char name[30];
9840 Lisp_Object old_buffer;
9841 int j;
9843 old_buffer = echo_buffer[i];
9844 sprintf (name, " *Echo Area %d*", i);
9845 echo_buffer[i] = Fget_buffer_create (build_string (name));
9846 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9847 /* to force word wrap in echo area -
9848 it was decided to postpone this*/
9849 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9851 for (j = 0; j < 2; ++j)
9852 if (EQ (old_buffer, echo_area_buffer[j]))
9853 echo_area_buffer[j] = echo_buffer[i];
9858 /* Call FN with args A1..A4 with either the current or last displayed
9859 echo_area_buffer as current buffer.
9861 WHICH zero means use the current message buffer
9862 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9863 from echo_buffer[] and clear it.
9865 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9866 suitable buffer from echo_buffer[] and clear it.
9868 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9869 that the current message becomes the last displayed one, make
9870 choose a suitable buffer for echo_area_buffer[0], and clear it.
9872 Value is what FN returns. */
9874 static int
9875 with_echo_area_buffer (struct window *w, int which,
9876 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9877 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9879 Lisp_Object buffer;
9880 int this_one, the_other, clear_buffer_p, rc;
9881 ptrdiff_t count = SPECPDL_INDEX ();
9883 /* If buffers aren't live, make new ones. */
9884 ensure_echo_area_buffers ();
9886 clear_buffer_p = 0;
9888 if (which == 0)
9889 this_one = 0, the_other = 1;
9890 else if (which > 0)
9891 this_one = 1, the_other = 0;
9892 else
9894 this_one = 0, the_other = 1;
9895 clear_buffer_p = 1;
9897 /* We need a fresh one in case the current echo buffer equals
9898 the one containing the last displayed echo area message. */
9899 if (!NILP (echo_area_buffer[this_one])
9900 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9901 echo_area_buffer[this_one] = Qnil;
9904 /* Choose a suitable buffer from echo_buffer[] is we don't
9905 have one. */
9906 if (NILP (echo_area_buffer[this_one]))
9908 echo_area_buffer[this_one]
9909 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9910 ? echo_buffer[the_other]
9911 : echo_buffer[this_one]);
9912 clear_buffer_p = 1;
9915 buffer = echo_area_buffer[this_one];
9917 /* Don't get confused by reusing the buffer used for echoing
9918 for a different purpose. */
9919 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9920 cancel_echoing ();
9922 record_unwind_protect (unwind_with_echo_area_buffer,
9923 with_echo_area_buffer_unwind_data (w));
9925 /* Make the echo area buffer current. Note that for display
9926 purposes, it is not necessary that the displayed window's buffer
9927 == current_buffer, except for text property lookup. So, let's
9928 only set that buffer temporarily here without doing a full
9929 Fset_window_buffer. We must also change w->pointm, though,
9930 because otherwise an assertions in unshow_buffer fails, and Emacs
9931 aborts. */
9932 set_buffer_internal_1 (XBUFFER (buffer));
9933 if (w)
9935 w->buffer = buffer;
9936 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9939 BVAR (current_buffer, undo_list) = Qt;
9940 BVAR (current_buffer, read_only) = Qnil;
9941 specbind (Qinhibit_read_only, Qt);
9942 specbind (Qinhibit_modification_hooks, Qt);
9944 if (clear_buffer_p && Z > BEG)
9945 del_range (BEG, Z);
9947 xassert (BEGV >= BEG);
9948 xassert (ZV <= Z && ZV >= BEGV);
9950 rc = fn (a1, a2, a3, a4);
9952 xassert (BEGV >= BEG);
9953 xassert (ZV <= Z && ZV >= BEGV);
9955 unbind_to (count, Qnil);
9956 return rc;
9960 /* Save state that should be preserved around the call to the function
9961 FN called in with_echo_area_buffer. */
9963 static Lisp_Object
9964 with_echo_area_buffer_unwind_data (struct window *w)
9966 int i = 0;
9967 Lisp_Object vector, tmp;
9969 /* Reduce consing by keeping one vector in
9970 Vwith_echo_area_save_vector. */
9971 vector = Vwith_echo_area_save_vector;
9972 Vwith_echo_area_save_vector = Qnil;
9974 if (NILP (vector))
9975 vector = Fmake_vector (make_number (7), Qnil);
9977 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9978 ASET (vector, i, Vdeactivate_mark); ++i;
9979 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9981 if (w)
9983 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9984 ASET (vector, i, w->buffer); ++i;
9985 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9986 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9988 else
9990 int end = i + 4;
9991 for (; i < end; ++i)
9992 ASET (vector, i, Qnil);
9995 xassert (i == ASIZE (vector));
9996 return vector;
10000 /* Restore global state from VECTOR which was created by
10001 with_echo_area_buffer_unwind_data. */
10003 static Lisp_Object
10004 unwind_with_echo_area_buffer (Lisp_Object vector)
10006 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10007 Vdeactivate_mark = AREF (vector, 1);
10008 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10010 if (WINDOWP (AREF (vector, 3)))
10012 struct window *w;
10013 Lisp_Object buffer, charpos, bytepos;
10015 w = XWINDOW (AREF (vector, 3));
10016 buffer = AREF (vector, 4);
10017 charpos = AREF (vector, 5);
10018 bytepos = AREF (vector, 6);
10020 w->buffer = buffer;
10021 set_marker_both (w->pointm, buffer,
10022 XFASTINT (charpos), XFASTINT (bytepos));
10025 Vwith_echo_area_save_vector = vector;
10026 return Qnil;
10030 /* Set up the echo area for use by print functions. MULTIBYTE_P
10031 non-zero means we will print multibyte. */
10033 void
10034 setup_echo_area_for_printing (int multibyte_p)
10036 /* If we can't find an echo area any more, exit. */
10037 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10038 Fkill_emacs (Qnil);
10040 ensure_echo_area_buffers ();
10042 if (!message_buf_print)
10044 /* A message has been output since the last time we printed.
10045 Choose a fresh echo area buffer. */
10046 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10047 echo_area_buffer[0] = echo_buffer[1];
10048 else
10049 echo_area_buffer[0] = echo_buffer[0];
10051 /* Switch to that buffer and clear it. */
10052 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10053 BVAR (current_buffer, truncate_lines) = Qnil;
10055 if (Z > BEG)
10057 ptrdiff_t count = SPECPDL_INDEX ();
10058 specbind (Qinhibit_read_only, Qt);
10059 /* Note that undo recording is always disabled. */
10060 del_range (BEG, Z);
10061 unbind_to (count, Qnil);
10063 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10065 /* Set up the buffer for the multibyteness we need. */
10066 if (multibyte_p
10067 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10068 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10070 /* Raise the frame containing the echo area. */
10071 if (minibuffer_auto_raise)
10073 struct frame *sf = SELECTED_FRAME ();
10074 Lisp_Object mini_window;
10075 mini_window = FRAME_MINIBUF_WINDOW (sf);
10076 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10079 message_log_maybe_newline ();
10080 message_buf_print = 1;
10082 else
10084 if (NILP (echo_area_buffer[0]))
10086 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10087 echo_area_buffer[0] = echo_buffer[1];
10088 else
10089 echo_area_buffer[0] = echo_buffer[0];
10092 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10094 /* Someone switched buffers between print requests. */
10095 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10096 BVAR (current_buffer, truncate_lines) = Qnil;
10102 /* Display an echo area message in window W. Value is non-zero if W's
10103 height is changed. If display_last_displayed_message_p is
10104 non-zero, display the message that was last displayed, otherwise
10105 display the current message. */
10107 static int
10108 display_echo_area (struct window *w)
10110 int i, no_message_p, window_height_changed_p;
10112 /* Temporarily disable garbage collections while displaying the echo
10113 area. This is done because a GC can print a message itself.
10114 That message would modify the echo area buffer's contents while a
10115 redisplay of the buffer is going on, and seriously confuse
10116 redisplay. */
10117 ptrdiff_t count = inhibit_garbage_collection ();
10119 /* If there is no message, we must call display_echo_area_1
10120 nevertheless because it resizes the window. But we will have to
10121 reset the echo_area_buffer in question to nil at the end because
10122 with_echo_area_buffer will sets it to an empty buffer. */
10123 i = display_last_displayed_message_p ? 1 : 0;
10124 no_message_p = NILP (echo_area_buffer[i]);
10126 window_height_changed_p
10127 = with_echo_area_buffer (w, display_last_displayed_message_p,
10128 display_echo_area_1,
10129 (intptr_t) w, Qnil, 0, 0);
10131 if (no_message_p)
10132 echo_area_buffer[i] = Qnil;
10134 unbind_to (count, Qnil);
10135 return window_height_changed_p;
10139 /* Helper for display_echo_area. Display the current buffer which
10140 contains the current echo area message in window W, a mini-window,
10141 a pointer to which is passed in A1. A2..A4 are currently not used.
10142 Change the height of W so that all of the message is displayed.
10143 Value is non-zero if height of W was changed. */
10145 static int
10146 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10148 intptr_t i1 = a1;
10149 struct window *w = (struct window *) i1;
10150 Lisp_Object window;
10151 struct text_pos start;
10152 int window_height_changed_p = 0;
10154 /* Do this before displaying, so that we have a large enough glyph
10155 matrix for the display. If we can't get enough space for the
10156 whole text, display the last N lines. That works by setting w->start. */
10157 window_height_changed_p = resize_mini_window (w, 0);
10159 /* Use the starting position chosen by resize_mini_window. */
10160 SET_TEXT_POS_FROM_MARKER (start, w->start);
10162 /* Display. */
10163 clear_glyph_matrix (w->desired_matrix);
10164 XSETWINDOW (window, w);
10165 try_window (window, start, 0);
10167 return window_height_changed_p;
10171 /* Resize the echo area window to exactly the size needed for the
10172 currently displayed message, if there is one. If a mini-buffer
10173 is active, don't shrink it. */
10175 void
10176 resize_echo_area_exactly (void)
10178 if (BUFFERP (echo_area_buffer[0])
10179 && WINDOWP (echo_area_window))
10181 struct window *w = XWINDOW (echo_area_window);
10182 int resized_p;
10183 Lisp_Object resize_exactly;
10185 if (minibuf_level == 0)
10186 resize_exactly = Qt;
10187 else
10188 resize_exactly = Qnil;
10190 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10191 (intptr_t) w, resize_exactly,
10192 0, 0);
10193 if (resized_p)
10195 ++windows_or_buffers_changed;
10196 ++update_mode_lines;
10197 redisplay_internal ();
10203 /* Callback function for with_echo_area_buffer, when used from
10204 resize_echo_area_exactly. A1 contains a pointer to the window to
10205 resize, EXACTLY non-nil means resize the mini-window exactly to the
10206 size of the text displayed. A3 and A4 are not used. Value is what
10207 resize_mini_window returns. */
10209 static int
10210 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10212 intptr_t i1 = a1;
10213 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10217 /* Resize mini-window W to fit the size of its contents. EXACT_P
10218 means size the window exactly to the size needed. Otherwise, it's
10219 only enlarged until W's buffer is empty.
10221 Set W->start to the right place to begin display. If the whole
10222 contents fit, start at the beginning. Otherwise, start so as
10223 to make the end of the contents appear. This is particularly
10224 important for y-or-n-p, but seems desirable generally.
10226 Value is non-zero if the window height has been changed. */
10229 resize_mini_window (struct window *w, int exact_p)
10231 struct frame *f = XFRAME (w->frame);
10232 int window_height_changed_p = 0;
10234 xassert (MINI_WINDOW_P (w));
10236 /* By default, start display at the beginning. */
10237 set_marker_both (w->start, w->buffer,
10238 BUF_BEGV (XBUFFER (w->buffer)),
10239 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10241 /* Don't resize windows while redisplaying a window; it would
10242 confuse redisplay functions when the size of the window they are
10243 displaying changes from under them. Such a resizing can happen,
10244 for instance, when which-func prints a long message while
10245 we are running fontification-functions. We're running these
10246 functions with safe_call which binds inhibit-redisplay to t. */
10247 if (!NILP (Vinhibit_redisplay))
10248 return 0;
10250 /* Nil means don't try to resize. */
10251 if (NILP (Vresize_mini_windows)
10252 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10253 return 0;
10255 if (!FRAME_MINIBUF_ONLY_P (f))
10257 struct it it;
10258 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10259 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10260 int height;
10261 EMACS_INT max_height;
10262 int unit = FRAME_LINE_HEIGHT (f);
10263 struct text_pos start;
10264 struct buffer *old_current_buffer = NULL;
10266 if (current_buffer != XBUFFER (w->buffer))
10268 old_current_buffer = current_buffer;
10269 set_buffer_internal (XBUFFER (w->buffer));
10272 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10274 /* Compute the max. number of lines specified by the user. */
10275 if (FLOATP (Vmax_mini_window_height))
10276 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10277 else if (INTEGERP (Vmax_mini_window_height))
10278 max_height = XINT (Vmax_mini_window_height);
10279 else
10280 max_height = total_height / 4;
10282 /* Correct that max. height if it's bogus. */
10283 max_height = max (1, max_height);
10284 max_height = min (total_height, max_height);
10286 /* Find out the height of the text in the window. */
10287 if (it.line_wrap == TRUNCATE)
10288 height = 1;
10289 else
10291 last_height = 0;
10292 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10293 if (it.max_ascent == 0 && it.max_descent == 0)
10294 height = it.current_y + last_height;
10295 else
10296 height = it.current_y + it.max_ascent + it.max_descent;
10297 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10298 height = (height + unit - 1) / unit;
10301 /* Compute a suitable window start. */
10302 if (height > max_height)
10304 height = max_height;
10305 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10306 move_it_vertically_backward (&it, (height - 1) * unit);
10307 start = it.current.pos;
10309 else
10310 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10311 SET_MARKER_FROM_TEXT_POS (w->start, start);
10313 if (EQ (Vresize_mini_windows, Qgrow_only))
10315 /* Let it grow only, until we display an empty message, in which
10316 case the window shrinks again. */
10317 if (height > WINDOW_TOTAL_LINES (w))
10319 int old_height = WINDOW_TOTAL_LINES (w);
10320 freeze_window_starts (f, 1);
10321 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10322 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10324 else if (height < WINDOW_TOTAL_LINES (w)
10325 && (exact_p || BEGV == ZV))
10327 int old_height = WINDOW_TOTAL_LINES (w);
10328 freeze_window_starts (f, 0);
10329 shrink_mini_window (w);
10330 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10333 else
10335 /* Always resize to exact size needed. */
10336 if (height > WINDOW_TOTAL_LINES (w))
10338 int old_height = WINDOW_TOTAL_LINES (w);
10339 freeze_window_starts (f, 1);
10340 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10341 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10343 else if (height < WINDOW_TOTAL_LINES (w))
10345 int old_height = WINDOW_TOTAL_LINES (w);
10346 freeze_window_starts (f, 0);
10347 shrink_mini_window (w);
10349 if (height)
10351 freeze_window_starts (f, 1);
10352 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10355 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10359 if (old_current_buffer)
10360 set_buffer_internal (old_current_buffer);
10363 return window_height_changed_p;
10367 /* Value is the current message, a string, or nil if there is no
10368 current message. */
10370 Lisp_Object
10371 current_message (void)
10373 Lisp_Object msg;
10375 if (!BUFFERP (echo_area_buffer[0]))
10376 msg = Qnil;
10377 else
10379 with_echo_area_buffer (0, 0, current_message_1,
10380 (intptr_t) &msg, Qnil, 0, 0);
10381 if (NILP (msg))
10382 echo_area_buffer[0] = Qnil;
10385 return msg;
10389 static int
10390 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10392 intptr_t i1 = a1;
10393 Lisp_Object *msg = (Lisp_Object *) i1;
10395 if (Z > BEG)
10396 *msg = make_buffer_string (BEG, Z, 1);
10397 else
10398 *msg = Qnil;
10399 return 0;
10403 /* Push the current message on Vmessage_stack for later restoration
10404 by restore_message. Value is non-zero if the current message isn't
10405 empty. This is a relatively infrequent operation, so it's not
10406 worth optimizing. */
10409 push_message (void)
10411 Lisp_Object msg;
10412 msg = current_message ();
10413 Vmessage_stack = Fcons (msg, Vmessage_stack);
10414 return STRINGP (msg);
10418 /* Restore message display from the top of Vmessage_stack. */
10420 void
10421 restore_message (void)
10423 Lisp_Object msg;
10425 xassert (CONSP (Vmessage_stack));
10426 msg = XCAR (Vmessage_stack);
10427 if (STRINGP (msg))
10428 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10429 else
10430 message3_nolog (msg, 0, 0);
10434 /* Handler for record_unwind_protect calling pop_message. */
10436 Lisp_Object
10437 pop_message_unwind (Lisp_Object dummy)
10439 pop_message ();
10440 return Qnil;
10443 /* Pop the top-most entry off Vmessage_stack. */
10445 static void
10446 pop_message (void)
10448 xassert (CONSP (Vmessage_stack));
10449 Vmessage_stack = XCDR (Vmessage_stack);
10453 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10454 exits. If the stack is not empty, we have a missing pop_message
10455 somewhere. */
10457 void
10458 check_message_stack (void)
10460 if (!NILP (Vmessage_stack))
10461 abort ();
10465 /* Truncate to NCHARS what will be displayed in the echo area the next
10466 time we display it---but don't redisplay it now. */
10468 void
10469 truncate_echo_area (ptrdiff_t nchars)
10471 if (nchars == 0)
10472 echo_area_buffer[0] = Qnil;
10473 /* A null message buffer means that the frame hasn't really been
10474 initialized yet. Error messages get reported properly by
10475 cmd_error, so this must be just an informative message; toss it. */
10476 else if (!noninteractive
10477 && INTERACTIVE
10478 && !NILP (echo_area_buffer[0]))
10480 struct frame *sf = SELECTED_FRAME ();
10481 if (FRAME_MESSAGE_BUF (sf))
10482 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10487 /* Helper function for truncate_echo_area. Truncate the current
10488 message to at most NCHARS characters. */
10490 static int
10491 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10493 if (BEG + nchars < Z)
10494 del_range (BEG + nchars, Z);
10495 if (Z == BEG)
10496 echo_area_buffer[0] = Qnil;
10497 return 0;
10501 /* Set the current message to a substring of S or STRING.
10503 If STRING is a Lisp string, set the message to the first NBYTES
10504 bytes from STRING. NBYTES zero means use the whole string. If
10505 STRING is multibyte, the message will be displayed multibyte.
10507 If S is not null, set the message to the first LEN bytes of S. LEN
10508 zero means use the whole string. MULTIBYTE_P non-zero means S is
10509 multibyte. Display the message multibyte in that case.
10511 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10512 to t before calling set_message_1 (which calls insert).
10515 static void
10516 set_message (const char *s, Lisp_Object string,
10517 ptrdiff_t nbytes, int multibyte_p)
10519 message_enable_multibyte
10520 = ((s && multibyte_p)
10521 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10523 with_echo_area_buffer (0, -1, set_message_1,
10524 (intptr_t) s, string, nbytes, multibyte_p);
10525 message_buf_print = 0;
10526 help_echo_showing_p = 0;
10530 /* Helper function for set_message. Arguments have the same meaning
10531 as there, with A1 corresponding to S and A2 corresponding to STRING
10532 This function is called with the echo area buffer being
10533 current. */
10535 static int
10536 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10538 intptr_t i1 = a1;
10539 const char *s = (const char *) i1;
10540 const unsigned char *msg = (const unsigned char *) s;
10541 Lisp_Object string = a2;
10543 /* Change multibyteness of the echo buffer appropriately. */
10544 if (message_enable_multibyte
10545 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10546 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10548 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10549 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10550 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10552 /* Insert new message at BEG. */
10553 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10555 if (STRINGP (string))
10557 ptrdiff_t nchars;
10559 if (nbytes == 0)
10560 nbytes = SBYTES (string);
10561 nchars = string_byte_to_char (string, nbytes);
10563 /* This function takes care of single/multibyte conversion. We
10564 just have to ensure that the echo area buffer has the right
10565 setting of enable_multibyte_characters. */
10566 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10568 else if (s)
10570 if (nbytes == 0)
10571 nbytes = strlen (s);
10573 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10575 /* Convert from multi-byte to single-byte. */
10576 ptrdiff_t i;
10577 int c, n;
10578 char work[1];
10580 /* Convert a multibyte string to single-byte. */
10581 for (i = 0; i < nbytes; i += n)
10583 c = string_char_and_length (msg + i, &n);
10584 work[0] = (ASCII_CHAR_P (c)
10586 : multibyte_char_to_unibyte (c));
10587 insert_1_both (work, 1, 1, 1, 0, 0);
10590 else if (!multibyte_p
10591 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10593 /* Convert from single-byte to multi-byte. */
10594 ptrdiff_t i;
10595 int c, n;
10596 unsigned char str[MAX_MULTIBYTE_LENGTH];
10598 /* Convert a single-byte string to multibyte. */
10599 for (i = 0; i < nbytes; i++)
10601 c = msg[i];
10602 MAKE_CHAR_MULTIBYTE (c);
10603 n = CHAR_STRING (c, str);
10604 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10607 else
10608 insert_1 (s, nbytes, 1, 0, 0);
10611 return 0;
10615 /* Clear messages. CURRENT_P non-zero means clear the current
10616 message. LAST_DISPLAYED_P non-zero means clear the message
10617 last displayed. */
10619 void
10620 clear_message (int current_p, int last_displayed_p)
10622 if (current_p)
10624 echo_area_buffer[0] = Qnil;
10625 message_cleared_p = 1;
10628 if (last_displayed_p)
10629 echo_area_buffer[1] = Qnil;
10631 message_buf_print = 0;
10634 /* Clear garbaged frames.
10636 This function is used where the old redisplay called
10637 redraw_garbaged_frames which in turn called redraw_frame which in
10638 turn called clear_frame. The call to clear_frame was a source of
10639 flickering. I believe a clear_frame is not necessary. It should
10640 suffice in the new redisplay to invalidate all current matrices,
10641 and ensure a complete redisplay of all windows. */
10643 static void
10644 clear_garbaged_frames (void)
10646 if (frame_garbaged)
10648 Lisp_Object tail, frame;
10649 int changed_count = 0;
10651 FOR_EACH_FRAME (tail, frame)
10653 struct frame *f = XFRAME (frame);
10655 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10657 if (f->resized_p)
10659 Fredraw_frame (frame);
10660 f->force_flush_display_p = 1;
10662 clear_current_matrices (f);
10663 changed_count++;
10664 f->garbaged = 0;
10665 f->resized_p = 0;
10669 frame_garbaged = 0;
10670 if (changed_count)
10671 ++windows_or_buffers_changed;
10676 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10677 is non-zero update selected_frame. Value is non-zero if the
10678 mini-windows height has been changed. */
10680 static int
10681 echo_area_display (int update_frame_p)
10683 Lisp_Object mini_window;
10684 struct window *w;
10685 struct frame *f;
10686 int window_height_changed_p = 0;
10687 struct frame *sf = SELECTED_FRAME ();
10689 mini_window = FRAME_MINIBUF_WINDOW (sf);
10690 w = XWINDOW (mini_window);
10691 f = XFRAME (WINDOW_FRAME (w));
10693 /* Don't display if frame is invisible or not yet initialized. */
10694 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10695 return 0;
10697 #ifdef HAVE_WINDOW_SYSTEM
10698 /* When Emacs starts, selected_frame may be the initial terminal
10699 frame. If we let this through, a message would be displayed on
10700 the terminal. */
10701 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10702 return 0;
10703 #endif /* HAVE_WINDOW_SYSTEM */
10705 /* Redraw garbaged frames. */
10706 if (frame_garbaged)
10707 clear_garbaged_frames ();
10709 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10711 echo_area_window = mini_window;
10712 window_height_changed_p = display_echo_area (w);
10713 w->must_be_updated_p = 1;
10715 /* Update the display, unless called from redisplay_internal.
10716 Also don't update the screen during redisplay itself. The
10717 update will happen at the end of redisplay, and an update
10718 here could cause confusion. */
10719 if (update_frame_p && !redisplaying_p)
10721 int n = 0;
10723 /* If the display update has been interrupted by pending
10724 input, update mode lines in the frame. Due to the
10725 pending input, it might have been that redisplay hasn't
10726 been called, so that mode lines above the echo area are
10727 garbaged. This looks odd, so we prevent it here. */
10728 if (!display_completed)
10729 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10731 if (window_height_changed_p
10732 /* Don't do this if Emacs is shutting down. Redisplay
10733 needs to run hooks. */
10734 && !NILP (Vrun_hooks))
10736 /* Must update other windows. Likewise as in other
10737 cases, don't let this update be interrupted by
10738 pending input. */
10739 ptrdiff_t count = SPECPDL_INDEX ();
10740 specbind (Qredisplay_dont_pause, Qt);
10741 windows_or_buffers_changed = 1;
10742 redisplay_internal ();
10743 unbind_to (count, Qnil);
10745 else if (FRAME_WINDOW_P (f) && n == 0)
10747 /* Window configuration is the same as before.
10748 Can do with a display update of the echo area,
10749 unless we displayed some mode lines. */
10750 update_single_window (w, 1);
10751 FRAME_RIF (f)->flush_display (f);
10753 else
10754 update_frame (f, 1, 1);
10756 /* If cursor is in the echo area, make sure that the next
10757 redisplay displays the minibuffer, so that the cursor will
10758 be replaced with what the minibuffer wants. */
10759 if (cursor_in_echo_area)
10760 ++windows_or_buffers_changed;
10763 else if (!EQ (mini_window, selected_window))
10764 windows_or_buffers_changed++;
10766 /* Last displayed message is now the current message. */
10767 echo_area_buffer[1] = echo_area_buffer[0];
10768 /* Inform read_char that we're not echoing. */
10769 echo_message_buffer = Qnil;
10771 /* Prevent redisplay optimization in redisplay_internal by resetting
10772 this_line_start_pos. This is done because the mini-buffer now
10773 displays the message instead of its buffer text. */
10774 if (EQ (mini_window, selected_window))
10775 CHARPOS (this_line_start_pos) = 0;
10777 return window_height_changed_p;
10782 /***********************************************************************
10783 Mode Lines and Frame Titles
10784 ***********************************************************************/
10786 /* A buffer for constructing non-propertized mode-line strings and
10787 frame titles in it; allocated from the heap in init_xdisp and
10788 resized as needed in store_mode_line_noprop_char. */
10790 static char *mode_line_noprop_buf;
10792 /* The buffer's end, and a current output position in it. */
10794 static char *mode_line_noprop_buf_end;
10795 static char *mode_line_noprop_ptr;
10797 #define MODE_LINE_NOPROP_LEN(start) \
10798 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10800 static enum {
10801 MODE_LINE_DISPLAY = 0,
10802 MODE_LINE_TITLE,
10803 MODE_LINE_NOPROP,
10804 MODE_LINE_STRING
10805 } mode_line_target;
10807 /* Alist that caches the results of :propertize.
10808 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10809 static Lisp_Object mode_line_proptrans_alist;
10811 /* List of strings making up the mode-line. */
10812 static Lisp_Object mode_line_string_list;
10814 /* Base face property when building propertized mode line string. */
10815 static Lisp_Object mode_line_string_face;
10816 static Lisp_Object mode_line_string_face_prop;
10819 /* Unwind data for mode line strings */
10821 static Lisp_Object Vmode_line_unwind_vector;
10823 static Lisp_Object
10824 format_mode_line_unwind_data (struct buffer *obuf,
10825 Lisp_Object owin,
10826 int save_proptrans)
10828 Lisp_Object vector, tmp;
10830 /* Reduce consing by keeping one vector in
10831 Vwith_echo_area_save_vector. */
10832 vector = Vmode_line_unwind_vector;
10833 Vmode_line_unwind_vector = Qnil;
10835 if (NILP (vector))
10836 vector = Fmake_vector (make_number (8), Qnil);
10838 ASET (vector, 0, make_number (mode_line_target));
10839 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10840 ASET (vector, 2, mode_line_string_list);
10841 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10842 ASET (vector, 4, mode_line_string_face);
10843 ASET (vector, 5, mode_line_string_face_prop);
10845 if (obuf)
10846 XSETBUFFER (tmp, obuf);
10847 else
10848 tmp = Qnil;
10849 ASET (vector, 6, tmp);
10850 ASET (vector, 7, owin);
10852 return vector;
10855 static Lisp_Object
10856 unwind_format_mode_line (Lisp_Object vector)
10858 mode_line_target = XINT (AREF (vector, 0));
10859 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10860 mode_line_string_list = AREF (vector, 2);
10861 if (! EQ (AREF (vector, 3), Qt))
10862 mode_line_proptrans_alist = AREF (vector, 3);
10863 mode_line_string_face = AREF (vector, 4);
10864 mode_line_string_face_prop = AREF (vector, 5);
10866 if (!NILP (AREF (vector, 7)))
10867 /* Select window before buffer, since it may change the buffer. */
10868 Fselect_window (AREF (vector, 7), Qt);
10870 if (!NILP (AREF (vector, 6)))
10872 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10873 ASET (vector, 6, Qnil);
10876 Vmode_line_unwind_vector = vector;
10877 return Qnil;
10881 /* Store a single character C for the frame title in mode_line_noprop_buf.
10882 Re-allocate mode_line_noprop_buf if necessary. */
10884 static void
10885 store_mode_line_noprop_char (char c)
10887 /* If output position has reached the end of the allocated buffer,
10888 increase the buffer's size. */
10889 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10891 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10892 ptrdiff_t size = len;
10893 mode_line_noprop_buf =
10894 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10895 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10896 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10899 *mode_line_noprop_ptr++ = c;
10903 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10904 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10905 characters that yield more columns than PRECISION; PRECISION <= 0
10906 means copy the whole string. Pad with spaces until FIELD_WIDTH
10907 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10908 pad. Called from display_mode_element when it is used to build a
10909 frame title. */
10911 static int
10912 store_mode_line_noprop (const char *string, int field_width, int precision)
10914 const unsigned char *str = (const unsigned char *) string;
10915 int n = 0;
10916 ptrdiff_t dummy, nbytes;
10918 /* Copy at most PRECISION chars from STR. */
10919 nbytes = strlen (string);
10920 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10921 while (nbytes--)
10922 store_mode_line_noprop_char (*str++);
10924 /* Fill up with spaces until FIELD_WIDTH reached. */
10925 while (field_width > 0
10926 && n < field_width)
10928 store_mode_line_noprop_char (' ');
10929 ++n;
10932 return n;
10935 /***********************************************************************
10936 Frame Titles
10937 ***********************************************************************/
10939 #ifdef HAVE_WINDOW_SYSTEM
10941 /* Set the title of FRAME, if it has changed. The title format is
10942 Vicon_title_format if FRAME is iconified, otherwise it is
10943 frame_title_format. */
10945 static void
10946 x_consider_frame_title (Lisp_Object frame)
10948 struct frame *f = XFRAME (frame);
10950 if (FRAME_WINDOW_P (f)
10951 || FRAME_MINIBUF_ONLY_P (f)
10952 || f->explicit_name)
10954 /* Do we have more than one visible frame on this X display? */
10955 Lisp_Object tail;
10956 Lisp_Object fmt;
10957 ptrdiff_t title_start;
10958 char *title;
10959 ptrdiff_t len;
10960 struct it it;
10961 ptrdiff_t count = SPECPDL_INDEX ();
10963 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10965 Lisp_Object other_frame = XCAR (tail);
10966 struct frame *tf = XFRAME (other_frame);
10968 if (tf != f
10969 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10970 && !FRAME_MINIBUF_ONLY_P (tf)
10971 && !EQ (other_frame, tip_frame)
10972 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10973 break;
10976 /* Set global variable indicating that multiple frames exist. */
10977 multiple_frames = CONSP (tail);
10979 /* Switch to the buffer of selected window of the frame. Set up
10980 mode_line_target so that display_mode_element will output into
10981 mode_line_noprop_buf; then display the title. */
10982 record_unwind_protect (unwind_format_mode_line,
10983 format_mode_line_unwind_data
10984 (current_buffer, selected_window, 0));
10986 Fselect_window (f->selected_window, Qt);
10987 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10988 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10990 mode_line_target = MODE_LINE_TITLE;
10991 title_start = MODE_LINE_NOPROP_LEN (0);
10992 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10993 NULL, DEFAULT_FACE_ID);
10994 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10995 len = MODE_LINE_NOPROP_LEN (title_start);
10996 title = mode_line_noprop_buf + title_start;
10997 unbind_to (count, Qnil);
10999 /* Set the title only if it's changed. This avoids consing in
11000 the common case where it hasn't. (If it turns out that we've
11001 already wasted too much time by walking through the list with
11002 display_mode_element, then we might need to optimize at a
11003 higher level than this.) */
11004 if (! STRINGP (f->name)
11005 || SBYTES (f->name) != len
11006 || memcmp (title, SDATA (f->name), len) != 0)
11007 x_implicitly_set_name (f, make_string (title, len), Qnil);
11011 #endif /* not HAVE_WINDOW_SYSTEM */
11016 /***********************************************************************
11017 Menu Bars
11018 ***********************************************************************/
11021 /* Prepare for redisplay by updating menu-bar item lists when
11022 appropriate. This can call eval. */
11024 void
11025 prepare_menu_bars (void)
11027 int all_windows;
11028 struct gcpro gcpro1, gcpro2;
11029 struct frame *f;
11030 Lisp_Object tooltip_frame;
11032 #ifdef HAVE_WINDOW_SYSTEM
11033 tooltip_frame = tip_frame;
11034 #else
11035 tooltip_frame = Qnil;
11036 #endif
11038 /* Update all frame titles based on their buffer names, etc. We do
11039 this before the menu bars so that the buffer-menu will show the
11040 up-to-date frame titles. */
11041 #ifdef HAVE_WINDOW_SYSTEM
11042 if (windows_or_buffers_changed || update_mode_lines)
11044 Lisp_Object tail, frame;
11046 FOR_EACH_FRAME (tail, frame)
11048 f = XFRAME (frame);
11049 if (!EQ (frame, tooltip_frame)
11050 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11051 x_consider_frame_title (frame);
11054 #endif /* HAVE_WINDOW_SYSTEM */
11056 /* Update the menu bar item lists, if appropriate. This has to be
11057 done before any actual redisplay or generation of display lines. */
11058 all_windows = (update_mode_lines
11059 || buffer_shared > 1
11060 || windows_or_buffers_changed);
11061 if (all_windows)
11063 Lisp_Object tail, frame;
11064 ptrdiff_t count = SPECPDL_INDEX ();
11065 /* 1 means that update_menu_bar has run its hooks
11066 so any further calls to update_menu_bar shouldn't do so again. */
11067 int menu_bar_hooks_run = 0;
11069 record_unwind_save_match_data ();
11071 FOR_EACH_FRAME (tail, frame)
11073 f = XFRAME (frame);
11075 /* Ignore tooltip frame. */
11076 if (EQ (frame, tooltip_frame))
11077 continue;
11079 /* If a window on this frame changed size, report that to
11080 the user and clear the size-change flag. */
11081 if (FRAME_WINDOW_SIZES_CHANGED (f))
11083 Lisp_Object functions;
11085 /* Clear flag first in case we get an error below. */
11086 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11087 functions = Vwindow_size_change_functions;
11088 GCPRO2 (tail, functions);
11090 while (CONSP (functions))
11092 if (!EQ (XCAR (functions), Qt))
11093 call1 (XCAR (functions), frame);
11094 functions = XCDR (functions);
11096 UNGCPRO;
11099 GCPRO1 (tail);
11100 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11101 #ifdef HAVE_WINDOW_SYSTEM
11102 update_tool_bar (f, 0);
11103 #endif
11104 #ifdef HAVE_NS
11105 if (windows_or_buffers_changed
11106 && FRAME_NS_P (f))
11107 ns_set_doc_edited (f, Fbuffer_modified_p
11108 (XWINDOW (f->selected_window)->buffer));
11109 #endif
11110 UNGCPRO;
11113 unbind_to (count, Qnil);
11115 else
11117 struct frame *sf = SELECTED_FRAME ();
11118 update_menu_bar (sf, 1, 0);
11119 #ifdef HAVE_WINDOW_SYSTEM
11120 update_tool_bar (sf, 1);
11121 #endif
11126 /* Update the menu bar item list for frame F. This has to be done
11127 before we start to fill in any display lines, because it can call
11128 eval.
11130 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11132 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11133 already ran the menu bar hooks for this redisplay, so there
11134 is no need to run them again. The return value is the
11135 updated value of this flag, to pass to the next call. */
11137 static int
11138 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11140 Lisp_Object window;
11141 register struct window *w;
11143 /* If called recursively during a menu update, do nothing. This can
11144 happen when, for instance, an activate-menubar-hook causes a
11145 redisplay. */
11146 if (inhibit_menubar_update)
11147 return hooks_run;
11149 window = FRAME_SELECTED_WINDOW (f);
11150 w = XWINDOW (window);
11152 if (FRAME_WINDOW_P (f)
11154 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11155 || defined (HAVE_NS) || defined (USE_GTK)
11156 FRAME_EXTERNAL_MENU_BAR (f)
11157 #else
11158 FRAME_MENU_BAR_LINES (f) > 0
11159 #endif
11160 : FRAME_MENU_BAR_LINES (f) > 0)
11162 /* If the user has switched buffers or windows, we need to
11163 recompute to reflect the new bindings. But we'll
11164 recompute when update_mode_lines is set too; that means
11165 that people can use force-mode-line-update to request
11166 that the menu bar be recomputed. The adverse effect on
11167 the rest of the redisplay algorithm is about the same as
11168 windows_or_buffers_changed anyway. */
11169 if (windows_or_buffers_changed
11170 /* This used to test w->update_mode_line, but we believe
11171 there is no need to recompute the menu in that case. */
11172 || update_mode_lines
11173 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11174 < BUF_MODIFF (XBUFFER (w->buffer)))
11175 != w->last_had_star)
11176 || ((!NILP (Vtransient_mark_mode)
11177 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11178 != !NILP (w->region_showing)))
11180 struct buffer *prev = current_buffer;
11181 ptrdiff_t count = SPECPDL_INDEX ();
11183 specbind (Qinhibit_menubar_update, Qt);
11185 set_buffer_internal_1 (XBUFFER (w->buffer));
11186 if (save_match_data)
11187 record_unwind_save_match_data ();
11188 if (NILP (Voverriding_local_map_menu_flag))
11190 specbind (Qoverriding_terminal_local_map, Qnil);
11191 specbind (Qoverriding_local_map, Qnil);
11194 if (!hooks_run)
11196 /* Run the Lucid hook. */
11197 safe_run_hooks (Qactivate_menubar_hook);
11199 /* If it has changed current-menubar from previous value,
11200 really recompute the menu-bar from the value. */
11201 if (! NILP (Vlucid_menu_bar_dirty_flag))
11202 call0 (Qrecompute_lucid_menubar);
11204 safe_run_hooks (Qmenu_bar_update_hook);
11206 hooks_run = 1;
11209 XSETFRAME (Vmenu_updating_frame, f);
11210 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11212 /* Redisplay the menu bar in case we changed it. */
11213 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11214 || defined (HAVE_NS) || defined (USE_GTK)
11215 if (FRAME_WINDOW_P (f))
11217 #if defined (HAVE_NS)
11218 /* All frames on Mac OS share the same menubar. So only
11219 the selected frame should be allowed to set it. */
11220 if (f == SELECTED_FRAME ())
11221 #endif
11222 set_frame_menubar (f, 0, 0);
11224 else
11225 /* On a terminal screen, the menu bar is an ordinary screen
11226 line, and this makes it get updated. */
11227 w->update_mode_line = 1;
11228 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11229 /* In the non-toolkit version, the menu bar is an ordinary screen
11230 line, and this makes it get updated. */
11231 w->update_mode_line = 1;
11232 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11234 unbind_to (count, Qnil);
11235 set_buffer_internal_1 (prev);
11239 return hooks_run;
11244 /***********************************************************************
11245 Output Cursor
11246 ***********************************************************************/
11248 #ifdef HAVE_WINDOW_SYSTEM
11250 /* EXPORT:
11251 Nominal cursor position -- where to draw output.
11252 HPOS and VPOS are window relative glyph matrix coordinates.
11253 X and Y are window relative pixel coordinates. */
11255 struct cursor_pos output_cursor;
11258 /* EXPORT:
11259 Set the global variable output_cursor to CURSOR. All cursor
11260 positions are relative to updated_window. */
11262 void
11263 set_output_cursor (struct cursor_pos *cursor)
11265 output_cursor.hpos = cursor->hpos;
11266 output_cursor.vpos = cursor->vpos;
11267 output_cursor.x = cursor->x;
11268 output_cursor.y = cursor->y;
11272 /* EXPORT for RIF:
11273 Set a nominal cursor position.
11275 HPOS and VPOS are column/row positions in a window glyph matrix. X
11276 and Y are window text area relative pixel positions.
11278 If this is done during an update, updated_window will contain the
11279 window that is being updated and the position is the future output
11280 cursor position for that window. If updated_window is null, use
11281 selected_window and display the cursor at the given position. */
11283 void
11284 x_cursor_to (int vpos, int hpos, int y, int x)
11286 struct window *w;
11288 /* If updated_window is not set, work on selected_window. */
11289 if (updated_window)
11290 w = updated_window;
11291 else
11292 w = XWINDOW (selected_window);
11294 /* Set the output cursor. */
11295 output_cursor.hpos = hpos;
11296 output_cursor.vpos = vpos;
11297 output_cursor.x = x;
11298 output_cursor.y = y;
11300 /* If not called as part of an update, really display the cursor.
11301 This will also set the cursor position of W. */
11302 if (updated_window == NULL)
11304 BLOCK_INPUT;
11305 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11306 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11307 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11308 UNBLOCK_INPUT;
11312 #endif /* HAVE_WINDOW_SYSTEM */
11315 /***********************************************************************
11316 Tool-bars
11317 ***********************************************************************/
11319 #ifdef HAVE_WINDOW_SYSTEM
11321 /* Where the mouse was last time we reported a mouse event. */
11323 FRAME_PTR last_mouse_frame;
11325 /* Tool-bar item index of the item on which a mouse button was pressed
11326 or -1. */
11328 int last_tool_bar_item;
11331 static Lisp_Object
11332 update_tool_bar_unwind (Lisp_Object frame)
11334 selected_frame = frame;
11335 return Qnil;
11338 /* Update the tool-bar item list for frame F. This has to be done
11339 before we start to fill in any display lines. Called from
11340 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11341 and restore it here. */
11343 static void
11344 update_tool_bar (struct frame *f, int save_match_data)
11346 #if defined (USE_GTK) || defined (HAVE_NS)
11347 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11348 #else
11349 int do_update = WINDOWP (f->tool_bar_window)
11350 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11351 #endif
11353 if (do_update)
11355 Lisp_Object window;
11356 struct window *w;
11358 window = FRAME_SELECTED_WINDOW (f);
11359 w = XWINDOW (window);
11361 /* If the user has switched buffers or windows, we need to
11362 recompute to reflect the new bindings. But we'll
11363 recompute when update_mode_lines is set too; that means
11364 that people can use force-mode-line-update to request
11365 that the menu bar be recomputed. The adverse effect on
11366 the rest of the redisplay algorithm is about the same as
11367 windows_or_buffers_changed anyway. */
11368 if (windows_or_buffers_changed
11369 || w->update_mode_line
11370 || update_mode_lines
11371 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11372 < BUF_MODIFF (XBUFFER (w->buffer)))
11373 != w->last_had_star)
11374 || ((!NILP (Vtransient_mark_mode)
11375 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11376 != !NILP (w->region_showing)))
11378 struct buffer *prev = current_buffer;
11379 ptrdiff_t count = SPECPDL_INDEX ();
11380 Lisp_Object frame, new_tool_bar;
11381 int new_n_tool_bar;
11382 struct gcpro gcpro1;
11384 /* Set current_buffer to the buffer of the selected
11385 window of the frame, so that we get the right local
11386 keymaps. */
11387 set_buffer_internal_1 (XBUFFER (w->buffer));
11389 /* Save match data, if we must. */
11390 if (save_match_data)
11391 record_unwind_save_match_data ();
11393 /* Make sure that we don't accidentally use bogus keymaps. */
11394 if (NILP (Voverriding_local_map_menu_flag))
11396 specbind (Qoverriding_terminal_local_map, Qnil);
11397 specbind (Qoverriding_local_map, Qnil);
11400 GCPRO1 (new_tool_bar);
11402 /* We must temporarily set the selected frame to this frame
11403 before calling tool_bar_items, because the calculation of
11404 the tool-bar keymap uses the selected frame (see
11405 `tool-bar-make-keymap' in tool-bar.el). */
11406 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11407 XSETFRAME (frame, f);
11408 selected_frame = frame;
11410 /* Build desired tool-bar items from keymaps. */
11411 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11412 &new_n_tool_bar);
11414 /* Redisplay the tool-bar if we changed it. */
11415 if (new_n_tool_bar != f->n_tool_bar_items
11416 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11418 /* Redisplay that happens asynchronously due to an expose event
11419 may access f->tool_bar_items. Make sure we update both
11420 variables within BLOCK_INPUT so no such event interrupts. */
11421 BLOCK_INPUT;
11422 f->tool_bar_items = new_tool_bar;
11423 f->n_tool_bar_items = new_n_tool_bar;
11424 w->update_mode_line = 1;
11425 UNBLOCK_INPUT;
11428 UNGCPRO;
11430 unbind_to (count, Qnil);
11431 set_buffer_internal_1 (prev);
11437 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11438 F's desired tool-bar contents. F->tool_bar_items must have
11439 been set up previously by calling prepare_menu_bars. */
11441 static void
11442 build_desired_tool_bar_string (struct frame *f)
11444 int i, size, size_needed;
11445 struct gcpro gcpro1, gcpro2, gcpro3;
11446 Lisp_Object image, plist, props;
11448 image = plist = props = Qnil;
11449 GCPRO3 (image, plist, props);
11451 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11452 Otherwise, make a new string. */
11454 /* The size of the string we might be able to reuse. */
11455 size = (STRINGP (f->desired_tool_bar_string)
11456 ? SCHARS (f->desired_tool_bar_string)
11457 : 0);
11459 /* We need one space in the string for each image. */
11460 size_needed = f->n_tool_bar_items;
11462 /* Reuse f->desired_tool_bar_string, if possible. */
11463 if (size < size_needed || NILP (f->desired_tool_bar_string))
11464 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11465 make_number (' '));
11466 else
11468 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11469 Fremove_text_properties (make_number (0), make_number (size),
11470 props, f->desired_tool_bar_string);
11473 /* Put a `display' property on the string for the images to display,
11474 put a `menu_item' property on tool-bar items with a value that
11475 is the index of the item in F's tool-bar item vector. */
11476 for (i = 0; i < f->n_tool_bar_items; ++i)
11478 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11480 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11481 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11482 int hmargin, vmargin, relief, idx, end;
11484 /* If image is a vector, choose the image according to the
11485 button state. */
11486 image = PROP (TOOL_BAR_ITEM_IMAGES);
11487 if (VECTORP (image))
11489 if (enabled_p)
11490 idx = (selected_p
11491 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11492 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11493 else
11494 idx = (selected_p
11495 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11496 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11498 xassert (ASIZE (image) >= idx);
11499 image = AREF (image, idx);
11501 else
11502 idx = -1;
11504 /* Ignore invalid image specifications. */
11505 if (!valid_image_p (image))
11506 continue;
11508 /* Display the tool-bar button pressed, or depressed. */
11509 plist = Fcopy_sequence (XCDR (image));
11511 /* Compute margin and relief to draw. */
11512 relief = (tool_bar_button_relief >= 0
11513 ? tool_bar_button_relief
11514 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11515 hmargin = vmargin = relief;
11517 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11518 INT_MAX - max (hmargin, vmargin)))
11520 hmargin += XFASTINT (Vtool_bar_button_margin);
11521 vmargin += XFASTINT (Vtool_bar_button_margin);
11523 else if (CONSP (Vtool_bar_button_margin))
11525 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11526 INT_MAX - hmargin))
11527 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11529 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11530 INT_MAX - vmargin))
11531 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11534 if (auto_raise_tool_bar_buttons_p)
11536 /* Add a `:relief' property to the image spec if the item is
11537 selected. */
11538 if (selected_p)
11540 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11541 hmargin -= relief;
11542 vmargin -= relief;
11545 else
11547 /* If image is selected, display it pressed, i.e. with a
11548 negative relief. If it's not selected, display it with a
11549 raised relief. */
11550 plist = Fplist_put (plist, QCrelief,
11551 (selected_p
11552 ? make_number (-relief)
11553 : make_number (relief)));
11554 hmargin -= relief;
11555 vmargin -= relief;
11558 /* Put a margin around the image. */
11559 if (hmargin || vmargin)
11561 if (hmargin == vmargin)
11562 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11563 else
11564 plist = Fplist_put (plist, QCmargin,
11565 Fcons (make_number (hmargin),
11566 make_number (vmargin)));
11569 /* If button is not enabled, and we don't have special images
11570 for the disabled state, make the image appear disabled by
11571 applying an appropriate algorithm to it. */
11572 if (!enabled_p && idx < 0)
11573 plist = Fplist_put (plist, QCconversion, Qdisabled);
11575 /* Put a `display' text property on the string for the image to
11576 display. Put a `menu-item' property on the string that gives
11577 the start of this item's properties in the tool-bar items
11578 vector. */
11579 image = Fcons (Qimage, plist);
11580 props = list4 (Qdisplay, image,
11581 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11583 /* Let the last image hide all remaining spaces in the tool bar
11584 string. The string can be longer than needed when we reuse a
11585 previous string. */
11586 if (i + 1 == f->n_tool_bar_items)
11587 end = SCHARS (f->desired_tool_bar_string);
11588 else
11589 end = i + 1;
11590 Fadd_text_properties (make_number (i), make_number (end),
11591 props, f->desired_tool_bar_string);
11592 #undef PROP
11595 UNGCPRO;
11599 /* Display one line of the tool-bar of frame IT->f.
11601 HEIGHT specifies the desired height of the tool-bar line.
11602 If the actual height of the glyph row is less than HEIGHT, the
11603 row's height is increased to HEIGHT, and the icons are centered
11604 vertically in the new height.
11606 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11607 count a final empty row in case the tool-bar width exactly matches
11608 the window width.
11611 static void
11612 display_tool_bar_line (struct it *it, int height)
11614 struct glyph_row *row = it->glyph_row;
11615 int max_x = it->last_visible_x;
11616 struct glyph *last;
11618 prepare_desired_row (row);
11619 row->y = it->current_y;
11621 /* Note that this isn't made use of if the face hasn't a box,
11622 so there's no need to check the face here. */
11623 it->start_of_box_run_p = 1;
11625 while (it->current_x < max_x)
11627 int x, n_glyphs_before, i, nglyphs;
11628 struct it it_before;
11630 /* Get the next display element. */
11631 if (!get_next_display_element (it))
11633 /* Don't count empty row if we are counting needed tool-bar lines. */
11634 if (height < 0 && !it->hpos)
11635 return;
11636 break;
11639 /* Produce glyphs. */
11640 n_glyphs_before = row->used[TEXT_AREA];
11641 it_before = *it;
11643 PRODUCE_GLYPHS (it);
11645 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11646 i = 0;
11647 x = it_before.current_x;
11648 while (i < nglyphs)
11650 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11652 if (x + glyph->pixel_width > max_x)
11654 /* Glyph doesn't fit on line. Backtrack. */
11655 row->used[TEXT_AREA] = n_glyphs_before;
11656 *it = it_before;
11657 /* If this is the only glyph on this line, it will never fit on the
11658 tool-bar, so skip it. But ensure there is at least one glyph,
11659 so we don't accidentally disable the tool-bar. */
11660 if (n_glyphs_before == 0
11661 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11662 break;
11663 goto out;
11666 ++it->hpos;
11667 x += glyph->pixel_width;
11668 ++i;
11671 /* Stop at line end. */
11672 if (ITERATOR_AT_END_OF_LINE_P (it))
11673 break;
11675 set_iterator_to_next (it, 1);
11678 out:;
11680 row->displays_text_p = row->used[TEXT_AREA] != 0;
11682 /* Use default face for the border below the tool bar.
11684 FIXME: When auto-resize-tool-bars is grow-only, there is
11685 no additional border below the possibly empty tool-bar lines.
11686 So to make the extra empty lines look "normal", we have to
11687 use the tool-bar face for the border too. */
11688 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11689 it->face_id = DEFAULT_FACE_ID;
11691 extend_face_to_end_of_line (it);
11692 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11693 last->right_box_line_p = 1;
11694 if (last == row->glyphs[TEXT_AREA])
11695 last->left_box_line_p = 1;
11697 /* Make line the desired height and center it vertically. */
11698 if ((height -= it->max_ascent + it->max_descent) > 0)
11700 /* Don't add more than one line height. */
11701 height %= FRAME_LINE_HEIGHT (it->f);
11702 it->max_ascent += height / 2;
11703 it->max_descent += (height + 1) / 2;
11706 compute_line_metrics (it);
11708 /* If line is empty, make it occupy the rest of the tool-bar. */
11709 if (!row->displays_text_p)
11711 row->height = row->phys_height = it->last_visible_y - row->y;
11712 row->visible_height = row->height;
11713 row->ascent = row->phys_ascent = 0;
11714 row->extra_line_spacing = 0;
11717 row->full_width_p = 1;
11718 row->continued_p = 0;
11719 row->truncated_on_left_p = 0;
11720 row->truncated_on_right_p = 0;
11722 it->current_x = it->hpos = 0;
11723 it->current_y += row->height;
11724 ++it->vpos;
11725 ++it->glyph_row;
11729 /* Max tool-bar height. */
11731 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11732 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11734 /* Value is the number of screen lines needed to make all tool-bar
11735 items of frame F visible. The number of actual rows needed is
11736 returned in *N_ROWS if non-NULL. */
11738 static int
11739 tool_bar_lines_needed (struct frame *f, int *n_rows)
11741 struct window *w = XWINDOW (f->tool_bar_window);
11742 struct it it;
11743 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11744 the desired matrix, so use (unused) mode-line row as temporary row to
11745 avoid destroying the first tool-bar row. */
11746 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11748 /* Initialize an iterator for iteration over
11749 F->desired_tool_bar_string in the tool-bar window of frame F. */
11750 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11751 it.first_visible_x = 0;
11752 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11753 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11754 it.paragraph_embedding = L2R;
11756 while (!ITERATOR_AT_END_P (&it))
11758 clear_glyph_row (temp_row);
11759 it.glyph_row = temp_row;
11760 display_tool_bar_line (&it, -1);
11762 clear_glyph_row (temp_row);
11764 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11765 if (n_rows)
11766 *n_rows = it.vpos > 0 ? it.vpos : -1;
11768 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11772 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11773 0, 1, 0,
11774 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11775 (Lisp_Object frame)
11777 struct frame *f;
11778 struct window *w;
11779 int nlines = 0;
11781 if (NILP (frame))
11782 frame = selected_frame;
11783 else
11784 CHECK_FRAME (frame);
11785 f = XFRAME (frame);
11787 if (WINDOWP (f->tool_bar_window)
11788 && (w = XWINDOW (f->tool_bar_window),
11789 WINDOW_TOTAL_LINES (w) > 0))
11791 update_tool_bar (f, 1);
11792 if (f->n_tool_bar_items)
11794 build_desired_tool_bar_string (f);
11795 nlines = tool_bar_lines_needed (f, NULL);
11799 return make_number (nlines);
11803 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11804 height should be changed. */
11806 static int
11807 redisplay_tool_bar (struct frame *f)
11809 struct window *w;
11810 struct it it;
11811 struct glyph_row *row;
11813 #if defined (USE_GTK) || defined (HAVE_NS)
11814 if (FRAME_EXTERNAL_TOOL_BAR (f))
11815 update_frame_tool_bar (f);
11816 return 0;
11817 #endif
11819 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11820 do anything. This means you must start with tool-bar-lines
11821 non-zero to get the auto-sizing effect. Or in other words, you
11822 can turn off tool-bars by specifying tool-bar-lines zero. */
11823 if (!WINDOWP (f->tool_bar_window)
11824 || (w = XWINDOW (f->tool_bar_window),
11825 WINDOW_TOTAL_LINES (w) == 0))
11826 return 0;
11828 /* Set up an iterator for the tool-bar window. */
11829 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11830 it.first_visible_x = 0;
11831 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11832 row = it.glyph_row;
11834 /* Build a string that represents the contents of the tool-bar. */
11835 build_desired_tool_bar_string (f);
11836 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11837 /* FIXME: This should be controlled by a user option. But it
11838 doesn't make sense to have an R2L tool bar if the menu bar cannot
11839 be drawn also R2L, and making the menu bar R2L is tricky due
11840 toolkit-specific code that implements it. If an R2L tool bar is
11841 ever supported, display_tool_bar_line should also be augmented to
11842 call unproduce_glyphs like display_line and display_string
11843 do. */
11844 it.paragraph_embedding = L2R;
11846 if (f->n_tool_bar_rows == 0)
11848 int nlines;
11850 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11851 nlines != WINDOW_TOTAL_LINES (w)))
11853 Lisp_Object frame;
11854 int old_height = WINDOW_TOTAL_LINES (w);
11856 XSETFRAME (frame, f);
11857 Fmodify_frame_parameters (frame,
11858 Fcons (Fcons (Qtool_bar_lines,
11859 make_number (nlines)),
11860 Qnil));
11861 if (WINDOW_TOTAL_LINES (w) != old_height)
11863 clear_glyph_matrix (w->desired_matrix);
11864 fonts_changed_p = 1;
11865 return 1;
11870 /* Display as many lines as needed to display all tool-bar items. */
11872 if (f->n_tool_bar_rows > 0)
11874 int border, rows, height, extra;
11876 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11877 border = XINT (Vtool_bar_border);
11878 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11879 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11880 else if (EQ (Vtool_bar_border, Qborder_width))
11881 border = f->border_width;
11882 else
11883 border = 0;
11884 if (border < 0)
11885 border = 0;
11887 rows = f->n_tool_bar_rows;
11888 height = max (1, (it.last_visible_y - border) / rows);
11889 extra = it.last_visible_y - border - height * rows;
11891 while (it.current_y < it.last_visible_y)
11893 int h = 0;
11894 if (extra > 0 && rows-- > 0)
11896 h = (extra + rows - 1) / rows;
11897 extra -= h;
11899 display_tool_bar_line (&it, height + h);
11902 else
11904 while (it.current_y < it.last_visible_y)
11905 display_tool_bar_line (&it, 0);
11908 /* It doesn't make much sense to try scrolling in the tool-bar
11909 window, so don't do it. */
11910 w->desired_matrix->no_scrolling_p = 1;
11911 w->must_be_updated_p = 1;
11913 if (!NILP (Vauto_resize_tool_bars))
11915 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11916 int change_height_p = 0;
11918 /* If we couldn't display everything, change the tool-bar's
11919 height if there is room for more. */
11920 if (IT_STRING_CHARPOS (it) < it.end_charpos
11921 && it.current_y < max_tool_bar_height)
11922 change_height_p = 1;
11924 row = it.glyph_row - 1;
11926 /* If there are blank lines at the end, except for a partially
11927 visible blank line at the end that is smaller than
11928 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11929 if (!row->displays_text_p
11930 && row->height >= FRAME_LINE_HEIGHT (f))
11931 change_height_p = 1;
11933 /* If row displays tool-bar items, but is partially visible,
11934 change the tool-bar's height. */
11935 if (row->displays_text_p
11936 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11937 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11938 change_height_p = 1;
11940 /* Resize windows as needed by changing the `tool-bar-lines'
11941 frame parameter. */
11942 if (change_height_p)
11944 Lisp_Object frame;
11945 int old_height = WINDOW_TOTAL_LINES (w);
11946 int nrows;
11947 int nlines = tool_bar_lines_needed (f, &nrows);
11949 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11950 && !f->minimize_tool_bar_window_p)
11951 ? (nlines > old_height)
11952 : (nlines != old_height));
11953 f->minimize_tool_bar_window_p = 0;
11955 if (change_height_p)
11957 XSETFRAME (frame, f);
11958 Fmodify_frame_parameters (frame,
11959 Fcons (Fcons (Qtool_bar_lines,
11960 make_number (nlines)),
11961 Qnil));
11962 if (WINDOW_TOTAL_LINES (w) != old_height)
11964 clear_glyph_matrix (w->desired_matrix);
11965 f->n_tool_bar_rows = nrows;
11966 fonts_changed_p = 1;
11967 return 1;
11973 f->minimize_tool_bar_window_p = 0;
11974 return 0;
11978 /* Get information about the tool-bar item which is displayed in GLYPH
11979 on frame F. Return in *PROP_IDX the index where tool-bar item
11980 properties start in F->tool_bar_items. Value is zero if
11981 GLYPH doesn't display a tool-bar item. */
11983 static int
11984 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11986 Lisp_Object prop;
11987 int success_p;
11988 int charpos;
11990 /* This function can be called asynchronously, which means we must
11991 exclude any possibility that Fget_text_property signals an
11992 error. */
11993 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11994 charpos = max (0, charpos);
11996 /* Get the text property `menu-item' at pos. The value of that
11997 property is the start index of this item's properties in
11998 F->tool_bar_items. */
11999 prop = Fget_text_property (make_number (charpos),
12000 Qmenu_item, f->current_tool_bar_string);
12001 if (INTEGERP (prop))
12003 *prop_idx = XINT (prop);
12004 success_p = 1;
12006 else
12007 success_p = 0;
12009 return success_p;
12013 /* Get information about the tool-bar item at position X/Y on frame F.
12014 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12015 the current matrix of the tool-bar window of F, or NULL if not
12016 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12017 item in F->tool_bar_items. Value is
12019 -1 if X/Y is not on a tool-bar item
12020 0 if X/Y is on the same item that was highlighted before.
12021 1 otherwise. */
12023 static int
12024 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12025 int *hpos, int *vpos, int *prop_idx)
12027 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12028 struct window *w = XWINDOW (f->tool_bar_window);
12029 int area;
12031 /* Find the glyph under X/Y. */
12032 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12033 if (*glyph == NULL)
12034 return -1;
12036 /* Get the start of this tool-bar item's properties in
12037 f->tool_bar_items. */
12038 if (!tool_bar_item_info (f, *glyph, prop_idx))
12039 return -1;
12041 /* Is mouse on the highlighted item? */
12042 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12043 && *vpos >= hlinfo->mouse_face_beg_row
12044 && *vpos <= hlinfo->mouse_face_end_row
12045 && (*vpos > hlinfo->mouse_face_beg_row
12046 || *hpos >= hlinfo->mouse_face_beg_col)
12047 && (*vpos < hlinfo->mouse_face_end_row
12048 || *hpos < hlinfo->mouse_face_end_col
12049 || hlinfo->mouse_face_past_end))
12050 return 0;
12052 return 1;
12056 /* EXPORT:
12057 Handle mouse button event on the tool-bar of frame F, at
12058 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12059 0 for button release. MODIFIERS is event modifiers for button
12060 release. */
12062 void
12063 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12064 int modifiers)
12066 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12067 struct window *w = XWINDOW (f->tool_bar_window);
12068 int hpos, vpos, prop_idx;
12069 struct glyph *glyph;
12070 Lisp_Object enabled_p;
12072 /* If not on the highlighted tool-bar item, return. */
12073 frame_to_window_pixel_xy (w, &x, &y);
12074 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12075 return;
12077 /* If item is disabled, do nothing. */
12078 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12079 if (NILP (enabled_p))
12080 return;
12082 if (down_p)
12084 /* Show item in pressed state. */
12085 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12086 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12087 last_tool_bar_item = prop_idx;
12089 else
12091 Lisp_Object key, frame;
12092 struct input_event event;
12093 EVENT_INIT (event);
12095 /* Show item in released state. */
12096 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12097 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12099 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12101 XSETFRAME (frame, f);
12102 event.kind = TOOL_BAR_EVENT;
12103 event.frame_or_window = frame;
12104 event.arg = frame;
12105 kbd_buffer_store_event (&event);
12107 event.kind = TOOL_BAR_EVENT;
12108 event.frame_or_window = frame;
12109 event.arg = key;
12110 event.modifiers = modifiers;
12111 kbd_buffer_store_event (&event);
12112 last_tool_bar_item = -1;
12117 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12118 tool-bar window-relative coordinates X/Y. Called from
12119 note_mouse_highlight. */
12121 static void
12122 note_tool_bar_highlight (struct frame *f, int x, int y)
12124 Lisp_Object window = f->tool_bar_window;
12125 struct window *w = XWINDOW (window);
12126 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12127 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12128 int hpos, vpos;
12129 struct glyph *glyph;
12130 struct glyph_row *row;
12131 int i;
12132 Lisp_Object enabled_p;
12133 int prop_idx;
12134 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12135 int mouse_down_p, rc;
12137 /* Function note_mouse_highlight is called with negative X/Y
12138 values when mouse moves outside of the frame. */
12139 if (x <= 0 || y <= 0)
12141 clear_mouse_face (hlinfo);
12142 return;
12145 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12146 if (rc < 0)
12148 /* Not on tool-bar item. */
12149 clear_mouse_face (hlinfo);
12150 return;
12152 else if (rc == 0)
12153 /* On same tool-bar item as before. */
12154 goto set_help_echo;
12156 clear_mouse_face (hlinfo);
12158 /* Mouse is down, but on different tool-bar item? */
12159 mouse_down_p = (dpyinfo->grabbed
12160 && f == last_mouse_frame
12161 && FRAME_LIVE_P (f));
12162 if (mouse_down_p
12163 && last_tool_bar_item != prop_idx)
12164 return;
12166 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12167 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12169 /* If tool-bar item is not enabled, don't highlight it. */
12170 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12171 if (!NILP (enabled_p))
12173 /* Compute the x-position of the glyph. In front and past the
12174 image is a space. We include this in the highlighted area. */
12175 row = MATRIX_ROW (w->current_matrix, vpos);
12176 for (i = x = 0; i < hpos; ++i)
12177 x += row->glyphs[TEXT_AREA][i].pixel_width;
12179 /* Record this as the current active region. */
12180 hlinfo->mouse_face_beg_col = hpos;
12181 hlinfo->mouse_face_beg_row = vpos;
12182 hlinfo->mouse_face_beg_x = x;
12183 hlinfo->mouse_face_beg_y = row->y;
12184 hlinfo->mouse_face_past_end = 0;
12186 hlinfo->mouse_face_end_col = hpos + 1;
12187 hlinfo->mouse_face_end_row = vpos;
12188 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12189 hlinfo->mouse_face_end_y = row->y;
12190 hlinfo->mouse_face_window = window;
12191 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12193 /* Display it as active. */
12194 show_mouse_face (hlinfo, draw);
12195 hlinfo->mouse_face_image_state = draw;
12198 set_help_echo:
12200 /* Set help_echo_string to a help string to display for this tool-bar item.
12201 XTread_socket does the rest. */
12202 help_echo_object = help_echo_window = Qnil;
12203 help_echo_pos = -1;
12204 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12205 if (NILP (help_echo_string))
12206 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12209 #endif /* HAVE_WINDOW_SYSTEM */
12213 /************************************************************************
12214 Horizontal scrolling
12215 ************************************************************************/
12217 static int hscroll_window_tree (Lisp_Object);
12218 static int hscroll_windows (Lisp_Object);
12220 /* For all leaf windows in the window tree rooted at WINDOW, set their
12221 hscroll value so that PT is (i) visible in the window, and (ii) so
12222 that it is not within a certain margin at the window's left and
12223 right border. Value is non-zero if any window's hscroll has been
12224 changed. */
12226 static int
12227 hscroll_window_tree (Lisp_Object window)
12229 int hscrolled_p = 0;
12230 int hscroll_relative_p = FLOATP (Vhscroll_step);
12231 int hscroll_step_abs = 0;
12232 double hscroll_step_rel = 0;
12234 if (hscroll_relative_p)
12236 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12237 if (hscroll_step_rel < 0)
12239 hscroll_relative_p = 0;
12240 hscroll_step_abs = 0;
12243 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12245 hscroll_step_abs = XINT (Vhscroll_step);
12246 if (hscroll_step_abs < 0)
12247 hscroll_step_abs = 0;
12249 else
12250 hscroll_step_abs = 0;
12252 while (WINDOWP (window))
12254 struct window *w = XWINDOW (window);
12256 if (WINDOWP (w->hchild))
12257 hscrolled_p |= hscroll_window_tree (w->hchild);
12258 else if (WINDOWP (w->vchild))
12259 hscrolled_p |= hscroll_window_tree (w->vchild);
12260 else if (w->cursor.vpos >= 0)
12262 int h_margin;
12263 int text_area_width;
12264 struct glyph_row *current_cursor_row
12265 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12266 struct glyph_row *desired_cursor_row
12267 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12268 struct glyph_row *cursor_row
12269 = (desired_cursor_row->enabled_p
12270 ? desired_cursor_row
12271 : current_cursor_row);
12272 int row_r2l_p = cursor_row->reversed_p;
12274 text_area_width = window_box_width (w, TEXT_AREA);
12276 /* Scroll when cursor is inside this scroll margin. */
12277 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12279 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12280 /* For left-to-right rows, hscroll when cursor is either
12281 (i) inside the right hscroll margin, or (ii) if it is
12282 inside the left margin and the window is already
12283 hscrolled. */
12284 && ((!row_r2l_p
12285 && ((XFASTINT (w->hscroll)
12286 && w->cursor.x <= h_margin)
12287 || (cursor_row->enabled_p
12288 && cursor_row->truncated_on_right_p
12289 && (w->cursor.x >= text_area_width - h_margin))))
12290 /* For right-to-left rows, the logic is similar,
12291 except that rules for scrolling to left and right
12292 are reversed. E.g., if cursor.x <= h_margin, we
12293 need to hscroll "to the right" unconditionally,
12294 and that will scroll the screen to the left so as
12295 to reveal the next portion of the row. */
12296 || (row_r2l_p
12297 && ((cursor_row->enabled_p
12298 /* FIXME: It is confusing to set the
12299 truncated_on_right_p flag when R2L rows
12300 are actually truncated on the left. */
12301 && cursor_row->truncated_on_right_p
12302 && w->cursor.x <= h_margin)
12303 || (XFASTINT (w->hscroll)
12304 && (w->cursor.x >= text_area_width - h_margin))))))
12306 struct it it;
12307 ptrdiff_t hscroll;
12308 struct buffer *saved_current_buffer;
12309 ptrdiff_t pt;
12310 int wanted_x;
12312 /* Find point in a display of infinite width. */
12313 saved_current_buffer = current_buffer;
12314 current_buffer = XBUFFER (w->buffer);
12316 if (w == XWINDOW (selected_window))
12317 pt = PT;
12318 else
12320 pt = marker_position (w->pointm);
12321 pt = max (BEGV, pt);
12322 pt = min (ZV, pt);
12325 /* Move iterator to pt starting at cursor_row->start in
12326 a line with infinite width. */
12327 init_to_row_start (&it, w, cursor_row);
12328 it.last_visible_x = INFINITY;
12329 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12330 current_buffer = saved_current_buffer;
12332 /* Position cursor in window. */
12333 if (!hscroll_relative_p && hscroll_step_abs == 0)
12334 hscroll = max (0, (it.current_x
12335 - (ITERATOR_AT_END_OF_LINE_P (&it)
12336 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12337 : (text_area_width / 2))))
12338 / FRAME_COLUMN_WIDTH (it.f);
12339 else if ((!row_r2l_p
12340 && w->cursor.x >= text_area_width - h_margin)
12341 || (row_r2l_p && w->cursor.x <= h_margin))
12343 if (hscroll_relative_p)
12344 wanted_x = text_area_width * (1 - hscroll_step_rel)
12345 - h_margin;
12346 else
12347 wanted_x = text_area_width
12348 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12349 - h_margin;
12350 hscroll
12351 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12353 else
12355 if (hscroll_relative_p)
12356 wanted_x = text_area_width * hscroll_step_rel
12357 + h_margin;
12358 else
12359 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12360 + h_margin;
12361 hscroll
12362 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12364 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12366 /* Don't prevent redisplay optimizations if hscroll
12367 hasn't changed, as it will unnecessarily slow down
12368 redisplay. */
12369 if (XFASTINT (w->hscroll) != hscroll)
12371 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12372 w->hscroll = make_number (hscroll);
12373 hscrolled_p = 1;
12378 window = w->next;
12381 /* Value is non-zero if hscroll of any leaf window has been changed. */
12382 return hscrolled_p;
12386 /* Set hscroll so that cursor is visible and not inside horizontal
12387 scroll margins for all windows in the tree rooted at WINDOW. See
12388 also hscroll_window_tree above. Value is non-zero if any window's
12389 hscroll has been changed. If it has, desired matrices on the frame
12390 of WINDOW are cleared. */
12392 static int
12393 hscroll_windows (Lisp_Object window)
12395 int hscrolled_p = hscroll_window_tree (window);
12396 if (hscrolled_p)
12397 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12398 return hscrolled_p;
12403 /************************************************************************
12404 Redisplay
12405 ************************************************************************/
12407 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12408 to a non-zero value. This is sometimes handy to have in a debugger
12409 session. */
12411 #if GLYPH_DEBUG
12413 /* First and last unchanged row for try_window_id. */
12415 static int debug_first_unchanged_at_end_vpos;
12416 static int debug_last_unchanged_at_beg_vpos;
12418 /* Delta vpos and y. */
12420 static int debug_dvpos, debug_dy;
12422 /* Delta in characters and bytes for try_window_id. */
12424 static ptrdiff_t debug_delta, debug_delta_bytes;
12426 /* Values of window_end_pos and window_end_vpos at the end of
12427 try_window_id. */
12429 static ptrdiff_t debug_end_vpos;
12431 /* Append a string to W->desired_matrix->method. FMT is a printf
12432 format string. If trace_redisplay_p is non-zero also printf the
12433 resulting string to stderr. */
12435 static void debug_method_add (struct window *, char const *, ...)
12436 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12438 static void
12439 debug_method_add (struct window *w, char const *fmt, ...)
12441 char buffer[512];
12442 char *method = w->desired_matrix->method;
12443 int len = strlen (method);
12444 int size = sizeof w->desired_matrix->method;
12445 int remaining = size - len - 1;
12446 va_list ap;
12448 va_start (ap, fmt);
12449 vsprintf (buffer, fmt, ap);
12450 va_end (ap);
12451 if (len && remaining)
12453 method[len] = '|';
12454 --remaining, ++len;
12457 strncpy (method + len, buffer, remaining);
12459 if (trace_redisplay_p)
12460 fprintf (stderr, "%p (%s): %s\n",
12462 ((BUFFERP (w->buffer)
12463 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12464 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12465 : "no buffer"),
12466 buffer);
12469 #endif /* GLYPH_DEBUG */
12472 /* Value is non-zero if all changes in window W, which displays
12473 current_buffer, are in the text between START and END. START is a
12474 buffer position, END is given as a distance from Z. Used in
12475 redisplay_internal for display optimization. */
12477 static inline int
12478 text_outside_line_unchanged_p (struct window *w,
12479 ptrdiff_t start, ptrdiff_t end)
12481 int unchanged_p = 1;
12483 /* If text or overlays have changed, see where. */
12484 if (XFASTINT (w->last_modified) < MODIFF
12485 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12487 /* Gap in the line? */
12488 if (GPT < start || Z - GPT < end)
12489 unchanged_p = 0;
12491 /* Changes start in front of the line, or end after it? */
12492 if (unchanged_p
12493 && (BEG_UNCHANGED < start - 1
12494 || END_UNCHANGED < end))
12495 unchanged_p = 0;
12497 /* If selective display, can't optimize if changes start at the
12498 beginning of the line. */
12499 if (unchanged_p
12500 && INTEGERP (BVAR (current_buffer, selective_display))
12501 && XINT (BVAR (current_buffer, selective_display)) > 0
12502 && (BEG_UNCHANGED < start || GPT <= start))
12503 unchanged_p = 0;
12505 /* If there are overlays at the start or end of the line, these
12506 may have overlay strings with newlines in them. A change at
12507 START, for instance, may actually concern the display of such
12508 overlay strings as well, and they are displayed on different
12509 lines. So, quickly rule out this case. (For the future, it
12510 might be desirable to implement something more telling than
12511 just BEG/END_UNCHANGED.) */
12512 if (unchanged_p)
12514 if (BEG + BEG_UNCHANGED == start
12515 && overlay_touches_p (start))
12516 unchanged_p = 0;
12517 if (END_UNCHANGED == end
12518 && overlay_touches_p (Z - end))
12519 unchanged_p = 0;
12522 /* Under bidi reordering, adding or deleting a character in the
12523 beginning of a paragraph, before the first strong directional
12524 character, can change the base direction of the paragraph (unless
12525 the buffer specifies a fixed paragraph direction), which will
12526 require to redisplay the whole paragraph. It might be worthwhile
12527 to find the paragraph limits and widen the range of redisplayed
12528 lines to that, but for now just give up this optimization. */
12529 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12530 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12531 unchanged_p = 0;
12534 return unchanged_p;
12538 /* Do a frame update, taking possible shortcuts into account. This is
12539 the main external entry point for redisplay.
12541 If the last redisplay displayed an echo area message and that message
12542 is no longer requested, we clear the echo area or bring back the
12543 mini-buffer if that is in use. */
12545 void
12546 redisplay (void)
12548 redisplay_internal ();
12552 static Lisp_Object
12553 overlay_arrow_string_or_property (Lisp_Object var)
12555 Lisp_Object val;
12557 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12558 return val;
12560 return Voverlay_arrow_string;
12563 /* Return 1 if there are any overlay-arrows in current_buffer. */
12564 static int
12565 overlay_arrow_in_current_buffer_p (void)
12567 Lisp_Object vlist;
12569 for (vlist = Voverlay_arrow_variable_list;
12570 CONSP (vlist);
12571 vlist = XCDR (vlist))
12573 Lisp_Object var = XCAR (vlist);
12574 Lisp_Object val;
12576 if (!SYMBOLP (var))
12577 continue;
12578 val = find_symbol_value (var);
12579 if (MARKERP (val)
12580 && current_buffer == XMARKER (val)->buffer)
12581 return 1;
12583 return 0;
12587 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12588 has changed. */
12590 static int
12591 overlay_arrows_changed_p (void)
12593 Lisp_Object vlist;
12595 for (vlist = Voverlay_arrow_variable_list;
12596 CONSP (vlist);
12597 vlist = XCDR (vlist))
12599 Lisp_Object var = XCAR (vlist);
12600 Lisp_Object val, pstr;
12602 if (!SYMBOLP (var))
12603 continue;
12604 val = find_symbol_value (var);
12605 if (!MARKERP (val))
12606 continue;
12607 if (! EQ (COERCE_MARKER (val),
12608 Fget (var, Qlast_arrow_position))
12609 || ! (pstr = overlay_arrow_string_or_property (var),
12610 EQ (pstr, Fget (var, Qlast_arrow_string))))
12611 return 1;
12613 return 0;
12616 /* Mark overlay arrows to be updated on next redisplay. */
12618 static void
12619 update_overlay_arrows (int up_to_date)
12621 Lisp_Object vlist;
12623 for (vlist = Voverlay_arrow_variable_list;
12624 CONSP (vlist);
12625 vlist = XCDR (vlist))
12627 Lisp_Object var = XCAR (vlist);
12629 if (!SYMBOLP (var))
12630 continue;
12632 if (up_to_date > 0)
12634 Lisp_Object val = find_symbol_value (var);
12635 Fput (var, Qlast_arrow_position,
12636 COERCE_MARKER (val));
12637 Fput (var, Qlast_arrow_string,
12638 overlay_arrow_string_or_property (var));
12640 else if (up_to_date < 0
12641 || !NILP (Fget (var, Qlast_arrow_position)))
12643 Fput (var, Qlast_arrow_position, Qt);
12644 Fput (var, Qlast_arrow_string, Qt);
12650 /* Return overlay arrow string to display at row.
12651 Return integer (bitmap number) for arrow bitmap in left fringe.
12652 Return nil if no overlay arrow. */
12654 static Lisp_Object
12655 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12657 Lisp_Object vlist;
12659 for (vlist = Voverlay_arrow_variable_list;
12660 CONSP (vlist);
12661 vlist = XCDR (vlist))
12663 Lisp_Object var = XCAR (vlist);
12664 Lisp_Object val;
12666 if (!SYMBOLP (var))
12667 continue;
12669 val = find_symbol_value (var);
12671 if (MARKERP (val)
12672 && current_buffer == XMARKER (val)->buffer
12673 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12675 if (FRAME_WINDOW_P (it->f)
12676 /* FIXME: if ROW->reversed_p is set, this should test
12677 the right fringe, not the left one. */
12678 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12680 #ifdef HAVE_WINDOW_SYSTEM
12681 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12683 int fringe_bitmap;
12684 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12685 return make_number (fringe_bitmap);
12687 #endif
12688 return make_number (-1); /* Use default arrow bitmap */
12690 return overlay_arrow_string_or_property (var);
12694 return Qnil;
12697 /* Return 1 if point moved out of or into a composition. Otherwise
12698 return 0. PREV_BUF and PREV_PT are the last point buffer and
12699 position. BUF and PT are the current point buffer and position. */
12701 static int
12702 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12703 struct buffer *buf, ptrdiff_t pt)
12705 ptrdiff_t start, end;
12706 Lisp_Object prop;
12707 Lisp_Object buffer;
12709 XSETBUFFER (buffer, buf);
12710 /* Check a composition at the last point if point moved within the
12711 same buffer. */
12712 if (prev_buf == buf)
12714 if (prev_pt == pt)
12715 /* Point didn't move. */
12716 return 0;
12718 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12719 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12720 && COMPOSITION_VALID_P (start, end, prop)
12721 && start < prev_pt && end > prev_pt)
12722 /* The last point was within the composition. Return 1 iff
12723 point moved out of the composition. */
12724 return (pt <= start || pt >= end);
12727 /* Check a composition at the current point. */
12728 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12729 && find_composition (pt, -1, &start, &end, &prop, buffer)
12730 && COMPOSITION_VALID_P (start, end, prop)
12731 && start < pt && end > pt);
12735 /* Reconsider the setting of B->clip_changed which is displayed
12736 in window W. */
12738 static inline void
12739 reconsider_clip_changes (struct window *w, struct buffer *b)
12741 if (b->clip_changed
12742 && !NILP (w->window_end_valid)
12743 && w->current_matrix->buffer == b
12744 && w->current_matrix->zv == BUF_ZV (b)
12745 && w->current_matrix->begv == BUF_BEGV (b))
12746 b->clip_changed = 0;
12748 /* If display wasn't paused, and W is not a tool bar window, see if
12749 point has been moved into or out of a composition. In that case,
12750 we set b->clip_changed to 1 to force updating the screen. If
12751 b->clip_changed has already been set to 1, we can skip this
12752 check. */
12753 if (!b->clip_changed
12754 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12756 ptrdiff_t pt;
12758 if (w == XWINDOW (selected_window))
12759 pt = PT;
12760 else
12761 pt = marker_position (w->pointm);
12763 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12764 || pt != XINT (w->last_point))
12765 && check_point_in_composition (w->current_matrix->buffer,
12766 XINT (w->last_point),
12767 XBUFFER (w->buffer), pt))
12768 b->clip_changed = 1;
12773 /* Select FRAME to forward the values of frame-local variables into C
12774 variables so that the redisplay routines can access those values
12775 directly. */
12777 static void
12778 select_frame_for_redisplay (Lisp_Object frame)
12780 Lisp_Object tail, tem;
12781 Lisp_Object old = selected_frame;
12782 struct Lisp_Symbol *sym;
12784 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12786 selected_frame = frame;
12788 do {
12789 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12790 if (CONSP (XCAR (tail))
12791 && (tem = XCAR (XCAR (tail)),
12792 SYMBOLP (tem))
12793 && (sym = indirect_variable (XSYMBOL (tem)),
12794 sym->redirect == SYMBOL_LOCALIZED)
12795 && sym->val.blv->frame_local)
12796 /* Use find_symbol_value rather than Fsymbol_value
12797 to avoid an error if it is void. */
12798 find_symbol_value (tem);
12799 } while (!EQ (frame, old) && (frame = old, 1));
12803 #define STOP_POLLING \
12804 do { if (! polling_stopped_here) stop_polling (); \
12805 polling_stopped_here = 1; } while (0)
12807 #define RESUME_POLLING \
12808 do { if (polling_stopped_here) start_polling (); \
12809 polling_stopped_here = 0; } while (0)
12812 /* Perhaps in the future avoid recentering windows if it
12813 is not necessary; currently that causes some problems. */
12815 static void
12816 redisplay_internal (void)
12818 struct window *w = XWINDOW (selected_window);
12819 struct window *sw;
12820 struct frame *fr;
12821 int pending;
12822 int must_finish = 0;
12823 struct text_pos tlbufpos, tlendpos;
12824 int number_of_visible_frames;
12825 ptrdiff_t count, count1;
12826 struct frame *sf;
12827 int polling_stopped_here = 0;
12828 Lisp_Object old_frame = selected_frame;
12830 /* Non-zero means redisplay has to consider all windows on all
12831 frames. Zero means, only selected_window is considered. */
12832 int consider_all_windows_p;
12834 /* Non-zero means redisplay has to redisplay the miniwindow */
12835 int update_miniwindow_p = 0;
12837 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12839 /* No redisplay if running in batch mode or frame is not yet fully
12840 initialized, or redisplay is explicitly turned off by setting
12841 Vinhibit_redisplay. */
12842 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12843 || !NILP (Vinhibit_redisplay))
12844 return;
12846 /* Don't examine these until after testing Vinhibit_redisplay.
12847 When Emacs is shutting down, perhaps because its connection to
12848 X has dropped, we should not look at them at all. */
12849 fr = XFRAME (w->frame);
12850 sf = SELECTED_FRAME ();
12852 if (!fr->glyphs_initialized_p)
12853 return;
12855 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12856 if (popup_activated ())
12857 return;
12858 #endif
12860 /* I don't think this happens but let's be paranoid. */
12861 if (redisplaying_p)
12862 return;
12864 /* Record a function that resets redisplaying_p to its old value
12865 when we leave this function. */
12866 count = SPECPDL_INDEX ();
12867 record_unwind_protect (unwind_redisplay,
12868 Fcons (make_number (redisplaying_p), selected_frame));
12869 ++redisplaying_p;
12870 specbind (Qinhibit_free_realized_faces, Qnil);
12873 Lisp_Object tail, frame;
12875 FOR_EACH_FRAME (tail, frame)
12877 struct frame *f = XFRAME (frame);
12878 f->already_hscrolled_p = 0;
12882 retry:
12883 /* Remember the currently selected window. */
12884 sw = w;
12886 if (!EQ (old_frame, selected_frame)
12887 && FRAME_LIVE_P (XFRAME (old_frame)))
12888 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12889 selected_frame and selected_window to be temporarily out-of-sync so
12890 when we come back here via `goto retry', we need to resync because we
12891 may need to run Elisp code (via prepare_menu_bars). */
12892 select_frame_for_redisplay (old_frame);
12894 pending = 0;
12895 reconsider_clip_changes (w, current_buffer);
12896 last_escape_glyph_frame = NULL;
12897 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12898 last_glyphless_glyph_frame = NULL;
12899 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12901 /* If new fonts have been loaded that make a glyph matrix adjustment
12902 necessary, do it. */
12903 if (fonts_changed_p)
12905 adjust_glyphs (NULL);
12906 ++windows_or_buffers_changed;
12907 fonts_changed_p = 0;
12910 /* If face_change_count is non-zero, init_iterator will free all
12911 realized faces, which includes the faces referenced from current
12912 matrices. So, we can't reuse current matrices in this case. */
12913 if (face_change_count)
12914 ++windows_or_buffers_changed;
12916 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12917 && FRAME_TTY (sf)->previous_frame != sf)
12919 /* Since frames on a single ASCII terminal share the same
12920 display area, displaying a different frame means redisplay
12921 the whole thing. */
12922 windows_or_buffers_changed++;
12923 SET_FRAME_GARBAGED (sf);
12924 #ifndef DOS_NT
12925 set_tty_color_mode (FRAME_TTY (sf), sf);
12926 #endif
12927 FRAME_TTY (sf)->previous_frame = sf;
12930 /* Set the visible flags for all frames. Do this before checking
12931 for resized or garbaged frames; they want to know if their frames
12932 are visible. See the comment in frame.h for
12933 FRAME_SAMPLE_VISIBILITY. */
12935 Lisp_Object tail, frame;
12937 number_of_visible_frames = 0;
12939 FOR_EACH_FRAME (tail, frame)
12941 struct frame *f = XFRAME (frame);
12943 FRAME_SAMPLE_VISIBILITY (f);
12944 if (FRAME_VISIBLE_P (f))
12945 ++number_of_visible_frames;
12946 clear_desired_matrices (f);
12950 /* Notice any pending interrupt request to change frame size. */
12951 do_pending_window_change (1);
12953 /* do_pending_window_change could change the selected_window due to
12954 frame resizing which makes the selected window too small. */
12955 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12957 sw = w;
12958 reconsider_clip_changes (w, current_buffer);
12961 /* Clear frames marked as garbaged. */
12962 if (frame_garbaged)
12963 clear_garbaged_frames ();
12965 /* Build menubar and tool-bar items. */
12966 if (NILP (Vmemory_full))
12967 prepare_menu_bars ();
12969 if (windows_or_buffers_changed)
12970 update_mode_lines++;
12972 /* Detect case that we need to write or remove a star in the mode line. */
12973 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12975 w->update_mode_line = 1;
12976 if (buffer_shared > 1)
12977 update_mode_lines++;
12980 /* Avoid invocation of point motion hooks by `current_column' below. */
12981 count1 = SPECPDL_INDEX ();
12982 specbind (Qinhibit_point_motion_hooks, Qt);
12984 /* If %c is in the mode line, update it if needed. */
12985 if (!NILP (w->column_number_displayed)
12986 /* This alternative quickly identifies a common case
12987 where no change is needed. */
12988 && !(PT == XFASTINT (w->last_point)
12989 && XFASTINT (w->last_modified) >= MODIFF
12990 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12991 && (XFASTINT (w->column_number_displayed) != current_column ()))
12992 w->update_mode_line = 1;
12994 unbind_to (count1, Qnil);
12996 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12998 /* The variable buffer_shared is set in redisplay_window and
12999 indicates that we redisplay a buffer in different windows. See
13000 there. */
13001 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13002 || cursor_type_changed);
13004 /* If specs for an arrow have changed, do thorough redisplay
13005 to ensure we remove any arrow that should no longer exist. */
13006 if (overlay_arrows_changed_p ())
13007 consider_all_windows_p = windows_or_buffers_changed = 1;
13009 /* Normally the message* functions will have already displayed and
13010 updated the echo area, but the frame may have been trashed, or
13011 the update may have been preempted, so display the echo area
13012 again here. Checking message_cleared_p captures the case that
13013 the echo area should be cleared. */
13014 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13015 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13016 || (message_cleared_p
13017 && minibuf_level == 0
13018 /* If the mini-window is currently selected, this means the
13019 echo-area doesn't show through. */
13020 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13022 int window_height_changed_p = echo_area_display (0);
13024 if (message_cleared_p)
13025 update_miniwindow_p = 1;
13027 must_finish = 1;
13029 /* If we don't display the current message, don't clear the
13030 message_cleared_p flag, because, if we did, we wouldn't clear
13031 the echo area in the next redisplay which doesn't preserve
13032 the echo area. */
13033 if (!display_last_displayed_message_p)
13034 message_cleared_p = 0;
13036 if (fonts_changed_p)
13037 goto retry;
13038 else if (window_height_changed_p)
13040 consider_all_windows_p = 1;
13041 ++update_mode_lines;
13042 ++windows_or_buffers_changed;
13044 /* If window configuration was changed, frames may have been
13045 marked garbaged. Clear them or we will experience
13046 surprises wrt scrolling. */
13047 if (frame_garbaged)
13048 clear_garbaged_frames ();
13051 else if (EQ (selected_window, minibuf_window)
13052 && (current_buffer->clip_changed
13053 || XFASTINT (w->last_modified) < MODIFF
13054 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13055 && resize_mini_window (w, 0))
13057 /* Resized active mini-window to fit the size of what it is
13058 showing if its contents might have changed. */
13059 must_finish = 1;
13060 /* FIXME: this causes all frames to be updated, which seems unnecessary
13061 since only the current frame needs to be considered. This function needs
13062 to be rewritten with two variables, consider_all_windows and
13063 consider_all_frames. */
13064 consider_all_windows_p = 1;
13065 ++windows_or_buffers_changed;
13066 ++update_mode_lines;
13068 /* If window configuration was changed, frames may have been
13069 marked garbaged. Clear them or we will experience
13070 surprises wrt scrolling. */
13071 if (frame_garbaged)
13072 clear_garbaged_frames ();
13076 /* If showing the region, and mark has changed, we must redisplay
13077 the whole window. The assignment to this_line_start_pos prevents
13078 the optimization directly below this if-statement. */
13079 if (((!NILP (Vtransient_mark_mode)
13080 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13081 != !NILP (w->region_showing))
13082 || (!NILP (w->region_showing)
13083 && !EQ (w->region_showing,
13084 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13085 CHARPOS (this_line_start_pos) = 0;
13087 /* Optimize the case that only the line containing the cursor in the
13088 selected window has changed. Variables starting with this_ are
13089 set in display_line and record information about the line
13090 containing the cursor. */
13091 tlbufpos = this_line_start_pos;
13092 tlendpos = this_line_end_pos;
13093 if (!consider_all_windows_p
13094 && CHARPOS (tlbufpos) > 0
13095 && !w->update_mode_line
13096 && !current_buffer->clip_changed
13097 && !current_buffer->prevent_redisplay_optimizations_p
13098 && FRAME_VISIBLE_P (XFRAME (w->frame))
13099 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13100 /* Make sure recorded data applies to current buffer, etc. */
13101 && this_line_buffer == current_buffer
13102 && current_buffer == XBUFFER (w->buffer)
13103 && !w->force_start
13104 && !w->optional_new_start
13105 /* Point must be on the line that we have info recorded about. */
13106 && PT >= CHARPOS (tlbufpos)
13107 && PT <= Z - CHARPOS (tlendpos)
13108 /* All text outside that line, including its final newline,
13109 must be unchanged. */
13110 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13111 CHARPOS (tlendpos)))
13113 if (CHARPOS (tlbufpos) > BEGV
13114 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13115 && (CHARPOS (tlbufpos) == ZV
13116 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13117 /* Former continuation line has disappeared by becoming empty. */
13118 goto cancel;
13119 else if (XFASTINT (w->last_modified) < MODIFF
13120 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13121 || MINI_WINDOW_P (w))
13123 /* We have to handle the case of continuation around a
13124 wide-column character (see the comment in indent.c around
13125 line 1340).
13127 For instance, in the following case:
13129 -------- Insert --------
13130 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13131 J_I_ ==> J_I_ `^^' are cursors.
13132 ^^ ^^
13133 -------- --------
13135 As we have to redraw the line above, we cannot use this
13136 optimization. */
13138 struct it it;
13139 int line_height_before = this_line_pixel_height;
13141 /* Note that start_display will handle the case that the
13142 line starting at tlbufpos is a continuation line. */
13143 start_display (&it, w, tlbufpos);
13145 /* Implementation note: It this still necessary? */
13146 if (it.current_x != this_line_start_x)
13147 goto cancel;
13149 TRACE ((stderr, "trying display optimization 1\n"));
13150 w->cursor.vpos = -1;
13151 overlay_arrow_seen = 0;
13152 it.vpos = this_line_vpos;
13153 it.current_y = this_line_y;
13154 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13155 display_line (&it);
13157 /* If line contains point, is not continued,
13158 and ends at same distance from eob as before, we win. */
13159 if (w->cursor.vpos >= 0
13160 /* Line is not continued, otherwise this_line_start_pos
13161 would have been set to 0 in display_line. */
13162 && CHARPOS (this_line_start_pos)
13163 /* Line ends as before. */
13164 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13165 /* Line has same height as before. Otherwise other lines
13166 would have to be shifted up or down. */
13167 && this_line_pixel_height == line_height_before)
13169 /* If this is not the window's last line, we must adjust
13170 the charstarts of the lines below. */
13171 if (it.current_y < it.last_visible_y)
13173 struct glyph_row *row
13174 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13175 ptrdiff_t delta, delta_bytes;
13177 /* We used to distinguish between two cases here,
13178 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13179 when the line ends in a newline or the end of the
13180 buffer's accessible portion. But both cases did
13181 the same, so they were collapsed. */
13182 delta = (Z
13183 - CHARPOS (tlendpos)
13184 - MATRIX_ROW_START_CHARPOS (row));
13185 delta_bytes = (Z_BYTE
13186 - BYTEPOS (tlendpos)
13187 - MATRIX_ROW_START_BYTEPOS (row));
13189 increment_matrix_positions (w->current_matrix,
13190 this_line_vpos + 1,
13191 w->current_matrix->nrows,
13192 delta, delta_bytes);
13195 /* If this row displays text now but previously didn't,
13196 or vice versa, w->window_end_vpos may have to be
13197 adjusted. */
13198 if ((it.glyph_row - 1)->displays_text_p)
13200 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13201 XSETINT (w->window_end_vpos, this_line_vpos);
13203 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13204 && this_line_vpos > 0)
13205 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13206 w->window_end_valid = Qnil;
13208 /* Update hint: No need to try to scroll in update_window. */
13209 w->desired_matrix->no_scrolling_p = 1;
13211 #if GLYPH_DEBUG
13212 *w->desired_matrix->method = 0;
13213 debug_method_add (w, "optimization 1");
13214 #endif
13215 #ifdef HAVE_WINDOW_SYSTEM
13216 update_window_fringes (w, 0);
13217 #endif
13218 goto update;
13220 else
13221 goto cancel;
13223 else if (/* Cursor position hasn't changed. */
13224 PT == XFASTINT (w->last_point)
13225 /* Make sure the cursor was last displayed
13226 in this window. Otherwise we have to reposition it. */
13227 && 0 <= w->cursor.vpos
13228 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13230 if (!must_finish)
13232 do_pending_window_change (1);
13233 /* If selected_window changed, redisplay again. */
13234 if (WINDOWP (selected_window)
13235 && (w = XWINDOW (selected_window)) != sw)
13236 goto retry;
13238 /* We used to always goto end_of_redisplay here, but this
13239 isn't enough if we have a blinking cursor. */
13240 if (w->cursor_off_p == w->last_cursor_off_p)
13241 goto end_of_redisplay;
13243 goto update;
13245 /* If highlighting the region, or if the cursor is in the echo area,
13246 then we can't just move the cursor. */
13247 else if (! (!NILP (Vtransient_mark_mode)
13248 && !NILP (BVAR (current_buffer, mark_active)))
13249 && (EQ (selected_window,
13250 BVAR (current_buffer, last_selected_window))
13251 || highlight_nonselected_windows)
13252 && NILP (w->region_showing)
13253 && NILP (Vshow_trailing_whitespace)
13254 && !cursor_in_echo_area)
13256 struct it it;
13257 struct glyph_row *row;
13259 /* Skip from tlbufpos to PT and see where it is. Note that
13260 PT may be in invisible text. If so, we will end at the
13261 next visible position. */
13262 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13263 NULL, DEFAULT_FACE_ID);
13264 it.current_x = this_line_start_x;
13265 it.current_y = this_line_y;
13266 it.vpos = this_line_vpos;
13268 /* The call to move_it_to stops in front of PT, but
13269 moves over before-strings. */
13270 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13272 if (it.vpos == this_line_vpos
13273 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13274 row->enabled_p))
13276 xassert (this_line_vpos == it.vpos);
13277 xassert (this_line_y == it.current_y);
13278 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13279 #if GLYPH_DEBUG
13280 *w->desired_matrix->method = 0;
13281 debug_method_add (w, "optimization 3");
13282 #endif
13283 goto update;
13285 else
13286 goto cancel;
13289 cancel:
13290 /* Text changed drastically or point moved off of line. */
13291 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13294 CHARPOS (this_line_start_pos) = 0;
13295 consider_all_windows_p |= buffer_shared > 1;
13296 ++clear_face_cache_count;
13297 #ifdef HAVE_WINDOW_SYSTEM
13298 ++clear_image_cache_count;
13299 #endif
13301 /* Build desired matrices, and update the display. If
13302 consider_all_windows_p is non-zero, do it for all windows on all
13303 frames. Otherwise do it for selected_window, only. */
13305 if (consider_all_windows_p)
13307 Lisp_Object tail, frame;
13309 FOR_EACH_FRAME (tail, frame)
13310 XFRAME (frame)->updated_p = 0;
13312 /* Recompute # windows showing selected buffer. This will be
13313 incremented each time such a window is displayed. */
13314 buffer_shared = 0;
13316 FOR_EACH_FRAME (tail, frame)
13318 struct frame *f = XFRAME (frame);
13320 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13322 if (! EQ (frame, selected_frame))
13323 /* Select the frame, for the sake of frame-local
13324 variables. */
13325 select_frame_for_redisplay (frame);
13327 /* Mark all the scroll bars to be removed; we'll redeem
13328 the ones we want when we redisplay their windows. */
13329 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13330 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13332 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13333 redisplay_windows (FRAME_ROOT_WINDOW (f));
13335 /* The X error handler may have deleted that frame. */
13336 if (!FRAME_LIVE_P (f))
13337 continue;
13339 /* Any scroll bars which redisplay_windows should have
13340 nuked should now go away. */
13341 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13342 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13344 /* If fonts changed, display again. */
13345 /* ??? rms: I suspect it is a mistake to jump all the way
13346 back to retry here. It should just retry this frame. */
13347 if (fonts_changed_p)
13348 goto retry;
13350 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13352 /* See if we have to hscroll. */
13353 if (!f->already_hscrolled_p)
13355 f->already_hscrolled_p = 1;
13356 if (hscroll_windows (f->root_window))
13357 goto retry;
13360 /* Prevent various kinds of signals during display
13361 update. stdio is not robust about handling
13362 signals, which can cause an apparent I/O
13363 error. */
13364 if (interrupt_input)
13365 unrequest_sigio ();
13366 STOP_POLLING;
13368 /* Update the display. */
13369 set_window_update_flags (XWINDOW (f->root_window), 1);
13370 pending |= update_frame (f, 0, 0);
13371 f->updated_p = 1;
13376 if (!EQ (old_frame, selected_frame)
13377 && FRAME_LIVE_P (XFRAME (old_frame)))
13378 /* We played a bit fast-and-loose above and allowed selected_frame
13379 and selected_window to be temporarily out-of-sync but let's make
13380 sure this stays contained. */
13381 select_frame_for_redisplay (old_frame);
13382 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13384 if (!pending)
13386 /* Do the mark_window_display_accurate after all windows have
13387 been redisplayed because this call resets flags in buffers
13388 which are needed for proper redisplay. */
13389 FOR_EACH_FRAME (tail, frame)
13391 struct frame *f = XFRAME (frame);
13392 if (f->updated_p)
13394 mark_window_display_accurate (f->root_window, 1);
13395 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13396 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13401 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13403 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13404 struct frame *mini_frame;
13406 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13407 /* Use list_of_error, not Qerror, so that
13408 we catch only errors and don't run the debugger. */
13409 internal_condition_case_1 (redisplay_window_1, selected_window,
13410 list_of_error,
13411 redisplay_window_error);
13412 if (update_miniwindow_p)
13413 internal_condition_case_1 (redisplay_window_1, mini_window,
13414 list_of_error,
13415 redisplay_window_error);
13417 /* Compare desired and current matrices, perform output. */
13419 update:
13420 /* If fonts changed, display again. */
13421 if (fonts_changed_p)
13422 goto retry;
13424 /* Prevent various kinds of signals during display update.
13425 stdio is not robust about handling signals,
13426 which can cause an apparent I/O error. */
13427 if (interrupt_input)
13428 unrequest_sigio ();
13429 STOP_POLLING;
13431 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13433 if (hscroll_windows (selected_window))
13434 goto retry;
13436 XWINDOW (selected_window)->must_be_updated_p = 1;
13437 pending = update_frame (sf, 0, 0);
13440 /* We may have called echo_area_display at the top of this
13441 function. If the echo area is on another frame, that may
13442 have put text on a frame other than the selected one, so the
13443 above call to update_frame would not have caught it. Catch
13444 it here. */
13445 mini_window = FRAME_MINIBUF_WINDOW (sf);
13446 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13448 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13450 XWINDOW (mini_window)->must_be_updated_p = 1;
13451 pending |= update_frame (mini_frame, 0, 0);
13452 if (!pending && hscroll_windows (mini_window))
13453 goto retry;
13457 /* If display was paused because of pending input, make sure we do a
13458 thorough update the next time. */
13459 if (pending)
13461 /* Prevent the optimization at the beginning of
13462 redisplay_internal that tries a single-line update of the
13463 line containing the cursor in the selected window. */
13464 CHARPOS (this_line_start_pos) = 0;
13466 /* Let the overlay arrow be updated the next time. */
13467 update_overlay_arrows (0);
13469 /* If we pause after scrolling, some rows in the current
13470 matrices of some windows are not valid. */
13471 if (!WINDOW_FULL_WIDTH_P (w)
13472 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13473 update_mode_lines = 1;
13475 else
13477 if (!consider_all_windows_p)
13479 /* This has already been done above if
13480 consider_all_windows_p is set. */
13481 mark_window_display_accurate_1 (w, 1);
13483 /* Say overlay arrows are up to date. */
13484 update_overlay_arrows (1);
13486 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13487 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13490 update_mode_lines = 0;
13491 windows_or_buffers_changed = 0;
13492 cursor_type_changed = 0;
13495 /* Start SIGIO interrupts coming again. Having them off during the
13496 code above makes it less likely one will discard output, but not
13497 impossible, since there might be stuff in the system buffer here.
13498 But it is much hairier to try to do anything about that. */
13499 if (interrupt_input)
13500 request_sigio ();
13501 RESUME_POLLING;
13503 /* If a frame has become visible which was not before, redisplay
13504 again, so that we display it. Expose events for such a frame
13505 (which it gets when becoming visible) don't call the parts of
13506 redisplay constructing glyphs, so simply exposing a frame won't
13507 display anything in this case. So, we have to display these
13508 frames here explicitly. */
13509 if (!pending)
13511 Lisp_Object tail, frame;
13512 int new_count = 0;
13514 FOR_EACH_FRAME (tail, frame)
13516 int this_is_visible = 0;
13518 if (XFRAME (frame)->visible)
13519 this_is_visible = 1;
13520 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13521 if (XFRAME (frame)->visible)
13522 this_is_visible = 1;
13524 if (this_is_visible)
13525 new_count++;
13528 if (new_count != number_of_visible_frames)
13529 windows_or_buffers_changed++;
13532 /* Change frame size now if a change is pending. */
13533 do_pending_window_change (1);
13535 /* If we just did a pending size change, or have additional
13536 visible frames, or selected_window changed, redisplay again. */
13537 if ((windows_or_buffers_changed && !pending)
13538 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13539 goto retry;
13541 /* Clear the face and image caches.
13543 We used to do this only if consider_all_windows_p. But the cache
13544 needs to be cleared if a timer creates images in the current
13545 buffer (e.g. the test case in Bug#6230). */
13547 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13549 clear_face_cache (0);
13550 clear_face_cache_count = 0;
13553 #ifdef HAVE_WINDOW_SYSTEM
13554 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13556 clear_image_caches (Qnil);
13557 clear_image_cache_count = 0;
13559 #endif /* HAVE_WINDOW_SYSTEM */
13561 end_of_redisplay:
13562 unbind_to (count, Qnil);
13563 RESUME_POLLING;
13567 /* Redisplay, but leave alone any recent echo area message unless
13568 another message has been requested in its place.
13570 This is useful in situations where you need to redisplay but no
13571 user action has occurred, making it inappropriate for the message
13572 area to be cleared. See tracking_off and
13573 wait_reading_process_output for examples of these situations.
13575 FROM_WHERE is an integer saying from where this function was
13576 called. This is useful for debugging. */
13578 void
13579 redisplay_preserve_echo_area (int from_where)
13581 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13583 if (!NILP (echo_area_buffer[1]))
13585 /* We have a previously displayed message, but no current
13586 message. Redisplay the previous message. */
13587 display_last_displayed_message_p = 1;
13588 redisplay_internal ();
13589 display_last_displayed_message_p = 0;
13591 else
13592 redisplay_internal ();
13594 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13595 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13596 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13600 /* Function registered with record_unwind_protect in
13601 redisplay_internal. Reset redisplaying_p to the value it had
13602 before redisplay_internal was called, and clear
13603 prevent_freeing_realized_faces_p. It also selects the previously
13604 selected frame, unless it has been deleted (by an X connection
13605 failure during redisplay, for example). */
13607 static Lisp_Object
13608 unwind_redisplay (Lisp_Object val)
13610 Lisp_Object old_redisplaying_p, old_frame;
13612 old_redisplaying_p = XCAR (val);
13613 redisplaying_p = XFASTINT (old_redisplaying_p);
13614 old_frame = XCDR (val);
13615 if (! EQ (old_frame, selected_frame)
13616 && FRAME_LIVE_P (XFRAME (old_frame)))
13617 select_frame_for_redisplay (old_frame);
13618 return Qnil;
13622 /* Mark the display of window W as accurate or inaccurate. If
13623 ACCURATE_P is non-zero mark display of W as accurate. If
13624 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13625 redisplay_internal is called. */
13627 static void
13628 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13630 if (BUFFERP (w->buffer))
13632 struct buffer *b = XBUFFER (w->buffer);
13634 w->last_modified
13635 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13636 w->last_overlay_modified
13637 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13638 w->last_had_star
13639 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13641 if (accurate_p)
13643 b->clip_changed = 0;
13644 b->prevent_redisplay_optimizations_p = 0;
13646 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13647 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13648 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13649 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13651 w->current_matrix->buffer = b;
13652 w->current_matrix->begv = BUF_BEGV (b);
13653 w->current_matrix->zv = BUF_ZV (b);
13655 w->last_cursor = w->cursor;
13656 w->last_cursor_off_p = w->cursor_off_p;
13658 if (w == XWINDOW (selected_window))
13659 w->last_point = make_number (BUF_PT (b));
13660 else
13661 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13665 if (accurate_p)
13667 w->window_end_valid = w->buffer;
13668 w->update_mode_line = 0;
13673 /* Mark the display of windows in the window tree rooted at WINDOW as
13674 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13675 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13676 be redisplayed the next time redisplay_internal is called. */
13678 void
13679 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13681 struct window *w;
13683 for (; !NILP (window); window = w->next)
13685 w = XWINDOW (window);
13686 mark_window_display_accurate_1 (w, accurate_p);
13688 if (!NILP (w->vchild))
13689 mark_window_display_accurate (w->vchild, accurate_p);
13690 if (!NILP (w->hchild))
13691 mark_window_display_accurate (w->hchild, accurate_p);
13694 if (accurate_p)
13696 update_overlay_arrows (1);
13698 else
13700 /* Force a thorough redisplay the next time by setting
13701 last_arrow_position and last_arrow_string to t, which is
13702 unequal to any useful value of Voverlay_arrow_... */
13703 update_overlay_arrows (-1);
13708 /* Return value in display table DP (Lisp_Char_Table *) for character
13709 C. Since a display table doesn't have any parent, we don't have to
13710 follow parent. Do not call this function directly but use the
13711 macro DISP_CHAR_VECTOR. */
13713 Lisp_Object
13714 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13716 Lisp_Object val;
13718 if (ASCII_CHAR_P (c))
13720 val = dp->ascii;
13721 if (SUB_CHAR_TABLE_P (val))
13722 val = XSUB_CHAR_TABLE (val)->contents[c];
13724 else
13726 Lisp_Object table;
13728 XSETCHAR_TABLE (table, dp);
13729 val = char_table_ref (table, c);
13731 if (NILP (val))
13732 val = dp->defalt;
13733 return val;
13738 /***********************************************************************
13739 Window Redisplay
13740 ***********************************************************************/
13742 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13744 static void
13745 redisplay_windows (Lisp_Object window)
13747 while (!NILP (window))
13749 struct window *w = XWINDOW (window);
13751 if (!NILP (w->hchild))
13752 redisplay_windows (w->hchild);
13753 else if (!NILP (w->vchild))
13754 redisplay_windows (w->vchild);
13755 else if (!NILP (w->buffer))
13757 displayed_buffer = XBUFFER (w->buffer);
13758 /* Use list_of_error, not Qerror, so that
13759 we catch only errors and don't run the debugger. */
13760 internal_condition_case_1 (redisplay_window_0, window,
13761 list_of_error,
13762 redisplay_window_error);
13765 window = w->next;
13769 static Lisp_Object
13770 redisplay_window_error (Lisp_Object ignore)
13772 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13773 return Qnil;
13776 static Lisp_Object
13777 redisplay_window_0 (Lisp_Object window)
13779 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13780 redisplay_window (window, 0);
13781 return Qnil;
13784 static Lisp_Object
13785 redisplay_window_1 (Lisp_Object window)
13787 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13788 redisplay_window (window, 1);
13789 return Qnil;
13793 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13794 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13795 which positions recorded in ROW differ from current buffer
13796 positions.
13798 Return 0 if cursor is not on this row, 1 otherwise. */
13800 static int
13801 set_cursor_from_row (struct window *w, struct glyph_row *row,
13802 struct glyph_matrix *matrix,
13803 ptrdiff_t delta, ptrdiff_t delta_bytes,
13804 int dy, int dvpos)
13806 struct glyph *glyph = row->glyphs[TEXT_AREA];
13807 struct glyph *end = glyph + row->used[TEXT_AREA];
13808 struct glyph *cursor = NULL;
13809 /* The last known character position in row. */
13810 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13811 int x = row->x;
13812 ptrdiff_t pt_old = PT - delta;
13813 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13814 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13815 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13816 /* A glyph beyond the edge of TEXT_AREA which we should never
13817 touch. */
13818 struct glyph *glyphs_end = end;
13819 /* Non-zero means we've found a match for cursor position, but that
13820 glyph has the avoid_cursor_p flag set. */
13821 int match_with_avoid_cursor = 0;
13822 /* Non-zero means we've seen at least one glyph that came from a
13823 display string. */
13824 int string_seen = 0;
13825 /* Largest and smallest buffer positions seen so far during scan of
13826 glyph row. */
13827 ptrdiff_t bpos_max = pos_before;
13828 ptrdiff_t bpos_min = pos_after;
13829 /* Last buffer position covered by an overlay string with an integer
13830 `cursor' property. */
13831 ptrdiff_t bpos_covered = 0;
13832 /* Non-zero means the display string on which to display the cursor
13833 comes from a text property, not from an overlay. */
13834 int string_from_text_prop = 0;
13836 /* Don't even try doing anything if called for a mode-line or
13837 header-line row, since the rest of the code isn't prepared to
13838 deal with such calamities. */
13839 xassert (!row->mode_line_p);
13840 if (row->mode_line_p)
13841 return 0;
13843 /* Skip over glyphs not having an object at the start and the end of
13844 the row. These are special glyphs like truncation marks on
13845 terminal frames. */
13846 if (row->displays_text_p)
13848 if (!row->reversed_p)
13850 while (glyph < end
13851 && INTEGERP (glyph->object)
13852 && glyph->charpos < 0)
13854 x += glyph->pixel_width;
13855 ++glyph;
13857 while (end > glyph
13858 && INTEGERP ((end - 1)->object)
13859 /* CHARPOS is zero for blanks and stretch glyphs
13860 inserted by extend_face_to_end_of_line. */
13861 && (end - 1)->charpos <= 0)
13862 --end;
13863 glyph_before = glyph - 1;
13864 glyph_after = end;
13866 else
13868 struct glyph *g;
13870 /* If the glyph row is reversed, we need to process it from back
13871 to front, so swap the edge pointers. */
13872 glyphs_end = end = glyph - 1;
13873 glyph += row->used[TEXT_AREA] - 1;
13875 while (glyph > end + 1
13876 && INTEGERP (glyph->object)
13877 && glyph->charpos < 0)
13879 --glyph;
13880 x -= glyph->pixel_width;
13882 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13883 --glyph;
13884 /* By default, in reversed rows we put the cursor on the
13885 rightmost (first in the reading order) glyph. */
13886 for (g = end + 1; g < glyph; g++)
13887 x += g->pixel_width;
13888 while (end < glyph
13889 && INTEGERP ((end + 1)->object)
13890 && (end + 1)->charpos <= 0)
13891 ++end;
13892 glyph_before = glyph + 1;
13893 glyph_after = end;
13896 else if (row->reversed_p)
13898 /* In R2L rows that don't display text, put the cursor on the
13899 rightmost glyph. Case in point: an empty last line that is
13900 part of an R2L paragraph. */
13901 cursor = end - 1;
13902 /* Avoid placing the cursor on the last glyph of the row, where
13903 on terminal frames we hold the vertical border between
13904 adjacent windows. */
13905 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13906 && !WINDOW_RIGHTMOST_P (w)
13907 && cursor == row->glyphs[LAST_AREA] - 1)
13908 cursor--;
13909 x = -1; /* will be computed below, at label compute_x */
13912 /* Step 1: Try to find the glyph whose character position
13913 corresponds to point. If that's not possible, find 2 glyphs
13914 whose character positions are the closest to point, one before
13915 point, the other after it. */
13916 if (!row->reversed_p)
13917 while (/* not marched to end of glyph row */
13918 glyph < end
13919 /* glyph was not inserted by redisplay for internal purposes */
13920 && !INTEGERP (glyph->object))
13922 if (BUFFERP (glyph->object))
13924 ptrdiff_t dpos = glyph->charpos - pt_old;
13926 if (glyph->charpos > bpos_max)
13927 bpos_max = glyph->charpos;
13928 if (glyph->charpos < bpos_min)
13929 bpos_min = glyph->charpos;
13930 if (!glyph->avoid_cursor_p)
13932 /* If we hit point, we've found the glyph on which to
13933 display the cursor. */
13934 if (dpos == 0)
13936 match_with_avoid_cursor = 0;
13937 break;
13939 /* See if we've found a better approximation to
13940 POS_BEFORE or to POS_AFTER. */
13941 if (0 > dpos && dpos > pos_before - pt_old)
13943 pos_before = glyph->charpos;
13944 glyph_before = glyph;
13946 else if (0 < dpos && dpos < pos_after - pt_old)
13948 pos_after = glyph->charpos;
13949 glyph_after = glyph;
13952 else if (dpos == 0)
13953 match_with_avoid_cursor = 1;
13955 else if (STRINGP (glyph->object))
13957 Lisp_Object chprop;
13958 ptrdiff_t glyph_pos = glyph->charpos;
13960 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13961 glyph->object);
13962 if (!NILP (chprop))
13964 /* If the string came from a `display' text property,
13965 look up the buffer position of that property and
13966 use that position to update bpos_max, as if we
13967 actually saw such a position in one of the row's
13968 glyphs. This helps with supporting integer values
13969 of `cursor' property on the display string in
13970 situations where most or all of the row's buffer
13971 text is completely covered by display properties,
13972 so that no glyph with valid buffer positions is
13973 ever seen in the row. */
13974 ptrdiff_t prop_pos =
13975 string_buffer_position_lim (glyph->object, pos_before,
13976 pos_after, 0);
13978 if (prop_pos >= pos_before)
13979 bpos_max = prop_pos - 1;
13981 if (INTEGERP (chprop))
13983 bpos_covered = bpos_max + XINT (chprop);
13984 /* If the `cursor' property covers buffer positions up
13985 to and including point, we should display cursor on
13986 this glyph. Note that, if a `cursor' property on one
13987 of the string's characters has an integer value, we
13988 will break out of the loop below _before_ we get to
13989 the position match above. IOW, integer values of
13990 the `cursor' property override the "exact match for
13991 point" strategy of positioning the cursor. */
13992 /* Implementation note: bpos_max == pt_old when, e.g.,
13993 we are in an empty line, where bpos_max is set to
13994 MATRIX_ROW_START_CHARPOS, see above. */
13995 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13997 cursor = glyph;
13998 break;
14002 string_seen = 1;
14004 x += glyph->pixel_width;
14005 ++glyph;
14007 else if (glyph > end) /* row is reversed */
14008 while (!INTEGERP (glyph->object))
14010 if (BUFFERP (glyph->object))
14012 ptrdiff_t dpos = glyph->charpos - pt_old;
14014 if (glyph->charpos > bpos_max)
14015 bpos_max = glyph->charpos;
14016 if (glyph->charpos < bpos_min)
14017 bpos_min = glyph->charpos;
14018 if (!glyph->avoid_cursor_p)
14020 if (dpos == 0)
14022 match_with_avoid_cursor = 0;
14023 break;
14025 if (0 > dpos && dpos > pos_before - pt_old)
14027 pos_before = glyph->charpos;
14028 glyph_before = glyph;
14030 else if (0 < dpos && dpos < pos_after - pt_old)
14032 pos_after = glyph->charpos;
14033 glyph_after = glyph;
14036 else if (dpos == 0)
14037 match_with_avoid_cursor = 1;
14039 else if (STRINGP (glyph->object))
14041 Lisp_Object chprop;
14042 ptrdiff_t glyph_pos = glyph->charpos;
14044 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14045 glyph->object);
14046 if (!NILP (chprop))
14048 ptrdiff_t prop_pos =
14049 string_buffer_position_lim (glyph->object, pos_before,
14050 pos_after, 0);
14052 if (prop_pos >= pos_before)
14053 bpos_max = prop_pos - 1;
14055 if (INTEGERP (chprop))
14057 bpos_covered = bpos_max + XINT (chprop);
14058 /* If the `cursor' property covers buffer positions up
14059 to and including point, we should display cursor on
14060 this glyph. */
14061 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14063 cursor = glyph;
14064 break;
14067 string_seen = 1;
14069 --glyph;
14070 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14072 x--; /* can't use any pixel_width */
14073 break;
14075 x -= glyph->pixel_width;
14078 /* Step 2: If we didn't find an exact match for point, we need to
14079 look for a proper place to put the cursor among glyphs between
14080 GLYPH_BEFORE and GLYPH_AFTER. */
14081 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14082 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14083 && bpos_covered < pt_old)
14085 /* An empty line has a single glyph whose OBJECT is zero and
14086 whose CHARPOS is the position of a newline on that line.
14087 Note that on a TTY, there are more glyphs after that, which
14088 were produced by extend_face_to_end_of_line, but their
14089 CHARPOS is zero or negative. */
14090 int empty_line_p =
14091 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14092 && INTEGERP (glyph->object) && glyph->charpos > 0;
14094 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14096 ptrdiff_t ellipsis_pos;
14098 /* Scan back over the ellipsis glyphs. */
14099 if (!row->reversed_p)
14101 ellipsis_pos = (glyph - 1)->charpos;
14102 while (glyph > row->glyphs[TEXT_AREA]
14103 && (glyph - 1)->charpos == ellipsis_pos)
14104 glyph--, x -= glyph->pixel_width;
14105 /* That loop always goes one position too far, including
14106 the glyph before the ellipsis. So scan forward over
14107 that one. */
14108 x += glyph->pixel_width;
14109 glyph++;
14111 else /* row is reversed */
14113 ellipsis_pos = (glyph + 1)->charpos;
14114 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14115 && (glyph + 1)->charpos == ellipsis_pos)
14116 glyph++, x += glyph->pixel_width;
14117 x -= glyph->pixel_width;
14118 glyph--;
14121 else if (match_with_avoid_cursor)
14123 cursor = glyph_after;
14124 x = -1;
14126 else if (string_seen)
14128 int incr = row->reversed_p ? -1 : +1;
14130 /* Need to find the glyph that came out of a string which is
14131 present at point. That glyph is somewhere between
14132 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14133 positioned between POS_BEFORE and POS_AFTER in the
14134 buffer. */
14135 struct glyph *start, *stop;
14136 ptrdiff_t pos = pos_before;
14138 x = -1;
14140 /* If the row ends in a newline from a display string,
14141 reordering could have moved the glyphs belonging to the
14142 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14143 in this case we extend the search to the last glyph in
14144 the row that was not inserted by redisplay. */
14145 if (row->ends_in_newline_from_string_p)
14147 glyph_after = end;
14148 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14151 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14152 correspond to POS_BEFORE and POS_AFTER, respectively. We
14153 need START and STOP in the order that corresponds to the
14154 row's direction as given by its reversed_p flag. If the
14155 directionality of characters between POS_BEFORE and
14156 POS_AFTER is the opposite of the row's base direction,
14157 these characters will have been reordered for display,
14158 and we need to reverse START and STOP. */
14159 if (!row->reversed_p)
14161 start = min (glyph_before, glyph_after);
14162 stop = max (glyph_before, glyph_after);
14164 else
14166 start = max (glyph_before, glyph_after);
14167 stop = min (glyph_before, glyph_after);
14169 for (glyph = start + incr;
14170 row->reversed_p ? glyph > stop : glyph < stop; )
14173 /* Any glyphs that come from the buffer are here because
14174 of bidi reordering. Skip them, and only pay
14175 attention to glyphs that came from some string. */
14176 if (STRINGP (glyph->object))
14178 Lisp_Object str;
14179 ptrdiff_t tem;
14180 /* If the display property covers the newline, we
14181 need to search for it one position farther. */
14182 ptrdiff_t lim = pos_after
14183 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14185 string_from_text_prop = 0;
14186 str = glyph->object;
14187 tem = string_buffer_position_lim (str, pos, lim, 0);
14188 if (tem == 0 /* from overlay */
14189 || pos <= tem)
14191 /* If the string from which this glyph came is
14192 found in the buffer at point, or at position
14193 that is closer to point than pos_after, then
14194 we've found the glyph we've been looking for.
14195 If it comes from an overlay (tem == 0), and
14196 it has the `cursor' property on one of its
14197 glyphs, record that glyph as a candidate for
14198 displaying the cursor. (As in the
14199 unidirectional version, we will display the
14200 cursor on the last candidate we find.) */
14201 if (tem == 0
14202 || tem == pt_old
14203 || (tem - pt_old > 0 && tem < pos_after))
14205 /* The glyphs from this string could have
14206 been reordered. Find the one with the
14207 smallest string position. Or there could
14208 be a character in the string with the
14209 `cursor' property, which means display
14210 cursor on that character's glyph. */
14211 ptrdiff_t strpos = glyph->charpos;
14213 if (tem)
14215 cursor = glyph;
14216 string_from_text_prop = 1;
14218 for ( ;
14219 (row->reversed_p ? glyph > stop : glyph < stop)
14220 && EQ (glyph->object, str);
14221 glyph += incr)
14223 Lisp_Object cprop;
14224 ptrdiff_t gpos = glyph->charpos;
14226 cprop = Fget_char_property (make_number (gpos),
14227 Qcursor,
14228 glyph->object);
14229 if (!NILP (cprop))
14231 cursor = glyph;
14232 break;
14234 if (tem && glyph->charpos < strpos)
14236 strpos = glyph->charpos;
14237 cursor = glyph;
14241 if (tem == pt_old
14242 || (tem - pt_old > 0 && tem < pos_after))
14243 goto compute_x;
14245 if (tem)
14246 pos = tem + 1; /* don't find previous instances */
14248 /* This string is not what we want; skip all of the
14249 glyphs that came from it. */
14250 while ((row->reversed_p ? glyph > stop : glyph < stop)
14251 && EQ (glyph->object, str))
14252 glyph += incr;
14254 else
14255 glyph += incr;
14258 /* If we reached the end of the line, and END was from a string,
14259 the cursor is not on this line. */
14260 if (cursor == NULL
14261 && (row->reversed_p ? glyph <= end : glyph >= end)
14262 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14263 && STRINGP (end->object)
14264 && row->continued_p)
14265 return 0;
14267 /* A truncated row may not include PT among its character positions.
14268 Setting the cursor inside the scroll margin will trigger
14269 recalculation of hscroll in hscroll_window_tree. But if a
14270 display string covers point, defer to the string-handling
14271 code below to figure this out. */
14272 else if (row->truncated_on_left_p && pt_old < bpos_min)
14274 cursor = glyph_before;
14275 x = -1;
14277 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14278 /* Zero-width characters produce no glyphs. */
14279 || (!empty_line_p
14280 && (row->reversed_p
14281 ? glyph_after > glyphs_end
14282 : glyph_after < glyphs_end)))
14284 cursor = glyph_after;
14285 x = -1;
14289 compute_x:
14290 if (cursor != NULL)
14291 glyph = cursor;
14292 else if (glyph == glyphs_end
14293 && pos_before == pos_after
14294 && STRINGP ((row->reversed_p
14295 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14296 : row->glyphs[TEXT_AREA])->object))
14298 /* If all the glyphs of this row came from strings, put the
14299 cursor on the first glyph of the row. This avoids having the
14300 cursor outside of the text area in this very rare and hard
14301 use case. */
14302 glyph =
14303 row->reversed_p
14304 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14305 : row->glyphs[TEXT_AREA];
14307 if (x < 0)
14309 struct glyph *g;
14311 /* Need to compute x that corresponds to GLYPH. */
14312 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14314 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14315 abort ();
14316 x += g->pixel_width;
14320 /* ROW could be part of a continued line, which, under bidi
14321 reordering, might have other rows whose start and end charpos
14322 occlude point. Only set w->cursor if we found a better
14323 approximation to the cursor position than we have from previously
14324 examined candidate rows belonging to the same continued line. */
14325 if (/* we already have a candidate row */
14326 w->cursor.vpos >= 0
14327 /* that candidate is not the row we are processing */
14328 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14329 /* Make sure cursor.vpos specifies a row whose start and end
14330 charpos occlude point, and it is valid candidate for being a
14331 cursor-row. This is because some callers of this function
14332 leave cursor.vpos at the row where the cursor was displayed
14333 during the last redisplay cycle. */
14334 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14335 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14336 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14338 struct glyph *g1 =
14339 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14341 /* Don't consider glyphs that are outside TEXT_AREA. */
14342 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14343 return 0;
14344 /* Keep the candidate whose buffer position is the closest to
14345 point or has the `cursor' property. */
14346 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14347 w->cursor.hpos >= 0
14348 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14349 && ((BUFFERP (g1->object)
14350 && (g1->charpos == pt_old /* an exact match always wins */
14351 || (BUFFERP (glyph->object)
14352 && eabs (g1->charpos - pt_old)
14353 < eabs (glyph->charpos - pt_old))))
14354 /* previous candidate is a glyph from a string that has
14355 a non-nil `cursor' property */
14356 || (STRINGP (g1->object)
14357 && (!NILP (Fget_char_property (make_number (g1->charpos),
14358 Qcursor, g1->object))
14359 /* previous candidate is from the same display
14360 string as this one, and the display string
14361 came from a text property */
14362 || (EQ (g1->object, glyph->object)
14363 && string_from_text_prop)
14364 /* this candidate is from newline and its
14365 position is not an exact match */
14366 || (INTEGERP (glyph->object)
14367 && glyph->charpos != pt_old)))))
14368 return 0;
14369 /* If this candidate gives an exact match, use that. */
14370 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14371 /* If this candidate is a glyph created for the
14372 terminating newline of a line, and point is on that
14373 newline, it wins because it's an exact match. */
14374 || (!row->continued_p
14375 && INTEGERP (glyph->object)
14376 && glyph->charpos == 0
14377 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14378 /* Otherwise, keep the candidate that comes from a row
14379 spanning less buffer positions. This may win when one or
14380 both candidate positions are on glyphs that came from
14381 display strings, for which we cannot compare buffer
14382 positions. */
14383 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14384 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14385 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14386 return 0;
14388 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14389 w->cursor.x = x;
14390 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14391 w->cursor.y = row->y + dy;
14393 if (w == XWINDOW (selected_window))
14395 if (!row->continued_p
14396 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14397 && row->x == 0)
14399 this_line_buffer = XBUFFER (w->buffer);
14401 CHARPOS (this_line_start_pos)
14402 = MATRIX_ROW_START_CHARPOS (row) + delta;
14403 BYTEPOS (this_line_start_pos)
14404 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14406 CHARPOS (this_line_end_pos)
14407 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14408 BYTEPOS (this_line_end_pos)
14409 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14411 this_line_y = w->cursor.y;
14412 this_line_pixel_height = row->height;
14413 this_line_vpos = w->cursor.vpos;
14414 this_line_start_x = row->x;
14416 else
14417 CHARPOS (this_line_start_pos) = 0;
14420 return 1;
14424 /* Run window scroll functions, if any, for WINDOW with new window
14425 start STARTP. Sets the window start of WINDOW to that position.
14427 We assume that the window's buffer is really current. */
14429 static inline struct text_pos
14430 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14432 struct window *w = XWINDOW (window);
14433 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14435 if (current_buffer != XBUFFER (w->buffer))
14436 abort ();
14438 if (!NILP (Vwindow_scroll_functions))
14440 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14441 make_number (CHARPOS (startp)));
14442 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14443 /* In case the hook functions switch buffers. */
14444 if (current_buffer != XBUFFER (w->buffer))
14445 set_buffer_internal_1 (XBUFFER (w->buffer));
14448 return startp;
14452 /* Make sure the line containing the cursor is fully visible.
14453 A value of 1 means there is nothing to be done.
14454 (Either the line is fully visible, or it cannot be made so,
14455 or we cannot tell.)
14457 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14458 is higher than window.
14460 A value of 0 means the caller should do scrolling
14461 as if point had gone off the screen. */
14463 static int
14464 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14466 struct glyph_matrix *matrix;
14467 struct glyph_row *row;
14468 int window_height;
14470 if (!make_cursor_line_fully_visible_p)
14471 return 1;
14473 /* It's not always possible to find the cursor, e.g, when a window
14474 is full of overlay strings. Don't do anything in that case. */
14475 if (w->cursor.vpos < 0)
14476 return 1;
14478 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14479 row = MATRIX_ROW (matrix, w->cursor.vpos);
14481 /* If the cursor row is not partially visible, there's nothing to do. */
14482 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14483 return 1;
14485 /* If the row the cursor is in is taller than the window's height,
14486 it's not clear what to do, so do nothing. */
14487 window_height = window_box_height (w);
14488 if (row->height >= window_height)
14490 if (!force_p || MINI_WINDOW_P (w)
14491 || w->vscroll || w->cursor.vpos == 0)
14492 return 1;
14494 return 0;
14498 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14499 non-zero means only WINDOW is redisplayed in redisplay_internal.
14500 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14501 in redisplay_window to bring a partially visible line into view in
14502 the case that only the cursor has moved.
14504 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14505 last screen line's vertical height extends past the end of the screen.
14507 Value is
14509 1 if scrolling succeeded
14511 0 if scrolling didn't find point.
14513 -1 if new fonts have been loaded so that we must interrupt
14514 redisplay, adjust glyph matrices, and try again. */
14516 enum
14518 SCROLLING_SUCCESS,
14519 SCROLLING_FAILED,
14520 SCROLLING_NEED_LARGER_MATRICES
14523 /* If scroll-conservatively is more than this, never recenter.
14525 If you change this, don't forget to update the doc string of
14526 `scroll-conservatively' and the Emacs manual. */
14527 #define SCROLL_LIMIT 100
14529 static int
14530 try_scrolling (Lisp_Object window, int just_this_one_p,
14531 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14532 int temp_scroll_step, int last_line_misfit)
14534 struct window *w = XWINDOW (window);
14535 struct frame *f = XFRAME (w->frame);
14536 struct text_pos pos, startp;
14537 struct it it;
14538 int this_scroll_margin, scroll_max, rc, height;
14539 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14540 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14541 Lisp_Object aggressive;
14542 /* We will never try scrolling more than this number of lines. */
14543 int scroll_limit = SCROLL_LIMIT;
14545 #if GLYPH_DEBUG
14546 debug_method_add (w, "try_scrolling");
14547 #endif
14549 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14551 /* Compute scroll margin height in pixels. We scroll when point is
14552 within this distance from the top or bottom of the window. */
14553 if (scroll_margin > 0)
14554 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14555 * FRAME_LINE_HEIGHT (f);
14556 else
14557 this_scroll_margin = 0;
14559 /* Force arg_scroll_conservatively to have a reasonable value, to
14560 avoid scrolling too far away with slow move_it_* functions. Note
14561 that the user can supply scroll-conservatively equal to
14562 `most-positive-fixnum', which can be larger than INT_MAX. */
14563 if (arg_scroll_conservatively > scroll_limit)
14565 arg_scroll_conservatively = scroll_limit + 1;
14566 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14568 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14569 /* Compute how much we should try to scroll maximally to bring
14570 point into view. */
14571 scroll_max = (max (scroll_step,
14572 max (arg_scroll_conservatively, temp_scroll_step))
14573 * FRAME_LINE_HEIGHT (f));
14574 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14575 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14576 /* We're trying to scroll because of aggressive scrolling but no
14577 scroll_step is set. Choose an arbitrary one. */
14578 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14579 else
14580 scroll_max = 0;
14582 too_near_end:
14584 /* Decide whether to scroll down. */
14585 if (PT > CHARPOS (startp))
14587 int scroll_margin_y;
14589 /* Compute the pixel ypos of the scroll margin, then move IT to
14590 either that ypos or PT, whichever comes first. */
14591 start_display (&it, w, startp);
14592 scroll_margin_y = it.last_visible_y - this_scroll_margin
14593 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14594 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14595 (MOVE_TO_POS | MOVE_TO_Y));
14597 if (PT > CHARPOS (it.current.pos))
14599 int y0 = line_bottom_y (&it);
14600 /* Compute how many pixels below window bottom to stop searching
14601 for PT. This avoids costly search for PT that is far away if
14602 the user limited scrolling by a small number of lines, but
14603 always finds PT if scroll_conservatively is set to a large
14604 number, such as most-positive-fixnum. */
14605 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14606 int y_to_move = it.last_visible_y + slack;
14608 /* Compute the distance from the scroll margin to PT or to
14609 the scroll limit, whichever comes first. This should
14610 include the height of the cursor line, to make that line
14611 fully visible. */
14612 move_it_to (&it, PT, -1, y_to_move,
14613 -1, MOVE_TO_POS | MOVE_TO_Y);
14614 dy = line_bottom_y (&it) - y0;
14616 if (dy > scroll_max)
14617 return SCROLLING_FAILED;
14619 if (dy > 0)
14620 scroll_down_p = 1;
14624 if (scroll_down_p)
14626 /* Point is in or below the bottom scroll margin, so move the
14627 window start down. If scrolling conservatively, move it just
14628 enough down to make point visible. If scroll_step is set,
14629 move it down by scroll_step. */
14630 if (arg_scroll_conservatively)
14631 amount_to_scroll
14632 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14633 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14634 else if (scroll_step || temp_scroll_step)
14635 amount_to_scroll = scroll_max;
14636 else
14638 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14639 height = WINDOW_BOX_TEXT_HEIGHT (w);
14640 if (NUMBERP (aggressive))
14642 double float_amount = XFLOATINT (aggressive) * height;
14643 amount_to_scroll = float_amount;
14644 if (amount_to_scroll == 0 && float_amount > 0)
14645 amount_to_scroll = 1;
14646 /* Don't let point enter the scroll margin near top of
14647 the window. */
14648 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14649 amount_to_scroll = height - 2*this_scroll_margin + dy;
14653 if (amount_to_scroll <= 0)
14654 return SCROLLING_FAILED;
14656 start_display (&it, w, startp);
14657 if (arg_scroll_conservatively <= scroll_limit)
14658 move_it_vertically (&it, amount_to_scroll);
14659 else
14661 /* Extra precision for users who set scroll-conservatively
14662 to a large number: make sure the amount we scroll
14663 the window start is never less than amount_to_scroll,
14664 which was computed as distance from window bottom to
14665 point. This matters when lines at window top and lines
14666 below window bottom have different height. */
14667 struct it it1;
14668 void *it1data = NULL;
14669 /* We use a temporary it1 because line_bottom_y can modify
14670 its argument, if it moves one line down; see there. */
14671 int start_y;
14673 SAVE_IT (it1, it, it1data);
14674 start_y = line_bottom_y (&it1);
14675 do {
14676 RESTORE_IT (&it, &it, it1data);
14677 move_it_by_lines (&it, 1);
14678 SAVE_IT (it1, it, it1data);
14679 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14682 /* If STARTP is unchanged, move it down another screen line. */
14683 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14684 move_it_by_lines (&it, 1);
14685 startp = it.current.pos;
14687 else
14689 struct text_pos scroll_margin_pos = startp;
14691 /* See if point is inside the scroll margin at the top of the
14692 window. */
14693 if (this_scroll_margin)
14695 start_display (&it, w, startp);
14696 move_it_vertically (&it, this_scroll_margin);
14697 scroll_margin_pos = it.current.pos;
14700 if (PT < CHARPOS (scroll_margin_pos))
14702 /* Point is in the scroll margin at the top of the window or
14703 above what is displayed in the window. */
14704 int y0, y_to_move;
14706 /* Compute the vertical distance from PT to the scroll
14707 margin position. Move as far as scroll_max allows, or
14708 one screenful, or 10 screen lines, whichever is largest.
14709 Give up if distance is greater than scroll_max. */
14710 SET_TEXT_POS (pos, PT, PT_BYTE);
14711 start_display (&it, w, pos);
14712 y0 = it.current_y;
14713 y_to_move = max (it.last_visible_y,
14714 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14715 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14716 y_to_move, -1,
14717 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14718 dy = it.current_y - y0;
14719 if (dy > scroll_max)
14720 return SCROLLING_FAILED;
14722 /* Compute new window start. */
14723 start_display (&it, w, startp);
14725 if (arg_scroll_conservatively)
14726 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14727 max (scroll_step, temp_scroll_step));
14728 else if (scroll_step || temp_scroll_step)
14729 amount_to_scroll = scroll_max;
14730 else
14732 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14733 height = WINDOW_BOX_TEXT_HEIGHT (w);
14734 if (NUMBERP (aggressive))
14736 double float_amount = XFLOATINT (aggressive) * height;
14737 amount_to_scroll = float_amount;
14738 if (amount_to_scroll == 0 && float_amount > 0)
14739 amount_to_scroll = 1;
14740 amount_to_scroll -=
14741 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14742 /* Don't let point enter the scroll margin near
14743 bottom of the window. */
14744 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14745 amount_to_scroll = height - 2*this_scroll_margin + dy;
14749 if (amount_to_scroll <= 0)
14750 return SCROLLING_FAILED;
14752 move_it_vertically_backward (&it, amount_to_scroll);
14753 startp = it.current.pos;
14757 /* Run window scroll functions. */
14758 startp = run_window_scroll_functions (window, startp);
14760 /* Display the window. Give up if new fonts are loaded, or if point
14761 doesn't appear. */
14762 if (!try_window (window, startp, 0))
14763 rc = SCROLLING_NEED_LARGER_MATRICES;
14764 else if (w->cursor.vpos < 0)
14766 clear_glyph_matrix (w->desired_matrix);
14767 rc = SCROLLING_FAILED;
14769 else
14771 /* Maybe forget recorded base line for line number display. */
14772 if (!just_this_one_p
14773 || current_buffer->clip_changed
14774 || BEG_UNCHANGED < CHARPOS (startp))
14775 w->base_line_number = Qnil;
14777 /* If cursor ends up on a partially visible line,
14778 treat that as being off the bottom of the screen. */
14779 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14780 /* It's possible that the cursor is on the first line of the
14781 buffer, which is partially obscured due to a vscroll
14782 (Bug#7537). In that case, avoid looping forever . */
14783 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14785 clear_glyph_matrix (w->desired_matrix);
14786 ++extra_scroll_margin_lines;
14787 goto too_near_end;
14789 rc = SCROLLING_SUCCESS;
14792 return rc;
14796 /* Compute a suitable window start for window W if display of W starts
14797 on a continuation line. Value is non-zero if a new window start
14798 was computed.
14800 The new window start will be computed, based on W's width, starting
14801 from the start of the continued line. It is the start of the
14802 screen line with the minimum distance from the old start W->start. */
14804 static int
14805 compute_window_start_on_continuation_line (struct window *w)
14807 struct text_pos pos, start_pos;
14808 int window_start_changed_p = 0;
14810 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14812 /* If window start is on a continuation line... Window start may be
14813 < BEGV in case there's invisible text at the start of the
14814 buffer (M-x rmail, for example). */
14815 if (CHARPOS (start_pos) > BEGV
14816 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14818 struct it it;
14819 struct glyph_row *row;
14821 /* Handle the case that the window start is out of range. */
14822 if (CHARPOS (start_pos) < BEGV)
14823 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14824 else if (CHARPOS (start_pos) > ZV)
14825 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14827 /* Find the start of the continued line. This should be fast
14828 because scan_buffer is fast (newline cache). */
14829 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14830 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14831 row, DEFAULT_FACE_ID);
14832 reseat_at_previous_visible_line_start (&it);
14834 /* If the line start is "too far" away from the window start,
14835 say it takes too much time to compute a new window start. */
14836 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14837 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14839 int min_distance, distance;
14841 /* Move forward by display lines to find the new window
14842 start. If window width was enlarged, the new start can
14843 be expected to be > the old start. If window width was
14844 decreased, the new window start will be < the old start.
14845 So, we're looking for the display line start with the
14846 minimum distance from the old window start. */
14847 pos = it.current.pos;
14848 min_distance = INFINITY;
14849 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14850 distance < min_distance)
14852 min_distance = distance;
14853 pos = it.current.pos;
14854 move_it_by_lines (&it, 1);
14857 /* Set the window start there. */
14858 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14859 window_start_changed_p = 1;
14863 return window_start_changed_p;
14867 /* Try cursor movement in case text has not changed in window WINDOW,
14868 with window start STARTP. Value is
14870 CURSOR_MOVEMENT_SUCCESS if successful
14872 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14874 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14875 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14876 we want to scroll as if scroll-step were set to 1. See the code.
14878 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14879 which case we have to abort this redisplay, and adjust matrices
14880 first. */
14882 enum
14884 CURSOR_MOVEMENT_SUCCESS,
14885 CURSOR_MOVEMENT_CANNOT_BE_USED,
14886 CURSOR_MOVEMENT_MUST_SCROLL,
14887 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14890 static int
14891 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14893 struct window *w = XWINDOW (window);
14894 struct frame *f = XFRAME (w->frame);
14895 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14897 #if GLYPH_DEBUG
14898 if (inhibit_try_cursor_movement)
14899 return rc;
14900 #endif
14902 /* Handle case where text has not changed, only point, and it has
14903 not moved off the frame. */
14904 if (/* Point may be in this window. */
14905 PT >= CHARPOS (startp)
14906 /* Selective display hasn't changed. */
14907 && !current_buffer->clip_changed
14908 /* Function force-mode-line-update is used to force a thorough
14909 redisplay. It sets either windows_or_buffers_changed or
14910 update_mode_lines. So don't take a shortcut here for these
14911 cases. */
14912 && !update_mode_lines
14913 && !windows_or_buffers_changed
14914 && !cursor_type_changed
14915 /* Can't use this case if highlighting a region. When a
14916 region exists, cursor movement has to do more than just
14917 set the cursor. */
14918 && !(!NILP (Vtransient_mark_mode)
14919 && !NILP (BVAR (current_buffer, mark_active)))
14920 && NILP (w->region_showing)
14921 && NILP (Vshow_trailing_whitespace)
14922 /* Right after splitting windows, last_point may be nil. */
14923 && INTEGERP (w->last_point)
14924 /* This code is not used for mini-buffer for the sake of the case
14925 of redisplaying to replace an echo area message; since in
14926 that case the mini-buffer contents per se are usually
14927 unchanged. This code is of no real use in the mini-buffer
14928 since the handling of this_line_start_pos, etc., in redisplay
14929 handles the same cases. */
14930 && !EQ (window, minibuf_window)
14931 /* When splitting windows or for new windows, it happens that
14932 redisplay is called with a nil window_end_vpos or one being
14933 larger than the window. This should really be fixed in
14934 window.c. I don't have this on my list, now, so we do
14935 approximately the same as the old redisplay code. --gerd. */
14936 && INTEGERP (w->window_end_vpos)
14937 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14938 && (FRAME_WINDOW_P (f)
14939 || !overlay_arrow_in_current_buffer_p ()))
14941 int this_scroll_margin, top_scroll_margin;
14942 struct glyph_row *row = NULL;
14944 #if GLYPH_DEBUG
14945 debug_method_add (w, "cursor movement");
14946 #endif
14948 /* Scroll if point within this distance from the top or bottom
14949 of the window. This is a pixel value. */
14950 if (scroll_margin > 0)
14952 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14953 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14955 else
14956 this_scroll_margin = 0;
14958 top_scroll_margin = this_scroll_margin;
14959 if (WINDOW_WANTS_HEADER_LINE_P (w))
14960 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14962 /* Start with the row the cursor was displayed during the last
14963 not paused redisplay. Give up if that row is not valid. */
14964 if (w->last_cursor.vpos < 0
14965 || w->last_cursor.vpos >= w->current_matrix->nrows)
14966 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14967 else
14969 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14970 if (row->mode_line_p)
14971 ++row;
14972 if (!row->enabled_p)
14973 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14976 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14978 int scroll_p = 0, must_scroll = 0;
14979 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14981 if (PT > XFASTINT (w->last_point))
14983 /* Point has moved forward. */
14984 while (MATRIX_ROW_END_CHARPOS (row) < PT
14985 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14987 xassert (row->enabled_p);
14988 ++row;
14991 /* If the end position of a row equals the start
14992 position of the next row, and PT is at that position,
14993 we would rather display cursor in the next line. */
14994 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14995 && MATRIX_ROW_END_CHARPOS (row) == PT
14996 && row < w->current_matrix->rows
14997 + w->current_matrix->nrows - 1
14998 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14999 && !cursor_row_p (row))
15000 ++row;
15002 /* If within the scroll margin, scroll. Note that
15003 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15004 the next line would be drawn, and that
15005 this_scroll_margin can be zero. */
15006 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15007 || PT > MATRIX_ROW_END_CHARPOS (row)
15008 /* Line is completely visible last line in window
15009 and PT is to be set in the next line. */
15010 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15011 && PT == MATRIX_ROW_END_CHARPOS (row)
15012 && !row->ends_at_zv_p
15013 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15014 scroll_p = 1;
15016 else if (PT < XFASTINT (w->last_point))
15018 /* Cursor has to be moved backward. Note that PT >=
15019 CHARPOS (startp) because of the outer if-statement. */
15020 while (!row->mode_line_p
15021 && (MATRIX_ROW_START_CHARPOS (row) > PT
15022 || (MATRIX_ROW_START_CHARPOS (row) == PT
15023 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15024 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15025 row > w->current_matrix->rows
15026 && (row-1)->ends_in_newline_from_string_p))))
15027 && (row->y > top_scroll_margin
15028 || CHARPOS (startp) == BEGV))
15030 xassert (row->enabled_p);
15031 --row;
15034 /* Consider the following case: Window starts at BEGV,
15035 there is invisible, intangible text at BEGV, so that
15036 display starts at some point START > BEGV. It can
15037 happen that we are called with PT somewhere between
15038 BEGV and START. Try to handle that case. */
15039 if (row < w->current_matrix->rows
15040 || row->mode_line_p)
15042 row = w->current_matrix->rows;
15043 if (row->mode_line_p)
15044 ++row;
15047 /* Due to newlines in overlay strings, we may have to
15048 skip forward over overlay strings. */
15049 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15050 && MATRIX_ROW_END_CHARPOS (row) == PT
15051 && !cursor_row_p (row))
15052 ++row;
15054 /* If within the scroll margin, scroll. */
15055 if (row->y < top_scroll_margin
15056 && CHARPOS (startp) != BEGV)
15057 scroll_p = 1;
15059 else
15061 /* Cursor did not move. So don't scroll even if cursor line
15062 is partially visible, as it was so before. */
15063 rc = CURSOR_MOVEMENT_SUCCESS;
15066 if (PT < MATRIX_ROW_START_CHARPOS (row)
15067 || PT > MATRIX_ROW_END_CHARPOS (row))
15069 /* if PT is not in the glyph row, give up. */
15070 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15071 must_scroll = 1;
15073 else if (rc != CURSOR_MOVEMENT_SUCCESS
15074 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15076 struct glyph_row *row1;
15078 /* If rows are bidi-reordered and point moved, back up
15079 until we find a row that does not belong to a
15080 continuation line. This is because we must consider
15081 all rows of a continued line as candidates for the
15082 new cursor positioning, since row start and end
15083 positions change non-linearly with vertical position
15084 in such rows. */
15085 /* FIXME: Revisit this when glyph ``spilling'' in
15086 continuation lines' rows is implemented for
15087 bidi-reordered rows. */
15088 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15089 MATRIX_ROW_CONTINUATION_LINE_P (row);
15090 --row)
15092 /* If we hit the beginning of the displayed portion
15093 without finding the first row of a continued
15094 line, give up. */
15095 if (row <= row1)
15097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15098 break;
15100 xassert (row->enabled_p);
15103 if (must_scroll)
15105 else if (rc != CURSOR_MOVEMENT_SUCCESS
15106 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15107 /* Make sure this isn't a header line by any chance, since
15108 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15109 && !row->mode_line_p
15110 && make_cursor_line_fully_visible_p)
15112 if (PT == MATRIX_ROW_END_CHARPOS (row)
15113 && !row->ends_at_zv_p
15114 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15115 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15116 else if (row->height > window_box_height (w))
15118 /* If we end up in a partially visible line, let's
15119 make it fully visible, except when it's taller
15120 than the window, in which case we can't do much
15121 about it. */
15122 *scroll_step = 1;
15123 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15125 else
15127 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15128 if (!cursor_row_fully_visible_p (w, 0, 1))
15129 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15130 else
15131 rc = CURSOR_MOVEMENT_SUCCESS;
15134 else if (scroll_p)
15135 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15136 else if (rc != CURSOR_MOVEMENT_SUCCESS
15137 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15139 /* With bidi-reordered rows, there could be more than
15140 one candidate row whose start and end positions
15141 occlude point. We need to let set_cursor_from_row
15142 find the best candidate. */
15143 /* FIXME: Revisit this when glyph ``spilling'' in
15144 continuation lines' rows is implemented for
15145 bidi-reordered rows. */
15146 int rv = 0;
15150 int at_zv_p = 0, exact_match_p = 0;
15152 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15153 && PT <= MATRIX_ROW_END_CHARPOS (row)
15154 && cursor_row_p (row))
15155 rv |= set_cursor_from_row (w, row, w->current_matrix,
15156 0, 0, 0, 0);
15157 /* As soon as we've found the exact match for point,
15158 or the first suitable row whose ends_at_zv_p flag
15159 is set, we are done. */
15160 at_zv_p =
15161 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15162 if (rv && !at_zv_p
15163 && w->cursor.hpos >= 0
15164 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15165 w->cursor.vpos))
15167 struct glyph_row *candidate =
15168 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15169 struct glyph *g =
15170 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15171 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15173 exact_match_p =
15174 (BUFFERP (g->object) && g->charpos == PT)
15175 || (INTEGERP (g->object)
15176 && (g->charpos == PT
15177 || (g->charpos == 0 && endpos - 1 == PT)));
15179 if (rv && (at_zv_p || exact_match_p))
15181 rc = CURSOR_MOVEMENT_SUCCESS;
15182 break;
15184 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15185 break;
15186 ++row;
15188 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15189 || row->continued_p)
15190 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15191 || (MATRIX_ROW_START_CHARPOS (row) == PT
15192 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15193 /* If we didn't find any candidate rows, or exited the
15194 loop before all the candidates were examined, signal
15195 to the caller that this method failed. */
15196 if (rc != CURSOR_MOVEMENT_SUCCESS
15197 && !(rv
15198 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15199 && !row->continued_p))
15200 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15201 else if (rv)
15202 rc = CURSOR_MOVEMENT_SUCCESS;
15204 else
15208 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15210 rc = CURSOR_MOVEMENT_SUCCESS;
15211 break;
15213 ++row;
15215 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15216 && MATRIX_ROW_START_CHARPOS (row) == PT
15217 && cursor_row_p (row));
15222 return rc;
15225 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15226 static
15227 #endif
15228 void
15229 set_vertical_scroll_bar (struct window *w)
15231 ptrdiff_t start, end, whole;
15233 /* Calculate the start and end positions for the current window.
15234 At some point, it would be nice to choose between scrollbars
15235 which reflect the whole buffer size, with special markers
15236 indicating narrowing, and scrollbars which reflect only the
15237 visible region.
15239 Note that mini-buffers sometimes aren't displaying any text. */
15240 if (!MINI_WINDOW_P (w)
15241 || (w == XWINDOW (minibuf_window)
15242 && NILP (echo_area_buffer[0])))
15244 struct buffer *buf = XBUFFER (w->buffer);
15245 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15246 start = marker_position (w->start) - BUF_BEGV (buf);
15247 /* I don't think this is guaranteed to be right. For the
15248 moment, we'll pretend it is. */
15249 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15251 if (end < start)
15252 end = start;
15253 if (whole < (end - start))
15254 whole = end - start;
15256 else
15257 start = end = whole = 0;
15259 /* Indicate what this scroll bar ought to be displaying now. */
15260 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15261 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15262 (w, end - start, whole, start);
15266 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15267 selected_window is redisplayed.
15269 We can return without actually redisplaying the window if
15270 fonts_changed_p is nonzero. In that case, redisplay_internal will
15271 retry. */
15273 static void
15274 redisplay_window (Lisp_Object window, int just_this_one_p)
15276 struct window *w = XWINDOW (window);
15277 struct frame *f = XFRAME (w->frame);
15278 struct buffer *buffer = XBUFFER (w->buffer);
15279 struct buffer *old = current_buffer;
15280 struct text_pos lpoint, opoint, startp;
15281 int update_mode_line;
15282 int tem;
15283 struct it it;
15284 /* Record it now because it's overwritten. */
15285 int current_matrix_up_to_date_p = 0;
15286 int used_current_matrix_p = 0;
15287 /* This is less strict than current_matrix_up_to_date_p.
15288 It indicates that the buffer contents and narrowing are unchanged. */
15289 int buffer_unchanged_p = 0;
15290 int temp_scroll_step = 0;
15291 ptrdiff_t count = SPECPDL_INDEX ();
15292 int rc;
15293 int centering_position = -1;
15294 int last_line_misfit = 0;
15295 ptrdiff_t beg_unchanged, end_unchanged;
15297 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15298 opoint = lpoint;
15300 /* W must be a leaf window here. */
15301 xassert (!NILP (w->buffer));
15302 #if GLYPH_DEBUG
15303 *w->desired_matrix->method = 0;
15304 #endif
15306 restart:
15307 reconsider_clip_changes (w, buffer);
15309 /* Has the mode line to be updated? */
15310 update_mode_line = (w->update_mode_line
15311 || update_mode_lines
15312 || buffer->clip_changed
15313 || buffer->prevent_redisplay_optimizations_p);
15315 if (MINI_WINDOW_P (w))
15317 if (w == XWINDOW (echo_area_window)
15318 && !NILP (echo_area_buffer[0]))
15320 if (update_mode_line)
15321 /* We may have to update a tty frame's menu bar or a
15322 tool-bar. Example `M-x C-h C-h C-g'. */
15323 goto finish_menu_bars;
15324 else
15325 /* We've already displayed the echo area glyphs in this window. */
15326 goto finish_scroll_bars;
15328 else if ((w != XWINDOW (minibuf_window)
15329 || minibuf_level == 0)
15330 /* When buffer is nonempty, redisplay window normally. */
15331 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15332 /* Quail displays non-mini buffers in minibuffer window.
15333 In that case, redisplay the window normally. */
15334 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15336 /* W is a mini-buffer window, but it's not active, so clear
15337 it. */
15338 int yb = window_text_bottom_y (w);
15339 struct glyph_row *row;
15340 int y;
15342 for (y = 0, row = w->desired_matrix->rows;
15343 y < yb;
15344 y += row->height, ++row)
15345 blank_row (w, row, y);
15346 goto finish_scroll_bars;
15349 clear_glyph_matrix (w->desired_matrix);
15352 /* Otherwise set up data on this window; select its buffer and point
15353 value. */
15354 /* Really select the buffer, for the sake of buffer-local
15355 variables. */
15356 set_buffer_internal_1 (XBUFFER (w->buffer));
15358 current_matrix_up_to_date_p
15359 = (!NILP (w->window_end_valid)
15360 && !current_buffer->clip_changed
15361 && !current_buffer->prevent_redisplay_optimizations_p
15362 && XFASTINT (w->last_modified) >= MODIFF
15363 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15365 /* Run the window-bottom-change-functions
15366 if it is possible that the text on the screen has changed
15367 (either due to modification of the text, or any other reason). */
15368 if (!current_matrix_up_to_date_p
15369 && !NILP (Vwindow_text_change_functions))
15371 safe_run_hooks (Qwindow_text_change_functions);
15372 goto restart;
15375 beg_unchanged = BEG_UNCHANGED;
15376 end_unchanged = END_UNCHANGED;
15378 SET_TEXT_POS (opoint, PT, PT_BYTE);
15380 specbind (Qinhibit_point_motion_hooks, Qt);
15382 buffer_unchanged_p
15383 = (!NILP (w->window_end_valid)
15384 && !current_buffer->clip_changed
15385 && XFASTINT (w->last_modified) >= MODIFF
15386 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15388 /* When windows_or_buffers_changed is non-zero, we can't rely on
15389 the window end being valid, so set it to nil there. */
15390 if (windows_or_buffers_changed)
15392 /* If window starts on a continuation line, maybe adjust the
15393 window start in case the window's width changed. */
15394 if (XMARKER (w->start)->buffer == current_buffer)
15395 compute_window_start_on_continuation_line (w);
15397 w->window_end_valid = Qnil;
15400 /* Some sanity checks. */
15401 CHECK_WINDOW_END (w);
15402 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15403 abort ();
15404 if (BYTEPOS (opoint) < CHARPOS (opoint))
15405 abort ();
15407 /* If %c is in mode line, update it if needed. */
15408 if (!NILP (w->column_number_displayed)
15409 /* This alternative quickly identifies a common case
15410 where no change is needed. */
15411 && !(PT == XFASTINT (w->last_point)
15412 && XFASTINT (w->last_modified) >= MODIFF
15413 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15414 && (XFASTINT (w->column_number_displayed) != current_column ()))
15415 update_mode_line = 1;
15417 /* Count number of windows showing the selected buffer. An indirect
15418 buffer counts as its base buffer. */
15419 if (!just_this_one_p)
15421 struct buffer *current_base, *window_base;
15422 current_base = current_buffer;
15423 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15424 if (current_base->base_buffer)
15425 current_base = current_base->base_buffer;
15426 if (window_base->base_buffer)
15427 window_base = window_base->base_buffer;
15428 if (current_base == window_base)
15429 buffer_shared++;
15432 /* Point refers normally to the selected window. For any other
15433 window, set up appropriate value. */
15434 if (!EQ (window, selected_window))
15436 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15437 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15438 if (new_pt < BEGV)
15440 new_pt = BEGV;
15441 new_pt_byte = BEGV_BYTE;
15442 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15444 else if (new_pt > (ZV - 1))
15446 new_pt = ZV;
15447 new_pt_byte = ZV_BYTE;
15448 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15451 /* We don't use SET_PT so that the point-motion hooks don't run. */
15452 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15455 /* If any of the character widths specified in the display table
15456 have changed, invalidate the width run cache. It's true that
15457 this may be a bit late to catch such changes, but the rest of
15458 redisplay goes (non-fatally) haywire when the display table is
15459 changed, so why should we worry about doing any better? */
15460 if (current_buffer->width_run_cache)
15462 struct Lisp_Char_Table *disptab = buffer_display_table ();
15464 if (! disptab_matches_widthtab (disptab,
15465 XVECTOR (BVAR (current_buffer, width_table))))
15467 invalidate_region_cache (current_buffer,
15468 current_buffer->width_run_cache,
15469 BEG, Z);
15470 recompute_width_table (current_buffer, disptab);
15474 /* If window-start is screwed up, choose a new one. */
15475 if (XMARKER (w->start)->buffer != current_buffer)
15476 goto recenter;
15478 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15480 /* If someone specified a new starting point but did not insist,
15481 check whether it can be used. */
15482 if (w->optional_new_start
15483 && CHARPOS (startp) >= BEGV
15484 && CHARPOS (startp) <= ZV)
15486 w->optional_new_start = 0;
15487 start_display (&it, w, startp);
15488 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15489 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15490 if (IT_CHARPOS (it) == PT)
15491 w->force_start = 1;
15492 /* IT may overshoot PT if text at PT is invisible. */
15493 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15494 w->force_start = 1;
15497 force_start:
15499 /* Handle case where place to start displaying has been specified,
15500 unless the specified location is outside the accessible range. */
15501 if (w->force_start || w->frozen_window_start_p)
15503 /* We set this later on if we have to adjust point. */
15504 int new_vpos = -1;
15506 w->force_start = 0;
15507 w->vscroll = 0;
15508 w->window_end_valid = Qnil;
15510 /* Forget any recorded base line for line number display. */
15511 if (!buffer_unchanged_p)
15512 w->base_line_number = Qnil;
15514 /* Redisplay the mode line. Select the buffer properly for that.
15515 Also, run the hook window-scroll-functions
15516 because we have scrolled. */
15517 /* Note, we do this after clearing force_start because
15518 if there's an error, it is better to forget about force_start
15519 than to get into an infinite loop calling the hook functions
15520 and having them get more errors. */
15521 if (!update_mode_line
15522 || ! NILP (Vwindow_scroll_functions))
15524 update_mode_line = 1;
15525 w->update_mode_line = 1;
15526 startp = run_window_scroll_functions (window, startp);
15529 w->last_modified = make_number (0);
15530 w->last_overlay_modified = make_number (0);
15531 if (CHARPOS (startp) < BEGV)
15532 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15533 else if (CHARPOS (startp) > ZV)
15534 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15536 /* Redisplay, then check if cursor has been set during the
15537 redisplay. Give up if new fonts were loaded. */
15538 /* We used to issue a CHECK_MARGINS argument to try_window here,
15539 but this causes scrolling to fail when point begins inside
15540 the scroll margin (bug#148) -- cyd */
15541 if (!try_window (window, startp, 0))
15543 w->force_start = 1;
15544 clear_glyph_matrix (w->desired_matrix);
15545 goto need_larger_matrices;
15548 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15550 /* If point does not appear, try to move point so it does
15551 appear. The desired matrix has been built above, so we
15552 can use it here. */
15553 new_vpos = window_box_height (w) / 2;
15556 if (!cursor_row_fully_visible_p (w, 0, 0))
15558 /* Point does appear, but on a line partly visible at end of window.
15559 Move it back to a fully-visible line. */
15560 new_vpos = window_box_height (w);
15563 /* If we need to move point for either of the above reasons,
15564 now actually do it. */
15565 if (new_vpos >= 0)
15567 struct glyph_row *row;
15569 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15570 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15571 ++row;
15573 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15574 MATRIX_ROW_START_BYTEPOS (row));
15576 if (w != XWINDOW (selected_window))
15577 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15578 else if (current_buffer == old)
15579 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15581 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15583 /* If we are highlighting the region, then we just changed
15584 the region, so redisplay to show it. */
15585 if (!NILP (Vtransient_mark_mode)
15586 && !NILP (BVAR (current_buffer, mark_active)))
15588 clear_glyph_matrix (w->desired_matrix);
15589 if (!try_window (window, startp, 0))
15590 goto need_larger_matrices;
15594 #if GLYPH_DEBUG
15595 debug_method_add (w, "forced window start");
15596 #endif
15597 goto done;
15600 /* Handle case where text has not changed, only point, and it has
15601 not moved off the frame, and we are not retrying after hscroll.
15602 (current_matrix_up_to_date_p is nonzero when retrying.) */
15603 if (current_matrix_up_to_date_p
15604 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15605 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15607 switch (rc)
15609 case CURSOR_MOVEMENT_SUCCESS:
15610 used_current_matrix_p = 1;
15611 goto done;
15613 case CURSOR_MOVEMENT_MUST_SCROLL:
15614 goto try_to_scroll;
15616 default:
15617 abort ();
15620 /* If current starting point was originally the beginning of a line
15621 but no longer is, find a new starting point. */
15622 else if (w->start_at_line_beg
15623 && !(CHARPOS (startp) <= BEGV
15624 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15626 #if GLYPH_DEBUG
15627 debug_method_add (w, "recenter 1");
15628 #endif
15629 goto recenter;
15632 /* Try scrolling with try_window_id. Value is > 0 if update has
15633 been done, it is -1 if we know that the same window start will
15634 not work. It is 0 if unsuccessful for some other reason. */
15635 else if ((tem = try_window_id (w)) != 0)
15637 #if GLYPH_DEBUG
15638 debug_method_add (w, "try_window_id %d", tem);
15639 #endif
15641 if (fonts_changed_p)
15642 goto need_larger_matrices;
15643 if (tem > 0)
15644 goto done;
15646 /* Otherwise try_window_id has returned -1 which means that we
15647 don't want the alternative below this comment to execute. */
15649 else if (CHARPOS (startp) >= BEGV
15650 && CHARPOS (startp) <= ZV
15651 && PT >= CHARPOS (startp)
15652 && (CHARPOS (startp) < ZV
15653 /* Avoid starting at end of buffer. */
15654 || CHARPOS (startp) == BEGV
15655 || (XFASTINT (w->last_modified) >= MODIFF
15656 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15658 int d1, d2, d3, d4, d5, d6;
15660 /* If first window line is a continuation line, and window start
15661 is inside the modified region, but the first change is before
15662 current window start, we must select a new window start.
15664 However, if this is the result of a down-mouse event (e.g. by
15665 extending the mouse-drag-overlay), we don't want to select a
15666 new window start, since that would change the position under
15667 the mouse, resulting in an unwanted mouse-movement rather
15668 than a simple mouse-click. */
15669 if (!w->start_at_line_beg
15670 && NILP (do_mouse_tracking)
15671 && CHARPOS (startp) > BEGV
15672 && CHARPOS (startp) > BEG + beg_unchanged
15673 && CHARPOS (startp) <= Z - end_unchanged
15674 /* Even if w->start_at_line_beg is nil, a new window may
15675 start at a line_beg, since that's how set_buffer_window
15676 sets it. So, we need to check the return value of
15677 compute_window_start_on_continuation_line. (See also
15678 bug#197). */
15679 && XMARKER (w->start)->buffer == current_buffer
15680 && compute_window_start_on_continuation_line (w)
15681 /* It doesn't make sense to force the window start like we
15682 do at label force_start if it is already known that point
15683 will not be visible in the resulting window, because
15684 doing so will move point from its correct position
15685 instead of scrolling the window to bring point into view.
15686 See bug#9324. */
15687 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15689 w->force_start = 1;
15690 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15691 goto force_start;
15694 #if GLYPH_DEBUG
15695 debug_method_add (w, "same window start");
15696 #endif
15698 /* Try to redisplay starting at same place as before.
15699 If point has not moved off frame, accept the results. */
15700 if (!current_matrix_up_to_date_p
15701 /* Don't use try_window_reusing_current_matrix in this case
15702 because a window scroll function can have changed the
15703 buffer. */
15704 || !NILP (Vwindow_scroll_functions)
15705 || MINI_WINDOW_P (w)
15706 || !(used_current_matrix_p
15707 = try_window_reusing_current_matrix (w)))
15709 IF_DEBUG (debug_method_add (w, "1"));
15710 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15711 /* -1 means we need to scroll.
15712 0 means we need new matrices, but fonts_changed_p
15713 is set in that case, so we will detect it below. */
15714 goto try_to_scroll;
15717 if (fonts_changed_p)
15718 goto need_larger_matrices;
15720 if (w->cursor.vpos >= 0)
15722 if (!just_this_one_p
15723 || current_buffer->clip_changed
15724 || BEG_UNCHANGED < CHARPOS (startp))
15725 /* Forget any recorded base line for line number display. */
15726 w->base_line_number = Qnil;
15728 if (!cursor_row_fully_visible_p (w, 1, 0))
15730 clear_glyph_matrix (w->desired_matrix);
15731 last_line_misfit = 1;
15733 /* Drop through and scroll. */
15734 else
15735 goto done;
15737 else
15738 clear_glyph_matrix (w->desired_matrix);
15741 try_to_scroll:
15743 w->last_modified = make_number (0);
15744 w->last_overlay_modified = make_number (0);
15746 /* Redisplay the mode line. Select the buffer properly for that. */
15747 if (!update_mode_line)
15749 update_mode_line = 1;
15750 w->update_mode_line = 1;
15753 /* Try to scroll by specified few lines. */
15754 if ((scroll_conservatively
15755 || emacs_scroll_step
15756 || temp_scroll_step
15757 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15758 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15759 && CHARPOS (startp) >= BEGV
15760 && CHARPOS (startp) <= ZV)
15762 /* The function returns -1 if new fonts were loaded, 1 if
15763 successful, 0 if not successful. */
15764 int ss = try_scrolling (window, just_this_one_p,
15765 scroll_conservatively,
15766 emacs_scroll_step,
15767 temp_scroll_step, last_line_misfit);
15768 switch (ss)
15770 case SCROLLING_SUCCESS:
15771 goto done;
15773 case SCROLLING_NEED_LARGER_MATRICES:
15774 goto need_larger_matrices;
15776 case SCROLLING_FAILED:
15777 break;
15779 default:
15780 abort ();
15784 /* Finally, just choose a place to start which positions point
15785 according to user preferences. */
15787 recenter:
15789 #if GLYPH_DEBUG
15790 debug_method_add (w, "recenter");
15791 #endif
15793 /* w->vscroll = 0; */
15795 /* Forget any previously recorded base line for line number display. */
15796 if (!buffer_unchanged_p)
15797 w->base_line_number = Qnil;
15799 /* Determine the window start relative to point. */
15800 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15801 it.current_y = it.last_visible_y;
15802 if (centering_position < 0)
15804 int margin =
15805 scroll_margin > 0
15806 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15807 : 0;
15808 ptrdiff_t margin_pos = CHARPOS (startp);
15809 Lisp_Object aggressive;
15810 int scrolling_up;
15812 /* If there is a scroll margin at the top of the window, find
15813 its character position. */
15814 if (margin
15815 /* Cannot call start_display if startp is not in the
15816 accessible region of the buffer. This can happen when we
15817 have just switched to a different buffer and/or changed
15818 its restriction. In that case, startp is initialized to
15819 the character position 1 (BEGV) because we did not yet
15820 have chance to display the buffer even once. */
15821 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15823 struct it it1;
15824 void *it1data = NULL;
15826 SAVE_IT (it1, it, it1data);
15827 start_display (&it1, w, startp);
15828 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15829 margin_pos = IT_CHARPOS (it1);
15830 RESTORE_IT (&it, &it, it1data);
15832 scrolling_up = PT > margin_pos;
15833 aggressive =
15834 scrolling_up
15835 ? BVAR (current_buffer, scroll_up_aggressively)
15836 : BVAR (current_buffer, scroll_down_aggressively);
15838 if (!MINI_WINDOW_P (w)
15839 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15841 int pt_offset = 0;
15843 /* Setting scroll-conservatively overrides
15844 scroll-*-aggressively. */
15845 if (!scroll_conservatively && NUMBERP (aggressive))
15847 double float_amount = XFLOATINT (aggressive);
15849 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15850 if (pt_offset == 0 && float_amount > 0)
15851 pt_offset = 1;
15852 if (pt_offset && margin > 0)
15853 margin -= 1;
15855 /* Compute how much to move the window start backward from
15856 point so that point will be displayed where the user
15857 wants it. */
15858 if (scrolling_up)
15860 centering_position = it.last_visible_y;
15861 if (pt_offset)
15862 centering_position -= pt_offset;
15863 centering_position -=
15864 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15865 + WINDOW_HEADER_LINE_HEIGHT (w);
15866 /* Don't let point enter the scroll margin near top of
15867 the window. */
15868 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15869 centering_position = margin * FRAME_LINE_HEIGHT (f);
15871 else
15872 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15874 else
15875 /* Set the window start half the height of the window backward
15876 from point. */
15877 centering_position = window_box_height (w) / 2;
15879 move_it_vertically_backward (&it, centering_position);
15881 xassert (IT_CHARPOS (it) >= BEGV);
15883 /* The function move_it_vertically_backward may move over more
15884 than the specified y-distance. If it->w is small, e.g. a
15885 mini-buffer window, we may end up in front of the window's
15886 display area. Start displaying at the start of the line
15887 containing PT in this case. */
15888 if (it.current_y <= 0)
15890 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15891 move_it_vertically_backward (&it, 0);
15892 it.current_y = 0;
15895 it.current_x = it.hpos = 0;
15897 /* Set the window start position here explicitly, to avoid an
15898 infinite loop in case the functions in window-scroll-functions
15899 get errors. */
15900 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15902 /* Run scroll hooks. */
15903 startp = run_window_scroll_functions (window, it.current.pos);
15905 /* Redisplay the window. */
15906 if (!current_matrix_up_to_date_p
15907 || windows_or_buffers_changed
15908 || cursor_type_changed
15909 /* Don't use try_window_reusing_current_matrix in this case
15910 because it can have changed the buffer. */
15911 || !NILP (Vwindow_scroll_functions)
15912 || !just_this_one_p
15913 || MINI_WINDOW_P (w)
15914 || !(used_current_matrix_p
15915 = try_window_reusing_current_matrix (w)))
15916 try_window (window, startp, 0);
15918 /* If new fonts have been loaded (due to fontsets), give up. We
15919 have to start a new redisplay since we need to re-adjust glyph
15920 matrices. */
15921 if (fonts_changed_p)
15922 goto need_larger_matrices;
15924 /* If cursor did not appear assume that the middle of the window is
15925 in the first line of the window. Do it again with the next line.
15926 (Imagine a window of height 100, displaying two lines of height
15927 60. Moving back 50 from it->last_visible_y will end in the first
15928 line.) */
15929 if (w->cursor.vpos < 0)
15931 if (!NILP (w->window_end_valid)
15932 && PT >= Z - XFASTINT (w->window_end_pos))
15934 clear_glyph_matrix (w->desired_matrix);
15935 move_it_by_lines (&it, 1);
15936 try_window (window, it.current.pos, 0);
15938 else if (PT < IT_CHARPOS (it))
15940 clear_glyph_matrix (w->desired_matrix);
15941 move_it_by_lines (&it, -1);
15942 try_window (window, it.current.pos, 0);
15944 else
15946 /* Not much we can do about it. */
15950 /* Consider the following case: Window starts at BEGV, there is
15951 invisible, intangible text at BEGV, so that display starts at
15952 some point START > BEGV. It can happen that we are called with
15953 PT somewhere between BEGV and START. Try to handle that case. */
15954 if (w->cursor.vpos < 0)
15956 struct glyph_row *row = w->current_matrix->rows;
15957 if (row->mode_line_p)
15958 ++row;
15959 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15962 if (!cursor_row_fully_visible_p (w, 0, 0))
15964 /* If vscroll is enabled, disable it and try again. */
15965 if (w->vscroll)
15967 w->vscroll = 0;
15968 clear_glyph_matrix (w->desired_matrix);
15969 goto recenter;
15972 /* Users who set scroll-conservatively to a large number want
15973 point just above/below the scroll margin. If we ended up
15974 with point's row partially visible, move the window start to
15975 make that row fully visible and out of the margin. */
15976 if (scroll_conservatively > SCROLL_LIMIT)
15978 int margin =
15979 scroll_margin > 0
15980 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15981 : 0;
15982 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15984 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15985 clear_glyph_matrix (w->desired_matrix);
15986 if (1 == try_window (window, it.current.pos,
15987 TRY_WINDOW_CHECK_MARGINS))
15988 goto done;
15991 /* If centering point failed to make the whole line visible,
15992 put point at the top instead. That has to make the whole line
15993 visible, if it can be done. */
15994 if (centering_position == 0)
15995 goto done;
15997 clear_glyph_matrix (w->desired_matrix);
15998 centering_position = 0;
15999 goto recenter;
16002 done:
16004 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16005 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16006 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16008 /* Display the mode line, if we must. */
16009 if ((update_mode_line
16010 /* If window not full width, must redo its mode line
16011 if (a) the window to its side is being redone and
16012 (b) we do a frame-based redisplay. This is a consequence
16013 of how inverted lines are drawn in frame-based redisplay. */
16014 || (!just_this_one_p
16015 && !FRAME_WINDOW_P (f)
16016 && !WINDOW_FULL_WIDTH_P (w))
16017 /* Line number to display. */
16018 || INTEGERP (w->base_line_pos)
16019 /* Column number is displayed and different from the one displayed. */
16020 || (!NILP (w->column_number_displayed)
16021 && (XFASTINT (w->column_number_displayed) != current_column ())))
16022 /* This means that the window has a mode line. */
16023 && (WINDOW_WANTS_MODELINE_P (w)
16024 || WINDOW_WANTS_HEADER_LINE_P (w)))
16026 display_mode_lines (w);
16028 /* If mode line height has changed, arrange for a thorough
16029 immediate redisplay using the correct mode line height. */
16030 if (WINDOW_WANTS_MODELINE_P (w)
16031 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16033 fonts_changed_p = 1;
16034 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16035 = DESIRED_MODE_LINE_HEIGHT (w);
16038 /* If header line height has changed, arrange for a thorough
16039 immediate redisplay using the correct header line height. */
16040 if (WINDOW_WANTS_HEADER_LINE_P (w)
16041 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16043 fonts_changed_p = 1;
16044 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16045 = DESIRED_HEADER_LINE_HEIGHT (w);
16048 if (fonts_changed_p)
16049 goto need_larger_matrices;
16052 if (!line_number_displayed
16053 && !BUFFERP (w->base_line_pos))
16055 w->base_line_pos = Qnil;
16056 w->base_line_number = Qnil;
16059 finish_menu_bars:
16061 /* When we reach a frame's selected window, redo the frame's menu bar. */
16062 if (update_mode_line
16063 && EQ (FRAME_SELECTED_WINDOW (f), window))
16065 int redisplay_menu_p = 0;
16067 if (FRAME_WINDOW_P (f))
16069 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16070 || defined (HAVE_NS) || defined (USE_GTK)
16071 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16072 #else
16073 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16074 #endif
16076 else
16077 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16079 if (redisplay_menu_p)
16080 display_menu_bar (w);
16082 #ifdef HAVE_WINDOW_SYSTEM
16083 if (FRAME_WINDOW_P (f))
16085 #if defined (USE_GTK) || defined (HAVE_NS)
16086 if (FRAME_EXTERNAL_TOOL_BAR (f))
16087 redisplay_tool_bar (f);
16088 #else
16089 if (WINDOWP (f->tool_bar_window)
16090 && (FRAME_TOOL_BAR_LINES (f) > 0
16091 || !NILP (Vauto_resize_tool_bars))
16092 && redisplay_tool_bar (f))
16093 ignore_mouse_drag_p = 1;
16094 #endif
16096 #endif
16099 #ifdef HAVE_WINDOW_SYSTEM
16100 if (FRAME_WINDOW_P (f)
16101 && update_window_fringes (w, (just_this_one_p
16102 || (!used_current_matrix_p && !overlay_arrow_seen)
16103 || w->pseudo_window_p)))
16105 update_begin (f);
16106 BLOCK_INPUT;
16107 if (draw_window_fringes (w, 1))
16108 x_draw_vertical_border (w);
16109 UNBLOCK_INPUT;
16110 update_end (f);
16112 #endif /* HAVE_WINDOW_SYSTEM */
16114 /* We go to this label, with fonts_changed_p nonzero,
16115 if it is necessary to try again using larger glyph matrices.
16116 We have to redeem the scroll bar even in this case,
16117 because the loop in redisplay_internal expects that. */
16118 need_larger_matrices:
16120 finish_scroll_bars:
16122 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16124 /* Set the thumb's position and size. */
16125 set_vertical_scroll_bar (w);
16127 /* Note that we actually used the scroll bar attached to this
16128 window, so it shouldn't be deleted at the end of redisplay. */
16129 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16130 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16133 /* Restore current_buffer and value of point in it. The window
16134 update may have changed the buffer, so first make sure `opoint'
16135 is still valid (Bug#6177). */
16136 if (CHARPOS (opoint) < BEGV)
16137 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16138 else if (CHARPOS (opoint) > ZV)
16139 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16140 else
16141 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16143 set_buffer_internal_1 (old);
16144 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16145 shorter. This can be caused by log truncation in *Messages*. */
16146 if (CHARPOS (lpoint) <= ZV)
16147 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16149 unbind_to (count, Qnil);
16153 /* Build the complete desired matrix of WINDOW with a window start
16154 buffer position POS.
16156 Value is 1 if successful. It is zero if fonts were loaded during
16157 redisplay which makes re-adjusting glyph matrices necessary, and -1
16158 if point would appear in the scroll margins.
16159 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16160 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16161 set in FLAGS.) */
16164 try_window (Lisp_Object window, struct text_pos pos, int flags)
16166 struct window *w = XWINDOW (window);
16167 struct it it;
16168 struct glyph_row *last_text_row = NULL;
16169 struct frame *f = XFRAME (w->frame);
16171 /* Make POS the new window start. */
16172 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16174 /* Mark cursor position as unknown. No overlay arrow seen. */
16175 w->cursor.vpos = -1;
16176 overlay_arrow_seen = 0;
16178 /* Initialize iterator and info to start at POS. */
16179 start_display (&it, w, pos);
16181 /* Display all lines of W. */
16182 while (it.current_y < it.last_visible_y)
16184 if (display_line (&it))
16185 last_text_row = it.glyph_row - 1;
16186 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16187 return 0;
16190 /* Don't let the cursor end in the scroll margins. */
16191 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16192 && !MINI_WINDOW_P (w))
16194 int this_scroll_margin;
16196 if (scroll_margin > 0)
16198 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16199 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16201 else
16202 this_scroll_margin = 0;
16204 if ((w->cursor.y >= 0 /* not vscrolled */
16205 && w->cursor.y < this_scroll_margin
16206 && CHARPOS (pos) > BEGV
16207 && IT_CHARPOS (it) < ZV)
16208 /* rms: considering make_cursor_line_fully_visible_p here
16209 seems to give wrong results. We don't want to recenter
16210 when the last line is partly visible, we want to allow
16211 that case to be handled in the usual way. */
16212 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16214 w->cursor.vpos = -1;
16215 clear_glyph_matrix (w->desired_matrix);
16216 return -1;
16220 /* If bottom moved off end of frame, change mode line percentage. */
16221 if (XFASTINT (w->window_end_pos) <= 0
16222 && Z != IT_CHARPOS (it))
16223 w->update_mode_line = 1;
16225 /* Set window_end_pos to the offset of the last character displayed
16226 on the window from the end of current_buffer. Set
16227 window_end_vpos to its row number. */
16228 if (last_text_row)
16230 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16231 w->window_end_bytepos
16232 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16233 w->window_end_pos
16234 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16235 w->window_end_vpos
16236 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16237 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16238 ->displays_text_p);
16240 else
16242 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16243 w->window_end_pos = make_number (Z - ZV);
16244 w->window_end_vpos = make_number (0);
16247 /* But that is not valid info until redisplay finishes. */
16248 w->window_end_valid = Qnil;
16249 return 1;
16254 /************************************************************************
16255 Window redisplay reusing current matrix when buffer has not changed
16256 ************************************************************************/
16258 /* Try redisplay of window W showing an unchanged buffer with a
16259 different window start than the last time it was displayed by
16260 reusing its current matrix. Value is non-zero if successful.
16261 W->start is the new window start. */
16263 static int
16264 try_window_reusing_current_matrix (struct window *w)
16266 struct frame *f = XFRAME (w->frame);
16267 struct glyph_row *bottom_row;
16268 struct it it;
16269 struct run run;
16270 struct text_pos start, new_start;
16271 int nrows_scrolled, i;
16272 struct glyph_row *last_text_row;
16273 struct glyph_row *last_reused_text_row;
16274 struct glyph_row *start_row;
16275 int start_vpos, min_y, max_y;
16277 #if GLYPH_DEBUG
16278 if (inhibit_try_window_reusing)
16279 return 0;
16280 #endif
16282 if (/* This function doesn't handle terminal frames. */
16283 !FRAME_WINDOW_P (f)
16284 /* Don't try to reuse the display if windows have been split
16285 or such. */
16286 || windows_or_buffers_changed
16287 || cursor_type_changed)
16288 return 0;
16290 /* Can't do this if region may have changed. */
16291 if ((!NILP (Vtransient_mark_mode)
16292 && !NILP (BVAR (current_buffer, mark_active)))
16293 || !NILP (w->region_showing)
16294 || !NILP (Vshow_trailing_whitespace))
16295 return 0;
16297 /* If top-line visibility has changed, give up. */
16298 if (WINDOW_WANTS_HEADER_LINE_P (w)
16299 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16300 return 0;
16302 /* Give up if old or new display is scrolled vertically. We could
16303 make this function handle this, but right now it doesn't. */
16304 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16305 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16306 return 0;
16308 /* The variable new_start now holds the new window start. The old
16309 start `start' can be determined from the current matrix. */
16310 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16311 start = start_row->minpos;
16312 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16314 /* Clear the desired matrix for the display below. */
16315 clear_glyph_matrix (w->desired_matrix);
16317 if (CHARPOS (new_start) <= CHARPOS (start))
16319 /* Don't use this method if the display starts with an ellipsis
16320 displayed for invisible text. It's not easy to handle that case
16321 below, and it's certainly not worth the effort since this is
16322 not a frequent case. */
16323 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16324 return 0;
16326 IF_DEBUG (debug_method_add (w, "twu1"));
16328 /* Display up to a row that can be reused. The variable
16329 last_text_row is set to the last row displayed that displays
16330 text. Note that it.vpos == 0 if or if not there is a
16331 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16332 start_display (&it, w, new_start);
16333 w->cursor.vpos = -1;
16334 last_text_row = last_reused_text_row = NULL;
16336 while (it.current_y < it.last_visible_y
16337 && !fonts_changed_p)
16339 /* If we have reached into the characters in the START row,
16340 that means the line boundaries have changed. So we
16341 can't start copying with the row START. Maybe it will
16342 work to start copying with the following row. */
16343 while (IT_CHARPOS (it) > CHARPOS (start))
16345 /* Advance to the next row as the "start". */
16346 start_row++;
16347 start = start_row->minpos;
16348 /* If there are no more rows to try, or just one, give up. */
16349 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16350 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16351 || CHARPOS (start) == ZV)
16353 clear_glyph_matrix (w->desired_matrix);
16354 return 0;
16357 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16359 /* If we have reached alignment, we can copy the rest of the
16360 rows. */
16361 if (IT_CHARPOS (it) == CHARPOS (start)
16362 /* Don't accept "alignment" inside a display vector,
16363 since start_row could have started in the middle of
16364 that same display vector (thus their character
16365 positions match), and we have no way of telling if
16366 that is the case. */
16367 && it.current.dpvec_index < 0)
16368 break;
16370 if (display_line (&it))
16371 last_text_row = it.glyph_row - 1;
16375 /* A value of current_y < last_visible_y means that we stopped
16376 at the previous window start, which in turn means that we
16377 have at least one reusable row. */
16378 if (it.current_y < it.last_visible_y)
16380 struct glyph_row *row;
16382 /* IT.vpos always starts from 0; it counts text lines. */
16383 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16385 /* Find PT if not already found in the lines displayed. */
16386 if (w->cursor.vpos < 0)
16388 int dy = it.current_y - start_row->y;
16390 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16391 row = row_containing_pos (w, PT, row, NULL, dy);
16392 if (row)
16393 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16394 dy, nrows_scrolled);
16395 else
16397 clear_glyph_matrix (w->desired_matrix);
16398 return 0;
16402 /* Scroll the display. Do it before the current matrix is
16403 changed. The problem here is that update has not yet
16404 run, i.e. part of the current matrix is not up to date.
16405 scroll_run_hook will clear the cursor, and use the
16406 current matrix to get the height of the row the cursor is
16407 in. */
16408 run.current_y = start_row->y;
16409 run.desired_y = it.current_y;
16410 run.height = it.last_visible_y - it.current_y;
16412 if (run.height > 0 && run.current_y != run.desired_y)
16414 update_begin (f);
16415 FRAME_RIF (f)->update_window_begin_hook (w);
16416 FRAME_RIF (f)->clear_window_mouse_face (w);
16417 FRAME_RIF (f)->scroll_run_hook (w, &run);
16418 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16419 update_end (f);
16422 /* Shift current matrix down by nrows_scrolled lines. */
16423 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16424 rotate_matrix (w->current_matrix,
16425 start_vpos,
16426 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16427 nrows_scrolled);
16429 /* Disable lines that must be updated. */
16430 for (i = 0; i < nrows_scrolled; ++i)
16431 (start_row + i)->enabled_p = 0;
16433 /* Re-compute Y positions. */
16434 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16435 max_y = it.last_visible_y;
16436 for (row = start_row + nrows_scrolled;
16437 row < bottom_row;
16438 ++row)
16440 row->y = it.current_y;
16441 row->visible_height = row->height;
16443 if (row->y < min_y)
16444 row->visible_height -= min_y - row->y;
16445 if (row->y + row->height > max_y)
16446 row->visible_height -= row->y + row->height - max_y;
16447 if (row->fringe_bitmap_periodic_p)
16448 row->redraw_fringe_bitmaps_p = 1;
16450 it.current_y += row->height;
16452 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16453 last_reused_text_row = row;
16454 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16455 break;
16458 /* Disable lines in the current matrix which are now
16459 below the window. */
16460 for (++row; row < bottom_row; ++row)
16461 row->enabled_p = row->mode_line_p = 0;
16464 /* Update window_end_pos etc.; last_reused_text_row is the last
16465 reused row from the current matrix containing text, if any.
16466 The value of last_text_row is the last displayed line
16467 containing text. */
16468 if (last_reused_text_row)
16470 w->window_end_bytepos
16471 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16472 w->window_end_pos
16473 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16474 w->window_end_vpos
16475 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16476 w->current_matrix));
16478 else if (last_text_row)
16480 w->window_end_bytepos
16481 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16482 w->window_end_pos
16483 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16484 w->window_end_vpos
16485 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16487 else
16489 /* This window must be completely empty. */
16490 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16491 w->window_end_pos = make_number (Z - ZV);
16492 w->window_end_vpos = make_number (0);
16494 w->window_end_valid = Qnil;
16496 /* Update hint: don't try scrolling again in update_window. */
16497 w->desired_matrix->no_scrolling_p = 1;
16499 #if GLYPH_DEBUG
16500 debug_method_add (w, "try_window_reusing_current_matrix 1");
16501 #endif
16502 return 1;
16504 else if (CHARPOS (new_start) > CHARPOS (start))
16506 struct glyph_row *pt_row, *row;
16507 struct glyph_row *first_reusable_row;
16508 struct glyph_row *first_row_to_display;
16509 int dy;
16510 int yb = window_text_bottom_y (w);
16512 /* Find the row starting at new_start, if there is one. Don't
16513 reuse a partially visible line at the end. */
16514 first_reusable_row = start_row;
16515 while (first_reusable_row->enabled_p
16516 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16517 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16518 < CHARPOS (new_start)))
16519 ++first_reusable_row;
16521 /* Give up if there is no row to reuse. */
16522 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16523 || !first_reusable_row->enabled_p
16524 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16525 != CHARPOS (new_start)))
16526 return 0;
16528 /* We can reuse fully visible rows beginning with
16529 first_reusable_row to the end of the window. Set
16530 first_row_to_display to the first row that cannot be reused.
16531 Set pt_row to the row containing point, if there is any. */
16532 pt_row = NULL;
16533 for (first_row_to_display = first_reusable_row;
16534 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16535 ++first_row_to_display)
16537 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16538 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16539 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16540 && first_row_to_display->ends_at_zv_p
16541 && pt_row == NULL)))
16542 pt_row = first_row_to_display;
16545 /* Start displaying at the start of first_row_to_display. */
16546 xassert (first_row_to_display->y < yb);
16547 init_to_row_start (&it, w, first_row_to_display);
16549 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16550 - start_vpos);
16551 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16552 - nrows_scrolled);
16553 it.current_y = (first_row_to_display->y - first_reusable_row->y
16554 + WINDOW_HEADER_LINE_HEIGHT (w));
16556 /* Display lines beginning with first_row_to_display in the
16557 desired matrix. Set last_text_row to the last row displayed
16558 that displays text. */
16559 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16560 if (pt_row == NULL)
16561 w->cursor.vpos = -1;
16562 last_text_row = NULL;
16563 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16564 if (display_line (&it))
16565 last_text_row = it.glyph_row - 1;
16567 /* If point is in a reused row, adjust y and vpos of the cursor
16568 position. */
16569 if (pt_row)
16571 w->cursor.vpos -= nrows_scrolled;
16572 w->cursor.y -= first_reusable_row->y - start_row->y;
16575 /* Give up if point isn't in a row displayed or reused. (This
16576 also handles the case where w->cursor.vpos < nrows_scrolled
16577 after the calls to display_line, which can happen with scroll
16578 margins. See bug#1295.) */
16579 if (w->cursor.vpos < 0)
16581 clear_glyph_matrix (w->desired_matrix);
16582 return 0;
16585 /* Scroll the display. */
16586 run.current_y = first_reusable_row->y;
16587 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16588 run.height = it.last_visible_y - run.current_y;
16589 dy = run.current_y - run.desired_y;
16591 if (run.height)
16593 update_begin (f);
16594 FRAME_RIF (f)->update_window_begin_hook (w);
16595 FRAME_RIF (f)->clear_window_mouse_face (w);
16596 FRAME_RIF (f)->scroll_run_hook (w, &run);
16597 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16598 update_end (f);
16601 /* Adjust Y positions of reused rows. */
16602 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16603 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16604 max_y = it.last_visible_y;
16605 for (row = first_reusable_row; row < first_row_to_display; ++row)
16607 row->y -= dy;
16608 row->visible_height = row->height;
16609 if (row->y < min_y)
16610 row->visible_height -= min_y - row->y;
16611 if (row->y + row->height > max_y)
16612 row->visible_height -= row->y + row->height - max_y;
16613 if (row->fringe_bitmap_periodic_p)
16614 row->redraw_fringe_bitmaps_p = 1;
16617 /* Scroll the current matrix. */
16618 xassert (nrows_scrolled > 0);
16619 rotate_matrix (w->current_matrix,
16620 start_vpos,
16621 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16622 -nrows_scrolled);
16624 /* Disable rows not reused. */
16625 for (row -= nrows_scrolled; row < bottom_row; ++row)
16626 row->enabled_p = 0;
16628 /* Point may have moved to a different line, so we cannot assume that
16629 the previous cursor position is valid; locate the correct row. */
16630 if (pt_row)
16632 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16633 row < bottom_row
16634 && PT >= MATRIX_ROW_END_CHARPOS (row)
16635 && !row->ends_at_zv_p;
16636 row++)
16638 w->cursor.vpos++;
16639 w->cursor.y = row->y;
16641 if (row < bottom_row)
16643 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16644 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16646 /* Can't use this optimization with bidi-reordered glyph
16647 rows, unless cursor is already at point. */
16648 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16650 if (!(w->cursor.hpos >= 0
16651 && w->cursor.hpos < row->used[TEXT_AREA]
16652 && BUFFERP (glyph->object)
16653 && glyph->charpos == PT))
16654 return 0;
16656 else
16657 for (; glyph < end
16658 && (!BUFFERP (glyph->object)
16659 || glyph->charpos < PT);
16660 glyph++)
16662 w->cursor.hpos++;
16663 w->cursor.x += glyph->pixel_width;
16668 /* Adjust window end. A null value of last_text_row means that
16669 the window end is in reused rows which in turn means that
16670 only its vpos can have changed. */
16671 if (last_text_row)
16673 w->window_end_bytepos
16674 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16675 w->window_end_pos
16676 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16677 w->window_end_vpos
16678 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16680 else
16682 w->window_end_vpos
16683 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16686 w->window_end_valid = Qnil;
16687 w->desired_matrix->no_scrolling_p = 1;
16689 #if GLYPH_DEBUG
16690 debug_method_add (w, "try_window_reusing_current_matrix 2");
16691 #endif
16692 return 1;
16695 return 0;
16700 /************************************************************************
16701 Window redisplay reusing current matrix when buffer has changed
16702 ************************************************************************/
16704 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16705 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16706 ptrdiff_t *, ptrdiff_t *);
16707 static struct glyph_row *
16708 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16709 struct glyph_row *);
16712 /* Return the last row in MATRIX displaying text. If row START is
16713 non-null, start searching with that row. IT gives the dimensions
16714 of the display. Value is null if matrix is empty; otherwise it is
16715 a pointer to the row found. */
16717 static struct glyph_row *
16718 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16719 struct glyph_row *start)
16721 struct glyph_row *row, *row_found;
16723 /* Set row_found to the last row in IT->w's current matrix
16724 displaying text. The loop looks funny but think of partially
16725 visible lines. */
16726 row_found = NULL;
16727 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16728 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16730 xassert (row->enabled_p);
16731 row_found = row;
16732 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16733 break;
16734 ++row;
16737 return row_found;
16741 /* Return the last row in the current matrix of W that is not affected
16742 by changes at the start of current_buffer that occurred since W's
16743 current matrix was built. Value is null if no such row exists.
16745 BEG_UNCHANGED us the number of characters unchanged at the start of
16746 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16747 first changed character in current_buffer. Characters at positions <
16748 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16749 when the current matrix was built. */
16751 static struct glyph_row *
16752 find_last_unchanged_at_beg_row (struct window *w)
16754 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16755 struct glyph_row *row;
16756 struct glyph_row *row_found = NULL;
16757 int yb = window_text_bottom_y (w);
16759 /* Find the last row displaying unchanged text. */
16760 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16761 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16762 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16763 ++row)
16765 if (/* If row ends before first_changed_pos, it is unchanged,
16766 except in some case. */
16767 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16768 /* When row ends in ZV and we write at ZV it is not
16769 unchanged. */
16770 && !row->ends_at_zv_p
16771 /* When first_changed_pos is the end of a continued line,
16772 row is not unchanged because it may be no longer
16773 continued. */
16774 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16775 && (row->continued_p
16776 || row->exact_window_width_line_p))
16777 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16778 needs to be recomputed, so don't consider this row as
16779 unchanged. This happens when the last line was
16780 bidi-reordered and was killed immediately before this
16781 redisplay cycle. In that case, ROW->end stores the
16782 buffer position of the first visual-order character of
16783 the killed text, which is now beyond ZV. */
16784 && CHARPOS (row->end.pos) <= ZV)
16785 row_found = row;
16787 /* Stop if last visible row. */
16788 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16789 break;
16792 return row_found;
16796 /* Find the first glyph row in the current matrix of W that is not
16797 affected by changes at the end of current_buffer since the
16798 time W's current matrix was built.
16800 Return in *DELTA the number of chars by which buffer positions in
16801 unchanged text at the end of current_buffer must be adjusted.
16803 Return in *DELTA_BYTES the corresponding number of bytes.
16805 Value is null if no such row exists, i.e. all rows are affected by
16806 changes. */
16808 static struct glyph_row *
16809 find_first_unchanged_at_end_row (struct window *w,
16810 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16812 struct glyph_row *row;
16813 struct glyph_row *row_found = NULL;
16815 *delta = *delta_bytes = 0;
16817 /* Display must not have been paused, otherwise the current matrix
16818 is not up to date. */
16819 eassert (!NILP (w->window_end_valid));
16821 /* A value of window_end_pos >= END_UNCHANGED means that the window
16822 end is in the range of changed text. If so, there is no
16823 unchanged row at the end of W's current matrix. */
16824 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16825 return NULL;
16827 /* Set row to the last row in W's current matrix displaying text. */
16828 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16830 /* If matrix is entirely empty, no unchanged row exists. */
16831 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16833 /* The value of row is the last glyph row in the matrix having a
16834 meaningful buffer position in it. The end position of row
16835 corresponds to window_end_pos. This allows us to translate
16836 buffer positions in the current matrix to current buffer
16837 positions for characters not in changed text. */
16838 ptrdiff_t Z_old =
16839 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16840 ptrdiff_t Z_BYTE_old =
16841 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16842 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16843 struct glyph_row *first_text_row
16844 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16846 *delta = Z - Z_old;
16847 *delta_bytes = Z_BYTE - Z_BYTE_old;
16849 /* Set last_unchanged_pos to the buffer position of the last
16850 character in the buffer that has not been changed. Z is the
16851 index + 1 of the last character in current_buffer, i.e. by
16852 subtracting END_UNCHANGED we get the index of the last
16853 unchanged character, and we have to add BEG to get its buffer
16854 position. */
16855 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16856 last_unchanged_pos_old = last_unchanged_pos - *delta;
16858 /* Search backward from ROW for a row displaying a line that
16859 starts at a minimum position >= last_unchanged_pos_old. */
16860 for (; row > first_text_row; --row)
16862 /* This used to abort, but it can happen.
16863 It is ok to just stop the search instead here. KFS. */
16864 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16865 break;
16867 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16868 row_found = row;
16872 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16874 return row_found;
16878 /* Make sure that glyph rows in the current matrix of window W
16879 reference the same glyph memory as corresponding rows in the
16880 frame's frame matrix. This function is called after scrolling W's
16881 current matrix on a terminal frame in try_window_id and
16882 try_window_reusing_current_matrix. */
16884 static void
16885 sync_frame_with_window_matrix_rows (struct window *w)
16887 struct frame *f = XFRAME (w->frame);
16888 struct glyph_row *window_row, *window_row_end, *frame_row;
16890 /* Preconditions: W must be a leaf window and full-width. Its frame
16891 must have a frame matrix. */
16892 xassert (NILP (w->hchild) && NILP (w->vchild));
16893 xassert (WINDOW_FULL_WIDTH_P (w));
16894 xassert (!FRAME_WINDOW_P (f));
16896 /* If W is a full-width window, glyph pointers in W's current matrix
16897 have, by definition, to be the same as glyph pointers in the
16898 corresponding frame matrix. Note that frame matrices have no
16899 marginal areas (see build_frame_matrix). */
16900 window_row = w->current_matrix->rows;
16901 window_row_end = window_row + w->current_matrix->nrows;
16902 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16903 while (window_row < window_row_end)
16905 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16906 struct glyph *end = window_row->glyphs[LAST_AREA];
16908 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16909 frame_row->glyphs[TEXT_AREA] = start;
16910 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16911 frame_row->glyphs[LAST_AREA] = end;
16913 /* Disable frame rows whose corresponding window rows have
16914 been disabled in try_window_id. */
16915 if (!window_row->enabled_p)
16916 frame_row->enabled_p = 0;
16918 ++window_row, ++frame_row;
16923 /* Find the glyph row in window W containing CHARPOS. Consider all
16924 rows between START and END (not inclusive). END null means search
16925 all rows to the end of the display area of W. Value is the row
16926 containing CHARPOS or null. */
16928 struct glyph_row *
16929 row_containing_pos (struct window *w, ptrdiff_t charpos,
16930 struct glyph_row *start, struct glyph_row *end, int dy)
16932 struct glyph_row *row = start;
16933 struct glyph_row *best_row = NULL;
16934 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16935 int last_y;
16937 /* If we happen to start on a header-line, skip that. */
16938 if (row->mode_line_p)
16939 ++row;
16941 if ((end && row >= end) || !row->enabled_p)
16942 return NULL;
16944 last_y = window_text_bottom_y (w) - dy;
16946 while (1)
16948 /* Give up if we have gone too far. */
16949 if (end && row >= end)
16950 return NULL;
16951 /* This formerly returned if they were equal.
16952 I think that both quantities are of a "last plus one" type;
16953 if so, when they are equal, the row is within the screen. -- rms. */
16954 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16955 return NULL;
16957 /* If it is in this row, return this row. */
16958 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16959 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16960 /* The end position of a row equals the start
16961 position of the next row. If CHARPOS is there, we
16962 would rather display it in the next line, except
16963 when this line ends in ZV. */
16964 && !row->ends_at_zv_p
16965 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16966 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16968 struct glyph *g;
16970 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16971 || (!best_row && !row->continued_p))
16972 return row;
16973 /* In bidi-reordered rows, there could be several rows
16974 occluding point, all of them belonging to the same
16975 continued line. We need to find the row which fits
16976 CHARPOS the best. */
16977 for (g = row->glyphs[TEXT_AREA];
16978 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16979 g++)
16981 if (!STRINGP (g->object))
16983 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16985 mindif = eabs (g->charpos - charpos);
16986 best_row = row;
16987 /* Exact match always wins. */
16988 if (mindif == 0)
16989 return best_row;
16994 else if (best_row && !row->continued_p)
16995 return best_row;
16996 ++row;
17001 /* Try to redisplay window W by reusing its existing display. W's
17002 current matrix must be up to date when this function is called,
17003 i.e. window_end_valid must not be nil.
17005 Value is
17007 1 if display has been updated
17008 0 if otherwise unsuccessful
17009 -1 if redisplay with same window start is known not to succeed
17011 The following steps are performed:
17013 1. Find the last row in the current matrix of W that is not
17014 affected by changes at the start of current_buffer. If no such row
17015 is found, give up.
17017 2. Find the first row in W's current matrix that is not affected by
17018 changes at the end of current_buffer. Maybe there is no such row.
17020 3. Display lines beginning with the row + 1 found in step 1 to the
17021 row found in step 2 or, if step 2 didn't find a row, to the end of
17022 the window.
17024 4. If cursor is not known to appear on the window, give up.
17026 5. If display stopped at the row found in step 2, scroll the
17027 display and current matrix as needed.
17029 6. Maybe display some lines at the end of W, if we must. This can
17030 happen under various circumstances, like a partially visible line
17031 becoming fully visible, or because newly displayed lines are displayed
17032 in smaller font sizes.
17034 7. Update W's window end information. */
17036 static int
17037 try_window_id (struct window *w)
17039 struct frame *f = XFRAME (w->frame);
17040 struct glyph_matrix *current_matrix = w->current_matrix;
17041 struct glyph_matrix *desired_matrix = w->desired_matrix;
17042 struct glyph_row *last_unchanged_at_beg_row;
17043 struct glyph_row *first_unchanged_at_end_row;
17044 struct glyph_row *row;
17045 struct glyph_row *bottom_row;
17046 int bottom_vpos;
17047 struct it it;
17048 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17049 int dvpos, dy;
17050 struct text_pos start_pos;
17051 struct run run;
17052 int first_unchanged_at_end_vpos = 0;
17053 struct glyph_row *last_text_row, *last_text_row_at_end;
17054 struct text_pos start;
17055 ptrdiff_t first_changed_charpos, last_changed_charpos;
17057 #if GLYPH_DEBUG
17058 if (inhibit_try_window_id)
17059 return 0;
17060 #endif
17062 /* This is handy for debugging. */
17063 #if 0
17064 #define GIVE_UP(X) \
17065 do { \
17066 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17067 return 0; \
17068 } while (0)
17069 #else
17070 #define GIVE_UP(X) return 0
17071 #endif
17073 SET_TEXT_POS_FROM_MARKER (start, w->start);
17075 /* Don't use this for mini-windows because these can show
17076 messages and mini-buffers, and we don't handle that here. */
17077 if (MINI_WINDOW_P (w))
17078 GIVE_UP (1);
17080 /* This flag is used to prevent redisplay optimizations. */
17081 if (windows_or_buffers_changed || cursor_type_changed)
17082 GIVE_UP (2);
17084 /* Verify that narrowing has not changed.
17085 Also verify that we were not told to prevent redisplay optimizations.
17086 It would be nice to further
17087 reduce the number of cases where this prevents try_window_id. */
17088 if (current_buffer->clip_changed
17089 || current_buffer->prevent_redisplay_optimizations_p)
17090 GIVE_UP (3);
17092 /* Window must either use window-based redisplay or be full width. */
17093 if (!FRAME_WINDOW_P (f)
17094 && (!FRAME_LINE_INS_DEL_OK (f)
17095 || !WINDOW_FULL_WIDTH_P (w)))
17096 GIVE_UP (4);
17098 /* Give up if point is known NOT to appear in W. */
17099 if (PT < CHARPOS (start))
17100 GIVE_UP (5);
17102 /* Another way to prevent redisplay optimizations. */
17103 if (XFASTINT (w->last_modified) == 0)
17104 GIVE_UP (6);
17106 /* Verify that window is not hscrolled. */
17107 if (XFASTINT (w->hscroll) != 0)
17108 GIVE_UP (7);
17110 /* Verify that display wasn't paused. */
17111 if (NILP (w->window_end_valid))
17112 GIVE_UP (8);
17114 /* Can't use this if highlighting a region because a cursor movement
17115 will do more than just set the cursor. */
17116 if (!NILP (Vtransient_mark_mode)
17117 && !NILP (BVAR (current_buffer, mark_active)))
17118 GIVE_UP (9);
17120 /* Likewise if highlighting trailing whitespace. */
17121 if (!NILP (Vshow_trailing_whitespace))
17122 GIVE_UP (11);
17124 /* Likewise if showing a region. */
17125 if (!NILP (w->region_showing))
17126 GIVE_UP (10);
17128 /* Can't use this if overlay arrow position and/or string have
17129 changed. */
17130 if (overlay_arrows_changed_p ())
17131 GIVE_UP (12);
17133 /* When word-wrap is on, adding a space to the first word of a
17134 wrapped line can change the wrap position, altering the line
17135 above it. It might be worthwhile to handle this more
17136 intelligently, but for now just redisplay from scratch. */
17137 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17138 GIVE_UP (21);
17140 /* Under bidi reordering, adding or deleting a character in the
17141 beginning of a paragraph, before the first strong directional
17142 character, can change the base direction of the paragraph (unless
17143 the buffer specifies a fixed paragraph direction), which will
17144 require to redisplay the whole paragraph. It might be worthwhile
17145 to find the paragraph limits and widen the range of redisplayed
17146 lines to that, but for now just give up this optimization and
17147 redisplay from scratch. */
17148 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17149 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17150 GIVE_UP (22);
17152 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17153 only if buffer has really changed. The reason is that the gap is
17154 initially at Z for freshly visited files. The code below would
17155 set end_unchanged to 0 in that case. */
17156 if (MODIFF > SAVE_MODIFF
17157 /* This seems to happen sometimes after saving a buffer. */
17158 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17160 if (GPT - BEG < BEG_UNCHANGED)
17161 BEG_UNCHANGED = GPT - BEG;
17162 if (Z - GPT < END_UNCHANGED)
17163 END_UNCHANGED = Z - GPT;
17166 /* The position of the first and last character that has been changed. */
17167 first_changed_charpos = BEG + BEG_UNCHANGED;
17168 last_changed_charpos = Z - END_UNCHANGED;
17170 /* If window starts after a line end, and the last change is in
17171 front of that newline, then changes don't affect the display.
17172 This case happens with stealth-fontification. Note that although
17173 the display is unchanged, glyph positions in the matrix have to
17174 be adjusted, of course. */
17175 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17177 && ((last_changed_charpos < CHARPOS (start)
17178 && CHARPOS (start) == BEGV)
17179 || (last_changed_charpos < CHARPOS (start) - 1
17180 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17182 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17183 struct glyph_row *r0;
17185 /* Compute how many chars/bytes have been added to or removed
17186 from the buffer. */
17187 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17188 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17189 Z_delta = Z - Z_old;
17190 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17192 /* Give up if PT is not in the window. Note that it already has
17193 been checked at the start of try_window_id that PT is not in
17194 front of the window start. */
17195 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17196 GIVE_UP (13);
17198 /* If window start is unchanged, we can reuse the whole matrix
17199 as is, after adjusting glyph positions. No need to compute
17200 the window end again, since its offset from Z hasn't changed. */
17201 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17202 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17203 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17204 /* PT must not be in a partially visible line. */
17205 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17206 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17208 /* Adjust positions in the glyph matrix. */
17209 if (Z_delta || Z_delta_bytes)
17211 struct glyph_row *r1
17212 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17213 increment_matrix_positions (w->current_matrix,
17214 MATRIX_ROW_VPOS (r0, current_matrix),
17215 MATRIX_ROW_VPOS (r1, current_matrix),
17216 Z_delta, Z_delta_bytes);
17219 /* Set the cursor. */
17220 row = row_containing_pos (w, PT, r0, NULL, 0);
17221 if (row)
17222 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17223 else
17224 abort ();
17225 return 1;
17229 /* Handle the case that changes are all below what is displayed in
17230 the window, and that PT is in the window. This shortcut cannot
17231 be taken if ZV is visible in the window, and text has been added
17232 there that is visible in the window. */
17233 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17234 /* ZV is not visible in the window, or there are no
17235 changes at ZV, actually. */
17236 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17237 || first_changed_charpos == last_changed_charpos))
17239 struct glyph_row *r0;
17241 /* Give up if PT is not in the window. Note that it already has
17242 been checked at the start of try_window_id that PT is not in
17243 front of the window start. */
17244 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17245 GIVE_UP (14);
17247 /* If window start is unchanged, we can reuse the whole matrix
17248 as is, without changing glyph positions since no text has
17249 been added/removed in front of the window end. */
17250 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17251 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17252 /* PT must not be in a partially visible line. */
17253 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17254 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17256 /* We have to compute the window end anew since text
17257 could have been added/removed after it. */
17258 w->window_end_pos
17259 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17260 w->window_end_bytepos
17261 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17263 /* Set the cursor. */
17264 row = row_containing_pos (w, PT, r0, NULL, 0);
17265 if (row)
17266 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17267 else
17268 abort ();
17269 return 2;
17273 /* Give up if window start is in the changed area.
17275 The condition used to read
17277 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17279 but why that was tested escapes me at the moment. */
17280 if (CHARPOS (start) >= first_changed_charpos
17281 && CHARPOS (start) <= last_changed_charpos)
17282 GIVE_UP (15);
17284 /* Check that window start agrees with the start of the first glyph
17285 row in its current matrix. Check this after we know the window
17286 start is not in changed text, otherwise positions would not be
17287 comparable. */
17288 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17289 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17290 GIVE_UP (16);
17292 /* Give up if the window ends in strings. Overlay strings
17293 at the end are difficult to handle, so don't try. */
17294 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17295 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17296 GIVE_UP (20);
17298 /* Compute the position at which we have to start displaying new
17299 lines. Some of the lines at the top of the window might be
17300 reusable because they are not displaying changed text. Find the
17301 last row in W's current matrix not affected by changes at the
17302 start of current_buffer. Value is null if changes start in the
17303 first line of window. */
17304 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17305 if (last_unchanged_at_beg_row)
17307 /* Avoid starting to display in the middle of a character, a TAB
17308 for instance. This is easier than to set up the iterator
17309 exactly, and it's not a frequent case, so the additional
17310 effort wouldn't really pay off. */
17311 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17312 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17313 && last_unchanged_at_beg_row > w->current_matrix->rows)
17314 --last_unchanged_at_beg_row;
17316 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17317 GIVE_UP (17);
17319 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17320 GIVE_UP (18);
17321 start_pos = it.current.pos;
17323 /* Start displaying new lines in the desired matrix at the same
17324 vpos we would use in the current matrix, i.e. below
17325 last_unchanged_at_beg_row. */
17326 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17327 current_matrix);
17328 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17329 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17331 xassert (it.hpos == 0 && it.current_x == 0);
17333 else
17335 /* There are no reusable lines at the start of the window.
17336 Start displaying in the first text line. */
17337 start_display (&it, w, start);
17338 it.vpos = it.first_vpos;
17339 start_pos = it.current.pos;
17342 /* Find the first row that is not affected by changes at the end of
17343 the buffer. Value will be null if there is no unchanged row, in
17344 which case we must redisplay to the end of the window. delta
17345 will be set to the value by which buffer positions beginning with
17346 first_unchanged_at_end_row have to be adjusted due to text
17347 changes. */
17348 first_unchanged_at_end_row
17349 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17350 IF_DEBUG (debug_delta = delta);
17351 IF_DEBUG (debug_delta_bytes = delta_bytes);
17353 /* Set stop_pos to the buffer position up to which we will have to
17354 display new lines. If first_unchanged_at_end_row != NULL, this
17355 is the buffer position of the start of the line displayed in that
17356 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17357 that we don't stop at a buffer position. */
17358 stop_pos = 0;
17359 if (first_unchanged_at_end_row)
17361 xassert (last_unchanged_at_beg_row == NULL
17362 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17364 /* If this is a continuation line, move forward to the next one
17365 that isn't. Changes in lines above affect this line.
17366 Caution: this may move first_unchanged_at_end_row to a row
17367 not displaying text. */
17368 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17369 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17370 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17371 < it.last_visible_y))
17372 ++first_unchanged_at_end_row;
17374 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17375 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17376 >= it.last_visible_y))
17377 first_unchanged_at_end_row = NULL;
17378 else
17380 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17381 + delta);
17382 first_unchanged_at_end_vpos
17383 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17384 xassert (stop_pos >= Z - END_UNCHANGED);
17387 else if (last_unchanged_at_beg_row == NULL)
17388 GIVE_UP (19);
17391 #if GLYPH_DEBUG
17393 /* Either there is no unchanged row at the end, or the one we have
17394 now displays text. This is a necessary condition for the window
17395 end pos calculation at the end of this function. */
17396 xassert (first_unchanged_at_end_row == NULL
17397 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17399 debug_last_unchanged_at_beg_vpos
17400 = (last_unchanged_at_beg_row
17401 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17402 : -1);
17403 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17405 #endif /* GLYPH_DEBUG != 0 */
17408 /* Display new lines. Set last_text_row to the last new line
17409 displayed which has text on it, i.e. might end up as being the
17410 line where the window_end_vpos is. */
17411 w->cursor.vpos = -1;
17412 last_text_row = NULL;
17413 overlay_arrow_seen = 0;
17414 while (it.current_y < it.last_visible_y
17415 && !fonts_changed_p
17416 && (first_unchanged_at_end_row == NULL
17417 || IT_CHARPOS (it) < stop_pos))
17419 if (display_line (&it))
17420 last_text_row = it.glyph_row - 1;
17423 if (fonts_changed_p)
17424 return -1;
17427 /* Compute differences in buffer positions, y-positions etc. for
17428 lines reused at the bottom of the window. Compute what we can
17429 scroll. */
17430 if (first_unchanged_at_end_row
17431 /* No lines reused because we displayed everything up to the
17432 bottom of the window. */
17433 && it.current_y < it.last_visible_y)
17435 dvpos = (it.vpos
17436 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17437 current_matrix));
17438 dy = it.current_y - first_unchanged_at_end_row->y;
17439 run.current_y = first_unchanged_at_end_row->y;
17440 run.desired_y = run.current_y + dy;
17441 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17443 else
17445 delta = delta_bytes = dvpos = dy
17446 = run.current_y = run.desired_y = run.height = 0;
17447 first_unchanged_at_end_row = NULL;
17449 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17452 /* Find the cursor if not already found. We have to decide whether
17453 PT will appear on this window (it sometimes doesn't, but this is
17454 not a very frequent case.) This decision has to be made before
17455 the current matrix is altered. A value of cursor.vpos < 0 means
17456 that PT is either in one of the lines beginning at
17457 first_unchanged_at_end_row or below the window. Don't care for
17458 lines that might be displayed later at the window end; as
17459 mentioned, this is not a frequent case. */
17460 if (w->cursor.vpos < 0)
17462 /* Cursor in unchanged rows at the top? */
17463 if (PT < CHARPOS (start_pos)
17464 && last_unchanged_at_beg_row)
17466 row = row_containing_pos (w, PT,
17467 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17468 last_unchanged_at_beg_row + 1, 0);
17469 if (row)
17470 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17473 /* Start from first_unchanged_at_end_row looking for PT. */
17474 else if (first_unchanged_at_end_row)
17476 row = row_containing_pos (w, PT - delta,
17477 first_unchanged_at_end_row, NULL, 0);
17478 if (row)
17479 set_cursor_from_row (w, row, w->current_matrix, delta,
17480 delta_bytes, dy, dvpos);
17483 /* Give up if cursor was not found. */
17484 if (w->cursor.vpos < 0)
17486 clear_glyph_matrix (w->desired_matrix);
17487 return -1;
17491 /* Don't let the cursor end in the scroll margins. */
17493 int this_scroll_margin, cursor_height;
17495 this_scroll_margin =
17496 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17497 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17498 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17500 if ((w->cursor.y < this_scroll_margin
17501 && CHARPOS (start) > BEGV)
17502 /* Old redisplay didn't take scroll margin into account at the bottom,
17503 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17504 || (w->cursor.y + (make_cursor_line_fully_visible_p
17505 ? cursor_height + this_scroll_margin
17506 : 1)) > it.last_visible_y)
17508 w->cursor.vpos = -1;
17509 clear_glyph_matrix (w->desired_matrix);
17510 return -1;
17514 /* Scroll the display. Do it before changing the current matrix so
17515 that xterm.c doesn't get confused about where the cursor glyph is
17516 found. */
17517 if (dy && run.height)
17519 update_begin (f);
17521 if (FRAME_WINDOW_P (f))
17523 FRAME_RIF (f)->update_window_begin_hook (w);
17524 FRAME_RIF (f)->clear_window_mouse_face (w);
17525 FRAME_RIF (f)->scroll_run_hook (w, &run);
17526 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17528 else
17530 /* Terminal frame. In this case, dvpos gives the number of
17531 lines to scroll by; dvpos < 0 means scroll up. */
17532 int from_vpos
17533 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17534 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17535 int end = (WINDOW_TOP_EDGE_LINE (w)
17536 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17537 + window_internal_height (w));
17539 #if defined (HAVE_GPM) || defined (MSDOS)
17540 x_clear_window_mouse_face (w);
17541 #endif
17542 /* Perform the operation on the screen. */
17543 if (dvpos > 0)
17545 /* Scroll last_unchanged_at_beg_row to the end of the
17546 window down dvpos lines. */
17547 set_terminal_window (f, end);
17549 /* On dumb terminals delete dvpos lines at the end
17550 before inserting dvpos empty lines. */
17551 if (!FRAME_SCROLL_REGION_OK (f))
17552 ins_del_lines (f, end - dvpos, -dvpos);
17554 /* Insert dvpos empty lines in front of
17555 last_unchanged_at_beg_row. */
17556 ins_del_lines (f, from, dvpos);
17558 else if (dvpos < 0)
17560 /* Scroll up last_unchanged_at_beg_vpos to the end of
17561 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17562 set_terminal_window (f, end);
17564 /* Delete dvpos lines in front of
17565 last_unchanged_at_beg_vpos. ins_del_lines will set
17566 the cursor to the given vpos and emit |dvpos| delete
17567 line sequences. */
17568 ins_del_lines (f, from + dvpos, dvpos);
17570 /* On a dumb terminal insert dvpos empty lines at the
17571 end. */
17572 if (!FRAME_SCROLL_REGION_OK (f))
17573 ins_del_lines (f, end + dvpos, -dvpos);
17576 set_terminal_window (f, 0);
17579 update_end (f);
17582 /* Shift reused rows of the current matrix to the right position.
17583 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17584 text. */
17585 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17586 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17587 if (dvpos < 0)
17589 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17590 bottom_vpos, dvpos);
17591 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17592 bottom_vpos, 0);
17594 else if (dvpos > 0)
17596 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17597 bottom_vpos, dvpos);
17598 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17599 first_unchanged_at_end_vpos + dvpos, 0);
17602 /* For frame-based redisplay, make sure that current frame and window
17603 matrix are in sync with respect to glyph memory. */
17604 if (!FRAME_WINDOW_P (f))
17605 sync_frame_with_window_matrix_rows (w);
17607 /* Adjust buffer positions in reused rows. */
17608 if (delta || delta_bytes)
17609 increment_matrix_positions (current_matrix,
17610 first_unchanged_at_end_vpos + dvpos,
17611 bottom_vpos, delta, delta_bytes);
17613 /* Adjust Y positions. */
17614 if (dy)
17615 shift_glyph_matrix (w, current_matrix,
17616 first_unchanged_at_end_vpos + dvpos,
17617 bottom_vpos, dy);
17619 if (first_unchanged_at_end_row)
17621 first_unchanged_at_end_row += dvpos;
17622 if (first_unchanged_at_end_row->y >= it.last_visible_y
17623 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17624 first_unchanged_at_end_row = NULL;
17627 /* If scrolling up, there may be some lines to display at the end of
17628 the window. */
17629 last_text_row_at_end = NULL;
17630 if (dy < 0)
17632 /* Scrolling up can leave for example a partially visible line
17633 at the end of the window to be redisplayed. */
17634 /* Set last_row to the glyph row in the current matrix where the
17635 window end line is found. It has been moved up or down in
17636 the matrix by dvpos. */
17637 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17638 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17640 /* If last_row is the window end line, it should display text. */
17641 xassert (last_row->displays_text_p);
17643 /* If window end line was partially visible before, begin
17644 displaying at that line. Otherwise begin displaying with the
17645 line following it. */
17646 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17648 init_to_row_start (&it, w, last_row);
17649 it.vpos = last_vpos;
17650 it.current_y = last_row->y;
17652 else
17654 init_to_row_end (&it, w, last_row);
17655 it.vpos = 1 + last_vpos;
17656 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17657 ++last_row;
17660 /* We may start in a continuation line. If so, we have to
17661 get the right continuation_lines_width and current_x. */
17662 it.continuation_lines_width = last_row->continuation_lines_width;
17663 it.hpos = it.current_x = 0;
17665 /* Display the rest of the lines at the window end. */
17666 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17667 while (it.current_y < it.last_visible_y
17668 && !fonts_changed_p)
17670 /* Is it always sure that the display agrees with lines in
17671 the current matrix? I don't think so, so we mark rows
17672 displayed invalid in the current matrix by setting their
17673 enabled_p flag to zero. */
17674 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17675 if (display_line (&it))
17676 last_text_row_at_end = it.glyph_row - 1;
17680 /* Update window_end_pos and window_end_vpos. */
17681 if (first_unchanged_at_end_row
17682 && !last_text_row_at_end)
17684 /* Window end line if one of the preserved rows from the current
17685 matrix. Set row to the last row displaying text in current
17686 matrix starting at first_unchanged_at_end_row, after
17687 scrolling. */
17688 xassert (first_unchanged_at_end_row->displays_text_p);
17689 row = find_last_row_displaying_text (w->current_matrix, &it,
17690 first_unchanged_at_end_row);
17691 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17693 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17694 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17695 w->window_end_vpos
17696 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17697 xassert (w->window_end_bytepos >= 0);
17698 IF_DEBUG (debug_method_add (w, "A"));
17700 else if (last_text_row_at_end)
17702 w->window_end_pos
17703 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17704 w->window_end_bytepos
17705 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17706 w->window_end_vpos
17707 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17708 xassert (w->window_end_bytepos >= 0);
17709 IF_DEBUG (debug_method_add (w, "B"));
17711 else if (last_text_row)
17713 /* We have displayed either to the end of the window or at the
17714 end of the window, i.e. the last row with text is to be found
17715 in the desired matrix. */
17716 w->window_end_pos
17717 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17718 w->window_end_bytepos
17719 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17720 w->window_end_vpos
17721 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17722 xassert (w->window_end_bytepos >= 0);
17724 else if (first_unchanged_at_end_row == NULL
17725 && last_text_row == NULL
17726 && last_text_row_at_end == NULL)
17728 /* Displayed to end of window, but no line containing text was
17729 displayed. Lines were deleted at the end of the window. */
17730 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17731 int vpos = XFASTINT (w->window_end_vpos);
17732 struct glyph_row *current_row = current_matrix->rows + vpos;
17733 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17735 for (row = NULL;
17736 row == NULL && vpos >= first_vpos;
17737 --vpos, --current_row, --desired_row)
17739 if (desired_row->enabled_p)
17741 if (desired_row->displays_text_p)
17742 row = desired_row;
17744 else if (current_row->displays_text_p)
17745 row = current_row;
17748 xassert (row != NULL);
17749 w->window_end_vpos = make_number (vpos + 1);
17750 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17751 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17752 xassert (w->window_end_bytepos >= 0);
17753 IF_DEBUG (debug_method_add (w, "C"));
17755 else
17756 abort ();
17758 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17759 debug_end_vpos = XFASTINT (w->window_end_vpos));
17761 /* Record that display has not been completed. */
17762 w->window_end_valid = Qnil;
17763 w->desired_matrix->no_scrolling_p = 1;
17764 return 3;
17766 #undef GIVE_UP
17771 /***********************************************************************
17772 More debugging support
17773 ***********************************************************************/
17775 #if GLYPH_DEBUG
17777 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17778 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17779 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17782 /* Dump the contents of glyph matrix MATRIX on stderr.
17784 GLYPHS 0 means don't show glyph contents.
17785 GLYPHS 1 means show glyphs in short form
17786 GLYPHS > 1 means show glyphs in long form. */
17788 void
17789 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17791 int i;
17792 for (i = 0; i < matrix->nrows; ++i)
17793 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17797 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17798 the glyph row and area where the glyph comes from. */
17800 void
17801 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17803 if (glyph->type == CHAR_GLYPH)
17805 fprintf (stderr,
17806 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17807 glyph - row->glyphs[TEXT_AREA],
17808 'C',
17809 glyph->charpos,
17810 (BUFFERP (glyph->object)
17811 ? 'B'
17812 : (STRINGP (glyph->object)
17813 ? 'S'
17814 : '-')),
17815 glyph->pixel_width,
17816 glyph->u.ch,
17817 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17818 ? glyph->u.ch
17819 : '.'),
17820 glyph->face_id,
17821 glyph->left_box_line_p,
17822 glyph->right_box_line_p);
17824 else if (glyph->type == STRETCH_GLYPH)
17826 fprintf (stderr,
17827 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17828 glyph - row->glyphs[TEXT_AREA],
17829 'S',
17830 glyph->charpos,
17831 (BUFFERP (glyph->object)
17832 ? 'B'
17833 : (STRINGP (glyph->object)
17834 ? 'S'
17835 : '-')),
17836 glyph->pixel_width,
17838 '.',
17839 glyph->face_id,
17840 glyph->left_box_line_p,
17841 glyph->right_box_line_p);
17843 else if (glyph->type == IMAGE_GLYPH)
17845 fprintf (stderr,
17846 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17847 glyph - row->glyphs[TEXT_AREA],
17848 'I',
17849 glyph->charpos,
17850 (BUFFERP (glyph->object)
17851 ? 'B'
17852 : (STRINGP (glyph->object)
17853 ? 'S'
17854 : '-')),
17855 glyph->pixel_width,
17856 glyph->u.img_id,
17857 '.',
17858 glyph->face_id,
17859 glyph->left_box_line_p,
17860 glyph->right_box_line_p);
17862 else if (glyph->type == COMPOSITE_GLYPH)
17864 fprintf (stderr,
17865 " %5td %4c %6"pI"d %c %3d 0x%05x",
17866 glyph - row->glyphs[TEXT_AREA],
17867 '+',
17868 glyph->charpos,
17869 (BUFFERP (glyph->object)
17870 ? 'B'
17871 : (STRINGP (glyph->object)
17872 ? 'S'
17873 : '-')),
17874 glyph->pixel_width,
17875 glyph->u.cmp.id);
17876 if (glyph->u.cmp.automatic)
17877 fprintf (stderr,
17878 "[%d-%d]",
17879 glyph->slice.cmp.from, glyph->slice.cmp.to);
17880 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17881 glyph->face_id,
17882 glyph->left_box_line_p,
17883 glyph->right_box_line_p);
17888 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17889 GLYPHS 0 means don't show glyph contents.
17890 GLYPHS 1 means show glyphs in short form
17891 GLYPHS > 1 means show glyphs in long form. */
17893 void
17894 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17896 if (glyphs != 1)
17898 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17899 fprintf (stderr, "======================================================================\n");
17901 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17902 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17903 vpos,
17904 MATRIX_ROW_START_CHARPOS (row),
17905 MATRIX_ROW_END_CHARPOS (row),
17906 row->used[TEXT_AREA],
17907 row->contains_overlapping_glyphs_p,
17908 row->enabled_p,
17909 row->truncated_on_left_p,
17910 row->truncated_on_right_p,
17911 row->continued_p,
17912 MATRIX_ROW_CONTINUATION_LINE_P (row),
17913 row->displays_text_p,
17914 row->ends_at_zv_p,
17915 row->fill_line_p,
17916 row->ends_in_middle_of_char_p,
17917 row->starts_in_middle_of_char_p,
17918 row->mouse_face_p,
17919 row->x,
17920 row->y,
17921 row->pixel_width,
17922 row->height,
17923 row->visible_height,
17924 row->ascent,
17925 row->phys_ascent);
17926 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17927 row->end.overlay_string_index,
17928 row->continuation_lines_width);
17929 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17930 CHARPOS (row->start.string_pos),
17931 CHARPOS (row->end.string_pos));
17932 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17933 row->end.dpvec_index);
17936 if (glyphs > 1)
17938 int area;
17940 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17942 struct glyph *glyph = row->glyphs[area];
17943 struct glyph *glyph_end = glyph + row->used[area];
17945 /* Glyph for a line end in text. */
17946 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17947 ++glyph_end;
17949 if (glyph < glyph_end)
17950 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17952 for (; glyph < glyph_end; ++glyph)
17953 dump_glyph (row, glyph, area);
17956 else if (glyphs == 1)
17958 int area;
17960 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17962 char *s = (char *) alloca (row->used[area] + 1);
17963 int i;
17965 for (i = 0; i < row->used[area]; ++i)
17967 struct glyph *glyph = row->glyphs[area] + i;
17968 if (glyph->type == CHAR_GLYPH
17969 && glyph->u.ch < 0x80
17970 && glyph->u.ch >= ' ')
17971 s[i] = glyph->u.ch;
17972 else
17973 s[i] = '.';
17976 s[i] = '\0';
17977 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17983 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17984 Sdump_glyph_matrix, 0, 1, "p",
17985 doc: /* Dump the current matrix of the selected window to stderr.
17986 Shows contents of glyph row structures. With non-nil
17987 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17988 glyphs in short form, otherwise show glyphs in long form. */)
17989 (Lisp_Object glyphs)
17991 struct window *w = XWINDOW (selected_window);
17992 struct buffer *buffer = XBUFFER (w->buffer);
17994 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17995 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17996 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17997 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17998 fprintf (stderr, "=============================================\n");
17999 dump_glyph_matrix (w->current_matrix,
18000 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18001 return Qnil;
18005 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18006 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18007 (void)
18009 struct frame *f = XFRAME (selected_frame);
18010 dump_glyph_matrix (f->current_matrix, 1);
18011 return Qnil;
18015 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18016 doc: /* Dump glyph row ROW to stderr.
18017 GLYPH 0 means don't dump glyphs.
18018 GLYPH 1 means dump glyphs in short form.
18019 GLYPH > 1 or omitted means dump glyphs in long form. */)
18020 (Lisp_Object row, Lisp_Object glyphs)
18022 struct glyph_matrix *matrix;
18023 EMACS_INT vpos;
18025 CHECK_NUMBER (row);
18026 matrix = XWINDOW (selected_window)->current_matrix;
18027 vpos = XINT (row);
18028 if (vpos >= 0 && vpos < matrix->nrows)
18029 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18030 vpos,
18031 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18032 return Qnil;
18036 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18037 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18038 GLYPH 0 means don't dump glyphs.
18039 GLYPH 1 means dump glyphs in short form.
18040 GLYPH > 1 or omitted means dump glyphs in long form. */)
18041 (Lisp_Object row, Lisp_Object glyphs)
18043 struct frame *sf = SELECTED_FRAME ();
18044 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18045 EMACS_INT vpos;
18047 CHECK_NUMBER (row);
18048 vpos = XINT (row);
18049 if (vpos >= 0 && vpos < m->nrows)
18050 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18051 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18052 return Qnil;
18056 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18057 doc: /* Toggle tracing of redisplay.
18058 With ARG, turn tracing on if and only if ARG is positive. */)
18059 (Lisp_Object arg)
18061 if (NILP (arg))
18062 trace_redisplay_p = !trace_redisplay_p;
18063 else
18065 arg = Fprefix_numeric_value (arg);
18066 trace_redisplay_p = XINT (arg) > 0;
18069 return Qnil;
18073 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18074 doc: /* Like `format', but print result to stderr.
18075 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18076 (ptrdiff_t nargs, Lisp_Object *args)
18078 Lisp_Object s = Fformat (nargs, args);
18079 fprintf (stderr, "%s", SDATA (s));
18080 return Qnil;
18083 #endif /* GLYPH_DEBUG */
18087 /***********************************************************************
18088 Building Desired Matrix Rows
18089 ***********************************************************************/
18091 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18092 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18094 static struct glyph_row *
18095 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18097 struct frame *f = XFRAME (WINDOW_FRAME (w));
18098 struct buffer *buffer = XBUFFER (w->buffer);
18099 struct buffer *old = current_buffer;
18100 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18101 int arrow_len = SCHARS (overlay_arrow_string);
18102 const unsigned char *arrow_end = arrow_string + arrow_len;
18103 const unsigned char *p;
18104 struct it it;
18105 int multibyte_p;
18106 int n_glyphs_before;
18108 set_buffer_temp (buffer);
18109 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18110 it.glyph_row->used[TEXT_AREA] = 0;
18111 SET_TEXT_POS (it.position, 0, 0);
18113 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18114 p = arrow_string;
18115 while (p < arrow_end)
18117 Lisp_Object face, ilisp;
18119 /* Get the next character. */
18120 if (multibyte_p)
18121 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18122 else
18124 it.c = it.char_to_display = *p, it.len = 1;
18125 if (! ASCII_CHAR_P (it.c))
18126 it.char_to_display = BYTE8_TO_CHAR (it.c);
18128 p += it.len;
18130 /* Get its face. */
18131 ilisp = make_number (p - arrow_string);
18132 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18133 it.face_id = compute_char_face (f, it.char_to_display, face);
18135 /* Compute its width, get its glyphs. */
18136 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18137 SET_TEXT_POS (it.position, -1, -1);
18138 PRODUCE_GLYPHS (&it);
18140 /* If this character doesn't fit any more in the line, we have
18141 to remove some glyphs. */
18142 if (it.current_x > it.last_visible_x)
18144 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18145 break;
18149 set_buffer_temp (old);
18150 return it.glyph_row;
18154 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18155 glyphs are only inserted for terminal frames since we can't really
18156 win with truncation glyphs when partially visible glyphs are
18157 involved. Which glyphs to insert is determined by
18158 produce_special_glyphs. */
18160 static void
18161 insert_left_trunc_glyphs (struct it *it)
18163 struct it truncate_it;
18164 struct glyph *from, *end, *to, *toend;
18166 xassert (!FRAME_WINDOW_P (it->f));
18168 /* Get the truncation glyphs. */
18169 truncate_it = *it;
18170 truncate_it.current_x = 0;
18171 truncate_it.face_id = DEFAULT_FACE_ID;
18172 truncate_it.glyph_row = &scratch_glyph_row;
18173 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18174 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18175 truncate_it.object = make_number (0);
18176 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18178 /* Overwrite glyphs from IT with truncation glyphs. */
18179 if (!it->glyph_row->reversed_p)
18181 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18182 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18183 to = it->glyph_row->glyphs[TEXT_AREA];
18184 toend = to + it->glyph_row->used[TEXT_AREA];
18186 while (from < end)
18187 *to++ = *from++;
18189 /* There may be padding glyphs left over. Overwrite them too. */
18190 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18192 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18193 while (from < end)
18194 *to++ = *from++;
18197 if (to > toend)
18198 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18200 else
18202 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18203 that back to front. */
18204 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18205 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18206 toend = it->glyph_row->glyphs[TEXT_AREA];
18207 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18209 while (from >= end && to >= toend)
18210 *to-- = *from--;
18211 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18213 from =
18214 truncate_it.glyph_row->glyphs[TEXT_AREA]
18215 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18216 while (from >= end && to >= toend)
18217 *to-- = *from--;
18219 if (from >= end)
18221 /* Need to free some room before prepending additional
18222 glyphs. */
18223 int move_by = from - end + 1;
18224 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18225 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18227 for ( ; g >= g0; g--)
18228 g[move_by] = *g;
18229 while (from >= end)
18230 *to-- = *from--;
18231 it->glyph_row->used[TEXT_AREA] += move_by;
18236 /* Compute the hash code for ROW. */
18237 unsigned
18238 row_hash (struct glyph_row *row)
18240 int area, k;
18241 unsigned hashval = 0;
18243 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18244 for (k = 0; k < row->used[area]; ++k)
18245 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18246 + row->glyphs[area][k].u.val
18247 + row->glyphs[area][k].face_id
18248 + row->glyphs[area][k].padding_p
18249 + (row->glyphs[area][k].type << 2));
18251 return hashval;
18254 /* Compute the pixel height and width of IT->glyph_row.
18256 Most of the time, ascent and height of a display line will be equal
18257 to the max_ascent and max_height values of the display iterator
18258 structure. This is not the case if
18260 1. We hit ZV without displaying anything. In this case, max_ascent
18261 and max_height will be zero.
18263 2. We have some glyphs that don't contribute to the line height.
18264 (The glyph row flag contributes_to_line_height_p is for future
18265 pixmap extensions).
18267 The first case is easily covered by using default values because in
18268 these cases, the line height does not really matter, except that it
18269 must not be zero. */
18271 static void
18272 compute_line_metrics (struct it *it)
18274 struct glyph_row *row = it->glyph_row;
18276 if (FRAME_WINDOW_P (it->f))
18278 int i, min_y, max_y;
18280 /* The line may consist of one space only, that was added to
18281 place the cursor on it. If so, the row's height hasn't been
18282 computed yet. */
18283 if (row->height == 0)
18285 if (it->max_ascent + it->max_descent == 0)
18286 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18287 row->ascent = it->max_ascent;
18288 row->height = it->max_ascent + it->max_descent;
18289 row->phys_ascent = it->max_phys_ascent;
18290 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18291 row->extra_line_spacing = it->max_extra_line_spacing;
18294 /* Compute the width of this line. */
18295 row->pixel_width = row->x;
18296 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18297 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18299 xassert (row->pixel_width >= 0);
18300 xassert (row->ascent >= 0 && row->height > 0);
18302 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18303 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18305 /* If first line's physical ascent is larger than its logical
18306 ascent, use the physical ascent, and make the row taller.
18307 This makes accented characters fully visible. */
18308 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18309 && row->phys_ascent > row->ascent)
18311 row->height += row->phys_ascent - row->ascent;
18312 row->ascent = row->phys_ascent;
18315 /* Compute how much of the line is visible. */
18316 row->visible_height = row->height;
18318 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18319 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18321 if (row->y < min_y)
18322 row->visible_height -= min_y - row->y;
18323 if (row->y + row->height > max_y)
18324 row->visible_height -= row->y + row->height - max_y;
18326 else
18328 row->pixel_width = row->used[TEXT_AREA];
18329 if (row->continued_p)
18330 row->pixel_width -= it->continuation_pixel_width;
18331 else if (row->truncated_on_right_p)
18332 row->pixel_width -= it->truncation_pixel_width;
18333 row->ascent = row->phys_ascent = 0;
18334 row->height = row->phys_height = row->visible_height = 1;
18335 row->extra_line_spacing = 0;
18338 /* Compute a hash code for this row. */
18339 row->hash = row_hash (row);
18341 it->max_ascent = it->max_descent = 0;
18342 it->max_phys_ascent = it->max_phys_descent = 0;
18346 /* Append one space to the glyph row of iterator IT if doing a
18347 window-based redisplay. The space has the same face as
18348 IT->face_id. Value is non-zero if a space was added.
18350 This function is called to make sure that there is always one glyph
18351 at the end of a glyph row that the cursor can be set on under
18352 window-systems. (If there weren't such a glyph we would not know
18353 how wide and tall a box cursor should be displayed).
18355 At the same time this space let's a nicely handle clearing to the
18356 end of the line if the row ends in italic text. */
18358 static int
18359 append_space_for_newline (struct it *it, int default_face_p)
18361 if (FRAME_WINDOW_P (it->f))
18363 int n = it->glyph_row->used[TEXT_AREA];
18365 if (it->glyph_row->glyphs[TEXT_AREA] + n
18366 < it->glyph_row->glyphs[1 + TEXT_AREA])
18368 /* Save some values that must not be changed.
18369 Must save IT->c and IT->len because otherwise
18370 ITERATOR_AT_END_P wouldn't work anymore after
18371 append_space_for_newline has been called. */
18372 enum display_element_type saved_what = it->what;
18373 int saved_c = it->c, saved_len = it->len;
18374 int saved_char_to_display = it->char_to_display;
18375 int saved_x = it->current_x;
18376 int saved_face_id = it->face_id;
18377 struct text_pos saved_pos;
18378 Lisp_Object saved_object;
18379 struct face *face;
18381 saved_object = it->object;
18382 saved_pos = it->position;
18384 it->what = IT_CHARACTER;
18385 memset (&it->position, 0, sizeof it->position);
18386 it->object = make_number (0);
18387 it->c = it->char_to_display = ' ';
18388 it->len = 1;
18390 /* If the default face was remapped, be sure to use the
18391 remapped face for the appended newline. */
18392 if (default_face_p)
18393 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18394 else if (it->face_before_selective_p)
18395 it->face_id = it->saved_face_id;
18396 face = FACE_FROM_ID (it->f, it->face_id);
18397 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18399 PRODUCE_GLYPHS (it);
18401 it->override_ascent = -1;
18402 it->constrain_row_ascent_descent_p = 0;
18403 it->current_x = saved_x;
18404 it->object = saved_object;
18405 it->position = saved_pos;
18406 it->what = saved_what;
18407 it->face_id = saved_face_id;
18408 it->len = saved_len;
18409 it->c = saved_c;
18410 it->char_to_display = saved_char_to_display;
18411 return 1;
18415 return 0;
18419 /* Extend the face of the last glyph in the text area of IT->glyph_row
18420 to the end of the display line. Called from display_line. If the
18421 glyph row is empty, add a space glyph to it so that we know the
18422 face to draw. Set the glyph row flag fill_line_p. If the glyph
18423 row is R2L, prepend a stretch glyph to cover the empty space to the
18424 left of the leftmost glyph. */
18426 static void
18427 extend_face_to_end_of_line (struct it *it)
18429 struct face *face, *default_face;
18430 struct frame *f = it->f;
18432 /* If line is already filled, do nothing. Non window-system frames
18433 get a grace of one more ``pixel'' because their characters are
18434 1-``pixel'' wide, so they hit the equality too early. This grace
18435 is needed only for R2L rows that are not continued, to produce
18436 one extra blank where we could display the cursor. */
18437 if (it->current_x >= it->last_visible_x
18438 + (!FRAME_WINDOW_P (f)
18439 && it->glyph_row->reversed_p
18440 && !it->glyph_row->continued_p))
18441 return;
18443 /* The default face, possibly remapped. */
18444 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18446 /* Face extension extends the background and box of IT->face_id
18447 to the end of the line. If the background equals the background
18448 of the frame, we don't have to do anything. */
18449 if (it->face_before_selective_p)
18450 face = FACE_FROM_ID (f, it->saved_face_id);
18451 else
18452 face = FACE_FROM_ID (f, it->face_id);
18454 if (FRAME_WINDOW_P (f)
18455 && it->glyph_row->displays_text_p
18456 && face->box == FACE_NO_BOX
18457 && face->background == FRAME_BACKGROUND_PIXEL (f)
18458 && !face->stipple
18459 && !it->glyph_row->reversed_p)
18460 return;
18462 /* Set the glyph row flag indicating that the face of the last glyph
18463 in the text area has to be drawn to the end of the text area. */
18464 it->glyph_row->fill_line_p = 1;
18466 /* If current character of IT is not ASCII, make sure we have the
18467 ASCII face. This will be automatically undone the next time
18468 get_next_display_element returns a multibyte character. Note
18469 that the character will always be single byte in unibyte
18470 text. */
18471 if (!ASCII_CHAR_P (it->c))
18473 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18476 if (FRAME_WINDOW_P (f))
18478 /* If the row is empty, add a space with the current face of IT,
18479 so that we know which face to draw. */
18480 if (it->glyph_row->used[TEXT_AREA] == 0)
18482 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18483 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18484 it->glyph_row->used[TEXT_AREA] = 1;
18486 #ifdef HAVE_WINDOW_SYSTEM
18487 if (it->glyph_row->reversed_p)
18489 /* Prepend a stretch glyph to the row, such that the
18490 rightmost glyph will be drawn flushed all the way to the
18491 right margin of the window. The stretch glyph that will
18492 occupy the empty space, if any, to the left of the
18493 glyphs. */
18494 struct font *font = face->font ? face->font : FRAME_FONT (f);
18495 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18496 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18497 struct glyph *g;
18498 int row_width, stretch_ascent, stretch_width;
18499 struct text_pos saved_pos;
18500 int saved_face_id, saved_avoid_cursor;
18502 for (row_width = 0, g = row_start; g < row_end; g++)
18503 row_width += g->pixel_width;
18504 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18505 if (stretch_width > 0)
18507 stretch_ascent =
18508 (((it->ascent + it->descent)
18509 * FONT_BASE (font)) / FONT_HEIGHT (font));
18510 saved_pos = it->position;
18511 memset (&it->position, 0, sizeof it->position);
18512 saved_avoid_cursor = it->avoid_cursor_p;
18513 it->avoid_cursor_p = 1;
18514 saved_face_id = it->face_id;
18515 /* The last row's stretch glyph should get the default
18516 face, to avoid painting the rest of the window with
18517 the region face, if the region ends at ZV. */
18518 if (it->glyph_row->ends_at_zv_p)
18519 it->face_id = default_face->id;
18520 else
18521 it->face_id = face->id;
18522 append_stretch_glyph (it, make_number (0), stretch_width,
18523 it->ascent + it->descent, stretch_ascent);
18524 it->position = saved_pos;
18525 it->avoid_cursor_p = saved_avoid_cursor;
18526 it->face_id = saved_face_id;
18529 #endif /* HAVE_WINDOW_SYSTEM */
18531 else
18533 /* Save some values that must not be changed. */
18534 int saved_x = it->current_x;
18535 struct text_pos saved_pos;
18536 Lisp_Object saved_object;
18537 enum display_element_type saved_what = it->what;
18538 int saved_face_id = it->face_id;
18540 saved_object = it->object;
18541 saved_pos = it->position;
18543 it->what = IT_CHARACTER;
18544 memset (&it->position, 0, sizeof it->position);
18545 it->object = make_number (0);
18546 it->c = it->char_to_display = ' ';
18547 it->len = 1;
18548 /* The last row's blank glyphs should get the default face, to
18549 avoid painting the rest of the window with the region face,
18550 if the region ends at ZV. */
18551 if (it->glyph_row->ends_at_zv_p)
18552 it->face_id = default_face->id;
18553 else
18554 it->face_id = face->id;
18556 PRODUCE_GLYPHS (it);
18558 while (it->current_x <= it->last_visible_x)
18559 PRODUCE_GLYPHS (it);
18561 /* Don't count these blanks really. It would let us insert a left
18562 truncation glyph below and make us set the cursor on them, maybe. */
18563 it->current_x = saved_x;
18564 it->object = saved_object;
18565 it->position = saved_pos;
18566 it->what = saved_what;
18567 it->face_id = saved_face_id;
18572 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18573 trailing whitespace. */
18575 static int
18576 trailing_whitespace_p (ptrdiff_t charpos)
18578 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18579 int c = 0;
18581 while (bytepos < ZV_BYTE
18582 && (c = FETCH_CHAR (bytepos),
18583 c == ' ' || c == '\t'))
18584 ++bytepos;
18586 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18588 if (bytepos != PT_BYTE)
18589 return 1;
18591 return 0;
18595 /* Highlight trailing whitespace, if any, in ROW. */
18597 static void
18598 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18600 int used = row->used[TEXT_AREA];
18602 if (used)
18604 struct glyph *start = row->glyphs[TEXT_AREA];
18605 struct glyph *glyph = start + used - 1;
18607 if (row->reversed_p)
18609 /* Right-to-left rows need to be processed in the opposite
18610 direction, so swap the edge pointers. */
18611 glyph = start;
18612 start = row->glyphs[TEXT_AREA] + used - 1;
18615 /* Skip over glyphs inserted to display the cursor at the
18616 end of a line, for extending the face of the last glyph
18617 to the end of the line on terminals, and for truncation
18618 and continuation glyphs. */
18619 if (!row->reversed_p)
18621 while (glyph >= start
18622 && glyph->type == CHAR_GLYPH
18623 && INTEGERP (glyph->object))
18624 --glyph;
18626 else
18628 while (glyph <= start
18629 && glyph->type == CHAR_GLYPH
18630 && INTEGERP (glyph->object))
18631 ++glyph;
18634 /* If last glyph is a space or stretch, and it's trailing
18635 whitespace, set the face of all trailing whitespace glyphs in
18636 IT->glyph_row to `trailing-whitespace'. */
18637 if ((row->reversed_p ? glyph <= start : glyph >= start)
18638 && BUFFERP (glyph->object)
18639 && (glyph->type == STRETCH_GLYPH
18640 || (glyph->type == CHAR_GLYPH
18641 && glyph->u.ch == ' '))
18642 && trailing_whitespace_p (glyph->charpos))
18644 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18645 if (face_id < 0)
18646 return;
18648 if (!row->reversed_p)
18650 while (glyph >= start
18651 && BUFFERP (glyph->object)
18652 && (glyph->type == STRETCH_GLYPH
18653 || (glyph->type == CHAR_GLYPH
18654 && glyph->u.ch == ' ')))
18655 (glyph--)->face_id = face_id;
18657 else
18659 while (glyph <= start
18660 && BUFFERP (glyph->object)
18661 && (glyph->type == STRETCH_GLYPH
18662 || (glyph->type == CHAR_GLYPH
18663 && glyph->u.ch == ' ')))
18664 (glyph++)->face_id = face_id;
18671 /* Value is non-zero if glyph row ROW should be
18672 used to hold the cursor. */
18674 static int
18675 cursor_row_p (struct glyph_row *row)
18677 int result = 1;
18679 if (PT == CHARPOS (row->end.pos)
18680 || PT == MATRIX_ROW_END_CHARPOS (row))
18682 /* Suppose the row ends on a string.
18683 Unless the row is continued, that means it ends on a newline
18684 in the string. If it's anything other than a display string
18685 (e.g., a before-string from an overlay), we don't want the
18686 cursor there. (This heuristic seems to give the optimal
18687 behavior for the various types of multi-line strings.)
18688 One exception: if the string has `cursor' property on one of
18689 its characters, we _do_ want the cursor there. */
18690 if (CHARPOS (row->end.string_pos) >= 0)
18692 if (row->continued_p)
18693 result = 1;
18694 else
18696 /* Check for `display' property. */
18697 struct glyph *beg = row->glyphs[TEXT_AREA];
18698 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18699 struct glyph *glyph;
18701 result = 0;
18702 for (glyph = end; glyph >= beg; --glyph)
18703 if (STRINGP (glyph->object))
18705 Lisp_Object prop
18706 = Fget_char_property (make_number (PT),
18707 Qdisplay, Qnil);
18708 result =
18709 (!NILP (prop)
18710 && display_prop_string_p (prop, glyph->object));
18711 /* If there's a `cursor' property on one of the
18712 string's characters, this row is a cursor row,
18713 even though this is not a display string. */
18714 if (!result)
18716 Lisp_Object s = glyph->object;
18718 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18720 ptrdiff_t gpos = glyph->charpos;
18722 if (!NILP (Fget_char_property (make_number (gpos),
18723 Qcursor, s)))
18725 result = 1;
18726 break;
18730 break;
18734 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18736 /* If the row ends in middle of a real character,
18737 and the line is continued, we want the cursor here.
18738 That's because CHARPOS (ROW->end.pos) would equal
18739 PT if PT is before the character. */
18740 if (!row->ends_in_ellipsis_p)
18741 result = row->continued_p;
18742 else
18743 /* If the row ends in an ellipsis, then
18744 CHARPOS (ROW->end.pos) will equal point after the
18745 invisible text. We want that position to be displayed
18746 after the ellipsis. */
18747 result = 0;
18749 /* If the row ends at ZV, display the cursor at the end of that
18750 row instead of at the start of the row below. */
18751 else if (row->ends_at_zv_p)
18752 result = 1;
18753 else
18754 result = 0;
18757 return result;
18762 /* Push the property PROP so that it will be rendered at the current
18763 position in IT. Return 1 if PROP was successfully pushed, 0
18764 otherwise. Called from handle_line_prefix to handle the
18765 `line-prefix' and `wrap-prefix' properties. */
18767 static int
18768 push_prefix_prop (struct it *it, Lisp_Object prop)
18770 struct text_pos pos =
18771 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18773 xassert (it->method == GET_FROM_BUFFER
18774 || it->method == GET_FROM_DISPLAY_VECTOR
18775 || it->method == GET_FROM_STRING);
18777 /* We need to save the current buffer/string position, so it will be
18778 restored by pop_it, because iterate_out_of_display_property
18779 depends on that being set correctly, but some situations leave
18780 it->position not yet set when this function is called. */
18781 push_it (it, &pos);
18783 if (STRINGP (prop))
18785 if (SCHARS (prop) == 0)
18787 pop_it (it);
18788 return 0;
18791 it->string = prop;
18792 it->string_from_prefix_prop_p = 1;
18793 it->multibyte_p = STRING_MULTIBYTE (it->string);
18794 it->current.overlay_string_index = -1;
18795 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18796 it->end_charpos = it->string_nchars = SCHARS (it->string);
18797 it->method = GET_FROM_STRING;
18798 it->stop_charpos = 0;
18799 it->prev_stop = 0;
18800 it->base_level_stop = 0;
18802 /* Force paragraph direction to be that of the parent
18803 buffer/string. */
18804 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18805 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18806 else
18807 it->paragraph_embedding = L2R;
18809 /* Set up the bidi iterator for this display string. */
18810 if (it->bidi_p)
18812 it->bidi_it.string.lstring = it->string;
18813 it->bidi_it.string.s = NULL;
18814 it->bidi_it.string.schars = it->end_charpos;
18815 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18816 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18817 it->bidi_it.string.unibyte = !it->multibyte_p;
18818 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18821 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18823 it->method = GET_FROM_STRETCH;
18824 it->object = prop;
18826 #ifdef HAVE_WINDOW_SYSTEM
18827 else if (IMAGEP (prop))
18829 it->what = IT_IMAGE;
18830 it->image_id = lookup_image (it->f, prop);
18831 it->method = GET_FROM_IMAGE;
18833 #endif /* HAVE_WINDOW_SYSTEM */
18834 else
18836 pop_it (it); /* bogus display property, give up */
18837 return 0;
18840 return 1;
18843 /* Return the character-property PROP at the current position in IT. */
18845 static Lisp_Object
18846 get_it_property (struct it *it, Lisp_Object prop)
18848 Lisp_Object position;
18850 if (STRINGP (it->object))
18851 position = make_number (IT_STRING_CHARPOS (*it));
18852 else if (BUFFERP (it->object))
18853 position = make_number (IT_CHARPOS (*it));
18854 else
18855 return Qnil;
18857 return Fget_char_property (position, prop, it->object);
18860 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18862 static void
18863 handle_line_prefix (struct it *it)
18865 Lisp_Object prefix;
18867 if (it->continuation_lines_width > 0)
18869 prefix = get_it_property (it, Qwrap_prefix);
18870 if (NILP (prefix))
18871 prefix = Vwrap_prefix;
18873 else
18875 prefix = get_it_property (it, Qline_prefix);
18876 if (NILP (prefix))
18877 prefix = Vline_prefix;
18879 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18881 /* If the prefix is wider than the window, and we try to wrap
18882 it, it would acquire its own wrap prefix, and so on till the
18883 iterator stack overflows. So, don't wrap the prefix. */
18884 it->line_wrap = TRUNCATE;
18885 it->avoid_cursor_p = 1;
18891 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18892 only for R2L lines from display_line and display_string, when they
18893 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18894 the line/string needs to be continued on the next glyph row. */
18895 static void
18896 unproduce_glyphs (struct it *it, int n)
18898 struct glyph *glyph, *end;
18900 xassert (it->glyph_row);
18901 xassert (it->glyph_row->reversed_p);
18902 xassert (it->area == TEXT_AREA);
18903 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18905 if (n > it->glyph_row->used[TEXT_AREA])
18906 n = it->glyph_row->used[TEXT_AREA];
18907 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18908 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18909 for ( ; glyph < end; glyph++)
18910 glyph[-n] = *glyph;
18913 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18914 and ROW->maxpos. */
18915 static void
18916 find_row_edges (struct it *it, struct glyph_row *row,
18917 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18918 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18920 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18921 lines' rows is implemented for bidi-reordered rows. */
18923 /* ROW->minpos is the value of min_pos, the minimal buffer position
18924 we have in ROW, or ROW->start.pos if that is smaller. */
18925 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18926 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18927 else
18928 /* We didn't find buffer positions smaller than ROW->start, or
18929 didn't find _any_ valid buffer positions in any of the glyphs,
18930 so we must trust the iterator's computed positions. */
18931 row->minpos = row->start.pos;
18932 if (max_pos <= 0)
18934 max_pos = CHARPOS (it->current.pos);
18935 max_bpos = BYTEPOS (it->current.pos);
18938 /* Here are the various use-cases for ending the row, and the
18939 corresponding values for ROW->maxpos:
18941 Line ends in a newline from buffer eol_pos + 1
18942 Line is continued from buffer max_pos + 1
18943 Line is truncated on right it->current.pos
18944 Line ends in a newline from string max_pos + 1(*)
18945 (*) + 1 only when line ends in a forward scan
18946 Line is continued from string max_pos
18947 Line is continued from display vector max_pos
18948 Line is entirely from a string min_pos == max_pos
18949 Line is entirely from a display vector min_pos == max_pos
18950 Line that ends at ZV ZV
18952 If you discover other use-cases, please add them here as
18953 appropriate. */
18954 if (row->ends_at_zv_p)
18955 row->maxpos = it->current.pos;
18956 else if (row->used[TEXT_AREA])
18958 int seen_this_string = 0;
18959 struct glyph_row *r1 = row - 1;
18961 /* Did we see the same display string on the previous row? */
18962 if (STRINGP (it->object)
18963 /* this is not the first row */
18964 && row > it->w->desired_matrix->rows
18965 /* previous row is not the header line */
18966 && !r1->mode_line_p
18967 /* previous row also ends in a newline from a string */
18968 && r1->ends_in_newline_from_string_p)
18970 struct glyph *start, *end;
18972 /* Search for the last glyph of the previous row that came
18973 from buffer or string. Depending on whether the row is
18974 L2R or R2L, we need to process it front to back or the
18975 other way round. */
18976 if (!r1->reversed_p)
18978 start = r1->glyphs[TEXT_AREA];
18979 end = start + r1->used[TEXT_AREA];
18980 /* Glyphs inserted by redisplay have an integer (zero)
18981 as their object. */
18982 while (end > start
18983 && INTEGERP ((end - 1)->object)
18984 && (end - 1)->charpos <= 0)
18985 --end;
18986 if (end > start)
18988 if (EQ ((end - 1)->object, it->object))
18989 seen_this_string = 1;
18991 else
18992 /* If all the glyphs of the previous row were inserted
18993 by redisplay, it means the previous row was
18994 produced from a single newline, which is only
18995 possible if that newline came from the same string
18996 as the one which produced this ROW. */
18997 seen_this_string = 1;
18999 else
19001 end = r1->glyphs[TEXT_AREA] - 1;
19002 start = end + r1->used[TEXT_AREA];
19003 while (end < start
19004 && INTEGERP ((end + 1)->object)
19005 && (end + 1)->charpos <= 0)
19006 ++end;
19007 if (end < start)
19009 if (EQ ((end + 1)->object, it->object))
19010 seen_this_string = 1;
19012 else
19013 seen_this_string = 1;
19016 /* Take note of each display string that covers a newline only
19017 once, the first time we see it. This is for when a display
19018 string includes more than one newline in it. */
19019 if (row->ends_in_newline_from_string_p && !seen_this_string)
19021 /* If we were scanning the buffer forward when we displayed
19022 the string, we want to account for at least one buffer
19023 position that belongs to this row (position covered by
19024 the display string), so that cursor positioning will
19025 consider this row as a candidate when point is at the end
19026 of the visual line represented by this row. This is not
19027 required when scanning back, because max_pos will already
19028 have a much larger value. */
19029 if (CHARPOS (row->end.pos) > max_pos)
19030 INC_BOTH (max_pos, max_bpos);
19031 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19033 else if (CHARPOS (it->eol_pos) > 0)
19034 SET_TEXT_POS (row->maxpos,
19035 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19036 else if (row->continued_p)
19038 /* If max_pos is different from IT's current position, it
19039 means IT->method does not belong to the display element
19040 at max_pos. However, it also means that the display
19041 element at max_pos was displayed in its entirety on this
19042 line, which is equivalent to saying that the next line
19043 starts at the next buffer position. */
19044 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19045 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19046 else
19048 INC_BOTH (max_pos, max_bpos);
19049 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19052 else if (row->truncated_on_right_p)
19053 /* display_line already called reseat_at_next_visible_line_start,
19054 which puts the iterator at the beginning of the next line, in
19055 the logical order. */
19056 row->maxpos = it->current.pos;
19057 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19058 /* A line that is entirely from a string/image/stretch... */
19059 row->maxpos = row->minpos;
19060 else
19061 abort ();
19063 else
19064 row->maxpos = it->current.pos;
19067 /* Construct the glyph row IT->glyph_row in the desired matrix of
19068 IT->w from text at the current position of IT. See dispextern.h
19069 for an overview of struct it. Value is non-zero if
19070 IT->glyph_row displays text, as opposed to a line displaying ZV
19071 only. */
19073 static int
19074 display_line (struct it *it)
19076 struct glyph_row *row = it->glyph_row;
19077 Lisp_Object overlay_arrow_string;
19078 struct it wrap_it;
19079 void *wrap_data = NULL;
19080 int may_wrap = 0, wrap_x IF_LINT (= 0);
19081 int wrap_row_used = -1;
19082 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19083 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19084 int wrap_row_extra_line_spacing IF_LINT (= 0);
19085 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19086 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19087 int cvpos;
19088 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19089 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19091 /* We always start displaying at hpos zero even if hscrolled. */
19092 xassert (it->hpos == 0 && it->current_x == 0);
19094 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19095 >= it->w->desired_matrix->nrows)
19097 it->w->nrows_scale_factor++;
19098 fonts_changed_p = 1;
19099 return 0;
19102 /* Is IT->w showing the region? */
19103 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19105 /* Clear the result glyph row and enable it. */
19106 prepare_desired_row (row);
19108 row->y = it->current_y;
19109 row->start = it->start;
19110 row->continuation_lines_width = it->continuation_lines_width;
19111 row->displays_text_p = 1;
19112 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19113 it->starts_in_middle_of_char_p = 0;
19115 /* Arrange the overlays nicely for our purposes. Usually, we call
19116 display_line on only one line at a time, in which case this
19117 can't really hurt too much, or we call it on lines which appear
19118 one after another in the buffer, in which case all calls to
19119 recenter_overlay_lists but the first will be pretty cheap. */
19120 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19122 /* Move over display elements that are not visible because we are
19123 hscrolled. This may stop at an x-position < IT->first_visible_x
19124 if the first glyph is partially visible or if we hit a line end. */
19125 if (it->current_x < it->first_visible_x)
19127 this_line_min_pos = row->start.pos;
19128 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19129 MOVE_TO_POS | MOVE_TO_X);
19130 /* Record the smallest positions seen while we moved over
19131 display elements that are not visible. This is needed by
19132 redisplay_internal for optimizing the case where the cursor
19133 stays inside the same line. The rest of this function only
19134 considers positions that are actually displayed, so
19135 RECORD_MAX_MIN_POS will not otherwise record positions that
19136 are hscrolled to the left of the left edge of the window. */
19137 min_pos = CHARPOS (this_line_min_pos);
19138 min_bpos = BYTEPOS (this_line_min_pos);
19140 else
19142 /* We only do this when not calling `move_it_in_display_line_to'
19143 above, because move_it_in_display_line_to calls
19144 handle_line_prefix itself. */
19145 handle_line_prefix (it);
19148 /* Get the initial row height. This is either the height of the
19149 text hscrolled, if there is any, or zero. */
19150 row->ascent = it->max_ascent;
19151 row->height = it->max_ascent + it->max_descent;
19152 row->phys_ascent = it->max_phys_ascent;
19153 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19154 row->extra_line_spacing = it->max_extra_line_spacing;
19156 /* Utility macro to record max and min buffer positions seen until now. */
19157 #define RECORD_MAX_MIN_POS(IT) \
19158 do \
19160 int composition_p = !STRINGP ((IT)->string) \
19161 && ((IT)->what == IT_COMPOSITION); \
19162 ptrdiff_t current_pos = \
19163 composition_p ? (IT)->cmp_it.charpos \
19164 : IT_CHARPOS (*(IT)); \
19165 ptrdiff_t current_bpos = \
19166 composition_p ? CHAR_TO_BYTE (current_pos) \
19167 : IT_BYTEPOS (*(IT)); \
19168 if (current_pos < min_pos) \
19170 min_pos = current_pos; \
19171 min_bpos = current_bpos; \
19173 if (IT_CHARPOS (*it) > max_pos) \
19175 max_pos = IT_CHARPOS (*it); \
19176 max_bpos = IT_BYTEPOS (*it); \
19179 while (0)
19181 /* Loop generating characters. The loop is left with IT on the next
19182 character to display. */
19183 while (1)
19185 int n_glyphs_before, hpos_before, x_before;
19186 int x, nglyphs;
19187 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19189 /* Retrieve the next thing to display. Value is zero if end of
19190 buffer reached. */
19191 if (!get_next_display_element (it))
19193 /* Maybe add a space at the end of this line that is used to
19194 display the cursor there under X. Set the charpos of the
19195 first glyph of blank lines not corresponding to any text
19196 to -1. */
19197 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19198 row->exact_window_width_line_p = 1;
19199 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19200 || row->used[TEXT_AREA] == 0)
19202 row->glyphs[TEXT_AREA]->charpos = -1;
19203 row->displays_text_p = 0;
19205 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19206 && (!MINI_WINDOW_P (it->w)
19207 || (minibuf_level && EQ (it->window, minibuf_window))))
19208 row->indicate_empty_line_p = 1;
19211 it->continuation_lines_width = 0;
19212 row->ends_at_zv_p = 1;
19213 /* A row that displays right-to-left text must always have
19214 its last face extended all the way to the end of line,
19215 even if this row ends in ZV, because we still write to
19216 the screen left to right. We also need to extend the
19217 last face if the default face is remapped to some
19218 different face, otherwise the functions that clear
19219 portions of the screen will clear with the default face's
19220 background color. */
19221 if (row->reversed_p
19222 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19223 extend_face_to_end_of_line (it);
19224 break;
19227 /* Now, get the metrics of what we want to display. This also
19228 generates glyphs in `row' (which is IT->glyph_row). */
19229 n_glyphs_before = row->used[TEXT_AREA];
19230 x = it->current_x;
19232 /* Remember the line height so far in case the next element doesn't
19233 fit on the line. */
19234 if (it->line_wrap != TRUNCATE)
19236 ascent = it->max_ascent;
19237 descent = it->max_descent;
19238 phys_ascent = it->max_phys_ascent;
19239 phys_descent = it->max_phys_descent;
19241 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19243 if (IT_DISPLAYING_WHITESPACE (it))
19244 may_wrap = 1;
19245 else if (may_wrap)
19247 SAVE_IT (wrap_it, *it, wrap_data);
19248 wrap_x = x;
19249 wrap_row_used = row->used[TEXT_AREA];
19250 wrap_row_ascent = row->ascent;
19251 wrap_row_height = row->height;
19252 wrap_row_phys_ascent = row->phys_ascent;
19253 wrap_row_phys_height = row->phys_height;
19254 wrap_row_extra_line_spacing = row->extra_line_spacing;
19255 wrap_row_min_pos = min_pos;
19256 wrap_row_min_bpos = min_bpos;
19257 wrap_row_max_pos = max_pos;
19258 wrap_row_max_bpos = max_bpos;
19259 may_wrap = 0;
19264 PRODUCE_GLYPHS (it);
19266 /* If this display element was in marginal areas, continue with
19267 the next one. */
19268 if (it->area != TEXT_AREA)
19270 row->ascent = max (row->ascent, it->max_ascent);
19271 row->height = max (row->height, it->max_ascent + it->max_descent);
19272 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19273 row->phys_height = max (row->phys_height,
19274 it->max_phys_ascent + it->max_phys_descent);
19275 row->extra_line_spacing = max (row->extra_line_spacing,
19276 it->max_extra_line_spacing);
19277 set_iterator_to_next (it, 1);
19278 continue;
19281 /* Does the display element fit on the line? If we truncate
19282 lines, we should draw past the right edge of the window. If
19283 we don't truncate, we want to stop so that we can display the
19284 continuation glyph before the right margin. If lines are
19285 continued, there are two possible strategies for characters
19286 resulting in more than 1 glyph (e.g. tabs): Display as many
19287 glyphs as possible in this line and leave the rest for the
19288 continuation line, or display the whole element in the next
19289 line. Original redisplay did the former, so we do it also. */
19290 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19291 hpos_before = it->hpos;
19292 x_before = x;
19294 if (/* Not a newline. */
19295 nglyphs > 0
19296 /* Glyphs produced fit entirely in the line. */
19297 && it->current_x < it->last_visible_x)
19299 it->hpos += nglyphs;
19300 row->ascent = max (row->ascent, it->max_ascent);
19301 row->height = max (row->height, it->max_ascent + it->max_descent);
19302 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19303 row->phys_height = max (row->phys_height,
19304 it->max_phys_ascent + it->max_phys_descent);
19305 row->extra_line_spacing = max (row->extra_line_spacing,
19306 it->max_extra_line_spacing);
19307 if (it->current_x - it->pixel_width < it->first_visible_x)
19308 row->x = x - it->first_visible_x;
19309 /* Record the maximum and minimum buffer positions seen so
19310 far in glyphs that will be displayed by this row. */
19311 if (it->bidi_p)
19312 RECORD_MAX_MIN_POS (it);
19314 else
19316 int i, new_x;
19317 struct glyph *glyph;
19319 for (i = 0; i < nglyphs; ++i, x = new_x)
19321 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19322 new_x = x + glyph->pixel_width;
19324 if (/* Lines are continued. */
19325 it->line_wrap != TRUNCATE
19326 && (/* Glyph doesn't fit on the line. */
19327 new_x > it->last_visible_x
19328 /* Or it fits exactly on a window system frame. */
19329 || (new_x == it->last_visible_x
19330 && FRAME_WINDOW_P (it->f))))
19332 /* End of a continued line. */
19334 if (it->hpos == 0
19335 || (new_x == it->last_visible_x
19336 && FRAME_WINDOW_P (it->f)))
19338 /* Current glyph is the only one on the line or
19339 fits exactly on the line. We must continue
19340 the line because we can't draw the cursor
19341 after the glyph. */
19342 row->continued_p = 1;
19343 it->current_x = new_x;
19344 it->continuation_lines_width += new_x;
19345 ++it->hpos;
19346 if (i == nglyphs - 1)
19348 /* If line-wrap is on, check if a previous
19349 wrap point was found. */
19350 if (wrap_row_used > 0
19351 /* Even if there is a previous wrap
19352 point, continue the line here as
19353 usual, if (i) the previous character
19354 was a space or tab AND (ii) the
19355 current character is not. */
19356 && (!may_wrap
19357 || IT_DISPLAYING_WHITESPACE (it)))
19358 goto back_to_wrap;
19360 /* Record the maximum and minimum buffer
19361 positions seen so far in glyphs that will be
19362 displayed by this row. */
19363 if (it->bidi_p)
19364 RECORD_MAX_MIN_POS (it);
19365 set_iterator_to_next (it, 1);
19366 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19368 if (!get_next_display_element (it))
19370 row->exact_window_width_line_p = 1;
19371 it->continuation_lines_width = 0;
19372 row->continued_p = 0;
19373 row->ends_at_zv_p = 1;
19375 else if (ITERATOR_AT_END_OF_LINE_P (it))
19377 row->continued_p = 0;
19378 row->exact_window_width_line_p = 1;
19382 else if (it->bidi_p)
19383 RECORD_MAX_MIN_POS (it);
19385 else if (CHAR_GLYPH_PADDING_P (*glyph)
19386 && !FRAME_WINDOW_P (it->f))
19388 /* A padding glyph that doesn't fit on this line.
19389 This means the whole character doesn't fit
19390 on the line. */
19391 if (row->reversed_p)
19392 unproduce_glyphs (it, row->used[TEXT_AREA]
19393 - n_glyphs_before);
19394 row->used[TEXT_AREA] = n_glyphs_before;
19396 /* Fill the rest of the row with continuation
19397 glyphs like in 20.x. */
19398 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19399 < row->glyphs[1 + TEXT_AREA])
19400 produce_special_glyphs (it, IT_CONTINUATION);
19402 row->continued_p = 1;
19403 it->current_x = x_before;
19404 it->continuation_lines_width += x_before;
19406 /* Restore the height to what it was before the
19407 element not fitting on the line. */
19408 it->max_ascent = ascent;
19409 it->max_descent = descent;
19410 it->max_phys_ascent = phys_ascent;
19411 it->max_phys_descent = phys_descent;
19413 else if (wrap_row_used > 0)
19415 back_to_wrap:
19416 if (row->reversed_p)
19417 unproduce_glyphs (it,
19418 row->used[TEXT_AREA] - wrap_row_used);
19419 RESTORE_IT (it, &wrap_it, wrap_data);
19420 it->continuation_lines_width += wrap_x;
19421 row->used[TEXT_AREA] = wrap_row_used;
19422 row->ascent = wrap_row_ascent;
19423 row->height = wrap_row_height;
19424 row->phys_ascent = wrap_row_phys_ascent;
19425 row->phys_height = wrap_row_phys_height;
19426 row->extra_line_spacing = wrap_row_extra_line_spacing;
19427 min_pos = wrap_row_min_pos;
19428 min_bpos = wrap_row_min_bpos;
19429 max_pos = wrap_row_max_pos;
19430 max_bpos = wrap_row_max_bpos;
19431 row->continued_p = 1;
19432 row->ends_at_zv_p = 0;
19433 row->exact_window_width_line_p = 0;
19434 it->continuation_lines_width += x;
19436 /* Make sure that a non-default face is extended
19437 up to the right margin of the window. */
19438 extend_face_to_end_of_line (it);
19440 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19442 /* A TAB that extends past the right edge of the
19443 window. This produces a single glyph on
19444 window system frames. We leave the glyph in
19445 this row and let it fill the row, but don't
19446 consume the TAB. */
19447 it->continuation_lines_width += it->last_visible_x;
19448 row->ends_in_middle_of_char_p = 1;
19449 row->continued_p = 1;
19450 glyph->pixel_width = it->last_visible_x - x;
19451 it->starts_in_middle_of_char_p = 1;
19453 else
19455 /* Something other than a TAB that draws past
19456 the right edge of the window. Restore
19457 positions to values before the element. */
19458 if (row->reversed_p)
19459 unproduce_glyphs (it, row->used[TEXT_AREA]
19460 - (n_glyphs_before + i));
19461 row->used[TEXT_AREA] = n_glyphs_before + i;
19463 /* Display continuation glyphs. */
19464 if (!FRAME_WINDOW_P (it->f))
19465 produce_special_glyphs (it, IT_CONTINUATION);
19466 row->continued_p = 1;
19468 it->current_x = x_before;
19469 it->continuation_lines_width += x;
19470 extend_face_to_end_of_line (it);
19472 if (nglyphs > 1 && i > 0)
19474 row->ends_in_middle_of_char_p = 1;
19475 it->starts_in_middle_of_char_p = 1;
19478 /* Restore the height to what it was before the
19479 element not fitting on the line. */
19480 it->max_ascent = ascent;
19481 it->max_descent = descent;
19482 it->max_phys_ascent = phys_ascent;
19483 it->max_phys_descent = phys_descent;
19486 break;
19488 else if (new_x > it->first_visible_x)
19490 /* Increment number of glyphs actually displayed. */
19491 ++it->hpos;
19493 /* Record the maximum and minimum buffer positions
19494 seen so far in glyphs that will be displayed by
19495 this row. */
19496 if (it->bidi_p)
19497 RECORD_MAX_MIN_POS (it);
19499 if (x < it->first_visible_x)
19500 /* Glyph is partially visible, i.e. row starts at
19501 negative X position. */
19502 row->x = x - it->first_visible_x;
19504 else
19506 /* Glyph is completely off the left margin of the
19507 window. This should not happen because of the
19508 move_it_in_display_line at the start of this
19509 function, unless the text display area of the
19510 window is empty. */
19511 xassert (it->first_visible_x <= it->last_visible_x);
19514 /* Even if this display element produced no glyphs at all,
19515 we want to record its position. */
19516 if (it->bidi_p && nglyphs == 0)
19517 RECORD_MAX_MIN_POS (it);
19519 row->ascent = max (row->ascent, it->max_ascent);
19520 row->height = max (row->height, it->max_ascent + it->max_descent);
19521 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19522 row->phys_height = max (row->phys_height,
19523 it->max_phys_ascent + it->max_phys_descent);
19524 row->extra_line_spacing = max (row->extra_line_spacing,
19525 it->max_extra_line_spacing);
19527 /* End of this display line if row is continued. */
19528 if (row->continued_p || row->ends_at_zv_p)
19529 break;
19532 at_end_of_line:
19533 /* Is this a line end? If yes, we're also done, after making
19534 sure that a non-default face is extended up to the right
19535 margin of the window. */
19536 if (ITERATOR_AT_END_OF_LINE_P (it))
19538 int used_before = row->used[TEXT_AREA];
19540 row->ends_in_newline_from_string_p = STRINGP (it->object);
19542 /* Add a space at the end of the line that is used to
19543 display the cursor there. */
19544 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19545 append_space_for_newline (it, 0);
19547 /* Extend the face to the end of the line. */
19548 extend_face_to_end_of_line (it);
19550 /* Make sure we have the position. */
19551 if (used_before == 0)
19552 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19554 /* Record the position of the newline, for use in
19555 find_row_edges. */
19556 it->eol_pos = it->current.pos;
19558 /* Consume the line end. This skips over invisible lines. */
19559 set_iterator_to_next (it, 1);
19560 it->continuation_lines_width = 0;
19561 break;
19564 /* Proceed with next display element. Note that this skips
19565 over lines invisible because of selective display. */
19566 set_iterator_to_next (it, 1);
19568 /* If we truncate lines, we are done when the last displayed
19569 glyphs reach past the right margin of the window. */
19570 if (it->line_wrap == TRUNCATE
19571 && (FRAME_WINDOW_P (it->f)
19572 ? (it->current_x >= it->last_visible_x)
19573 : (it->current_x > it->last_visible_x)))
19575 /* Maybe add truncation glyphs. */
19576 if (!FRAME_WINDOW_P (it->f))
19578 int i, n;
19580 if (!row->reversed_p)
19582 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19583 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19584 break;
19586 else
19588 for (i = 0; i < row->used[TEXT_AREA]; i++)
19589 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19590 break;
19591 /* Remove any padding glyphs at the front of ROW, to
19592 make room for the truncation glyphs we will be
19593 adding below. The loop below always inserts at
19594 least one truncation glyph, so also remove the
19595 last glyph added to ROW. */
19596 unproduce_glyphs (it, i + 1);
19597 /* Adjust i for the loop below. */
19598 i = row->used[TEXT_AREA] - (i + 1);
19601 for (n = row->used[TEXT_AREA]; i < n; ++i)
19603 row->used[TEXT_AREA] = i;
19604 produce_special_glyphs (it, IT_TRUNCATION);
19607 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19609 /* Don't truncate if we can overflow newline into fringe. */
19610 if (!get_next_display_element (it))
19612 it->continuation_lines_width = 0;
19613 row->ends_at_zv_p = 1;
19614 row->exact_window_width_line_p = 1;
19615 break;
19617 if (ITERATOR_AT_END_OF_LINE_P (it))
19619 row->exact_window_width_line_p = 1;
19620 goto at_end_of_line;
19624 row->truncated_on_right_p = 1;
19625 it->continuation_lines_width = 0;
19626 reseat_at_next_visible_line_start (it, 0);
19627 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19628 it->hpos = hpos_before;
19629 it->current_x = x_before;
19630 break;
19634 if (wrap_data)
19635 bidi_unshelve_cache (wrap_data, 1);
19637 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19638 at the left window margin. */
19639 if (it->first_visible_x
19640 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19642 if (!FRAME_WINDOW_P (it->f))
19643 insert_left_trunc_glyphs (it);
19644 row->truncated_on_left_p = 1;
19647 /* Remember the position at which this line ends.
19649 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19650 cannot be before the call to find_row_edges below, since that is
19651 where these positions are determined. */
19652 row->end = it->current;
19653 if (!it->bidi_p)
19655 row->minpos = row->start.pos;
19656 row->maxpos = row->end.pos;
19658 else
19660 /* ROW->minpos and ROW->maxpos must be the smallest and
19661 `1 + the largest' buffer positions in ROW. But if ROW was
19662 bidi-reordered, these two positions can be anywhere in the
19663 row, so we must determine them now. */
19664 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19667 /* If the start of this line is the overlay arrow-position, then
19668 mark this glyph row as the one containing the overlay arrow.
19669 This is clearly a mess with variable size fonts. It would be
19670 better to let it be displayed like cursors under X. */
19671 if ((row->displays_text_p || !overlay_arrow_seen)
19672 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19673 !NILP (overlay_arrow_string)))
19675 /* Overlay arrow in window redisplay is a fringe bitmap. */
19676 if (STRINGP (overlay_arrow_string))
19678 struct glyph_row *arrow_row
19679 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19680 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19681 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19682 struct glyph *p = row->glyphs[TEXT_AREA];
19683 struct glyph *p2, *end;
19685 /* Copy the arrow glyphs. */
19686 while (glyph < arrow_end)
19687 *p++ = *glyph++;
19689 /* Throw away padding glyphs. */
19690 p2 = p;
19691 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19692 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19693 ++p2;
19694 if (p2 > p)
19696 while (p2 < end)
19697 *p++ = *p2++;
19698 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19701 else
19703 xassert (INTEGERP (overlay_arrow_string));
19704 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19706 overlay_arrow_seen = 1;
19709 /* Highlight trailing whitespace. */
19710 if (!NILP (Vshow_trailing_whitespace))
19711 highlight_trailing_whitespace (it->f, it->glyph_row);
19713 /* Compute pixel dimensions of this line. */
19714 compute_line_metrics (it);
19716 /* Implementation note: No changes in the glyphs of ROW or in their
19717 faces can be done past this point, because compute_line_metrics
19718 computes ROW's hash value and stores it within the glyph_row
19719 structure. */
19721 /* Record whether this row ends inside an ellipsis. */
19722 row->ends_in_ellipsis_p
19723 = (it->method == GET_FROM_DISPLAY_VECTOR
19724 && it->ellipsis_p);
19726 /* Save fringe bitmaps in this row. */
19727 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19728 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19729 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19730 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19732 it->left_user_fringe_bitmap = 0;
19733 it->left_user_fringe_face_id = 0;
19734 it->right_user_fringe_bitmap = 0;
19735 it->right_user_fringe_face_id = 0;
19737 /* Maybe set the cursor. */
19738 cvpos = it->w->cursor.vpos;
19739 if ((cvpos < 0
19740 /* In bidi-reordered rows, keep checking for proper cursor
19741 position even if one has been found already, because buffer
19742 positions in such rows change non-linearly with ROW->VPOS,
19743 when a line is continued. One exception: when we are at ZV,
19744 display cursor on the first suitable glyph row, since all
19745 the empty rows after that also have their position set to ZV. */
19746 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19747 lines' rows is implemented for bidi-reordered rows. */
19748 || (it->bidi_p
19749 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19750 && PT >= MATRIX_ROW_START_CHARPOS (row)
19751 && PT <= MATRIX_ROW_END_CHARPOS (row)
19752 && cursor_row_p (row))
19753 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19755 /* Prepare for the next line. This line starts horizontally at (X
19756 HPOS) = (0 0). Vertical positions are incremented. As a
19757 convenience for the caller, IT->glyph_row is set to the next
19758 row to be used. */
19759 it->current_x = it->hpos = 0;
19760 it->current_y += row->height;
19761 SET_TEXT_POS (it->eol_pos, 0, 0);
19762 ++it->vpos;
19763 ++it->glyph_row;
19764 /* The next row should by default use the same value of the
19765 reversed_p flag as this one. set_iterator_to_next decides when
19766 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19767 the flag accordingly. */
19768 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19769 it->glyph_row->reversed_p = row->reversed_p;
19770 it->start = row->end;
19771 return row->displays_text_p;
19773 #undef RECORD_MAX_MIN_POS
19776 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19777 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19778 doc: /* Return paragraph direction at point in BUFFER.
19779 Value is either `left-to-right' or `right-to-left'.
19780 If BUFFER is omitted or nil, it defaults to the current buffer.
19782 Paragraph direction determines how the text in the paragraph is displayed.
19783 In left-to-right paragraphs, text begins at the left margin of the window
19784 and the reading direction is generally left to right. In right-to-left
19785 paragraphs, text begins at the right margin and is read from right to left.
19787 See also `bidi-paragraph-direction'. */)
19788 (Lisp_Object buffer)
19790 struct buffer *buf = current_buffer;
19791 struct buffer *old = buf;
19793 if (! NILP (buffer))
19795 CHECK_BUFFER (buffer);
19796 buf = XBUFFER (buffer);
19799 if (NILP (BVAR (buf, bidi_display_reordering))
19800 || NILP (BVAR (buf, enable_multibyte_characters))
19801 /* When we are loading loadup.el, the character property tables
19802 needed for bidi iteration are not yet available. */
19803 || !NILP (Vpurify_flag))
19804 return Qleft_to_right;
19805 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19806 return BVAR (buf, bidi_paragraph_direction);
19807 else
19809 /* Determine the direction from buffer text. We could try to
19810 use current_matrix if it is up to date, but this seems fast
19811 enough as it is. */
19812 struct bidi_it itb;
19813 ptrdiff_t pos = BUF_PT (buf);
19814 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19815 int c;
19816 void *itb_data = bidi_shelve_cache ();
19818 set_buffer_temp (buf);
19819 /* bidi_paragraph_init finds the base direction of the paragraph
19820 by searching forward from paragraph start. We need the base
19821 direction of the current or _previous_ paragraph, so we need
19822 to make sure we are within that paragraph. To that end, find
19823 the previous non-empty line. */
19824 if (pos >= ZV && pos > BEGV)
19826 pos--;
19827 bytepos = CHAR_TO_BYTE (pos);
19829 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19830 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19832 while ((c = FETCH_BYTE (bytepos)) == '\n'
19833 || c == ' ' || c == '\t' || c == '\f')
19835 if (bytepos <= BEGV_BYTE)
19836 break;
19837 bytepos--;
19838 pos--;
19840 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19841 bytepos--;
19843 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19844 itb.paragraph_dir = NEUTRAL_DIR;
19845 itb.string.s = NULL;
19846 itb.string.lstring = Qnil;
19847 itb.string.bufpos = 0;
19848 itb.string.unibyte = 0;
19849 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19850 bidi_unshelve_cache (itb_data, 0);
19851 set_buffer_temp (old);
19852 switch (itb.paragraph_dir)
19854 case L2R:
19855 return Qleft_to_right;
19856 break;
19857 case R2L:
19858 return Qright_to_left;
19859 break;
19860 default:
19861 abort ();
19868 /***********************************************************************
19869 Menu Bar
19870 ***********************************************************************/
19872 /* Redisplay the menu bar in the frame for window W.
19874 The menu bar of X frames that don't have X toolkit support is
19875 displayed in a special window W->frame->menu_bar_window.
19877 The menu bar of terminal frames is treated specially as far as
19878 glyph matrices are concerned. Menu bar lines are not part of
19879 windows, so the update is done directly on the frame matrix rows
19880 for the menu bar. */
19882 static void
19883 display_menu_bar (struct window *w)
19885 struct frame *f = XFRAME (WINDOW_FRAME (w));
19886 struct it it;
19887 Lisp_Object items;
19888 int i;
19890 /* Don't do all this for graphical frames. */
19891 #ifdef HAVE_NTGUI
19892 if (FRAME_W32_P (f))
19893 return;
19894 #endif
19895 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19896 if (FRAME_X_P (f))
19897 return;
19898 #endif
19900 #ifdef HAVE_NS
19901 if (FRAME_NS_P (f))
19902 return;
19903 #endif /* HAVE_NS */
19905 #ifdef USE_X_TOOLKIT
19906 xassert (!FRAME_WINDOW_P (f));
19907 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19908 it.first_visible_x = 0;
19909 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19910 #else /* not USE_X_TOOLKIT */
19911 if (FRAME_WINDOW_P (f))
19913 /* Menu bar lines are displayed in the desired matrix of the
19914 dummy window menu_bar_window. */
19915 struct window *menu_w;
19916 xassert (WINDOWP (f->menu_bar_window));
19917 menu_w = XWINDOW (f->menu_bar_window);
19918 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19919 MENU_FACE_ID);
19920 it.first_visible_x = 0;
19921 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19923 else
19925 /* This is a TTY frame, i.e. character hpos/vpos are used as
19926 pixel x/y. */
19927 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19928 MENU_FACE_ID);
19929 it.first_visible_x = 0;
19930 it.last_visible_x = FRAME_COLS (f);
19932 #endif /* not USE_X_TOOLKIT */
19934 /* FIXME: This should be controlled by a user option. See the
19935 comments in redisplay_tool_bar and display_mode_line about
19936 this. */
19937 it.paragraph_embedding = L2R;
19939 if (! mode_line_inverse_video)
19940 /* Force the menu-bar to be displayed in the default face. */
19941 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19943 /* Clear all rows of the menu bar. */
19944 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19946 struct glyph_row *row = it.glyph_row + i;
19947 clear_glyph_row (row);
19948 row->enabled_p = 1;
19949 row->full_width_p = 1;
19952 /* Display all items of the menu bar. */
19953 items = FRAME_MENU_BAR_ITEMS (it.f);
19954 for (i = 0; i < ASIZE (items); i += 4)
19956 Lisp_Object string;
19958 /* Stop at nil string. */
19959 string = AREF (items, i + 1);
19960 if (NILP (string))
19961 break;
19963 /* Remember where item was displayed. */
19964 ASET (items, i + 3, make_number (it.hpos));
19966 /* Display the item, pad with one space. */
19967 if (it.current_x < it.last_visible_x)
19968 display_string (NULL, string, Qnil, 0, 0, &it,
19969 SCHARS (string) + 1, 0, 0, -1);
19972 /* Fill out the line with spaces. */
19973 if (it.current_x < it.last_visible_x)
19974 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19976 /* Compute the total height of the lines. */
19977 compute_line_metrics (&it);
19982 /***********************************************************************
19983 Mode Line
19984 ***********************************************************************/
19986 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19987 FORCE is non-zero, redisplay mode lines unconditionally.
19988 Otherwise, redisplay only mode lines that are garbaged. Value is
19989 the number of windows whose mode lines were redisplayed. */
19991 static int
19992 redisplay_mode_lines (Lisp_Object window, int force)
19994 int nwindows = 0;
19996 while (!NILP (window))
19998 struct window *w = XWINDOW (window);
20000 if (WINDOWP (w->hchild))
20001 nwindows += redisplay_mode_lines (w->hchild, force);
20002 else if (WINDOWP (w->vchild))
20003 nwindows += redisplay_mode_lines (w->vchild, force);
20004 else if (force
20005 || FRAME_GARBAGED_P (XFRAME (w->frame))
20006 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20008 struct text_pos lpoint;
20009 struct buffer *old = current_buffer;
20011 /* Set the window's buffer for the mode line display. */
20012 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20013 set_buffer_internal_1 (XBUFFER (w->buffer));
20015 /* Point refers normally to the selected window. For any
20016 other window, set up appropriate value. */
20017 if (!EQ (window, selected_window))
20019 struct text_pos pt;
20021 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20022 if (CHARPOS (pt) < BEGV)
20023 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20024 else if (CHARPOS (pt) > (ZV - 1))
20025 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20026 else
20027 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20030 /* Display mode lines. */
20031 clear_glyph_matrix (w->desired_matrix);
20032 if (display_mode_lines (w))
20034 ++nwindows;
20035 w->must_be_updated_p = 1;
20038 /* Restore old settings. */
20039 set_buffer_internal_1 (old);
20040 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20043 window = w->next;
20046 return nwindows;
20050 /* Display the mode and/or header line of window W. Value is the
20051 sum number of mode lines and header lines displayed. */
20053 static int
20054 display_mode_lines (struct window *w)
20056 Lisp_Object old_selected_window, old_selected_frame;
20057 int n = 0;
20059 old_selected_frame = selected_frame;
20060 selected_frame = w->frame;
20061 old_selected_window = selected_window;
20062 XSETWINDOW (selected_window, w);
20064 /* These will be set while the mode line specs are processed. */
20065 line_number_displayed = 0;
20066 w->column_number_displayed = Qnil;
20068 if (WINDOW_WANTS_MODELINE_P (w))
20070 struct window *sel_w = XWINDOW (old_selected_window);
20072 /* Select mode line face based on the real selected window. */
20073 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20074 BVAR (current_buffer, mode_line_format));
20075 ++n;
20078 if (WINDOW_WANTS_HEADER_LINE_P (w))
20080 display_mode_line (w, HEADER_LINE_FACE_ID,
20081 BVAR (current_buffer, header_line_format));
20082 ++n;
20085 selected_frame = old_selected_frame;
20086 selected_window = old_selected_window;
20087 return n;
20091 /* Display mode or header line of window W. FACE_ID specifies which
20092 line to display; it is either MODE_LINE_FACE_ID or
20093 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20094 display. Value is the pixel height of the mode/header line
20095 displayed. */
20097 static int
20098 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20100 struct it it;
20101 struct face *face;
20102 ptrdiff_t count = SPECPDL_INDEX ();
20104 init_iterator (&it, w, -1, -1, NULL, face_id);
20105 /* Don't extend on a previously drawn mode-line.
20106 This may happen if called from pos_visible_p. */
20107 it.glyph_row->enabled_p = 0;
20108 prepare_desired_row (it.glyph_row);
20110 it.glyph_row->mode_line_p = 1;
20112 if (! mode_line_inverse_video)
20113 /* Force the mode-line to be displayed in the default face. */
20114 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20116 /* FIXME: This should be controlled by a user option. But
20117 supporting such an option is not trivial, since the mode line is
20118 made up of many separate strings. */
20119 it.paragraph_embedding = L2R;
20121 record_unwind_protect (unwind_format_mode_line,
20122 format_mode_line_unwind_data (NULL, Qnil, 0));
20124 mode_line_target = MODE_LINE_DISPLAY;
20126 /* Temporarily make frame's keyboard the current kboard so that
20127 kboard-local variables in the mode_line_format will get the right
20128 values. */
20129 push_kboard (FRAME_KBOARD (it.f));
20130 record_unwind_save_match_data ();
20131 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20132 pop_kboard ();
20134 unbind_to (count, Qnil);
20136 /* Fill up with spaces. */
20137 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20139 compute_line_metrics (&it);
20140 it.glyph_row->full_width_p = 1;
20141 it.glyph_row->continued_p = 0;
20142 it.glyph_row->truncated_on_left_p = 0;
20143 it.glyph_row->truncated_on_right_p = 0;
20145 /* Make a 3D mode-line have a shadow at its right end. */
20146 face = FACE_FROM_ID (it.f, face_id);
20147 extend_face_to_end_of_line (&it);
20148 if (face->box != FACE_NO_BOX)
20150 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20151 + it.glyph_row->used[TEXT_AREA] - 1);
20152 last->right_box_line_p = 1;
20155 return it.glyph_row->height;
20158 /* Move element ELT in LIST to the front of LIST.
20159 Return the updated list. */
20161 static Lisp_Object
20162 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20164 register Lisp_Object tail, prev;
20165 register Lisp_Object tem;
20167 tail = list;
20168 prev = Qnil;
20169 while (CONSP (tail))
20171 tem = XCAR (tail);
20173 if (EQ (elt, tem))
20175 /* Splice out the link TAIL. */
20176 if (NILP (prev))
20177 list = XCDR (tail);
20178 else
20179 Fsetcdr (prev, XCDR (tail));
20181 /* Now make it the first. */
20182 Fsetcdr (tail, list);
20183 return tail;
20185 else
20186 prev = tail;
20187 tail = XCDR (tail);
20188 QUIT;
20191 /* Not found--return unchanged LIST. */
20192 return list;
20195 /* Contribute ELT to the mode line for window IT->w. How it
20196 translates into text depends on its data type.
20198 IT describes the display environment in which we display, as usual.
20200 DEPTH is the depth in recursion. It is used to prevent
20201 infinite recursion here.
20203 FIELD_WIDTH is the number of characters the display of ELT should
20204 occupy in the mode line, and PRECISION is the maximum number of
20205 characters to display from ELT's representation. See
20206 display_string for details.
20208 Returns the hpos of the end of the text generated by ELT.
20210 PROPS is a property list to add to any string we encounter.
20212 If RISKY is nonzero, remove (disregard) any properties in any string
20213 we encounter, and ignore :eval and :propertize.
20215 The global variable `mode_line_target' determines whether the
20216 output is passed to `store_mode_line_noprop',
20217 `store_mode_line_string', or `display_string'. */
20219 static int
20220 display_mode_element (struct it *it, int depth, int field_width, int precision,
20221 Lisp_Object elt, Lisp_Object props, int risky)
20223 int n = 0, field, prec;
20224 int literal = 0;
20226 tail_recurse:
20227 if (depth > 100)
20228 elt = build_string ("*too-deep*");
20230 depth++;
20232 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20234 case Lisp_String:
20236 /* A string: output it and check for %-constructs within it. */
20237 unsigned char c;
20238 ptrdiff_t offset = 0;
20240 if (SCHARS (elt) > 0
20241 && (!NILP (props) || risky))
20243 Lisp_Object oprops, aelt;
20244 oprops = Ftext_properties_at (make_number (0), elt);
20246 /* If the starting string's properties are not what
20247 we want, translate the string. Also, if the string
20248 is risky, do that anyway. */
20250 if (NILP (Fequal (props, oprops)) || risky)
20252 /* If the starting string has properties,
20253 merge the specified ones onto the existing ones. */
20254 if (! NILP (oprops) && !risky)
20256 Lisp_Object tem;
20258 oprops = Fcopy_sequence (oprops);
20259 tem = props;
20260 while (CONSP (tem))
20262 oprops = Fplist_put (oprops, XCAR (tem),
20263 XCAR (XCDR (tem)));
20264 tem = XCDR (XCDR (tem));
20266 props = oprops;
20269 aelt = Fassoc (elt, mode_line_proptrans_alist);
20270 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20272 /* AELT is what we want. Move it to the front
20273 without consing. */
20274 elt = XCAR (aelt);
20275 mode_line_proptrans_alist
20276 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20278 else
20280 Lisp_Object tem;
20282 /* If AELT has the wrong props, it is useless.
20283 so get rid of it. */
20284 if (! NILP (aelt))
20285 mode_line_proptrans_alist
20286 = Fdelq (aelt, mode_line_proptrans_alist);
20288 elt = Fcopy_sequence (elt);
20289 Fset_text_properties (make_number (0), Flength (elt),
20290 props, elt);
20291 /* Add this item to mode_line_proptrans_alist. */
20292 mode_line_proptrans_alist
20293 = Fcons (Fcons (elt, props),
20294 mode_line_proptrans_alist);
20295 /* Truncate mode_line_proptrans_alist
20296 to at most 50 elements. */
20297 tem = Fnthcdr (make_number (50),
20298 mode_line_proptrans_alist);
20299 if (! NILP (tem))
20300 XSETCDR (tem, Qnil);
20305 offset = 0;
20307 if (literal)
20309 prec = precision - n;
20310 switch (mode_line_target)
20312 case MODE_LINE_NOPROP:
20313 case MODE_LINE_TITLE:
20314 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20315 break;
20316 case MODE_LINE_STRING:
20317 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20318 break;
20319 case MODE_LINE_DISPLAY:
20320 n += display_string (NULL, elt, Qnil, 0, 0, it,
20321 0, prec, 0, STRING_MULTIBYTE (elt));
20322 break;
20325 break;
20328 /* Handle the non-literal case. */
20330 while ((precision <= 0 || n < precision)
20331 && SREF (elt, offset) != 0
20332 && (mode_line_target != MODE_LINE_DISPLAY
20333 || it->current_x < it->last_visible_x))
20335 ptrdiff_t last_offset = offset;
20337 /* Advance to end of string or next format specifier. */
20338 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20341 if (offset - 1 != last_offset)
20343 ptrdiff_t nchars, nbytes;
20345 /* Output to end of string or up to '%'. Field width
20346 is length of string. Don't output more than
20347 PRECISION allows us. */
20348 offset--;
20350 prec = c_string_width (SDATA (elt) + last_offset,
20351 offset - last_offset, precision - n,
20352 &nchars, &nbytes);
20354 switch (mode_line_target)
20356 case MODE_LINE_NOPROP:
20357 case MODE_LINE_TITLE:
20358 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20359 break;
20360 case MODE_LINE_STRING:
20362 ptrdiff_t bytepos = last_offset;
20363 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20364 ptrdiff_t endpos = (precision <= 0
20365 ? string_byte_to_char (elt, offset)
20366 : charpos + nchars);
20368 n += store_mode_line_string (NULL,
20369 Fsubstring (elt, make_number (charpos),
20370 make_number (endpos)),
20371 0, 0, 0, Qnil);
20373 break;
20374 case MODE_LINE_DISPLAY:
20376 ptrdiff_t bytepos = last_offset;
20377 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20379 if (precision <= 0)
20380 nchars = string_byte_to_char (elt, offset) - charpos;
20381 n += display_string (NULL, elt, Qnil, 0, charpos,
20382 it, 0, nchars, 0,
20383 STRING_MULTIBYTE (elt));
20385 break;
20388 else /* c == '%' */
20390 ptrdiff_t percent_position = offset;
20392 /* Get the specified minimum width. Zero means
20393 don't pad. */
20394 field = 0;
20395 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20396 field = field * 10 + c - '0';
20398 /* Don't pad beyond the total padding allowed. */
20399 if (field_width - n > 0 && field > field_width - n)
20400 field = field_width - n;
20402 /* Note that either PRECISION <= 0 or N < PRECISION. */
20403 prec = precision - n;
20405 if (c == 'M')
20406 n += display_mode_element (it, depth, field, prec,
20407 Vglobal_mode_string, props,
20408 risky);
20409 else if (c != 0)
20411 int multibyte;
20412 ptrdiff_t bytepos, charpos;
20413 const char *spec;
20414 Lisp_Object string;
20416 bytepos = percent_position;
20417 charpos = (STRING_MULTIBYTE (elt)
20418 ? string_byte_to_char (elt, bytepos)
20419 : bytepos);
20420 spec = decode_mode_spec (it->w, c, field, &string);
20421 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20423 switch (mode_line_target)
20425 case MODE_LINE_NOPROP:
20426 case MODE_LINE_TITLE:
20427 n += store_mode_line_noprop (spec, field, prec);
20428 break;
20429 case MODE_LINE_STRING:
20431 Lisp_Object tem = build_string (spec);
20432 props = Ftext_properties_at (make_number (charpos), elt);
20433 /* Should only keep face property in props */
20434 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20436 break;
20437 case MODE_LINE_DISPLAY:
20439 int nglyphs_before, nwritten;
20441 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20442 nwritten = display_string (spec, string, elt,
20443 charpos, 0, it,
20444 field, prec, 0,
20445 multibyte);
20447 /* Assign to the glyphs written above the
20448 string where the `%x' came from, position
20449 of the `%'. */
20450 if (nwritten > 0)
20452 struct glyph *glyph
20453 = (it->glyph_row->glyphs[TEXT_AREA]
20454 + nglyphs_before);
20455 int i;
20457 for (i = 0; i < nwritten; ++i)
20459 glyph[i].object = elt;
20460 glyph[i].charpos = charpos;
20463 n += nwritten;
20466 break;
20469 else /* c == 0 */
20470 break;
20474 break;
20476 case Lisp_Symbol:
20477 /* A symbol: process the value of the symbol recursively
20478 as if it appeared here directly. Avoid error if symbol void.
20479 Special case: if value of symbol is a string, output the string
20480 literally. */
20482 register Lisp_Object tem;
20484 /* If the variable is not marked as risky to set
20485 then its contents are risky to use. */
20486 if (NILP (Fget (elt, Qrisky_local_variable)))
20487 risky = 1;
20489 tem = Fboundp (elt);
20490 if (!NILP (tem))
20492 tem = Fsymbol_value (elt);
20493 /* If value is a string, output that string literally:
20494 don't check for % within it. */
20495 if (STRINGP (tem))
20496 literal = 1;
20498 if (!EQ (tem, elt))
20500 /* Give up right away for nil or t. */
20501 elt = tem;
20502 goto tail_recurse;
20506 break;
20508 case Lisp_Cons:
20510 register Lisp_Object car, tem;
20512 /* A cons cell: five distinct cases.
20513 If first element is :eval or :propertize, do something special.
20514 If first element is a string or a cons, process all the elements
20515 and effectively concatenate them.
20516 If first element is a negative number, truncate displaying cdr to
20517 at most that many characters. If positive, pad (with spaces)
20518 to at least that many characters.
20519 If first element is a symbol, process the cadr or caddr recursively
20520 according to whether the symbol's value is non-nil or nil. */
20521 car = XCAR (elt);
20522 if (EQ (car, QCeval))
20524 /* An element of the form (:eval FORM) means evaluate FORM
20525 and use the result as mode line elements. */
20527 if (risky)
20528 break;
20530 if (CONSP (XCDR (elt)))
20532 Lisp_Object spec;
20533 spec = safe_eval (XCAR (XCDR (elt)));
20534 n += display_mode_element (it, depth, field_width - n,
20535 precision - n, spec, props,
20536 risky);
20539 else if (EQ (car, QCpropertize))
20541 /* An element of the form (:propertize ELT PROPS...)
20542 means display ELT but applying properties PROPS. */
20544 if (risky)
20545 break;
20547 if (CONSP (XCDR (elt)))
20548 n += display_mode_element (it, depth, field_width - n,
20549 precision - n, XCAR (XCDR (elt)),
20550 XCDR (XCDR (elt)), risky);
20552 else if (SYMBOLP (car))
20554 tem = Fboundp (car);
20555 elt = XCDR (elt);
20556 if (!CONSP (elt))
20557 goto invalid;
20558 /* elt is now the cdr, and we know it is a cons cell.
20559 Use its car if CAR has a non-nil value. */
20560 if (!NILP (tem))
20562 tem = Fsymbol_value (car);
20563 if (!NILP (tem))
20565 elt = XCAR (elt);
20566 goto tail_recurse;
20569 /* Symbol's value is nil (or symbol is unbound)
20570 Get the cddr of the original list
20571 and if possible find the caddr and use that. */
20572 elt = XCDR (elt);
20573 if (NILP (elt))
20574 break;
20575 else if (!CONSP (elt))
20576 goto invalid;
20577 elt = XCAR (elt);
20578 goto tail_recurse;
20580 else if (INTEGERP (car))
20582 register int lim = XINT (car);
20583 elt = XCDR (elt);
20584 if (lim < 0)
20586 /* Negative int means reduce maximum width. */
20587 if (precision <= 0)
20588 precision = -lim;
20589 else
20590 precision = min (precision, -lim);
20592 else if (lim > 0)
20594 /* Padding specified. Don't let it be more than
20595 current maximum. */
20596 if (precision > 0)
20597 lim = min (precision, lim);
20599 /* If that's more padding than already wanted, queue it.
20600 But don't reduce padding already specified even if
20601 that is beyond the current truncation point. */
20602 field_width = max (lim, field_width);
20604 goto tail_recurse;
20606 else if (STRINGP (car) || CONSP (car))
20608 Lisp_Object halftail = elt;
20609 int len = 0;
20611 while (CONSP (elt)
20612 && (precision <= 0 || n < precision))
20614 n += display_mode_element (it, depth,
20615 /* Do padding only after the last
20616 element in the list. */
20617 (! CONSP (XCDR (elt))
20618 ? field_width - n
20619 : 0),
20620 precision - n, XCAR (elt),
20621 props, risky);
20622 elt = XCDR (elt);
20623 len++;
20624 if ((len & 1) == 0)
20625 halftail = XCDR (halftail);
20626 /* Check for cycle. */
20627 if (EQ (halftail, elt))
20628 break;
20632 break;
20634 default:
20635 invalid:
20636 elt = build_string ("*invalid*");
20637 goto tail_recurse;
20640 /* Pad to FIELD_WIDTH. */
20641 if (field_width > 0 && n < field_width)
20643 switch (mode_line_target)
20645 case MODE_LINE_NOPROP:
20646 case MODE_LINE_TITLE:
20647 n += store_mode_line_noprop ("", field_width - n, 0);
20648 break;
20649 case MODE_LINE_STRING:
20650 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20651 break;
20652 case MODE_LINE_DISPLAY:
20653 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20654 0, 0, 0);
20655 break;
20659 return n;
20662 /* Store a mode-line string element in mode_line_string_list.
20664 If STRING is non-null, display that C string. Otherwise, the Lisp
20665 string LISP_STRING is displayed.
20667 FIELD_WIDTH is the minimum number of output glyphs to produce.
20668 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20669 with spaces. FIELD_WIDTH <= 0 means don't pad.
20671 PRECISION is the maximum number of characters to output from
20672 STRING. PRECISION <= 0 means don't truncate the string.
20674 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20675 properties to the string.
20677 PROPS are the properties to add to the string.
20678 The mode_line_string_face face property is always added to the string.
20681 static int
20682 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20683 int field_width, int precision, Lisp_Object props)
20685 ptrdiff_t len;
20686 int n = 0;
20688 if (string != NULL)
20690 len = strlen (string);
20691 if (precision > 0 && len > precision)
20692 len = precision;
20693 lisp_string = make_string (string, len);
20694 if (NILP (props))
20695 props = mode_line_string_face_prop;
20696 else if (!NILP (mode_line_string_face))
20698 Lisp_Object face = Fplist_get (props, Qface);
20699 props = Fcopy_sequence (props);
20700 if (NILP (face))
20701 face = mode_line_string_face;
20702 else
20703 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20704 props = Fplist_put (props, Qface, face);
20706 Fadd_text_properties (make_number (0), make_number (len),
20707 props, lisp_string);
20709 else
20711 len = XFASTINT (Flength (lisp_string));
20712 if (precision > 0 && len > precision)
20714 len = precision;
20715 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20716 precision = -1;
20718 if (!NILP (mode_line_string_face))
20720 Lisp_Object face;
20721 if (NILP (props))
20722 props = Ftext_properties_at (make_number (0), lisp_string);
20723 face = Fplist_get (props, Qface);
20724 if (NILP (face))
20725 face = mode_line_string_face;
20726 else
20727 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20728 props = Fcons (Qface, Fcons (face, Qnil));
20729 if (copy_string)
20730 lisp_string = Fcopy_sequence (lisp_string);
20732 if (!NILP (props))
20733 Fadd_text_properties (make_number (0), make_number (len),
20734 props, lisp_string);
20737 if (len > 0)
20739 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20740 n += len;
20743 if (field_width > len)
20745 field_width -= len;
20746 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20747 if (!NILP (props))
20748 Fadd_text_properties (make_number (0), make_number (field_width),
20749 props, lisp_string);
20750 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20751 n += field_width;
20754 return n;
20758 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20759 1, 4, 0,
20760 doc: /* Format a string out of a mode line format specification.
20761 First arg FORMAT specifies the mode line format (see `mode-line-format'
20762 for details) to use.
20764 By default, the format is evaluated for the currently selected window.
20766 Optional second arg FACE specifies the face property to put on all
20767 characters for which no face is specified. The value nil means the
20768 default face. The value t means whatever face the window's mode line
20769 currently uses (either `mode-line' or `mode-line-inactive',
20770 depending on whether the window is the selected window or not).
20771 An integer value means the value string has no text
20772 properties.
20774 Optional third and fourth args WINDOW and BUFFER specify the window
20775 and buffer to use as the context for the formatting (defaults
20776 are the selected window and the WINDOW's buffer). */)
20777 (Lisp_Object format, Lisp_Object face,
20778 Lisp_Object window, Lisp_Object buffer)
20780 struct it it;
20781 int len;
20782 struct window *w;
20783 struct buffer *old_buffer = NULL;
20784 int face_id;
20785 int no_props = INTEGERP (face);
20786 ptrdiff_t count = SPECPDL_INDEX ();
20787 Lisp_Object str;
20788 int string_start = 0;
20790 if (NILP (window))
20791 window = selected_window;
20792 CHECK_WINDOW (window);
20793 w = XWINDOW (window);
20795 if (NILP (buffer))
20796 buffer = w->buffer;
20797 CHECK_BUFFER (buffer);
20799 /* Make formatting the modeline a non-op when noninteractive, otherwise
20800 there will be problems later caused by a partially initialized frame. */
20801 if (NILP (format) || noninteractive)
20802 return empty_unibyte_string;
20804 if (no_props)
20805 face = Qnil;
20807 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20808 : EQ (face, Qt) ? (EQ (window, selected_window)
20809 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20810 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20811 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20812 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20813 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20814 : DEFAULT_FACE_ID;
20816 if (XBUFFER (buffer) != current_buffer)
20817 old_buffer = current_buffer;
20819 /* Save things including mode_line_proptrans_alist,
20820 and set that to nil so that we don't alter the outer value. */
20821 record_unwind_protect (unwind_format_mode_line,
20822 format_mode_line_unwind_data
20823 (old_buffer, selected_window, 1));
20824 mode_line_proptrans_alist = Qnil;
20826 Fselect_window (window, Qt);
20827 if (old_buffer)
20828 set_buffer_internal_1 (XBUFFER (buffer));
20830 init_iterator (&it, w, -1, -1, NULL, face_id);
20832 if (no_props)
20834 mode_line_target = MODE_LINE_NOPROP;
20835 mode_line_string_face_prop = Qnil;
20836 mode_line_string_list = Qnil;
20837 string_start = MODE_LINE_NOPROP_LEN (0);
20839 else
20841 mode_line_target = MODE_LINE_STRING;
20842 mode_line_string_list = Qnil;
20843 mode_line_string_face = face;
20844 mode_line_string_face_prop
20845 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20848 push_kboard (FRAME_KBOARD (it.f));
20849 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20850 pop_kboard ();
20852 if (no_props)
20854 len = MODE_LINE_NOPROP_LEN (string_start);
20855 str = make_string (mode_line_noprop_buf + string_start, len);
20857 else
20859 mode_line_string_list = Fnreverse (mode_line_string_list);
20860 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20861 empty_unibyte_string);
20864 unbind_to (count, Qnil);
20865 return str;
20868 /* Write a null-terminated, right justified decimal representation of
20869 the positive integer D to BUF using a minimal field width WIDTH. */
20871 static void
20872 pint2str (register char *buf, register int width, register ptrdiff_t d)
20874 register char *p = buf;
20876 if (d <= 0)
20877 *p++ = '0';
20878 else
20880 while (d > 0)
20882 *p++ = d % 10 + '0';
20883 d /= 10;
20887 for (width -= (int) (p - buf); width > 0; --width)
20888 *p++ = ' ';
20889 *p-- = '\0';
20890 while (p > buf)
20892 d = *buf;
20893 *buf++ = *p;
20894 *p-- = d;
20898 /* Write a null-terminated, right justified decimal and "human
20899 readable" representation of the nonnegative integer D to BUF using
20900 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20902 static const char power_letter[] =
20904 0, /* no letter */
20905 'k', /* kilo */
20906 'M', /* mega */
20907 'G', /* giga */
20908 'T', /* tera */
20909 'P', /* peta */
20910 'E', /* exa */
20911 'Z', /* zetta */
20912 'Y' /* yotta */
20915 static void
20916 pint2hrstr (char *buf, int width, ptrdiff_t d)
20918 /* We aim to represent the nonnegative integer D as
20919 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20920 ptrdiff_t quotient = d;
20921 int remainder = 0;
20922 /* -1 means: do not use TENTHS. */
20923 int tenths = -1;
20924 int exponent = 0;
20926 /* Length of QUOTIENT.TENTHS as a string. */
20927 int length;
20929 char * psuffix;
20930 char * p;
20932 if (1000 <= quotient)
20934 /* Scale to the appropriate EXPONENT. */
20937 remainder = quotient % 1000;
20938 quotient /= 1000;
20939 exponent++;
20941 while (1000 <= quotient);
20943 /* Round to nearest and decide whether to use TENTHS or not. */
20944 if (quotient <= 9)
20946 tenths = remainder / 100;
20947 if (50 <= remainder % 100)
20949 if (tenths < 9)
20950 tenths++;
20951 else
20953 quotient++;
20954 if (quotient == 10)
20955 tenths = -1;
20956 else
20957 tenths = 0;
20961 else
20962 if (500 <= remainder)
20964 if (quotient < 999)
20965 quotient++;
20966 else
20968 quotient = 1;
20969 exponent++;
20970 tenths = 0;
20975 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20976 if (tenths == -1 && quotient <= 99)
20977 if (quotient <= 9)
20978 length = 1;
20979 else
20980 length = 2;
20981 else
20982 length = 3;
20983 p = psuffix = buf + max (width, length);
20985 /* Print EXPONENT. */
20986 *psuffix++ = power_letter[exponent];
20987 *psuffix = '\0';
20989 /* Print TENTHS. */
20990 if (tenths >= 0)
20992 *--p = '0' + tenths;
20993 *--p = '.';
20996 /* Print QUOTIENT. */
20999 int digit = quotient % 10;
21000 *--p = '0' + digit;
21002 while ((quotient /= 10) != 0);
21004 /* Print leading spaces. */
21005 while (buf < p)
21006 *--p = ' ';
21009 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21010 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21011 type of CODING_SYSTEM. Return updated pointer into BUF. */
21013 static unsigned char invalid_eol_type[] = "(*invalid*)";
21015 static char *
21016 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21018 Lisp_Object val;
21019 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21020 const unsigned char *eol_str;
21021 int eol_str_len;
21022 /* The EOL conversion we are using. */
21023 Lisp_Object eoltype;
21025 val = CODING_SYSTEM_SPEC (coding_system);
21026 eoltype = Qnil;
21028 if (!VECTORP (val)) /* Not yet decided. */
21030 *buf++ = multibyte ? '-' : ' ';
21031 if (eol_flag)
21032 eoltype = eol_mnemonic_undecided;
21033 /* Don't mention EOL conversion if it isn't decided. */
21035 else
21037 Lisp_Object attrs;
21038 Lisp_Object eolvalue;
21040 attrs = AREF (val, 0);
21041 eolvalue = AREF (val, 2);
21043 *buf++ = multibyte
21044 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21045 : ' ';
21047 if (eol_flag)
21049 /* The EOL conversion that is normal on this system. */
21051 if (NILP (eolvalue)) /* Not yet decided. */
21052 eoltype = eol_mnemonic_undecided;
21053 else if (VECTORP (eolvalue)) /* Not yet decided. */
21054 eoltype = eol_mnemonic_undecided;
21055 else /* eolvalue is Qunix, Qdos, or Qmac. */
21056 eoltype = (EQ (eolvalue, Qunix)
21057 ? eol_mnemonic_unix
21058 : (EQ (eolvalue, Qdos) == 1
21059 ? eol_mnemonic_dos : eol_mnemonic_mac));
21063 if (eol_flag)
21065 /* Mention the EOL conversion if it is not the usual one. */
21066 if (STRINGP (eoltype))
21068 eol_str = SDATA (eoltype);
21069 eol_str_len = SBYTES (eoltype);
21071 else if (CHARACTERP (eoltype))
21073 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21074 int c = XFASTINT (eoltype);
21075 eol_str_len = CHAR_STRING (c, tmp);
21076 eol_str = tmp;
21078 else
21080 eol_str = invalid_eol_type;
21081 eol_str_len = sizeof (invalid_eol_type) - 1;
21083 memcpy (buf, eol_str, eol_str_len);
21084 buf += eol_str_len;
21087 return buf;
21090 /* Return a string for the output of a mode line %-spec for window W,
21091 generated by character C. FIELD_WIDTH > 0 means pad the string
21092 returned with spaces to that value. Return a Lisp string in
21093 *STRING if the resulting string is taken from that Lisp string.
21095 Note we operate on the current buffer for most purposes,
21096 the exception being w->base_line_pos. */
21098 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21100 static const char *
21101 decode_mode_spec (struct window *w, register int c, int field_width,
21102 Lisp_Object *string)
21104 Lisp_Object obj;
21105 struct frame *f = XFRAME (WINDOW_FRAME (w));
21106 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21107 struct buffer *b = current_buffer;
21109 obj = Qnil;
21110 *string = Qnil;
21112 switch (c)
21114 case '*':
21115 if (!NILP (BVAR (b, read_only)))
21116 return "%";
21117 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21118 return "*";
21119 return "-";
21121 case '+':
21122 /* This differs from %* only for a modified read-only buffer. */
21123 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21124 return "*";
21125 if (!NILP (BVAR (b, read_only)))
21126 return "%";
21127 return "-";
21129 case '&':
21130 /* This differs from %* in ignoring read-only-ness. */
21131 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21132 return "*";
21133 return "-";
21135 case '%':
21136 return "%";
21138 case '[':
21140 int i;
21141 char *p;
21143 if (command_loop_level > 5)
21144 return "[[[... ";
21145 p = decode_mode_spec_buf;
21146 for (i = 0; i < command_loop_level; i++)
21147 *p++ = '[';
21148 *p = 0;
21149 return decode_mode_spec_buf;
21152 case ']':
21154 int i;
21155 char *p;
21157 if (command_loop_level > 5)
21158 return " ...]]]";
21159 p = decode_mode_spec_buf;
21160 for (i = 0; i < command_loop_level; i++)
21161 *p++ = ']';
21162 *p = 0;
21163 return decode_mode_spec_buf;
21166 case '-':
21168 register int i;
21170 /* Let lots_of_dashes be a string of infinite length. */
21171 if (mode_line_target == MODE_LINE_NOPROP ||
21172 mode_line_target == MODE_LINE_STRING)
21173 return "--";
21174 if (field_width <= 0
21175 || field_width > sizeof (lots_of_dashes))
21177 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21178 decode_mode_spec_buf[i] = '-';
21179 decode_mode_spec_buf[i] = '\0';
21180 return decode_mode_spec_buf;
21182 else
21183 return lots_of_dashes;
21186 case 'b':
21187 obj = BVAR (b, name);
21188 break;
21190 case 'c':
21191 /* %c and %l are ignored in `frame-title-format'.
21192 (In redisplay_internal, the frame title is drawn _before_ the
21193 windows are updated, so the stuff which depends on actual
21194 window contents (such as %l) may fail to render properly, or
21195 even crash emacs.) */
21196 if (mode_line_target == MODE_LINE_TITLE)
21197 return "";
21198 else
21200 ptrdiff_t col = current_column ();
21201 w->column_number_displayed = make_number (col);
21202 pint2str (decode_mode_spec_buf, field_width, col);
21203 return decode_mode_spec_buf;
21206 case 'e':
21207 #ifndef SYSTEM_MALLOC
21209 if (NILP (Vmemory_full))
21210 return "";
21211 else
21212 return "!MEM FULL! ";
21214 #else
21215 return "";
21216 #endif
21218 case 'F':
21219 /* %F displays the frame name. */
21220 if (!NILP (f->title))
21221 return SSDATA (f->title);
21222 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21223 return SSDATA (f->name);
21224 return "Emacs";
21226 case 'f':
21227 obj = BVAR (b, filename);
21228 break;
21230 case 'i':
21232 ptrdiff_t size = ZV - BEGV;
21233 pint2str (decode_mode_spec_buf, field_width, size);
21234 return decode_mode_spec_buf;
21237 case 'I':
21239 ptrdiff_t size = ZV - BEGV;
21240 pint2hrstr (decode_mode_spec_buf, field_width, size);
21241 return decode_mode_spec_buf;
21244 case 'l':
21246 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21247 ptrdiff_t topline, nlines, height;
21248 ptrdiff_t junk;
21250 /* %c and %l are ignored in `frame-title-format'. */
21251 if (mode_line_target == MODE_LINE_TITLE)
21252 return "";
21254 startpos = XMARKER (w->start)->charpos;
21255 startpos_byte = marker_byte_position (w->start);
21256 height = WINDOW_TOTAL_LINES (w);
21258 /* If we decided that this buffer isn't suitable for line numbers,
21259 don't forget that too fast. */
21260 if (EQ (w->base_line_pos, w->buffer))
21261 goto no_value;
21262 /* But do forget it, if the window shows a different buffer now. */
21263 else if (BUFFERP (w->base_line_pos))
21264 w->base_line_pos = Qnil;
21266 /* If the buffer is very big, don't waste time. */
21267 if (INTEGERP (Vline_number_display_limit)
21268 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21270 w->base_line_pos = Qnil;
21271 w->base_line_number = Qnil;
21272 goto no_value;
21275 if (INTEGERP (w->base_line_number)
21276 && INTEGERP (w->base_line_pos)
21277 && XFASTINT (w->base_line_pos) <= startpos)
21279 line = XFASTINT (w->base_line_number);
21280 linepos = XFASTINT (w->base_line_pos);
21281 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21283 else
21285 line = 1;
21286 linepos = BUF_BEGV (b);
21287 linepos_byte = BUF_BEGV_BYTE (b);
21290 /* Count lines from base line to window start position. */
21291 nlines = display_count_lines (linepos_byte,
21292 startpos_byte,
21293 startpos, &junk);
21295 topline = nlines + line;
21297 /* Determine a new base line, if the old one is too close
21298 or too far away, or if we did not have one.
21299 "Too close" means it's plausible a scroll-down would
21300 go back past it. */
21301 if (startpos == BUF_BEGV (b))
21303 w->base_line_number = make_number (topline);
21304 w->base_line_pos = make_number (BUF_BEGV (b));
21306 else if (nlines < height + 25 || nlines > height * 3 + 50
21307 || linepos == BUF_BEGV (b))
21309 ptrdiff_t limit = BUF_BEGV (b);
21310 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21311 ptrdiff_t position;
21312 ptrdiff_t distance =
21313 (height * 2 + 30) * line_number_display_limit_width;
21315 if (startpos - distance > limit)
21317 limit = startpos - distance;
21318 limit_byte = CHAR_TO_BYTE (limit);
21321 nlines = display_count_lines (startpos_byte,
21322 limit_byte,
21323 - (height * 2 + 30),
21324 &position);
21325 /* If we couldn't find the lines we wanted within
21326 line_number_display_limit_width chars per line,
21327 give up on line numbers for this window. */
21328 if (position == limit_byte && limit == startpos - distance)
21330 w->base_line_pos = w->buffer;
21331 w->base_line_number = Qnil;
21332 goto no_value;
21335 w->base_line_number = make_number (topline - nlines);
21336 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21339 /* Now count lines from the start pos to point. */
21340 nlines = display_count_lines (startpos_byte,
21341 PT_BYTE, PT, &junk);
21343 /* Record that we did display the line number. */
21344 line_number_displayed = 1;
21346 /* Make the string to show. */
21347 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21348 return decode_mode_spec_buf;
21349 no_value:
21351 char* p = decode_mode_spec_buf;
21352 int pad = field_width - 2;
21353 while (pad-- > 0)
21354 *p++ = ' ';
21355 *p++ = '?';
21356 *p++ = '?';
21357 *p = '\0';
21358 return decode_mode_spec_buf;
21361 break;
21363 case 'm':
21364 obj = BVAR (b, mode_name);
21365 break;
21367 case 'n':
21368 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21369 return " Narrow";
21370 break;
21372 case 'p':
21374 ptrdiff_t pos = marker_position (w->start);
21375 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21377 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21379 if (pos <= BUF_BEGV (b))
21380 return "All";
21381 else
21382 return "Bottom";
21384 else if (pos <= BUF_BEGV (b))
21385 return "Top";
21386 else
21388 if (total > 1000000)
21389 /* Do it differently for a large value, to avoid overflow. */
21390 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21391 else
21392 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21393 /* We can't normally display a 3-digit number,
21394 so get us a 2-digit number that is close. */
21395 if (total == 100)
21396 total = 99;
21397 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21398 return decode_mode_spec_buf;
21402 /* Display percentage of size above the bottom of the screen. */
21403 case 'P':
21405 ptrdiff_t toppos = marker_position (w->start);
21406 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21407 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21409 if (botpos >= BUF_ZV (b))
21411 if (toppos <= BUF_BEGV (b))
21412 return "All";
21413 else
21414 return "Bottom";
21416 else
21418 if (total > 1000000)
21419 /* Do it differently for a large value, to avoid overflow. */
21420 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21421 else
21422 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21423 /* We can't normally display a 3-digit number,
21424 so get us a 2-digit number that is close. */
21425 if (total == 100)
21426 total = 99;
21427 if (toppos <= BUF_BEGV (b))
21428 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21429 else
21430 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21431 return decode_mode_spec_buf;
21435 case 's':
21436 /* status of process */
21437 obj = Fget_buffer_process (Fcurrent_buffer ());
21438 if (NILP (obj))
21439 return "no process";
21440 #ifndef MSDOS
21441 obj = Fsymbol_name (Fprocess_status (obj));
21442 #endif
21443 break;
21445 case '@':
21447 ptrdiff_t count = inhibit_garbage_collection ();
21448 Lisp_Object val = call1 (intern ("file-remote-p"),
21449 BVAR (current_buffer, directory));
21450 unbind_to (count, Qnil);
21452 if (NILP (val))
21453 return "-";
21454 else
21455 return "@";
21458 case 't': /* indicate TEXT or BINARY */
21459 return "T";
21461 case 'z':
21462 /* coding-system (not including end-of-line format) */
21463 case 'Z':
21464 /* coding-system (including end-of-line type) */
21466 int eol_flag = (c == 'Z');
21467 char *p = decode_mode_spec_buf;
21469 if (! FRAME_WINDOW_P (f))
21471 /* No need to mention EOL here--the terminal never needs
21472 to do EOL conversion. */
21473 p = decode_mode_spec_coding (CODING_ID_NAME
21474 (FRAME_KEYBOARD_CODING (f)->id),
21475 p, 0);
21476 p = decode_mode_spec_coding (CODING_ID_NAME
21477 (FRAME_TERMINAL_CODING (f)->id),
21478 p, 0);
21480 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21481 p, eol_flag);
21483 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21484 #ifdef subprocesses
21485 obj = Fget_buffer_process (Fcurrent_buffer ());
21486 if (PROCESSP (obj))
21488 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21489 p, eol_flag);
21490 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21491 p, eol_flag);
21493 #endif /* subprocesses */
21494 #endif /* 0 */
21495 *p = 0;
21496 return decode_mode_spec_buf;
21500 if (STRINGP (obj))
21502 *string = obj;
21503 return SSDATA (obj);
21505 else
21506 return "";
21510 /* Count up to COUNT lines starting from START_BYTE.
21511 But don't go beyond LIMIT_BYTE.
21512 Return the number of lines thus found (always nonnegative).
21514 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21516 static ptrdiff_t
21517 display_count_lines (ptrdiff_t start_byte,
21518 ptrdiff_t limit_byte, ptrdiff_t count,
21519 ptrdiff_t *byte_pos_ptr)
21521 register unsigned char *cursor;
21522 unsigned char *base;
21524 register ptrdiff_t ceiling;
21525 register unsigned char *ceiling_addr;
21526 ptrdiff_t orig_count = count;
21528 /* If we are not in selective display mode,
21529 check only for newlines. */
21530 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21531 && !INTEGERP (BVAR (current_buffer, selective_display)));
21533 if (count > 0)
21535 while (start_byte < limit_byte)
21537 ceiling = BUFFER_CEILING_OF (start_byte);
21538 ceiling = min (limit_byte - 1, ceiling);
21539 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21540 base = (cursor = BYTE_POS_ADDR (start_byte));
21541 while (1)
21543 if (selective_display)
21544 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21546 else
21547 while (*cursor != '\n' && ++cursor != ceiling_addr)
21550 if (cursor != ceiling_addr)
21552 if (--count == 0)
21554 start_byte += cursor - base + 1;
21555 *byte_pos_ptr = start_byte;
21556 return orig_count;
21558 else
21559 if (++cursor == ceiling_addr)
21560 break;
21562 else
21563 break;
21565 start_byte += cursor - base;
21568 else
21570 while (start_byte > limit_byte)
21572 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21573 ceiling = max (limit_byte, ceiling);
21574 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21575 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21576 while (1)
21578 if (selective_display)
21579 while (--cursor != ceiling_addr
21580 && *cursor != '\n' && *cursor != 015)
21582 else
21583 while (--cursor != ceiling_addr && *cursor != '\n')
21586 if (cursor != ceiling_addr)
21588 if (++count == 0)
21590 start_byte += cursor - base + 1;
21591 *byte_pos_ptr = start_byte;
21592 /* When scanning backwards, we should
21593 not count the newline posterior to which we stop. */
21594 return - orig_count - 1;
21597 else
21598 break;
21600 /* Here we add 1 to compensate for the last decrement
21601 of CURSOR, which took it past the valid range. */
21602 start_byte += cursor - base + 1;
21606 *byte_pos_ptr = limit_byte;
21608 if (count < 0)
21609 return - orig_count + count;
21610 return orig_count - count;
21616 /***********************************************************************
21617 Displaying strings
21618 ***********************************************************************/
21620 /* Display a NUL-terminated string, starting with index START.
21622 If STRING is non-null, display that C string. Otherwise, the Lisp
21623 string LISP_STRING is displayed. There's a case that STRING is
21624 non-null and LISP_STRING is not nil. It means STRING is a string
21625 data of LISP_STRING. In that case, we display LISP_STRING while
21626 ignoring its text properties.
21628 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21629 FACE_STRING. Display STRING or LISP_STRING with the face at
21630 FACE_STRING_POS in FACE_STRING:
21632 Display the string in the environment given by IT, but use the
21633 standard display table, temporarily.
21635 FIELD_WIDTH is the minimum number of output glyphs to produce.
21636 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21637 with spaces. If STRING has more characters, more than FIELD_WIDTH
21638 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21640 PRECISION is the maximum number of characters to output from
21641 STRING. PRECISION < 0 means don't truncate the string.
21643 This is roughly equivalent to printf format specifiers:
21645 FIELD_WIDTH PRECISION PRINTF
21646 ----------------------------------------
21647 -1 -1 %s
21648 -1 10 %.10s
21649 10 -1 %10s
21650 20 10 %20.10s
21652 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21653 display them, and < 0 means obey the current buffer's value of
21654 enable_multibyte_characters.
21656 Value is the number of columns displayed. */
21658 static int
21659 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21660 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21661 int field_width, int precision, int max_x, int multibyte)
21663 int hpos_at_start = it->hpos;
21664 int saved_face_id = it->face_id;
21665 struct glyph_row *row = it->glyph_row;
21666 ptrdiff_t it_charpos;
21668 /* Initialize the iterator IT for iteration over STRING beginning
21669 with index START. */
21670 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21671 precision, field_width, multibyte);
21672 if (string && STRINGP (lisp_string))
21673 /* LISP_STRING is the one returned by decode_mode_spec. We should
21674 ignore its text properties. */
21675 it->stop_charpos = it->end_charpos;
21677 /* If displaying STRING, set up the face of the iterator from
21678 FACE_STRING, if that's given. */
21679 if (STRINGP (face_string))
21681 ptrdiff_t endptr;
21682 struct face *face;
21684 it->face_id
21685 = face_at_string_position (it->w, face_string, face_string_pos,
21686 0, it->region_beg_charpos,
21687 it->region_end_charpos,
21688 &endptr, it->base_face_id, 0);
21689 face = FACE_FROM_ID (it->f, it->face_id);
21690 it->face_box_p = face->box != FACE_NO_BOX;
21693 /* Set max_x to the maximum allowed X position. Don't let it go
21694 beyond the right edge of the window. */
21695 if (max_x <= 0)
21696 max_x = it->last_visible_x;
21697 else
21698 max_x = min (max_x, it->last_visible_x);
21700 /* Skip over display elements that are not visible. because IT->w is
21701 hscrolled. */
21702 if (it->current_x < it->first_visible_x)
21703 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21704 MOVE_TO_POS | MOVE_TO_X);
21706 row->ascent = it->max_ascent;
21707 row->height = it->max_ascent + it->max_descent;
21708 row->phys_ascent = it->max_phys_ascent;
21709 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21710 row->extra_line_spacing = it->max_extra_line_spacing;
21712 if (STRINGP (it->string))
21713 it_charpos = IT_STRING_CHARPOS (*it);
21714 else
21715 it_charpos = IT_CHARPOS (*it);
21717 /* This condition is for the case that we are called with current_x
21718 past last_visible_x. */
21719 while (it->current_x < max_x)
21721 int x_before, x, n_glyphs_before, i, nglyphs;
21723 /* Get the next display element. */
21724 if (!get_next_display_element (it))
21725 break;
21727 /* Produce glyphs. */
21728 x_before = it->current_x;
21729 n_glyphs_before = row->used[TEXT_AREA];
21730 PRODUCE_GLYPHS (it);
21732 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21733 i = 0;
21734 x = x_before;
21735 while (i < nglyphs)
21737 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21739 if (it->line_wrap != TRUNCATE
21740 && x + glyph->pixel_width > max_x)
21742 /* End of continued line or max_x reached. */
21743 if (CHAR_GLYPH_PADDING_P (*glyph))
21745 /* A wide character is unbreakable. */
21746 if (row->reversed_p)
21747 unproduce_glyphs (it, row->used[TEXT_AREA]
21748 - n_glyphs_before);
21749 row->used[TEXT_AREA] = n_glyphs_before;
21750 it->current_x = x_before;
21752 else
21754 if (row->reversed_p)
21755 unproduce_glyphs (it, row->used[TEXT_AREA]
21756 - (n_glyphs_before + i));
21757 row->used[TEXT_AREA] = n_glyphs_before + i;
21758 it->current_x = x;
21760 break;
21762 else if (x + glyph->pixel_width >= it->first_visible_x)
21764 /* Glyph is at least partially visible. */
21765 ++it->hpos;
21766 if (x < it->first_visible_x)
21767 row->x = x - it->first_visible_x;
21769 else
21771 /* Glyph is off the left margin of the display area.
21772 Should not happen. */
21773 abort ();
21776 row->ascent = max (row->ascent, it->max_ascent);
21777 row->height = max (row->height, it->max_ascent + it->max_descent);
21778 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21779 row->phys_height = max (row->phys_height,
21780 it->max_phys_ascent + it->max_phys_descent);
21781 row->extra_line_spacing = max (row->extra_line_spacing,
21782 it->max_extra_line_spacing);
21783 x += glyph->pixel_width;
21784 ++i;
21787 /* Stop if max_x reached. */
21788 if (i < nglyphs)
21789 break;
21791 /* Stop at line ends. */
21792 if (ITERATOR_AT_END_OF_LINE_P (it))
21794 it->continuation_lines_width = 0;
21795 break;
21798 set_iterator_to_next (it, 1);
21799 if (STRINGP (it->string))
21800 it_charpos = IT_STRING_CHARPOS (*it);
21801 else
21802 it_charpos = IT_CHARPOS (*it);
21804 /* Stop if truncating at the right edge. */
21805 if (it->line_wrap == TRUNCATE
21806 && it->current_x >= it->last_visible_x)
21808 /* Add truncation mark, but don't do it if the line is
21809 truncated at a padding space. */
21810 if (it_charpos < it->string_nchars)
21812 if (!FRAME_WINDOW_P (it->f))
21814 int ii, n;
21816 if (it->current_x > it->last_visible_x)
21818 if (!row->reversed_p)
21820 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21821 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21822 break;
21824 else
21826 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21827 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21828 break;
21829 unproduce_glyphs (it, ii + 1);
21830 ii = row->used[TEXT_AREA] - (ii + 1);
21832 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21834 row->used[TEXT_AREA] = ii;
21835 produce_special_glyphs (it, IT_TRUNCATION);
21838 produce_special_glyphs (it, IT_TRUNCATION);
21840 row->truncated_on_right_p = 1;
21842 break;
21846 /* Maybe insert a truncation at the left. */
21847 if (it->first_visible_x
21848 && it_charpos > 0)
21850 if (!FRAME_WINDOW_P (it->f))
21851 insert_left_trunc_glyphs (it);
21852 row->truncated_on_left_p = 1;
21855 it->face_id = saved_face_id;
21857 /* Value is number of columns displayed. */
21858 return it->hpos - hpos_at_start;
21863 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21864 appears as an element of LIST or as the car of an element of LIST.
21865 If PROPVAL is a list, compare each element against LIST in that
21866 way, and return 1/2 if any element of PROPVAL is found in LIST.
21867 Otherwise return 0. This function cannot quit.
21868 The return value is 2 if the text is invisible but with an ellipsis
21869 and 1 if it's invisible and without an ellipsis. */
21872 invisible_p (register Lisp_Object propval, Lisp_Object list)
21874 register Lisp_Object tail, proptail;
21876 for (tail = list; CONSP (tail); tail = XCDR (tail))
21878 register Lisp_Object tem;
21879 tem = XCAR (tail);
21880 if (EQ (propval, tem))
21881 return 1;
21882 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21883 return NILP (XCDR (tem)) ? 1 : 2;
21886 if (CONSP (propval))
21888 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21890 Lisp_Object propelt;
21891 propelt = XCAR (proptail);
21892 for (tail = list; CONSP (tail); tail = XCDR (tail))
21894 register Lisp_Object tem;
21895 tem = XCAR (tail);
21896 if (EQ (propelt, tem))
21897 return 1;
21898 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21899 return NILP (XCDR (tem)) ? 1 : 2;
21904 return 0;
21907 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21908 doc: /* Non-nil if the property makes the text invisible.
21909 POS-OR-PROP can be a marker or number, in which case it is taken to be
21910 a position in the current buffer and the value of the `invisible' property
21911 is checked; or it can be some other value, which is then presumed to be the
21912 value of the `invisible' property of the text of interest.
21913 The non-nil value returned can be t for truly invisible text or something
21914 else if the text is replaced by an ellipsis. */)
21915 (Lisp_Object pos_or_prop)
21917 Lisp_Object prop
21918 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21919 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21920 : pos_or_prop);
21921 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21922 return (invis == 0 ? Qnil
21923 : invis == 1 ? Qt
21924 : make_number (invis));
21927 /* Calculate a width or height in pixels from a specification using
21928 the following elements:
21930 SPEC ::=
21931 NUM - a (fractional) multiple of the default font width/height
21932 (NUM) - specifies exactly NUM pixels
21933 UNIT - a fixed number of pixels, see below.
21934 ELEMENT - size of a display element in pixels, see below.
21935 (NUM . SPEC) - equals NUM * SPEC
21936 (+ SPEC SPEC ...) - add pixel values
21937 (- SPEC SPEC ...) - subtract pixel values
21938 (- SPEC) - negate pixel value
21940 NUM ::=
21941 INT or FLOAT - a number constant
21942 SYMBOL - use symbol's (buffer local) variable binding.
21944 UNIT ::=
21945 in - pixels per inch *)
21946 mm - pixels per 1/1000 meter *)
21947 cm - pixels per 1/100 meter *)
21948 width - width of current font in pixels.
21949 height - height of current font in pixels.
21951 *) using the ratio(s) defined in display-pixels-per-inch.
21953 ELEMENT ::=
21955 left-fringe - left fringe width in pixels
21956 right-fringe - right fringe width in pixels
21958 left-margin - left margin width in pixels
21959 right-margin - right margin width in pixels
21961 scroll-bar - scroll-bar area width in pixels
21963 Examples:
21965 Pixels corresponding to 5 inches:
21966 (5 . in)
21968 Total width of non-text areas on left side of window (if scroll-bar is on left):
21969 '(space :width (+ left-fringe left-margin scroll-bar))
21971 Align to first text column (in header line):
21972 '(space :align-to 0)
21974 Align to middle of text area minus half the width of variable `my-image'
21975 containing a loaded image:
21976 '(space :align-to (0.5 . (- text my-image)))
21978 Width of left margin minus width of 1 character in the default font:
21979 '(space :width (- left-margin 1))
21981 Width of left margin minus width of 2 characters in the current font:
21982 '(space :width (- left-margin (2 . width)))
21984 Center 1 character over left-margin (in header line):
21985 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21987 Different ways to express width of left fringe plus left margin minus one pixel:
21988 '(space :width (- (+ left-fringe left-margin) (1)))
21989 '(space :width (+ left-fringe left-margin (- (1))))
21990 '(space :width (+ left-fringe left-margin (-1)))
21994 #define NUMVAL(X) \
21995 ((INTEGERP (X) || FLOATP (X)) \
21996 ? XFLOATINT (X) \
21997 : - 1)
21999 static int
22000 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22001 struct font *font, int width_p, int *align_to)
22003 double pixels;
22005 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22006 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22008 if (NILP (prop))
22009 return OK_PIXELS (0);
22011 xassert (FRAME_LIVE_P (it->f));
22013 if (SYMBOLP (prop))
22015 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22017 char *unit = SSDATA (SYMBOL_NAME (prop));
22019 if (unit[0] == 'i' && unit[1] == 'n')
22020 pixels = 1.0;
22021 else if (unit[0] == 'm' && unit[1] == 'm')
22022 pixels = 25.4;
22023 else if (unit[0] == 'c' && unit[1] == 'm')
22024 pixels = 2.54;
22025 else
22026 pixels = 0;
22027 if (pixels > 0)
22029 double ppi;
22030 #ifdef HAVE_WINDOW_SYSTEM
22031 if (FRAME_WINDOW_P (it->f)
22032 && (ppi = (width_p
22033 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22034 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22035 ppi > 0))
22036 return OK_PIXELS (ppi / pixels);
22037 #endif
22039 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22040 || (CONSP (Vdisplay_pixels_per_inch)
22041 && (ppi = (width_p
22042 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22043 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22044 ppi > 0)))
22045 return OK_PIXELS (ppi / pixels);
22047 return 0;
22051 #ifdef HAVE_WINDOW_SYSTEM
22052 if (EQ (prop, Qheight))
22053 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22054 if (EQ (prop, Qwidth))
22055 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22056 #else
22057 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22058 return OK_PIXELS (1);
22059 #endif
22061 if (EQ (prop, Qtext))
22062 return OK_PIXELS (width_p
22063 ? window_box_width (it->w, TEXT_AREA)
22064 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22066 if (align_to && *align_to < 0)
22068 *res = 0;
22069 if (EQ (prop, Qleft))
22070 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22071 if (EQ (prop, Qright))
22072 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22073 if (EQ (prop, Qcenter))
22074 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22075 + window_box_width (it->w, TEXT_AREA) / 2);
22076 if (EQ (prop, Qleft_fringe))
22077 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22078 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22079 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22080 if (EQ (prop, Qright_fringe))
22081 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22082 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22083 : window_box_right_offset (it->w, TEXT_AREA));
22084 if (EQ (prop, Qleft_margin))
22085 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22086 if (EQ (prop, Qright_margin))
22087 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22088 if (EQ (prop, Qscroll_bar))
22089 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22091 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22092 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22093 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22094 : 0)));
22096 else
22098 if (EQ (prop, Qleft_fringe))
22099 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22100 if (EQ (prop, Qright_fringe))
22101 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22102 if (EQ (prop, Qleft_margin))
22103 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22104 if (EQ (prop, Qright_margin))
22105 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22106 if (EQ (prop, Qscroll_bar))
22107 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22110 prop = buffer_local_value_1 (prop, it->w->buffer);
22111 if (EQ (prop, Qunbound))
22112 prop = Qnil;
22115 if (INTEGERP (prop) || FLOATP (prop))
22117 int base_unit = (width_p
22118 ? FRAME_COLUMN_WIDTH (it->f)
22119 : FRAME_LINE_HEIGHT (it->f));
22120 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22123 if (CONSP (prop))
22125 Lisp_Object car = XCAR (prop);
22126 Lisp_Object cdr = XCDR (prop);
22128 if (SYMBOLP (car))
22130 #ifdef HAVE_WINDOW_SYSTEM
22131 if (FRAME_WINDOW_P (it->f)
22132 && valid_image_p (prop))
22134 ptrdiff_t id = lookup_image (it->f, prop);
22135 struct image *img = IMAGE_FROM_ID (it->f, id);
22137 return OK_PIXELS (width_p ? img->width : img->height);
22139 #endif
22140 if (EQ (car, Qplus) || EQ (car, Qminus))
22142 int first = 1;
22143 double px;
22145 pixels = 0;
22146 while (CONSP (cdr))
22148 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22149 font, width_p, align_to))
22150 return 0;
22151 if (first)
22152 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22153 else
22154 pixels += px;
22155 cdr = XCDR (cdr);
22157 if (EQ (car, Qminus))
22158 pixels = -pixels;
22159 return OK_PIXELS (pixels);
22162 car = buffer_local_value_1 (car, it->w->buffer);
22163 if (EQ (car, Qunbound))
22164 car = Qnil;
22167 if (INTEGERP (car) || FLOATP (car))
22169 double fact;
22170 pixels = XFLOATINT (car);
22171 if (NILP (cdr))
22172 return OK_PIXELS (pixels);
22173 if (calc_pixel_width_or_height (&fact, it, cdr,
22174 font, width_p, align_to))
22175 return OK_PIXELS (pixels * fact);
22176 return 0;
22179 return 0;
22182 return 0;
22186 /***********************************************************************
22187 Glyph Display
22188 ***********************************************************************/
22190 #ifdef HAVE_WINDOW_SYSTEM
22192 #if GLYPH_DEBUG
22194 void
22195 dump_glyph_string (struct glyph_string *s)
22197 fprintf (stderr, "glyph string\n");
22198 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22199 s->x, s->y, s->width, s->height);
22200 fprintf (stderr, " ybase = %d\n", s->ybase);
22201 fprintf (stderr, " hl = %d\n", s->hl);
22202 fprintf (stderr, " left overhang = %d, right = %d\n",
22203 s->left_overhang, s->right_overhang);
22204 fprintf (stderr, " nchars = %d\n", s->nchars);
22205 fprintf (stderr, " extends to end of line = %d\n",
22206 s->extends_to_end_of_line_p);
22207 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22208 fprintf (stderr, " bg width = %d\n", s->background_width);
22211 #endif /* GLYPH_DEBUG */
22213 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22214 of XChar2b structures for S; it can't be allocated in
22215 init_glyph_string because it must be allocated via `alloca'. W
22216 is the window on which S is drawn. ROW and AREA are the glyph row
22217 and area within the row from which S is constructed. START is the
22218 index of the first glyph structure covered by S. HL is a
22219 face-override for drawing S. */
22221 #ifdef HAVE_NTGUI
22222 #define OPTIONAL_HDC(hdc) HDC hdc,
22223 #define DECLARE_HDC(hdc) HDC hdc;
22224 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22225 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22226 #endif
22228 #ifndef OPTIONAL_HDC
22229 #define OPTIONAL_HDC(hdc)
22230 #define DECLARE_HDC(hdc)
22231 #define ALLOCATE_HDC(hdc, f)
22232 #define RELEASE_HDC(hdc, f)
22233 #endif
22235 static void
22236 init_glyph_string (struct glyph_string *s,
22237 OPTIONAL_HDC (hdc)
22238 XChar2b *char2b, struct window *w, struct glyph_row *row,
22239 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22241 memset (s, 0, sizeof *s);
22242 s->w = w;
22243 s->f = XFRAME (w->frame);
22244 #ifdef HAVE_NTGUI
22245 s->hdc = hdc;
22246 #endif
22247 s->display = FRAME_X_DISPLAY (s->f);
22248 s->window = FRAME_X_WINDOW (s->f);
22249 s->char2b = char2b;
22250 s->hl = hl;
22251 s->row = row;
22252 s->area = area;
22253 s->first_glyph = row->glyphs[area] + start;
22254 s->height = row->height;
22255 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22256 s->ybase = s->y + row->ascent;
22260 /* Append the list of glyph strings with head H and tail T to the list
22261 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22263 static inline void
22264 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22265 struct glyph_string *h, struct glyph_string *t)
22267 if (h)
22269 if (*head)
22270 (*tail)->next = h;
22271 else
22272 *head = h;
22273 h->prev = *tail;
22274 *tail = t;
22279 /* Prepend the list of glyph strings with head H and tail T to the
22280 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22281 result. */
22283 static inline void
22284 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22285 struct glyph_string *h, struct glyph_string *t)
22287 if (h)
22289 if (*head)
22290 (*head)->prev = t;
22291 else
22292 *tail = t;
22293 t->next = *head;
22294 *head = h;
22299 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22300 Set *HEAD and *TAIL to the resulting list. */
22302 static inline void
22303 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22304 struct glyph_string *s)
22306 s->next = s->prev = NULL;
22307 append_glyph_string_lists (head, tail, s, s);
22311 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22312 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22313 make sure that X resources for the face returned are allocated.
22314 Value is a pointer to a realized face that is ready for display if
22315 DISPLAY_P is non-zero. */
22317 static inline struct face *
22318 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22319 XChar2b *char2b, int display_p)
22321 struct face *face = FACE_FROM_ID (f, face_id);
22323 if (face->font)
22325 unsigned code = face->font->driver->encode_char (face->font, c);
22327 if (code != FONT_INVALID_CODE)
22328 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22329 else
22330 STORE_XCHAR2B (char2b, 0, 0);
22333 /* Make sure X resources of the face are allocated. */
22334 #ifdef HAVE_X_WINDOWS
22335 if (display_p)
22336 #endif
22338 xassert (face != NULL);
22339 PREPARE_FACE_FOR_DISPLAY (f, face);
22342 return face;
22346 /* Get face and two-byte form of character glyph GLYPH on frame F.
22347 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22348 a pointer to a realized face that is ready for display. */
22350 static inline struct face *
22351 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22352 XChar2b *char2b, int *two_byte_p)
22354 struct face *face;
22356 xassert (glyph->type == CHAR_GLYPH);
22357 face = FACE_FROM_ID (f, glyph->face_id);
22359 if (two_byte_p)
22360 *two_byte_p = 0;
22362 if (face->font)
22364 unsigned code;
22366 if (CHAR_BYTE8_P (glyph->u.ch))
22367 code = CHAR_TO_BYTE8 (glyph->u.ch);
22368 else
22369 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22371 if (code != FONT_INVALID_CODE)
22372 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22373 else
22374 STORE_XCHAR2B (char2b, 0, 0);
22377 /* Make sure X resources of the face are allocated. */
22378 xassert (face != NULL);
22379 PREPARE_FACE_FOR_DISPLAY (f, face);
22380 return face;
22384 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22385 Return 1 if FONT has a glyph for C, otherwise return 0. */
22387 static inline int
22388 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22390 unsigned code;
22392 if (CHAR_BYTE8_P (c))
22393 code = CHAR_TO_BYTE8 (c);
22394 else
22395 code = font->driver->encode_char (font, c);
22397 if (code == FONT_INVALID_CODE)
22398 return 0;
22399 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22400 return 1;
22404 /* Fill glyph string S with composition components specified by S->cmp.
22406 BASE_FACE is the base face of the composition.
22407 S->cmp_from is the index of the first component for S.
22409 OVERLAPS non-zero means S should draw the foreground only, and use
22410 its physical height for clipping. See also draw_glyphs.
22412 Value is the index of a component not in S. */
22414 static int
22415 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22416 int overlaps)
22418 int i;
22419 /* For all glyphs of this composition, starting at the offset
22420 S->cmp_from, until we reach the end of the definition or encounter a
22421 glyph that requires the different face, add it to S. */
22422 struct face *face;
22424 xassert (s);
22426 s->for_overlaps = overlaps;
22427 s->face = NULL;
22428 s->font = NULL;
22429 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22431 int c = COMPOSITION_GLYPH (s->cmp, i);
22433 /* TAB in a composition means display glyphs with padding space
22434 on the left or right. */
22435 if (c != '\t')
22437 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22438 -1, Qnil);
22440 face = get_char_face_and_encoding (s->f, c, face_id,
22441 s->char2b + i, 1);
22442 if (face)
22444 if (! s->face)
22446 s->face = face;
22447 s->font = s->face->font;
22449 else if (s->face != face)
22450 break;
22453 ++s->nchars;
22455 s->cmp_to = i;
22457 if (s->face == NULL)
22459 s->face = base_face->ascii_face;
22460 s->font = s->face->font;
22463 /* All glyph strings for the same composition has the same width,
22464 i.e. the width set for the first component of the composition. */
22465 s->width = s->first_glyph->pixel_width;
22467 /* If the specified font could not be loaded, use the frame's
22468 default font, but record the fact that we couldn't load it in
22469 the glyph string so that we can draw rectangles for the
22470 characters of the glyph string. */
22471 if (s->font == NULL)
22473 s->font_not_found_p = 1;
22474 s->font = FRAME_FONT (s->f);
22477 /* Adjust base line for subscript/superscript text. */
22478 s->ybase += s->first_glyph->voffset;
22480 /* This glyph string must always be drawn with 16-bit functions. */
22481 s->two_byte_p = 1;
22483 return s->cmp_to;
22486 static int
22487 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22488 int start, int end, int overlaps)
22490 struct glyph *glyph, *last;
22491 Lisp_Object lgstring;
22492 int i;
22494 s->for_overlaps = overlaps;
22495 glyph = s->row->glyphs[s->area] + start;
22496 last = s->row->glyphs[s->area] + end;
22497 s->cmp_id = glyph->u.cmp.id;
22498 s->cmp_from = glyph->slice.cmp.from;
22499 s->cmp_to = glyph->slice.cmp.to + 1;
22500 s->face = FACE_FROM_ID (s->f, face_id);
22501 lgstring = composition_gstring_from_id (s->cmp_id);
22502 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22503 glyph++;
22504 while (glyph < last
22505 && glyph->u.cmp.automatic
22506 && glyph->u.cmp.id == s->cmp_id
22507 && s->cmp_to == glyph->slice.cmp.from)
22508 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22510 for (i = s->cmp_from; i < s->cmp_to; i++)
22512 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22513 unsigned code = LGLYPH_CODE (lglyph);
22515 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22517 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22518 return glyph - s->row->glyphs[s->area];
22522 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22523 See the comment of fill_glyph_string for arguments.
22524 Value is the index of the first glyph not in S. */
22527 static int
22528 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22529 int start, int end, int overlaps)
22531 struct glyph *glyph, *last;
22532 int voffset;
22534 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22535 s->for_overlaps = overlaps;
22536 glyph = s->row->glyphs[s->area] + start;
22537 last = s->row->glyphs[s->area] + end;
22538 voffset = glyph->voffset;
22539 s->face = FACE_FROM_ID (s->f, face_id);
22540 s->font = s->face->font;
22541 s->nchars = 1;
22542 s->width = glyph->pixel_width;
22543 glyph++;
22544 while (glyph < last
22545 && glyph->type == GLYPHLESS_GLYPH
22546 && glyph->voffset == voffset
22547 && glyph->face_id == face_id)
22549 s->nchars++;
22550 s->width += glyph->pixel_width;
22551 glyph++;
22553 s->ybase += voffset;
22554 return glyph - s->row->glyphs[s->area];
22558 /* Fill glyph string S from a sequence of character glyphs.
22560 FACE_ID is the face id of the string. START is the index of the
22561 first glyph to consider, END is the index of the last + 1.
22562 OVERLAPS non-zero means S should draw the foreground only, and use
22563 its physical height for clipping. See also draw_glyphs.
22565 Value is the index of the first glyph not in S. */
22567 static int
22568 fill_glyph_string (struct glyph_string *s, int face_id,
22569 int start, int end, int overlaps)
22571 struct glyph *glyph, *last;
22572 int voffset;
22573 int glyph_not_available_p;
22575 xassert (s->f == XFRAME (s->w->frame));
22576 xassert (s->nchars == 0);
22577 xassert (start >= 0 && end > start);
22579 s->for_overlaps = overlaps;
22580 glyph = s->row->glyphs[s->area] + start;
22581 last = s->row->glyphs[s->area] + end;
22582 voffset = glyph->voffset;
22583 s->padding_p = glyph->padding_p;
22584 glyph_not_available_p = glyph->glyph_not_available_p;
22586 while (glyph < last
22587 && glyph->type == CHAR_GLYPH
22588 && glyph->voffset == voffset
22589 /* Same face id implies same font, nowadays. */
22590 && glyph->face_id == face_id
22591 && glyph->glyph_not_available_p == glyph_not_available_p)
22593 int two_byte_p;
22595 s->face = get_glyph_face_and_encoding (s->f, glyph,
22596 s->char2b + s->nchars,
22597 &two_byte_p);
22598 s->two_byte_p = two_byte_p;
22599 ++s->nchars;
22600 xassert (s->nchars <= end - start);
22601 s->width += glyph->pixel_width;
22602 if (glyph++->padding_p != s->padding_p)
22603 break;
22606 s->font = s->face->font;
22608 /* If the specified font could not be loaded, use the frame's font,
22609 but record the fact that we couldn't load it in
22610 S->font_not_found_p so that we can draw rectangles for the
22611 characters of the glyph string. */
22612 if (s->font == NULL || glyph_not_available_p)
22614 s->font_not_found_p = 1;
22615 s->font = FRAME_FONT (s->f);
22618 /* Adjust base line for subscript/superscript text. */
22619 s->ybase += voffset;
22621 xassert (s->face && s->face->gc);
22622 return glyph - s->row->glyphs[s->area];
22626 /* Fill glyph string S from image glyph S->first_glyph. */
22628 static void
22629 fill_image_glyph_string (struct glyph_string *s)
22631 xassert (s->first_glyph->type == IMAGE_GLYPH);
22632 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22633 xassert (s->img);
22634 s->slice = s->first_glyph->slice.img;
22635 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22636 s->font = s->face->font;
22637 s->width = s->first_glyph->pixel_width;
22639 /* Adjust base line for subscript/superscript text. */
22640 s->ybase += s->first_glyph->voffset;
22644 /* Fill glyph string S from a sequence of stretch glyphs.
22646 START is the index of the first glyph to consider,
22647 END is the index of the last + 1.
22649 Value is the index of the first glyph not in S. */
22651 static int
22652 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22654 struct glyph *glyph, *last;
22655 int voffset, face_id;
22657 xassert (s->first_glyph->type == STRETCH_GLYPH);
22659 glyph = s->row->glyphs[s->area] + start;
22660 last = s->row->glyphs[s->area] + end;
22661 face_id = glyph->face_id;
22662 s->face = FACE_FROM_ID (s->f, face_id);
22663 s->font = s->face->font;
22664 s->width = glyph->pixel_width;
22665 s->nchars = 1;
22666 voffset = glyph->voffset;
22668 for (++glyph;
22669 (glyph < last
22670 && glyph->type == STRETCH_GLYPH
22671 && glyph->voffset == voffset
22672 && glyph->face_id == face_id);
22673 ++glyph)
22674 s->width += glyph->pixel_width;
22676 /* Adjust base line for subscript/superscript text. */
22677 s->ybase += voffset;
22679 /* The case that face->gc == 0 is handled when drawing the glyph
22680 string by calling PREPARE_FACE_FOR_DISPLAY. */
22681 xassert (s->face);
22682 return glyph - s->row->glyphs[s->area];
22685 static struct font_metrics *
22686 get_per_char_metric (struct font *font, XChar2b *char2b)
22688 static struct font_metrics metrics;
22689 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22691 if (! font || code == FONT_INVALID_CODE)
22692 return NULL;
22693 font->driver->text_extents (font, &code, 1, &metrics);
22694 return &metrics;
22697 /* EXPORT for RIF:
22698 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22699 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22700 assumed to be zero. */
22702 void
22703 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22705 *left = *right = 0;
22707 if (glyph->type == CHAR_GLYPH)
22709 struct face *face;
22710 XChar2b char2b;
22711 struct font_metrics *pcm;
22713 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22714 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22716 if (pcm->rbearing > pcm->width)
22717 *right = pcm->rbearing - pcm->width;
22718 if (pcm->lbearing < 0)
22719 *left = -pcm->lbearing;
22722 else if (glyph->type == COMPOSITE_GLYPH)
22724 if (! glyph->u.cmp.automatic)
22726 struct composition *cmp = composition_table[glyph->u.cmp.id];
22728 if (cmp->rbearing > cmp->pixel_width)
22729 *right = cmp->rbearing - cmp->pixel_width;
22730 if (cmp->lbearing < 0)
22731 *left = - cmp->lbearing;
22733 else
22735 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22736 struct font_metrics metrics;
22738 composition_gstring_width (gstring, glyph->slice.cmp.from,
22739 glyph->slice.cmp.to + 1, &metrics);
22740 if (metrics.rbearing > metrics.width)
22741 *right = metrics.rbearing - metrics.width;
22742 if (metrics.lbearing < 0)
22743 *left = - metrics.lbearing;
22749 /* Return the index of the first glyph preceding glyph string S that
22750 is overwritten by S because of S's left overhang. Value is -1
22751 if no glyphs are overwritten. */
22753 static int
22754 left_overwritten (struct glyph_string *s)
22756 int k;
22758 if (s->left_overhang)
22760 int x = 0, i;
22761 struct glyph *glyphs = s->row->glyphs[s->area];
22762 int first = s->first_glyph - glyphs;
22764 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22765 x -= glyphs[i].pixel_width;
22767 k = i + 1;
22769 else
22770 k = -1;
22772 return k;
22776 /* Return the index of the first glyph preceding glyph string S that
22777 is overwriting S because of its right overhang. Value is -1 if no
22778 glyph in front of S overwrites S. */
22780 static int
22781 left_overwriting (struct glyph_string *s)
22783 int i, k, x;
22784 struct glyph *glyphs = s->row->glyphs[s->area];
22785 int first = s->first_glyph - glyphs;
22787 k = -1;
22788 x = 0;
22789 for (i = first - 1; i >= 0; --i)
22791 int left, right;
22792 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22793 if (x + right > 0)
22794 k = i;
22795 x -= glyphs[i].pixel_width;
22798 return k;
22802 /* Return the index of the last glyph following glyph string S that is
22803 overwritten by S because of S's right overhang. Value is -1 if
22804 no such glyph is found. */
22806 static int
22807 right_overwritten (struct glyph_string *s)
22809 int k = -1;
22811 if (s->right_overhang)
22813 int x = 0, i;
22814 struct glyph *glyphs = s->row->glyphs[s->area];
22815 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22816 int end = s->row->used[s->area];
22818 for (i = first; i < end && s->right_overhang > x; ++i)
22819 x += glyphs[i].pixel_width;
22821 k = i;
22824 return k;
22828 /* Return the index of the last glyph following glyph string S that
22829 overwrites S because of its left overhang. Value is negative
22830 if no such glyph is found. */
22832 static int
22833 right_overwriting (struct glyph_string *s)
22835 int i, k, x;
22836 int end = s->row->used[s->area];
22837 struct glyph *glyphs = s->row->glyphs[s->area];
22838 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22840 k = -1;
22841 x = 0;
22842 for (i = first; i < end; ++i)
22844 int left, right;
22845 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22846 if (x - left < 0)
22847 k = i;
22848 x += glyphs[i].pixel_width;
22851 return k;
22855 /* Set background width of glyph string S. START is the index of the
22856 first glyph following S. LAST_X is the right-most x-position + 1
22857 in the drawing area. */
22859 static inline void
22860 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22862 /* If the face of this glyph string has to be drawn to the end of
22863 the drawing area, set S->extends_to_end_of_line_p. */
22865 if (start == s->row->used[s->area]
22866 && s->area == TEXT_AREA
22867 && ((s->row->fill_line_p
22868 && (s->hl == DRAW_NORMAL_TEXT
22869 || s->hl == DRAW_IMAGE_RAISED
22870 || s->hl == DRAW_IMAGE_SUNKEN))
22871 || s->hl == DRAW_MOUSE_FACE))
22872 s->extends_to_end_of_line_p = 1;
22874 /* If S extends its face to the end of the line, set its
22875 background_width to the distance to the right edge of the drawing
22876 area. */
22877 if (s->extends_to_end_of_line_p)
22878 s->background_width = last_x - s->x + 1;
22879 else
22880 s->background_width = s->width;
22884 /* Compute overhangs and x-positions for glyph string S and its
22885 predecessors, or successors. X is the starting x-position for S.
22886 BACKWARD_P non-zero means process predecessors. */
22888 static void
22889 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22891 if (backward_p)
22893 while (s)
22895 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22896 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22897 x -= s->width;
22898 s->x = x;
22899 s = s->prev;
22902 else
22904 while (s)
22906 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22907 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22908 s->x = x;
22909 x += s->width;
22910 s = s->next;
22917 /* The following macros are only called from draw_glyphs below.
22918 They reference the following parameters of that function directly:
22919 `w', `row', `area', and `overlap_p'
22920 as well as the following local variables:
22921 `s', `f', and `hdc' (in W32) */
22923 #ifdef HAVE_NTGUI
22924 /* On W32, silently add local `hdc' variable to argument list of
22925 init_glyph_string. */
22926 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22927 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22928 #else
22929 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22930 init_glyph_string (s, char2b, w, row, area, start, hl)
22931 #endif
22933 /* Add a glyph string for a stretch glyph to the list of strings
22934 between HEAD and TAIL. START is the index of the stretch glyph in
22935 row area AREA of glyph row ROW. END is the index of the last glyph
22936 in that glyph row area. X is the current output position assigned
22937 to the new glyph string constructed. HL overrides that face of the
22938 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22939 is the right-most x-position of the drawing area. */
22941 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22942 and below -- keep them on one line. */
22943 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22944 do \
22946 s = (struct glyph_string *) alloca (sizeof *s); \
22947 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22948 START = fill_stretch_glyph_string (s, START, END); \
22949 append_glyph_string (&HEAD, &TAIL, s); \
22950 s->x = (X); \
22952 while (0)
22955 /* Add a glyph string for an image glyph to the list of strings
22956 between HEAD and TAIL. START is the index of the image glyph in
22957 row area AREA of glyph row ROW. END is the index of the last glyph
22958 in that glyph row area. X is the current output position assigned
22959 to the new glyph string constructed. HL overrides that face of the
22960 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22961 is the right-most x-position of the drawing area. */
22963 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22964 do \
22966 s = (struct glyph_string *) alloca (sizeof *s); \
22967 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22968 fill_image_glyph_string (s); \
22969 append_glyph_string (&HEAD, &TAIL, s); \
22970 ++START; \
22971 s->x = (X); \
22973 while (0)
22976 /* Add a glyph string for a sequence of character glyphs to the list
22977 of strings between HEAD and TAIL. START is the index of the first
22978 glyph in row area AREA of glyph row ROW that is part of the new
22979 glyph string. END is the index of the last glyph in that glyph row
22980 area. X is the current output position assigned to the new glyph
22981 string constructed. HL overrides that face of the glyph; e.g. it
22982 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22983 right-most x-position of the drawing area. */
22985 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22986 do \
22988 int face_id; \
22989 XChar2b *char2b; \
22991 face_id = (row)->glyphs[area][START].face_id; \
22993 s = (struct glyph_string *) alloca (sizeof *s); \
22994 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22995 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22996 append_glyph_string (&HEAD, &TAIL, s); \
22997 s->x = (X); \
22998 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23000 while (0)
23003 /* Add a glyph string for a composite sequence to the list of strings
23004 between HEAD and TAIL. START is the index of the first glyph in
23005 row area AREA of glyph row ROW that is part of the new glyph
23006 string. END is the index of the last glyph in that glyph row area.
23007 X is the current output position assigned to the new glyph string
23008 constructed. HL overrides that face of the glyph; e.g. it is
23009 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23010 x-position of the drawing area. */
23012 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23013 do { \
23014 int face_id = (row)->glyphs[area][START].face_id; \
23015 struct face *base_face = FACE_FROM_ID (f, face_id); \
23016 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23017 struct composition *cmp = composition_table[cmp_id]; \
23018 XChar2b *char2b; \
23019 struct glyph_string *first_s = NULL; \
23020 int n; \
23022 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
23024 /* Make glyph_strings for each glyph sequence that is drawable by \
23025 the same face, and append them to HEAD/TAIL. */ \
23026 for (n = 0; n < cmp->glyph_len;) \
23028 s = (struct glyph_string *) alloca (sizeof *s); \
23029 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23030 append_glyph_string (&(HEAD), &(TAIL), s); \
23031 s->cmp = cmp; \
23032 s->cmp_from = n; \
23033 s->x = (X); \
23034 if (n == 0) \
23035 first_s = s; \
23036 n = fill_composite_glyph_string (s, base_face, overlaps); \
23039 ++START; \
23040 s = first_s; \
23041 } while (0)
23044 /* Add a glyph string for a glyph-string sequence to the list of strings
23045 between HEAD and TAIL. */
23047 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23048 do { \
23049 int face_id; \
23050 XChar2b *char2b; \
23051 Lisp_Object gstring; \
23053 face_id = (row)->glyphs[area][START].face_id; \
23054 gstring = (composition_gstring_from_id \
23055 ((row)->glyphs[area][START].u.cmp.id)); \
23056 s = (struct glyph_string *) alloca (sizeof *s); \
23057 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23058 * LGSTRING_GLYPH_LEN (gstring)); \
23059 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23060 append_glyph_string (&(HEAD), &(TAIL), s); \
23061 s->x = (X); \
23062 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23063 } while (0)
23066 /* Add a glyph string for a sequence of glyphless character's glyphs
23067 to the list of strings between HEAD and TAIL. The meanings of
23068 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23070 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23071 do \
23073 int face_id; \
23075 face_id = (row)->glyphs[area][START].face_id; \
23077 s = (struct glyph_string *) alloca (sizeof *s); \
23078 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23079 append_glyph_string (&HEAD, &TAIL, s); \
23080 s->x = (X); \
23081 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23082 overlaps); \
23084 while (0)
23087 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23088 of AREA of glyph row ROW on window W between indices START and END.
23089 HL overrides the face for drawing glyph strings, e.g. it is
23090 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23091 x-positions of the drawing area.
23093 This is an ugly monster macro construct because we must use alloca
23094 to allocate glyph strings (because draw_glyphs can be called
23095 asynchronously). */
23097 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23098 do \
23100 HEAD = TAIL = NULL; \
23101 while (START < END) \
23103 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23104 switch (first_glyph->type) \
23106 case CHAR_GLYPH: \
23107 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23108 HL, X, LAST_X); \
23109 break; \
23111 case COMPOSITE_GLYPH: \
23112 if (first_glyph->u.cmp.automatic) \
23113 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23114 HL, X, LAST_X); \
23115 else \
23116 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23117 HL, X, LAST_X); \
23118 break; \
23120 case STRETCH_GLYPH: \
23121 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23122 HL, X, LAST_X); \
23123 break; \
23125 case IMAGE_GLYPH: \
23126 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23127 HL, X, LAST_X); \
23128 break; \
23130 case GLYPHLESS_GLYPH: \
23131 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23132 HL, X, LAST_X); \
23133 break; \
23135 default: \
23136 abort (); \
23139 if (s) \
23141 set_glyph_string_background_width (s, START, LAST_X); \
23142 (X) += s->width; \
23145 } while (0)
23148 /* Draw glyphs between START and END in AREA of ROW on window W,
23149 starting at x-position X. X is relative to AREA in W. HL is a
23150 face-override with the following meaning:
23152 DRAW_NORMAL_TEXT draw normally
23153 DRAW_CURSOR draw in cursor face
23154 DRAW_MOUSE_FACE draw in mouse face.
23155 DRAW_INVERSE_VIDEO draw in mode line face
23156 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23157 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23159 If OVERLAPS is non-zero, draw only the foreground of characters and
23160 clip to the physical height of ROW. Non-zero value also defines
23161 the overlapping part to be drawn:
23163 OVERLAPS_PRED overlap with preceding rows
23164 OVERLAPS_SUCC overlap with succeeding rows
23165 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23166 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23168 Value is the x-position reached, relative to AREA of W. */
23170 static int
23171 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23172 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23173 enum draw_glyphs_face hl, int overlaps)
23175 struct glyph_string *head, *tail;
23176 struct glyph_string *s;
23177 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23178 int i, j, x_reached, last_x, area_left = 0;
23179 struct frame *f = XFRAME (WINDOW_FRAME (w));
23180 DECLARE_HDC (hdc);
23182 ALLOCATE_HDC (hdc, f);
23184 /* Let's rather be paranoid than getting a SEGV. */
23185 end = min (end, row->used[area]);
23186 start = max (0, start);
23187 start = min (end, start);
23189 /* Translate X to frame coordinates. Set last_x to the right
23190 end of the drawing area. */
23191 if (row->full_width_p)
23193 /* X is relative to the left edge of W, without scroll bars
23194 or fringes. */
23195 area_left = WINDOW_LEFT_EDGE_X (w);
23196 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23198 else
23200 area_left = window_box_left (w, area);
23201 last_x = area_left + window_box_width (w, area);
23203 x += area_left;
23205 /* Build a doubly-linked list of glyph_string structures between
23206 head and tail from what we have to draw. Note that the macro
23207 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23208 the reason we use a separate variable `i'. */
23209 i = start;
23210 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23211 if (tail)
23212 x_reached = tail->x + tail->background_width;
23213 else
23214 x_reached = x;
23216 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23217 the row, redraw some glyphs in front or following the glyph
23218 strings built above. */
23219 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23221 struct glyph_string *h, *t;
23222 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23223 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23224 int check_mouse_face = 0;
23225 int dummy_x = 0;
23227 /* If mouse highlighting is on, we may need to draw adjacent
23228 glyphs using mouse-face highlighting. */
23229 if (area == TEXT_AREA && row->mouse_face_p)
23231 struct glyph_row *mouse_beg_row, *mouse_end_row;
23233 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23234 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23236 if (row >= mouse_beg_row && row <= mouse_end_row)
23238 check_mouse_face = 1;
23239 mouse_beg_col = (row == mouse_beg_row)
23240 ? hlinfo->mouse_face_beg_col : 0;
23241 mouse_end_col = (row == mouse_end_row)
23242 ? hlinfo->mouse_face_end_col
23243 : row->used[TEXT_AREA];
23247 /* Compute overhangs for all glyph strings. */
23248 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23249 for (s = head; s; s = s->next)
23250 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23252 /* Prepend glyph strings for glyphs in front of the first glyph
23253 string that are overwritten because of the first glyph
23254 string's left overhang. The background of all strings
23255 prepended must be drawn because the first glyph string
23256 draws over it. */
23257 i = left_overwritten (head);
23258 if (i >= 0)
23260 enum draw_glyphs_face overlap_hl;
23262 /* If this row contains mouse highlighting, attempt to draw
23263 the overlapped glyphs with the correct highlight. This
23264 code fails if the overlap encompasses more than one glyph
23265 and mouse-highlight spans only some of these glyphs.
23266 However, making it work perfectly involves a lot more
23267 code, and I don't know if the pathological case occurs in
23268 practice, so we'll stick to this for now. --- cyd */
23269 if (check_mouse_face
23270 && mouse_beg_col < start && mouse_end_col > i)
23271 overlap_hl = DRAW_MOUSE_FACE;
23272 else
23273 overlap_hl = DRAW_NORMAL_TEXT;
23275 j = i;
23276 BUILD_GLYPH_STRINGS (j, start, h, t,
23277 overlap_hl, dummy_x, last_x);
23278 start = i;
23279 compute_overhangs_and_x (t, head->x, 1);
23280 prepend_glyph_string_lists (&head, &tail, h, t);
23281 clip_head = head;
23284 /* Prepend glyph strings for glyphs in front of the first glyph
23285 string that overwrite that glyph string because of their
23286 right overhang. For these strings, only the foreground must
23287 be drawn, because it draws over the glyph string at `head'.
23288 The background must not be drawn because this would overwrite
23289 right overhangs of preceding glyphs for which no glyph
23290 strings exist. */
23291 i = left_overwriting (head);
23292 if (i >= 0)
23294 enum draw_glyphs_face overlap_hl;
23296 if (check_mouse_face
23297 && mouse_beg_col < start && mouse_end_col > i)
23298 overlap_hl = DRAW_MOUSE_FACE;
23299 else
23300 overlap_hl = DRAW_NORMAL_TEXT;
23302 clip_head = head;
23303 BUILD_GLYPH_STRINGS (i, start, h, t,
23304 overlap_hl, dummy_x, last_x);
23305 for (s = h; s; s = s->next)
23306 s->background_filled_p = 1;
23307 compute_overhangs_and_x (t, head->x, 1);
23308 prepend_glyph_string_lists (&head, &tail, h, t);
23311 /* Append glyphs strings for glyphs following the last glyph
23312 string tail that are overwritten by tail. The background of
23313 these strings has to be drawn because tail's foreground draws
23314 over it. */
23315 i = right_overwritten (tail);
23316 if (i >= 0)
23318 enum draw_glyphs_face overlap_hl;
23320 if (check_mouse_face
23321 && mouse_beg_col < i && mouse_end_col > end)
23322 overlap_hl = DRAW_MOUSE_FACE;
23323 else
23324 overlap_hl = DRAW_NORMAL_TEXT;
23326 BUILD_GLYPH_STRINGS (end, i, h, t,
23327 overlap_hl, x, last_x);
23328 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23329 we don't have `end = i;' here. */
23330 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23331 append_glyph_string_lists (&head, &tail, h, t);
23332 clip_tail = tail;
23335 /* Append glyph strings for glyphs following the last glyph
23336 string tail that overwrite tail. The foreground of such
23337 glyphs has to be drawn because it writes into the background
23338 of tail. The background must not be drawn because it could
23339 paint over the foreground of following glyphs. */
23340 i = right_overwriting (tail);
23341 if (i >= 0)
23343 enum draw_glyphs_face overlap_hl;
23344 if (check_mouse_face
23345 && mouse_beg_col < i && mouse_end_col > end)
23346 overlap_hl = DRAW_MOUSE_FACE;
23347 else
23348 overlap_hl = DRAW_NORMAL_TEXT;
23350 clip_tail = tail;
23351 i++; /* We must include the Ith glyph. */
23352 BUILD_GLYPH_STRINGS (end, i, h, t,
23353 overlap_hl, x, last_x);
23354 for (s = h; s; s = s->next)
23355 s->background_filled_p = 1;
23356 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23357 append_glyph_string_lists (&head, &tail, h, t);
23359 if (clip_head || clip_tail)
23360 for (s = head; s; s = s->next)
23362 s->clip_head = clip_head;
23363 s->clip_tail = clip_tail;
23367 /* Draw all strings. */
23368 for (s = head; s; s = s->next)
23369 FRAME_RIF (f)->draw_glyph_string (s);
23371 #ifndef HAVE_NS
23372 /* When focus a sole frame and move horizontally, this sets on_p to 0
23373 causing a failure to erase prev cursor position. */
23374 if (area == TEXT_AREA
23375 && !row->full_width_p
23376 /* When drawing overlapping rows, only the glyph strings'
23377 foreground is drawn, which doesn't erase a cursor
23378 completely. */
23379 && !overlaps)
23381 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23382 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23383 : (tail ? tail->x + tail->background_width : x));
23384 x0 -= area_left;
23385 x1 -= area_left;
23387 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23388 row->y, MATRIX_ROW_BOTTOM_Y (row));
23390 #endif
23392 /* Value is the x-position up to which drawn, relative to AREA of W.
23393 This doesn't include parts drawn because of overhangs. */
23394 if (row->full_width_p)
23395 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23396 else
23397 x_reached -= area_left;
23399 RELEASE_HDC (hdc, f);
23401 return x_reached;
23404 /* Expand row matrix if too narrow. Don't expand if area
23405 is not present. */
23407 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23409 if (!fonts_changed_p \
23410 && (it->glyph_row->glyphs[area] \
23411 < it->glyph_row->glyphs[area + 1])) \
23413 it->w->ncols_scale_factor++; \
23414 fonts_changed_p = 1; \
23418 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23419 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23421 static inline void
23422 append_glyph (struct it *it)
23424 struct glyph *glyph;
23425 enum glyph_row_area area = it->area;
23427 xassert (it->glyph_row);
23428 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23430 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23431 if (glyph < it->glyph_row->glyphs[area + 1])
23433 /* If the glyph row is reversed, we need to prepend the glyph
23434 rather than append it. */
23435 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23437 struct glyph *g;
23439 /* Make room for the additional glyph. */
23440 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23441 g[1] = *g;
23442 glyph = it->glyph_row->glyphs[area];
23444 glyph->charpos = CHARPOS (it->position);
23445 glyph->object = it->object;
23446 if (it->pixel_width > 0)
23448 glyph->pixel_width = it->pixel_width;
23449 glyph->padding_p = 0;
23451 else
23453 /* Assure at least 1-pixel width. Otherwise, cursor can't
23454 be displayed correctly. */
23455 glyph->pixel_width = 1;
23456 glyph->padding_p = 1;
23458 glyph->ascent = it->ascent;
23459 glyph->descent = it->descent;
23460 glyph->voffset = it->voffset;
23461 glyph->type = CHAR_GLYPH;
23462 glyph->avoid_cursor_p = it->avoid_cursor_p;
23463 glyph->multibyte_p = it->multibyte_p;
23464 glyph->left_box_line_p = it->start_of_box_run_p;
23465 glyph->right_box_line_p = it->end_of_box_run_p;
23466 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23467 || it->phys_descent > it->descent);
23468 glyph->glyph_not_available_p = it->glyph_not_available_p;
23469 glyph->face_id = it->face_id;
23470 glyph->u.ch = it->char_to_display;
23471 glyph->slice.img = null_glyph_slice;
23472 glyph->font_type = FONT_TYPE_UNKNOWN;
23473 if (it->bidi_p)
23475 glyph->resolved_level = it->bidi_it.resolved_level;
23476 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23477 abort ();
23478 glyph->bidi_type = it->bidi_it.type;
23480 else
23482 glyph->resolved_level = 0;
23483 glyph->bidi_type = UNKNOWN_BT;
23485 ++it->glyph_row->used[area];
23487 else
23488 IT_EXPAND_MATRIX_WIDTH (it, area);
23491 /* Store one glyph for the composition IT->cmp_it.id in
23492 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23493 non-null. */
23495 static inline void
23496 append_composite_glyph (struct it *it)
23498 struct glyph *glyph;
23499 enum glyph_row_area area = it->area;
23501 xassert (it->glyph_row);
23503 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23504 if (glyph < it->glyph_row->glyphs[area + 1])
23506 /* If the glyph row is reversed, we need to prepend the glyph
23507 rather than append it. */
23508 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23510 struct glyph *g;
23512 /* Make room for the new glyph. */
23513 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23514 g[1] = *g;
23515 glyph = it->glyph_row->glyphs[it->area];
23517 glyph->charpos = it->cmp_it.charpos;
23518 glyph->object = it->object;
23519 glyph->pixel_width = it->pixel_width;
23520 glyph->ascent = it->ascent;
23521 glyph->descent = it->descent;
23522 glyph->voffset = it->voffset;
23523 glyph->type = COMPOSITE_GLYPH;
23524 if (it->cmp_it.ch < 0)
23526 glyph->u.cmp.automatic = 0;
23527 glyph->u.cmp.id = it->cmp_it.id;
23528 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23530 else
23532 glyph->u.cmp.automatic = 1;
23533 glyph->u.cmp.id = it->cmp_it.id;
23534 glyph->slice.cmp.from = it->cmp_it.from;
23535 glyph->slice.cmp.to = it->cmp_it.to - 1;
23537 glyph->avoid_cursor_p = it->avoid_cursor_p;
23538 glyph->multibyte_p = it->multibyte_p;
23539 glyph->left_box_line_p = it->start_of_box_run_p;
23540 glyph->right_box_line_p = it->end_of_box_run_p;
23541 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23542 || it->phys_descent > it->descent);
23543 glyph->padding_p = 0;
23544 glyph->glyph_not_available_p = 0;
23545 glyph->face_id = it->face_id;
23546 glyph->font_type = FONT_TYPE_UNKNOWN;
23547 if (it->bidi_p)
23549 glyph->resolved_level = it->bidi_it.resolved_level;
23550 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23551 abort ();
23552 glyph->bidi_type = it->bidi_it.type;
23554 ++it->glyph_row->used[area];
23556 else
23557 IT_EXPAND_MATRIX_WIDTH (it, area);
23561 /* Change IT->ascent and IT->height according to the setting of
23562 IT->voffset. */
23564 static inline void
23565 take_vertical_position_into_account (struct it *it)
23567 if (it->voffset)
23569 if (it->voffset < 0)
23570 /* Increase the ascent so that we can display the text higher
23571 in the line. */
23572 it->ascent -= it->voffset;
23573 else
23574 /* Increase the descent so that we can display the text lower
23575 in the line. */
23576 it->descent += it->voffset;
23581 /* Produce glyphs/get display metrics for the image IT is loaded with.
23582 See the description of struct display_iterator in dispextern.h for
23583 an overview of struct display_iterator. */
23585 static void
23586 produce_image_glyph (struct it *it)
23588 struct image *img;
23589 struct face *face;
23590 int glyph_ascent, crop;
23591 struct glyph_slice slice;
23593 xassert (it->what == IT_IMAGE);
23595 face = FACE_FROM_ID (it->f, it->face_id);
23596 xassert (face);
23597 /* Make sure X resources of the face is loaded. */
23598 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23600 if (it->image_id < 0)
23602 /* Fringe bitmap. */
23603 it->ascent = it->phys_ascent = 0;
23604 it->descent = it->phys_descent = 0;
23605 it->pixel_width = 0;
23606 it->nglyphs = 0;
23607 return;
23610 img = IMAGE_FROM_ID (it->f, it->image_id);
23611 xassert (img);
23612 /* Make sure X resources of the image is loaded. */
23613 prepare_image_for_display (it->f, img);
23615 slice.x = slice.y = 0;
23616 slice.width = img->width;
23617 slice.height = img->height;
23619 if (INTEGERP (it->slice.x))
23620 slice.x = XINT (it->slice.x);
23621 else if (FLOATP (it->slice.x))
23622 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23624 if (INTEGERP (it->slice.y))
23625 slice.y = XINT (it->slice.y);
23626 else if (FLOATP (it->slice.y))
23627 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23629 if (INTEGERP (it->slice.width))
23630 slice.width = XINT (it->slice.width);
23631 else if (FLOATP (it->slice.width))
23632 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23634 if (INTEGERP (it->slice.height))
23635 slice.height = XINT (it->slice.height);
23636 else if (FLOATP (it->slice.height))
23637 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23639 if (slice.x >= img->width)
23640 slice.x = img->width;
23641 if (slice.y >= img->height)
23642 slice.y = img->height;
23643 if (slice.x + slice.width >= img->width)
23644 slice.width = img->width - slice.x;
23645 if (slice.y + slice.height > img->height)
23646 slice.height = img->height - slice.y;
23648 if (slice.width == 0 || slice.height == 0)
23649 return;
23651 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23653 it->descent = slice.height - glyph_ascent;
23654 if (slice.y == 0)
23655 it->descent += img->vmargin;
23656 if (slice.y + slice.height == img->height)
23657 it->descent += img->vmargin;
23658 it->phys_descent = it->descent;
23660 it->pixel_width = slice.width;
23661 if (slice.x == 0)
23662 it->pixel_width += img->hmargin;
23663 if (slice.x + slice.width == img->width)
23664 it->pixel_width += img->hmargin;
23666 /* It's quite possible for images to have an ascent greater than
23667 their height, so don't get confused in that case. */
23668 if (it->descent < 0)
23669 it->descent = 0;
23671 it->nglyphs = 1;
23673 if (face->box != FACE_NO_BOX)
23675 if (face->box_line_width > 0)
23677 if (slice.y == 0)
23678 it->ascent += face->box_line_width;
23679 if (slice.y + slice.height == img->height)
23680 it->descent += face->box_line_width;
23683 if (it->start_of_box_run_p && slice.x == 0)
23684 it->pixel_width += eabs (face->box_line_width);
23685 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23686 it->pixel_width += eabs (face->box_line_width);
23689 take_vertical_position_into_account (it);
23691 /* Automatically crop wide image glyphs at right edge so we can
23692 draw the cursor on same display row. */
23693 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23694 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23696 it->pixel_width -= crop;
23697 slice.width -= crop;
23700 if (it->glyph_row)
23702 struct glyph *glyph;
23703 enum glyph_row_area area = it->area;
23705 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23706 if (glyph < it->glyph_row->glyphs[area + 1])
23708 glyph->charpos = CHARPOS (it->position);
23709 glyph->object = it->object;
23710 glyph->pixel_width = it->pixel_width;
23711 glyph->ascent = glyph_ascent;
23712 glyph->descent = it->descent;
23713 glyph->voffset = it->voffset;
23714 glyph->type = IMAGE_GLYPH;
23715 glyph->avoid_cursor_p = it->avoid_cursor_p;
23716 glyph->multibyte_p = it->multibyte_p;
23717 glyph->left_box_line_p = it->start_of_box_run_p;
23718 glyph->right_box_line_p = it->end_of_box_run_p;
23719 glyph->overlaps_vertically_p = 0;
23720 glyph->padding_p = 0;
23721 glyph->glyph_not_available_p = 0;
23722 glyph->face_id = it->face_id;
23723 glyph->u.img_id = img->id;
23724 glyph->slice.img = slice;
23725 glyph->font_type = FONT_TYPE_UNKNOWN;
23726 if (it->bidi_p)
23728 glyph->resolved_level = it->bidi_it.resolved_level;
23729 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23730 abort ();
23731 glyph->bidi_type = it->bidi_it.type;
23733 ++it->glyph_row->used[area];
23735 else
23736 IT_EXPAND_MATRIX_WIDTH (it, area);
23741 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23742 of the glyph, WIDTH and HEIGHT are the width and height of the
23743 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23745 static void
23746 append_stretch_glyph (struct it *it, Lisp_Object object,
23747 int width, int height, int ascent)
23749 struct glyph *glyph;
23750 enum glyph_row_area area = it->area;
23752 xassert (ascent >= 0 && ascent <= height);
23754 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23755 if (glyph < it->glyph_row->glyphs[area + 1])
23757 /* If the glyph row is reversed, we need to prepend the glyph
23758 rather than append it. */
23759 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23761 struct glyph *g;
23763 /* Make room for the additional glyph. */
23764 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23765 g[1] = *g;
23766 glyph = it->glyph_row->glyphs[area];
23768 glyph->charpos = CHARPOS (it->position);
23769 glyph->object = object;
23770 glyph->pixel_width = width;
23771 glyph->ascent = ascent;
23772 glyph->descent = height - ascent;
23773 glyph->voffset = it->voffset;
23774 glyph->type = STRETCH_GLYPH;
23775 glyph->avoid_cursor_p = it->avoid_cursor_p;
23776 glyph->multibyte_p = it->multibyte_p;
23777 glyph->left_box_line_p = it->start_of_box_run_p;
23778 glyph->right_box_line_p = it->end_of_box_run_p;
23779 glyph->overlaps_vertically_p = 0;
23780 glyph->padding_p = 0;
23781 glyph->glyph_not_available_p = 0;
23782 glyph->face_id = it->face_id;
23783 glyph->u.stretch.ascent = ascent;
23784 glyph->u.stretch.height = height;
23785 glyph->slice.img = null_glyph_slice;
23786 glyph->font_type = FONT_TYPE_UNKNOWN;
23787 if (it->bidi_p)
23789 glyph->resolved_level = it->bidi_it.resolved_level;
23790 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23791 abort ();
23792 glyph->bidi_type = it->bidi_it.type;
23794 else
23796 glyph->resolved_level = 0;
23797 glyph->bidi_type = UNKNOWN_BT;
23799 ++it->glyph_row->used[area];
23801 else
23802 IT_EXPAND_MATRIX_WIDTH (it, area);
23805 #endif /* HAVE_WINDOW_SYSTEM */
23807 /* Produce a stretch glyph for iterator IT. IT->object is the value
23808 of the glyph property displayed. The value must be a list
23809 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23810 being recognized:
23812 1. `:width WIDTH' specifies that the space should be WIDTH *
23813 canonical char width wide. WIDTH may be an integer or floating
23814 point number.
23816 2. `:relative-width FACTOR' specifies that the width of the stretch
23817 should be computed from the width of the first character having the
23818 `glyph' property, and should be FACTOR times that width.
23820 3. `:align-to HPOS' specifies that the space should be wide enough
23821 to reach HPOS, a value in canonical character units.
23823 Exactly one of the above pairs must be present.
23825 4. `:height HEIGHT' specifies that the height of the stretch produced
23826 should be HEIGHT, measured in canonical character units.
23828 5. `:relative-height FACTOR' specifies that the height of the
23829 stretch should be FACTOR times the height of the characters having
23830 the glyph property.
23832 Either none or exactly one of 4 or 5 must be present.
23834 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23835 of the stretch should be used for the ascent of the stretch.
23836 ASCENT must be in the range 0 <= ASCENT <= 100. */
23838 void
23839 produce_stretch_glyph (struct it *it)
23841 /* (space :width WIDTH :height HEIGHT ...) */
23842 Lisp_Object prop, plist;
23843 int width = 0, height = 0, align_to = -1;
23844 int zero_width_ok_p = 0;
23845 int ascent = 0;
23846 double tem;
23847 struct face *face = NULL;
23848 struct font *font = NULL;
23850 #ifdef HAVE_WINDOW_SYSTEM
23851 int zero_height_ok_p = 0;
23853 if (FRAME_WINDOW_P (it->f))
23855 face = FACE_FROM_ID (it->f, it->face_id);
23856 font = face->font ? face->font : FRAME_FONT (it->f);
23857 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23859 #endif
23861 /* List should start with `space'. */
23862 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23863 plist = XCDR (it->object);
23865 /* Compute the width of the stretch. */
23866 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23867 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23869 /* Absolute width `:width WIDTH' specified and valid. */
23870 zero_width_ok_p = 1;
23871 width = (int)tem;
23873 #ifdef HAVE_WINDOW_SYSTEM
23874 else if (FRAME_WINDOW_P (it->f)
23875 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23877 /* Relative width `:relative-width FACTOR' specified and valid.
23878 Compute the width of the characters having the `glyph'
23879 property. */
23880 struct it it2;
23881 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23883 it2 = *it;
23884 if (it->multibyte_p)
23885 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23886 else
23888 it2.c = it2.char_to_display = *p, it2.len = 1;
23889 if (! ASCII_CHAR_P (it2.c))
23890 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23893 it2.glyph_row = NULL;
23894 it2.what = IT_CHARACTER;
23895 x_produce_glyphs (&it2);
23896 width = NUMVAL (prop) * it2.pixel_width;
23898 #endif /* HAVE_WINDOW_SYSTEM */
23899 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23900 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23902 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23903 align_to = (align_to < 0
23905 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23906 else if (align_to < 0)
23907 align_to = window_box_left_offset (it->w, TEXT_AREA);
23908 width = max (0, (int)tem + align_to - it->current_x);
23909 zero_width_ok_p = 1;
23911 else
23912 /* Nothing specified -> width defaults to canonical char width. */
23913 width = FRAME_COLUMN_WIDTH (it->f);
23915 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23916 width = 1;
23918 #ifdef HAVE_WINDOW_SYSTEM
23919 /* Compute height. */
23920 if (FRAME_WINDOW_P (it->f))
23922 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23923 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23925 height = (int)tem;
23926 zero_height_ok_p = 1;
23928 else if (prop = Fplist_get (plist, QCrelative_height),
23929 NUMVAL (prop) > 0)
23930 height = FONT_HEIGHT (font) * NUMVAL (prop);
23931 else
23932 height = FONT_HEIGHT (font);
23934 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23935 height = 1;
23937 /* Compute percentage of height used for ascent. If
23938 `:ascent ASCENT' is present and valid, use that. Otherwise,
23939 derive the ascent from the font in use. */
23940 if (prop = Fplist_get (plist, QCascent),
23941 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23942 ascent = height * NUMVAL (prop) / 100.0;
23943 else if (!NILP (prop)
23944 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23945 ascent = min (max (0, (int)tem), height);
23946 else
23947 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23949 else
23950 #endif /* HAVE_WINDOW_SYSTEM */
23951 height = 1;
23953 if (width > 0 && it->line_wrap != TRUNCATE
23954 && it->current_x + width > it->last_visible_x)
23956 width = it->last_visible_x - it->current_x;
23957 #ifdef HAVE_WINDOW_SYSTEM
23958 /* Subtract one more pixel from the stretch width, but only on
23959 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23960 width -= FRAME_WINDOW_P (it->f);
23961 #endif
23964 if (width > 0 && height > 0 && it->glyph_row)
23966 Lisp_Object o_object = it->object;
23967 Lisp_Object object = it->stack[it->sp - 1].string;
23968 int n = width;
23970 if (!STRINGP (object))
23971 object = it->w->buffer;
23972 #ifdef HAVE_WINDOW_SYSTEM
23973 if (FRAME_WINDOW_P (it->f))
23974 append_stretch_glyph (it, object, width, height, ascent);
23975 else
23976 #endif
23978 it->object = object;
23979 it->char_to_display = ' ';
23980 it->pixel_width = it->len = 1;
23981 while (n--)
23982 tty_append_glyph (it);
23983 it->object = o_object;
23987 it->pixel_width = width;
23988 #ifdef HAVE_WINDOW_SYSTEM
23989 if (FRAME_WINDOW_P (it->f))
23991 it->ascent = it->phys_ascent = ascent;
23992 it->descent = it->phys_descent = height - it->ascent;
23993 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23994 take_vertical_position_into_account (it);
23996 else
23997 #endif
23998 it->nglyphs = width;
24001 #ifdef HAVE_WINDOW_SYSTEM
24003 /* Calculate line-height and line-spacing properties.
24004 An integer value specifies explicit pixel value.
24005 A float value specifies relative value to current face height.
24006 A cons (float . face-name) specifies relative value to
24007 height of specified face font.
24009 Returns height in pixels, or nil. */
24012 static Lisp_Object
24013 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24014 int boff, int override)
24016 Lisp_Object face_name = Qnil;
24017 int ascent, descent, height;
24019 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24020 return val;
24022 if (CONSP (val))
24024 face_name = XCAR (val);
24025 val = XCDR (val);
24026 if (!NUMBERP (val))
24027 val = make_number (1);
24028 if (NILP (face_name))
24030 height = it->ascent + it->descent;
24031 goto scale;
24035 if (NILP (face_name))
24037 font = FRAME_FONT (it->f);
24038 boff = FRAME_BASELINE_OFFSET (it->f);
24040 else if (EQ (face_name, Qt))
24042 override = 0;
24044 else
24046 int face_id;
24047 struct face *face;
24049 face_id = lookup_named_face (it->f, face_name, 0);
24050 if (face_id < 0)
24051 return make_number (-1);
24053 face = FACE_FROM_ID (it->f, face_id);
24054 font = face->font;
24055 if (font == NULL)
24056 return make_number (-1);
24057 boff = font->baseline_offset;
24058 if (font->vertical_centering)
24059 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24062 ascent = FONT_BASE (font) + boff;
24063 descent = FONT_DESCENT (font) - boff;
24065 if (override)
24067 it->override_ascent = ascent;
24068 it->override_descent = descent;
24069 it->override_boff = boff;
24072 height = ascent + descent;
24074 scale:
24075 if (FLOATP (val))
24076 height = (int)(XFLOAT_DATA (val) * height);
24077 else if (INTEGERP (val))
24078 height *= XINT (val);
24080 return make_number (height);
24084 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24085 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24086 and only if this is for a character for which no font was found.
24088 If the display method (it->glyphless_method) is
24089 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24090 length of the acronym or the hexadecimal string, UPPER_XOFF and
24091 UPPER_YOFF are pixel offsets for the upper part of the string,
24092 LOWER_XOFF and LOWER_YOFF are for the lower part.
24094 For the other display methods, LEN through LOWER_YOFF are zero. */
24096 static void
24097 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24098 short upper_xoff, short upper_yoff,
24099 short lower_xoff, short lower_yoff)
24101 struct glyph *glyph;
24102 enum glyph_row_area area = it->area;
24104 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24105 if (glyph < it->glyph_row->glyphs[area + 1])
24107 /* If the glyph row is reversed, we need to prepend the glyph
24108 rather than append it. */
24109 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24111 struct glyph *g;
24113 /* Make room for the additional glyph. */
24114 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24115 g[1] = *g;
24116 glyph = it->glyph_row->glyphs[area];
24118 glyph->charpos = CHARPOS (it->position);
24119 glyph->object = it->object;
24120 glyph->pixel_width = it->pixel_width;
24121 glyph->ascent = it->ascent;
24122 glyph->descent = it->descent;
24123 glyph->voffset = it->voffset;
24124 glyph->type = GLYPHLESS_GLYPH;
24125 glyph->u.glyphless.method = it->glyphless_method;
24126 glyph->u.glyphless.for_no_font = for_no_font;
24127 glyph->u.glyphless.len = len;
24128 glyph->u.glyphless.ch = it->c;
24129 glyph->slice.glyphless.upper_xoff = upper_xoff;
24130 glyph->slice.glyphless.upper_yoff = upper_yoff;
24131 glyph->slice.glyphless.lower_xoff = lower_xoff;
24132 glyph->slice.glyphless.lower_yoff = lower_yoff;
24133 glyph->avoid_cursor_p = it->avoid_cursor_p;
24134 glyph->multibyte_p = it->multibyte_p;
24135 glyph->left_box_line_p = it->start_of_box_run_p;
24136 glyph->right_box_line_p = it->end_of_box_run_p;
24137 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24138 || it->phys_descent > it->descent);
24139 glyph->padding_p = 0;
24140 glyph->glyph_not_available_p = 0;
24141 glyph->face_id = face_id;
24142 glyph->font_type = FONT_TYPE_UNKNOWN;
24143 if (it->bidi_p)
24145 glyph->resolved_level = it->bidi_it.resolved_level;
24146 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24147 abort ();
24148 glyph->bidi_type = it->bidi_it.type;
24150 ++it->glyph_row->used[area];
24152 else
24153 IT_EXPAND_MATRIX_WIDTH (it, area);
24157 /* Produce a glyph for a glyphless character for iterator IT.
24158 IT->glyphless_method specifies which method to use for displaying
24159 the character. See the description of enum
24160 glyphless_display_method in dispextern.h for the detail.
24162 FOR_NO_FONT is nonzero if and only if this is for a character for
24163 which no font was found. ACRONYM, if non-nil, is an acronym string
24164 for the character. */
24166 static void
24167 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24169 int face_id;
24170 struct face *face;
24171 struct font *font;
24172 int base_width, base_height, width, height;
24173 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24174 int len;
24176 /* Get the metrics of the base font. We always refer to the current
24177 ASCII face. */
24178 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24179 font = face->font ? face->font : FRAME_FONT (it->f);
24180 it->ascent = FONT_BASE (font) + font->baseline_offset;
24181 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24182 base_height = it->ascent + it->descent;
24183 base_width = font->average_width;
24185 /* Get a face ID for the glyph by utilizing a cache (the same way as
24186 done for `escape-glyph' in get_next_display_element). */
24187 if (it->f == last_glyphless_glyph_frame
24188 && it->face_id == last_glyphless_glyph_face_id)
24190 face_id = last_glyphless_glyph_merged_face_id;
24192 else
24194 /* Merge the `glyphless-char' face into the current face. */
24195 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24196 last_glyphless_glyph_frame = it->f;
24197 last_glyphless_glyph_face_id = it->face_id;
24198 last_glyphless_glyph_merged_face_id = face_id;
24201 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24203 it->pixel_width = THIN_SPACE_WIDTH;
24204 len = 0;
24205 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24207 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24209 width = CHAR_WIDTH (it->c);
24210 if (width == 0)
24211 width = 1;
24212 else if (width > 4)
24213 width = 4;
24214 it->pixel_width = base_width * width;
24215 len = 0;
24216 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24218 else
24220 char buf[7];
24221 const char *str;
24222 unsigned int code[6];
24223 int upper_len;
24224 int ascent, descent;
24225 struct font_metrics metrics_upper, metrics_lower;
24227 face = FACE_FROM_ID (it->f, face_id);
24228 font = face->font ? face->font : FRAME_FONT (it->f);
24229 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24231 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24233 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24234 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24235 if (CONSP (acronym))
24236 acronym = XCAR (acronym);
24237 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24239 else
24241 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24242 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24243 str = buf;
24245 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24246 code[len] = font->driver->encode_char (font, str[len]);
24247 upper_len = (len + 1) / 2;
24248 font->driver->text_extents (font, code, upper_len,
24249 &metrics_upper);
24250 font->driver->text_extents (font, code + upper_len, len - upper_len,
24251 &metrics_lower);
24255 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24256 width = max (metrics_upper.width, metrics_lower.width) + 4;
24257 upper_xoff = upper_yoff = 2; /* the typical case */
24258 if (base_width >= width)
24260 /* Align the upper to the left, the lower to the right. */
24261 it->pixel_width = base_width;
24262 lower_xoff = base_width - 2 - metrics_lower.width;
24264 else
24266 /* Center the shorter one. */
24267 it->pixel_width = width;
24268 if (metrics_upper.width >= metrics_lower.width)
24269 lower_xoff = (width - metrics_lower.width) / 2;
24270 else
24272 /* FIXME: This code doesn't look right. It formerly was
24273 missing the "lower_xoff = 0;", which couldn't have
24274 been right since it left lower_xoff uninitialized. */
24275 lower_xoff = 0;
24276 upper_xoff = (width - metrics_upper.width) / 2;
24280 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24281 top, bottom, and between upper and lower strings. */
24282 height = (metrics_upper.ascent + metrics_upper.descent
24283 + metrics_lower.ascent + metrics_lower.descent) + 5;
24284 /* Center vertically.
24285 H:base_height, D:base_descent
24286 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24288 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24289 descent = D - H/2 + h/2;
24290 lower_yoff = descent - 2 - ld;
24291 upper_yoff = lower_yoff - la - 1 - ud; */
24292 ascent = - (it->descent - (base_height + height + 1) / 2);
24293 descent = it->descent - (base_height - height) / 2;
24294 lower_yoff = descent - 2 - metrics_lower.descent;
24295 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24296 - metrics_upper.descent);
24297 /* Don't make the height shorter than the base height. */
24298 if (height > base_height)
24300 it->ascent = ascent;
24301 it->descent = descent;
24305 it->phys_ascent = it->ascent;
24306 it->phys_descent = it->descent;
24307 if (it->glyph_row)
24308 append_glyphless_glyph (it, face_id, for_no_font, len,
24309 upper_xoff, upper_yoff,
24310 lower_xoff, lower_yoff);
24311 it->nglyphs = 1;
24312 take_vertical_position_into_account (it);
24316 /* RIF:
24317 Produce glyphs/get display metrics for the display element IT is
24318 loaded with. See the description of struct it in dispextern.h
24319 for an overview of struct it. */
24321 void
24322 x_produce_glyphs (struct it *it)
24324 int extra_line_spacing = it->extra_line_spacing;
24326 it->glyph_not_available_p = 0;
24328 if (it->what == IT_CHARACTER)
24330 XChar2b char2b;
24331 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24332 struct font *font = face->font;
24333 struct font_metrics *pcm = NULL;
24334 int boff; /* baseline offset */
24336 if (font == NULL)
24338 /* When no suitable font is found, display this character by
24339 the method specified in the first extra slot of
24340 Vglyphless_char_display. */
24341 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24343 xassert (it->what == IT_GLYPHLESS);
24344 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24345 goto done;
24348 boff = font->baseline_offset;
24349 if (font->vertical_centering)
24350 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24352 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24354 int stretched_p;
24356 it->nglyphs = 1;
24358 if (it->override_ascent >= 0)
24360 it->ascent = it->override_ascent;
24361 it->descent = it->override_descent;
24362 boff = it->override_boff;
24364 else
24366 it->ascent = FONT_BASE (font) + boff;
24367 it->descent = FONT_DESCENT (font) - boff;
24370 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24372 pcm = get_per_char_metric (font, &char2b);
24373 if (pcm->width == 0
24374 && pcm->rbearing == 0 && pcm->lbearing == 0)
24375 pcm = NULL;
24378 if (pcm)
24380 it->phys_ascent = pcm->ascent + boff;
24381 it->phys_descent = pcm->descent - boff;
24382 it->pixel_width = pcm->width;
24384 else
24386 it->glyph_not_available_p = 1;
24387 it->phys_ascent = it->ascent;
24388 it->phys_descent = it->descent;
24389 it->pixel_width = font->space_width;
24392 if (it->constrain_row_ascent_descent_p)
24394 if (it->descent > it->max_descent)
24396 it->ascent += it->descent - it->max_descent;
24397 it->descent = it->max_descent;
24399 if (it->ascent > it->max_ascent)
24401 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24402 it->ascent = it->max_ascent;
24404 it->phys_ascent = min (it->phys_ascent, it->ascent);
24405 it->phys_descent = min (it->phys_descent, it->descent);
24406 extra_line_spacing = 0;
24409 /* If this is a space inside a region of text with
24410 `space-width' property, change its width. */
24411 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24412 if (stretched_p)
24413 it->pixel_width *= XFLOATINT (it->space_width);
24415 /* If face has a box, add the box thickness to the character
24416 height. If character has a box line to the left and/or
24417 right, add the box line width to the character's width. */
24418 if (face->box != FACE_NO_BOX)
24420 int thick = face->box_line_width;
24422 if (thick > 0)
24424 it->ascent += thick;
24425 it->descent += thick;
24427 else
24428 thick = -thick;
24430 if (it->start_of_box_run_p)
24431 it->pixel_width += thick;
24432 if (it->end_of_box_run_p)
24433 it->pixel_width += thick;
24436 /* If face has an overline, add the height of the overline
24437 (1 pixel) and a 1 pixel margin to the character height. */
24438 if (face->overline_p)
24439 it->ascent += overline_margin;
24441 if (it->constrain_row_ascent_descent_p)
24443 if (it->ascent > it->max_ascent)
24444 it->ascent = it->max_ascent;
24445 if (it->descent > it->max_descent)
24446 it->descent = it->max_descent;
24449 take_vertical_position_into_account (it);
24451 /* If we have to actually produce glyphs, do it. */
24452 if (it->glyph_row)
24454 if (stretched_p)
24456 /* Translate a space with a `space-width' property
24457 into a stretch glyph. */
24458 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24459 / FONT_HEIGHT (font));
24460 append_stretch_glyph (it, it->object, it->pixel_width,
24461 it->ascent + it->descent, ascent);
24463 else
24464 append_glyph (it);
24466 /* If characters with lbearing or rbearing are displayed
24467 in this line, record that fact in a flag of the
24468 glyph row. This is used to optimize X output code. */
24469 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24470 it->glyph_row->contains_overlapping_glyphs_p = 1;
24472 if (! stretched_p && it->pixel_width == 0)
24473 /* We assure that all visible glyphs have at least 1-pixel
24474 width. */
24475 it->pixel_width = 1;
24477 else if (it->char_to_display == '\n')
24479 /* A newline has no width, but we need the height of the
24480 line. But if previous part of the line sets a height,
24481 don't increase that height */
24483 Lisp_Object height;
24484 Lisp_Object total_height = Qnil;
24486 it->override_ascent = -1;
24487 it->pixel_width = 0;
24488 it->nglyphs = 0;
24490 height = get_it_property (it, Qline_height);
24491 /* Split (line-height total-height) list */
24492 if (CONSP (height)
24493 && CONSP (XCDR (height))
24494 && NILP (XCDR (XCDR (height))))
24496 total_height = XCAR (XCDR (height));
24497 height = XCAR (height);
24499 height = calc_line_height_property (it, height, font, boff, 1);
24501 if (it->override_ascent >= 0)
24503 it->ascent = it->override_ascent;
24504 it->descent = it->override_descent;
24505 boff = it->override_boff;
24507 else
24509 it->ascent = FONT_BASE (font) + boff;
24510 it->descent = FONT_DESCENT (font) - boff;
24513 if (EQ (height, Qt))
24515 if (it->descent > it->max_descent)
24517 it->ascent += it->descent - it->max_descent;
24518 it->descent = it->max_descent;
24520 if (it->ascent > it->max_ascent)
24522 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24523 it->ascent = it->max_ascent;
24525 it->phys_ascent = min (it->phys_ascent, it->ascent);
24526 it->phys_descent = min (it->phys_descent, it->descent);
24527 it->constrain_row_ascent_descent_p = 1;
24528 extra_line_spacing = 0;
24530 else
24532 Lisp_Object spacing;
24534 it->phys_ascent = it->ascent;
24535 it->phys_descent = it->descent;
24537 if ((it->max_ascent > 0 || it->max_descent > 0)
24538 && face->box != FACE_NO_BOX
24539 && face->box_line_width > 0)
24541 it->ascent += face->box_line_width;
24542 it->descent += face->box_line_width;
24544 if (!NILP (height)
24545 && XINT (height) > it->ascent + it->descent)
24546 it->ascent = XINT (height) - it->descent;
24548 if (!NILP (total_height))
24549 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24550 else
24552 spacing = get_it_property (it, Qline_spacing);
24553 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24555 if (INTEGERP (spacing))
24557 extra_line_spacing = XINT (spacing);
24558 if (!NILP (total_height))
24559 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24563 else /* i.e. (it->char_to_display == '\t') */
24565 if (font->space_width > 0)
24567 int tab_width = it->tab_width * font->space_width;
24568 int x = it->current_x + it->continuation_lines_width;
24569 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24571 /* If the distance from the current position to the next tab
24572 stop is less than a space character width, use the
24573 tab stop after that. */
24574 if (next_tab_x - x < font->space_width)
24575 next_tab_x += tab_width;
24577 it->pixel_width = next_tab_x - x;
24578 it->nglyphs = 1;
24579 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24580 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24582 if (it->glyph_row)
24584 append_stretch_glyph (it, it->object, it->pixel_width,
24585 it->ascent + it->descent, it->ascent);
24588 else
24590 it->pixel_width = 0;
24591 it->nglyphs = 1;
24595 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24597 /* A static composition.
24599 Note: A composition is represented as one glyph in the
24600 glyph matrix. There are no padding glyphs.
24602 Important note: pixel_width, ascent, and descent are the
24603 values of what is drawn by draw_glyphs (i.e. the values of
24604 the overall glyphs composed). */
24605 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24606 int boff; /* baseline offset */
24607 struct composition *cmp = composition_table[it->cmp_it.id];
24608 int glyph_len = cmp->glyph_len;
24609 struct font *font = face->font;
24611 it->nglyphs = 1;
24613 /* If we have not yet calculated pixel size data of glyphs of
24614 the composition for the current face font, calculate them
24615 now. Theoretically, we have to check all fonts for the
24616 glyphs, but that requires much time and memory space. So,
24617 here we check only the font of the first glyph. This may
24618 lead to incorrect display, but it's very rare, and C-l
24619 (recenter-top-bottom) can correct the display anyway. */
24620 if (! cmp->font || cmp->font != font)
24622 /* Ascent and descent of the font of the first character
24623 of this composition (adjusted by baseline offset).
24624 Ascent and descent of overall glyphs should not be less
24625 than these, respectively. */
24626 int font_ascent, font_descent, font_height;
24627 /* Bounding box of the overall glyphs. */
24628 int leftmost, rightmost, lowest, highest;
24629 int lbearing, rbearing;
24630 int i, width, ascent, descent;
24631 int left_padded = 0, right_padded = 0;
24632 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24633 XChar2b char2b;
24634 struct font_metrics *pcm;
24635 int font_not_found_p;
24636 ptrdiff_t pos;
24638 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24639 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24640 break;
24641 if (glyph_len < cmp->glyph_len)
24642 right_padded = 1;
24643 for (i = 0; i < glyph_len; i++)
24645 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24646 break;
24647 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24649 if (i > 0)
24650 left_padded = 1;
24652 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24653 : IT_CHARPOS (*it));
24654 /* If no suitable font is found, use the default font. */
24655 font_not_found_p = font == NULL;
24656 if (font_not_found_p)
24658 face = face->ascii_face;
24659 font = face->font;
24661 boff = font->baseline_offset;
24662 if (font->vertical_centering)
24663 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24664 font_ascent = FONT_BASE (font) + boff;
24665 font_descent = FONT_DESCENT (font) - boff;
24666 font_height = FONT_HEIGHT (font);
24668 cmp->font = (void *) font;
24670 pcm = NULL;
24671 if (! font_not_found_p)
24673 get_char_face_and_encoding (it->f, c, it->face_id,
24674 &char2b, 0);
24675 pcm = get_per_char_metric (font, &char2b);
24678 /* Initialize the bounding box. */
24679 if (pcm)
24681 width = cmp->glyph_len > 0 ? pcm->width : 0;
24682 ascent = pcm->ascent;
24683 descent = pcm->descent;
24684 lbearing = pcm->lbearing;
24685 rbearing = pcm->rbearing;
24687 else
24689 width = cmp->glyph_len > 0 ? font->space_width : 0;
24690 ascent = FONT_BASE (font);
24691 descent = FONT_DESCENT (font);
24692 lbearing = 0;
24693 rbearing = width;
24696 rightmost = width;
24697 leftmost = 0;
24698 lowest = - descent + boff;
24699 highest = ascent + boff;
24701 if (! font_not_found_p
24702 && font->default_ascent
24703 && CHAR_TABLE_P (Vuse_default_ascent)
24704 && !NILP (Faref (Vuse_default_ascent,
24705 make_number (it->char_to_display))))
24706 highest = font->default_ascent + boff;
24708 /* Draw the first glyph at the normal position. It may be
24709 shifted to right later if some other glyphs are drawn
24710 at the left. */
24711 cmp->offsets[i * 2] = 0;
24712 cmp->offsets[i * 2 + 1] = boff;
24713 cmp->lbearing = lbearing;
24714 cmp->rbearing = rbearing;
24716 /* Set cmp->offsets for the remaining glyphs. */
24717 for (i++; i < glyph_len; i++)
24719 int left, right, btm, top;
24720 int ch = COMPOSITION_GLYPH (cmp, i);
24721 int face_id;
24722 struct face *this_face;
24724 if (ch == '\t')
24725 ch = ' ';
24726 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24727 this_face = FACE_FROM_ID (it->f, face_id);
24728 font = this_face->font;
24730 if (font == NULL)
24731 pcm = NULL;
24732 else
24734 get_char_face_and_encoding (it->f, ch, face_id,
24735 &char2b, 0);
24736 pcm = get_per_char_metric (font, &char2b);
24738 if (! pcm)
24739 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24740 else
24742 width = pcm->width;
24743 ascent = pcm->ascent;
24744 descent = pcm->descent;
24745 lbearing = pcm->lbearing;
24746 rbearing = pcm->rbearing;
24747 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24749 /* Relative composition with or without
24750 alternate chars. */
24751 left = (leftmost + rightmost - width) / 2;
24752 btm = - descent + boff;
24753 if (font->relative_compose
24754 && (! CHAR_TABLE_P (Vignore_relative_composition)
24755 || NILP (Faref (Vignore_relative_composition,
24756 make_number (ch)))))
24759 if (- descent >= font->relative_compose)
24760 /* One extra pixel between two glyphs. */
24761 btm = highest + 1;
24762 else if (ascent <= 0)
24763 /* One extra pixel between two glyphs. */
24764 btm = lowest - 1 - ascent - descent;
24767 else
24769 /* A composition rule is specified by an integer
24770 value that encodes global and new reference
24771 points (GREF and NREF). GREF and NREF are
24772 specified by numbers as below:
24774 0---1---2 -- ascent
24778 9--10--11 -- center
24780 ---3---4---5--- baseline
24782 6---7---8 -- descent
24784 int rule = COMPOSITION_RULE (cmp, i);
24785 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24787 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24788 grefx = gref % 3, nrefx = nref % 3;
24789 grefy = gref / 3, nrefy = nref / 3;
24790 if (xoff)
24791 xoff = font_height * (xoff - 128) / 256;
24792 if (yoff)
24793 yoff = font_height * (yoff - 128) / 256;
24795 left = (leftmost
24796 + grefx * (rightmost - leftmost) / 2
24797 - nrefx * width / 2
24798 + xoff);
24800 btm = ((grefy == 0 ? highest
24801 : grefy == 1 ? 0
24802 : grefy == 2 ? lowest
24803 : (highest + lowest) / 2)
24804 - (nrefy == 0 ? ascent + descent
24805 : nrefy == 1 ? descent - boff
24806 : nrefy == 2 ? 0
24807 : (ascent + descent) / 2)
24808 + yoff);
24811 cmp->offsets[i * 2] = left;
24812 cmp->offsets[i * 2 + 1] = btm + descent;
24814 /* Update the bounding box of the overall glyphs. */
24815 if (width > 0)
24817 right = left + width;
24818 if (left < leftmost)
24819 leftmost = left;
24820 if (right > rightmost)
24821 rightmost = right;
24823 top = btm + descent + ascent;
24824 if (top > highest)
24825 highest = top;
24826 if (btm < lowest)
24827 lowest = btm;
24829 if (cmp->lbearing > left + lbearing)
24830 cmp->lbearing = left + lbearing;
24831 if (cmp->rbearing < left + rbearing)
24832 cmp->rbearing = left + rbearing;
24836 /* If there are glyphs whose x-offsets are negative,
24837 shift all glyphs to the right and make all x-offsets
24838 non-negative. */
24839 if (leftmost < 0)
24841 for (i = 0; i < cmp->glyph_len; i++)
24842 cmp->offsets[i * 2] -= leftmost;
24843 rightmost -= leftmost;
24844 cmp->lbearing -= leftmost;
24845 cmp->rbearing -= leftmost;
24848 if (left_padded && cmp->lbearing < 0)
24850 for (i = 0; i < cmp->glyph_len; i++)
24851 cmp->offsets[i * 2] -= cmp->lbearing;
24852 rightmost -= cmp->lbearing;
24853 cmp->rbearing -= cmp->lbearing;
24854 cmp->lbearing = 0;
24856 if (right_padded && rightmost < cmp->rbearing)
24858 rightmost = cmp->rbearing;
24861 cmp->pixel_width = rightmost;
24862 cmp->ascent = highest;
24863 cmp->descent = - lowest;
24864 if (cmp->ascent < font_ascent)
24865 cmp->ascent = font_ascent;
24866 if (cmp->descent < font_descent)
24867 cmp->descent = font_descent;
24870 if (it->glyph_row
24871 && (cmp->lbearing < 0
24872 || cmp->rbearing > cmp->pixel_width))
24873 it->glyph_row->contains_overlapping_glyphs_p = 1;
24875 it->pixel_width = cmp->pixel_width;
24876 it->ascent = it->phys_ascent = cmp->ascent;
24877 it->descent = it->phys_descent = cmp->descent;
24878 if (face->box != FACE_NO_BOX)
24880 int thick = face->box_line_width;
24882 if (thick > 0)
24884 it->ascent += thick;
24885 it->descent += thick;
24887 else
24888 thick = - thick;
24890 if (it->start_of_box_run_p)
24891 it->pixel_width += thick;
24892 if (it->end_of_box_run_p)
24893 it->pixel_width += thick;
24896 /* If face has an overline, add the height of the overline
24897 (1 pixel) and a 1 pixel margin to the character height. */
24898 if (face->overline_p)
24899 it->ascent += overline_margin;
24901 take_vertical_position_into_account (it);
24902 if (it->ascent < 0)
24903 it->ascent = 0;
24904 if (it->descent < 0)
24905 it->descent = 0;
24907 if (it->glyph_row && cmp->glyph_len > 0)
24908 append_composite_glyph (it);
24910 else if (it->what == IT_COMPOSITION)
24912 /* A dynamic (automatic) composition. */
24913 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24914 Lisp_Object gstring;
24915 struct font_metrics metrics;
24917 it->nglyphs = 1;
24919 gstring = composition_gstring_from_id (it->cmp_it.id);
24920 it->pixel_width
24921 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24922 &metrics);
24923 if (it->glyph_row
24924 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24925 it->glyph_row->contains_overlapping_glyphs_p = 1;
24926 it->ascent = it->phys_ascent = metrics.ascent;
24927 it->descent = it->phys_descent = metrics.descent;
24928 if (face->box != FACE_NO_BOX)
24930 int thick = face->box_line_width;
24932 if (thick > 0)
24934 it->ascent += thick;
24935 it->descent += thick;
24937 else
24938 thick = - thick;
24940 if (it->start_of_box_run_p)
24941 it->pixel_width += thick;
24942 if (it->end_of_box_run_p)
24943 it->pixel_width += thick;
24945 /* If face has an overline, add the height of the overline
24946 (1 pixel) and a 1 pixel margin to the character height. */
24947 if (face->overline_p)
24948 it->ascent += overline_margin;
24949 take_vertical_position_into_account (it);
24950 if (it->ascent < 0)
24951 it->ascent = 0;
24952 if (it->descent < 0)
24953 it->descent = 0;
24955 if (it->glyph_row)
24956 append_composite_glyph (it);
24958 else if (it->what == IT_GLYPHLESS)
24959 produce_glyphless_glyph (it, 0, Qnil);
24960 else if (it->what == IT_IMAGE)
24961 produce_image_glyph (it);
24962 else if (it->what == IT_STRETCH)
24963 produce_stretch_glyph (it);
24965 done:
24966 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24967 because this isn't true for images with `:ascent 100'. */
24968 xassert (it->ascent >= 0 && it->descent >= 0);
24969 if (it->area == TEXT_AREA)
24970 it->current_x += it->pixel_width;
24972 if (extra_line_spacing > 0)
24974 it->descent += extra_line_spacing;
24975 if (extra_line_spacing > it->max_extra_line_spacing)
24976 it->max_extra_line_spacing = extra_line_spacing;
24979 it->max_ascent = max (it->max_ascent, it->ascent);
24980 it->max_descent = max (it->max_descent, it->descent);
24981 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24982 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24985 /* EXPORT for RIF:
24986 Output LEN glyphs starting at START at the nominal cursor position.
24987 Advance the nominal cursor over the text. The global variable
24988 updated_window contains the window being updated, updated_row is
24989 the glyph row being updated, and updated_area is the area of that
24990 row being updated. */
24992 void
24993 x_write_glyphs (struct glyph *start, int len)
24995 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24997 xassert (updated_window && updated_row);
24998 /* When the window is hscrolled, cursor hpos can legitimately be out
24999 of bounds, but we draw the cursor at the corresponding window
25000 margin in that case. */
25001 if (!updated_row->reversed_p && chpos < 0)
25002 chpos = 0;
25003 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25004 chpos = updated_row->used[TEXT_AREA] - 1;
25006 BLOCK_INPUT;
25008 /* Write glyphs. */
25010 hpos = start - updated_row->glyphs[updated_area];
25011 x = draw_glyphs (updated_window, output_cursor.x,
25012 updated_row, updated_area,
25013 hpos, hpos + len,
25014 DRAW_NORMAL_TEXT, 0);
25016 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25017 if (updated_area == TEXT_AREA
25018 && updated_window->phys_cursor_on_p
25019 && updated_window->phys_cursor.vpos == output_cursor.vpos
25020 && chpos >= hpos
25021 && chpos < hpos + len)
25022 updated_window->phys_cursor_on_p = 0;
25024 UNBLOCK_INPUT;
25026 /* Advance the output cursor. */
25027 output_cursor.hpos += len;
25028 output_cursor.x = x;
25032 /* EXPORT for RIF:
25033 Insert LEN glyphs from START at the nominal cursor position. */
25035 void
25036 x_insert_glyphs (struct glyph *start, int len)
25038 struct frame *f;
25039 struct window *w;
25040 int line_height, shift_by_width, shifted_region_width;
25041 struct glyph_row *row;
25042 struct glyph *glyph;
25043 int frame_x, frame_y;
25044 ptrdiff_t hpos;
25046 xassert (updated_window && updated_row);
25047 BLOCK_INPUT;
25048 w = updated_window;
25049 f = XFRAME (WINDOW_FRAME (w));
25051 /* Get the height of the line we are in. */
25052 row = updated_row;
25053 line_height = row->height;
25055 /* Get the width of the glyphs to insert. */
25056 shift_by_width = 0;
25057 for (glyph = start; glyph < start + len; ++glyph)
25058 shift_by_width += glyph->pixel_width;
25060 /* Get the width of the region to shift right. */
25061 shifted_region_width = (window_box_width (w, updated_area)
25062 - output_cursor.x
25063 - shift_by_width);
25065 /* Shift right. */
25066 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25067 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25069 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25070 line_height, shift_by_width);
25072 /* Write the glyphs. */
25073 hpos = start - row->glyphs[updated_area];
25074 draw_glyphs (w, output_cursor.x, row, updated_area,
25075 hpos, hpos + len,
25076 DRAW_NORMAL_TEXT, 0);
25078 /* Advance the output cursor. */
25079 output_cursor.hpos += len;
25080 output_cursor.x += shift_by_width;
25081 UNBLOCK_INPUT;
25085 /* EXPORT for RIF:
25086 Erase the current text line from the nominal cursor position
25087 (inclusive) to pixel column TO_X (exclusive). The idea is that
25088 everything from TO_X onward is already erased.
25090 TO_X is a pixel position relative to updated_area of
25091 updated_window. TO_X == -1 means clear to the end of this area. */
25093 void
25094 x_clear_end_of_line (int to_x)
25096 struct frame *f;
25097 struct window *w = updated_window;
25098 int max_x, min_y, max_y;
25099 int from_x, from_y, to_y;
25101 xassert (updated_window && updated_row);
25102 f = XFRAME (w->frame);
25104 if (updated_row->full_width_p)
25105 max_x = WINDOW_TOTAL_WIDTH (w);
25106 else
25107 max_x = window_box_width (w, updated_area);
25108 max_y = window_text_bottom_y (w);
25110 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25111 of window. For TO_X > 0, truncate to end of drawing area. */
25112 if (to_x == 0)
25113 return;
25114 else if (to_x < 0)
25115 to_x = max_x;
25116 else
25117 to_x = min (to_x, max_x);
25119 to_y = min (max_y, output_cursor.y + updated_row->height);
25121 /* Notice if the cursor will be cleared by this operation. */
25122 if (!updated_row->full_width_p)
25123 notice_overwritten_cursor (w, updated_area,
25124 output_cursor.x, -1,
25125 updated_row->y,
25126 MATRIX_ROW_BOTTOM_Y (updated_row));
25128 from_x = output_cursor.x;
25130 /* Translate to frame coordinates. */
25131 if (updated_row->full_width_p)
25133 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25134 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25136 else
25138 int area_left = window_box_left (w, updated_area);
25139 from_x += area_left;
25140 to_x += area_left;
25143 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25144 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25145 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25147 /* Prevent inadvertently clearing to end of the X window. */
25148 if (to_x > from_x && to_y > from_y)
25150 BLOCK_INPUT;
25151 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25152 to_x - from_x, to_y - from_y);
25153 UNBLOCK_INPUT;
25157 #endif /* HAVE_WINDOW_SYSTEM */
25161 /***********************************************************************
25162 Cursor types
25163 ***********************************************************************/
25165 /* Value is the internal representation of the specified cursor type
25166 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25167 of the bar cursor. */
25169 static enum text_cursor_kinds
25170 get_specified_cursor_type (Lisp_Object arg, int *width)
25172 enum text_cursor_kinds type;
25174 if (NILP (arg))
25175 return NO_CURSOR;
25177 if (EQ (arg, Qbox))
25178 return FILLED_BOX_CURSOR;
25180 if (EQ (arg, Qhollow))
25181 return HOLLOW_BOX_CURSOR;
25183 if (EQ (arg, Qbar))
25185 *width = 2;
25186 return BAR_CURSOR;
25189 if (CONSP (arg)
25190 && EQ (XCAR (arg), Qbar)
25191 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25193 *width = XINT (XCDR (arg));
25194 return BAR_CURSOR;
25197 if (EQ (arg, Qhbar))
25199 *width = 2;
25200 return HBAR_CURSOR;
25203 if (CONSP (arg)
25204 && EQ (XCAR (arg), Qhbar)
25205 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25207 *width = XINT (XCDR (arg));
25208 return HBAR_CURSOR;
25211 /* Treat anything unknown as "hollow box cursor".
25212 It was bad to signal an error; people have trouble fixing
25213 .Xdefaults with Emacs, when it has something bad in it. */
25214 type = HOLLOW_BOX_CURSOR;
25216 return type;
25219 /* Set the default cursor types for specified frame. */
25220 void
25221 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25223 int width = 1;
25224 Lisp_Object tem;
25226 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25227 FRAME_CURSOR_WIDTH (f) = width;
25229 /* By default, set up the blink-off state depending on the on-state. */
25231 tem = Fassoc (arg, Vblink_cursor_alist);
25232 if (!NILP (tem))
25234 FRAME_BLINK_OFF_CURSOR (f)
25235 = get_specified_cursor_type (XCDR (tem), &width);
25236 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25238 else
25239 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25243 #ifdef HAVE_WINDOW_SYSTEM
25245 /* Return the cursor we want to be displayed in window W. Return
25246 width of bar/hbar cursor through WIDTH arg. Return with
25247 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25248 (i.e. if the `system caret' should track this cursor).
25250 In a mini-buffer window, we want the cursor only to appear if we
25251 are reading input from this window. For the selected window, we
25252 want the cursor type given by the frame parameter or buffer local
25253 setting of cursor-type. If explicitly marked off, draw no cursor.
25254 In all other cases, we want a hollow box cursor. */
25256 static enum text_cursor_kinds
25257 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25258 int *active_cursor)
25260 struct frame *f = XFRAME (w->frame);
25261 struct buffer *b = XBUFFER (w->buffer);
25262 int cursor_type = DEFAULT_CURSOR;
25263 Lisp_Object alt_cursor;
25264 int non_selected = 0;
25266 *active_cursor = 1;
25268 /* Echo area */
25269 if (cursor_in_echo_area
25270 && FRAME_HAS_MINIBUF_P (f)
25271 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25273 if (w == XWINDOW (echo_area_window))
25275 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25277 *width = FRAME_CURSOR_WIDTH (f);
25278 return FRAME_DESIRED_CURSOR (f);
25280 else
25281 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25284 *active_cursor = 0;
25285 non_selected = 1;
25288 /* Detect a nonselected window or nonselected frame. */
25289 else if (w != XWINDOW (f->selected_window)
25290 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25292 *active_cursor = 0;
25294 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25295 return NO_CURSOR;
25297 non_selected = 1;
25300 /* Never display a cursor in a window in which cursor-type is nil. */
25301 if (NILP (BVAR (b, cursor_type)))
25302 return NO_CURSOR;
25304 /* Get the normal cursor type for this window. */
25305 if (EQ (BVAR (b, cursor_type), Qt))
25307 cursor_type = FRAME_DESIRED_CURSOR (f);
25308 *width = FRAME_CURSOR_WIDTH (f);
25310 else
25311 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25313 /* Use cursor-in-non-selected-windows instead
25314 for non-selected window or frame. */
25315 if (non_selected)
25317 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25318 if (!EQ (Qt, alt_cursor))
25319 return get_specified_cursor_type (alt_cursor, width);
25320 /* t means modify the normal cursor type. */
25321 if (cursor_type == FILLED_BOX_CURSOR)
25322 cursor_type = HOLLOW_BOX_CURSOR;
25323 else if (cursor_type == BAR_CURSOR && *width > 1)
25324 --*width;
25325 return cursor_type;
25328 /* Use normal cursor if not blinked off. */
25329 if (!w->cursor_off_p)
25331 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25333 if (cursor_type == FILLED_BOX_CURSOR)
25335 /* Using a block cursor on large images can be very annoying.
25336 So use a hollow cursor for "large" images.
25337 If image is not transparent (no mask), also use hollow cursor. */
25338 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25339 if (img != NULL && IMAGEP (img->spec))
25341 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25342 where N = size of default frame font size.
25343 This should cover most of the "tiny" icons people may use. */
25344 if (!img->mask
25345 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25346 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25347 cursor_type = HOLLOW_BOX_CURSOR;
25350 else if (cursor_type != NO_CURSOR)
25352 /* Display current only supports BOX and HOLLOW cursors for images.
25353 So for now, unconditionally use a HOLLOW cursor when cursor is
25354 not a solid box cursor. */
25355 cursor_type = HOLLOW_BOX_CURSOR;
25358 return cursor_type;
25361 /* Cursor is blinked off, so determine how to "toggle" it. */
25363 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25364 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25365 return get_specified_cursor_type (XCDR (alt_cursor), width);
25367 /* Then see if frame has specified a specific blink off cursor type. */
25368 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25370 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25371 return FRAME_BLINK_OFF_CURSOR (f);
25374 #if 0
25375 /* Some people liked having a permanently visible blinking cursor,
25376 while others had very strong opinions against it. So it was
25377 decided to remove it. KFS 2003-09-03 */
25379 /* Finally perform built-in cursor blinking:
25380 filled box <-> hollow box
25381 wide [h]bar <-> narrow [h]bar
25382 narrow [h]bar <-> no cursor
25383 other type <-> no cursor */
25385 if (cursor_type == FILLED_BOX_CURSOR)
25386 return HOLLOW_BOX_CURSOR;
25388 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25390 *width = 1;
25391 return cursor_type;
25393 #endif
25395 return NO_CURSOR;
25399 /* Notice when the text cursor of window W has been completely
25400 overwritten by a drawing operation that outputs glyphs in AREA
25401 starting at X0 and ending at X1 in the line starting at Y0 and
25402 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25403 the rest of the line after X0 has been written. Y coordinates
25404 are window-relative. */
25406 static void
25407 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25408 int x0, int x1, int y0, int y1)
25410 int cx0, cx1, cy0, cy1;
25411 struct glyph_row *row;
25413 if (!w->phys_cursor_on_p)
25414 return;
25415 if (area != TEXT_AREA)
25416 return;
25418 if (w->phys_cursor.vpos < 0
25419 || w->phys_cursor.vpos >= w->current_matrix->nrows
25420 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25421 !(row->enabled_p && row->displays_text_p)))
25422 return;
25424 if (row->cursor_in_fringe_p)
25426 row->cursor_in_fringe_p = 0;
25427 draw_fringe_bitmap (w, row, row->reversed_p);
25428 w->phys_cursor_on_p = 0;
25429 return;
25432 cx0 = w->phys_cursor.x;
25433 cx1 = cx0 + w->phys_cursor_width;
25434 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25435 return;
25437 /* The cursor image will be completely removed from the
25438 screen if the output area intersects the cursor area in
25439 y-direction. When we draw in [y0 y1[, and some part of
25440 the cursor is at y < y0, that part must have been drawn
25441 before. When scrolling, the cursor is erased before
25442 actually scrolling, so we don't come here. When not
25443 scrolling, the rows above the old cursor row must have
25444 changed, and in this case these rows must have written
25445 over the cursor image.
25447 Likewise if part of the cursor is below y1, with the
25448 exception of the cursor being in the first blank row at
25449 the buffer and window end because update_text_area
25450 doesn't draw that row. (Except when it does, but
25451 that's handled in update_text_area.) */
25453 cy0 = w->phys_cursor.y;
25454 cy1 = cy0 + w->phys_cursor_height;
25455 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25456 return;
25458 w->phys_cursor_on_p = 0;
25461 #endif /* HAVE_WINDOW_SYSTEM */
25464 /************************************************************************
25465 Mouse Face
25466 ************************************************************************/
25468 #ifdef HAVE_WINDOW_SYSTEM
25470 /* EXPORT for RIF:
25471 Fix the display of area AREA of overlapping row ROW in window W
25472 with respect to the overlapping part OVERLAPS. */
25474 void
25475 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25476 enum glyph_row_area area, int overlaps)
25478 int i, x;
25480 BLOCK_INPUT;
25482 x = 0;
25483 for (i = 0; i < row->used[area];)
25485 if (row->glyphs[area][i].overlaps_vertically_p)
25487 int start = i, start_x = x;
25491 x += row->glyphs[area][i].pixel_width;
25492 ++i;
25494 while (i < row->used[area]
25495 && row->glyphs[area][i].overlaps_vertically_p);
25497 draw_glyphs (w, start_x, row, area,
25498 start, i,
25499 DRAW_NORMAL_TEXT, overlaps);
25501 else
25503 x += row->glyphs[area][i].pixel_width;
25504 ++i;
25508 UNBLOCK_INPUT;
25512 /* EXPORT:
25513 Draw the cursor glyph of window W in glyph row ROW. See the
25514 comment of draw_glyphs for the meaning of HL. */
25516 void
25517 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25518 enum draw_glyphs_face hl)
25520 /* If cursor hpos is out of bounds, don't draw garbage. This can
25521 happen in mini-buffer windows when switching between echo area
25522 glyphs and mini-buffer. */
25523 if ((row->reversed_p
25524 ? (w->phys_cursor.hpos >= 0)
25525 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25527 int on_p = w->phys_cursor_on_p;
25528 int x1;
25529 int hpos = w->phys_cursor.hpos;
25531 /* When the window is hscrolled, cursor hpos can legitimately be
25532 out of bounds, but we draw the cursor at the corresponding
25533 window margin in that case. */
25534 if (!row->reversed_p && hpos < 0)
25535 hpos = 0;
25536 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25537 hpos = row->used[TEXT_AREA] - 1;
25539 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25540 hl, 0);
25541 w->phys_cursor_on_p = on_p;
25543 if (hl == DRAW_CURSOR)
25544 w->phys_cursor_width = x1 - w->phys_cursor.x;
25545 /* When we erase the cursor, and ROW is overlapped by other
25546 rows, make sure that these overlapping parts of other rows
25547 are redrawn. */
25548 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25550 w->phys_cursor_width = x1 - w->phys_cursor.x;
25552 if (row > w->current_matrix->rows
25553 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25554 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25555 OVERLAPS_ERASED_CURSOR);
25557 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25558 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25559 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25560 OVERLAPS_ERASED_CURSOR);
25566 /* EXPORT:
25567 Erase the image of a cursor of window W from the screen. */
25569 void
25570 erase_phys_cursor (struct window *w)
25572 struct frame *f = XFRAME (w->frame);
25573 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25574 int hpos = w->phys_cursor.hpos;
25575 int vpos = w->phys_cursor.vpos;
25576 int mouse_face_here_p = 0;
25577 struct glyph_matrix *active_glyphs = w->current_matrix;
25578 struct glyph_row *cursor_row;
25579 struct glyph *cursor_glyph;
25580 enum draw_glyphs_face hl;
25582 /* No cursor displayed or row invalidated => nothing to do on the
25583 screen. */
25584 if (w->phys_cursor_type == NO_CURSOR)
25585 goto mark_cursor_off;
25587 /* VPOS >= active_glyphs->nrows means that window has been resized.
25588 Don't bother to erase the cursor. */
25589 if (vpos >= active_glyphs->nrows)
25590 goto mark_cursor_off;
25592 /* If row containing cursor is marked invalid, there is nothing we
25593 can do. */
25594 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25595 if (!cursor_row->enabled_p)
25596 goto mark_cursor_off;
25598 /* If line spacing is > 0, old cursor may only be partially visible in
25599 window after split-window. So adjust visible height. */
25600 cursor_row->visible_height = min (cursor_row->visible_height,
25601 window_text_bottom_y (w) - cursor_row->y);
25603 /* If row is completely invisible, don't attempt to delete a cursor which
25604 isn't there. This can happen if cursor is at top of a window, and
25605 we switch to a buffer with a header line in that window. */
25606 if (cursor_row->visible_height <= 0)
25607 goto mark_cursor_off;
25609 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25610 if (cursor_row->cursor_in_fringe_p)
25612 cursor_row->cursor_in_fringe_p = 0;
25613 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25614 goto mark_cursor_off;
25617 /* This can happen when the new row is shorter than the old one.
25618 In this case, either draw_glyphs or clear_end_of_line
25619 should have cleared the cursor. Note that we wouldn't be
25620 able to erase the cursor in this case because we don't have a
25621 cursor glyph at hand. */
25622 if ((cursor_row->reversed_p
25623 ? (w->phys_cursor.hpos < 0)
25624 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25625 goto mark_cursor_off;
25627 /* When the window is hscrolled, cursor hpos can legitimately be out
25628 of bounds, but we draw the cursor at the corresponding window
25629 margin in that case. */
25630 if (!cursor_row->reversed_p && hpos < 0)
25631 hpos = 0;
25632 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25633 hpos = cursor_row->used[TEXT_AREA] - 1;
25635 /* If the cursor is in the mouse face area, redisplay that when
25636 we clear the cursor. */
25637 if (! NILP (hlinfo->mouse_face_window)
25638 && coords_in_mouse_face_p (w, hpos, vpos)
25639 /* Don't redraw the cursor's spot in mouse face if it is at the
25640 end of a line (on a newline). The cursor appears there, but
25641 mouse highlighting does not. */
25642 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25643 mouse_face_here_p = 1;
25645 /* Maybe clear the display under the cursor. */
25646 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25648 int x, y, left_x;
25649 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25650 int width;
25652 cursor_glyph = get_phys_cursor_glyph (w);
25653 if (cursor_glyph == NULL)
25654 goto mark_cursor_off;
25656 width = cursor_glyph->pixel_width;
25657 left_x = window_box_left_offset (w, TEXT_AREA);
25658 x = w->phys_cursor.x;
25659 if (x < left_x)
25660 width -= left_x - x;
25661 width = min (width, window_box_width (w, TEXT_AREA) - x);
25662 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25663 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25665 if (width > 0)
25666 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25669 /* Erase the cursor by redrawing the character underneath it. */
25670 if (mouse_face_here_p)
25671 hl = DRAW_MOUSE_FACE;
25672 else
25673 hl = DRAW_NORMAL_TEXT;
25674 draw_phys_cursor_glyph (w, cursor_row, hl);
25676 mark_cursor_off:
25677 w->phys_cursor_on_p = 0;
25678 w->phys_cursor_type = NO_CURSOR;
25682 /* EXPORT:
25683 Display or clear cursor of window W. If ON is zero, clear the
25684 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25685 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25687 void
25688 display_and_set_cursor (struct window *w, int on,
25689 int hpos, int vpos, int x, int y)
25691 struct frame *f = XFRAME (w->frame);
25692 int new_cursor_type;
25693 int new_cursor_width;
25694 int active_cursor;
25695 struct glyph_row *glyph_row;
25696 struct glyph *glyph;
25698 /* This is pointless on invisible frames, and dangerous on garbaged
25699 windows and frames; in the latter case, the frame or window may
25700 be in the midst of changing its size, and x and y may be off the
25701 window. */
25702 if (! FRAME_VISIBLE_P (f)
25703 || FRAME_GARBAGED_P (f)
25704 || vpos >= w->current_matrix->nrows
25705 || hpos >= w->current_matrix->matrix_w)
25706 return;
25708 /* If cursor is off and we want it off, return quickly. */
25709 if (!on && !w->phys_cursor_on_p)
25710 return;
25712 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25713 /* If cursor row is not enabled, we don't really know where to
25714 display the cursor. */
25715 if (!glyph_row->enabled_p)
25717 w->phys_cursor_on_p = 0;
25718 return;
25721 glyph = NULL;
25722 if (!glyph_row->exact_window_width_line_p
25723 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25724 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25726 xassert (interrupt_input_blocked);
25728 /* Set new_cursor_type to the cursor we want to be displayed. */
25729 new_cursor_type = get_window_cursor_type (w, glyph,
25730 &new_cursor_width, &active_cursor);
25732 /* If cursor is currently being shown and we don't want it to be or
25733 it is in the wrong place, or the cursor type is not what we want,
25734 erase it. */
25735 if (w->phys_cursor_on_p
25736 && (!on
25737 || w->phys_cursor.x != x
25738 || w->phys_cursor.y != y
25739 || new_cursor_type != w->phys_cursor_type
25740 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25741 && new_cursor_width != w->phys_cursor_width)))
25742 erase_phys_cursor (w);
25744 /* Don't check phys_cursor_on_p here because that flag is only set
25745 to zero in some cases where we know that the cursor has been
25746 completely erased, to avoid the extra work of erasing the cursor
25747 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25748 still not be visible, or it has only been partly erased. */
25749 if (on)
25751 w->phys_cursor_ascent = glyph_row->ascent;
25752 w->phys_cursor_height = glyph_row->height;
25754 /* Set phys_cursor_.* before x_draw_.* is called because some
25755 of them may need the information. */
25756 w->phys_cursor.x = x;
25757 w->phys_cursor.y = glyph_row->y;
25758 w->phys_cursor.hpos = hpos;
25759 w->phys_cursor.vpos = vpos;
25762 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25763 new_cursor_type, new_cursor_width,
25764 on, active_cursor);
25768 /* Switch the display of W's cursor on or off, according to the value
25769 of ON. */
25771 static void
25772 update_window_cursor (struct window *w, int on)
25774 /* Don't update cursor in windows whose frame is in the process
25775 of being deleted. */
25776 if (w->current_matrix)
25778 int hpos = w->phys_cursor.hpos;
25779 int vpos = w->phys_cursor.vpos;
25780 struct glyph_row *row;
25782 if (vpos >= w->current_matrix->nrows
25783 || hpos >= w->current_matrix->matrix_w)
25784 return;
25786 row = MATRIX_ROW (w->current_matrix, vpos);
25788 /* When the window is hscrolled, cursor hpos can legitimately be
25789 out of bounds, but we draw the cursor at the corresponding
25790 window margin in that case. */
25791 if (!row->reversed_p && hpos < 0)
25792 hpos = 0;
25793 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25794 hpos = row->used[TEXT_AREA] - 1;
25796 BLOCK_INPUT;
25797 display_and_set_cursor (w, on, hpos, vpos,
25798 w->phys_cursor.x, w->phys_cursor.y);
25799 UNBLOCK_INPUT;
25804 /* Call update_window_cursor with parameter ON_P on all leaf windows
25805 in the window tree rooted at W. */
25807 static void
25808 update_cursor_in_window_tree (struct window *w, int on_p)
25810 while (w)
25812 if (!NILP (w->hchild))
25813 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25814 else if (!NILP (w->vchild))
25815 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25816 else
25817 update_window_cursor (w, on_p);
25819 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25824 /* EXPORT:
25825 Display the cursor on window W, or clear it, according to ON_P.
25826 Don't change the cursor's position. */
25828 void
25829 x_update_cursor (struct frame *f, int on_p)
25831 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25835 /* EXPORT:
25836 Clear the cursor of window W to background color, and mark the
25837 cursor as not shown. This is used when the text where the cursor
25838 is about to be rewritten. */
25840 void
25841 x_clear_cursor (struct window *w)
25843 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25844 update_window_cursor (w, 0);
25847 #endif /* HAVE_WINDOW_SYSTEM */
25849 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25850 and MSDOS. */
25851 static void
25852 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25853 int start_hpos, int end_hpos,
25854 enum draw_glyphs_face draw)
25856 #ifdef HAVE_WINDOW_SYSTEM
25857 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25859 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25860 return;
25862 #endif
25863 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
25864 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25865 #endif
25868 /* Display the active region described by mouse_face_* according to DRAW. */
25870 static void
25871 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25873 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25874 struct frame *f = XFRAME (WINDOW_FRAME (w));
25876 if (/* If window is in the process of being destroyed, don't bother
25877 to do anything. */
25878 w->current_matrix != NULL
25879 /* Don't update mouse highlight if hidden */
25880 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25881 /* Recognize when we are called to operate on rows that don't exist
25882 anymore. This can happen when a window is split. */
25883 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25885 int phys_cursor_on_p = w->phys_cursor_on_p;
25886 struct glyph_row *row, *first, *last;
25888 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25889 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25891 for (row = first; row <= last && row->enabled_p; ++row)
25893 int start_hpos, end_hpos, start_x;
25895 /* For all but the first row, the highlight starts at column 0. */
25896 if (row == first)
25898 /* R2L rows have BEG and END in reversed order, but the
25899 screen drawing geometry is always left to right. So
25900 we need to mirror the beginning and end of the
25901 highlighted area in R2L rows. */
25902 if (!row->reversed_p)
25904 start_hpos = hlinfo->mouse_face_beg_col;
25905 start_x = hlinfo->mouse_face_beg_x;
25907 else if (row == last)
25909 start_hpos = hlinfo->mouse_face_end_col;
25910 start_x = hlinfo->mouse_face_end_x;
25912 else
25914 start_hpos = 0;
25915 start_x = 0;
25918 else if (row->reversed_p && row == last)
25920 start_hpos = hlinfo->mouse_face_end_col;
25921 start_x = hlinfo->mouse_face_end_x;
25923 else
25925 start_hpos = 0;
25926 start_x = 0;
25929 if (row == last)
25931 if (!row->reversed_p)
25932 end_hpos = hlinfo->mouse_face_end_col;
25933 else if (row == first)
25934 end_hpos = hlinfo->mouse_face_beg_col;
25935 else
25937 end_hpos = row->used[TEXT_AREA];
25938 if (draw == DRAW_NORMAL_TEXT)
25939 row->fill_line_p = 1; /* Clear to end of line */
25942 else if (row->reversed_p && row == first)
25943 end_hpos = hlinfo->mouse_face_beg_col;
25944 else
25946 end_hpos = row->used[TEXT_AREA];
25947 if (draw == DRAW_NORMAL_TEXT)
25948 row->fill_line_p = 1; /* Clear to end of line */
25951 if (end_hpos > start_hpos)
25953 draw_row_with_mouse_face (w, start_x, row,
25954 start_hpos, end_hpos, draw);
25956 row->mouse_face_p
25957 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25961 #ifdef HAVE_WINDOW_SYSTEM
25962 /* When we've written over the cursor, arrange for it to
25963 be displayed again. */
25964 if (FRAME_WINDOW_P (f)
25965 && phys_cursor_on_p && !w->phys_cursor_on_p)
25967 int hpos = w->phys_cursor.hpos;
25969 /* When the window is hscrolled, cursor hpos can legitimately be
25970 out of bounds, but we draw the cursor at the corresponding
25971 window margin in that case. */
25972 if (!row->reversed_p && hpos < 0)
25973 hpos = 0;
25974 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25975 hpos = row->used[TEXT_AREA] - 1;
25977 BLOCK_INPUT;
25978 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25979 w->phys_cursor.x, w->phys_cursor.y);
25980 UNBLOCK_INPUT;
25982 #endif /* HAVE_WINDOW_SYSTEM */
25985 #ifdef HAVE_WINDOW_SYSTEM
25986 /* Change the mouse cursor. */
25987 if (FRAME_WINDOW_P (f))
25989 if (draw == DRAW_NORMAL_TEXT
25990 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25991 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25992 else if (draw == DRAW_MOUSE_FACE)
25993 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25994 else
25995 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25997 #endif /* HAVE_WINDOW_SYSTEM */
26000 /* EXPORT:
26001 Clear out the mouse-highlighted active region.
26002 Redraw it un-highlighted first. Value is non-zero if mouse
26003 face was actually drawn unhighlighted. */
26006 clear_mouse_face (Mouse_HLInfo *hlinfo)
26008 int cleared = 0;
26010 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26012 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26013 cleared = 1;
26016 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26017 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26018 hlinfo->mouse_face_window = Qnil;
26019 hlinfo->mouse_face_overlay = Qnil;
26020 return cleared;
26023 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26024 within the mouse face on that window. */
26025 static int
26026 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26028 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26030 /* Quickly resolve the easy cases. */
26031 if (!(WINDOWP (hlinfo->mouse_face_window)
26032 && XWINDOW (hlinfo->mouse_face_window) == w))
26033 return 0;
26034 if (vpos < hlinfo->mouse_face_beg_row
26035 || vpos > hlinfo->mouse_face_end_row)
26036 return 0;
26037 if (vpos > hlinfo->mouse_face_beg_row
26038 && vpos < hlinfo->mouse_face_end_row)
26039 return 1;
26041 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26043 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26045 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26046 return 1;
26048 else if ((vpos == hlinfo->mouse_face_beg_row
26049 && hpos >= hlinfo->mouse_face_beg_col)
26050 || (vpos == hlinfo->mouse_face_end_row
26051 && hpos < hlinfo->mouse_face_end_col))
26052 return 1;
26054 else
26056 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26058 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26059 return 1;
26061 else if ((vpos == hlinfo->mouse_face_beg_row
26062 && hpos <= hlinfo->mouse_face_beg_col)
26063 || (vpos == hlinfo->mouse_face_end_row
26064 && hpos > hlinfo->mouse_face_end_col))
26065 return 1;
26067 return 0;
26071 /* EXPORT:
26072 Non-zero if physical cursor of window W is within mouse face. */
26075 cursor_in_mouse_face_p (struct window *w)
26077 int hpos = w->phys_cursor.hpos;
26078 int vpos = w->phys_cursor.vpos;
26079 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26081 /* When the window is hscrolled, cursor hpos can legitimately be out
26082 of bounds, but we draw the cursor at the corresponding window
26083 margin in that case. */
26084 if (!row->reversed_p && hpos < 0)
26085 hpos = 0;
26086 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26087 hpos = row->used[TEXT_AREA] - 1;
26089 return coords_in_mouse_face_p (w, hpos, vpos);
26094 /* Find the glyph rows START_ROW and END_ROW of window W that display
26095 characters between buffer positions START_CHARPOS and END_CHARPOS
26096 (excluding END_CHARPOS). DISP_STRING is a display string that
26097 covers these buffer positions. This is similar to
26098 row_containing_pos, but is more accurate when bidi reordering makes
26099 buffer positions change non-linearly with glyph rows. */
26100 static void
26101 rows_from_pos_range (struct window *w,
26102 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26103 Lisp_Object disp_string,
26104 struct glyph_row **start, struct glyph_row **end)
26106 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26107 int last_y = window_text_bottom_y (w);
26108 struct glyph_row *row;
26110 *start = NULL;
26111 *end = NULL;
26113 while (!first->enabled_p
26114 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26115 first++;
26117 /* Find the START row. */
26118 for (row = first;
26119 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26120 row++)
26122 /* A row can potentially be the START row if the range of the
26123 characters it displays intersects the range
26124 [START_CHARPOS..END_CHARPOS). */
26125 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26126 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26127 /* See the commentary in row_containing_pos, for the
26128 explanation of the complicated way to check whether
26129 some position is beyond the end of the characters
26130 displayed by a row. */
26131 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26132 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26133 && !row->ends_at_zv_p
26134 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26135 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26136 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26137 && !row->ends_at_zv_p
26138 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26140 /* Found a candidate row. Now make sure at least one of the
26141 glyphs it displays has a charpos from the range
26142 [START_CHARPOS..END_CHARPOS).
26144 This is not obvious because bidi reordering could make
26145 buffer positions of a row be 1,2,3,102,101,100, and if we
26146 want to highlight characters in [50..60), we don't want
26147 this row, even though [50..60) does intersect [1..103),
26148 the range of character positions given by the row's start
26149 and end positions. */
26150 struct glyph *g = row->glyphs[TEXT_AREA];
26151 struct glyph *e = g + row->used[TEXT_AREA];
26153 while (g < e)
26155 if (((BUFFERP (g->object) || INTEGERP (g->object))
26156 && start_charpos <= g->charpos && g->charpos < end_charpos)
26157 /* A glyph that comes from DISP_STRING is by
26158 definition to be highlighted. */
26159 || EQ (g->object, disp_string))
26160 *start = row;
26161 g++;
26163 if (*start)
26164 break;
26168 /* Find the END row. */
26169 if (!*start
26170 /* If the last row is partially visible, start looking for END
26171 from that row, instead of starting from FIRST. */
26172 && !(row->enabled_p
26173 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26174 row = first;
26175 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26177 struct glyph_row *next = row + 1;
26178 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26180 if (!next->enabled_p
26181 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26182 /* The first row >= START whose range of displayed characters
26183 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26184 is the row END + 1. */
26185 || (start_charpos < next_start
26186 && end_charpos < next_start)
26187 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26188 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26189 && !next->ends_at_zv_p
26190 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26191 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26192 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26193 && !next->ends_at_zv_p
26194 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26196 *end = row;
26197 break;
26199 else
26201 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26202 but none of the characters it displays are in the range, it is
26203 also END + 1. */
26204 struct glyph *g = next->glyphs[TEXT_AREA];
26205 struct glyph *s = g;
26206 struct glyph *e = g + next->used[TEXT_AREA];
26208 while (g < e)
26210 if (((BUFFERP (g->object) || INTEGERP (g->object))
26211 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26212 /* If the buffer position of the first glyph in
26213 the row is equal to END_CHARPOS, it means
26214 the last character to be highlighted is the
26215 newline of ROW, and we must consider NEXT as
26216 END, not END+1. */
26217 || (((!next->reversed_p && g == s)
26218 || (next->reversed_p && g == e - 1))
26219 && (g->charpos == end_charpos
26220 /* Special case for when NEXT is an
26221 empty line at ZV. */
26222 || (g->charpos == -1
26223 && !row->ends_at_zv_p
26224 && next_start == end_charpos)))))
26225 /* A glyph that comes from DISP_STRING is by
26226 definition to be highlighted. */
26227 || EQ (g->object, disp_string))
26228 break;
26229 g++;
26231 if (g == e)
26233 *end = row;
26234 break;
26236 /* The first row that ends at ZV must be the last to be
26237 highlighted. */
26238 else if (next->ends_at_zv_p)
26240 *end = next;
26241 break;
26247 /* This function sets the mouse_face_* elements of HLINFO, assuming
26248 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26249 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26250 for the overlay or run of text properties specifying the mouse
26251 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26252 before-string and after-string that must also be highlighted.
26253 DISP_STRING, if non-nil, is a display string that may cover some
26254 or all of the highlighted text. */
26256 static void
26257 mouse_face_from_buffer_pos (Lisp_Object window,
26258 Mouse_HLInfo *hlinfo,
26259 ptrdiff_t mouse_charpos,
26260 ptrdiff_t start_charpos,
26261 ptrdiff_t end_charpos,
26262 Lisp_Object before_string,
26263 Lisp_Object after_string,
26264 Lisp_Object disp_string)
26266 struct window *w = XWINDOW (window);
26267 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26268 struct glyph_row *r1, *r2;
26269 struct glyph *glyph, *end;
26270 ptrdiff_t ignore, pos;
26271 int x;
26273 xassert (NILP (disp_string) || STRINGP (disp_string));
26274 xassert (NILP (before_string) || STRINGP (before_string));
26275 xassert (NILP (after_string) || STRINGP (after_string));
26277 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26278 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26279 if (r1 == NULL)
26280 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26281 /* If the before-string or display-string contains newlines,
26282 rows_from_pos_range skips to its last row. Move back. */
26283 if (!NILP (before_string) || !NILP (disp_string))
26285 struct glyph_row *prev;
26286 while ((prev = r1 - 1, prev >= first)
26287 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26288 && prev->used[TEXT_AREA] > 0)
26290 struct glyph *beg = prev->glyphs[TEXT_AREA];
26291 glyph = beg + prev->used[TEXT_AREA];
26292 while (--glyph >= beg && INTEGERP (glyph->object));
26293 if (glyph < beg
26294 || !(EQ (glyph->object, before_string)
26295 || EQ (glyph->object, disp_string)))
26296 break;
26297 r1 = prev;
26300 if (r2 == NULL)
26302 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26303 hlinfo->mouse_face_past_end = 1;
26305 else if (!NILP (after_string))
26307 /* If the after-string has newlines, advance to its last row. */
26308 struct glyph_row *next;
26309 struct glyph_row *last
26310 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26312 for (next = r2 + 1;
26313 next <= last
26314 && next->used[TEXT_AREA] > 0
26315 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26316 ++next)
26317 r2 = next;
26319 /* The rest of the display engine assumes that mouse_face_beg_row is
26320 either above mouse_face_end_row or identical to it. But with
26321 bidi-reordered continued lines, the row for START_CHARPOS could
26322 be below the row for END_CHARPOS. If so, swap the rows and store
26323 them in correct order. */
26324 if (r1->y > r2->y)
26326 struct glyph_row *tem = r2;
26328 r2 = r1;
26329 r1 = tem;
26332 hlinfo->mouse_face_beg_y = r1->y;
26333 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26334 hlinfo->mouse_face_end_y = r2->y;
26335 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26337 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26338 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26339 could be anywhere in the row and in any order. The strategy
26340 below is to find the leftmost and the rightmost glyph that
26341 belongs to either of these 3 strings, or whose position is
26342 between START_CHARPOS and END_CHARPOS, and highlight all the
26343 glyphs between those two. This may cover more than just the text
26344 between START_CHARPOS and END_CHARPOS if the range of characters
26345 strides the bidi level boundary, e.g. if the beginning is in R2L
26346 text while the end is in L2R text or vice versa. */
26347 if (!r1->reversed_p)
26349 /* This row is in a left to right paragraph. Scan it left to
26350 right. */
26351 glyph = r1->glyphs[TEXT_AREA];
26352 end = glyph + r1->used[TEXT_AREA];
26353 x = r1->x;
26355 /* Skip truncation glyphs at the start of the glyph row. */
26356 if (r1->displays_text_p)
26357 for (; glyph < end
26358 && INTEGERP (glyph->object)
26359 && glyph->charpos < 0;
26360 ++glyph)
26361 x += glyph->pixel_width;
26363 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26364 or DISP_STRING, and the first glyph from buffer whose
26365 position is between START_CHARPOS and END_CHARPOS. */
26366 for (; glyph < end
26367 && !INTEGERP (glyph->object)
26368 && !EQ (glyph->object, disp_string)
26369 && !(BUFFERP (glyph->object)
26370 && (glyph->charpos >= start_charpos
26371 && glyph->charpos < end_charpos));
26372 ++glyph)
26374 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26375 are present at buffer positions between START_CHARPOS and
26376 END_CHARPOS, or if they come from an overlay. */
26377 if (EQ (glyph->object, before_string))
26379 pos = string_buffer_position (before_string,
26380 start_charpos);
26381 /* If pos == 0, it means before_string came from an
26382 overlay, not from a buffer position. */
26383 if (!pos || (pos >= start_charpos && pos < end_charpos))
26384 break;
26386 else if (EQ (glyph->object, after_string))
26388 pos = string_buffer_position (after_string, end_charpos);
26389 if (!pos || (pos >= start_charpos && pos < end_charpos))
26390 break;
26392 x += glyph->pixel_width;
26394 hlinfo->mouse_face_beg_x = x;
26395 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26397 else
26399 /* This row is in a right to left paragraph. Scan it right to
26400 left. */
26401 struct glyph *g;
26403 end = r1->glyphs[TEXT_AREA] - 1;
26404 glyph = end + r1->used[TEXT_AREA];
26406 /* Skip truncation glyphs at the start of the glyph row. */
26407 if (r1->displays_text_p)
26408 for (; glyph > end
26409 && INTEGERP (glyph->object)
26410 && glyph->charpos < 0;
26411 --glyph)
26414 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26415 or DISP_STRING, and the first glyph from buffer whose
26416 position is between START_CHARPOS and END_CHARPOS. */
26417 for (; glyph > end
26418 && !INTEGERP (glyph->object)
26419 && !EQ (glyph->object, disp_string)
26420 && !(BUFFERP (glyph->object)
26421 && (glyph->charpos >= start_charpos
26422 && glyph->charpos < end_charpos));
26423 --glyph)
26425 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26426 are present at buffer positions between START_CHARPOS and
26427 END_CHARPOS, or if they come from an overlay. */
26428 if (EQ (glyph->object, before_string))
26430 pos = string_buffer_position (before_string, start_charpos);
26431 /* If pos == 0, it means before_string came from an
26432 overlay, not from a buffer position. */
26433 if (!pos || (pos >= start_charpos && pos < end_charpos))
26434 break;
26436 else if (EQ (glyph->object, after_string))
26438 pos = string_buffer_position (after_string, end_charpos);
26439 if (!pos || (pos >= start_charpos && pos < end_charpos))
26440 break;
26444 glyph++; /* first glyph to the right of the highlighted area */
26445 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26446 x += g->pixel_width;
26447 hlinfo->mouse_face_beg_x = x;
26448 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26451 /* If the highlight ends in a different row, compute GLYPH and END
26452 for the end row. Otherwise, reuse the values computed above for
26453 the row where the highlight begins. */
26454 if (r2 != r1)
26456 if (!r2->reversed_p)
26458 glyph = r2->glyphs[TEXT_AREA];
26459 end = glyph + r2->used[TEXT_AREA];
26460 x = r2->x;
26462 else
26464 end = r2->glyphs[TEXT_AREA] - 1;
26465 glyph = end + r2->used[TEXT_AREA];
26469 if (!r2->reversed_p)
26471 /* Skip truncation and continuation glyphs near the end of the
26472 row, and also blanks and stretch glyphs inserted by
26473 extend_face_to_end_of_line. */
26474 while (end > glyph
26475 && INTEGERP ((end - 1)->object))
26476 --end;
26477 /* Scan the rest of the glyph row from the end, looking for the
26478 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26479 DISP_STRING, or whose position is between START_CHARPOS
26480 and END_CHARPOS */
26481 for (--end;
26482 end > glyph
26483 && !INTEGERP (end->object)
26484 && !EQ (end->object, disp_string)
26485 && !(BUFFERP (end->object)
26486 && (end->charpos >= start_charpos
26487 && end->charpos < end_charpos));
26488 --end)
26490 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26491 are present at buffer positions between START_CHARPOS and
26492 END_CHARPOS, or if they come from an overlay. */
26493 if (EQ (end->object, before_string))
26495 pos = string_buffer_position (before_string, start_charpos);
26496 if (!pos || (pos >= start_charpos && pos < end_charpos))
26497 break;
26499 else if (EQ (end->object, after_string))
26501 pos = string_buffer_position (after_string, end_charpos);
26502 if (!pos || (pos >= start_charpos && pos < end_charpos))
26503 break;
26506 /* Find the X coordinate of the last glyph to be highlighted. */
26507 for (; glyph <= end; ++glyph)
26508 x += glyph->pixel_width;
26510 hlinfo->mouse_face_end_x = x;
26511 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26513 else
26515 /* Skip truncation and continuation glyphs near the end of the
26516 row, and also blanks and stretch glyphs inserted by
26517 extend_face_to_end_of_line. */
26518 x = r2->x;
26519 end++;
26520 while (end < glyph
26521 && INTEGERP (end->object))
26523 x += end->pixel_width;
26524 ++end;
26526 /* Scan the rest of the glyph row from the end, looking for the
26527 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26528 DISP_STRING, or whose position is between START_CHARPOS
26529 and END_CHARPOS */
26530 for ( ;
26531 end < glyph
26532 && !INTEGERP (end->object)
26533 && !EQ (end->object, disp_string)
26534 && !(BUFFERP (end->object)
26535 && (end->charpos >= start_charpos
26536 && end->charpos < end_charpos));
26537 ++end)
26539 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26540 are present at buffer positions between START_CHARPOS and
26541 END_CHARPOS, or if they come from an overlay. */
26542 if (EQ (end->object, before_string))
26544 pos = string_buffer_position (before_string, start_charpos);
26545 if (!pos || (pos >= start_charpos && pos < end_charpos))
26546 break;
26548 else if (EQ (end->object, after_string))
26550 pos = string_buffer_position (after_string, end_charpos);
26551 if (!pos || (pos >= start_charpos && pos < end_charpos))
26552 break;
26554 x += end->pixel_width;
26556 /* If we exited the above loop because we arrived at the last
26557 glyph of the row, and its buffer position is still not in
26558 range, it means the last character in range is the preceding
26559 newline. Bump the end column and x values to get past the
26560 last glyph. */
26561 if (end == glyph
26562 && BUFFERP (end->object)
26563 && (end->charpos < start_charpos
26564 || end->charpos >= end_charpos))
26566 x += end->pixel_width;
26567 ++end;
26569 hlinfo->mouse_face_end_x = x;
26570 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26573 hlinfo->mouse_face_window = window;
26574 hlinfo->mouse_face_face_id
26575 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26576 mouse_charpos + 1,
26577 !hlinfo->mouse_face_hidden, -1);
26578 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26581 /* The following function is not used anymore (replaced with
26582 mouse_face_from_string_pos), but I leave it here for the time
26583 being, in case someone would. */
26585 #if 0 /* not used */
26587 /* Find the position of the glyph for position POS in OBJECT in
26588 window W's current matrix, and return in *X, *Y the pixel
26589 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26591 RIGHT_P non-zero means return the position of the right edge of the
26592 glyph, RIGHT_P zero means return the left edge position.
26594 If no glyph for POS exists in the matrix, return the position of
26595 the glyph with the next smaller position that is in the matrix, if
26596 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26597 exists in the matrix, return the position of the glyph with the
26598 next larger position in OBJECT.
26600 Value is non-zero if a glyph was found. */
26602 static int
26603 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26604 int *hpos, int *vpos, int *x, int *y, int right_p)
26606 int yb = window_text_bottom_y (w);
26607 struct glyph_row *r;
26608 struct glyph *best_glyph = NULL;
26609 struct glyph_row *best_row = NULL;
26610 int best_x = 0;
26612 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26613 r->enabled_p && r->y < yb;
26614 ++r)
26616 struct glyph *g = r->glyphs[TEXT_AREA];
26617 struct glyph *e = g + r->used[TEXT_AREA];
26618 int gx;
26620 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26621 if (EQ (g->object, object))
26623 if (g->charpos == pos)
26625 best_glyph = g;
26626 best_x = gx;
26627 best_row = r;
26628 goto found;
26630 else if (best_glyph == NULL
26631 || ((eabs (g->charpos - pos)
26632 < eabs (best_glyph->charpos - pos))
26633 && (right_p
26634 ? g->charpos < pos
26635 : g->charpos > pos)))
26637 best_glyph = g;
26638 best_x = gx;
26639 best_row = r;
26644 found:
26646 if (best_glyph)
26648 *x = best_x;
26649 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26651 if (right_p)
26653 *x += best_glyph->pixel_width;
26654 ++*hpos;
26657 *y = best_row->y;
26658 *vpos = best_row - w->current_matrix->rows;
26661 return best_glyph != NULL;
26663 #endif /* not used */
26665 /* Find the positions of the first and the last glyphs in window W's
26666 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26667 (assumed to be a string), and return in HLINFO's mouse_face_*
26668 members the pixel and column/row coordinates of those glyphs. */
26670 static void
26671 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26672 Lisp_Object object,
26673 ptrdiff_t startpos, ptrdiff_t endpos)
26675 int yb = window_text_bottom_y (w);
26676 struct glyph_row *r;
26677 struct glyph *g, *e;
26678 int gx;
26679 int found = 0;
26681 /* Find the glyph row with at least one position in the range
26682 [STARTPOS..ENDPOS], and the first glyph in that row whose
26683 position belongs to that range. */
26684 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26685 r->enabled_p && r->y < yb;
26686 ++r)
26688 if (!r->reversed_p)
26690 g = r->glyphs[TEXT_AREA];
26691 e = g + r->used[TEXT_AREA];
26692 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26693 if (EQ (g->object, object)
26694 && startpos <= g->charpos && g->charpos <= endpos)
26696 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26697 hlinfo->mouse_face_beg_y = r->y;
26698 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26699 hlinfo->mouse_face_beg_x = gx;
26700 found = 1;
26701 break;
26704 else
26706 struct glyph *g1;
26708 e = r->glyphs[TEXT_AREA];
26709 g = e + r->used[TEXT_AREA];
26710 for ( ; g > e; --g)
26711 if (EQ ((g-1)->object, object)
26712 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26714 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26715 hlinfo->mouse_face_beg_y = r->y;
26716 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26717 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26718 gx += g1->pixel_width;
26719 hlinfo->mouse_face_beg_x = gx;
26720 found = 1;
26721 break;
26724 if (found)
26725 break;
26728 if (!found)
26729 return;
26731 /* Starting with the next row, look for the first row which does NOT
26732 include any glyphs whose positions are in the range. */
26733 for (++r; r->enabled_p && r->y < yb; ++r)
26735 g = r->glyphs[TEXT_AREA];
26736 e = g + r->used[TEXT_AREA];
26737 found = 0;
26738 for ( ; g < e; ++g)
26739 if (EQ (g->object, object)
26740 && startpos <= g->charpos && g->charpos <= endpos)
26742 found = 1;
26743 break;
26745 if (!found)
26746 break;
26749 /* The highlighted region ends on the previous row. */
26750 r--;
26752 /* Set the end row and its vertical pixel coordinate. */
26753 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26754 hlinfo->mouse_face_end_y = r->y;
26756 /* Compute and set the end column and the end column's horizontal
26757 pixel coordinate. */
26758 if (!r->reversed_p)
26760 g = r->glyphs[TEXT_AREA];
26761 e = g + r->used[TEXT_AREA];
26762 for ( ; e > g; --e)
26763 if (EQ ((e-1)->object, object)
26764 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26765 break;
26766 hlinfo->mouse_face_end_col = e - g;
26768 for (gx = r->x; g < e; ++g)
26769 gx += g->pixel_width;
26770 hlinfo->mouse_face_end_x = gx;
26772 else
26774 e = r->glyphs[TEXT_AREA];
26775 g = e + r->used[TEXT_AREA];
26776 for (gx = r->x ; e < g; ++e)
26778 if (EQ (e->object, object)
26779 && startpos <= e->charpos && e->charpos <= endpos)
26780 break;
26781 gx += e->pixel_width;
26783 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26784 hlinfo->mouse_face_end_x = gx;
26788 #ifdef HAVE_WINDOW_SYSTEM
26790 /* See if position X, Y is within a hot-spot of an image. */
26792 static int
26793 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26795 if (!CONSP (hot_spot))
26796 return 0;
26798 if (EQ (XCAR (hot_spot), Qrect))
26800 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26801 Lisp_Object rect = XCDR (hot_spot);
26802 Lisp_Object tem;
26803 if (!CONSP (rect))
26804 return 0;
26805 if (!CONSP (XCAR (rect)))
26806 return 0;
26807 if (!CONSP (XCDR (rect)))
26808 return 0;
26809 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26810 return 0;
26811 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26812 return 0;
26813 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26814 return 0;
26815 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26816 return 0;
26817 return 1;
26819 else if (EQ (XCAR (hot_spot), Qcircle))
26821 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26822 Lisp_Object circ = XCDR (hot_spot);
26823 Lisp_Object lr, lx0, ly0;
26824 if (CONSP (circ)
26825 && CONSP (XCAR (circ))
26826 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26827 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26828 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26830 double r = XFLOATINT (lr);
26831 double dx = XINT (lx0) - x;
26832 double dy = XINT (ly0) - y;
26833 return (dx * dx + dy * dy <= r * r);
26836 else if (EQ (XCAR (hot_spot), Qpoly))
26838 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26839 if (VECTORP (XCDR (hot_spot)))
26841 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26842 Lisp_Object *poly = v->contents;
26843 ptrdiff_t n = v->header.size;
26844 ptrdiff_t i;
26845 int inside = 0;
26846 Lisp_Object lx, ly;
26847 int x0, y0;
26849 /* Need an even number of coordinates, and at least 3 edges. */
26850 if (n < 6 || n & 1)
26851 return 0;
26853 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26854 If count is odd, we are inside polygon. Pixels on edges
26855 may or may not be included depending on actual geometry of the
26856 polygon. */
26857 if ((lx = poly[n-2], !INTEGERP (lx))
26858 || (ly = poly[n-1], !INTEGERP (lx)))
26859 return 0;
26860 x0 = XINT (lx), y0 = XINT (ly);
26861 for (i = 0; i < n; i += 2)
26863 int x1 = x0, y1 = y0;
26864 if ((lx = poly[i], !INTEGERP (lx))
26865 || (ly = poly[i+1], !INTEGERP (ly)))
26866 return 0;
26867 x0 = XINT (lx), y0 = XINT (ly);
26869 /* Does this segment cross the X line? */
26870 if (x0 >= x)
26872 if (x1 >= x)
26873 continue;
26875 else if (x1 < x)
26876 continue;
26877 if (y > y0 && y > y1)
26878 continue;
26879 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26880 inside = !inside;
26882 return inside;
26885 return 0;
26888 Lisp_Object
26889 find_hot_spot (Lisp_Object map, int x, int y)
26891 while (CONSP (map))
26893 if (CONSP (XCAR (map))
26894 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26895 return XCAR (map);
26896 map = XCDR (map);
26899 return Qnil;
26902 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26903 3, 3, 0,
26904 doc: /* Lookup in image map MAP coordinates X and Y.
26905 An image map is an alist where each element has the format (AREA ID PLIST).
26906 An AREA is specified as either a rectangle, a circle, or a polygon:
26907 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26908 pixel coordinates of the upper left and bottom right corners.
26909 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26910 and the radius of the circle; r may be a float or integer.
26911 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26912 vector describes one corner in the polygon.
26913 Returns the alist element for the first matching AREA in MAP. */)
26914 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26916 if (NILP (map))
26917 return Qnil;
26919 CHECK_NUMBER (x);
26920 CHECK_NUMBER (y);
26922 return find_hot_spot (map,
26923 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26924 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26928 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26929 static void
26930 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26932 /* Do not change cursor shape while dragging mouse. */
26933 if (!NILP (do_mouse_tracking))
26934 return;
26936 if (!NILP (pointer))
26938 if (EQ (pointer, Qarrow))
26939 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26940 else if (EQ (pointer, Qhand))
26941 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26942 else if (EQ (pointer, Qtext))
26943 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26944 else if (EQ (pointer, intern ("hdrag")))
26945 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26946 #ifdef HAVE_X_WINDOWS
26947 else if (EQ (pointer, intern ("vdrag")))
26948 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26949 #endif
26950 else if (EQ (pointer, intern ("hourglass")))
26951 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26952 else if (EQ (pointer, Qmodeline))
26953 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26954 else
26955 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26958 if (cursor != No_Cursor)
26959 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26962 #endif /* HAVE_WINDOW_SYSTEM */
26964 /* Take proper action when mouse has moved to the mode or header line
26965 or marginal area AREA of window W, x-position X and y-position Y.
26966 X is relative to the start of the text display area of W, so the
26967 width of bitmap areas and scroll bars must be subtracted to get a
26968 position relative to the start of the mode line. */
26970 static void
26971 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26972 enum window_part area)
26974 struct window *w = XWINDOW (window);
26975 struct frame *f = XFRAME (w->frame);
26976 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26977 #ifdef HAVE_WINDOW_SYSTEM
26978 Display_Info *dpyinfo;
26979 #endif
26980 Cursor cursor = No_Cursor;
26981 Lisp_Object pointer = Qnil;
26982 int dx, dy, width, height;
26983 ptrdiff_t charpos;
26984 Lisp_Object string, object = Qnil;
26985 Lisp_Object pos IF_LINT (= Qnil), help;
26987 Lisp_Object mouse_face;
26988 int original_x_pixel = x;
26989 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26990 struct glyph_row *row IF_LINT (= 0);
26992 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26994 int x0;
26995 struct glyph *end;
26997 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26998 returns them in row/column units! */
26999 string = mode_line_string (w, area, &x, &y, &charpos,
27000 &object, &dx, &dy, &width, &height);
27002 row = (area == ON_MODE_LINE
27003 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27004 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27006 /* Find the glyph under the mouse pointer. */
27007 if (row->mode_line_p && row->enabled_p)
27009 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27010 end = glyph + row->used[TEXT_AREA];
27012 for (x0 = original_x_pixel;
27013 glyph < end && x0 >= glyph->pixel_width;
27014 ++glyph)
27015 x0 -= glyph->pixel_width;
27017 if (glyph >= end)
27018 glyph = NULL;
27021 else
27023 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27024 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27025 returns them in row/column units! */
27026 string = marginal_area_string (w, area, &x, &y, &charpos,
27027 &object, &dx, &dy, &width, &height);
27030 help = Qnil;
27032 #ifdef HAVE_WINDOW_SYSTEM
27033 if (IMAGEP (object))
27035 Lisp_Object image_map, hotspot;
27036 if ((image_map = Fplist_get (XCDR (object), QCmap),
27037 !NILP (image_map))
27038 && (hotspot = find_hot_spot (image_map, dx, dy),
27039 CONSP (hotspot))
27040 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27042 Lisp_Object plist;
27044 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27045 If so, we could look for mouse-enter, mouse-leave
27046 properties in PLIST (and do something...). */
27047 hotspot = XCDR (hotspot);
27048 if (CONSP (hotspot)
27049 && (plist = XCAR (hotspot), CONSP (plist)))
27051 pointer = Fplist_get (plist, Qpointer);
27052 if (NILP (pointer))
27053 pointer = Qhand;
27054 help = Fplist_get (plist, Qhelp_echo);
27055 if (!NILP (help))
27057 help_echo_string = help;
27058 XSETWINDOW (help_echo_window, w);
27059 help_echo_object = w->buffer;
27060 help_echo_pos = charpos;
27064 if (NILP (pointer))
27065 pointer = Fplist_get (XCDR (object), QCpointer);
27067 #endif /* HAVE_WINDOW_SYSTEM */
27069 if (STRINGP (string))
27070 pos = make_number (charpos);
27072 /* Set the help text and mouse pointer. If the mouse is on a part
27073 of the mode line without any text (e.g. past the right edge of
27074 the mode line text), use the default help text and pointer. */
27075 if (STRINGP (string) || area == ON_MODE_LINE)
27077 /* Arrange to display the help by setting the global variables
27078 help_echo_string, help_echo_object, and help_echo_pos. */
27079 if (NILP (help))
27081 if (STRINGP (string))
27082 help = Fget_text_property (pos, Qhelp_echo, string);
27084 if (!NILP (help))
27086 help_echo_string = help;
27087 XSETWINDOW (help_echo_window, w);
27088 help_echo_object = string;
27089 help_echo_pos = charpos;
27091 else if (area == ON_MODE_LINE)
27093 Lisp_Object default_help
27094 = buffer_local_value_1 (Qmode_line_default_help_echo,
27095 w->buffer);
27097 if (STRINGP (default_help))
27099 help_echo_string = default_help;
27100 XSETWINDOW (help_echo_window, w);
27101 help_echo_object = Qnil;
27102 help_echo_pos = -1;
27107 #ifdef HAVE_WINDOW_SYSTEM
27108 /* Change the mouse pointer according to what is under it. */
27109 if (FRAME_WINDOW_P (f))
27111 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27112 if (STRINGP (string))
27114 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27116 if (NILP (pointer))
27117 pointer = Fget_text_property (pos, Qpointer, string);
27119 /* Change the mouse pointer according to what is under X/Y. */
27120 if (NILP (pointer)
27121 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27123 Lisp_Object map;
27124 map = Fget_text_property (pos, Qlocal_map, string);
27125 if (!KEYMAPP (map))
27126 map = Fget_text_property (pos, Qkeymap, string);
27127 if (!KEYMAPP (map))
27128 cursor = dpyinfo->vertical_scroll_bar_cursor;
27131 else
27132 /* Default mode-line pointer. */
27133 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27135 #endif
27138 /* Change the mouse face according to what is under X/Y. */
27139 if (STRINGP (string))
27141 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27142 if (!NILP (mouse_face)
27143 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27144 && glyph)
27146 Lisp_Object b, e;
27148 struct glyph * tmp_glyph;
27150 int gpos;
27151 int gseq_length;
27152 int total_pixel_width;
27153 ptrdiff_t begpos, endpos, ignore;
27155 int vpos, hpos;
27157 b = Fprevious_single_property_change (make_number (charpos + 1),
27158 Qmouse_face, string, Qnil);
27159 if (NILP (b))
27160 begpos = 0;
27161 else
27162 begpos = XINT (b);
27164 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27165 if (NILP (e))
27166 endpos = SCHARS (string);
27167 else
27168 endpos = XINT (e);
27170 /* Calculate the glyph position GPOS of GLYPH in the
27171 displayed string, relative to the beginning of the
27172 highlighted part of the string.
27174 Note: GPOS is different from CHARPOS. CHARPOS is the
27175 position of GLYPH in the internal string object. A mode
27176 line string format has structures which are converted to
27177 a flattened string by the Emacs Lisp interpreter. The
27178 internal string is an element of those structures. The
27179 displayed string is the flattened string. */
27180 tmp_glyph = row_start_glyph;
27181 while (tmp_glyph < glyph
27182 && (!(EQ (tmp_glyph->object, glyph->object)
27183 && begpos <= tmp_glyph->charpos
27184 && tmp_glyph->charpos < endpos)))
27185 tmp_glyph++;
27186 gpos = glyph - tmp_glyph;
27188 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27189 the highlighted part of the displayed string to which
27190 GLYPH belongs. Note: GSEQ_LENGTH is different from
27191 SCHARS (STRING), because the latter returns the length of
27192 the internal string. */
27193 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27194 tmp_glyph > glyph
27195 && (!(EQ (tmp_glyph->object, glyph->object)
27196 && begpos <= tmp_glyph->charpos
27197 && tmp_glyph->charpos < endpos));
27198 tmp_glyph--)
27200 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27202 /* Calculate the total pixel width of all the glyphs between
27203 the beginning of the highlighted area and GLYPH. */
27204 total_pixel_width = 0;
27205 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27206 total_pixel_width += tmp_glyph->pixel_width;
27208 /* Pre calculation of re-rendering position. Note: X is in
27209 column units here, after the call to mode_line_string or
27210 marginal_area_string. */
27211 hpos = x - gpos;
27212 vpos = (area == ON_MODE_LINE
27213 ? (w->current_matrix)->nrows - 1
27214 : 0);
27216 /* If GLYPH's position is included in the region that is
27217 already drawn in mouse face, we have nothing to do. */
27218 if ( EQ (window, hlinfo->mouse_face_window)
27219 && (!row->reversed_p
27220 ? (hlinfo->mouse_face_beg_col <= hpos
27221 && hpos < hlinfo->mouse_face_end_col)
27222 /* In R2L rows we swap BEG and END, see below. */
27223 : (hlinfo->mouse_face_end_col <= hpos
27224 && hpos < hlinfo->mouse_face_beg_col))
27225 && hlinfo->mouse_face_beg_row == vpos )
27226 return;
27228 if (clear_mouse_face (hlinfo))
27229 cursor = No_Cursor;
27231 if (!row->reversed_p)
27233 hlinfo->mouse_face_beg_col = hpos;
27234 hlinfo->mouse_face_beg_x = original_x_pixel
27235 - (total_pixel_width + dx);
27236 hlinfo->mouse_face_end_col = hpos + gseq_length;
27237 hlinfo->mouse_face_end_x = 0;
27239 else
27241 /* In R2L rows, show_mouse_face expects BEG and END
27242 coordinates to be swapped. */
27243 hlinfo->mouse_face_end_col = hpos;
27244 hlinfo->mouse_face_end_x = original_x_pixel
27245 - (total_pixel_width + dx);
27246 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27247 hlinfo->mouse_face_beg_x = 0;
27250 hlinfo->mouse_face_beg_row = vpos;
27251 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27252 hlinfo->mouse_face_beg_y = 0;
27253 hlinfo->mouse_face_end_y = 0;
27254 hlinfo->mouse_face_past_end = 0;
27255 hlinfo->mouse_face_window = window;
27257 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27258 charpos,
27259 0, 0, 0,
27260 &ignore,
27261 glyph->face_id,
27263 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27265 if (NILP (pointer))
27266 pointer = Qhand;
27268 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27269 clear_mouse_face (hlinfo);
27271 #ifdef HAVE_WINDOW_SYSTEM
27272 if (FRAME_WINDOW_P (f))
27273 define_frame_cursor1 (f, cursor, pointer);
27274 #endif
27278 /* EXPORT:
27279 Take proper action when the mouse has moved to position X, Y on
27280 frame F as regards highlighting characters that have mouse-face
27281 properties. Also de-highlighting chars where the mouse was before.
27282 X and Y can be negative or out of range. */
27284 void
27285 note_mouse_highlight (struct frame *f, int x, int y)
27287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27288 enum window_part part = ON_NOTHING;
27289 Lisp_Object window;
27290 struct window *w;
27291 Cursor cursor = No_Cursor;
27292 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27293 struct buffer *b;
27295 /* When a menu is active, don't highlight because this looks odd. */
27296 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27297 if (popup_activated ())
27298 return;
27299 #endif
27301 if (NILP (Vmouse_highlight)
27302 || !f->glyphs_initialized_p
27303 || f->pointer_invisible)
27304 return;
27306 hlinfo->mouse_face_mouse_x = x;
27307 hlinfo->mouse_face_mouse_y = y;
27308 hlinfo->mouse_face_mouse_frame = f;
27310 if (hlinfo->mouse_face_defer)
27311 return;
27313 if (gc_in_progress)
27315 hlinfo->mouse_face_deferred_gc = 1;
27316 return;
27319 /* Which window is that in? */
27320 window = window_from_coordinates (f, x, y, &part, 1);
27322 /* If displaying active text in another window, clear that. */
27323 if (! EQ (window, hlinfo->mouse_face_window)
27324 /* Also clear if we move out of text area in same window. */
27325 || (!NILP (hlinfo->mouse_face_window)
27326 && !NILP (window)
27327 && part != ON_TEXT
27328 && part != ON_MODE_LINE
27329 && part != ON_HEADER_LINE))
27330 clear_mouse_face (hlinfo);
27332 /* Not on a window -> return. */
27333 if (!WINDOWP (window))
27334 return;
27336 /* Reset help_echo_string. It will get recomputed below. */
27337 help_echo_string = Qnil;
27339 /* Convert to window-relative pixel coordinates. */
27340 w = XWINDOW (window);
27341 frame_to_window_pixel_xy (w, &x, &y);
27343 #ifdef HAVE_WINDOW_SYSTEM
27344 /* Handle tool-bar window differently since it doesn't display a
27345 buffer. */
27346 if (EQ (window, f->tool_bar_window))
27348 note_tool_bar_highlight (f, x, y);
27349 return;
27351 #endif
27353 /* Mouse is on the mode, header line or margin? */
27354 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27355 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27357 note_mode_line_or_margin_highlight (window, x, y, part);
27358 return;
27361 #ifdef HAVE_WINDOW_SYSTEM
27362 if (part == ON_VERTICAL_BORDER)
27364 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27365 help_echo_string = build_string ("drag-mouse-1: resize");
27367 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27368 || part == ON_SCROLL_BAR)
27369 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27370 else
27371 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27372 #endif
27374 /* Are we in a window whose display is up to date?
27375 And verify the buffer's text has not changed. */
27376 b = XBUFFER (w->buffer);
27377 if (part == ON_TEXT
27378 && EQ (w->window_end_valid, w->buffer)
27379 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27380 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27382 int hpos, vpos, dx, dy, area = LAST_AREA;
27383 ptrdiff_t pos;
27384 struct glyph *glyph;
27385 Lisp_Object object;
27386 Lisp_Object mouse_face = Qnil, position;
27387 Lisp_Object *overlay_vec = NULL;
27388 ptrdiff_t i, noverlays;
27389 struct buffer *obuf;
27390 ptrdiff_t obegv, ozv;
27391 int same_region;
27393 /* Find the glyph under X/Y. */
27394 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27396 #ifdef HAVE_WINDOW_SYSTEM
27397 /* Look for :pointer property on image. */
27398 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27400 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27401 if (img != NULL && IMAGEP (img->spec))
27403 Lisp_Object image_map, hotspot;
27404 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27405 !NILP (image_map))
27406 && (hotspot = find_hot_spot (image_map,
27407 glyph->slice.img.x + dx,
27408 glyph->slice.img.y + dy),
27409 CONSP (hotspot))
27410 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27412 Lisp_Object plist;
27414 /* Could check XCAR (hotspot) to see if we enter/leave
27415 this hot-spot.
27416 If so, we could look for mouse-enter, mouse-leave
27417 properties in PLIST (and do something...). */
27418 hotspot = XCDR (hotspot);
27419 if (CONSP (hotspot)
27420 && (plist = XCAR (hotspot), CONSP (plist)))
27422 pointer = Fplist_get (plist, Qpointer);
27423 if (NILP (pointer))
27424 pointer = Qhand;
27425 help_echo_string = Fplist_get (plist, Qhelp_echo);
27426 if (!NILP (help_echo_string))
27428 help_echo_window = window;
27429 help_echo_object = glyph->object;
27430 help_echo_pos = glyph->charpos;
27434 if (NILP (pointer))
27435 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27438 #endif /* HAVE_WINDOW_SYSTEM */
27440 /* Clear mouse face if X/Y not over text. */
27441 if (glyph == NULL
27442 || area != TEXT_AREA
27443 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27444 /* Glyph's OBJECT is an integer for glyphs inserted by the
27445 display engine for its internal purposes, like truncation
27446 and continuation glyphs and blanks beyond the end of
27447 line's text on text terminals. If we are over such a
27448 glyph, we are not over any text. */
27449 || INTEGERP (glyph->object)
27450 /* R2L rows have a stretch glyph at their front, which
27451 stands for no text, whereas L2R rows have no glyphs at
27452 all beyond the end of text. Treat such stretch glyphs
27453 like we do with NULL glyphs in L2R rows. */
27454 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27455 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27456 && glyph->type == STRETCH_GLYPH
27457 && glyph->avoid_cursor_p))
27459 if (clear_mouse_face (hlinfo))
27460 cursor = No_Cursor;
27461 #ifdef HAVE_WINDOW_SYSTEM
27462 if (FRAME_WINDOW_P (f) && NILP (pointer))
27464 if (area != TEXT_AREA)
27465 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27466 else
27467 pointer = Vvoid_text_area_pointer;
27469 #endif
27470 goto set_cursor;
27473 pos = glyph->charpos;
27474 object = glyph->object;
27475 if (!STRINGP (object) && !BUFFERP (object))
27476 goto set_cursor;
27478 /* If we get an out-of-range value, return now; avoid an error. */
27479 if (BUFFERP (object) && pos > BUF_Z (b))
27480 goto set_cursor;
27482 /* Make the window's buffer temporarily current for
27483 overlays_at and compute_char_face. */
27484 obuf = current_buffer;
27485 current_buffer = b;
27486 obegv = BEGV;
27487 ozv = ZV;
27488 BEGV = BEG;
27489 ZV = Z;
27491 /* Is this char mouse-active or does it have help-echo? */
27492 position = make_number (pos);
27494 if (BUFFERP (object))
27496 /* Put all the overlays we want in a vector in overlay_vec. */
27497 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27498 /* Sort overlays into increasing priority order. */
27499 noverlays = sort_overlays (overlay_vec, noverlays, w);
27501 else
27502 noverlays = 0;
27504 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27506 if (same_region)
27507 cursor = No_Cursor;
27509 /* Check mouse-face highlighting. */
27510 if (! same_region
27511 /* If there exists an overlay with mouse-face overlapping
27512 the one we are currently highlighting, we have to
27513 check if we enter the overlapping overlay, and then
27514 highlight only that. */
27515 || (OVERLAYP (hlinfo->mouse_face_overlay)
27516 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27518 /* Find the highest priority overlay with a mouse-face. */
27519 Lisp_Object overlay = Qnil;
27520 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27522 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27523 if (!NILP (mouse_face))
27524 overlay = overlay_vec[i];
27527 /* If we're highlighting the same overlay as before, there's
27528 no need to do that again. */
27529 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27530 goto check_help_echo;
27531 hlinfo->mouse_face_overlay = overlay;
27533 /* Clear the display of the old active region, if any. */
27534 if (clear_mouse_face (hlinfo))
27535 cursor = No_Cursor;
27537 /* If no overlay applies, get a text property. */
27538 if (NILP (overlay))
27539 mouse_face = Fget_text_property (position, Qmouse_face, object);
27541 /* Next, compute the bounds of the mouse highlighting and
27542 display it. */
27543 if (!NILP (mouse_face) && STRINGP (object))
27545 /* The mouse-highlighting comes from a display string
27546 with a mouse-face. */
27547 Lisp_Object s, e;
27548 ptrdiff_t ignore;
27550 s = Fprevious_single_property_change
27551 (make_number (pos + 1), Qmouse_face, object, Qnil);
27552 e = Fnext_single_property_change
27553 (position, Qmouse_face, object, Qnil);
27554 if (NILP (s))
27555 s = make_number (0);
27556 if (NILP (e))
27557 e = make_number (SCHARS (object) - 1);
27558 mouse_face_from_string_pos (w, hlinfo, object,
27559 XINT (s), XINT (e));
27560 hlinfo->mouse_face_past_end = 0;
27561 hlinfo->mouse_face_window = window;
27562 hlinfo->mouse_face_face_id
27563 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27564 glyph->face_id, 1);
27565 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27566 cursor = No_Cursor;
27568 else
27570 /* The mouse-highlighting, if any, comes from an overlay
27571 or text property in the buffer. */
27572 Lisp_Object buffer IF_LINT (= Qnil);
27573 Lisp_Object disp_string IF_LINT (= Qnil);
27575 if (STRINGP (object))
27577 /* If we are on a display string with no mouse-face,
27578 check if the text under it has one. */
27579 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27580 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27581 pos = string_buffer_position (object, start);
27582 if (pos > 0)
27584 mouse_face = get_char_property_and_overlay
27585 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27586 buffer = w->buffer;
27587 disp_string = object;
27590 else
27592 buffer = object;
27593 disp_string = Qnil;
27596 if (!NILP (mouse_face))
27598 Lisp_Object before, after;
27599 Lisp_Object before_string, after_string;
27600 /* To correctly find the limits of mouse highlight
27601 in a bidi-reordered buffer, we must not use the
27602 optimization of limiting the search in
27603 previous-single-property-change and
27604 next-single-property-change, because
27605 rows_from_pos_range needs the real start and end
27606 positions to DTRT in this case. That's because
27607 the first row visible in a window does not
27608 necessarily display the character whose position
27609 is the smallest. */
27610 Lisp_Object lim1 =
27611 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27612 ? Fmarker_position (w->start)
27613 : Qnil;
27614 Lisp_Object lim2 =
27615 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27616 ? make_number (BUF_Z (XBUFFER (buffer))
27617 - XFASTINT (w->window_end_pos))
27618 : Qnil;
27620 if (NILP (overlay))
27622 /* Handle the text property case. */
27623 before = Fprevious_single_property_change
27624 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27625 after = Fnext_single_property_change
27626 (make_number (pos), Qmouse_face, buffer, lim2);
27627 before_string = after_string = Qnil;
27629 else
27631 /* Handle the overlay case. */
27632 before = Foverlay_start (overlay);
27633 after = Foverlay_end (overlay);
27634 before_string = Foverlay_get (overlay, Qbefore_string);
27635 after_string = Foverlay_get (overlay, Qafter_string);
27637 if (!STRINGP (before_string)) before_string = Qnil;
27638 if (!STRINGP (after_string)) after_string = Qnil;
27641 mouse_face_from_buffer_pos (window, hlinfo, pos,
27642 NILP (before)
27644 : XFASTINT (before),
27645 NILP (after)
27646 ? BUF_Z (XBUFFER (buffer))
27647 : XFASTINT (after),
27648 before_string, after_string,
27649 disp_string);
27650 cursor = No_Cursor;
27655 check_help_echo:
27657 /* Look for a `help-echo' property. */
27658 if (NILP (help_echo_string)) {
27659 Lisp_Object help, overlay;
27661 /* Check overlays first. */
27662 help = overlay = Qnil;
27663 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27665 overlay = overlay_vec[i];
27666 help = Foverlay_get (overlay, Qhelp_echo);
27669 if (!NILP (help))
27671 help_echo_string = help;
27672 help_echo_window = window;
27673 help_echo_object = overlay;
27674 help_echo_pos = pos;
27676 else
27678 Lisp_Object obj = glyph->object;
27679 ptrdiff_t charpos = glyph->charpos;
27681 /* Try text properties. */
27682 if (STRINGP (obj)
27683 && charpos >= 0
27684 && charpos < SCHARS (obj))
27686 help = Fget_text_property (make_number (charpos),
27687 Qhelp_echo, obj);
27688 if (NILP (help))
27690 /* If the string itself doesn't specify a help-echo,
27691 see if the buffer text ``under'' it does. */
27692 struct glyph_row *r
27693 = MATRIX_ROW (w->current_matrix, vpos);
27694 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27695 ptrdiff_t p = string_buffer_position (obj, start);
27696 if (p > 0)
27698 help = Fget_char_property (make_number (p),
27699 Qhelp_echo, w->buffer);
27700 if (!NILP (help))
27702 charpos = p;
27703 obj = w->buffer;
27708 else if (BUFFERP (obj)
27709 && charpos >= BEGV
27710 && charpos < ZV)
27711 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27712 obj);
27714 if (!NILP (help))
27716 help_echo_string = help;
27717 help_echo_window = window;
27718 help_echo_object = obj;
27719 help_echo_pos = charpos;
27724 #ifdef HAVE_WINDOW_SYSTEM
27725 /* Look for a `pointer' property. */
27726 if (FRAME_WINDOW_P (f) && NILP (pointer))
27728 /* Check overlays first. */
27729 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27730 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27732 if (NILP (pointer))
27734 Lisp_Object obj = glyph->object;
27735 ptrdiff_t charpos = glyph->charpos;
27737 /* Try text properties. */
27738 if (STRINGP (obj)
27739 && charpos >= 0
27740 && charpos < SCHARS (obj))
27742 pointer = Fget_text_property (make_number (charpos),
27743 Qpointer, obj);
27744 if (NILP (pointer))
27746 /* If the string itself doesn't specify a pointer,
27747 see if the buffer text ``under'' it does. */
27748 struct glyph_row *r
27749 = MATRIX_ROW (w->current_matrix, vpos);
27750 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27751 ptrdiff_t p = string_buffer_position (obj, start);
27752 if (p > 0)
27753 pointer = Fget_char_property (make_number (p),
27754 Qpointer, w->buffer);
27757 else if (BUFFERP (obj)
27758 && charpos >= BEGV
27759 && charpos < ZV)
27760 pointer = Fget_text_property (make_number (charpos),
27761 Qpointer, obj);
27764 #endif /* HAVE_WINDOW_SYSTEM */
27766 BEGV = obegv;
27767 ZV = ozv;
27768 current_buffer = obuf;
27771 set_cursor:
27773 #ifdef HAVE_WINDOW_SYSTEM
27774 if (FRAME_WINDOW_P (f))
27775 define_frame_cursor1 (f, cursor, pointer);
27776 #else
27777 /* This is here to prevent a compiler error, about "label at end of
27778 compound statement". */
27779 return;
27780 #endif
27784 /* EXPORT for RIF:
27785 Clear any mouse-face on window W. This function is part of the
27786 redisplay interface, and is called from try_window_id and similar
27787 functions to ensure the mouse-highlight is off. */
27789 void
27790 x_clear_window_mouse_face (struct window *w)
27792 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27793 Lisp_Object window;
27795 BLOCK_INPUT;
27796 XSETWINDOW (window, w);
27797 if (EQ (window, hlinfo->mouse_face_window))
27798 clear_mouse_face (hlinfo);
27799 UNBLOCK_INPUT;
27803 /* EXPORT:
27804 Just discard the mouse face information for frame F, if any.
27805 This is used when the size of F is changed. */
27807 void
27808 cancel_mouse_face (struct frame *f)
27810 Lisp_Object window;
27811 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27813 window = hlinfo->mouse_face_window;
27814 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27816 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27817 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27818 hlinfo->mouse_face_window = Qnil;
27824 /***********************************************************************
27825 Exposure Events
27826 ***********************************************************************/
27828 #ifdef HAVE_WINDOW_SYSTEM
27830 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27831 which intersects rectangle R. R is in window-relative coordinates. */
27833 static void
27834 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27835 enum glyph_row_area area)
27837 struct glyph *first = row->glyphs[area];
27838 struct glyph *end = row->glyphs[area] + row->used[area];
27839 struct glyph *last;
27840 int first_x, start_x, x;
27842 if (area == TEXT_AREA && row->fill_line_p)
27843 /* If row extends face to end of line write the whole line. */
27844 draw_glyphs (w, 0, row, area,
27845 0, row->used[area],
27846 DRAW_NORMAL_TEXT, 0);
27847 else
27849 /* Set START_X to the window-relative start position for drawing glyphs of
27850 AREA. The first glyph of the text area can be partially visible.
27851 The first glyphs of other areas cannot. */
27852 start_x = window_box_left_offset (w, area);
27853 x = start_x;
27854 if (area == TEXT_AREA)
27855 x += row->x;
27857 /* Find the first glyph that must be redrawn. */
27858 while (first < end
27859 && x + first->pixel_width < r->x)
27861 x += first->pixel_width;
27862 ++first;
27865 /* Find the last one. */
27866 last = first;
27867 first_x = x;
27868 while (last < end
27869 && x < r->x + r->width)
27871 x += last->pixel_width;
27872 ++last;
27875 /* Repaint. */
27876 if (last > first)
27877 draw_glyphs (w, first_x - start_x, row, area,
27878 first - row->glyphs[area], last - row->glyphs[area],
27879 DRAW_NORMAL_TEXT, 0);
27884 /* Redraw the parts of the glyph row ROW on window W intersecting
27885 rectangle R. R is in window-relative coordinates. Value is
27886 non-zero if mouse-face was overwritten. */
27888 static int
27889 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27891 xassert (row->enabled_p);
27893 if (row->mode_line_p || w->pseudo_window_p)
27894 draw_glyphs (w, 0, row, TEXT_AREA,
27895 0, row->used[TEXT_AREA],
27896 DRAW_NORMAL_TEXT, 0);
27897 else
27899 if (row->used[LEFT_MARGIN_AREA])
27900 expose_area (w, row, r, LEFT_MARGIN_AREA);
27901 if (row->used[TEXT_AREA])
27902 expose_area (w, row, r, TEXT_AREA);
27903 if (row->used[RIGHT_MARGIN_AREA])
27904 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27905 draw_row_fringe_bitmaps (w, row);
27908 return row->mouse_face_p;
27912 /* Redraw those parts of glyphs rows during expose event handling that
27913 overlap other rows. Redrawing of an exposed line writes over parts
27914 of lines overlapping that exposed line; this function fixes that.
27916 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27917 row in W's current matrix that is exposed and overlaps other rows.
27918 LAST_OVERLAPPING_ROW is the last such row. */
27920 static void
27921 expose_overlaps (struct window *w,
27922 struct glyph_row *first_overlapping_row,
27923 struct glyph_row *last_overlapping_row,
27924 XRectangle *r)
27926 struct glyph_row *row;
27928 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27929 if (row->overlapping_p)
27931 xassert (row->enabled_p && !row->mode_line_p);
27933 row->clip = r;
27934 if (row->used[LEFT_MARGIN_AREA])
27935 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27937 if (row->used[TEXT_AREA])
27938 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27940 if (row->used[RIGHT_MARGIN_AREA])
27941 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27942 row->clip = NULL;
27947 /* Return non-zero if W's cursor intersects rectangle R. */
27949 static int
27950 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27952 XRectangle cr, result;
27953 struct glyph *cursor_glyph;
27954 struct glyph_row *row;
27956 if (w->phys_cursor.vpos >= 0
27957 && w->phys_cursor.vpos < w->current_matrix->nrows
27958 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27959 row->enabled_p)
27960 && row->cursor_in_fringe_p)
27962 /* Cursor is in the fringe. */
27963 cr.x = window_box_right_offset (w,
27964 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27965 ? RIGHT_MARGIN_AREA
27966 : TEXT_AREA));
27967 cr.y = row->y;
27968 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27969 cr.height = row->height;
27970 return x_intersect_rectangles (&cr, r, &result);
27973 cursor_glyph = get_phys_cursor_glyph (w);
27974 if (cursor_glyph)
27976 /* r is relative to W's box, but w->phys_cursor.x is relative
27977 to left edge of W's TEXT area. Adjust it. */
27978 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27979 cr.y = w->phys_cursor.y;
27980 cr.width = cursor_glyph->pixel_width;
27981 cr.height = w->phys_cursor_height;
27982 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27983 I assume the effect is the same -- and this is portable. */
27984 return x_intersect_rectangles (&cr, r, &result);
27986 /* If we don't understand the format, pretend we're not in the hot-spot. */
27987 return 0;
27991 /* EXPORT:
27992 Draw a vertical window border to the right of window W if W doesn't
27993 have vertical scroll bars. */
27995 void
27996 x_draw_vertical_border (struct window *w)
27998 struct frame *f = XFRAME (WINDOW_FRAME (w));
28000 /* We could do better, if we knew what type of scroll-bar the adjacent
28001 windows (on either side) have... But we don't :-(
28002 However, I think this works ok. ++KFS 2003-04-25 */
28004 /* Redraw borders between horizontally adjacent windows. Don't
28005 do it for frames with vertical scroll bars because either the
28006 right scroll bar of a window, or the left scroll bar of its
28007 neighbor will suffice as a border. */
28008 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28009 return;
28011 if (!WINDOW_RIGHTMOST_P (w)
28012 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28014 int x0, x1, y0, y1;
28016 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28017 y1 -= 1;
28019 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28020 x1 -= 1;
28022 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28024 else if (!WINDOW_LEFTMOST_P (w)
28025 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28027 int x0, x1, y0, y1;
28029 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28030 y1 -= 1;
28032 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28033 x0 -= 1;
28035 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28040 /* Redraw the part of window W intersection rectangle FR. Pixel
28041 coordinates in FR are frame-relative. Call this function with
28042 input blocked. Value is non-zero if the exposure overwrites
28043 mouse-face. */
28045 static int
28046 expose_window (struct window *w, XRectangle *fr)
28048 struct frame *f = XFRAME (w->frame);
28049 XRectangle wr, r;
28050 int mouse_face_overwritten_p = 0;
28052 /* If window is not yet fully initialized, do nothing. This can
28053 happen when toolkit scroll bars are used and a window is split.
28054 Reconfiguring the scroll bar will generate an expose for a newly
28055 created window. */
28056 if (w->current_matrix == NULL)
28057 return 0;
28059 /* When we're currently updating the window, display and current
28060 matrix usually don't agree. Arrange for a thorough display
28061 later. */
28062 if (w == updated_window)
28064 SET_FRAME_GARBAGED (f);
28065 return 0;
28068 /* Frame-relative pixel rectangle of W. */
28069 wr.x = WINDOW_LEFT_EDGE_X (w);
28070 wr.y = WINDOW_TOP_EDGE_Y (w);
28071 wr.width = WINDOW_TOTAL_WIDTH (w);
28072 wr.height = WINDOW_TOTAL_HEIGHT (w);
28074 if (x_intersect_rectangles (fr, &wr, &r))
28076 int yb = window_text_bottom_y (w);
28077 struct glyph_row *row;
28078 int cursor_cleared_p, phys_cursor_on_p;
28079 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28081 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28082 r.x, r.y, r.width, r.height));
28084 /* Convert to window coordinates. */
28085 r.x -= WINDOW_LEFT_EDGE_X (w);
28086 r.y -= WINDOW_TOP_EDGE_Y (w);
28088 /* Turn off the cursor. */
28089 if (!w->pseudo_window_p
28090 && phys_cursor_in_rect_p (w, &r))
28092 x_clear_cursor (w);
28093 cursor_cleared_p = 1;
28095 else
28096 cursor_cleared_p = 0;
28098 /* If the row containing the cursor extends face to end of line,
28099 then expose_area might overwrite the cursor outside the
28100 rectangle and thus notice_overwritten_cursor might clear
28101 w->phys_cursor_on_p. We remember the original value and
28102 check later if it is changed. */
28103 phys_cursor_on_p = w->phys_cursor_on_p;
28105 /* Update lines intersecting rectangle R. */
28106 first_overlapping_row = last_overlapping_row = NULL;
28107 for (row = w->current_matrix->rows;
28108 row->enabled_p;
28109 ++row)
28111 int y0 = row->y;
28112 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28114 if ((y0 >= r.y && y0 < r.y + r.height)
28115 || (y1 > r.y && y1 < r.y + r.height)
28116 || (r.y >= y0 && r.y < y1)
28117 || (r.y + r.height > y0 && r.y + r.height < y1))
28119 /* A header line may be overlapping, but there is no need
28120 to fix overlapping areas for them. KFS 2005-02-12 */
28121 if (row->overlapping_p && !row->mode_line_p)
28123 if (first_overlapping_row == NULL)
28124 first_overlapping_row = row;
28125 last_overlapping_row = row;
28128 row->clip = fr;
28129 if (expose_line (w, row, &r))
28130 mouse_face_overwritten_p = 1;
28131 row->clip = NULL;
28133 else if (row->overlapping_p)
28135 /* We must redraw a row overlapping the exposed area. */
28136 if (y0 < r.y
28137 ? y0 + row->phys_height > r.y
28138 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28140 if (first_overlapping_row == NULL)
28141 first_overlapping_row = row;
28142 last_overlapping_row = row;
28146 if (y1 >= yb)
28147 break;
28150 /* Display the mode line if there is one. */
28151 if (WINDOW_WANTS_MODELINE_P (w)
28152 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28153 row->enabled_p)
28154 && row->y < r.y + r.height)
28156 if (expose_line (w, row, &r))
28157 mouse_face_overwritten_p = 1;
28160 if (!w->pseudo_window_p)
28162 /* Fix the display of overlapping rows. */
28163 if (first_overlapping_row)
28164 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28165 fr);
28167 /* Draw border between windows. */
28168 x_draw_vertical_border (w);
28170 /* Turn the cursor on again. */
28171 if (cursor_cleared_p
28172 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28173 update_window_cursor (w, 1);
28177 return mouse_face_overwritten_p;
28182 /* Redraw (parts) of all windows in the window tree rooted at W that
28183 intersect R. R contains frame pixel coordinates. Value is
28184 non-zero if the exposure overwrites mouse-face. */
28186 static int
28187 expose_window_tree (struct window *w, XRectangle *r)
28189 struct frame *f = XFRAME (w->frame);
28190 int mouse_face_overwritten_p = 0;
28192 while (w && !FRAME_GARBAGED_P (f))
28194 if (!NILP (w->hchild))
28195 mouse_face_overwritten_p
28196 |= expose_window_tree (XWINDOW (w->hchild), r);
28197 else if (!NILP (w->vchild))
28198 mouse_face_overwritten_p
28199 |= expose_window_tree (XWINDOW (w->vchild), r);
28200 else
28201 mouse_face_overwritten_p |= expose_window (w, r);
28203 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28206 return mouse_face_overwritten_p;
28210 /* EXPORT:
28211 Redisplay an exposed area of frame F. X and Y are the upper-left
28212 corner of the exposed rectangle. W and H are width and height of
28213 the exposed area. All are pixel values. W or H zero means redraw
28214 the entire frame. */
28216 void
28217 expose_frame (struct frame *f, int x, int y, int w, int h)
28219 XRectangle r;
28220 int mouse_face_overwritten_p = 0;
28222 TRACE ((stderr, "expose_frame "));
28224 /* No need to redraw if frame will be redrawn soon. */
28225 if (FRAME_GARBAGED_P (f))
28227 TRACE ((stderr, " garbaged\n"));
28228 return;
28231 /* If basic faces haven't been realized yet, there is no point in
28232 trying to redraw anything. This can happen when we get an expose
28233 event while Emacs is starting, e.g. by moving another window. */
28234 if (FRAME_FACE_CACHE (f) == NULL
28235 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28237 TRACE ((stderr, " no faces\n"));
28238 return;
28241 if (w == 0 || h == 0)
28243 r.x = r.y = 0;
28244 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28245 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28247 else
28249 r.x = x;
28250 r.y = y;
28251 r.width = w;
28252 r.height = h;
28255 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28256 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28258 if (WINDOWP (f->tool_bar_window))
28259 mouse_face_overwritten_p
28260 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28262 #ifdef HAVE_X_WINDOWS
28263 #ifndef MSDOS
28264 #ifndef USE_X_TOOLKIT
28265 if (WINDOWP (f->menu_bar_window))
28266 mouse_face_overwritten_p
28267 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28268 #endif /* not USE_X_TOOLKIT */
28269 #endif
28270 #endif
28272 /* Some window managers support a focus-follows-mouse style with
28273 delayed raising of frames. Imagine a partially obscured frame,
28274 and moving the mouse into partially obscured mouse-face on that
28275 frame. The visible part of the mouse-face will be highlighted,
28276 then the WM raises the obscured frame. With at least one WM, KDE
28277 2.1, Emacs is not getting any event for the raising of the frame
28278 (even tried with SubstructureRedirectMask), only Expose events.
28279 These expose events will draw text normally, i.e. not
28280 highlighted. Which means we must redo the highlight here.
28281 Subsume it under ``we love X''. --gerd 2001-08-15 */
28282 /* Included in Windows version because Windows most likely does not
28283 do the right thing if any third party tool offers
28284 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28285 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28287 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28288 if (f == hlinfo->mouse_face_mouse_frame)
28290 int mouse_x = hlinfo->mouse_face_mouse_x;
28291 int mouse_y = hlinfo->mouse_face_mouse_y;
28292 clear_mouse_face (hlinfo);
28293 note_mouse_highlight (f, mouse_x, mouse_y);
28299 /* EXPORT:
28300 Determine the intersection of two rectangles R1 and R2. Return
28301 the intersection in *RESULT. Value is non-zero if RESULT is not
28302 empty. */
28305 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28307 XRectangle *left, *right;
28308 XRectangle *upper, *lower;
28309 int intersection_p = 0;
28311 /* Rearrange so that R1 is the left-most rectangle. */
28312 if (r1->x < r2->x)
28313 left = r1, right = r2;
28314 else
28315 left = r2, right = r1;
28317 /* X0 of the intersection is right.x0, if this is inside R1,
28318 otherwise there is no intersection. */
28319 if (right->x <= left->x + left->width)
28321 result->x = right->x;
28323 /* The right end of the intersection is the minimum of
28324 the right ends of left and right. */
28325 result->width = (min (left->x + left->width, right->x + right->width)
28326 - result->x);
28328 /* Same game for Y. */
28329 if (r1->y < r2->y)
28330 upper = r1, lower = r2;
28331 else
28332 upper = r2, lower = r1;
28334 /* The upper end of the intersection is lower.y0, if this is inside
28335 of upper. Otherwise, there is no intersection. */
28336 if (lower->y <= upper->y + upper->height)
28338 result->y = lower->y;
28340 /* The lower end of the intersection is the minimum of the lower
28341 ends of upper and lower. */
28342 result->height = (min (lower->y + lower->height,
28343 upper->y + upper->height)
28344 - result->y);
28345 intersection_p = 1;
28349 return intersection_p;
28352 #endif /* HAVE_WINDOW_SYSTEM */
28355 /***********************************************************************
28356 Initialization
28357 ***********************************************************************/
28359 void
28360 syms_of_xdisp (void)
28362 Vwith_echo_area_save_vector = Qnil;
28363 staticpro (&Vwith_echo_area_save_vector);
28365 Vmessage_stack = Qnil;
28366 staticpro (&Vmessage_stack);
28368 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28370 message_dolog_marker1 = Fmake_marker ();
28371 staticpro (&message_dolog_marker1);
28372 message_dolog_marker2 = Fmake_marker ();
28373 staticpro (&message_dolog_marker2);
28374 message_dolog_marker3 = Fmake_marker ();
28375 staticpro (&message_dolog_marker3);
28377 #if GLYPH_DEBUG
28378 defsubr (&Sdump_frame_glyph_matrix);
28379 defsubr (&Sdump_glyph_matrix);
28380 defsubr (&Sdump_glyph_row);
28381 defsubr (&Sdump_tool_bar_row);
28382 defsubr (&Strace_redisplay);
28383 defsubr (&Strace_to_stderr);
28384 #endif
28385 #ifdef HAVE_WINDOW_SYSTEM
28386 defsubr (&Stool_bar_lines_needed);
28387 defsubr (&Slookup_image_map);
28388 #endif
28389 defsubr (&Sformat_mode_line);
28390 defsubr (&Sinvisible_p);
28391 defsubr (&Scurrent_bidi_paragraph_direction);
28393 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28394 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28395 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28396 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28397 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28398 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28399 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28400 DEFSYM (Qeval, "eval");
28401 DEFSYM (QCdata, ":data");
28402 DEFSYM (Qdisplay, "display");
28403 DEFSYM (Qspace_width, "space-width");
28404 DEFSYM (Qraise, "raise");
28405 DEFSYM (Qslice, "slice");
28406 DEFSYM (Qspace, "space");
28407 DEFSYM (Qmargin, "margin");
28408 DEFSYM (Qpointer, "pointer");
28409 DEFSYM (Qleft_margin, "left-margin");
28410 DEFSYM (Qright_margin, "right-margin");
28411 DEFSYM (Qcenter, "center");
28412 DEFSYM (Qline_height, "line-height");
28413 DEFSYM (QCalign_to, ":align-to");
28414 DEFSYM (QCrelative_width, ":relative-width");
28415 DEFSYM (QCrelative_height, ":relative-height");
28416 DEFSYM (QCeval, ":eval");
28417 DEFSYM (QCpropertize, ":propertize");
28418 DEFSYM (QCfile, ":file");
28419 DEFSYM (Qfontified, "fontified");
28420 DEFSYM (Qfontification_functions, "fontification-functions");
28421 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28422 DEFSYM (Qescape_glyph, "escape-glyph");
28423 DEFSYM (Qnobreak_space, "nobreak-space");
28424 DEFSYM (Qimage, "image");
28425 DEFSYM (Qtext, "text");
28426 DEFSYM (Qboth, "both");
28427 DEFSYM (Qboth_horiz, "both-horiz");
28428 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28429 DEFSYM (QCmap, ":map");
28430 DEFSYM (QCpointer, ":pointer");
28431 DEFSYM (Qrect, "rect");
28432 DEFSYM (Qcircle, "circle");
28433 DEFSYM (Qpoly, "poly");
28434 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28435 DEFSYM (Qgrow_only, "grow-only");
28436 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28437 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28438 DEFSYM (Qposition, "position");
28439 DEFSYM (Qbuffer_position, "buffer-position");
28440 DEFSYM (Qobject, "object");
28441 DEFSYM (Qbar, "bar");
28442 DEFSYM (Qhbar, "hbar");
28443 DEFSYM (Qbox, "box");
28444 DEFSYM (Qhollow, "hollow");
28445 DEFSYM (Qhand, "hand");
28446 DEFSYM (Qarrow, "arrow");
28447 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28449 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28450 Fcons (intern_c_string ("void-variable"), Qnil)),
28451 Qnil);
28452 staticpro (&list_of_error);
28454 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28455 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28456 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28457 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28459 echo_buffer[0] = echo_buffer[1] = Qnil;
28460 staticpro (&echo_buffer[0]);
28461 staticpro (&echo_buffer[1]);
28463 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28464 staticpro (&echo_area_buffer[0]);
28465 staticpro (&echo_area_buffer[1]);
28467 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28468 staticpro (&Vmessages_buffer_name);
28470 mode_line_proptrans_alist = Qnil;
28471 staticpro (&mode_line_proptrans_alist);
28472 mode_line_string_list = Qnil;
28473 staticpro (&mode_line_string_list);
28474 mode_line_string_face = Qnil;
28475 staticpro (&mode_line_string_face);
28476 mode_line_string_face_prop = Qnil;
28477 staticpro (&mode_line_string_face_prop);
28478 Vmode_line_unwind_vector = Qnil;
28479 staticpro (&Vmode_line_unwind_vector);
28481 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28483 help_echo_string = Qnil;
28484 staticpro (&help_echo_string);
28485 help_echo_object = Qnil;
28486 staticpro (&help_echo_object);
28487 help_echo_window = Qnil;
28488 staticpro (&help_echo_window);
28489 previous_help_echo_string = Qnil;
28490 staticpro (&previous_help_echo_string);
28491 help_echo_pos = -1;
28493 DEFSYM (Qright_to_left, "right-to-left");
28494 DEFSYM (Qleft_to_right, "left-to-right");
28496 #ifdef HAVE_WINDOW_SYSTEM
28497 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28498 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28499 For example, if a block cursor is over a tab, it will be drawn as
28500 wide as that tab on the display. */);
28501 x_stretch_cursor_p = 0;
28502 #endif
28504 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28505 doc: /* Non-nil means highlight trailing whitespace.
28506 The face used for trailing whitespace is `trailing-whitespace'. */);
28507 Vshow_trailing_whitespace = Qnil;
28509 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28510 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28511 If the value is t, Emacs highlights non-ASCII chars which have the
28512 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28513 or `escape-glyph' face respectively.
28515 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28516 U+2011 (non-breaking hyphen) are affected.
28518 Any other non-nil value means to display these characters as a escape
28519 glyph followed by an ordinary space or hyphen.
28521 A value of nil means no special handling of these characters. */);
28522 Vnobreak_char_display = Qt;
28524 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28525 doc: /* The pointer shape to show in void text areas.
28526 A value of nil means to show the text pointer. Other options are `arrow',
28527 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28528 Vvoid_text_area_pointer = Qarrow;
28530 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28531 doc: /* Non-nil means don't actually do any redisplay.
28532 This is used for internal purposes. */);
28533 Vinhibit_redisplay = Qnil;
28535 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28536 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28537 Vglobal_mode_string = Qnil;
28539 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28540 doc: /* Marker for where to display an arrow on top of the buffer text.
28541 This must be the beginning of a line in order to work.
28542 See also `overlay-arrow-string'. */);
28543 Voverlay_arrow_position = Qnil;
28545 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28546 doc: /* String to display as an arrow in non-window frames.
28547 See also `overlay-arrow-position'. */);
28548 Voverlay_arrow_string = make_pure_c_string ("=>");
28550 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28551 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28552 The symbols on this list are examined during redisplay to determine
28553 where to display overlay arrows. */);
28554 Voverlay_arrow_variable_list
28555 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28557 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28558 doc: /* The number of lines to try scrolling a window by when point moves out.
28559 If that fails to bring point back on frame, point is centered instead.
28560 If this is zero, point is always centered after it moves off frame.
28561 If you want scrolling to always be a line at a time, you should set
28562 `scroll-conservatively' to a large value rather than set this to 1. */);
28564 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28565 doc: /* Scroll up to this many lines, to bring point back on screen.
28566 If point moves off-screen, redisplay will scroll by up to
28567 `scroll-conservatively' lines in order to bring point just barely
28568 onto the screen again. If that cannot be done, then redisplay
28569 recenters point as usual.
28571 If the value is greater than 100, redisplay will never recenter point,
28572 but will always scroll just enough text to bring point into view, even
28573 if you move far away.
28575 A value of zero means always recenter point if it moves off screen. */);
28576 scroll_conservatively = 0;
28578 DEFVAR_INT ("scroll-margin", scroll_margin,
28579 doc: /* Number of lines of margin at the top and bottom of a window.
28580 Recenter the window whenever point gets within this many lines
28581 of the top or bottom of the window. */);
28582 scroll_margin = 0;
28584 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28585 doc: /* Pixels per inch value for non-window system displays.
28586 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28587 Vdisplay_pixels_per_inch = make_float (72.0);
28589 #if GLYPH_DEBUG
28590 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28591 #endif
28593 DEFVAR_LISP ("truncate-partial-width-windows",
28594 Vtruncate_partial_width_windows,
28595 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28596 For an integer value, truncate lines in each window narrower than the
28597 full frame width, provided the window width is less than that integer;
28598 otherwise, respect the value of `truncate-lines'.
28600 For any other non-nil value, truncate lines in all windows that do
28601 not span the full frame width.
28603 A value of nil means to respect the value of `truncate-lines'.
28605 If `word-wrap' is enabled, you might want to reduce this. */);
28606 Vtruncate_partial_width_windows = make_number (50);
28608 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28609 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28610 Any other value means to use the appropriate face, `mode-line',
28611 `header-line', or `menu' respectively. */);
28612 mode_line_inverse_video = 1;
28614 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28615 doc: /* Maximum buffer size for which line number should be displayed.
28616 If the buffer is bigger than this, the line number does not appear
28617 in the mode line. A value of nil means no limit. */);
28618 Vline_number_display_limit = Qnil;
28620 DEFVAR_INT ("line-number-display-limit-width",
28621 line_number_display_limit_width,
28622 doc: /* Maximum line width (in characters) for line number display.
28623 If the average length of the lines near point is bigger than this, then the
28624 line number may be omitted from the mode line. */);
28625 line_number_display_limit_width = 200;
28627 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28628 doc: /* Non-nil means highlight region even in nonselected windows. */);
28629 highlight_nonselected_windows = 0;
28631 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28632 doc: /* Non-nil if more than one frame is visible on this display.
28633 Minibuffer-only frames don't count, but iconified frames do.
28634 This variable is not guaranteed to be accurate except while processing
28635 `frame-title-format' and `icon-title-format'. */);
28637 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28638 doc: /* Template for displaying the title bar of visible frames.
28639 \(Assuming the window manager supports this feature.)
28641 This variable has the same structure as `mode-line-format', except that
28642 the %c and %l constructs are ignored. It is used only on frames for
28643 which no explicit name has been set \(see `modify-frame-parameters'). */);
28645 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28646 doc: /* Template for displaying the title bar of an iconified frame.
28647 \(Assuming the window manager supports this feature.)
28648 This variable has the same structure as `mode-line-format' (which see),
28649 and is used only on frames for which no explicit name has been set
28650 \(see `modify-frame-parameters'). */);
28651 Vicon_title_format
28652 = Vframe_title_format
28653 = pure_cons (intern_c_string ("multiple-frames"),
28654 pure_cons (make_pure_c_string ("%b"),
28655 pure_cons (pure_cons (empty_unibyte_string,
28656 pure_cons (intern_c_string ("invocation-name"),
28657 pure_cons (make_pure_c_string ("@"),
28658 pure_cons (intern_c_string ("system-name"),
28659 Qnil)))),
28660 Qnil)));
28662 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28663 doc: /* Maximum number of lines to keep in the message log buffer.
28664 If nil, disable message logging. If t, log messages but don't truncate
28665 the buffer when it becomes large. */);
28666 Vmessage_log_max = make_number (100);
28668 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28669 doc: /* Functions called before redisplay, if window sizes have changed.
28670 The value should be a list of functions that take one argument.
28671 Just before redisplay, for each frame, if any of its windows have changed
28672 size since the last redisplay, or have been split or deleted,
28673 all the functions in the list are called, with the frame as argument. */);
28674 Vwindow_size_change_functions = Qnil;
28676 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28677 doc: /* List of functions to call before redisplaying a window with scrolling.
28678 Each function is called with two arguments, the window and its new
28679 display-start position. Note that these functions are also called by
28680 `set-window-buffer'. Also note that the value of `window-end' is not
28681 valid when these functions are called.
28683 Warning: Do not use this feature to alter the way the window
28684 is scrolled. It is not designed for that, and such use probably won't
28685 work. */);
28686 Vwindow_scroll_functions = Qnil;
28688 DEFVAR_LISP ("window-text-change-functions",
28689 Vwindow_text_change_functions,
28690 doc: /* Functions to call in redisplay when text in the window might change. */);
28691 Vwindow_text_change_functions = Qnil;
28693 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28694 doc: /* Functions called when redisplay of a window reaches the end trigger.
28695 Each function is called with two arguments, the window and the end trigger value.
28696 See `set-window-redisplay-end-trigger'. */);
28697 Vredisplay_end_trigger_functions = Qnil;
28699 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28700 doc: /* Non-nil means autoselect window with mouse pointer.
28701 If nil, do not autoselect windows.
28702 A positive number means delay autoselection by that many seconds: a
28703 window is autoselected only after the mouse has remained in that
28704 window for the duration of the delay.
28705 A negative number has a similar effect, but causes windows to be
28706 autoselected only after the mouse has stopped moving. \(Because of
28707 the way Emacs compares mouse events, you will occasionally wait twice
28708 that time before the window gets selected.\)
28709 Any other value means to autoselect window instantaneously when the
28710 mouse pointer enters it.
28712 Autoselection selects the minibuffer only if it is active, and never
28713 unselects the minibuffer if it is active.
28715 When customizing this variable make sure that the actual value of
28716 `focus-follows-mouse' matches the behavior of your window manager. */);
28717 Vmouse_autoselect_window = Qnil;
28719 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28720 doc: /* Non-nil means automatically resize tool-bars.
28721 This dynamically changes the tool-bar's height to the minimum height
28722 that is needed to make all tool-bar items visible.
28723 If value is `grow-only', the tool-bar's height is only increased
28724 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28725 Vauto_resize_tool_bars = Qt;
28727 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28728 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28729 auto_raise_tool_bar_buttons_p = 1;
28731 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28732 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28733 make_cursor_line_fully_visible_p = 1;
28735 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28736 doc: /* Border below tool-bar in pixels.
28737 If an integer, use it as the height of the border.
28738 If it is one of `internal-border-width' or `border-width', use the
28739 value of the corresponding frame parameter.
28740 Otherwise, no border is added below the tool-bar. */);
28741 Vtool_bar_border = Qinternal_border_width;
28743 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28744 doc: /* Margin around tool-bar buttons in pixels.
28745 If an integer, use that for both horizontal and vertical margins.
28746 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28747 HORZ specifying the horizontal margin, and VERT specifying the
28748 vertical margin. */);
28749 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28751 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28752 doc: /* Relief thickness of tool-bar buttons. */);
28753 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28755 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28756 doc: /* Tool bar style to use.
28757 It can be one of
28758 image - show images only
28759 text - show text only
28760 both - show both, text below image
28761 both-horiz - show text to the right of the image
28762 text-image-horiz - show text to the left of the image
28763 any other - use system default or image if no system default.
28765 This variable only affects the GTK+ toolkit version of Emacs. */);
28766 Vtool_bar_style = Qnil;
28768 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28769 doc: /* Maximum number of characters a label can have to be shown.
28770 The tool bar style must also show labels for this to have any effect, see
28771 `tool-bar-style'. */);
28772 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28774 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28775 doc: /* List of functions to call to fontify regions of text.
28776 Each function is called with one argument POS. Functions must
28777 fontify a region starting at POS in the current buffer, and give
28778 fontified regions the property `fontified'. */);
28779 Vfontification_functions = Qnil;
28780 Fmake_variable_buffer_local (Qfontification_functions);
28782 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28783 unibyte_display_via_language_environment,
28784 doc: /* Non-nil means display unibyte text according to language environment.
28785 Specifically, this means that raw bytes in the range 160-255 decimal
28786 are displayed by converting them to the equivalent multibyte characters
28787 according to the current language environment. As a result, they are
28788 displayed according to the current fontset.
28790 Note that this variable affects only how these bytes are displayed,
28791 but does not change the fact they are interpreted as raw bytes. */);
28792 unibyte_display_via_language_environment = 0;
28794 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28795 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28796 If a float, it specifies a fraction of the mini-window frame's height.
28797 If an integer, it specifies a number of lines. */);
28798 Vmax_mini_window_height = make_float (0.25);
28800 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28801 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28802 A value of nil means don't automatically resize mini-windows.
28803 A value of t means resize them to fit the text displayed in them.
28804 A value of `grow-only', the default, means let mini-windows grow only;
28805 they return to their normal size when the minibuffer is closed, or the
28806 echo area becomes empty. */);
28807 Vresize_mini_windows = Qgrow_only;
28809 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28810 doc: /* Alist specifying how to blink the cursor off.
28811 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28812 `cursor-type' frame-parameter or variable equals ON-STATE,
28813 comparing using `equal', Emacs uses OFF-STATE to specify
28814 how to blink it off. ON-STATE and OFF-STATE are values for
28815 the `cursor-type' frame parameter.
28817 If a frame's ON-STATE has no entry in this list,
28818 the frame's other specifications determine how to blink the cursor off. */);
28819 Vblink_cursor_alist = Qnil;
28821 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28822 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28823 If non-nil, windows are automatically scrolled horizontally to make
28824 point visible. */);
28825 automatic_hscrolling_p = 1;
28826 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28828 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28829 doc: /* How many columns away from the window edge point is allowed to get
28830 before automatic hscrolling will horizontally scroll the window. */);
28831 hscroll_margin = 5;
28833 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28834 doc: /* How many columns to scroll the window when point gets too close to the edge.
28835 When point is less than `hscroll-margin' columns from the window
28836 edge, automatic hscrolling will scroll the window by the amount of columns
28837 determined by this variable. If its value is a positive integer, scroll that
28838 many columns. If it's a positive floating-point number, it specifies the
28839 fraction of the window's width to scroll. If it's nil or zero, point will be
28840 centered horizontally after the scroll. Any other value, including negative
28841 numbers, are treated as if the value were zero.
28843 Automatic hscrolling always moves point outside the scroll margin, so if
28844 point was more than scroll step columns inside the margin, the window will
28845 scroll more than the value given by the scroll step.
28847 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28848 and `scroll-right' overrides this variable's effect. */);
28849 Vhscroll_step = make_number (0);
28851 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28852 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28853 Bind this around calls to `message' to let it take effect. */);
28854 message_truncate_lines = 0;
28856 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28857 doc: /* Normal hook run to update the menu bar definitions.
28858 Redisplay runs this hook before it redisplays the menu bar.
28859 This is used to update submenus such as Buffers,
28860 whose contents depend on various data. */);
28861 Vmenu_bar_update_hook = Qnil;
28863 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28864 doc: /* Frame for which we are updating a menu.
28865 The enable predicate for a menu binding should check this variable. */);
28866 Vmenu_updating_frame = Qnil;
28868 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28869 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28870 inhibit_menubar_update = 0;
28872 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28873 doc: /* Prefix prepended to all continuation lines at display time.
28874 The value may be a string, an image, or a stretch-glyph; it is
28875 interpreted in the same way as the value of a `display' text property.
28877 This variable is overridden by any `wrap-prefix' text or overlay
28878 property.
28880 To add a prefix to non-continuation lines, use `line-prefix'. */);
28881 Vwrap_prefix = Qnil;
28882 DEFSYM (Qwrap_prefix, "wrap-prefix");
28883 Fmake_variable_buffer_local (Qwrap_prefix);
28885 DEFVAR_LISP ("line-prefix", Vline_prefix,
28886 doc: /* Prefix prepended to all non-continuation lines at display time.
28887 The value may be a string, an image, or a stretch-glyph; it is
28888 interpreted in the same way as the value of a `display' text property.
28890 This variable is overridden by any `line-prefix' text or overlay
28891 property.
28893 To add a prefix to continuation lines, use `wrap-prefix'. */);
28894 Vline_prefix = Qnil;
28895 DEFSYM (Qline_prefix, "line-prefix");
28896 Fmake_variable_buffer_local (Qline_prefix);
28898 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28899 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28900 inhibit_eval_during_redisplay = 0;
28902 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28903 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28904 inhibit_free_realized_faces = 0;
28906 #if GLYPH_DEBUG
28907 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28908 doc: /* Inhibit try_window_id display optimization. */);
28909 inhibit_try_window_id = 0;
28911 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28912 doc: /* Inhibit try_window_reusing display optimization. */);
28913 inhibit_try_window_reusing = 0;
28915 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28916 doc: /* Inhibit try_cursor_movement display optimization. */);
28917 inhibit_try_cursor_movement = 0;
28918 #endif /* GLYPH_DEBUG */
28920 DEFVAR_INT ("overline-margin", overline_margin,
28921 doc: /* Space between overline and text, in pixels.
28922 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28923 margin to the character height. */);
28924 overline_margin = 2;
28926 DEFVAR_INT ("underline-minimum-offset",
28927 underline_minimum_offset,
28928 doc: /* Minimum distance between baseline and underline.
28929 This can improve legibility of underlined text at small font sizes,
28930 particularly when using variable `x-use-underline-position-properties'
28931 with fonts that specify an UNDERLINE_POSITION relatively close to the
28932 baseline. The default value is 1. */);
28933 underline_minimum_offset = 1;
28935 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28936 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28937 This feature only works when on a window system that can change
28938 cursor shapes. */);
28939 display_hourglass_p = 1;
28941 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28942 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28943 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28945 hourglass_atimer = NULL;
28946 hourglass_shown_p = 0;
28948 DEFSYM (Qglyphless_char, "glyphless-char");
28949 DEFSYM (Qhex_code, "hex-code");
28950 DEFSYM (Qempty_box, "empty-box");
28951 DEFSYM (Qthin_space, "thin-space");
28952 DEFSYM (Qzero_width, "zero-width");
28954 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28955 /* Intern this now in case it isn't already done.
28956 Setting this variable twice is harmless.
28957 But don't staticpro it here--that is done in alloc.c. */
28958 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28959 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28961 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28962 doc: /* Char-table defining glyphless characters.
28963 Each element, if non-nil, should be one of the following:
28964 an ASCII acronym string: display this string in a box
28965 `hex-code': display the hexadecimal code of a character in a box
28966 `empty-box': display as an empty box
28967 `thin-space': display as 1-pixel width space
28968 `zero-width': don't display
28969 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28970 display method for graphical terminals and text terminals respectively.
28971 GRAPHICAL and TEXT should each have one of the values listed above.
28973 The char-table has one extra slot to control the display of a character for
28974 which no font is found. This slot only takes effect on graphical terminals.
28975 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28976 `thin-space'. The default is `empty-box'. */);
28977 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28978 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28979 Qempty_box);
28983 /* Initialize this module when Emacs starts. */
28985 void
28986 init_xdisp (void)
28988 current_header_line_height = current_mode_line_height = -1;
28990 CHARPOS (this_line_start_pos) = 0;
28992 if (!noninteractive)
28994 struct window *m = XWINDOW (minibuf_window);
28995 Lisp_Object frame = m->frame;
28996 struct frame *f = XFRAME (frame);
28997 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28998 struct window *r = XWINDOW (root);
28999 int i;
29001 echo_area_window = minibuf_window;
29003 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29004 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29005 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29006 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29007 XSETFASTINT (m->total_lines, 1);
29008 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29010 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29011 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29012 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29014 /* The default ellipsis glyphs `...'. */
29015 for (i = 0; i < 3; ++i)
29016 default_invis_vector[i] = make_number ('.');
29020 /* Allocate the buffer for frame titles.
29021 Also used for `format-mode-line'. */
29022 int size = 100;
29023 mode_line_noprop_buf = (char *) xmalloc (size);
29024 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29025 mode_line_noprop_ptr = mode_line_noprop_buf;
29026 mode_line_target = MODE_LINE_DISPLAY;
29029 help_echo_showing_p = 0;
29032 /* Since w32 does not support atimers, it defines its own implementation of
29033 the following three functions in w32fns.c. */
29034 #ifndef WINDOWSNT
29036 /* Platform-independent portion of hourglass implementation. */
29038 /* Cancel a currently active hourglass timer, and start a new one. */
29039 void
29040 start_hourglass (void)
29042 #if defined (HAVE_WINDOW_SYSTEM)
29043 EMACS_TIME delay;
29044 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
29046 cancel_hourglass ();
29048 if (NUMBERP (Vhourglass_delay))
29050 double duration = extract_float (Vhourglass_delay);
29051 if (0 < duration)
29052 duration_to_sec_usec (duration, &secs, &usecs);
29055 EMACS_SET_SECS_USECS (delay, secs, usecs);
29056 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29057 show_hourglass, NULL);
29058 #endif
29062 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29063 shown. */
29064 void
29065 cancel_hourglass (void)
29067 #if defined (HAVE_WINDOW_SYSTEM)
29068 if (hourglass_atimer)
29070 cancel_atimer (hourglass_atimer);
29071 hourglass_atimer = NULL;
29074 if (hourglass_shown_p)
29075 hide_hourglass ();
29076 #endif
29078 #endif /* ! WINDOWSNT */