* frame.h (FSET): Remove (Bug#12215).
[emacs.git] / src / xdisp.c
blobab4375d2479dafd8d4c78fac1ba8e5f865c8320f
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22 Redisplay.
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
53 expose_window (asynchronous) |
55 X expose events -----+
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
84 . try_cursor_movement
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
90 . try_window_reusing_current_matrix
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
96 . try_window_id
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
102 . try_window
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
115 Desired matrices.
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
160 Frame matrices.
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
181 Bidirectional display.
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
224 Bidirectional display and character compositions
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
251 Moving an iterator in bidirectional text
252 without producing glyphs
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
315 #include "font.h"
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
321 #define INFINITY 10000000
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
346 static Lisp_Object Qfontification_functions;
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
351 /* Non-nil means don't actually do any redisplay. */
353 Lisp_Object Qinhibit_redisplay;
355 /* Names of text properties relevant for redisplay. */
357 Lisp_Object Qdisplay;
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
368 #ifdef HAVE_WINDOW_SYSTEM
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
402 /* Name of the face used to highlight trailing whitespace. */
404 static Lisp_Object Qtrailing_whitespace;
406 /* Name and number of the face used to highlight escape glyphs. */
408 static Lisp_Object Qescape_glyph;
410 /* Name and number of the face used to highlight non-breaking spaces. */
412 static Lisp_Object Qnobreak_space;
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
417 Lisp_Object Qimage;
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
430 int noninteractive_need_newline;
432 /* Non-zero means print newline to message log before next message. */
434 static int message_log_need_newline;
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
448 static struct text_pos this_line_start_pos;
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
453 static struct text_pos this_line_end_pos;
455 /* The vertical positions and the height of this line. */
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
464 static int this_line_start_x;
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
470 static struct text_pos this_line_min_pos;
472 /* Buffer that this_line_.* variables are referring to. */
474 static struct buffer *this_line_buffer;
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
489 Lisp_Object Qmenu_bar_update_hook;
491 /* Nonzero if an overlay arrow has been displayed in this window. */
493 static int overlay_arrow_seen;
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
499 int buffer_shared;
501 /* Vector containing glyphs for an ellipsis `...'. */
503 static Lisp_Object default_invis_vector[3];
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
509 Lisp_Object echo_area_window;
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
516 static Lisp_Object Vmessage_stack;
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
521 static int message_enable_multibyte;
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
525 int update_mode_lines;
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
530 int windows_or_buffers_changed;
532 /* Nonzero means a frame's cursor type has been changed. */
534 int cursor_type_changed;
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
539 static int line_number_displayed;
541 /* The name of the *Messages* buffer, a string. */
543 static Lisp_Object Vmessages_buffer_name;
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
548 Lisp_Object echo_area_buffer[2];
550 /* The buffers referenced from echo_area_buffer. */
552 static Lisp_Object echo_buffer[2];
554 /* A vector saved used in with_area_buffer to reduce consing. */
556 static Lisp_Object Vwith_echo_area_save_vector;
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
561 static int display_last_displayed_message_p;
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
566 static int message_buf_print;
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
576 static int message_cleared_p;
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
585 /* Ascent and height of the last line processed by move_it_to. */
587 static int last_max_ascent, last_height;
589 /* Non-zero if there's a help-echo in the echo area. */
591 int help_echo_showing_p;
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
597 int current_mode_line_height, current_header_line_height;
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
605 #define TEXT_PROP_DISTANCE_LIMIT 100
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
630 #ifdef GLYPH_DEBUG
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG defined. */
635 int trace_redisplay_p;
637 #endif /* GLYPH_DEBUG */
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
648 static Lisp_Object Qauto_hscroll_mode;
650 /* Buffer being redisplayed -- for redisplay_window_error. */
652 static struct buffer *displayed_buffer;
654 /* Value returned from text property handlers (see below). */
656 enum prop_handled
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
664 /* A description of text properties that redisplay is interested
665 in. */
667 struct props
669 /* The name of the property. */
670 Lisp_Object *name;
672 /* A unique index for the property. */
673 enum prop_idx idx;
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
687 /* Properties handled by iterators. */
689 static struct props it_props[] =
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
706 /* Enumeration returned by some move_it_.* functions internally. */
708 enum move_it_result
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
739 /* Similarly for the image cache. */
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
749 /* Non-zero while redisplay_internal is in progress. */
751 int redisplaying_p;
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
764 /* Temporary variable for XTread_socket. */
766 Lisp_Object previous_help_echo_string;
768 /* Platform-independent portion of hourglass implementation. */
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
794 /* Function prototypes. */
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
807 static void handle_line_prefix (struct it *);
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static void insert_left_trunc_glyphs (struct it *);
842 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
843 Lisp_Object);
844 static void extend_face_to_end_of_line (struct it *);
845 static int append_space_for_newline (struct it *, int);
846 static int cursor_row_fully_visible_p (struct window *, int, int);
847 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
848 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
849 static int trailing_whitespace_p (ptrdiff_t);
850 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
851 static void push_it (struct it *, struct text_pos *);
852 static void iterate_out_of_display_property (struct it *);
853 static void pop_it (struct it *);
854 static void sync_frame_with_window_matrix_rows (struct window *);
855 static void select_frame_for_redisplay (Lisp_Object);
856 static void redisplay_internal (void);
857 static int echo_area_display (int);
858 static void redisplay_windows (Lisp_Object);
859 static void redisplay_window (Lisp_Object, int);
860 static Lisp_Object redisplay_window_error (Lisp_Object);
861 static Lisp_Object redisplay_window_0 (Lisp_Object);
862 static Lisp_Object redisplay_window_1 (Lisp_Object);
863 static int set_cursor_from_row (struct window *, struct glyph_row *,
864 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
865 int, int);
866 static int update_menu_bar (struct frame *, int, int);
867 static int try_window_reusing_current_matrix (struct window *);
868 static int try_window_id (struct window *);
869 static int display_line (struct it *);
870 static int display_mode_lines (struct window *);
871 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
872 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
873 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
874 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
875 static void display_menu_bar (struct window *);
876 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
877 ptrdiff_t *);
878 static int display_string (const char *, Lisp_Object, Lisp_Object,
879 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
880 static void compute_line_metrics (struct it *);
881 static void run_redisplay_end_trigger_hook (struct it *);
882 static int get_overlay_strings (struct it *, ptrdiff_t);
883 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
884 static void next_overlay_string (struct it *);
885 static void reseat (struct it *, struct text_pos, int);
886 static void reseat_1 (struct it *, struct text_pos, int);
887 static void back_to_previous_visible_line_start (struct it *);
888 void reseat_at_previous_visible_line_start (struct it *);
889 static void reseat_at_next_visible_line_start (struct it *, int);
890 static int next_element_from_ellipsis (struct it *);
891 static int next_element_from_display_vector (struct it *);
892 static int next_element_from_string (struct it *);
893 static int next_element_from_c_string (struct it *);
894 static int next_element_from_buffer (struct it *);
895 static int next_element_from_composition (struct it *);
896 static int next_element_from_image (struct it *);
897 static int next_element_from_stretch (struct it *);
898 static void load_overlay_strings (struct it *, ptrdiff_t);
899 static int init_from_display_pos (struct it *, struct window *,
900 struct display_pos *);
901 static void reseat_to_string (struct it *, const char *,
902 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
903 static int get_next_display_element (struct it *);
904 static enum move_it_result
905 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
906 enum move_operation_enum);
907 void move_it_vertically_backward (struct it *, int);
908 static void init_to_row_start (struct it *, struct window *,
909 struct glyph_row *);
910 static int init_to_row_end (struct it *, struct window *,
911 struct glyph_row *);
912 static void back_to_previous_line_start (struct it *);
913 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
914 static struct text_pos string_pos_nchars_ahead (struct text_pos,
915 Lisp_Object, ptrdiff_t);
916 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
917 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
918 static ptrdiff_t number_of_chars (const char *, int);
919 static void compute_stop_pos (struct it *);
920 static void compute_string_pos (struct text_pos *, struct text_pos,
921 Lisp_Object);
922 static int face_before_or_after_it_pos (struct it *, int);
923 static ptrdiff_t next_overlay_change (ptrdiff_t);
924 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
925 Lisp_Object, struct text_pos *, ptrdiff_t, int);
926 static int handle_single_display_spec (struct it *, Lisp_Object,
927 Lisp_Object, Lisp_Object,
928 struct text_pos *, ptrdiff_t, int, int);
929 static int underlying_face_id (struct it *);
930 static int in_ellipses_for_invisible_text_p (struct display_pos *,
931 struct window *);
933 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
934 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936 #ifdef HAVE_WINDOW_SYSTEM
938 static void x_consider_frame_title (Lisp_Object);
939 static int tool_bar_lines_needed (struct frame *, int *);
940 static void update_tool_bar (struct frame *, int);
941 static void build_desired_tool_bar_string (struct frame *f);
942 static int redisplay_tool_bar (struct frame *);
943 static void display_tool_bar_line (struct it *, int);
944 static void notice_overwritten_cursor (struct window *,
945 enum glyph_row_area,
946 int, int, int, int);
947 static void append_stretch_glyph (struct it *, Lisp_Object,
948 int, int, int);
951 #endif /* HAVE_WINDOW_SYSTEM */
953 static void produce_special_glyphs (struct it *, enum display_element_type);
954 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
967 This is the height of W minus the height of a mode line, if any. */
970 window_text_bottom_y (struct window *w)
972 int height = WINDOW_TOTAL_HEIGHT (w);
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
984 window_box_width (struct window *w, int area)
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
989 if (!w->pseudo_window_p)
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
993 if (area == TEXT_AREA)
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1001 else if (area == LEFT_MARGIN_AREA)
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1007 else if (area == RIGHT_MARGIN_AREA)
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1023 window_box_height (struct window *w)
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1028 eassert (height >= 0);
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1036 if (WINDOW_WANTS_MODELINE_P (w))
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1070 window_box_left_offset (struct window *w, int area)
1072 int x;
1074 if (w->pseudo_window_p)
1075 return 0;
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1093 return x;
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1102 window_box_right_offset (struct window *w, int area)
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1112 window_box_left (struct window *w, int area)
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1123 return x;
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1132 window_box_right (struct window *w, int area)
1134 return window_box_left (w, area) + window_box_width (w, area);
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1191 line_bottom_y (struct it *it)
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1196 if (line_height == 0)
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1207 else
1209 struct glyph_row *row = it->glyph_row;
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1222 return line_top_y + line_height;
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1230 if (CONSP (spec))
1232 while (CONSP (spec))
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1239 else if (VECTORP (spec))
1241 ptrdiff_t i;
1243 for (i = 0; i < ASIZE (spec); i++)
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1248 return Qnil;
1251 return spec;
1255 /* Limit insanely large values of W->hscroll on frame F to the largest
1256 value that will still prevent first_visible_x and last_visible_x of
1257 'struct it' from overflowing an int. */
1258 static inline int
1259 window_hscroll_limited (struct window *w, struct frame *f)
1261 ptrdiff_t window_hscroll = w->hscroll;
1262 int window_text_width = window_box_width (w, TEXT_AREA);
1263 int colwidth = FRAME_COLUMN_WIDTH (f);
1265 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1266 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1268 return window_hscroll;
1271 /* Return 1 if position CHARPOS is visible in window W.
1272 CHARPOS < 0 means return info about WINDOW_END position.
1273 If visible, set *X and *Y to pixel coordinates of top left corner.
1274 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1275 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1278 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1279 int *rtop, int *rbot, int *rowh, int *vpos)
1281 struct it it;
1282 void *itdata = bidi_shelve_cache ();
1283 struct text_pos top;
1284 int visible_p = 0;
1285 struct buffer *old_buffer = NULL;
1287 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1288 return visible_p;
1290 if (XBUFFER (w->buffer) != current_buffer)
1292 old_buffer = current_buffer;
1293 set_buffer_internal_1 (XBUFFER (w->buffer));
1296 SET_TEXT_POS_FROM_MARKER (top, w->start);
1297 /* Scrolling a minibuffer window via scroll bar when the echo area
1298 shows long text sometimes resets the minibuffer contents behind
1299 our backs. */
1300 if (CHARPOS (top) > ZV)
1301 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1303 /* Compute exact mode line heights. */
1304 if (WINDOW_WANTS_MODELINE_P (w))
1305 current_mode_line_height
1306 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1307 BVAR (current_buffer, mode_line_format));
1309 if (WINDOW_WANTS_HEADER_LINE_P (w))
1310 current_header_line_height
1311 = display_mode_line (w, HEADER_LINE_FACE_ID,
1312 BVAR (current_buffer, header_line_format));
1314 start_display (&it, w, top);
1315 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1316 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1318 if (charpos >= 0
1319 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1320 && IT_CHARPOS (it) >= charpos)
1321 /* When scanning backwards under bidi iteration, move_it_to
1322 stops at or _before_ CHARPOS, because it stops at or to
1323 the _right_ of the character at CHARPOS. */
1324 || (it.bidi_p && it.bidi_it.scan_dir == -1
1325 && IT_CHARPOS (it) <= charpos)))
1327 /* We have reached CHARPOS, or passed it. How the call to
1328 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1329 or covered by a display property, move_it_to stops at the end
1330 of the invisible text, to the right of CHARPOS. (ii) If
1331 CHARPOS is in a display vector, move_it_to stops on its last
1332 glyph. */
1333 int top_x = it.current_x;
1334 int top_y = it.current_y;
1335 /* Calling line_bottom_y may change it.method, it.position, etc. */
1336 enum it_method it_method = it.method;
1337 int bottom_y = (last_height = 0, line_bottom_y (&it));
1338 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1340 if (top_y < window_top_y)
1341 visible_p = bottom_y > window_top_y;
1342 else if (top_y < it.last_visible_y)
1343 visible_p = 1;
1344 if (bottom_y >= it.last_visible_y
1345 && it.bidi_p && it.bidi_it.scan_dir == -1
1346 && IT_CHARPOS (it) < charpos)
1348 /* When the last line of the window is scanned backwards
1349 under bidi iteration, we could be duped into thinking
1350 that we have passed CHARPOS, when in fact move_it_to
1351 simply stopped short of CHARPOS because it reached
1352 last_visible_y. To see if that's what happened, we call
1353 move_it_to again with a slightly larger vertical limit,
1354 and see if it actually moved vertically; if it did, we
1355 didn't really reach CHARPOS, which is beyond window end. */
1356 struct it save_it = it;
1357 /* Why 10? because we don't know how many canonical lines
1358 will the height of the next line(s) be. So we guess. */
1359 int ten_more_lines =
1360 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1362 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1363 MOVE_TO_POS | MOVE_TO_Y);
1364 if (it.current_y > top_y)
1365 visible_p = 0;
1367 it = save_it;
1369 if (visible_p)
1371 if (it_method == GET_FROM_DISPLAY_VECTOR)
1373 /* We stopped on the last glyph of a display vector.
1374 Try and recompute. Hack alert! */
1375 if (charpos < 2 || top.charpos >= charpos)
1376 top_x = it.glyph_row->x;
1377 else
1379 struct it it2;
1380 start_display (&it2, w, top);
1381 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1382 get_next_display_element (&it2);
1383 PRODUCE_GLYPHS (&it2);
1384 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1385 || it2.current_x > it2.last_visible_x)
1386 top_x = it.glyph_row->x;
1387 else
1389 top_x = it2.current_x;
1390 top_y = it2.current_y;
1394 else if (IT_CHARPOS (it) != charpos)
1396 Lisp_Object cpos = make_number (charpos);
1397 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1398 Lisp_Object string = string_from_display_spec (spec);
1399 int newline_in_string = 0;
1401 if (STRINGP (string))
1403 const char *s = SSDATA (string);
1404 const char *e = s + SBYTES (string);
1405 while (s < e)
1407 if (*s++ == '\n')
1409 newline_in_string = 1;
1410 break;
1414 /* The tricky code below is needed because there's a
1415 discrepancy between move_it_to and how we set cursor
1416 when the display line ends in a newline from a
1417 display string. move_it_to will stop _after_ such
1418 display strings, whereas set_cursor_from_row
1419 conspires with cursor_row_p to place the cursor on
1420 the first glyph produced from the display string. */
1422 /* We have overshoot PT because it is covered by a
1423 display property whose value is a string. If the
1424 string includes embedded newlines, we are also in the
1425 wrong display line. Backtrack to the correct line,
1426 where the display string begins. */
1427 if (newline_in_string)
1429 Lisp_Object startpos, endpos;
1430 EMACS_INT start, end;
1431 struct it it3;
1432 int it3_moved;
1434 /* Find the first and the last buffer positions
1435 covered by the display string. */
1436 endpos =
1437 Fnext_single_char_property_change (cpos, Qdisplay,
1438 Qnil, Qnil);
1439 startpos =
1440 Fprevious_single_char_property_change (endpos, Qdisplay,
1441 Qnil, Qnil);
1442 start = XFASTINT (startpos);
1443 end = XFASTINT (endpos);
1444 /* Move to the last buffer position before the
1445 display property. */
1446 start_display (&it3, w, top);
1447 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1448 /* Move forward one more line if the position before
1449 the display string is a newline or if it is the
1450 rightmost character on a line that is
1451 continued or word-wrapped. */
1452 if (it3.method == GET_FROM_BUFFER
1453 && it3.c == '\n')
1454 move_it_by_lines (&it3, 1);
1455 else if (move_it_in_display_line_to (&it3, -1,
1456 it3.current_x
1457 + it3.pixel_width,
1458 MOVE_TO_X)
1459 == MOVE_LINE_CONTINUED)
1461 move_it_by_lines (&it3, 1);
1462 /* When we are under word-wrap, the #$@%!
1463 move_it_by_lines moves 2 lines, so we need to
1464 fix that up. */
1465 if (it3.line_wrap == WORD_WRAP)
1466 move_it_by_lines (&it3, -1);
1469 /* Record the vertical coordinate of the display
1470 line where we wound up. */
1471 top_y = it3.current_y;
1472 if (it3.bidi_p)
1474 /* When characters are reordered for display,
1475 the character displayed to the left of the
1476 display string could be _after_ the display
1477 property in the logical order. Use the
1478 smallest vertical position of these two. */
1479 start_display (&it3, w, top);
1480 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1481 if (it3.current_y < top_y)
1482 top_y = it3.current_y;
1484 /* Move from the top of the window to the beginning
1485 of the display line where the display string
1486 begins. */
1487 start_display (&it3, w, top);
1488 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1489 /* If it3_moved stays zero after the 'while' loop
1490 below, that means we already were at a newline
1491 before the loop (e.g., the display string begins
1492 with a newline), so we don't need to (and cannot)
1493 inspect the glyphs of it3.glyph_row, because
1494 PRODUCE_GLYPHS will not produce anything for a
1495 newline, and thus it3.glyph_row stays at its
1496 stale content it got at top of the window. */
1497 it3_moved = 0;
1498 /* Finally, advance the iterator until we hit the
1499 first display element whose character position is
1500 CHARPOS, or until the first newline from the
1501 display string, which signals the end of the
1502 display line. */
1503 while (get_next_display_element (&it3))
1505 PRODUCE_GLYPHS (&it3);
1506 if (IT_CHARPOS (it3) == charpos
1507 || ITERATOR_AT_END_OF_LINE_P (&it3))
1508 break;
1509 it3_moved = 1;
1510 set_iterator_to_next (&it3, 0);
1512 top_x = it3.current_x - it3.pixel_width;
1513 /* Normally, we would exit the above loop because we
1514 found the display element whose character
1515 position is CHARPOS. For the contingency that we
1516 didn't, and stopped at the first newline from the
1517 display string, move back over the glyphs
1518 produced from the string, until we find the
1519 rightmost glyph not from the string. */
1520 if (it3_moved
1521 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1523 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1524 + it3.glyph_row->used[TEXT_AREA];
1526 while (EQ ((g - 1)->object, string))
1528 --g;
1529 top_x -= g->pixel_width;
1531 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1532 + it3.glyph_row->used[TEXT_AREA]);
1537 *x = top_x;
1538 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1539 *rtop = max (0, window_top_y - top_y);
1540 *rbot = max (0, bottom_y - it.last_visible_y);
1541 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1542 - max (top_y, window_top_y)));
1543 *vpos = it.vpos;
1546 else
1548 /* We were asked to provide info about WINDOW_END. */
1549 struct it it2;
1550 void *it2data = NULL;
1552 SAVE_IT (it2, it, it2data);
1553 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1554 move_it_by_lines (&it, 1);
1555 if (charpos < IT_CHARPOS (it)
1556 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1558 visible_p = 1;
1559 RESTORE_IT (&it2, &it2, it2data);
1560 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1561 *x = it2.current_x;
1562 *y = it2.current_y + it2.max_ascent - it2.ascent;
1563 *rtop = max (0, -it2.current_y);
1564 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1565 - it.last_visible_y));
1566 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1567 it.last_visible_y)
1568 - max (it2.current_y,
1569 WINDOW_HEADER_LINE_HEIGHT (w))));
1570 *vpos = it2.vpos;
1572 else
1573 bidi_unshelve_cache (it2data, 1);
1575 bidi_unshelve_cache (itdata, 0);
1577 if (old_buffer)
1578 set_buffer_internal_1 (old_buffer);
1580 current_header_line_height = current_mode_line_height = -1;
1582 if (visible_p && w->hscroll > 0)
1583 *x -=
1584 window_hscroll_limited (w, WINDOW_XFRAME (w))
1585 * WINDOW_FRAME_COLUMN_WIDTH (w);
1587 #if 0
1588 /* Debugging code. */
1589 if (visible_p)
1590 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1591 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1592 else
1593 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1594 #endif
1596 return visible_p;
1600 /* Return the next character from STR. Return in *LEN the length of
1601 the character. This is like STRING_CHAR_AND_LENGTH but never
1602 returns an invalid character. If we find one, we return a `?', but
1603 with the length of the invalid character. */
1605 static inline int
1606 string_char_and_length (const unsigned char *str, int *len)
1608 int c;
1610 c = STRING_CHAR_AND_LENGTH (str, *len);
1611 if (!CHAR_VALID_P (c))
1612 /* We may not change the length here because other places in Emacs
1613 don't use this function, i.e. they silently accept invalid
1614 characters. */
1615 c = '?';
1617 return c;
1622 /* Given a position POS containing a valid character and byte position
1623 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1625 static struct text_pos
1626 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1628 eassert (STRINGP (string) && nchars >= 0);
1630 if (STRING_MULTIBYTE (string))
1632 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1633 int len;
1635 while (nchars--)
1637 string_char_and_length (p, &len);
1638 p += len;
1639 CHARPOS (pos) += 1;
1640 BYTEPOS (pos) += len;
1643 else
1644 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1646 return pos;
1650 /* Value is the text position, i.e. character and byte position,
1651 for character position CHARPOS in STRING. */
1653 static inline struct text_pos
1654 string_pos (ptrdiff_t charpos, Lisp_Object string)
1656 struct text_pos pos;
1657 eassert (STRINGP (string));
1658 eassert (charpos >= 0);
1659 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1660 return pos;
1664 /* Value is a text position, i.e. character and byte position, for
1665 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1666 means recognize multibyte characters. */
1668 static struct text_pos
1669 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1671 struct text_pos pos;
1673 eassert (s != NULL);
1674 eassert (charpos >= 0);
1676 if (multibyte_p)
1678 int len;
1680 SET_TEXT_POS (pos, 0, 0);
1681 while (charpos--)
1683 string_char_and_length ((const unsigned char *) s, &len);
1684 s += len;
1685 CHARPOS (pos) += 1;
1686 BYTEPOS (pos) += len;
1689 else
1690 SET_TEXT_POS (pos, charpos, charpos);
1692 return pos;
1696 /* Value is the number of characters in C string S. MULTIBYTE_P
1697 non-zero means recognize multibyte characters. */
1699 static ptrdiff_t
1700 number_of_chars (const char *s, int multibyte_p)
1702 ptrdiff_t nchars;
1704 if (multibyte_p)
1706 ptrdiff_t rest = strlen (s);
1707 int len;
1708 const unsigned char *p = (const unsigned char *) s;
1710 for (nchars = 0; rest > 0; ++nchars)
1712 string_char_and_length (p, &len);
1713 rest -= len, p += len;
1716 else
1717 nchars = strlen (s);
1719 return nchars;
1723 /* Compute byte position NEWPOS->bytepos corresponding to
1724 NEWPOS->charpos. POS is a known position in string STRING.
1725 NEWPOS->charpos must be >= POS.charpos. */
1727 static void
1728 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1730 eassert (STRINGP (string));
1731 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1733 if (STRING_MULTIBYTE (string))
1734 *newpos = string_pos_nchars_ahead (pos, string,
1735 CHARPOS (*newpos) - CHARPOS (pos));
1736 else
1737 BYTEPOS (*newpos) = CHARPOS (*newpos);
1740 /* EXPORT:
1741 Return an estimation of the pixel height of mode or header lines on
1742 frame F. FACE_ID specifies what line's height to estimate. */
1745 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1747 #ifdef HAVE_WINDOW_SYSTEM
1748 if (FRAME_WINDOW_P (f))
1750 int height = FONT_HEIGHT (FRAME_FONT (f));
1752 /* This function is called so early when Emacs starts that the face
1753 cache and mode line face are not yet initialized. */
1754 if (FRAME_FACE_CACHE (f))
1756 struct face *face = FACE_FROM_ID (f, face_id);
1757 if (face)
1759 if (face->font)
1760 height = FONT_HEIGHT (face->font);
1761 if (face->box_line_width > 0)
1762 height += 2 * face->box_line_width;
1766 return height;
1768 #endif
1770 return 1;
1773 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1774 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1775 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1776 not force the value into range. */
1778 void
1779 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1780 int *x, int *y, NativeRectangle *bounds, int noclip)
1783 #ifdef HAVE_WINDOW_SYSTEM
1784 if (FRAME_WINDOW_P (f))
1786 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1787 even for negative values. */
1788 if (pix_x < 0)
1789 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1790 if (pix_y < 0)
1791 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1793 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1794 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1796 if (bounds)
1797 STORE_NATIVE_RECT (*bounds,
1798 FRAME_COL_TO_PIXEL_X (f, pix_x),
1799 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1800 FRAME_COLUMN_WIDTH (f) - 1,
1801 FRAME_LINE_HEIGHT (f) - 1);
1803 if (!noclip)
1805 if (pix_x < 0)
1806 pix_x = 0;
1807 else if (pix_x > FRAME_TOTAL_COLS (f))
1808 pix_x = FRAME_TOTAL_COLS (f);
1810 if (pix_y < 0)
1811 pix_y = 0;
1812 else if (pix_y > FRAME_LINES (f))
1813 pix_y = FRAME_LINES (f);
1816 #endif
1818 *x = pix_x;
1819 *y = pix_y;
1823 /* Find the glyph under window-relative coordinates X/Y in window W.
1824 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1825 strings. Return in *HPOS and *VPOS the row and column number of
1826 the glyph found. Return in *AREA the glyph area containing X.
1827 Value is a pointer to the glyph found or null if X/Y is not on
1828 text, or we can't tell because W's current matrix is not up to
1829 date. */
1831 static
1832 struct glyph *
1833 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1834 int *dx, int *dy, int *area)
1836 struct glyph *glyph, *end;
1837 struct glyph_row *row = NULL;
1838 int x0, i;
1840 /* Find row containing Y. Give up if some row is not enabled. */
1841 for (i = 0; i < w->current_matrix->nrows; ++i)
1843 row = MATRIX_ROW (w->current_matrix, i);
1844 if (!row->enabled_p)
1845 return NULL;
1846 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1847 break;
1850 *vpos = i;
1851 *hpos = 0;
1853 /* Give up if Y is not in the window. */
1854 if (i == w->current_matrix->nrows)
1855 return NULL;
1857 /* Get the glyph area containing X. */
1858 if (w->pseudo_window_p)
1860 *area = TEXT_AREA;
1861 x0 = 0;
1863 else
1865 if (x < window_box_left_offset (w, TEXT_AREA))
1867 *area = LEFT_MARGIN_AREA;
1868 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1870 else if (x < window_box_right_offset (w, TEXT_AREA))
1872 *area = TEXT_AREA;
1873 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1875 else
1877 *area = RIGHT_MARGIN_AREA;
1878 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1882 /* Find glyph containing X. */
1883 glyph = row->glyphs[*area];
1884 end = glyph + row->used[*area];
1885 x -= x0;
1886 while (glyph < end && x >= glyph->pixel_width)
1888 x -= glyph->pixel_width;
1889 ++glyph;
1892 if (glyph == end)
1893 return NULL;
1895 if (dx)
1897 *dx = x;
1898 *dy = y - (row->y + row->ascent - glyph->ascent);
1901 *hpos = glyph - row->glyphs[*area];
1902 return glyph;
1905 /* Convert frame-relative x/y to coordinates relative to window W.
1906 Takes pseudo-windows into account. */
1908 static void
1909 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1911 if (w->pseudo_window_p)
1913 /* A pseudo-window is always full-width, and starts at the
1914 left edge of the frame, plus a frame border. */
1915 struct frame *f = XFRAME (w->frame);
1916 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1917 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 else
1921 *x -= WINDOW_LEFT_EDGE_X (w);
1922 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1926 #ifdef HAVE_WINDOW_SYSTEM
1928 /* EXPORT:
1929 Return in RECTS[] at most N clipping rectangles for glyph string S.
1930 Return the number of stored rectangles. */
1933 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1935 XRectangle r;
1937 if (n <= 0)
1938 return 0;
1940 if (s->row->full_width_p)
1942 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1943 r.x = WINDOW_LEFT_EDGE_X (s->w);
1944 r.width = WINDOW_TOTAL_WIDTH (s->w);
1946 /* Unless displaying a mode or menu bar line, which are always
1947 fully visible, clip to the visible part of the row. */
1948 if (s->w->pseudo_window_p)
1949 r.height = s->row->visible_height;
1950 else
1951 r.height = s->height;
1953 else
1955 /* This is a text line that may be partially visible. */
1956 r.x = window_box_left (s->w, s->area);
1957 r.width = window_box_width (s->w, s->area);
1958 r.height = s->row->visible_height;
1961 if (s->clip_head)
1962 if (r.x < s->clip_head->x)
1964 if (r.width >= s->clip_head->x - r.x)
1965 r.width -= s->clip_head->x - r.x;
1966 else
1967 r.width = 0;
1968 r.x = s->clip_head->x;
1970 if (s->clip_tail)
1971 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1973 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1974 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1975 else
1976 r.width = 0;
1979 /* If S draws overlapping rows, it's sufficient to use the top and
1980 bottom of the window for clipping because this glyph string
1981 intentionally draws over other lines. */
1982 if (s->for_overlaps)
1984 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1985 r.height = window_text_bottom_y (s->w) - r.y;
1987 /* Alas, the above simple strategy does not work for the
1988 environments with anti-aliased text: if the same text is
1989 drawn onto the same place multiple times, it gets thicker.
1990 If the overlap we are processing is for the erased cursor, we
1991 take the intersection with the rectangle of the cursor. */
1992 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1994 XRectangle rc, r_save = r;
1996 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1997 rc.y = s->w->phys_cursor.y;
1998 rc.width = s->w->phys_cursor_width;
1999 rc.height = s->w->phys_cursor_height;
2001 x_intersect_rectangles (&r_save, &rc, &r);
2004 else
2006 /* Don't use S->y for clipping because it doesn't take partially
2007 visible lines into account. For example, it can be negative for
2008 partially visible lines at the top of a window. */
2009 if (!s->row->full_width_p
2010 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2011 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2012 else
2013 r.y = max (0, s->row->y);
2016 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2018 /* If drawing the cursor, don't let glyph draw outside its
2019 advertised boundaries. Cleartype does this under some circumstances. */
2020 if (s->hl == DRAW_CURSOR)
2022 struct glyph *glyph = s->first_glyph;
2023 int height, max_y;
2025 if (s->x > r.x)
2027 r.width -= s->x - r.x;
2028 r.x = s->x;
2030 r.width = min (r.width, glyph->pixel_width);
2032 /* If r.y is below window bottom, ensure that we still see a cursor. */
2033 height = min (glyph->ascent + glyph->descent,
2034 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2035 max_y = window_text_bottom_y (s->w) - height;
2036 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2037 if (s->ybase - glyph->ascent > max_y)
2039 r.y = max_y;
2040 r.height = height;
2042 else
2044 /* Don't draw cursor glyph taller than our actual glyph. */
2045 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2046 if (height < r.height)
2048 max_y = r.y + r.height;
2049 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2050 r.height = min (max_y - r.y, height);
2055 if (s->row->clip)
2057 XRectangle r_save = r;
2059 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2060 r.width = 0;
2063 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2064 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2066 #ifdef CONVERT_FROM_XRECT
2067 CONVERT_FROM_XRECT (r, *rects);
2068 #else
2069 *rects = r;
2070 #endif
2071 return 1;
2073 else
2075 /* If we are processing overlapping and allowed to return
2076 multiple clipping rectangles, we exclude the row of the glyph
2077 string from the clipping rectangle. This is to avoid drawing
2078 the same text on the environment with anti-aliasing. */
2079 #ifdef CONVERT_FROM_XRECT
2080 XRectangle rs[2];
2081 #else
2082 XRectangle *rs = rects;
2083 #endif
2084 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2086 if (s->for_overlaps & OVERLAPS_PRED)
2088 rs[i] = r;
2089 if (r.y + r.height > row_y)
2091 if (r.y < row_y)
2092 rs[i].height = row_y - r.y;
2093 else
2094 rs[i].height = 0;
2096 i++;
2098 if (s->for_overlaps & OVERLAPS_SUCC)
2100 rs[i] = r;
2101 if (r.y < row_y + s->row->visible_height)
2103 if (r.y + r.height > row_y + s->row->visible_height)
2105 rs[i].y = row_y + s->row->visible_height;
2106 rs[i].height = r.y + r.height - rs[i].y;
2108 else
2109 rs[i].height = 0;
2111 i++;
2114 n = i;
2115 #ifdef CONVERT_FROM_XRECT
2116 for (i = 0; i < n; i++)
2117 CONVERT_FROM_XRECT (rs[i], rects[i]);
2118 #endif
2119 return n;
2123 /* EXPORT:
2124 Return in *NR the clipping rectangle for glyph string S. */
2126 void
2127 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2129 get_glyph_string_clip_rects (s, nr, 1);
2133 /* EXPORT:
2134 Return the position and height of the phys cursor in window W.
2135 Set w->phys_cursor_width to width of phys cursor.
2138 void
2139 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2140 struct glyph *glyph, int *xp, int *yp, int *heightp)
2142 struct frame *f = XFRAME (WINDOW_FRAME (w));
2143 int x, y, wd, h, h0, y0;
2145 /* Compute the width of the rectangle to draw. If on a stretch
2146 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2147 rectangle as wide as the glyph, but use a canonical character
2148 width instead. */
2149 wd = glyph->pixel_width - 1;
2150 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2151 wd++; /* Why? */
2152 #endif
2154 x = w->phys_cursor.x;
2155 if (x < 0)
2157 wd += x;
2158 x = 0;
2161 if (glyph->type == STRETCH_GLYPH
2162 && !x_stretch_cursor_p)
2163 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2164 w->phys_cursor_width = wd;
2166 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2168 /* If y is below window bottom, ensure that we still see a cursor. */
2169 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2171 h = max (h0, glyph->ascent + glyph->descent);
2172 h0 = min (h0, glyph->ascent + glyph->descent);
2174 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2175 if (y < y0)
2177 h = max (h - (y0 - y) + 1, h0);
2178 y = y0 - 1;
2180 else
2182 y0 = window_text_bottom_y (w) - h0;
2183 if (y > y0)
2185 h += y - y0;
2186 y = y0;
2190 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2191 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2192 *heightp = h;
2196 * Remember which glyph the mouse is over.
2199 void
2200 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2202 Lisp_Object window;
2203 struct window *w;
2204 struct glyph_row *r, *gr, *end_row;
2205 enum window_part part;
2206 enum glyph_row_area area;
2207 int x, y, width, height;
2209 /* Try to determine frame pixel position and size of the glyph under
2210 frame pixel coordinates X/Y on frame F. */
2212 if (!f->glyphs_initialized_p
2213 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2214 NILP (window)))
2216 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2217 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2218 goto virtual_glyph;
2221 w = XWINDOW (window);
2222 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2223 height = WINDOW_FRAME_LINE_HEIGHT (w);
2225 x = window_relative_x_coord (w, part, gx);
2226 y = gy - WINDOW_TOP_EDGE_Y (w);
2228 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2229 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2231 if (w->pseudo_window_p)
2233 area = TEXT_AREA;
2234 part = ON_MODE_LINE; /* Don't adjust margin. */
2235 goto text_glyph;
2238 switch (part)
2240 case ON_LEFT_MARGIN:
2241 area = LEFT_MARGIN_AREA;
2242 goto text_glyph;
2244 case ON_RIGHT_MARGIN:
2245 area = RIGHT_MARGIN_AREA;
2246 goto text_glyph;
2248 case ON_HEADER_LINE:
2249 case ON_MODE_LINE:
2250 gr = (part == ON_HEADER_LINE
2251 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2252 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2253 gy = gr->y;
2254 area = TEXT_AREA;
2255 goto text_glyph_row_found;
2257 case ON_TEXT:
2258 area = TEXT_AREA;
2260 text_glyph:
2261 gr = 0; gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2265 gr = r; gy = r->y;
2266 break;
2269 text_glyph_row_found:
2270 if (gr && gy <= y)
2272 struct glyph *g = gr->glyphs[area];
2273 struct glyph *end = g + gr->used[area];
2275 height = gr->height;
2276 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2277 if (gx + g->pixel_width > x)
2278 break;
2280 if (g < end)
2282 if (g->type == IMAGE_GLYPH)
2284 /* Don't remember when mouse is over image, as
2285 image may have hot-spots. */
2286 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2287 return;
2289 width = g->pixel_width;
2291 else
2293 /* Use nominal char spacing at end of line. */
2294 x -= gx;
2295 gx += (x / width) * width;
2298 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2299 gx += window_box_left_offset (w, area);
2301 else
2303 /* Use nominal line height at end of window. */
2304 gx = (x / width) * width;
2305 y -= gy;
2306 gy += (y / height) * height;
2308 break;
2310 case ON_LEFT_FRINGE:
2311 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2312 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2313 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2314 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2315 goto row_glyph;
2317 case ON_RIGHT_FRINGE:
2318 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2319 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2320 : window_box_right_offset (w, TEXT_AREA));
2321 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2322 goto row_glyph;
2324 case ON_SCROLL_BAR:
2325 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2327 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2328 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2329 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2330 : 0)));
2331 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2333 row_glyph:
2334 gr = 0, gy = 0;
2335 for (; r <= end_row && r->enabled_p; ++r)
2336 if (r->y + r->height > y)
2338 gr = r; gy = r->y;
2339 break;
2342 if (gr && gy <= y)
2343 height = gr->height;
2344 else
2346 /* Use nominal line height at end of window. */
2347 y -= gy;
2348 gy += (y / height) * height;
2350 break;
2352 default:
2354 virtual_glyph:
2355 /* If there is no glyph under the mouse, then we divide the screen
2356 into a grid of the smallest glyph in the frame, and use that
2357 as our "glyph". */
2359 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2360 round down even for negative values. */
2361 if (gx < 0)
2362 gx -= width - 1;
2363 if (gy < 0)
2364 gy -= height - 1;
2366 gx = (gx / width) * width;
2367 gy = (gy / height) * height;
2369 goto store_rect;
2372 gx += WINDOW_LEFT_EDGE_X (w);
2373 gy += WINDOW_TOP_EDGE_Y (w);
2375 store_rect:
2376 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2378 /* Visible feedback for debugging. */
2379 #if 0
2380 #if HAVE_X_WINDOWS
2381 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2382 f->output_data.x->normal_gc,
2383 gx, gy, width, height);
2384 #endif
2385 #endif
2389 #endif /* HAVE_WINDOW_SYSTEM */
2392 /***********************************************************************
2393 Lisp form evaluation
2394 ***********************************************************************/
2396 /* Error handler for safe_eval and safe_call. */
2398 static Lisp_Object
2399 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2401 add_to_log ("Error during redisplay: %S signalled %S",
2402 Flist (nargs, args), arg);
2403 return Qnil;
2406 /* Call function FUNC with the rest of NARGS - 1 arguments
2407 following. Return the result, or nil if something went
2408 wrong. Prevent redisplay during the evaluation. */
2410 Lisp_Object
2411 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2413 Lisp_Object val;
2415 if (inhibit_eval_during_redisplay)
2416 val = Qnil;
2417 else
2419 va_list ap;
2420 ptrdiff_t i;
2421 ptrdiff_t count = SPECPDL_INDEX ();
2422 struct gcpro gcpro1;
2423 Lisp_Object *args = alloca (nargs * word_size);
2425 args[0] = func;
2426 va_start (ap, func);
2427 for (i = 1; i < nargs; i++)
2428 args[i] = va_arg (ap, Lisp_Object);
2429 va_end (ap);
2431 GCPRO1 (args[0]);
2432 gcpro1.nvars = nargs;
2433 specbind (Qinhibit_redisplay, Qt);
2434 /* Use Qt to ensure debugger does not run,
2435 so there is no possibility of wanting to redisplay. */
2436 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2437 safe_eval_handler);
2438 UNGCPRO;
2439 val = unbind_to (count, val);
2442 return val;
2446 /* Call function FN with one argument ARG.
2447 Return the result, or nil if something went wrong. */
2449 Lisp_Object
2450 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2452 return safe_call (2, fn, arg);
2455 static Lisp_Object Qeval;
2457 Lisp_Object
2458 safe_eval (Lisp_Object sexpr)
2460 return safe_call1 (Qeval, sexpr);
2463 /* Call function FN with two arguments ARG1 and ARG2.
2464 Return the result, or nil if something went wrong. */
2466 Lisp_Object
2467 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2469 return safe_call (3, fn, arg1, arg2);
2474 /***********************************************************************
2475 Debugging
2476 ***********************************************************************/
2478 #if 0
2480 /* Define CHECK_IT to perform sanity checks on iterators.
2481 This is for debugging. It is too slow to do unconditionally. */
2483 static void
2484 check_it (struct it *it)
2486 if (it->method == GET_FROM_STRING)
2488 eassert (STRINGP (it->string));
2489 eassert (IT_STRING_CHARPOS (*it) >= 0);
2491 else
2493 eassert (IT_STRING_CHARPOS (*it) < 0);
2494 if (it->method == GET_FROM_BUFFER)
2496 /* Check that character and byte positions agree. */
2497 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2501 if (it->dpvec)
2502 eassert (it->current.dpvec_index >= 0);
2503 else
2504 eassert (it->current.dpvec_index < 0);
2507 #define CHECK_IT(IT) check_it ((IT))
2509 #else /* not 0 */
2511 #define CHECK_IT(IT) (void) 0
2513 #endif /* not 0 */
2516 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2518 /* Check that the window end of window W is what we expect it
2519 to be---the last row in the current matrix displaying text. */
2521 static void
2522 check_window_end (struct window *w)
2524 if (!MINI_WINDOW_P (w)
2525 && !NILP (w->window_end_valid))
2527 struct glyph_row *row;
2528 eassert ((row = MATRIX_ROW (w->current_matrix,
2529 XFASTINT (w->window_end_vpos)),
2530 !row->enabled_p
2531 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2532 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2536 #define CHECK_WINDOW_END(W) check_window_end ((W))
2538 #else
2540 #define CHECK_WINDOW_END(W) (void) 0
2542 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2546 /***********************************************************************
2547 Iterator initialization
2548 ***********************************************************************/
2550 /* Initialize IT for displaying current_buffer in window W, starting
2551 at character position CHARPOS. CHARPOS < 0 means that no buffer
2552 position is specified which is useful when the iterator is assigned
2553 a position later. BYTEPOS is the byte position corresponding to
2554 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2556 If ROW is not null, calls to produce_glyphs with IT as parameter
2557 will produce glyphs in that row.
2559 BASE_FACE_ID is the id of a base face to use. It must be one of
2560 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2561 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2562 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2564 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2565 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2566 will be initialized to use the corresponding mode line glyph row of
2567 the desired matrix of W. */
2569 void
2570 init_iterator (struct it *it, struct window *w,
2571 ptrdiff_t charpos, ptrdiff_t bytepos,
2572 struct glyph_row *row, enum face_id base_face_id)
2574 int highlight_region_p;
2575 enum face_id remapped_base_face_id = base_face_id;
2577 /* Some precondition checks. */
2578 eassert (w != NULL && it != NULL);
2579 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2580 && charpos <= ZV));
2582 /* If face attributes have been changed since the last redisplay,
2583 free realized faces now because they depend on face definitions
2584 that might have changed. Don't free faces while there might be
2585 desired matrices pending which reference these faces. */
2586 if (face_change_count && !inhibit_free_realized_faces)
2588 face_change_count = 0;
2589 free_all_realized_faces (Qnil);
2592 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2593 if (! NILP (Vface_remapping_alist))
2594 remapped_base_face_id
2595 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2597 /* Use one of the mode line rows of W's desired matrix if
2598 appropriate. */
2599 if (row == NULL)
2601 if (base_face_id == MODE_LINE_FACE_ID
2602 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2603 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2604 else if (base_face_id == HEADER_LINE_FACE_ID)
2605 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2608 /* Clear IT. */
2609 memset (it, 0, sizeof *it);
2610 it->current.overlay_string_index = -1;
2611 it->current.dpvec_index = -1;
2612 it->base_face_id = remapped_base_face_id;
2613 it->string = Qnil;
2614 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2615 it->paragraph_embedding = L2R;
2616 it->bidi_it.string.lstring = Qnil;
2617 it->bidi_it.string.s = NULL;
2618 it->bidi_it.string.bufpos = 0;
2620 /* The window in which we iterate over current_buffer: */
2621 XSETWINDOW (it->window, w);
2622 it->w = w;
2623 it->f = XFRAME (w->frame);
2625 it->cmp_it.id = -1;
2627 /* Extra space between lines (on window systems only). */
2628 if (base_face_id == DEFAULT_FACE_ID
2629 && FRAME_WINDOW_P (it->f))
2631 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2632 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2633 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2634 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2635 * FRAME_LINE_HEIGHT (it->f));
2636 else if (it->f->extra_line_spacing > 0)
2637 it->extra_line_spacing = it->f->extra_line_spacing;
2638 it->max_extra_line_spacing = 0;
2641 /* If realized faces have been removed, e.g. because of face
2642 attribute changes of named faces, recompute them. When running
2643 in batch mode, the face cache of the initial frame is null. If
2644 we happen to get called, make a dummy face cache. */
2645 if (FRAME_FACE_CACHE (it->f) == NULL)
2646 init_frame_faces (it->f);
2647 if (FRAME_FACE_CACHE (it->f)->used == 0)
2648 recompute_basic_faces (it->f);
2650 /* Current value of the `slice', `space-width', and 'height' properties. */
2651 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2652 it->space_width = Qnil;
2653 it->font_height = Qnil;
2654 it->override_ascent = -1;
2656 /* Are control characters displayed as `^C'? */
2657 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2659 /* -1 means everything between a CR and the following line end
2660 is invisible. >0 means lines indented more than this value are
2661 invisible. */
2662 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2663 ? (clip_to_bounds
2664 (-1, XINT (BVAR (current_buffer, selective_display)),
2665 PTRDIFF_MAX))
2666 : (!NILP (BVAR (current_buffer, selective_display))
2667 ? -1 : 0));
2668 it->selective_display_ellipsis_p
2669 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2671 /* Display table to use. */
2672 it->dp = window_display_table (w);
2674 /* Are multibyte characters enabled in current_buffer? */
2675 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2677 /* Non-zero if we should highlight the region. */
2678 highlight_region_p
2679 = (!NILP (Vtransient_mark_mode)
2680 && !NILP (BVAR (current_buffer, mark_active))
2681 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2683 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2684 start and end of a visible region in window IT->w. Set both to
2685 -1 to indicate no region. */
2686 if (highlight_region_p
2687 /* Maybe highlight only in selected window. */
2688 && (/* Either show region everywhere. */
2689 highlight_nonselected_windows
2690 /* Or show region in the selected window. */
2691 || w == XWINDOW (selected_window)
2692 /* Or show the region if we are in the mini-buffer and W is
2693 the window the mini-buffer refers to. */
2694 || (MINI_WINDOW_P (XWINDOW (selected_window))
2695 && WINDOWP (minibuf_selected_window)
2696 && w == XWINDOW (minibuf_selected_window))))
2698 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2699 it->region_beg_charpos = min (PT, markpos);
2700 it->region_end_charpos = max (PT, markpos);
2702 else
2703 it->region_beg_charpos = it->region_end_charpos = -1;
2705 /* Get the position at which the redisplay_end_trigger hook should
2706 be run, if it is to be run at all. */
2707 if (MARKERP (w->redisplay_end_trigger)
2708 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2709 it->redisplay_end_trigger_charpos
2710 = marker_position (w->redisplay_end_trigger);
2711 else if (INTEGERP (w->redisplay_end_trigger))
2712 it->redisplay_end_trigger_charpos =
2713 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2715 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2717 /* Are lines in the display truncated? */
2718 if (base_face_id != DEFAULT_FACE_ID
2719 || it->w->hscroll
2720 || (! WINDOW_FULL_WIDTH_P (it->w)
2721 && ((!NILP (Vtruncate_partial_width_windows)
2722 && !INTEGERP (Vtruncate_partial_width_windows))
2723 || (INTEGERP (Vtruncate_partial_width_windows)
2724 && (WINDOW_TOTAL_COLS (it->w)
2725 < XINT (Vtruncate_partial_width_windows))))))
2726 it->line_wrap = TRUNCATE;
2727 else if (NILP (BVAR (current_buffer, truncate_lines)))
2728 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2729 ? WINDOW_WRAP : WORD_WRAP;
2730 else
2731 it->line_wrap = TRUNCATE;
2733 /* Get dimensions of truncation and continuation glyphs. These are
2734 displayed as fringe bitmaps under X, but we need them for such
2735 frames when the fringes are turned off. But leave the dimensions
2736 zero for tooltip frames, as these glyphs look ugly there and also
2737 sabotage calculations of tooltip dimensions in x-show-tip. */
2738 #ifdef HAVE_WINDOW_SYSTEM
2739 if (!(FRAME_WINDOW_P (it->f)
2740 && FRAMEP (tip_frame)
2741 && it->f == XFRAME (tip_frame)))
2742 #endif
2744 if (it->line_wrap == TRUNCATE)
2746 /* We will need the truncation glyph. */
2747 eassert (it->glyph_row == NULL);
2748 produce_special_glyphs (it, IT_TRUNCATION);
2749 it->truncation_pixel_width = it->pixel_width;
2751 else
2753 /* We will need the continuation glyph. */
2754 eassert (it->glyph_row == NULL);
2755 produce_special_glyphs (it, IT_CONTINUATION);
2756 it->continuation_pixel_width = it->pixel_width;
2760 /* Reset these values to zero because the produce_special_glyphs
2761 above has changed them. */
2762 it->pixel_width = it->ascent = it->descent = 0;
2763 it->phys_ascent = it->phys_descent = 0;
2765 /* Set this after getting the dimensions of truncation and
2766 continuation glyphs, so that we don't produce glyphs when calling
2767 produce_special_glyphs, above. */
2768 it->glyph_row = row;
2769 it->area = TEXT_AREA;
2771 /* Forget any previous info about this row being reversed. */
2772 if (it->glyph_row)
2773 it->glyph_row->reversed_p = 0;
2775 /* Get the dimensions of the display area. The display area
2776 consists of the visible window area plus a horizontally scrolled
2777 part to the left of the window. All x-values are relative to the
2778 start of this total display area. */
2779 if (base_face_id != DEFAULT_FACE_ID)
2781 /* Mode lines, menu bar in terminal frames. */
2782 it->first_visible_x = 0;
2783 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2785 else
2787 it->first_visible_x =
2788 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2789 it->last_visible_x = (it->first_visible_x
2790 + window_box_width (w, TEXT_AREA));
2792 /* If we truncate lines, leave room for the truncation glyph(s) at
2793 the right margin. Otherwise, leave room for the continuation
2794 glyph(s). Done only if the window has no fringes. Since we
2795 don't know at this point whether there will be any R2L lines in
2796 the window, we reserve space for truncation/continuation glyphs
2797 even if only one of the fringes is absent. */
2798 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2799 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2801 if (it->line_wrap == TRUNCATE)
2802 it->last_visible_x -= it->truncation_pixel_width;
2803 else
2804 it->last_visible_x -= it->continuation_pixel_width;
2807 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2808 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2811 /* Leave room for a border glyph. */
2812 if (!FRAME_WINDOW_P (it->f)
2813 && !WINDOW_RIGHTMOST_P (it->w))
2814 it->last_visible_x -= 1;
2816 it->last_visible_y = window_text_bottom_y (w);
2818 /* For mode lines and alike, arrange for the first glyph having a
2819 left box line if the face specifies a box. */
2820 if (base_face_id != DEFAULT_FACE_ID)
2822 struct face *face;
2824 it->face_id = remapped_base_face_id;
2826 /* If we have a boxed mode line, make the first character appear
2827 with a left box line. */
2828 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2829 if (face->box != FACE_NO_BOX)
2830 it->start_of_box_run_p = 1;
2833 /* If a buffer position was specified, set the iterator there,
2834 getting overlays and face properties from that position. */
2835 if (charpos >= BUF_BEG (current_buffer))
2837 it->end_charpos = ZV;
2838 IT_CHARPOS (*it) = charpos;
2840 /* We will rely on `reseat' to set this up properly, via
2841 handle_face_prop. */
2842 it->face_id = it->base_face_id;
2844 /* Compute byte position if not specified. */
2845 if (bytepos < charpos)
2846 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2847 else
2848 IT_BYTEPOS (*it) = bytepos;
2850 it->start = it->current;
2851 /* Do we need to reorder bidirectional text? Not if this is a
2852 unibyte buffer: by definition, none of the single-byte
2853 characters are strong R2L, so no reordering is needed. And
2854 bidi.c doesn't support unibyte buffers anyway. Also, don't
2855 reorder while we are loading loadup.el, since the tables of
2856 character properties needed for reordering are not yet
2857 available. */
2858 it->bidi_p =
2859 NILP (Vpurify_flag)
2860 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2861 && it->multibyte_p;
2863 /* If we are to reorder bidirectional text, init the bidi
2864 iterator. */
2865 if (it->bidi_p)
2867 /* Note the paragraph direction that this buffer wants to
2868 use. */
2869 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2870 Qleft_to_right))
2871 it->paragraph_embedding = L2R;
2872 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2873 Qright_to_left))
2874 it->paragraph_embedding = R2L;
2875 else
2876 it->paragraph_embedding = NEUTRAL_DIR;
2877 bidi_unshelve_cache (NULL, 0);
2878 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2879 &it->bidi_it);
2882 /* Compute faces etc. */
2883 reseat (it, it->current.pos, 1);
2886 CHECK_IT (it);
2890 /* Initialize IT for the display of window W with window start POS. */
2892 void
2893 start_display (struct it *it, struct window *w, struct text_pos pos)
2895 struct glyph_row *row;
2896 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2898 row = w->desired_matrix->rows + first_vpos;
2899 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2900 it->first_vpos = first_vpos;
2902 /* Don't reseat to previous visible line start if current start
2903 position is in a string or image. */
2904 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2906 int start_at_line_beg_p;
2907 int first_y = it->current_y;
2909 /* If window start is not at a line start, skip forward to POS to
2910 get the correct continuation lines width. */
2911 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2912 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2913 if (!start_at_line_beg_p)
2915 int new_x;
2917 reseat_at_previous_visible_line_start (it);
2918 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2920 new_x = it->current_x + it->pixel_width;
2922 /* If lines are continued, this line may end in the middle
2923 of a multi-glyph character (e.g. a control character
2924 displayed as \003, or in the middle of an overlay
2925 string). In this case move_it_to above will not have
2926 taken us to the start of the continuation line but to the
2927 end of the continued line. */
2928 if (it->current_x > 0
2929 && it->line_wrap != TRUNCATE /* Lines are continued. */
2930 && (/* And glyph doesn't fit on the line. */
2931 new_x > it->last_visible_x
2932 /* Or it fits exactly and we're on a window
2933 system frame. */
2934 || (new_x == it->last_visible_x
2935 && FRAME_WINDOW_P (it->f)
2936 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2937 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2938 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2940 if ((it->current.dpvec_index >= 0
2941 || it->current.overlay_string_index >= 0)
2942 /* If we are on a newline from a display vector or
2943 overlay string, then we are already at the end of
2944 a screen line; no need to go to the next line in
2945 that case, as this line is not really continued.
2946 (If we do go to the next line, C-e will not DTRT.) */
2947 && it->c != '\n')
2949 set_iterator_to_next (it, 1);
2950 move_it_in_display_line_to (it, -1, -1, 0);
2953 it->continuation_lines_width += it->current_x;
2955 /* If the character at POS is displayed via a display
2956 vector, move_it_to above stops at the final glyph of
2957 IT->dpvec. To make the caller redisplay that character
2958 again (a.k.a. start at POS), we need to reset the
2959 dpvec_index to the beginning of IT->dpvec. */
2960 else if (it->current.dpvec_index >= 0)
2961 it->current.dpvec_index = 0;
2963 /* We're starting a new display line, not affected by the
2964 height of the continued line, so clear the appropriate
2965 fields in the iterator structure. */
2966 it->max_ascent = it->max_descent = 0;
2967 it->max_phys_ascent = it->max_phys_descent = 0;
2969 it->current_y = first_y;
2970 it->vpos = 0;
2971 it->current_x = it->hpos = 0;
2977 /* Return 1 if POS is a position in ellipses displayed for invisible
2978 text. W is the window we display, for text property lookup. */
2980 static int
2981 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2983 Lisp_Object prop, window;
2984 int ellipses_p = 0;
2985 ptrdiff_t charpos = CHARPOS (pos->pos);
2987 /* If POS specifies a position in a display vector, this might
2988 be for an ellipsis displayed for invisible text. We won't
2989 get the iterator set up for delivering that ellipsis unless
2990 we make sure that it gets aware of the invisible text. */
2991 if (pos->dpvec_index >= 0
2992 && pos->overlay_string_index < 0
2993 && CHARPOS (pos->string_pos) < 0
2994 && charpos > BEGV
2995 && (XSETWINDOW (window, w),
2996 prop = Fget_char_property (make_number (charpos),
2997 Qinvisible, window),
2998 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3000 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3001 window);
3002 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3005 return ellipses_p;
3009 /* Initialize IT for stepping through current_buffer in window W,
3010 starting at position POS that includes overlay string and display
3011 vector/ control character translation position information. Value
3012 is zero if there are overlay strings with newlines at POS. */
3014 static int
3015 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3017 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3018 int i, overlay_strings_with_newlines = 0;
3020 /* If POS specifies a position in a display vector, this might
3021 be for an ellipsis displayed for invisible text. We won't
3022 get the iterator set up for delivering that ellipsis unless
3023 we make sure that it gets aware of the invisible text. */
3024 if (in_ellipses_for_invisible_text_p (pos, w))
3026 --charpos;
3027 bytepos = 0;
3030 /* Keep in mind: the call to reseat in init_iterator skips invisible
3031 text, so we might end up at a position different from POS. This
3032 is only a problem when POS is a row start after a newline and an
3033 overlay starts there with an after-string, and the overlay has an
3034 invisible property. Since we don't skip invisible text in
3035 display_line and elsewhere immediately after consuming the
3036 newline before the row start, such a POS will not be in a string,
3037 but the call to init_iterator below will move us to the
3038 after-string. */
3039 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3041 /* This only scans the current chunk -- it should scan all chunks.
3042 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3043 to 16 in 22.1 to make this a lesser problem. */
3044 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3046 const char *s = SSDATA (it->overlay_strings[i]);
3047 const char *e = s + SBYTES (it->overlay_strings[i]);
3049 while (s < e && *s != '\n')
3050 ++s;
3052 if (s < e)
3054 overlay_strings_with_newlines = 1;
3055 break;
3059 /* If position is within an overlay string, set up IT to the right
3060 overlay string. */
3061 if (pos->overlay_string_index >= 0)
3063 int relative_index;
3065 /* If the first overlay string happens to have a `display'
3066 property for an image, the iterator will be set up for that
3067 image, and we have to undo that setup first before we can
3068 correct the overlay string index. */
3069 if (it->method == GET_FROM_IMAGE)
3070 pop_it (it);
3072 /* We already have the first chunk of overlay strings in
3073 IT->overlay_strings. Load more until the one for
3074 pos->overlay_string_index is in IT->overlay_strings. */
3075 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3077 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3078 it->current.overlay_string_index = 0;
3079 while (n--)
3081 load_overlay_strings (it, 0);
3082 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3086 it->current.overlay_string_index = pos->overlay_string_index;
3087 relative_index = (it->current.overlay_string_index
3088 % OVERLAY_STRING_CHUNK_SIZE);
3089 it->string = it->overlay_strings[relative_index];
3090 eassert (STRINGP (it->string));
3091 it->current.string_pos = pos->string_pos;
3092 it->method = GET_FROM_STRING;
3095 if (CHARPOS (pos->string_pos) >= 0)
3097 /* Recorded position is not in an overlay string, but in another
3098 string. This can only be a string from a `display' property.
3099 IT should already be filled with that string. */
3100 it->current.string_pos = pos->string_pos;
3101 eassert (STRINGP (it->string));
3104 /* Restore position in display vector translations, control
3105 character translations or ellipses. */
3106 if (pos->dpvec_index >= 0)
3108 if (it->dpvec == NULL)
3109 get_next_display_element (it);
3110 eassert (it->dpvec && it->current.dpvec_index == 0);
3111 it->current.dpvec_index = pos->dpvec_index;
3114 CHECK_IT (it);
3115 return !overlay_strings_with_newlines;
3119 /* Initialize IT for stepping through current_buffer in window W
3120 starting at ROW->start. */
3122 static void
3123 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3125 init_from_display_pos (it, w, &row->start);
3126 it->start = row->start;
3127 it->continuation_lines_width = row->continuation_lines_width;
3128 CHECK_IT (it);
3132 /* Initialize IT for stepping through current_buffer in window W
3133 starting in the line following ROW, i.e. starting at ROW->end.
3134 Value is zero if there are overlay strings with newlines at ROW's
3135 end position. */
3137 static int
3138 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3140 int success = 0;
3142 if (init_from_display_pos (it, w, &row->end))
3144 if (row->continued_p)
3145 it->continuation_lines_width
3146 = row->continuation_lines_width + row->pixel_width;
3147 CHECK_IT (it);
3148 success = 1;
3151 return success;
3157 /***********************************************************************
3158 Text properties
3159 ***********************************************************************/
3161 /* Called when IT reaches IT->stop_charpos. Handle text property and
3162 overlay changes. Set IT->stop_charpos to the next position where
3163 to stop. */
3165 static void
3166 handle_stop (struct it *it)
3168 enum prop_handled handled;
3169 int handle_overlay_change_p;
3170 struct props *p;
3172 it->dpvec = NULL;
3173 it->current.dpvec_index = -1;
3174 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3175 it->ignore_overlay_strings_at_pos_p = 0;
3176 it->ellipsis_p = 0;
3178 /* Use face of preceding text for ellipsis (if invisible) */
3179 if (it->selective_display_ellipsis_p)
3180 it->saved_face_id = it->face_id;
3184 handled = HANDLED_NORMALLY;
3186 /* Call text property handlers. */
3187 for (p = it_props; p->handler; ++p)
3189 handled = p->handler (it);
3191 if (handled == HANDLED_RECOMPUTE_PROPS)
3192 break;
3193 else if (handled == HANDLED_RETURN)
3195 /* We still want to show before and after strings from
3196 overlays even if the actual buffer text is replaced. */
3197 if (!handle_overlay_change_p
3198 || it->sp > 1
3199 /* Don't call get_overlay_strings_1 if we already
3200 have overlay strings loaded, because doing so
3201 will load them again and push the iterator state
3202 onto the stack one more time, which is not
3203 expected by the rest of the code that processes
3204 overlay strings. */
3205 || (it->current.overlay_string_index < 0
3206 ? !get_overlay_strings_1 (it, 0, 0)
3207 : 0))
3209 if (it->ellipsis_p)
3210 setup_for_ellipsis (it, 0);
3211 /* When handling a display spec, we might load an
3212 empty string. In that case, discard it here. We
3213 used to discard it in handle_single_display_spec,
3214 but that causes get_overlay_strings_1, above, to
3215 ignore overlay strings that we must check. */
3216 if (STRINGP (it->string) && !SCHARS (it->string))
3217 pop_it (it);
3218 return;
3220 else if (STRINGP (it->string) && !SCHARS (it->string))
3221 pop_it (it);
3222 else
3224 it->ignore_overlay_strings_at_pos_p = 1;
3225 it->string_from_display_prop_p = 0;
3226 it->from_disp_prop_p = 0;
3227 handle_overlay_change_p = 0;
3229 handled = HANDLED_RECOMPUTE_PROPS;
3230 break;
3232 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3233 handle_overlay_change_p = 0;
3236 if (handled != HANDLED_RECOMPUTE_PROPS)
3238 /* Don't check for overlay strings below when set to deliver
3239 characters from a display vector. */
3240 if (it->method == GET_FROM_DISPLAY_VECTOR)
3241 handle_overlay_change_p = 0;
3243 /* Handle overlay changes.
3244 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3245 if it finds overlays. */
3246 if (handle_overlay_change_p)
3247 handled = handle_overlay_change (it);
3250 if (it->ellipsis_p)
3252 setup_for_ellipsis (it, 0);
3253 break;
3256 while (handled == HANDLED_RECOMPUTE_PROPS);
3258 /* Determine where to stop next. */
3259 if (handled == HANDLED_NORMALLY)
3260 compute_stop_pos (it);
3264 /* Compute IT->stop_charpos from text property and overlay change
3265 information for IT's current position. */
3267 static void
3268 compute_stop_pos (struct it *it)
3270 register INTERVAL iv, next_iv;
3271 Lisp_Object object, limit, position;
3272 ptrdiff_t charpos, bytepos;
3274 if (STRINGP (it->string))
3276 /* Strings are usually short, so don't limit the search for
3277 properties. */
3278 it->stop_charpos = it->end_charpos;
3279 object = it->string;
3280 limit = Qnil;
3281 charpos = IT_STRING_CHARPOS (*it);
3282 bytepos = IT_STRING_BYTEPOS (*it);
3284 else
3286 ptrdiff_t pos;
3288 /* If end_charpos is out of range for some reason, such as a
3289 misbehaving display function, rationalize it (Bug#5984). */
3290 if (it->end_charpos > ZV)
3291 it->end_charpos = ZV;
3292 it->stop_charpos = it->end_charpos;
3294 /* If next overlay change is in front of the current stop pos
3295 (which is IT->end_charpos), stop there. Note: value of
3296 next_overlay_change is point-max if no overlay change
3297 follows. */
3298 charpos = IT_CHARPOS (*it);
3299 bytepos = IT_BYTEPOS (*it);
3300 pos = next_overlay_change (charpos);
3301 if (pos < it->stop_charpos)
3302 it->stop_charpos = pos;
3304 /* If showing the region, we have to stop at the region
3305 start or end because the face might change there. */
3306 if (it->region_beg_charpos > 0)
3308 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3309 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3310 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3311 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3314 /* Set up variables for computing the stop position from text
3315 property changes. */
3316 XSETBUFFER (object, current_buffer);
3317 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3320 /* Get the interval containing IT's position. Value is a null
3321 interval if there isn't such an interval. */
3322 position = make_number (charpos);
3323 iv = validate_interval_range (object, &position, &position, 0);
3324 if (iv)
3326 Lisp_Object values_here[LAST_PROP_IDX];
3327 struct props *p;
3329 /* Get properties here. */
3330 for (p = it_props; p->handler; ++p)
3331 values_here[p->idx] = textget (iv->plist, *p->name);
3333 /* Look for an interval following iv that has different
3334 properties. */
3335 for (next_iv = next_interval (iv);
3336 (next_iv
3337 && (NILP (limit)
3338 || XFASTINT (limit) > next_iv->position));
3339 next_iv = next_interval (next_iv))
3341 for (p = it_props; p->handler; ++p)
3343 Lisp_Object new_value;
3345 new_value = textget (next_iv->plist, *p->name);
3346 if (!EQ (values_here[p->idx], new_value))
3347 break;
3350 if (p->handler)
3351 break;
3354 if (next_iv)
3356 if (INTEGERP (limit)
3357 && next_iv->position >= XFASTINT (limit))
3358 /* No text property change up to limit. */
3359 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3360 else
3361 /* Text properties change in next_iv. */
3362 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3366 if (it->cmp_it.id < 0)
3368 ptrdiff_t stoppos = it->end_charpos;
3370 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3371 stoppos = -1;
3372 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3373 stoppos, it->string);
3376 eassert (STRINGP (it->string)
3377 || (it->stop_charpos >= BEGV
3378 && it->stop_charpos >= IT_CHARPOS (*it)));
3382 /* Return the position of the next overlay change after POS in
3383 current_buffer. Value is point-max if no overlay change
3384 follows. This is like `next-overlay-change' but doesn't use
3385 xmalloc. */
3387 static ptrdiff_t
3388 next_overlay_change (ptrdiff_t pos)
3390 ptrdiff_t i, noverlays;
3391 ptrdiff_t endpos;
3392 Lisp_Object *overlays;
3394 /* Get all overlays at the given position. */
3395 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3397 /* If any of these overlays ends before endpos,
3398 use its ending point instead. */
3399 for (i = 0; i < noverlays; ++i)
3401 Lisp_Object oend;
3402 ptrdiff_t oendpos;
3404 oend = OVERLAY_END (overlays[i]);
3405 oendpos = OVERLAY_POSITION (oend);
3406 endpos = min (endpos, oendpos);
3409 return endpos;
3412 /* How many characters forward to search for a display property or
3413 display string. Searching too far forward makes the bidi display
3414 sluggish, especially in small windows. */
3415 #define MAX_DISP_SCAN 250
3417 /* Return the character position of a display string at or after
3418 position specified by POSITION. If no display string exists at or
3419 after POSITION, return ZV. A display string is either an overlay
3420 with `display' property whose value is a string, or a `display'
3421 text property whose value is a string. STRING is data about the
3422 string to iterate; if STRING->lstring is nil, we are iterating a
3423 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3424 on a GUI frame. DISP_PROP is set to zero if we searched
3425 MAX_DISP_SCAN characters forward without finding any display
3426 strings, non-zero otherwise. It is set to 2 if the display string
3427 uses any kind of `(space ...)' spec that will produce a stretch of
3428 white space in the text area. */
3429 ptrdiff_t
3430 compute_display_string_pos (struct text_pos *position,
3431 struct bidi_string_data *string,
3432 int frame_window_p, int *disp_prop)
3434 /* OBJECT = nil means current buffer. */
3435 Lisp_Object object =
3436 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3437 Lisp_Object pos, spec, limpos;
3438 int string_p = (string && (STRINGP (string->lstring) || string->s));
3439 ptrdiff_t eob = string_p ? string->schars : ZV;
3440 ptrdiff_t begb = string_p ? 0 : BEGV;
3441 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3442 ptrdiff_t lim =
3443 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3444 struct text_pos tpos;
3445 int rv = 0;
3447 *disp_prop = 1;
3449 if (charpos >= eob
3450 /* We don't support display properties whose values are strings
3451 that have display string properties. */
3452 || string->from_disp_str
3453 /* C strings cannot have display properties. */
3454 || (string->s && !STRINGP (object)))
3456 *disp_prop = 0;
3457 return eob;
3460 /* If the character at CHARPOS is where the display string begins,
3461 return CHARPOS. */
3462 pos = make_number (charpos);
3463 if (STRINGP (object))
3464 bufpos = string->bufpos;
3465 else
3466 bufpos = charpos;
3467 tpos = *position;
3468 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3469 && (charpos <= begb
3470 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3471 object),
3472 spec))
3473 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3474 frame_window_p)))
3476 if (rv == 2)
3477 *disp_prop = 2;
3478 return charpos;
3481 /* Look forward for the first character with a `display' property
3482 that will replace the underlying text when displayed. */
3483 limpos = make_number (lim);
3484 do {
3485 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3486 CHARPOS (tpos) = XFASTINT (pos);
3487 if (CHARPOS (tpos) >= lim)
3489 *disp_prop = 0;
3490 break;
3492 if (STRINGP (object))
3493 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3494 else
3495 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3496 spec = Fget_char_property (pos, Qdisplay, object);
3497 if (!STRINGP (object))
3498 bufpos = CHARPOS (tpos);
3499 } while (NILP (spec)
3500 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3501 bufpos, frame_window_p)));
3502 if (rv == 2)
3503 *disp_prop = 2;
3505 return CHARPOS (tpos);
3508 /* Return the character position of the end of the display string that
3509 started at CHARPOS. If there's no display string at CHARPOS,
3510 return -1. A display string is either an overlay with `display'
3511 property whose value is a string or a `display' text property whose
3512 value is a string. */
3513 ptrdiff_t
3514 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3516 /* OBJECT = nil means current buffer. */
3517 Lisp_Object object =
3518 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3519 Lisp_Object pos = make_number (charpos);
3520 ptrdiff_t eob =
3521 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3523 if (charpos >= eob || (string->s && !STRINGP (object)))
3524 return eob;
3526 /* It could happen that the display property or overlay was removed
3527 since we found it in compute_display_string_pos above. One way
3528 this can happen is if JIT font-lock was called (through
3529 handle_fontified_prop), and jit-lock-functions remove text
3530 properties or overlays from the portion of buffer that includes
3531 CHARPOS. Muse mode is known to do that, for example. In this
3532 case, we return -1 to the caller, to signal that no display
3533 string is actually present at CHARPOS. See bidi_fetch_char for
3534 how this is handled.
3536 An alternative would be to never look for display properties past
3537 it->stop_charpos. But neither compute_display_string_pos nor
3538 bidi_fetch_char that calls it know or care where the next
3539 stop_charpos is. */
3540 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3541 return -1;
3543 /* Look forward for the first character where the `display' property
3544 changes. */
3545 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3547 return XFASTINT (pos);
3552 /***********************************************************************
3553 Fontification
3554 ***********************************************************************/
3556 /* Handle changes in the `fontified' property of the current buffer by
3557 calling hook functions from Qfontification_functions to fontify
3558 regions of text. */
3560 static enum prop_handled
3561 handle_fontified_prop (struct it *it)
3563 Lisp_Object prop, pos;
3564 enum prop_handled handled = HANDLED_NORMALLY;
3566 if (!NILP (Vmemory_full))
3567 return handled;
3569 /* Get the value of the `fontified' property at IT's current buffer
3570 position. (The `fontified' property doesn't have a special
3571 meaning in strings.) If the value is nil, call functions from
3572 Qfontification_functions. */
3573 if (!STRINGP (it->string)
3574 && it->s == NULL
3575 && !NILP (Vfontification_functions)
3576 && !NILP (Vrun_hooks)
3577 && (pos = make_number (IT_CHARPOS (*it)),
3578 prop = Fget_char_property (pos, Qfontified, Qnil),
3579 /* Ignore the special cased nil value always present at EOB since
3580 no amount of fontifying will be able to change it. */
3581 NILP (prop) && IT_CHARPOS (*it) < Z))
3583 ptrdiff_t count = SPECPDL_INDEX ();
3584 Lisp_Object val;
3585 struct buffer *obuf = current_buffer;
3586 int begv = BEGV, zv = ZV;
3587 int old_clip_changed = current_buffer->clip_changed;
3589 val = Vfontification_functions;
3590 specbind (Qfontification_functions, Qnil);
3592 eassert (it->end_charpos == ZV);
3594 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3595 safe_call1 (val, pos);
3596 else
3598 Lisp_Object fns, fn;
3599 struct gcpro gcpro1, gcpro2;
3601 fns = Qnil;
3602 GCPRO2 (val, fns);
3604 for (; CONSP (val); val = XCDR (val))
3606 fn = XCAR (val);
3608 if (EQ (fn, Qt))
3610 /* A value of t indicates this hook has a local
3611 binding; it means to run the global binding too.
3612 In a global value, t should not occur. If it
3613 does, we must ignore it to avoid an endless
3614 loop. */
3615 for (fns = Fdefault_value (Qfontification_functions);
3616 CONSP (fns);
3617 fns = XCDR (fns))
3619 fn = XCAR (fns);
3620 if (!EQ (fn, Qt))
3621 safe_call1 (fn, pos);
3624 else
3625 safe_call1 (fn, pos);
3628 UNGCPRO;
3631 unbind_to (count, Qnil);
3633 /* Fontification functions routinely call `save-restriction'.
3634 Normally, this tags clip_changed, which can confuse redisplay
3635 (see discussion in Bug#6671). Since we don't perform any
3636 special handling of fontification changes in the case where
3637 `save-restriction' isn't called, there's no point doing so in
3638 this case either. So, if the buffer's restrictions are
3639 actually left unchanged, reset clip_changed. */
3640 if (obuf == current_buffer)
3642 if (begv == BEGV && zv == ZV)
3643 current_buffer->clip_changed = old_clip_changed;
3645 /* There isn't much we can reasonably do to protect against
3646 misbehaving fontification, but here's a fig leaf. */
3647 else if (!NILP (BVAR (obuf, name)))
3648 set_buffer_internal_1 (obuf);
3650 /* The fontification code may have added/removed text.
3651 It could do even a lot worse, but let's at least protect against
3652 the most obvious case where only the text past `pos' gets changed',
3653 as is/was done in grep.el where some escapes sequences are turned
3654 into face properties (bug#7876). */
3655 it->end_charpos = ZV;
3657 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3658 something. This avoids an endless loop if they failed to
3659 fontify the text for which reason ever. */
3660 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3661 handled = HANDLED_RECOMPUTE_PROPS;
3664 return handled;
3669 /***********************************************************************
3670 Faces
3671 ***********************************************************************/
3673 /* Set up iterator IT from face properties at its current position.
3674 Called from handle_stop. */
3676 static enum prop_handled
3677 handle_face_prop (struct it *it)
3679 int new_face_id;
3680 ptrdiff_t next_stop;
3682 if (!STRINGP (it->string))
3684 new_face_id
3685 = face_at_buffer_position (it->w,
3686 IT_CHARPOS (*it),
3687 it->region_beg_charpos,
3688 it->region_end_charpos,
3689 &next_stop,
3690 (IT_CHARPOS (*it)
3691 + TEXT_PROP_DISTANCE_LIMIT),
3692 0, it->base_face_id);
3694 /* Is this a start of a run of characters with box face?
3695 Caveat: this can be called for a freshly initialized
3696 iterator; face_id is -1 in this case. We know that the new
3697 face will not change until limit, i.e. if the new face has a
3698 box, all characters up to limit will have one. But, as
3699 usual, we don't know whether limit is really the end. */
3700 if (new_face_id != it->face_id)
3702 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3704 /* If new face has a box but old face has not, this is
3705 the start of a run of characters with box, i.e. it has
3706 a shadow on the left side. The value of face_id of the
3707 iterator will be -1 if this is the initial call that gets
3708 the face. In this case, we have to look in front of IT's
3709 position and see whether there is a face != new_face_id. */
3710 it->start_of_box_run_p
3711 = (new_face->box != FACE_NO_BOX
3712 && (it->face_id >= 0
3713 || IT_CHARPOS (*it) == BEG
3714 || new_face_id != face_before_it_pos (it)));
3715 it->face_box_p = new_face->box != FACE_NO_BOX;
3718 else
3720 int base_face_id;
3721 ptrdiff_t bufpos;
3722 int i;
3723 Lisp_Object from_overlay
3724 = (it->current.overlay_string_index >= 0
3725 ? it->string_overlays[it->current.overlay_string_index
3726 % OVERLAY_STRING_CHUNK_SIZE]
3727 : Qnil);
3729 /* See if we got to this string directly or indirectly from
3730 an overlay property. That includes the before-string or
3731 after-string of an overlay, strings in display properties
3732 provided by an overlay, their text properties, etc.
3734 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3735 if (! NILP (from_overlay))
3736 for (i = it->sp - 1; i >= 0; i--)
3738 if (it->stack[i].current.overlay_string_index >= 0)
3739 from_overlay
3740 = it->string_overlays[it->stack[i].current.overlay_string_index
3741 % OVERLAY_STRING_CHUNK_SIZE];
3742 else if (! NILP (it->stack[i].from_overlay))
3743 from_overlay = it->stack[i].from_overlay;
3745 if (!NILP (from_overlay))
3746 break;
3749 if (! NILP (from_overlay))
3751 bufpos = IT_CHARPOS (*it);
3752 /* For a string from an overlay, the base face depends
3753 only on text properties and ignores overlays. */
3754 base_face_id
3755 = face_for_overlay_string (it->w,
3756 IT_CHARPOS (*it),
3757 it->region_beg_charpos,
3758 it->region_end_charpos,
3759 &next_stop,
3760 (IT_CHARPOS (*it)
3761 + TEXT_PROP_DISTANCE_LIMIT),
3763 from_overlay);
3765 else
3767 bufpos = 0;
3769 /* For strings from a `display' property, use the face at
3770 IT's current buffer position as the base face to merge
3771 with, so that overlay strings appear in the same face as
3772 surrounding text, unless they specify their own
3773 faces. */
3774 base_face_id = it->string_from_prefix_prop_p
3775 ? DEFAULT_FACE_ID
3776 : underlying_face_id (it);
3779 new_face_id = face_at_string_position (it->w,
3780 it->string,
3781 IT_STRING_CHARPOS (*it),
3782 bufpos,
3783 it->region_beg_charpos,
3784 it->region_end_charpos,
3785 &next_stop,
3786 base_face_id, 0);
3788 /* Is this a start of a run of characters with box? Caveat:
3789 this can be called for a freshly allocated iterator; face_id
3790 is -1 is this case. We know that the new face will not
3791 change until the next check pos, i.e. if the new face has a
3792 box, all characters up to that position will have a
3793 box. But, as usual, we don't know whether that position
3794 is really the end. */
3795 if (new_face_id != it->face_id)
3797 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3798 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3800 /* If new face has a box but old face hasn't, this is the
3801 start of a run of characters with box, i.e. it has a
3802 shadow on the left side. */
3803 it->start_of_box_run_p
3804 = new_face->box && (old_face == NULL || !old_face->box);
3805 it->face_box_p = new_face->box != FACE_NO_BOX;
3809 it->face_id = new_face_id;
3810 return HANDLED_NORMALLY;
3814 /* Return the ID of the face ``underlying'' IT's current position,
3815 which is in a string. If the iterator is associated with a
3816 buffer, return the face at IT's current buffer position.
3817 Otherwise, use the iterator's base_face_id. */
3819 static int
3820 underlying_face_id (struct it *it)
3822 int face_id = it->base_face_id, i;
3824 eassert (STRINGP (it->string));
3826 for (i = it->sp - 1; i >= 0; --i)
3827 if (NILP (it->stack[i].string))
3828 face_id = it->stack[i].face_id;
3830 return face_id;
3834 /* Compute the face one character before or after the current position
3835 of IT, in the visual order. BEFORE_P non-zero means get the face
3836 in front (to the left in L2R paragraphs, to the right in R2L
3837 paragraphs) of IT's screen position. Value is the ID of the face. */
3839 static int
3840 face_before_or_after_it_pos (struct it *it, int before_p)
3842 int face_id, limit;
3843 ptrdiff_t next_check_charpos;
3844 struct it it_copy;
3845 void *it_copy_data = NULL;
3847 eassert (it->s == NULL);
3849 if (STRINGP (it->string))
3851 ptrdiff_t bufpos, charpos;
3852 int base_face_id;
3854 /* No face change past the end of the string (for the case
3855 we are padding with spaces). No face change before the
3856 string start. */
3857 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3858 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3859 return it->face_id;
3861 if (!it->bidi_p)
3863 /* Set charpos to the position before or after IT's current
3864 position, in the logical order, which in the non-bidi
3865 case is the same as the visual order. */
3866 if (before_p)
3867 charpos = IT_STRING_CHARPOS (*it) - 1;
3868 else if (it->what == IT_COMPOSITION)
3869 /* For composition, we must check the character after the
3870 composition. */
3871 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3872 else
3873 charpos = IT_STRING_CHARPOS (*it) + 1;
3875 else
3877 if (before_p)
3879 /* With bidi iteration, the character before the current
3880 in the visual order cannot be found by simple
3881 iteration, because "reverse" reordering is not
3882 supported. Instead, we need to use the move_it_*
3883 family of functions. */
3884 /* Ignore face changes before the first visible
3885 character on this display line. */
3886 if (it->current_x <= it->first_visible_x)
3887 return it->face_id;
3888 SAVE_IT (it_copy, *it, it_copy_data);
3889 /* Implementation note: Since move_it_in_display_line
3890 works in the iterator geometry, and thinks the first
3891 character is always the leftmost, even in R2L lines,
3892 we don't need to distinguish between the R2L and L2R
3893 cases here. */
3894 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3895 it_copy.current_x - 1, MOVE_TO_X);
3896 charpos = IT_STRING_CHARPOS (it_copy);
3897 RESTORE_IT (it, it, it_copy_data);
3899 else
3901 /* Set charpos to the string position of the character
3902 that comes after IT's current position in the visual
3903 order. */
3904 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3906 it_copy = *it;
3907 while (n--)
3908 bidi_move_to_visually_next (&it_copy.bidi_it);
3910 charpos = it_copy.bidi_it.charpos;
3913 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3915 if (it->current.overlay_string_index >= 0)
3916 bufpos = IT_CHARPOS (*it);
3917 else
3918 bufpos = 0;
3920 base_face_id = underlying_face_id (it);
3922 /* Get the face for ASCII, or unibyte. */
3923 face_id = face_at_string_position (it->w,
3924 it->string,
3925 charpos,
3926 bufpos,
3927 it->region_beg_charpos,
3928 it->region_end_charpos,
3929 &next_check_charpos,
3930 base_face_id, 0);
3932 /* Correct the face for charsets different from ASCII. Do it
3933 for the multibyte case only. The face returned above is
3934 suitable for unibyte text if IT->string is unibyte. */
3935 if (STRING_MULTIBYTE (it->string))
3937 struct text_pos pos1 = string_pos (charpos, it->string);
3938 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3939 int c, len;
3940 struct face *face = FACE_FROM_ID (it->f, face_id);
3942 c = string_char_and_length (p, &len);
3943 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3946 else
3948 struct text_pos pos;
3950 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3951 || (IT_CHARPOS (*it) <= BEGV && before_p))
3952 return it->face_id;
3954 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3955 pos = it->current.pos;
3957 if (!it->bidi_p)
3959 if (before_p)
3960 DEC_TEXT_POS (pos, it->multibyte_p);
3961 else
3963 if (it->what == IT_COMPOSITION)
3965 /* For composition, we must check the position after
3966 the composition. */
3967 pos.charpos += it->cmp_it.nchars;
3968 pos.bytepos += it->len;
3970 else
3971 INC_TEXT_POS (pos, it->multibyte_p);
3974 else
3976 if (before_p)
3978 /* With bidi iteration, the character before the current
3979 in the visual order cannot be found by simple
3980 iteration, because "reverse" reordering is not
3981 supported. Instead, we need to use the move_it_*
3982 family of functions. */
3983 /* Ignore face changes before the first visible
3984 character on this display line. */
3985 if (it->current_x <= it->first_visible_x)
3986 return it->face_id;
3987 SAVE_IT (it_copy, *it, it_copy_data);
3988 /* Implementation note: Since move_it_in_display_line
3989 works in the iterator geometry, and thinks the first
3990 character is always the leftmost, even in R2L lines,
3991 we don't need to distinguish between the R2L and L2R
3992 cases here. */
3993 move_it_in_display_line (&it_copy, ZV,
3994 it_copy.current_x - 1, MOVE_TO_X);
3995 pos = it_copy.current.pos;
3996 RESTORE_IT (it, it, it_copy_data);
3998 else
4000 /* Set charpos to the buffer position of the character
4001 that comes after IT's current position in the visual
4002 order. */
4003 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4005 it_copy = *it;
4006 while (n--)
4007 bidi_move_to_visually_next (&it_copy.bidi_it);
4009 SET_TEXT_POS (pos,
4010 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4013 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4015 /* Determine face for CHARSET_ASCII, or unibyte. */
4016 face_id = face_at_buffer_position (it->w,
4017 CHARPOS (pos),
4018 it->region_beg_charpos,
4019 it->region_end_charpos,
4020 &next_check_charpos,
4021 limit, 0, -1);
4023 /* Correct the face for charsets different from ASCII. Do it
4024 for the multibyte case only. The face returned above is
4025 suitable for unibyte text if current_buffer is unibyte. */
4026 if (it->multibyte_p)
4028 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4029 struct face *face = FACE_FROM_ID (it->f, face_id);
4030 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4034 return face_id;
4039 /***********************************************************************
4040 Invisible text
4041 ***********************************************************************/
4043 /* Set up iterator IT from invisible properties at its current
4044 position. Called from handle_stop. */
4046 static enum prop_handled
4047 handle_invisible_prop (struct it *it)
4049 enum prop_handled handled = HANDLED_NORMALLY;
4051 if (STRINGP (it->string))
4053 Lisp_Object prop, end_charpos, limit, charpos;
4055 /* Get the value of the invisible text property at the
4056 current position. Value will be nil if there is no such
4057 property. */
4058 charpos = make_number (IT_STRING_CHARPOS (*it));
4059 prop = Fget_text_property (charpos, Qinvisible, it->string);
4061 if (!NILP (prop)
4062 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4064 ptrdiff_t endpos;
4066 handled = HANDLED_RECOMPUTE_PROPS;
4068 /* Get the position at which the next change of the
4069 invisible text property can be found in IT->string.
4070 Value will be nil if the property value is the same for
4071 all the rest of IT->string. */
4072 XSETINT (limit, SCHARS (it->string));
4073 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4074 it->string, limit);
4076 /* Text at current position is invisible. The next
4077 change in the property is at position end_charpos.
4078 Move IT's current position to that position. */
4079 if (INTEGERP (end_charpos)
4080 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4082 struct text_pos old;
4083 ptrdiff_t oldpos;
4085 old = it->current.string_pos;
4086 oldpos = CHARPOS (old);
4087 if (it->bidi_p)
4089 if (it->bidi_it.first_elt
4090 && it->bidi_it.charpos < SCHARS (it->string))
4091 bidi_paragraph_init (it->paragraph_embedding,
4092 &it->bidi_it, 1);
4093 /* Bidi-iterate out of the invisible text. */
4096 bidi_move_to_visually_next (&it->bidi_it);
4098 while (oldpos <= it->bidi_it.charpos
4099 && it->bidi_it.charpos < endpos);
4101 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4102 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4103 if (IT_CHARPOS (*it) >= endpos)
4104 it->prev_stop = endpos;
4106 else
4108 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4109 compute_string_pos (&it->current.string_pos, old, it->string);
4112 else
4114 /* The rest of the string is invisible. If this is an
4115 overlay string, proceed with the next overlay string
4116 or whatever comes and return a character from there. */
4117 if (it->current.overlay_string_index >= 0)
4119 next_overlay_string (it);
4120 /* Don't check for overlay strings when we just
4121 finished processing them. */
4122 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4124 else
4126 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4127 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4132 else
4134 int invis_p;
4135 ptrdiff_t newpos, next_stop, start_charpos, tem;
4136 Lisp_Object pos, prop, overlay;
4138 /* First of all, is there invisible text at this position? */
4139 tem = start_charpos = IT_CHARPOS (*it);
4140 pos = make_number (tem);
4141 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4142 &overlay);
4143 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4145 /* If we are on invisible text, skip over it. */
4146 if (invis_p && start_charpos < it->end_charpos)
4148 /* Record whether we have to display an ellipsis for the
4149 invisible text. */
4150 int display_ellipsis_p = invis_p == 2;
4152 handled = HANDLED_RECOMPUTE_PROPS;
4154 /* Loop skipping over invisible text. The loop is left at
4155 ZV or with IT on the first char being visible again. */
4158 /* Try to skip some invisible text. Return value is the
4159 position reached which can be equal to where we start
4160 if there is nothing invisible there. This skips both
4161 over invisible text properties and overlays with
4162 invisible property. */
4163 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4165 /* If we skipped nothing at all we weren't at invisible
4166 text in the first place. If everything to the end of
4167 the buffer was skipped, end the loop. */
4168 if (newpos == tem || newpos >= ZV)
4169 invis_p = 0;
4170 else
4172 /* We skipped some characters but not necessarily
4173 all there are. Check if we ended up on visible
4174 text. Fget_char_property returns the property of
4175 the char before the given position, i.e. if we
4176 get invis_p = 0, this means that the char at
4177 newpos is visible. */
4178 pos = make_number (newpos);
4179 prop = Fget_char_property (pos, Qinvisible, it->window);
4180 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4183 /* If we ended up on invisible text, proceed to
4184 skip starting with next_stop. */
4185 if (invis_p)
4186 tem = next_stop;
4188 /* If there are adjacent invisible texts, don't lose the
4189 second one's ellipsis. */
4190 if (invis_p == 2)
4191 display_ellipsis_p = 1;
4193 while (invis_p);
4195 /* The position newpos is now either ZV or on visible text. */
4196 if (it->bidi_p)
4198 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4199 int on_newline =
4200 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4201 int after_newline =
4202 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4204 /* If the invisible text ends on a newline or on a
4205 character after a newline, we can avoid the costly,
4206 character by character, bidi iteration to NEWPOS, and
4207 instead simply reseat the iterator there. That's
4208 because all bidi reordering information is tossed at
4209 the newline. This is a big win for modes that hide
4210 complete lines, like Outline, Org, etc. */
4211 if (on_newline || after_newline)
4213 struct text_pos tpos;
4214 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4216 SET_TEXT_POS (tpos, newpos, bpos);
4217 reseat_1 (it, tpos, 0);
4218 /* If we reseat on a newline/ZV, we need to prep the
4219 bidi iterator for advancing to the next character
4220 after the newline/EOB, keeping the current paragraph
4221 direction (so that PRODUCE_GLYPHS does TRT wrt
4222 prepending/appending glyphs to a glyph row). */
4223 if (on_newline)
4225 it->bidi_it.first_elt = 0;
4226 it->bidi_it.paragraph_dir = pdir;
4227 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4228 it->bidi_it.nchars = 1;
4229 it->bidi_it.ch_len = 1;
4232 else /* Must use the slow method. */
4234 /* With bidi iteration, the region of invisible text
4235 could start and/or end in the middle of a
4236 non-base embedding level. Therefore, we need to
4237 skip invisible text using the bidi iterator,
4238 starting at IT's current position, until we find
4239 ourselves outside of the invisible text.
4240 Skipping invisible text _after_ bidi iteration
4241 avoids affecting the visual order of the
4242 displayed text when invisible properties are
4243 added or removed. */
4244 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4246 /* If we were `reseat'ed to a new paragraph,
4247 determine the paragraph base direction. We
4248 need to do it now because
4249 next_element_from_buffer may not have a
4250 chance to do it, if we are going to skip any
4251 text at the beginning, which resets the
4252 FIRST_ELT flag. */
4253 bidi_paragraph_init (it->paragraph_embedding,
4254 &it->bidi_it, 1);
4258 bidi_move_to_visually_next (&it->bidi_it);
4260 while (it->stop_charpos <= it->bidi_it.charpos
4261 && it->bidi_it.charpos < newpos);
4262 IT_CHARPOS (*it) = it->bidi_it.charpos;
4263 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4264 /* If we overstepped NEWPOS, record its position in
4265 the iterator, so that we skip invisible text if
4266 later the bidi iteration lands us in the
4267 invisible region again. */
4268 if (IT_CHARPOS (*it) >= newpos)
4269 it->prev_stop = newpos;
4272 else
4274 IT_CHARPOS (*it) = newpos;
4275 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4278 /* If there are before-strings at the start of invisible
4279 text, and the text is invisible because of a text
4280 property, arrange to show before-strings because 20.x did
4281 it that way. (If the text is invisible because of an
4282 overlay property instead of a text property, this is
4283 already handled in the overlay code.) */
4284 if (NILP (overlay)
4285 && get_overlay_strings (it, it->stop_charpos))
4287 handled = HANDLED_RECOMPUTE_PROPS;
4288 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4290 else if (display_ellipsis_p)
4292 /* Make sure that the glyphs of the ellipsis will get
4293 correct `charpos' values. If we would not update
4294 it->position here, the glyphs would belong to the
4295 last visible character _before_ the invisible
4296 text, which confuses `set_cursor_from_row'.
4298 We use the last invisible position instead of the
4299 first because this way the cursor is always drawn on
4300 the first "." of the ellipsis, whenever PT is inside
4301 the invisible text. Otherwise the cursor would be
4302 placed _after_ the ellipsis when the point is after the
4303 first invisible character. */
4304 if (!STRINGP (it->object))
4306 it->position.charpos = newpos - 1;
4307 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4309 it->ellipsis_p = 1;
4310 /* Let the ellipsis display before
4311 considering any properties of the following char.
4312 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4313 handled = HANDLED_RETURN;
4318 return handled;
4322 /* Make iterator IT return `...' next.
4323 Replaces LEN characters from buffer. */
4325 static void
4326 setup_for_ellipsis (struct it *it, int len)
4328 /* Use the display table definition for `...'. Invalid glyphs
4329 will be handled by the method returning elements from dpvec. */
4330 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4332 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4333 it->dpvec = v->contents;
4334 it->dpend = v->contents + v->header.size;
4336 else
4338 /* Default `...'. */
4339 it->dpvec = default_invis_vector;
4340 it->dpend = default_invis_vector + 3;
4343 it->dpvec_char_len = len;
4344 it->current.dpvec_index = 0;
4345 it->dpvec_face_id = -1;
4347 /* Remember the current face id in case glyphs specify faces.
4348 IT's face is restored in set_iterator_to_next.
4349 saved_face_id was set to preceding char's face in handle_stop. */
4350 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4351 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4353 it->method = GET_FROM_DISPLAY_VECTOR;
4354 it->ellipsis_p = 1;
4359 /***********************************************************************
4360 'display' property
4361 ***********************************************************************/
4363 /* Set up iterator IT from `display' property at its current position.
4364 Called from handle_stop.
4365 We return HANDLED_RETURN if some part of the display property
4366 overrides the display of the buffer text itself.
4367 Otherwise we return HANDLED_NORMALLY. */
4369 static enum prop_handled
4370 handle_display_prop (struct it *it)
4372 Lisp_Object propval, object, overlay;
4373 struct text_pos *position;
4374 ptrdiff_t bufpos;
4375 /* Nonzero if some property replaces the display of the text itself. */
4376 int display_replaced_p = 0;
4378 if (STRINGP (it->string))
4380 object = it->string;
4381 position = &it->current.string_pos;
4382 bufpos = CHARPOS (it->current.pos);
4384 else
4386 XSETWINDOW (object, it->w);
4387 position = &it->current.pos;
4388 bufpos = CHARPOS (*position);
4391 /* Reset those iterator values set from display property values. */
4392 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4393 it->space_width = Qnil;
4394 it->font_height = Qnil;
4395 it->voffset = 0;
4397 /* We don't support recursive `display' properties, i.e. string
4398 values that have a string `display' property, that have a string
4399 `display' property etc. */
4400 if (!it->string_from_display_prop_p)
4401 it->area = TEXT_AREA;
4403 propval = get_char_property_and_overlay (make_number (position->charpos),
4404 Qdisplay, object, &overlay);
4405 if (NILP (propval))
4406 return HANDLED_NORMALLY;
4407 /* Now OVERLAY is the overlay that gave us this property, or nil
4408 if it was a text property. */
4410 if (!STRINGP (it->string))
4411 object = it->w->buffer;
4413 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4414 position, bufpos,
4415 FRAME_WINDOW_P (it->f));
4417 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4420 /* Subroutine of handle_display_prop. Returns non-zero if the display
4421 specification in SPEC is a replacing specification, i.e. it would
4422 replace the text covered by `display' property with something else,
4423 such as an image or a display string. If SPEC includes any kind or
4424 `(space ...) specification, the value is 2; this is used by
4425 compute_display_string_pos, which see.
4427 See handle_single_display_spec for documentation of arguments.
4428 frame_window_p is non-zero if the window being redisplayed is on a
4429 GUI frame; this argument is used only if IT is NULL, see below.
4431 IT can be NULL, if this is called by the bidi reordering code
4432 through compute_display_string_pos, which see. In that case, this
4433 function only examines SPEC, but does not otherwise "handle" it, in
4434 the sense that it doesn't set up members of IT from the display
4435 spec. */
4436 static int
4437 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4438 Lisp_Object overlay, struct text_pos *position,
4439 ptrdiff_t bufpos, int frame_window_p)
4441 int replacing_p = 0;
4442 int rv;
4444 if (CONSP (spec)
4445 /* Simple specifications. */
4446 && !EQ (XCAR (spec), Qimage)
4447 && !EQ (XCAR (spec), Qspace)
4448 && !EQ (XCAR (spec), Qwhen)
4449 && !EQ (XCAR (spec), Qslice)
4450 && !EQ (XCAR (spec), Qspace_width)
4451 && !EQ (XCAR (spec), Qheight)
4452 && !EQ (XCAR (spec), Qraise)
4453 /* Marginal area specifications. */
4454 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4455 && !EQ (XCAR (spec), Qleft_fringe)
4456 && !EQ (XCAR (spec), Qright_fringe)
4457 && !NILP (XCAR (spec)))
4459 for (; CONSP (spec); spec = XCDR (spec))
4461 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4462 overlay, position, bufpos,
4463 replacing_p, frame_window_p)))
4465 replacing_p = rv;
4466 /* If some text in a string is replaced, `position' no
4467 longer points to the position of `object'. */
4468 if (!it || STRINGP (object))
4469 break;
4473 else if (VECTORP (spec))
4475 ptrdiff_t i;
4476 for (i = 0; i < ASIZE (spec); ++i)
4477 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4478 overlay, position, bufpos,
4479 replacing_p, frame_window_p)))
4481 replacing_p = rv;
4482 /* If some text in a string is replaced, `position' no
4483 longer points to the position of `object'. */
4484 if (!it || STRINGP (object))
4485 break;
4488 else
4490 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4491 position, bufpos, 0,
4492 frame_window_p)))
4493 replacing_p = rv;
4496 return replacing_p;
4499 /* Value is the position of the end of the `display' property starting
4500 at START_POS in OBJECT. */
4502 static struct text_pos
4503 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4505 Lisp_Object end;
4506 struct text_pos end_pos;
4508 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4509 Qdisplay, object, Qnil);
4510 CHARPOS (end_pos) = XFASTINT (end);
4511 if (STRINGP (object))
4512 compute_string_pos (&end_pos, start_pos, it->string);
4513 else
4514 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4516 return end_pos;
4520 /* Set up IT from a single `display' property specification SPEC. OBJECT
4521 is the object in which the `display' property was found. *POSITION
4522 is the position in OBJECT at which the `display' property was found.
4523 BUFPOS is the buffer position of OBJECT (different from POSITION if
4524 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4525 previously saw a display specification which already replaced text
4526 display with something else, for example an image; we ignore such
4527 properties after the first one has been processed.
4529 OVERLAY is the overlay this `display' property came from,
4530 or nil if it was a text property.
4532 If SPEC is a `space' or `image' specification, and in some other
4533 cases too, set *POSITION to the position where the `display'
4534 property ends.
4536 If IT is NULL, only examine the property specification in SPEC, but
4537 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4538 is intended to be displayed in a window on a GUI frame.
4540 Value is non-zero if something was found which replaces the display
4541 of buffer or string text. */
4543 static int
4544 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4545 Lisp_Object overlay, struct text_pos *position,
4546 ptrdiff_t bufpos, int display_replaced_p,
4547 int frame_window_p)
4549 Lisp_Object form;
4550 Lisp_Object location, value;
4551 struct text_pos start_pos = *position;
4552 int valid_p;
4554 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4555 If the result is non-nil, use VALUE instead of SPEC. */
4556 form = Qt;
4557 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4559 spec = XCDR (spec);
4560 if (!CONSP (spec))
4561 return 0;
4562 form = XCAR (spec);
4563 spec = XCDR (spec);
4566 if (!NILP (form) && !EQ (form, Qt))
4568 ptrdiff_t count = SPECPDL_INDEX ();
4569 struct gcpro gcpro1;
4571 /* Bind `object' to the object having the `display' property, a
4572 buffer or string. Bind `position' to the position in the
4573 object where the property was found, and `buffer-position'
4574 to the current position in the buffer. */
4576 if (NILP (object))
4577 XSETBUFFER (object, current_buffer);
4578 specbind (Qobject, object);
4579 specbind (Qposition, make_number (CHARPOS (*position)));
4580 specbind (Qbuffer_position, make_number (bufpos));
4581 GCPRO1 (form);
4582 form = safe_eval (form);
4583 UNGCPRO;
4584 unbind_to (count, Qnil);
4587 if (NILP (form))
4588 return 0;
4590 /* Handle `(height HEIGHT)' specifications. */
4591 if (CONSP (spec)
4592 && EQ (XCAR (spec), Qheight)
4593 && CONSP (XCDR (spec)))
4595 if (it)
4597 if (!FRAME_WINDOW_P (it->f))
4598 return 0;
4600 it->font_height = XCAR (XCDR (spec));
4601 if (!NILP (it->font_height))
4603 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4604 int new_height = -1;
4606 if (CONSP (it->font_height)
4607 && (EQ (XCAR (it->font_height), Qplus)
4608 || EQ (XCAR (it->font_height), Qminus))
4609 && CONSP (XCDR (it->font_height))
4610 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4612 /* `(+ N)' or `(- N)' where N is an integer. */
4613 int steps = XINT (XCAR (XCDR (it->font_height)));
4614 if (EQ (XCAR (it->font_height), Qplus))
4615 steps = - steps;
4616 it->face_id = smaller_face (it->f, it->face_id, steps);
4618 else if (FUNCTIONP (it->font_height))
4620 /* Call function with current height as argument.
4621 Value is the new height. */
4622 Lisp_Object height;
4623 height = safe_call1 (it->font_height,
4624 face->lface[LFACE_HEIGHT_INDEX]);
4625 if (NUMBERP (height))
4626 new_height = XFLOATINT (height);
4628 else if (NUMBERP (it->font_height))
4630 /* Value is a multiple of the canonical char height. */
4631 struct face *f;
4633 f = FACE_FROM_ID (it->f,
4634 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4635 new_height = (XFLOATINT (it->font_height)
4636 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4638 else
4640 /* Evaluate IT->font_height with `height' bound to the
4641 current specified height to get the new height. */
4642 ptrdiff_t count = SPECPDL_INDEX ();
4644 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4645 value = safe_eval (it->font_height);
4646 unbind_to (count, Qnil);
4648 if (NUMBERP (value))
4649 new_height = XFLOATINT (value);
4652 if (new_height > 0)
4653 it->face_id = face_with_height (it->f, it->face_id, new_height);
4657 return 0;
4660 /* Handle `(space-width WIDTH)'. */
4661 if (CONSP (spec)
4662 && EQ (XCAR (spec), Qspace_width)
4663 && CONSP (XCDR (spec)))
4665 if (it)
4667 if (!FRAME_WINDOW_P (it->f))
4668 return 0;
4670 value = XCAR (XCDR (spec));
4671 if (NUMBERP (value) && XFLOATINT (value) > 0)
4672 it->space_width = value;
4675 return 0;
4678 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4679 if (CONSP (spec)
4680 && EQ (XCAR (spec), Qslice))
4682 Lisp_Object tem;
4684 if (it)
4686 if (!FRAME_WINDOW_P (it->f))
4687 return 0;
4689 if (tem = XCDR (spec), CONSP (tem))
4691 it->slice.x = XCAR (tem);
4692 if (tem = XCDR (tem), CONSP (tem))
4694 it->slice.y = XCAR (tem);
4695 if (tem = XCDR (tem), CONSP (tem))
4697 it->slice.width = XCAR (tem);
4698 if (tem = XCDR (tem), CONSP (tem))
4699 it->slice.height = XCAR (tem);
4705 return 0;
4708 /* Handle `(raise FACTOR)'. */
4709 if (CONSP (spec)
4710 && EQ (XCAR (spec), Qraise)
4711 && CONSP (XCDR (spec)))
4713 if (it)
4715 if (!FRAME_WINDOW_P (it->f))
4716 return 0;
4718 #ifdef HAVE_WINDOW_SYSTEM
4719 value = XCAR (XCDR (spec));
4720 if (NUMBERP (value))
4722 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4723 it->voffset = - (XFLOATINT (value)
4724 * (FONT_HEIGHT (face->font)));
4726 #endif /* HAVE_WINDOW_SYSTEM */
4729 return 0;
4732 /* Don't handle the other kinds of display specifications
4733 inside a string that we got from a `display' property. */
4734 if (it && it->string_from_display_prop_p)
4735 return 0;
4737 /* Characters having this form of property are not displayed, so
4738 we have to find the end of the property. */
4739 if (it)
4741 start_pos = *position;
4742 *position = display_prop_end (it, object, start_pos);
4744 value = Qnil;
4746 /* Stop the scan at that end position--we assume that all
4747 text properties change there. */
4748 if (it)
4749 it->stop_charpos = position->charpos;
4751 /* Handle `(left-fringe BITMAP [FACE])'
4752 and `(right-fringe BITMAP [FACE])'. */
4753 if (CONSP (spec)
4754 && (EQ (XCAR (spec), Qleft_fringe)
4755 || EQ (XCAR (spec), Qright_fringe))
4756 && CONSP (XCDR (spec)))
4758 int fringe_bitmap;
4760 if (it)
4762 if (!FRAME_WINDOW_P (it->f))
4763 /* If we return here, POSITION has been advanced
4764 across the text with this property. */
4766 /* Synchronize the bidi iterator with POSITION. This is
4767 needed because we are not going to push the iterator
4768 on behalf of this display property, so there will be
4769 no pop_it call to do this synchronization for us. */
4770 if (it->bidi_p)
4772 it->position = *position;
4773 iterate_out_of_display_property (it);
4774 *position = it->position;
4776 return 1;
4779 else if (!frame_window_p)
4780 return 1;
4782 #ifdef HAVE_WINDOW_SYSTEM
4783 value = XCAR (XCDR (spec));
4784 if (!SYMBOLP (value)
4785 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4786 /* If we return here, POSITION has been advanced
4787 across the text with this property. */
4789 if (it && it->bidi_p)
4791 it->position = *position;
4792 iterate_out_of_display_property (it);
4793 *position = it->position;
4795 return 1;
4798 if (it)
4800 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4802 if (CONSP (XCDR (XCDR (spec))))
4804 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4805 int face_id2 = lookup_derived_face (it->f, face_name,
4806 FRINGE_FACE_ID, 0);
4807 if (face_id2 >= 0)
4808 face_id = face_id2;
4811 /* Save current settings of IT so that we can restore them
4812 when we are finished with the glyph property value. */
4813 push_it (it, position);
4815 it->area = TEXT_AREA;
4816 it->what = IT_IMAGE;
4817 it->image_id = -1; /* no image */
4818 it->position = start_pos;
4819 it->object = NILP (object) ? it->w->buffer : object;
4820 it->method = GET_FROM_IMAGE;
4821 it->from_overlay = Qnil;
4822 it->face_id = face_id;
4823 it->from_disp_prop_p = 1;
4825 /* Say that we haven't consumed the characters with
4826 `display' property yet. The call to pop_it in
4827 set_iterator_to_next will clean this up. */
4828 *position = start_pos;
4830 if (EQ (XCAR (spec), Qleft_fringe))
4832 it->left_user_fringe_bitmap = fringe_bitmap;
4833 it->left_user_fringe_face_id = face_id;
4835 else
4837 it->right_user_fringe_bitmap = fringe_bitmap;
4838 it->right_user_fringe_face_id = face_id;
4841 #endif /* HAVE_WINDOW_SYSTEM */
4842 return 1;
4845 /* Prepare to handle `((margin left-margin) ...)',
4846 `((margin right-margin) ...)' and `((margin nil) ...)'
4847 prefixes for display specifications. */
4848 location = Qunbound;
4849 if (CONSP (spec) && CONSP (XCAR (spec)))
4851 Lisp_Object tem;
4853 value = XCDR (spec);
4854 if (CONSP (value))
4855 value = XCAR (value);
4857 tem = XCAR (spec);
4858 if (EQ (XCAR (tem), Qmargin)
4859 && (tem = XCDR (tem),
4860 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4861 (NILP (tem)
4862 || EQ (tem, Qleft_margin)
4863 || EQ (tem, Qright_margin))))
4864 location = tem;
4867 if (EQ (location, Qunbound))
4869 location = Qnil;
4870 value = spec;
4873 /* After this point, VALUE is the property after any
4874 margin prefix has been stripped. It must be a string,
4875 an image specification, or `(space ...)'.
4877 LOCATION specifies where to display: `left-margin',
4878 `right-margin' or nil. */
4880 valid_p = (STRINGP (value)
4881 #ifdef HAVE_WINDOW_SYSTEM
4882 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4883 && valid_image_p (value))
4884 #endif /* not HAVE_WINDOW_SYSTEM */
4885 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4887 if (valid_p && !display_replaced_p)
4889 int retval = 1;
4891 if (!it)
4893 /* Callers need to know whether the display spec is any kind
4894 of `(space ...)' spec that is about to affect text-area
4895 display. */
4896 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4897 retval = 2;
4898 return retval;
4901 /* Save current settings of IT so that we can restore them
4902 when we are finished with the glyph property value. */
4903 push_it (it, position);
4904 it->from_overlay = overlay;
4905 it->from_disp_prop_p = 1;
4907 if (NILP (location))
4908 it->area = TEXT_AREA;
4909 else if (EQ (location, Qleft_margin))
4910 it->area = LEFT_MARGIN_AREA;
4911 else
4912 it->area = RIGHT_MARGIN_AREA;
4914 if (STRINGP (value))
4916 it->string = value;
4917 it->multibyte_p = STRING_MULTIBYTE (it->string);
4918 it->current.overlay_string_index = -1;
4919 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4920 it->end_charpos = it->string_nchars = SCHARS (it->string);
4921 it->method = GET_FROM_STRING;
4922 it->stop_charpos = 0;
4923 it->prev_stop = 0;
4924 it->base_level_stop = 0;
4925 it->string_from_display_prop_p = 1;
4926 /* Say that we haven't consumed the characters with
4927 `display' property yet. The call to pop_it in
4928 set_iterator_to_next will clean this up. */
4929 if (BUFFERP (object))
4930 *position = start_pos;
4932 /* Force paragraph direction to be that of the parent
4933 object. If the parent object's paragraph direction is
4934 not yet determined, default to L2R. */
4935 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4936 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4937 else
4938 it->paragraph_embedding = L2R;
4940 /* Set up the bidi iterator for this display string. */
4941 if (it->bidi_p)
4943 it->bidi_it.string.lstring = it->string;
4944 it->bidi_it.string.s = NULL;
4945 it->bidi_it.string.schars = it->end_charpos;
4946 it->bidi_it.string.bufpos = bufpos;
4947 it->bidi_it.string.from_disp_str = 1;
4948 it->bidi_it.string.unibyte = !it->multibyte_p;
4949 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4952 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4954 it->method = GET_FROM_STRETCH;
4955 it->object = value;
4956 *position = it->position = start_pos;
4957 retval = 1 + (it->area == TEXT_AREA);
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 else
4962 it->what = IT_IMAGE;
4963 it->image_id = lookup_image (it->f, value);
4964 it->position = start_pos;
4965 it->object = NILP (object) ? it->w->buffer : object;
4966 it->method = GET_FROM_IMAGE;
4968 /* Say that we haven't consumed the characters with
4969 `display' property yet. The call to pop_it in
4970 set_iterator_to_next will clean this up. */
4971 *position = start_pos;
4973 #endif /* HAVE_WINDOW_SYSTEM */
4975 return retval;
4978 /* Invalid property or property not supported. Restore
4979 POSITION to what it was before. */
4980 *position = start_pos;
4981 return 0;
4984 /* Check if PROP is a display property value whose text should be
4985 treated as intangible. OVERLAY is the overlay from which PROP
4986 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4987 specify the buffer position covered by PROP. */
4990 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4991 ptrdiff_t charpos, ptrdiff_t bytepos)
4993 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4994 struct text_pos position;
4996 SET_TEXT_POS (position, charpos, bytepos);
4997 return handle_display_spec (NULL, prop, Qnil, overlay,
4998 &position, charpos, frame_window_p);
5002 /* Return 1 if PROP is a display sub-property value containing STRING.
5004 Implementation note: this and the following function are really
5005 special cases of handle_display_spec and
5006 handle_single_display_spec, and should ideally use the same code.
5007 Until they do, these two pairs must be consistent and must be
5008 modified in sync. */
5010 static int
5011 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5013 if (EQ (string, prop))
5014 return 1;
5016 /* Skip over `when FORM'. */
5017 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5019 prop = XCDR (prop);
5020 if (!CONSP (prop))
5021 return 0;
5022 /* Actually, the condition following `when' should be eval'ed,
5023 like handle_single_display_spec does, and we should return
5024 zero if it evaluates to nil. However, this function is
5025 called only when the buffer was already displayed and some
5026 glyph in the glyph matrix was found to come from a display
5027 string. Therefore, the condition was already evaluated, and
5028 the result was non-nil, otherwise the display string wouldn't
5029 have been displayed and we would have never been called for
5030 this property. Thus, we can skip the evaluation and assume
5031 its result is non-nil. */
5032 prop = XCDR (prop);
5035 if (CONSP (prop))
5036 /* Skip over `margin LOCATION'. */
5037 if (EQ (XCAR (prop), Qmargin))
5039 prop = XCDR (prop);
5040 if (!CONSP (prop))
5041 return 0;
5043 prop = XCDR (prop);
5044 if (!CONSP (prop))
5045 return 0;
5048 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5052 /* Return 1 if STRING appears in the `display' property PROP. */
5054 static int
5055 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5057 if (CONSP (prop)
5058 && !EQ (XCAR (prop), Qwhen)
5059 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5061 /* A list of sub-properties. */
5062 while (CONSP (prop))
5064 if (single_display_spec_string_p (XCAR (prop), string))
5065 return 1;
5066 prop = XCDR (prop);
5069 else if (VECTORP (prop))
5071 /* A vector of sub-properties. */
5072 ptrdiff_t i;
5073 for (i = 0; i < ASIZE (prop); ++i)
5074 if (single_display_spec_string_p (AREF (prop, i), string))
5075 return 1;
5077 else
5078 return single_display_spec_string_p (prop, string);
5080 return 0;
5083 /* Look for STRING in overlays and text properties in the current
5084 buffer, between character positions FROM and TO (excluding TO).
5085 BACK_P non-zero means look back (in this case, TO is supposed to be
5086 less than FROM).
5087 Value is the first character position where STRING was found, or
5088 zero if it wasn't found before hitting TO.
5090 This function may only use code that doesn't eval because it is
5091 called asynchronously from note_mouse_highlight. */
5093 static ptrdiff_t
5094 string_buffer_position_lim (Lisp_Object string,
5095 ptrdiff_t from, ptrdiff_t to, int back_p)
5097 Lisp_Object limit, prop, pos;
5098 int found = 0;
5100 pos = make_number (max (from, BEGV));
5102 if (!back_p) /* looking forward */
5104 limit = make_number (min (to, ZV));
5105 while (!found && !EQ (pos, limit))
5107 prop = Fget_char_property (pos, Qdisplay, Qnil);
5108 if (!NILP (prop) && display_prop_string_p (prop, string))
5109 found = 1;
5110 else
5111 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5112 limit);
5115 else /* looking back */
5117 limit = make_number (max (to, BEGV));
5118 while (!found && !EQ (pos, limit))
5120 prop = Fget_char_property (pos, Qdisplay, Qnil);
5121 if (!NILP (prop) && display_prop_string_p (prop, string))
5122 found = 1;
5123 else
5124 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5125 limit);
5129 return found ? XINT (pos) : 0;
5132 /* Determine which buffer position in current buffer STRING comes from.
5133 AROUND_CHARPOS is an approximate position where it could come from.
5134 Value is the buffer position or 0 if it couldn't be determined.
5136 This function is necessary because we don't record buffer positions
5137 in glyphs generated from strings (to keep struct glyph small).
5138 This function may only use code that doesn't eval because it is
5139 called asynchronously from note_mouse_highlight. */
5141 static ptrdiff_t
5142 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5144 const int MAX_DISTANCE = 1000;
5145 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5146 around_charpos + MAX_DISTANCE,
5149 if (!found)
5150 found = string_buffer_position_lim (string, around_charpos,
5151 around_charpos - MAX_DISTANCE, 1);
5152 return found;
5157 /***********************************************************************
5158 `composition' property
5159 ***********************************************************************/
5161 /* Set up iterator IT from `composition' property at its current
5162 position. Called from handle_stop. */
5164 static enum prop_handled
5165 handle_composition_prop (struct it *it)
5167 Lisp_Object prop, string;
5168 ptrdiff_t pos, pos_byte, start, end;
5170 if (STRINGP (it->string))
5172 unsigned char *s;
5174 pos = IT_STRING_CHARPOS (*it);
5175 pos_byte = IT_STRING_BYTEPOS (*it);
5176 string = it->string;
5177 s = SDATA (string) + pos_byte;
5178 it->c = STRING_CHAR (s);
5180 else
5182 pos = IT_CHARPOS (*it);
5183 pos_byte = IT_BYTEPOS (*it);
5184 string = Qnil;
5185 it->c = FETCH_CHAR (pos_byte);
5188 /* If there's a valid composition and point is not inside of the
5189 composition (in the case that the composition is from the current
5190 buffer), draw a glyph composed from the composition components. */
5191 if (find_composition (pos, -1, &start, &end, &prop, string)
5192 && COMPOSITION_VALID_P (start, end, prop)
5193 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5195 if (start < pos)
5196 /* As we can't handle this situation (perhaps font-lock added
5197 a new composition), we just return here hoping that next
5198 redisplay will detect this composition much earlier. */
5199 return HANDLED_NORMALLY;
5200 if (start != pos)
5202 if (STRINGP (it->string))
5203 pos_byte = string_char_to_byte (it->string, start);
5204 else
5205 pos_byte = CHAR_TO_BYTE (start);
5207 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5208 prop, string);
5210 if (it->cmp_it.id >= 0)
5212 it->cmp_it.ch = -1;
5213 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5214 it->cmp_it.nglyphs = -1;
5218 return HANDLED_NORMALLY;
5223 /***********************************************************************
5224 Overlay strings
5225 ***********************************************************************/
5227 /* The following structure is used to record overlay strings for
5228 later sorting in load_overlay_strings. */
5230 struct overlay_entry
5232 Lisp_Object overlay;
5233 Lisp_Object string;
5234 EMACS_INT priority;
5235 int after_string_p;
5239 /* Set up iterator IT from overlay strings at its current position.
5240 Called from handle_stop. */
5242 static enum prop_handled
5243 handle_overlay_change (struct it *it)
5245 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5246 return HANDLED_RECOMPUTE_PROPS;
5247 else
5248 return HANDLED_NORMALLY;
5252 /* Set up the next overlay string for delivery by IT, if there is an
5253 overlay string to deliver. Called by set_iterator_to_next when the
5254 end of the current overlay string is reached. If there are more
5255 overlay strings to display, IT->string and
5256 IT->current.overlay_string_index are set appropriately here.
5257 Otherwise IT->string is set to nil. */
5259 static void
5260 next_overlay_string (struct it *it)
5262 ++it->current.overlay_string_index;
5263 if (it->current.overlay_string_index == it->n_overlay_strings)
5265 /* No more overlay strings. Restore IT's settings to what
5266 they were before overlay strings were processed, and
5267 continue to deliver from current_buffer. */
5269 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5270 pop_it (it);
5271 eassert (it->sp > 0
5272 || (NILP (it->string)
5273 && it->method == GET_FROM_BUFFER
5274 && it->stop_charpos >= BEGV
5275 && it->stop_charpos <= it->end_charpos));
5276 it->current.overlay_string_index = -1;
5277 it->n_overlay_strings = 0;
5278 it->overlay_strings_charpos = -1;
5279 /* If there's an empty display string on the stack, pop the
5280 stack, to resync the bidi iterator with IT's position. Such
5281 empty strings are pushed onto the stack in
5282 get_overlay_strings_1. */
5283 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5284 pop_it (it);
5286 /* If we're at the end of the buffer, record that we have
5287 processed the overlay strings there already, so that
5288 next_element_from_buffer doesn't try it again. */
5289 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5290 it->overlay_strings_at_end_processed_p = 1;
5292 else
5294 /* There are more overlay strings to process. If
5295 IT->current.overlay_string_index has advanced to a position
5296 where we must load IT->overlay_strings with more strings, do
5297 it. We must load at the IT->overlay_strings_charpos where
5298 IT->n_overlay_strings was originally computed; when invisible
5299 text is present, this might not be IT_CHARPOS (Bug#7016). */
5300 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5302 if (it->current.overlay_string_index && i == 0)
5303 load_overlay_strings (it, it->overlay_strings_charpos);
5305 /* Initialize IT to deliver display elements from the overlay
5306 string. */
5307 it->string = it->overlay_strings[i];
5308 it->multibyte_p = STRING_MULTIBYTE (it->string);
5309 SET_TEXT_POS (it->current.string_pos, 0, 0);
5310 it->method = GET_FROM_STRING;
5311 it->stop_charpos = 0;
5312 if (it->cmp_it.stop_pos >= 0)
5313 it->cmp_it.stop_pos = 0;
5314 it->prev_stop = 0;
5315 it->base_level_stop = 0;
5317 /* Set up the bidi iterator for this overlay string. */
5318 if (it->bidi_p)
5320 it->bidi_it.string.lstring = it->string;
5321 it->bidi_it.string.s = NULL;
5322 it->bidi_it.string.schars = SCHARS (it->string);
5323 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5324 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5325 it->bidi_it.string.unibyte = !it->multibyte_p;
5326 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5330 CHECK_IT (it);
5334 /* Compare two overlay_entry structures E1 and E2. Used as a
5335 comparison function for qsort in load_overlay_strings. Overlay
5336 strings for the same position are sorted so that
5338 1. All after-strings come in front of before-strings, except
5339 when they come from the same overlay.
5341 2. Within after-strings, strings are sorted so that overlay strings
5342 from overlays with higher priorities come first.
5344 2. Within before-strings, strings are sorted so that overlay
5345 strings from overlays with higher priorities come last.
5347 Value is analogous to strcmp. */
5350 static int
5351 compare_overlay_entries (const void *e1, const void *e2)
5353 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5354 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5355 int result;
5357 if (entry1->after_string_p != entry2->after_string_p)
5359 /* Let after-strings appear in front of before-strings if
5360 they come from different overlays. */
5361 if (EQ (entry1->overlay, entry2->overlay))
5362 result = entry1->after_string_p ? 1 : -1;
5363 else
5364 result = entry1->after_string_p ? -1 : 1;
5366 else if (entry1->priority != entry2->priority)
5368 if (entry1->after_string_p)
5369 /* After-strings sorted in order of decreasing priority. */
5370 result = entry2->priority < entry1->priority ? -1 : 1;
5371 else
5372 /* Before-strings sorted in order of increasing priority. */
5373 result = entry1->priority < entry2->priority ? -1 : 1;
5375 else
5376 result = 0;
5378 return result;
5382 /* Load the vector IT->overlay_strings with overlay strings from IT's
5383 current buffer position, or from CHARPOS if that is > 0. Set
5384 IT->n_overlays to the total number of overlay strings found.
5386 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5387 a time. On entry into load_overlay_strings,
5388 IT->current.overlay_string_index gives the number of overlay
5389 strings that have already been loaded by previous calls to this
5390 function.
5392 IT->add_overlay_start contains an additional overlay start
5393 position to consider for taking overlay strings from, if non-zero.
5394 This position comes into play when the overlay has an `invisible'
5395 property, and both before and after-strings. When we've skipped to
5396 the end of the overlay, because of its `invisible' property, we
5397 nevertheless want its before-string to appear.
5398 IT->add_overlay_start will contain the overlay start position
5399 in this case.
5401 Overlay strings are sorted so that after-string strings come in
5402 front of before-string strings. Within before and after-strings,
5403 strings are sorted by overlay priority. See also function
5404 compare_overlay_entries. */
5406 static void
5407 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5409 Lisp_Object overlay, window, str, invisible;
5410 struct Lisp_Overlay *ov;
5411 ptrdiff_t start, end;
5412 ptrdiff_t size = 20;
5413 ptrdiff_t n = 0, i, j;
5414 int invis_p;
5415 struct overlay_entry *entries = alloca (size * sizeof *entries);
5416 USE_SAFE_ALLOCA;
5418 if (charpos <= 0)
5419 charpos = IT_CHARPOS (*it);
5421 /* Append the overlay string STRING of overlay OVERLAY to vector
5422 `entries' which has size `size' and currently contains `n'
5423 elements. AFTER_P non-zero means STRING is an after-string of
5424 OVERLAY. */
5425 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5426 do \
5428 Lisp_Object priority; \
5430 if (n == size) \
5432 struct overlay_entry *old = entries; \
5433 SAFE_NALLOCA (entries, 2, size); \
5434 memcpy (entries, old, size * sizeof *entries); \
5435 size *= 2; \
5438 entries[n].string = (STRING); \
5439 entries[n].overlay = (OVERLAY); \
5440 priority = Foverlay_get ((OVERLAY), Qpriority); \
5441 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5442 entries[n].after_string_p = (AFTER_P); \
5443 ++n; \
5445 while (0)
5447 /* Process overlay before the overlay center. */
5448 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5450 XSETMISC (overlay, ov);
5451 eassert (OVERLAYP (overlay));
5452 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5453 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5455 if (end < charpos)
5456 break;
5458 /* Skip this overlay if it doesn't start or end at IT's current
5459 position. */
5460 if (end != charpos && start != charpos)
5461 continue;
5463 /* Skip this overlay if it doesn't apply to IT->w. */
5464 window = Foverlay_get (overlay, Qwindow);
5465 if (WINDOWP (window) && XWINDOW (window) != it->w)
5466 continue;
5468 /* If the text ``under'' the overlay is invisible, both before-
5469 and after-strings from this overlay are visible; start and
5470 end position are indistinguishable. */
5471 invisible = Foverlay_get (overlay, Qinvisible);
5472 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5474 /* If overlay has a non-empty before-string, record it. */
5475 if ((start == charpos || (end == charpos && invis_p))
5476 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5477 && SCHARS (str))
5478 RECORD_OVERLAY_STRING (overlay, str, 0);
5480 /* If overlay has a non-empty after-string, record it. */
5481 if ((end == charpos || (start == charpos && invis_p))
5482 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5483 && SCHARS (str))
5484 RECORD_OVERLAY_STRING (overlay, str, 1);
5487 /* Process overlays after the overlay center. */
5488 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5490 XSETMISC (overlay, ov);
5491 eassert (OVERLAYP (overlay));
5492 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5493 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5495 if (start > charpos)
5496 break;
5498 /* Skip this overlay if it doesn't start or end at IT's current
5499 position. */
5500 if (end != charpos && start != charpos)
5501 continue;
5503 /* Skip this overlay if it doesn't apply to IT->w. */
5504 window = Foverlay_get (overlay, Qwindow);
5505 if (WINDOWP (window) && XWINDOW (window) != it->w)
5506 continue;
5508 /* If the text ``under'' the overlay is invisible, it has a zero
5509 dimension, and both before- and after-strings apply. */
5510 invisible = Foverlay_get (overlay, Qinvisible);
5511 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5513 /* If overlay has a non-empty before-string, record it. */
5514 if ((start == charpos || (end == charpos && invis_p))
5515 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5516 && SCHARS (str))
5517 RECORD_OVERLAY_STRING (overlay, str, 0);
5519 /* If overlay has a non-empty after-string, record it. */
5520 if ((end == charpos || (start == charpos && invis_p))
5521 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5522 && SCHARS (str))
5523 RECORD_OVERLAY_STRING (overlay, str, 1);
5526 #undef RECORD_OVERLAY_STRING
5528 /* Sort entries. */
5529 if (n > 1)
5530 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5532 /* Record number of overlay strings, and where we computed it. */
5533 it->n_overlay_strings = n;
5534 it->overlay_strings_charpos = charpos;
5536 /* IT->current.overlay_string_index is the number of overlay strings
5537 that have already been consumed by IT. Copy some of the
5538 remaining overlay strings to IT->overlay_strings. */
5539 i = 0;
5540 j = it->current.overlay_string_index;
5541 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5543 it->overlay_strings[i] = entries[j].string;
5544 it->string_overlays[i++] = entries[j++].overlay;
5547 CHECK_IT (it);
5548 SAFE_FREE ();
5552 /* Get the first chunk of overlay strings at IT's current buffer
5553 position, or at CHARPOS if that is > 0. Value is non-zero if at
5554 least one overlay string was found. */
5556 static int
5557 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5559 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5560 process. This fills IT->overlay_strings with strings, and sets
5561 IT->n_overlay_strings to the total number of strings to process.
5562 IT->pos.overlay_string_index has to be set temporarily to zero
5563 because load_overlay_strings needs this; it must be set to -1
5564 when no overlay strings are found because a zero value would
5565 indicate a position in the first overlay string. */
5566 it->current.overlay_string_index = 0;
5567 load_overlay_strings (it, charpos);
5569 /* If we found overlay strings, set up IT to deliver display
5570 elements from the first one. Otherwise set up IT to deliver
5571 from current_buffer. */
5572 if (it->n_overlay_strings)
5574 /* Make sure we know settings in current_buffer, so that we can
5575 restore meaningful values when we're done with the overlay
5576 strings. */
5577 if (compute_stop_p)
5578 compute_stop_pos (it);
5579 eassert (it->face_id >= 0);
5581 /* Save IT's settings. They are restored after all overlay
5582 strings have been processed. */
5583 eassert (!compute_stop_p || it->sp == 0);
5585 /* When called from handle_stop, there might be an empty display
5586 string loaded. In that case, don't bother saving it. But
5587 don't use this optimization with the bidi iterator, since we
5588 need the corresponding pop_it call to resync the bidi
5589 iterator's position with IT's position, after we are done
5590 with the overlay strings. (The corresponding call to pop_it
5591 in case of an empty display string is in
5592 next_overlay_string.) */
5593 if (!(!it->bidi_p
5594 && STRINGP (it->string) && !SCHARS (it->string)))
5595 push_it (it, NULL);
5597 /* Set up IT to deliver display elements from the first overlay
5598 string. */
5599 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5600 it->string = it->overlay_strings[0];
5601 it->from_overlay = Qnil;
5602 it->stop_charpos = 0;
5603 eassert (STRINGP (it->string));
5604 it->end_charpos = SCHARS (it->string);
5605 it->prev_stop = 0;
5606 it->base_level_stop = 0;
5607 it->multibyte_p = STRING_MULTIBYTE (it->string);
5608 it->method = GET_FROM_STRING;
5609 it->from_disp_prop_p = 0;
5611 /* Force paragraph direction to be that of the parent
5612 buffer. */
5613 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5614 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5615 else
5616 it->paragraph_embedding = L2R;
5618 /* Set up the bidi iterator for this overlay string. */
5619 if (it->bidi_p)
5621 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5623 it->bidi_it.string.lstring = it->string;
5624 it->bidi_it.string.s = NULL;
5625 it->bidi_it.string.schars = SCHARS (it->string);
5626 it->bidi_it.string.bufpos = pos;
5627 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5628 it->bidi_it.string.unibyte = !it->multibyte_p;
5629 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5631 return 1;
5634 it->current.overlay_string_index = -1;
5635 return 0;
5638 static int
5639 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5641 it->string = Qnil;
5642 it->method = GET_FROM_BUFFER;
5644 (void) get_overlay_strings_1 (it, charpos, 1);
5646 CHECK_IT (it);
5648 /* Value is non-zero if we found at least one overlay string. */
5649 return STRINGP (it->string);
5654 /***********************************************************************
5655 Saving and restoring state
5656 ***********************************************************************/
5658 /* Save current settings of IT on IT->stack. Called, for example,
5659 before setting up IT for an overlay string, to be able to restore
5660 IT's settings to what they were after the overlay string has been
5661 processed. If POSITION is non-NULL, it is the position to save on
5662 the stack instead of IT->position. */
5664 static void
5665 push_it (struct it *it, struct text_pos *position)
5667 struct iterator_stack_entry *p;
5669 eassert (it->sp < IT_STACK_SIZE);
5670 p = it->stack + it->sp;
5672 p->stop_charpos = it->stop_charpos;
5673 p->prev_stop = it->prev_stop;
5674 p->base_level_stop = it->base_level_stop;
5675 p->cmp_it = it->cmp_it;
5676 eassert (it->face_id >= 0);
5677 p->face_id = it->face_id;
5678 p->string = it->string;
5679 p->method = it->method;
5680 p->from_overlay = it->from_overlay;
5681 switch (p->method)
5683 case GET_FROM_IMAGE:
5684 p->u.image.object = it->object;
5685 p->u.image.image_id = it->image_id;
5686 p->u.image.slice = it->slice;
5687 break;
5688 case GET_FROM_STRETCH:
5689 p->u.stretch.object = it->object;
5690 break;
5692 p->position = position ? *position : it->position;
5693 p->current = it->current;
5694 p->end_charpos = it->end_charpos;
5695 p->string_nchars = it->string_nchars;
5696 p->area = it->area;
5697 p->multibyte_p = it->multibyte_p;
5698 p->avoid_cursor_p = it->avoid_cursor_p;
5699 p->space_width = it->space_width;
5700 p->font_height = it->font_height;
5701 p->voffset = it->voffset;
5702 p->string_from_display_prop_p = it->string_from_display_prop_p;
5703 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5704 p->display_ellipsis_p = 0;
5705 p->line_wrap = it->line_wrap;
5706 p->bidi_p = it->bidi_p;
5707 p->paragraph_embedding = it->paragraph_embedding;
5708 p->from_disp_prop_p = it->from_disp_prop_p;
5709 ++it->sp;
5711 /* Save the state of the bidi iterator as well. */
5712 if (it->bidi_p)
5713 bidi_push_it (&it->bidi_it);
5716 static void
5717 iterate_out_of_display_property (struct it *it)
5719 int buffer_p = !STRINGP (it->string);
5720 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5721 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5723 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5725 /* Maybe initialize paragraph direction. If we are at the beginning
5726 of a new paragraph, next_element_from_buffer may not have a
5727 chance to do that. */
5728 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5729 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5730 /* prev_stop can be zero, so check against BEGV as well. */
5731 while (it->bidi_it.charpos >= bob
5732 && it->prev_stop <= it->bidi_it.charpos
5733 && it->bidi_it.charpos < CHARPOS (it->position)
5734 && it->bidi_it.charpos < eob)
5735 bidi_move_to_visually_next (&it->bidi_it);
5736 /* Record the stop_pos we just crossed, for when we cross it
5737 back, maybe. */
5738 if (it->bidi_it.charpos > CHARPOS (it->position))
5739 it->prev_stop = CHARPOS (it->position);
5740 /* If we ended up not where pop_it put us, resync IT's
5741 positional members with the bidi iterator. */
5742 if (it->bidi_it.charpos != CHARPOS (it->position))
5743 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5744 if (buffer_p)
5745 it->current.pos = it->position;
5746 else
5747 it->current.string_pos = it->position;
5750 /* Restore IT's settings from IT->stack. Called, for example, when no
5751 more overlay strings must be processed, and we return to delivering
5752 display elements from a buffer, or when the end of a string from a
5753 `display' property is reached and we return to delivering display
5754 elements from an overlay string, or from a buffer. */
5756 static void
5757 pop_it (struct it *it)
5759 struct iterator_stack_entry *p;
5760 int from_display_prop = it->from_disp_prop_p;
5762 eassert (it->sp > 0);
5763 --it->sp;
5764 p = it->stack + it->sp;
5765 it->stop_charpos = p->stop_charpos;
5766 it->prev_stop = p->prev_stop;
5767 it->base_level_stop = p->base_level_stop;
5768 it->cmp_it = p->cmp_it;
5769 it->face_id = p->face_id;
5770 it->current = p->current;
5771 it->position = p->position;
5772 it->string = p->string;
5773 it->from_overlay = p->from_overlay;
5774 if (NILP (it->string))
5775 SET_TEXT_POS (it->current.string_pos, -1, -1);
5776 it->method = p->method;
5777 switch (it->method)
5779 case GET_FROM_IMAGE:
5780 it->image_id = p->u.image.image_id;
5781 it->object = p->u.image.object;
5782 it->slice = p->u.image.slice;
5783 break;
5784 case GET_FROM_STRETCH:
5785 it->object = p->u.stretch.object;
5786 break;
5787 case GET_FROM_BUFFER:
5788 it->object = it->w->buffer;
5789 break;
5790 case GET_FROM_STRING:
5791 it->object = it->string;
5792 break;
5793 case GET_FROM_DISPLAY_VECTOR:
5794 if (it->s)
5795 it->method = GET_FROM_C_STRING;
5796 else if (STRINGP (it->string))
5797 it->method = GET_FROM_STRING;
5798 else
5800 it->method = GET_FROM_BUFFER;
5801 it->object = it->w->buffer;
5804 it->end_charpos = p->end_charpos;
5805 it->string_nchars = p->string_nchars;
5806 it->area = p->area;
5807 it->multibyte_p = p->multibyte_p;
5808 it->avoid_cursor_p = p->avoid_cursor_p;
5809 it->space_width = p->space_width;
5810 it->font_height = p->font_height;
5811 it->voffset = p->voffset;
5812 it->string_from_display_prop_p = p->string_from_display_prop_p;
5813 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5814 it->line_wrap = p->line_wrap;
5815 it->bidi_p = p->bidi_p;
5816 it->paragraph_embedding = p->paragraph_embedding;
5817 it->from_disp_prop_p = p->from_disp_prop_p;
5818 if (it->bidi_p)
5820 bidi_pop_it (&it->bidi_it);
5821 /* Bidi-iterate until we get out of the portion of text, if any,
5822 covered by a `display' text property or by an overlay with
5823 `display' property. (We cannot just jump there, because the
5824 internal coherency of the bidi iterator state can not be
5825 preserved across such jumps.) We also must determine the
5826 paragraph base direction if the overlay we just processed is
5827 at the beginning of a new paragraph. */
5828 if (from_display_prop
5829 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5830 iterate_out_of_display_property (it);
5832 eassert ((BUFFERP (it->object)
5833 && IT_CHARPOS (*it) == it->bidi_it.charpos
5834 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5835 || (STRINGP (it->object)
5836 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5837 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5838 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5844 /***********************************************************************
5845 Moving over lines
5846 ***********************************************************************/
5848 /* Set IT's current position to the previous line start. */
5850 static void
5851 back_to_previous_line_start (struct it *it)
5853 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5854 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5858 /* Move IT to the next line start.
5860 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5861 we skipped over part of the text (as opposed to moving the iterator
5862 continuously over the text). Otherwise, don't change the value
5863 of *SKIPPED_P.
5865 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5866 iterator on the newline, if it was found.
5868 Newlines may come from buffer text, overlay strings, or strings
5869 displayed via the `display' property. That's the reason we can't
5870 simply use find_next_newline_no_quit.
5872 Note that this function may not skip over invisible text that is so
5873 because of text properties and immediately follows a newline. If
5874 it would, function reseat_at_next_visible_line_start, when called
5875 from set_iterator_to_next, would effectively make invisible
5876 characters following a newline part of the wrong glyph row, which
5877 leads to wrong cursor motion. */
5879 static int
5880 forward_to_next_line_start (struct it *it, int *skipped_p,
5881 struct bidi_it *bidi_it_prev)
5883 ptrdiff_t old_selective;
5884 int newline_found_p, n;
5885 const int MAX_NEWLINE_DISTANCE = 500;
5887 /* If already on a newline, just consume it to avoid unintended
5888 skipping over invisible text below. */
5889 if (it->what == IT_CHARACTER
5890 && it->c == '\n'
5891 && CHARPOS (it->position) == IT_CHARPOS (*it))
5893 if (it->bidi_p && bidi_it_prev)
5894 *bidi_it_prev = it->bidi_it;
5895 set_iterator_to_next (it, 0);
5896 it->c = 0;
5897 return 1;
5900 /* Don't handle selective display in the following. It's (a)
5901 unnecessary because it's done by the caller, and (b) leads to an
5902 infinite recursion because next_element_from_ellipsis indirectly
5903 calls this function. */
5904 old_selective = it->selective;
5905 it->selective = 0;
5907 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5908 from buffer text. */
5909 for (n = newline_found_p = 0;
5910 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5911 n += STRINGP (it->string) ? 0 : 1)
5913 if (!get_next_display_element (it))
5914 return 0;
5915 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5916 if (newline_found_p && it->bidi_p && bidi_it_prev)
5917 *bidi_it_prev = it->bidi_it;
5918 set_iterator_to_next (it, 0);
5921 /* If we didn't find a newline near enough, see if we can use a
5922 short-cut. */
5923 if (!newline_found_p)
5925 ptrdiff_t start = IT_CHARPOS (*it);
5926 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5927 Lisp_Object pos;
5929 eassert (!STRINGP (it->string));
5931 /* If there isn't any `display' property in sight, and no
5932 overlays, we can just use the position of the newline in
5933 buffer text. */
5934 if (it->stop_charpos >= limit
5935 || ((pos = Fnext_single_property_change (make_number (start),
5936 Qdisplay, Qnil,
5937 make_number (limit)),
5938 NILP (pos))
5939 && next_overlay_change (start) == ZV))
5941 if (!it->bidi_p)
5943 IT_CHARPOS (*it) = limit;
5944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5946 else
5948 struct bidi_it bprev;
5950 /* Help bidi.c avoid expensive searches for display
5951 properties and overlays, by telling it that there are
5952 none up to `limit'. */
5953 if (it->bidi_it.disp_pos < limit)
5955 it->bidi_it.disp_pos = limit;
5956 it->bidi_it.disp_prop = 0;
5958 do {
5959 bprev = it->bidi_it;
5960 bidi_move_to_visually_next (&it->bidi_it);
5961 } while (it->bidi_it.charpos != limit);
5962 IT_CHARPOS (*it) = limit;
5963 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5964 if (bidi_it_prev)
5965 *bidi_it_prev = bprev;
5967 *skipped_p = newline_found_p = 1;
5969 else
5971 while (get_next_display_element (it)
5972 && !newline_found_p)
5974 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5975 if (newline_found_p && it->bidi_p && bidi_it_prev)
5976 *bidi_it_prev = it->bidi_it;
5977 set_iterator_to_next (it, 0);
5982 it->selective = old_selective;
5983 return newline_found_p;
5987 /* Set IT's current position to the previous visible line start. Skip
5988 invisible text that is so either due to text properties or due to
5989 selective display. Caution: this does not change IT->current_x and
5990 IT->hpos. */
5992 static void
5993 back_to_previous_visible_line_start (struct it *it)
5995 while (IT_CHARPOS (*it) > BEGV)
5997 back_to_previous_line_start (it);
5999 if (IT_CHARPOS (*it) <= BEGV)
6000 break;
6002 /* If selective > 0, then lines indented more than its value are
6003 invisible. */
6004 if (it->selective > 0
6005 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6006 it->selective))
6007 continue;
6009 /* Check the newline before point for invisibility. */
6011 Lisp_Object prop;
6012 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6013 Qinvisible, it->window);
6014 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6015 continue;
6018 if (IT_CHARPOS (*it) <= BEGV)
6019 break;
6022 struct it it2;
6023 void *it2data = NULL;
6024 ptrdiff_t pos;
6025 ptrdiff_t beg, end;
6026 Lisp_Object val, overlay;
6028 SAVE_IT (it2, *it, it2data);
6030 /* If newline is part of a composition, continue from start of composition */
6031 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6032 && beg < IT_CHARPOS (*it))
6033 goto replaced;
6035 /* If newline is replaced by a display property, find start of overlay
6036 or interval and continue search from that point. */
6037 pos = --IT_CHARPOS (it2);
6038 --IT_BYTEPOS (it2);
6039 it2.sp = 0;
6040 bidi_unshelve_cache (NULL, 0);
6041 it2.string_from_display_prop_p = 0;
6042 it2.from_disp_prop_p = 0;
6043 if (handle_display_prop (&it2) == HANDLED_RETURN
6044 && !NILP (val = get_char_property_and_overlay
6045 (make_number (pos), Qdisplay, Qnil, &overlay))
6046 && (OVERLAYP (overlay)
6047 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6048 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6050 RESTORE_IT (it, it, it2data);
6051 goto replaced;
6054 /* Newline is not replaced by anything -- so we are done. */
6055 RESTORE_IT (it, it, it2data);
6056 break;
6058 replaced:
6059 if (beg < BEGV)
6060 beg = BEGV;
6061 IT_CHARPOS (*it) = beg;
6062 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6066 it->continuation_lines_width = 0;
6068 eassert (IT_CHARPOS (*it) >= BEGV);
6069 eassert (IT_CHARPOS (*it) == BEGV
6070 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6071 CHECK_IT (it);
6075 /* Reseat iterator IT at the previous visible line start. Skip
6076 invisible text that is so either due to text properties or due to
6077 selective display. At the end, update IT's overlay information,
6078 face information etc. */
6080 void
6081 reseat_at_previous_visible_line_start (struct it *it)
6083 back_to_previous_visible_line_start (it);
6084 reseat (it, it->current.pos, 1);
6085 CHECK_IT (it);
6089 /* Reseat iterator IT on the next visible line start in the current
6090 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6091 preceding the line start. Skip over invisible text that is so
6092 because of selective display. Compute faces, overlays etc at the
6093 new position. Note that this function does not skip over text that
6094 is invisible because of text properties. */
6096 static void
6097 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6099 int newline_found_p, skipped_p = 0;
6100 struct bidi_it bidi_it_prev;
6102 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6104 /* Skip over lines that are invisible because they are indented
6105 more than the value of IT->selective. */
6106 if (it->selective > 0)
6107 while (IT_CHARPOS (*it) < ZV
6108 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6109 it->selective))
6111 eassert (IT_BYTEPOS (*it) == BEGV
6112 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6113 newline_found_p =
6114 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6117 /* Position on the newline if that's what's requested. */
6118 if (on_newline_p && newline_found_p)
6120 if (STRINGP (it->string))
6122 if (IT_STRING_CHARPOS (*it) > 0)
6124 if (!it->bidi_p)
6126 --IT_STRING_CHARPOS (*it);
6127 --IT_STRING_BYTEPOS (*it);
6129 else
6131 /* We need to restore the bidi iterator to the state
6132 it had on the newline, and resync the IT's
6133 position with that. */
6134 it->bidi_it = bidi_it_prev;
6135 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6136 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6140 else if (IT_CHARPOS (*it) > BEGV)
6142 if (!it->bidi_p)
6144 --IT_CHARPOS (*it);
6145 --IT_BYTEPOS (*it);
6147 else
6149 /* We need to restore the bidi iterator to the state it
6150 had on the newline and resync IT with that. */
6151 it->bidi_it = bidi_it_prev;
6152 IT_CHARPOS (*it) = it->bidi_it.charpos;
6153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6155 reseat (it, it->current.pos, 0);
6158 else if (skipped_p)
6159 reseat (it, it->current.pos, 0);
6161 CHECK_IT (it);
6166 /***********************************************************************
6167 Changing an iterator's position
6168 ***********************************************************************/
6170 /* Change IT's current position to POS in current_buffer. If FORCE_P
6171 is non-zero, always check for text properties at the new position.
6172 Otherwise, text properties are only looked up if POS >=
6173 IT->check_charpos of a property. */
6175 static void
6176 reseat (struct it *it, struct text_pos pos, int force_p)
6178 ptrdiff_t original_pos = IT_CHARPOS (*it);
6180 reseat_1 (it, pos, 0);
6182 /* Determine where to check text properties. Avoid doing it
6183 where possible because text property lookup is very expensive. */
6184 if (force_p
6185 || CHARPOS (pos) > it->stop_charpos
6186 || CHARPOS (pos) < original_pos)
6188 if (it->bidi_p)
6190 /* For bidi iteration, we need to prime prev_stop and
6191 base_level_stop with our best estimations. */
6192 /* Implementation note: Of course, POS is not necessarily a
6193 stop position, so assigning prev_pos to it is a lie; we
6194 should have called compute_stop_backwards. However, if
6195 the current buffer does not include any R2L characters,
6196 that call would be a waste of cycles, because the
6197 iterator will never move back, and thus never cross this
6198 "fake" stop position. So we delay that backward search
6199 until the time we really need it, in next_element_from_buffer. */
6200 if (CHARPOS (pos) != it->prev_stop)
6201 it->prev_stop = CHARPOS (pos);
6202 if (CHARPOS (pos) < it->base_level_stop)
6203 it->base_level_stop = 0; /* meaning it's unknown */
6204 handle_stop (it);
6206 else
6208 handle_stop (it);
6209 it->prev_stop = it->base_level_stop = 0;
6214 CHECK_IT (it);
6218 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6219 IT->stop_pos to POS, also. */
6221 static void
6222 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6224 /* Don't call this function when scanning a C string. */
6225 eassert (it->s == NULL);
6227 /* POS must be a reasonable value. */
6228 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6230 it->current.pos = it->position = pos;
6231 it->end_charpos = ZV;
6232 it->dpvec = NULL;
6233 it->current.dpvec_index = -1;
6234 it->current.overlay_string_index = -1;
6235 IT_STRING_CHARPOS (*it) = -1;
6236 IT_STRING_BYTEPOS (*it) = -1;
6237 it->string = Qnil;
6238 it->method = GET_FROM_BUFFER;
6239 it->object = it->w->buffer;
6240 it->area = TEXT_AREA;
6241 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6242 it->sp = 0;
6243 it->string_from_display_prop_p = 0;
6244 it->string_from_prefix_prop_p = 0;
6246 it->from_disp_prop_p = 0;
6247 it->face_before_selective_p = 0;
6248 if (it->bidi_p)
6250 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6251 &it->bidi_it);
6252 bidi_unshelve_cache (NULL, 0);
6253 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6254 it->bidi_it.string.s = NULL;
6255 it->bidi_it.string.lstring = Qnil;
6256 it->bidi_it.string.bufpos = 0;
6257 it->bidi_it.string.unibyte = 0;
6260 if (set_stop_p)
6262 it->stop_charpos = CHARPOS (pos);
6263 it->base_level_stop = CHARPOS (pos);
6268 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6269 If S is non-null, it is a C string to iterate over. Otherwise,
6270 STRING gives a Lisp string to iterate over.
6272 If PRECISION > 0, don't return more then PRECISION number of
6273 characters from the string.
6275 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6276 characters have been returned. FIELD_WIDTH < 0 means an infinite
6277 field width.
6279 MULTIBYTE = 0 means disable processing of multibyte characters,
6280 MULTIBYTE > 0 means enable it,
6281 MULTIBYTE < 0 means use IT->multibyte_p.
6283 IT must be initialized via a prior call to init_iterator before
6284 calling this function. */
6286 static void
6287 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6288 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6289 int multibyte)
6291 /* No region in strings. */
6292 it->region_beg_charpos = it->region_end_charpos = -1;
6294 /* No text property checks performed by default, but see below. */
6295 it->stop_charpos = -1;
6297 /* Set iterator position and end position. */
6298 memset (&it->current, 0, sizeof it->current);
6299 it->current.overlay_string_index = -1;
6300 it->current.dpvec_index = -1;
6301 eassert (charpos >= 0);
6303 /* If STRING is specified, use its multibyteness, otherwise use the
6304 setting of MULTIBYTE, if specified. */
6305 if (multibyte >= 0)
6306 it->multibyte_p = multibyte > 0;
6308 /* Bidirectional reordering of strings is controlled by the default
6309 value of bidi-display-reordering. Don't try to reorder while
6310 loading loadup.el, as the necessary character property tables are
6311 not yet available. */
6312 it->bidi_p =
6313 NILP (Vpurify_flag)
6314 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6316 if (s == NULL)
6318 eassert (STRINGP (string));
6319 it->string = string;
6320 it->s = NULL;
6321 it->end_charpos = it->string_nchars = SCHARS (string);
6322 it->method = GET_FROM_STRING;
6323 it->current.string_pos = string_pos (charpos, string);
6325 if (it->bidi_p)
6327 it->bidi_it.string.lstring = string;
6328 it->bidi_it.string.s = NULL;
6329 it->bidi_it.string.schars = it->end_charpos;
6330 it->bidi_it.string.bufpos = 0;
6331 it->bidi_it.string.from_disp_str = 0;
6332 it->bidi_it.string.unibyte = !it->multibyte_p;
6333 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6334 FRAME_WINDOW_P (it->f), &it->bidi_it);
6337 else
6339 it->s = (const unsigned char *) s;
6340 it->string = Qnil;
6342 /* Note that we use IT->current.pos, not it->current.string_pos,
6343 for displaying C strings. */
6344 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6345 if (it->multibyte_p)
6347 it->current.pos = c_string_pos (charpos, s, 1);
6348 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6350 else
6352 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6353 it->end_charpos = it->string_nchars = strlen (s);
6356 if (it->bidi_p)
6358 it->bidi_it.string.lstring = Qnil;
6359 it->bidi_it.string.s = (const unsigned char *) s;
6360 it->bidi_it.string.schars = it->end_charpos;
6361 it->bidi_it.string.bufpos = 0;
6362 it->bidi_it.string.from_disp_str = 0;
6363 it->bidi_it.string.unibyte = !it->multibyte_p;
6364 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6365 &it->bidi_it);
6367 it->method = GET_FROM_C_STRING;
6370 /* PRECISION > 0 means don't return more than PRECISION characters
6371 from the string. */
6372 if (precision > 0 && it->end_charpos - charpos > precision)
6374 it->end_charpos = it->string_nchars = charpos + precision;
6375 if (it->bidi_p)
6376 it->bidi_it.string.schars = it->end_charpos;
6379 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6380 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6381 FIELD_WIDTH < 0 means infinite field width. This is useful for
6382 padding with `-' at the end of a mode line. */
6383 if (field_width < 0)
6384 field_width = INFINITY;
6385 /* Implementation note: We deliberately don't enlarge
6386 it->bidi_it.string.schars here to fit it->end_charpos, because
6387 the bidi iterator cannot produce characters out of thin air. */
6388 if (field_width > it->end_charpos - charpos)
6389 it->end_charpos = charpos + field_width;
6391 /* Use the standard display table for displaying strings. */
6392 if (DISP_TABLE_P (Vstandard_display_table))
6393 it->dp = XCHAR_TABLE (Vstandard_display_table);
6395 it->stop_charpos = charpos;
6396 it->prev_stop = charpos;
6397 it->base_level_stop = 0;
6398 if (it->bidi_p)
6400 it->bidi_it.first_elt = 1;
6401 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6402 it->bidi_it.disp_pos = -1;
6404 if (s == NULL && it->multibyte_p)
6406 ptrdiff_t endpos = SCHARS (it->string);
6407 if (endpos > it->end_charpos)
6408 endpos = it->end_charpos;
6409 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6410 it->string);
6412 CHECK_IT (it);
6417 /***********************************************************************
6418 Iteration
6419 ***********************************************************************/
6421 /* Map enum it_method value to corresponding next_element_from_* function. */
6423 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6425 next_element_from_buffer,
6426 next_element_from_display_vector,
6427 next_element_from_string,
6428 next_element_from_c_string,
6429 next_element_from_image,
6430 next_element_from_stretch
6433 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6436 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6437 (possibly with the following characters). */
6439 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6440 ((IT)->cmp_it.id >= 0 \
6441 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6442 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6443 END_CHARPOS, (IT)->w, \
6444 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6445 (IT)->string)))
6448 /* Lookup the char-table Vglyphless_char_display for character C (-1
6449 if we want information for no-font case), and return the display
6450 method symbol. By side-effect, update it->what and
6451 it->glyphless_method. This function is called from
6452 get_next_display_element for each character element, and from
6453 x_produce_glyphs when no suitable font was found. */
6455 Lisp_Object
6456 lookup_glyphless_char_display (int c, struct it *it)
6458 Lisp_Object glyphless_method = Qnil;
6460 if (CHAR_TABLE_P (Vglyphless_char_display)
6461 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6463 if (c >= 0)
6465 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6466 if (CONSP (glyphless_method))
6467 glyphless_method = FRAME_WINDOW_P (it->f)
6468 ? XCAR (glyphless_method)
6469 : XCDR (glyphless_method);
6471 else
6472 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6475 retry:
6476 if (NILP (glyphless_method))
6478 if (c >= 0)
6479 /* The default is to display the character by a proper font. */
6480 return Qnil;
6481 /* The default for the no-font case is to display an empty box. */
6482 glyphless_method = Qempty_box;
6484 if (EQ (glyphless_method, Qzero_width))
6486 if (c >= 0)
6487 return glyphless_method;
6488 /* This method can't be used for the no-font case. */
6489 glyphless_method = Qempty_box;
6491 if (EQ (glyphless_method, Qthin_space))
6492 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6493 else if (EQ (glyphless_method, Qempty_box))
6494 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6495 else if (EQ (glyphless_method, Qhex_code))
6496 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6497 else if (STRINGP (glyphless_method))
6498 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6499 else
6501 /* Invalid value. We use the default method. */
6502 glyphless_method = Qnil;
6503 goto retry;
6505 it->what = IT_GLYPHLESS;
6506 return glyphless_method;
6509 /* Load IT's display element fields with information about the next
6510 display element from the current position of IT. Value is zero if
6511 end of buffer (or C string) is reached. */
6513 static struct frame *last_escape_glyph_frame = NULL;
6514 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6515 static int last_escape_glyph_merged_face_id = 0;
6517 struct frame *last_glyphless_glyph_frame = NULL;
6518 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6519 int last_glyphless_glyph_merged_face_id = 0;
6521 static int
6522 get_next_display_element (struct it *it)
6524 /* Non-zero means that we found a display element. Zero means that
6525 we hit the end of what we iterate over. Performance note: the
6526 function pointer `method' used here turns out to be faster than
6527 using a sequence of if-statements. */
6528 int success_p;
6530 get_next:
6531 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6533 if (it->what == IT_CHARACTER)
6535 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6536 and only if (a) the resolved directionality of that character
6537 is R..." */
6538 /* FIXME: Do we need an exception for characters from display
6539 tables? */
6540 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6541 it->c = bidi_mirror_char (it->c);
6542 /* Map via display table or translate control characters.
6543 IT->c, IT->len etc. have been set to the next character by
6544 the function call above. If we have a display table, and it
6545 contains an entry for IT->c, translate it. Don't do this if
6546 IT->c itself comes from a display table, otherwise we could
6547 end up in an infinite recursion. (An alternative could be to
6548 count the recursion depth of this function and signal an
6549 error when a certain maximum depth is reached.) Is it worth
6550 it? */
6551 if (success_p && it->dpvec == NULL)
6553 Lisp_Object dv;
6554 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6555 int nonascii_space_p = 0;
6556 int nonascii_hyphen_p = 0;
6557 int c = it->c; /* This is the character to display. */
6559 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6561 eassert (SINGLE_BYTE_CHAR_P (c));
6562 if (unibyte_display_via_language_environment)
6564 c = DECODE_CHAR (unibyte, c);
6565 if (c < 0)
6566 c = BYTE8_TO_CHAR (it->c);
6568 else
6569 c = BYTE8_TO_CHAR (it->c);
6572 if (it->dp
6573 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6574 VECTORP (dv)))
6576 struct Lisp_Vector *v = XVECTOR (dv);
6578 /* Return the first character from the display table
6579 entry, if not empty. If empty, don't display the
6580 current character. */
6581 if (v->header.size)
6583 it->dpvec_char_len = it->len;
6584 it->dpvec = v->contents;
6585 it->dpend = v->contents + v->header.size;
6586 it->current.dpvec_index = 0;
6587 it->dpvec_face_id = -1;
6588 it->saved_face_id = it->face_id;
6589 it->method = GET_FROM_DISPLAY_VECTOR;
6590 it->ellipsis_p = 0;
6592 else
6594 set_iterator_to_next (it, 0);
6596 goto get_next;
6599 if (! NILP (lookup_glyphless_char_display (c, it)))
6601 if (it->what == IT_GLYPHLESS)
6602 goto done;
6603 /* Don't display this character. */
6604 set_iterator_to_next (it, 0);
6605 goto get_next;
6608 /* If `nobreak-char-display' is non-nil, we display
6609 non-ASCII spaces and hyphens specially. */
6610 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6612 if (c == 0xA0)
6613 nonascii_space_p = 1;
6614 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6615 nonascii_hyphen_p = 1;
6618 /* Translate control characters into `\003' or `^C' form.
6619 Control characters coming from a display table entry are
6620 currently not translated because we use IT->dpvec to hold
6621 the translation. This could easily be changed but I
6622 don't believe that it is worth doing.
6624 The characters handled by `nobreak-char-display' must be
6625 translated too.
6627 Non-printable characters and raw-byte characters are also
6628 translated to octal form. */
6629 if (((c < ' ' || c == 127) /* ASCII control chars */
6630 ? (it->area != TEXT_AREA
6631 /* In mode line, treat \n, \t like other crl chars. */
6632 || (c != '\t'
6633 && it->glyph_row
6634 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6635 || (c != '\n' && c != '\t'))
6636 : (nonascii_space_p
6637 || nonascii_hyphen_p
6638 || CHAR_BYTE8_P (c)
6639 || ! CHAR_PRINTABLE_P (c))))
6641 /* C is a control character, non-ASCII space/hyphen,
6642 raw-byte, or a non-printable character which must be
6643 displayed either as '\003' or as `^C' where the '\\'
6644 and '^' can be defined in the display table. Fill
6645 IT->ctl_chars with glyphs for what we have to
6646 display. Then, set IT->dpvec to these glyphs. */
6647 Lisp_Object gc;
6648 int ctl_len;
6649 int face_id;
6650 int lface_id = 0;
6651 int escape_glyph;
6653 /* Handle control characters with ^. */
6655 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6657 int g;
6659 g = '^'; /* default glyph for Control */
6660 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6661 if (it->dp
6662 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6664 g = GLYPH_CODE_CHAR (gc);
6665 lface_id = GLYPH_CODE_FACE (gc);
6667 if (lface_id)
6669 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6671 else if (it->f == last_escape_glyph_frame
6672 && it->face_id == last_escape_glyph_face_id)
6674 face_id = last_escape_glyph_merged_face_id;
6676 else
6678 /* Merge the escape-glyph face into the current face. */
6679 face_id = merge_faces (it->f, Qescape_glyph, 0,
6680 it->face_id);
6681 last_escape_glyph_frame = it->f;
6682 last_escape_glyph_face_id = it->face_id;
6683 last_escape_glyph_merged_face_id = face_id;
6686 XSETINT (it->ctl_chars[0], g);
6687 XSETINT (it->ctl_chars[1], c ^ 0100);
6688 ctl_len = 2;
6689 goto display_control;
6692 /* Handle non-ascii space in the mode where it only gets
6693 highlighting. */
6695 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6697 /* Merge `nobreak-space' into the current face. */
6698 face_id = merge_faces (it->f, Qnobreak_space, 0,
6699 it->face_id);
6700 XSETINT (it->ctl_chars[0], ' ');
6701 ctl_len = 1;
6702 goto display_control;
6705 /* Handle sequences that start with the "escape glyph". */
6707 /* the default escape glyph is \. */
6708 escape_glyph = '\\';
6710 if (it->dp
6711 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6713 escape_glyph = GLYPH_CODE_CHAR (gc);
6714 lface_id = GLYPH_CODE_FACE (gc);
6716 if (lface_id)
6718 /* The display table specified a face.
6719 Merge it into face_id and also into escape_glyph. */
6720 face_id = merge_faces (it->f, Qt, lface_id,
6721 it->face_id);
6723 else if (it->f == last_escape_glyph_frame
6724 && it->face_id == last_escape_glyph_face_id)
6726 face_id = last_escape_glyph_merged_face_id;
6728 else
6730 /* Merge the escape-glyph face into the current face. */
6731 face_id = merge_faces (it->f, Qescape_glyph, 0,
6732 it->face_id);
6733 last_escape_glyph_frame = it->f;
6734 last_escape_glyph_face_id = it->face_id;
6735 last_escape_glyph_merged_face_id = face_id;
6738 /* Draw non-ASCII hyphen with just highlighting: */
6740 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6742 XSETINT (it->ctl_chars[0], '-');
6743 ctl_len = 1;
6744 goto display_control;
6747 /* Draw non-ASCII space/hyphen with escape glyph: */
6749 if (nonascii_space_p || nonascii_hyphen_p)
6751 XSETINT (it->ctl_chars[0], escape_glyph);
6752 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6753 ctl_len = 2;
6754 goto display_control;
6758 char str[10];
6759 int len, i;
6761 if (CHAR_BYTE8_P (c))
6762 /* Display \200 instead of \17777600. */
6763 c = CHAR_TO_BYTE8 (c);
6764 len = sprintf (str, "%03o", c);
6766 XSETINT (it->ctl_chars[0], escape_glyph);
6767 for (i = 0; i < len; i++)
6768 XSETINT (it->ctl_chars[i + 1], str[i]);
6769 ctl_len = len + 1;
6772 display_control:
6773 /* Set up IT->dpvec and return first character from it. */
6774 it->dpvec_char_len = it->len;
6775 it->dpvec = it->ctl_chars;
6776 it->dpend = it->dpvec + ctl_len;
6777 it->current.dpvec_index = 0;
6778 it->dpvec_face_id = face_id;
6779 it->saved_face_id = it->face_id;
6780 it->method = GET_FROM_DISPLAY_VECTOR;
6781 it->ellipsis_p = 0;
6782 goto get_next;
6784 it->char_to_display = c;
6786 else if (success_p)
6788 it->char_to_display = it->c;
6792 /* Adjust face id for a multibyte character. There are no multibyte
6793 character in unibyte text. */
6794 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6795 && it->multibyte_p
6796 && success_p
6797 && FRAME_WINDOW_P (it->f))
6799 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6801 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6803 /* Automatic composition with glyph-string. */
6804 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6806 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6808 else
6810 ptrdiff_t pos = (it->s ? -1
6811 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6812 : IT_CHARPOS (*it));
6813 int c;
6815 if (it->what == IT_CHARACTER)
6816 c = it->char_to_display;
6817 else
6819 struct composition *cmp = composition_table[it->cmp_it.id];
6820 int i;
6822 c = ' ';
6823 for (i = 0; i < cmp->glyph_len; i++)
6824 /* TAB in a composition means display glyphs with
6825 padding space on the left or right. */
6826 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6827 break;
6829 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6833 done:
6834 /* Is this character the last one of a run of characters with
6835 box? If yes, set IT->end_of_box_run_p to 1. */
6836 if (it->face_box_p
6837 && it->s == NULL)
6839 if (it->method == GET_FROM_STRING && it->sp)
6841 int face_id = underlying_face_id (it);
6842 struct face *face = FACE_FROM_ID (it->f, face_id);
6844 if (face)
6846 if (face->box == FACE_NO_BOX)
6848 /* If the box comes from face properties in a
6849 display string, check faces in that string. */
6850 int string_face_id = face_after_it_pos (it);
6851 it->end_of_box_run_p
6852 = (FACE_FROM_ID (it->f, string_face_id)->box
6853 == FACE_NO_BOX);
6855 /* Otherwise, the box comes from the underlying face.
6856 If this is the last string character displayed, check
6857 the next buffer location. */
6858 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6859 && (it->current.overlay_string_index
6860 == it->n_overlay_strings - 1))
6862 ptrdiff_t ignore;
6863 int next_face_id;
6864 struct text_pos pos = it->current.pos;
6865 INC_TEXT_POS (pos, it->multibyte_p);
6867 next_face_id = face_at_buffer_position
6868 (it->w, CHARPOS (pos), it->region_beg_charpos,
6869 it->region_end_charpos, &ignore,
6870 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6871 -1);
6872 it->end_of_box_run_p
6873 = (FACE_FROM_ID (it->f, next_face_id)->box
6874 == FACE_NO_BOX);
6878 else
6880 int face_id = face_after_it_pos (it);
6881 it->end_of_box_run_p
6882 = (face_id != it->face_id
6883 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6886 /* If we reached the end of the object we've been iterating (e.g., a
6887 display string or an overlay string), and there's something on
6888 IT->stack, proceed with what's on the stack. It doesn't make
6889 sense to return zero if there's unprocessed stuff on the stack,
6890 because otherwise that stuff will never be displayed. */
6891 if (!success_p && it->sp > 0)
6893 set_iterator_to_next (it, 0);
6894 success_p = get_next_display_element (it);
6897 /* Value is 0 if end of buffer or string reached. */
6898 return success_p;
6902 /* Move IT to the next display element.
6904 RESEAT_P non-zero means if called on a newline in buffer text,
6905 skip to the next visible line start.
6907 Functions get_next_display_element and set_iterator_to_next are
6908 separate because I find this arrangement easier to handle than a
6909 get_next_display_element function that also increments IT's
6910 position. The way it is we can first look at an iterator's current
6911 display element, decide whether it fits on a line, and if it does,
6912 increment the iterator position. The other way around we probably
6913 would either need a flag indicating whether the iterator has to be
6914 incremented the next time, or we would have to implement a
6915 decrement position function which would not be easy to write. */
6917 void
6918 set_iterator_to_next (struct it *it, int reseat_p)
6920 /* Reset flags indicating start and end of a sequence of characters
6921 with box. Reset them at the start of this function because
6922 moving the iterator to a new position might set them. */
6923 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6925 switch (it->method)
6927 case GET_FROM_BUFFER:
6928 /* The current display element of IT is a character from
6929 current_buffer. Advance in the buffer, and maybe skip over
6930 invisible lines that are so because of selective display. */
6931 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6932 reseat_at_next_visible_line_start (it, 0);
6933 else if (it->cmp_it.id >= 0)
6935 /* We are currently getting glyphs from a composition. */
6936 int i;
6938 if (! it->bidi_p)
6940 IT_CHARPOS (*it) += it->cmp_it.nchars;
6941 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6942 if (it->cmp_it.to < it->cmp_it.nglyphs)
6944 it->cmp_it.from = it->cmp_it.to;
6946 else
6948 it->cmp_it.id = -1;
6949 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6950 IT_BYTEPOS (*it),
6951 it->end_charpos, Qnil);
6954 else if (! it->cmp_it.reversed_p)
6956 /* Composition created while scanning forward. */
6957 /* Update IT's char/byte positions to point to the first
6958 character of the next grapheme cluster, or to the
6959 character visually after the current composition. */
6960 for (i = 0; i < it->cmp_it.nchars; i++)
6961 bidi_move_to_visually_next (&it->bidi_it);
6962 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6963 IT_CHARPOS (*it) = it->bidi_it.charpos;
6965 if (it->cmp_it.to < it->cmp_it.nglyphs)
6967 /* Proceed to the next grapheme cluster. */
6968 it->cmp_it.from = it->cmp_it.to;
6970 else
6972 /* No more grapheme clusters in this composition.
6973 Find the next stop position. */
6974 ptrdiff_t stop = it->end_charpos;
6975 if (it->bidi_it.scan_dir < 0)
6976 /* Now we are scanning backward and don't know
6977 where to stop. */
6978 stop = -1;
6979 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6980 IT_BYTEPOS (*it), stop, Qnil);
6983 else
6985 /* Composition created while scanning backward. */
6986 /* Update IT's char/byte positions to point to the last
6987 character of the previous grapheme cluster, or the
6988 character visually after the current composition. */
6989 for (i = 0; i < it->cmp_it.nchars; i++)
6990 bidi_move_to_visually_next (&it->bidi_it);
6991 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6992 IT_CHARPOS (*it) = it->bidi_it.charpos;
6993 if (it->cmp_it.from > 0)
6995 /* Proceed to the previous grapheme cluster. */
6996 it->cmp_it.to = it->cmp_it.from;
6998 else
7000 /* No more grapheme clusters in this composition.
7001 Find the next stop position. */
7002 ptrdiff_t stop = it->end_charpos;
7003 if (it->bidi_it.scan_dir < 0)
7004 /* Now we are scanning backward and don't know
7005 where to stop. */
7006 stop = -1;
7007 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7008 IT_BYTEPOS (*it), stop, Qnil);
7012 else
7014 eassert (it->len != 0);
7016 if (!it->bidi_p)
7018 IT_BYTEPOS (*it) += it->len;
7019 IT_CHARPOS (*it) += 1;
7021 else
7023 int prev_scan_dir = it->bidi_it.scan_dir;
7024 /* If this is a new paragraph, determine its base
7025 direction (a.k.a. its base embedding level). */
7026 if (it->bidi_it.new_paragraph)
7027 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7028 bidi_move_to_visually_next (&it->bidi_it);
7029 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7030 IT_CHARPOS (*it) = it->bidi_it.charpos;
7031 if (prev_scan_dir != it->bidi_it.scan_dir)
7033 /* As the scan direction was changed, we must
7034 re-compute the stop position for composition. */
7035 ptrdiff_t stop = it->end_charpos;
7036 if (it->bidi_it.scan_dir < 0)
7037 stop = -1;
7038 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7039 IT_BYTEPOS (*it), stop, Qnil);
7042 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7044 break;
7046 case GET_FROM_C_STRING:
7047 /* Current display element of IT is from a C string. */
7048 if (!it->bidi_p
7049 /* If the string position is beyond string's end, it means
7050 next_element_from_c_string is padding the string with
7051 blanks, in which case we bypass the bidi iterator,
7052 because it cannot deal with such virtual characters. */
7053 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7055 IT_BYTEPOS (*it) += it->len;
7056 IT_CHARPOS (*it) += 1;
7058 else
7060 bidi_move_to_visually_next (&it->bidi_it);
7061 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7062 IT_CHARPOS (*it) = it->bidi_it.charpos;
7064 break;
7066 case GET_FROM_DISPLAY_VECTOR:
7067 /* Current display element of IT is from a display table entry.
7068 Advance in the display table definition. Reset it to null if
7069 end reached, and continue with characters from buffers/
7070 strings. */
7071 ++it->current.dpvec_index;
7073 /* Restore face of the iterator to what they were before the
7074 display vector entry (these entries may contain faces). */
7075 it->face_id = it->saved_face_id;
7077 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7079 int recheck_faces = it->ellipsis_p;
7081 if (it->s)
7082 it->method = GET_FROM_C_STRING;
7083 else if (STRINGP (it->string))
7084 it->method = GET_FROM_STRING;
7085 else
7087 it->method = GET_FROM_BUFFER;
7088 it->object = it->w->buffer;
7091 it->dpvec = NULL;
7092 it->current.dpvec_index = -1;
7094 /* Skip over characters which were displayed via IT->dpvec. */
7095 if (it->dpvec_char_len < 0)
7096 reseat_at_next_visible_line_start (it, 1);
7097 else if (it->dpvec_char_len > 0)
7099 if (it->method == GET_FROM_STRING
7100 && it->n_overlay_strings > 0)
7101 it->ignore_overlay_strings_at_pos_p = 1;
7102 it->len = it->dpvec_char_len;
7103 set_iterator_to_next (it, reseat_p);
7106 /* Maybe recheck faces after display vector */
7107 if (recheck_faces)
7108 it->stop_charpos = IT_CHARPOS (*it);
7110 break;
7112 case GET_FROM_STRING:
7113 /* Current display element is a character from a Lisp string. */
7114 eassert (it->s == NULL && STRINGP (it->string));
7115 /* Don't advance past string end. These conditions are true
7116 when set_iterator_to_next is called at the end of
7117 get_next_display_element, in which case the Lisp string is
7118 already exhausted, and all we want is pop the iterator
7119 stack. */
7120 if (it->current.overlay_string_index >= 0)
7122 /* This is an overlay string, so there's no padding with
7123 spaces, and the number of characters in the string is
7124 where the string ends. */
7125 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7126 goto consider_string_end;
7128 else
7130 /* Not an overlay string. There could be padding, so test
7131 against it->end_charpos . */
7132 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7133 goto consider_string_end;
7135 if (it->cmp_it.id >= 0)
7137 int i;
7139 if (! it->bidi_p)
7141 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7142 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7143 if (it->cmp_it.to < it->cmp_it.nglyphs)
7144 it->cmp_it.from = it->cmp_it.to;
7145 else
7147 it->cmp_it.id = -1;
7148 composition_compute_stop_pos (&it->cmp_it,
7149 IT_STRING_CHARPOS (*it),
7150 IT_STRING_BYTEPOS (*it),
7151 it->end_charpos, it->string);
7154 else if (! it->cmp_it.reversed_p)
7156 for (i = 0; i < it->cmp_it.nchars; i++)
7157 bidi_move_to_visually_next (&it->bidi_it);
7158 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7159 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7161 if (it->cmp_it.to < it->cmp_it.nglyphs)
7162 it->cmp_it.from = it->cmp_it.to;
7163 else
7165 ptrdiff_t stop = it->end_charpos;
7166 if (it->bidi_it.scan_dir < 0)
7167 stop = -1;
7168 composition_compute_stop_pos (&it->cmp_it,
7169 IT_STRING_CHARPOS (*it),
7170 IT_STRING_BYTEPOS (*it), stop,
7171 it->string);
7174 else
7176 for (i = 0; i < it->cmp_it.nchars; i++)
7177 bidi_move_to_visually_next (&it->bidi_it);
7178 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7179 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7180 if (it->cmp_it.from > 0)
7181 it->cmp_it.to = it->cmp_it.from;
7182 else
7184 ptrdiff_t stop = it->end_charpos;
7185 if (it->bidi_it.scan_dir < 0)
7186 stop = -1;
7187 composition_compute_stop_pos (&it->cmp_it,
7188 IT_STRING_CHARPOS (*it),
7189 IT_STRING_BYTEPOS (*it), stop,
7190 it->string);
7194 else
7196 if (!it->bidi_p
7197 /* If the string position is beyond string's end, it
7198 means next_element_from_string is padding the string
7199 with blanks, in which case we bypass the bidi
7200 iterator, because it cannot deal with such virtual
7201 characters. */
7202 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7204 IT_STRING_BYTEPOS (*it) += it->len;
7205 IT_STRING_CHARPOS (*it) += 1;
7207 else
7209 int prev_scan_dir = it->bidi_it.scan_dir;
7211 bidi_move_to_visually_next (&it->bidi_it);
7212 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7213 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7214 if (prev_scan_dir != it->bidi_it.scan_dir)
7216 ptrdiff_t stop = it->end_charpos;
7218 if (it->bidi_it.scan_dir < 0)
7219 stop = -1;
7220 composition_compute_stop_pos (&it->cmp_it,
7221 IT_STRING_CHARPOS (*it),
7222 IT_STRING_BYTEPOS (*it), stop,
7223 it->string);
7228 consider_string_end:
7230 if (it->current.overlay_string_index >= 0)
7232 /* IT->string is an overlay string. Advance to the
7233 next, if there is one. */
7234 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7236 it->ellipsis_p = 0;
7237 next_overlay_string (it);
7238 if (it->ellipsis_p)
7239 setup_for_ellipsis (it, 0);
7242 else
7244 /* IT->string is not an overlay string. If we reached
7245 its end, and there is something on IT->stack, proceed
7246 with what is on the stack. This can be either another
7247 string, this time an overlay string, or a buffer. */
7248 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7249 && it->sp > 0)
7251 pop_it (it);
7252 if (it->method == GET_FROM_STRING)
7253 goto consider_string_end;
7256 break;
7258 case GET_FROM_IMAGE:
7259 case GET_FROM_STRETCH:
7260 /* The position etc with which we have to proceed are on
7261 the stack. The position may be at the end of a string,
7262 if the `display' property takes up the whole string. */
7263 eassert (it->sp > 0);
7264 pop_it (it);
7265 if (it->method == GET_FROM_STRING)
7266 goto consider_string_end;
7267 break;
7269 default:
7270 /* There are no other methods defined, so this should be a bug. */
7271 abort ();
7274 eassert (it->method != GET_FROM_STRING
7275 || (STRINGP (it->string)
7276 && IT_STRING_CHARPOS (*it) >= 0));
7279 /* Load IT's display element fields with information about the next
7280 display element which comes from a display table entry or from the
7281 result of translating a control character to one of the forms `^C'
7282 or `\003'.
7284 IT->dpvec holds the glyphs to return as characters.
7285 IT->saved_face_id holds the face id before the display vector--it
7286 is restored into IT->face_id in set_iterator_to_next. */
7288 static int
7289 next_element_from_display_vector (struct it *it)
7291 Lisp_Object gc;
7293 /* Precondition. */
7294 eassert (it->dpvec && it->current.dpvec_index >= 0);
7296 it->face_id = it->saved_face_id;
7298 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7299 That seemed totally bogus - so I changed it... */
7300 gc = it->dpvec[it->current.dpvec_index];
7302 if (GLYPH_CODE_P (gc))
7304 it->c = GLYPH_CODE_CHAR (gc);
7305 it->len = CHAR_BYTES (it->c);
7307 /* The entry may contain a face id to use. Such a face id is
7308 the id of a Lisp face, not a realized face. A face id of
7309 zero means no face is specified. */
7310 if (it->dpvec_face_id >= 0)
7311 it->face_id = it->dpvec_face_id;
7312 else
7314 int lface_id = GLYPH_CODE_FACE (gc);
7315 if (lface_id > 0)
7316 it->face_id = merge_faces (it->f, Qt, lface_id,
7317 it->saved_face_id);
7320 else
7321 /* Display table entry is invalid. Return a space. */
7322 it->c = ' ', it->len = 1;
7324 /* Don't change position and object of the iterator here. They are
7325 still the values of the character that had this display table
7326 entry or was translated, and that's what we want. */
7327 it->what = IT_CHARACTER;
7328 return 1;
7331 /* Get the first element of string/buffer in the visual order, after
7332 being reseated to a new position in a string or a buffer. */
7333 static void
7334 get_visually_first_element (struct it *it)
7336 int string_p = STRINGP (it->string) || it->s;
7337 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7338 ptrdiff_t bob = (string_p ? 0 : BEGV);
7340 if (STRINGP (it->string))
7342 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7343 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7345 else
7347 it->bidi_it.charpos = IT_CHARPOS (*it);
7348 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7351 if (it->bidi_it.charpos == eob)
7353 /* Nothing to do, but reset the FIRST_ELT flag, like
7354 bidi_paragraph_init does, because we are not going to
7355 call it. */
7356 it->bidi_it.first_elt = 0;
7358 else if (it->bidi_it.charpos == bob
7359 || (!string_p
7360 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7361 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7363 /* If we are at the beginning of a line/string, we can produce
7364 the next element right away. */
7365 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7366 bidi_move_to_visually_next (&it->bidi_it);
7368 else
7370 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7372 /* We need to prime the bidi iterator starting at the line's or
7373 string's beginning, before we will be able to produce the
7374 next element. */
7375 if (string_p)
7376 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7377 else
7379 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7380 -1);
7381 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7383 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7386 /* Now return to buffer/string position where we were asked
7387 to get the next display element, and produce that. */
7388 bidi_move_to_visually_next (&it->bidi_it);
7390 while (it->bidi_it.bytepos != orig_bytepos
7391 && it->bidi_it.charpos < eob);
7394 /* Adjust IT's position information to where we ended up. */
7395 if (STRINGP (it->string))
7397 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7398 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7400 else
7402 IT_CHARPOS (*it) = it->bidi_it.charpos;
7403 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7406 if (STRINGP (it->string) || !it->s)
7408 ptrdiff_t stop, charpos, bytepos;
7410 if (STRINGP (it->string))
7412 eassert (!it->s);
7413 stop = SCHARS (it->string);
7414 if (stop > it->end_charpos)
7415 stop = it->end_charpos;
7416 charpos = IT_STRING_CHARPOS (*it);
7417 bytepos = IT_STRING_BYTEPOS (*it);
7419 else
7421 stop = it->end_charpos;
7422 charpos = IT_CHARPOS (*it);
7423 bytepos = IT_BYTEPOS (*it);
7425 if (it->bidi_it.scan_dir < 0)
7426 stop = -1;
7427 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7428 it->string);
7432 /* Load IT with the next display element from Lisp string IT->string.
7433 IT->current.string_pos is the current position within the string.
7434 If IT->current.overlay_string_index >= 0, the Lisp string is an
7435 overlay string. */
7437 static int
7438 next_element_from_string (struct it *it)
7440 struct text_pos position;
7442 eassert (STRINGP (it->string));
7443 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7444 eassert (IT_STRING_CHARPOS (*it) >= 0);
7445 position = it->current.string_pos;
7447 /* With bidi reordering, the character to display might not be the
7448 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7449 that we were reseat()ed to a new string, whose paragraph
7450 direction is not known. */
7451 if (it->bidi_p && it->bidi_it.first_elt)
7453 get_visually_first_element (it);
7454 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7457 /* Time to check for invisible text? */
7458 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7460 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7462 if (!(!it->bidi_p
7463 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7464 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7466 /* With bidi non-linear iteration, we could find
7467 ourselves far beyond the last computed stop_charpos,
7468 with several other stop positions in between that we
7469 missed. Scan them all now, in buffer's logical
7470 order, until we find and handle the last stop_charpos
7471 that precedes our current position. */
7472 handle_stop_backwards (it, it->stop_charpos);
7473 return GET_NEXT_DISPLAY_ELEMENT (it);
7475 else
7477 if (it->bidi_p)
7479 /* Take note of the stop position we just moved
7480 across, for when we will move back across it. */
7481 it->prev_stop = it->stop_charpos;
7482 /* If we are at base paragraph embedding level, take
7483 note of the last stop position seen at this
7484 level. */
7485 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7486 it->base_level_stop = it->stop_charpos;
7488 handle_stop (it);
7490 /* Since a handler may have changed IT->method, we must
7491 recurse here. */
7492 return GET_NEXT_DISPLAY_ELEMENT (it);
7495 else if (it->bidi_p
7496 /* If we are before prev_stop, we may have overstepped
7497 on our way backwards a stop_pos, and if so, we need
7498 to handle that stop_pos. */
7499 && IT_STRING_CHARPOS (*it) < it->prev_stop
7500 /* We can sometimes back up for reasons that have nothing
7501 to do with bidi reordering. E.g., compositions. The
7502 code below is only needed when we are above the base
7503 embedding level, so test for that explicitly. */
7504 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7506 /* If we lost track of base_level_stop, we have no better
7507 place for handle_stop_backwards to start from than string
7508 beginning. This happens, e.g., when we were reseated to
7509 the previous screenful of text by vertical-motion. */
7510 if (it->base_level_stop <= 0
7511 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7512 it->base_level_stop = 0;
7513 handle_stop_backwards (it, it->base_level_stop);
7514 return GET_NEXT_DISPLAY_ELEMENT (it);
7518 if (it->current.overlay_string_index >= 0)
7520 /* Get the next character from an overlay string. In overlay
7521 strings, there is no field width or padding with spaces to
7522 do. */
7523 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7525 it->what = IT_EOB;
7526 return 0;
7528 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7529 IT_STRING_BYTEPOS (*it),
7530 it->bidi_it.scan_dir < 0
7531 ? -1
7532 : SCHARS (it->string))
7533 && next_element_from_composition (it))
7535 return 1;
7537 else if (STRING_MULTIBYTE (it->string))
7539 const unsigned char *s = (SDATA (it->string)
7540 + IT_STRING_BYTEPOS (*it));
7541 it->c = string_char_and_length (s, &it->len);
7543 else
7545 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7546 it->len = 1;
7549 else
7551 /* Get the next character from a Lisp string that is not an
7552 overlay string. Such strings come from the mode line, for
7553 example. We may have to pad with spaces, or truncate the
7554 string. See also next_element_from_c_string. */
7555 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7557 it->what = IT_EOB;
7558 return 0;
7560 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7562 /* Pad with spaces. */
7563 it->c = ' ', it->len = 1;
7564 CHARPOS (position) = BYTEPOS (position) = -1;
7566 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7567 IT_STRING_BYTEPOS (*it),
7568 it->bidi_it.scan_dir < 0
7569 ? -1
7570 : it->string_nchars)
7571 && next_element_from_composition (it))
7573 return 1;
7575 else if (STRING_MULTIBYTE (it->string))
7577 const unsigned char *s = (SDATA (it->string)
7578 + IT_STRING_BYTEPOS (*it));
7579 it->c = string_char_and_length (s, &it->len);
7581 else
7583 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7584 it->len = 1;
7588 /* Record what we have and where it came from. */
7589 it->what = IT_CHARACTER;
7590 it->object = it->string;
7591 it->position = position;
7592 return 1;
7596 /* Load IT with next display element from C string IT->s.
7597 IT->string_nchars is the maximum number of characters to return
7598 from the string. IT->end_charpos may be greater than
7599 IT->string_nchars when this function is called, in which case we
7600 may have to return padding spaces. Value is zero if end of string
7601 reached, including padding spaces. */
7603 static int
7604 next_element_from_c_string (struct it *it)
7606 int success_p = 1;
7608 eassert (it->s);
7609 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7610 it->what = IT_CHARACTER;
7611 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7612 it->object = Qnil;
7614 /* With bidi reordering, the character to display might not be the
7615 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7616 we were reseated to a new string, whose paragraph direction is
7617 not known. */
7618 if (it->bidi_p && it->bidi_it.first_elt)
7619 get_visually_first_element (it);
7621 /* IT's position can be greater than IT->string_nchars in case a
7622 field width or precision has been specified when the iterator was
7623 initialized. */
7624 if (IT_CHARPOS (*it) >= it->end_charpos)
7626 /* End of the game. */
7627 it->what = IT_EOB;
7628 success_p = 0;
7630 else if (IT_CHARPOS (*it) >= it->string_nchars)
7632 /* Pad with spaces. */
7633 it->c = ' ', it->len = 1;
7634 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7636 else if (it->multibyte_p)
7637 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7638 else
7639 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7641 return success_p;
7645 /* Set up IT to return characters from an ellipsis, if appropriate.
7646 The definition of the ellipsis glyphs may come from a display table
7647 entry. This function fills IT with the first glyph from the
7648 ellipsis if an ellipsis is to be displayed. */
7650 static int
7651 next_element_from_ellipsis (struct it *it)
7653 if (it->selective_display_ellipsis_p)
7654 setup_for_ellipsis (it, it->len);
7655 else
7657 /* The face at the current position may be different from the
7658 face we find after the invisible text. Remember what it
7659 was in IT->saved_face_id, and signal that it's there by
7660 setting face_before_selective_p. */
7661 it->saved_face_id = it->face_id;
7662 it->method = GET_FROM_BUFFER;
7663 it->object = it->w->buffer;
7664 reseat_at_next_visible_line_start (it, 1);
7665 it->face_before_selective_p = 1;
7668 return GET_NEXT_DISPLAY_ELEMENT (it);
7672 /* Deliver an image display element. The iterator IT is already
7673 filled with image information (done in handle_display_prop). Value
7674 is always 1. */
7677 static int
7678 next_element_from_image (struct it *it)
7680 it->what = IT_IMAGE;
7681 it->ignore_overlay_strings_at_pos_p = 0;
7682 return 1;
7686 /* Fill iterator IT with next display element from a stretch glyph
7687 property. IT->object is the value of the text property. Value is
7688 always 1. */
7690 static int
7691 next_element_from_stretch (struct it *it)
7693 it->what = IT_STRETCH;
7694 return 1;
7697 /* Scan backwards from IT's current position until we find a stop
7698 position, or until BEGV. This is called when we find ourself
7699 before both the last known prev_stop and base_level_stop while
7700 reordering bidirectional text. */
7702 static void
7703 compute_stop_pos_backwards (struct it *it)
7705 const int SCAN_BACK_LIMIT = 1000;
7706 struct text_pos pos;
7707 struct display_pos save_current = it->current;
7708 struct text_pos save_position = it->position;
7709 ptrdiff_t charpos = IT_CHARPOS (*it);
7710 ptrdiff_t where_we_are = charpos;
7711 ptrdiff_t save_stop_pos = it->stop_charpos;
7712 ptrdiff_t save_end_pos = it->end_charpos;
7714 eassert (NILP (it->string) && !it->s);
7715 eassert (it->bidi_p);
7716 it->bidi_p = 0;
7719 it->end_charpos = min (charpos + 1, ZV);
7720 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7721 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7722 reseat_1 (it, pos, 0);
7723 compute_stop_pos (it);
7724 /* We must advance forward, right? */
7725 if (it->stop_charpos <= charpos)
7726 abort ();
7728 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7730 if (it->stop_charpos <= where_we_are)
7731 it->prev_stop = it->stop_charpos;
7732 else
7733 it->prev_stop = BEGV;
7734 it->bidi_p = 1;
7735 it->current = save_current;
7736 it->position = save_position;
7737 it->stop_charpos = save_stop_pos;
7738 it->end_charpos = save_end_pos;
7741 /* Scan forward from CHARPOS in the current buffer/string, until we
7742 find a stop position > current IT's position. Then handle the stop
7743 position before that. This is called when we bump into a stop
7744 position while reordering bidirectional text. CHARPOS should be
7745 the last previously processed stop_pos (or BEGV/0, if none were
7746 processed yet) whose position is less that IT's current
7747 position. */
7749 static void
7750 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7752 int bufp = !STRINGP (it->string);
7753 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7754 struct display_pos save_current = it->current;
7755 struct text_pos save_position = it->position;
7756 struct text_pos pos1;
7757 ptrdiff_t next_stop;
7759 /* Scan in strict logical order. */
7760 eassert (it->bidi_p);
7761 it->bidi_p = 0;
7764 it->prev_stop = charpos;
7765 if (bufp)
7767 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7768 reseat_1 (it, pos1, 0);
7770 else
7771 it->current.string_pos = string_pos (charpos, it->string);
7772 compute_stop_pos (it);
7773 /* We must advance forward, right? */
7774 if (it->stop_charpos <= it->prev_stop)
7775 abort ();
7776 charpos = it->stop_charpos;
7778 while (charpos <= where_we_are);
7780 it->bidi_p = 1;
7781 it->current = save_current;
7782 it->position = save_position;
7783 next_stop = it->stop_charpos;
7784 it->stop_charpos = it->prev_stop;
7785 handle_stop (it);
7786 it->stop_charpos = next_stop;
7789 /* Load IT with the next display element from current_buffer. Value
7790 is zero if end of buffer reached. IT->stop_charpos is the next
7791 position at which to stop and check for text properties or buffer
7792 end. */
7794 static int
7795 next_element_from_buffer (struct it *it)
7797 int success_p = 1;
7799 eassert (IT_CHARPOS (*it) >= BEGV);
7800 eassert (NILP (it->string) && !it->s);
7801 eassert (!it->bidi_p
7802 || (EQ (it->bidi_it.string.lstring, Qnil)
7803 && it->bidi_it.string.s == NULL));
7805 /* With bidi reordering, the character to display might not be the
7806 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7807 we were reseat()ed to a new buffer position, which is potentially
7808 a different paragraph. */
7809 if (it->bidi_p && it->bidi_it.first_elt)
7811 get_visually_first_element (it);
7812 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7815 if (IT_CHARPOS (*it) >= it->stop_charpos)
7817 if (IT_CHARPOS (*it) >= it->end_charpos)
7819 int overlay_strings_follow_p;
7821 /* End of the game, except when overlay strings follow that
7822 haven't been returned yet. */
7823 if (it->overlay_strings_at_end_processed_p)
7824 overlay_strings_follow_p = 0;
7825 else
7827 it->overlay_strings_at_end_processed_p = 1;
7828 overlay_strings_follow_p = get_overlay_strings (it, 0);
7831 if (overlay_strings_follow_p)
7832 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7833 else
7835 it->what = IT_EOB;
7836 it->position = it->current.pos;
7837 success_p = 0;
7840 else if (!(!it->bidi_p
7841 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7842 || IT_CHARPOS (*it) == it->stop_charpos))
7844 /* With bidi non-linear iteration, we could find ourselves
7845 far beyond the last computed stop_charpos, with several
7846 other stop positions in between that we missed. Scan
7847 them all now, in buffer's logical order, until we find
7848 and handle the last stop_charpos that precedes our
7849 current position. */
7850 handle_stop_backwards (it, it->stop_charpos);
7851 return GET_NEXT_DISPLAY_ELEMENT (it);
7853 else
7855 if (it->bidi_p)
7857 /* Take note of the stop position we just moved across,
7858 for when we will move back across it. */
7859 it->prev_stop = it->stop_charpos;
7860 /* If we are at base paragraph embedding level, take
7861 note of the last stop position seen at this
7862 level. */
7863 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7864 it->base_level_stop = it->stop_charpos;
7866 handle_stop (it);
7867 return GET_NEXT_DISPLAY_ELEMENT (it);
7870 else if (it->bidi_p
7871 /* If we are before prev_stop, we may have overstepped on
7872 our way backwards a stop_pos, and if so, we need to
7873 handle that stop_pos. */
7874 && IT_CHARPOS (*it) < it->prev_stop
7875 /* We can sometimes back up for reasons that have nothing
7876 to do with bidi reordering. E.g., compositions. The
7877 code below is only needed when we are above the base
7878 embedding level, so test for that explicitly. */
7879 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7881 if (it->base_level_stop <= 0
7882 || IT_CHARPOS (*it) < it->base_level_stop)
7884 /* If we lost track of base_level_stop, we need to find
7885 prev_stop by looking backwards. This happens, e.g., when
7886 we were reseated to the previous screenful of text by
7887 vertical-motion. */
7888 it->base_level_stop = BEGV;
7889 compute_stop_pos_backwards (it);
7890 handle_stop_backwards (it, it->prev_stop);
7892 else
7893 handle_stop_backwards (it, it->base_level_stop);
7894 return GET_NEXT_DISPLAY_ELEMENT (it);
7896 else
7898 /* No face changes, overlays etc. in sight, so just return a
7899 character from current_buffer. */
7900 unsigned char *p;
7901 ptrdiff_t stop;
7903 /* Maybe run the redisplay end trigger hook. Performance note:
7904 This doesn't seem to cost measurable time. */
7905 if (it->redisplay_end_trigger_charpos
7906 && it->glyph_row
7907 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7908 run_redisplay_end_trigger_hook (it);
7910 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7911 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7912 stop)
7913 && next_element_from_composition (it))
7915 return 1;
7918 /* Get the next character, maybe multibyte. */
7919 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7920 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7921 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7922 else
7923 it->c = *p, it->len = 1;
7925 /* Record what we have and where it came from. */
7926 it->what = IT_CHARACTER;
7927 it->object = it->w->buffer;
7928 it->position = it->current.pos;
7930 /* Normally we return the character found above, except when we
7931 really want to return an ellipsis for selective display. */
7932 if (it->selective)
7934 if (it->c == '\n')
7936 /* A value of selective > 0 means hide lines indented more
7937 than that number of columns. */
7938 if (it->selective > 0
7939 && IT_CHARPOS (*it) + 1 < ZV
7940 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7941 IT_BYTEPOS (*it) + 1,
7942 it->selective))
7944 success_p = next_element_from_ellipsis (it);
7945 it->dpvec_char_len = -1;
7948 else if (it->c == '\r' && it->selective == -1)
7950 /* A value of selective == -1 means that everything from the
7951 CR to the end of the line is invisible, with maybe an
7952 ellipsis displayed for it. */
7953 success_p = next_element_from_ellipsis (it);
7954 it->dpvec_char_len = -1;
7959 /* Value is zero if end of buffer reached. */
7960 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7961 return success_p;
7965 /* Run the redisplay end trigger hook for IT. */
7967 static void
7968 run_redisplay_end_trigger_hook (struct it *it)
7970 Lisp_Object args[3];
7972 /* IT->glyph_row should be non-null, i.e. we should be actually
7973 displaying something, or otherwise we should not run the hook. */
7974 eassert (it->glyph_row);
7976 /* Set up hook arguments. */
7977 args[0] = Qredisplay_end_trigger_functions;
7978 args[1] = it->window;
7979 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7980 it->redisplay_end_trigger_charpos = 0;
7982 /* Since we are *trying* to run these functions, don't try to run
7983 them again, even if they get an error. */
7984 WSET (it->w, redisplay_end_trigger, Qnil);
7985 Frun_hook_with_args (3, args);
7987 /* Notice if it changed the face of the character we are on. */
7988 handle_face_prop (it);
7992 /* Deliver a composition display element. Unlike the other
7993 next_element_from_XXX, this function is not registered in the array
7994 get_next_element[]. It is called from next_element_from_buffer and
7995 next_element_from_string when necessary. */
7997 static int
7998 next_element_from_composition (struct it *it)
8000 it->what = IT_COMPOSITION;
8001 it->len = it->cmp_it.nbytes;
8002 if (STRINGP (it->string))
8004 if (it->c < 0)
8006 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8007 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8008 return 0;
8010 it->position = it->current.string_pos;
8011 it->object = it->string;
8012 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8013 IT_STRING_BYTEPOS (*it), it->string);
8015 else
8017 if (it->c < 0)
8019 IT_CHARPOS (*it) += it->cmp_it.nchars;
8020 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8021 if (it->bidi_p)
8023 if (it->bidi_it.new_paragraph)
8024 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8025 /* Resync the bidi iterator with IT's new position.
8026 FIXME: this doesn't support bidirectional text. */
8027 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8028 bidi_move_to_visually_next (&it->bidi_it);
8030 return 0;
8032 it->position = it->current.pos;
8033 it->object = it->w->buffer;
8034 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8035 IT_BYTEPOS (*it), Qnil);
8037 return 1;
8042 /***********************************************************************
8043 Moving an iterator without producing glyphs
8044 ***********************************************************************/
8046 /* Check if iterator is at a position corresponding to a valid buffer
8047 position after some move_it_ call. */
8049 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8050 ((it)->method == GET_FROM_STRING \
8051 ? IT_STRING_CHARPOS (*it) == 0 \
8052 : 1)
8055 /* Move iterator IT to a specified buffer or X position within one
8056 line on the display without producing glyphs.
8058 OP should be a bit mask including some or all of these bits:
8059 MOVE_TO_X: Stop upon reaching x-position TO_X.
8060 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8061 Regardless of OP's value, stop upon reaching the end of the display line.
8063 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8064 This means, in particular, that TO_X includes window's horizontal
8065 scroll amount.
8067 The return value has several possible values that
8068 say what condition caused the scan to stop:
8070 MOVE_POS_MATCH_OR_ZV
8071 - when TO_POS or ZV was reached.
8073 MOVE_X_REACHED
8074 -when TO_X was reached before TO_POS or ZV were reached.
8076 MOVE_LINE_CONTINUED
8077 - when we reached the end of the display area and the line must
8078 be continued.
8080 MOVE_LINE_TRUNCATED
8081 - when we reached the end of the display area and the line is
8082 truncated.
8084 MOVE_NEWLINE_OR_CR
8085 - when we stopped at a line end, i.e. a newline or a CR and selective
8086 display is on. */
8088 static enum move_it_result
8089 move_it_in_display_line_to (struct it *it,
8090 ptrdiff_t to_charpos, int to_x,
8091 enum move_operation_enum op)
8093 enum move_it_result result = MOVE_UNDEFINED;
8094 struct glyph_row *saved_glyph_row;
8095 struct it wrap_it, atpos_it, atx_it, ppos_it;
8096 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8097 void *ppos_data = NULL;
8098 int may_wrap = 0;
8099 enum it_method prev_method = it->method;
8100 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8101 int saw_smaller_pos = prev_pos < to_charpos;
8103 /* Don't produce glyphs in produce_glyphs. */
8104 saved_glyph_row = it->glyph_row;
8105 it->glyph_row = NULL;
8107 /* Use wrap_it to save a copy of IT wherever a word wrap could
8108 occur. Use atpos_it to save a copy of IT at the desired buffer
8109 position, if found, so that we can scan ahead and check if the
8110 word later overshoots the window edge. Use atx_it similarly, for
8111 pixel positions. */
8112 wrap_it.sp = -1;
8113 atpos_it.sp = -1;
8114 atx_it.sp = -1;
8116 /* Use ppos_it under bidi reordering to save a copy of IT for the
8117 position > CHARPOS that is the closest to CHARPOS. We restore
8118 that position in IT when we have scanned the entire display line
8119 without finding a match for CHARPOS and all the character
8120 positions are greater than CHARPOS. */
8121 if (it->bidi_p)
8123 SAVE_IT (ppos_it, *it, ppos_data);
8124 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8125 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8126 SAVE_IT (ppos_it, *it, ppos_data);
8129 #define BUFFER_POS_REACHED_P() \
8130 ((op & MOVE_TO_POS) != 0 \
8131 && BUFFERP (it->object) \
8132 && (IT_CHARPOS (*it) == to_charpos \
8133 || ((!it->bidi_p \
8134 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8135 && IT_CHARPOS (*it) > to_charpos) \
8136 || (it->what == IT_COMPOSITION \
8137 && ((IT_CHARPOS (*it) > to_charpos \
8138 && to_charpos >= it->cmp_it.charpos) \
8139 || (IT_CHARPOS (*it) < to_charpos \
8140 && to_charpos <= it->cmp_it.charpos)))) \
8141 && (it->method == GET_FROM_BUFFER \
8142 || (it->method == GET_FROM_DISPLAY_VECTOR \
8143 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8145 /* If there's a line-/wrap-prefix, handle it. */
8146 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8147 && it->current_y < it->last_visible_y)
8148 handle_line_prefix (it);
8150 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8151 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8153 while (1)
8155 int x, i, ascent = 0, descent = 0;
8157 /* Utility macro to reset an iterator with x, ascent, and descent. */
8158 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8159 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8160 (IT)->max_descent = descent)
8162 /* Stop if we move beyond TO_CHARPOS (after an image or a
8163 display string or stretch glyph). */
8164 if ((op & MOVE_TO_POS) != 0
8165 && BUFFERP (it->object)
8166 && it->method == GET_FROM_BUFFER
8167 && (((!it->bidi_p
8168 /* When the iterator is at base embedding level, we
8169 are guaranteed that characters are delivered for
8170 display in strictly increasing order of their
8171 buffer positions. */
8172 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8173 && IT_CHARPOS (*it) > to_charpos)
8174 || (it->bidi_p
8175 && (prev_method == GET_FROM_IMAGE
8176 || prev_method == GET_FROM_STRETCH
8177 || prev_method == GET_FROM_STRING)
8178 /* Passed TO_CHARPOS from left to right. */
8179 && ((prev_pos < to_charpos
8180 && IT_CHARPOS (*it) > to_charpos)
8181 /* Passed TO_CHARPOS from right to left. */
8182 || (prev_pos > to_charpos
8183 && IT_CHARPOS (*it) < to_charpos)))))
8185 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8187 result = MOVE_POS_MATCH_OR_ZV;
8188 break;
8190 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8191 /* If wrap_it is valid, the current position might be in a
8192 word that is wrapped. So, save the iterator in
8193 atpos_it and continue to see if wrapping happens. */
8194 SAVE_IT (atpos_it, *it, atpos_data);
8197 /* Stop when ZV reached.
8198 We used to stop here when TO_CHARPOS reached as well, but that is
8199 too soon if this glyph does not fit on this line. So we handle it
8200 explicitly below. */
8201 if (!get_next_display_element (it))
8203 result = MOVE_POS_MATCH_OR_ZV;
8204 break;
8207 if (it->line_wrap == TRUNCATE)
8209 if (BUFFER_POS_REACHED_P ())
8211 result = MOVE_POS_MATCH_OR_ZV;
8212 break;
8215 else
8217 if (it->line_wrap == WORD_WRAP)
8219 if (IT_DISPLAYING_WHITESPACE (it))
8220 may_wrap = 1;
8221 else if (may_wrap)
8223 /* We have reached a glyph that follows one or more
8224 whitespace characters. If the position is
8225 already found, we are done. */
8226 if (atpos_it.sp >= 0)
8228 RESTORE_IT (it, &atpos_it, atpos_data);
8229 result = MOVE_POS_MATCH_OR_ZV;
8230 goto done;
8232 if (atx_it.sp >= 0)
8234 RESTORE_IT (it, &atx_it, atx_data);
8235 result = MOVE_X_REACHED;
8236 goto done;
8238 /* Otherwise, we can wrap here. */
8239 SAVE_IT (wrap_it, *it, wrap_data);
8240 may_wrap = 0;
8245 /* Remember the line height for the current line, in case
8246 the next element doesn't fit on the line. */
8247 ascent = it->max_ascent;
8248 descent = it->max_descent;
8250 /* The call to produce_glyphs will get the metrics of the
8251 display element IT is loaded with. Record the x-position
8252 before this display element, in case it doesn't fit on the
8253 line. */
8254 x = it->current_x;
8256 PRODUCE_GLYPHS (it);
8258 if (it->area != TEXT_AREA)
8260 prev_method = it->method;
8261 if (it->method == GET_FROM_BUFFER)
8262 prev_pos = IT_CHARPOS (*it);
8263 set_iterator_to_next (it, 1);
8264 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8265 SET_TEXT_POS (this_line_min_pos,
8266 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8267 if (it->bidi_p
8268 && (op & MOVE_TO_POS)
8269 && IT_CHARPOS (*it) > to_charpos
8270 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8271 SAVE_IT (ppos_it, *it, ppos_data);
8272 continue;
8275 /* The number of glyphs we get back in IT->nglyphs will normally
8276 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8277 character on a terminal frame, or (iii) a line end. For the
8278 second case, IT->nglyphs - 1 padding glyphs will be present.
8279 (On X frames, there is only one glyph produced for a
8280 composite character.)
8282 The behavior implemented below means, for continuation lines,
8283 that as many spaces of a TAB as fit on the current line are
8284 displayed there. For terminal frames, as many glyphs of a
8285 multi-glyph character are displayed in the current line, too.
8286 This is what the old redisplay code did, and we keep it that
8287 way. Under X, the whole shape of a complex character must
8288 fit on the line or it will be completely displayed in the
8289 next line.
8291 Note that both for tabs and padding glyphs, all glyphs have
8292 the same width. */
8293 if (it->nglyphs)
8295 /* More than one glyph or glyph doesn't fit on line. All
8296 glyphs have the same width. */
8297 int single_glyph_width = it->pixel_width / it->nglyphs;
8298 int new_x;
8299 int x_before_this_char = x;
8300 int hpos_before_this_char = it->hpos;
8302 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8304 new_x = x + single_glyph_width;
8306 /* We want to leave anything reaching TO_X to the caller. */
8307 if ((op & MOVE_TO_X) && new_x > to_x)
8309 if (BUFFER_POS_REACHED_P ())
8311 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8312 goto buffer_pos_reached;
8313 if (atpos_it.sp < 0)
8315 SAVE_IT (atpos_it, *it, atpos_data);
8316 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8319 else
8321 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8323 it->current_x = x;
8324 result = MOVE_X_REACHED;
8325 break;
8327 if (atx_it.sp < 0)
8329 SAVE_IT (atx_it, *it, atx_data);
8330 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8335 if (/* Lines are continued. */
8336 it->line_wrap != TRUNCATE
8337 && (/* And glyph doesn't fit on the line. */
8338 new_x > it->last_visible_x
8339 /* Or it fits exactly and we're on a window
8340 system frame. */
8341 || (new_x == it->last_visible_x
8342 && FRAME_WINDOW_P (it->f)
8343 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8344 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8345 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8347 if (/* IT->hpos == 0 means the very first glyph
8348 doesn't fit on the line, e.g. a wide image. */
8349 it->hpos == 0
8350 || (new_x == it->last_visible_x
8351 && FRAME_WINDOW_P (it->f)))
8353 ++it->hpos;
8354 it->current_x = new_x;
8356 /* The character's last glyph just barely fits
8357 in this row. */
8358 if (i == it->nglyphs - 1)
8360 /* If this is the destination position,
8361 return a position *before* it in this row,
8362 now that we know it fits in this row. */
8363 if (BUFFER_POS_REACHED_P ())
8365 if (it->line_wrap != WORD_WRAP
8366 || wrap_it.sp < 0)
8368 it->hpos = hpos_before_this_char;
8369 it->current_x = x_before_this_char;
8370 result = MOVE_POS_MATCH_OR_ZV;
8371 break;
8373 if (it->line_wrap == WORD_WRAP
8374 && atpos_it.sp < 0)
8376 SAVE_IT (atpos_it, *it, atpos_data);
8377 atpos_it.current_x = x_before_this_char;
8378 atpos_it.hpos = hpos_before_this_char;
8382 prev_method = it->method;
8383 if (it->method == GET_FROM_BUFFER)
8384 prev_pos = IT_CHARPOS (*it);
8385 set_iterator_to_next (it, 1);
8386 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8387 SET_TEXT_POS (this_line_min_pos,
8388 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8389 /* On graphical terminals, newlines may
8390 "overflow" into the fringe if
8391 overflow-newline-into-fringe is non-nil.
8392 On text terminals, and on graphical
8393 terminals with no right margin, newlines
8394 may overflow into the last glyph on the
8395 display line.*/
8396 if (!FRAME_WINDOW_P (it->f)
8397 || ((it->bidi_p
8398 && it->bidi_it.paragraph_dir == R2L)
8399 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8400 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8401 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8403 if (!get_next_display_element (it))
8405 result = MOVE_POS_MATCH_OR_ZV;
8406 break;
8408 if (BUFFER_POS_REACHED_P ())
8410 if (ITERATOR_AT_END_OF_LINE_P (it))
8411 result = MOVE_POS_MATCH_OR_ZV;
8412 else
8413 result = MOVE_LINE_CONTINUED;
8414 break;
8416 if (ITERATOR_AT_END_OF_LINE_P (it))
8418 result = MOVE_NEWLINE_OR_CR;
8419 break;
8424 else
8425 IT_RESET_X_ASCENT_DESCENT (it);
8427 if (wrap_it.sp >= 0)
8429 RESTORE_IT (it, &wrap_it, wrap_data);
8430 atpos_it.sp = -1;
8431 atx_it.sp = -1;
8434 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8435 IT_CHARPOS (*it)));
8436 result = MOVE_LINE_CONTINUED;
8437 break;
8440 if (BUFFER_POS_REACHED_P ())
8442 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8443 goto buffer_pos_reached;
8444 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8446 SAVE_IT (atpos_it, *it, atpos_data);
8447 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8451 if (new_x > it->first_visible_x)
8453 /* Glyph is visible. Increment number of glyphs that
8454 would be displayed. */
8455 ++it->hpos;
8459 if (result != MOVE_UNDEFINED)
8460 break;
8462 else if (BUFFER_POS_REACHED_P ())
8464 buffer_pos_reached:
8465 IT_RESET_X_ASCENT_DESCENT (it);
8466 result = MOVE_POS_MATCH_OR_ZV;
8467 break;
8469 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8471 /* Stop when TO_X specified and reached. This check is
8472 necessary here because of lines consisting of a line end,
8473 only. The line end will not produce any glyphs and we
8474 would never get MOVE_X_REACHED. */
8475 eassert (it->nglyphs == 0);
8476 result = MOVE_X_REACHED;
8477 break;
8480 /* Is this a line end? If yes, we're done. */
8481 if (ITERATOR_AT_END_OF_LINE_P (it))
8483 /* If we are past TO_CHARPOS, but never saw any character
8484 positions smaller than TO_CHARPOS, return
8485 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8486 did. */
8487 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8489 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8491 if (IT_CHARPOS (ppos_it) < ZV)
8493 RESTORE_IT (it, &ppos_it, ppos_data);
8494 result = MOVE_POS_MATCH_OR_ZV;
8496 else
8497 goto buffer_pos_reached;
8499 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8500 && IT_CHARPOS (*it) > to_charpos)
8501 goto buffer_pos_reached;
8502 else
8503 result = MOVE_NEWLINE_OR_CR;
8505 else
8506 result = MOVE_NEWLINE_OR_CR;
8507 break;
8510 prev_method = it->method;
8511 if (it->method == GET_FROM_BUFFER)
8512 prev_pos = IT_CHARPOS (*it);
8513 /* The current display element has been consumed. Advance
8514 to the next. */
8515 set_iterator_to_next (it, 1);
8516 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8517 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8518 if (IT_CHARPOS (*it) < to_charpos)
8519 saw_smaller_pos = 1;
8520 if (it->bidi_p
8521 && (op & MOVE_TO_POS)
8522 && IT_CHARPOS (*it) >= to_charpos
8523 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8524 SAVE_IT (ppos_it, *it, ppos_data);
8526 /* Stop if lines are truncated and IT's current x-position is
8527 past the right edge of the window now. */
8528 if (it->line_wrap == TRUNCATE
8529 && it->current_x >= it->last_visible_x)
8531 if (!FRAME_WINDOW_P (it->f)
8532 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8533 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8534 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8535 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8537 int at_eob_p = 0;
8539 if ((at_eob_p = !get_next_display_element (it))
8540 || BUFFER_POS_REACHED_P ()
8541 /* If we are past TO_CHARPOS, but never saw any
8542 character positions smaller than TO_CHARPOS,
8543 return MOVE_POS_MATCH_OR_ZV, like the
8544 unidirectional display did. */
8545 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8546 && !saw_smaller_pos
8547 && IT_CHARPOS (*it) > to_charpos))
8549 if (it->bidi_p
8550 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8551 RESTORE_IT (it, &ppos_it, ppos_data);
8552 result = MOVE_POS_MATCH_OR_ZV;
8553 break;
8555 if (ITERATOR_AT_END_OF_LINE_P (it))
8557 result = MOVE_NEWLINE_OR_CR;
8558 break;
8561 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8562 && !saw_smaller_pos
8563 && IT_CHARPOS (*it) > to_charpos)
8565 if (IT_CHARPOS (ppos_it) < ZV)
8566 RESTORE_IT (it, &ppos_it, ppos_data);
8567 result = MOVE_POS_MATCH_OR_ZV;
8568 break;
8570 result = MOVE_LINE_TRUNCATED;
8571 break;
8573 #undef IT_RESET_X_ASCENT_DESCENT
8576 #undef BUFFER_POS_REACHED_P
8578 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8579 restore the saved iterator. */
8580 if (atpos_it.sp >= 0)
8581 RESTORE_IT (it, &atpos_it, atpos_data);
8582 else if (atx_it.sp >= 0)
8583 RESTORE_IT (it, &atx_it, atx_data);
8585 done:
8587 if (atpos_data)
8588 bidi_unshelve_cache (atpos_data, 1);
8589 if (atx_data)
8590 bidi_unshelve_cache (atx_data, 1);
8591 if (wrap_data)
8592 bidi_unshelve_cache (wrap_data, 1);
8593 if (ppos_data)
8594 bidi_unshelve_cache (ppos_data, 1);
8596 /* Restore the iterator settings altered at the beginning of this
8597 function. */
8598 it->glyph_row = saved_glyph_row;
8599 return result;
8602 /* For external use. */
8603 void
8604 move_it_in_display_line (struct it *it,
8605 ptrdiff_t to_charpos, int to_x,
8606 enum move_operation_enum op)
8608 if (it->line_wrap == WORD_WRAP
8609 && (op & MOVE_TO_X))
8611 struct it save_it;
8612 void *save_data = NULL;
8613 int skip;
8615 SAVE_IT (save_it, *it, save_data);
8616 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8617 /* When word-wrap is on, TO_X may lie past the end
8618 of a wrapped line. Then it->current is the
8619 character on the next line, so backtrack to the
8620 space before the wrap point. */
8621 if (skip == MOVE_LINE_CONTINUED)
8623 int prev_x = max (it->current_x - 1, 0);
8624 RESTORE_IT (it, &save_it, save_data);
8625 move_it_in_display_line_to
8626 (it, -1, prev_x, MOVE_TO_X);
8628 else
8629 bidi_unshelve_cache (save_data, 1);
8631 else
8632 move_it_in_display_line_to (it, to_charpos, to_x, op);
8636 /* Move IT forward until it satisfies one or more of the criteria in
8637 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8639 OP is a bit-mask that specifies where to stop, and in particular,
8640 which of those four position arguments makes a difference. See the
8641 description of enum move_operation_enum.
8643 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8644 screen line, this function will set IT to the next position that is
8645 displayed to the right of TO_CHARPOS on the screen. */
8647 void
8648 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8650 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8651 int line_height, line_start_x = 0, reached = 0;
8652 void *backup_data = NULL;
8654 for (;;)
8656 if (op & MOVE_TO_VPOS)
8658 /* If no TO_CHARPOS and no TO_X specified, stop at the
8659 start of the line TO_VPOS. */
8660 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8662 if (it->vpos == to_vpos)
8664 reached = 1;
8665 break;
8667 else
8668 skip = move_it_in_display_line_to (it, -1, -1, 0);
8670 else
8672 /* TO_VPOS >= 0 means stop at TO_X in the line at
8673 TO_VPOS, or at TO_POS, whichever comes first. */
8674 if (it->vpos == to_vpos)
8676 reached = 2;
8677 break;
8680 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8682 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8684 reached = 3;
8685 break;
8687 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8689 /* We have reached TO_X but not in the line we want. */
8690 skip = move_it_in_display_line_to (it, to_charpos,
8691 -1, MOVE_TO_POS);
8692 if (skip == MOVE_POS_MATCH_OR_ZV)
8694 reached = 4;
8695 break;
8700 else if (op & MOVE_TO_Y)
8702 struct it it_backup;
8704 if (it->line_wrap == WORD_WRAP)
8705 SAVE_IT (it_backup, *it, backup_data);
8707 /* TO_Y specified means stop at TO_X in the line containing
8708 TO_Y---or at TO_CHARPOS if this is reached first. The
8709 problem is that we can't really tell whether the line
8710 contains TO_Y before we have completely scanned it, and
8711 this may skip past TO_X. What we do is to first scan to
8712 TO_X.
8714 If TO_X is not specified, use a TO_X of zero. The reason
8715 is to make the outcome of this function more predictable.
8716 If we didn't use TO_X == 0, we would stop at the end of
8717 the line which is probably not what a caller would expect
8718 to happen. */
8719 skip = move_it_in_display_line_to
8720 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8721 (MOVE_TO_X | (op & MOVE_TO_POS)));
8723 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8724 if (skip == MOVE_POS_MATCH_OR_ZV)
8725 reached = 5;
8726 else if (skip == MOVE_X_REACHED)
8728 /* If TO_X was reached, we want to know whether TO_Y is
8729 in the line. We know this is the case if the already
8730 scanned glyphs make the line tall enough. Otherwise,
8731 we must check by scanning the rest of the line. */
8732 line_height = it->max_ascent + it->max_descent;
8733 if (to_y >= it->current_y
8734 && to_y < it->current_y + line_height)
8736 reached = 6;
8737 break;
8739 SAVE_IT (it_backup, *it, backup_data);
8740 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8741 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8742 op & MOVE_TO_POS);
8743 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8744 line_height = it->max_ascent + it->max_descent;
8745 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8747 if (to_y >= it->current_y
8748 && to_y < it->current_y + line_height)
8750 /* If TO_Y is in this line and TO_X was reached
8751 above, we scanned too far. We have to restore
8752 IT's settings to the ones before skipping. But
8753 keep the more accurate values of max_ascent and
8754 max_descent we've found while skipping the rest
8755 of the line, for the sake of callers, such as
8756 pos_visible_p, that need to know the line
8757 height. */
8758 int max_ascent = it->max_ascent;
8759 int max_descent = it->max_descent;
8761 RESTORE_IT (it, &it_backup, backup_data);
8762 it->max_ascent = max_ascent;
8763 it->max_descent = max_descent;
8764 reached = 6;
8766 else
8768 skip = skip2;
8769 if (skip == MOVE_POS_MATCH_OR_ZV)
8770 reached = 7;
8773 else
8775 /* Check whether TO_Y is in this line. */
8776 line_height = it->max_ascent + it->max_descent;
8777 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8779 if (to_y >= it->current_y
8780 && to_y < it->current_y + line_height)
8782 /* When word-wrap is on, TO_X may lie past the end
8783 of a wrapped line. Then it->current is the
8784 character on the next line, so backtrack to the
8785 space before the wrap point. */
8786 if (skip == MOVE_LINE_CONTINUED
8787 && it->line_wrap == WORD_WRAP)
8789 int prev_x = max (it->current_x - 1, 0);
8790 RESTORE_IT (it, &it_backup, backup_data);
8791 skip = move_it_in_display_line_to
8792 (it, -1, prev_x, MOVE_TO_X);
8794 reached = 6;
8798 if (reached)
8799 break;
8801 else if (BUFFERP (it->object)
8802 && (it->method == GET_FROM_BUFFER
8803 || it->method == GET_FROM_STRETCH)
8804 && IT_CHARPOS (*it) >= to_charpos
8805 /* Under bidi iteration, a call to set_iterator_to_next
8806 can scan far beyond to_charpos if the initial
8807 portion of the next line needs to be reordered. In
8808 that case, give move_it_in_display_line_to another
8809 chance below. */
8810 && !(it->bidi_p
8811 && it->bidi_it.scan_dir == -1))
8812 skip = MOVE_POS_MATCH_OR_ZV;
8813 else
8814 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8816 switch (skip)
8818 case MOVE_POS_MATCH_OR_ZV:
8819 reached = 8;
8820 goto out;
8822 case MOVE_NEWLINE_OR_CR:
8823 set_iterator_to_next (it, 1);
8824 it->continuation_lines_width = 0;
8825 break;
8827 case MOVE_LINE_TRUNCATED:
8828 it->continuation_lines_width = 0;
8829 reseat_at_next_visible_line_start (it, 0);
8830 if ((op & MOVE_TO_POS) != 0
8831 && IT_CHARPOS (*it) > to_charpos)
8833 reached = 9;
8834 goto out;
8836 break;
8838 case MOVE_LINE_CONTINUED:
8839 /* For continued lines ending in a tab, some of the glyphs
8840 associated with the tab are displayed on the current
8841 line. Since it->current_x does not include these glyphs,
8842 we use it->last_visible_x instead. */
8843 if (it->c == '\t')
8845 it->continuation_lines_width += it->last_visible_x;
8846 /* When moving by vpos, ensure that the iterator really
8847 advances to the next line (bug#847, bug#969). Fixme:
8848 do we need to do this in other circumstances? */
8849 if (it->current_x != it->last_visible_x
8850 && (op & MOVE_TO_VPOS)
8851 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8853 line_start_x = it->current_x + it->pixel_width
8854 - it->last_visible_x;
8855 set_iterator_to_next (it, 0);
8858 else
8859 it->continuation_lines_width += it->current_x;
8860 break;
8862 default:
8863 abort ();
8866 /* Reset/increment for the next run. */
8867 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8868 it->current_x = line_start_x;
8869 line_start_x = 0;
8870 it->hpos = 0;
8871 it->current_y += it->max_ascent + it->max_descent;
8872 ++it->vpos;
8873 last_height = it->max_ascent + it->max_descent;
8874 last_max_ascent = it->max_ascent;
8875 it->max_ascent = it->max_descent = 0;
8878 out:
8880 /* On text terminals, we may stop at the end of a line in the middle
8881 of a multi-character glyph. If the glyph itself is continued,
8882 i.e. it is actually displayed on the next line, don't treat this
8883 stopping point as valid; move to the next line instead (unless
8884 that brings us offscreen). */
8885 if (!FRAME_WINDOW_P (it->f)
8886 && op & MOVE_TO_POS
8887 && IT_CHARPOS (*it) == to_charpos
8888 && it->what == IT_CHARACTER
8889 && it->nglyphs > 1
8890 && it->line_wrap == WINDOW_WRAP
8891 && it->current_x == it->last_visible_x - 1
8892 && it->c != '\n'
8893 && it->c != '\t'
8894 && it->vpos < XFASTINT (it->w->window_end_vpos))
8896 it->continuation_lines_width += it->current_x;
8897 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8898 it->current_y += it->max_ascent + it->max_descent;
8899 ++it->vpos;
8900 last_height = it->max_ascent + it->max_descent;
8901 last_max_ascent = it->max_ascent;
8904 if (backup_data)
8905 bidi_unshelve_cache (backup_data, 1);
8907 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8911 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8913 If DY > 0, move IT backward at least that many pixels. DY = 0
8914 means move IT backward to the preceding line start or BEGV. This
8915 function may move over more than DY pixels if IT->current_y - DY
8916 ends up in the middle of a line; in this case IT->current_y will be
8917 set to the top of the line moved to. */
8919 void
8920 move_it_vertically_backward (struct it *it, int dy)
8922 int nlines, h;
8923 struct it it2, it3;
8924 void *it2data = NULL, *it3data = NULL;
8925 ptrdiff_t start_pos;
8927 move_further_back:
8928 eassert (dy >= 0);
8930 start_pos = IT_CHARPOS (*it);
8932 /* Estimate how many newlines we must move back. */
8933 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8935 /* Set the iterator's position that many lines back. */
8936 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8937 back_to_previous_visible_line_start (it);
8939 /* Reseat the iterator here. When moving backward, we don't want
8940 reseat to skip forward over invisible text, set up the iterator
8941 to deliver from overlay strings at the new position etc. So,
8942 use reseat_1 here. */
8943 reseat_1 (it, it->current.pos, 1);
8945 /* We are now surely at a line start. */
8946 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8947 reordering is in effect. */
8948 it->continuation_lines_width = 0;
8950 /* Move forward and see what y-distance we moved. First move to the
8951 start of the next line so that we get its height. We need this
8952 height to be able to tell whether we reached the specified
8953 y-distance. */
8954 SAVE_IT (it2, *it, it2data);
8955 it2.max_ascent = it2.max_descent = 0;
8958 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8959 MOVE_TO_POS | MOVE_TO_VPOS);
8961 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8962 /* If we are in a display string which starts at START_POS,
8963 and that display string includes a newline, and we are
8964 right after that newline (i.e. at the beginning of a
8965 display line), exit the loop, because otherwise we will
8966 infloop, since move_it_to will see that it is already at
8967 START_POS and will not move. */
8968 || (it2.method == GET_FROM_STRING
8969 && IT_CHARPOS (it2) == start_pos
8970 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8971 eassert (IT_CHARPOS (*it) >= BEGV);
8972 SAVE_IT (it3, it2, it3data);
8974 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8975 eassert (IT_CHARPOS (*it) >= BEGV);
8976 /* H is the actual vertical distance from the position in *IT
8977 and the starting position. */
8978 h = it2.current_y - it->current_y;
8979 /* NLINES is the distance in number of lines. */
8980 nlines = it2.vpos - it->vpos;
8982 /* Correct IT's y and vpos position
8983 so that they are relative to the starting point. */
8984 it->vpos -= nlines;
8985 it->current_y -= h;
8987 if (dy == 0)
8989 /* DY == 0 means move to the start of the screen line. The
8990 value of nlines is > 0 if continuation lines were involved,
8991 or if the original IT position was at start of a line. */
8992 RESTORE_IT (it, it, it2data);
8993 if (nlines > 0)
8994 move_it_by_lines (it, nlines);
8995 /* The above code moves us to some position NLINES down,
8996 usually to its first glyph (leftmost in an L2R line), but
8997 that's not necessarily the start of the line, under bidi
8998 reordering. We want to get to the character position
8999 that is immediately after the newline of the previous
9000 line. */
9001 if (it->bidi_p
9002 && !it->continuation_lines_width
9003 && !STRINGP (it->string)
9004 && IT_CHARPOS (*it) > BEGV
9005 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9007 ptrdiff_t nl_pos =
9008 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9010 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9012 bidi_unshelve_cache (it3data, 1);
9014 else
9016 /* The y-position we try to reach, relative to *IT.
9017 Note that H has been subtracted in front of the if-statement. */
9018 int target_y = it->current_y + h - dy;
9019 int y0 = it3.current_y;
9020 int y1;
9021 int line_height;
9023 RESTORE_IT (&it3, &it3, it3data);
9024 y1 = line_bottom_y (&it3);
9025 line_height = y1 - y0;
9026 RESTORE_IT (it, it, it2data);
9027 /* If we did not reach target_y, try to move further backward if
9028 we can. If we moved too far backward, try to move forward. */
9029 if (target_y < it->current_y
9030 /* This is heuristic. In a window that's 3 lines high, with
9031 a line height of 13 pixels each, recentering with point
9032 on the bottom line will try to move -39/2 = 19 pixels
9033 backward. Try to avoid moving into the first line. */
9034 && (it->current_y - target_y
9035 > min (window_box_height (it->w), line_height * 2 / 3))
9036 && IT_CHARPOS (*it) > BEGV)
9038 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9039 target_y - it->current_y));
9040 dy = it->current_y - target_y;
9041 goto move_further_back;
9043 else if (target_y >= it->current_y + line_height
9044 && IT_CHARPOS (*it) < ZV)
9046 /* Should move forward by at least one line, maybe more.
9048 Note: Calling move_it_by_lines can be expensive on
9049 terminal frames, where compute_motion is used (via
9050 vmotion) to do the job, when there are very long lines
9051 and truncate-lines is nil. That's the reason for
9052 treating terminal frames specially here. */
9054 if (!FRAME_WINDOW_P (it->f))
9055 move_it_vertically (it, target_y - (it->current_y + line_height));
9056 else
9060 move_it_by_lines (it, 1);
9062 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9069 /* Move IT by a specified amount of pixel lines DY. DY negative means
9070 move backwards. DY = 0 means move to start of screen line. At the
9071 end, IT will be on the start of a screen line. */
9073 void
9074 move_it_vertically (struct it *it, int dy)
9076 if (dy <= 0)
9077 move_it_vertically_backward (it, -dy);
9078 else
9080 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9081 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9082 MOVE_TO_POS | MOVE_TO_Y);
9083 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9085 /* If buffer ends in ZV without a newline, move to the start of
9086 the line to satisfy the post-condition. */
9087 if (IT_CHARPOS (*it) == ZV
9088 && ZV > BEGV
9089 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9090 move_it_by_lines (it, 0);
9095 /* Move iterator IT past the end of the text line it is in. */
9097 void
9098 move_it_past_eol (struct it *it)
9100 enum move_it_result rc;
9102 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9103 if (rc == MOVE_NEWLINE_OR_CR)
9104 set_iterator_to_next (it, 0);
9108 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9109 negative means move up. DVPOS == 0 means move to the start of the
9110 screen line.
9112 Optimization idea: If we would know that IT->f doesn't use
9113 a face with proportional font, we could be faster for
9114 truncate-lines nil. */
9116 void
9117 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9120 /* The commented-out optimization uses vmotion on terminals. This
9121 gives bad results, because elements like it->what, on which
9122 callers such as pos_visible_p rely, aren't updated. */
9123 /* struct position pos;
9124 if (!FRAME_WINDOW_P (it->f))
9126 struct text_pos textpos;
9128 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9129 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9130 reseat (it, textpos, 1);
9131 it->vpos += pos.vpos;
9132 it->current_y += pos.vpos;
9134 else */
9136 if (dvpos == 0)
9138 /* DVPOS == 0 means move to the start of the screen line. */
9139 move_it_vertically_backward (it, 0);
9140 /* Let next call to line_bottom_y calculate real line height */
9141 last_height = 0;
9143 else if (dvpos > 0)
9145 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9146 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9148 /* Only move to the next buffer position if we ended up in a
9149 string from display property, not in an overlay string
9150 (before-string or after-string). That is because the
9151 latter don't conceal the underlying buffer position, so
9152 we can ask to move the iterator to the exact position we
9153 are interested in. Note that, even if we are already at
9154 IT_CHARPOS (*it), the call below is not a no-op, as it
9155 will detect that we are at the end of the string, pop the
9156 iterator, and compute it->current_x and it->hpos
9157 correctly. */
9158 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9159 -1, -1, -1, MOVE_TO_POS);
9162 else
9164 struct it it2;
9165 void *it2data = NULL;
9166 ptrdiff_t start_charpos, i;
9168 /* Start at the beginning of the screen line containing IT's
9169 position. This may actually move vertically backwards,
9170 in case of overlays, so adjust dvpos accordingly. */
9171 dvpos += it->vpos;
9172 move_it_vertically_backward (it, 0);
9173 dvpos -= it->vpos;
9175 /* Go back -DVPOS visible lines and reseat the iterator there. */
9176 start_charpos = IT_CHARPOS (*it);
9177 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9178 back_to_previous_visible_line_start (it);
9179 reseat (it, it->current.pos, 1);
9181 /* Move further back if we end up in a string or an image. */
9182 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9184 /* First try to move to start of display line. */
9185 dvpos += it->vpos;
9186 move_it_vertically_backward (it, 0);
9187 dvpos -= it->vpos;
9188 if (IT_POS_VALID_AFTER_MOVE_P (it))
9189 break;
9190 /* If start of line is still in string or image,
9191 move further back. */
9192 back_to_previous_visible_line_start (it);
9193 reseat (it, it->current.pos, 1);
9194 dvpos--;
9197 it->current_x = it->hpos = 0;
9199 /* Above call may have moved too far if continuation lines
9200 are involved. Scan forward and see if it did. */
9201 SAVE_IT (it2, *it, it2data);
9202 it2.vpos = it2.current_y = 0;
9203 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9204 it->vpos -= it2.vpos;
9205 it->current_y -= it2.current_y;
9206 it->current_x = it->hpos = 0;
9208 /* If we moved too far back, move IT some lines forward. */
9209 if (it2.vpos > -dvpos)
9211 int delta = it2.vpos + dvpos;
9213 RESTORE_IT (&it2, &it2, it2data);
9214 SAVE_IT (it2, *it, it2data);
9215 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9216 /* Move back again if we got too far ahead. */
9217 if (IT_CHARPOS (*it) >= start_charpos)
9218 RESTORE_IT (it, &it2, it2data);
9219 else
9220 bidi_unshelve_cache (it2data, 1);
9222 else
9223 RESTORE_IT (it, it, it2data);
9227 /* Return 1 if IT points into the middle of a display vector. */
9230 in_display_vector_p (struct it *it)
9232 return (it->method == GET_FROM_DISPLAY_VECTOR
9233 && it->current.dpvec_index > 0
9234 && it->dpvec + it->current.dpvec_index != it->dpend);
9238 /***********************************************************************
9239 Messages
9240 ***********************************************************************/
9243 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9244 to *Messages*. */
9246 void
9247 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9249 Lisp_Object args[3];
9250 Lisp_Object msg, fmt;
9251 char *buffer;
9252 ptrdiff_t len;
9253 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9254 USE_SAFE_ALLOCA;
9256 /* Do nothing if called asynchronously. Inserting text into
9257 a buffer may call after-change-functions and alike and
9258 that would means running Lisp asynchronously. */
9259 if (handling_signal)
9260 return;
9262 fmt = msg = Qnil;
9263 GCPRO4 (fmt, msg, arg1, arg2);
9265 args[0] = fmt = build_string (format);
9266 args[1] = arg1;
9267 args[2] = arg2;
9268 msg = Fformat (3, args);
9270 len = SBYTES (msg) + 1;
9271 buffer = SAFE_ALLOCA (len);
9272 memcpy (buffer, SDATA (msg), len);
9274 message_dolog (buffer, len - 1, 1, 0);
9275 SAFE_FREE ();
9277 UNGCPRO;
9281 /* Output a newline in the *Messages* buffer if "needs" one. */
9283 void
9284 message_log_maybe_newline (void)
9286 if (message_log_need_newline)
9287 message_dolog ("", 0, 1, 0);
9291 /* Add a string M of length NBYTES to the message log, optionally
9292 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9293 nonzero, means interpret the contents of M as multibyte. This
9294 function calls low-level routines in order to bypass text property
9295 hooks, etc. which might not be safe to run.
9297 This may GC (insert may run before/after change hooks),
9298 so the buffer M must NOT point to a Lisp string. */
9300 void
9301 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9303 const unsigned char *msg = (const unsigned char *) m;
9305 if (!NILP (Vmemory_full))
9306 return;
9308 if (!NILP (Vmessage_log_max))
9310 struct buffer *oldbuf;
9311 Lisp_Object oldpoint, oldbegv, oldzv;
9312 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9313 ptrdiff_t point_at_end = 0;
9314 ptrdiff_t zv_at_end = 0;
9315 Lisp_Object old_deactivate_mark, tem;
9316 struct gcpro gcpro1;
9318 old_deactivate_mark = Vdeactivate_mark;
9319 oldbuf = current_buffer;
9320 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9321 BSET (current_buffer, undo_list, Qt);
9323 oldpoint = message_dolog_marker1;
9324 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9325 oldbegv = message_dolog_marker2;
9326 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9327 oldzv = message_dolog_marker3;
9328 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9329 GCPRO1 (old_deactivate_mark);
9331 if (PT == Z)
9332 point_at_end = 1;
9333 if (ZV == Z)
9334 zv_at_end = 1;
9336 BEGV = BEG;
9337 BEGV_BYTE = BEG_BYTE;
9338 ZV = Z;
9339 ZV_BYTE = Z_BYTE;
9340 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9342 /* Insert the string--maybe converting multibyte to single byte
9343 or vice versa, so that all the text fits the buffer. */
9344 if (multibyte
9345 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9347 ptrdiff_t i;
9348 int c, char_bytes;
9349 char work[1];
9351 /* Convert a multibyte string to single-byte
9352 for the *Message* buffer. */
9353 for (i = 0; i < nbytes; i += char_bytes)
9355 c = string_char_and_length (msg + i, &char_bytes);
9356 work[0] = (ASCII_CHAR_P (c)
9358 : multibyte_char_to_unibyte (c));
9359 insert_1_both (work, 1, 1, 1, 0, 0);
9362 else if (! multibyte
9363 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9365 ptrdiff_t i;
9366 int c, char_bytes;
9367 unsigned char str[MAX_MULTIBYTE_LENGTH];
9368 /* Convert a single-byte string to multibyte
9369 for the *Message* buffer. */
9370 for (i = 0; i < nbytes; i++)
9372 c = msg[i];
9373 MAKE_CHAR_MULTIBYTE (c);
9374 char_bytes = CHAR_STRING (c, str);
9375 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9378 else if (nbytes)
9379 insert_1 (m, nbytes, 1, 0, 0);
9381 if (nlflag)
9383 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9384 printmax_t dups;
9385 insert_1 ("\n", 1, 1, 0, 0);
9387 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9388 this_bol = PT;
9389 this_bol_byte = PT_BYTE;
9391 /* See if this line duplicates the previous one.
9392 If so, combine duplicates. */
9393 if (this_bol > BEG)
9395 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9396 prev_bol = PT;
9397 prev_bol_byte = PT_BYTE;
9399 dups = message_log_check_duplicate (prev_bol_byte,
9400 this_bol_byte);
9401 if (dups)
9403 del_range_both (prev_bol, prev_bol_byte,
9404 this_bol, this_bol_byte, 0);
9405 if (dups > 1)
9407 char dupstr[sizeof " [ times]"
9408 + INT_STRLEN_BOUND (printmax_t)];
9410 /* If you change this format, don't forget to also
9411 change message_log_check_duplicate. */
9412 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9413 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9414 insert_1 (dupstr, duplen, 1, 0, 1);
9419 /* If we have more than the desired maximum number of lines
9420 in the *Messages* buffer now, delete the oldest ones.
9421 This is safe because we don't have undo in this buffer. */
9423 if (NATNUMP (Vmessage_log_max))
9425 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9426 -XFASTINT (Vmessage_log_max) - 1, 0);
9427 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9430 BEGV = XMARKER (oldbegv)->charpos;
9431 BEGV_BYTE = marker_byte_position (oldbegv);
9433 if (zv_at_end)
9435 ZV = Z;
9436 ZV_BYTE = Z_BYTE;
9438 else
9440 ZV = XMARKER (oldzv)->charpos;
9441 ZV_BYTE = marker_byte_position (oldzv);
9444 if (point_at_end)
9445 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9446 else
9447 /* We can't do Fgoto_char (oldpoint) because it will run some
9448 Lisp code. */
9449 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9450 XMARKER (oldpoint)->bytepos);
9452 UNGCPRO;
9453 unchain_marker (XMARKER (oldpoint));
9454 unchain_marker (XMARKER (oldbegv));
9455 unchain_marker (XMARKER (oldzv));
9457 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9458 set_buffer_internal (oldbuf);
9459 if (NILP (tem))
9460 windows_or_buffers_changed = old_windows_or_buffers_changed;
9461 message_log_need_newline = !nlflag;
9462 Vdeactivate_mark = old_deactivate_mark;
9467 /* We are at the end of the buffer after just having inserted a newline.
9468 (Note: We depend on the fact we won't be crossing the gap.)
9469 Check to see if the most recent message looks a lot like the previous one.
9470 Return 0 if different, 1 if the new one should just replace it, or a
9471 value N > 1 if we should also append " [N times]". */
9473 static intmax_t
9474 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9476 ptrdiff_t i;
9477 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9478 int seen_dots = 0;
9479 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9480 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9482 for (i = 0; i < len; i++)
9484 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9485 seen_dots = 1;
9486 if (p1[i] != p2[i])
9487 return seen_dots;
9489 p1 += len;
9490 if (*p1 == '\n')
9491 return 2;
9492 if (*p1++ == ' ' && *p1++ == '[')
9494 char *pend;
9495 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9496 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9497 return n+1;
9499 return 0;
9503 /* Display an echo area message M with a specified length of NBYTES
9504 bytes. The string may include null characters. If M is 0, clear
9505 out any existing message, and let the mini-buffer text show
9506 through.
9508 This may GC, so the buffer M must NOT point to a Lisp string. */
9510 void
9511 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9513 /* First flush out any partial line written with print. */
9514 message_log_maybe_newline ();
9515 if (m)
9516 message_dolog (m, nbytes, 1, multibyte);
9517 message2_nolog (m, nbytes, multibyte);
9521 /* The non-logging counterpart of message2. */
9523 void
9524 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9526 struct frame *sf = SELECTED_FRAME ();
9527 message_enable_multibyte = multibyte;
9529 if (FRAME_INITIAL_P (sf))
9531 if (noninteractive_need_newline)
9532 putc ('\n', stderr);
9533 noninteractive_need_newline = 0;
9534 if (m)
9535 fwrite (m, nbytes, 1, stderr);
9536 if (cursor_in_echo_area == 0)
9537 fprintf (stderr, "\n");
9538 fflush (stderr);
9540 /* A null message buffer means that the frame hasn't really been
9541 initialized yet. Error messages get reported properly by
9542 cmd_error, so this must be just an informative message; toss it. */
9543 else if (INTERACTIVE
9544 && sf->glyphs_initialized_p
9545 && FRAME_MESSAGE_BUF (sf))
9547 Lisp_Object mini_window;
9548 struct frame *f;
9550 /* Get the frame containing the mini-buffer
9551 that the selected frame is using. */
9552 mini_window = FRAME_MINIBUF_WINDOW (sf);
9553 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9555 FRAME_SAMPLE_VISIBILITY (f);
9556 if (FRAME_VISIBLE_P (sf)
9557 && ! FRAME_VISIBLE_P (f))
9558 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9560 if (m)
9562 set_message (m, Qnil, nbytes, multibyte);
9563 if (minibuffer_auto_raise)
9564 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9566 else
9567 clear_message (1, 1);
9569 do_pending_window_change (0);
9570 echo_area_display (1);
9571 do_pending_window_change (0);
9572 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9573 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9578 /* Display an echo area message M with a specified length of NBYTES
9579 bytes. The string may include null characters. If M is not a
9580 string, clear out any existing message, and let the mini-buffer
9581 text show through.
9583 This function cancels echoing. */
9585 void
9586 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9588 struct gcpro gcpro1;
9590 GCPRO1 (m);
9591 clear_message (1,1);
9592 cancel_echoing ();
9594 /* First flush out any partial line written with print. */
9595 message_log_maybe_newline ();
9596 if (STRINGP (m))
9598 USE_SAFE_ALLOCA;
9599 char *buffer = SAFE_ALLOCA (nbytes);
9600 memcpy (buffer, SDATA (m), nbytes);
9601 message_dolog (buffer, nbytes, 1, multibyte);
9602 SAFE_FREE ();
9604 message3_nolog (m, nbytes, multibyte);
9606 UNGCPRO;
9610 /* The non-logging version of message3.
9611 This does not cancel echoing, because it is used for echoing.
9612 Perhaps we need to make a separate function for echoing
9613 and make this cancel echoing. */
9615 void
9616 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9618 struct frame *sf = SELECTED_FRAME ();
9619 message_enable_multibyte = multibyte;
9621 if (FRAME_INITIAL_P (sf))
9623 if (noninteractive_need_newline)
9624 putc ('\n', stderr);
9625 noninteractive_need_newline = 0;
9626 if (STRINGP (m))
9627 fwrite (SDATA (m), nbytes, 1, stderr);
9628 if (cursor_in_echo_area == 0)
9629 fprintf (stderr, "\n");
9630 fflush (stderr);
9632 /* A null message buffer means that the frame hasn't really been
9633 initialized yet. Error messages get reported properly by
9634 cmd_error, so this must be just an informative message; toss it. */
9635 else if (INTERACTIVE
9636 && sf->glyphs_initialized_p
9637 && FRAME_MESSAGE_BUF (sf))
9639 Lisp_Object mini_window;
9640 Lisp_Object frame;
9641 struct frame *f;
9643 /* Get the frame containing the mini-buffer
9644 that the selected frame is using. */
9645 mini_window = FRAME_MINIBUF_WINDOW (sf);
9646 frame = XWINDOW (mini_window)->frame;
9647 f = XFRAME (frame);
9649 FRAME_SAMPLE_VISIBILITY (f);
9650 if (FRAME_VISIBLE_P (sf)
9651 && !FRAME_VISIBLE_P (f))
9652 Fmake_frame_visible (frame);
9654 if (STRINGP (m) && SCHARS (m) > 0)
9656 set_message (NULL, m, nbytes, multibyte);
9657 if (minibuffer_auto_raise)
9658 Fraise_frame (frame);
9659 /* Assume we are not echoing.
9660 (If we are, echo_now will override this.) */
9661 echo_message_buffer = Qnil;
9663 else
9664 clear_message (1, 1);
9666 do_pending_window_change (0);
9667 echo_area_display (1);
9668 do_pending_window_change (0);
9669 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9670 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9675 /* Display a null-terminated echo area message M. If M is 0, clear
9676 out any existing message, and let the mini-buffer text show through.
9678 The buffer M must continue to exist until after the echo area gets
9679 cleared or some other message gets displayed there. Do not pass
9680 text that is stored in a Lisp string. Do not pass text in a buffer
9681 that was alloca'd. */
9683 void
9684 message1 (const char *m)
9686 message2 (m, (m ? strlen (m) : 0), 0);
9690 /* The non-logging counterpart of message1. */
9692 void
9693 message1_nolog (const char *m)
9695 message2_nolog (m, (m ? strlen (m) : 0), 0);
9698 /* Display a message M which contains a single %s
9699 which gets replaced with STRING. */
9701 void
9702 message_with_string (const char *m, Lisp_Object string, int log)
9704 CHECK_STRING (string);
9706 if (noninteractive)
9708 if (m)
9710 if (noninteractive_need_newline)
9711 putc ('\n', stderr);
9712 noninteractive_need_newline = 0;
9713 fprintf (stderr, m, SDATA (string));
9714 if (!cursor_in_echo_area)
9715 fprintf (stderr, "\n");
9716 fflush (stderr);
9719 else if (INTERACTIVE)
9721 /* The frame whose minibuffer we're going to display the message on.
9722 It may be larger than the selected frame, so we need
9723 to use its buffer, not the selected frame's buffer. */
9724 Lisp_Object mini_window;
9725 struct frame *f, *sf = SELECTED_FRAME ();
9727 /* Get the frame containing the minibuffer
9728 that the selected frame is using. */
9729 mini_window = FRAME_MINIBUF_WINDOW (sf);
9730 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9732 /* A null message buffer means that the frame hasn't really been
9733 initialized yet. Error messages get reported properly by
9734 cmd_error, so this must be just an informative message; toss it. */
9735 if (FRAME_MESSAGE_BUF (f))
9737 Lisp_Object args[2], msg;
9738 struct gcpro gcpro1, gcpro2;
9740 args[0] = build_string (m);
9741 args[1] = msg = string;
9742 GCPRO2 (args[0], msg);
9743 gcpro1.nvars = 2;
9745 msg = Fformat (2, args);
9747 if (log)
9748 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9749 else
9750 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9752 UNGCPRO;
9754 /* Print should start at the beginning of the message
9755 buffer next time. */
9756 message_buf_print = 0;
9762 /* Dump an informative message to the minibuf. If M is 0, clear out
9763 any existing message, and let the mini-buffer text show through. */
9765 static void
9766 vmessage (const char *m, va_list ap)
9768 if (noninteractive)
9770 if (m)
9772 if (noninteractive_need_newline)
9773 putc ('\n', stderr);
9774 noninteractive_need_newline = 0;
9775 vfprintf (stderr, m, ap);
9776 if (cursor_in_echo_area == 0)
9777 fprintf (stderr, "\n");
9778 fflush (stderr);
9781 else if (INTERACTIVE)
9783 /* The frame whose mini-buffer we're going to display the message
9784 on. It may be larger than the selected frame, so we need to
9785 use its buffer, not the selected frame's buffer. */
9786 Lisp_Object mini_window;
9787 struct frame *f, *sf = SELECTED_FRAME ();
9789 /* Get the frame containing the mini-buffer
9790 that the selected frame is using. */
9791 mini_window = FRAME_MINIBUF_WINDOW (sf);
9792 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9794 /* A null message buffer means that the frame hasn't really been
9795 initialized yet. Error messages get reported properly by
9796 cmd_error, so this must be just an informative message; toss
9797 it. */
9798 if (FRAME_MESSAGE_BUF (f))
9800 if (m)
9802 ptrdiff_t len;
9804 len = doprnt (FRAME_MESSAGE_BUF (f),
9805 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9807 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9809 else
9810 message1 (0);
9812 /* Print should start at the beginning of the message
9813 buffer next time. */
9814 message_buf_print = 0;
9819 void
9820 message (const char *m, ...)
9822 va_list ap;
9823 va_start (ap, m);
9824 vmessage (m, ap);
9825 va_end (ap);
9829 #if 0
9830 /* The non-logging version of message. */
9832 void
9833 message_nolog (const char *m, ...)
9835 Lisp_Object old_log_max;
9836 va_list ap;
9837 va_start (ap, m);
9838 old_log_max = Vmessage_log_max;
9839 Vmessage_log_max = Qnil;
9840 vmessage (m, ap);
9841 Vmessage_log_max = old_log_max;
9842 va_end (ap);
9844 #endif
9847 /* Display the current message in the current mini-buffer. This is
9848 only called from error handlers in process.c, and is not time
9849 critical. */
9851 void
9852 update_echo_area (void)
9854 if (!NILP (echo_area_buffer[0]))
9856 Lisp_Object string;
9857 string = Fcurrent_message ();
9858 message3 (string, SBYTES (string),
9859 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9864 /* Make sure echo area buffers in `echo_buffers' are live.
9865 If they aren't, make new ones. */
9867 static void
9868 ensure_echo_area_buffers (void)
9870 int i;
9872 for (i = 0; i < 2; ++i)
9873 if (!BUFFERP (echo_buffer[i])
9874 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9876 char name[30];
9877 Lisp_Object old_buffer;
9878 int j;
9880 old_buffer = echo_buffer[i];
9881 echo_buffer[i] = Fget_buffer_create
9882 (make_formatted_string (name, " *Echo Area %d*", i));
9883 BSET (XBUFFER (echo_buffer[i]), truncate_lines, Qnil);
9884 /* to force word wrap in echo area -
9885 it was decided to postpone this*/
9886 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9888 for (j = 0; j < 2; ++j)
9889 if (EQ (old_buffer, echo_area_buffer[j]))
9890 echo_area_buffer[j] = echo_buffer[i];
9895 /* Call FN with args A1..A4 with either the current or last displayed
9896 echo_area_buffer as current buffer.
9898 WHICH zero means use the current message buffer
9899 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9900 from echo_buffer[] and clear it.
9902 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9903 suitable buffer from echo_buffer[] and clear it.
9905 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9906 that the current message becomes the last displayed one, make
9907 choose a suitable buffer for echo_area_buffer[0], and clear it.
9909 Value is what FN returns. */
9911 static int
9912 with_echo_area_buffer (struct window *w, int which,
9913 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9914 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9916 Lisp_Object buffer;
9917 int this_one, the_other, clear_buffer_p, rc;
9918 ptrdiff_t count = SPECPDL_INDEX ();
9920 /* If buffers aren't live, make new ones. */
9921 ensure_echo_area_buffers ();
9923 clear_buffer_p = 0;
9925 if (which == 0)
9926 this_one = 0, the_other = 1;
9927 else if (which > 0)
9928 this_one = 1, the_other = 0;
9929 else
9931 this_one = 0, the_other = 1;
9932 clear_buffer_p = 1;
9934 /* We need a fresh one in case the current echo buffer equals
9935 the one containing the last displayed echo area message. */
9936 if (!NILP (echo_area_buffer[this_one])
9937 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9938 echo_area_buffer[this_one] = Qnil;
9941 /* Choose a suitable buffer from echo_buffer[] is we don't
9942 have one. */
9943 if (NILP (echo_area_buffer[this_one]))
9945 echo_area_buffer[this_one]
9946 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9947 ? echo_buffer[the_other]
9948 : echo_buffer[this_one]);
9949 clear_buffer_p = 1;
9952 buffer = echo_area_buffer[this_one];
9954 /* Don't get confused by reusing the buffer used for echoing
9955 for a different purpose. */
9956 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9957 cancel_echoing ();
9959 record_unwind_protect (unwind_with_echo_area_buffer,
9960 with_echo_area_buffer_unwind_data (w));
9962 /* Make the echo area buffer current. Note that for display
9963 purposes, it is not necessary that the displayed window's buffer
9964 == current_buffer, except for text property lookup. So, let's
9965 only set that buffer temporarily here without doing a full
9966 Fset_window_buffer. We must also change w->pointm, though,
9967 because otherwise an assertions in unshow_buffer fails, and Emacs
9968 aborts. */
9969 set_buffer_internal_1 (XBUFFER (buffer));
9970 if (w)
9972 WSET (w, buffer, buffer);
9973 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9976 BSET (current_buffer, undo_list, Qt);
9977 BSET (current_buffer, read_only, Qnil);
9978 specbind (Qinhibit_read_only, Qt);
9979 specbind (Qinhibit_modification_hooks, Qt);
9981 if (clear_buffer_p && Z > BEG)
9982 del_range (BEG, Z);
9984 eassert (BEGV >= BEG);
9985 eassert (ZV <= Z && ZV >= BEGV);
9987 rc = fn (a1, a2, a3, a4);
9989 eassert (BEGV >= BEG);
9990 eassert (ZV <= Z && ZV >= BEGV);
9992 unbind_to (count, Qnil);
9993 return rc;
9997 /* Save state that should be preserved around the call to the function
9998 FN called in with_echo_area_buffer. */
10000 static Lisp_Object
10001 with_echo_area_buffer_unwind_data (struct window *w)
10003 int i = 0;
10004 Lisp_Object vector, tmp;
10006 /* Reduce consing by keeping one vector in
10007 Vwith_echo_area_save_vector. */
10008 vector = Vwith_echo_area_save_vector;
10009 Vwith_echo_area_save_vector = Qnil;
10011 if (NILP (vector))
10012 vector = Fmake_vector (make_number (7), Qnil);
10014 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10015 ASET (vector, i, Vdeactivate_mark); ++i;
10016 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10018 if (w)
10020 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10021 ASET (vector, i, w->buffer); ++i;
10022 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10023 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10025 else
10027 int end = i + 4;
10028 for (; i < end; ++i)
10029 ASET (vector, i, Qnil);
10032 eassert (i == ASIZE (vector));
10033 return vector;
10037 /* Restore global state from VECTOR which was created by
10038 with_echo_area_buffer_unwind_data. */
10040 static Lisp_Object
10041 unwind_with_echo_area_buffer (Lisp_Object vector)
10043 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10044 Vdeactivate_mark = AREF (vector, 1);
10045 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10047 if (WINDOWP (AREF (vector, 3)))
10049 struct window *w;
10050 Lisp_Object buffer, charpos, bytepos;
10052 w = XWINDOW (AREF (vector, 3));
10053 buffer = AREF (vector, 4);
10054 charpos = AREF (vector, 5);
10055 bytepos = AREF (vector, 6);
10057 WSET (w, buffer, buffer);
10058 set_marker_both (w->pointm, buffer,
10059 XFASTINT (charpos), XFASTINT (bytepos));
10062 Vwith_echo_area_save_vector = vector;
10063 return Qnil;
10067 /* Set up the echo area for use by print functions. MULTIBYTE_P
10068 non-zero means we will print multibyte. */
10070 void
10071 setup_echo_area_for_printing (int multibyte_p)
10073 /* If we can't find an echo area any more, exit. */
10074 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10075 Fkill_emacs (Qnil);
10077 ensure_echo_area_buffers ();
10079 if (!message_buf_print)
10081 /* A message has been output since the last time we printed.
10082 Choose a fresh echo area buffer. */
10083 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10084 echo_area_buffer[0] = echo_buffer[1];
10085 else
10086 echo_area_buffer[0] = echo_buffer[0];
10088 /* Switch to that buffer and clear it. */
10089 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10090 BSET (current_buffer, truncate_lines, Qnil);
10092 if (Z > BEG)
10094 ptrdiff_t count = SPECPDL_INDEX ();
10095 specbind (Qinhibit_read_only, Qt);
10096 /* Note that undo recording is always disabled. */
10097 del_range (BEG, Z);
10098 unbind_to (count, Qnil);
10100 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10102 /* Set up the buffer for the multibyteness we need. */
10103 if (multibyte_p
10104 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10105 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10107 /* Raise the frame containing the echo area. */
10108 if (minibuffer_auto_raise)
10110 struct frame *sf = SELECTED_FRAME ();
10111 Lisp_Object mini_window;
10112 mini_window = FRAME_MINIBUF_WINDOW (sf);
10113 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10116 message_log_maybe_newline ();
10117 message_buf_print = 1;
10119 else
10121 if (NILP (echo_area_buffer[0]))
10123 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10124 echo_area_buffer[0] = echo_buffer[1];
10125 else
10126 echo_area_buffer[0] = echo_buffer[0];
10129 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10131 /* Someone switched buffers between print requests. */
10132 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10133 BSET (current_buffer, truncate_lines, Qnil);
10139 /* Display an echo area message in window W. Value is non-zero if W's
10140 height is changed. If display_last_displayed_message_p is
10141 non-zero, display the message that was last displayed, otherwise
10142 display the current message. */
10144 static int
10145 display_echo_area (struct window *w)
10147 int i, no_message_p, window_height_changed_p;
10149 /* Temporarily disable garbage collections while displaying the echo
10150 area. This is done because a GC can print a message itself.
10151 That message would modify the echo area buffer's contents while a
10152 redisplay of the buffer is going on, and seriously confuse
10153 redisplay. */
10154 ptrdiff_t count = inhibit_garbage_collection ();
10156 /* If there is no message, we must call display_echo_area_1
10157 nevertheless because it resizes the window. But we will have to
10158 reset the echo_area_buffer in question to nil at the end because
10159 with_echo_area_buffer will sets it to an empty buffer. */
10160 i = display_last_displayed_message_p ? 1 : 0;
10161 no_message_p = NILP (echo_area_buffer[i]);
10163 window_height_changed_p
10164 = with_echo_area_buffer (w, display_last_displayed_message_p,
10165 display_echo_area_1,
10166 (intptr_t) w, Qnil, 0, 0);
10168 if (no_message_p)
10169 echo_area_buffer[i] = Qnil;
10171 unbind_to (count, Qnil);
10172 return window_height_changed_p;
10176 /* Helper for display_echo_area. Display the current buffer which
10177 contains the current echo area message in window W, a mini-window,
10178 a pointer to which is passed in A1. A2..A4 are currently not used.
10179 Change the height of W so that all of the message is displayed.
10180 Value is non-zero if height of W was changed. */
10182 static int
10183 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10185 intptr_t i1 = a1;
10186 struct window *w = (struct window *) i1;
10187 Lisp_Object window;
10188 struct text_pos start;
10189 int window_height_changed_p = 0;
10191 /* Do this before displaying, so that we have a large enough glyph
10192 matrix for the display. If we can't get enough space for the
10193 whole text, display the last N lines. That works by setting w->start. */
10194 window_height_changed_p = resize_mini_window (w, 0);
10196 /* Use the starting position chosen by resize_mini_window. */
10197 SET_TEXT_POS_FROM_MARKER (start, w->start);
10199 /* Display. */
10200 clear_glyph_matrix (w->desired_matrix);
10201 XSETWINDOW (window, w);
10202 try_window (window, start, 0);
10204 return window_height_changed_p;
10208 /* Resize the echo area window to exactly the size needed for the
10209 currently displayed message, if there is one. If a mini-buffer
10210 is active, don't shrink it. */
10212 void
10213 resize_echo_area_exactly (void)
10215 if (BUFFERP (echo_area_buffer[0])
10216 && WINDOWP (echo_area_window))
10218 struct window *w = XWINDOW (echo_area_window);
10219 int resized_p;
10220 Lisp_Object resize_exactly;
10222 if (minibuf_level == 0)
10223 resize_exactly = Qt;
10224 else
10225 resize_exactly = Qnil;
10227 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10228 (intptr_t) w, resize_exactly,
10229 0, 0);
10230 if (resized_p)
10232 ++windows_or_buffers_changed;
10233 ++update_mode_lines;
10234 redisplay_internal ();
10240 /* Callback function for with_echo_area_buffer, when used from
10241 resize_echo_area_exactly. A1 contains a pointer to the window to
10242 resize, EXACTLY non-nil means resize the mini-window exactly to the
10243 size of the text displayed. A3 and A4 are not used. Value is what
10244 resize_mini_window returns. */
10246 static int
10247 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10249 intptr_t i1 = a1;
10250 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10254 /* Resize mini-window W to fit the size of its contents. EXACT_P
10255 means size the window exactly to the size needed. Otherwise, it's
10256 only enlarged until W's buffer is empty.
10258 Set W->start to the right place to begin display. If the whole
10259 contents fit, start at the beginning. Otherwise, start so as
10260 to make the end of the contents appear. This is particularly
10261 important for y-or-n-p, but seems desirable generally.
10263 Value is non-zero if the window height has been changed. */
10266 resize_mini_window (struct window *w, int exact_p)
10268 struct frame *f = XFRAME (w->frame);
10269 int window_height_changed_p = 0;
10271 eassert (MINI_WINDOW_P (w));
10273 /* By default, start display at the beginning. */
10274 set_marker_both (w->start, w->buffer,
10275 BUF_BEGV (XBUFFER (w->buffer)),
10276 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10278 /* Don't resize windows while redisplaying a window; it would
10279 confuse redisplay functions when the size of the window they are
10280 displaying changes from under them. Such a resizing can happen,
10281 for instance, when which-func prints a long message while
10282 we are running fontification-functions. We're running these
10283 functions with safe_call which binds inhibit-redisplay to t. */
10284 if (!NILP (Vinhibit_redisplay))
10285 return 0;
10287 /* Nil means don't try to resize. */
10288 if (NILP (Vresize_mini_windows)
10289 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10290 return 0;
10292 if (!FRAME_MINIBUF_ONLY_P (f))
10294 struct it it;
10295 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10296 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10297 int height;
10298 EMACS_INT max_height;
10299 int unit = FRAME_LINE_HEIGHT (f);
10300 struct text_pos start;
10301 struct buffer *old_current_buffer = NULL;
10303 if (current_buffer != XBUFFER (w->buffer))
10305 old_current_buffer = current_buffer;
10306 set_buffer_internal (XBUFFER (w->buffer));
10309 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10311 /* Compute the max. number of lines specified by the user. */
10312 if (FLOATP (Vmax_mini_window_height))
10313 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10314 else if (INTEGERP (Vmax_mini_window_height))
10315 max_height = XINT (Vmax_mini_window_height);
10316 else
10317 max_height = total_height / 4;
10319 /* Correct that max. height if it's bogus. */
10320 max_height = max (1, max_height);
10321 max_height = min (total_height, max_height);
10323 /* Find out the height of the text in the window. */
10324 if (it.line_wrap == TRUNCATE)
10325 height = 1;
10326 else
10328 last_height = 0;
10329 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10330 if (it.max_ascent == 0 && it.max_descent == 0)
10331 height = it.current_y + last_height;
10332 else
10333 height = it.current_y + it.max_ascent + it.max_descent;
10334 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10335 height = (height + unit - 1) / unit;
10338 /* Compute a suitable window start. */
10339 if (height > max_height)
10341 height = max_height;
10342 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10343 move_it_vertically_backward (&it, (height - 1) * unit);
10344 start = it.current.pos;
10346 else
10347 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10348 SET_MARKER_FROM_TEXT_POS (w->start, start);
10350 if (EQ (Vresize_mini_windows, Qgrow_only))
10352 /* Let it grow only, until we display an empty message, in which
10353 case the window shrinks again. */
10354 if (height > WINDOW_TOTAL_LINES (w))
10356 int old_height = WINDOW_TOTAL_LINES (w);
10357 freeze_window_starts (f, 1);
10358 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10359 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10361 else if (height < WINDOW_TOTAL_LINES (w)
10362 && (exact_p || BEGV == ZV))
10364 int old_height = WINDOW_TOTAL_LINES (w);
10365 freeze_window_starts (f, 0);
10366 shrink_mini_window (w);
10367 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10370 else
10372 /* Always resize to exact size needed. */
10373 if (height > WINDOW_TOTAL_LINES (w))
10375 int old_height = WINDOW_TOTAL_LINES (w);
10376 freeze_window_starts (f, 1);
10377 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10378 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10380 else if (height < WINDOW_TOTAL_LINES (w))
10382 int old_height = WINDOW_TOTAL_LINES (w);
10383 freeze_window_starts (f, 0);
10384 shrink_mini_window (w);
10386 if (height)
10388 freeze_window_starts (f, 1);
10389 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10392 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10396 if (old_current_buffer)
10397 set_buffer_internal (old_current_buffer);
10400 return window_height_changed_p;
10404 /* Value is the current message, a string, or nil if there is no
10405 current message. */
10407 Lisp_Object
10408 current_message (void)
10410 Lisp_Object msg;
10412 if (!BUFFERP (echo_area_buffer[0]))
10413 msg = Qnil;
10414 else
10416 with_echo_area_buffer (0, 0, current_message_1,
10417 (intptr_t) &msg, Qnil, 0, 0);
10418 if (NILP (msg))
10419 echo_area_buffer[0] = Qnil;
10422 return msg;
10426 static int
10427 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10429 intptr_t i1 = a1;
10430 Lisp_Object *msg = (Lisp_Object *) i1;
10432 if (Z > BEG)
10433 *msg = make_buffer_string (BEG, Z, 1);
10434 else
10435 *msg = Qnil;
10436 return 0;
10440 /* Push the current message on Vmessage_stack for later restoration
10441 by restore_message. Value is non-zero if the current message isn't
10442 empty. This is a relatively infrequent operation, so it's not
10443 worth optimizing. */
10446 push_message (void)
10448 Lisp_Object msg;
10449 msg = current_message ();
10450 Vmessage_stack = Fcons (msg, Vmessage_stack);
10451 return STRINGP (msg);
10455 /* Restore message display from the top of Vmessage_stack. */
10457 void
10458 restore_message (void)
10460 Lisp_Object msg;
10462 eassert (CONSP (Vmessage_stack));
10463 msg = XCAR (Vmessage_stack);
10464 if (STRINGP (msg))
10465 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10466 else
10467 message3_nolog (msg, 0, 0);
10471 /* Handler for record_unwind_protect calling pop_message. */
10473 Lisp_Object
10474 pop_message_unwind (Lisp_Object dummy)
10476 pop_message ();
10477 return Qnil;
10480 /* Pop the top-most entry off Vmessage_stack. */
10482 static void
10483 pop_message (void)
10485 eassert (CONSP (Vmessage_stack));
10486 Vmessage_stack = XCDR (Vmessage_stack);
10490 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10491 exits. If the stack is not empty, we have a missing pop_message
10492 somewhere. */
10494 void
10495 check_message_stack (void)
10497 if (!NILP (Vmessage_stack))
10498 abort ();
10502 /* Truncate to NCHARS what will be displayed in the echo area the next
10503 time we display it---but don't redisplay it now. */
10505 void
10506 truncate_echo_area (ptrdiff_t nchars)
10508 if (nchars == 0)
10509 echo_area_buffer[0] = Qnil;
10510 /* A null message buffer means that the frame hasn't really been
10511 initialized yet. Error messages get reported properly by
10512 cmd_error, so this must be just an informative message; toss it. */
10513 else if (!noninteractive
10514 && INTERACTIVE
10515 && !NILP (echo_area_buffer[0]))
10517 struct frame *sf = SELECTED_FRAME ();
10518 if (FRAME_MESSAGE_BUF (sf))
10519 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10524 /* Helper function for truncate_echo_area. Truncate the current
10525 message to at most NCHARS characters. */
10527 static int
10528 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10530 if (BEG + nchars < Z)
10531 del_range (BEG + nchars, Z);
10532 if (Z == BEG)
10533 echo_area_buffer[0] = Qnil;
10534 return 0;
10538 /* Set the current message to a substring of S or STRING.
10540 If STRING is a Lisp string, set the message to the first NBYTES
10541 bytes from STRING. NBYTES zero means use the whole string. If
10542 STRING is multibyte, the message will be displayed multibyte.
10544 If S is not null, set the message to the first LEN bytes of S. LEN
10545 zero means use the whole string. MULTIBYTE_P non-zero means S is
10546 multibyte. Display the message multibyte in that case.
10548 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10549 to t before calling set_message_1 (which calls insert).
10552 static void
10553 set_message (const char *s, Lisp_Object string,
10554 ptrdiff_t nbytes, int multibyte_p)
10556 message_enable_multibyte
10557 = ((s && multibyte_p)
10558 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10560 with_echo_area_buffer (0, -1, set_message_1,
10561 (intptr_t) s, string, nbytes, multibyte_p);
10562 message_buf_print = 0;
10563 help_echo_showing_p = 0;
10567 /* Helper function for set_message. Arguments have the same meaning
10568 as there, with A1 corresponding to S and A2 corresponding to STRING
10569 This function is called with the echo area buffer being
10570 current. */
10572 static int
10573 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10575 intptr_t i1 = a1;
10576 const char *s = (const char *) i1;
10577 const unsigned char *msg = (const unsigned char *) s;
10578 Lisp_Object string = a2;
10580 /* Change multibyteness of the echo buffer appropriately. */
10581 if (message_enable_multibyte
10582 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10583 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10585 BSET (current_buffer, truncate_lines, message_truncate_lines ? Qt : Qnil);
10586 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10587 BSET (current_buffer, bidi_paragraph_direction, Qleft_to_right);
10589 /* Insert new message at BEG. */
10590 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10592 if (STRINGP (string))
10594 ptrdiff_t nchars;
10596 if (nbytes == 0)
10597 nbytes = SBYTES (string);
10598 nchars = string_byte_to_char (string, nbytes);
10600 /* This function takes care of single/multibyte conversion. We
10601 just have to ensure that the echo area buffer has the right
10602 setting of enable_multibyte_characters. */
10603 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10605 else if (s)
10607 if (nbytes == 0)
10608 nbytes = strlen (s);
10610 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10612 /* Convert from multi-byte to single-byte. */
10613 ptrdiff_t i;
10614 int c, n;
10615 char work[1];
10617 /* Convert a multibyte string to single-byte. */
10618 for (i = 0; i < nbytes; i += n)
10620 c = string_char_and_length (msg + i, &n);
10621 work[0] = (ASCII_CHAR_P (c)
10623 : multibyte_char_to_unibyte (c));
10624 insert_1_both (work, 1, 1, 1, 0, 0);
10627 else if (!multibyte_p
10628 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10630 /* Convert from single-byte to multi-byte. */
10631 ptrdiff_t i;
10632 int c, n;
10633 unsigned char str[MAX_MULTIBYTE_LENGTH];
10635 /* Convert a single-byte string to multibyte. */
10636 for (i = 0; i < nbytes; i++)
10638 c = msg[i];
10639 MAKE_CHAR_MULTIBYTE (c);
10640 n = CHAR_STRING (c, str);
10641 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10644 else
10645 insert_1 (s, nbytes, 1, 0, 0);
10648 return 0;
10652 /* Clear messages. CURRENT_P non-zero means clear the current
10653 message. LAST_DISPLAYED_P non-zero means clear the message
10654 last displayed. */
10656 void
10657 clear_message (int current_p, int last_displayed_p)
10659 if (current_p)
10661 echo_area_buffer[0] = Qnil;
10662 message_cleared_p = 1;
10665 if (last_displayed_p)
10666 echo_area_buffer[1] = Qnil;
10668 message_buf_print = 0;
10671 /* Clear garbaged frames.
10673 This function is used where the old redisplay called
10674 redraw_garbaged_frames which in turn called redraw_frame which in
10675 turn called clear_frame. The call to clear_frame was a source of
10676 flickering. I believe a clear_frame is not necessary. It should
10677 suffice in the new redisplay to invalidate all current matrices,
10678 and ensure a complete redisplay of all windows. */
10680 static void
10681 clear_garbaged_frames (void)
10683 if (frame_garbaged)
10685 Lisp_Object tail, frame;
10686 int changed_count = 0;
10688 FOR_EACH_FRAME (tail, frame)
10690 struct frame *f = XFRAME (frame);
10692 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10694 if (f->resized_p)
10696 Fredraw_frame (frame);
10697 f->force_flush_display_p = 1;
10699 clear_current_matrices (f);
10700 changed_count++;
10701 f->garbaged = 0;
10702 f->resized_p = 0;
10706 frame_garbaged = 0;
10707 if (changed_count)
10708 ++windows_or_buffers_changed;
10713 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10714 is non-zero update selected_frame. Value is non-zero if the
10715 mini-windows height has been changed. */
10717 static int
10718 echo_area_display (int update_frame_p)
10720 Lisp_Object mini_window;
10721 struct window *w;
10722 struct frame *f;
10723 int window_height_changed_p = 0;
10724 struct frame *sf = SELECTED_FRAME ();
10726 mini_window = FRAME_MINIBUF_WINDOW (sf);
10727 w = XWINDOW (mini_window);
10728 f = XFRAME (WINDOW_FRAME (w));
10730 /* Don't display if frame is invisible or not yet initialized. */
10731 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10732 return 0;
10734 #ifdef HAVE_WINDOW_SYSTEM
10735 /* When Emacs starts, selected_frame may be the initial terminal
10736 frame. If we let this through, a message would be displayed on
10737 the terminal. */
10738 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10739 return 0;
10740 #endif /* HAVE_WINDOW_SYSTEM */
10742 /* Redraw garbaged frames. */
10743 if (frame_garbaged)
10744 clear_garbaged_frames ();
10746 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10748 echo_area_window = mini_window;
10749 window_height_changed_p = display_echo_area (w);
10750 w->must_be_updated_p = 1;
10752 /* Update the display, unless called from redisplay_internal.
10753 Also don't update the screen during redisplay itself. The
10754 update will happen at the end of redisplay, and an update
10755 here could cause confusion. */
10756 if (update_frame_p && !redisplaying_p)
10758 int n = 0;
10760 /* If the display update has been interrupted by pending
10761 input, update mode lines in the frame. Due to the
10762 pending input, it might have been that redisplay hasn't
10763 been called, so that mode lines above the echo area are
10764 garbaged. This looks odd, so we prevent it here. */
10765 if (!display_completed)
10766 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10768 if (window_height_changed_p
10769 /* Don't do this if Emacs is shutting down. Redisplay
10770 needs to run hooks. */
10771 && !NILP (Vrun_hooks))
10773 /* Must update other windows. Likewise as in other
10774 cases, don't let this update be interrupted by
10775 pending input. */
10776 ptrdiff_t count = SPECPDL_INDEX ();
10777 specbind (Qredisplay_dont_pause, Qt);
10778 windows_or_buffers_changed = 1;
10779 redisplay_internal ();
10780 unbind_to (count, Qnil);
10782 else if (FRAME_WINDOW_P (f) && n == 0)
10784 /* Window configuration is the same as before.
10785 Can do with a display update of the echo area,
10786 unless we displayed some mode lines. */
10787 update_single_window (w, 1);
10788 FRAME_RIF (f)->flush_display (f);
10790 else
10791 update_frame (f, 1, 1);
10793 /* If cursor is in the echo area, make sure that the next
10794 redisplay displays the minibuffer, so that the cursor will
10795 be replaced with what the minibuffer wants. */
10796 if (cursor_in_echo_area)
10797 ++windows_or_buffers_changed;
10800 else if (!EQ (mini_window, selected_window))
10801 windows_or_buffers_changed++;
10803 /* Last displayed message is now the current message. */
10804 echo_area_buffer[1] = echo_area_buffer[0];
10805 /* Inform read_char that we're not echoing. */
10806 echo_message_buffer = Qnil;
10808 /* Prevent redisplay optimization in redisplay_internal by resetting
10809 this_line_start_pos. This is done because the mini-buffer now
10810 displays the message instead of its buffer text. */
10811 if (EQ (mini_window, selected_window))
10812 CHARPOS (this_line_start_pos) = 0;
10814 return window_height_changed_p;
10819 /***********************************************************************
10820 Mode Lines and Frame Titles
10821 ***********************************************************************/
10823 /* A buffer for constructing non-propertized mode-line strings and
10824 frame titles in it; allocated from the heap in init_xdisp and
10825 resized as needed in store_mode_line_noprop_char. */
10827 static char *mode_line_noprop_buf;
10829 /* The buffer's end, and a current output position in it. */
10831 static char *mode_line_noprop_buf_end;
10832 static char *mode_line_noprop_ptr;
10834 #define MODE_LINE_NOPROP_LEN(start) \
10835 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10837 static enum {
10838 MODE_LINE_DISPLAY = 0,
10839 MODE_LINE_TITLE,
10840 MODE_LINE_NOPROP,
10841 MODE_LINE_STRING
10842 } mode_line_target;
10844 /* Alist that caches the results of :propertize.
10845 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10846 static Lisp_Object mode_line_proptrans_alist;
10848 /* List of strings making up the mode-line. */
10849 static Lisp_Object mode_line_string_list;
10851 /* Base face property when building propertized mode line string. */
10852 static Lisp_Object mode_line_string_face;
10853 static Lisp_Object mode_line_string_face_prop;
10856 /* Unwind data for mode line strings */
10858 static Lisp_Object Vmode_line_unwind_vector;
10860 static Lisp_Object
10861 format_mode_line_unwind_data (struct frame *target_frame,
10862 struct buffer *obuf,
10863 Lisp_Object owin,
10864 int save_proptrans)
10866 Lisp_Object vector, tmp;
10868 /* Reduce consing by keeping one vector in
10869 Vwith_echo_area_save_vector. */
10870 vector = Vmode_line_unwind_vector;
10871 Vmode_line_unwind_vector = Qnil;
10873 if (NILP (vector))
10874 vector = Fmake_vector (make_number (10), Qnil);
10876 ASET (vector, 0, make_number (mode_line_target));
10877 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10878 ASET (vector, 2, mode_line_string_list);
10879 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10880 ASET (vector, 4, mode_line_string_face);
10881 ASET (vector, 5, mode_line_string_face_prop);
10883 if (obuf)
10884 XSETBUFFER (tmp, obuf);
10885 else
10886 tmp = Qnil;
10887 ASET (vector, 6, tmp);
10888 ASET (vector, 7, owin);
10889 if (target_frame)
10891 /* Similarly to `with-selected-window', if the operation selects
10892 a window on another frame, we must restore that frame's
10893 selected window, and (for a tty) the top-frame. */
10894 ASET (vector, 8, target_frame->selected_window);
10895 if (FRAME_TERMCAP_P (target_frame))
10896 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10899 return vector;
10902 static Lisp_Object
10903 unwind_format_mode_line (Lisp_Object vector)
10905 Lisp_Object old_window = AREF (vector, 7);
10906 Lisp_Object target_frame_window = AREF (vector, 8);
10907 Lisp_Object old_top_frame = AREF (vector, 9);
10909 mode_line_target = XINT (AREF (vector, 0));
10910 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10911 mode_line_string_list = AREF (vector, 2);
10912 if (! EQ (AREF (vector, 3), Qt))
10913 mode_line_proptrans_alist = AREF (vector, 3);
10914 mode_line_string_face = AREF (vector, 4);
10915 mode_line_string_face_prop = AREF (vector, 5);
10917 /* Select window before buffer, since it may change the buffer. */
10918 if (!NILP (old_window))
10920 /* If the operation that we are unwinding had selected a window
10921 on a different frame, reset its frame-selected-window. For a
10922 text terminal, reset its top-frame if necessary. */
10923 if (!NILP (target_frame_window))
10925 Lisp_Object frame
10926 = WINDOW_FRAME (XWINDOW (target_frame_window));
10928 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10929 Fselect_window (target_frame_window, Qt);
10931 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10932 Fselect_frame (old_top_frame, Qt);
10935 Fselect_window (old_window, Qt);
10938 if (!NILP (AREF (vector, 6)))
10940 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10941 ASET (vector, 6, Qnil);
10944 Vmode_line_unwind_vector = vector;
10945 return Qnil;
10949 /* Store a single character C for the frame title in mode_line_noprop_buf.
10950 Re-allocate mode_line_noprop_buf if necessary. */
10952 static void
10953 store_mode_line_noprop_char (char c)
10955 /* If output position has reached the end of the allocated buffer,
10956 increase the buffer's size. */
10957 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10959 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10960 ptrdiff_t size = len;
10961 mode_line_noprop_buf =
10962 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10963 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10964 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10967 *mode_line_noprop_ptr++ = c;
10971 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10972 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10973 characters that yield more columns than PRECISION; PRECISION <= 0
10974 means copy the whole string. Pad with spaces until FIELD_WIDTH
10975 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10976 pad. Called from display_mode_element when it is used to build a
10977 frame title. */
10979 static int
10980 store_mode_line_noprop (const char *string, int field_width, int precision)
10982 const unsigned char *str = (const unsigned char *) string;
10983 int n = 0;
10984 ptrdiff_t dummy, nbytes;
10986 /* Copy at most PRECISION chars from STR. */
10987 nbytes = strlen (string);
10988 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10989 while (nbytes--)
10990 store_mode_line_noprop_char (*str++);
10992 /* Fill up with spaces until FIELD_WIDTH reached. */
10993 while (field_width > 0
10994 && n < field_width)
10996 store_mode_line_noprop_char (' ');
10997 ++n;
11000 return n;
11003 /***********************************************************************
11004 Frame Titles
11005 ***********************************************************************/
11007 #ifdef HAVE_WINDOW_SYSTEM
11009 /* Set the title of FRAME, if it has changed. The title format is
11010 Vicon_title_format if FRAME is iconified, otherwise it is
11011 frame_title_format. */
11013 static void
11014 x_consider_frame_title (Lisp_Object frame)
11016 struct frame *f = XFRAME (frame);
11018 if (FRAME_WINDOW_P (f)
11019 || FRAME_MINIBUF_ONLY_P (f)
11020 || f->explicit_name)
11022 /* Do we have more than one visible frame on this X display? */
11023 Lisp_Object tail;
11024 Lisp_Object fmt;
11025 ptrdiff_t title_start;
11026 char *title;
11027 ptrdiff_t len;
11028 struct it it;
11029 ptrdiff_t count = SPECPDL_INDEX ();
11031 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11033 Lisp_Object other_frame = XCAR (tail);
11034 struct frame *tf = XFRAME (other_frame);
11036 if (tf != f
11037 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11038 && !FRAME_MINIBUF_ONLY_P (tf)
11039 && !EQ (other_frame, tip_frame)
11040 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11041 break;
11044 /* Set global variable indicating that multiple frames exist. */
11045 multiple_frames = CONSP (tail);
11047 /* Switch to the buffer of selected window of the frame. Set up
11048 mode_line_target so that display_mode_element will output into
11049 mode_line_noprop_buf; then display the title. */
11050 record_unwind_protect (unwind_format_mode_line,
11051 format_mode_line_unwind_data
11052 (f, current_buffer, selected_window, 0));
11054 Fselect_window (f->selected_window, Qt);
11055 set_buffer_internal_1
11056 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11057 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11059 mode_line_target = MODE_LINE_TITLE;
11060 title_start = MODE_LINE_NOPROP_LEN (0);
11061 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11062 NULL, DEFAULT_FACE_ID);
11063 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11064 len = MODE_LINE_NOPROP_LEN (title_start);
11065 title = mode_line_noprop_buf + title_start;
11066 unbind_to (count, Qnil);
11068 /* Set the title only if it's changed. This avoids consing in
11069 the common case where it hasn't. (If it turns out that we've
11070 already wasted too much time by walking through the list with
11071 display_mode_element, then we might need to optimize at a
11072 higher level than this.) */
11073 if (! STRINGP (f->name)
11074 || SBYTES (f->name) != len
11075 || memcmp (title, SDATA (f->name), len) != 0)
11076 x_implicitly_set_name (f, make_string (title, len), Qnil);
11080 #endif /* not HAVE_WINDOW_SYSTEM */
11083 /***********************************************************************
11084 Menu Bars
11085 ***********************************************************************/
11088 /* Prepare for redisplay by updating menu-bar item lists when
11089 appropriate. This can call eval. */
11091 void
11092 prepare_menu_bars (void)
11094 int all_windows;
11095 struct gcpro gcpro1, gcpro2;
11096 struct frame *f;
11097 Lisp_Object tooltip_frame;
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 tooltip_frame = tip_frame;
11101 #else
11102 tooltip_frame = Qnil;
11103 #endif
11105 /* Update all frame titles based on their buffer names, etc. We do
11106 this before the menu bars so that the buffer-menu will show the
11107 up-to-date frame titles. */
11108 #ifdef HAVE_WINDOW_SYSTEM
11109 if (windows_or_buffers_changed || update_mode_lines)
11111 Lisp_Object tail, frame;
11113 FOR_EACH_FRAME (tail, frame)
11115 f = XFRAME (frame);
11116 if (!EQ (frame, tooltip_frame)
11117 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11118 x_consider_frame_title (frame);
11121 #endif /* HAVE_WINDOW_SYSTEM */
11123 /* Update the menu bar item lists, if appropriate. This has to be
11124 done before any actual redisplay or generation of display lines. */
11125 all_windows = (update_mode_lines
11126 || buffer_shared > 1
11127 || windows_or_buffers_changed);
11128 if (all_windows)
11130 Lisp_Object tail, frame;
11131 ptrdiff_t count = SPECPDL_INDEX ();
11132 /* 1 means that update_menu_bar has run its hooks
11133 so any further calls to update_menu_bar shouldn't do so again. */
11134 int menu_bar_hooks_run = 0;
11136 record_unwind_save_match_data ();
11138 FOR_EACH_FRAME (tail, frame)
11140 f = XFRAME (frame);
11142 /* Ignore tooltip frame. */
11143 if (EQ (frame, tooltip_frame))
11144 continue;
11146 /* If a window on this frame changed size, report that to
11147 the user and clear the size-change flag. */
11148 if (FRAME_WINDOW_SIZES_CHANGED (f))
11150 Lisp_Object functions;
11152 /* Clear flag first in case we get an error below. */
11153 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11154 functions = Vwindow_size_change_functions;
11155 GCPRO2 (tail, functions);
11157 while (CONSP (functions))
11159 if (!EQ (XCAR (functions), Qt))
11160 call1 (XCAR (functions), frame);
11161 functions = XCDR (functions);
11163 UNGCPRO;
11166 GCPRO1 (tail);
11167 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11168 #ifdef HAVE_WINDOW_SYSTEM
11169 update_tool_bar (f, 0);
11170 #endif
11171 #ifdef HAVE_NS
11172 if (windows_or_buffers_changed
11173 && FRAME_NS_P (f))
11174 ns_set_doc_edited
11175 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11176 #endif
11177 UNGCPRO;
11180 unbind_to (count, Qnil);
11182 else
11184 struct frame *sf = SELECTED_FRAME ();
11185 update_menu_bar (sf, 1, 0);
11186 #ifdef HAVE_WINDOW_SYSTEM
11187 update_tool_bar (sf, 1);
11188 #endif
11193 /* Update the menu bar item list for frame F. This has to be done
11194 before we start to fill in any display lines, because it can call
11195 eval.
11197 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11199 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11200 already ran the menu bar hooks for this redisplay, so there
11201 is no need to run them again. The return value is the
11202 updated value of this flag, to pass to the next call. */
11204 static int
11205 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11207 Lisp_Object window;
11208 register struct window *w;
11210 /* If called recursively during a menu update, do nothing. This can
11211 happen when, for instance, an activate-menubar-hook causes a
11212 redisplay. */
11213 if (inhibit_menubar_update)
11214 return hooks_run;
11216 window = FRAME_SELECTED_WINDOW (f);
11217 w = XWINDOW (window);
11219 if (FRAME_WINDOW_P (f)
11221 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11222 || defined (HAVE_NS) || defined (USE_GTK)
11223 FRAME_EXTERNAL_MENU_BAR (f)
11224 #else
11225 FRAME_MENU_BAR_LINES (f) > 0
11226 #endif
11227 : FRAME_MENU_BAR_LINES (f) > 0)
11229 /* If the user has switched buffers or windows, we need to
11230 recompute to reflect the new bindings. But we'll
11231 recompute when update_mode_lines is set too; that means
11232 that people can use force-mode-line-update to request
11233 that the menu bar be recomputed. The adverse effect on
11234 the rest of the redisplay algorithm is about the same as
11235 windows_or_buffers_changed anyway. */
11236 if (windows_or_buffers_changed
11237 /* This used to test w->update_mode_line, but we believe
11238 there is no need to recompute the menu in that case. */
11239 || update_mode_lines
11240 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11241 < BUF_MODIFF (XBUFFER (w->buffer)))
11242 != w->last_had_star)
11243 || ((!NILP (Vtransient_mark_mode)
11244 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11245 != !NILP (w->region_showing)))
11247 struct buffer *prev = current_buffer;
11248 ptrdiff_t count = SPECPDL_INDEX ();
11250 specbind (Qinhibit_menubar_update, Qt);
11252 set_buffer_internal_1 (XBUFFER (w->buffer));
11253 if (save_match_data)
11254 record_unwind_save_match_data ();
11255 if (NILP (Voverriding_local_map_menu_flag))
11257 specbind (Qoverriding_terminal_local_map, Qnil);
11258 specbind (Qoverriding_local_map, Qnil);
11261 if (!hooks_run)
11263 /* Run the Lucid hook. */
11264 safe_run_hooks (Qactivate_menubar_hook);
11266 /* If it has changed current-menubar from previous value,
11267 really recompute the menu-bar from the value. */
11268 if (! NILP (Vlucid_menu_bar_dirty_flag))
11269 call0 (Qrecompute_lucid_menubar);
11271 safe_run_hooks (Qmenu_bar_update_hook);
11273 hooks_run = 1;
11276 XSETFRAME (Vmenu_updating_frame, f);
11277 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11279 /* Redisplay the menu bar in case we changed it. */
11280 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11281 || defined (HAVE_NS) || defined (USE_GTK)
11282 if (FRAME_WINDOW_P (f))
11284 #if defined (HAVE_NS)
11285 /* All frames on Mac OS share the same menubar. So only
11286 the selected frame should be allowed to set it. */
11287 if (f == SELECTED_FRAME ())
11288 #endif
11289 set_frame_menubar (f, 0, 0);
11291 else
11292 /* On a terminal screen, the menu bar is an ordinary screen
11293 line, and this makes it get updated. */
11294 w->update_mode_line = 1;
11295 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11296 /* In the non-toolkit version, the menu bar is an ordinary screen
11297 line, and this makes it get updated. */
11298 w->update_mode_line = 1;
11299 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11301 unbind_to (count, Qnil);
11302 set_buffer_internal_1 (prev);
11306 return hooks_run;
11311 /***********************************************************************
11312 Output Cursor
11313 ***********************************************************************/
11315 #ifdef HAVE_WINDOW_SYSTEM
11317 /* EXPORT:
11318 Nominal cursor position -- where to draw output.
11319 HPOS and VPOS are window relative glyph matrix coordinates.
11320 X and Y are window relative pixel coordinates. */
11322 struct cursor_pos output_cursor;
11325 /* EXPORT:
11326 Set the global variable output_cursor to CURSOR. All cursor
11327 positions are relative to updated_window. */
11329 void
11330 set_output_cursor (struct cursor_pos *cursor)
11332 output_cursor.hpos = cursor->hpos;
11333 output_cursor.vpos = cursor->vpos;
11334 output_cursor.x = cursor->x;
11335 output_cursor.y = cursor->y;
11339 /* EXPORT for RIF:
11340 Set a nominal cursor position.
11342 HPOS and VPOS are column/row positions in a window glyph matrix. X
11343 and Y are window text area relative pixel positions.
11345 If this is done during an update, updated_window will contain the
11346 window that is being updated and the position is the future output
11347 cursor position for that window. If updated_window is null, use
11348 selected_window and display the cursor at the given position. */
11350 void
11351 x_cursor_to (int vpos, int hpos, int y, int x)
11353 struct window *w;
11355 /* If updated_window is not set, work on selected_window. */
11356 if (updated_window)
11357 w = updated_window;
11358 else
11359 w = XWINDOW (selected_window);
11361 /* Set the output cursor. */
11362 output_cursor.hpos = hpos;
11363 output_cursor.vpos = vpos;
11364 output_cursor.x = x;
11365 output_cursor.y = y;
11367 /* If not called as part of an update, really display the cursor.
11368 This will also set the cursor position of W. */
11369 if (updated_window == NULL)
11371 BLOCK_INPUT;
11372 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11373 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11374 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11375 UNBLOCK_INPUT;
11379 #endif /* HAVE_WINDOW_SYSTEM */
11382 /***********************************************************************
11383 Tool-bars
11384 ***********************************************************************/
11386 #ifdef HAVE_WINDOW_SYSTEM
11388 /* Where the mouse was last time we reported a mouse event. */
11390 FRAME_PTR last_mouse_frame;
11392 /* Tool-bar item index of the item on which a mouse button was pressed
11393 or -1. */
11395 int last_tool_bar_item;
11398 static Lisp_Object
11399 update_tool_bar_unwind (Lisp_Object frame)
11401 selected_frame = frame;
11402 return Qnil;
11405 /* Update the tool-bar item list for frame F. This has to be done
11406 before we start to fill in any display lines. Called from
11407 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11408 and restore it here. */
11410 static void
11411 update_tool_bar (struct frame *f, int save_match_data)
11413 #if defined (USE_GTK) || defined (HAVE_NS)
11414 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11415 #else
11416 int do_update = WINDOWP (f->tool_bar_window)
11417 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11418 #endif
11420 if (do_update)
11422 Lisp_Object window;
11423 struct window *w;
11425 window = FRAME_SELECTED_WINDOW (f);
11426 w = XWINDOW (window);
11428 /* If the user has switched buffers or windows, we need to
11429 recompute to reflect the new bindings. But we'll
11430 recompute when update_mode_lines is set too; that means
11431 that people can use force-mode-line-update to request
11432 that the menu bar be recomputed. The adverse effect on
11433 the rest of the redisplay algorithm is about the same as
11434 windows_or_buffers_changed anyway. */
11435 if (windows_or_buffers_changed
11436 || w->update_mode_line
11437 || update_mode_lines
11438 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11439 < BUF_MODIFF (XBUFFER (w->buffer)))
11440 != w->last_had_star)
11441 || ((!NILP (Vtransient_mark_mode)
11442 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11443 != !NILP (w->region_showing)))
11445 struct buffer *prev = current_buffer;
11446 ptrdiff_t count = SPECPDL_INDEX ();
11447 Lisp_Object frame, new_tool_bar;
11448 int new_n_tool_bar;
11449 struct gcpro gcpro1;
11451 /* Set current_buffer to the buffer of the selected
11452 window of the frame, so that we get the right local
11453 keymaps. */
11454 set_buffer_internal_1 (XBUFFER (w->buffer));
11456 /* Save match data, if we must. */
11457 if (save_match_data)
11458 record_unwind_save_match_data ();
11460 /* Make sure that we don't accidentally use bogus keymaps. */
11461 if (NILP (Voverriding_local_map_menu_flag))
11463 specbind (Qoverriding_terminal_local_map, Qnil);
11464 specbind (Qoverriding_local_map, Qnil);
11467 GCPRO1 (new_tool_bar);
11469 /* We must temporarily set the selected frame to this frame
11470 before calling tool_bar_items, because the calculation of
11471 the tool-bar keymap uses the selected frame (see
11472 `tool-bar-make-keymap' in tool-bar.el). */
11473 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11474 XSETFRAME (frame, f);
11475 selected_frame = frame;
11477 /* Build desired tool-bar items from keymaps. */
11478 new_tool_bar
11479 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11480 &new_n_tool_bar);
11482 /* Redisplay the tool-bar if we changed it. */
11483 if (new_n_tool_bar != f->n_tool_bar_items
11484 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11486 /* Redisplay that happens asynchronously due to an expose event
11487 may access f->tool_bar_items. Make sure we update both
11488 variables within BLOCK_INPUT so no such event interrupts. */
11489 BLOCK_INPUT;
11490 fset_tool_bar_items (f, new_tool_bar);
11491 f->n_tool_bar_items = new_n_tool_bar;
11492 w->update_mode_line = 1;
11493 UNBLOCK_INPUT;
11496 UNGCPRO;
11498 unbind_to (count, Qnil);
11499 set_buffer_internal_1 (prev);
11505 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11506 F's desired tool-bar contents. F->tool_bar_items must have
11507 been set up previously by calling prepare_menu_bars. */
11509 static void
11510 build_desired_tool_bar_string (struct frame *f)
11512 int i, size, size_needed;
11513 struct gcpro gcpro1, gcpro2, gcpro3;
11514 Lisp_Object image, plist, props;
11516 image = plist = props = Qnil;
11517 GCPRO3 (image, plist, props);
11519 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11520 Otherwise, make a new string. */
11522 /* The size of the string we might be able to reuse. */
11523 size = (STRINGP (f->desired_tool_bar_string)
11524 ? SCHARS (f->desired_tool_bar_string)
11525 : 0);
11527 /* We need one space in the string for each image. */
11528 size_needed = f->n_tool_bar_items;
11530 /* Reuse f->desired_tool_bar_string, if possible. */
11531 if (size < size_needed || NILP (f->desired_tool_bar_string))
11532 fset_desired_tool_bar_string
11533 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11534 else
11536 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11537 Fremove_text_properties (make_number (0), make_number (size),
11538 props, f->desired_tool_bar_string);
11541 /* Put a `display' property on the string for the images to display,
11542 put a `menu_item' property on tool-bar items with a value that
11543 is the index of the item in F's tool-bar item vector. */
11544 for (i = 0; i < f->n_tool_bar_items; ++i)
11546 #define PROP(IDX) \
11547 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11549 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11550 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11551 int hmargin, vmargin, relief, idx, end;
11553 /* If image is a vector, choose the image according to the
11554 button state. */
11555 image = PROP (TOOL_BAR_ITEM_IMAGES);
11556 if (VECTORP (image))
11558 if (enabled_p)
11559 idx = (selected_p
11560 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11561 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11562 else
11563 idx = (selected_p
11564 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11565 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11567 eassert (ASIZE (image) >= idx);
11568 image = AREF (image, idx);
11570 else
11571 idx = -1;
11573 /* Ignore invalid image specifications. */
11574 if (!valid_image_p (image))
11575 continue;
11577 /* Display the tool-bar button pressed, or depressed. */
11578 plist = Fcopy_sequence (XCDR (image));
11580 /* Compute margin and relief to draw. */
11581 relief = (tool_bar_button_relief >= 0
11582 ? tool_bar_button_relief
11583 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11584 hmargin = vmargin = relief;
11586 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11587 INT_MAX - max (hmargin, vmargin)))
11589 hmargin += XFASTINT (Vtool_bar_button_margin);
11590 vmargin += XFASTINT (Vtool_bar_button_margin);
11592 else if (CONSP (Vtool_bar_button_margin))
11594 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11595 INT_MAX - hmargin))
11596 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11598 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11599 INT_MAX - vmargin))
11600 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11603 if (auto_raise_tool_bar_buttons_p)
11605 /* Add a `:relief' property to the image spec if the item is
11606 selected. */
11607 if (selected_p)
11609 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11610 hmargin -= relief;
11611 vmargin -= relief;
11614 else
11616 /* If image is selected, display it pressed, i.e. with a
11617 negative relief. If it's not selected, display it with a
11618 raised relief. */
11619 plist = Fplist_put (plist, QCrelief,
11620 (selected_p
11621 ? make_number (-relief)
11622 : make_number (relief)));
11623 hmargin -= relief;
11624 vmargin -= relief;
11627 /* Put a margin around the image. */
11628 if (hmargin || vmargin)
11630 if (hmargin == vmargin)
11631 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11632 else
11633 plist = Fplist_put (plist, QCmargin,
11634 Fcons (make_number (hmargin),
11635 make_number (vmargin)));
11638 /* If button is not enabled, and we don't have special images
11639 for the disabled state, make the image appear disabled by
11640 applying an appropriate algorithm to it. */
11641 if (!enabled_p && idx < 0)
11642 plist = Fplist_put (plist, QCconversion, Qdisabled);
11644 /* Put a `display' text property on the string for the image to
11645 display. Put a `menu-item' property on the string that gives
11646 the start of this item's properties in the tool-bar items
11647 vector. */
11648 image = Fcons (Qimage, plist);
11649 props = list4 (Qdisplay, image,
11650 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11652 /* Let the last image hide all remaining spaces in the tool bar
11653 string. The string can be longer than needed when we reuse a
11654 previous string. */
11655 if (i + 1 == f->n_tool_bar_items)
11656 end = SCHARS (f->desired_tool_bar_string);
11657 else
11658 end = i + 1;
11659 Fadd_text_properties (make_number (i), make_number (end),
11660 props, f->desired_tool_bar_string);
11661 #undef PROP
11664 UNGCPRO;
11668 /* Display one line of the tool-bar of frame IT->f.
11670 HEIGHT specifies the desired height of the tool-bar line.
11671 If the actual height of the glyph row is less than HEIGHT, the
11672 row's height is increased to HEIGHT, and the icons are centered
11673 vertically in the new height.
11675 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11676 count a final empty row in case the tool-bar width exactly matches
11677 the window width.
11680 static void
11681 display_tool_bar_line (struct it *it, int height)
11683 struct glyph_row *row = it->glyph_row;
11684 int max_x = it->last_visible_x;
11685 struct glyph *last;
11687 prepare_desired_row (row);
11688 row->y = it->current_y;
11690 /* Note that this isn't made use of if the face hasn't a box,
11691 so there's no need to check the face here. */
11692 it->start_of_box_run_p = 1;
11694 while (it->current_x < max_x)
11696 int x, n_glyphs_before, i, nglyphs;
11697 struct it it_before;
11699 /* Get the next display element. */
11700 if (!get_next_display_element (it))
11702 /* Don't count empty row if we are counting needed tool-bar lines. */
11703 if (height < 0 && !it->hpos)
11704 return;
11705 break;
11708 /* Produce glyphs. */
11709 n_glyphs_before = row->used[TEXT_AREA];
11710 it_before = *it;
11712 PRODUCE_GLYPHS (it);
11714 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11715 i = 0;
11716 x = it_before.current_x;
11717 while (i < nglyphs)
11719 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11721 if (x + glyph->pixel_width > max_x)
11723 /* Glyph doesn't fit on line. Backtrack. */
11724 row->used[TEXT_AREA] = n_glyphs_before;
11725 *it = it_before;
11726 /* If this is the only glyph on this line, it will never fit on the
11727 tool-bar, so skip it. But ensure there is at least one glyph,
11728 so we don't accidentally disable the tool-bar. */
11729 if (n_glyphs_before == 0
11730 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11731 break;
11732 goto out;
11735 ++it->hpos;
11736 x += glyph->pixel_width;
11737 ++i;
11740 /* Stop at line end. */
11741 if (ITERATOR_AT_END_OF_LINE_P (it))
11742 break;
11744 set_iterator_to_next (it, 1);
11747 out:;
11749 row->displays_text_p = row->used[TEXT_AREA] != 0;
11751 /* Use default face for the border below the tool bar.
11753 FIXME: When auto-resize-tool-bars is grow-only, there is
11754 no additional border below the possibly empty tool-bar lines.
11755 So to make the extra empty lines look "normal", we have to
11756 use the tool-bar face for the border too. */
11757 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11758 it->face_id = DEFAULT_FACE_ID;
11760 extend_face_to_end_of_line (it);
11761 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11762 last->right_box_line_p = 1;
11763 if (last == row->glyphs[TEXT_AREA])
11764 last->left_box_line_p = 1;
11766 /* Make line the desired height and center it vertically. */
11767 if ((height -= it->max_ascent + it->max_descent) > 0)
11769 /* Don't add more than one line height. */
11770 height %= FRAME_LINE_HEIGHT (it->f);
11771 it->max_ascent += height / 2;
11772 it->max_descent += (height + 1) / 2;
11775 compute_line_metrics (it);
11777 /* If line is empty, make it occupy the rest of the tool-bar. */
11778 if (!row->displays_text_p)
11780 row->height = row->phys_height = it->last_visible_y - row->y;
11781 row->visible_height = row->height;
11782 row->ascent = row->phys_ascent = 0;
11783 row->extra_line_spacing = 0;
11786 row->full_width_p = 1;
11787 row->continued_p = 0;
11788 row->truncated_on_left_p = 0;
11789 row->truncated_on_right_p = 0;
11791 it->current_x = it->hpos = 0;
11792 it->current_y += row->height;
11793 ++it->vpos;
11794 ++it->glyph_row;
11798 /* Max tool-bar height. */
11800 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11801 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11803 /* Value is the number of screen lines needed to make all tool-bar
11804 items of frame F visible. The number of actual rows needed is
11805 returned in *N_ROWS if non-NULL. */
11807 static int
11808 tool_bar_lines_needed (struct frame *f, int *n_rows)
11810 struct window *w = XWINDOW (f->tool_bar_window);
11811 struct it it;
11812 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11813 the desired matrix, so use (unused) mode-line row as temporary row to
11814 avoid destroying the first tool-bar row. */
11815 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11817 /* Initialize an iterator for iteration over
11818 F->desired_tool_bar_string in the tool-bar window of frame F. */
11819 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11820 it.first_visible_x = 0;
11821 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11822 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11823 it.paragraph_embedding = L2R;
11825 while (!ITERATOR_AT_END_P (&it))
11827 clear_glyph_row (temp_row);
11828 it.glyph_row = temp_row;
11829 display_tool_bar_line (&it, -1);
11831 clear_glyph_row (temp_row);
11833 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11834 if (n_rows)
11835 *n_rows = it.vpos > 0 ? it.vpos : -1;
11837 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11841 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11842 0, 1, 0,
11843 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11844 (Lisp_Object frame)
11846 struct frame *f;
11847 struct window *w;
11848 int nlines = 0;
11850 if (NILP (frame))
11851 frame = selected_frame;
11852 else
11853 CHECK_FRAME (frame);
11854 f = XFRAME (frame);
11856 if (WINDOWP (f->tool_bar_window)
11857 && (w = XWINDOW (f->tool_bar_window),
11858 WINDOW_TOTAL_LINES (w) > 0))
11860 update_tool_bar (f, 1);
11861 if (f->n_tool_bar_items)
11863 build_desired_tool_bar_string (f);
11864 nlines = tool_bar_lines_needed (f, NULL);
11868 return make_number (nlines);
11872 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11873 height should be changed. */
11875 static int
11876 redisplay_tool_bar (struct frame *f)
11878 struct window *w;
11879 struct it it;
11880 struct glyph_row *row;
11882 #if defined (USE_GTK) || defined (HAVE_NS)
11883 if (FRAME_EXTERNAL_TOOL_BAR (f))
11884 update_frame_tool_bar (f);
11885 return 0;
11886 #endif
11888 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11889 do anything. This means you must start with tool-bar-lines
11890 non-zero to get the auto-sizing effect. Or in other words, you
11891 can turn off tool-bars by specifying tool-bar-lines zero. */
11892 if (!WINDOWP (f->tool_bar_window)
11893 || (w = XWINDOW (f->tool_bar_window),
11894 WINDOW_TOTAL_LINES (w) == 0))
11895 return 0;
11897 /* Set up an iterator for the tool-bar window. */
11898 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11899 it.first_visible_x = 0;
11900 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11901 row = it.glyph_row;
11903 /* Build a string that represents the contents of the tool-bar. */
11904 build_desired_tool_bar_string (f);
11905 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11906 /* FIXME: This should be controlled by a user option. But it
11907 doesn't make sense to have an R2L tool bar if the menu bar cannot
11908 be drawn also R2L, and making the menu bar R2L is tricky due
11909 toolkit-specific code that implements it. If an R2L tool bar is
11910 ever supported, display_tool_bar_line should also be augmented to
11911 call unproduce_glyphs like display_line and display_string
11912 do. */
11913 it.paragraph_embedding = L2R;
11915 if (f->n_tool_bar_rows == 0)
11917 int nlines;
11919 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11920 nlines != WINDOW_TOTAL_LINES (w)))
11922 Lisp_Object frame;
11923 int old_height = WINDOW_TOTAL_LINES (w);
11925 XSETFRAME (frame, f);
11926 Fmodify_frame_parameters (frame,
11927 Fcons (Fcons (Qtool_bar_lines,
11928 make_number (nlines)),
11929 Qnil));
11930 if (WINDOW_TOTAL_LINES (w) != old_height)
11932 clear_glyph_matrix (w->desired_matrix);
11933 fonts_changed_p = 1;
11934 return 1;
11939 /* Display as many lines as needed to display all tool-bar items. */
11941 if (f->n_tool_bar_rows > 0)
11943 int border, rows, height, extra;
11945 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11946 border = XINT (Vtool_bar_border);
11947 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11948 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11949 else if (EQ (Vtool_bar_border, Qborder_width))
11950 border = f->border_width;
11951 else
11952 border = 0;
11953 if (border < 0)
11954 border = 0;
11956 rows = f->n_tool_bar_rows;
11957 height = max (1, (it.last_visible_y - border) / rows);
11958 extra = it.last_visible_y - border - height * rows;
11960 while (it.current_y < it.last_visible_y)
11962 int h = 0;
11963 if (extra > 0 && rows-- > 0)
11965 h = (extra + rows - 1) / rows;
11966 extra -= h;
11968 display_tool_bar_line (&it, height + h);
11971 else
11973 while (it.current_y < it.last_visible_y)
11974 display_tool_bar_line (&it, 0);
11977 /* It doesn't make much sense to try scrolling in the tool-bar
11978 window, so don't do it. */
11979 w->desired_matrix->no_scrolling_p = 1;
11980 w->must_be_updated_p = 1;
11982 if (!NILP (Vauto_resize_tool_bars))
11984 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11985 int change_height_p = 0;
11987 /* If we couldn't display everything, change the tool-bar's
11988 height if there is room for more. */
11989 if (IT_STRING_CHARPOS (it) < it.end_charpos
11990 && it.current_y < max_tool_bar_height)
11991 change_height_p = 1;
11993 row = it.glyph_row - 1;
11995 /* If there are blank lines at the end, except for a partially
11996 visible blank line at the end that is smaller than
11997 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11998 if (!row->displays_text_p
11999 && row->height >= FRAME_LINE_HEIGHT (f))
12000 change_height_p = 1;
12002 /* If row displays tool-bar items, but is partially visible,
12003 change the tool-bar's height. */
12004 if (row->displays_text_p
12005 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12006 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12007 change_height_p = 1;
12009 /* Resize windows as needed by changing the `tool-bar-lines'
12010 frame parameter. */
12011 if (change_height_p)
12013 Lisp_Object frame;
12014 int old_height = WINDOW_TOTAL_LINES (w);
12015 int nrows;
12016 int nlines = tool_bar_lines_needed (f, &nrows);
12018 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12019 && !f->minimize_tool_bar_window_p)
12020 ? (nlines > old_height)
12021 : (nlines != old_height));
12022 f->minimize_tool_bar_window_p = 0;
12024 if (change_height_p)
12026 XSETFRAME (frame, f);
12027 Fmodify_frame_parameters (frame,
12028 Fcons (Fcons (Qtool_bar_lines,
12029 make_number (nlines)),
12030 Qnil));
12031 if (WINDOW_TOTAL_LINES (w) != old_height)
12033 clear_glyph_matrix (w->desired_matrix);
12034 f->n_tool_bar_rows = nrows;
12035 fonts_changed_p = 1;
12036 return 1;
12042 f->minimize_tool_bar_window_p = 0;
12043 return 0;
12047 /* Get information about the tool-bar item which is displayed in GLYPH
12048 on frame F. Return in *PROP_IDX the index where tool-bar item
12049 properties start in F->tool_bar_items. Value is zero if
12050 GLYPH doesn't display a tool-bar item. */
12052 static int
12053 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12055 Lisp_Object prop;
12056 int success_p;
12057 int charpos;
12059 /* This function can be called asynchronously, which means we must
12060 exclude any possibility that Fget_text_property signals an
12061 error. */
12062 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12063 charpos = max (0, charpos);
12065 /* Get the text property `menu-item' at pos. The value of that
12066 property is the start index of this item's properties in
12067 F->tool_bar_items. */
12068 prop = Fget_text_property (make_number (charpos),
12069 Qmenu_item, f->current_tool_bar_string);
12070 if (INTEGERP (prop))
12072 *prop_idx = XINT (prop);
12073 success_p = 1;
12075 else
12076 success_p = 0;
12078 return success_p;
12082 /* Get information about the tool-bar item at position X/Y on frame F.
12083 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12084 the current matrix of the tool-bar window of F, or NULL if not
12085 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12086 item in F->tool_bar_items. Value is
12088 -1 if X/Y is not on a tool-bar item
12089 0 if X/Y is on the same item that was highlighted before.
12090 1 otherwise. */
12092 static int
12093 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12094 int *hpos, int *vpos, int *prop_idx)
12096 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12097 struct window *w = XWINDOW (f->tool_bar_window);
12098 int area;
12100 /* Find the glyph under X/Y. */
12101 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12102 if (*glyph == NULL)
12103 return -1;
12105 /* Get the start of this tool-bar item's properties in
12106 f->tool_bar_items. */
12107 if (!tool_bar_item_info (f, *glyph, prop_idx))
12108 return -1;
12110 /* Is mouse on the highlighted item? */
12111 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12112 && *vpos >= hlinfo->mouse_face_beg_row
12113 && *vpos <= hlinfo->mouse_face_end_row
12114 && (*vpos > hlinfo->mouse_face_beg_row
12115 || *hpos >= hlinfo->mouse_face_beg_col)
12116 && (*vpos < hlinfo->mouse_face_end_row
12117 || *hpos < hlinfo->mouse_face_end_col
12118 || hlinfo->mouse_face_past_end))
12119 return 0;
12121 return 1;
12125 /* EXPORT:
12126 Handle mouse button event on the tool-bar of frame F, at
12127 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12128 0 for button release. MODIFIERS is event modifiers for button
12129 release. */
12131 void
12132 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12133 int modifiers)
12135 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12136 struct window *w = XWINDOW (f->tool_bar_window);
12137 int hpos, vpos, prop_idx;
12138 struct glyph *glyph;
12139 Lisp_Object enabled_p;
12141 /* If not on the highlighted tool-bar item, return. */
12142 frame_to_window_pixel_xy (w, &x, &y);
12143 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12144 return;
12146 /* If item is disabled, do nothing. */
12147 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12148 if (NILP (enabled_p))
12149 return;
12151 if (down_p)
12153 /* Show item in pressed state. */
12154 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12155 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12156 last_tool_bar_item = prop_idx;
12158 else
12160 Lisp_Object key, frame;
12161 struct input_event event;
12162 EVENT_INIT (event);
12164 /* Show item in released state. */
12165 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12166 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12168 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12170 XSETFRAME (frame, f);
12171 event.kind = TOOL_BAR_EVENT;
12172 event.frame_or_window = frame;
12173 event.arg = frame;
12174 kbd_buffer_store_event (&event);
12176 event.kind = TOOL_BAR_EVENT;
12177 event.frame_or_window = frame;
12178 event.arg = key;
12179 event.modifiers = modifiers;
12180 kbd_buffer_store_event (&event);
12181 last_tool_bar_item = -1;
12186 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12187 tool-bar window-relative coordinates X/Y. Called from
12188 note_mouse_highlight. */
12190 static void
12191 note_tool_bar_highlight (struct frame *f, int x, int y)
12193 Lisp_Object window = f->tool_bar_window;
12194 struct window *w = XWINDOW (window);
12195 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12197 int hpos, vpos;
12198 struct glyph *glyph;
12199 struct glyph_row *row;
12200 int i;
12201 Lisp_Object enabled_p;
12202 int prop_idx;
12203 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12204 int mouse_down_p, rc;
12206 /* Function note_mouse_highlight is called with negative X/Y
12207 values when mouse moves outside of the frame. */
12208 if (x <= 0 || y <= 0)
12210 clear_mouse_face (hlinfo);
12211 return;
12214 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12215 if (rc < 0)
12217 /* Not on tool-bar item. */
12218 clear_mouse_face (hlinfo);
12219 return;
12221 else if (rc == 0)
12222 /* On same tool-bar item as before. */
12223 goto set_help_echo;
12225 clear_mouse_face (hlinfo);
12227 /* Mouse is down, but on different tool-bar item? */
12228 mouse_down_p = (dpyinfo->grabbed
12229 && f == last_mouse_frame
12230 && FRAME_LIVE_P (f));
12231 if (mouse_down_p
12232 && last_tool_bar_item != prop_idx)
12233 return;
12235 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12236 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12238 /* If tool-bar item is not enabled, don't highlight it. */
12239 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12240 if (!NILP (enabled_p))
12242 /* Compute the x-position of the glyph. In front and past the
12243 image is a space. We include this in the highlighted area. */
12244 row = MATRIX_ROW (w->current_matrix, vpos);
12245 for (i = x = 0; i < hpos; ++i)
12246 x += row->glyphs[TEXT_AREA][i].pixel_width;
12248 /* Record this as the current active region. */
12249 hlinfo->mouse_face_beg_col = hpos;
12250 hlinfo->mouse_face_beg_row = vpos;
12251 hlinfo->mouse_face_beg_x = x;
12252 hlinfo->mouse_face_beg_y = row->y;
12253 hlinfo->mouse_face_past_end = 0;
12255 hlinfo->mouse_face_end_col = hpos + 1;
12256 hlinfo->mouse_face_end_row = vpos;
12257 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12258 hlinfo->mouse_face_end_y = row->y;
12259 hlinfo->mouse_face_window = window;
12260 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12262 /* Display it as active. */
12263 show_mouse_face (hlinfo, draw);
12264 hlinfo->mouse_face_image_state = draw;
12267 set_help_echo:
12269 /* Set help_echo_string to a help string to display for this tool-bar item.
12270 XTread_socket does the rest. */
12271 help_echo_object = help_echo_window = Qnil;
12272 help_echo_pos = -1;
12273 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12274 if (NILP (help_echo_string))
12275 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12278 #endif /* HAVE_WINDOW_SYSTEM */
12282 /************************************************************************
12283 Horizontal scrolling
12284 ************************************************************************/
12286 static int hscroll_window_tree (Lisp_Object);
12287 static int hscroll_windows (Lisp_Object);
12289 /* For all leaf windows in the window tree rooted at WINDOW, set their
12290 hscroll value so that PT is (i) visible in the window, and (ii) so
12291 that it is not within a certain margin at the window's left and
12292 right border. Value is non-zero if any window's hscroll has been
12293 changed. */
12295 static int
12296 hscroll_window_tree (Lisp_Object window)
12298 int hscrolled_p = 0;
12299 int hscroll_relative_p = FLOATP (Vhscroll_step);
12300 int hscroll_step_abs = 0;
12301 double hscroll_step_rel = 0;
12303 if (hscroll_relative_p)
12305 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12306 if (hscroll_step_rel < 0)
12308 hscroll_relative_p = 0;
12309 hscroll_step_abs = 0;
12312 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12314 hscroll_step_abs = XINT (Vhscroll_step);
12315 if (hscroll_step_abs < 0)
12316 hscroll_step_abs = 0;
12318 else
12319 hscroll_step_abs = 0;
12321 while (WINDOWP (window))
12323 struct window *w = XWINDOW (window);
12325 if (WINDOWP (w->hchild))
12326 hscrolled_p |= hscroll_window_tree (w->hchild);
12327 else if (WINDOWP (w->vchild))
12328 hscrolled_p |= hscroll_window_tree (w->vchild);
12329 else if (w->cursor.vpos >= 0)
12331 int h_margin;
12332 int text_area_width;
12333 struct glyph_row *current_cursor_row
12334 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12335 struct glyph_row *desired_cursor_row
12336 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12337 struct glyph_row *cursor_row
12338 = (desired_cursor_row->enabled_p
12339 ? desired_cursor_row
12340 : current_cursor_row);
12341 int row_r2l_p = cursor_row->reversed_p;
12343 text_area_width = window_box_width (w, TEXT_AREA);
12345 /* Scroll when cursor is inside this scroll margin. */
12346 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12348 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12349 /* For left-to-right rows, hscroll when cursor is either
12350 (i) inside the right hscroll margin, or (ii) if it is
12351 inside the left margin and the window is already
12352 hscrolled. */
12353 && ((!row_r2l_p
12354 && ((w->hscroll
12355 && w->cursor.x <= h_margin)
12356 || (cursor_row->enabled_p
12357 && cursor_row->truncated_on_right_p
12358 && (w->cursor.x >= text_area_width - h_margin))))
12359 /* For right-to-left rows, the logic is similar,
12360 except that rules for scrolling to left and right
12361 are reversed. E.g., if cursor.x <= h_margin, we
12362 need to hscroll "to the right" unconditionally,
12363 and that will scroll the screen to the left so as
12364 to reveal the next portion of the row. */
12365 || (row_r2l_p
12366 && ((cursor_row->enabled_p
12367 /* FIXME: It is confusing to set the
12368 truncated_on_right_p flag when R2L rows
12369 are actually truncated on the left. */
12370 && cursor_row->truncated_on_right_p
12371 && w->cursor.x <= h_margin)
12372 || (w->hscroll
12373 && (w->cursor.x >= text_area_width - h_margin))))))
12375 struct it it;
12376 ptrdiff_t hscroll;
12377 struct buffer *saved_current_buffer;
12378 ptrdiff_t pt;
12379 int wanted_x;
12381 /* Find point in a display of infinite width. */
12382 saved_current_buffer = current_buffer;
12383 current_buffer = XBUFFER (w->buffer);
12385 if (w == XWINDOW (selected_window))
12386 pt = PT;
12387 else
12389 pt = marker_position (w->pointm);
12390 pt = max (BEGV, pt);
12391 pt = min (ZV, pt);
12394 /* Move iterator to pt starting at cursor_row->start in
12395 a line with infinite width. */
12396 init_to_row_start (&it, w, cursor_row);
12397 it.last_visible_x = INFINITY;
12398 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12399 current_buffer = saved_current_buffer;
12401 /* Position cursor in window. */
12402 if (!hscroll_relative_p && hscroll_step_abs == 0)
12403 hscroll = max (0, (it.current_x
12404 - (ITERATOR_AT_END_OF_LINE_P (&it)
12405 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12406 : (text_area_width / 2))))
12407 / FRAME_COLUMN_WIDTH (it.f);
12408 else if ((!row_r2l_p
12409 && w->cursor.x >= text_area_width - h_margin)
12410 || (row_r2l_p && w->cursor.x <= h_margin))
12412 if (hscroll_relative_p)
12413 wanted_x = text_area_width * (1 - hscroll_step_rel)
12414 - h_margin;
12415 else
12416 wanted_x = text_area_width
12417 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12418 - h_margin;
12419 hscroll
12420 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12422 else
12424 if (hscroll_relative_p)
12425 wanted_x = text_area_width * hscroll_step_rel
12426 + h_margin;
12427 else
12428 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12429 + h_margin;
12430 hscroll
12431 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12433 hscroll = max (hscroll, w->min_hscroll);
12435 /* Don't prevent redisplay optimizations if hscroll
12436 hasn't changed, as it will unnecessarily slow down
12437 redisplay. */
12438 if (w->hscroll != hscroll)
12440 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12441 w->hscroll = hscroll;
12442 hscrolled_p = 1;
12447 window = w->next;
12450 /* Value is non-zero if hscroll of any leaf window has been changed. */
12451 return hscrolled_p;
12455 /* Set hscroll so that cursor is visible and not inside horizontal
12456 scroll margins for all windows in the tree rooted at WINDOW. See
12457 also hscroll_window_tree above. Value is non-zero if any window's
12458 hscroll has been changed. If it has, desired matrices on the frame
12459 of WINDOW are cleared. */
12461 static int
12462 hscroll_windows (Lisp_Object window)
12464 int hscrolled_p = hscroll_window_tree (window);
12465 if (hscrolled_p)
12466 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12467 return hscrolled_p;
12472 /************************************************************************
12473 Redisplay
12474 ************************************************************************/
12476 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12477 to a non-zero value. This is sometimes handy to have in a debugger
12478 session. */
12480 #ifdef GLYPH_DEBUG
12482 /* First and last unchanged row for try_window_id. */
12484 static int debug_first_unchanged_at_end_vpos;
12485 static int debug_last_unchanged_at_beg_vpos;
12487 /* Delta vpos and y. */
12489 static int debug_dvpos, debug_dy;
12491 /* Delta in characters and bytes for try_window_id. */
12493 static ptrdiff_t debug_delta, debug_delta_bytes;
12495 /* Values of window_end_pos and window_end_vpos at the end of
12496 try_window_id. */
12498 static ptrdiff_t debug_end_vpos;
12500 /* Append a string to W->desired_matrix->method. FMT is a printf
12501 format string. If trace_redisplay_p is non-zero also printf the
12502 resulting string to stderr. */
12504 static void debug_method_add (struct window *, char const *, ...)
12505 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12507 static void
12508 debug_method_add (struct window *w, char const *fmt, ...)
12510 char *method = w->desired_matrix->method;
12511 int len = strlen (method);
12512 int size = sizeof w->desired_matrix->method;
12513 int remaining = size - len - 1;
12514 va_list ap;
12516 if (len && remaining)
12518 method[len] = '|';
12519 --remaining, ++len;
12522 va_start (ap, fmt);
12523 vsnprintf (method + len, remaining + 1, fmt, ap);
12524 va_end (ap);
12526 if (trace_redisplay_p)
12527 fprintf (stderr, "%p (%s): %s\n",
12529 ((BUFFERP (w->buffer)
12530 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12531 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12532 : "no buffer"),
12533 method + len);
12536 #endif /* GLYPH_DEBUG */
12539 /* Value is non-zero if all changes in window W, which displays
12540 current_buffer, are in the text between START and END. START is a
12541 buffer position, END is given as a distance from Z. Used in
12542 redisplay_internal for display optimization. */
12544 static inline int
12545 text_outside_line_unchanged_p (struct window *w,
12546 ptrdiff_t start, ptrdiff_t end)
12548 int unchanged_p = 1;
12550 /* If text or overlays have changed, see where. */
12551 if (w->last_modified < MODIFF
12552 || w->last_overlay_modified < OVERLAY_MODIFF)
12554 /* Gap in the line? */
12555 if (GPT < start || Z - GPT < end)
12556 unchanged_p = 0;
12558 /* Changes start in front of the line, or end after it? */
12559 if (unchanged_p
12560 && (BEG_UNCHANGED < start - 1
12561 || END_UNCHANGED < end))
12562 unchanged_p = 0;
12564 /* If selective display, can't optimize if changes start at the
12565 beginning of the line. */
12566 if (unchanged_p
12567 && INTEGERP (BVAR (current_buffer, selective_display))
12568 && XINT (BVAR (current_buffer, selective_display)) > 0
12569 && (BEG_UNCHANGED < start || GPT <= start))
12570 unchanged_p = 0;
12572 /* If there are overlays at the start or end of the line, these
12573 may have overlay strings with newlines in them. A change at
12574 START, for instance, may actually concern the display of such
12575 overlay strings as well, and they are displayed on different
12576 lines. So, quickly rule out this case. (For the future, it
12577 might be desirable to implement something more telling than
12578 just BEG/END_UNCHANGED.) */
12579 if (unchanged_p)
12581 if (BEG + BEG_UNCHANGED == start
12582 && overlay_touches_p (start))
12583 unchanged_p = 0;
12584 if (END_UNCHANGED == end
12585 && overlay_touches_p (Z - end))
12586 unchanged_p = 0;
12589 /* Under bidi reordering, adding or deleting a character in the
12590 beginning of a paragraph, before the first strong directional
12591 character, can change the base direction of the paragraph (unless
12592 the buffer specifies a fixed paragraph direction), which will
12593 require to redisplay the whole paragraph. It might be worthwhile
12594 to find the paragraph limits and widen the range of redisplayed
12595 lines to that, but for now just give up this optimization. */
12596 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12597 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12598 unchanged_p = 0;
12601 return unchanged_p;
12605 /* Do a frame update, taking possible shortcuts into account. This is
12606 the main external entry point for redisplay.
12608 If the last redisplay displayed an echo area message and that message
12609 is no longer requested, we clear the echo area or bring back the
12610 mini-buffer if that is in use. */
12612 void
12613 redisplay (void)
12615 redisplay_internal ();
12619 static Lisp_Object
12620 overlay_arrow_string_or_property (Lisp_Object var)
12622 Lisp_Object val;
12624 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12625 return val;
12627 return Voverlay_arrow_string;
12630 /* Return 1 if there are any overlay-arrows in current_buffer. */
12631 static int
12632 overlay_arrow_in_current_buffer_p (void)
12634 Lisp_Object vlist;
12636 for (vlist = Voverlay_arrow_variable_list;
12637 CONSP (vlist);
12638 vlist = XCDR (vlist))
12640 Lisp_Object var = XCAR (vlist);
12641 Lisp_Object val;
12643 if (!SYMBOLP (var))
12644 continue;
12645 val = find_symbol_value (var);
12646 if (MARKERP (val)
12647 && current_buffer == XMARKER (val)->buffer)
12648 return 1;
12650 return 0;
12654 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12655 has changed. */
12657 static int
12658 overlay_arrows_changed_p (void)
12660 Lisp_Object vlist;
12662 for (vlist = Voverlay_arrow_variable_list;
12663 CONSP (vlist);
12664 vlist = XCDR (vlist))
12666 Lisp_Object var = XCAR (vlist);
12667 Lisp_Object val, pstr;
12669 if (!SYMBOLP (var))
12670 continue;
12671 val = find_symbol_value (var);
12672 if (!MARKERP (val))
12673 continue;
12674 if (! EQ (COERCE_MARKER (val),
12675 Fget (var, Qlast_arrow_position))
12676 || ! (pstr = overlay_arrow_string_or_property (var),
12677 EQ (pstr, Fget (var, Qlast_arrow_string))))
12678 return 1;
12680 return 0;
12683 /* Mark overlay arrows to be updated on next redisplay. */
12685 static void
12686 update_overlay_arrows (int up_to_date)
12688 Lisp_Object vlist;
12690 for (vlist = Voverlay_arrow_variable_list;
12691 CONSP (vlist);
12692 vlist = XCDR (vlist))
12694 Lisp_Object var = XCAR (vlist);
12696 if (!SYMBOLP (var))
12697 continue;
12699 if (up_to_date > 0)
12701 Lisp_Object val = find_symbol_value (var);
12702 Fput (var, Qlast_arrow_position,
12703 COERCE_MARKER (val));
12704 Fput (var, Qlast_arrow_string,
12705 overlay_arrow_string_or_property (var));
12707 else if (up_to_date < 0
12708 || !NILP (Fget (var, Qlast_arrow_position)))
12710 Fput (var, Qlast_arrow_position, Qt);
12711 Fput (var, Qlast_arrow_string, Qt);
12717 /* Return overlay arrow string to display at row.
12718 Return integer (bitmap number) for arrow bitmap in left fringe.
12719 Return nil if no overlay arrow. */
12721 static Lisp_Object
12722 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12724 Lisp_Object vlist;
12726 for (vlist = Voverlay_arrow_variable_list;
12727 CONSP (vlist);
12728 vlist = XCDR (vlist))
12730 Lisp_Object var = XCAR (vlist);
12731 Lisp_Object val;
12733 if (!SYMBOLP (var))
12734 continue;
12736 val = find_symbol_value (var);
12738 if (MARKERP (val)
12739 && current_buffer == XMARKER (val)->buffer
12740 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12742 if (FRAME_WINDOW_P (it->f)
12743 /* FIXME: if ROW->reversed_p is set, this should test
12744 the right fringe, not the left one. */
12745 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12747 #ifdef HAVE_WINDOW_SYSTEM
12748 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12750 int fringe_bitmap;
12751 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12752 return make_number (fringe_bitmap);
12754 #endif
12755 return make_number (-1); /* Use default arrow bitmap */
12757 return overlay_arrow_string_or_property (var);
12761 return Qnil;
12764 /* Return 1 if point moved out of or into a composition. Otherwise
12765 return 0. PREV_BUF and PREV_PT are the last point buffer and
12766 position. BUF and PT are the current point buffer and position. */
12768 static int
12769 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12770 struct buffer *buf, ptrdiff_t pt)
12772 ptrdiff_t start, end;
12773 Lisp_Object prop;
12774 Lisp_Object buffer;
12776 XSETBUFFER (buffer, buf);
12777 /* Check a composition at the last point if point moved within the
12778 same buffer. */
12779 if (prev_buf == buf)
12781 if (prev_pt == pt)
12782 /* Point didn't move. */
12783 return 0;
12785 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12786 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12787 && COMPOSITION_VALID_P (start, end, prop)
12788 && start < prev_pt && end > prev_pt)
12789 /* The last point was within the composition. Return 1 iff
12790 point moved out of the composition. */
12791 return (pt <= start || pt >= end);
12794 /* Check a composition at the current point. */
12795 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12796 && find_composition (pt, -1, &start, &end, &prop, buffer)
12797 && COMPOSITION_VALID_P (start, end, prop)
12798 && start < pt && end > pt);
12802 /* Reconsider the setting of B->clip_changed which is displayed
12803 in window W. */
12805 static inline void
12806 reconsider_clip_changes (struct window *w, struct buffer *b)
12808 if (b->clip_changed
12809 && !NILP (w->window_end_valid)
12810 && w->current_matrix->buffer == b
12811 && w->current_matrix->zv == BUF_ZV (b)
12812 && w->current_matrix->begv == BUF_BEGV (b))
12813 b->clip_changed = 0;
12815 /* If display wasn't paused, and W is not a tool bar window, see if
12816 point has been moved into or out of a composition. In that case,
12817 we set b->clip_changed to 1 to force updating the screen. If
12818 b->clip_changed has already been set to 1, we can skip this
12819 check. */
12820 if (!b->clip_changed
12821 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12823 ptrdiff_t pt;
12825 if (w == XWINDOW (selected_window))
12826 pt = PT;
12827 else
12828 pt = marker_position (w->pointm);
12830 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12831 || pt != w->last_point)
12832 && check_point_in_composition (w->current_matrix->buffer,
12833 w->last_point,
12834 XBUFFER (w->buffer), pt))
12835 b->clip_changed = 1;
12840 /* Select FRAME to forward the values of frame-local variables into C
12841 variables so that the redisplay routines can access those values
12842 directly. */
12844 static void
12845 select_frame_for_redisplay (Lisp_Object frame)
12847 Lisp_Object tail, tem;
12848 Lisp_Object old = selected_frame;
12849 struct Lisp_Symbol *sym;
12851 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12853 selected_frame = frame;
12855 do {
12856 for (tail = XFRAME (frame)->param_alist;
12857 CONSP (tail); tail = XCDR (tail))
12858 if (CONSP (XCAR (tail))
12859 && (tem = XCAR (XCAR (tail)),
12860 SYMBOLP (tem))
12861 && (sym = indirect_variable (XSYMBOL (tem)),
12862 sym->redirect == SYMBOL_LOCALIZED)
12863 && sym->val.blv->frame_local)
12864 /* Use find_symbol_value rather than Fsymbol_value
12865 to avoid an error if it is void. */
12866 find_symbol_value (tem);
12867 } while (!EQ (frame, old) && (frame = old, 1));
12871 #define STOP_POLLING \
12872 do { if (! polling_stopped_here) stop_polling (); \
12873 polling_stopped_here = 1; } while (0)
12875 #define RESUME_POLLING \
12876 do { if (polling_stopped_here) start_polling (); \
12877 polling_stopped_here = 0; } while (0)
12880 /* Perhaps in the future avoid recentering windows if it
12881 is not necessary; currently that causes some problems. */
12883 static void
12884 redisplay_internal (void)
12886 struct window *w = XWINDOW (selected_window);
12887 struct window *sw;
12888 struct frame *fr;
12889 int pending;
12890 int must_finish = 0;
12891 struct text_pos tlbufpos, tlendpos;
12892 int number_of_visible_frames;
12893 ptrdiff_t count, count1;
12894 struct frame *sf;
12895 int polling_stopped_here = 0;
12896 Lisp_Object old_frame = selected_frame;
12898 /* Non-zero means redisplay has to consider all windows on all
12899 frames. Zero means, only selected_window is considered. */
12900 int consider_all_windows_p;
12902 /* Non-zero means redisplay has to redisplay the miniwindow */
12903 int update_miniwindow_p = 0;
12905 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12907 /* No redisplay if running in batch mode or frame is not yet fully
12908 initialized, or redisplay is explicitly turned off by setting
12909 Vinhibit_redisplay. */
12910 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12911 || !NILP (Vinhibit_redisplay))
12912 return;
12914 /* Don't examine these until after testing Vinhibit_redisplay.
12915 When Emacs is shutting down, perhaps because its connection to
12916 X has dropped, we should not look at them at all. */
12917 fr = XFRAME (w->frame);
12918 sf = SELECTED_FRAME ();
12920 if (!fr->glyphs_initialized_p)
12921 return;
12923 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12924 if (popup_activated ())
12925 return;
12926 #endif
12928 /* I don't think this happens but let's be paranoid. */
12929 if (redisplaying_p)
12930 return;
12932 /* Record a function that resets redisplaying_p to its old value
12933 when we leave this function. */
12934 count = SPECPDL_INDEX ();
12935 record_unwind_protect (unwind_redisplay,
12936 Fcons (make_number (redisplaying_p), selected_frame));
12937 ++redisplaying_p;
12938 specbind (Qinhibit_free_realized_faces, Qnil);
12941 Lisp_Object tail, frame;
12943 FOR_EACH_FRAME (tail, frame)
12945 struct frame *f = XFRAME (frame);
12946 f->already_hscrolled_p = 0;
12950 retry:
12951 /* Remember the currently selected window. */
12952 sw = w;
12954 if (!EQ (old_frame, selected_frame)
12955 && FRAME_LIVE_P (XFRAME (old_frame)))
12956 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12957 selected_frame and selected_window to be temporarily out-of-sync so
12958 when we come back here via `goto retry', we need to resync because we
12959 may need to run Elisp code (via prepare_menu_bars). */
12960 select_frame_for_redisplay (old_frame);
12962 pending = 0;
12963 reconsider_clip_changes (w, current_buffer);
12964 last_escape_glyph_frame = NULL;
12965 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12966 last_glyphless_glyph_frame = NULL;
12967 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12969 /* If new fonts have been loaded that make a glyph matrix adjustment
12970 necessary, do it. */
12971 if (fonts_changed_p)
12973 adjust_glyphs (NULL);
12974 ++windows_or_buffers_changed;
12975 fonts_changed_p = 0;
12978 /* If face_change_count is non-zero, init_iterator will free all
12979 realized faces, which includes the faces referenced from current
12980 matrices. So, we can't reuse current matrices in this case. */
12981 if (face_change_count)
12982 ++windows_or_buffers_changed;
12984 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12985 && FRAME_TTY (sf)->previous_frame != sf)
12987 /* Since frames on a single ASCII terminal share the same
12988 display area, displaying a different frame means redisplay
12989 the whole thing. */
12990 windows_or_buffers_changed++;
12991 SET_FRAME_GARBAGED (sf);
12992 #ifndef DOS_NT
12993 set_tty_color_mode (FRAME_TTY (sf), sf);
12994 #endif
12995 FRAME_TTY (sf)->previous_frame = sf;
12998 /* Set the visible flags for all frames. Do this before checking
12999 for resized or garbaged frames; they want to know if their frames
13000 are visible. See the comment in frame.h for
13001 FRAME_SAMPLE_VISIBILITY. */
13003 Lisp_Object tail, frame;
13005 number_of_visible_frames = 0;
13007 FOR_EACH_FRAME (tail, frame)
13009 struct frame *f = XFRAME (frame);
13011 FRAME_SAMPLE_VISIBILITY (f);
13012 if (FRAME_VISIBLE_P (f))
13013 ++number_of_visible_frames;
13014 clear_desired_matrices (f);
13018 /* Notice any pending interrupt request to change frame size. */
13019 do_pending_window_change (1);
13021 /* do_pending_window_change could change the selected_window due to
13022 frame resizing which makes the selected window too small. */
13023 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13025 sw = w;
13026 reconsider_clip_changes (w, current_buffer);
13029 /* Clear frames marked as garbaged. */
13030 if (frame_garbaged)
13031 clear_garbaged_frames ();
13033 /* Build menubar and tool-bar items. */
13034 if (NILP (Vmemory_full))
13035 prepare_menu_bars ();
13037 if (windows_or_buffers_changed)
13038 update_mode_lines++;
13040 /* Detect case that we need to write or remove a star in the mode line. */
13041 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13043 w->update_mode_line = 1;
13044 if (buffer_shared > 1)
13045 update_mode_lines++;
13048 /* Avoid invocation of point motion hooks by `current_column' below. */
13049 count1 = SPECPDL_INDEX ();
13050 specbind (Qinhibit_point_motion_hooks, Qt);
13052 /* If %c is in the mode line, update it if needed. */
13053 if (!NILP (w->column_number_displayed)
13054 /* This alternative quickly identifies a common case
13055 where no change is needed. */
13056 && !(PT == w->last_point
13057 && w->last_modified >= MODIFF
13058 && w->last_overlay_modified >= OVERLAY_MODIFF)
13059 && (XFASTINT (w->column_number_displayed) != current_column ()))
13060 w->update_mode_line = 1;
13062 unbind_to (count1, Qnil);
13064 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13066 /* The variable buffer_shared is set in redisplay_window and
13067 indicates that we redisplay a buffer in different windows. See
13068 there. */
13069 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13070 || cursor_type_changed);
13072 /* If specs for an arrow have changed, do thorough redisplay
13073 to ensure we remove any arrow that should no longer exist. */
13074 if (overlay_arrows_changed_p ())
13075 consider_all_windows_p = windows_or_buffers_changed = 1;
13077 /* Normally the message* functions will have already displayed and
13078 updated the echo area, but the frame may have been trashed, or
13079 the update may have been preempted, so display the echo area
13080 again here. Checking message_cleared_p captures the case that
13081 the echo area should be cleared. */
13082 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13083 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13084 || (message_cleared_p
13085 && minibuf_level == 0
13086 /* If the mini-window is currently selected, this means the
13087 echo-area doesn't show through. */
13088 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13090 int window_height_changed_p = echo_area_display (0);
13092 if (message_cleared_p)
13093 update_miniwindow_p = 1;
13095 must_finish = 1;
13097 /* If we don't display the current message, don't clear the
13098 message_cleared_p flag, because, if we did, we wouldn't clear
13099 the echo area in the next redisplay which doesn't preserve
13100 the echo area. */
13101 if (!display_last_displayed_message_p)
13102 message_cleared_p = 0;
13104 if (fonts_changed_p)
13105 goto retry;
13106 else if (window_height_changed_p)
13108 consider_all_windows_p = 1;
13109 ++update_mode_lines;
13110 ++windows_or_buffers_changed;
13112 /* If window configuration was changed, frames may have been
13113 marked garbaged. Clear them or we will experience
13114 surprises wrt scrolling. */
13115 if (frame_garbaged)
13116 clear_garbaged_frames ();
13119 else if (EQ (selected_window, minibuf_window)
13120 && (current_buffer->clip_changed
13121 || w->last_modified < MODIFF
13122 || w->last_overlay_modified < OVERLAY_MODIFF)
13123 && resize_mini_window (w, 0))
13125 /* Resized active mini-window to fit the size of what it is
13126 showing if its contents might have changed. */
13127 must_finish = 1;
13128 /* FIXME: this causes all frames to be updated, which seems unnecessary
13129 since only the current frame needs to be considered. This function needs
13130 to be rewritten with two variables, consider_all_windows and
13131 consider_all_frames. */
13132 consider_all_windows_p = 1;
13133 ++windows_or_buffers_changed;
13134 ++update_mode_lines;
13136 /* If window configuration was changed, frames may have been
13137 marked garbaged. Clear them or we will experience
13138 surprises wrt scrolling. */
13139 if (frame_garbaged)
13140 clear_garbaged_frames ();
13144 /* If showing the region, and mark has changed, we must redisplay
13145 the whole window. The assignment to this_line_start_pos prevents
13146 the optimization directly below this if-statement. */
13147 if (((!NILP (Vtransient_mark_mode)
13148 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13149 != !NILP (w->region_showing))
13150 || (!NILP (w->region_showing)
13151 && !EQ (w->region_showing,
13152 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13153 CHARPOS (this_line_start_pos) = 0;
13155 /* Optimize the case that only the line containing the cursor in the
13156 selected window has changed. Variables starting with this_ are
13157 set in display_line and record information about the line
13158 containing the cursor. */
13159 tlbufpos = this_line_start_pos;
13160 tlendpos = this_line_end_pos;
13161 if (!consider_all_windows_p
13162 && CHARPOS (tlbufpos) > 0
13163 && !w->update_mode_line
13164 && !current_buffer->clip_changed
13165 && !current_buffer->prevent_redisplay_optimizations_p
13166 && FRAME_VISIBLE_P (XFRAME (w->frame))
13167 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13168 /* Make sure recorded data applies to current buffer, etc. */
13169 && this_line_buffer == current_buffer
13170 && current_buffer == XBUFFER (w->buffer)
13171 && !w->force_start
13172 && !w->optional_new_start
13173 /* Point must be on the line that we have info recorded about. */
13174 && PT >= CHARPOS (tlbufpos)
13175 && PT <= Z - CHARPOS (tlendpos)
13176 /* All text outside that line, including its final newline,
13177 must be unchanged. */
13178 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13179 CHARPOS (tlendpos)))
13181 if (CHARPOS (tlbufpos) > BEGV
13182 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13183 && (CHARPOS (tlbufpos) == ZV
13184 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13185 /* Former continuation line has disappeared by becoming empty. */
13186 goto cancel;
13187 else if (w->last_modified < MODIFF
13188 || w->last_overlay_modified < OVERLAY_MODIFF
13189 || MINI_WINDOW_P (w))
13191 /* We have to handle the case of continuation around a
13192 wide-column character (see the comment in indent.c around
13193 line 1340).
13195 For instance, in the following case:
13197 -------- Insert --------
13198 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13199 J_I_ ==> J_I_ `^^' are cursors.
13200 ^^ ^^
13201 -------- --------
13203 As we have to redraw the line above, we cannot use this
13204 optimization. */
13206 struct it it;
13207 int line_height_before = this_line_pixel_height;
13209 /* Note that start_display will handle the case that the
13210 line starting at tlbufpos is a continuation line. */
13211 start_display (&it, w, tlbufpos);
13213 /* Implementation note: It this still necessary? */
13214 if (it.current_x != this_line_start_x)
13215 goto cancel;
13217 TRACE ((stderr, "trying display optimization 1\n"));
13218 w->cursor.vpos = -1;
13219 overlay_arrow_seen = 0;
13220 it.vpos = this_line_vpos;
13221 it.current_y = this_line_y;
13222 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13223 display_line (&it);
13225 /* If line contains point, is not continued,
13226 and ends at same distance from eob as before, we win. */
13227 if (w->cursor.vpos >= 0
13228 /* Line is not continued, otherwise this_line_start_pos
13229 would have been set to 0 in display_line. */
13230 && CHARPOS (this_line_start_pos)
13231 /* Line ends as before. */
13232 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13233 /* Line has same height as before. Otherwise other lines
13234 would have to be shifted up or down. */
13235 && this_line_pixel_height == line_height_before)
13237 /* If this is not the window's last line, we must adjust
13238 the charstarts of the lines below. */
13239 if (it.current_y < it.last_visible_y)
13241 struct glyph_row *row
13242 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13243 ptrdiff_t delta, delta_bytes;
13245 /* We used to distinguish between two cases here,
13246 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13247 when the line ends in a newline or the end of the
13248 buffer's accessible portion. But both cases did
13249 the same, so they were collapsed. */
13250 delta = (Z
13251 - CHARPOS (tlendpos)
13252 - MATRIX_ROW_START_CHARPOS (row));
13253 delta_bytes = (Z_BYTE
13254 - BYTEPOS (tlendpos)
13255 - MATRIX_ROW_START_BYTEPOS (row));
13257 increment_matrix_positions (w->current_matrix,
13258 this_line_vpos + 1,
13259 w->current_matrix->nrows,
13260 delta, delta_bytes);
13263 /* If this row displays text now but previously didn't,
13264 or vice versa, w->window_end_vpos may have to be
13265 adjusted. */
13266 if ((it.glyph_row - 1)->displays_text_p)
13268 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13269 WSET (w, window_end_vpos, make_number (this_line_vpos));
13271 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13272 && this_line_vpos > 0)
13273 WSET (w, window_end_vpos, make_number (this_line_vpos - 1));
13274 WSET (w, window_end_valid, Qnil);
13276 /* Update hint: No need to try to scroll in update_window. */
13277 w->desired_matrix->no_scrolling_p = 1;
13279 #ifdef GLYPH_DEBUG
13280 *w->desired_matrix->method = 0;
13281 debug_method_add (w, "optimization 1");
13282 #endif
13283 #ifdef HAVE_WINDOW_SYSTEM
13284 update_window_fringes (w, 0);
13285 #endif
13286 goto update;
13288 else
13289 goto cancel;
13291 else if (/* Cursor position hasn't changed. */
13292 PT == w->last_point
13293 /* Make sure the cursor was last displayed
13294 in this window. Otherwise we have to reposition it. */
13295 && 0 <= w->cursor.vpos
13296 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13298 if (!must_finish)
13300 do_pending_window_change (1);
13301 /* If selected_window changed, redisplay again. */
13302 if (WINDOWP (selected_window)
13303 && (w = XWINDOW (selected_window)) != sw)
13304 goto retry;
13306 /* We used to always goto end_of_redisplay here, but this
13307 isn't enough if we have a blinking cursor. */
13308 if (w->cursor_off_p == w->last_cursor_off_p)
13309 goto end_of_redisplay;
13311 goto update;
13313 /* If highlighting the region, or if the cursor is in the echo area,
13314 then we can't just move the cursor. */
13315 else if (! (!NILP (Vtransient_mark_mode)
13316 && !NILP (BVAR (current_buffer, mark_active)))
13317 && (EQ (selected_window,
13318 BVAR (current_buffer, last_selected_window))
13319 || highlight_nonselected_windows)
13320 && NILP (w->region_showing)
13321 && NILP (Vshow_trailing_whitespace)
13322 && !cursor_in_echo_area)
13324 struct it it;
13325 struct glyph_row *row;
13327 /* Skip from tlbufpos to PT and see where it is. Note that
13328 PT may be in invisible text. If so, we will end at the
13329 next visible position. */
13330 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13331 NULL, DEFAULT_FACE_ID);
13332 it.current_x = this_line_start_x;
13333 it.current_y = this_line_y;
13334 it.vpos = this_line_vpos;
13336 /* The call to move_it_to stops in front of PT, but
13337 moves over before-strings. */
13338 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13340 if (it.vpos == this_line_vpos
13341 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13342 row->enabled_p))
13344 eassert (this_line_vpos == it.vpos);
13345 eassert (this_line_y == it.current_y);
13346 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13347 #ifdef GLYPH_DEBUG
13348 *w->desired_matrix->method = 0;
13349 debug_method_add (w, "optimization 3");
13350 #endif
13351 goto update;
13353 else
13354 goto cancel;
13357 cancel:
13358 /* Text changed drastically or point moved off of line. */
13359 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13362 CHARPOS (this_line_start_pos) = 0;
13363 consider_all_windows_p |= buffer_shared > 1;
13364 ++clear_face_cache_count;
13365 #ifdef HAVE_WINDOW_SYSTEM
13366 ++clear_image_cache_count;
13367 #endif
13369 /* Build desired matrices, and update the display. If
13370 consider_all_windows_p is non-zero, do it for all windows on all
13371 frames. Otherwise do it for selected_window, only. */
13373 if (consider_all_windows_p)
13375 Lisp_Object tail, frame;
13377 FOR_EACH_FRAME (tail, frame)
13378 XFRAME (frame)->updated_p = 0;
13380 /* Recompute # windows showing selected buffer. This will be
13381 incremented each time such a window is displayed. */
13382 buffer_shared = 0;
13384 FOR_EACH_FRAME (tail, frame)
13386 struct frame *f = XFRAME (frame);
13388 /* We don't have to do anything for unselected terminal
13389 frames. */
13390 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13391 && !EQ (FRAME_TTY (f)->top_frame, frame))
13392 continue;
13394 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13396 if (! EQ (frame, selected_frame))
13397 /* Select the frame, for the sake of frame-local
13398 variables. */
13399 select_frame_for_redisplay (frame);
13401 /* Mark all the scroll bars to be removed; we'll redeem
13402 the ones we want when we redisplay their windows. */
13403 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13404 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13406 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13407 redisplay_windows (FRAME_ROOT_WINDOW (f));
13409 /* The X error handler may have deleted that frame. */
13410 if (!FRAME_LIVE_P (f))
13411 continue;
13413 /* Any scroll bars which redisplay_windows should have
13414 nuked should now go away. */
13415 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13416 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13418 /* If fonts changed, display again. */
13419 /* ??? rms: I suspect it is a mistake to jump all the way
13420 back to retry here. It should just retry this frame. */
13421 if (fonts_changed_p)
13422 goto retry;
13424 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13426 /* See if we have to hscroll. */
13427 if (!f->already_hscrolled_p)
13429 f->already_hscrolled_p = 1;
13430 if (hscroll_windows (f->root_window))
13431 goto retry;
13434 /* Prevent various kinds of signals during display
13435 update. stdio is not robust about handling
13436 signals, which can cause an apparent I/O
13437 error. */
13438 if (interrupt_input)
13439 unrequest_sigio ();
13440 STOP_POLLING;
13442 /* Update the display. */
13443 set_window_update_flags (XWINDOW (f->root_window), 1);
13444 pending |= update_frame (f, 0, 0);
13445 f->updated_p = 1;
13450 if (!EQ (old_frame, selected_frame)
13451 && FRAME_LIVE_P (XFRAME (old_frame)))
13452 /* We played a bit fast-and-loose above and allowed selected_frame
13453 and selected_window to be temporarily out-of-sync but let's make
13454 sure this stays contained. */
13455 select_frame_for_redisplay (old_frame);
13456 eassert (EQ (XFRAME (selected_frame)->selected_window,
13457 selected_window));
13459 if (!pending)
13461 /* Do the mark_window_display_accurate after all windows have
13462 been redisplayed because this call resets flags in buffers
13463 which are needed for proper redisplay. */
13464 FOR_EACH_FRAME (tail, frame)
13466 struct frame *f = XFRAME (frame);
13467 if (f->updated_p)
13469 mark_window_display_accurate (f->root_window, 1);
13470 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13471 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13476 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13478 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13479 struct frame *mini_frame;
13481 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13482 /* Use list_of_error, not Qerror, so that
13483 we catch only errors and don't run the debugger. */
13484 internal_condition_case_1 (redisplay_window_1, selected_window,
13485 list_of_error,
13486 redisplay_window_error);
13487 if (update_miniwindow_p)
13488 internal_condition_case_1 (redisplay_window_1, mini_window,
13489 list_of_error,
13490 redisplay_window_error);
13492 /* Compare desired and current matrices, perform output. */
13494 update:
13495 /* If fonts changed, display again. */
13496 if (fonts_changed_p)
13497 goto retry;
13499 /* Prevent various kinds of signals during display update.
13500 stdio is not robust about handling signals,
13501 which can cause an apparent I/O error. */
13502 if (interrupt_input)
13503 unrequest_sigio ();
13504 STOP_POLLING;
13506 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13508 if (hscroll_windows (selected_window))
13509 goto retry;
13511 XWINDOW (selected_window)->must_be_updated_p = 1;
13512 pending = update_frame (sf, 0, 0);
13515 /* We may have called echo_area_display at the top of this
13516 function. If the echo area is on another frame, that may
13517 have put text on a frame other than the selected one, so the
13518 above call to update_frame would not have caught it. Catch
13519 it here. */
13520 mini_window = FRAME_MINIBUF_WINDOW (sf);
13521 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13523 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13525 XWINDOW (mini_window)->must_be_updated_p = 1;
13526 pending |= update_frame (mini_frame, 0, 0);
13527 if (!pending && hscroll_windows (mini_window))
13528 goto retry;
13532 /* If display was paused because of pending input, make sure we do a
13533 thorough update the next time. */
13534 if (pending)
13536 /* Prevent the optimization at the beginning of
13537 redisplay_internal that tries a single-line update of the
13538 line containing the cursor in the selected window. */
13539 CHARPOS (this_line_start_pos) = 0;
13541 /* Let the overlay arrow be updated the next time. */
13542 update_overlay_arrows (0);
13544 /* If we pause after scrolling, some rows in the current
13545 matrices of some windows are not valid. */
13546 if (!WINDOW_FULL_WIDTH_P (w)
13547 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13548 update_mode_lines = 1;
13550 else
13552 if (!consider_all_windows_p)
13554 /* This has already been done above if
13555 consider_all_windows_p is set. */
13556 mark_window_display_accurate_1 (w, 1);
13558 /* Say overlay arrows are up to date. */
13559 update_overlay_arrows (1);
13561 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13562 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13565 update_mode_lines = 0;
13566 windows_or_buffers_changed = 0;
13567 cursor_type_changed = 0;
13570 /* Start SIGIO interrupts coming again. Having them off during the
13571 code above makes it less likely one will discard output, but not
13572 impossible, since there might be stuff in the system buffer here.
13573 But it is much hairier to try to do anything about that. */
13574 if (interrupt_input)
13575 request_sigio ();
13576 RESUME_POLLING;
13578 /* If a frame has become visible which was not before, redisplay
13579 again, so that we display it. Expose events for such a frame
13580 (which it gets when becoming visible) don't call the parts of
13581 redisplay constructing glyphs, so simply exposing a frame won't
13582 display anything in this case. So, we have to display these
13583 frames here explicitly. */
13584 if (!pending)
13586 Lisp_Object tail, frame;
13587 int new_count = 0;
13589 FOR_EACH_FRAME (tail, frame)
13591 int this_is_visible = 0;
13593 if (XFRAME (frame)->visible)
13594 this_is_visible = 1;
13595 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13596 if (XFRAME (frame)->visible)
13597 this_is_visible = 1;
13599 if (this_is_visible)
13600 new_count++;
13603 if (new_count != number_of_visible_frames)
13604 windows_or_buffers_changed++;
13607 /* Change frame size now if a change is pending. */
13608 do_pending_window_change (1);
13610 /* If we just did a pending size change, or have additional
13611 visible frames, or selected_window changed, redisplay again. */
13612 if ((windows_or_buffers_changed && !pending)
13613 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13614 goto retry;
13616 /* Clear the face and image caches.
13618 We used to do this only if consider_all_windows_p. But the cache
13619 needs to be cleared if a timer creates images in the current
13620 buffer (e.g. the test case in Bug#6230). */
13622 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13624 clear_face_cache (0);
13625 clear_face_cache_count = 0;
13628 #ifdef HAVE_WINDOW_SYSTEM
13629 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13631 clear_image_caches (Qnil);
13632 clear_image_cache_count = 0;
13634 #endif /* HAVE_WINDOW_SYSTEM */
13636 end_of_redisplay:
13637 unbind_to (count, Qnil);
13638 RESUME_POLLING;
13642 /* Redisplay, but leave alone any recent echo area message unless
13643 another message has been requested in its place.
13645 This is useful in situations where you need to redisplay but no
13646 user action has occurred, making it inappropriate for the message
13647 area to be cleared. See tracking_off and
13648 wait_reading_process_output for examples of these situations.
13650 FROM_WHERE is an integer saying from where this function was
13651 called. This is useful for debugging. */
13653 void
13654 redisplay_preserve_echo_area (int from_where)
13656 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13658 if (!NILP (echo_area_buffer[1]))
13660 /* We have a previously displayed message, but no current
13661 message. Redisplay the previous message. */
13662 display_last_displayed_message_p = 1;
13663 redisplay_internal ();
13664 display_last_displayed_message_p = 0;
13666 else
13667 redisplay_internal ();
13669 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13670 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13671 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13675 /* Function registered with record_unwind_protect in
13676 redisplay_internal. Reset redisplaying_p to the value it had
13677 before redisplay_internal was called, and clear
13678 prevent_freeing_realized_faces_p. It also selects the previously
13679 selected frame, unless it has been deleted (by an X connection
13680 failure during redisplay, for example). */
13682 static Lisp_Object
13683 unwind_redisplay (Lisp_Object val)
13685 Lisp_Object old_redisplaying_p, old_frame;
13687 old_redisplaying_p = XCAR (val);
13688 redisplaying_p = XFASTINT (old_redisplaying_p);
13689 old_frame = XCDR (val);
13690 if (! EQ (old_frame, selected_frame)
13691 && FRAME_LIVE_P (XFRAME (old_frame)))
13692 select_frame_for_redisplay (old_frame);
13693 return Qnil;
13697 /* Mark the display of window W as accurate or inaccurate. If
13698 ACCURATE_P is non-zero mark display of W as accurate. If
13699 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13700 redisplay_internal is called. */
13702 static void
13703 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13705 if (BUFFERP (w->buffer))
13707 struct buffer *b = XBUFFER (w->buffer);
13709 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13710 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13711 w->last_had_star
13712 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13714 if (accurate_p)
13716 b->clip_changed = 0;
13717 b->prevent_redisplay_optimizations_p = 0;
13719 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13720 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13721 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13722 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13724 w->current_matrix->buffer = b;
13725 w->current_matrix->begv = BUF_BEGV (b);
13726 w->current_matrix->zv = BUF_ZV (b);
13728 w->last_cursor = w->cursor;
13729 w->last_cursor_off_p = w->cursor_off_p;
13731 if (w == XWINDOW (selected_window))
13732 w->last_point = BUF_PT (b);
13733 else
13734 w->last_point = XMARKER (w->pointm)->charpos;
13738 if (accurate_p)
13740 WSET (w, window_end_valid, w->buffer);
13741 w->update_mode_line = 0;
13746 /* Mark the display of windows in the window tree rooted at WINDOW as
13747 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13748 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13749 be redisplayed the next time redisplay_internal is called. */
13751 void
13752 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13754 struct window *w;
13756 for (; !NILP (window); window = w->next)
13758 w = XWINDOW (window);
13759 mark_window_display_accurate_1 (w, accurate_p);
13761 if (!NILP (w->vchild))
13762 mark_window_display_accurate (w->vchild, accurate_p);
13763 if (!NILP (w->hchild))
13764 mark_window_display_accurate (w->hchild, accurate_p);
13767 if (accurate_p)
13769 update_overlay_arrows (1);
13771 else
13773 /* Force a thorough redisplay the next time by setting
13774 last_arrow_position and last_arrow_string to t, which is
13775 unequal to any useful value of Voverlay_arrow_... */
13776 update_overlay_arrows (-1);
13781 /* Return value in display table DP (Lisp_Char_Table *) for character
13782 C. Since a display table doesn't have any parent, we don't have to
13783 follow parent. Do not call this function directly but use the
13784 macro DISP_CHAR_VECTOR. */
13786 Lisp_Object
13787 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13789 Lisp_Object val;
13791 if (ASCII_CHAR_P (c))
13793 val = dp->ascii;
13794 if (SUB_CHAR_TABLE_P (val))
13795 val = XSUB_CHAR_TABLE (val)->contents[c];
13797 else
13799 Lisp_Object table;
13801 XSETCHAR_TABLE (table, dp);
13802 val = char_table_ref (table, c);
13804 if (NILP (val))
13805 val = dp->defalt;
13806 return val;
13811 /***********************************************************************
13812 Window Redisplay
13813 ***********************************************************************/
13815 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13817 static void
13818 redisplay_windows (Lisp_Object window)
13820 while (!NILP (window))
13822 struct window *w = XWINDOW (window);
13824 if (!NILP (w->hchild))
13825 redisplay_windows (w->hchild);
13826 else if (!NILP (w->vchild))
13827 redisplay_windows (w->vchild);
13828 else if (!NILP (w->buffer))
13830 displayed_buffer = XBUFFER (w->buffer);
13831 /* Use list_of_error, not Qerror, so that
13832 we catch only errors and don't run the debugger. */
13833 internal_condition_case_1 (redisplay_window_0, window,
13834 list_of_error,
13835 redisplay_window_error);
13838 window = w->next;
13842 static Lisp_Object
13843 redisplay_window_error (Lisp_Object ignore)
13845 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13846 return Qnil;
13849 static Lisp_Object
13850 redisplay_window_0 (Lisp_Object window)
13852 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13853 redisplay_window (window, 0);
13854 return Qnil;
13857 static Lisp_Object
13858 redisplay_window_1 (Lisp_Object window)
13860 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13861 redisplay_window (window, 1);
13862 return Qnil;
13866 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13867 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13868 which positions recorded in ROW differ from current buffer
13869 positions.
13871 Return 0 if cursor is not on this row, 1 otherwise. */
13873 static int
13874 set_cursor_from_row (struct window *w, struct glyph_row *row,
13875 struct glyph_matrix *matrix,
13876 ptrdiff_t delta, ptrdiff_t delta_bytes,
13877 int dy, int dvpos)
13879 struct glyph *glyph = row->glyphs[TEXT_AREA];
13880 struct glyph *end = glyph + row->used[TEXT_AREA];
13881 struct glyph *cursor = NULL;
13882 /* The last known character position in row. */
13883 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13884 int x = row->x;
13885 ptrdiff_t pt_old = PT - delta;
13886 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13887 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13888 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13889 /* A glyph beyond the edge of TEXT_AREA which we should never
13890 touch. */
13891 struct glyph *glyphs_end = end;
13892 /* Non-zero means we've found a match for cursor position, but that
13893 glyph has the avoid_cursor_p flag set. */
13894 int match_with_avoid_cursor = 0;
13895 /* Non-zero means we've seen at least one glyph that came from a
13896 display string. */
13897 int string_seen = 0;
13898 /* Largest and smallest buffer positions seen so far during scan of
13899 glyph row. */
13900 ptrdiff_t bpos_max = pos_before;
13901 ptrdiff_t bpos_min = pos_after;
13902 /* Last buffer position covered by an overlay string with an integer
13903 `cursor' property. */
13904 ptrdiff_t bpos_covered = 0;
13905 /* Non-zero means the display string on which to display the cursor
13906 comes from a text property, not from an overlay. */
13907 int string_from_text_prop = 0;
13909 /* Don't even try doing anything if called for a mode-line or
13910 header-line row, since the rest of the code isn't prepared to
13911 deal with such calamities. */
13912 eassert (!row->mode_line_p);
13913 if (row->mode_line_p)
13914 return 0;
13916 /* Skip over glyphs not having an object at the start and the end of
13917 the row. These are special glyphs like truncation marks on
13918 terminal frames. */
13919 if (row->displays_text_p)
13921 if (!row->reversed_p)
13923 while (glyph < end
13924 && INTEGERP (glyph->object)
13925 && glyph->charpos < 0)
13927 x += glyph->pixel_width;
13928 ++glyph;
13930 while (end > glyph
13931 && INTEGERP ((end - 1)->object)
13932 /* CHARPOS is zero for blanks and stretch glyphs
13933 inserted by extend_face_to_end_of_line. */
13934 && (end - 1)->charpos <= 0)
13935 --end;
13936 glyph_before = glyph - 1;
13937 glyph_after = end;
13939 else
13941 struct glyph *g;
13943 /* If the glyph row is reversed, we need to process it from back
13944 to front, so swap the edge pointers. */
13945 glyphs_end = end = glyph - 1;
13946 glyph += row->used[TEXT_AREA] - 1;
13948 while (glyph > end + 1
13949 && INTEGERP (glyph->object)
13950 && glyph->charpos < 0)
13952 --glyph;
13953 x -= glyph->pixel_width;
13955 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13956 --glyph;
13957 /* By default, in reversed rows we put the cursor on the
13958 rightmost (first in the reading order) glyph. */
13959 for (g = end + 1; g < glyph; g++)
13960 x += g->pixel_width;
13961 while (end < glyph
13962 && INTEGERP ((end + 1)->object)
13963 && (end + 1)->charpos <= 0)
13964 ++end;
13965 glyph_before = glyph + 1;
13966 glyph_after = end;
13969 else if (row->reversed_p)
13971 /* In R2L rows that don't display text, put the cursor on the
13972 rightmost glyph. Case in point: an empty last line that is
13973 part of an R2L paragraph. */
13974 cursor = end - 1;
13975 /* Avoid placing the cursor on the last glyph of the row, where
13976 on terminal frames we hold the vertical border between
13977 adjacent windows. */
13978 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13979 && !WINDOW_RIGHTMOST_P (w)
13980 && cursor == row->glyphs[LAST_AREA] - 1)
13981 cursor--;
13982 x = -1; /* will be computed below, at label compute_x */
13985 /* Step 1: Try to find the glyph whose character position
13986 corresponds to point. If that's not possible, find 2 glyphs
13987 whose character positions are the closest to point, one before
13988 point, the other after it. */
13989 if (!row->reversed_p)
13990 while (/* not marched to end of glyph row */
13991 glyph < end
13992 /* glyph was not inserted by redisplay for internal purposes */
13993 && !INTEGERP (glyph->object))
13995 if (BUFFERP (glyph->object))
13997 ptrdiff_t dpos = glyph->charpos - pt_old;
13999 if (glyph->charpos > bpos_max)
14000 bpos_max = glyph->charpos;
14001 if (glyph->charpos < bpos_min)
14002 bpos_min = glyph->charpos;
14003 if (!glyph->avoid_cursor_p)
14005 /* If we hit point, we've found the glyph on which to
14006 display the cursor. */
14007 if (dpos == 0)
14009 match_with_avoid_cursor = 0;
14010 break;
14012 /* See if we've found a better approximation to
14013 POS_BEFORE or to POS_AFTER. */
14014 if (0 > dpos && dpos > pos_before - pt_old)
14016 pos_before = glyph->charpos;
14017 glyph_before = glyph;
14019 else if (0 < dpos && dpos < pos_after - pt_old)
14021 pos_after = glyph->charpos;
14022 glyph_after = glyph;
14025 else if (dpos == 0)
14026 match_with_avoid_cursor = 1;
14028 else if (STRINGP (glyph->object))
14030 Lisp_Object chprop;
14031 ptrdiff_t glyph_pos = glyph->charpos;
14033 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14034 glyph->object);
14035 if (!NILP (chprop))
14037 /* If the string came from a `display' text property,
14038 look up the buffer position of that property and
14039 use that position to update bpos_max, as if we
14040 actually saw such a position in one of the row's
14041 glyphs. This helps with supporting integer values
14042 of `cursor' property on the display string in
14043 situations where most or all of the row's buffer
14044 text is completely covered by display properties,
14045 so that no glyph with valid buffer positions is
14046 ever seen in the row. */
14047 ptrdiff_t prop_pos =
14048 string_buffer_position_lim (glyph->object, pos_before,
14049 pos_after, 0);
14051 if (prop_pos >= pos_before)
14052 bpos_max = prop_pos - 1;
14054 if (INTEGERP (chprop))
14056 bpos_covered = bpos_max + XINT (chprop);
14057 /* If the `cursor' property covers buffer positions up
14058 to and including point, we should display cursor on
14059 this glyph. Note that, if a `cursor' property on one
14060 of the string's characters has an integer value, we
14061 will break out of the loop below _before_ we get to
14062 the position match above. IOW, integer values of
14063 the `cursor' property override the "exact match for
14064 point" strategy of positioning the cursor. */
14065 /* Implementation note: bpos_max == pt_old when, e.g.,
14066 we are in an empty line, where bpos_max is set to
14067 MATRIX_ROW_START_CHARPOS, see above. */
14068 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14070 cursor = glyph;
14071 break;
14075 string_seen = 1;
14077 x += glyph->pixel_width;
14078 ++glyph;
14080 else if (glyph > end) /* row is reversed */
14081 while (!INTEGERP (glyph->object))
14083 if (BUFFERP (glyph->object))
14085 ptrdiff_t dpos = glyph->charpos - pt_old;
14087 if (glyph->charpos > bpos_max)
14088 bpos_max = glyph->charpos;
14089 if (glyph->charpos < bpos_min)
14090 bpos_min = glyph->charpos;
14091 if (!glyph->avoid_cursor_p)
14093 if (dpos == 0)
14095 match_with_avoid_cursor = 0;
14096 break;
14098 if (0 > dpos && dpos > pos_before - pt_old)
14100 pos_before = glyph->charpos;
14101 glyph_before = glyph;
14103 else if (0 < dpos && dpos < pos_after - pt_old)
14105 pos_after = glyph->charpos;
14106 glyph_after = glyph;
14109 else if (dpos == 0)
14110 match_with_avoid_cursor = 1;
14112 else if (STRINGP (glyph->object))
14114 Lisp_Object chprop;
14115 ptrdiff_t glyph_pos = glyph->charpos;
14117 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14118 glyph->object);
14119 if (!NILP (chprop))
14121 ptrdiff_t prop_pos =
14122 string_buffer_position_lim (glyph->object, pos_before,
14123 pos_after, 0);
14125 if (prop_pos >= pos_before)
14126 bpos_max = prop_pos - 1;
14128 if (INTEGERP (chprop))
14130 bpos_covered = bpos_max + XINT (chprop);
14131 /* If the `cursor' property covers buffer positions up
14132 to and including point, we should display cursor on
14133 this glyph. */
14134 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14136 cursor = glyph;
14137 break;
14140 string_seen = 1;
14142 --glyph;
14143 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14145 x--; /* can't use any pixel_width */
14146 break;
14148 x -= glyph->pixel_width;
14151 /* Step 2: If we didn't find an exact match for point, we need to
14152 look for a proper place to put the cursor among glyphs between
14153 GLYPH_BEFORE and GLYPH_AFTER. */
14154 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14155 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14156 && bpos_covered < pt_old)
14158 /* An empty line has a single glyph whose OBJECT is zero and
14159 whose CHARPOS is the position of a newline on that line.
14160 Note that on a TTY, there are more glyphs after that, which
14161 were produced by extend_face_to_end_of_line, but their
14162 CHARPOS is zero or negative. */
14163 int empty_line_p =
14164 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14165 && INTEGERP (glyph->object) && glyph->charpos > 0;
14167 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14169 ptrdiff_t ellipsis_pos;
14171 /* Scan back over the ellipsis glyphs. */
14172 if (!row->reversed_p)
14174 ellipsis_pos = (glyph - 1)->charpos;
14175 while (glyph > row->glyphs[TEXT_AREA]
14176 && (glyph - 1)->charpos == ellipsis_pos)
14177 glyph--, x -= glyph->pixel_width;
14178 /* That loop always goes one position too far, including
14179 the glyph before the ellipsis. So scan forward over
14180 that one. */
14181 x += glyph->pixel_width;
14182 glyph++;
14184 else /* row is reversed */
14186 ellipsis_pos = (glyph + 1)->charpos;
14187 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14188 && (glyph + 1)->charpos == ellipsis_pos)
14189 glyph++, x += glyph->pixel_width;
14190 x -= glyph->pixel_width;
14191 glyph--;
14194 else if (match_with_avoid_cursor)
14196 cursor = glyph_after;
14197 x = -1;
14199 else if (string_seen)
14201 int incr = row->reversed_p ? -1 : +1;
14203 /* Need to find the glyph that came out of a string which is
14204 present at point. That glyph is somewhere between
14205 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14206 positioned between POS_BEFORE and POS_AFTER in the
14207 buffer. */
14208 struct glyph *start, *stop;
14209 ptrdiff_t pos = pos_before;
14211 x = -1;
14213 /* If the row ends in a newline from a display string,
14214 reordering could have moved the glyphs belonging to the
14215 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14216 in this case we extend the search to the last glyph in
14217 the row that was not inserted by redisplay. */
14218 if (row->ends_in_newline_from_string_p)
14220 glyph_after = end;
14221 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14224 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14225 correspond to POS_BEFORE and POS_AFTER, respectively. We
14226 need START and STOP in the order that corresponds to the
14227 row's direction as given by its reversed_p flag. If the
14228 directionality of characters between POS_BEFORE and
14229 POS_AFTER is the opposite of the row's base direction,
14230 these characters will have been reordered for display,
14231 and we need to reverse START and STOP. */
14232 if (!row->reversed_p)
14234 start = min (glyph_before, glyph_after);
14235 stop = max (glyph_before, glyph_after);
14237 else
14239 start = max (glyph_before, glyph_after);
14240 stop = min (glyph_before, glyph_after);
14242 for (glyph = start + incr;
14243 row->reversed_p ? glyph > stop : glyph < stop; )
14246 /* Any glyphs that come from the buffer are here because
14247 of bidi reordering. Skip them, and only pay
14248 attention to glyphs that came from some string. */
14249 if (STRINGP (glyph->object))
14251 Lisp_Object str;
14252 ptrdiff_t tem;
14253 /* If the display property covers the newline, we
14254 need to search for it one position farther. */
14255 ptrdiff_t lim = pos_after
14256 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14258 string_from_text_prop = 0;
14259 str = glyph->object;
14260 tem = string_buffer_position_lim (str, pos, lim, 0);
14261 if (tem == 0 /* from overlay */
14262 || pos <= tem)
14264 /* If the string from which this glyph came is
14265 found in the buffer at point, or at position
14266 that is closer to point than pos_after, then
14267 we've found the glyph we've been looking for.
14268 If it comes from an overlay (tem == 0), and
14269 it has the `cursor' property on one of its
14270 glyphs, record that glyph as a candidate for
14271 displaying the cursor. (As in the
14272 unidirectional version, we will display the
14273 cursor on the last candidate we find.) */
14274 if (tem == 0
14275 || tem == pt_old
14276 || (tem - pt_old > 0 && tem < pos_after))
14278 /* The glyphs from this string could have
14279 been reordered. Find the one with the
14280 smallest string position. Or there could
14281 be a character in the string with the
14282 `cursor' property, which means display
14283 cursor on that character's glyph. */
14284 ptrdiff_t strpos = glyph->charpos;
14286 if (tem)
14288 cursor = glyph;
14289 string_from_text_prop = 1;
14291 for ( ;
14292 (row->reversed_p ? glyph > stop : glyph < stop)
14293 && EQ (glyph->object, str);
14294 glyph += incr)
14296 Lisp_Object cprop;
14297 ptrdiff_t gpos = glyph->charpos;
14299 cprop = Fget_char_property (make_number (gpos),
14300 Qcursor,
14301 glyph->object);
14302 if (!NILP (cprop))
14304 cursor = glyph;
14305 break;
14307 if (tem && glyph->charpos < strpos)
14309 strpos = glyph->charpos;
14310 cursor = glyph;
14314 if (tem == pt_old
14315 || (tem - pt_old > 0 && tem < pos_after))
14316 goto compute_x;
14318 if (tem)
14319 pos = tem + 1; /* don't find previous instances */
14321 /* This string is not what we want; skip all of the
14322 glyphs that came from it. */
14323 while ((row->reversed_p ? glyph > stop : glyph < stop)
14324 && EQ (glyph->object, str))
14325 glyph += incr;
14327 else
14328 glyph += incr;
14331 /* If we reached the end of the line, and END was from a string,
14332 the cursor is not on this line. */
14333 if (cursor == NULL
14334 && (row->reversed_p ? glyph <= end : glyph >= end)
14335 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14336 && STRINGP (end->object)
14337 && row->continued_p)
14338 return 0;
14340 /* A truncated row may not include PT among its character positions.
14341 Setting the cursor inside the scroll margin will trigger
14342 recalculation of hscroll in hscroll_window_tree. But if a
14343 display string covers point, defer to the string-handling
14344 code below to figure this out. */
14345 else if (row->truncated_on_left_p && pt_old < bpos_min)
14347 cursor = glyph_before;
14348 x = -1;
14350 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14351 /* Zero-width characters produce no glyphs. */
14352 || (!empty_line_p
14353 && (row->reversed_p
14354 ? glyph_after > glyphs_end
14355 : glyph_after < glyphs_end)))
14357 cursor = glyph_after;
14358 x = -1;
14362 compute_x:
14363 if (cursor != NULL)
14364 glyph = cursor;
14365 else if (glyph == glyphs_end
14366 && pos_before == pos_after
14367 && STRINGP ((row->reversed_p
14368 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14369 : row->glyphs[TEXT_AREA])->object))
14371 /* If all the glyphs of this row came from strings, put the
14372 cursor on the first glyph of the row. This avoids having the
14373 cursor outside of the text area in this very rare and hard
14374 use case. */
14375 glyph =
14376 row->reversed_p
14377 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14378 : row->glyphs[TEXT_AREA];
14380 if (x < 0)
14382 struct glyph *g;
14384 /* Need to compute x that corresponds to GLYPH. */
14385 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14387 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14388 abort ();
14389 x += g->pixel_width;
14393 /* ROW could be part of a continued line, which, under bidi
14394 reordering, might have other rows whose start and end charpos
14395 occlude point. Only set w->cursor if we found a better
14396 approximation to the cursor position than we have from previously
14397 examined candidate rows belonging to the same continued line. */
14398 if (/* we already have a candidate row */
14399 w->cursor.vpos >= 0
14400 /* that candidate is not the row we are processing */
14401 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14402 /* Make sure cursor.vpos specifies a row whose start and end
14403 charpos occlude point, and it is valid candidate for being a
14404 cursor-row. This is because some callers of this function
14405 leave cursor.vpos at the row where the cursor was displayed
14406 during the last redisplay cycle. */
14407 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14408 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14409 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14411 struct glyph *g1 =
14412 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14414 /* Don't consider glyphs that are outside TEXT_AREA. */
14415 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14416 return 0;
14417 /* Keep the candidate whose buffer position is the closest to
14418 point or has the `cursor' property. */
14419 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14420 w->cursor.hpos >= 0
14421 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14422 && ((BUFFERP (g1->object)
14423 && (g1->charpos == pt_old /* an exact match always wins */
14424 || (BUFFERP (glyph->object)
14425 && eabs (g1->charpos - pt_old)
14426 < eabs (glyph->charpos - pt_old))))
14427 /* previous candidate is a glyph from a string that has
14428 a non-nil `cursor' property */
14429 || (STRINGP (g1->object)
14430 && (!NILP (Fget_char_property (make_number (g1->charpos),
14431 Qcursor, g1->object))
14432 /* previous candidate is from the same display
14433 string as this one, and the display string
14434 came from a text property */
14435 || (EQ (g1->object, glyph->object)
14436 && string_from_text_prop)
14437 /* this candidate is from newline and its
14438 position is not an exact match */
14439 || (INTEGERP (glyph->object)
14440 && glyph->charpos != pt_old)))))
14441 return 0;
14442 /* If this candidate gives an exact match, use that. */
14443 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14444 /* If this candidate is a glyph created for the
14445 terminating newline of a line, and point is on that
14446 newline, it wins because it's an exact match. */
14447 || (!row->continued_p
14448 && INTEGERP (glyph->object)
14449 && glyph->charpos == 0
14450 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14451 /* Otherwise, keep the candidate that comes from a row
14452 spanning less buffer positions. This may win when one or
14453 both candidate positions are on glyphs that came from
14454 display strings, for which we cannot compare buffer
14455 positions. */
14456 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14457 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14458 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14459 return 0;
14461 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14462 w->cursor.x = x;
14463 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14464 w->cursor.y = row->y + dy;
14466 if (w == XWINDOW (selected_window))
14468 if (!row->continued_p
14469 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14470 && row->x == 0)
14472 this_line_buffer = XBUFFER (w->buffer);
14474 CHARPOS (this_line_start_pos)
14475 = MATRIX_ROW_START_CHARPOS (row) + delta;
14476 BYTEPOS (this_line_start_pos)
14477 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14479 CHARPOS (this_line_end_pos)
14480 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14481 BYTEPOS (this_line_end_pos)
14482 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14484 this_line_y = w->cursor.y;
14485 this_line_pixel_height = row->height;
14486 this_line_vpos = w->cursor.vpos;
14487 this_line_start_x = row->x;
14489 else
14490 CHARPOS (this_line_start_pos) = 0;
14493 return 1;
14497 /* Run window scroll functions, if any, for WINDOW with new window
14498 start STARTP. Sets the window start of WINDOW to that position.
14500 We assume that the window's buffer is really current. */
14502 static inline struct text_pos
14503 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14505 struct window *w = XWINDOW (window);
14506 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14508 if (current_buffer != XBUFFER (w->buffer))
14509 abort ();
14511 if (!NILP (Vwindow_scroll_functions))
14513 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14514 make_number (CHARPOS (startp)));
14515 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14516 /* In case the hook functions switch buffers. */
14517 if (current_buffer != XBUFFER (w->buffer))
14518 set_buffer_internal_1 (XBUFFER (w->buffer));
14521 return startp;
14525 /* Make sure the line containing the cursor is fully visible.
14526 A value of 1 means there is nothing to be done.
14527 (Either the line is fully visible, or it cannot be made so,
14528 or we cannot tell.)
14530 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14531 is higher than window.
14533 A value of 0 means the caller should do scrolling
14534 as if point had gone off the screen. */
14536 static int
14537 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14539 struct glyph_matrix *matrix;
14540 struct glyph_row *row;
14541 int window_height;
14543 if (!make_cursor_line_fully_visible_p)
14544 return 1;
14546 /* It's not always possible to find the cursor, e.g, when a window
14547 is full of overlay strings. Don't do anything in that case. */
14548 if (w->cursor.vpos < 0)
14549 return 1;
14551 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14552 row = MATRIX_ROW (matrix, w->cursor.vpos);
14554 /* If the cursor row is not partially visible, there's nothing to do. */
14555 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14556 return 1;
14558 /* If the row the cursor is in is taller than the window's height,
14559 it's not clear what to do, so do nothing. */
14560 window_height = window_box_height (w);
14561 if (row->height >= window_height)
14563 if (!force_p || MINI_WINDOW_P (w)
14564 || w->vscroll || w->cursor.vpos == 0)
14565 return 1;
14567 return 0;
14571 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14572 non-zero means only WINDOW is redisplayed in redisplay_internal.
14573 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14574 in redisplay_window to bring a partially visible line into view in
14575 the case that only the cursor has moved.
14577 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14578 last screen line's vertical height extends past the end of the screen.
14580 Value is
14582 1 if scrolling succeeded
14584 0 if scrolling didn't find point.
14586 -1 if new fonts have been loaded so that we must interrupt
14587 redisplay, adjust glyph matrices, and try again. */
14589 enum
14591 SCROLLING_SUCCESS,
14592 SCROLLING_FAILED,
14593 SCROLLING_NEED_LARGER_MATRICES
14596 /* If scroll-conservatively is more than this, never recenter.
14598 If you change this, don't forget to update the doc string of
14599 `scroll-conservatively' and the Emacs manual. */
14600 #define SCROLL_LIMIT 100
14602 static int
14603 try_scrolling (Lisp_Object window, int just_this_one_p,
14604 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14605 int temp_scroll_step, int last_line_misfit)
14607 struct window *w = XWINDOW (window);
14608 struct frame *f = XFRAME (w->frame);
14609 struct text_pos pos, startp;
14610 struct it it;
14611 int this_scroll_margin, scroll_max, rc, height;
14612 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14613 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14614 Lisp_Object aggressive;
14615 /* We will never try scrolling more than this number of lines. */
14616 int scroll_limit = SCROLL_LIMIT;
14618 #ifdef GLYPH_DEBUG
14619 debug_method_add (w, "try_scrolling");
14620 #endif
14622 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14624 /* Compute scroll margin height in pixels. We scroll when point is
14625 within this distance from the top or bottom of the window. */
14626 if (scroll_margin > 0)
14627 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14628 * FRAME_LINE_HEIGHT (f);
14629 else
14630 this_scroll_margin = 0;
14632 /* Force arg_scroll_conservatively to have a reasonable value, to
14633 avoid scrolling too far away with slow move_it_* functions. Note
14634 that the user can supply scroll-conservatively equal to
14635 `most-positive-fixnum', which can be larger than INT_MAX. */
14636 if (arg_scroll_conservatively > scroll_limit)
14638 arg_scroll_conservatively = scroll_limit + 1;
14639 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14641 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14642 /* Compute how much we should try to scroll maximally to bring
14643 point into view. */
14644 scroll_max = (max (scroll_step,
14645 max (arg_scroll_conservatively, temp_scroll_step))
14646 * FRAME_LINE_HEIGHT (f));
14647 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14648 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14649 /* We're trying to scroll because of aggressive scrolling but no
14650 scroll_step is set. Choose an arbitrary one. */
14651 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14652 else
14653 scroll_max = 0;
14655 too_near_end:
14657 /* Decide whether to scroll down. */
14658 if (PT > CHARPOS (startp))
14660 int scroll_margin_y;
14662 /* Compute the pixel ypos of the scroll margin, then move IT to
14663 either that ypos or PT, whichever comes first. */
14664 start_display (&it, w, startp);
14665 scroll_margin_y = it.last_visible_y - this_scroll_margin
14666 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14667 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14668 (MOVE_TO_POS | MOVE_TO_Y));
14670 if (PT > CHARPOS (it.current.pos))
14672 int y0 = line_bottom_y (&it);
14673 /* Compute how many pixels below window bottom to stop searching
14674 for PT. This avoids costly search for PT that is far away if
14675 the user limited scrolling by a small number of lines, but
14676 always finds PT if scroll_conservatively is set to a large
14677 number, such as most-positive-fixnum. */
14678 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14679 int y_to_move = it.last_visible_y + slack;
14681 /* Compute the distance from the scroll margin to PT or to
14682 the scroll limit, whichever comes first. This should
14683 include the height of the cursor line, to make that line
14684 fully visible. */
14685 move_it_to (&it, PT, -1, y_to_move,
14686 -1, MOVE_TO_POS | MOVE_TO_Y);
14687 dy = line_bottom_y (&it) - y0;
14689 if (dy > scroll_max)
14690 return SCROLLING_FAILED;
14692 if (dy > 0)
14693 scroll_down_p = 1;
14697 if (scroll_down_p)
14699 /* Point is in or below the bottom scroll margin, so move the
14700 window start down. If scrolling conservatively, move it just
14701 enough down to make point visible. If scroll_step is set,
14702 move it down by scroll_step. */
14703 if (arg_scroll_conservatively)
14704 amount_to_scroll
14705 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14706 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14707 else if (scroll_step || temp_scroll_step)
14708 amount_to_scroll = scroll_max;
14709 else
14711 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14712 height = WINDOW_BOX_TEXT_HEIGHT (w);
14713 if (NUMBERP (aggressive))
14715 double float_amount = XFLOATINT (aggressive) * height;
14716 amount_to_scroll = float_amount;
14717 if (amount_to_scroll == 0 && float_amount > 0)
14718 amount_to_scroll = 1;
14719 /* Don't let point enter the scroll margin near top of
14720 the window. */
14721 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14722 amount_to_scroll = height - 2*this_scroll_margin + dy;
14726 if (amount_to_scroll <= 0)
14727 return SCROLLING_FAILED;
14729 start_display (&it, w, startp);
14730 if (arg_scroll_conservatively <= scroll_limit)
14731 move_it_vertically (&it, amount_to_scroll);
14732 else
14734 /* Extra precision for users who set scroll-conservatively
14735 to a large number: make sure the amount we scroll
14736 the window start is never less than amount_to_scroll,
14737 which was computed as distance from window bottom to
14738 point. This matters when lines at window top and lines
14739 below window bottom have different height. */
14740 struct it it1;
14741 void *it1data = NULL;
14742 /* We use a temporary it1 because line_bottom_y can modify
14743 its argument, if it moves one line down; see there. */
14744 int start_y;
14746 SAVE_IT (it1, it, it1data);
14747 start_y = line_bottom_y (&it1);
14748 do {
14749 RESTORE_IT (&it, &it, it1data);
14750 move_it_by_lines (&it, 1);
14751 SAVE_IT (it1, it, it1data);
14752 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14755 /* If STARTP is unchanged, move it down another screen line. */
14756 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14757 move_it_by_lines (&it, 1);
14758 startp = it.current.pos;
14760 else
14762 struct text_pos scroll_margin_pos = startp;
14764 /* See if point is inside the scroll margin at the top of the
14765 window. */
14766 if (this_scroll_margin)
14768 start_display (&it, w, startp);
14769 move_it_vertically (&it, this_scroll_margin);
14770 scroll_margin_pos = it.current.pos;
14773 if (PT < CHARPOS (scroll_margin_pos))
14775 /* Point is in the scroll margin at the top of the window or
14776 above what is displayed in the window. */
14777 int y0, y_to_move;
14779 /* Compute the vertical distance from PT to the scroll
14780 margin position. Move as far as scroll_max allows, or
14781 one screenful, or 10 screen lines, whichever is largest.
14782 Give up if distance is greater than scroll_max. */
14783 SET_TEXT_POS (pos, PT, PT_BYTE);
14784 start_display (&it, w, pos);
14785 y0 = it.current_y;
14786 y_to_move = max (it.last_visible_y,
14787 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14788 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14789 y_to_move, -1,
14790 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14791 dy = it.current_y - y0;
14792 if (dy > scroll_max)
14793 return SCROLLING_FAILED;
14795 /* Compute new window start. */
14796 start_display (&it, w, startp);
14798 if (arg_scroll_conservatively)
14799 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14800 max (scroll_step, temp_scroll_step));
14801 else if (scroll_step || temp_scroll_step)
14802 amount_to_scroll = scroll_max;
14803 else
14805 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14806 height = WINDOW_BOX_TEXT_HEIGHT (w);
14807 if (NUMBERP (aggressive))
14809 double float_amount = XFLOATINT (aggressive) * height;
14810 amount_to_scroll = float_amount;
14811 if (amount_to_scroll == 0 && float_amount > 0)
14812 amount_to_scroll = 1;
14813 amount_to_scroll -=
14814 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14815 /* Don't let point enter the scroll margin near
14816 bottom of the window. */
14817 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14818 amount_to_scroll = height - 2*this_scroll_margin + dy;
14822 if (amount_to_scroll <= 0)
14823 return SCROLLING_FAILED;
14825 move_it_vertically_backward (&it, amount_to_scroll);
14826 startp = it.current.pos;
14830 /* Run window scroll functions. */
14831 startp = run_window_scroll_functions (window, startp);
14833 /* Display the window. Give up if new fonts are loaded, or if point
14834 doesn't appear. */
14835 if (!try_window (window, startp, 0))
14836 rc = SCROLLING_NEED_LARGER_MATRICES;
14837 else if (w->cursor.vpos < 0)
14839 clear_glyph_matrix (w->desired_matrix);
14840 rc = SCROLLING_FAILED;
14842 else
14844 /* Maybe forget recorded base line for line number display. */
14845 if (!just_this_one_p
14846 || current_buffer->clip_changed
14847 || BEG_UNCHANGED < CHARPOS (startp))
14848 WSET (w, base_line_number, Qnil);
14850 /* If cursor ends up on a partially visible line,
14851 treat that as being off the bottom of the screen. */
14852 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14853 /* It's possible that the cursor is on the first line of the
14854 buffer, which is partially obscured due to a vscroll
14855 (Bug#7537). In that case, avoid looping forever . */
14856 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14858 clear_glyph_matrix (w->desired_matrix);
14859 ++extra_scroll_margin_lines;
14860 goto too_near_end;
14862 rc = SCROLLING_SUCCESS;
14865 return rc;
14869 /* Compute a suitable window start for window W if display of W starts
14870 on a continuation line. Value is non-zero if a new window start
14871 was computed.
14873 The new window start will be computed, based on W's width, starting
14874 from the start of the continued line. It is the start of the
14875 screen line with the minimum distance from the old start W->start. */
14877 static int
14878 compute_window_start_on_continuation_line (struct window *w)
14880 struct text_pos pos, start_pos;
14881 int window_start_changed_p = 0;
14883 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14885 /* If window start is on a continuation line... Window start may be
14886 < BEGV in case there's invisible text at the start of the
14887 buffer (M-x rmail, for example). */
14888 if (CHARPOS (start_pos) > BEGV
14889 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14891 struct it it;
14892 struct glyph_row *row;
14894 /* Handle the case that the window start is out of range. */
14895 if (CHARPOS (start_pos) < BEGV)
14896 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14897 else if (CHARPOS (start_pos) > ZV)
14898 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14900 /* Find the start of the continued line. This should be fast
14901 because scan_buffer is fast (newline cache). */
14902 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14903 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14904 row, DEFAULT_FACE_ID);
14905 reseat_at_previous_visible_line_start (&it);
14907 /* If the line start is "too far" away from the window start,
14908 say it takes too much time to compute a new window start. */
14909 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14910 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14912 int min_distance, distance;
14914 /* Move forward by display lines to find the new window
14915 start. If window width was enlarged, the new start can
14916 be expected to be > the old start. If window width was
14917 decreased, the new window start will be < the old start.
14918 So, we're looking for the display line start with the
14919 minimum distance from the old window start. */
14920 pos = it.current.pos;
14921 min_distance = INFINITY;
14922 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14923 distance < min_distance)
14925 min_distance = distance;
14926 pos = it.current.pos;
14927 move_it_by_lines (&it, 1);
14930 /* Set the window start there. */
14931 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14932 window_start_changed_p = 1;
14936 return window_start_changed_p;
14940 /* Try cursor movement in case text has not changed in window WINDOW,
14941 with window start STARTP. Value is
14943 CURSOR_MOVEMENT_SUCCESS if successful
14945 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14947 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14948 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14949 we want to scroll as if scroll-step were set to 1. See the code.
14951 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14952 which case we have to abort this redisplay, and adjust matrices
14953 first. */
14955 enum
14957 CURSOR_MOVEMENT_SUCCESS,
14958 CURSOR_MOVEMENT_CANNOT_BE_USED,
14959 CURSOR_MOVEMENT_MUST_SCROLL,
14960 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14963 static int
14964 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14966 struct window *w = XWINDOW (window);
14967 struct frame *f = XFRAME (w->frame);
14968 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14970 #ifdef GLYPH_DEBUG
14971 if (inhibit_try_cursor_movement)
14972 return rc;
14973 #endif
14975 /* Previously, there was a check for Lisp integer in the
14976 if-statement below. Now, this field is converted to
14977 ptrdiff_t, thus zero means invalid position in a buffer. */
14978 eassert (w->last_point > 0);
14980 /* Handle case where text has not changed, only point, and it has
14981 not moved off the frame. */
14982 if (/* Point may be in this window. */
14983 PT >= CHARPOS (startp)
14984 /* Selective display hasn't changed. */
14985 && !current_buffer->clip_changed
14986 /* Function force-mode-line-update is used to force a thorough
14987 redisplay. It sets either windows_or_buffers_changed or
14988 update_mode_lines. So don't take a shortcut here for these
14989 cases. */
14990 && !update_mode_lines
14991 && !windows_or_buffers_changed
14992 && !cursor_type_changed
14993 /* Can't use this case if highlighting a region. When a
14994 region exists, cursor movement has to do more than just
14995 set the cursor. */
14996 && !(!NILP (Vtransient_mark_mode)
14997 && !NILP (BVAR (current_buffer, mark_active)))
14998 && NILP (w->region_showing)
14999 && NILP (Vshow_trailing_whitespace)
15000 /* This code is not used for mini-buffer for the sake of the case
15001 of redisplaying to replace an echo area message; since in
15002 that case the mini-buffer contents per se are usually
15003 unchanged. This code is of no real use in the mini-buffer
15004 since the handling of this_line_start_pos, etc., in redisplay
15005 handles the same cases. */
15006 && !EQ (window, minibuf_window)
15007 /* When splitting windows or for new windows, it happens that
15008 redisplay is called with a nil window_end_vpos or one being
15009 larger than the window. This should really be fixed in
15010 window.c. I don't have this on my list, now, so we do
15011 approximately the same as the old redisplay code. --gerd. */
15012 && INTEGERP (w->window_end_vpos)
15013 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15014 && (FRAME_WINDOW_P (f)
15015 || !overlay_arrow_in_current_buffer_p ()))
15017 int this_scroll_margin, top_scroll_margin;
15018 struct glyph_row *row = NULL;
15020 #ifdef GLYPH_DEBUG
15021 debug_method_add (w, "cursor movement");
15022 #endif
15024 /* Scroll if point within this distance from the top or bottom
15025 of the window. This is a pixel value. */
15026 if (scroll_margin > 0)
15028 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15029 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15031 else
15032 this_scroll_margin = 0;
15034 top_scroll_margin = this_scroll_margin;
15035 if (WINDOW_WANTS_HEADER_LINE_P (w))
15036 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15038 /* Start with the row the cursor was displayed during the last
15039 not paused redisplay. Give up if that row is not valid. */
15040 if (w->last_cursor.vpos < 0
15041 || w->last_cursor.vpos >= w->current_matrix->nrows)
15042 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15043 else
15045 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15046 if (row->mode_line_p)
15047 ++row;
15048 if (!row->enabled_p)
15049 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15052 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15054 int scroll_p = 0, must_scroll = 0;
15055 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15057 if (PT > w->last_point)
15059 /* Point has moved forward. */
15060 while (MATRIX_ROW_END_CHARPOS (row) < PT
15061 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15063 eassert (row->enabled_p);
15064 ++row;
15067 /* If the end position of a row equals the start
15068 position of the next row, and PT is at that position,
15069 we would rather display cursor in the next line. */
15070 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15071 && MATRIX_ROW_END_CHARPOS (row) == PT
15072 && row < w->current_matrix->rows
15073 + w->current_matrix->nrows - 1
15074 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15075 && !cursor_row_p (row))
15076 ++row;
15078 /* If within the scroll margin, scroll. Note that
15079 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15080 the next line would be drawn, and that
15081 this_scroll_margin can be zero. */
15082 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15083 || PT > MATRIX_ROW_END_CHARPOS (row)
15084 /* Line is completely visible last line in window
15085 and PT is to be set in the next line. */
15086 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15087 && PT == MATRIX_ROW_END_CHARPOS (row)
15088 && !row->ends_at_zv_p
15089 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15090 scroll_p = 1;
15092 else if (PT < w->last_point)
15094 /* Cursor has to be moved backward. Note that PT >=
15095 CHARPOS (startp) because of the outer if-statement. */
15096 while (!row->mode_line_p
15097 && (MATRIX_ROW_START_CHARPOS (row) > PT
15098 || (MATRIX_ROW_START_CHARPOS (row) == PT
15099 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15100 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15101 row > w->current_matrix->rows
15102 && (row-1)->ends_in_newline_from_string_p))))
15103 && (row->y > top_scroll_margin
15104 || CHARPOS (startp) == BEGV))
15106 eassert (row->enabled_p);
15107 --row;
15110 /* Consider the following case: Window starts at BEGV,
15111 there is invisible, intangible text at BEGV, so that
15112 display starts at some point START > BEGV. It can
15113 happen that we are called with PT somewhere between
15114 BEGV and START. Try to handle that case. */
15115 if (row < w->current_matrix->rows
15116 || row->mode_line_p)
15118 row = w->current_matrix->rows;
15119 if (row->mode_line_p)
15120 ++row;
15123 /* Due to newlines in overlay strings, we may have to
15124 skip forward over overlay strings. */
15125 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15126 && MATRIX_ROW_END_CHARPOS (row) == PT
15127 && !cursor_row_p (row))
15128 ++row;
15130 /* If within the scroll margin, scroll. */
15131 if (row->y < top_scroll_margin
15132 && CHARPOS (startp) != BEGV)
15133 scroll_p = 1;
15135 else
15137 /* Cursor did not move. So don't scroll even if cursor line
15138 is partially visible, as it was so before. */
15139 rc = CURSOR_MOVEMENT_SUCCESS;
15142 if (PT < MATRIX_ROW_START_CHARPOS (row)
15143 || PT > MATRIX_ROW_END_CHARPOS (row))
15145 /* if PT is not in the glyph row, give up. */
15146 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15147 must_scroll = 1;
15149 else if (rc != CURSOR_MOVEMENT_SUCCESS
15150 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15152 struct glyph_row *row1;
15154 /* If rows are bidi-reordered and point moved, back up
15155 until we find a row that does not belong to a
15156 continuation line. This is because we must consider
15157 all rows of a continued line as candidates for the
15158 new cursor positioning, since row start and end
15159 positions change non-linearly with vertical position
15160 in such rows. */
15161 /* FIXME: Revisit this when glyph ``spilling'' in
15162 continuation lines' rows is implemented for
15163 bidi-reordered rows. */
15164 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15165 MATRIX_ROW_CONTINUATION_LINE_P (row);
15166 --row)
15168 /* If we hit the beginning of the displayed portion
15169 without finding the first row of a continued
15170 line, give up. */
15171 if (row <= row1)
15173 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15174 break;
15176 eassert (row->enabled_p);
15179 if (must_scroll)
15181 else if (rc != CURSOR_MOVEMENT_SUCCESS
15182 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15183 /* Make sure this isn't a header line by any chance, since
15184 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15185 && !row->mode_line_p
15186 && make_cursor_line_fully_visible_p)
15188 if (PT == MATRIX_ROW_END_CHARPOS (row)
15189 && !row->ends_at_zv_p
15190 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15191 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15192 else if (row->height > window_box_height (w))
15194 /* If we end up in a partially visible line, let's
15195 make it fully visible, except when it's taller
15196 than the window, in which case we can't do much
15197 about it. */
15198 *scroll_step = 1;
15199 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15201 else
15203 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15204 if (!cursor_row_fully_visible_p (w, 0, 1))
15205 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15206 else
15207 rc = CURSOR_MOVEMENT_SUCCESS;
15210 else if (scroll_p)
15211 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15212 else if (rc != CURSOR_MOVEMENT_SUCCESS
15213 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15215 /* With bidi-reordered rows, there could be more than
15216 one candidate row whose start and end positions
15217 occlude point. We need to let set_cursor_from_row
15218 find the best candidate. */
15219 /* FIXME: Revisit this when glyph ``spilling'' in
15220 continuation lines' rows is implemented for
15221 bidi-reordered rows. */
15222 int rv = 0;
15226 int at_zv_p = 0, exact_match_p = 0;
15228 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15229 && PT <= MATRIX_ROW_END_CHARPOS (row)
15230 && cursor_row_p (row))
15231 rv |= set_cursor_from_row (w, row, w->current_matrix,
15232 0, 0, 0, 0);
15233 /* As soon as we've found the exact match for point,
15234 or the first suitable row whose ends_at_zv_p flag
15235 is set, we are done. */
15236 at_zv_p =
15237 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15238 if (rv && !at_zv_p
15239 && w->cursor.hpos >= 0
15240 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15241 w->cursor.vpos))
15243 struct glyph_row *candidate =
15244 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15245 struct glyph *g =
15246 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15247 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15249 exact_match_p =
15250 (BUFFERP (g->object) && g->charpos == PT)
15251 || (INTEGERP (g->object)
15252 && (g->charpos == PT
15253 || (g->charpos == 0 && endpos - 1 == PT)));
15255 if (rv && (at_zv_p || exact_match_p))
15257 rc = CURSOR_MOVEMENT_SUCCESS;
15258 break;
15260 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15261 break;
15262 ++row;
15264 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15265 || row->continued_p)
15266 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15267 || (MATRIX_ROW_START_CHARPOS (row) == PT
15268 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15269 /* If we didn't find any candidate rows, or exited the
15270 loop before all the candidates were examined, signal
15271 to the caller that this method failed. */
15272 if (rc != CURSOR_MOVEMENT_SUCCESS
15273 && !(rv
15274 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15275 && !row->continued_p))
15276 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15277 else if (rv)
15278 rc = CURSOR_MOVEMENT_SUCCESS;
15280 else
15284 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15286 rc = CURSOR_MOVEMENT_SUCCESS;
15287 break;
15289 ++row;
15291 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15292 && MATRIX_ROW_START_CHARPOS (row) == PT
15293 && cursor_row_p (row));
15298 return rc;
15301 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15302 static
15303 #endif
15304 void
15305 set_vertical_scroll_bar (struct window *w)
15307 ptrdiff_t start, end, whole;
15309 /* Calculate the start and end positions for the current window.
15310 At some point, it would be nice to choose between scrollbars
15311 which reflect the whole buffer size, with special markers
15312 indicating narrowing, and scrollbars which reflect only the
15313 visible region.
15315 Note that mini-buffers sometimes aren't displaying any text. */
15316 if (!MINI_WINDOW_P (w)
15317 || (w == XWINDOW (minibuf_window)
15318 && NILP (echo_area_buffer[0])))
15320 struct buffer *buf = XBUFFER (w->buffer);
15321 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15322 start = marker_position (w->start) - BUF_BEGV (buf);
15323 /* I don't think this is guaranteed to be right. For the
15324 moment, we'll pretend it is. */
15325 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15327 if (end < start)
15328 end = start;
15329 if (whole < (end - start))
15330 whole = end - start;
15332 else
15333 start = end = whole = 0;
15335 /* Indicate what this scroll bar ought to be displaying now. */
15336 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15337 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15338 (w, end - start, whole, start);
15342 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15343 selected_window is redisplayed.
15345 We can return without actually redisplaying the window if
15346 fonts_changed_p is nonzero. In that case, redisplay_internal will
15347 retry. */
15349 static void
15350 redisplay_window (Lisp_Object window, int just_this_one_p)
15352 struct window *w = XWINDOW (window);
15353 struct frame *f = XFRAME (w->frame);
15354 struct buffer *buffer = XBUFFER (w->buffer);
15355 struct buffer *old = current_buffer;
15356 struct text_pos lpoint, opoint, startp;
15357 int update_mode_line;
15358 int tem;
15359 struct it it;
15360 /* Record it now because it's overwritten. */
15361 int current_matrix_up_to_date_p = 0;
15362 int used_current_matrix_p = 0;
15363 /* This is less strict than current_matrix_up_to_date_p.
15364 It indicates that the buffer contents and narrowing are unchanged. */
15365 int buffer_unchanged_p = 0;
15366 int temp_scroll_step = 0;
15367 ptrdiff_t count = SPECPDL_INDEX ();
15368 int rc;
15369 int centering_position = -1;
15370 int last_line_misfit = 0;
15371 ptrdiff_t beg_unchanged, end_unchanged;
15373 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15374 opoint = lpoint;
15376 /* W must be a leaf window here. */
15377 eassert (!NILP (w->buffer));
15378 #ifdef GLYPH_DEBUG
15379 *w->desired_matrix->method = 0;
15380 #endif
15382 restart:
15383 reconsider_clip_changes (w, buffer);
15385 /* Has the mode line to be updated? */
15386 update_mode_line = (w->update_mode_line
15387 || update_mode_lines
15388 || buffer->clip_changed
15389 || buffer->prevent_redisplay_optimizations_p);
15391 if (MINI_WINDOW_P (w))
15393 if (w == XWINDOW (echo_area_window)
15394 && !NILP (echo_area_buffer[0]))
15396 if (update_mode_line)
15397 /* We may have to update a tty frame's menu bar or a
15398 tool-bar. Example `M-x C-h C-h C-g'. */
15399 goto finish_menu_bars;
15400 else
15401 /* We've already displayed the echo area glyphs in this window. */
15402 goto finish_scroll_bars;
15404 else if ((w != XWINDOW (minibuf_window)
15405 || minibuf_level == 0)
15406 /* When buffer is nonempty, redisplay window normally. */
15407 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15408 /* Quail displays non-mini buffers in minibuffer window.
15409 In that case, redisplay the window normally. */
15410 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15412 /* W is a mini-buffer window, but it's not active, so clear
15413 it. */
15414 int yb = window_text_bottom_y (w);
15415 struct glyph_row *row;
15416 int y;
15418 for (y = 0, row = w->desired_matrix->rows;
15419 y < yb;
15420 y += row->height, ++row)
15421 blank_row (w, row, y);
15422 goto finish_scroll_bars;
15425 clear_glyph_matrix (w->desired_matrix);
15428 /* Otherwise set up data on this window; select its buffer and point
15429 value. */
15430 /* Really select the buffer, for the sake of buffer-local
15431 variables. */
15432 set_buffer_internal_1 (XBUFFER (w->buffer));
15434 current_matrix_up_to_date_p
15435 = (!NILP (w->window_end_valid)
15436 && !current_buffer->clip_changed
15437 && !current_buffer->prevent_redisplay_optimizations_p
15438 && w->last_modified >= MODIFF
15439 && w->last_overlay_modified >= OVERLAY_MODIFF);
15441 /* Run the window-bottom-change-functions
15442 if it is possible that the text on the screen has changed
15443 (either due to modification of the text, or any other reason). */
15444 if (!current_matrix_up_to_date_p
15445 && !NILP (Vwindow_text_change_functions))
15447 safe_run_hooks (Qwindow_text_change_functions);
15448 goto restart;
15451 beg_unchanged = BEG_UNCHANGED;
15452 end_unchanged = END_UNCHANGED;
15454 SET_TEXT_POS (opoint, PT, PT_BYTE);
15456 specbind (Qinhibit_point_motion_hooks, Qt);
15458 buffer_unchanged_p
15459 = (!NILP (w->window_end_valid)
15460 && !current_buffer->clip_changed
15461 && w->last_modified >= MODIFF
15462 && w->last_overlay_modified >= OVERLAY_MODIFF);
15464 /* When windows_or_buffers_changed is non-zero, we can't rely on
15465 the window end being valid, so set it to nil there. */
15466 if (windows_or_buffers_changed)
15468 /* If window starts on a continuation line, maybe adjust the
15469 window start in case the window's width changed. */
15470 if (XMARKER (w->start)->buffer == current_buffer)
15471 compute_window_start_on_continuation_line (w);
15473 WSET (w, window_end_valid, Qnil);
15476 /* Some sanity checks. */
15477 CHECK_WINDOW_END (w);
15478 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15479 abort ();
15480 if (BYTEPOS (opoint) < CHARPOS (opoint))
15481 abort ();
15483 /* If %c is in mode line, update it if needed. */
15484 if (!NILP (w->column_number_displayed)
15485 /* This alternative quickly identifies a common case
15486 where no change is needed. */
15487 && !(PT == w->last_point
15488 && w->last_modified >= MODIFF
15489 && w->last_overlay_modified >= OVERLAY_MODIFF)
15490 && (XFASTINT (w->column_number_displayed) != current_column ()))
15491 update_mode_line = 1;
15493 /* Count number of windows showing the selected buffer. An indirect
15494 buffer counts as its base buffer. */
15495 if (!just_this_one_p)
15497 struct buffer *current_base, *window_base;
15498 current_base = current_buffer;
15499 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15500 if (current_base->base_buffer)
15501 current_base = current_base->base_buffer;
15502 if (window_base->base_buffer)
15503 window_base = window_base->base_buffer;
15504 if (current_base == window_base)
15505 buffer_shared++;
15508 /* Point refers normally to the selected window. For any other
15509 window, set up appropriate value. */
15510 if (!EQ (window, selected_window))
15512 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15513 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15514 if (new_pt < BEGV)
15516 new_pt = BEGV;
15517 new_pt_byte = BEGV_BYTE;
15518 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15520 else if (new_pt > (ZV - 1))
15522 new_pt = ZV;
15523 new_pt_byte = ZV_BYTE;
15524 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15527 /* We don't use SET_PT so that the point-motion hooks don't run. */
15528 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15531 /* If any of the character widths specified in the display table
15532 have changed, invalidate the width run cache. It's true that
15533 this may be a bit late to catch such changes, but the rest of
15534 redisplay goes (non-fatally) haywire when the display table is
15535 changed, so why should we worry about doing any better? */
15536 if (current_buffer->width_run_cache)
15538 struct Lisp_Char_Table *disptab = buffer_display_table ();
15540 if (! disptab_matches_widthtab
15541 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15543 invalidate_region_cache (current_buffer,
15544 current_buffer->width_run_cache,
15545 BEG, Z);
15546 recompute_width_table (current_buffer, disptab);
15550 /* If window-start is screwed up, choose a new one. */
15551 if (XMARKER (w->start)->buffer != current_buffer)
15552 goto recenter;
15554 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15556 /* If someone specified a new starting point but did not insist,
15557 check whether it can be used. */
15558 if (w->optional_new_start
15559 && CHARPOS (startp) >= BEGV
15560 && CHARPOS (startp) <= ZV)
15562 w->optional_new_start = 0;
15563 start_display (&it, w, startp);
15564 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15565 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15566 if (IT_CHARPOS (it) == PT)
15567 w->force_start = 1;
15568 /* IT may overshoot PT if text at PT is invisible. */
15569 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15570 w->force_start = 1;
15573 force_start:
15575 /* Handle case where place to start displaying has been specified,
15576 unless the specified location is outside the accessible range. */
15577 if (w->force_start || w->frozen_window_start_p)
15579 /* We set this later on if we have to adjust point. */
15580 int new_vpos = -1;
15582 w->force_start = 0;
15583 w->vscroll = 0;
15584 WSET (w, window_end_valid, Qnil);
15586 /* Forget any recorded base line for line number display. */
15587 if (!buffer_unchanged_p)
15588 WSET (w, base_line_number, Qnil);
15590 /* Redisplay the mode line. Select the buffer properly for that.
15591 Also, run the hook window-scroll-functions
15592 because we have scrolled. */
15593 /* Note, we do this after clearing force_start because
15594 if there's an error, it is better to forget about force_start
15595 than to get into an infinite loop calling the hook functions
15596 and having them get more errors. */
15597 if (!update_mode_line
15598 || ! NILP (Vwindow_scroll_functions))
15600 update_mode_line = 1;
15601 w->update_mode_line = 1;
15602 startp = run_window_scroll_functions (window, startp);
15605 w->last_modified = 0;
15606 w->last_overlay_modified = 0;
15607 if (CHARPOS (startp) < BEGV)
15608 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15609 else if (CHARPOS (startp) > ZV)
15610 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15612 /* Redisplay, then check if cursor has been set during the
15613 redisplay. Give up if new fonts were loaded. */
15614 /* We used to issue a CHECK_MARGINS argument to try_window here,
15615 but this causes scrolling to fail when point begins inside
15616 the scroll margin (bug#148) -- cyd */
15617 if (!try_window (window, startp, 0))
15619 w->force_start = 1;
15620 clear_glyph_matrix (w->desired_matrix);
15621 goto need_larger_matrices;
15624 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15626 /* If point does not appear, try to move point so it does
15627 appear. The desired matrix has been built above, so we
15628 can use it here. */
15629 new_vpos = window_box_height (w) / 2;
15632 if (!cursor_row_fully_visible_p (w, 0, 0))
15634 /* Point does appear, but on a line partly visible at end of window.
15635 Move it back to a fully-visible line. */
15636 new_vpos = window_box_height (w);
15639 /* If we need to move point for either of the above reasons,
15640 now actually do it. */
15641 if (new_vpos >= 0)
15643 struct glyph_row *row;
15645 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15646 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15647 ++row;
15649 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15650 MATRIX_ROW_START_BYTEPOS (row));
15652 if (w != XWINDOW (selected_window))
15653 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15654 else if (current_buffer == old)
15655 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15657 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15659 /* If we are highlighting the region, then we just changed
15660 the region, so redisplay to show it. */
15661 if (!NILP (Vtransient_mark_mode)
15662 && !NILP (BVAR (current_buffer, mark_active)))
15664 clear_glyph_matrix (w->desired_matrix);
15665 if (!try_window (window, startp, 0))
15666 goto need_larger_matrices;
15670 #ifdef GLYPH_DEBUG
15671 debug_method_add (w, "forced window start");
15672 #endif
15673 goto done;
15676 /* Handle case where text has not changed, only point, and it has
15677 not moved off the frame, and we are not retrying after hscroll.
15678 (current_matrix_up_to_date_p is nonzero when retrying.) */
15679 if (current_matrix_up_to_date_p
15680 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15681 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15683 switch (rc)
15685 case CURSOR_MOVEMENT_SUCCESS:
15686 used_current_matrix_p = 1;
15687 goto done;
15689 case CURSOR_MOVEMENT_MUST_SCROLL:
15690 goto try_to_scroll;
15692 default:
15693 abort ();
15696 /* If current starting point was originally the beginning of a line
15697 but no longer is, find a new starting point. */
15698 else if (w->start_at_line_beg
15699 && !(CHARPOS (startp) <= BEGV
15700 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15702 #ifdef GLYPH_DEBUG
15703 debug_method_add (w, "recenter 1");
15704 #endif
15705 goto recenter;
15708 /* Try scrolling with try_window_id. Value is > 0 if update has
15709 been done, it is -1 if we know that the same window start will
15710 not work. It is 0 if unsuccessful for some other reason. */
15711 else if ((tem = try_window_id (w)) != 0)
15713 #ifdef GLYPH_DEBUG
15714 debug_method_add (w, "try_window_id %d", tem);
15715 #endif
15717 if (fonts_changed_p)
15718 goto need_larger_matrices;
15719 if (tem > 0)
15720 goto done;
15722 /* Otherwise try_window_id has returned -1 which means that we
15723 don't want the alternative below this comment to execute. */
15725 else if (CHARPOS (startp) >= BEGV
15726 && CHARPOS (startp) <= ZV
15727 && PT >= CHARPOS (startp)
15728 && (CHARPOS (startp) < ZV
15729 /* Avoid starting at end of buffer. */
15730 || CHARPOS (startp) == BEGV
15731 || (w->last_modified >= MODIFF
15732 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15734 int d1, d2, d3, d4, d5, d6;
15736 /* If first window line is a continuation line, and window start
15737 is inside the modified region, but the first change is before
15738 current window start, we must select a new window start.
15740 However, if this is the result of a down-mouse event (e.g. by
15741 extending the mouse-drag-overlay), we don't want to select a
15742 new window start, since that would change the position under
15743 the mouse, resulting in an unwanted mouse-movement rather
15744 than a simple mouse-click. */
15745 if (!w->start_at_line_beg
15746 && NILP (do_mouse_tracking)
15747 && CHARPOS (startp) > BEGV
15748 && CHARPOS (startp) > BEG + beg_unchanged
15749 && CHARPOS (startp) <= Z - end_unchanged
15750 /* Even if w->start_at_line_beg is nil, a new window may
15751 start at a line_beg, since that's how set_buffer_window
15752 sets it. So, we need to check the return value of
15753 compute_window_start_on_continuation_line. (See also
15754 bug#197). */
15755 && XMARKER (w->start)->buffer == current_buffer
15756 && compute_window_start_on_continuation_line (w)
15757 /* It doesn't make sense to force the window start like we
15758 do at label force_start if it is already known that point
15759 will not be visible in the resulting window, because
15760 doing so will move point from its correct position
15761 instead of scrolling the window to bring point into view.
15762 See bug#9324. */
15763 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15765 w->force_start = 1;
15766 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15767 goto force_start;
15770 #ifdef GLYPH_DEBUG
15771 debug_method_add (w, "same window start");
15772 #endif
15774 /* Try to redisplay starting at same place as before.
15775 If point has not moved off frame, accept the results. */
15776 if (!current_matrix_up_to_date_p
15777 /* Don't use try_window_reusing_current_matrix in this case
15778 because a window scroll function can have changed the
15779 buffer. */
15780 || !NILP (Vwindow_scroll_functions)
15781 || MINI_WINDOW_P (w)
15782 || !(used_current_matrix_p
15783 = try_window_reusing_current_matrix (w)))
15785 IF_DEBUG (debug_method_add (w, "1"));
15786 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15787 /* -1 means we need to scroll.
15788 0 means we need new matrices, but fonts_changed_p
15789 is set in that case, so we will detect it below. */
15790 goto try_to_scroll;
15793 if (fonts_changed_p)
15794 goto need_larger_matrices;
15796 if (w->cursor.vpos >= 0)
15798 if (!just_this_one_p
15799 || current_buffer->clip_changed
15800 || BEG_UNCHANGED < CHARPOS (startp))
15801 /* Forget any recorded base line for line number display. */
15802 WSET (w, base_line_number, Qnil);
15804 if (!cursor_row_fully_visible_p (w, 1, 0))
15806 clear_glyph_matrix (w->desired_matrix);
15807 last_line_misfit = 1;
15809 /* Drop through and scroll. */
15810 else
15811 goto done;
15813 else
15814 clear_glyph_matrix (w->desired_matrix);
15817 try_to_scroll:
15819 w->last_modified = 0;
15820 w->last_overlay_modified = 0;
15822 /* Redisplay the mode line. Select the buffer properly for that. */
15823 if (!update_mode_line)
15825 update_mode_line = 1;
15826 w->update_mode_line = 1;
15829 /* Try to scroll by specified few lines. */
15830 if ((scroll_conservatively
15831 || emacs_scroll_step
15832 || temp_scroll_step
15833 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15834 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15835 && CHARPOS (startp) >= BEGV
15836 && CHARPOS (startp) <= ZV)
15838 /* The function returns -1 if new fonts were loaded, 1 if
15839 successful, 0 if not successful. */
15840 int ss = try_scrolling (window, just_this_one_p,
15841 scroll_conservatively,
15842 emacs_scroll_step,
15843 temp_scroll_step, last_line_misfit);
15844 switch (ss)
15846 case SCROLLING_SUCCESS:
15847 goto done;
15849 case SCROLLING_NEED_LARGER_MATRICES:
15850 goto need_larger_matrices;
15852 case SCROLLING_FAILED:
15853 break;
15855 default:
15856 abort ();
15860 /* Finally, just choose a place to start which positions point
15861 according to user preferences. */
15863 recenter:
15865 #ifdef GLYPH_DEBUG
15866 debug_method_add (w, "recenter");
15867 #endif
15869 /* w->vscroll = 0; */
15871 /* Forget any previously recorded base line for line number display. */
15872 if (!buffer_unchanged_p)
15873 WSET (w, base_line_number, Qnil);
15875 /* Determine the window start relative to point. */
15876 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15877 it.current_y = it.last_visible_y;
15878 if (centering_position < 0)
15880 int margin =
15881 scroll_margin > 0
15882 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15883 : 0;
15884 ptrdiff_t margin_pos = CHARPOS (startp);
15885 Lisp_Object aggressive;
15886 int scrolling_up;
15888 /* If there is a scroll margin at the top of the window, find
15889 its character position. */
15890 if (margin
15891 /* Cannot call start_display if startp is not in the
15892 accessible region of the buffer. This can happen when we
15893 have just switched to a different buffer and/or changed
15894 its restriction. In that case, startp is initialized to
15895 the character position 1 (BEGV) because we did not yet
15896 have chance to display the buffer even once. */
15897 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15899 struct it it1;
15900 void *it1data = NULL;
15902 SAVE_IT (it1, it, it1data);
15903 start_display (&it1, w, startp);
15904 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15905 margin_pos = IT_CHARPOS (it1);
15906 RESTORE_IT (&it, &it, it1data);
15908 scrolling_up = PT > margin_pos;
15909 aggressive =
15910 scrolling_up
15911 ? BVAR (current_buffer, scroll_up_aggressively)
15912 : BVAR (current_buffer, scroll_down_aggressively);
15914 if (!MINI_WINDOW_P (w)
15915 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15917 int pt_offset = 0;
15919 /* Setting scroll-conservatively overrides
15920 scroll-*-aggressively. */
15921 if (!scroll_conservatively && NUMBERP (aggressive))
15923 double float_amount = XFLOATINT (aggressive);
15925 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15926 if (pt_offset == 0 && float_amount > 0)
15927 pt_offset = 1;
15928 if (pt_offset && margin > 0)
15929 margin -= 1;
15931 /* Compute how much to move the window start backward from
15932 point so that point will be displayed where the user
15933 wants it. */
15934 if (scrolling_up)
15936 centering_position = it.last_visible_y;
15937 if (pt_offset)
15938 centering_position -= pt_offset;
15939 centering_position -=
15940 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15941 + WINDOW_HEADER_LINE_HEIGHT (w);
15942 /* Don't let point enter the scroll margin near top of
15943 the window. */
15944 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15945 centering_position = margin * FRAME_LINE_HEIGHT (f);
15947 else
15948 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15950 else
15951 /* Set the window start half the height of the window backward
15952 from point. */
15953 centering_position = window_box_height (w) / 2;
15955 move_it_vertically_backward (&it, centering_position);
15957 eassert (IT_CHARPOS (it) >= BEGV);
15959 /* The function move_it_vertically_backward may move over more
15960 than the specified y-distance. If it->w is small, e.g. a
15961 mini-buffer window, we may end up in front of the window's
15962 display area. Start displaying at the start of the line
15963 containing PT in this case. */
15964 if (it.current_y <= 0)
15966 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15967 move_it_vertically_backward (&it, 0);
15968 it.current_y = 0;
15971 it.current_x = it.hpos = 0;
15973 /* Set the window start position here explicitly, to avoid an
15974 infinite loop in case the functions in window-scroll-functions
15975 get errors. */
15976 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15978 /* Run scroll hooks. */
15979 startp = run_window_scroll_functions (window, it.current.pos);
15981 /* Redisplay the window. */
15982 if (!current_matrix_up_to_date_p
15983 || windows_or_buffers_changed
15984 || cursor_type_changed
15985 /* Don't use try_window_reusing_current_matrix in this case
15986 because it can have changed the buffer. */
15987 || !NILP (Vwindow_scroll_functions)
15988 || !just_this_one_p
15989 || MINI_WINDOW_P (w)
15990 || !(used_current_matrix_p
15991 = try_window_reusing_current_matrix (w)))
15992 try_window (window, startp, 0);
15994 /* If new fonts have been loaded (due to fontsets), give up. We
15995 have to start a new redisplay since we need to re-adjust glyph
15996 matrices. */
15997 if (fonts_changed_p)
15998 goto need_larger_matrices;
16000 /* If cursor did not appear assume that the middle of the window is
16001 in the first line of the window. Do it again with the next line.
16002 (Imagine a window of height 100, displaying two lines of height
16003 60. Moving back 50 from it->last_visible_y will end in the first
16004 line.) */
16005 if (w->cursor.vpos < 0)
16007 if (!NILP (w->window_end_valid)
16008 && PT >= Z - XFASTINT (w->window_end_pos))
16010 clear_glyph_matrix (w->desired_matrix);
16011 move_it_by_lines (&it, 1);
16012 try_window (window, it.current.pos, 0);
16014 else if (PT < IT_CHARPOS (it))
16016 clear_glyph_matrix (w->desired_matrix);
16017 move_it_by_lines (&it, -1);
16018 try_window (window, it.current.pos, 0);
16020 else
16022 /* Not much we can do about it. */
16026 /* Consider the following case: Window starts at BEGV, there is
16027 invisible, intangible text at BEGV, so that display starts at
16028 some point START > BEGV. It can happen that we are called with
16029 PT somewhere between BEGV and START. Try to handle that case. */
16030 if (w->cursor.vpos < 0)
16032 struct glyph_row *row = w->current_matrix->rows;
16033 if (row->mode_line_p)
16034 ++row;
16035 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16038 if (!cursor_row_fully_visible_p (w, 0, 0))
16040 /* If vscroll is enabled, disable it and try again. */
16041 if (w->vscroll)
16043 w->vscroll = 0;
16044 clear_glyph_matrix (w->desired_matrix);
16045 goto recenter;
16048 /* Users who set scroll-conservatively to a large number want
16049 point just above/below the scroll margin. If we ended up
16050 with point's row partially visible, move the window start to
16051 make that row fully visible and out of the margin. */
16052 if (scroll_conservatively > SCROLL_LIMIT)
16054 int margin =
16055 scroll_margin > 0
16056 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16057 : 0;
16058 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16060 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16061 clear_glyph_matrix (w->desired_matrix);
16062 if (1 == try_window (window, it.current.pos,
16063 TRY_WINDOW_CHECK_MARGINS))
16064 goto done;
16067 /* If centering point failed to make the whole line visible,
16068 put point at the top instead. That has to make the whole line
16069 visible, if it can be done. */
16070 if (centering_position == 0)
16071 goto done;
16073 clear_glyph_matrix (w->desired_matrix);
16074 centering_position = 0;
16075 goto recenter;
16078 done:
16080 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16081 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16082 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16084 /* Display the mode line, if we must. */
16085 if ((update_mode_line
16086 /* If window not full width, must redo its mode line
16087 if (a) the window to its side is being redone and
16088 (b) we do a frame-based redisplay. This is a consequence
16089 of how inverted lines are drawn in frame-based redisplay. */
16090 || (!just_this_one_p
16091 && !FRAME_WINDOW_P (f)
16092 && !WINDOW_FULL_WIDTH_P (w))
16093 /* Line number to display. */
16094 || INTEGERP (w->base_line_pos)
16095 /* Column number is displayed and different from the one displayed. */
16096 || (!NILP (w->column_number_displayed)
16097 && (XFASTINT (w->column_number_displayed) != current_column ())))
16098 /* This means that the window has a mode line. */
16099 && (WINDOW_WANTS_MODELINE_P (w)
16100 || WINDOW_WANTS_HEADER_LINE_P (w)))
16102 display_mode_lines (w);
16104 /* If mode line height has changed, arrange for a thorough
16105 immediate redisplay using the correct mode line height. */
16106 if (WINDOW_WANTS_MODELINE_P (w)
16107 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16109 fonts_changed_p = 1;
16110 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16111 = DESIRED_MODE_LINE_HEIGHT (w);
16114 /* If header line height has changed, arrange for a thorough
16115 immediate redisplay using the correct header line height. */
16116 if (WINDOW_WANTS_HEADER_LINE_P (w)
16117 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16119 fonts_changed_p = 1;
16120 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16121 = DESIRED_HEADER_LINE_HEIGHT (w);
16124 if (fonts_changed_p)
16125 goto need_larger_matrices;
16128 if (!line_number_displayed
16129 && !BUFFERP (w->base_line_pos))
16131 WSET (w, base_line_pos, Qnil);
16132 WSET (w, base_line_number, Qnil);
16135 finish_menu_bars:
16137 /* When we reach a frame's selected window, redo the frame's menu bar. */
16138 if (update_mode_line
16139 && EQ (FRAME_SELECTED_WINDOW (f), window))
16141 int redisplay_menu_p = 0;
16143 if (FRAME_WINDOW_P (f))
16145 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16146 || defined (HAVE_NS) || defined (USE_GTK)
16147 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16148 #else
16149 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16150 #endif
16152 else
16153 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16155 if (redisplay_menu_p)
16156 display_menu_bar (w);
16158 #ifdef HAVE_WINDOW_SYSTEM
16159 if (FRAME_WINDOW_P (f))
16161 #if defined (USE_GTK) || defined (HAVE_NS)
16162 if (FRAME_EXTERNAL_TOOL_BAR (f))
16163 redisplay_tool_bar (f);
16164 #else
16165 if (WINDOWP (f->tool_bar_window)
16166 && (FRAME_TOOL_BAR_LINES (f) > 0
16167 || !NILP (Vauto_resize_tool_bars))
16168 && redisplay_tool_bar (f))
16169 ignore_mouse_drag_p = 1;
16170 #endif
16172 #endif
16175 #ifdef HAVE_WINDOW_SYSTEM
16176 if (FRAME_WINDOW_P (f)
16177 && update_window_fringes (w, (just_this_one_p
16178 || (!used_current_matrix_p && !overlay_arrow_seen)
16179 || w->pseudo_window_p)))
16181 update_begin (f);
16182 BLOCK_INPUT;
16183 if (draw_window_fringes (w, 1))
16184 x_draw_vertical_border (w);
16185 UNBLOCK_INPUT;
16186 update_end (f);
16188 #endif /* HAVE_WINDOW_SYSTEM */
16190 /* We go to this label, with fonts_changed_p nonzero,
16191 if it is necessary to try again using larger glyph matrices.
16192 We have to redeem the scroll bar even in this case,
16193 because the loop in redisplay_internal expects that. */
16194 need_larger_matrices:
16196 finish_scroll_bars:
16198 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16200 /* Set the thumb's position and size. */
16201 set_vertical_scroll_bar (w);
16203 /* Note that we actually used the scroll bar attached to this
16204 window, so it shouldn't be deleted at the end of redisplay. */
16205 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16206 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16209 /* Restore current_buffer and value of point in it. The window
16210 update may have changed the buffer, so first make sure `opoint'
16211 is still valid (Bug#6177). */
16212 if (CHARPOS (opoint) < BEGV)
16213 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16214 else if (CHARPOS (opoint) > ZV)
16215 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16216 else
16217 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16219 set_buffer_internal_1 (old);
16220 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16221 shorter. This can be caused by log truncation in *Messages*. */
16222 if (CHARPOS (lpoint) <= ZV)
16223 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16225 unbind_to (count, Qnil);
16229 /* Build the complete desired matrix of WINDOW with a window start
16230 buffer position POS.
16232 Value is 1 if successful. It is zero if fonts were loaded during
16233 redisplay which makes re-adjusting glyph matrices necessary, and -1
16234 if point would appear in the scroll margins.
16235 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16236 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16237 set in FLAGS.) */
16240 try_window (Lisp_Object window, struct text_pos pos, int flags)
16242 struct window *w = XWINDOW (window);
16243 struct it it;
16244 struct glyph_row *last_text_row = NULL;
16245 struct frame *f = XFRAME (w->frame);
16247 /* Make POS the new window start. */
16248 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16250 /* Mark cursor position as unknown. No overlay arrow seen. */
16251 w->cursor.vpos = -1;
16252 overlay_arrow_seen = 0;
16254 /* Initialize iterator and info to start at POS. */
16255 start_display (&it, w, pos);
16257 /* Display all lines of W. */
16258 while (it.current_y < it.last_visible_y)
16260 if (display_line (&it))
16261 last_text_row = it.glyph_row - 1;
16262 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16263 return 0;
16266 /* Don't let the cursor end in the scroll margins. */
16267 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16268 && !MINI_WINDOW_P (w))
16270 int this_scroll_margin;
16272 if (scroll_margin > 0)
16274 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16275 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16277 else
16278 this_scroll_margin = 0;
16280 if ((w->cursor.y >= 0 /* not vscrolled */
16281 && w->cursor.y < this_scroll_margin
16282 && CHARPOS (pos) > BEGV
16283 && IT_CHARPOS (it) < ZV)
16284 /* rms: considering make_cursor_line_fully_visible_p here
16285 seems to give wrong results. We don't want to recenter
16286 when the last line is partly visible, we want to allow
16287 that case to be handled in the usual way. */
16288 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16290 w->cursor.vpos = -1;
16291 clear_glyph_matrix (w->desired_matrix);
16292 return -1;
16296 /* If bottom moved off end of frame, change mode line percentage. */
16297 if (XFASTINT (w->window_end_pos) <= 0
16298 && Z != IT_CHARPOS (it))
16299 w->update_mode_line = 1;
16301 /* Set window_end_pos to the offset of the last character displayed
16302 on the window from the end of current_buffer. Set
16303 window_end_vpos to its row number. */
16304 if (last_text_row)
16306 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16307 w->window_end_bytepos
16308 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16309 WSET (w, window_end_pos,
16310 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16311 WSET (w, window_end_vpos,
16312 make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16313 eassert
16314 (MATRIX_ROW (w->desired_matrix,
16315 XFASTINT (w->window_end_vpos))->displays_text_p);
16317 else
16319 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16320 WSET (w, window_end_pos, make_number (Z - ZV));
16321 WSET (w, window_end_vpos, make_number (0));
16324 /* But that is not valid info until redisplay finishes. */
16325 WSET (w, window_end_valid, Qnil);
16326 return 1;
16331 /************************************************************************
16332 Window redisplay reusing current matrix when buffer has not changed
16333 ************************************************************************/
16335 /* Try redisplay of window W showing an unchanged buffer with a
16336 different window start than the last time it was displayed by
16337 reusing its current matrix. Value is non-zero if successful.
16338 W->start is the new window start. */
16340 static int
16341 try_window_reusing_current_matrix (struct window *w)
16343 struct frame *f = XFRAME (w->frame);
16344 struct glyph_row *bottom_row;
16345 struct it it;
16346 struct run run;
16347 struct text_pos start, new_start;
16348 int nrows_scrolled, i;
16349 struct glyph_row *last_text_row;
16350 struct glyph_row *last_reused_text_row;
16351 struct glyph_row *start_row;
16352 int start_vpos, min_y, max_y;
16354 #ifdef GLYPH_DEBUG
16355 if (inhibit_try_window_reusing)
16356 return 0;
16357 #endif
16359 if (/* This function doesn't handle terminal frames. */
16360 !FRAME_WINDOW_P (f)
16361 /* Don't try to reuse the display if windows have been split
16362 or such. */
16363 || windows_or_buffers_changed
16364 || cursor_type_changed)
16365 return 0;
16367 /* Can't do this if region may have changed. */
16368 if ((!NILP (Vtransient_mark_mode)
16369 && !NILP (BVAR (current_buffer, mark_active)))
16370 || !NILP (w->region_showing)
16371 || !NILP (Vshow_trailing_whitespace))
16372 return 0;
16374 /* If top-line visibility has changed, give up. */
16375 if (WINDOW_WANTS_HEADER_LINE_P (w)
16376 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16377 return 0;
16379 /* Give up if old or new display is scrolled vertically. We could
16380 make this function handle this, but right now it doesn't. */
16381 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16382 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16383 return 0;
16385 /* The variable new_start now holds the new window start. The old
16386 start `start' can be determined from the current matrix. */
16387 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16388 start = start_row->minpos;
16389 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16391 /* Clear the desired matrix for the display below. */
16392 clear_glyph_matrix (w->desired_matrix);
16394 if (CHARPOS (new_start) <= CHARPOS (start))
16396 /* Don't use this method if the display starts with an ellipsis
16397 displayed for invisible text. It's not easy to handle that case
16398 below, and it's certainly not worth the effort since this is
16399 not a frequent case. */
16400 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16401 return 0;
16403 IF_DEBUG (debug_method_add (w, "twu1"));
16405 /* Display up to a row that can be reused. The variable
16406 last_text_row is set to the last row displayed that displays
16407 text. Note that it.vpos == 0 if or if not there is a
16408 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16409 start_display (&it, w, new_start);
16410 w->cursor.vpos = -1;
16411 last_text_row = last_reused_text_row = NULL;
16413 while (it.current_y < it.last_visible_y
16414 && !fonts_changed_p)
16416 /* If we have reached into the characters in the START row,
16417 that means the line boundaries have changed. So we
16418 can't start copying with the row START. Maybe it will
16419 work to start copying with the following row. */
16420 while (IT_CHARPOS (it) > CHARPOS (start))
16422 /* Advance to the next row as the "start". */
16423 start_row++;
16424 start = start_row->minpos;
16425 /* If there are no more rows to try, or just one, give up. */
16426 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16427 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16428 || CHARPOS (start) == ZV)
16430 clear_glyph_matrix (w->desired_matrix);
16431 return 0;
16434 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16436 /* If we have reached alignment, we can copy the rest of the
16437 rows. */
16438 if (IT_CHARPOS (it) == CHARPOS (start)
16439 /* Don't accept "alignment" inside a display vector,
16440 since start_row could have started in the middle of
16441 that same display vector (thus their character
16442 positions match), and we have no way of telling if
16443 that is the case. */
16444 && it.current.dpvec_index < 0)
16445 break;
16447 if (display_line (&it))
16448 last_text_row = it.glyph_row - 1;
16452 /* A value of current_y < last_visible_y means that we stopped
16453 at the previous window start, which in turn means that we
16454 have at least one reusable row. */
16455 if (it.current_y < it.last_visible_y)
16457 struct glyph_row *row;
16459 /* IT.vpos always starts from 0; it counts text lines. */
16460 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16462 /* Find PT if not already found in the lines displayed. */
16463 if (w->cursor.vpos < 0)
16465 int dy = it.current_y - start_row->y;
16467 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16468 row = row_containing_pos (w, PT, row, NULL, dy);
16469 if (row)
16470 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16471 dy, nrows_scrolled);
16472 else
16474 clear_glyph_matrix (w->desired_matrix);
16475 return 0;
16479 /* Scroll the display. Do it before the current matrix is
16480 changed. The problem here is that update has not yet
16481 run, i.e. part of the current matrix is not up to date.
16482 scroll_run_hook will clear the cursor, and use the
16483 current matrix to get the height of the row the cursor is
16484 in. */
16485 run.current_y = start_row->y;
16486 run.desired_y = it.current_y;
16487 run.height = it.last_visible_y - it.current_y;
16489 if (run.height > 0 && run.current_y != run.desired_y)
16491 update_begin (f);
16492 FRAME_RIF (f)->update_window_begin_hook (w);
16493 FRAME_RIF (f)->clear_window_mouse_face (w);
16494 FRAME_RIF (f)->scroll_run_hook (w, &run);
16495 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16496 update_end (f);
16499 /* Shift current matrix down by nrows_scrolled lines. */
16500 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16501 rotate_matrix (w->current_matrix,
16502 start_vpos,
16503 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16504 nrows_scrolled);
16506 /* Disable lines that must be updated. */
16507 for (i = 0; i < nrows_scrolled; ++i)
16508 (start_row + i)->enabled_p = 0;
16510 /* Re-compute Y positions. */
16511 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16512 max_y = it.last_visible_y;
16513 for (row = start_row + nrows_scrolled;
16514 row < bottom_row;
16515 ++row)
16517 row->y = it.current_y;
16518 row->visible_height = row->height;
16520 if (row->y < min_y)
16521 row->visible_height -= min_y - row->y;
16522 if (row->y + row->height > max_y)
16523 row->visible_height -= row->y + row->height - max_y;
16524 if (row->fringe_bitmap_periodic_p)
16525 row->redraw_fringe_bitmaps_p = 1;
16527 it.current_y += row->height;
16529 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16530 last_reused_text_row = row;
16531 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16532 break;
16535 /* Disable lines in the current matrix which are now
16536 below the window. */
16537 for (++row; row < bottom_row; ++row)
16538 row->enabled_p = row->mode_line_p = 0;
16541 /* Update window_end_pos etc.; last_reused_text_row is the last
16542 reused row from the current matrix containing text, if any.
16543 The value of last_text_row is the last displayed line
16544 containing text. */
16545 if (last_reused_text_row)
16547 w->window_end_bytepos
16548 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16549 WSET (w, window_end_pos,
16550 make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16551 WSET (w, window_end_vpos,
16552 make_number (MATRIX_ROW_VPOS (last_reused_text_row, w->current_matrix)));
16554 else if (last_text_row)
16556 w->window_end_bytepos
16557 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16558 WSET (w, window_end_pos,
16559 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16560 WSET (w, window_end_vpos,
16561 make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16563 else
16565 /* This window must be completely empty. */
16566 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16567 WSET (w, window_end_pos, make_number (Z - ZV));
16568 WSET (w, window_end_vpos, make_number (0));
16570 WSET (w, window_end_valid, Qnil);
16572 /* Update hint: don't try scrolling again in update_window. */
16573 w->desired_matrix->no_scrolling_p = 1;
16575 #ifdef GLYPH_DEBUG
16576 debug_method_add (w, "try_window_reusing_current_matrix 1");
16577 #endif
16578 return 1;
16580 else if (CHARPOS (new_start) > CHARPOS (start))
16582 struct glyph_row *pt_row, *row;
16583 struct glyph_row *first_reusable_row;
16584 struct glyph_row *first_row_to_display;
16585 int dy;
16586 int yb = window_text_bottom_y (w);
16588 /* Find the row starting at new_start, if there is one. Don't
16589 reuse a partially visible line at the end. */
16590 first_reusable_row = start_row;
16591 while (first_reusable_row->enabled_p
16592 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16593 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16594 < CHARPOS (new_start)))
16595 ++first_reusable_row;
16597 /* Give up if there is no row to reuse. */
16598 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16599 || !first_reusable_row->enabled_p
16600 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16601 != CHARPOS (new_start)))
16602 return 0;
16604 /* We can reuse fully visible rows beginning with
16605 first_reusable_row to the end of the window. Set
16606 first_row_to_display to the first row that cannot be reused.
16607 Set pt_row to the row containing point, if there is any. */
16608 pt_row = NULL;
16609 for (first_row_to_display = first_reusable_row;
16610 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16611 ++first_row_to_display)
16613 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16614 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16615 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16616 && first_row_to_display->ends_at_zv_p
16617 && pt_row == NULL)))
16618 pt_row = first_row_to_display;
16621 /* Start displaying at the start of first_row_to_display. */
16622 eassert (first_row_to_display->y < yb);
16623 init_to_row_start (&it, w, first_row_to_display);
16625 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16626 - start_vpos);
16627 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16628 - nrows_scrolled);
16629 it.current_y = (first_row_to_display->y - first_reusable_row->y
16630 + WINDOW_HEADER_LINE_HEIGHT (w));
16632 /* Display lines beginning with first_row_to_display in the
16633 desired matrix. Set last_text_row to the last row displayed
16634 that displays text. */
16635 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16636 if (pt_row == NULL)
16637 w->cursor.vpos = -1;
16638 last_text_row = NULL;
16639 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16640 if (display_line (&it))
16641 last_text_row = it.glyph_row - 1;
16643 /* If point is in a reused row, adjust y and vpos of the cursor
16644 position. */
16645 if (pt_row)
16647 w->cursor.vpos -= nrows_scrolled;
16648 w->cursor.y -= first_reusable_row->y - start_row->y;
16651 /* Give up if point isn't in a row displayed or reused. (This
16652 also handles the case where w->cursor.vpos < nrows_scrolled
16653 after the calls to display_line, which can happen with scroll
16654 margins. See bug#1295.) */
16655 if (w->cursor.vpos < 0)
16657 clear_glyph_matrix (w->desired_matrix);
16658 return 0;
16661 /* Scroll the display. */
16662 run.current_y = first_reusable_row->y;
16663 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16664 run.height = it.last_visible_y - run.current_y;
16665 dy = run.current_y - run.desired_y;
16667 if (run.height)
16669 update_begin (f);
16670 FRAME_RIF (f)->update_window_begin_hook (w);
16671 FRAME_RIF (f)->clear_window_mouse_face (w);
16672 FRAME_RIF (f)->scroll_run_hook (w, &run);
16673 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16674 update_end (f);
16677 /* Adjust Y positions of reused rows. */
16678 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16679 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16680 max_y = it.last_visible_y;
16681 for (row = first_reusable_row; row < first_row_to_display; ++row)
16683 row->y -= dy;
16684 row->visible_height = row->height;
16685 if (row->y < min_y)
16686 row->visible_height -= min_y - row->y;
16687 if (row->y + row->height > max_y)
16688 row->visible_height -= row->y + row->height - max_y;
16689 if (row->fringe_bitmap_periodic_p)
16690 row->redraw_fringe_bitmaps_p = 1;
16693 /* Scroll the current matrix. */
16694 eassert (nrows_scrolled > 0);
16695 rotate_matrix (w->current_matrix,
16696 start_vpos,
16697 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16698 -nrows_scrolled);
16700 /* Disable rows not reused. */
16701 for (row -= nrows_scrolled; row < bottom_row; ++row)
16702 row->enabled_p = 0;
16704 /* Point may have moved to a different line, so we cannot assume that
16705 the previous cursor position is valid; locate the correct row. */
16706 if (pt_row)
16708 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16709 row < bottom_row
16710 && PT >= MATRIX_ROW_END_CHARPOS (row)
16711 && !row->ends_at_zv_p;
16712 row++)
16714 w->cursor.vpos++;
16715 w->cursor.y = row->y;
16717 if (row < bottom_row)
16719 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16720 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16722 /* Can't use this optimization with bidi-reordered glyph
16723 rows, unless cursor is already at point. */
16724 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16726 if (!(w->cursor.hpos >= 0
16727 && w->cursor.hpos < row->used[TEXT_AREA]
16728 && BUFFERP (glyph->object)
16729 && glyph->charpos == PT))
16730 return 0;
16732 else
16733 for (; glyph < end
16734 && (!BUFFERP (glyph->object)
16735 || glyph->charpos < PT);
16736 glyph++)
16738 w->cursor.hpos++;
16739 w->cursor.x += glyph->pixel_width;
16744 /* Adjust window end. A null value of last_text_row means that
16745 the window end is in reused rows which in turn means that
16746 only its vpos can have changed. */
16747 if (last_text_row)
16749 w->window_end_bytepos
16750 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16751 WSET (w, window_end_pos,
16752 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16753 WSET (w, window_end_vpos,
16754 make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16756 else
16758 WSET (w, window_end_vpos,
16759 make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16762 WSET (w, window_end_valid, Qnil);
16763 w->desired_matrix->no_scrolling_p = 1;
16765 #ifdef GLYPH_DEBUG
16766 debug_method_add (w, "try_window_reusing_current_matrix 2");
16767 #endif
16768 return 1;
16771 return 0;
16776 /************************************************************************
16777 Window redisplay reusing current matrix when buffer has changed
16778 ************************************************************************/
16780 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16781 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16782 ptrdiff_t *, ptrdiff_t *);
16783 static struct glyph_row *
16784 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16785 struct glyph_row *);
16788 /* Return the last row in MATRIX displaying text. If row START is
16789 non-null, start searching with that row. IT gives the dimensions
16790 of the display. Value is null if matrix is empty; otherwise it is
16791 a pointer to the row found. */
16793 static struct glyph_row *
16794 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16795 struct glyph_row *start)
16797 struct glyph_row *row, *row_found;
16799 /* Set row_found to the last row in IT->w's current matrix
16800 displaying text. The loop looks funny but think of partially
16801 visible lines. */
16802 row_found = NULL;
16803 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16804 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16806 eassert (row->enabled_p);
16807 row_found = row;
16808 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16809 break;
16810 ++row;
16813 return row_found;
16817 /* Return the last row in the current matrix of W that is not affected
16818 by changes at the start of current_buffer that occurred since W's
16819 current matrix was built. Value is null if no such row exists.
16821 BEG_UNCHANGED us the number of characters unchanged at the start of
16822 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16823 first changed character in current_buffer. Characters at positions <
16824 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16825 when the current matrix was built. */
16827 static struct glyph_row *
16828 find_last_unchanged_at_beg_row (struct window *w)
16830 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16831 struct glyph_row *row;
16832 struct glyph_row *row_found = NULL;
16833 int yb = window_text_bottom_y (w);
16835 /* Find the last row displaying unchanged text. */
16836 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16837 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16838 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16839 ++row)
16841 if (/* If row ends before first_changed_pos, it is unchanged,
16842 except in some case. */
16843 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16844 /* When row ends in ZV and we write at ZV it is not
16845 unchanged. */
16846 && !row->ends_at_zv_p
16847 /* When first_changed_pos is the end of a continued line,
16848 row is not unchanged because it may be no longer
16849 continued. */
16850 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16851 && (row->continued_p
16852 || row->exact_window_width_line_p))
16853 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16854 needs to be recomputed, so don't consider this row as
16855 unchanged. This happens when the last line was
16856 bidi-reordered and was killed immediately before this
16857 redisplay cycle. In that case, ROW->end stores the
16858 buffer position of the first visual-order character of
16859 the killed text, which is now beyond ZV. */
16860 && CHARPOS (row->end.pos) <= ZV)
16861 row_found = row;
16863 /* Stop if last visible row. */
16864 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16865 break;
16868 return row_found;
16872 /* Find the first glyph row in the current matrix of W that is not
16873 affected by changes at the end of current_buffer since the
16874 time W's current matrix was built.
16876 Return in *DELTA the number of chars by which buffer positions in
16877 unchanged text at the end of current_buffer must be adjusted.
16879 Return in *DELTA_BYTES the corresponding number of bytes.
16881 Value is null if no such row exists, i.e. all rows are affected by
16882 changes. */
16884 static struct glyph_row *
16885 find_first_unchanged_at_end_row (struct window *w,
16886 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16888 struct glyph_row *row;
16889 struct glyph_row *row_found = NULL;
16891 *delta = *delta_bytes = 0;
16893 /* Display must not have been paused, otherwise the current matrix
16894 is not up to date. */
16895 eassert (!NILP (w->window_end_valid));
16897 /* A value of window_end_pos >= END_UNCHANGED means that the window
16898 end is in the range of changed text. If so, there is no
16899 unchanged row at the end of W's current matrix. */
16900 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16901 return NULL;
16903 /* Set row to the last row in W's current matrix displaying text. */
16904 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16906 /* If matrix is entirely empty, no unchanged row exists. */
16907 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16909 /* The value of row is the last glyph row in the matrix having a
16910 meaningful buffer position in it. The end position of row
16911 corresponds to window_end_pos. This allows us to translate
16912 buffer positions in the current matrix to current buffer
16913 positions for characters not in changed text. */
16914 ptrdiff_t Z_old =
16915 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16916 ptrdiff_t Z_BYTE_old =
16917 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16918 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16919 struct glyph_row *first_text_row
16920 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16922 *delta = Z - Z_old;
16923 *delta_bytes = Z_BYTE - Z_BYTE_old;
16925 /* Set last_unchanged_pos to the buffer position of the last
16926 character in the buffer that has not been changed. Z is the
16927 index + 1 of the last character in current_buffer, i.e. by
16928 subtracting END_UNCHANGED we get the index of the last
16929 unchanged character, and we have to add BEG to get its buffer
16930 position. */
16931 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16932 last_unchanged_pos_old = last_unchanged_pos - *delta;
16934 /* Search backward from ROW for a row displaying a line that
16935 starts at a minimum position >= last_unchanged_pos_old. */
16936 for (; row > first_text_row; --row)
16938 /* This used to abort, but it can happen.
16939 It is ok to just stop the search instead here. KFS. */
16940 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16941 break;
16943 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16944 row_found = row;
16948 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16950 return row_found;
16954 /* Make sure that glyph rows in the current matrix of window W
16955 reference the same glyph memory as corresponding rows in the
16956 frame's frame matrix. This function is called after scrolling W's
16957 current matrix on a terminal frame in try_window_id and
16958 try_window_reusing_current_matrix. */
16960 static void
16961 sync_frame_with_window_matrix_rows (struct window *w)
16963 struct frame *f = XFRAME (w->frame);
16964 struct glyph_row *window_row, *window_row_end, *frame_row;
16966 /* Preconditions: W must be a leaf window and full-width. Its frame
16967 must have a frame matrix. */
16968 eassert (NILP (w->hchild) && NILP (w->vchild));
16969 eassert (WINDOW_FULL_WIDTH_P (w));
16970 eassert (!FRAME_WINDOW_P (f));
16972 /* If W is a full-width window, glyph pointers in W's current matrix
16973 have, by definition, to be the same as glyph pointers in the
16974 corresponding frame matrix. Note that frame matrices have no
16975 marginal areas (see build_frame_matrix). */
16976 window_row = w->current_matrix->rows;
16977 window_row_end = window_row + w->current_matrix->nrows;
16978 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16979 while (window_row < window_row_end)
16981 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16982 struct glyph *end = window_row->glyphs[LAST_AREA];
16984 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16985 frame_row->glyphs[TEXT_AREA] = start;
16986 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16987 frame_row->glyphs[LAST_AREA] = end;
16989 /* Disable frame rows whose corresponding window rows have
16990 been disabled in try_window_id. */
16991 if (!window_row->enabled_p)
16992 frame_row->enabled_p = 0;
16994 ++window_row, ++frame_row;
16999 /* Find the glyph row in window W containing CHARPOS. Consider all
17000 rows between START and END (not inclusive). END null means search
17001 all rows to the end of the display area of W. Value is the row
17002 containing CHARPOS or null. */
17004 struct glyph_row *
17005 row_containing_pos (struct window *w, ptrdiff_t charpos,
17006 struct glyph_row *start, struct glyph_row *end, int dy)
17008 struct glyph_row *row = start;
17009 struct glyph_row *best_row = NULL;
17010 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17011 int last_y;
17013 /* If we happen to start on a header-line, skip that. */
17014 if (row->mode_line_p)
17015 ++row;
17017 if ((end && row >= end) || !row->enabled_p)
17018 return NULL;
17020 last_y = window_text_bottom_y (w) - dy;
17022 while (1)
17024 /* Give up if we have gone too far. */
17025 if (end && row >= end)
17026 return NULL;
17027 /* This formerly returned if they were equal.
17028 I think that both quantities are of a "last plus one" type;
17029 if so, when they are equal, the row is within the screen. -- rms. */
17030 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17031 return NULL;
17033 /* If it is in this row, return this row. */
17034 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17035 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17036 /* The end position of a row equals the start
17037 position of the next row. If CHARPOS is there, we
17038 would rather display it in the next line, except
17039 when this line ends in ZV. */
17040 && !row->ends_at_zv_p
17041 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17042 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17044 struct glyph *g;
17046 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17047 || (!best_row && !row->continued_p))
17048 return row;
17049 /* In bidi-reordered rows, there could be several rows
17050 occluding point, all of them belonging to the same
17051 continued line. We need to find the row which fits
17052 CHARPOS the best. */
17053 for (g = row->glyphs[TEXT_AREA];
17054 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17055 g++)
17057 if (!STRINGP (g->object))
17059 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17061 mindif = eabs (g->charpos - charpos);
17062 best_row = row;
17063 /* Exact match always wins. */
17064 if (mindif == 0)
17065 return best_row;
17070 else if (best_row && !row->continued_p)
17071 return best_row;
17072 ++row;
17077 /* Try to redisplay window W by reusing its existing display. W's
17078 current matrix must be up to date when this function is called,
17079 i.e. window_end_valid must not be nil.
17081 Value is
17083 1 if display has been updated
17084 0 if otherwise unsuccessful
17085 -1 if redisplay with same window start is known not to succeed
17087 The following steps are performed:
17089 1. Find the last row in the current matrix of W that is not
17090 affected by changes at the start of current_buffer. If no such row
17091 is found, give up.
17093 2. Find the first row in W's current matrix that is not affected by
17094 changes at the end of current_buffer. Maybe there is no such row.
17096 3. Display lines beginning with the row + 1 found in step 1 to the
17097 row found in step 2 or, if step 2 didn't find a row, to the end of
17098 the window.
17100 4. If cursor is not known to appear on the window, give up.
17102 5. If display stopped at the row found in step 2, scroll the
17103 display and current matrix as needed.
17105 6. Maybe display some lines at the end of W, if we must. This can
17106 happen under various circumstances, like a partially visible line
17107 becoming fully visible, or because newly displayed lines are displayed
17108 in smaller font sizes.
17110 7. Update W's window end information. */
17112 static int
17113 try_window_id (struct window *w)
17115 struct frame *f = XFRAME (w->frame);
17116 struct glyph_matrix *current_matrix = w->current_matrix;
17117 struct glyph_matrix *desired_matrix = w->desired_matrix;
17118 struct glyph_row *last_unchanged_at_beg_row;
17119 struct glyph_row *first_unchanged_at_end_row;
17120 struct glyph_row *row;
17121 struct glyph_row *bottom_row;
17122 int bottom_vpos;
17123 struct it it;
17124 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17125 int dvpos, dy;
17126 struct text_pos start_pos;
17127 struct run run;
17128 int first_unchanged_at_end_vpos = 0;
17129 struct glyph_row *last_text_row, *last_text_row_at_end;
17130 struct text_pos start;
17131 ptrdiff_t first_changed_charpos, last_changed_charpos;
17133 #ifdef GLYPH_DEBUG
17134 if (inhibit_try_window_id)
17135 return 0;
17136 #endif
17138 /* This is handy for debugging. */
17139 #if 0
17140 #define GIVE_UP(X) \
17141 do { \
17142 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17143 return 0; \
17144 } while (0)
17145 #else
17146 #define GIVE_UP(X) return 0
17147 #endif
17149 SET_TEXT_POS_FROM_MARKER (start, w->start);
17151 /* Don't use this for mini-windows because these can show
17152 messages and mini-buffers, and we don't handle that here. */
17153 if (MINI_WINDOW_P (w))
17154 GIVE_UP (1);
17156 /* This flag is used to prevent redisplay optimizations. */
17157 if (windows_or_buffers_changed || cursor_type_changed)
17158 GIVE_UP (2);
17160 /* Verify that narrowing has not changed.
17161 Also verify that we were not told to prevent redisplay optimizations.
17162 It would be nice to further
17163 reduce the number of cases where this prevents try_window_id. */
17164 if (current_buffer->clip_changed
17165 || current_buffer->prevent_redisplay_optimizations_p)
17166 GIVE_UP (3);
17168 /* Window must either use window-based redisplay or be full width. */
17169 if (!FRAME_WINDOW_P (f)
17170 && (!FRAME_LINE_INS_DEL_OK (f)
17171 || !WINDOW_FULL_WIDTH_P (w)))
17172 GIVE_UP (4);
17174 /* Give up if point is known NOT to appear in W. */
17175 if (PT < CHARPOS (start))
17176 GIVE_UP (5);
17178 /* Another way to prevent redisplay optimizations. */
17179 if (w->last_modified == 0)
17180 GIVE_UP (6);
17182 /* Verify that window is not hscrolled. */
17183 if (w->hscroll != 0)
17184 GIVE_UP (7);
17186 /* Verify that display wasn't paused. */
17187 if (NILP (w->window_end_valid))
17188 GIVE_UP (8);
17190 /* Can't use this if highlighting a region because a cursor movement
17191 will do more than just set the cursor. */
17192 if (!NILP (Vtransient_mark_mode)
17193 && !NILP (BVAR (current_buffer, mark_active)))
17194 GIVE_UP (9);
17196 /* Likewise if highlighting trailing whitespace. */
17197 if (!NILP (Vshow_trailing_whitespace))
17198 GIVE_UP (11);
17200 /* Likewise if showing a region. */
17201 if (!NILP (w->region_showing))
17202 GIVE_UP (10);
17204 /* Can't use this if overlay arrow position and/or string have
17205 changed. */
17206 if (overlay_arrows_changed_p ())
17207 GIVE_UP (12);
17209 /* When word-wrap is on, adding a space to the first word of a
17210 wrapped line can change the wrap position, altering the line
17211 above it. It might be worthwhile to handle this more
17212 intelligently, but for now just redisplay from scratch. */
17213 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17214 GIVE_UP (21);
17216 /* Under bidi reordering, adding or deleting a character in the
17217 beginning of a paragraph, before the first strong directional
17218 character, can change the base direction of the paragraph (unless
17219 the buffer specifies a fixed paragraph direction), which will
17220 require to redisplay the whole paragraph. It might be worthwhile
17221 to find the paragraph limits and widen the range of redisplayed
17222 lines to that, but for now just give up this optimization and
17223 redisplay from scratch. */
17224 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17225 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17226 GIVE_UP (22);
17228 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17229 only if buffer has really changed. The reason is that the gap is
17230 initially at Z for freshly visited files. The code below would
17231 set end_unchanged to 0 in that case. */
17232 if (MODIFF > SAVE_MODIFF
17233 /* This seems to happen sometimes after saving a buffer. */
17234 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17236 if (GPT - BEG < BEG_UNCHANGED)
17237 BEG_UNCHANGED = GPT - BEG;
17238 if (Z - GPT < END_UNCHANGED)
17239 END_UNCHANGED = Z - GPT;
17242 /* The position of the first and last character that has been changed. */
17243 first_changed_charpos = BEG + BEG_UNCHANGED;
17244 last_changed_charpos = Z - END_UNCHANGED;
17246 /* If window starts after a line end, and the last change is in
17247 front of that newline, then changes don't affect the display.
17248 This case happens with stealth-fontification. Note that although
17249 the display is unchanged, glyph positions in the matrix have to
17250 be adjusted, of course. */
17251 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17252 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17253 && ((last_changed_charpos < CHARPOS (start)
17254 && CHARPOS (start) == BEGV)
17255 || (last_changed_charpos < CHARPOS (start) - 1
17256 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17258 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17259 struct glyph_row *r0;
17261 /* Compute how many chars/bytes have been added to or removed
17262 from the buffer. */
17263 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17264 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17265 Z_delta = Z - Z_old;
17266 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17268 /* Give up if PT is not in the window. Note that it already has
17269 been checked at the start of try_window_id that PT is not in
17270 front of the window start. */
17271 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17272 GIVE_UP (13);
17274 /* If window start is unchanged, we can reuse the whole matrix
17275 as is, after adjusting glyph positions. No need to compute
17276 the window end again, since its offset from Z hasn't changed. */
17277 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17278 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17279 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17280 /* PT must not be in a partially visible line. */
17281 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17282 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17284 /* Adjust positions in the glyph matrix. */
17285 if (Z_delta || Z_delta_bytes)
17287 struct glyph_row *r1
17288 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17289 increment_matrix_positions (w->current_matrix,
17290 MATRIX_ROW_VPOS (r0, current_matrix),
17291 MATRIX_ROW_VPOS (r1, current_matrix),
17292 Z_delta, Z_delta_bytes);
17295 /* Set the cursor. */
17296 row = row_containing_pos (w, PT, r0, NULL, 0);
17297 if (row)
17298 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17299 else
17300 abort ();
17301 return 1;
17305 /* Handle the case that changes are all below what is displayed in
17306 the window, and that PT is in the window. This shortcut cannot
17307 be taken if ZV is visible in the window, and text has been added
17308 there that is visible in the window. */
17309 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17310 /* ZV is not visible in the window, or there are no
17311 changes at ZV, actually. */
17312 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17313 || first_changed_charpos == last_changed_charpos))
17315 struct glyph_row *r0;
17317 /* Give up if PT is not in the window. Note that it already has
17318 been checked at the start of try_window_id that PT is not in
17319 front of the window start. */
17320 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17321 GIVE_UP (14);
17323 /* If window start is unchanged, we can reuse the whole matrix
17324 as is, without changing glyph positions since no text has
17325 been added/removed in front of the window end. */
17326 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17327 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17328 /* PT must not be in a partially visible line. */
17329 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17330 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17332 /* We have to compute the window end anew since text
17333 could have been added/removed after it. */
17334 WSET (w, window_end_pos,
17335 make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17336 w->window_end_bytepos
17337 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17339 /* Set the cursor. */
17340 row = row_containing_pos (w, PT, r0, NULL, 0);
17341 if (row)
17342 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17343 else
17344 abort ();
17345 return 2;
17349 /* Give up if window start is in the changed area.
17351 The condition used to read
17353 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17355 but why that was tested escapes me at the moment. */
17356 if (CHARPOS (start) >= first_changed_charpos
17357 && CHARPOS (start) <= last_changed_charpos)
17358 GIVE_UP (15);
17360 /* Check that window start agrees with the start of the first glyph
17361 row in its current matrix. Check this after we know the window
17362 start is not in changed text, otherwise positions would not be
17363 comparable. */
17364 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17365 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17366 GIVE_UP (16);
17368 /* Give up if the window ends in strings. Overlay strings
17369 at the end are difficult to handle, so don't try. */
17370 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17371 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17372 GIVE_UP (20);
17374 /* Compute the position at which we have to start displaying new
17375 lines. Some of the lines at the top of the window might be
17376 reusable because they are not displaying changed text. Find the
17377 last row in W's current matrix not affected by changes at the
17378 start of current_buffer. Value is null if changes start in the
17379 first line of window. */
17380 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17381 if (last_unchanged_at_beg_row)
17383 /* Avoid starting to display in the middle of a character, a TAB
17384 for instance. This is easier than to set up the iterator
17385 exactly, and it's not a frequent case, so the additional
17386 effort wouldn't really pay off. */
17387 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17388 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17389 && last_unchanged_at_beg_row > w->current_matrix->rows)
17390 --last_unchanged_at_beg_row;
17392 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17393 GIVE_UP (17);
17395 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17396 GIVE_UP (18);
17397 start_pos = it.current.pos;
17399 /* Start displaying new lines in the desired matrix at the same
17400 vpos we would use in the current matrix, i.e. below
17401 last_unchanged_at_beg_row. */
17402 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17403 current_matrix);
17404 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17405 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17407 eassert (it.hpos == 0 && it.current_x == 0);
17409 else
17411 /* There are no reusable lines at the start of the window.
17412 Start displaying in the first text line. */
17413 start_display (&it, w, start);
17414 it.vpos = it.first_vpos;
17415 start_pos = it.current.pos;
17418 /* Find the first row that is not affected by changes at the end of
17419 the buffer. Value will be null if there is no unchanged row, in
17420 which case we must redisplay to the end of the window. delta
17421 will be set to the value by which buffer positions beginning with
17422 first_unchanged_at_end_row have to be adjusted due to text
17423 changes. */
17424 first_unchanged_at_end_row
17425 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17426 IF_DEBUG (debug_delta = delta);
17427 IF_DEBUG (debug_delta_bytes = delta_bytes);
17429 /* Set stop_pos to the buffer position up to which we will have to
17430 display new lines. If first_unchanged_at_end_row != NULL, this
17431 is the buffer position of the start of the line displayed in that
17432 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17433 that we don't stop at a buffer position. */
17434 stop_pos = 0;
17435 if (first_unchanged_at_end_row)
17437 eassert (last_unchanged_at_beg_row == NULL
17438 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17440 /* If this is a continuation line, move forward to the next one
17441 that isn't. Changes in lines above affect this line.
17442 Caution: this may move first_unchanged_at_end_row to a row
17443 not displaying text. */
17444 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17445 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17446 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17447 < it.last_visible_y))
17448 ++first_unchanged_at_end_row;
17450 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17451 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17452 >= it.last_visible_y))
17453 first_unchanged_at_end_row = NULL;
17454 else
17456 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17457 + delta);
17458 first_unchanged_at_end_vpos
17459 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17460 eassert (stop_pos >= Z - END_UNCHANGED);
17463 else if (last_unchanged_at_beg_row == NULL)
17464 GIVE_UP (19);
17467 #ifdef GLYPH_DEBUG
17469 /* Either there is no unchanged row at the end, or the one we have
17470 now displays text. This is a necessary condition for the window
17471 end pos calculation at the end of this function. */
17472 eassert (first_unchanged_at_end_row == NULL
17473 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17475 debug_last_unchanged_at_beg_vpos
17476 = (last_unchanged_at_beg_row
17477 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17478 : -1);
17479 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17481 #endif /* GLYPH_DEBUG */
17484 /* Display new lines. Set last_text_row to the last new line
17485 displayed which has text on it, i.e. might end up as being the
17486 line where the window_end_vpos is. */
17487 w->cursor.vpos = -1;
17488 last_text_row = NULL;
17489 overlay_arrow_seen = 0;
17490 while (it.current_y < it.last_visible_y
17491 && !fonts_changed_p
17492 && (first_unchanged_at_end_row == NULL
17493 || IT_CHARPOS (it) < stop_pos))
17495 if (display_line (&it))
17496 last_text_row = it.glyph_row - 1;
17499 if (fonts_changed_p)
17500 return -1;
17503 /* Compute differences in buffer positions, y-positions etc. for
17504 lines reused at the bottom of the window. Compute what we can
17505 scroll. */
17506 if (first_unchanged_at_end_row
17507 /* No lines reused because we displayed everything up to the
17508 bottom of the window. */
17509 && it.current_y < it.last_visible_y)
17511 dvpos = (it.vpos
17512 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17513 current_matrix));
17514 dy = it.current_y - first_unchanged_at_end_row->y;
17515 run.current_y = first_unchanged_at_end_row->y;
17516 run.desired_y = run.current_y + dy;
17517 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17519 else
17521 delta = delta_bytes = dvpos = dy
17522 = run.current_y = run.desired_y = run.height = 0;
17523 first_unchanged_at_end_row = NULL;
17525 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17528 /* Find the cursor if not already found. We have to decide whether
17529 PT will appear on this window (it sometimes doesn't, but this is
17530 not a very frequent case.) This decision has to be made before
17531 the current matrix is altered. A value of cursor.vpos < 0 means
17532 that PT is either in one of the lines beginning at
17533 first_unchanged_at_end_row or below the window. Don't care for
17534 lines that might be displayed later at the window end; as
17535 mentioned, this is not a frequent case. */
17536 if (w->cursor.vpos < 0)
17538 /* Cursor in unchanged rows at the top? */
17539 if (PT < CHARPOS (start_pos)
17540 && last_unchanged_at_beg_row)
17542 row = row_containing_pos (w, PT,
17543 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17544 last_unchanged_at_beg_row + 1, 0);
17545 if (row)
17546 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17549 /* Start from first_unchanged_at_end_row looking for PT. */
17550 else if (first_unchanged_at_end_row)
17552 row = row_containing_pos (w, PT - delta,
17553 first_unchanged_at_end_row, NULL, 0);
17554 if (row)
17555 set_cursor_from_row (w, row, w->current_matrix, delta,
17556 delta_bytes, dy, dvpos);
17559 /* Give up if cursor was not found. */
17560 if (w->cursor.vpos < 0)
17562 clear_glyph_matrix (w->desired_matrix);
17563 return -1;
17567 /* Don't let the cursor end in the scroll margins. */
17569 int this_scroll_margin, cursor_height;
17571 this_scroll_margin =
17572 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17573 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17574 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17576 if ((w->cursor.y < this_scroll_margin
17577 && CHARPOS (start) > BEGV)
17578 /* Old redisplay didn't take scroll margin into account at the bottom,
17579 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17580 || (w->cursor.y + (make_cursor_line_fully_visible_p
17581 ? cursor_height + this_scroll_margin
17582 : 1)) > it.last_visible_y)
17584 w->cursor.vpos = -1;
17585 clear_glyph_matrix (w->desired_matrix);
17586 return -1;
17590 /* Scroll the display. Do it before changing the current matrix so
17591 that xterm.c doesn't get confused about where the cursor glyph is
17592 found. */
17593 if (dy && run.height)
17595 update_begin (f);
17597 if (FRAME_WINDOW_P (f))
17599 FRAME_RIF (f)->update_window_begin_hook (w);
17600 FRAME_RIF (f)->clear_window_mouse_face (w);
17601 FRAME_RIF (f)->scroll_run_hook (w, &run);
17602 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17604 else
17606 /* Terminal frame. In this case, dvpos gives the number of
17607 lines to scroll by; dvpos < 0 means scroll up. */
17608 int from_vpos
17609 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17610 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17611 int end = (WINDOW_TOP_EDGE_LINE (w)
17612 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17613 + window_internal_height (w));
17615 #if defined (HAVE_GPM) || defined (MSDOS)
17616 x_clear_window_mouse_face (w);
17617 #endif
17618 /* Perform the operation on the screen. */
17619 if (dvpos > 0)
17621 /* Scroll last_unchanged_at_beg_row to the end of the
17622 window down dvpos lines. */
17623 set_terminal_window (f, end);
17625 /* On dumb terminals delete dvpos lines at the end
17626 before inserting dvpos empty lines. */
17627 if (!FRAME_SCROLL_REGION_OK (f))
17628 ins_del_lines (f, end - dvpos, -dvpos);
17630 /* Insert dvpos empty lines in front of
17631 last_unchanged_at_beg_row. */
17632 ins_del_lines (f, from, dvpos);
17634 else if (dvpos < 0)
17636 /* Scroll up last_unchanged_at_beg_vpos to the end of
17637 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17638 set_terminal_window (f, end);
17640 /* Delete dvpos lines in front of
17641 last_unchanged_at_beg_vpos. ins_del_lines will set
17642 the cursor to the given vpos and emit |dvpos| delete
17643 line sequences. */
17644 ins_del_lines (f, from + dvpos, dvpos);
17646 /* On a dumb terminal insert dvpos empty lines at the
17647 end. */
17648 if (!FRAME_SCROLL_REGION_OK (f))
17649 ins_del_lines (f, end + dvpos, -dvpos);
17652 set_terminal_window (f, 0);
17655 update_end (f);
17658 /* Shift reused rows of the current matrix to the right position.
17659 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17660 text. */
17661 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17662 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17663 if (dvpos < 0)
17665 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17666 bottom_vpos, dvpos);
17667 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17668 bottom_vpos, 0);
17670 else if (dvpos > 0)
17672 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17673 bottom_vpos, dvpos);
17674 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17675 first_unchanged_at_end_vpos + dvpos, 0);
17678 /* For frame-based redisplay, make sure that current frame and window
17679 matrix are in sync with respect to glyph memory. */
17680 if (!FRAME_WINDOW_P (f))
17681 sync_frame_with_window_matrix_rows (w);
17683 /* Adjust buffer positions in reused rows. */
17684 if (delta || delta_bytes)
17685 increment_matrix_positions (current_matrix,
17686 first_unchanged_at_end_vpos + dvpos,
17687 bottom_vpos, delta, delta_bytes);
17689 /* Adjust Y positions. */
17690 if (dy)
17691 shift_glyph_matrix (w, current_matrix,
17692 first_unchanged_at_end_vpos + dvpos,
17693 bottom_vpos, dy);
17695 if (first_unchanged_at_end_row)
17697 first_unchanged_at_end_row += dvpos;
17698 if (first_unchanged_at_end_row->y >= it.last_visible_y
17699 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17700 first_unchanged_at_end_row = NULL;
17703 /* If scrolling up, there may be some lines to display at the end of
17704 the window. */
17705 last_text_row_at_end = NULL;
17706 if (dy < 0)
17708 /* Scrolling up can leave for example a partially visible line
17709 at the end of the window to be redisplayed. */
17710 /* Set last_row to the glyph row in the current matrix where the
17711 window end line is found. It has been moved up or down in
17712 the matrix by dvpos. */
17713 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17714 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17716 /* If last_row is the window end line, it should display text. */
17717 eassert (last_row->displays_text_p);
17719 /* If window end line was partially visible before, begin
17720 displaying at that line. Otherwise begin displaying with the
17721 line following it. */
17722 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17724 init_to_row_start (&it, w, last_row);
17725 it.vpos = last_vpos;
17726 it.current_y = last_row->y;
17728 else
17730 init_to_row_end (&it, w, last_row);
17731 it.vpos = 1 + last_vpos;
17732 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17733 ++last_row;
17736 /* We may start in a continuation line. If so, we have to
17737 get the right continuation_lines_width and current_x. */
17738 it.continuation_lines_width = last_row->continuation_lines_width;
17739 it.hpos = it.current_x = 0;
17741 /* Display the rest of the lines at the window end. */
17742 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17743 while (it.current_y < it.last_visible_y
17744 && !fonts_changed_p)
17746 /* Is it always sure that the display agrees with lines in
17747 the current matrix? I don't think so, so we mark rows
17748 displayed invalid in the current matrix by setting their
17749 enabled_p flag to zero. */
17750 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17751 if (display_line (&it))
17752 last_text_row_at_end = it.glyph_row - 1;
17756 /* Update window_end_pos and window_end_vpos. */
17757 if (first_unchanged_at_end_row
17758 && !last_text_row_at_end)
17760 /* Window end line if one of the preserved rows from the current
17761 matrix. Set row to the last row displaying text in current
17762 matrix starting at first_unchanged_at_end_row, after
17763 scrolling. */
17764 eassert (first_unchanged_at_end_row->displays_text_p);
17765 row = find_last_row_displaying_text (w->current_matrix, &it,
17766 first_unchanged_at_end_row);
17767 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17769 WSET (w, window_end_pos, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17770 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17771 WSET (w, window_end_vpos,
17772 make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17773 eassert (w->window_end_bytepos >= 0);
17774 IF_DEBUG (debug_method_add (w, "A"));
17776 else if (last_text_row_at_end)
17778 WSET (w, window_end_pos,
17779 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17780 w->window_end_bytepos
17781 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17782 WSET (w, window_end_vpos,
17783 make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix)));
17784 eassert (w->window_end_bytepos >= 0);
17785 IF_DEBUG (debug_method_add (w, "B"));
17787 else if (last_text_row)
17789 /* We have displayed either to the end of the window or at the
17790 end of the window, i.e. the last row with text is to be found
17791 in the desired matrix. */
17792 WSET (w, window_end_pos,
17793 make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17794 w->window_end_bytepos
17795 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17796 WSET (w, window_end_vpos,
17797 make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17798 eassert (w->window_end_bytepos >= 0);
17800 else if (first_unchanged_at_end_row == NULL
17801 && last_text_row == NULL
17802 && last_text_row_at_end == NULL)
17804 /* Displayed to end of window, but no line containing text was
17805 displayed. Lines were deleted at the end of the window. */
17806 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17807 int vpos = XFASTINT (w->window_end_vpos);
17808 struct glyph_row *current_row = current_matrix->rows + vpos;
17809 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17811 for (row = NULL;
17812 row == NULL && vpos >= first_vpos;
17813 --vpos, --current_row, --desired_row)
17815 if (desired_row->enabled_p)
17817 if (desired_row->displays_text_p)
17818 row = desired_row;
17820 else if (current_row->displays_text_p)
17821 row = current_row;
17824 eassert (row != NULL);
17825 WSET (w, window_end_vpos, make_number (vpos + 1));
17826 WSET (w, window_end_pos, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17827 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17828 eassert (w->window_end_bytepos >= 0);
17829 IF_DEBUG (debug_method_add (w, "C"));
17831 else
17832 abort ();
17834 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17835 debug_end_vpos = XFASTINT (w->window_end_vpos));
17837 /* Record that display has not been completed. */
17838 WSET (w, window_end_valid, Qnil);
17839 w->desired_matrix->no_scrolling_p = 1;
17840 return 3;
17842 #undef GIVE_UP
17847 /***********************************************************************
17848 More debugging support
17849 ***********************************************************************/
17851 #ifdef GLYPH_DEBUG
17853 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17854 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17855 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17858 /* Dump the contents of glyph matrix MATRIX on stderr.
17860 GLYPHS 0 means don't show glyph contents.
17861 GLYPHS 1 means show glyphs in short form
17862 GLYPHS > 1 means show glyphs in long form. */
17864 void
17865 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17867 int i;
17868 for (i = 0; i < matrix->nrows; ++i)
17869 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17873 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17874 the glyph row and area where the glyph comes from. */
17876 void
17877 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17879 if (glyph->type == CHAR_GLYPH)
17881 fprintf (stderr,
17882 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17883 glyph - row->glyphs[TEXT_AREA],
17884 'C',
17885 glyph->charpos,
17886 (BUFFERP (glyph->object)
17887 ? 'B'
17888 : (STRINGP (glyph->object)
17889 ? 'S'
17890 : '-')),
17891 glyph->pixel_width,
17892 glyph->u.ch,
17893 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17894 ? glyph->u.ch
17895 : '.'),
17896 glyph->face_id,
17897 glyph->left_box_line_p,
17898 glyph->right_box_line_p);
17900 else if (glyph->type == STRETCH_GLYPH)
17902 fprintf (stderr,
17903 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17904 glyph - row->glyphs[TEXT_AREA],
17905 'S',
17906 glyph->charpos,
17907 (BUFFERP (glyph->object)
17908 ? 'B'
17909 : (STRINGP (glyph->object)
17910 ? 'S'
17911 : '-')),
17912 glyph->pixel_width,
17914 '.',
17915 glyph->face_id,
17916 glyph->left_box_line_p,
17917 glyph->right_box_line_p);
17919 else if (glyph->type == IMAGE_GLYPH)
17921 fprintf (stderr,
17922 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17923 glyph - row->glyphs[TEXT_AREA],
17924 'I',
17925 glyph->charpos,
17926 (BUFFERP (glyph->object)
17927 ? 'B'
17928 : (STRINGP (glyph->object)
17929 ? 'S'
17930 : '-')),
17931 glyph->pixel_width,
17932 glyph->u.img_id,
17933 '.',
17934 glyph->face_id,
17935 glyph->left_box_line_p,
17936 glyph->right_box_line_p);
17938 else if (glyph->type == COMPOSITE_GLYPH)
17940 fprintf (stderr,
17941 " %5td %4c %6"pI"d %c %3d 0x%05x",
17942 glyph - row->glyphs[TEXT_AREA],
17943 '+',
17944 glyph->charpos,
17945 (BUFFERP (glyph->object)
17946 ? 'B'
17947 : (STRINGP (glyph->object)
17948 ? 'S'
17949 : '-')),
17950 glyph->pixel_width,
17951 glyph->u.cmp.id);
17952 if (glyph->u.cmp.automatic)
17953 fprintf (stderr,
17954 "[%d-%d]",
17955 glyph->slice.cmp.from, glyph->slice.cmp.to);
17956 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17957 glyph->face_id,
17958 glyph->left_box_line_p,
17959 glyph->right_box_line_p);
17964 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17965 GLYPHS 0 means don't show glyph contents.
17966 GLYPHS 1 means show glyphs in short form
17967 GLYPHS > 1 means show glyphs in long form. */
17969 void
17970 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17972 if (glyphs != 1)
17974 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17975 fprintf (stderr, "======================================================================\n");
17977 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17978 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17979 vpos,
17980 MATRIX_ROW_START_CHARPOS (row),
17981 MATRIX_ROW_END_CHARPOS (row),
17982 row->used[TEXT_AREA],
17983 row->contains_overlapping_glyphs_p,
17984 row->enabled_p,
17985 row->truncated_on_left_p,
17986 row->truncated_on_right_p,
17987 row->continued_p,
17988 MATRIX_ROW_CONTINUATION_LINE_P (row),
17989 row->displays_text_p,
17990 row->ends_at_zv_p,
17991 row->fill_line_p,
17992 row->ends_in_middle_of_char_p,
17993 row->starts_in_middle_of_char_p,
17994 row->mouse_face_p,
17995 row->x,
17996 row->y,
17997 row->pixel_width,
17998 row->height,
17999 row->visible_height,
18000 row->ascent,
18001 row->phys_ascent);
18002 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18003 row->end.overlay_string_index,
18004 row->continuation_lines_width);
18005 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18006 CHARPOS (row->start.string_pos),
18007 CHARPOS (row->end.string_pos));
18008 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18009 row->end.dpvec_index);
18012 if (glyphs > 1)
18014 int area;
18016 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18018 struct glyph *glyph = row->glyphs[area];
18019 struct glyph *glyph_end = glyph + row->used[area];
18021 /* Glyph for a line end in text. */
18022 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18023 ++glyph_end;
18025 if (glyph < glyph_end)
18026 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18028 for (; glyph < glyph_end; ++glyph)
18029 dump_glyph (row, glyph, area);
18032 else if (glyphs == 1)
18034 int area;
18036 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18038 char *s = alloca (row->used[area] + 1);
18039 int i;
18041 for (i = 0; i < row->used[area]; ++i)
18043 struct glyph *glyph = row->glyphs[area] + i;
18044 if (glyph->type == CHAR_GLYPH
18045 && glyph->u.ch < 0x80
18046 && glyph->u.ch >= ' ')
18047 s[i] = glyph->u.ch;
18048 else
18049 s[i] = '.';
18052 s[i] = '\0';
18053 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18059 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18060 Sdump_glyph_matrix, 0, 1, "p",
18061 doc: /* Dump the current matrix of the selected window to stderr.
18062 Shows contents of glyph row structures. With non-nil
18063 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18064 glyphs in short form, otherwise show glyphs in long form. */)
18065 (Lisp_Object glyphs)
18067 struct window *w = XWINDOW (selected_window);
18068 struct buffer *buffer = XBUFFER (w->buffer);
18070 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18071 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18072 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18073 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18074 fprintf (stderr, "=============================================\n");
18075 dump_glyph_matrix (w->current_matrix,
18076 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18077 return Qnil;
18081 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18082 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18083 (void)
18085 struct frame *f = XFRAME (selected_frame);
18086 dump_glyph_matrix (f->current_matrix, 1);
18087 return Qnil;
18091 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18092 doc: /* Dump glyph row ROW to stderr.
18093 GLYPH 0 means don't dump glyphs.
18094 GLYPH 1 means dump glyphs in short form.
18095 GLYPH > 1 or omitted means dump glyphs in long form. */)
18096 (Lisp_Object row, Lisp_Object glyphs)
18098 struct glyph_matrix *matrix;
18099 EMACS_INT vpos;
18101 CHECK_NUMBER (row);
18102 matrix = XWINDOW (selected_window)->current_matrix;
18103 vpos = XINT (row);
18104 if (vpos >= 0 && vpos < matrix->nrows)
18105 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18106 vpos,
18107 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18108 return Qnil;
18112 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18113 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18114 GLYPH 0 means don't dump glyphs.
18115 GLYPH 1 means dump glyphs in short form.
18116 GLYPH > 1 or omitted means dump glyphs in long form. */)
18117 (Lisp_Object row, Lisp_Object glyphs)
18119 struct frame *sf = SELECTED_FRAME ();
18120 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18121 EMACS_INT vpos;
18123 CHECK_NUMBER (row);
18124 vpos = XINT (row);
18125 if (vpos >= 0 && vpos < m->nrows)
18126 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18127 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18128 return Qnil;
18132 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18133 doc: /* Toggle tracing of redisplay.
18134 With ARG, turn tracing on if and only if ARG is positive. */)
18135 (Lisp_Object arg)
18137 if (NILP (arg))
18138 trace_redisplay_p = !trace_redisplay_p;
18139 else
18141 arg = Fprefix_numeric_value (arg);
18142 trace_redisplay_p = XINT (arg) > 0;
18145 return Qnil;
18149 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18150 doc: /* Like `format', but print result to stderr.
18151 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18152 (ptrdiff_t nargs, Lisp_Object *args)
18154 Lisp_Object s = Fformat (nargs, args);
18155 fprintf (stderr, "%s", SDATA (s));
18156 return Qnil;
18159 #endif /* GLYPH_DEBUG */
18163 /***********************************************************************
18164 Building Desired Matrix Rows
18165 ***********************************************************************/
18167 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18168 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18170 static struct glyph_row *
18171 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18173 struct frame *f = XFRAME (WINDOW_FRAME (w));
18174 struct buffer *buffer = XBUFFER (w->buffer);
18175 struct buffer *old = current_buffer;
18176 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18177 int arrow_len = SCHARS (overlay_arrow_string);
18178 const unsigned char *arrow_end = arrow_string + arrow_len;
18179 const unsigned char *p;
18180 struct it it;
18181 int multibyte_p;
18182 int n_glyphs_before;
18184 set_buffer_temp (buffer);
18185 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18186 it.glyph_row->used[TEXT_AREA] = 0;
18187 SET_TEXT_POS (it.position, 0, 0);
18189 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18190 p = arrow_string;
18191 while (p < arrow_end)
18193 Lisp_Object face, ilisp;
18195 /* Get the next character. */
18196 if (multibyte_p)
18197 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18198 else
18200 it.c = it.char_to_display = *p, it.len = 1;
18201 if (! ASCII_CHAR_P (it.c))
18202 it.char_to_display = BYTE8_TO_CHAR (it.c);
18204 p += it.len;
18206 /* Get its face. */
18207 ilisp = make_number (p - arrow_string);
18208 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18209 it.face_id = compute_char_face (f, it.char_to_display, face);
18211 /* Compute its width, get its glyphs. */
18212 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18213 SET_TEXT_POS (it.position, -1, -1);
18214 PRODUCE_GLYPHS (&it);
18216 /* If this character doesn't fit any more in the line, we have
18217 to remove some glyphs. */
18218 if (it.current_x > it.last_visible_x)
18220 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18221 break;
18225 set_buffer_temp (old);
18226 return it.glyph_row;
18230 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18231 glyphs to insert is determined by produce_special_glyphs. */
18233 static void
18234 insert_left_trunc_glyphs (struct it *it)
18236 struct it truncate_it;
18237 struct glyph *from, *end, *to, *toend;
18239 eassert (!FRAME_WINDOW_P (it->f)
18240 || (!it->glyph_row->reversed_p
18241 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18242 || (it->glyph_row->reversed_p
18243 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18245 /* Get the truncation glyphs. */
18246 truncate_it = *it;
18247 truncate_it.current_x = 0;
18248 truncate_it.face_id = DEFAULT_FACE_ID;
18249 truncate_it.glyph_row = &scratch_glyph_row;
18250 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18251 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18252 truncate_it.object = make_number (0);
18253 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18255 /* Overwrite glyphs from IT with truncation glyphs. */
18256 if (!it->glyph_row->reversed_p)
18258 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18260 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18261 end = from + tused;
18262 to = it->glyph_row->glyphs[TEXT_AREA];
18263 toend = to + it->glyph_row->used[TEXT_AREA];
18264 if (FRAME_WINDOW_P (it->f))
18266 /* On GUI frames, when variable-size fonts are displayed,
18267 the truncation glyphs may need more pixels than the row's
18268 glyphs they overwrite. We overwrite more glyphs to free
18269 enough screen real estate, and enlarge the stretch glyph
18270 on the right (see display_line), if there is one, to
18271 preserve the screen position of the truncation glyphs on
18272 the right. */
18273 int w = 0;
18274 struct glyph *g = to;
18275 short used;
18277 /* The first glyph could be partially visible, in which case
18278 it->glyph_row->x will be negative. But we want the left
18279 truncation glyphs to be aligned at the left margin of the
18280 window, so we override the x coordinate at which the row
18281 will begin. */
18282 it->glyph_row->x = 0;
18283 while (g < toend && w < it->truncation_pixel_width)
18285 w += g->pixel_width;
18286 ++g;
18288 if (g - to - tused > 0)
18290 memmove (to + tused, g, (toend - g) * sizeof(*g));
18291 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18293 used = it->glyph_row->used[TEXT_AREA];
18294 if (it->glyph_row->truncated_on_right_p
18295 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18296 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18297 == STRETCH_GLYPH)
18299 int extra = w - it->truncation_pixel_width;
18301 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18305 while (from < end)
18306 *to++ = *from++;
18308 /* There may be padding glyphs left over. Overwrite them too. */
18309 if (!FRAME_WINDOW_P (it->f))
18311 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18313 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18314 while (from < end)
18315 *to++ = *from++;
18319 if (to > toend)
18320 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18322 else
18324 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18326 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18327 that back to front. */
18328 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18329 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18330 toend = it->glyph_row->glyphs[TEXT_AREA];
18331 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18332 if (FRAME_WINDOW_P (it->f))
18334 int w = 0;
18335 struct glyph *g = to;
18337 while (g >= toend && w < it->truncation_pixel_width)
18339 w += g->pixel_width;
18340 --g;
18342 if (to - g - tused > 0)
18343 to = g + tused;
18344 if (it->glyph_row->truncated_on_right_p
18345 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18346 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18348 int extra = w - it->truncation_pixel_width;
18350 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18354 while (from >= end && to >= toend)
18355 *to-- = *from--;
18356 if (!FRAME_WINDOW_P (it->f))
18358 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18360 from =
18361 truncate_it.glyph_row->glyphs[TEXT_AREA]
18362 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18363 while (from >= end && to >= toend)
18364 *to-- = *from--;
18367 if (from >= end)
18369 /* Need to free some room before prepending additional
18370 glyphs. */
18371 int move_by = from - end + 1;
18372 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18373 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18375 for ( ; g >= g0; g--)
18376 g[move_by] = *g;
18377 while (from >= end)
18378 *to-- = *from--;
18379 it->glyph_row->used[TEXT_AREA] += move_by;
18384 /* Compute the hash code for ROW. */
18385 unsigned
18386 row_hash (struct glyph_row *row)
18388 int area, k;
18389 unsigned hashval = 0;
18391 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18392 for (k = 0; k < row->used[area]; ++k)
18393 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18394 + row->glyphs[area][k].u.val
18395 + row->glyphs[area][k].face_id
18396 + row->glyphs[area][k].padding_p
18397 + (row->glyphs[area][k].type << 2));
18399 return hashval;
18402 /* Compute the pixel height and width of IT->glyph_row.
18404 Most of the time, ascent and height of a display line will be equal
18405 to the max_ascent and max_height values of the display iterator
18406 structure. This is not the case if
18408 1. We hit ZV without displaying anything. In this case, max_ascent
18409 and max_height will be zero.
18411 2. We have some glyphs that don't contribute to the line height.
18412 (The glyph row flag contributes_to_line_height_p is for future
18413 pixmap extensions).
18415 The first case is easily covered by using default values because in
18416 these cases, the line height does not really matter, except that it
18417 must not be zero. */
18419 static void
18420 compute_line_metrics (struct it *it)
18422 struct glyph_row *row = it->glyph_row;
18424 if (FRAME_WINDOW_P (it->f))
18426 int i, min_y, max_y;
18428 /* The line may consist of one space only, that was added to
18429 place the cursor on it. If so, the row's height hasn't been
18430 computed yet. */
18431 if (row->height == 0)
18433 if (it->max_ascent + it->max_descent == 0)
18434 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18435 row->ascent = it->max_ascent;
18436 row->height = it->max_ascent + it->max_descent;
18437 row->phys_ascent = it->max_phys_ascent;
18438 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18439 row->extra_line_spacing = it->max_extra_line_spacing;
18442 /* Compute the width of this line. */
18443 row->pixel_width = row->x;
18444 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18445 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18447 eassert (row->pixel_width >= 0);
18448 eassert (row->ascent >= 0 && row->height > 0);
18450 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18451 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18453 /* If first line's physical ascent is larger than its logical
18454 ascent, use the physical ascent, and make the row taller.
18455 This makes accented characters fully visible. */
18456 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18457 && row->phys_ascent > row->ascent)
18459 row->height += row->phys_ascent - row->ascent;
18460 row->ascent = row->phys_ascent;
18463 /* Compute how much of the line is visible. */
18464 row->visible_height = row->height;
18466 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18467 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18469 if (row->y < min_y)
18470 row->visible_height -= min_y - row->y;
18471 if (row->y + row->height > max_y)
18472 row->visible_height -= row->y + row->height - max_y;
18474 else
18476 row->pixel_width = row->used[TEXT_AREA];
18477 if (row->continued_p)
18478 row->pixel_width -= it->continuation_pixel_width;
18479 else if (row->truncated_on_right_p)
18480 row->pixel_width -= it->truncation_pixel_width;
18481 row->ascent = row->phys_ascent = 0;
18482 row->height = row->phys_height = row->visible_height = 1;
18483 row->extra_line_spacing = 0;
18486 /* Compute a hash code for this row. */
18487 row->hash = row_hash (row);
18489 it->max_ascent = it->max_descent = 0;
18490 it->max_phys_ascent = it->max_phys_descent = 0;
18494 /* Append one space to the glyph row of iterator IT if doing a
18495 window-based redisplay. The space has the same face as
18496 IT->face_id. Value is non-zero if a space was added.
18498 This function is called to make sure that there is always one glyph
18499 at the end of a glyph row that the cursor can be set on under
18500 window-systems. (If there weren't such a glyph we would not know
18501 how wide and tall a box cursor should be displayed).
18503 At the same time this space let's a nicely handle clearing to the
18504 end of the line if the row ends in italic text. */
18506 static int
18507 append_space_for_newline (struct it *it, int default_face_p)
18509 if (FRAME_WINDOW_P (it->f))
18511 int n = it->glyph_row->used[TEXT_AREA];
18513 if (it->glyph_row->glyphs[TEXT_AREA] + n
18514 < it->glyph_row->glyphs[1 + TEXT_AREA])
18516 /* Save some values that must not be changed.
18517 Must save IT->c and IT->len because otherwise
18518 ITERATOR_AT_END_P wouldn't work anymore after
18519 append_space_for_newline has been called. */
18520 enum display_element_type saved_what = it->what;
18521 int saved_c = it->c, saved_len = it->len;
18522 int saved_char_to_display = it->char_to_display;
18523 int saved_x = it->current_x;
18524 int saved_face_id = it->face_id;
18525 struct text_pos saved_pos;
18526 Lisp_Object saved_object;
18527 struct face *face;
18529 saved_object = it->object;
18530 saved_pos = it->position;
18532 it->what = IT_CHARACTER;
18533 memset (&it->position, 0, sizeof it->position);
18534 it->object = make_number (0);
18535 it->c = it->char_to_display = ' ';
18536 it->len = 1;
18538 /* If the default face was remapped, be sure to use the
18539 remapped face for the appended newline. */
18540 if (default_face_p)
18541 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18542 else if (it->face_before_selective_p)
18543 it->face_id = it->saved_face_id;
18544 face = FACE_FROM_ID (it->f, it->face_id);
18545 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18547 PRODUCE_GLYPHS (it);
18549 it->override_ascent = -1;
18550 it->constrain_row_ascent_descent_p = 0;
18551 it->current_x = saved_x;
18552 it->object = saved_object;
18553 it->position = saved_pos;
18554 it->what = saved_what;
18555 it->face_id = saved_face_id;
18556 it->len = saved_len;
18557 it->c = saved_c;
18558 it->char_to_display = saved_char_to_display;
18559 return 1;
18563 return 0;
18567 /* Extend the face of the last glyph in the text area of IT->glyph_row
18568 to the end of the display line. Called from display_line. If the
18569 glyph row is empty, add a space glyph to it so that we know the
18570 face to draw. Set the glyph row flag fill_line_p. If the glyph
18571 row is R2L, prepend a stretch glyph to cover the empty space to the
18572 left of the leftmost glyph. */
18574 static void
18575 extend_face_to_end_of_line (struct it *it)
18577 struct face *face, *default_face;
18578 struct frame *f = it->f;
18580 /* If line is already filled, do nothing. Non window-system frames
18581 get a grace of one more ``pixel'' because their characters are
18582 1-``pixel'' wide, so they hit the equality too early. This grace
18583 is needed only for R2L rows that are not continued, to produce
18584 one extra blank where we could display the cursor. */
18585 if (it->current_x >= it->last_visible_x
18586 + (!FRAME_WINDOW_P (f)
18587 && it->glyph_row->reversed_p
18588 && !it->glyph_row->continued_p))
18589 return;
18591 /* The default face, possibly remapped. */
18592 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18594 /* Face extension extends the background and box of IT->face_id
18595 to the end of the line. If the background equals the background
18596 of the frame, we don't have to do anything. */
18597 if (it->face_before_selective_p)
18598 face = FACE_FROM_ID (f, it->saved_face_id);
18599 else
18600 face = FACE_FROM_ID (f, it->face_id);
18602 if (FRAME_WINDOW_P (f)
18603 && it->glyph_row->displays_text_p
18604 && face->box == FACE_NO_BOX
18605 && face->background == FRAME_BACKGROUND_PIXEL (f)
18606 && !face->stipple
18607 && !it->glyph_row->reversed_p)
18608 return;
18610 /* Set the glyph row flag indicating that the face of the last glyph
18611 in the text area has to be drawn to the end of the text area. */
18612 it->glyph_row->fill_line_p = 1;
18614 /* If current character of IT is not ASCII, make sure we have the
18615 ASCII face. This will be automatically undone the next time
18616 get_next_display_element returns a multibyte character. Note
18617 that the character will always be single byte in unibyte
18618 text. */
18619 if (!ASCII_CHAR_P (it->c))
18621 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18624 if (FRAME_WINDOW_P (f))
18626 /* If the row is empty, add a space with the current face of IT,
18627 so that we know which face to draw. */
18628 if (it->glyph_row->used[TEXT_AREA] == 0)
18630 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18631 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18632 it->glyph_row->used[TEXT_AREA] = 1;
18634 #ifdef HAVE_WINDOW_SYSTEM
18635 if (it->glyph_row->reversed_p)
18637 /* Prepend a stretch glyph to the row, such that the
18638 rightmost glyph will be drawn flushed all the way to the
18639 right margin of the window. The stretch glyph that will
18640 occupy the empty space, if any, to the left of the
18641 glyphs. */
18642 struct font *font = face->font ? face->font : FRAME_FONT (f);
18643 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18644 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18645 struct glyph *g;
18646 int row_width, stretch_ascent, stretch_width;
18647 struct text_pos saved_pos;
18648 int saved_face_id, saved_avoid_cursor;
18650 for (row_width = 0, g = row_start; g < row_end; g++)
18651 row_width += g->pixel_width;
18652 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18653 if (stretch_width > 0)
18655 stretch_ascent =
18656 (((it->ascent + it->descent)
18657 * FONT_BASE (font)) / FONT_HEIGHT (font));
18658 saved_pos = it->position;
18659 memset (&it->position, 0, sizeof it->position);
18660 saved_avoid_cursor = it->avoid_cursor_p;
18661 it->avoid_cursor_p = 1;
18662 saved_face_id = it->face_id;
18663 /* The last row's stretch glyph should get the default
18664 face, to avoid painting the rest of the window with
18665 the region face, if the region ends at ZV. */
18666 if (it->glyph_row->ends_at_zv_p)
18667 it->face_id = default_face->id;
18668 else
18669 it->face_id = face->id;
18670 append_stretch_glyph (it, make_number (0), stretch_width,
18671 it->ascent + it->descent, stretch_ascent);
18672 it->position = saved_pos;
18673 it->avoid_cursor_p = saved_avoid_cursor;
18674 it->face_id = saved_face_id;
18677 #endif /* HAVE_WINDOW_SYSTEM */
18679 else
18681 /* Save some values that must not be changed. */
18682 int saved_x = it->current_x;
18683 struct text_pos saved_pos;
18684 Lisp_Object saved_object;
18685 enum display_element_type saved_what = it->what;
18686 int saved_face_id = it->face_id;
18688 saved_object = it->object;
18689 saved_pos = it->position;
18691 it->what = IT_CHARACTER;
18692 memset (&it->position, 0, sizeof it->position);
18693 it->object = make_number (0);
18694 it->c = it->char_to_display = ' ';
18695 it->len = 1;
18696 /* The last row's blank glyphs should get the default face, to
18697 avoid painting the rest of the window with the region face,
18698 if the region ends at ZV. */
18699 if (it->glyph_row->ends_at_zv_p)
18700 it->face_id = default_face->id;
18701 else
18702 it->face_id = face->id;
18704 PRODUCE_GLYPHS (it);
18706 while (it->current_x <= it->last_visible_x)
18707 PRODUCE_GLYPHS (it);
18709 /* Don't count these blanks really. It would let us insert a left
18710 truncation glyph below and make us set the cursor on them, maybe. */
18711 it->current_x = saved_x;
18712 it->object = saved_object;
18713 it->position = saved_pos;
18714 it->what = saved_what;
18715 it->face_id = saved_face_id;
18720 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18721 trailing whitespace. */
18723 static int
18724 trailing_whitespace_p (ptrdiff_t charpos)
18726 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18727 int c = 0;
18729 while (bytepos < ZV_BYTE
18730 && (c = FETCH_CHAR (bytepos),
18731 c == ' ' || c == '\t'))
18732 ++bytepos;
18734 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18736 if (bytepos != PT_BYTE)
18737 return 1;
18739 return 0;
18743 /* Highlight trailing whitespace, if any, in ROW. */
18745 static void
18746 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18748 int used = row->used[TEXT_AREA];
18750 if (used)
18752 struct glyph *start = row->glyphs[TEXT_AREA];
18753 struct glyph *glyph = start + used - 1;
18755 if (row->reversed_p)
18757 /* Right-to-left rows need to be processed in the opposite
18758 direction, so swap the edge pointers. */
18759 glyph = start;
18760 start = row->glyphs[TEXT_AREA] + used - 1;
18763 /* Skip over glyphs inserted to display the cursor at the
18764 end of a line, for extending the face of the last glyph
18765 to the end of the line on terminals, and for truncation
18766 and continuation glyphs. */
18767 if (!row->reversed_p)
18769 while (glyph >= start
18770 && glyph->type == CHAR_GLYPH
18771 && INTEGERP (glyph->object))
18772 --glyph;
18774 else
18776 while (glyph <= start
18777 && glyph->type == CHAR_GLYPH
18778 && INTEGERP (glyph->object))
18779 ++glyph;
18782 /* If last glyph is a space or stretch, and it's trailing
18783 whitespace, set the face of all trailing whitespace glyphs in
18784 IT->glyph_row to `trailing-whitespace'. */
18785 if ((row->reversed_p ? glyph <= start : glyph >= start)
18786 && BUFFERP (glyph->object)
18787 && (glyph->type == STRETCH_GLYPH
18788 || (glyph->type == CHAR_GLYPH
18789 && glyph->u.ch == ' '))
18790 && trailing_whitespace_p (glyph->charpos))
18792 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18793 if (face_id < 0)
18794 return;
18796 if (!row->reversed_p)
18798 while (glyph >= start
18799 && BUFFERP (glyph->object)
18800 && (glyph->type == STRETCH_GLYPH
18801 || (glyph->type == CHAR_GLYPH
18802 && glyph->u.ch == ' ')))
18803 (glyph--)->face_id = face_id;
18805 else
18807 while (glyph <= start
18808 && BUFFERP (glyph->object)
18809 && (glyph->type == STRETCH_GLYPH
18810 || (glyph->type == CHAR_GLYPH
18811 && glyph->u.ch == ' ')))
18812 (glyph++)->face_id = face_id;
18819 /* Value is non-zero if glyph row ROW should be
18820 used to hold the cursor. */
18822 static int
18823 cursor_row_p (struct glyph_row *row)
18825 int result = 1;
18827 if (PT == CHARPOS (row->end.pos)
18828 || PT == MATRIX_ROW_END_CHARPOS (row))
18830 /* Suppose the row ends on a string.
18831 Unless the row is continued, that means it ends on a newline
18832 in the string. If it's anything other than a display string
18833 (e.g., a before-string from an overlay), we don't want the
18834 cursor there. (This heuristic seems to give the optimal
18835 behavior for the various types of multi-line strings.)
18836 One exception: if the string has `cursor' property on one of
18837 its characters, we _do_ want the cursor there. */
18838 if (CHARPOS (row->end.string_pos) >= 0)
18840 if (row->continued_p)
18841 result = 1;
18842 else
18844 /* Check for `display' property. */
18845 struct glyph *beg = row->glyphs[TEXT_AREA];
18846 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18847 struct glyph *glyph;
18849 result = 0;
18850 for (glyph = end; glyph >= beg; --glyph)
18851 if (STRINGP (glyph->object))
18853 Lisp_Object prop
18854 = Fget_char_property (make_number (PT),
18855 Qdisplay, Qnil);
18856 result =
18857 (!NILP (prop)
18858 && display_prop_string_p (prop, glyph->object));
18859 /* If there's a `cursor' property on one of the
18860 string's characters, this row is a cursor row,
18861 even though this is not a display string. */
18862 if (!result)
18864 Lisp_Object s = glyph->object;
18866 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18868 ptrdiff_t gpos = glyph->charpos;
18870 if (!NILP (Fget_char_property (make_number (gpos),
18871 Qcursor, s)))
18873 result = 1;
18874 break;
18878 break;
18882 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18884 /* If the row ends in middle of a real character,
18885 and the line is continued, we want the cursor here.
18886 That's because CHARPOS (ROW->end.pos) would equal
18887 PT if PT is before the character. */
18888 if (!row->ends_in_ellipsis_p)
18889 result = row->continued_p;
18890 else
18891 /* If the row ends in an ellipsis, then
18892 CHARPOS (ROW->end.pos) will equal point after the
18893 invisible text. We want that position to be displayed
18894 after the ellipsis. */
18895 result = 0;
18897 /* If the row ends at ZV, display the cursor at the end of that
18898 row instead of at the start of the row below. */
18899 else if (row->ends_at_zv_p)
18900 result = 1;
18901 else
18902 result = 0;
18905 return result;
18910 /* Push the property PROP so that it will be rendered at the current
18911 position in IT. Return 1 if PROP was successfully pushed, 0
18912 otherwise. Called from handle_line_prefix to handle the
18913 `line-prefix' and `wrap-prefix' properties. */
18915 static int
18916 push_prefix_prop (struct it *it, Lisp_Object prop)
18918 struct text_pos pos =
18919 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18921 eassert (it->method == GET_FROM_BUFFER
18922 || it->method == GET_FROM_DISPLAY_VECTOR
18923 || it->method == GET_FROM_STRING);
18925 /* We need to save the current buffer/string position, so it will be
18926 restored by pop_it, because iterate_out_of_display_property
18927 depends on that being set correctly, but some situations leave
18928 it->position not yet set when this function is called. */
18929 push_it (it, &pos);
18931 if (STRINGP (prop))
18933 if (SCHARS (prop) == 0)
18935 pop_it (it);
18936 return 0;
18939 it->string = prop;
18940 it->string_from_prefix_prop_p = 1;
18941 it->multibyte_p = STRING_MULTIBYTE (it->string);
18942 it->current.overlay_string_index = -1;
18943 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18944 it->end_charpos = it->string_nchars = SCHARS (it->string);
18945 it->method = GET_FROM_STRING;
18946 it->stop_charpos = 0;
18947 it->prev_stop = 0;
18948 it->base_level_stop = 0;
18950 /* Force paragraph direction to be that of the parent
18951 buffer/string. */
18952 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18953 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18954 else
18955 it->paragraph_embedding = L2R;
18957 /* Set up the bidi iterator for this display string. */
18958 if (it->bidi_p)
18960 it->bidi_it.string.lstring = it->string;
18961 it->bidi_it.string.s = NULL;
18962 it->bidi_it.string.schars = it->end_charpos;
18963 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18964 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18965 it->bidi_it.string.unibyte = !it->multibyte_p;
18966 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18969 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18971 it->method = GET_FROM_STRETCH;
18972 it->object = prop;
18974 #ifdef HAVE_WINDOW_SYSTEM
18975 else if (IMAGEP (prop))
18977 it->what = IT_IMAGE;
18978 it->image_id = lookup_image (it->f, prop);
18979 it->method = GET_FROM_IMAGE;
18981 #endif /* HAVE_WINDOW_SYSTEM */
18982 else
18984 pop_it (it); /* bogus display property, give up */
18985 return 0;
18988 return 1;
18991 /* Return the character-property PROP at the current position in IT. */
18993 static Lisp_Object
18994 get_it_property (struct it *it, Lisp_Object prop)
18996 Lisp_Object position;
18998 if (STRINGP (it->object))
18999 position = make_number (IT_STRING_CHARPOS (*it));
19000 else if (BUFFERP (it->object))
19001 position = make_number (IT_CHARPOS (*it));
19002 else
19003 return Qnil;
19005 return Fget_char_property (position, prop, it->object);
19008 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19010 static void
19011 handle_line_prefix (struct it *it)
19013 Lisp_Object prefix;
19015 if (it->continuation_lines_width > 0)
19017 prefix = get_it_property (it, Qwrap_prefix);
19018 if (NILP (prefix))
19019 prefix = Vwrap_prefix;
19021 else
19023 prefix = get_it_property (it, Qline_prefix);
19024 if (NILP (prefix))
19025 prefix = Vline_prefix;
19027 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19029 /* If the prefix is wider than the window, and we try to wrap
19030 it, it would acquire its own wrap prefix, and so on till the
19031 iterator stack overflows. So, don't wrap the prefix. */
19032 it->line_wrap = TRUNCATE;
19033 it->avoid_cursor_p = 1;
19039 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19040 only for R2L lines from display_line and display_string, when they
19041 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19042 the line/string needs to be continued on the next glyph row. */
19043 static void
19044 unproduce_glyphs (struct it *it, int n)
19046 struct glyph *glyph, *end;
19048 eassert (it->glyph_row);
19049 eassert (it->glyph_row->reversed_p);
19050 eassert (it->area == TEXT_AREA);
19051 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19053 if (n > it->glyph_row->used[TEXT_AREA])
19054 n = it->glyph_row->used[TEXT_AREA];
19055 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19056 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19057 for ( ; glyph < end; glyph++)
19058 glyph[-n] = *glyph;
19061 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19062 and ROW->maxpos. */
19063 static void
19064 find_row_edges (struct it *it, struct glyph_row *row,
19065 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19066 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19068 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19069 lines' rows is implemented for bidi-reordered rows. */
19071 /* ROW->minpos is the value of min_pos, the minimal buffer position
19072 we have in ROW, or ROW->start.pos if that is smaller. */
19073 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19074 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19075 else
19076 /* We didn't find buffer positions smaller than ROW->start, or
19077 didn't find _any_ valid buffer positions in any of the glyphs,
19078 so we must trust the iterator's computed positions. */
19079 row->minpos = row->start.pos;
19080 if (max_pos <= 0)
19082 max_pos = CHARPOS (it->current.pos);
19083 max_bpos = BYTEPOS (it->current.pos);
19086 /* Here are the various use-cases for ending the row, and the
19087 corresponding values for ROW->maxpos:
19089 Line ends in a newline from buffer eol_pos + 1
19090 Line is continued from buffer max_pos + 1
19091 Line is truncated on right it->current.pos
19092 Line ends in a newline from string max_pos + 1(*)
19093 (*) + 1 only when line ends in a forward scan
19094 Line is continued from string max_pos
19095 Line is continued from display vector max_pos
19096 Line is entirely from a string min_pos == max_pos
19097 Line is entirely from a display vector min_pos == max_pos
19098 Line that ends at ZV ZV
19100 If you discover other use-cases, please add them here as
19101 appropriate. */
19102 if (row->ends_at_zv_p)
19103 row->maxpos = it->current.pos;
19104 else if (row->used[TEXT_AREA])
19106 int seen_this_string = 0;
19107 struct glyph_row *r1 = row - 1;
19109 /* Did we see the same display string on the previous row? */
19110 if (STRINGP (it->object)
19111 /* this is not the first row */
19112 && row > it->w->desired_matrix->rows
19113 /* previous row is not the header line */
19114 && !r1->mode_line_p
19115 /* previous row also ends in a newline from a string */
19116 && r1->ends_in_newline_from_string_p)
19118 struct glyph *start, *end;
19120 /* Search for the last glyph of the previous row that came
19121 from buffer or string. Depending on whether the row is
19122 L2R or R2L, we need to process it front to back or the
19123 other way round. */
19124 if (!r1->reversed_p)
19126 start = r1->glyphs[TEXT_AREA];
19127 end = start + r1->used[TEXT_AREA];
19128 /* Glyphs inserted by redisplay have an integer (zero)
19129 as their object. */
19130 while (end > start
19131 && INTEGERP ((end - 1)->object)
19132 && (end - 1)->charpos <= 0)
19133 --end;
19134 if (end > start)
19136 if (EQ ((end - 1)->object, it->object))
19137 seen_this_string = 1;
19139 else
19140 /* If all the glyphs of the previous row were inserted
19141 by redisplay, it means the previous row was
19142 produced from a single newline, which is only
19143 possible if that newline came from the same string
19144 as the one which produced this ROW. */
19145 seen_this_string = 1;
19147 else
19149 end = r1->glyphs[TEXT_AREA] - 1;
19150 start = end + r1->used[TEXT_AREA];
19151 while (end < start
19152 && INTEGERP ((end + 1)->object)
19153 && (end + 1)->charpos <= 0)
19154 ++end;
19155 if (end < start)
19157 if (EQ ((end + 1)->object, it->object))
19158 seen_this_string = 1;
19160 else
19161 seen_this_string = 1;
19164 /* Take note of each display string that covers a newline only
19165 once, the first time we see it. This is for when a display
19166 string includes more than one newline in it. */
19167 if (row->ends_in_newline_from_string_p && !seen_this_string)
19169 /* If we were scanning the buffer forward when we displayed
19170 the string, we want to account for at least one buffer
19171 position that belongs to this row (position covered by
19172 the display string), so that cursor positioning will
19173 consider this row as a candidate when point is at the end
19174 of the visual line represented by this row. This is not
19175 required when scanning back, because max_pos will already
19176 have a much larger value. */
19177 if (CHARPOS (row->end.pos) > max_pos)
19178 INC_BOTH (max_pos, max_bpos);
19179 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19181 else if (CHARPOS (it->eol_pos) > 0)
19182 SET_TEXT_POS (row->maxpos,
19183 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19184 else if (row->continued_p)
19186 /* If max_pos is different from IT's current position, it
19187 means IT->method does not belong to the display element
19188 at max_pos. However, it also means that the display
19189 element at max_pos was displayed in its entirety on this
19190 line, which is equivalent to saying that the next line
19191 starts at the next buffer position. */
19192 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19193 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19194 else
19196 INC_BOTH (max_pos, max_bpos);
19197 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19200 else if (row->truncated_on_right_p)
19201 /* display_line already called reseat_at_next_visible_line_start,
19202 which puts the iterator at the beginning of the next line, in
19203 the logical order. */
19204 row->maxpos = it->current.pos;
19205 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19206 /* A line that is entirely from a string/image/stretch... */
19207 row->maxpos = row->minpos;
19208 else
19209 abort ();
19211 else
19212 row->maxpos = it->current.pos;
19215 /* Construct the glyph row IT->glyph_row in the desired matrix of
19216 IT->w from text at the current position of IT. See dispextern.h
19217 for an overview of struct it. Value is non-zero if
19218 IT->glyph_row displays text, as opposed to a line displaying ZV
19219 only. */
19221 static int
19222 display_line (struct it *it)
19224 struct glyph_row *row = it->glyph_row;
19225 Lisp_Object overlay_arrow_string;
19226 struct it wrap_it;
19227 void *wrap_data = NULL;
19228 int may_wrap = 0, wrap_x IF_LINT (= 0);
19229 int wrap_row_used = -1;
19230 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19231 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19232 int wrap_row_extra_line_spacing IF_LINT (= 0);
19233 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19234 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19235 int cvpos;
19236 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19237 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19239 /* We always start displaying at hpos zero even if hscrolled. */
19240 eassert (it->hpos == 0 && it->current_x == 0);
19242 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19243 >= it->w->desired_matrix->nrows)
19245 it->w->nrows_scale_factor++;
19246 fonts_changed_p = 1;
19247 return 0;
19250 /* Is IT->w showing the region? */
19251 WSET (it->w, region_showing, it->region_beg_charpos > 0 ? Qt : Qnil);
19253 /* Clear the result glyph row and enable it. */
19254 prepare_desired_row (row);
19256 row->y = it->current_y;
19257 row->start = it->start;
19258 row->continuation_lines_width = it->continuation_lines_width;
19259 row->displays_text_p = 1;
19260 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19261 it->starts_in_middle_of_char_p = 0;
19263 /* Arrange the overlays nicely for our purposes. Usually, we call
19264 display_line on only one line at a time, in which case this
19265 can't really hurt too much, or we call it on lines which appear
19266 one after another in the buffer, in which case all calls to
19267 recenter_overlay_lists but the first will be pretty cheap. */
19268 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19270 /* Move over display elements that are not visible because we are
19271 hscrolled. This may stop at an x-position < IT->first_visible_x
19272 if the first glyph is partially visible or if we hit a line end. */
19273 if (it->current_x < it->first_visible_x)
19275 enum move_it_result move_result;
19277 this_line_min_pos = row->start.pos;
19278 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19279 MOVE_TO_POS | MOVE_TO_X);
19280 /* If we are under a large hscroll, move_it_in_display_line_to
19281 could hit the end of the line without reaching
19282 it->first_visible_x. Pretend that we did reach it. This is
19283 especially important on a TTY, where we will call
19284 extend_face_to_end_of_line, which needs to know how many
19285 blank glyphs to produce. */
19286 if (it->current_x < it->first_visible_x
19287 && (move_result == MOVE_NEWLINE_OR_CR
19288 || move_result == MOVE_POS_MATCH_OR_ZV))
19289 it->current_x = it->first_visible_x;
19291 /* Record the smallest positions seen while we moved over
19292 display elements that are not visible. This is needed by
19293 redisplay_internal for optimizing the case where the cursor
19294 stays inside the same line. The rest of this function only
19295 considers positions that are actually displayed, so
19296 RECORD_MAX_MIN_POS will not otherwise record positions that
19297 are hscrolled to the left of the left edge of the window. */
19298 min_pos = CHARPOS (this_line_min_pos);
19299 min_bpos = BYTEPOS (this_line_min_pos);
19301 else
19303 /* We only do this when not calling `move_it_in_display_line_to'
19304 above, because move_it_in_display_line_to calls
19305 handle_line_prefix itself. */
19306 handle_line_prefix (it);
19309 /* Get the initial row height. This is either the height of the
19310 text hscrolled, if there is any, or zero. */
19311 row->ascent = it->max_ascent;
19312 row->height = it->max_ascent + it->max_descent;
19313 row->phys_ascent = it->max_phys_ascent;
19314 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19315 row->extra_line_spacing = it->max_extra_line_spacing;
19317 /* Utility macro to record max and min buffer positions seen until now. */
19318 #define RECORD_MAX_MIN_POS(IT) \
19319 do \
19321 int composition_p = !STRINGP ((IT)->string) \
19322 && ((IT)->what == IT_COMPOSITION); \
19323 ptrdiff_t current_pos = \
19324 composition_p ? (IT)->cmp_it.charpos \
19325 : IT_CHARPOS (*(IT)); \
19326 ptrdiff_t current_bpos = \
19327 composition_p ? CHAR_TO_BYTE (current_pos) \
19328 : IT_BYTEPOS (*(IT)); \
19329 if (current_pos < min_pos) \
19331 min_pos = current_pos; \
19332 min_bpos = current_bpos; \
19334 if (IT_CHARPOS (*it) > max_pos) \
19336 max_pos = IT_CHARPOS (*it); \
19337 max_bpos = IT_BYTEPOS (*it); \
19340 while (0)
19342 /* Loop generating characters. The loop is left with IT on the next
19343 character to display. */
19344 while (1)
19346 int n_glyphs_before, hpos_before, x_before;
19347 int x, nglyphs;
19348 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19350 /* Retrieve the next thing to display. Value is zero if end of
19351 buffer reached. */
19352 if (!get_next_display_element (it))
19354 /* Maybe add a space at the end of this line that is used to
19355 display the cursor there under X. Set the charpos of the
19356 first glyph of blank lines not corresponding to any text
19357 to -1. */
19358 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19359 row->exact_window_width_line_p = 1;
19360 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19361 || row->used[TEXT_AREA] == 0)
19363 row->glyphs[TEXT_AREA]->charpos = -1;
19364 row->displays_text_p = 0;
19366 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19367 && (!MINI_WINDOW_P (it->w)
19368 || (minibuf_level && EQ (it->window, minibuf_window))))
19369 row->indicate_empty_line_p = 1;
19372 it->continuation_lines_width = 0;
19373 row->ends_at_zv_p = 1;
19374 /* A row that displays right-to-left text must always have
19375 its last face extended all the way to the end of line,
19376 even if this row ends in ZV, because we still write to
19377 the screen left to right. We also need to extend the
19378 last face if the default face is remapped to some
19379 different face, otherwise the functions that clear
19380 portions of the screen will clear with the default face's
19381 background color. */
19382 if (row->reversed_p
19383 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19384 extend_face_to_end_of_line (it);
19385 break;
19388 /* Now, get the metrics of what we want to display. This also
19389 generates glyphs in `row' (which is IT->glyph_row). */
19390 n_glyphs_before = row->used[TEXT_AREA];
19391 x = it->current_x;
19393 /* Remember the line height so far in case the next element doesn't
19394 fit on the line. */
19395 if (it->line_wrap != TRUNCATE)
19397 ascent = it->max_ascent;
19398 descent = it->max_descent;
19399 phys_ascent = it->max_phys_ascent;
19400 phys_descent = it->max_phys_descent;
19402 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19404 if (IT_DISPLAYING_WHITESPACE (it))
19405 may_wrap = 1;
19406 else if (may_wrap)
19408 SAVE_IT (wrap_it, *it, wrap_data);
19409 wrap_x = x;
19410 wrap_row_used = row->used[TEXT_AREA];
19411 wrap_row_ascent = row->ascent;
19412 wrap_row_height = row->height;
19413 wrap_row_phys_ascent = row->phys_ascent;
19414 wrap_row_phys_height = row->phys_height;
19415 wrap_row_extra_line_spacing = row->extra_line_spacing;
19416 wrap_row_min_pos = min_pos;
19417 wrap_row_min_bpos = min_bpos;
19418 wrap_row_max_pos = max_pos;
19419 wrap_row_max_bpos = max_bpos;
19420 may_wrap = 0;
19425 PRODUCE_GLYPHS (it);
19427 /* If this display element was in marginal areas, continue with
19428 the next one. */
19429 if (it->area != TEXT_AREA)
19431 row->ascent = max (row->ascent, it->max_ascent);
19432 row->height = max (row->height, it->max_ascent + it->max_descent);
19433 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19434 row->phys_height = max (row->phys_height,
19435 it->max_phys_ascent + it->max_phys_descent);
19436 row->extra_line_spacing = max (row->extra_line_spacing,
19437 it->max_extra_line_spacing);
19438 set_iterator_to_next (it, 1);
19439 continue;
19442 /* Does the display element fit on the line? If we truncate
19443 lines, we should draw past the right edge of the window. If
19444 we don't truncate, we want to stop so that we can display the
19445 continuation glyph before the right margin. If lines are
19446 continued, there are two possible strategies for characters
19447 resulting in more than 1 glyph (e.g. tabs): Display as many
19448 glyphs as possible in this line and leave the rest for the
19449 continuation line, or display the whole element in the next
19450 line. Original redisplay did the former, so we do it also. */
19451 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19452 hpos_before = it->hpos;
19453 x_before = x;
19455 if (/* Not a newline. */
19456 nglyphs > 0
19457 /* Glyphs produced fit entirely in the line. */
19458 && it->current_x < it->last_visible_x)
19460 it->hpos += nglyphs;
19461 row->ascent = max (row->ascent, it->max_ascent);
19462 row->height = max (row->height, it->max_ascent + it->max_descent);
19463 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19464 row->phys_height = max (row->phys_height,
19465 it->max_phys_ascent + it->max_phys_descent);
19466 row->extra_line_spacing = max (row->extra_line_spacing,
19467 it->max_extra_line_spacing);
19468 if (it->current_x - it->pixel_width < it->first_visible_x)
19469 row->x = x - it->first_visible_x;
19470 /* Record the maximum and minimum buffer positions seen so
19471 far in glyphs that will be displayed by this row. */
19472 if (it->bidi_p)
19473 RECORD_MAX_MIN_POS (it);
19475 else
19477 int i, new_x;
19478 struct glyph *glyph;
19480 for (i = 0; i < nglyphs; ++i, x = new_x)
19482 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19483 new_x = x + glyph->pixel_width;
19485 if (/* Lines are continued. */
19486 it->line_wrap != TRUNCATE
19487 && (/* Glyph doesn't fit on the line. */
19488 new_x > it->last_visible_x
19489 /* Or it fits exactly on a window system frame. */
19490 || (new_x == it->last_visible_x
19491 && FRAME_WINDOW_P (it->f)
19492 && (row->reversed_p
19493 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19494 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19496 /* End of a continued line. */
19498 if (it->hpos == 0
19499 || (new_x == it->last_visible_x
19500 && FRAME_WINDOW_P (it->f)
19501 && (row->reversed_p
19502 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19503 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19505 /* Current glyph is the only one on the line or
19506 fits exactly on the line. We must continue
19507 the line because we can't draw the cursor
19508 after the glyph. */
19509 row->continued_p = 1;
19510 it->current_x = new_x;
19511 it->continuation_lines_width += new_x;
19512 ++it->hpos;
19513 if (i == nglyphs - 1)
19515 /* If line-wrap is on, check if a previous
19516 wrap point was found. */
19517 if (wrap_row_used > 0
19518 /* Even if there is a previous wrap
19519 point, continue the line here as
19520 usual, if (i) the previous character
19521 was a space or tab AND (ii) the
19522 current character is not. */
19523 && (!may_wrap
19524 || IT_DISPLAYING_WHITESPACE (it)))
19525 goto back_to_wrap;
19527 /* Record the maximum and minimum buffer
19528 positions seen so far in glyphs that will be
19529 displayed by this row. */
19530 if (it->bidi_p)
19531 RECORD_MAX_MIN_POS (it);
19532 set_iterator_to_next (it, 1);
19533 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19535 if (!get_next_display_element (it))
19537 row->exact_window_width_line_p = 1;
19538 it->continuation_lines_width = 0;
19539 row->continued_p = 0;
19540 row->ends_at_zv_p = 1;
19542 else if (ITERATOR_AT_END_OF_LINE_P (it))
19544 row->continued_p = 0;
19545 row->exact_window_width_line_p = 1;
19549 else if (it->bidi_p)
19550 RECORD_MAX_MIN_POS (it);
19552 else if (CHAR_GLYPH_PADDING_P (*glyph)
19553 && !FRAME_WINDOW_P (it->f))
19555 /* A padding glyph that doesn't fit on this line.
19556 This means the whole character doesn't fit
19557 on the line. */
19558 if (row->reversed_p)
19559 unproduce_glyphs (it, row->used[TEXT_AREA]
19560 - n_glyphs_before);
19561 row->used[TEXT_AREA] = n_glyphs_before;
19563 /* Fill the rest of the row with continuation
19564 glyphs like in 20.x. */
19565 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19566 < row->glyphs[1 + TEXT_AREA])
19567 produce_special_glyphs (it, IT_CONTINUATION);
19569 row->continued_p = 1;
19570 it->current_x = x_before;
19571 it->continuation_lines_width += x_before;
19573 /* Restore the height to what it was before the
19574 element not fitting on the line. */
19575 it->max_ascent = ascent;
19576 it->max_descent = descent;
19577 it->max_phys_ascent = phys_ascent;
19578 it->max_phys_descent = phys_descent;
19580 else if (wrap_row_used > 0)
19582 back_to_wrap:
19583 if (row->reversed_p)
19584 unproduce_glyphs (it,
19585 row->used[TEXT_AREA] - wrap_row_used);
19586 RESTORE_IT (it, &wrap_it, wrap_data);
19587 it->continuation_lines_width += wrap_x;
19588 row->used[TEXT_AREA] = wrap_row_used;
19589 row->ascent = wrap_row_ascent;
19590 row->height = wrap_row_height;
19591 row->phys_ascent = wrap_row_phys_ascent;
19592 row->phys_height = wrap_row_phys_height;
19593 row->extra_line_spacing = wrap_row_extra_line_spacing;
19594 min_pos = wrap_row_min_pos;
19595 min_bpos = wrap_row_min_bpos;
19596 max_pos = wrap_row_max_pos;
19597 max_bpos = wrap_row_max_bpos;
19598 row->continued_p = 1;
19599 row->ends_at_zv_p = 0;
19600 row->exact_window_width_line_p = 0;
19601 it->continuation_lines_width += x;
19603 /* Make sure that a non-default face is extended
19604 up to the right margin of the window. */
19605 extend_face_to_end_of_line (it);
19607 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19609 /* A TAB that extends past the right edge of the
19610 window. This produces a single glyph on
19611 window system frames. We leave the glyph in
19612 this row and let it fill the row, but don't
19613 consume the TAB. */
19614 if ((row->reversed_p
19615 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19616 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19617 produce_special_glyphs (it, IT_CONTINUATION);
19618 it->continuation_lines_width += it->last_visible_x;
19619 row->ends_in_middle_of_char_p = 1;
19620 row->continued_p = 1;
19621 glyph->pixel_width = it->last_visible_x - x;
19622 it->starts_in_middle_of_char_p = 1;
19624 else
19626 /* Something other than a TAB that draws past
19627 the right edge of the window. Restore
19628 positions to values before the element. */
19629 if (row->reversed_p)
19630 unproduce_glyphs (it, row->used[TEXT_AREA]
19631 - (n_glyphs_before + i));
19632 row->used[TEXT_AREA] = n_glyphs_before + i;
19634 /* Display continuation glyphs. */
19635 it->current_x = x_before;
19636 it->continuation_lines_width += x;
19637 if (!FRAME_WINDOW_P (it->f)
19638 || (row->reversed_p
19639 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19640 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19641 produce_special_glyphs (it, IT_CONTINUATION);
19642 row->continued_p = 1;
19644 extend_face_to_end_of_line (it);
19646 if (nglyphs > 1 && i > 0)
19648 row->ends_in_middle_of_char_p = 1;
19649 it->starts_in_middle_of_char_p = 1;
19652 /* Restore the height to what it was before the
19653 element not fitting on the line. */
19654 it->max_ascent = ascent;
19655 it->max_descent = descent;
19656 it->max_phys_ascent = phys_ascent;
19657 it->max_phys_descent = phys_descent;
19660 break;
19662 else if (new_x > it->first_visible_x)
19664 /* Increment number of glyphs actually displayed. */
19665 ++it->hpos;
19667 /* Record the maximum and minimum buffer positions
19668 seen so far in glyphs that will be displayed by
19669 this row. */
19670 if (it->bidi_p)
19671 RECORD_MAX_MIN_POS (it);
19673 if (x < it->first_visible_x)
19674 /* Glyph is partially visible, i.e. row starts at
19675 negative X position. */
19676 row->x = x - it->first_visible_x;
19678 else
19680 /* Glyph is completely off the left margin of the
19681 window. This should not happen because of the
19682 move_it_in_display_line at the start of this
19683 function, unless the text display area of the
19684 window is empty. */
19685 eassert (it->first_visible_x <= it->last_visible_x);
19688 /* Even if this display element produced no glyphs at all,
19689 we want to record its position. */
19690 if (it->bidi_p && nglyphs == 0)
19691 RECORD_MAX_MIN_POS (it);
19693 row->ascent = max (row->ascent, it->max_ascent);
19694 row->height = max (row->height, it->max_ascent + it->max_descent);
19695 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19696 row->phys_height = max (row->phys_height,
19697 it->max_phys_ascent + it->max_phys_descent);
19698 row->extra_line_spacing = max (row->extra_line_spacing,
19699 it->max_extra_line_spacing);
19701 /* End of this display line if row is continued. */
19702 if (row->continued_p || row->ends_at_zv_p)
19703 break;
19706 at_end_of_line:
19707 /* Is this a line end? If yes, we're also done, after making
19708 sure that a non-default face is extended up to the right
19709 margin of the window. */
19710 if (ITERATOR_AT_END_OF_LINE_P (it))
19712 int used_before = row->used[TEXT_AREA];
19714 row->ends_in_newline_from_string_p = STRINGP (it->object);
19716 /* Add a space at the end of the line that is used to
19717 display the cursor there. */
19718 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19719 append_space_for_newline (it, 0);
19721 /* Extend the face to the end of the line. */
19722 extend_face_to_end_of_line (it);
19724 /* Make sure we have the position. */
19725 if (used_before == 0)
19726 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19728 /* Record the position of the newline, for use in
19729 find_row_edges. */
19730 it->eol_pos = it->current.pos;
19732 /* Consume the line end. This skips over invisible lines. */
19733 set_iterator_to_next (it, 1);
19734 it->continuation_lines_width = 0;
19735 break;
19738 /* Proceed with next display element. Note that this skips
19739 over lines invisible because of selective display. */
19740 set_iterator_to_next (it, 1);
19742 /* If we truncate lines, we are done when the last displayed
19743 glyphs reach past the right margin of the window. */
19744 if (it->line_wrap == TRUNCATE
19745 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19746 ? (it->current_x >= it->last_visible_x)
19747 : (it->current_x > it->last_visible_x)))
19749 /* Maybe add truncation glyphs. */
19750 if (!FRAME_WINDOW_P (it->f)
19751 || (row->reversed_p
19752 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19753 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19755 int i, n;
19757 if (!row->reversed_p)
19759 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19760 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19761 break;
19763 else
19765 for (i = 0; i < row->used[TEXT_AREA]; i++)
19766 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19767 break;
19768 /* Remove any padding glyphs at the front of ROW, to
19769 make room for the truncation glyphs we will be
19770 adding below. The loop below always inserts at
19771 least one truncation glyph, so also remove the
19772 last glyph added to ROW. */
19773 unproduce_glyphs (it, i + 1);
19774 /* Adjust i for the loop below. */
19775 i = row->used[TEXT_AREA] - (i + 1);
19778 it->current_x = x_before;
19779 if (!FRAME_WINDOW_P (it->f))
19781 for (n = row->used[TEXT_AREA]; i < n; ++i)
19783 row->used[TEXT_AREA] = i;
19784 produce_special_glyphs (it, IT_TRUNCATION);
19787 else
19789 row->used[TEXT_AREA] = i;
19790 produce_special_glyphs (it, IT_TRUNCATION);
19793 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19795 /* Don't truncate if we can overflow newline into fringe. */
19796 if (!get_next_display_element (it))
19798 it->continuation_lines_width = 0;
19799 row->ends_at_zv_p = 1;
19800 row->exact_window_width_line_p = 1;
19801 break;
19803 if (ITERATOR_AT_END_OF_LINE_P (it))
19805 row->exact_window_width_line_p = 1;
19806 goto at_end_of_line;
19808 it->current_x = x_before;
19811 row->truncated_on_right_p = 1;
19812 it->continuation_lines_width = 0;
19813 reseat_at_next_visible_line_start (it, 0);
19814 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19815 it->hpos = hpos_before;
19816 break;
19820 if (wrap_data)
19821 bidi_unshelve_cache (wrap_data, 1);
19823 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19824 at the left window margin. */
19825 if (it->first_visible_x
19826 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19828 if (!FRAME_WINDOW_P (it->f)
19829 || (row->reversed_p
19830 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19831 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19832 insert_left_trunc_glyphs (it);
19833 row->truncated_on_left_p = 1;
19836 /* Remember the position at which this line ends.
19838 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19839 cannot be before the call to find_row_edges below, since that is
19840 where these positions are determined. */
19841 row->end = it->current;
19842 if (!it->bidi_p)
19844 row->minpos = row->start.pos;
19845 row->maxpos = row->end.pos;
19847 else
19849 /* ROW->minpos and ROW->maxpos must be the smallest and
19850 `1 + the largest' buffer positions in ROW. But if ROW was
19851 bidi-reordered, these two positions can be anywhere in the
19852 row, so we must determine them now. */
19853 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19856 /* If the start of this line is the overlay arrow-position, then
19857 mark this glyph row as the one containing the overlay arrow.
19858 This is clearly a mess with variable size fonts. It would be
19859 better to let it be displayed like cursors under X. */
19860 if ((row->displays_text_p || !overlay_arrow_seen)
19861 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19862 !NILP (overlay_arrow_string)))
19864 /* Overlay arrow in window redisplay is a fringe bitmap. */
19865 if (STRINGP (overlay_arrow_string))
19867 struct glyph_row *arrow_row
19868 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19869 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19870 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19871 struct glyph *p = row->glyphs[TEXT_AREA];
19872 struct glyph *p2, *end;
19874 /* Copy the arrow glyphs. */
19875 while (glyph < arrow_end)
19876 *p++ = *glyph++;
19878 /* Throw away padding glyphs. */
19879 p2 = p;
19880 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19881 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19882 ++p2;
19883 if (p2 > p)
19885 while (p2 < end)
19886 *p++ = *p2++;
19887 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19890 else
19892 eassert (INTEGERP (overlay_arrow_string));
19893 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19895 overlay_arrow_seen = 1;
19898 /* Highlight trailing whitespace. */
19899 if (!NILP (Vshow_trailing_whitespace))
19900 highlight_trailing_whitespace (it->f, it->glyph_row);
19902 /* Compute pixel dimensions of this line. */
19903 compute_line_metrics (it);
19905 /* Implementation note: No changes in the glyphs of ROW or in their
19906 faces can be done past this point, because compute_line_metrics
19907 computes ROW's hash value and stores it within the glyph_row
19908 structure. */
19910 /* Record whether this row ends inside an ellipsis. */
19911 row->ends_in_ellipsis_p
19912 = (it->method == GET_FROM_DISPLAY_VECTOR
19913 && it->ellipsis_p);
19915 /* Save fringe bitmaps in this row. */
19916 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19917 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19918 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19919 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19921 it->left_user_fringe_bitmap = 0;
19922 it->left_user_fringe_face_id = 0;
19923 it->right_user_fringe_bitmap = 0;
19924 it->right_user_fringe_face_id = 0;
19926 /* Maybe set the cursor. */
19927 cvpos = it->w->cursor.vpos;
19928 if ((cvpos < 0
19929 /* In bidi-reordered rows, keep checking for proper cursor
19930 position even if one has been found already, because buffer
19931 positions in such rows change non-linearly with ROW->VPOS,
19932 when a line is continued. One exception: when we are at ZV,
19933 display cursor on the first suitable glyph row, since all
19934 the empty rows after that also have their position set to ZV. */
19935 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19936 lines' rows is implemented for bidi-reordered rows. */
19937 || (it->bidi_p
19938 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19939 && PT >= MATRIX_ROW_START_CHARPOS (row)
19940 && PT <= MATRIX_ROW_END_CHARPOS (row)
19941 && cursor_row_p (row))
19942 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19944 /* Prepare for the next line. This line starts horizontally at (X
19945 HPOS) = (0 0). Vertical positions are incremented. As a
19946 convenience for the caller, IT->glyph_row is set to the next
19947 row to be used. */
19948 it->current_x = it->hpos = 0;
19949 it->current_y += row->height;
19950 SET_TEXT_POS (it->eol_pos, 0, 0);
19951 ++it->vpos;
19952 ++it->glyph_row;
19953 /* The next row should by default use the same value of the
19954 reversed_p flag as this one. set_iterator_to_next decides when
19955 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19956 the flag accordingly. */
19957 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19958 it->glyph_row->reversed_p = row->reversed_p;
19959 it->start = row->end;
19960 return row->displays_text_p;
19962 #undef RECORD_MAX_MIN_POS
19965 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19966 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19967 doc: /* Return paragraph direction at point in BUFFER.
19968 Value is either `left-to-right' or `right-to-left'.
19969 If BUFFER is omitted or nil, it defaults to the current buffer.
19971 Paragraph direction determines how the text in the paragraph is displayed.
19972 In left-to-right paragraphs, text begins at the left margin of the window
19973 and the reading direction is generally left to right. In right-to-left
19974 paragraphs, text begins at the right margin and is read from right to left.
19976 See also `bidi-paragraph-direction'. */)
19977 (Lisp_Object buffer)
19979 struct buffer *buf = current_buffer;
19980 struct buffer *old = buf;
19982 if (! NILP (buffer))
19984 CHECK_BUFFER (buffer);
19985 buf = XBUFFER (buffer);
19988 if (NILP (BVAR (buf, bidi_display_reordering))
19989 || NILP (BVAR (buf, enable_multibyte_characters))
19990 /* When we are loading loadup.el, the character property tables
19991 needed for bidi iteration are not yet available. */
19992 || !NILP (Vpurify_flag))
19993 return Qleft_to_right;
19994 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19995 return BVAR (buf, bidi_paragraph_direction);
19996 else
19998 /* Determine the direction from buffer text. We could try to
19999 use current_matrix if it is up to date, but this seems fast
20000 enough as it is. */
20001 struct bidi_it itb;
20002 ptrdiff_t pos = BUF_PT (buf);
20003 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20004 int c;
20005 void *itb_data = bidi_shelve_cache ();
20007 set_buffer_temp (buf);
20008 /* bidi_paragraph_init finds the base direction of the paragraph
20009 by searching forward from paragraph start. We need the base
20010 direction of the current or _previous_ paragraph, so we need
20011 to make sure we are within that paragraph. To that end, find
20012 the previous non-empty line. */
20013 if (pos >= ZV && pos > BEGV)
20015 pos--;
20016 bytepos = CHAR_TO_BYTE (pos);
20018 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20019 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20021 while ((c = FETCH_BYTE (bytepos)) == '\n'
20022 || c == ' ' || c == '\t' || c == '\f')
20024 if (bytepos <= BEGV_BYTE)
20025 break;
20026 bytepos--;
20027 pos--;
20029 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20030 bytepos--;
20032 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20033 itb.paragraph_dir = NEUTRAL_DIR;
20034 itb.string.s = NULL;
20035 itb.string.lstring = Qnil;
20036 itb.string.bufpos = 0;
20037 itb.string.unibyte = 0;
20038 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20039 bidi_unshelve_cache (itb_data, 0);
20040 set_buffer_temp (old);
20041 switch (itb.paragraph_dir)
20043 case L2R:
20044 return Qleft_to_right;
20045 break;
20046 case R2L:
20047 return Qright_to_left;
20048 break;
20049 default:
20050 abort ();
20057 /***********************************************************************
20058 Menu Bar
20059 ***********************************************************************/
20061 /* Redisplay the menu bar in the frame for window W.
20063 The menu bar of X frames that don't have X toolkit support is
20064 displayed in a special window W->frame->menu_bar_window.
20066 The menu bar of terminal frames is treated specially as far as
20067 glyph matrices are concerned. Menu bar lines are not part of
20068 windows, so the update is done directly on the frame matrix rows
20069 for the menu bar. */
20071 static void
20072 display_menu_bar (struct window *w)
20074 struct frame *f = XFRAME (WINDOW_FRAME (w));
20075 struct it it;
20076 Lisp_Object items;
20077 int i;
20079 /* Don't do all this for graphical frames. */
20080 #ifdef HAVE_NTGUI
20081 if (FRAME_W32_P (f))
20082 return;
20083 #endif
20084 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20085 if (FRAME_X_P (f))
20086 return;
20087 #endif
20089 #ifdef HAVE_NS
20090 if (FRAME_NS_P (f))
20091 return;
20092 #endif /* HAVE_NS */
20094 #ifdef USE_X_TOOLKIT
20095 eassert (!FRAME_WINDOW_P (f));
20096 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20097 it.first_visible_x = 0;
20098 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20099 #else /* not USE_X_TOOLKIT */
20100 if (FRAME_WINDOW_P (f))
20102 /* Menu bar lines are displayed in the desired matrix of the
20103 dummy window menu_bar_window. */
20104 struct window *menu_w;
20105 eassert (WINDOWP (f->menu_bar_window));
20106 menu_w = XWINDOW (f->menu_bar_window);
20107 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20108 MENU_FACE_ID);
20109 it.first_visible_x = 0;
20110 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20112 else
20114 /* This is a TTY frame, i.e. character hpos/vpos are used as
20115 pixel x/y. */
20116 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20117 MENU_FACE_ID);
20118 it.first_visible_x = 0;
20119 it.last_visible_x = FRAME_COLS (f);
20121 #endif /* not USE_X_TOOLKIT */
20123 /* FIXME: This should be controlled by a user option. See the
20124 comments in redisplay_tool_bar and display_mode_line about
20125 this. */
20126 it.paragraph_embedding = L2R;
20128 if (! mode_line_inverse_video)
20129 /* Force the menu-bar to be displayed in the default face. */
20130 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20132 /* Clear all rows of the menu bar. */
20133 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20135 struct glyph_row *row = it.glyph_row + i;
20136 clear_glyph_row (row);
20137 row->enabled_p = 1;
20138 row->full_width_p = 1;
20141 /* Display all items of the menu bar. */
20142 items = FRAME_MENU_BAR_ITEMS (it.f);
20143 for (i = 0; i < ASIZE (items); i += 4)
20145 Lisp_Object string;
20147 /* Stop at nil string. */
20148 string = AREF (items, i + 1);
20149 if (NILP (string))
20150 break;
20152 /* Remember where item was displayed. */
20153 ASET (items, i + 3, make_number (it.hpos));
20155 /* Display the item, pad with one space. */
20156 if (it.current_x < it.last_visible_x)
20157 display_string (NULL, string, Qnil, 0, 0, &it,
20158 SCHARS (string) + 1, 0, 0, -1);
20161 /* Fill out the line with spaces. */
20162 if (it.current_x < it.last_visible_x)
20163 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20165 /* Compute the total height of the lines. */
20166 compute_line_metrics (&it);
20171 /***********************************************************************
20172 Mode Line
20173 ***********************************************************************/
20175 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20176 FORCE is non-zero, redisplay mode lines unconditionally.
20177 Otherwise, redisplay only mode lines that are garbaged. Value is
20178 the number of windows whose mode lines were redisplayed. */
20180 static int
20181 redisplay_mode_lines (Lisp_Object window, int force)
20183 int nwindows = 0;
20185 while (!NILP (window))
20187 struct window *w = XWINDOW (window);
20189 if (WINDOWP (w->hchild))
20190 nwindows += redisplay_mode_lines (w->hchild, force);
20191 else if (WINDOWP (w->vchild))
20192 nwindows += redisplay_mode_lines (w->vchild, force);
20193 else if (force
20194 || FRAME_GARBAGED_P (XFRAME (w->frame))
20195 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20197 struct text_pos lpoint;
20198 struct buffer *old = current_buffer;
20200 /* Set the window's buffer for the mode line display. */
20201 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20202 set_buffer_internal_1 (XBUFFER (w->buffer));
20204 /* Point refers normally to the selected window. For any
20205 other window, set up appropriate value. */
20206 if (!EQ (window, selected_window))
20208 struct text_pos pt;
20210 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20211 if (CHARPOS (pt) < BEGV)
20212 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20213 else if (CHARPOS (pt) > (ZV - 1))
20214 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20215 else
20216 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20219 /* Display mode lines. */
20220 clear_glyph_matrix (w->desired_matrix);
20221 if (display_mode_lines (w))
20223 ++nwindows;
20224 w->must_be_updated_p = 1;
20227 /* Restore old settings. */
20228 set_buffer_internal_1 (old);
20229 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20232 window = w->next;
20235 return nwindows;
20239 /* Display the mode and/or header line of window W. Value is the
20240 sum number of mode lines and header lines displayed. */
20242 static int
20243 display_mode_lines (struct window *w)
20245 Lisp_Object old_selected_window, old_selected_frame;
20246 int n = 0;
20248 old_selected_frame = selected_frame;
20249 selected_frame = w->frame;
20250 old_selected_window = selected_window;
20251 XSETWINDOW (selected_window, w);
20253 /* These will be set while the mode line specs are processed. */
20254 line_number_displayed = 0;
20255 WSET (w, column_number_displayed, Qnil);
20257 if (WINDOW_WANTS_MODELINE_P (w))
20259 struct window *sel_w = XWINDOW (old_selected_window);
20261 /* Select mode line face based on the real selected window. */
20262 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20263 BVAR (current_buffer, mode_line_format));
20264 ++n;
20267 if (WINDOW_WANTS_HEADER_LINE_P (w))
20269 display_mode_line (w, HEADER_LINE_FACE_ID,
20270 BVAR (current_buffer, header_line_format));
20271 ++n;
20274 selected_frame = old_selected_frame;
20275 selected_window = old_selected_window;
20276 return n;
20280 /* Display mode or header line of window W. FACE_ID specifies which
20281 line to display; it is either MODE_LINE_FACE_ID or
20282 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20283 display. Value is the pixel height of the mode/header line
20284 displayed. */
20286 static int
20287 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20289 struct it it;
20290 struct face *face;
20291 ptrdiff_t count = SPECPDL_INDEX ();
20293 init_iterator (&it, w, -1, -1, NULL, face_id);
20294 /* Don't extend on a previously drawn mode-line.
20295 This may happen if called from pos_visible_p. */
20296 it.glyph_row->enabled_p = 0;
20297 prepare_desired_row (it.glyph_row);
20299 it.glyph_row->mode_line_p = 1;
20301 if (! mode_line_inverse_video)
20302 /* Force the mode-line to be displayed in the default face. */
20303 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20305 /* FIXME: This should be controlled by a user option. But
20306 supporting such an option is not trivial, since the mode line is
20307 made up of many separate strings. */
20308 it.paragraph_embedding = L2R;
20310 record_unwind_protect (unwind_format_mode_line,
20311 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20313 mode_line_target = MODE_LINE_DISPLAY;
20315 /* Temporarily make frame's keyboard the current kboard so that
20316 kboard-local variables in the mode_line_format will get the right
20317 values. */
20318 push_kboard (FRAME_KBOARD (it.f));
20319 record_unwind_save_match_data ();
20320 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20321 pop_kboard ();
20323 unbind_to (count, Qnil);
20325 /* Fill up with spaces. */
20326 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20328 compute_line_metrics (&it);
20329 it.glyph_row->full_width_p = 1;
20330 it.glyph_row->continued_p = 0;
20331 it.glyph_row->truncated_on_left_p = 0;
20332 it.glyph_row->truncated_on_right_p = 0;
20334 /* Make a 3D mode-line have a shadow at its right end. */
20335 face = FACE_FROM_ID (it.f, face_id);
20336 extend_face_to_end_of_line (&it);
20337 if (face->box != FACE_NO_BOX)
20339 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20340 + it.glyph_row->used[TEXT_AREA] - 1);
20341 last->right_box_line_p = 1;
20344 return it.glyph_row->height;
20347 /* Move element ELT in LIST to the front of LIST.
20348 Return the updated list. */
20350 static Lisp_Object
20351 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20353 register Lisp_Object tail, prev;
20354 register Lisp_Object tem;
20356 tail = list;
20357 prev = Qnil;
20358 while (CONSP (tail))
20360 tem = XCAR (tail);
20362 if (EQ (elt, tem))
20364 /* Splice out the link TAIL. */
20365 if (NILP (prev))
20366 list = XCDR (tail);
20367 else
20368 Fsetcdr (prev, XCDR (tail));
20370 /* Now make it the first. */
20371 Fsetcdr (tail, list);
20372 return tail;
20374 else
20375 prev = tail;
20376 tail = XCDR (tail);
20377 QUIT;
20380 /* Not found--return unchanged LIST. */
20381 return list;
20384 /* Contribute ELT to the mode line for window IT->w. How it
20385 translates into text depends on its data type.
20387 IT describes the display environment in which we display, as usual.
20389 DEPTH is the depth in recursion. It is used to prevent
20390 infinite recursion here.
20392 FIELD_WIDTH is the number of characters the display of ELT should
20393 occupy in the mode line, and PRECISION is the maximum number of
20394 characters to display from ELT's representation. See
20395 display_string for details.
20397 Returns the hpos of the end of the text generated by ELT.
20399 PROPS is a property list to add to any string we encounter.
20401 If RISKY is nonzero, remove (disregard) any properties in any string
20402 we encounter, and ignore :eval and :propertize.
20404 The global variable `mode_line_target' determines whether the
20405 output is passed to `store_mode_line_noprop',
20406 `store_mode_line_string', or `display_string'. */
20408 static int
20409 display_mode_element (struct it *it, int depth, int field_width, int precision,
20410 Lisp_Object elt, Lisp_Object props, int risky)
20412 int n = 0, field, prec;
20413 int literal = 0;
20415 tail_recurse:
20416 if (depth > 100)
20417 elt = build_string ("*too-deep*");
20419 depth++;
20421 switch (XTYPE (elt))
20423 case Lisp_String:
20425 /* A string: output it and check for %-constructs within it. */
20426 unsigned char c;
20427 ptrdiff_t offset = 0;
20429 if (SCHARS (elt) > 0
20430 && (!NILP (props) || risky))
20432 Lisp_Object oprops, aelt;
20433 oprops = Ftext_properties_at (make_number (0), elt);
20435 /* If the starting string's properties are not what
20436 we want, translate the string. Also, if the string
20437 is risky, do that anyway. */
20439 if (NILP (Fequal (props, oprops)) || risky)
20441 /* If the starting string has properties,
20442 merge the specified ones onto the existing ones. */
20443 if (! NILP (oprops) && !risky)
20445 Lisp_Object tem;
20447 oprops = Fcopy_sequence (oprops);
20448 tem = props;
20449 while (CONSP (tem))
20451 oprops = Fplist_put (oprops, XCAR (tem),
20452 XCAR (XCDR (tem)));
20453 tem = XCDR (XCDR (tem));
20455 props = oprops;
20458 aelt = Fassoc (elt, mode_line_proptrans_alist);
20459 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20461 /* AELT is what we want. Move it to the front
20462 without consing. */
20463 elt = XCAR (aelt);
20464 mode_line_proptrans_alist
20465 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20467 else
20469 Lisp_Object tem;
20471 /* If AELT has the wrong props, it is useless.
20472 so get rid of it. */
20473 if (! NILP (aelt))
20474 mode_line_proptrans_alist
20475 = Fdelq (aelt, mode_line_proptrans_alist);
20477 elt = Fcopy_sequence (elt);
20478 Fset_text_properties (make_number (0), Flength (elt),
20479 props, elt);
20480 /* Add this item to mode_line_proptrans_alist. */
20481 mode_line_proptrans_alist
20482 = Fcons (Fcons (elt, props),
20483 mode_line_proptrans_alist);
20484 /* Truncate mode_line_proptrans_alist
20485 to at most 50 elements. */
20486 tem = Fnthcdr (make_number (50),
20487 mode_line_proptrans_alist);
20488 if (! NILP (tem))
20489 XSETCDR (tem, Qnil);
20494 offset = 0;
20496 if (literal)
20498 prec = precision - n;
20499 switch (mode_line_target)
20501 case MODE_LINE_NOPROP:
20502 case MODE_LINE_TITLE:
20503 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20504 break;
20505 case MODE_LINE_STRING:
20506 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20507 break;
20508 case MODE_LINE_DISPLAY:
20509 n += display_string (NULL, elt, Qnil, 0, 0, it,
20510 0, prec, 0, STRING_MULTIBYTE (elt));
20511 break;
20514 break;
20517 /* Handle the non-literal case. */
20519 while ((precision <= 0 || n < precision)
20520 && SREF (elt, offset) != 0
20521 && (mode_line_target != MODE_LINE_DISPLAY
20522 || it->current_x < it->last_visible_x))
20524 ptrdiff_t last_offset = offset;
20526 /* Advance to end of string or next format specifier. */
20527 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20530 if (offset - 1 != last_offset)
20532 ptrdiff_t nchars, nbytes;
20534 /* Output to end of string or up to '%'. Field width
20535 is length of string. Don't output more than
20536 PRECISION allows us. */
20537 offset--;
20539 prec = c_string_width (SDATA (elt) + last_offset,
20540 offset - last_offset, precision - n,
20541 &nchars, &nbytes);
20543 switch (mode_line_target)
20545 case MODE_LINE_NOPROP:
20546 case MODE_LINE_TITLE:
20547 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20548 break;
20549 case MODE_LINE_STRING:
20551 ptrdiff_t bytepos = last_offset;
20552 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20553 ptrdiff_t endpos = (precision <= 0
20554 ? string_byte_to_char (elt, offset)
20555 : charpos + nchars);
20557 n += store_mode_line_string (NULL,
20558 Fsubstring (elt, make_number (charpos),
20559 make_number (endpos)),
20560 0, 0, 0, Qnil);
20562 break;
20563 case MODE_LINE_DISPLAY:
20565 ptrdiff_t bytepos = last_offset;
20566 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20568 if (precision <= 0)
20569 nchars = string_byte_to_char (elt, offset) - charpos;
20570 n += display_string (NULL, elt, Qnil, 0, charpos,
20571 it, 0, nchars, 0,
20572 STRING_MULTIBYTE (elt));
20574 break;
20577 else /* c == '%' */
20579 ptrdiff_t percent_position = offset;
20581 /* Get the specified minimum width. Zero means
20582 don't pad. */
20583 field = 0;
20584 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20585 field = field * 10 + c - '0';
20587 /* Don't pad beyond the total padding allowed. */
20588 if (field_width - n > 0 && field > field_width - n)
20589 field = field_width - n;
20591 /* Note that either PRECISION <= 0 or N < PRECISION. */
20592 prec = precision - n;
20594 if (c == 'M')
20595 n += display_mode_element (it, depth, field, prec,
20596 Vglobal_mode_string, props,
20597 risky);
20598 else if (c != 0)
20600 int multibyte;
20601 ptrdiff_t bytepos, charpos;
20602 const char *spec;
20603 Lisp_Object string;
20605 bytepos = percent_position;
20606 charpos = (STRING_MULTIBYTE (elt)
20607 ? string_byte_to_char (elt, bytepos)
20608 : bytepos);
20609 spec = decode_mode_spec (it->w, c, field, &string);
20610 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20612 switch (mode_line_target)
20614 case MODE_LINE_NOPROP:
20615 case MODE_LINE_TITLE:
20616 n += store_mode_line_noprop (spec, field, prec);
20617 break;
20618 case MODE_LINE_STRING:
20620 Lisp_Object tem = build_string (spec);
20621 props = Ftext_properties_at (make_number (charpos), elt);
20622 /* Should only keep face property in props */
20623 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20625 break;
20626 case MODE_LINE_DISPLAY:
20628 int nglyphs_before, nwritten;
20630 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20631 nwritten = display_string (spec, string, elt,
20632 charpos, 0, it,
20633 field, prec, 0,
20634 multibyte);
20636 /* Assign to the glyphs written above the
20637 string where the `%x' came from, position
20638 of the `%'. */
20639 if (nwritten > 0)
20641 struct glyph *glyph
20642 = (it->glyph_row->glyphs[TEXT_AREA]
20643 + nglyphs_before);
20644 int i;
20646 for (i = 0; i < nwritten; ++i)
20648 glyph[i].object = elt;
20649 glyph[i].charpos = charpos;
20652 n += nwritten;
20655 break;
20658 else /* c == 0 */
20659 break;
20663 break;
20665 case Lisp_Symbol:
20666 /* A symbol: process the value of the symbol recursively
20667 as if it appeared here directly. Avoid error if symbol void.
20668 Special case: if value of symbol is a string, output the string
20669 literally. */
20671 register Lisp_Object tem;
20673 /* If the variable is not marked as risky to set
20674 then its contents are risky to use. */
20675 if (NILP (Fget (elt, Qrisky_local_variable)))
20676 risky = 1;
20678 tem = Fboundp (elt);
20679 if (!NILP (tem))
20681 tem = Fsymbol_value (elt);
20682 /* If value is a string, output that string literally:
20683 don't check for % within it. */
20684 if (STRINGP (tem))
20685 literal = 1;
20687 if (!EQ (tem, elt))
20689 /* Give up right away for nil or t. */
20690 elt = tem;
20691 goto tail_recurse;
20695 break;
20697 case Lisp_Cons:
20699 register Lisp_Object car, tem;
20701 /* A cons cell: five distinct cases.
20702 If first element is :eval or :propertize, do something special.
20703 If first element is a string or a cons, process all the elements
20704 and effectively concatenate them.
20705 If first element is a negative number, truncate displaying cdr to
20706 at most that many characters. If positive, pad (with spaces)
20707 to at least that many characters.
20708 If first element is a symbol, process the cadr or caddr recursively
20709 according to whether the symbol's value is non-nil or nil. */
20710 car = XCAR (elt);
20711 if (EQ (car, QCeval))
20713 /* An element of the form (:eval FORM) means evaluate FORM
20714 and use the result as mode line elements. */
20716 if (risky)
20717 break;
20719 if (CONSP (XCDR (elt)))
20721 Lisp_Object spec;
20722 spec = safe_eval (XCAR (XCDR (elt)));
20723 n += display_mode_element (it, depth, field_width - n,
20724 precision - n, spec, props,
20725 risky);
20728 else if (EQ (car, QCpropertize))
20730 /* An element of the form (:propertize ELT PROPS...)
20731 means display ELT but applying properties PROPS. */
20733 if (risky)
20734 break;
20736 if (CONSP (XCDR (elt)))
20737 n += display_mode_element (it, depth, field_width - n,
20738 precision - n, XCAR (XCDR (elt)),
20739 XCDR (XCDR (elt)), risky);
20741 else if (SYMBOLP (car))
20743 tem = Fboundp (car);
20744 elt = XCDR (elt);
20745 if (!CONSP (elt))
20746 goto invalid;
20747 /* elt is now the cdr, and we know it is a cons cell.
20748 Use its car if CAR has a non-nil value. */
20749 if (!NILP (tem))
20751 tem = Fsymbol_value (car);
20752 if (!NILP (tem))
20754 elt = XCAR (elt);
20755 goto tail_recurse;
20758 /* Symbol's value is nil (or symbol is unbound)
20759 Get the cddr of the original list
20760 and if possible find the caddr and use that. */
20761 elt = XCDR (elt);
20762 if (NILP (elt))
20763 break;
20764 else if (!CONSP (elt))
20765 goto invalid;
20766 elt = XCAR (elt);
20767 goto tail_recurse;
20769 else if (INTEGERP (car))
20771 register int lim = XINT (car);
20772 elt = XCDR (elt);
20773 if (lim < 0)
20775 /* Negative int means reduce maximum width. */
20776 if (precision <= 0)
20777 precision = -lim;
20778 else
20779 precision = min (precision, -lim);
20781 else if (lim > 0)
20783 /* Padding specified. Don't let it be more than
20784 current maximum. */
20785 if (precision > 0)
20786 lim = min (precision, lim);
20788 /* If that's more padding than already wanted, queue it.
20789 But don't reduce padding already specified even if
20790 that is beyond the current truncation point. */
20791 field_width = max (lim, field_width);
20793 goto tail_recurse;
20795 else if (STRINGP (car) || CONSP (car))
20797 Lisp_Object halftail = elt;
20798 int len = 0;
20800 while (CONSP (elt)
20801 && (precision <= 0 || n < precision))
20803 n += display_mode_element (it, depth,
20804 /* Do padding only after the last
20805 element in the list. */
20806 (! CONSP (XCDR (elt))
20807 ? field_width - n
20808 : 0),
20809 precision - n, XCAR (elt),
20810 props, risky);
20811 elt = XCDR (elt);
20812 len++;
20813 if ((len & 1) == 0)
20814 halftail = XCDR (halftail);
20815 /* Check for cycle. */
20816 if (EQ (halftail, elt))
20817 break;
20821 break;
20823 default:
20824 invalid:
20825 elt = build_string ("*invalid*");
20826 goto tail_recurse;
20829 /* Pad to FIELD_WIDTH. */
20830 if (field_width > 0 && n < field_width)
20832 switch (mode_line_target)
20834 case MODE_LINE_NOPROP:
20835 case MODE_LINE_TITLE:
20836 n += store_mode_line_noprop ("", field_width - n, 0);
20837 break;
20838 case MODE_LINE_STRING:
20839 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20840 break;
20841 case MODE_LINE_DISPLAY:
20842 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20843 0, 0, 0);
20844 break;
20848 return n;
20851 /* Store a mode-line string element in mode_line_string_list.
20853 If STRING is non-null, display that C string. Otherwise, the Lisp
20854 string LISP_STRING is displayed.
20856 FIELD_WIDTH is the minimum number of output glyphs to produce.
20857 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20858 with spaces. FIELD_WIDTH <= 0 means don't pad.
20860 PRECISION is the maximum number of characters to output from
20861 STRING. PRECISION <= 0 means don't truncate the string.
20863 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20864 properties to the string.
20866 PROPS are the properties to add to the string.
20867 The mode_line_string_face face property is always added to the string.
20870 static int
20871 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20872 int field_width, int precision, Lisp_Object props)
20874 ptrdiff_t len;
20875 int n = 0;
20877 if (string != NULL)
20879 len = strlen (string);
20880 if (precision > 0 && len > precision)
20881 len = precision;
20882 lisp_string = make_string (string, len);
20883 if (NILP (props))
20884 props = mode_line_string_face_prop;
20885 else if (!NILP (mode_line_string_face))
20887 Lisp_Object face = Fplist_get (props, Qface);
20888 props = Fcopy_sequence (props);
20889 if (NILP (face))
20890 face = mode_line_string_face;
20891 else
20892 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20893 props = Fplist_put (props, Qface, face);
20895 Fadd_text_properties (make_number (0), make_number (len),
20896 props, lisp_string);
20898 else
20900 len = XFASTINT (Flength (lisp_string));
20901 if (precision > 0 && len > precision)
20903 len = precision;
20904 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20905 precision = -1;
20907 if (!NILP (mode_line_string_face))
20909 Lisp_Object face;
20910 if (NILP (props))
20911 props = Ftext_properties_at (make_number (0), lisp_string);
20912 face = Fplist_get (props, Qface);
20913 if (NILP (face))
20914 face = mode_line_string_face;
20915 else
20916 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20917 props = Fcons (Qface, Fcons (face, Qnil));
20918 if (copy_string)
20919 lisp_string = Fcopy_sequence (lisp_string);
20921 if (!NILP (props))
20922 Fadd_text_properties (make_number (0), make_number (len),
20923 props, lisp_string);
20926 if (len > 0)
20928 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20929 n += len;
20932 if (field_width > len)
20934 field_width -= len;
20935 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20936 if (!NILP (props))
20937 Fadd_text_properties (make_number (0), make_number (field_width),
20938 props, lisp_string);
20939 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20940 n += field_width;
20943 return n;
20947 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20948 1, 4, 0,
20949 doc: /* Format a string out of a mode line format specification.
20950 First arg FORMAT specifies the mode line format (see `mode-line-format'
20951 for details) to use.
20953 By default, the format is evaluated for the currently selected window.
20955 Optional second arg FACE specifies the face property to put on all
20956 characters for which no face is specified. The value nil means the
20957 default face. The value t means whatever face the window's mode line
20958 currently uses (either `mode-line' or `mode-line-inactive',
20959 depending on whether the window is the selected window or not).
20960 An integer value means the value string has no text
20961 properties.
20963 Optional third and fourth args WINDOW and BUFFER specify the window
20964 and buffer to use as the context for the formatting (defaults
20965 are the selected window and the WINDOW's buffer). */)
20966 (Lisp_Object format, Lisp_Object face,
20967 Lisp_Object window, Lisp_Object buffer)
20969 struct it it;
20970 int len;
20971 struct window *w;
20972 struct buffer *old_buffer = NULL;
20973 int face_id;
20974 int no_props = INTEGERP (face);
20975 ptrdiff_t count = SPECPDL_INDEX ();
20976 Lisp_Object str;
20977 int string_start = 0;
20979 if (NILP (window))
20980 window = selected_window;
20981 CHECK_WINDOW (window);
20982 w = XWINDOW (window);
20984 if (NILP (buffer))
20985 buffer = w->buffer;
20986 CHECK_BUFFER (buffer);
20988 /* Make formatting the modeline a non-op when noninteractive, otherwise
20989 there will be problems later caused by a partially initialized frame. */
20990 if (NILP (format) || noninteractive)
20991 return empty_unibyte_string;
20993 if (no_props)
20994 face = Qnil;
20996 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20997 : EQ (face, Qt) ? (EQ (window, selected_window)
20998 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20999 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21000 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21001 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21002 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21003 : DEFAULT_FACE_ID;
21005 if (XBUFFER (buffer) != current_buffer)
21006 old_buffer = current_buffer;
21008 /* Save things including mode_line_proptrans_alist,
21009 and set that to nil so that we don't alter the outer value. */
21010 record_unwind_protect (unwind_format_mode_line,
21011 format_mode_line_unwind_data
21012 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21013 old_buffer, selected_window, 1));
21014 mode_line_proptrans_alist = Qnil;
21016 Fselect_window (window, Qt);
21017 if (old_buffer)
21018 set_buffer_internal_1 (XBUFFER (buffer));
21020 init_iterator (&it, w, -1, -1, NULL, face_id);
21022 if (no_props)
21024 mode_line_target = MODE_LINE_NOPROP;
21025 mode_line_string_face_prop = Qnil;
21026 mode_line_string_list = Qnil;
21027 string_start = MODE_LINE_NOPROP_LEN (0);
21029 else
21031 mode_line_target = MODE_LINE_STRING;
21032 mode_line_string_list = Qnil;
21033 mode_line_string_face = face;
21034 mode_line_string_face_prop
21035 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21038 push_kboard (FRAME_KBOARD (it.f));
21039 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21040 pop_kboard ();
21042 if (no_props)
21044 len = MODE_LINE_NOPROP_LEN (string_start);
21045 str = make_string (mode_line_noprop_buf + string_start, len);
21047 else
21049 mode_line_string_list = Fnreverse (mode_line_string_list);
21050 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21051 empty_unibyte_string);
21054 unbind_to (count, Qnil);
21055 return str;
21058 /* Write a null-terminated, right justified decimal representation of
21059 the positive integer D to BUF using a minimal field width WIDTH. */
21061 static void
21062 pint2str (register char *buf, register int width, register ptrdiff_t d)
21064 register char *p = buf;
21066 if (d <= 0)
21067 *p++ = '0';
21068 else
21070 while (d > 0)
21072 *p++ = d % 10 + '0';
21073 d /= 10;
21077 for (width -= (int) (p - buf); width > 0; --width)
21078 *p++ = ' ';
21079 *p-- = '\0';
21080 while (p > buf)
21082 d = *buf;
21083 *buf++ = *p;
21084 *p-- = d;
21088 /* Write a null-terminated, right justified decimal and "human
21089 readable" representation of the nonnegative integer D to BUF using
21090 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21092 static const char power_letter[] =
21094 0, /* no letter */
21095 'k', /* kilo */
21096 'M', /* mega */
21097 'G', /* giga */
21098 'T', /* tera */
21099 'P', /* peta */
21100 'E', /* exa */
21101 'Z', /* zetta */
21102 'Y' /* yotta */
21105 static void
21106 pint2hrstr (char *buf, int width, ptrdiff_t d)
21108 /* We aim to represent the nonnegative integer D as
21109 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21110 ptrdiff_t quotient = d;
21111 int remainder = 0;
21112 /* -1 means: do not use TENTHS. */
21113 int tenths = -1;
21114 int exponent = 0;
21116 /* Length of QUOTIENT.TENTHS as a string. */
21117 int length;
21119 char * psuffix;
21120 char * p;
21122 if (1000 <= quotient)
21124 /* Scale to the appropriate EXPONENT. */
21127 remainder = quotient % 1000;
21128 quotient /= 1000;
21129 exponent++;
21131 while (1000 <= quotient);
21133 /* Round to nearest and decide whether to use TENTHS or not. */
21134 if (quotient <= 9)
21136 tenths = remainder / 100;
21137 if (50 <= remainder % 100)
21139 if (tenths < 9)
21140 tenths++;
21141 else
21143 quotient++;
21144 if (quotient == 10)
21145 tenths = -1;
21146 else
21147 tenths = 0;
21151 else
21152 if (500 <= remainder)
21154 if (quotient < 999)
21155 quotient++;
21156 else
21158 quotient = 1;
21159 exponent++;
21160 tenths = 0;
21165 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21166 if (tenths == -1 && quotient <= 99)
21167 if (quotient <= 9)
21168 length = 1;
21169 else
21170 length = 2;
21171 else
21172 length = 3;
21173 p = psuffix = buf + max (width, length);
21175 /* Print EXPONENT. */
21176 *psuffix++ = power_letter[exponent];
21177 *psuffix = '\0';
21179 /* Print TENTHS. */
21180 if (tenths >= 0)
21182 *--p = '0' + tenths;
21183 *--p = '.';
21186 /* Print QUOTIENT. */
21189 int digit = quotient % 10;
21190 *--p = '0' + digit;
21192 while ((quotient /= 10) != 0);
21194 /* Print leading spaces. */
21195 while (buf < p)
21196 *--p = ' ';
21199 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21200 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21201 type of CODING_SYSTEM. Return updated pointer into BUF. */
21203 static unsigned char invalid_eol_type[] = "(*invalid*)";
21205 static char *
21206 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21208 Lisp_Object val;
21209 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21210 const unsigned char *eol_str;
21211 int eol_str_len;
21212 /* The EOL conversion we are using. */
21213 Lisp_Object eoltype;
21215 val = CODING_SYSTEM_SPEC (coding_system);
21216 eoltype = Qnil;
21218 if (!VECTORP (val)) /* Not yet decided. */
21220 *buf++ = multibyte ? '-' : ' ';
21221 if (eol_flag)
21222 eoltype = eol_mnemonic_undecided;
21223 /* Don't mention EOL conversion if it isn't decided. */
21225 else
21227 Lisp_Object attrs;
21228 Lisp_Object eolvalue;
21230 attrs = AREF (val, 0);
21231 eolvalue = AREF (val, 2);
21233 *buf++ = multibyte
21234 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21235 : ' ';
21237 if (eol_flag)
21239 /* The EOL conversion that is normal on this system. */
21241 if (NILP (eolvalue)) /* Not yet decided. */
21242 eoltype = eol_mnemonic_undecided;
21243 else if (VECTORP (eolvalue)) /* Not yet decided. */
21244 eoltype = eol_mnemonic_undecided;
21245 else /* eolvalue is Qunix, Qdos, or Qmac. */
21246 eoltype = (EQ (eolvalue, Qunix)
21247 ? eol_mnemonic_unix
21248 : (EQ (eolvalue, Qdos) == 1
21249 ? eol_mnemonic_dos : eol_mnemonic_mac));
21253 if (eol_flag)
21255 /* Mention the EOL conversion if it is not the usual one. */
21256 if (STRINGP (eoltype))
21258 eol_str = SDATA (eoltype);
21259 eol_str_len = SBYTES (eoltype);
21261 else if (CHARACTERP (eoltype))
21263 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21264 int c = XFASTINT (eoltype);
21265 eol_str_len = CHAR_STRING (c, tmp);
21266 eol_str = tmp;
21268 else
21270 eol_str = invalid_eol_type;
21271 eol_str_len = sizeof (invalid_eol_type) - 1;
21273 memcpy (buf, eol_str, eol_str_len);
21274 buf += eol_str_len;
21277 return buf;
21280 /* Return a string for the output of a mode line %-spec for window W,
21281 generated by character C. FIELD_WIDTH > 0 means pad the string
21282 returned with spaces to that value. Return a Lisp string in
21283 *STRING if the resulting string is taken from that Lisp string.
21285 Note we operate on the current buffer for most purposes,
21286 the exception being w->base_line_pos. */
21288 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21290 static const char *
21291 decode_mode_spec (struct window *w, register int c, int field_width,
21292 Lisp_Object *string)
21294 Lisp_Object obj;
21295 struct frame *f = XFRAME (WINDOW_FRAME (w));
21296 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21297 struct buffer *b = current_buffer;
21299 obj = Qnil;
21300 *string = Qnil;
21302 switch (c)
21304 case '*':
21305 if (!NILP (BVAR (b, read_only)))
21306 return "%";
21307 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21308 return "*";
21309 return "-";
21311 case '+':
21312 /* This differs from %* only for a modified read-only buffer. */
21313 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21314 return "*";
21315 if (!NILP (BVAR (b, read_only)))
21316 return "%";
21317 return "-";
21319 case '&':
21320 /* This differs from %* in ignoring read-only-ness. */
21321 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21322 return "*";
21323 return "-";
21325 case '%':
21326 return "%";
21328 case '[':
21330 int i;
21331 char *p;
21333 if (command_loop_level > 5)
21334 return "[[[... ";
21335 p = decode_mode_spec_buf;
21336 for (i = 0; i < command_loop_level; i++)
21337 *p++ = '[';
21338 *p = 0;
21339 return decode_mode_spec_buf;
21342 case ']':
21344 int i;
21345 char *p;
21347 if (command_loop_level > 5)
21348 return " ...]]]";
21349 p = decode_mode_spec_buf;
21350 for (i = 0; i < command_loop_level; i++)
21351 *p++ = ']';
21352 *p = 0;
21353 return decode_mode_spec_buf;
21356 case '-':
21358 register int i;
21360 /* Let lots_of_dashes be a string of infinite length. */
21361 if (mode_line_target == MODE_LINE_NOPROP ||
21362 mode_line_target == MODE_LINE_STRING)
21363 return "--";
21364 if (field_width <= 0
21365 || field_width > sizeof (lots_of_dashes))
21367 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21368 decode_mode_spec_buf[i] = '-';
21369 decode_mode_spec_buf[i] = '\0';
21370 return decode_mode_spec_buf;
21372 else
21373 return lots_of_dashes;
21376 case 'b':
21377 obj = BVAR (b, name);
21378 break;
21380 case 'c':
21381 /* %c and %l are ignored in `frame-title-format'.
21382 (In redisplay_internal, the frame title is drawn _before_ the
21383 windows are updated, so the stuff which depends on actual
21384 window contents (such as %l) may fail to render properly, or
21385 even crash emacs.) */
21386 if (mode_line_target == MODE_LINE_TITLE)
21387 return "";
21388 else
21390 ptrdiff_t col = current_column ();
21391 WSET (w, column_number_displayed, make_number (col));
21392 pint2str (decode_mode_spec_buf, field_width, col);
21393 return decode_mode_spec_buf;
21396 case 'e':
21397 #ifndef SYSTEM_MALLOC
21399 if (NILP (Vmemory_full))
21400 return "";
21401 else
21402 return "!MEM FULL! ";
21404 #else
21405 return "";
21406 #endif
21408 case 'F':
21409 /* %F displays the frame name. */
21410 if (!NILP (f->title))
21411 return SSDATA (f->title);
21412 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21413 return SSDATA (f->name);
21414 return "Emacs";
21416 case 'f':
21417 obj = BVAR (b, filename);
21418 break;
21420 case 'i':
21422 ptrdiff_t size = ZV - BEGV;
21423 pint2str (decode_mode_spec_buf, field_width, size);
21424 return decode_mode_spec_buf;
21427 case 'I':
21429 ptrdiff_t size = ZV - BEGV;
21430 pint2hrstr (decode_mode_spec_buf, field_width, size);
21431 return decode_mode_spec_buf;
21434 case 'l':
21436 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21437 ptrdiff_t topline, nlines, height;
21438 ptrdiff_t junk;
21440 /* %c and %l are ignored in `frame-title-format'. */
21441 if (mode_line_target == MODE_LINE_TITLE)
21442 return "";
21444 startpos = XMARKER (w->start)->charpos;
21445 startpos_byte = marker_byte_position (w->start);
21446 height = WINDOW_TOTAL_LINES (w);
21448 /* If we decided that this buffer isn't suitable for line numbers,
21449 don't forget that too fast. */
21450 if (EQ (w->base_line_pos, w->buffer))
21451 goto no_value;
21452 /* But do forget it, if the window shows a different buffer now. */
21453 else if (BUFFERP (w->base_line_pos))
21454 WSET (w, base_line_pos, Qnil);
21456 /* If the buffer is very big, don't waste time. */
21457 if (INTEGERP (Vline_number_display_limit)
21458 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21460 WSET (w, base_line_pos, Qnil);
21461 WSET (w, base_line_number, Qnil);
21462 goto no_value;
21465 if (INTEGERP (w->base_line_number)
21466 && INTEGERP (w->base_line_pos)
21467 && XFASTINT (w->base_line_pos) <= startpos)
21469 line = XFASTINT (w->base_line_number);
21470 linepos = XFASTINT (w->base_line_pos);
21471 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21473 else
21475 line = 1;
21476 linepos = BUF_BEGV (b);
21477 linepos_byte = BUF_BEGV_BYTE (b);
21480 /* Count lines from base line to window start position. */
21481 nlines = display_count_lines (linepos_byte,
21482 startpos_byte,
21483 startpos, &junk);
21485 topline = nlines + line;
21487 /* Determine a new base line, if the old one is too close
21488 or too far away, or if we did not have one.
21489 "Too close" means it's plausible a scroll-down would
21490 go back past it. */
21491 if (startpos == BUF_BEGV (b))
21493 WSET (w, base_line_number, make_number (topline));
21494 WSET (w, base_line_pos, make_number (BUF_BEGV (b)));
21496 else if (nlines < height + 25 || nlines > height * 3 + 50
21497 || linepos == BUF_BEGV (b))
21499 ptrdiff_t limit = BUF_BEGV (b);
21500 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21501 ptrdiff_t position;
21502 ptrdiff_t distance =
21503 (height * 2 + 30) * line_number_display_limit_width;
21505 if (startpos - distance > limit)
21507 limit = startpos - distance;
21508 limit_byte = CHAR_TO_BYTE (limit);
21511 nlines = display_count_lines (startpos_byte,
21512 limit_byte,
21513 - (height * 2 + 30),
21514 &position);
21515 /* If we couldn't find the lines we wanted within
21516 line_number_display_limit_width chars per line,
21517 give up on line numbers for this window. */
21518 if (position == limit_byte && limit == startpos - distance)
21520 WSET (w, base_line_pos, w->buffer);
21521 WSET (w, base_line_number, Qnil);
21522 goto no_value;
21525 WSET (w, base_line_number, make_number (topline - nlines));
21526 WSET (w, base_line_pos, make_number (BYTE_TO_CHAR (position)));
21529 /* Now count lines from the start pos to point. */
21530 nlines = display_count_lines (startpos_byte,
21531 PT_BYTE, PT, &junk);
21533 /* Record that we did display the line number. */
21534 line_number_displayed = 1;
21536 /* Make the string to show. */
21537 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21538 return decode_mode_spec_buf;
21539 no_value:
21541 char* p = decode_mode_spec_buf;
21542 int pad = field_width - 2;
21543 while (pad-- > 0)
21544 *p++ = ' ';
21545 *p++ = '?';
21546 *p++ = '?';
21547 *p = '\0';
21548 return decode_mode_spec_buf;
21551 break;
21553 case 'm':
21554 obj = BVAR (b, mode_name);
21555 break;
21557 case 'n':
21558 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21559 return " Narrow";
21560 break;
21562 case 'p':
21564 ptrdiff_t pos = marker_position (w->start);
21565 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21567 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21569 if (pos <= BUF_BEGV (b))
21570 return "All";
21571 else
21572 return "Bottom";
21574 else if (pos <= BUF_BEGV (b))
21575 return "Top";
21576 else
21578 if (total > 1000000)
21579 /* Do it differently for a large value, to avoid overflow. */
21580 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21581 else
21582 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21583 /* We can't normally display a 3-digit number,
21584 so get us a 2-digit number that is close. */
21585 if (total == 100)
21586 total = 99;
21587 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21588 return decode_mode_spec_buf;
21592 /* Display percentage of size above the bottom of the screen. */
21593 case 'P':
21595 ptrdiff_t toppos = marker_position (w->start);
21596 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21597 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21599 if (botpos >= BUF_ZV (b))
21601 if (toppos <= BUF_BEGV (b))
21602 return "All";
21603 else
21604 return "Bottom";
21606 else
21608 if (total > 1000000)
21609 /* Do it differently for a large value, to avoid overflow. */
21610 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21611 else
21612 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21613 /* We can't normally display a 3-digit number,
21614 so get us a 2-digit number that is close. */
21615 if (total == 100)
21616 total = 99;
21617 if (toppos <= BUF_BEGV (b))
21618 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21619 else
21620 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21621 return decode_mode_spec_buf;
21625 case 's':
21626 /* status of process */
21627 obj = Fget_buffer_process (Fcurrent_buffer ());
21628 if (NILP (obj))
21629 return "no process";
21630 #ifndef MSDOS
21631 obj = Fsymbol_name (Fprocess_status (obj));
21632 #endif
21633 break;
21635 case '@':
21637 ptrdiff_t count = inhibit_garbage_collection ();
21638 Lisp_Object val = call1 (intern ("file-remote-p"),
21639 BVAR (current_buffer, directory));
21640 unbind_to (count, Qnil);
21642 if (NILP (val))
21643 return "-";
21644 else
21645 return "@";
21648 case 't': /* indicate TEXT or BINARY */
21649 return "T";
21651 case 'z':
21652 /* coding-system (not including end-of-line format) */
21653 case 'Z':
21654 /* coding-system (including end-of-line type) */
21656 int eol_flag = (c == 'Z');
21657 char *p = decode_mode_spec_buf;
21659 if (! FRAME_WINDOW_P (f))
21661 /* No need to mention EOL here--the terminal never needs
21662 to do EOL conversion. */
21663 p = decode_mode_spec_coding (CODING_ID_NAME
21664 (FRAME_KEYBOARD_CODING (f)->id),
21665 p, 0);
21666 p = decode_mode_spec_coding (CODING_ID_NAME
21667 (FRAME_TERMINAL_CODING (f)->id),
21668 p, 0);
21670 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21671 p, eol_flag);
21673 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21674 #ifdef subprocesses
21675 obj = Fget_buffer_process (Fcurrent_buffer ());
21676 if (PROCESSP (obj))
21678 p = decode_mode_spec_coding
21679 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21680 p = decode_mode_spec_coding
21681 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21683 #endif /* subprocesses */
21684 #endif /* 0 */
21685 *p = 0;
21686 return decode_mode_spec_buf;
21690 if (STRINGP (obj))
21692 *string = obj;
21693 return SSDATA (obj);
21695 else
21696 return "";
21700 /* Count up to COUNT lines starting from START_BYTE.
21701 But don't go beyond LIMIT_BYTE.
21702 Return the number of lines thus found (always nonnegative).
21704 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21706 static ptrdiff_t
21707 display_count_lines (ptrdiff_t start_byte,
21708 ptrdiff_t limit_byte, ptrdiff_t count,
21709 ptrdiff_t *byte_pos_ptr)
21711 register unsigned char *cursor;
21712 unsigned char *base;
21714 register ptrdiff_t ceiling;
21715 register unsigned char *ceiling_addr;
21716 ptrdiff_t orig_count = count;
21718 /* If we are not in selective display mode,
21719 check only for newlines. */
21720 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21721 && !INTEGERP (BVAR (current_buffer, selective_display)));
21723 if (count > 0)
21725 while (start_byte < limit_byte)
21727 ceiling = BUFFER_CEILING_OF (start_byte);
21728 ceiling = min (limit_byte - 1, ceiling);
21729 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21730 base = (cursor = BYTE_POS_ADDR (start_byte));
21731 while (1)
21733 if (selective_display)
21734 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21736 else
21737 while (*cursor != '\n' && ++cursor != ceiling_addr)
21740 if (cursor != ceiling_addr)
21742 if (--count == 0)
21744 start_byte += cursor - base + 1;
21745 *byte_pos_ptr = start_byte;
21746 return orig_count;
21748 else
21749 if (++cursor == ceiling_addr)
21750 break;
21752 else
21753 break;
21755 start_byte += cursor - base;
21758 else
21760 while (start_byte > limit_byte)
21762 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21763 ceiling = max (limit_byte, ceiling);
21764 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21765 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21766 while (1)
21768 if (selective_display)
21769 while (--cursor != ceiling_addr
21770 && *cursor != '\n' && *cursor != 015)
21772 else
21773 while (--cursor != ceiling_addr && *cursor != '\n')
21776 if (cursor != ceiling_addr)
21778 if (++count == 0)
21780 start_byte += cursor - base + 1;
21781 *byte_pos_ptr = start_byte;
21782 /* When scanning backwards, we should
21783 not count the newline posterior to which we stop. */
21784 return - orig_count - 1;
21787 else
21788 break;
21790 /* Here we add 1 to compensate for the last decrement
21791 of CURSOR, which took it past the valid range. */
21792 start_byte += cursor - base + 1;
21796 *byte_pos_ptr = limit_byte;
21798 if (count < 0)
21799 return - orig_count + count;
21800 return orig_count - count;
21806 /***********************************************************************
21807 Displaying strings
21808 ***********************************************************************/
21810 /* Display a NUL-terminated string, starting with index START.
21812 If STRING is non-null, display that C string. Otherwise, the Lisp
21813 string LISP_STRING is displayed. There's a case that STRING is
21814 non-null and LISP_STRING is not nil. It means STRING is a string
21815 data of LISP_STRING. In that case, we display LISP_STRING while
21816 ignoring its text properties.
21818 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21819 FACE_STRING. Display STRING or LISP_STRING with the face at
21820 FACE_STRING_POS in FACE_STRING:
21822 Display the string in the environment given by IT, but use the
21823 standard display table, temporarily.
21825 FIELD_WIDTH is the minimum number of output glyphs to produce.
21826 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21827 with spaces. If STRING has more characters, more than FIELD_WIDTH
21828 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21830 PRECISION is the maximum number of characters to output from
21831 STRING. PRECISION < 0 means don't truncate the string.
21833 This is roughly equivalent to printf format specifiers:
21835 FIELD_WIDTH PRECISION PRINTF
21836 ----------------------------------------
21837 -1 -1 %s
21838 -1 10 %.10s
21839 10 -1 %10s
21840 20 10 %20.10s
21842 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21843 display them, and < 0 means obey the current buffer's value of
21844 enable_multibyte_characters.
21846 Value is the number of columns displayed. */
21848 static int
21849 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21850 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21851 int field_width, int precision, int max_x, int multibyte)
21853 int hpos_at_start = it->hpos;
21854 int saved_face_id = it->face_id;
21855 struct glyph_row *row = it->glyph_row;
21856 ptrdiff_t it_charpos;
21858 /* Initialize the iterator IT for iteration over STRING beginning
21859 with index START. */
21860 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21861 precision, field_width, multibyte);
21862 if (string && STRINGP (lisp_string))
21863 /* LISP_STRING is the one returned by decode_mode_spec. We should
21864 ignore its text properties. */
21865 it->stop_charpos = it->end_charpos;
21867 /* If displaying STRING, set up the face of the iterator from
21868 FACE_STRING, if that's given. */
21869 if (STRINGP (face_string))
21871 ptrdiff_t endptr;
21872 struct face *face;
21874 it->face_id
21875 = face_at_string_position (it->w, face_string, face_string_pos,
21876 0, it->region_beg_charpos,
21877 it->region_end_charpos,
21878 &endptr, it->base_face_id, 0);
21879 face = FACE_FROM_ID (it->f, it->face_id);
21880 it->face_box_p = face->box != FACE_NO_BOX;
21883 /* Set max_x to the maximum allowed X position. Don't let it go
21884 beyond the right edge of the window. */
21885 if (max_x <= 0)
21886 max_x = it->last_visible_x;
21887 else
21888 max_x = min (max_x, it->last_visible_x);
21890 /* Skip over display elements that are not visible. because IT->w is
21891 hscrolled. */
21892 if (it->current_x < it->first_visible_x)
21893 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21894 MOVE_TO_POS | MOVE_TO_X);
21896 row->ascent = it->max_ascent;
21897 row->height = it->max_ascent + it->max_descent;
21898 row->phys_ascent = it->max_phys_ascent;
21899 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21900 row->extra_line_spacing = it->max_extra_line_spacing;
21902 if (STRINGP (it->string))
21903 it_charpos = IT_STRING_CHARPOS (*it);
21904 else
21905 it_charpos = IT_CHARPOS (*it);
21907 /* This condition is for the case that we are called with current_x
21908 past last_visible_x. */
21909 while (it->current_x < max_x)
21911 int x_before, x, n_glyphs_before, i, nglyphs;
21913 /* Get the next display element. */
21914 if (!get_next_display_element (it))
21915 break;
21917 /* Produce glyphs. */
21918 x_before = it->current_x;
21919 n_glyphs_before = row->used[TEXT_AREA];
21920 PRODUCE_GLYPHS (it);
21922 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21923 i = 0;
21924 x = x_before;
21925 while (i < nglyphs)
21927 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21929 if (it->line_wrap != TRUNCATE
21930 && x + glyph->pixel_width > max_x)
21932 /* End of continued line or max_x reached. */
21933 if (CHAR_GLYPH_PADDING_P (*glyph))
21935 /* A wide character is unbreakable. */
21936 if (row->reversed_p)
21937 unproduce_glyphs (it, row->used[TEXT_AREA]
21938 - n_glyphs_before);
21939 row->used[TEXT_AREA] = n_glyphs_before;
21940 it->current_x = x_before;
21942 else
21944 if (row->reversed_p)
21945 unproduce_glyphs (it, row->used[TEXT_AREA]
21946 - (n_glyphs_before + i));
21947 row->used[TEXT_AREA] = n_glyphs_before + i;
21948 it->current_x = x;
21950 break;
21952 else if (x + glyph->pixel_width >= it->first_visible_x)
21954 /* Glyph is at least partially visible. */
21955 ++it->hpos;
21956 if (x < it->first_visible_x)
21957 row->x = x - it->first_visible_x;
21959 else
21961 /* Glyph is off the left margin of the display area.
21962 Should not happen. */
21963 abort ();
21966 row->ascent = max (row->ascent, it->max_ascent);
21967 row->height = max (row->height, it->max_ascent + it->max_descent);
21968 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21969 row->phys_height = max (row->phys_height,
21970 it->max_phys_ascent + it->max_phys_descent);
21971 row->extra_line_spacing = max (row->extra_line_spacing,
21972 it->max_extra_line_spacing);
21973 x += glyph->pixel_width;
21974 ++i;
21977 /* Stop if max_x reached. */
21978 if (i < nglyphs)
21979 break;
21981 /* Stop at line ends. */
21982 if (ITERATOR_AT_END_OF_LINE_P (it))
21984 it->continuation_lines_width = 0;
21985 break;
21988 set_iterator_to_next (it, 1);
21989 if (STRINGP (it->string))
21990 it_charpos = IT_STRING_CHARPOS (*it);
21991 else
21992 it_charpos = IT_CHARPOS (*it);
21994 /* Stop if truncating at the right edge. */
21995 if (it->line_wrap == TRUNCATE
21996 && it->current_x >= it->last_visible_x)
21998 /* Add truncation mark, but don't do it if the line is
21999 truncated at a padding space. */
22000 if (it_charpos < it->string_nchars)
22002 if (!FRAME_WINDOW_P (it->f))
22004 int ii, n;
22006 if (it->current_x > it->last_visible_x)
22008 if (!row->reversed_p)
22010 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22011 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22012 break;
22014 else
22016 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22017 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22018 break;
22019 unproduce_glyphs (it, ii + 1);
22020 ii = row->used[TEXT_AREA] - (ii + 1);
22022 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22024 row->used[TEXT_AREA] = ii;
22025 produce_special_glyphs (it, IT_TRUNCATION);
22028 produce_special_glyphs (it, IT_TRUNCATION);
22030 row->truncated_on_right_p = 1;
22032 break;
22036 /* Maybe insert a truncation at the left. */
22037 if (it->first_visible_x
22038 && it_charpos > 0)
22040 if (!FRAME_WINDOW_P (it->f)
22041 || (row->reversed_p
22042 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22043 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22044 insert_left_trunc_glyphs (it);
22045 row->truncated_on_left_p = 1;
22048 it->face_id = saved_face_id;
22050 /* Value is number of columns displayed. */
22051 return it->hpos - hpos_at_start;
22056 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22057 appears as an element of LIST or as the car of an element of LIST.
22058 If PROPVAL is a list, compare each element against LIST in that
22059 way, and return 1/2 if any element of PROPVAL is found in LIST.
22060 Otherwise return 0. This function cannot quit.
22061 The return value is 2 if the text is invisible but with an ellipsis
22062 and 1 if it's invisible and without an ellipsis. */
22065 invisible_p (register Lisp_Object propval, Lisp_Object list)
22067 register Lisp_Object tail, proptail;
22069 for (tail = list; CONSP (tail); tail = XCDR (tail))
22071 register Lisp_Object tem;
22072 tem = XCAR (tail);
22073 if (EQ (propval, tem))
22074 return 1;
22075 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22076 return NILP (XCDR (tem)) ? 1 : 2;
22079 if (CONSP (propval))
22081 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22083 Lisp_Object propelt;
22084 propelt = XCAR (proptail);
22085 for (tail = list; CONSP (tail); tail = XCDR (tail))
22087 register Lisp_Object tem;
22088 tem = XCAR (tail);
22089 if (EQ (propelt, tem))
22090 return 1;
22091 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22092 return NILP (XCDR (tem)) ? 1 : 2;
22097 return 0;
22100 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22101 doc: /* Non-nil if the property makes the text invisible.
22102 POS-OR-PROP can be a marker or number, in which case it is taken to be
22103 a position in the current buffer and the value of the `invisible' property
22104 is checked; or it can be some other value, which is then presumed to be the
22105 value of the `invisible' property of the text of interest.
22106 The non-nil value returned can be t for truly invisible text or something
22107 else if the text is replaced by an ellipsis. */)
22108 (Lisp_Object pos_or_prop)
22110 Lisp_Object prop
22111 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22112 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22113 : pos_or_prop);
22114 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22115 return (invis == 0 ? Qnil
22116 : invis == 1 ? Qt
22117 : make_number (invis));
22120 /* Calculate a width or height in pixels from a specification using
22121 the following elements:
22123 SPEC ::=
22124 NUM - a (fractional) multiple of the default font width/height
22125 (NUM) - specifies exactly NUM pixels
22126 UNIT - a fixed number of pixels, see below.
22127 ELEMENT - size of a display element in pixels, see below.
22128 (NUM . SPEC) - equals NUM * SPEC
22129 (+ SPEC SPEC ...) - add pixel values
22130 (- SPEC SPEC ...) - subtract pixel values
22131 (- SPEC) - negate pixel value
22133 NUM ::=
22134 INT or FLOAT - a number constant
22135 SYMBOL - use symbol's (buffer local) variable binding.
22137 UNIT ::=
22138 in - pixels per inch *)
22139 mm - pixels per 1/1000 meter *)
22140 cm - pixels per 1/100 meter *)
22141 width - width of current font in pixels.
22142 height - height of current font in pixels.
22144 *) using the ratio(s) defined in display-pixels-per-inch.
22146 ELEMENT ::=
22148 left-fringe - left fringe width in pixels
22149 right-fringe - right fringe width in pixels
22151 left-margin - left margin width in pixels
22152 right-margin - right margin width in pixels
22154 scroll-bar - scroll-bar area width in pixels
22156 Examples:
22158 Pixels corresponding to 5 inches:
22159 (5 . in)
22161 Total width of non-text areas on left side of window (if scroll-bar is on left):
22162 '(space :width (+ left-fringe left-margin scroll-bar))
22164 Align to first text column (in header line):
22165 '(space :align-to 0)
22167 Align to middle of text area minus half the width of variable `my-image'
22168 containing a loaded image:
22169 '(space :align-to (0.5 . (- text my-image)))
22171 Width of left margin minus width of 1 character in the default font:
22172 '(space :width (- left-margin 1))
22174 Width of left margin minus width of 2 characters in the current font:
22175 '(space :width (- left-margin (2 . width)))
22177 Center 1 character over left-margin (in header line):
22178 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22180 Different ways to express width of left fringe plus left margin minus one pixel:
22181 '(space :width (- (+ left-fringe left-margin) (1)))
22182 '(space :width (+ left-fringe left-margin (- (1))))
22183 '(space :width (+ left-fringe left-margin (-1)))
22187 #define NUMVAL(X) \
22188 ((INTEGERP (X) || FLOATP (X)) \
22189 ? XFLOATINT (X) \
22190 : - 1)
22192 static int
22193 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22194 struct font *font, int width_p, int *align_to)
22196 double pixels;
22198 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22199 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22201 if (NILP (prop))
22202 return OK_PIXELS (0);
22204 eassert (FRAME_LIVE_P (it->f));
22206 if (SYMBOLP (prop))
22208 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22210 char *unit = SSDATA (SYMBOL_NAME (prop));
22212 if (unit[0] == 'i' && unit[1] == 'n')
22213 pixels = 1.0;
22214 else if (unit[0] == 'm' && unit[1] == 'm')
22215 pixels = 25.4;
22216 else if (unit[0] == 'c' && unit[1] == 'm')
22217 pixels = 2.54;
22218 else
22219 pixels = 0;
22220 if (pixels > 0)
22222 double ppi;
22223 #ifdef HAVE_WINDOW_SYSTEM
22224 if (FRAME_WINDOW_P (it->f)
22225 && (ppi = (width_p
22226 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22227 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22228 ppi > 0))
22229 return OK_PIXELS (ppi / pixels);
22230 #endif
22232 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22233 || (CONSP (Vdisplay_pixels_per_inch)
22234 && (ppi = (width_p
22235 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22236 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22237 ppi > 0)))
22238 return OK_PIXELS (ppi / pixels);
22240 return 0;
22244 #ifdef HAVE_WINDOW_SYSTEM
22245 if (EQ (prop, Qheight))
22246 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22247 if (EQ (prop, Qwidth))
22248 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22249 #else
22250 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22251 return OK_PIXELS (1);
22252 #endif
22254 if (EQ (prop, Qtext))
22255 return OK_PIXELS (width_p
22256 ? window_box_width (it->w, TEXT_AREA)
22257 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22259 if (align_to && *align_to < 0)
22261 *res = 0;
22262 if (EQ (prop, Qleft))
22263 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22264 if (EQ (prop, Qright))
22265 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22266 if (EQ (prop, Qcenter))
22267 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22268 + window_box_width (it->w, TEXT_AREA) / 2);
22269 if (EQ (prop, Qleft_fringe))
22270 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22271 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22272 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22273 if (EQ (prop, Qright_fringe))
22274 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22275 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22276 : window_box_right_offset (it->w, TEXT_AREA));
22277 if (EQ (prop, Qleft_margin))
22278 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22279 if (EQ (prop, Qright_margin))
22280 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22281 if (EQ (prop, Qscroll_bar))
22282 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22284 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22285 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22286 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22287 : 0)));
22289 else
22291 if (EQ (prop, Qleft_fringe))
22292 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22293 if (EQ (prop, Qright_fringe))
22294 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22295 if (EQ (prop, Qleft_margin))
22296 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22297 if (EQ (prop, Qright_margin))
22298 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22299 if (EQ (prop, Qscroll_bar))
22300 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22303 prop = buffer_local_value_1 (prop, it->w->buffer);
22304 if (EQ (prop, Qunbound))
22305 prop = Qnil;
22308 if (INTEGERP (prop) || FLOATP (prop))
22310 int base_unit = (width_p
22311 ? FRAME_COLUMN_WIDTH (it->f)
22312 : FRAME_LINE_HEIGHT (it->f));
22313 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22316 if (CONSP (prop))
22318 Lisp_Object car = XCAR (prop);
22319 Lisp_Object cdr = XCDR (prop);
22321 if (SYMBOLP (car))
22323 #ifdef HAVE_WINDOW_SYSTEM
22324 if (FRAME_WINDOW_P (it->f)
22325 && valid_image_p (prop))
22327 ptrdiff_t id = lookup_image (it->f, prop);
22328 struct image *img = IMAGE_FROM_ID (it->f, id);
22330 return OK_PIXELS (width_p ? img->width : img->height);
22332 #endif
22333 if (EQ (car, Qplus) || EQ (car, Qminus))
22335 int first = 1;
22336 double px;
22338 pixels = 0;
22339 while (CONSP (cdr))
22341 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22342 font, width_p, align_to))
22343 return 0;
22344 if (first)
22345 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22346 else
22347 pixels += px;
22348 cdr = XCDR (cdr);
22350 if (EQ (car, Qminus))
22351 pixels = -pixels;
22352 return OK_PIXELS (pixels);
22355 car = buffer_local_value_1 (car, it->w->buffer);
22356 if (EQ (car, Qunbound))
22357 car = Qnil;
22360 if (INTEGERP (car) || FLOATP (car))
22362 double fact;
22363 pixels = XFLOATINT (car);
22364 if (NILP (cdr))
22365 return OK_PIXELS (pixels);
22366 if (calc_pixel_width_or_height (&fact, it, cdr,
22367 font, width_p, align_to))
22368 return OK_PIXELS (pixels * fact);
22369 return 0;
22372 return 0;
22375 return 0;
22379 /***********************************************************************
22380 Glyph Display
22381 ***********************************************************************/
22383 #ifdef HAVE_WINDOW_SYSTEM
22385 #ifdef GLYPH_DEBUG
22387 void
22388 dump_glyph_string (struct glyph_string *s)
22390 fprintf (stderr, "glyph string\n");
22391 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22392 s->x, s->y, s->width, s->height);
22393 fprintf (stderr, " ybase = %d\n", s->ybase);
22394 fprintf (stderr, " hl = %d\n", s->hl);
22395 fprintf (stderr, " left overhang = %d, right = %d\n",
22396 s->left_overhang, s->right_overhang);
22397 fprintf (stderr, " nchars = %d\n", s->nchars);
22398 fprintf (stderr, " extends to end of line = %d\n",
22399 s->extends_to_end_of_line_p);
22400 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22401 fprintf (stderr, " bg width = %d\n", s->background_width);
22404 #endif /* GLYPH_DEBUG */
22406 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22407 of XChar2b structures for S; it can't be allocated in
22408 init_glyph_string because it must be allocated via `alloca'. W
22409 is the window on which S is drawn. ROW and AREA are the glyph row
22410 and area within the row from which S is constructed. START is the
22411 index of the first glyph structure covered by S. HL is a
22412 face-override for drawing S. */
22414 #ifdef HAVE_NTGUI
22415 #define OPTIONAL_HDC(hdc) HDC hdc,
22416 #define DECLARE_HDC(hdc) HDC hdc;
22417 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22418 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22419 #endif
22421 #ifndef OPTIONAL_HDC
22422 #define OPTIONAL_HDC(hdc)
22423 #define DECLARE_HDC(hdc)
22424 #define ALLOCATE_HDC(hdc, f)
22425 #define RELEASE_HDC(hdc, f)
22426 #endif
22428 static void
22429 init_glyph_string (struct glyph_string *s,
22430 OPTIONAL_HDC (hdc)
22431 XChar2b *char2b, struct window *w, struct glyph_row *row,
22432 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22434 memset (s, 0, sizeof *s);
22435 s->w = w;
22436 s->f = XFRAME (w->frame);
22437 #ifdef HAVE_NTGUI
22438 s->hdc = hdc;
22439 #endif
22440 s->display = FRAME_X_DISPLAY (s->f);
22441 s->window = FRAME_X_WINDOW (s->f);
22442 s->char2b = char2b;
22443 s->hl = hl;
22444 s->row = row;
22445 s->area = area;
22446 s->first_glyph = row->glyphs[area] + start;
22447 s->height = row->height;
22448 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22449 s->ybase = s->y + row->ascent;
22453 /* Append the list of glyph strings with head H and tail T to the list
22454 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22456 static inline void
22457 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22458 struct glyph_string *h, struct glyph_string *t)
22460 if (h)
22462 if (*head)
22463 (*tail)->next = h;
22464 else
22465 *head = h;
22466 h->prev = *tail;
22467 *tail = t;
22472 /* Prepend the list of glyph strings with head H and tail T to the
22473 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22474 result. */
22476 static inline void
22477 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22478 struct glyph_string *h, struct glyph_string *t)
22480 if (h)
22482 if (*head)
22483 (*head)->prev = t;
22484 else
22485 *tail = t;
22486 t->next = *head;
22487 *head = h;
22492 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22493 Set *HEAD and *TAIL to the resulting list. */
22495 static inline void
22496 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22497 struct glyph_string *s)
22499 s->next = s->prev = NULL;
22500 append_glyph_string_lists (head, tail, s, s);
22504 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22505 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22506 make sure that X resources for the face returned are allocated.
22507 Value is a pointer to a realized face that is ready for display if
22508 DISPLAY_P is non-zero. */
22510 static inline struct face *
22511 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22512 XChar2b *char2b, int display_p)
22514 struct face *face = FACE_FROM_ID (f, face_id);
22516 if (face->font)
22518 unsigned code = face->font->driver->encode_char (face->font, c);
22520 if (code != FONT_INVALID_CODE)
22521 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22522 else
22523 STORE_XCHAR2B (char2b, 0, 0);
22526 /* Make sure X resources of the face are allocated. */
22527 #ifdef HAVE_X_WINDOWS
22528 if (display_p)
22529 #endif
22531 eassert (face != NULL);
22532 PREPARE_FACE_FOR_DISPLAY (f, face);
22535 return face;
22539 /* Get face and two-byte form of character glyph GLYPH on frame F.
22540 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22541 a pointer to a realized face that is ready for display. */
22543 static inline struct face *
22544 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22545 XChar2b *char2b, int *two_byte_p)
22547 struct face *face;
22549 eassert (glyph->type == CHAR_GLYPH);
22550 face = FACE_FROM_ID (f, glyph->face_id);
22552 if (two_byte_p)
22553 *two_byte_p = 0;
22555 if (face->font)
22557 unsigned code;
22559 if (CHAR_BYTE8_P (glyph->u.ch))
22560 code = CHAR_TO_BYTE8 (glyph->u.ch);
22561 else
22562 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22564 if (code != FONT_INVALID_CODE)
22565 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22566 else
22567 STORE_XCHAR2B (char2b, 0, 0);
22570 /* Make sure X resources of the face are allocated. */
22571 eassert (face != NULL);
22572 PREPARE_FACE_FOR_DISPLAY (f, face);
22573 return face;
22577 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22578 Return 1 if FONT has a glyph for C, otherwise return 0. */
22580 static inline int
22581 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22583 unsigned code;
22585 if (CHAR_BYTE8_P (c))
22586 code = CHAR_TO_BYTE8 (c);
22587 else
22588 code = font->driver->encode_char (font, c);
22590 if (code == FONT_INVALID_CODE)
22591 return 0;
22592 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22593 return 1;
22597 /* Fill glyph string S with composition components specified by S->cmp.
22599 BASE_FACE is the base face of the composition.
22600 S->cmp_from is the index of the first component for S.
22602 OVERLAPS non-zero means S should draw the foreground only, and use
22603 its physical height for clipping. See also draw_glyphs.
22605 Value is the index of a component not in S. */
22607 static int
22608 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22609 int overlaps)
22611 int i;
22612 /* For all glyphs of this composition, starting at the offset
22613 S->cmp_from, until we reach the end of the definition or encounter a
22614 glyph that requires the different face, add it to S. */
22615 struct face *face;
22617 eassert (s);
22619 s->for_overlaps = overlaps;
22620 s->face = NULL;
22621 s->font = NULL;
22622 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22624 int c = COMPOSITION_GLYPH (s->cmp, i);
22626 /* TAB in a composition means display glyphs with padding space
22627 on the left or right. */
22628 if (c != '\t')
22630 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22631 -1, Qnil);
22633 face = get_char_face_and_encoding (s->f, c, face_id,
22634 s->char2b + i, 1);
22635 if (face)
22637 if (! s->face)
22639 s->face = face;
22640 s->font = s->face->font;
22642 else if (s->face != face)
22643 break;
22646 ++s->nchars;
22648 s->cmp_to = i;
22650 if (s->face == NULL)
22652 s->face = base_face->ascii_face;
22653 s->font = s->face->font;
22656 /* All glyph strings for the same composition has the same width,
22657 i.e. the width set for the first component of the composition. */
22658 s->width = s->first_glyph->pixel_width;
22660 /* If the specified font could not be loaded, use the frame's
22661 default font, but record the fact that we couldn't load it in
22662 the glyph string so that we can draw rectangles for the
22663 characters of the glyph string. */
22664 if (s->font == NULL)
22666 s->font_not_found_p = 1;
22667 s->font = FRAME_FONT (s->f);
22670 /* Adjust base line for subscript/superscript text. */
22671 s->ybase += s->first_glyph->voffset;
22673 /* This glyph string must always be drawn with 16-bit functions. */
22674 s->two_byte_p = 1;
22676 return s->cmp_to;
22679 static int
22680 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22681 int start, int end, int overlaps)
22683 struct glyph *glyph, *last;
22684 Lisp_Object lgstring;
22685 int i;
22687 s->for_overlaps = overlaps;
22688 glyph = s->row->glyphs[s->area] + start;
22689 last = s->row->glyphs[s->area] + end;
22690 s->cmp_id = glyph->u.cmp.id;
22691 s->cmp_from = glyph->slice.cmp.from;
22692 s->cmp_to = glyph->slice.cmp.to + 1;
22693 s->face = FACE_FROM_ID (s->f, face_id);
22694 lgstring = composition_gstring_from_id (s->cmp_id);
22695 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22696 glyph++;
22697 while (glyph < last
22698 && glyph->u.cmp.automatic
22699 && glyph->u.cmp.id == s->cmp_id
22700 && s->cmp_to == glyph->slice.cmp.from)
22701 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22703 for (i = s->cmp_from; i < s->cmp_to; i++)
22705 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22706 unsigned code = LGLYPH_CODE (lglyph);
22708 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22710 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22711 return glyph - s->row->glyphs[s->area];
22715 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22716 See the comment of fill_glyph_string for arguments.
22717 Value is the index of the first glyph not in S. */
22720 static int
22721 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22722 int start, int end, int overlaps)
22724 struct glyph *glyph, *last;
22725 int voffset;
22727 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22728 s->for_overlaps = overlaps;
22729 glyph = s->row->glyphs[s->area] + start;
22730 last = s->row->glyphs[s->area] + end;
22731 voffset = glyph->voffset;
22732 s->face = FACE_FROM_ID (s->f, face_id);
22733 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22734 s->nchars = 1;
22735 s->width = glyph->pixel_width;
22736 glyph++;
22737 while (glyph < last
22738 && glyph->type == GLYPHLESS_GLYPH
22739 && glyph->voffset == voffset
22740 && glyph->face_id == face_id)
22742 s->nchars++;
22743 s->width += glyph->pixel_width;
22744 glyph++;
22746 s->ybase += voffset;
22747 return glyph - s->row->glyphs[s->area];
22751 /* Fill glyph string S from a sequence of character glyphs.
22753 FACE_ID is the face id of the string. START is the index of the
22754 first glyph to consider, END is the index of the last + 1.
22755 OVERLAPS non-zero means S should draw the foreground only, and use
22756 its physical height for clipping. See also draw_glyphs.
22758 Value is the index of the first glyph not in S. */
22760 static int
22761 fill_glyph_string (struct glyph_string *s, int face_id,
22762 int start, int end, int overlaps)
22764 struct glyph *glyph, *last;
22765 int voffset;
22766 int glyph_not_available_p;
22768 eassert (s->f == XFRAME (s->w->frame));
22769 eassert (s->nchars == 0);
22770 eassert (start >= 0 && end > start);
22772 s->for_overlaps = overlaps;
22773 glyph = s->row->glyphs[s->area] + start;
22774 last = s->row->glyphs[s->area] + end;
22775 voffset = glyph->voffset;
22776 s->padding_p = glyph->padding_p;
22777 glyph_not_available_p = glyph->glyph_not_available_p;
22779 while (glyph < last
22780 && glyph->type == CHAR_GLYPH
22781 && glyph->voffset == voffset
22782 /* Same face id implies same font, nowadays. */
22783 && glyph->face_id == face_id
22784 && glyph->glyph_not_available_p == glyph_not_available_p)
22786 int two_byte_p;
22788 s->face = get_glyph_face_and_encoding (s->f, glyph,
22789 s->char2b + s->nchars,
22790 &two_byte_p);
22791 s->two_byte_p = two_byte_p;
22792 ++s->nchars;
22793 eassert (s->nchars <= end - start);
22794 s->width += glyph->pixel_width;
22795 if (glyph++->padding_p != s->padding_p)
22796 break;
22799 s->font = s->face->font;
22801 /* If the specified font could not be loaded, use the frame's font,
22802 but record the fact that we couldn't load it in
22803 S->font_not_found_p so that we can draw rectangles for the
22804 characters of the glyph string. */
22805 if (s->font == NULL || glyph_not_available_p)
22807 s->font_not_found_p = 1;
22808 s->font = FRAME_FONT (s->f);
22811 /* Adjust base line for subscript/superscript text. */
22812 s->ybase += voffset;
22814 eassert (s->face && s->face->gc);
22815 return glyph - s->row->glyphs[s->area];
22819 /* Fill glyph string S from image glyph S->first_glyph. */
22821 static void
22822 fill_image_glyph_string (struct glyph_string *s)
22824 eassert (s->first_glyph->type == IMAGE_GLYPH);
22825 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22826 eassert (s->img);
22827 s->slice = s->first_glyph->slice.img;
22828 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22829 s->font = s->face->font;
22830 s->width = s->first_glyph->pixel_width;
22832 /* Adjust base line for subscript/superscript text. */
22833 s->ybase += s->first_glyph->voffset;
22837 /* Fill glyph string S from a sequence of stretch glyphs.
22839 START is the index of the first glyph to consider,
22840 END is the index of the last + 1.
22842 Value is the index of the first glyph not in S. */
22844 static int
22845 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22847 struct glyph *glyph, *last;
22848 int voffset, face_id;
22850 eassert (s->first_glyph->type == STRETCH_GLYPH);
22852 glyph = s->row->glyphs[s->area] + start;
22853 last = s->row->glyphs[s->area] + end;
22854 face_id = glyph->face_id;
22855 s->face = FACE_FROM_ID (s->f, face_id);
22856 s->font = s->face->font;
22857 s->width = glyph->pixel_width;
22858 s->nchars = 1;
22859 voffset = glyph->voffset;
22861 for (++glyph;
22862 (glyph < last
22863 && glyph->type == STRETCH_GLYPH
22864 && glyph->voffset == voffset
22865 && glyph->face_id == face_id);
22866 ++glyph)
22867 s->width += glyph->pixel_width;
22869 /* Adjust base line for subscript/superscript text. */
22870 s->ybase += voffset;
22872 /* The case that face->gc == 0 is handled when drawing the glyph
22873 string by calling PREPARE_FACE_FOR_DISPLAY. */
22874 eassert (s->face);
22875 return glyph - s->row->glyphs[s->area];
22878 static struct font_metrics *
22879 get_per_char_metric (struct font *font, XChar2b *char2b)
22881 static struct font_metrics metrics;
22882 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22884 if (! font || code == FONT_INVALID_CODE)
22885 return NULL;
22886 font->driver->text_extents (font, &code, 1, &metrics);
22887 return &metrics;
22890 /* EXPORT for RIF:
22891 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22892 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22893 assumed to be zero. */
22895 void
22896 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22898 *left = *right = 0;
22900 if (glyph->type == CHAR_GLYPH)
22902 struct face *face;
22903 XChar2b char2b;
22904 struct font_metrics *pcm;
22906 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22907 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22909 if (pcm->rbearing > pcm->width)
22910 *right = pcm->rbearing - pcm->width;
22911 if (pcm->lbearing < 0)
22912 *left = -pcm->lbearing;
22915 else if (glyph->type == COMPOSITE_GLYPH)
22917 if (! glyph->u.cmp.automatic)
22919 struct composition *cmp = composition_table[glyph->u.cmp.id];
22921 if (cmp->rbearing > cmp->pixel_width)
22922 *right = cmp->rbearing - cmp->pixel_width;
22923 if (cmp->lbearing < 0)
22924 *left = - cmp->lbearing;
22926 else
22928 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22929 struct font_metrics metrics;
22931 composition_gstring_width (gstring, glyph->slice.cmp.from,
22932 glyph->slice.cmp.to + 1, &metrics);
22933 if (metrics.rbearing > metrics.width)
22934 *right = metrics.rbearing - metrics.width;
22935 if (metrics.lbearing < 0)
22936 *left = - metrics.lbearing;
22942 /* Return the index of the first glyph preceding glyph string S that
22943 is overwritten by S because of S's left overhang. Value is -1
22944 if no glyphs are overwritten. */
22946 static int
22947 left_overwritten (struct glyph_string *s)
22949 int k;
22951 if (s->left_overhang)
22953 int x = 0, i;
22954 struct glyph *glyphs = s->row->glyphs[s->area];
22955 int first = s->first_glyph - glyphs;
22957 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22958 x -= glyphs[i].pixel_width;
22960 k = i + 1;
22962 else
22963 k = -1;
22965 return k;
22969 /* Return the index of the first glyph preceding glyph string S that
22970 is overwriting S because of its right overhang. Value is -1 if no
22971 glyph in front of S overwrites S. */
22973 static int
22974 left_overwriting (struct glyph_string *s)
22976 int i, k, x;
22977 struct glyph *glyphs = s->row->glyphs[s->area];
22978 int first = s->first_glyph - glyphs;
22980 k = -1;
22981 x = 0;
22982 for (i = first - 1; i >= 0; --i)
22984 int left, right;
22985 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22986 if (x + right > 0)
22987 k = i;
22988 x -= glyphs[i].pixel_width;
22991 return k;
22995 /* Return the index of the last glyph following glyph string S that is
22996 overwritten by S because of S's right overhang. Value is -1 if
22997 no such glyph is found. */
22999 static int
23000 right_overwritten (struct glyph_string *s)
23002 int k = -1;
23004 if (s->right_overhang)
23006 int x = 0, i;
23007 struct glyph *glyphs = s->row->glyphs[s->area];
23008 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23009 int end = s->row->used[s->area];
23011 for (i = first; i < end && s->right_overhang > x; ++i)
23012 x += glyphs[i].pixel_width;
23014 k = i;
23017 return k;
23021 /* Return the index of the last glyph following glyph string S that
23022 overwrites S because of its left overhang. Value is negative
23023 if no such glyph is found. */
23025 static int
23026 right_overwriting (struct glyph_string *s)
23028 int i, k, x;
23029 int end = s->row->used[s->area];
23030 struct glyph *glyphs = s->row->glyphs[s->area];
23031 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
23033 k = -1;
23034 x = 0;
23035 for (i = first; i < end; ++i)
23037 int left, right;
23038 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23039 if (x - left < 0)
23040 k = i;
23041 x += glyphs[i].pixel_width;
23044 return k;
23048 /* Set background width of glyph string S. START is the index of the
23049 first glyph following S. LAST_X is the right-most x-position + 1
23050 in the drawing area. */
23052 static inline void
23053 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23055 /* If the face of this glyph string has to be drawn to the end of
23056 the drawing area, set S->extends_to_end_of_line_p. */
23058 if (start == s->row->used[s->area]
23059 && s->area == TEXT_AREA
23060 && ((s->row->fill_line_p
23061 && (s->hl == DRAW_NORMAL_TEXT
23062 || s->hl == DRAW_IMAGE_RAISED
23063 || s->hl == DRAW_IMAGE_SUNKEN))
23064 || s->hl == DRAW_MOUSE_FACE))
23065 s->extends_to_end_of_line_p = 1;
23067 /* If S extends its face to the end of the line, set its
23068 background_width to the distance to the right edge of the drawing
23069 area. */
23070 if (s->extends_to_end_of_line_p)
23071 s->background_width = last_x - s->x + 1;
23072 else
23073 s->background_width = s->width;
23077 /* Compute overhangs and x-positions for glyph string S and its
23078 predecessors, or successors. X is the starting x-position for S.
23079 BACKWARD_P non-zero means process predecessors. */
23081 static void
23082 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23084 if (backward_p)
23086 while (s)
23088 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23089 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23090 x -= s->width;
23091 s->x = x;
23092 s = s->prev;
23095 else
23097 while (s)
23099 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23100 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23101 s->x = x;
23102 x += s->width;
23103 s = s->next;
23110 /* The following macros are only called from draw_glyphs below.
23111 They reference the following parameters of that function directly:
23112 `w', `row', `area', and `overlap_p'
23113 as well as the following local variables:
23114 `s', `f', and `hdc' (in W32) */
23116 #ifdef HAVE_NTGUI
23117 /* On W32, silently add local `hdc' variable to argument list of
23118 init_glyph_string. */
23119 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23120 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23121 #else
23122 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23123 init_glyph_string (s, char2b, w, row, area, start, hl)
23124 #endif
23126 /* Add a glyph string for a stretch glyph to the list of strings
23127 between HEAD and TAIL. START is the index of the stretch glyph in
23128 row area AREA of glyph row ROW. END is the index of the last glyph
23129 in that glyph row area. X is the current output position assigned
23130 to the new glyph string constructed. HL overrides that face of the
23131 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23132 is the right-most x-position of the drawing area. */
23134 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23135 and below -- keep them on one line. */
23136 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23137 do \
23139 s = alloca (sizeof *s); \
23140 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23141 START = fill_stretch_glyph_string (s, START, END); \
23142 append_glyph_string (&HEAD, &TAIL, s); \
23143 s->x = (X); \
23145 while (0)
23148 /* Add a glyph string for an image glyph to the list of strings
23149 between HEAD and TAIL. START is the index of the image glyph in
23150 row area AREA of glyph row ROW. END is the index of the last glyph
23151 in that glyph row area. X is the current output position assigned
23152 to the new glyph string constructed. HL overrides that face of the
23153 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23154 is the right-most x-position of the drawing area. */
23156 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23157 do \
23159 s = alloca (sizeof *s); \
23160 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23161 fill_image_glyph_string (s); \
23162 append_glyph_string (&HEAD, &TAIL, s); \
23163 ++START; \
23164 s->x = (X); \
23166 while (0)
23169 /* Add a glyph string for a sequence of character glyphs to the list
23170 of strings between HEAD and TAIL. START is the index of the first
23171 glyph in row area AREA of glyph row ROW that is part of the new
23172 glyph string. END is the index of the last glyph in that glyph row
23173 area. X is the current output position assigned to the new glyph
23174 string constructed. HL overrides that face of the glyph; e.g. it
23175 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23176 right-most x-position of the drawing area. */
23178 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23179 do \
23181 int face_id; \
23182 XChar2b *char2b; \
23184 face_id = (row)->glyphs[area][START].face_id; \
23186 s = alloca (sizeof *s); \
23187 char2b = alloca ((END - START) * sizeof *char2b); \
23188 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23189 append_glyph_string (&HEAD, &TAIL, s); \
23190 s->x = (X); \
23191 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23193 while (0)
23196 /* Add a glyph string for a composite sequence to the list of strings
23197 between HEAD and TAIL. START is the index of the first glyph in
23198 row area AREA of glyph row ROW that is part of the new glyph
23199 string. END is the index of the last glyph in that glyph row area.
23200 X is the current output position assigned to the new glyph string
23201 constructed. HL overrides that face of the glyph; e.g. it is
23202 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23203 x-position of the drawing area. */
23205 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23206 do { \
23207 int face_id = (row)->glyphs[area][START].face_id; \
23208 struct face *base_face = FACE_FROM_ID (f, face_id); \
23209 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23210 struct composition *cmp = composition_table[cmp_id]; \
23211 XChar2b *char2b; \
23212 struct glyph_string *first_s = NULL; \
23213 int n; \
23215 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23217 /* Make glyph_strings for each glyph sequence that is drawable by \
23218 the same face, and append them to HEAD/TAIL. */ \
23219 for (n = 0; n < cmp->glyph_len;) \
23221 s = alloca (sizeof *s); \
23222 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23223 append_glyph_string (&(HEAD), &(TAIL), s); \
23224 s->cmp = cmp; \
23225 s->cmp_from = n; \
23226 s->x = (X); \
23227 if (n == 0) \
23228 first_s = s; \
23229 n = fill_composite_glyph_string (s, base_face, overlaps); \
23232 ++START; \
23233 s = first_s; \
23234 } while (0)
23237 /* Add a glyph string for a glyph-string sequence to the list of strings
23238 between HEAD and TAIL. */
23240 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23241 do { \
23242 int face_id; \
23243 XChar2b *char2b; \
23244 Lisp_Object gstring; \
23246 face_id = (row)->glyphs[area][START].face_id; \
23247 gstring = (composition_gstring_from_id \
23248 ((row)->glyphs[area][START].u.cmp.id)); \
23249 s = alloca (sizeof *s); \
23250 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23251 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23252 append_glyph_string (&(HEAD), &(TAIL), s); \
23253 s->x = (X); \
23254 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23255 } while (0)
23258 /* Add a glyph string for a sequence of glyphless character's glyphs
23259 to the list of strings between HEAD and TAIL. The meanings of
23260 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23262 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23263 do \
23265 int face_id; \
23267 face_id = (row)->glyphs[area][START].face_id; \
23269 s = alloca (sizeof *s); \
23270 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23271 append_glyph_string (&HEAD, &TAIL, s); \
23272 s->x = (X); \
23273 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23274 overlaps); \
23276 while (0)
23279 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23280 of AREA of glyph row ROW on window W between indices START and END.
23281 HL overrides the face for drawing glyph strings, e.g. it is
23282 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23283 x-positions of the drawing area.
23285 This is an ugly monster macro construct because we must use alloca
23286 to allocate glyph strings (because draw_glyphs can be called
23287 asynchronously). */
23289 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23290 do \
23292 HEAD = TAIL = NULL; \
23293 while (START < END) \
23295 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23296 switch (first_glyph->type) \
23298 case CHAR_GLYPH: \
23299 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23300 HL, X, LAST_X); \
23301 break; \
23303 case COMPOSITE_GLYPH: \
23304 if (first_glyph->u.cmp.automatic) \
23305 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23306 HL, X, LAST_X); \
23307 else \
23308 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23309 HL, X, LAST_X); \
23310 break; \
23312 case STRETCH_GLYPH: \
23313 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23314 HL, X, LAST_X); \
23315 break; \
23317 case IMAGE_GLYPH: \
23318 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23319 HL, X, LAST_X); \
23320 break; \
23322 case GLYPHLESS_GLYPH: \
23323 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23324 HL, X, LAST_X); \
23325 break; \
23327 default: \
23328 abort (); \
23331 if (s) \
23333 set_glyph_string_background_width (s, START, LAST_X); \
23334 (X) += s->width; \
23337 } while (0)
23340 /* Draw glyphs between START and END in AREA of ROW on window W,
23341 starting at x-position X. X is relative to AREA in W. HL is a
23342 face-override with the following meaning:
23344 DRAW_NORMAL_TEXT draw normally
23345 DRAW_CURSOR draw in cursor face
23346 DRAW_MOUSE_FACE draw in mouse face.
23347 DRAW_INVERSE_VIDEO draw in mode line face
23348 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23349 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23351 If OVERLAPS is non-zero, draw only the foreground of characters and
23352 clip to the physical height of ROW. Non-zero value also defines
23353 the overlapping part to be drawn:
23355 OVERLAPS_PRED overlap with preceding rows
23356 OVERLAPS_SUCC overlap with succeeding rows
23357 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23358 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23360 Value is the x-position reached, relative to AREA of W. */
23362 static int
23363 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23364 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23365 enum draw_glyphs_face hl, int overlaps)
23367 struct glyph_string *head, *tail;
23368 struct glyph_string *s;
23369 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23370 int i, j, x_reached, last_x, area_left = 0;
23371 struct frame *f = XFRAME (WINDOW_FRAME (w));
23372 DECLARE_HDC (hdc);
23374 ALLOCATE_HDC (hdc, f);
23376 /* Let's rather be paranoid than getting a SEGV. */
23377 end = min (end, row->used[area]);
23378 start = max (0, start);
23379 start = min (end, start);
23381 /* Translate X to frame coordinates. Set last_x to the right
23382 end of the drawing area. */
23383 if (row->full_width_p)
23385 /* X is relative to the left edge of W, without scroll bars
23386 or fringes. */
23387 area_left = WINDOW_LEFT_EDGE_X (w);
23388 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23390 else
23392 area_left = window_box_left (w, area);
23393 last_x = area_left + window_box_width (w, area);
23395 x += area_left;
23397 /* Build a doubly-linked list of glyph_string structures between
23398 head and tail from what we have to draw. Note that the macro
23399 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23400 the reason we use a separate variable `i'. */
23401 i = start;
23402 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23403 if (tail)
23404 x_reached = tail->x + tail->background_width;
23405 else
23406 x_reached = x;
23408 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23409 the row, redraw some glyphs in front or following the glyph
23410 strings built above. */
23411 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23413 struct glyph_string *h, *t;
23414 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23415 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23416 int check_mouse_face = 0;
23417 int dummy_x = 0;
23419 /* If mouse highlighting is on, we may need to draw adjacent
23420 glyphs using mouse-face highlighting. */
23421 if (area == TEXT_AREA && row->mouse_face_p)
23423 struct glyph_row *mouse_beg_row, *mouse_end_row;
23425 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23426 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23428 if (row >= mouse_beg_row && row <= mouse_end_row)
23430 check_mouse_face = 1;
23431 mouse_beg_col = (row == mouse_beg_row)
23432 ? hlinfo->mouse_face_beg_col : 0;
23433 mouse_end_col = (row == mouse_end_row)
23434 ? hlinfo->mouse_face_end_col
23435 : row->used[TEXT_AREA];
23439 /* Compute overhangs for all glyph strings. */
23440 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23441 for (s = head; s; s = s->next)
23442 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23444 /* Prepend glyph strings for glyphs in front of the first glyph
23445 string that are overwritten because of the first glyph
23446 string's left overhang. The background of all strings
23447 prepended must be drawn because the first glyph string
23448 draws over it. */
23449 i = left_overwritten (head);
23450 if (i >= 0)
23452 enum draw_glyphs_face overlap_hl;
23454 /* If this row contains mouse highlighting, attempt to draw
23455 the overlapped glyphs with the correct highlight. This
23456 code fails if the overlap encompasses more than one glyph
23457 and mouse-highlight spans only some of these glyphs.
23458 However, making it work perfectly involves a lot more
23459 code, and I don't know if the pathological case occurs in
23460 practice, so we'll stick to this for now. --- cyd */
23461 if (check_mouse_face
23462 && mouse_beg_col < start && mouse_end_col > i)
23463 overlap_hl = DRAW_MOUSE_FACE;
23464 else
23465 overlap_hl = DRAW_NORMAL_TEXT;
23467 j = i;
23468 BUILD_GLYPH_STRINGS (j, start, h, t,
23469 overlap_hl, dummy_x, last_x);
23470 start = i;
23471 compute_overhangs_and_x (t, head->x, 1);
23472 prepend_glyph_string_lists (&head, &tail, h, t);
23473 clip_head = head;
23476 /* Prepend glyph strings for glyphs in front of the first glyph
23477 string that overwrite that glyph string because of their
23478 right overhang. For these strings, only the foreground must
23479 be drawn, because it draws over the glyph string at `head'.
23480 The background must not be drawn because this would overwrite
23481 right overhangs of preceding glyphs for which no glyph
23482 strings exist. */
23483 i = left_overwriting (head);
23484 if (i >= 0)
23486 enum draw_glyphs_face overlap_hl;
23488 if (check_mouse_face
23489 && mouse_beg_col < start && mouse_end_col > i)
23490 overlap_hl = DRAW_MOUSE_FACE;
23491 else
23492 overlap_hl = DRAW_NORMAL_TEXT;
23494 clip_head = head;
23495 BUILD_GLYPH_STRINGS (i, start, h, t,
23496 overlap_hl, dummy_x, last_x);
23497 for (s = h; s; s = s->next)
23498 s->background_filled_p = 1;
23499 compute_overhangs_and_x (t, head->x, 1);
23500 prepend_glyph_string_lists (&head, &tail, h, t);
23503 /* Append glyphs strings for glyphs following the last glyph
23504 string tail that are overwritten by tail. The background of
23505 these strings has to be drawn because tail's foreground draws
23506 over it. */
23507 i = right_overwritten (tail);
23508 if (i >= 0)
23510 enum draw_glyphs_face overlap_hl;
23512 if (check_mouse_face
23513 && mouse_beg_col < i && mouse_end_col > end)
23514 overlap_hl = DRAW_MOUSE_FACE;
23515 else
23516 overlap_hl = DRAW_NORMAL_TEXT;
23518 BUILD_GLYPH_STRINGS (end, i, h, t,
23519 overlap_hl, x, last_x);
23520 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23521 we don't have `end = i;' here. */
23522 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23523 append_glyph_string_lists (&head, &tail, h, t);
23524 clip_tail = tail;
23527 /* Append glyph strings for glyphs following the last glyph
23528 string tail that overwrite tail. The foreground of such
23529 glyphs has to be drawn because it writes into the background
23530 of tail. The background must not be drawn because it could
23531 paint over the foreground of following glyphs. */
23532 i = right_overwriting (tail);
23533 if (i >= 0)
23535 enum draw_glyphs_face overlap_hl;
23536 if (check_mouse_face
23537 && mouse_beg_col < i && mouse_end_col > end)
23538 overlap_hl = DRAW_MOUSE_FACE;
23539 else
23540 overlap_hl = DRAW_NORMAL_TEXT;
23542 clip_tail = tail;
23543 i++; /* We must include the Ith glyph. */
23544 BUILD_GLYPH_STRINGS (end, i, h, t,
23545 overlap_hl, x, last_x);
23546 for (s = h; s; s = s->next)
23547 s->background_filled_p = 1;
23548 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23549 append_glyph_string_lists (&head, &tail, h, t);
23551 if (clip_head || clip_tail)
23552 for (s = head; s; s = s->next)
23554 s->clip_head = clip_head;
23555 s->clip_tail = clip_tail;
23559 /* Draw all strings. */
23560 for (s = head; s; s = s->next)
23561 FRAME_RIF (f)->draw_glyph_string (s);
23563 #ifndef HAVE_NS
23564 /* When focus a sole frame and move horizontally, this sets on_p to 0
23565 causing a failure to erase prev cursor position. */
23566 if (area == TEXT_AREA
23567 && !row->full_width_p
23568 /* When drawing overlapping rows, only the glyph strings'
23569 foreground is drawn, which doesn't erase a cursor
23570 completely. */
23571 && !overlaps)
23573 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23574 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23575 : (tail ? tail->x + tail->background_width : x));
23576 x0 -= area_left;
23577 x1 -= area_left;
23579 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23580 row->y, MATRIX_ROW_BOTTOM_Y (row));
23582 #endif
23584 /* Value is the x-position up to which drawn, relative to AREA of W.
23585 This doesn't include parts drawn because of overhangs. */
23586 if (row->full_width_p)
23587 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23588 else
23589 x_reached -= area_left;
23591 RELEASE_HDC (hdc, f);
23593 return x_reached;
23596 /* Expand row matrix if too narrow. Don't expand if area
23597 is not present. */
23599 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23601 if (!fonts_changed_p \
23602 && (it->glyph_row->glyphs[area] \
23603 < it->glyph_row->glyphs[area + 1])) \
23605 it->w->ncols_scale_factor++; \
23606 fonts_changed_p = 1; \
23610 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23611 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23613 static inline void
23614 append_glyph (struct it *it)
23616 struct glyph *glyph;
23617 enum glyph_row_area area = it->area;
23619 eassert (it->glyph_row);
23620 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23622 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23623 if (glyph < it->glyph_row->glyphs[area + 1])
23625 /* If the glyph row is reversed, we need to prepend the glyph
23626 rather than append it. */
23627 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23629 struct glyph *g;
23631 /* Make room for the additional glyph. */
23632 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23633 g[1] = *g;
23634 glyph = it->glyph_row->glyphs[area];
23636 glyph->charpos = CHARPOS (it->position);
23637 glyph->object = it->object;
23638 if (it->pixel_width > 0)
23640 glyph->pixel_width = it->pixel_width;
23641 glyph->padding_p = 0;
23643 else
23645 /* Assure at least 1-pixel width. Otherwise, cursor can't
23646 be displayed correctly. */
23647 glyph->pixel_width = 1;
23648 glyph->padding_p = 1;
23650 glyph->ascent = it->ascent;
23651 glyph->descent = it->descent;
23652 glyph->voffset = it->voffset;
23653 glyph->type = CHAR_GLYPH;
23654 glyph->avoid_cursor_p = it->avoid_cursor_p;
23655 glyph->multibyte_p = it->multibyte_p;
23656 glyph->left_box_line_p = it->start_of_box_run_p;
23657 glyph->right_box_line_p = it->end_of_box_run_p;
23658 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23659 || it->phys_descent > it->descent);
23660 glyph->glyph_not_available_p = it->glyph_not_available_p;
23661 glyph->face_id = it->face_id;
23662 glyph->u.ch = it->char_to_display;
23663 glyph->slice.img = null_glyph_slice;
23664 glyph->font_type = FONT_TYPE_UNKNOWN;
23665 if (it->bidi_p)
23667 glyph->resolved_level = it->bidi_it.resolved_level;
23668 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23669 abort ();
23670 glyph->bidi_type = it->bidi_it.type;
23672 else
23674 glyph->resolved_level = 0;
23675 glyph->bidi_type = UNKNOWN_BT;
23677 ++it->glyph_row->used[area];
23679 else
23680 IT_EXPAND_MATRIX_WIDTH (it, area);
23683 /* Store one glyph for the composition IT->cmp_it.id in
23684 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23685 non-null. */
23687 static inline void
23688 append_composite_glyph (struct it *it)
23690 struct glyph *glyph;
23691 enum glyph_row_area area = it->area;
23693 eassert (it->glyph_row);
23695 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23696 if (glyph < it->glyph_row->glyphs[area + 1])
23698 /* If the glyph row is reversed, we need to prepend the glyph
23699 rather than append it. */
23700 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23702 struct glyph *g;
23704 /* Make room for the new glyph. */
23705 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23706 g[1] = *g;
23707 glyph = it->glyph_row->glyphs[it->area];
23709 glyph->charpos = it->cmp_it.charpos;
23710 glyph->object = it->object;
23711 glyph->pixel_width = it->pixel_width;
23712 glyph->ascent = it->ascent;
23713 glyph->descent = it->descent;
23714 glyph->voffset = it->voffset;
23715 glyph->type = COMPOSITE_GLYPH;
23716 if (it->cmp_it.ch < 0)
23718 glyph->u.cmp.automatic = 0;
23719 glyph->u.cmp.id = it->cmp_it.id;
23720 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23722 else
23724 glyph->u.cmp.automatic = 1;
23725 glyph->u.cmp.id = it->cmp_it.id;
23726 glyph->slice.cmp.from = it->cmp_it.from;
23727 glyph->slice.cmp.to = it->cmp_it.to - 1;
23729 glyph->avoid_cursor_p = it->avoid_cursor_p;
23730 glyph->multibyte_p = it->multibyte_p;
23731 glyph->left_box_line_p = it->start_of_box_run_p;
23732 glyph->right_box_line_p = it->end_of_box_run_p;
23733 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23734 || it->phys_descent > it->descent);
23735 glyph->padding_p = 0;
23736 glyph->glyph_not_available_p = 0;
23737 glyph->face_id = it->face_id;
23738 glyph->font_type = FONT_TYPE_UNKNOWN;
23739 if (it->bidi_p)
23741 glyph->resolved_level = it->bidi_it.resolved_level;
23742 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23743 abort ();
23744 glyph->bidi_type = it->bidi_it.type;
23746 ++it->glyph_row->used[area];
23748 else
23749 IT_EXPAND_MATRIX_WIDTH (it, area);
23753 /* Change IT->ascent and IT->height according to the setting of
23754 IT->voffset. */
23756 static inline void
23757 take_vertical_position_into_account (struct it *it)
23759 if (it->voffset)
23761 if (it->voffset < 0)
23762 /* Increase the ascent so that we can display the text higher
23763 in the line. */
23764 it->ascent -= it->voffset;
23765 else
23766 /* Increase the descent so that we can display the text lower
23767 in the line. */
23768 it->descent += it->voffset;
23773 /* Produce glyphs/get display metrics for the image IT is loaded with.
23774 See the description of struct display_iterator in dispextern.h for
23775 an overview of struct display_iterator. */
23777 static void
23778 produce_image_glyph (struct it *it)
23780 struct image *img;
23781 struct face *face;
23782 int glyph_ascent, crop;
23783 struct glyph_slice slice;
23785 eassert (it->what == IT_IMAGE);
23787 face = FACE_FROM_ID (it->f, it->face_id);
23788 eassert (face);
23789 /* Make sure X resources of the face is loaded. */
23790 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23792 if (it->image_id < 0)
23794 /* Fringe bitmap. */
23795 it->ascent = it->phys_ascent = 0;
23796 it->descent = it->phys_descent = 0;
23797 it->pixel_width = 0;
23798 it->nglyphs = 0;
23799 return;
23802 img = IMAGE_FROM_ID (it->f, it->image_id);
23803 eassert (img);
23804 /* Make sure X resources of the image is loaded. */
23805 prepare_image_for_display (it->f, img);
23807 slice.x = slice.y = 0;
23808 slice.width = img->width;
23809 slice.height = img->height;
23811 if (INTEGERP (it->slice.x))
23812 slice.x = XINT (it->slice.x);
23813 else if (FLOATP (it->slice.x))
23814 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23816 if (INTEGERP (it->slice.y))
23817 slice.y = XINT (it->slice.y);
23818 else if (FLOATP (it->slice.y))
23819 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23821 if (INTEGERP (it->slice.width))
23822 slice.width = XINT (it->slice.width);
23823 else if (FLOATP (it->slice.width))
23824 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23826 if (INTEGERP (it->slice.height))
23827 slice.height = XINT (it->slice.height);
23828 else if (FLOATP (it->slice.height))
23829 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23831 if (slice.x >= img->width)
23832 slice.x = img->width;
23833 if (slice.y >= img->height)
23834 slice.y = img->height;
23835 if (slice.x + slice.width >= img->width)
23836 slice.width = img->width - slice.x;
23837 if (slice.y + slice.height > img->height)
23838 slice.height = img->height - slice.y;
23840 if (slice.width == 0 || slice.height == 0)
23841 return;
23843 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23845 it->descent = slice.height - glyph_ascent;
23846 if (slice.y == 0)
23847 it->descent += img->vmargin;
23848 if (slice.y + slice.height == img->height)
23849 it->descent += img->vmargin;
23850 it->phys_descent = it->descent;
23852 it->pixel_width = slice.width;
23853 if (slice.x == 0)
23854 it->pixel_width += img->hmargin;
23855 if (slice.x + slice.width == img->width)
23856 it->pixel_width += img->hmargin;
23858 /* It's quite possible for images to have an ascent greater than
23859 their height, so don't get confused in that case. */
23860 if (it->descent < 0)
23861 it->descent = 0;
23863 it->nglyphs = 1;
23865 if (face->box != FACE_NO_BOX)
23867 if (face->box_line_width > 0)
23869 if (slice.y == 0)
23870 it->ascent += face->box_line_width;
23871 if (slice.y + slice.height == img->height)
23872 it->descent += face->box_line_width;
23875 if (it->start_of_box_run_p && slice.x == 0)
23876 it->pixel_width += eabs (face->box_line_width);
23877 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23878 it->pixel_width += eabs (face->box_line_width);
23881 take_vertical_position_into_account (it);
23883 /* Automatically crop wide image glyphs at right edge so we can
23884 draw the cursor on same display row. */
23885 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23886 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23888 it->pixel_width -= crop;
23889 slice.width -= crop;
23892 if (it->glyph_row)
23894 struct glyph *glyph;
23895 enum glyph_row_area area = it->area;
23897 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23898 if (glyph < it->glyph_row->glyphs[area + 1])
23900 glyph->charpos = CHARPOS (it->position);
23901 glyph->object = it->object;
23902 glyph->pixel_width = it->pixel_width;
23903 glyph->ascent = glyph_ascent;
23904 glyph->descent = it->descent;
23905 glyph->voffset = it->voffset;
23906 glyph->type = IMAGE_GLYPH;
23907 glyph->avoid_cursor_p = it->avoid_cursor_p;
23908 glyph->multibyte_p = it->multibyte_p;
23909 glyph->left_box_line_p = it->start_of_box_run_p;
23910 glyph->right_box_line_p = it->end_of_box_run_p;
23911 glyph->overlaps_vertically_p = 0;
23912 glyph->padding_p = 0;
23913 glyph->glyph_not_available_p = 0;
23914 glyph->face_id = it->face_id;
23915 glyph->u.img_id = img->id;
23916 glyph->slice.img = slice;
23917 glyph->font_type = FONT_TYPE_UNKNOWN;
23918 if (it->bidi_p)
23920 glyph->resolved_level = it->bidi_it.resolved_level;
23921 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23922 abort ();
23923 glyph->bidi_type = it->bidi_it.type;
23925 ++it->glyph_row->used[area];
23927 else
23928 IT_EXPAND_MATRIX_WIDTH (it, area);
23933 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23934 of the glyph, WIDTH and HEIGHT are the width and height of the
23935 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23937 static void
23938 append_stretch_glyph (struct it *it, Lisp_Object object,
23939 int width, int height, int ascent)
23941 struct glyph *glyph;
23942 enum glyph_row_area area = it->area;
23944 eassert (ascent >= 0 && ascent <= height);
23946 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23947 if (glyph < it->glyph_row->glyphs[area + 1])
23949 /* If the glyph row is reversed, we need to prepend the glyph
23950 rather than append it. */
23951 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23953 struct glyph *g;
23955 /* Make room for the additional glyph. */
23956 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23957 g[1] = *g;
23958 glyph = it->glyph_row->glyphs[area];
23960 glyph->charpos = CHARPOS (it->position);
23961 glyph->object = object;
23962 glyph->pixel_width = width;
23963 glyph->ascent = ascent;
23964 glyph->descent = height - ascent;
23965 glyph->voffset = it->voffset;
23966 glyph->type = STRETCH_GLYPH;
23967 glyph->avoid_cursor_p = it->avoid_cursor_p;
23968 glyph->multibyte_p = it->multibyte_p;
23969 glyph->left_box_line_p = it->start_of_box_run_p;
23970 glyph->right_box_line_p = it->end_of_box_run_p;
23971 glyph->overlaps_vertically_p = 0;
23972 glyph->padding_p = 0;
23973 glyph->glyph_not_available_p = 0;
23974 glyph->face_id = it->face_id;
23975 glyph->u.stretch.ascent = ascent;
23976 glyph->u.stretch.height = height;
23977 glyph->slice.img = null_glyph_slice;
23978 glyph->font_type = FONT_TYPE_UNKNOWN;
23979 if (it->bidi_p)
23981 glyph->resolved_level = it->bidi_it.resolved_level;
23982 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23983 abort ();
23984 glyph->bidi_type = it->bidi_it.type;
23986 else
23988 glyph->resolved_level = 0;
23989 glyph->bidi_type = UNKNOWN_BT;
23991 ++it->glyph_row->used[area];
23993 else
23994 IT_EXPAND_MATRIX_WIDTH (it, area);
23997 #endif /* HAVE_WINDOW_SYSTEM */
23999 /* Produce a stretch glyph for iterator IT. IT->object is the value
24000 of the glyph property displayed. The value must be a list
24001 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24002 being recognized:
24004 1. `:width WIDTH' specifies that the space should be WIDTH *
24005 canonical char width wide. WIDTH may be an integer or floating
24006 point number.
24008 2. `:relative-width FACTOR' specifies that the width of the stretch
24009 should be computed from the width of the first character having the
24010 `glyph' property, and should be FACTOR times that width.
24012 3. `:align-to HPOS' specifies that the space should be wide enough
24013 to reach HPOS, a value in canonical character units.
24015 Exactly one of the above pairs must be present.
24017 4. `:height HEIGHT' specifies that the height of the stretch produced
24018 should be HEIGHT, measured in canonical character units.
24020 5. `:relative-height FACTOR' specifies that the height of the
24021 stretch should be FACTOR times the height of the characters having
24022 the glyph property.
24024 Either none or exactly one of 4 or 5 must be present.
24026 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24027 of the stretch should be used for the ascent of the stretch.
24028 ASCENT must be in the range 0 <= ASCENT <= 100. */
24030 void
24031 produce_stretch_glyph (struct it *it)
24033 /* (space :width WIDTH :height HEIGHT ...) */
24034 Lisp_Object prop, plist;
24035 int width = 0, height = 0, align_to = -1;
24036 int zero_width_ok_p = 0;
24037 int ascent = 0;
24038 double tem;
24039 struct face *face = NULL;
24040 struct font *font = NULL;
24042 #ifdef HAVE_WINDOW_SYSTEM
24043 int zero_height_ok_p = 0;
24045 if (FRAME_WINDOW_P (it->f))
24047 face = FACE_FROM_ID (it->f, it->face_id);
24048 font = face->font ? face->font : FRAME_FONT (it->f);
24049 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24051 #endif
24053 /* List should start with `space'. */
24054 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24055 plist = XCDR (it->object);
24057 /* Compute the width of the stretch. */
24058 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24059 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24061 /* Absolute width `:width WIDTH' specified and valid. */
24062 zero_width_ok_p = 1;
24063 width = (int)tem;
24065 #ifdef HAVE_WINDOW_SYSTEM
24066 else if (FRAME_WINDOW_P (it->f)
24067 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24069 /* Relative width `:relative-width FACTOR' specified and valid.
24070 Compute the width of the characters having the `glyph'
24071 property. */
24072 struct it it2;
24073 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24075 it2 = *it;
24076 if (it->multibyte_p)
24077 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24078 else
24080 it2.c = it2.char_to_display = *p, it2.len = 1;
24081 if (! ASCII_CHAR_P (it2.c))
24082 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24085 it2.glyph_row = NULL;
24086 it2.what = IT_CHARACTER;
24087 x_produce_glyphs (&it2);
24088 width = NUMVAL (prop) * it2.pixel_width;
24090 #endif /* HAVE_WINDOW_SYSTEM */
24091 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24092 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24094 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24095 align_to = (align_to < 0
24097 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24098 else if (align_to < 0)
24099 align_to = window_box_left_offset (it->w, TEXT_AREA);
24100 width = max (0, (int)tem + align_to - it->current_x);
24101 zero_width_ok_p = 1;
24103 else
24104 /* Nothing specified -> width defaults to canonical char width. */
24105 width = FRAME_COLUMN_WIDTH (it->f);
24107 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24108 width = 1;
24110 #ifdef HAVE_WINDOW_SYSTEM
24111 /* Compute height. */
24112 if (FRAME_WINDOW_P (it->f))
24114 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24115 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24117 height = (int)tem;
24118 zero_height_ok_p = 1;
24120 else if (prop = Fplist_get (plist, QCrelative_height),
24121 NUMVAL (prop) > 0)
24122 height = FONT_HEIGHT (font) * NUMVAL (prop);
24123 else
24124 height = FONT_HEIGHT (font);
24126 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24127 height = 1;
24129 /* Compute percentage of height used for ascent. If
24130 `:ascent ASCENT' is present and valid, use that. Otherwise,
24131 derive the ascent from the font in use. */
24132 if (prop = Fplist_get (plist, QCascent),
24133 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24134 ascent = height * NUMVAL (prop) / 100.0;
24135 else if (!NILP (prop)
24136 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24137 ascent = min (max (0, (int)tem), height);
24138 else
24139 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24141 else
24142 #endif /* HAVE_WINDOW_SYSTEM */
24143 height = 1;
24145 if (width > 0 && it->line_wrap != TRUNCATE
24146 && it->current_x + width > it->last_visible_x)
24148 width = it->last_visible_x - it->current_x;
24149 #ifdef HAVE_WINDOW_SYSTEM
24150 /* Subtract one more pixel from the stretch width, but only on
24151 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24152 width -= FRAME_WINDOW_P (it->f);
24153 #endif
24156 if (width > 0 && height > 0 && it->glyph_row)
24158 Lisp_Object o_object = it->object;
24159 Lisp_Object object = it->stack[it->sp - 1].string;
24160 int n = width;
24162 if (!STRINGP (object))
24163 object = it->w->buffer;
24164 #ifdef HAVE_WINDOW_SYSTEM
24165 if (FRAME_WINDOW_P (it->f))
24166 append_stretch_glyph (it, object, width, height, ascent);
24167 else
24168 #endif
24170 it->object = object;
24171 it->char_to_display = ' ';
24172 it->pixel_width = it->len = 1;
24173 while (n--)
24174 tty_append_glyph (it);
24175 it->object = o_object;
24179 it->pixel_width = width;
24180 #ifdef HAVE_WINDOW_SYSTEM
24181 if (FRAME_WINDOW_P (it->f))
24183 it->ascent = it->phys_ascent = ascent;
24184 it->descent = it->phys_descent = height - it->ascent;
24185 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24186 take_vertical_position_into_account (it);
24188 else
24189 #endif
24190 it->nglyphs = width;
24193 /* Get information about special display element WHAT in an
24194 environment described by IT. WHAT is one of IT_TRUNCATION or
24195 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24196 non-null glyph_row member. This function ensures that fields like
24197 face_id, c, len of IT are left untouched. */
24199 static void
24200 produce_special_glyphs (struct it *it, enum display_element_type what)
24202 struct it temp_it;
24203 Lisp_Object gc;
24204 GLYPH glyph;
24206 temp_it = *it;
24207 temp_it.object = make_number (0);
24208 memset (&temp_it.current, 0, sizeof temp_it.current);
24210 if (what == IT_CONTINUATION)
24212 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24213 if (it->bidi_it.paragraph_dir == R2L)
24214 SET_GLYPH_FROM_CHAR (glyph, '/');
24215 else
24216 SET_GLYPH_FROM_CHAR (glyph, '\\');
24217 if (it->dp
24218 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24220 /* FIXME: Should we mirror GC for R2L lines? */
24221 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24222 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24225 else if (what == IT_TRUNCATION)
24227 /* Truncation glyph. */
24228 SET_GLYPH_FROM_CHAR (glyph, '$');
24229 if (it->dp
24230 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24232 /* FIXME: Should we mirror GC for R2L lines? */
24233 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24234 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24237 else
24238 abort ();
24240 #ifdef HAVE_WINDOW_SYSTEM
24241 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24242 is turned off, we precede the truncation/continuation glyphs by a
24243 stretch glyph whose width is computed such that these special
24244 glyphs are aligned at the window margin, even when very different
24245 fonts are used in different glyph rows. */
24246 if (FRAME_WINDOW_P (temp_it.f)
24247 /* init_iterator calls this with it->glyph_row == NULL, and it
24248 wants only the pixel width of the truncation/continuation
24249 glyphs. */
24250 && temp_it.glyph_row
24251 /* insert_left_trunc_glyphs calls us at the beginning of the
24252 row, and it has its own calculation of the stretch glyph
24253 width. */
24254 && temp_it.glyph_row->used[TEXT_AREA] > 0
24255 && (temp_it.glyph_row->reversed_p
24256 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24257 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24259 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24261 if (stretch_width > 0)
24263 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24264 struct font *font =
24265 face->font ? face->font : FRAME_FONT (temp_it.f);
24266 int stretch_ascent =
24267 (((temp_it.ascent + temp_it.descent)
24268 * FONT_BASE (font)) / FONT_HEIGHT (font));
24270 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24271 temp_it.ascent + temp_it.descent,
24272 stretch_ascent);
24275 #endif
24277 temp_it.dp = NULL;
24278 temp_it.what = IT_CHARACTER;
24279 temp_it.len = 1;
24280 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24281 temp_it.face_id = GLYPH_FACE (glyph);
24282 temp_it.len = CHAR_BYTES (temp_it.c);
24284 PRODUCE_GLYPHS (&temp_it);
24285 it->pixel_width = temp_it.pixel_width;
24286 it->nglyphs = temp_it.pixel_width;
24289 #ifdef HAVE_WINDOW_SYSTEM
24291 /* Calculate line-height and line-spacing properties.
24292 An integer value specifies explicit pixel value.
24293 A float value specifies relative value to current face height.
24294 A cons (float . face-name) specifies relative value to
24295 height of specified face font.
24297 Returns height in pixels, or nil. */
24300 static Lisp_Object
24301 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24302 int boff, int override)
24304 Lisp_Object face_name = Qnil;
24305 int ascent, descent, height;
24307 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24308 return val;
24310 if (CONSP (val))
24312 face_name = XCAR (val);
24313 val = XCDR (val);
24314 if (!NUMBERP (val))
24315 val = make_number (1);
24316 if (NILP (face_name))
24318 height = it->ascent + it->descent;
24319 goto scale;
24323 if (NILP (face_name))
24325 font = FRAME_FONT (it->f);
24326 boff = FRAME_BASELINE_OFFSET (it->f);
24328 else if (EQ (face_name, Qt))
24330 override = 0;
24332 else
24334 int face_id;
24335 struct face *face;
24337 face_id = lookup_named_face (it->f, face_name, 0);
24338 if (face_id < 0)
24339 return make_number (-1);
24341 face = FACE_FROM_ID (it->f, face_id);
24342 font = face->font;
24343 if (font == NULL)
24344 return make_number (-1);
24345 boff = font->baseline_offset;
24346 if (font->vertical_centering)
24347 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24350 ascent = FONT_BASE (font) + boff;
24351 descent = FONT_DESCENT (font) - boff;
24353 if (override)
24355 it->override_ascent = ascent;
24356 it->override_descent = descent;
24357 it->override_boff = boff;
24360 height = ascent + descent;
24362 scale:
24363 if (FLOATP (val))
24364 height = (int)(XFLOAT_DATA (val) * height);
24365 else if (INTEGERP (val))
24366 height *= XINT (val);
24368 return make_number (height);
24372 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24373 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24374 and only if this is for a character for which no font was found.
24376 If the display method (it->glyphless_method) is
24377 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24378 length of the acronym or the hexadecimal string, UPPER_XOFF and
24379 UPPER_YOFF are pixel offsets for the upper part of the string,
24380 LOWER_XOFF and LOWER_YOFF are for the lower part.
24382 For the other display methods, LEN through LOWER_YOFF are zero. */
24384 static void
24385 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24386 short upper_xoff, short upper_yoff,
24387 short lower_xoff, short lower_yoff)
24389 struct glyph *glyph;
24390 enum glyph_row_area area = it->area;
24392 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24393 if (glyph < it->glyph_row->glyphs[area + 1])
24395 /* If the glyph row is reversed, we need to prepend the glyph
24396 rather than append it. */
24397 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24399 struct glyph *g;
24401 /* Make room for the additional glyph. */
24402 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24403 g[1] = *g;
24404 glyph = it->glyph_row->glyphs[area];
24406 glyph->charpos = CHARPOS (it->position);
24407 glyph->object = it->object;
24408 glyph->pixel_width = it->pixel_width;
24409 glyph->ascent = it->ascent;
24410 glyph->descent = it->descent;
24411 glyph->voffset = it->voffset;
24412 glyph->type = GLYPHLESS_GLYPH;
24413 glyph->u.glyphless.method = it->glyphless_method;
24414 glyph->u.glyphless.for_no_font = for_no_font;
24415 glyph->u.glyphless.len = len;
24416 glyph->u.glyphless.ch = it->c;
24417 glyph->slice.glyphless.upper_xoff = upper_xoff;
24418 glyph->slice.glyphless.upper_yoff = upper_yoff;
24419 glyph->slice.glyphless.lower_xoff = lower_xoff;
24420 glyph->slice.glyphless.lower_yoff = lower_yoff;
24421 glyph->avoid_cursor_p = it->avoid_cursor_p;
24422 glyph->multibyte_p = it->multibyte_p;
24423 glyph->left_box_line_p = it->start_of_box_run_p;
24424 glyph->right_box_line_p = it->end_of_box_run_p;
24425 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24426 || it->phys_descent > it->descent);
24427 glyph->padding_p = 0;
24428 glyph->glyph_not_available_p = 0;
24429 glyph->face_id = face_id;
24430 glyph->font_type = FONT_TYPE_UNKNOWN;
24431 if (it->bidi_p)
24433 glyph->resolved_level = it->bidi_it.resolved_level;
24434 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24435 abort ();
24436 glyph->bidi_type = it->bidi_it.type;
24438 ++it->glyph_row->used[area];
24440 else
24441 IT_EXPAND_MATRIX_WIDTH (it, area);
24445 /* Produce a glyph for a glyphless character for iterator IT.
24446 IT->glyphless_method specifies which method to use for displaying
24447 the character. See the description of enum
24448 glyphless_display_method in dispextern.h for the detail.
24450 FOR_NO_FONT is nonzero if and only if this is for a character for
24451 which no font was found. ACRONYM, if non-nil, is an acronym string
24452 for the character. */
24454 static void
24455 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24457 int face_id;
24458 struct face *face;
24459 struct font *font;
24460 int base_width, base_height, width, height;
24461 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24462 int len;
24464 /* Get the metrics of the base font. We always refer to the current
24465 ASCII face. */
24466 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24467 font = face->font ? face->font : FRAME_FONT (it->f);
24468 it->ascent = FONT_BASE (font) + font->baseline_offset;
24469 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24470 base_height = it->ascent + it->descent;
24471 base_width = font->average_width;
24473 /* Get a face ID for the glyph by utilizing a cache (the same way as
24474 done for `escape-glyph' in get_next_display_element). */
24475 if (it->f == last_glyphless_glyph_frame
24476 && it->face_id == last_glyphless_glyph_face_id)
24478 face_id = last_glyphless_glyph_merged_face_id;
24480 else
24482 /* Merge the `glyphless-char' face into the current face. */
24483 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24484 last_glyphless_glyph_frame = it->f;
24485 last_glyphless_glyph_face_id = it->face_id;
24486 last_glyphless_glyph_merged_face_id = face_id;
24489 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24491 it->pixel_width = THIN_SPACE_WIDTH;
24492 len = 0;
24493 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24495 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24497 width = CHAR_WIDTH (it->c);
24498 if (width == 0)
24499 width = 1;
24500 else if (width > 4)
24501 width = 4;
24502 it->pixel_width = base_width * width;
24503 len = 0;
24504 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24506 else
24508 char buf[7];
24509 const char *str;
24510 unsigned int code[6];
24511 int upper_len;
24512 int ascent, descent;
24513 struct font_metrics metrics_upper, metrics_lower;
24515 face = FACE_FROM_ID (it->f, face_id);
24516 font = face->font ? face->font : FRAME_FONT (it->f);
24517 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24519 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24521 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24522 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24523 if (CONSP (acronym))
24524 acronym = XCAR (acronym);
24525 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24527 else
24529 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24530 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24531 str = buf;
24533 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24534 code[len] = font->driver->encode_char (font, str[len]);
24535 upper_len = (len + 1) / 2;
24536 font->driver->text_extents (font, code, upper_len,
24537 &metrics_upper);
24538 font->driver->text_extents (font, code + upper_len, len - upper_len,
24539 &metrics_lower);
24543 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24544 width = max (metrics_upper.width, metrics_lower.width) + 4;
24545 upper_xoff = upper_yoff = 2; /* the typical case */
24546 if (base_width >= width)
24548 /* Align the upper to the left, the lower to the right. */
24549 it->pixel_width = base_width;
24550 lower_xoff = base_width - 2 - metrics_lower.width;
24552 else
24554 /* Center the shorter one. */
24555 it->pixel_width = width;
24556 if (metrics_upper.width >= metrics_lower.width)
24557 lower_xoff = (width - metrics_lower.width) / 2;
24558 else
24560 /* FIXME: This code doesn't look right. It formerly was
24561 missing the "lower_xoff = 0;", which couldn't have
24562 been right since it left lower_xoff uninitialized. */
24563 lower_xoff = 0;
24564 upper_xoff = (width - metrics_upper.width) / 2;
24568 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24569 top, bottom, and between upper and lower strings. */
24570 height = (metrics_upper.ascent + metrics_upper.descent
24571 + metrics_lower.ascent + metrics_lower.descent) + 5;
24572 /* Center vertically.
24573 H:base_height, D:base_descent
24574 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24576 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24577 descent = D - H/2 + h/2;
24578 lower_yoff = descent - 2 - ld;
24579 upper_yoff = lower_yoff - la - 1 - ud; */
24580 ascent = - (it->descent - (base_height + height + 1) / 2);
24581 descent = it->descent - (base_height - height) / 2;
24582 lower_yoff = descent - 2 - metrics_lower.descent;
24583 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24584 - metrics_upper.descent);
24585 /* Don't make the height shorter than the base height. */
24586 if (height > base_height)
24588 it->ascent = ascent;
24589 it->descent = descent;
24593 it->phys_ascent = it->ascent;
24594 it->phys_descent = it->descent;
24595 if (it->glyph_row)
24596 append_glyphless_glyph (it, face_id, for_no_font, len,
24597 upper_xoff, upper_yoff,
24598 lower_xoff, lower_yoff);
24599 it->nglyphs = 1;
24600 take_vertical_position_into_account (it);
24604 /* RIF:
24605 Produce glyphs/get display metrics for the display element IT is
24606 loaded with. See the description of struct it in dispextern.h
24607 for an overview of struct it. */
24609 void
24610 x_produce_glyphs (struct it *it)
24612 int extra_line_spacing = it->extra_line_spacing;
24614 it->glyph_not_available_p = 0;
24616 if (it->what == IT_CHARACTER)
24618 XChar2b char2b;
24619 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24620 struct font *font = face->font;
24621 struct font_metrics *pcm = NULL;
24622 int boff; /* baseline offset */
24624 if (font == NULL)
24626 /* When no suitable font is found, display this character by
24627 the method specified in the first extra slot of
24628 Vglyphless_char_display. */
24629 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24631 eassert (it->what == IT_GLYPHLESS);
24632 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24633 goto done;
24636 boff = font->baseline_offset;
24637 if (font->vertical_centering)
24638 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24640 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24642 int stretched_p;
24644 it->nglyphs = 1;
24646 if (it->override_ascent >= 0)
24648 it->ascent = it->override_ascent;
24649 it->descent = it->override_descent;
24650 boff = it->override_boff;
24652 else
24654 it->ascent = FONT_BASE (font) + boff;
24655 it->descent = FONT_DESCENT (font) - boff;
24658 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24660 pcm = get_per_char_metric (font, &char2b);
24661 if (pcm->width == 0
24662 && pcm->rbearing == 0 && pcm->lbearing == 0)
24663 pcm = NULL;
24666 if (pcm)
24668 it->phys_ascent = pcm->ascent + boff;
24669 it->phys_descent = pcm->descent - boff;
24670 it->pixel_width = pcm->width;
24672 else
24674 it->glyph_not_available_p = 1;
24675 it->phys_ascent = it->ascent;
24676 it->phys_descent = it->descent;
24677 it->pixel_width = font->space_width;
24680 if (it->constrain_row_ascent_descent_p)
24682 if (it->descent > it->max_descent)
24684 it->ascent += it->descent - it->max_descent;
24685 it->descent = it->max_descent;
24687 if (it->ascent > it->max_ascent)
24689 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24690 it->ascent = it->max_ascent;
24692 it->phys_ascent = min (it->phys_ascent, it->ascent);
24693 it->phys_descent = min (it->phys_descent, it->descent);
24694 extra_line_spacing = 0;
24697 /* If this is a space inside a region of text with
24698 `space-width' property, change its width. */
24699 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24700 if (stretched_p)
24701 it->pixel_width *= XFLOATINT (it->space_width);
24703 /* If face has a box, add the box thickness to the character
24704 height. If character has a box line to the left and/or
24705 right, add the box line width to the character's width. */
24706 if (face->box != FACE_NO_BOX)
24708 int thick = face->box_line_width;
24710 if (thick > 0)
24712 it->ascent += thick;
24713 it->descent += thick;
24715 else
24716 thick = -thick;
24718 if (it->start_of_box_run_p)
24719 it->pixel_width += thick;
24720 if (it->end_of_box_run_p)
24721 it->pixel_width += thick;
24724 /* If face has an overline, add the height of the overline
24725 (1 pixel) and a 1 pixel margin to the character height. */
24726 if (face->overline_p)
24727 it->ascent += overline_margin;
24729 if (it->constrain_row_ascent_descent_p)
24731 if (it->ascent > it->max_ascent)
24732 it->ascent = it->max_ascent;
24733 if (it->descent > it->max_descent)
24734 it->descent = it->max_descent;
24737 take_vertical_position_into_account (it);
24739 /* If we have to actually produce glyphs, do it. */
24740 if (it->glyph_row)
24742 if (stretched_p)
24744 /* Translate a space with a `space-width' property
24745 into a stretch glyph. */
24746 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24747 / FONT_HEIGHT (font));
24748 append_stretch_glyph (it, it->object, it->pixel_width,
24749 it->ascent + it->descent, ascent);
24751 else
24752 append_glyph (it);
24754 /* If characters with lbearing or rbearing are displayed
24755 in this line, record that fact in a flag of the
24756 glyph row. This is used to optimize X output code. */
24757 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24758 it->glyph_row->contains_overlapping_glyphs_p = 1;
24760 if (! stretched_p && it->pixel_width == 0)
24761 /* We assure that all visible glyphs have at least 1-pixel
24762 width. */
24763 it->pixel_width = 1;
24765 else if (it->char_to_display == '\n')
24767 /* A newline has no width, but we need the height of the
24768 line. But if previous part of the line sets a height,
24769 don't increase that height */
24771 Lisp_Object height;
24772 Lisp_Object total_height = Qnil;
24774 it->override_ascent = -1;
24775 it->pixel_width = 0;
24776 it->nglyphs = 0;
24778 height = get_it_property (it, Qline_height);
24779 /* Split (line-height total-height) list */
24780 if (CONSP (height)
24781 && CONSP (XCDR (height))
24782 && NILP (XCDR (XCDR (height))))
24784 total_height = XCAR (XCDR (height));
24785 height = XCAR (height);
24787 height = calc_line_height_property (it, height, font, boff, 1);
24789 if (it->override_ascent >= 0)
24791 it->ascent = it->override_ascent;
24792 it->descent = it->override_descent;
24793 boff = it->override_boff;
24795 else
24797 it->ascent = FONT_BASE (font) + boff;
24798 it->descent = FONT_DESCENT (font) - boff;
24801 if (EQ (height, Qt))
24803 if (it->descent > it->max_descent)
24805 it->ascent += it->descent - it->max_descent;
24806 it->descent = it->max_descent;
24808 if (it->ascent > it->max_ascent)
24810 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24811 it->ascent = it->max_ascent;
24813 it->phys_ascent = min (it->phys_ascent, it->ascent);
24814 it->phys_descent = min (it->phys_descent, it->descent);
24815 it->constrain_row_ascent_descent_p = 1;
24816 extra_line_spacing = 0;
24818 else
24820 Lisp_Object spacing;
24822 it->phys_ascent = it->ascent;
24823 it->phys_descent = it->descent;
24825 if ((it->max_ascent > 0 || it->max_descent > 0)
24826 && face->box != FACE_NO_BOX
24827 && face->box_line_width > 0)
24829 it->ascent += face->box_line_width;
24830 it->descent += face->box_line_width;
24832 if (!NILP (height)
24833 && XINT (height) > it->ascent + it->descent)
24834 it->ascent = XINT (height) - it->descent;
24836 if (!NILP (total_height))
24837 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24838 else
24840 spacing = get_it_property (it, Qline_spacing);
24841 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24843 if (INTEGERP (spacing))
24845 extra_line_spacing = XINT (spacing);
24846 if (!NILP (total_height))
24847 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24851 else /* i.e. (it->char_to_display == '\t') */
24853 if (font->space_width > 0)
24855 int tab_width = it->tab_width * font->space_width;
24856 int x = it->current_x + it->continuation_lines_width;
24857 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24859 /* If the distance from the current position to the next tab
24860 stop is less than a space character width, use the
24861 tab stop after that. */
24862 if (next_tab_x - x < font->space_width)
24863 next_tab_x += tab_width;
24865 it->pixel_width = next_tab_x - x;
24866 it->nglyphs = 1;
24867 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24868 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24870 if (it->glyph_row)
24872 append_stretch_glyph (it, it->object, it->pixel_width,
24873 it->ascent + it->descent, it->ascent);
24876 else
24878 it->pixel_width = 0;
24879 it->nglyphs = 1;
24883 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24885 /* A static composition.
24887 Note: A composition is represented as one glyph in the
24888 glyph matrix. There are no padding glyphs.
24890 Important note: pixel_width, ascent, and descent are the
24891 values of what is drawn by draw_glyphs (i.e. the values of
24892 the overall glyphs composed). */
24893 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24894 int boff; /* baseline offset */
24895 struct composition *cmp = composition_table[it->cmp_it.id];
24896 int glyph_len = cmp->glyph_len;
24897 struct font *font = face->font;
24899 it->nglyphs = 1;
24901 /* If we have not yet calculated pixel size data of glyphs of
24902 the composition for the current face font, calculate them
24903 now. Theoretically, we have to check all fonts for the
24904 glyphs, but that requires much time and memory space. So,
24905 here we check only the font of the first glyph. This may
24906 lead to incorrect display, but it's very rare, and C-l
24907 (recenter-top-bottom) can correct the display anyway. */
24908 if (! cmp->font || cmp->font != font)
24910 /* Ascent and descent of the font of the first character
24911 of this composition (adjusted by baseline offset).
24912 Ascent and descent of overall glyphs should not be less
24913 than these, respectively. */
24914 int font_ascent, font_descent, font_height;
24915 /* Bounding box of the overall glyphs. */
24916 int leftmost, rightmost, lowest, highest;
24917 int lbearing, rbearing;
24918 int i, width, ascent, descent;
24919 int left_padded = 0, right_padded = 0;
24920 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24921 XChar2b char2b;
24922 struct font_metrics *pcm;
24923 int font_not_found_p;
24924 ptrdiff_t pos;
24926 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24927 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24928 break;
24929 if (glyph_len < cmp->glyph_len)
24930 right_padded = 1;
24931 for (i = 0; i < glyph_len; i++)
24933 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24934 break;
24935 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24937 if (i > 0)
24938 left_padded = 1;
24940 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24941 : IT_CHARPOS (*it));
24942 /* If no suitable font is found, use the default font. */
24943 font_not_found_p = font == NULL;
24944 if (font_not_found_p)
24946 face = face->ascii_face;
24947 font = face->font;
24949 boff = font->baseline_offset;
24950 if (font->vertical_centering)
24951 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24952 font_ascent = FONT_BASE (font) + boff;
24953 font_descent = FONT_DESCENT (font) - boff;
24954 font_height = FONT_HEIGHT (font);
24956 cmp->font = font;
24958 pcm = NULL;
24959 if (! font_not_found_p)
24961 get_char_face_and_encoding (it->f, c, it->face_id,
24962 &char2b, 0);
24963 pcm = get_per_char_metric (font, &char2b);
24966 /* Initialize the bounding box. */
24967 if (pcm)
24969 width = cmp->glyph_len > 0 ? pcm->width : 0;
24970 ascent = pcm->ascent;
24971 descent = pcm->descent;
24972 lbearing = pcm->lbearing;
24973 rbearing = pcm->rbearing;
24975 else
24977 width = cmp->glyph_len > 0 ? font->space_width : 0;
24978 ascent = FONT_BASE (font);
24979 descent = FONT_DESCENT (font);
24980 lbearing = 0;
24981 rbearing = width;
24984 rightmost = width;
24985 leftmost = 0;
24986 lowest = - descent + boff;
24987 highest = ascent + boff;
24989 if (! font_not_found_p
24990 && font->default_ascent
24991 && CHAR_TABLE_P (Vuse_default_ascent)
24992 && !NILP (Faref (Vuse_default_ascent,
24993 make_number (it->char_to_display))))
24994 highest = font->default_ascent + boff;
24996 /* Draw the first glyph at the normal position. It may be
24997 shifted to right later if some other glyphs are drawn
24998 at the left. */
24999 cmp->offsets[i * 2] = 0;
25000 cmp->offsets[i * 2 + 1] = boff;
25001 cmp->lbearing = lbearing;
25002 cmp->rbearing = rbearing;
25004 /* Set cmp->offsets for the remaining glyphs. */
25005 for (i++; i < glyph_len; i++)
25007 int left, right, btm, top;
25008 int ch = COMPOSITION_GLYPH (cmp, i);
25009 int face_id;
25010 struct face *this_face;
25012 if (ch == '\t')
25013 ch = ' ';
25014 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25015 this_face = FACE_FROM_ID (it->f, face_id);
25016 font = this_face->font;
25018 if (font == NULL)
25019 pcm = NULL;
25020 else
25022 get_char_face_and_encoding (it->f, ch, face_id,
25023 &char2b, 0);
25024 pcm = get_per_char_metric (font, &char2b);
25026 if (! pcm)
25027 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25028 else
25030 width = pcm->width;
25031 ascent = pcm->ascent;
25032 descent = pcm->descent;
25033 lbearing = pcm->lbearing;
25034 rbearing = pcm->rbearing;
25035 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25037 /* Relative composition with or without
25038 alternate chars. */
25039 left = (leftmost + rightmost - width) / 2;
25040 btm = - descent + boff;
25041 if (font->relative_compose
25042 && (! CHAR_TABLE_P (Vignore_relative_composition)
25043 || NILP (Faref (Vignore_relative_composition,
25044 make_number (ch)))))
25047 if (- descent >= font->relative_compose)
25048 /* One extra pixel between two glyphs. */
25049 btm = highest + 1;
25050 else if (ascent <= 0)
25051 /* One extra pixel between two glyphs. */
25052 btm = lowest - 1 - ascent - descent;
25055 else
25057 /* A composition rule is specified by an integer
25058 value that encodes global and new reference
25059 points (GREF and NREF). GREF and NREF are
25060 specified by numbers as below:
25062 0---1---2 -- ascent
25066 9--10--11 -- center
25068 ---3---4---5--- baseline
25070 6---7---8 -- descent
25072 int rule = COMPOSITION_RULE (cmp, i);
25073 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25075 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25076 grefx = gref % 3, nrefx = nref % 3;
25077 grefy = gref / 3, nrefy = nref / 3;
25078 if (xoff)
25079 xoff = font_height * (xoff - 128) / 256;
25080 if (yoff)
25081 yoff = font_height * (yoff - 128) / 256;
25083 left = (leftmost
25084 + grefx * (rightmost - leftmost) / 2
25085 - nrefx * width / 2
25086 + xoff);
25088 btm = ((grefy == 0 ? highest
25089 : grefy == 1 ? 0
25090 : grefy == 2 ? lowest
25091 : (highest + lowest) / 2)
25092 - (nrefy == 0 ? ascent + descent
25093 : nrefy == 1 ? descent - boff
25094 : nrefy == 2 ? 0
25095 : (ascent + descent) / 2)
25096 + yoff);
25099 cmp->offsets[i * 2] = left;
25100 cmp->offsets[i * 2 + 1] = btm + descent;
25102 /* Update the bounding box of the overall glyphs. */
25103 if (width > 0)
25105 right = left + width;
25106 if (left < leftmost)
25107 leftmost = left;
25108 if (right > rightmost)
25109 rightmost = right;
25111 top = btm + descent + ascent;
25112 if (top > highest)
25113 highest = top;
25114 if (btm < lowest)
25115 lowest = btm;
25117 if (cmp->lbearing > left + lbearing)
25118 cmp->lbearing = left + lbearing;
25119 if (cmp->rbearing < left + rbearing)
25120 cmp->rbearing = left + rbearing;
25124 /* If there are glyphs whose x-offsets are negative,
25125 shift all glyphs to the right and make all x-offsets
25126 non-negative. */
25127 if (leftmost < 0)
25129 for (i = 0; i < cmp->glyph_len; i++)
25130 cmp->offsets[i * 2] -= leftmost;
25131 rightmost -= leftmost;
25132 cmp->lbearing -= leftmost;
25133 cmp->rbearing -= leftmost;
25136 if (left_padded && cmp->lbearing < 0)
25138 for (i = 0; i < cmp->glyph_len; i++)
25139 cmp->offsets[i * 2] -= cmp->lbearing;
25140 rightmost -= cmp->lbearing;
25141 cmp->rbearing -= cmp->lbearing;
25142 cmp->lbearing = 0;
25144 if (right_padded && rightmost < cmp->rbearing)
25146 rightmost = cmp->rbearing;
25149 cmp->pixel_width = rightmost;
25150 cmp->ascent = highest;
25151 cmp->descent = - lowest;
25152 if (cmp->ascent < font_ascent)
25153 cmp->ascent = font_ascent;
25154 if (cmp->descent < font_descent)
25155 cmp->descent = font_descent;
25158 if (it->glyph_row
25159 && (cmp->lbearing < 0
25160 || cmp->rbearing > cmp->pixel_width))
25161 it->glyph_row->contains_overlapping_glyphs_p = 1;
25163 it->pixel_width = cmp->pixel_width;
25164 it->ascent = it->phys_ascent = cmp->ascent;
25165 it->descent = it->phys_descent = cmp->descent;
25166 if (face->box != FACE_NO_BOX)
25168 int thick = face->box_line_width;
25170 if (thick > 0)
25172 it->ascent += thick;
25173 it->descent += thick;
25175 else
25176 thick = - thick;
25178 if (it->start_of_box_run_p)
25179 it->pixel_width += thick;
25180 if (it->end_of_box_run_p)
25181 it->pixel_width += thick;
25184 /* If face has an overline, add the height of the overline
25185 (1 pixel) and a 1 pixel margin to the character height. */
25186 if (face->overline_p)
25187 it->ascent += overline_margin;
25189 take_vertical_position_into_account (it);
25190 if (it->ascent < 0)
25191 it->ascent = 0;
25192 if (it->descent < 0)
25193 it->descent = 0;
25195 if (it->glyph_row && cmp->glyph_len > 0)
25196 append_composite_glyph (it);
25198 else if (it->what == IT_COMPOSITION)
25200 /* A dynamic (automatic) composition. */
25201 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25202 Lisp_Object gstring;
25203 struct font_metrics metrics;
25205 it->nglyphs = 1;
25207 gstring = composition_gstring_from_id (it->cmp_it.id);
25208 it->pixel_width
25209 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25210 &metrics);
25211 if (it->glyph_row
25212 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25213 it->glyph_row->contains_overlapping_glyphs_p = 1;
25214 it->ascent = it->phys_ascent = metrics.ascent;
25215 it->descent = it->phys_descent = metrics.descent;
25216 if (face->box != FACE_NO_BOX)
25218 int thick = face->box_line_width;
25220 if (thick > 0)
25222 it->ascent += thick;
25223 it->descent += thick;
25225 else
25226 thick = - thick;
25228 if (it->start_of_box_run_p)
25229 it->pixel_width += thick;
25230 if (it->end_of_box_run_p)
25231 it->pixel_width += thick;
25233 /* If face has an overline, add the height of the overline
25234 (1 pixel) and a 1 pixel margin to the character height. */
25235 if (face->overline_p)
25236 it->ascent += overline_margin;
25237 take_vertical_position_into_account (it);
25238 if (it->ascent < 0)
25239 it->ascent = 0;
25240 if (it->descent < 0)
25241 it->descent = 0;
25243 if (it->glyph_row)
25244 append_composite_glyph (it);
25246 else if (it->what == IT_GLYPHLESS)
25247 produce_glyphless_glyph (it, 0, Qnil);
25248 else if (it->what == IT_IMAGE)
25249 produce_image_glyph (it);
25250 else if (it->what == IT_STRETCH)
25251 produce_stretch_glyph (it);
25253 done:
25254 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25255 because this isn't true for images with `:ascent 100'. */
25256 eassert (it->ascent >= 0 && it->descent >= 0);
25257 if (it->area == TEXT_AREA)
25258 it->current_x += it->pixel_width;
25260 if (extra_line_spacing > 0)
25262 it->descent += extra_line_spacing;
25263 if (extra_line_spacing > it->max_extra_line_spacing)
25264 it->max_extra_line_spacing = extra_line_spacing;
25267 it->max_ascent = max (it->max_ascent, it->ascent);
25268 it->max_descent = max (it->max_descent, it->descent);
25269 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25270 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25273 /* EXPORT for RIF:
25274 Output LEN glyphs starting at START at the nominal cursor position.
25275 Advance the nominal cursor over the text. The global variable
25276 updated_window contains the window being updated, updated_row is
25277 the glyph row being updated, and updated_area is the area of that
25278 row being updated. */
25280 void
25281 x_write_glyphs (struct glyph *start, int len)
25283 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25285 eassert (updated_window && updated_row);
25286 /* When the window is hscrolled, cursor hpos can legitimately be out
25287 of bounds, but we draw the cursor at the corresponding window
25288 margin in that case. */
25289 if (!updated_row->reversed_p && chpos < 0)
25290 chpos = 0;
25291 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25292 chpos = updated_row->used[TEXT_AREA] - 1;
25294 BLOCK_INPUT;
25296 /* Write glyphs. */
25298 hpos = start - updated_row->glyphs[updated_area];
25299 x = draw_glyphs (updated_window, output_cursor.x,
25300 updated_row, updated_area,
25301 hpos, hpos + len,
25302 DRAW_NORMAL_TEXT, 0);
25304 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25305 if (updated_area == TEXT_AREA
25306 && updated_window->phys_cursor_on_p
25307 && updated_window->phys_cursor.vpos == output_cursor.vpos
25308 && chpos >= hpos
25309 && chpos < hpos + len)
25310 updated_window->phys_cursor_on_p = 0;
25312 UNBLOCK_INPUT;
25314 /* Advance the output cursor. */
25315 output_cursor.hpos += len;
25316 output_cursor.x = x;
25320 /* EXPORT for RIF:
25321 Insert LEN glyphs from START at the nominal cursor position. */
25323 void
25324 x_insert_glyphs (struct glyph *start, int len)
25326 struct frame *f;
25327 struct window *w;
25328 int line_height, shift_by_width, shifted_region_width;
25329 struct glyph_row *row;
25330 struct glyph *glyph;
25331 int frame_x, frame_y;
25332 ptrdiff_t hpos;
25334 eassert (updated_window && updated_row);
25335 BLOCK_INPUT;
25336 w = updated_window;
25337 f = XFRAME (WINDOW_FRAME (w));
25339 /* Get the height of the line we are in. */
25340 row = updated_row;
25341 line_height = row->height;
25343 /* Get the width of the glyphs to insert. */
25344 shift_by_width = 0;
25345 for (glyph = start; glyph < start + len; ++glyph)
25346 shift_by_width += glyph->pixel_width;
25348 /* Get the width of the region to shift right. */
25349 shifted_region_width = (window_box_width (w, updated_area)
25350 - output_cursor.x
25351 - shift_by_width);
25353 /* Shift right. */
25354 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25355 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25357 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25358 line_height, shift_by_width);
25360 /* Write the glyphs. */
25361 hpos = start - row->glyphs[updated_area];
25362 draw_glyphs (w, output_cursor.x, row, updated_area,
25363 hpos, hpos + len,
25364 DRAW_NORMAL_TEXT, 0);
25366 /* Advance the output cursor. */
25367 output_cursor.hpos += len;
25368 output_cursor.x += shift_by_width;
25369 UNBLOCK_INPUT;
25373 /* EXPORT for RIF:
25374 Erase the current text line from the nominal cursor position
25375 (inclusive) to pixel column TO_X (exclusive). The idea is that
25376 everything from TO_X onward is already erased.
25378 TO_X is a pixel position relative to updated_area of
25379 updated_window. TO_X == -1 means clear to the end of this area. */
25381 void
25382 x_clear_end_of_line (int to_x)
25384 struct frame *f;
25385 struct window *w = updated_window;
25386 int max_x, min_y, max_y;
25387 int from_x, from_y, to_y;
25389 eassert (updated_window && updated_row);
25390 f = XFRAME (w->frame);
25392 if (updated_row->full_width_p)
25393 max_x = WINDOW_TOTAL_WIDTH (w);
25394 else
25395 max_x = window_box_width (w, updated_area);
25396 max_y = window_text_bottom_y (w);
25398 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25399 of window. For TO_X > 0, truncate to end of drawing area. */
25400 if (to_x == 0)
25401 return;
25402 else if (to_x < 0)
25403 to_x = max_x;
25404 else
25405 to_x = min (to_x, max_x);
25407 to_y = min (max_y, output_cursor.y + updated_row->height);
25409 /* Notice if the cursor will be cleared by this operation. */
25410 if (!updated_row->full_width_p)
25411 notice_overwritten_cursor (w, updated_area,
25412 output_cursor.x, -1,
25413 updated_row->y,
25414 MATRIX_ROW_BOTTOM_Y (updated_row));
25416 from_x = output_cursor.x;
25418 /* Translate to frame coordinates. */
25419 if (updated_row->full_width_p)
25421 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25422 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25424 else
25426 int area_left = window_box_left (w, updated_area);
25427 from_x += area_left;
25428 to_x += area_left;
25431 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25432 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25433 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25435 /* Prevent inadvertently clearing to end of the X window. */
25436 if (to_x > from_x && to_y > from_y)
25438 BLOCK_INPUT;
25439 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25440 to_x - from_x, to_y - from_y);
25441 UNBLOCK_INPUT;
25445 #endif /* HAVE_WINDOW_SYSTEM */
25449 /***********************************************************************
25450 Cursor types
25451 ***********************************************************************/
25453 /* Value is the internal representation of the specified cursor type
25454 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25455 of the bar cursor. */
25457 static enum text_cursor_kinds
25458 get_specified_cursor_type (Lisp_Object arg, int *width)
25460 enum text_cursor_kinds type;
25462 if (NILP (arg))
25463 return NO_CURSOR;
25465 if (EQ (arg, Qbox))
25466 return FILLED_BOX_CURSOR;
25468 if (EQ (arg, Qhollow))
25469 return HOLLOW_BOX_CURSOR;
25471 if (EQ (arg, Qbar))
25473 *width = 2;
25474 return BAR_CURSOR;
25477 if (CONSP (arg)
25478 && EQ (XCAR (arg), Qbar)
25479 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25481 *width = XINT (XCDR (arg));
25482 return BAR_CURSOR;
25485 if (EQ (arg, Qhbar))
25487 *width = 2;
25488 return HBAR_CURSOR;
25491 if (CONSP (arg)
25492 && EQ (XCAR (arg), Qhbar)
25493 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25495 *width = XINT (XCDR (arg));
25496 return HBAR_CURSOR;
25499 /* Treat anything unknown as "hollow box cursor".
25500 It was bad to signal an error; people have trouble fixing
25501 .Xdefaults with Emacs, when it has something bad in it. */
25502 type = HOLLOW_BOX_CURSOR;
25504 return type;
25507 /* Set the default cursor types for specified frame. */
25508 void
25509 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25511 int width = 1;
25512 Lisp_Object tem;
25514 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25515 FRAME_CURSOR_WIDTH (f) = width;
25517 /* By default, set up the blink-off state depending on the on-state. */
25519 tem = Fassoc (arg, Vblink_cursor_alist);
25520 if (!NILP (tem))
25522 FRAME_BLINK_OFF_CURSOR (f)
25523 = get_specified_cursor_type (XCDR (tem), &width);
25524 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25526 else
25527 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25531 #ifdef HAVE_WINDOW_SYSTEM
25533 /* Return the cursor we want to be displayed in window W. Return
25534 width of bar/hbar cursor through WIDTH arg. Return with
25535 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25536 (i.e. if the `system caret' should track this cursor).
25538 In a mini-buffer window, we want the cursor only to appear if we
25539 are reading input from this window. For the selected window, we
25540 want the cursor type given by the frame parameter or buffer local
25541 setting of cursor-type. If explicitly marked off, draw no cursor.
25542 In all other cases, we want a hollow box cursor. */
25544 static enum text_cursor_kinds
25545 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25546 int *active_cursor)
25548 struct frame *f = XFRAME (w->frame);
25549 struct buffer *b = XBUFFER (w->buffer);
25550 int cursor_type = DEFAULT_CURSOR;
25551 Lisp_Object alt_cursor;
25552 int non_selected = 0;
25554 *active_cursor = 1;
25556 /* Echo area */
25557 if (cursor_in_echo_area
25558 && FRAME_HAS_MINIBUF_P (f)
25559 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25561 if (w == XWINDOW (echo_area_window))
25563 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25565 *width = FRAME_CURSOR_WIDTH (f);
25566 return FRAME_DESIRED_CURSOR (f);
25568 else
25569 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25572 *active_cursor = 0;
25573 non_selected = 1;
25576 /* Detect a nonselected window or nonselected frame. */
25577 else if (w != XWINDOW (f->selected_window)
25578 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25580 *active_cursor = 0;
25582 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25583 return NO_CURSOR;
25585 non_selected = 1;
25588 /* Never display a cursor in a window in which cursor-type is nil. */
25589 if (NILP (BVAR (b, cursor_type)))
25590 return NO_CURSOR;
25592 /* Get the normal cursor type for this window. */
25593 if (EQ (BVAR (b, cursor_type), Qt))
25595 cursor_type = FRAME_DESIRED_CURSOR (f);
25596 *width = FRAME_CURSOR_WIDTH (f);
25598 else
25599 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25601 /* Use cursor-in-non-selected-windows instead
25602 for non-selected window or frame. */
25603 if (non_selected)
25605 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25606 if (!EQ (Qt, alt_cursor))
25607 return get_specified_cursor_type (alt_cursor, width);
25608 /* t means modify the normal cursor type. */
25609 if (cursor_type == FILLED_BOX_CURSOR)
25610 cursor_type = HOLLOW_BOX_CURSOR;
25611 else if (cursor_type == BAR_CURSOR && *width > 1)
25612 --*width;
25613 return cursor_type;
25616 /* Use normal cursor if not blinked off. */
25617 if (!w->cursor_off_p)
25619 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25621 if (cursor_type == FILLED_BOX_CURSOR)
25623 /* Using a block cursor on large images can be very annoying.
25624 So use a hollow cursor for "large" images.
25625 If image is not transparent (no mask), also use hollow cursor. */
25626 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25627 if (img != NULL && IMAGEP (img->spec))
25629 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25630 where N = size of default frame font size.
25631 This should cover most of the "tiny" icons people may use. */
25632 if (!img->mask
25633 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25634 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25635 cursor_type = HOLLOW_BOX_CURSOR;
25638 else if (cursor_type != NO_CURSOR)
25640 /* Display current only supports BOX and HOLLOW cursors for images.
25641 So for now, unconditionally use a HOLLOW cursor when cursor is
25642 not a solid box cursor. */
25643 cursor_type = HOLLOW_BOX_CURSOR;
25646 return cursor_type;
25649 /* Cursor is blinked off, so determine how to "toggle" it. */
25651 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25652 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25653 return get_specified_cursor_type (XCDR (alt_cursor), width);
25655 /* Then see if frame has specified a specific blink off cursor type. */
25656 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25658 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25659 return FRAME_BLINK_OFF_CURSOR (f);
25662 #if 0
25663 /* Some people liked having a permanently visible blinking cursor,
25664 while others had very strong opinions against it. So it was
25665 decided to remove it. KFS 2003-09-03 */
25667 /* Finally perform built-in cursor blinking:
25668 filled box <-> hollow box
25669 wide [h]bar <-> narrow [h]bar
25670 narrow [h]bar <-> no cursor
25671 other type <-> no cursor */
25673 if (cursor_type == FILLED_BOX_CURSOR)
25674 return HOLLOW_BOX_CURSOR;
25676 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25678 *width = 1;
25679 return cursor_type;
25681 #endif
25683 return NO_CURSOR;
25687 /* Notice when the text cursor of window W has been completely
25688 overwritten by a drawing operation that outputs glyphs in AREA
25689 starting at X0 and ending at X1 in the line starting at Y0 and
25690 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25691 the rest of the line after X0 has been written. Y coordinates
25692 are window-relative. */
25694 static void
25695 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25696 int x0, int x1, int y0, int y1)
25698 int cx0, cx1, cy0, cy1;
25699 struct glyph_row *row;
25701 if (!w->phys_cursor_on_p)
25702 return;
25703 if (area != TEXT_AREA)
25704 return;
25706 if (w->phys_cursor.vpos < 0
25707 || w->phys_cursor.vpos >= w->current_matrix->nrows
25708 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25709 !(row->enabled_p && row->displays_text_p)))
25710 return;
25712 if (row->cursor_in_fringe_p)
25714 row->cursor_in_fringe_p = 0;
25715 draw_fringe_bitmap (w, row, row->reversed_p);
25716 w->phys_cursor_on_p = 0;
25717 return;
25720 cx0 = w->phys_cursor.x;
25721 cx1 = cx0 + w->phys_cursor_width;
25722 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25723 return;
25725 /* The cursor image will be completely removed from the
25726 screen if the output area intersects the cursor area in
25727 y-direction. When we draw in [y0 y1[, and some part of
25728 the cursor is at y < y0, that part must have been drawn
25729 before. When scrolling, the cursor is erased before
25730 actually scrolling, so we don't come here. When not
25731 scrolling, the rows above the old cursor row must have
25732 changed, and in this case these rows must have written
25733 over the cursor image.
25735 Likewise if part of the cursor is below y1, with the
25736 exception of the cursor being in the first blank row at
25737 the buffer and window end because update_text_area
25738 doesn't draw that row. (Except when it does, but
25739 that's handled in update_text_area.) */
25741 cy0 = w->phys_cursor.y;
25742 cy1 = cy0 + w->phys_cursor_height;
25743 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25744 return;
25746 w->phys_cursor_on_p = 0;
25749 #endif /* HAVE_WINDOW_SYSTEM */
25752 /************************************************************************
25753 Mouse Face
25754 ************************************************************************/
25756 #ifdef HAVE_WINDOW_SYSTEM
25758 /* EXPORT for RIF:
25759 Fix the display of area AREA of overlapping row ROW in window W
25760 with respect to the overlapping part OVERLAPS. */
25762 void
25763 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25764 enum glyph_row_area area, int overlaps)
25766 int i, x;
25768 BLOCK_INPUT;
25770 x = 0;
25771 for (i = 0; i < row->used[area];)
25773 if (row->glyphs[area][i].overlaps_vertically_p)
25775 int start = i, start_x = x;
25779 x += row->glyphs[area][i].pixel_width;
25780 ++i;
25782 while (i < row->used[area]
25783 && row->glyphs[area][i].overlaps_vertically_p);
25785 draw_glyphs (w, start_x, row, area,
25786 start, i,
25787 DRAW_NORMAL_TEXT, overlaps);
25789 else
25791 x += row->glyphs[area][i].pixel_width;
25792 ++i;
25796 UNBLOCK_INPUT;
25800 /* EXPORT:
25801 Draw the cursor glyph of window W in glyph row ROW. See the
25802 comment of draw_glyphs for the meaning of HL. */
25804 void
25805 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25806 enum draw_glyphs_face hl)
25808 /* If cursor hpos is out of bounds, don't draw garbage. This can
25809 happen in mini-buffer windows when switching between echo area
25810 glyphs and mini-buffer. */
25811 if ((row->reversed_p
25812 ? (w->phys_cursor.hpos >= 0)
25813 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25815 int on_p = w->phys_cursor_on_p;
25816 int x1;
25817 int hpos = w->phys_cursor.hpos;
25819 /* When the window is hscrolled, cursor hpos can legitimately be
25820 out of bounds, but we draw the cursor at the corresponding
25821 window margin in that case. */
25822 if (!row->reversed_p && hpos < 0)
25823 hpos = 0;
25824 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25825 hpos = row->used[TEXT_AREA] - 1;
25827 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25828 hl, 0);
25829 w->phys_cursor_on_p = on_p;
25831 if (hl == DRAW_CURSOR)
25832 w->phys_cursor_width = x1 - w->phys_cursor.x;
25833 /* When we erase the cursor, and ROW is overlapped by other
25834 rows, make sure that these overlapping parts of other rows
25835 are redrawn. */
25836 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25838 w->phys_cursor_width = x1 - w->phys_cursor.x;
25840 if (row > w->current_matrix->rows
25841 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25842 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25843 OVERLAPS_ERASED_CURSOR);
25845 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25846 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25847 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25848 OVERLAPS_ERASED_CURSOR);
25854 /* EXPORT:
25855 Erase the image of a cursor of window W from the screen. */
25857 void
25858 erase_phys_cursor (struct window *w)
25860 struct frame *f = XFRAME (w->frame);
25861 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25862 int hpos = w->phys_cursor.hpos;
25863 int vpos = w->phys_cursor.vpos;
25864 int mouse_face_here_p = 0;
25865 struct glyph_matrix *active_glyphs = w->current_matrix;
25866 struct glyph_row *cursor_row;
25867 struct glyph *cursor_glyph;
25868 enum draw_glyphs_face hl;
25870 /* No cursor displayed or row invalidated => nothing to do on the
25871 screen. */
25872 if (w->phys_cursor_type == NO_CURSOR)
25873 goto mark_cursor_off;
25875 /* VPOS >= active_glyphs->nrows means that window has been resized.
25876 Don't bother to erase the cursor. */
25877 if (vpos >= active_glyphs->nrows)
25878 goto mark_cursor_off;
25880 /* If row containing cursor is marked invalid, there is nothing we
25881 can do. */
25882 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25883 if (!cursor_row->enabled_p)
25884 goto mark_cursor_off;
25886 /* If line spacing is > 0, old cursor may only be partially visible in
25887 window after split-window. So adjust visible height. */
25888 cursor_row->visible_height = min (cursor_row->visible_height,
25889 window_text_bottom_y (w) - cursor_row->y);
25891 /* If row is completely invisible, don't attempt to delete a cursor which
25892 isn't there. This can happen if cursor is at top of a window, and
25893 we switch to a buffer with a header line in that window. */
25894 if (cursor_row->visible_height <= 0)
25895 goto mark_cursor_off;
25897 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25898 if (cursor_row->cursor_in_fringe_p)
25900 cursor_row->cursor_in_fringe_p = 0;
25901 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25902 goto mark_cursor_off;
25905 /* This can happen when the new row is shorter than the old one.
25906 In this case, either draw_glyphs or clear_end_of_line
25907 should have cleared the cursor. Note that we wouldn't be
25908 able to erase the cursor in this case because we don't have a
25909 cursor glyph at hand. */
25910 if ((cursor_row->reversed_p
25911 ? (w->phys_cursor.hpos < 0)
25912 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25913 goto mark_cursor_off;
25915 /* When the window is hscrolled, cursor hpos can legitimately be out
25916 of bounds, but we draw the cursor at the corresponding window
25917 margin in that case. */
25918 if (!cursor_row->reversed_p && hpos < 0)
25919 hpos = 0;
25920 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25921 hpos = cursor_row->used[TEXT_AREA] - 1;
25923 /* If the cursor is in the mouse face area, redisplay that when
25924 we clear the cursor. */
25925 if (! NILP (hlinfo->mouse_face_window)
25926 && coords_in_mouse_face_p (w, hpos, vpos)
25927 /* Don't redraw the cursor's spot in mouse face if it is at the
25928 end of a line (on a newline). The cursor appears there, but
25929 mouse highlighting does not. */
25930 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25931 mouse_face_here_p = 1;
25933 /* Maybe clear the display under the cursor. */
25934 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25936 int x, y, left_x;
25937 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25938 int width;
25940 cursor_glyph = get_phys_cursor_glyph (w);
25941 if (cursor_glyph == NULL)
25942 goto mark_cursor_off;
25944 width = cursor_glyph->pixel_width;
25945 left_x = window_box_left_offset (w, TEXT_AREA);
25946 x = w->phys_cursor.x;
25947 if (x < left_x)
25948 width -= left_x - x;
25949 width = min (width, window_box_width (w, TEXT_AREA) - x);
25950 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25951 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25953 if (width > 0)
25954 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25957 /* Erase the cursor by redrawing the character underneath it. */
25958 if (mouse_face_here_p)
25959 hl = DRAW_MOUSE_FACE;
25960 else
25961 hl = DRAW_NORMAL_TEXT;
25962 draw_phys_cursor_glyph (w, cursor_row, hl);
25964 mark_cursor_off:
25965 w->phys_cursor_on_p = 0;
25966 w->phys_cursor_type = NO_CURSOR;
25970 /* EXPORT:
25971 Display or clear cursor of window W. If ON is zero, clear the
25972 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25973 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25975 void
25976 display_and_set_cursor (struct window *w, int on,
25977 int hpos, int vpos, int x, int y)
25979 struct frame *f = XFRAME (w->frame);
25980 int new_cursor_type;
25981 int new_cursor_width;
25982 int active_cursor;
25983 struct glyph_row *glyph_row;
25984 struct glyph *glyph;
25986 /* This is pointless on invisible frames, and dangerous on garbaged
25987 windows and frames; in the latter case, the frame or window may
25988 be in the midst of changing its size, and x and y may be off the
25989 window. */
25990 if (! FRAME_VISIBLE_P (f)
25991 || FRAME_GARBAGED_P (f)
25992 || vpos >= w->current_matrix->nrows
25993 || hpos >= w->current_matrix->matrix_w)
25994 return;
25996 /* If cursor is off and we want it off, return quickly. */
25997 if (!on && !w->phys_cursor_on_p)
25998 return;
26000 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26001 /* If cursor row is not enabled, we don't really know where to
26002 display the cursor. */
26003 if (!glyph_row->enabled_p)
26005 w->phys_cursor_on_p = 0;
26006 return;
26009 glyph = NULL;
26010 if (!glyph_row->exact_window_width_line_p
26011 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26012 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26014 eassert (interrupt_input_blocked);
26016 /* Set new_cursor_type to the cursor we want to be displayed. */
26017 new_cursor_type = get_window_cursor_type (w, glyph,
26018 &new_cursor_width, &active_cursor);
26020 /* If cursor is currently being shown and we don't want it to be or
26021 it is in the wrong place, or the cursor type is not what we want,
26022 erase it. */
26023 if (w->phys_cursor_on_p
26024 && (!on
26025 || w->phys_cursor.x != x
26026 || w->phys_cursor.y != y
26027 || new_cursor_type != w->phys_cursor_type
26028 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26029 && new_cursor_width != w->phys_cursor_width)))
26030 erase_phys_cursor (w);
26032 /* Don't check phys_cursor_on_p here because that flag is only set
26033 to zero in some cases where we know that the cursor has been
26034 completely erased, to avoid the extra work of erasing the cursor
26035 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26036 still not be visible, or it has only been partly erased. */
26037 if (on)
26039 w->phys_cursor_ascent = glyph_row->ascent;
26040 w->phys_cursor_height = glyph_row->height;
26042 /* Set phys_cursor_.* before x_draw_.* is called because some
26043 of them may need the information. */
26044 w->phys_cursor.x = x;
26045 w->phys_cursor.y = glyph_row->y;
26046 w->phys_cursor.hpos = hpos;
26047 w->phys_cursor.vpos = vpos;
26050 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26051 new_cursor_type, new_cursor_width,
26052 on, active_cursor);
26056 /* Switch the display of W's cursor on or off, according to the value
26057 of ON. */
26059 static void
26060 update_window_cursor (struct window *w, int on)
26062 /* Don't update cursor in windows whose frame is in the process
26063 of being deleted. */
26064 if (w->current_matrix)
26066 int hpos = w->phys_cursor.hpos;
26067 int vpos = w->phys_cursor.vpos;
26068 struct glyph_row *row;
26070 if (vpos >= w->current_matrix->nrows
26071 || hpos >= w->current_matrix->matrix_w)
26072 return;
26074 row = MATRIX_ROW (w->current_matrix, vpos);
26076 /* When the window is hscrolled, cursor hpos can legitimately be
26077 out of bounds, but we draw the cursor at the corresponding
26078 window margin in that case. */
26079 if (!row->reversed_p && hpos < 0)
26080 hpos = 0;
26081 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26082 hpos = row->used[TEXT_AREA] - 1;
26084 BLOCK_INPUT;
26085 display_and_set_cursor (w, on, hpos, vpos,
26086 w->phys_cursor.x, w->phys_cursor.y);
26087 UNBLOCK_INPUT;
26092 /* Call update_window_cursor with parameter ON_P on all leaf windows
26093 in the window tree rooted at W. */
26095 static void
26096 update_cursor_in_window_tree (struct window *w, int on_p)
26098 while (w)
26100 if (!NILP (w->hchild))
26101 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26102 else if (!NILP (w->vchild))
26103 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26104 else
26105 update_window_cursor (w, on_p);
26107 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26112 /* EXPORT:
26113 Display the cursor on window W, or clear it, according to ON_P.
26114 Don't change the cursor's position. */
26116 void
26117 x_update_cursor (struct frame *f, int on_p)
26119 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26123 /* EXPORT:
26124 Clear the cursor of window W to background color, and mark the
26125 cursor as not shown. This is used when the text where the cursor
26126 is about to be rewritten. */
26128 void
26129 x_clear_cursor (struct window *w)
26131 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26132 update_window_cursor (w, 0);
26135 #endif /* HAVE_WINDOW_SYSTEM */
26137 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26138 and MSDOS. */
26139 static void
26140 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26141 int start_hpos, int end_hpos,
26142 enum draw_glyphs_face draw)
26144 #ifdef HAVE_WINDOW_SYSTEM
26145 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26147 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26148 return;
26150 #endif
26151 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26152 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26153 #endif
26156 /* Display the active region described by mouse_face_* according to DRAW. */
26158 static void
26159 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26161 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26162 struct frame *f = XFRAME (WINDOW_FRAME (w));
26164 if (/* If window is in the process of being destroyed, don't bother
26165 to do anything. */
26166 w->current_matrix != NULL
26167 /* Don't update mouse highlight if hidden */
26168 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26169 /* Recognize when we are called to operate on rows that don't exist
26170 anymore. This can happen when a window is split. */
26171 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26173 int phys_cursor_on_p = w->phys_cursor_on_p;
26174 struct glyph_row *row, *first, *last;
26176 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26177 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26179 for (row = first; row <= last && row->enabled_p; ++row)
26181 int start_hpos, end_hpos, start_x;
26183 /* For all but the first row, the highlight starts at column 0. */
26184 if (row == first)
26186 /* R2L rows have BEG and END in reversed order, but the
26187 screen drawing geometry is always left to right. So
26188 we need to mirror the beginning and end of the
26189 highlighted area in R2L rows. */
26190 if (!row->reversed_p)
26192 start_hpos = hlinfo->mouse_face_beg_col;
26193 start_x = hlinfo->mouse_face_beg_x;
26195 else if (row == last)
26197 start_hpos = hlinfo->mouse_face_end_col;
26198 start_x = hlinfo->mouse_face_end_x;
26200 else
26202 start_hpos = 0;
26203 start_x = 0;
26206 else if (row->reversed_p && row == last)
26208 start_hpos = hlinfo->mouse_face_end_col;
26209 start_x = hlinfo->mouse_face_end_x;
26211 else
26213 start_hpos = 0;
26214 start_x = 0;
26217 if (row == last)
26219 if (!row->reversed_p)
26220 end_hpos = hlinfo->mouse_face_end_col;
26221 else if (row == first)
26222 end_hpos = hlinfo->mouse_face_beg_col;
26223 else
26225 end_hpos = row->used[TEXT_AREA];
26226 if (draw == DRAW_NORMAL_TEXT)
26227 row->fill_line_p = 1; /* Clear to end of line */
26230 else if (row->reversed_p && row == first)
26231 end_hpos = hlinfo->mouse_face_beg_col;
26232 else
26234 end_hpos = row->used[TEXT_AREA];
26235 if (draw == DRAW_NORMAL_TEXT)
26236 row->fill_line_p = 1; /* Clear to end of line */
26239 if (end_hpos > start_hpos)
26241 draw_row_with_mouse_face (w, start_x, row,
26242 start_hpos, end_hpos, draw);
26244 row->mouse_face_p
26245 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26249 #ifdef HAVE_WINDOW_SYSTEM
26250 /* When we've written over the cursor, arrange for it to
26251 be displayed again. */
26252 if (FRAME_WINDOW_P (f)
26253 && phys_cursor_on_p && !w->phys_cursor_on_p)
26255 int hpos = w->phys_cursor.hpos;
26257 /* When the window is hscrolled, cursor hpos can legitimately be
26258 out of bounds, but we draw the cursor at the corresponding
26259 window margin in that case. */
26260 if (!row->reversed_p && hpos < 0)
26261 hpos = 0;
26262 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26263 hpos = row->used[TEXT_AREA] - 1;
26265 BLOCK_INPUT;
26266 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26267 w->phys_cursor.x, w->phys_cursor.y);
26268 UNBLOCK_INPUT;
26270 #endif /* HAVE_WINDOW_SYSTEM */
26273 #ifdef HAVE_WINDOW_SYSTEM
26274 /* Change the mouse cursor. */
26275 if (FRAME_WINDOW_P (f))
26277 if (draw == DRAW_NORMAL_TEXT
26278 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26279 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26280 else if (draw == DRAW_MOUSE_FACE)
26281 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26282 else
26283 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26285 #endif /* HAVE_WINDOW_SYSTEM */
26288 /* EXPORT:
26289 Clear out the mouse-highlighted active region.
26290 Redraw it un-highlighted first. Value is non-zero if mouse
26291 face was actually drawn unhighlighted. */
26294 clear_mouse_face (Mouse_HLInfo *hlinfo)
26296 int cleared = 0;
26298 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26300 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26301 cleared = 1;
26304 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26305 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26306 hlinfo->mouse_face_window = Qnil;
26307 hlinfo->mouse_face_overlay = Qnil;
26308 return cleared;
26311 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26312 within the mouse face on that window. */
26313 static int
26314 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26316 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26318 /* Quickly resolve the easy cases. */
26319 if (!(WINDOWP (hlinfo->mouse_face_window)
26320 && XWINDOW (hlinfo->mouse_face_window) == w))
26321 return 0;
26322 if (vpos < hlinfo->mouse_face_beg_row
26323 || vpos > hlinfo->mouse_face_end_row)
26324 return 0;
26325 if (vpos > hlinfo->mouse_face_beg_row
26326 && vpos < hlinfo->mouse_face_end_row)
26327 return 1;
26329 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26331 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26333 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26334 return 1;
26336 else if ((vpos == hlinfo->mouse_face_beg_row
26337 && hpos >= hlinfo->mouse_face_beg_col)
26338 || (vpos == hlinfo->mouse_face_end_row
26339 && hpos < hlinfo->mouse_face_end_col))
26340 return 1;
26342 else
26344 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26346 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26347 return 1;
26349 else if ((vpos == hlinfo->mouse_face_beg_row
26350 && hpos <= hlinfo->mouse_face_beg_col)
26351 || (vpos == hlinfo->mouse_face_end_row
26352 && hpos > hlinfo->mouse_face_end_col))
26353 return 1;
26355 return 0;
26359 /* EXPORT:
26360 Non-zero if physical cursor of window W is within mouse face. */
26363 cursor_in_mouse_face_p (struct window *w)
26365 int hpos = w->phys_cursor.hpos;
26366 int vpos = w->phys_cursor.vpos;
26367 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26369 /* When the window is hscrolled, cursor hpos can legitimately be out
26370 of bounds, but we draw the cursor at the corresponding window
26371 margin in that case. */
26372 if (!row->reversed_p && hpos < 0)
26373 hpos = 0;
26374 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26375 hpos = row->used[TEXT_AREA] - 1;
26377 return coords_in_mouse_face_p (w, hpos, vpos);
26382 /* Find the glyph rows START_ROW and END_ROW of window W that display
26383 characters between buffer positions START_CHARPOS and END_CHARPOS
26384 (excluding END_CHARPOS). DISP_STRING is a display string that
26385 covers these buffer positions. This is similar to
26386 row_containing_pos, but is more accurate when bidi reordering makes
26387 buffer positions change non-linearly with glyph rows. */
26388 static void
26389 rows_from_pos_range (struct window *w,
26390 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26391 Lisp_Object disp_string,
26392 struct glyph_row **start, struct glyph_row **end)
26394 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26395 int last_y = window_text_bottom_y (w);
26396 struct glyph_row *row;
26398 *start = NULL;
26399 *end = NULL;
26401 while (!first->enabled_p
26402 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26403 first++;
26405 /* Find the START row. */
26406 for (row = first;
26407 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26408 row++)
26410 /* A row can potentially be the START row if the range of the
26411 characters it displays intersects the range
26412 [START_CHARPOS..END_CHARPOS). */
26413 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26414 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26415 /* See the commentary in row_containing_pos, for the
26416 explanation of the complicated way to check whether
26417 some position is beyond the end of the characters
26418 displayed by a row. */
26419 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26420 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26421 && !row->ends_at_zv_p
26422 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26423 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26424 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26425 && !row->ends_at_zv_p
26426 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26428 /* Found a candidate row. Now make sure at least one of the
26429 glyphs it displays has a charpos from the range
26430 [START_CHARPOS..END_CHARPOS).
26432 This is not obvious because bidi reordering could make
26433 buffer positions of a row be 1,2,3,102,101,100, and if we
26434 want to highlight characters in [50..60), we don't want
26435 this row, even though [50..60) does intersect [1..103),
26436 the range of character positions given by the row's start
26437 and end positions. */
26438 struct glyph *g = row->glyphs[TEXT_AREA];
26439 struct glyph *e = g + row->used[TEXT_AREA];
26441 while (g < e)
26443 if (((BUFFERP (g->object) || INTEGERP (g->object))
26444 && start_charpos <= g->charpos && g->charpos < end_charpos)
26445 /* A glyph that comes from DISP_STRING is by
26446 definition to be highlighted. */
26447 || EQ (g->object, disp_string))
26448 *start = row;
26449 g++;
26451 if (*start)
26452 break;
26456 /* Find the END row. */
26457 if (!*start
26458 /* If the last row is partially visible, start looking for END
26459 from that row, instead of starting from FIRST. */
26460 && !(row->enabled_p
26461 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26462 row = first;
26463 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26465 struct glyph_row *next = row + 1;
26466 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26468 if (!next->enabled_p
26469 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26470 /* The first row >= START whose range of displayed characters
26471 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26472 is the row END + 1. */
26473 || (start_charpos < next_start
26474 && end_charpos < next_start)
26475 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26476 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26477 && !next->ends_at_zv_p
26478 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26479 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26480 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26481 && !next->ends_at_zv_p
26482 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26484 *end = row;
26485 break;
26487 else
26489 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26490 but none of the characters it displays are in the range, it is
26491 also END + 1. */
26492 struct glyph *g = next->glyphs[TEXT_AREA];
26493 struct glyph *s = g;
26494 struct glyph *e = g + next->used[TEXT_AREA];
26496 while (g < e)
26498 if (((BUFFERP (g->object) || INTEGERP (g->object))
26499 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26500 /* If the buffer position of the first glyph in
26501 the row is equal to END_CHARPOS, it means
26502 the last character to be highlighted is the
26503 newline of ROW, and we must consider NEXT as
26504 END, not END+1. */
26505 || (((!next->reversed_p && g == s)
26506 || (next->reversed_p && g == e - 1))
26507 && (g->charpos == end_charpos
26508 /* Special case for when NEXT is an
26509 empty line at ZV. */
26510 || (g->charpos == -1
26511 && !row->ends_at_zv_p
26512 && next_start == end_charpos)))))
26513 /* A glyph that comes from DISP_STRING is by
26514 definition to be highlighted. */
26515 || EQ (g->object, disp_string))
26516 break;
26517 g++;
26519 if (g == e)
26521 *end = row;
26522 break;
26524 /* The first row that ends at ZV must be the last to be
26525 highlighted. */
26526 else if (next->ends_at_zv_p)
26528 *end = next;
26529 break;
26535 /* This function sets the mouse_face_* elements of HLINFO, assuming
26536 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26537 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26538 for the overlay or run of text properties specifying the mouse
26539 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26540 before-string and after-string that must also be highlighted.
26541 DISP_STRING, if non-nil, is a display string that may cover some
26542 or all of the highlighted text. */
26544 static void
26545 mouse_face_from_buffer_pos (Lisp_Object window,
26546 Mouse_HLInfo *hlinfo,
26547 ptrdiff_t mouse_charpos,
26548 ptrdiff_t start_charpos,
26549 ptrdiff_t end_charpos,
26550 Lisp_Object before_string,
26551 Lisp_Object after_string,
26552 Lisp_Object disp_string)
26554 struct window *w = XWINDOW (window);
26555 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26556 struct glyph_row *r1, *r2;
26557 struct glyph *glyph, *end;
26558 ptrdiff_t ignore, pos;
26559 int x;
26561 eassert (NILP (disp_string) || STRINGP (disp_string));
26562 eassert (NILP (before_string) || STRINGP (before_string));
26563 eassert (NILP (after_string) || STRINGP (after_string));
26565 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26566 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26567 if (r1 == NULL)
26568 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26569 /* If the before-string or display-string contains newlines,
26570 rows_from_pos_range skips to its last row. Move back. */
26571 if (!NILP (before_string) || !NILP (disp_string))
26573 struct glyph_row *prev;
26574 while ((prev = r1 - 1, prev >= first)
26575 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26576 && prev->used[TEXT_AREA] > 0)
26578 struct glyph *beg = prev->glyphs[TEXT_AREA];
26579 glyph = beg + prev->used[TEXT_AREA];
26580 while (--glyph >= beg && INTEGERP (glyph->object));
26581 if (glyph < beg
26582 || !(EQ (glyph->object, before_string)
26583 || EQ (glyph->object, disp_string)))
26584 break;
26585 r1 = prev;
26588 if (r2 == NULL)
26590 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26591 hlinfo->mouse_face_past_end = 1;
26593 else if (!NILP (after_string))
26595 /* If the after-string has newlines, advance to its last row. */
26596 struct glyph_row *next;
26597 struct glyph_row *last
26598 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26600 for (next = r2 + 1;
26601 next <= last
26602 && next->used[TEXT_AREA] > 0
26603 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26604 ++next)
26605 r2 = next;
26607 /* The rest of the display engine assumes that mouse_face_beg_row is
26608 either above mouse_face_end_row or identical to it. But with
26609 bidi-reordered continued lines, the row for START_CHARPOS could
26610 be below the row for END_CHARPOS. If so, swap the rows and store
26611 them in correct order. */
26612 if (r1->y > r2->y)
26614 struct glyph_row *tem = r2;
26616 r2 = r1;
26617 r1 = tem;
26620 hlinfo->mouse_face_beg_y = r1->y;
26621 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26622 hlinfo->mouse_face_end_y = r2->y;
26623 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26625 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26626 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26627 could be anywhere in the row and in any order. The strategy
26628 below is to find the leftmost and the rightmost glyph that
26629 belongs to either of these 3 strings, or whose position is
26630 between START_CHARPOS and END_CHARPOS, and highlight all the
26631 glyphs between those two. This may cover more than just the text
26632 between START_CHARPOS and END_CHARPOS if the range of characters
26633 strides the bidi level boundary, e.g. if the beginning is in R2L
26634 text while the end is in L2R text or vice versa. */
26635 if (!r1->reversed_p)
26637 /* This row is in a left to right paragraph. Scan it left to
26638 right. */
26639 glyph = r1->glyphs[TEXT_AREA];
26640 end = glyph + r1->used[TEXT_AREA];
26641 x = r1->x;
26643 /* Skip truncation glyphs at the start of the glyph row. */
26644 if (r1->displays_text_p)
26645 for (; glyph < end
26646 && INTEGERP (glyph->object)
26647 && glyph->charpos < 0;
26648 ++glyph)
26649 x += glyph->pixel_width;
26651 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26652 or DISP_STRING, and the first glyph from buffer whose
26653 position is between START_CHARPOS and END_CHARPOS. */
26654 for (; glyph < end
26655 && !INTEGERP (glyph->object)
26656 && !EQ (glyph->object, disp_string)
26657 && !(BUFFERP (glyph->object)
26658 && (glyph->charpos >= start_charpos
26659 && glyph->charpos < end_charpos));
26660 ++glyph)
26662 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26663 are present at buffer positions between START_CHARPOS and
26664 END_CHARPOS, or if they come from an overlay. */
26665 if (EQ (glyph->object, before_string))
26667 pos = string_buffer_position (before_string,
26668 start_charpos);
26669 /* If pos == 0, it means before_string came from an
26670 overlay, not from a buffer position. */
26671 if (!pos || (pos >= start_charpos && pos < end_charpos))
26672 break;
26674 else if (EQ (glyph->object, after_string))
26676 pos = string_buffer_position (after_string, end_charpos);
26677 if (!pos || (pos >= start_charpos && pos < end_charpos))
26678 break;
26680 x += glyph->pixel_width;
26682 hlinfo->mouse_face_beg_x = x;
26683 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26685 else
26687 /* This row is in a right to left paragraph. Scan it right to
26688 left. */
26689 struct glyph *g;
26691 end = r1->glyphs[TEXT_AREA] - 1;
26692 glyph = end + r1->used[TEXT_AREA];
26694 /* Skip truncation glyphs at the start of the glyph row. */
26695 if (r1->displays_text_p)
26696 for (; glyph > end
26697 && INTEGERP (glyph->object)
26698 && glyph->charpos < 0;
26699 --glyph)
26702 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26703 or DISP_STRING, and the first glyph from buffer whose
26704 position is between START_CHARPOS and END_CHARPOS. */
26705 for (; glyph > end
26706 && !INTEGERP (glyph->object)
26707 && !EQ (glyph->object, disp_string)
26708 && !(BUFFERP (glyph->object)
26709 && (glyph->charpos >= start_charpos
26710 && glyph->charpos < end_charpos));
26711 --glyph)
26713 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26714 are present at buffer positions between START_CHARPOS and
26715 END_CHARPOS, or if they come from an overlay. */
26716 if (EQ (glyph->object, before_string))
26718 pos = string_buffer_position (before_string, start_charpos);
26719 /* If pos == 0, it means before_string came from an
26720 overlay, not from a buffer position. */
26721 if (!pos || (pos >= start_charpos && pos < end_charpos))
26722 break;
26724 else if (EQ (glyph->object, after_string))
26726 pos = string_buffer_position (after_string, end_charpos);
26727 if (!pos || (pos >= start_charpos && pos < end_charpos))
26728 break;
26732 glyph++; /* first glyph to the right of the highlighted area */
26733 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26734 x += g->pixel_width;
26735 hlinfo->mouse_face_beg_x = x;
26736 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26739 /* If the highlight ends in a different row, compute GLYPH and END
26740 for the end row. Otherwise, reuse the values computed above for
26741 the row where the highlight begins. */
26742 if (r2 != r1)
26744 if (!r2->reversed_p)
26746 glyph = r2->glyphs[TEXT_AREA];
26747 end = glyph + r2->used[TEXT_AREA];
26748 x = r2->x;
26750 else
26752 end = r2->glyphs[TEXT_AREA] - 1;
26753 glyph = end + r2->used[TEXT_AREA];
26757 if (!r2->reversed_p)
26759 /* Skip truncation and continuation glyphs near the end of the
26760 row, and also blanks and stretch glyphs inserted by
26761 extend_face_to_end_of_line. */
26762 while (end > glyph
26763 && INTEGERP ((end - 1)->object))
26764 --end;
26765 /* Scan the rest of the glyph row from the end, looking for the
26766 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26767 DISP_STRING, or whose position is between START_CHARPOS
26768 and END_CHARPOS */
26769 for (--end;
26770 end > glyph
26771 && !INTEGERP (end->object)
26772 && !EQ (end->object, disp_string)
26773 && !(BUFFERP (end->object)
26774 && (end->charpos >= start_charpos
26775 && end->charpos < end_charpos));
26776 --end)
26778 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26779 are present at buffer positions between START_CHARPOS and
26780 END_CHARPOS, or if they come from an overlay. */
26781 if (EQ (end->object, before_string))
26783 pos = string_buffer_position (before_string, start_charpos);
26784 if (!pos || (pos >= start_charpos && pos < end_charpos))
26785 break;
26787 else if (EQ (end->object, after_string))
26789 pos = string_buffer_position (after_string, end_charpos);
26790 if (!pos || (pos >= start_charpos && pos < end_charpos))
26791 break;
26794 /* Find the X coordinate of the last glyph to be highlighted. */
26795 for (; glyph <= end; ++glyph)
26796 x += glyph->pixel_width;
26798 hlinfo->mouse_face_end_x = x;
26799 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26801 else
26803 /* Skip truncation and continuation glyphs near the end of the
26804 row, and also blanks and stretch glyphs inserted by
26805 extend_face_to_end_of_line. */
26806 x = r2->x;
26807 end++;
26808 while (end < glyph
26809 && INTEGERP (end->object))
26811 x += end->pixel_width;
26812 ++end;
26814 /* Scan the rest of the glyph row from the end, looking for the
26815 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26816 DISP_STRING, or whose position is between START_CHARPOS
26817 and END_CHARPOS */
26818 for ( ;
26819 end < glyph
26820 && !INTEGERP (end->object)
26821 && !EQ (end->object, disp_string)
26822 && !(BUFFERP (end->object)
26823 && (end->charpos >= start_charpos
26824 && end->charpos < end_charpos));
26825 ++end)
26827 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26828 are present at buffer positions between START_CHARPOS and
26829 END_CHARPOS, or if they come from an overlay. */
26830 if (EQ (end->object, before_string))
26832 pos = string_buffer_position (before_string, start_charpos);
26833 if (!pos || (pos >= start_charpos && pos < end_charpos))
26834 break;
26836 else if (EQ (end->object, after_string))
26838 pos = string_buffer_position (after_string, end_charpos);
26839 if (!pos || (pos >= start_charpos && pos < end_charpos))
26840 break;
26842 x += end->pixel_width;
26844 /* If we exited the above loop because we arrived at the last
26845 glyph of the row, and its buffer position is still not in
26846 range, it means the last character in range is the preceding
26847 newline. Bump the end column and x values to get past the
26848 last glyph. */
26849 if (end == glyph
26850 && BUFFERP (end->object)
26851 && (end->charpos < start_charpos
26852 || end->charpos >= end_charpos))
26854 x += end->pixel_width;
26855 ++end;
26857 hlinfo->mouse_face_end_x = x;
26858 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26861 hlinfo->mouse_face_window = window;
26862 hlinfo->mouse_face_face_id
26863 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26864 mouse_charpos + 1,
26865 !hlinfo->mouse_face_hidden, -1);
26866 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26869 /* The following function is not used anymore (replaced with
26870 mouse_face_from_string_pos), but I leave it here for the time
26871 being, in case someone would. */
26873 #if 0 /* not used */
26875 /* Find the position of the glyph for position POS in OBJECT in
26876 window W's current matrix, and return in *X, *Y the pixel
26877 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26879 RIGHT_P non-zero means return the position of the right edge of the
26880 glyph, RIGHT_P zero means return the left edge position.
26882 If no glyph for POS exists in the matrix, return the position of
26883 the glyph with the next smaller position that is in the matrix, if
26884 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26885 exists in the matrix, return the position of the glyph with the
26886 next larger position in OBJECT.
26888 Value is non-zero if a glyph was found. */
26890 static int
26891 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26892 int *hpos, int *vpos, int *x, int *y, int right_p)
26894 int yb = window_text_bottom_y (w);
26895 struct glyph_row *r;
26896 struct glyph *best_glyph = NULL;
26897 struct glyph_row *best_row = NULL;
26898 int best_x = 0;
26900 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26901 r->enabled_p && r->y < yb;
26902 ++r)
26904 struct glyph *g = r->glyphs[TEXT_AREA];
26905 struct glyph *e = g + r->used[TEXT_AREA];
26906 int gx;
26908 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26909 if (EQ (g->object, object))
26911 if (g->charpos == pos)
26913 best_glyph = g;
26914 best_x = gx;
26915 best_row = r;
26916 goto found;
26918 else if (best_glyph == NULL
26919 || ((eabs (g->charpos - pos)
26920 < eabs (best_glyph->charpos - pos))
26921 && (right_p
26922 ? g->charpos < pos
26923 : g->charpos > pos)))
26925 best_glyph = g;
26926 best_x = gx;
26927 best_row = r;
26932 found:
26934 if (best_glyph)
26936 *x = best_x;
26937 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26939 if (right_p)
26941 *x += best_glyph->pixel_width;
26942 ++*hpos;
26945 *y = best_row->y;
26946 *vpos = best_row - w->current_matrix->rows;
26949 return best_glyph != NULL;
26951 #endif /* not used */
26953 /* Find the positions of the first and the last glyphs in window W's
26954 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26955 (assumed to be a string), and return in HLINFO's mouse_face_*
26956 members the pixel and column/row coordinates of those glyphs. */
26958 static void
26959 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26960 Lisp_Object object,
26961 ptrdiff_t startpos, ptrdiff_t endpos)
26963 int yb = window_text_bottom_y (w);
26964 struct glyph_row *r;
26965 struct glyph *g, *e;
26966 int gx;
26967 int found = 0;
26969 /* Find the glyph row with at least one position in the range
26970 [STARTPOS..ENDPOS], and the first glyph in that row whose
26971 position belongs to that range. */
26972 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26973 r->enabled_p && r->y < yb;
26974 ++r)
26976 if (!r->reversed_p)
26978 g = r->glyphs[TEXT_AREA];
26979 e = g + r->used[TEXT_AREA];
26980 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26981 if (EQ (g->object, object)
26982 && startpos <= g->charpos && g->charpos <= endpos)
26984 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26985 hlinfo->mouse_face_beg_y = r->y;
26986 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26987 hlinfo->mouse_face_beg_x = gx;
26988 found = 1;
26989 break;
26992 else
26994 struct glyph *g1;
26996 e = r->glyphs[TEXT_AREA];
26997 g = e + r->used[TEXT_AREA];
26998 for ( ; g > e; --g)
26999 if (EQ ((g-1)->object, object)
27000 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27002 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27003 hlinfo->mouse_face_beg_y = r->y;
27004 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27005 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27006 gx += g1->pixel_width;
27007 hlinfo->mouse_face_beg_x = gx;
27008 found = 1;
27009 break;
27012 if (found)
27013 break;
27016 if (!found)
27017 return;
27019 /* Starting with the next row, look for the first row which does NOT
27020 include any glyphs whose positions are in the range. */
27021 for (++r; r->enabled_p && r->y < yb; ++r)
27023 g = r->glyphs[TEXT_AREA];
27024 e = g + r->used[TEXT_AREA];
27025 found = 0;
27026 for ( ; g < e; ++g)
27027 if (EQ (g->object, object)
27028 && startpos <= g->charpos && g->charpos <= endpos)
27030 found = 1;
27031 break;
27033 if (!found)
27034 break;
27037 /* The highlighted region ends on the previous row. */
27038 r--;
27040 /* Set the end row and its vertical pixel coordinate. */
27041 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27042 hlinfo->mouse_face_end_y = r->y;
27044 /* Compute and set the end column and the end column's horizontal
27045 pixel coordinate. */
27046 if (!r->reversed_p)
27048 g = r->glyphs[TEXT_AREA];
27049 e = g + r->used[TEXT_AREA];
27050 for ( ; e > g; --e)
27051 if (EQ ((e-1)->object, object)
27052 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27053 break;
27054 hlinfo->mouse_face_end_col = e - g;
27056 for (gx = r->x; g < e; ++g)
27057 gx += g->pixel_width;
27058 hlinfo->mouse_face_end_x = gx;
27060 else
27062 e = r->glyphs[TEXT_AREA];
27063 g = e + r->used[TEXT_AREA];
27064 for (gx = r->x ; e < g; ++e)
27066 if (EQ (e->object, object)
27067 && startpos <= e->charpos && e->charpos <= endpos)
27068 break;
27069 gx += e->pixel_width;
27071 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27072 hlinfo->mouse_face_end_x = gx;
27076 #ifdef HAVE_WINDOW_SYSTEM
27078 /* See if position X, Y is within a hot-spot of an image. */
27080 static int
27081 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27083 if (!CONSP (hot_spot))
27084 return 0;
27086 if (EQ (XCAR (hot_spot), Qrect))
27088 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27089 Lisp_Object rect = XCDR (hot_spot);
27090 Lisp_Object tem;
27091 if (!CONSP (rect))
27092 return 0;
27093 if (!CONSP (XCAR (rect)))
27094 return 0;
27095 if (!CONSP (XCDR (rect)))
27096 return 0;
27097 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27098 return 0;
27099 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27100 return 0;
27101 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27102 return 0;
27103 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27104 return 0;
27105 return 1;
27107 else if (EQ (XCAR (hot_spot), Qcircle))
27109 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27110 Lisp_Object circ = XCDR (hot_spot);
27111 Lisp_Object lr, lx0, ly0;
27112 if (CONSP (circ)
27113 && CONSP (XCAR (circ))
27114 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27115 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27116 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27118 double r = XFLOATINT (lr);
27119 double dx = XINT (lx0) - x;
27120 double dy = XINT (ly0) - y;
27121 return (dx * dx + dy * dy <= r * r);
27124 else if (EQ (XCAR (hot_spot), Qpoly))
27126 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27127 if (VECTORP (XCDR (hot_spot)))
27129 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27130 Lisp_Object *poly = v->contents;
27131 ptrdiff_t n = v->header.size;
27132 ptrdiff_t i;
27133 int inside = 0;
27134 Lisp_Object lx, ly;
27135 int x0, y0;
27137 /* Need an even number of coordinates, and at least 3 edges. */
27138 if (n < 6 || n & 1)
27139 return 0;
27141 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27142 If count is odd, we are inside polygon. Pixels on edges
27143 may or may not be included depending on actual geometry of the
27144 polygon. */
27145 if ((lx = poly[n-2], !INTEGERP (lx))
27146 || (ly = poly[n-1], !INTEGERP (lx)))
27147 return 0;
27148 x0 = XINT (lx), y0 = XINT (ly);
27149 for (i = 0; i < n; i += 2)
27151 int x1 = x0, y1 = y0;
27152 if ((lx = poly[i], !INTEGERP (lx))
27153 || (ly = poly[i+1], !INTEGERP (ly)))
27154 return 0;
27155 x0 = XINT (lx), y0 = XINT (ly);
27157 /* Does this segment cross the X line? */
27158 if (x0 >= x)
27160 if (x1 >= x)
27161 continue;
27163 else if (x1 < x)
27164 continue;
27165 if (y > y0 && y > y1)
27166 continue;
27167 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27168 inside = !inside;
27170 return inside;
27173 return 0;
27176 Lisp_Object
27177 find_hot_spot (Lisp_Object map, int x, int y)
27179 while (CONSP (map))
27181 if (CONSP (XCAR (map))
27182 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27183 return XCAR (map);
27184 map = XCDR (map);
27187 return Qnil;
27190 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27191 3, 3, 0,
27192 doc: /* Lookup in image map MAP coordinates X and Y.
27193 An image map is an alist where each element has the format (AREA ID PLIST).
27194 An AREA is specified as either a rectangle, a circle, or a polygon:
27195 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27196 pixel coordinates of the upper left and bottom right corners.
27197 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27198 and the radius of the circle; r may be a float or integer.
27199 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27200 vector describes one corner in the polygon.
27201 Returns the alist element for the first matching AREA in MAP. */)
27202 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27204 if (NILP (map))
27205 return Qnil;
27207 CHECK_NUMBER (x);
27208 CHECK_NUMBER (y);
27210 return find_hot_spot (map,
27211 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27212 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27216 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27217 static void
27218 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27220 /* Do not change cursor shape while dragging mouse. */
27221 if (!NILP (do_mouse_tracking))
27222 return;
27224 if (!NILP (pointer))
27226 if (EQ (pointer, Qarrow))
27227 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27228 else if (EQ (pointer, Qhand))
27229 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27230 else if (EQ (pointer, Qtext))
27231 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27232 else if (EQ (pointer, intern ("hdrag")))
27233 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27234 #ifdef HAVE_X_WINDOWS
27235 else if (EQ (pointer, intern ("vdrag")))
27236 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27237 #endif
27238 else if (EQ (pointer, intern ("hourglass")))
27239 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27240 else if (EQ (pointer, Qmodeline))
27241 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27242 else
27243 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27246 if (cursor != No_Cursor)
27247 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27250 #endif /* HAVE_WINDOW_SYSTEM */
27252 /* Take proper action when mouse has moved to the mode or header line
27253 or marginal area AREA of window W, x-position X and y-position Y.
27254 X is relative to the start of the text display area of W, so the
27255 width of bitmap areas and scroll bars must be subtracted to get a
27256 position relative to the start of the mode line. */
27258 static void
27259 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27260 enum window_part area)
27262 struct window *w = XWINDOW (window);
27263 struct frame *f = XFRAME (w->frame);
27264 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27265 #ifdef HAVE_WINDOW_SYSTEM
27266 Display_Info *dpyinfo;
27267 #endif
27268 Cursor cursor = No_Cursor;
27269 Lisp_Object pointer = Qnil;
27270 int dx, dy, width, height;
27271 ptrdiff_t charpos;
27272 Lisp_Object string, object = Qnil;
27273 Lisp_Object pos IF_LINT (= Qnil), help;
27275 Lisp_Object mouse_face;
27276 int original_x_pixel = x;
27277 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27278 struct glyph_row *row IF_LINT (= 0);
27280 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27282 int x0;
27283 struct glyph *end;
27285 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27286 returns them in row/column units! */
27287 string = mode_line_string (w, area, &x, &y, &charpos,
27288 &object, &dx, &dy, &width, &height);
27290 row = (area == ON_MODE_LINE
27291 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27292 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27294 /* Find the glyph under the mouse pointer. */
27295 if (row->mode_line_p && row->enabled_p)
27297 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27298 end = glyph + row->used[TEXT_AREA];
27300 for (x0 = original_x_pixel;
27301 glyph < end && x0 >= glyph->pixel_width;
27302 ++glyph)
27303 x0 -= glyph->pixel_width;
27305 if (glyph >= end)
27306 glyph = NULL;
27309 else
27311 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27312 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27313 returns them in row/column units! */
27314 string = marginal_area_string (w, area, &x, &y, &charpos,
27315 &object, &dx, &dy, &width, &height);
27318 help = Qnil;
27320 #ifdef HAVE_WINDOW_SYSTEM
27321 if (IMAGEP (object))
27323 Lisp_Object image_map, hotspot;
27324 if ((image_map = Fplist_get (XCDR (object), QCmap),
27325 !NILP (image_map))
27326 && (hotspot = find_hot_spot (image_map, dx, dy),
27327 CONSP (hotspot))
27328 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27330 Lisp_Object plist;
27332 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27333 If so, we could look for mouse-enter, mouse-leave
27334 properties in PLIST (and do something...). */
27335 hotspot = XCDR (hotspot);
27336 if (CONSP (hotspot)
27337 && (plist = XCAR (hotspot), CONSP (plist)))
27339 pointer = Fplist_get (plist, Qpointer);
27340 if (NILP (pointer))
27341 pointer = Qhand;
27342 help = Fplist_get (plist, Qhelp_echo);
27343 if (!NILP (help))
27345 help_echo_string = help;
27346 XSETWINDOW (help_echo_window, w);
27347 help_echo_object = w->buffer;
27348 help_echo_pos = charpos;
27352 if (NILP (pointer))
27353 pointer = Fplist_get (XCDR (object), QCpointer);
27355 #endif /* HAVE_WINDOW_SYSTEM */
27357 if (STRINGP (string))
27358 pos = make_number (charpos);
27360 /* Set the help text and mouse pointer. If the mouse is on a part
27361 of the mode line without any text (e.g. past the right edge of
27362 the mode line text), use the default help text and pointer. */
27363 if (STRINGP (string) || area == ON_MODE_LINE)
27365 /* Arrange to display the help by setting the global variables
27366 help_echo_string, help_echo_object, and help_echo_pos. */
27367 if (NILP (help))
27369 if (STRINGP (string))
27370 help = Fget_text_property (pos, Qhelp_echo, string);
27372 if (!NILP (help))
27374 help_echo_string = help;
27375 XSETWINDOW (help_echo_window, w);
27376 help_echo_object = string;
27377 help_echo_pos = charpos;
27379 else if (area == ON_MODE_LINE)
27381 Lisp_Object default_help
27382 = buffer_local_value_1 (Qmode_line_default_help_echo,
27383 w->buffer);
27385 if (STRINGP (default_help))
27387 help_echo_string = default_help;
27388 XSETWINDOW (help_echo_window, w);
27389 help_echo_object = Qnil;
27390 help_echo_pos = -1;
27395 #ifdef HAVE_WINDOW_SYSTEM
27396 /* Change the mouse pointer according to what is under it. */
27397 if (FRAME_WINDOW_P (f))
27399 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27400 if (STRINGP (string))
27402 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27404 if (NILP (pointer))
27405 pointer = Fget_text_property (pos, Qpointer, string);
27407 /* Change the mouse pointer according to what is under X/Y. */
27408 if (NILP (pointer)
27409 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27411 Lisp_Object map;
27412 map = Fget_text_property (pos, Qlocal_map, string);
27413 if (!KEYMAPP (map))
27414 map = Fget_text_property (pos, Qkeymap, string);
27415 if (!KEYMAPP (map))
27416 cursor = dpyinfo->vertical_scroll_bar_cursor;
27419 else
27420 /* Default mode-line pointer. */
27421 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27423 #endif
27426 /* Change the mouse face according to what is under X/Y. */
27427 if (STRINGP (string))
27429 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27430 if (!NILP (mouse_face)
27431 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27432 && glyph)
27434 Lisp_Object b, e;
27436 struct glyph * tmp_glyph;
27438 int gpos;
27439 int gseq_length;
27440 int total_pixel_width;
27441 ptrdiff_t begpos, endpos, ignore;
27443 int vpos, hpos;
27445 b = Fprevious_single_property_change (make_number (charpos + 1),
27446 Qmouse_face, string, Qnil);
27447 if (NILP (b))
27448 begpos = 0;
27449 else
27450 begpos = XINT (b);
27452 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27453 if (NILP (e))
27454 endpos = SCHARS (string);
27455 else
27456 endpos = XINT (e);
27458 /* Calculate the glyph position GPOS of GLYPH in the
27459 displayed string, relative to the beginning of the
27460 highlighted part of the string.
27462 Note: GPOS is different from CHARPOS. CHARPOS is the
27463 position of GLYPH in the internal string object. A mode
27464 line string format has structures which are converted to
27465 a flattened string by the Emacs Lisp interpreter. The
27466 internal string is an element of those structures. The
27467 displayed string is the flattened string. */
27468 tmp_glyph = row_start_glyph;
27469 while (tmp_glyph < glyph
27470 && (!(EQ (tmp_glyph->object, glyph->object)
27471 && begpos <= tmp_glyph->charpos
27472 && tmp_glyph->charpos < endpos)))
27473 tmp_glyph++;
27474 gpos = glyph - tmp_glyph;
27476 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27477 the highlighted part of the displayed string to which
27478 GLYPH belongs. Note: GSEQ_LENGTH is different from
27479 SCHARS (STRING), because the latter returns the length of
27480 the internal string. */
27481 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27482 tmp_glyph > glyph
27483 && (!(EQ (tmp_glyph->object, glyph->object)
27484 && begpos <= tmp_glyph->charpos
27485 && tmp_glyph->charpos < endpos));
27486 tmp_glyph--)
27488 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27490 /* Calculate the total pixel width of all the glyphs between
27491 the beginning of the highlighted area and GLYPH. */
27492 total_pixel_width = 0;
27493 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27494 total_pixel_width += tmp_glyph->pixel_width;
27496 /* Pre calculation of re-rendering position. Note: X is in
27497 column units here, after the call to mode_line_string or
27498 marginal_area_string. */
27499 hpos = x - gpos;
27500 vpos = (area == ON_MODE_LINE
27501 ? (w->current_matrix)->nrows - 1
27502 : 0);
27504 /* If GLYPH's position is included in the region that is
27505 already drawn in mouse face, we have nothing to do. */
27506 if ( EQ (window, hlinfo->mouse_face_window)
27507 && (!row->reversed_p
27508 ? (hlinfo->mouse_face_beg_col <= hpos
27509 && hpos < hlinfo->mouse_face_end_col)
27510 /* In R2L rows we swap BEG and END, see below. */
27511 : (hlinfo->mouse_face_end_col <= hpos
27512 && hpos < hlinfo->mouse_face_beg_col))
27513 && hlinfo->mouse_face_beg_row == vpos )
27514 return;
27516 if (clear_mouse_face (hlinfo))
27517 cursor = No_Cursor;
27519 if (!row->reversed_p)
27521 hlinfo->mouse_face_beg_col = hpos;
27522 hlinfo->mouse_face_beg_x = original_x_pixel
27523 - (total_pixel_width + dx);
27524 hlinfo->mouse_face_end_col = hpos + gseq_length;
27525 hlinfo->mouse_face_end_x = 0;
27527 else
27529 /* In R2L rows, show_mouse_face expects BEG and END
27530 coordinates to be swapped. */
27531 hlinfo->mouse_face_end_col = hpos;
27532 hlinfo->mouse_face_end_x = original_x_pixel
27533 - (total_pixel_width + dx);
27534 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27535 hlinfo->mouse_face_beg_x = 0;
27538 hlinfo->mouse_face_beg_row = vpos;
27539 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27540 hlinfo->mouse_face_beg_y = 0;
27541 hlinfo->mouse_face_end_y = 0;
27542 hlinfo->mouse_face_past_end = 0;
27543 hlinfo->mouse_face_window = window;
27545 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27546 charpos,
27547 0, 0, 0,
27548 &ignore,
27549 glyph->face_id,
27551 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27553 if (NILP (pointer))
27554 pointer = Qhand;
27556 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27557 clear_mouse_face (hlinfo);
27559 #ifdef HAVE_WINDOW_SYSTEM
27560 if (FRAME_WINDOW_P (f))
27561 define_frame_cursor1 (f, cursor, pointer);
27562 #endif
27566 /* EXPORT:
27567 Take proper action when the mouse has moved to position X, Y on
27568 frame F as regards highlighting characters that have mouse-face
27569 properties. Also de-highlighting chars where the mouse was before.
27570 X and Y can be negative or out of range. */
27572 void
27573 note_mouse_highlight (struct frame *f, int x, int y)
27575 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27576 enum window_part part = ON_NOTHING;
27577 Lisp_Object window;
27578 struct window *w;
27579 Cursor cursor = No_Cursor;
27580 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27581 struct buffer *b;
27583 /* When a menu is active, don't highlight because this looks odd. */
27584 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27585 if (popup_activated ())
27586 return;
27587 #endif
27589 if (NILP (Vmouse_highlight)
27590 || !f->glyphs_initialized_p
27591 || f->pointer_invisible)
27592 return;
27594 hlinfo->mouse_face_mouse_x = x;
27595 hlinfo->mouse_face_mouse_y = y;
27596 hlinfo->mouse_face_mouse_frame = f;
27598 if (hlinfo->mouse_face_defer)
27599 return;
27601 if (gc_in_progress)
27603 hlinfo->mouse_face_deferred_gc = 1;
27604 return;
27607 /* Which window is that in? */
27608 window = window_from_coordinates (f, x, y, &part, 1);
27610 /* If displaying active text in another window, clear that. */
27611 if (! EQ (window, hlinfo->mouse_face_window)
27612 /* Also clear if we move out of text area in same window. */
27613 || (!NILP (hlinfo->mouse_face_window)
27614 && !NILP (window)
27615 && part != ON_TEXT
27616 && part != ON_MODE_LINE
27617 && part != ON_HEADER_LINE))
27618 clear_mouse_face (hlinfo);
27620 /* Not on a window -> return. */
27621 if (!WINDOWP (window))
27622 return;
27624 /* Reset help_echo_string. It will get recomputed below. */
27625 help_echo_string = Qnil;
27627 /* Convert to window-relative pixel coordinates. */
27628 w = XWINDOW (window);
27629 frame_to_window_pixel_xy (w, &x, &y);
27631 #ifdef HAVE_WINDOW_SYSTEM
27632 /* Handle tool-bar window differently since it doesn't display a
27633 buffer. */
27634 if (EQ (window, f->tool_bar_window))
27636 note_tool_bar_highlight (f, x, y);
27637 return;
27639 #endif
27641 /* Mouse is on the mode, header line or margin? */
27642 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27643 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27645 note_mode_line_or_margin_highlight (window, x, y, part);
27646 return;
27649 #ifdef HAVE_WINDOW_SYSTEM
27650 if (part == ON_VERTICAL_BORDER)
27652 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27653 help_echo_string = build_string ("drag-mouse-1: resize");
27655 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27656 || part == ON_SCROLL_BAR)
27657 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27658 else
27659 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27660 #endif
27662 /* Are we in a window whose display is up to date?
27663 And verify the buffer's text has not changed. */
27664 b = XBUFFER (w->buffer);
27665 if (part == ON_TEXT
27666 && EQ (w->window_end_valid, w->buffer)
27667 && w->last_modified == BUF_MODIFF (b)
27668 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27670 int hpos, vpos, dx, dy, area = LAST_AREA;
27671 ptrdiff_t pos;
27672 struct glyph *glyph;
27673 Lisp_Object object;
27674 Lisp_Object mouse_face = Qnil, position;
27675 Lisp_Object *overlay_vec = NULL;
27676 ptrdiff_t i, noverlays;
27677 struct buffer *obuf;
27678 ptrdiff_t obegv, ozv;
27679 int same_region;
27681 /* Find the glyph under X/Y. */
27682 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27684 #ifdef HAVE_WINDOW_SYSTEM
27685 /* Look for :pointer property on image. */
27686 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27688 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27689 if (img != NULL && IMAGEP (img->spec))
27691 Lisp_Object image_map, hotspot;
27692 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27693 !NILP (image_map))
27694 && (hotspot = find_hot_spot (image_map,
27695 glyph->slice.img.x + dx,
27696 glyph->slice.img.y + dy),
27697 CONSP (hotspot))
27698 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27700 Lisp_Object plist;
27702 /* Could check XCAR (hotspot) to see if we enter/leave
27703 this hot-spot.
27704 If so, we could look for mouse-enter, mouse-leave
27705 properties in PLIST (and do something...). */
27706 hotspot = XCDR (hotspot);
27707 if (CONSP (hotspot)
27708 && (plist = XCAR (hotspot), CONSP (plist)))
27710 pointer = Fplist_get (plist, Qpointer);
27711 if (NILP (pointer))
27712 pointer = Qhand;
27713 help_echo_string = Fplist_get (plist, Qhelp_echo);
27714 if (!NILP (help_echo_string))
27716 help_echo_window = window;
27717 help_echo_object = glyph->object;
27718 help_echo_pos = glyph->charpos;
27722 if (NILP (pointer))
27723 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27726 #endif /* HAVE_WINDOW_SYSTEM */
27728 /* Clear mouse face if X/Y not over text. */
27729 if (glyph == NULL
27730 || area != TEXT_AREA
27731 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27732 /* Glyph's OBJECT is an integer for glyphs inserted by the
27733 display engine for its internal purposes, like truncation
27734 and continuation glyphs and blanks beyond the end of
27735 line's text on text terminals. If we are over such a
27736 glyph, we are not over any text. */
27737 || INTEGERP (glyph->object)
27738 /* R2L rows have a stretch glyph at their front, which
27739 stands for no text, whereas L2R rows have no glyphs at
27740 all beyond the end of text. Treat such stretch glyphs
27741 like we do with NULL glyphs in L2R rows. */
27742 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27743 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27744 && glyph->type == STRETCH_GLYPH
27745 && glyph->avoid_cursor_p))
27747 if (clear_mouse_face (hlinfo))
27748 cursor = No_Cursor;
27749 #ifdef HAVE_WINDOW_SYSTEM
27750 if (FRAME_WINDOW_P (f) && NILP (pointer))
27752 if (area != TEXT_AREA)
27753 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27754 else
27755 pointer = Vvoid_text_area_pointer;
27757 #endif
27758 goto set_cursor;
27761 pos = glyph->charpos;
27762 object = glyph->object;
27763 if (!STRINGP (object) && !BUFFERP (object))
27764 goto set_cursor;
27766 /* If we get an out-of-range value, return now; avoid an error. */
27767 if (BUFFERP (object) && pos > BUF_Z (b))
27768 goto set_cursor;
27770 /* Make the window's buffer temporarily current for
27771 overlays_at and compute_char_face. */
27772 obuf = current_buffer;
27773 current_buffer = b;
27774 obegv = BEGV;
27775 ozv = ZV;
27776 BEGV = BEG;
27777 ZV = Z;
27779 /* Is this char mouse-active or does it have help-echo? */
27780 position = make_number (pos);
27782 if (BUFFERP (object))
27784 /* Put all the overlays we want in a vector in overlay_vec. */
27785 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27786 /* Sort overlays into increasing priority order. */
27787 noverlays = sort_overlays (overlay_vec, noverlays, w);
27789 else
27790 noverlays = 0;
27792 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27794 if (same_region)
27795 cursor = No_Cursor;
27797 /* Check mouse-face highlighting. */
27798 if (! same_region
27799 /* If there exists an overlay with mouse-face overlapping
27800 the one we are currently highlighting, we have to
27801 check if we enter the overlapping overlay, and then
27802 highlight only that. */
27803 || (OVERLAYP (hlinfo->mouse_face_overlay)
27804 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27806 /* Find the highest priority overlay with a mouse-face. */
27807 Lisp_Object overlay = Qnil;
27808 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27810 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27811 if (!NILP (mouse_face))
27812 overlay = overlay_vec[i];
27815 /* If we're highlighting the same overlay as before, there's
27816 no need to do that again. */
27817 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27818 goto check_help_echo;
27819 hlinfo->mouse_face_overlay = overlay;
27821 /* Clear the display of the old active region, if any. */
27822 if (clear_mouse_face (hlinfo))
27823 cursor = No_Cursor;
27825 /* If no overlay applies, get a text property. */
27826 if (NILP (overlay))
27827 mouse_face = Fget_text_property (position, Qmouse_face, object);
27829 /* Next, compute the bounds of the mouse highlighting and
27830 display it. */
27831 if (!NILP (mouse_face) && STRINGP (object))
27833 /* The mouse-highlighting comes from a display string
27834 with a mouse-face. */
27835 Lisp_Object s, e;
27836 ptrdiff_t ignore;
27838 s = Fprevious_single_property_change
27839 (make_number (pos + 1), Qmouse_face, object, Qnil);
27840 e = Fnext_single_property_change
27841 (position, Qmouse_face, object, Qnil);
27842 if (NILP (s))
27843 s = make_number (0);
27844 if (NILP (e))
27845 e = make_number (SCHARS (object) - 1);
27846 mouse_face_from_string_pos (w, hlinfo, object,
27847 XINT (s), XINT (e));
27848 hlinfo->mouse_face_past_end = 0;
27849 hlinfo->mouse_face_window = window;
27850 hlinfo->mouse_face_face_id
27851 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27852 glyph->face_id, 1);
27853 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27854 cursor = No_Cursor;
27856 else
27858 /* The mouse-highlighting, if any, comes from an overlay
27859 or text property in the buffer. */
27860 Lisp_Object buffer IF_LINT (= Qnil);
27861 Lisp_Object disp_string IF_LINT (= Qnil);
27863 if (STRINGP (object))
27865 /* If we are on a display string with no mouse-face,
27866 check if the text under it has one. */
27867 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27868 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27869 pos = string_buffer_position (object, start);
27870 if (pos > 0)
27872 mouse_face = get_char_property_and_overlay
27873 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27874 buffer = w->buffer;
27875 disp_string = object;
27878 else
27880 buffer = object;
27881 disp_string = Qnil;
27884 if (!NILP (mouse_face))
27886 Lisp_Object before, after;
27887 Lisp_Object before_string, after_string;
27888 /* To correctly find the limits of mouse highlight
27889 in a bidi-reordered buffer, we must not use the
27890 optimization of limiting the search in
27891 previous-single-property-change and
27892 next-single-property-change, because
27893 rows_from_pos_range needs the real start and end
27894 positions to DTRT in this case. That's because
27895 the first row visible in a window does not
27896 necessarily display the character whose position
27897 is the smallest. */
27898 Lisp_Object lim1 =
27899 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27900 ? Fmarker_position (w->start)
27901 : Qnil;
27902 Lisp_Object lim2 =
27903 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27904 ? make_number (BUF_Z (XBUFFER (buffer))
27905 - XFASTINT (w->window_end_pos))
27906 : Qnil;
27908 if (NILP (overlay))
27910 /* Handle the text property case. */
27911 before = Fprevious_single_property_change
27912 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27913 after = Fnext_single_property_change
27914 (make_number (pos), Qmouse_face, buffer, lim2);
27915 before_string = after_string = Qnil;
27917 else
27919 /* Handle the overlay case. */
27920 before = Foverlay_start (overlay);
27921 after = Foverlay_end (overlay);
27922 before_string = Foverlay_get (overlay, Qbefore_string);
27923 after_string = Foverlay_get (overlay, Qafter_string);
27925 if (!STRINGP (before_string)) before_string = Qnil;
27926 if (!STRINGP (after_string)) after_string = Qnil;
27929 mouse_face_from_buffer_pos (window, hlinfo, pos,
27930 NILP (before)
27932 : XFASTINT (before),
27933 NILP (after)
27934 ? BUF_Z (XBUFFER (buffer))
27935 : XFASTINT (after),
27936 before_string, after_string,
27937 disp_string);
27938 cursor = No_Cursor;
27943 check_help_echo:
27945 /* Look for a `help-echo' property. */
27946 if (NILP (help_echo_string)) {
27947 Lisp_Object help, overlay;
27949 /* Check overlays first. */
27950 help = overlay = Qnil;
27951 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27953 overlay = overlay_vec[i];
27954 help = Foverlay_get (overlay, Qhelp_echo);
27957 if (!NILP (help))
27959 help_echo_string = help;
27960 help_echo_window = window;
27961 help_echo_object = overlay;
27962 help_echo_pos = pos;
27964 else
27966 Lisp_Object obj = glyph->object;
27967 ptrdiff_t charpos = glyph->charpos;
27969 /* Try text properties. */
27970 if (STRINGP (obj)
27971 && charpos >= 0
27972 && charpos < SCHARS (obj))
27974 help = Fget_text_property (make_number (charpos),
27975 Qhelp_echo, obj);
27976 if (NILP (help))
27978 /* If the string itself doesn't specify a help-echo,
27979 see if the buffer text ``under'' it does. */
27980 struct glyph_row *r
27981 = MATRIX_ROW (w->current_matrix, vpos);
27982 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27983 ptrdiff_t p = string_buffer_position (obj, start);
27984 if (p > 0)
27986 help = Fget_char_property (make_number (p),
27987 Qhelp_echo, w->buffer);
27988 if (!NILP (help))
27990 charpos = p;
27991 obj = w->buffer;
27996 else if (BUFFERP (obj)
27997 && charpos >= BEGV
27998 && charpos < ZV)
27999 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28000 obj);
28002 if (!NILP (help))
28004 help_echo_string = help;
28005 help_echo_window = window;
28006 help_echo_object = obj;
28007 help_echo_pos = charpos;
28012 #ifdef HAVE_WINDOW_SYSTEM
28013 /* Look for a `pointer' property. */
28014 if (FRAME_WINDOW_P (f) && NILP (pointer))
28016 /* Check overlays first. */
28017 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28018 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28020 if (NILP (pointer))
28022 Lisp_Object obj = glyph->object;
28023 ptrdiff_t charpos = glyph->charpos;
28025 /* Try text properties. */
28026 if (STRINGP (obj)
28027 && charpos >= 0
28028 && charpos < SCHARS (obj))
28030 pointer = Fget_text_property (make_number (charpos),
28031 Qpointer, obj);
28032 if (NILP (pointer))
28034 /* If the string itself doesn't specify a pointer,
28035 see if the buffer text ``under'' it does. */
28036 struct glyph_row *r
28037 = MATRIX_ROW (w->current_matrix, vpos);
28038 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28039 ptrdiff_t p = string_buffer_position (obj, start);
28040 if (p > 0)
28041 pointer = Fget_char_property (make_number (p),
28042 Qpointer, w->buffer);
28045 else if (BUFFERP (obj)
28046 && charpos >= BEGV
28047 && charpos < ZV)
28048 pointer = Fget_text_property (make_number (charpos),
28049 Qpointer, obj);
28052 #endif /* HAVE_WINDOW_SYSTEM */
28054 BEGV = obegv;
28055 ZV = ozv;
28056 current_buffer = obuf;
28059 set_cursor:
28061 #ifdef HAVE_WINDOW_SYSTEM
28062 if (FRAME_WINDOW_P (f))
28063 define_frame_cursor1 (f, cursor, pointer);
28064 #else
28065 /* This is here to prevent a compiler error, about "label at end of
28066 compound statement". */
28067 return;
28068 #endif
28072 /* EXPORT for RIF:
28073 Clear any mouse-face on window W. This function is part of the
28074 redisplay interface, and is called from try_window_id and similar
28075 functions to ensure the mouse-highlight is off. */
28077 void
28078 x_clear_window_mouse_face (struct window *w)
28080 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28081 Lisp_Object window;
28083 BLOCK_INPUT;
28084 XSETWINDOW (window, w);
28085 if (EQ (window, hlinfo->mouse_face_window))
28086 clear_mouse_face (hlinfo);
28087 UNBLOCK_INPUT;
28091 /* EXPORT:
28092 Just discard the mouse face information for frame F, if any.
28093 This is used when the size of F is changed. */
28095 void
28096 cancel_mouse_face (struct frame *f)
28098 Lisp_Object window;
28099 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28101 window = hlinfo->mouse_face_window;
28102 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28104 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28105 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28106 hlinfo->mouse_face_window = Qnil;
28112 /***********************************************************************
28113 Exposure Events
28114 ***********************************************************************/
28116 #ifdef HAVE_WINDOW_SYSTEM
28118 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28119 which intersects rectangle R. R is in window-relative coordinates. */
28121 static void
28122 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28123 enum glyph_row_area area)
28125 struct glyph *first = row->glyphs[area];
28126 struct glyph *end = row->glyphs[area] + row->used[area];
28127 struct glyph *last;
28128 int first_x, start_x, x;
28130 if (area == TEXT_AREA && row->fill_line_p)
28131 /* If row extends face to end of line write the whole line. */
28132 draw_glyphs (w, 0, row, area,
28133 0, row->used[area],
28134 DRAW_NORMAL_TEXT, 0);
28135 else
28137 /* Set START_X to the window-relative start position for drawing glyphs of
28138 AREA. The first glyph of the text area can be partially visible.
28139 The first glyphs of other areas cannot. */
28140 start_x = window_box_left_offset (w, area);
28141 x = start_x;
28142 if (area == TEXT_AREA)
28143 x += row->x;
28145 /* Find the first glyph that must be redrawn. */
28146 while (first < end
28147 && x + first->pixel_width < r->x)
28149 x += first->pixel_width;
28150 ++first;
28153 /* Find the last one. */
28154 last = first;
28155 first_x = x;
28156 while (last < end
28157 && x < r->x + r->width)
28159 x += last->pixel_width;
28160 ++last;
28163 /* Repaint. */
28164 if (last > first)
28165 draw_glyphs (w, first_x - start_x, row, area,
28166 first - row->glyphs[area], last - row->glyphs[area],
28167 DRAW_NORMAL_TEXT, 0);
28172 /* Redraw the parts of the glyph row ROW on window W intersecting
28173 rectangle R. R is in window-relative coordinates. Value is
28174 non-zero if mouse-face was overwritten. */
28176 static int
28177 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28179 eassert (row->enabled_p);
28181 if (row->mode_line_p || w->pseudo_window_p)
28182 draw_glyphs (w, 0, row, TEXT_AREA,
28183 0, row->used[TEXT_AREA],
28184 DRAW_NORMAL_TEXT, 0);
28185 else
28187 if (row->used[LEFT_MARGIN_AREA])
28188 expose_area (w, row, r, LEFT_MARGIN_AREA);
28189 if (row->used[TEXT_AREA])
28190 expose_area (w, row, r, TEXT_AREA);
28191 if (row->used[RIGHT_MARGIN_AREA])
28192 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28193 draw_row_fringe_bitmaps (w, row);
28196 return row->mouse_face_p;
28200 /* Redraw those parts of glyphs rows during expose event handling that
28201 overlap other rows. Redrawing of an exposed line writes over parts
28202 of lines overlapping that exposed line; this function fixes that.
28204 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28205 row in W's current matrix that is exposed and overlaps other rows.
28206 LAST_OVERLAPPING_ROW is the last such row. */
28208 static void
28209 expose_overlaps (struct window *w,
28210 struct glyph_row *first_overlapping_row,
28211 struct glyph_row *last_overlapping_row,
28212 XRectangle *r)
28214 struct glyph_row *row;
28216 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28217 if (row->overlapping_p)
28219 eassert (row->enabled_p && !row->mode_line_p);
28221 row->clip = r;
28222 if (row->used[LEFT_MARGIN_AREA])
28223 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28225 if (row->used[TEXT_AREA])
28226 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28228 if (row->used[RIGHT_MARGIN_AREA])
28229 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28230 row->clip = NULL;
28235 /* Return non-zero if W's cursor intersects rectangle R. */
28237 static int
28238 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28240 XRectangle cr, result;
28241 struct glyph *cursor_glyph;
28242 struct glyph_row *row;
28244 if (w->phys_cursor.vpos >= 0
28245 && w->phys_cursor.vpos < w->current_matrix->nrows
28246 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28247 row->enabled_p)
28248 && row->cursor_in_fringe_p)
28250 /* Cursor is in the fringe. */
28251 cr.x = window_box_right_offset (w,
28252 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28253 ? RIGHT_MARGIN_AREA
28254 : TEXT_AREA));
28255 cr.y = row->y;
28256 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28257 cr.height = row->height;
28258 return x_intersect_rectangles (&cr, r, &result);
28261 cursor_glyph = get_phys_cursor_glyph (w);
28262 if (cursor_glyph)
28264 /* r is relative to W's box, but w->phys_cursor.x is relative
28265 to left edge of W's TEXT area. Adjust it. */
28266 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28267 cr.y = w->phys_cursor.y;
28268 cr.width = cursor_glyph->pixel_width;
28269 cr.height = w->phys_cursor_height;
28270 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28271 I assume the effect is the same -- and this is portable. */
28272 return x_intersect_rectangles (&cr, r, &result);
28274 /* If we don't understand the format, pretend we're not in the hot-spot. */
28275 return 0;
28279 /* EXPORT:
28280 Draw a vertical window border to the right of window W if W doesn't
28281 have vertical scroll bars. */
28283 void
28284 x_draw_vertical_border (struct window *w)
28286 struct frame *f = XFRAME (WINDOW_FRAME (w));
28288 /* We could do better, if we knew what type of scroll-bar the adjacent
28289 windows (on either side) have... But we don't :-(
28290 However, I think this works ok. ++KFS 2003-04-25 */
28292 /* Redraw borders between horizontally adjacent windows. Don't
28293 do it for frames with vertical scroll bars because either the
28294 right scroll bar of a window, or the left scroll bar of its
28295 neighbor will suffice as a border. */
28296 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28297 return;
28299 if (!WINDOW_RIGHTMOST_P (w)
28300 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28302 int x0, x1, y0, y1;
28304 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28305 y1 -= 1;
28307 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28308 x1 -= 1;
28310 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28312 else if (!WINDOW_LEFTMOST_P (w)
28313 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28315 int x0, x1, y0, y1;
28317 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28318 y1 -= 1;
28320 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28321 x0 -= 1;
28323 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28328 /* Redraw the part of window W intersection rectangle FR. Pixel
28329 coordinates in FR are frame-relative. Call this function with
28330 input blocked. Value is non-zero if the exposure overwrites
28331 mouse-face. */
28333 static int
28334 expose_window (struct window *w, XRectangle *fr)
28336 struct frame *f = XFRAME (w->frame);
28337 XRectangle wr, r;
28338 int mouse_face_overwritten_p = 0;
28340 /* If window is not yet fully initialized, do nothing. This can
28341 happen when toolkit scroll bars are used and a window is split.
28342 Reconfiguring the scroll bar will generate an expose for a newly
28343 created window. */
28344 if (w->current_matrix == NULL)
28345 return 0;
28347 /* When we're currently updating the window, display and current
28348 matrix usually don't agree. Arrange for a thorough display
28349 later. */
28350 if (w == updated_window)
28352 SET_FRAME_GARBAGED (f);
28353 return 0;
28356 /* Frame-relative pixel rectangle of W. */
28357 wr.x = WINDOW_LEFT_EDGE_X (w);
28358 wr.y = WINDOW_TOP_EDGE_Y (w);
28359 wr.width = WINDOW_TOTAL_WIDTH (w);
28360 wr.height = WINDOW_TOTAL_HEIGHT (w);
28362 if (x_intersect_rectangles (fr, &wr, &r))
28364 int yb = window_text_bottom_y (w);
28365 struct glyph_row *row;
28366 int cursor_cleared_p, phys_cursor_on_p;
28367 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28369 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28370 r.x, r.y, r.width, r.height));
28372 /* Convert to window coordinates. */
28373 r.x -= WINDOW_LEFT_EDGE_X (w);
28374 r.y -= WINDOW_TOP_EDGE_Y (w);
28376 /* Turn off the cursor. */
28377 if (!w->pseudo_window_p
28378 && phys_cursor_in_rect_p (w, &r))
28380 x_clear_cursor (w);
28381 cursor_cleared_p = 1;
28383 else
28384 cursor_cleared_p = 0;
28386 /* If the row containing the cursor extends face to end of line,
28387 then expose_area might overwrite the cursor outside the
28388 rectangle and thus notice_overwritten_cursor might clear
28389 w->phys_cursor_on_p. We remember the original value and
28390 check later if it is changed. */
28391 phys_cursor_on_p = w->phys_cursor_on_p;
28393 /* Update lines intersecting rectangle R. */
28394 first_overlapping_row = last_overlapping_row = NULL;
28395 for (row = w->current_matrix->rows;
28396 row->enabled_p;
28397 ++row)
28399 int y0 = row->y;
28400 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28402 if ((y0 >= r.y && y0 < r.y + r.height)
28403 || (y1 > r.y && y1 < r.y + r.height)
28404 || (r.y >= y0 && r.y < y1)
28405 || (r.y + r.height > y0 && r.y + r.height < y1))
28407 /* A header line may be overlapping, but there is no need
28408 to fix overlapping areas for them. KFS 2005-02-12 */
28409 if (row->overlapping_p && !row->mode_line_p)
28411 if (first_overlapping_row == NULL)
28412 first_overlapping_row = row;
28413 last_overlapping_row = row;
28416 row->clip = fr;
28417 if (expose_line (w, row, &r))
28418 mouse_face_overwritten_p = 1;
28419 row->clip = NULL;
28421 else if (row->overlapping_p)
28423 /* We must redraw a row overlapping the exposed area. */
28424 if (y0 < r.y
28425 ? y0 + row->phys_height > r.y
28426 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28428 if (first_overlapping_row == NULL)
28429 first_overlapping_row = row;
28430 last_overlapping_row = row;
28434 if (y1 >= yb)
28435 break;
28438 /* Display the mode line if there is one. */
28439 if (WINDOW_WANTS_MODELINE_P (w)
28440 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28441 row->enabled_p)
28442 && row->y < r.y + r.height)
28444 if (expose_line (w, row, &r))
28445 mouse_face_overwritten_p = 1;
28448 if (!w->pseudo_window_p)
28450 /* Fix the display of overlapping rows. */
28451 if (first_overlapping_row)
28452 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28453 fr);
28455 /* Draw border between windows. */
28456 x_draw_vertical_border (w);
28458 /* Turn the cursor on again. */
28459 if (cursor_cleared_p
28460 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28461 update_window_cursor (w, 1);
28465 return mouse_face_overwritten_p;
28470 /* Redraw (parts) of all windows in the window tree rooted at W that
28471 intersect R. R contains frame pixel coordinates. Value is
28472 non-zero if the exposure overwrites mouse-face. */
28474 static int
28475 expose_window_tree (struct window *w, XRectangle *r)
28477 struct frame *f = XFRAME (w->frame);
28478 int mouse_face_overwritten_p = 0;
28480 while (w && !FRAME_GARBAGED_P (f))
28482 if (!NILP (w->hchild))
28483 mouse_face_overwritten_p
28484 |= expose_window_tree (XWINDOW (w->hchild), r);
28485 else if (!NILP (w->vchild))
28486 mouse_face_overwritten_p
28487 |= expose_window_tree (XWINDOW (w->vchild), r);
28488 else
28489 mouse_face_overwritten_p |= expose_window (w, r);
28491 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28494 return mouse_face_overwritten_p;
28498 /* EXPORT:
28499 Redisplay an exposed area of frame F. X and Y are the upper-left
28500 corner of the exposed rectangle. W and H are width and height of
28501 the exposed area. All are pixel values. W or H zero means redraw
28502 the entire frame. */
28504 void
28505 expose_frame (struct frame *f, int x, int y, int w, int h)
28507 XRectangle r;
28508 int mouse_face_overwritten_p = 0;
28510 TRACE ((stderr, "expose_frame "));
28512 /* No need to redraw if frame will be redrawn soon. */
28513 if (FRAME_GARBAGED_P (f))
28515 TRACE ((stderr, " garbaged\n"));
28516 return;
28519 /* If basic faces haven't been realized yet, there is no point in
28520 trying to redraw anything. This can happen when we get an expose
28521 event while Emacs is starting, e.g. by moving another window. */
28522 if (FRAME_FACE_CACHE (f) == NULL
28523 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28525 TRACE ((stderr, " no faces\n"));
28526 return;
28529 if (w == 0 || h == 0)
28531 r.x = r.y = 0;
28532 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28533 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28535 else
28537 r.x = x;
28538 r.y = y;
28539 r.width = w;
28540 r.height = h;
28543 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28544 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28546 if (WINDOWP (f->tool_bar_window))
28547 mouse_face_overwritten_p
28548 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28550 #ifdef HAVE_X_WINDOWS
28551 #ifndef MSDOS
28552 #ifndef USE_X_TOOLKIT
28553 if (WINDOWP (f->menu_bar_window))
28554 mouse_face_overwritten_p
28555 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28556 #endif /* not USE_X_TOOLKIT */
28557 #endif
28558 #endif
28560 /* Some window managers support a focus-follows-mouse style with
28561 delayed raising of frames. Imagine a partially obscured frame,
28562 and moving the mouse into partially obscured mouse-face on that
28563 frame. The visible part of the mouse-face will be highlighted,
28564 then the WM raises the obscured frame. With at least one WM, KDE
28565 2.1, Emacs is not getting any event for the raising of the frame
28566 (even tried with SubstructureRedirectMask), only Expose events.
28567 These expose events will draw text normally, i.e. not
28568 highlighted. Which means we must redo the highlight here.
28569 Subsume it under ``we love X''. --gerd 2001-08-15 */
28570 /* Included in Windows version because Windows most likely does not
28571 do the right thing if any third party tool offers
28572 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28573 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28575 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28576 if (f == hlinfo->mouse_face_mouse_frame)
28578 int mouse_x = hlinfo->mouse_face_mouse_x;
28579 int mouse_y = hlinfo->mouse_face_mouse_y;
28580 clear_mouse_face (hlinfo);
28581 note_mouse_highlight (f, mouse_x, mouse_y);
28587 /* EXPORT:
28588 Determine the intersection of two rectangles R1 and R2. Return
28589 the intersection in *RESULT. Value is non-zero if RESULT is not
28590 empty. */
28593 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28595 XRectangle *left, *right;
28596 XRectangle *upper, *lower;
28597 int intersection_p = 0;
28599 /* Rearrange so that R1 is the left-most rectangle. */
28600 if (r1->x < r2->x)
28601 left = r1, right = r2;
28602 else
28603 left = r2, right = r1;
28605 /* X0 of the intersection is right.x0, if this is inside R1,
28606 otherwise there is no intersection. */
28607 if (right->x <= left->x + left->width)
28609 result->x = right->x;
28611 /* The right end of the intersection is the minimum of
28612 the right ends of left and right. */
28613 result->width = (min (left->x + left->width, right->x + right->width)
28614 - result->x);
28616 /* Same game for Y. */
28617 if (r1->y < r2->y)
28618 upper = r1, lower = r2;
28619 else
28620 upper = r2, lower = r1;
28622 /* The upper end of the intersection is lower.y0, if this is inside
28623 of upper. Otherwise, there is no intersection. */
28624 if (lower->y <= upper->y + upper->height)
28626 result->y = lower->y;
28628 /* The lower end of the intersection is the minimum of the lower
28629 ends of upper and lower. */
28630 result->height = (min (lower->y + lower->height,
28631 upper->y + upper->height)
28632 - result->y);
28633 intersection_p = 1;
28637 return intersection_p;
28640 #endif /* HAVE_WINDOW_SYSTEM */
28643 /***********************************************************************
28644 Initialization
28645 ***********************************************************************/
28647 void
28648 syms_of_xdisp (void)
28650 Vwith_echo_area_save_vector = Qnil;
28651 staticpro (&Vwith_echo_area_save_vector);
28653 Vmessage_stack = Qnil;
28654 staticpro (&Vmessage_stack);
28656 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28658 message_dolog_marker1 = Fmake_marker ();
28659 staticpro (&message_dolog_marker1);
28660 message_dolog_marker2 = Fmake_marker ();
28661 staticpro (&message_dolog_marker2);
28662 message_dolog_marker3 = Fmake_marker ();
28663 staticpro (&message_dolog_marker3);
28665 #ifdef GLYPH_DEBUG
28666 defsubr (&Sdump_frame_glyph_matrix);
28667 defsubr (&Sdump_glyph_matrix);
28668 defsubr (&Sdump_glyph_row);
28669 defsubr (&Sdump_tool_bar_row);
28670 defsubr (&Strace_redisplay);
28671 defsubr (&Strace_to_stderr);
28672 #endif
28673 #ifdef HAVE_WINDOW_SYSTEM
28674 defsubr (&Stool_bar_lines_needed);
28675 defsubr (&Slookup_image_map);
28676 #endif
28677 defsubr (&Sformat_mode_line);
28678 defsubr (&Sinvisible_p);
28679 defsubr (&Scurrent_bidi_paragraph_direction);
28681 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28682 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28683 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28684 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28685 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28686 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28687 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28688 DEFSYM (Qeval, "eval");
28689 DEFSYM (QCdata, ":data");
28690 DEFSYM (Qdisplay, "display");
28691 DEFSYM (Qspace_width, "space-width");
28692 DEFSYM (Qraise, "raise");
28693 DEFSYM (Qslice, "slice");
28694 DEFSYM (Qspace, "space");
28695 DEFSYM (Qmargin, "margin");
28696 DEFSYM (Qpointer, "pointer");
28697 DEFSYM (Qleft_margin, "left-margin");
28698 DEFSYM (Qright_margin, "right-margin");
28699 DEFSYM (Qcenter, "center");
28700 DEFSYM (Qline_height, "line-height");
28701 DEFSYM (QCalign_to, ":align-to");
28702 DEFSYM (QCrelative_width, ":relative-width");
28703 DEFSYM (QCrelative_height, ":relative-height");
28704 DEFSYM (QCeval, ":eval");
28705 DEFSYM (QCpropertize, ":propertize");
28706 DEFSYM (QCfile, ":file");
28707 DEFSYM (Qfontified, "fontified");
28708 DEFSYM (Qfontification_functions, "fontification-functions");
28709 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28710 DEFSYM (Qescape_glyph, "escape-glyph");
28711 DEFSYM (Qnobreak_space, "nobreak-space");
28712 DEFSYM (Qimage, "image");
28713 DEFSYM (Qtext, "text");
28714 DEFSYM (Qboth, "both");
28715 DEFSYM (Qboth_horiz, "both-horiz");
28716 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28717 DEFSYM (QCmap, ":map");
28718 DEFSYM (QCpointer, ":pointer");
28719 DEFSYM (Qrect, "rect");
28720 DEFSYM (Qcircle, "circle");
28721 DEFSYM (Qpoly, "poly");
28722 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28723 DEFSYM (Qgrow_only, "grow-only");
28724 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28725 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28726 DEFSYM (Qposition, "position");
28727 DEFSYM (Qbuffer_position, "buffer-position");
28728 DEFSYM (Qobject, "object");
28729 DEFSYM (Qbar, "bar");
28730 DEFSYM (Qhbar, "hbar");
28731 DEFSYM (Qbox, "box");
28732 DEFSYM (Qhollow, "hollow");
28733 DEFSYM (Qhand, "hand");
28734 DEFSYM (Qarrow, "arrow");
28735 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28737 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28738 Fcons (intern_c_string ("void-variable"), Qnil)),
28739 Qnil);
28740 staticpro (&list_of_error);
28742 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28743 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28744 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28745 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28747 echo_buffer[0] = echo_buffer[1] = Qnil;
28748 staticpro (&echo_buffer[0]);
28749 staticpro (&echo_buffer[1]);
28751 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28752 staticpro (&echo_area_buffer[0]);
28753 staticpro (&echo_area_buffer[1]);
28755 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28756 staticpro (&Vmessages_buffer_name);
28758 mode_line_proptrans_alist = Qnil;
28759 staticpro (&mode_line_proptrans_alist);
28760 mode_line_string_list = Qnil;
28761 staticpro (&mode_line_string_list);
28762 mode_line_string_face = Qnil;
28763 staticpro (&mode_line_string_face);
28764 mode_line_string_face_prop = Qnil;
28765 staticpro (&mode_line_string_face_prop);
28766 Vmode_line_unwind_vector = Qnil;
28767 staticpro (&Vmode_line_unwind_vector);
28769 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28771 help_echo_string = Qnil;
28772 staticpro (&help_echo_string);
28773 help_echo_object = Qnil;
28774 staticpro (&help_echo_object);
28775 help_echo_window = Qnil;
28776 staticpro (&help_echo_window);
28777 previous_help_echo_string = Qnil;
28778 staticpro (&previous_help_echo_string);
28779 help_echo_pos = -1;
28781 DEFSYM (Qright_to_left, "right-to-left");
28782 DEFSYM (Qleft_to_right, "left-to-right");
28784 #ifdef HAVE_WINDOW_SYSTEM
28785 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28786 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28787 For example, if a block cursor is over a tab, it will be drawn as
28788 wide as that tab on the display. */);
28789 x_stretch_cursor_p = 0;
28790 #endif
28792 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28793 doc: /* Non-nil means highlight trailing whitespace.
28794 The face used for trailing whitespace is `trailing-whitespace'. */);
28795 Vshow_trailing_whitespace = Qnil;
28797 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28798 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28799 If the value is t, Emacs highlights non-ASCII chars which have the
28800 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28801 or `escape-glyph' face respectively.
28803 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28804 U+2011 (non-breaking hyphen) are affected.
28806 Any other non-nil value means to display these characters as a escape
28807 glyph followed by an ordinary space or hyphen.
28809 A value of nil means no special handling of these characters. */);
28810 Vnobreak_char_display = Qt;
28812 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28813 doc: /* The pointer shape to show in void text areas.
28814 A value of nil means to show the text pointer. Other options are `arrow',
28815 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28816 Vvoid_text_area_pointer = Qarrow;
28818 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28819 doc: /* Non-nil means don't actually do any redisplay.
28820 This is used for internal purposes. */);
28821 Vinhibit_redisplay = Qnil;
28823 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28824 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28825 Vglobal_mode_string = Qnil;
28827 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28828 doc: /* Marker for where to display an arrow on top of the buffer text.
28829 This must be the beginning of a line in order to work.
28830 See also `overlay-arrow-string'. */);
28831 Voverlay_arrow_position = Qnil;
28833 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28834 doc: /* String to display as an arrow in non-window frames.
28835 See also `overlay-arrow-position'. */);
28836 Voverlay_arrow_string = build_pure_c_string ("=>");
28838 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28839 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28840 The symbols on this list are examined during redisplay to determine
28841 where to display overlay arrows. */);
28842 Voverlay_arrow_variable_list
28843 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28845 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28846 doc: /* The number of lines to try scrolling a window by when point moves out.
28847 If that fails to bring point back on frame, point is centered instead.
28848 If this is zero, point is always centered after it moves off frame.
28849 If you want scrolling to always be a line at a time, you should set
28850 `scroll-conservatively' to a large value rather than set this to 1. */);
28852 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28853 doc: /* Scroll up to this many lines, to bring point back on screen.
28854 If point moves off-screen, redisplay will scroll by up to
28855 `scroll-conservatively' lines in order to bring point just barely
28856 onto the screen again. If that cannot be done, then redisplay
28857 recenters point as usual.
28859 If the value is greater than 100, redisplay will never recenter point,
28860 but will always scroll just enough text to bring point into view, even
28861 if you move far away.
28863 A value of zero means always recenter point if it moves off screen. */);
28864 scroll_conservatively = 0;
28866 DEFVAR_INT ("scroll-margin", scroll_margin,
28867 doc: /* Number of lines of margin at the top and bottom of a window.
28868 Recenter the window whenever point gets within this many lines
28869 of the top or bottom of the window. */);
28870 scroll_margin = 0;
28872 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28873 doc: /* Pixels per inch value for non-window system displays.
28874 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28875 Vdisplay_pixels_per_inch = make_float (72.0);
28877 #ifdef GLYPH_DEBUG
28878 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28879 #endif
28881 DEFVAR_LISP ("truncate-partial-width-windows",
28882 Vtruncate_partial_width_windows,
28883 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28884 For an integer value, truncate lines in each window narrower than the
28885 full frame width, provided the window width is less than that integer;
28886 otherwise, respect the value of `truncate-lines'.
28888 For any other non-nil value, truncate lines in all windows that do
28889 not span the full frame width.
28891 A value of nil means to respect the value of `truncate-lines'.
28893 If `word-wrap' is enabled, you might want to reduce this. */);
28894 Vtruncate_partial_width_windows = make_number (50);
28896 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28897 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28898 Any other value means to use the appropriate face, `mode-line',
28899 `header-line', or `menu' respectively. */);
28900 mode_line_inverse_video = 1;
28902 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28903 doc: /* Maximum buffer size for which line number should be displayed.
28904 If the buffer is bigger than this, the line number does not appear
28905 in the mode line. A value of nil means no limit. */);
28906 Vline_number_display_limit = Qnil;
28908 DEFVAR_INT ("line-number-display-limit-width",
28909 line_number_display_limit_width,
28910 doc: /* Maximum line width (in characters) for line number display.
28911 If the average length of the lines near point is bigger than this, then the
28912 line number may be omitted from the mode line. */);
28913 line_number_display_limit_width = 200;
28915 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28916 doc: /* Non-nil means highlight region even in nonselected windows. */);
28917 highlight_nonselected_windows = 0;
28919 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28920 doc: /* Non-nil if more than one frame is visible on this display.
28921 Minibuffer-only frames don't count, but iconified frames do.
28922 This variable is not guaranteed to be accurate except while processing
28923 `frame-title-format' and `icon-title-format'. */);
28925 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28926 doc: /* Template for displaying the title bar of visible frames.
28927 \(Assuming the window manager supports this feature.)
28929 This variable has the same structure as `mode-line-format', except that
28930 the %c and %l constructs are ignored. It is used only on frames for
28931 which no explicit name has been set \(see `modify-frame-parameters'). */);
28933 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28934 doc: /* Template for displaying the title bar of an iconified frame.
28935 \(Assuming the window manager supports this feature.)
28936 This variable has the same structure as `mode-line-format' (which see),
28937 and is used only on frames for which no explicit name has been set
28938 \(see `modify-frame-parameters'). */);
28939 Vicon_title_format
28940 = Vframe_title_format
28941 = listn (CONSTYPE_PURE, 3,
28942 intern_c_string ("multiple-frames"),
28943 build_pure_c_string ("%b"),
28944 listn (CONSTYPE_PURE, 4,
28945 empty_unibyte_string,
28946 intern_c_string ("invocation-name"),
28947 build_pure_c_string ("@"),
28948 intern_c_string ("system-name")));
28950 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28951 doc: /* Maximum number of lines to keep in the message log buffer.
28952 If nil, disable message logging. If t, log messages but don't truncate
28953 the buffer when it becomes large. */);
28954 Vmessage_log_max = make_number (100);
28956 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28957 doc: /* Functions called before redisplay, if window sizes have changed.
28958 The value should be a list of functions that take one argument.
28959 Just before redisplay, for each frame, if any of its windows have changed
28960 size since the last redisplay, or have been split or deleted,
28961 all the functions in the list are called, with the frame as argument. */);
28962 Vwindow_size_change_functions = Qnil;
28964 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28965 doc: /* List of functions to call before redisplaying a window with scrolling.
28966 Each function is called with two arguments, the window and its new
28967 display-start position. Note that these functions are also called by
28968 `set-window-buffer'. Also note that the value of `window-end' is not
28969 valid when these functions are called.
28971 Warning: Do not use this feature to alter the way the window
28972 is scrolled. It is not designed for that, and such use probably won't
28973 work. */);
28974 Vwindow_scroll_functions = Qnil;
28976 DEFVAR_LISP ("window-text-change-functions",
28977 Vwindow_text_change_functions,
28978 doc: /* Functions to call in redisplay when text in the window might change. */);
28979 Vwindow_text_change_functions = Qnil;
28981 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28982 doc: /* Functions called when redisplay of a window reaches the end trigger.
28983 Each function is called with two arguments, the window and the end trigger value.
28984 See `set-window-redisplay-end-trigger'. */);
28985 Vredisplay_end_trigger_functions = Qnil;
28987 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28988 doc: /* Non-nil means autoselect window with mouse pointer.
28989 If nil, do not autoselect windows.
28990 A positive number means delay autoselection by that many seconds: a
28991 window is autoselected only after the mouse has remained in that
28992 window for the duration of the delay.
28993 A negative number has a similar effect, but causes windows to be
28994 autoselected only after the mouse has stopped moving. \(Because of
28995 the way Emacs compares mouse events, you will occasionally wait twice
28996 that time before the window gets selected.\)
28997 Any other value means to autoselect window instantaneously when the
28998 mouse pointer enters it.
29000 Autoselection selects the minibuffer only if it is active, and never
29001 unselects the minibuffer if it is active.
29003 When customizing this variable make sure that the actual value of
29004 `focus-follows-mouse' matches the behavior of your window manager. */);
29005 Vmouse_autoselect_window = Qnil;
29007 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29008 doc: /* Non-nil means automatically resize tool-bars.
29009 This dynamically changes the tool-bar's height to the minimum height
29010 that is needed to make all tool-bar items visible.
29011 If value is `grow-only', the tool-bar's height is only increased
29012 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29013 Vauto_resize_tool_bars = Qt;
29015 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29016 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29017 auto_raise_tool_bar_buttons_p = 1;
29019 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29020 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29021 make_cursor_line_fully_visible_p = 1;
29023 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29024 doc: /* Border below tool-bar in pixels.
29025 If an integer, use it as the height of the border.
29026 If it is one of `internal-border-width' or `border-width', use the
29027 value of the corresponding frame parameter.
29028 Otherwise, no border is added below the tool-bar. */);
29029 Vtool_bar_border = Qinternal_border_width;
29031 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29032 doc: /* Margin around tool-bar buttons in pixels.
29033 If an integer, use that for both horizontal and vertical margins.
29034 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29035 HORZ specifying the horizontal margin, and VERT specifying the
29036 vertical margin. */);
29037 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29039 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29040 doc: /* Relief thickness of tool-bar buttons. */);
29041 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29043 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29044 doc: /* Tool bar style to use.
29045 It can be one of
29046 image - show images only
29047 text - show text only
29048 both - show both, text below image
29049 both-horiz - show text to the right of the image
29050 text-image-horiz - show text to the left of the image
29051 any other - use system default or image if no system default.
29053 This variable only affects the GTK+ toolkit version of Emacs. */);
29054 Vtool_bar_style = Qnil;
29056 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29057 doc: /* Maximum number of characters a label can have to be shown.
29058 The tool bar style must also show labels for this to have any effect, see
29059 `tool-bar-style'. */);
29060 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29062 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29063 doc: /* List of functions to call to fontify regions of text.
29064 Each function is called with one argument POS. Functions must
29065 fontify a region starting at POS in the current buffer, and give
29066 fontified regions the property `fontified'. */);
29067 Vfontification_functions = Qnil;
29068 Fmake_variable_buffer_local (Qfontification_functions);
29070 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29071 unibyte_display_via_language_environment,
29072 doc: /* Non-nil means display unibyte text according to language environment.
29073 Specifically, this means that raw bytes in the range 160-255 decimal
29074 are displayed by converting them to the equivalent multibyte characters
29075 according to the current language environment. As a result, they are
29076 displayed according to the current fontset.
29078 Note that this variable affects only how these bytes are displayed,
29079 but does not change the fact they are interpreted as raw bytes. */);
29080 unibyte_display_via_language_environment = 0;
29082 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29083 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29084 If a float, it specifies a fraction of the mini-window frame's height.
29085 If an integer, it specifies a number of lines. */);
29086 Vmax_mini_window_height = make_float (0.25);
29088 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29089 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29090 A value of nil means don't automatically resize mini-windows.
29091 A value of t means resize them to fit the text displayed in them.
29092 A value of `grow-only', the default, means let mini-windows grow only;
29093 they return to their normal size when the minibuffer is closed, or the
29094 echo area becomes empty. */);
29095 Vresize_mini_windows = Qgrow_only;
29097 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29098 doc: /* Alist specifying how to blink the cursor off.
29099 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29100 `cursor-type' frame-parameter or variable equals ON-STATE,
29101 comparing using `equal', Emacs uses OFF-STATE to specify
29102 how to blink it off. ON-STATE and OFF-STATE are values for
29103 the `cursor-type' frame parameter.
29105 If a frame's ON-STATE has no entry in this list,
29106 the frame's other specifications determine how to blink the cursor off. */);
29107 Vblink_cursor_alist = Qnil;
29109 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29110 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29111 If non-nil, windows are automatically scrolled horizontally to make
29112 point visible. */);
29113 automatic_hscrolling_p = 1;
29114 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29116 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29117 doc: /* How many columns away from the window edge point is allowed to get
29118 before automatic hscrolling will horizontally scroll the window. */);
29119 hscroll_margin = 5;
29121 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29122 doc: /* How many columns to scroll the window when point gets too close to the edge.
29123 When point is less than `hscroll-margin' columns from the window
29124 edge, automatic hscrolling will scroll the window by the amount of columns
29125 determined by this variable. If its value is a positive integer, scroll that
29126 many columns. If it's a positive floating-point number, it specifies the
29127 fraction of the window's width to scroll. If it's nil or zero, point will be
29128 centered horizontally after the scroll. Any other value, including negative
29129 numbers, are treated as if the value were zero.
29131 Automatic hscrolling always moves point outside the scroll margin, so if
29132 point was more than scroll step columns inside the margin, the window will
29133 scroll more than the value given by the scroll step.
29135 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29136 and `scroll-right' overrides this variable's effect. */);
29137 Vhscroll_step = make_number (0);
29139 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29140 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29141 Bind this around calls to `message' to let it take effect. */);
29142 message_truncate_lines = 0;
29144 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29145 doc: /* Normal hook run to update the menu bar definitions.
29146 Redisplay runs this hook before it redisplays the menu bar.
29147 This is used to update submenus such as Buffers,
29148 whose contents depend on various data. */);
29149 Vmenu_bar_update_hook = Qnil;
29151 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29152 doc: /* Frame for which we are updating a menu.
29153 The enable predicate for a menu binding should check this variable. */);
29154 Vmenu_updating_frame = Qnil;
29156 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29157 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29158 inhibit_menubar_update = 0;
29160 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29161 doc: /* Prefix prepended to all continuation lines at display time.
29162 The value may be a string, an image, or a stretch-glyph; it is
29163 interpreted in the same way as the value of a `display' text property.
29165 This variable is overridden by any `wrap-prefix' text or overlay
29166 property.
29168 To add a prefix to non-continuation lines, use `line-prefix'. */);
29169 Vwrap_prefix = Qnil;
29170 DEFSYM (Qwrap_prefix, "wrap-prefix");
29171 Fmake_variable_buffer_local (Qwrap_prefix);
29173 DEFVAR_LISP ("line-prefix", Vline_prefix,
29174 doc: /* Prefix prepended to all non-continuation lines at display time.
29175 The value may be a string, an image, or a stretch-glyph; it is
29176 interpreted in the same way as the value of a `display' text property.
29178 This variable is overridden by any `line-prefix' text or overlay
29179 property.
29181 To add a prefix to continuation lines, use `wrap-prefix'. */);
29182 Vline_prefix = Qnil;
29183 DEFSYM (Qline_prefix, "line-prefix");
29184 Fmake_variable_buffer_local (Qline_prefix);
29186 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29187 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29188 inhibit_eval_during_redisplay = 0;
29190 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29191 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29192 inhibit_free_realized_faces = 0;
29194 #ifdef GLYPH_DEBUG
29195 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29196 doc: /* Inhibit try_window_id display optimization. */);
29197 inhibit_try_window_id = 0;
29199 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29200 doc: /* Inhibit try_window_reusing display optimization. */);
29201 inhibit_try_window_reusing = 0;
29203 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29204 doc: /* Inhibit try_cursor_movement display optimization. */);
29205 inhibit_try_cursor_movement = 0;
29206 #endif /* GLYPH_DEBUG */
29208 DEFVAR_INT ("overline-margin", overline_margin,
29209 doc: /* Space between overline and text, in pixels.
29210 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29211 margin to the character height. */);
29212 overline_margin = 2;
29214 DEFVAR_INT ("underline-minimum-offset",
29215 underline_minimum_offset,
29216 doc: /* Minimum distance between baseline and underline.
29217 This can improve legibility of underlined text at small font sizes,
29218 particularly when using variable `x-use-underline-position-properties'
29219 with fonts that specify an UNDERLINE_POSITION relatively close to the
29220 baseline. The default value is 1. */);
29221 underline_minimum_offset = 1;
29223 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29224 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29225 This feature only works when on a window system that can change
29226 cursor shapes. */);
29227 display_hourglass_p = 1;
29229 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29230 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29231 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29233 hourglass_atimer = NULL;
29234 hourglass_shown_p = 0;
29236 DEFSYM (Qglyphless_char, "glyphless-char");
29237 DEFSYM (Qhex_code, "hex-code");
29238 DEFSYM (Qempty_box, "empty-box");
29239 DEFSYM (Qthin_space, "thin-space");
29240 DEFSYM (Qzero_width, "zero-width");
29242 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29243 /* Intern this now in case it isn't already done.
29244 Setting this variable twice is harmless.
29245 But don't staticpro it here--that is done in alloc.c. */
29246 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29247 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29249 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29250 doc: /* Char-table defining glyphless characters.
29251 Each element, if non-nil, should be one of the following:
29252 an ASCII acronym string: display this string in a box
29253 `hex-code': display the hexadecimal code of a character in a box
29254 `empty-box': display as an empty box
29255 `thin-space': display as 1-pixel width space
29256 `zero-width': don't display
29257 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29258 display method for graphical terminals and text terminals respectively.
29259 GRAPHICAL and TEXT should each have one of the values listed above.
29261 The char-table has one extra slot to control the display of a character for
29262 which no font is found. This slot only takes effect on graphical terminals.
29263 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29264 `thin-space'. The default is `empty-box'. */);
29265 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29266 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29267 Qempty_box);
29271 /* Initialize this module when Emacs starts. */
29273 void
29274 init_xdisp (void)
29276 current_header_line_height = current_mode_line_height = -1;
29278 CHARPOS (this_line_start_pos) = 0;
29280 if (!noninteractive)
29282 struct window *m = XWINDOW (minibuf_window);
29283 Lisp_Object frame = m->frame;
29284 struct frame *f = XFRAME (frame);
29285 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29286 struct window *r = XWINDOW (root);
29287 int i;
29289 echo_area_window = minibuf_window;
29291 WSET (r, top_line, make_number (FRAME_TOP_MARGIN (f)));
29292 WSET (r, total_lines, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29293 WSET (r, total_cols, make_number (FRAME_COLS (f)));
29294 WSET (m, top_line, make_number (FRAME_LINES (f) - 1));
29295 WSET (m, total_lines, make_number (1));
29296 WSET (m, total_cols, make_number (FRAME_COLS (f)));
29298 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29299 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29300 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29302 /* The default ellipsis glyphs `...'. */
29303 for (i = 0; i < 3; ++i)
29304 default_invis_vector[i] = make_number ('.');
29308 /* Allocate the buffer for frame titles.
29309 Also used for `format-mode-line'. */
29310 int size = 100;
29311 mode_line_noprop_buf = xmalloc (size);
29312 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29313 mode_line_noprop_ptr = mode_line_noprop_buf;
29314 mode_line_target = MODE_LINE_DISPLAY;
29317 help_echo_showing_p = 0;
29320 /* Since w32 does not support atimers, it defines its own implementation of
29321 the following three functions in w32fns.c. */
29322 #ifndef WINDOWSNT
29324 /* Platform-independent portion of hourglass implementation. */
29326 /* Cancel a currently active hourglass timer, and start a new one. */
29327 void
29328 start_hourglass (void)
29330 #if defined (HAVE_WINDOW_SYSTEM)
29331 EMACS_TIME delay;
29333 cancel_hourglass ();
29335 if (INTEGERP (Vhourglass_delay)
29336 && XINT (Vhourglass_delay) > 0)
29337 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29338 TYPE_MAXIMUM (time_t)),
29340 else if (FLOATP (Vhourglass_delay)
29341 && XFLOAT_DATA (Vhourglass_delay) > 0)
29342 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29343 else
29344 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29346 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29347 show_hourglass, NULL);
29348 #endif
29352 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29353 shown. */
29354 void
29355 cancel_hourglass (void)
29357 #if defined (HAVE_WINDOW_SYSTEM)
29358 if (hourglass_atimer)
29360 cancel_atimer (hourglass_atimer);
29361 hourglass_atimer = NULL;
29364 if (hourglass_shown_p)
29365 hide_hourglass ();
29366 #endif
29368 #endif /* ! WINDOWSNT */