* emacs-lisp/package.el (package-dir-info): New function.
[emacs.git] / src / xdisp.c
blobf006f8e0b94fe32b74c134f0385abc7a657cbd5d
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
4 Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
23 Redisplay.
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
54 expose_window (asynchronous) |
56 X expose events -----+
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
85 . try_cursor_movement
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
91 . try_window_reusing_current_matrix
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
97 . try_window_id
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest. (The "id" part in the function's
102 name stands for "insert/delete", not for "identification" or
103 somesuch.)
105 . try_window
107 This function performs the full redisplay of a single window
108 assuming that its fonts were not changed and that the cursor
109 will not end up in the scroll margins. (Loading fonts requires
110 re-adjustment of dimensions of glyph matrices, which makes this
111 method impossible to use.)
113 These optimizations are tried in sequence (some can be skipped if
114 it is known that they are not applicable). If none of the
115 optimizations were successful, redisplay calls redisplay_windows,
116 which performs a full redisplay of all windows.
118 Note that there's one more important optimization up Emacs's
119 sleeve, but it is related to actually redrawing the potentially
120 changed portions of the window/frame, not to reproducing the
121 desired matrices of those potentially changed portions. Namely,
122 the function update_frame and its subroutines, which you will find
123 in dispnew.c, compare the desired matrices with the current
124 matrices, and only redraw the portions that changed. So it could
125 happen that the functions in this file for some reason decide that
126 the entire desired matrix needs to be regenerated from scratch, and
127 still only parts of the Emacs display, or even nothing at all, will
128 be actually delivered to the glass, because update_frame has found
129 that the new and the old screen contents are similar or identical.
131 Desired matrices.
133 Desired matrices are always built per Emacs window. The function
134 `display_line' is the central function to look at if you are
135 interested. It constructs one row in a desired matrix given an
136 iterator structure containing both a buffer position and a
137 description of the environment in which the text is to be
138 displayed. But this is too early, read on.
140 Characters and pixmaps displayed for a range of buffer text depend
141 on various settings of buffers and windows, on overlays and text
142 properties, on display tables, on selective display. The good news
143 is that all this hairy stuff is hidden behind a small set of
144 interface functions taking an iterator structure (struct it)
145 argument.
147 Iteration over things to be displayed is then simple. It is
148 started by initializing an iterator with a call to init_iterator,
149 passing it the buffer position where to start iteration. For
150 iteration over strings, pass -1 as the position to init_iterator,
151 and call reseat_to_string when the string is ready, to initialize
152 the iterator for that string. Thereafter, calls to
153 get_next_display_element fill the iterator structure with relevant
154 information about the next thing to display. Calls to
155 set_iterator_to_next move the iterator to the next thing.
157 Besides this, an iterator also contains information about the
158 display environment in which glyphs for display elements are to be
159 produced. It has fields for the width and height of the display,
160 the information whether long lines are truncated or continued, a
161 current X and Y position, and lots of other stuff you can better
162 see in dispextern.h.
164 Glyphs in a desired matrix are normally constructed in a loop
165 calling get_next_display_element and then PRODUCE_GLYPHS. The call
166 to PRODUCE_GLYPHS will fill the iterator structure with pixel
167 information about the element being displayed and at the same time
168 produce glyphs for it. If the display element fits on the line
169 being displayed, set_iterator_to_next is called next, otherwise the
170 glyphs produced are discarded. The function display_line is the
171 workhorse of filling glyph rows in the desired matrix with glyphs.
172 In addition to producing glyphs, it also handles line truncation
173 and continuation, word wrap, and cursor positioning (for the
174 latter, see also set_cursor_from_row).
176 Frame matrices.
178 That just couldn't be all, could it? What about terminal types not
179 supporting operations on sub-windows of the screen? To update the
180 display on such a terminal, window-based glyph matrices are not
181 well suited. To be able to reuse part of the display (scrolling
182 lines up and down), we must instead have a view of the whole
183 screen. This is what `frame matrices' are for. They are a trick.
185 Frames on terminals like above have a glyph pool. Windows on such
186 a frame sub-allocate their glyph memory from their frame's glyph
187 pool. The frame itself is given its own glyph matrices. By
188 coincidence---or maybe something else---rows in window glyph
189 matrices are slices of corresponding rows in frame matrices. Thus
190 writing to window matrices implicitly updates a frame matrix which
191 provides us with the view of the whole screen that we originally
192 wanted to have without having to move many bytes around. To be
193 honest, there is a little bit more done, but not much more. If you
194 plan to extend that code, take a look at dispnew.c. The function
195 build_frame_matrix is a good starting point.
197 Bidirectional display.
199 Bidirectional display adds quite some hair to this already complex
200 design. The good news are that a large portion of that hairy stuff
201 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
202 reordering engine which is called by set_iterator_to_next and
203 returns the next character to display in the visual order. See
204 commentary on bidi.c for more details. As far as redisplay is
205 concerned, the effect of calling bidi_move_to_visually_next, the
206 main interface of the reordering engine, is that the iterator gets
207 magically placed on the buffer or string position that is to be
208 displayed next. In other words, a linear iteration through the
209 buffer/string is replaced with a non-linear one. All the rest of
210 the redisplay is oblivious to the bidi reordering.
212 Well, almost oblivious---there are still complications, most of
213 them due to the fact that buffer and string positions no longer
214 change monotonously with glyph indices in a glyph row. Moreover,
215 for continued lines, the buffer positions may not even be
216 monotonously changing with vertical positions. Also, accounting
217 for face changes, overlays, etc. becomes more complex because
218 non-linear iteration could potentially skip many positions with
219 changes, and then cross them again on the way back...
221 One other prominent effect of bidirectional display is that some
222 paragraphs of text need to be displayed starting at the right
223 margin of the window---the so-called right-to-left, or R2L
224 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
225 which have their reversed_p flag set. The bidi reordering engine
226 produces characters in such rows starting from the character which
227 should be the rightmost on display. PRODUCE_GLYPHS then reverses
228 the order, when it fills up the glyph row whose reversed_p flag is
229 set, by prepending each new glyph to what is already there, instead
230 of appending it. When the glyph row is complete, the function
231 extend_face_to_end_of_line fills the empty space to the left of the
232 leftmost character with special glyphs, which will display as,
233 well, empty. On text terminals, these special glyphs are simply
234 blank characters. On graphics terminals, there's a single stretch
235 glyph of a suitably computed width. Both the blanks and the
236 stretch glyph are given the face of the background of the line.
237 This way, the terminal-specific back-end can still draw the glyphs
238 left to right, even for R2L lines.
240 Bidirectional display and character compositions
242 Some scripts cannot be displayed by drawing each character
243 individually, because adjacent characters change each other's shape
244 on display. For example, Arabic and Indic scripts belong to this
245 category.
247 Emacs display supports this by providing "character compositions",
248 most of which is implemented in composite.c. During the buffer
249 scan that delivers characters to PRODUCE_GLYPHS, if the next
250 character to be delivered is a composed character, the iteration
251 calls composition_reseat_it and next_element_from_composition. If
252 they succeed to compose the character with one or more of the
253 following characters, the whole sequence of characters that where
254 composed is recorded in the `struct composition_it' object that is
255 part of the buffer iterator. The composed sequence could produce
256 one or more font glyphs (called "grapheme clusters") on the screen.
257 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
258 in the direction corresponding to the current bidi scan direction
259 (recorded in the scan_dir member of the `struct bidi_it' object
260 that is part of the buffer iterator). In particular, if the bidi
261 iterator currently scans the buffer backwards, the grapheme
262 clusters are delivered back to front. This reorders the grapheme
263 clusters as appropriate for the current bidi context. Note that
264 this means that the grapheme clusters are always stored in the
265 LGSTRING object (see composite.c) in the logical order.
267 Moving an iterator in bidirectional text
268 without producing glyphs
270 Note one important detail mentioned above: that the bidi reordering
271 engine, driven by the iterator, produces characters in R2L rows
272 starting at the character that will be the rightmost on display.
273 As far as the iterator is concerned, the geometry of such rows is
274 still left to right, i.e. the iterator "thinks" the first character
275 is at the leftmost pixel position. The iterator does not know that
276 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
277 delivers. This is important when functions from the move_it_*
278 family are used to get to certain screen position or to match
279 screen coordinates with buffer coordinates: these functions use the
280 iterator geometry, which is left to right even in R2L paragraphs.
281 This works well with most callers of move_it_*, because they need
282 to get to a specific column, and columns are still numbered in the
283 reading order, i.e. the rightmost character in a R2L paragraph is
284 still column zero. But some callers do not get well with this; a
285 notable example is mouse clicks that need to find the character
286 that corresponds to certain pixel coordinates. See
287 buffer_posn_from_coords in dispnew.c for how this is handled. */
289 #include <config.h>
290 #include <stdio.h>
291 #include <limits.h>
293 #include "lisp.h"
294 #include "atimer.h"
295 #include "keyboard.h"
296 #include "frame.h"
297 #include "window.h"
298 #include "termchar.h"
299 #include "dispextern.h"
300 #include "character.h"
301 #include "buffer.h"
302 #include "charset.h"
303 #include "indent.h"
304 #include "commands.h"
305 #include "keymap.h"
306 #include "macros.h"
307 #include "disptab.h"
308 #include "termhooks.h"
309 #include "termopts.h"
310 #include "intervals.h"
311 #include "coding.h"
312 #include "process.h"
313 #include "region-cache.h"
314 #include "font.h"
315 #include "fontset.h"
316 #include "blockinput.h"
317 #ifdef HAVE_WINDOW_SYSTEM
318 #include TERM_HEADER
319 #endif /* HAVE_WINDOW_SYSTEM */
321 #ifndef FRAME_X_OUTPUT
322 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
323 #endif
325 #define INFINITY 10000000
327 /* Holds the list (error). */
328 static Lisp_Object list_of_error;
330 #ifdef HAVE_WINDOW_SYSTEM
332 /* Test if overflow newline into fringe. Called with iterator IT
333 at or past right window margin, and with IT->current_x set. */
335 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
336 (!NILP (Voverflow_newline_into_fringe) \
337 && FRAME_WINDOW_P ((IT)->f) \
338 && ((IT)->bidi_it.paragraph_dir == R2L \
339 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
340 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
341 && (IT)->current_x == (IT)->last_visible_x)
343 #else /* !HAVE_WINDOW_SYSTEM */
344 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
345 #endif /* HAVE_WINDOW_SYSTEM */
347 /* Test if the display element loaded in IT, or the underlying buffer
348 or string character, is a space or a TAB character. This is used
349 to determine where word wrapping can occur. */
351 #define IT_DISPLAYING_WHITESPACE(it) \
352 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
353 || ((STRINGP (it->string) \
354 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
355 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
356 || (it->s \
357 && (it->s[IT_BYTEPOS (*it)] == ' ' \
358 || it->s[IT_BYTEPOS (*it)] == '\t')) \
359 || (IT_BYTEPOS (*it) < ZV_BYTE \
360 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
361 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
363 /* Non-zero means print newline to stdout before next mini-buffer
364 message. */
366 bool noninteractive_need_newline;
368 /* Non-zero means print newline to message log before next message. */
370 static bool message_log_need_newline;
372 /* Three markers that message_dolog uses.
373 It could allocate them itself, but that causes trouble
374 in handling memory-full errors. */
375 static Lisp_Object message_dolog_marker1;
376 static Lisp_Object message_dolog_marker2;
377 static Lisp_Object message_dolog_marker3;
379 /* The buffer position of the first character appearing entirely or
380 partially on the line of the selected window which contains the
381 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
382 redisplay optimization in redisplay_internal. */
384 static struct text_pos this_line_start_pos;
386 /* Number of characters past the end of the line above, including the
387 terminating newline. */
389 static struct text_pos this_line_end_pos;
391 /* The vertical positions and the height of this line. */
393 static int this_line_vpos;
394 static int this_line_y;
395 static int this_line_pixel_height;
397 /* X position at which this display line starts. Usually zero;
398 negative if first character is partially visible. */
400 static int this_line_start_x;
402 /* The smallest character position seen by move_it_* functions as they
403 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
404 hscrolled lines, see display_line. */
406 static struct text_pos this_line_min_pos;
408 /* Buffer that this_line_.* variables are referring to. */
410 static struct buffer *this_line_buffer;
412 /* Nonzero if an overlay arrow has been displayed in this window. */
414 static bool overlay_arrow_seen;
416 /* Vector containing glyphs for an ellipsis `...'. */
418 static Lisp_Object default_invis_vector[3];
420 /* This is the window where the echo area message was displayed. It
421 is always a mini-buffer window, but it may not be the same window
422 currently active as a mini-buffer. */
424 Lisp_Object echo_area_window;
426 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
427 pushes the current message and the value of
428 message_enable_multibyte on the stack, the function restore_message
429 pops the stack and displays MESSAGE again. */
431 static Lisp_Object Vmessage_stack;
433 /* Nonzero means multibyte characters were enabled when the echo area
434 message was specified. */
436 static bool message_enable_multibyte;
438 /* Nonzero if we should redraw the mode lines on the next redisplay.
439 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
440 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
441 (the number used is then only used to track down the cause for this
442 full-redisplay). */
444 int update_mode_lines;
446 /* Nonzero if window sizes or contents other than selected-window have changed
447 since last redisplay that finished.
448 If it has value REDISPLAY_SOME, then only redisplay the windows where
449 the `redisplay' bit has been set. Otherwise, redisplay all windows
450 (the number used is then only used to track down the cause for this
451 full-redisplay). */
453 int windows_or_buffers_changed;
455 /* Nonzero after display_mode_line if %l was used and it displayed a
456 line number. */
458 static bool line_number_displayed;
460 /* The name of the *Messages* buffer, a string. */
462 static Lisp_Object Vmessages_buffer_name;
464 /* Current, index 0, and last displayed echo area message. Either
465 buffers from echo_buffers, or nil to indicate no message. */
467 Lisp_Object echo_area_buffer[2];
469 /* The buffers referenced from echo_area_buffer. */
471 static Lisp_Object echo_buffer[2];
473 /* A vector saved used in with_area_buffer to reduce consing. */
475 static Lisp_Object Vwith_echo_area_save_vector;
477 /* Non-zero means display_echo_area should display the last echo area
478 message again. Set by redisplay_preserve_echo_area. */
480 static bool display_last_displayed_message_p;
482 /* Nonzero if echo area is being used by print; zero if being used by
483 message. */
485 static bool message_buf_print;
487 /* Set to 1 in clear_message to make redisplay_internal aware
488 of an emptied echo area. */
490 static bool message_cleared_p;
492 /* A scratch glyph row with contents used for generating truncation
493 glyphs. Also used in direct_output_for_insert. */
495 #define MAX_SCRATCH_GLYPHS 100
496 static struct glyph_row scratch_glyph_row;
497 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
499 /* Ascent and height of the last line processed by move_it_to. */
501 static int last_height;
503 /* Non-zero if there's a help-echo in the echo area. */
505 bool help_echo_showing_p;
507 /* The maximum distance to look ahead for text properties. Values
508 that are too small let us call compute_char_face and similar
509 functions too often which is expensive. Values that are too large
510 let us call compute_char_face and alike too often because we
511 might not be interested in text properties that far away. */
513 #define TEXT_PROP_DISTANCE_LIMIT 100
515 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
516 iterator state and later restore it. This is needed because the
517 bidi iterator on bidi.c keeps a stacked cache of its states, which
518 is really a singleton. When we use scratch iterator objects to
519 move around the buffer, we can cause the bidi cache to be pushed or
520 popped, and therefore we need to restore the cache state when we
521 return to the original iterator. */
522 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
523 do { \
524 if (CACHE) \
525 bidi_unshelve_cache (CACHE, 1); \
526 ITCOPY = ITORIG; \
527 CACHE = bidi_shelve_cache (); \
528 } while (0)
530 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
531 do { \
532 if (pITORIG != pITCOPY) \
533 *(pITORIG) = *(pITCOPY); \
534 bidi_unshelve_cache (CACHE, 0); \
535 CACHE = NULL; \
536 } while (0)
538 /* Functions to mark elements as needing redisplay. */
539 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
541 void
542 redisplay_other_windows (void)
544 if (!windows_or_buffers_changed)
545 windows_or_buffers_changed = REDISPLAY_SOME;
548 void
549 wset_redisplay (struct window *w)
551 /* Beware: selected_window can be nil during early stages. */
552 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
553 redisplay_other_windows ();
554 w->redisplay = true;
557 void
558 fset_redisplay (struct frame *f)
560 redisplay_other_windows ();
561 f->redisplay = true;
564 void
565 bset_redisplay (struct buffer *b)
567 int count = buffer_window_count (b);
568 if (count > 0)
570 /* ... it's visible in other window than selected, */
571 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
572 redisplay_other_windows ();
573 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
574 so that if we later set windows_or_buffers_changed, this buffer will
575 not be omitted. */
576 b->text->redisplay = true;
580 void
581 bset_update_mode_line (struct buffer *b)
583 if (!update_mode_lines)
584 update_mode_lines = REDISPLAY_SOME;
585 b->text->redisplay = true;
588 #ifdef GLYPH_DEBUG
590 /* Non-zero means print traces of redisplay if compiled with
591 GLYPH_DEBUG defined. */
593 bool trace_redisplay_p;
595 #endif /* GLYPH_DEBUG */
597 #ifdef DEBUG_TRACE_MOVE
598 /* Non-zero means trace with TRACE_MOVE to stderr. */
599 int trace_move;
601 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
602 #else
603 #define TRACE_MOVE(x) (void) 0
604 #endif
606 /* Buffer being redisplayed -- for redisplay_window_error. */
608 static struct buffer *displayed_buffer;
610 /* Value returned from text property handlers (see below). */
612 enum prop_handled
614 HANDLED_NORMALLY,
615 HANDLED_RECOMPUTE_PROPS,
616 HANDLED_OVERLAY_STRING_CONSUMED,
617 HANDLED_RETURN
620 /* A description of text properties that redisplay is interested
621 in. */
623 struct props
625 /* The symbol index of the name of the property. */
626 short name;
628 /* A unique index for the property. */
629 enum prop_idx idx;
631 /* A handler function called to set up iterator IT from the property
632 at IT's current position. Value is used to steer handle_stop. */
633 enum prop_handled (*handler) (struct it *it);
636 static enum prop_handled handle_face_prop (struct it *);
637 static enum prop_handled handle_invisible_prop (struct it *);
638 static enum prop_handled handle_display_prop (struct it *);
639 static enum prop_handled handle_composition_prop (struct it *);
640 static enum prop_handled handle_overlay_change (struct it *);
641 static enum prop_handled handle_fontified_prop (struct it *);
643 /* Properties handled by iterators. */
645 static struct props it_props[] =
647 {SYMBOL_INDEX (Qfontified), FONTIFIED_PROP_IDX, handle_fontified_prop},
648 /* Handle `face' before `display' because some sub-properties of
649 `display' need to know the face. */
650 {SYMBOL_INDEX (Qface), FACE_PROP_IDX, handle_face_prop},
651 {SYMBOL_INDEX (Qdisplay), DISPLAY_PROP_IDX, handle_display_prop},
652 {SYMBOL_INDEX (Qinvisible), INVISIBLE_PROP_IDX, handle_invisible_prop},
653 {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
654 {0, 0, NULL}
657 /* Value is the position described by X. If X is a marker, value is
658 the marker_position of X. Otherwise, value is X. */
660 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
662 /* Enumeration returned by some move_it_.* functions internally. */
664 enum move_it_result
666 /* Not used. Undefined value. */
667 MOVE_UNDEFINED,
669 /* Move ended at the requested buffer position or ZV. */
670 MOVE_POS_MATCH_OR_ZV,
672 /* Move ended at the requested X pixel position. */
673 MOVE_X_REACHED,
675 /* Move within a line ended at the end of a line that must be
676 continued. */
677 MOVE_LINE_CONTINUED,
679 /* Move within a line ended at the end of a line that would
680 be displayed truncated. */
681 MOVE_LINE_TRUNCATED,
683 /* Move within a line ended at a line end. */
684 MOVE_NEWLINE_OR_CR
687 /* This counter is used to clear the face cache every once in a while
688 in redisplay_internal. It is incremented for each redisplay.
689 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
690 cleared. */
692 #define CLEAR_FACE_CACHE_COUNT 500
693 static int clear_face_cache_count;
695 /* Similarly for the image cache. */
697 #ifdef HAVE_WINDOW_SYSTEM
698 #define CLEAR_IMAGE_CACHE_COUNT 101
699 static int clear_image_cache_count;
701 /* Null glyph slice */
702 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
703 #endif
705 /* True while redisplay_internal is in progress. */
707 bool redisplaying_p;
709 /* If a string, XTread_socket generates an event to display that string.
710 (The display is done in read_char.) */
712 Lisp_Object help_echo_string;
713 Lisp_Object help_echo_window;
714 Lisp_Object help_echo_object;
715 ptrdiff_t help_echo_pos;
717 /* Temporary variable for XTread_socket. */
719 Lisp_Object previous_help_echo_string;
721 /* Platform-independent portion of hourglass implementation. */
723 #ifdef HAVE_WINDOW_SYSTEM
725 /* Non-zero means an hourglass cursor is currently shown. */
726 static bool hourglass_shown_p;
728 /* If non-null, an asynchronous timer that, when it expires, displays
729 an hourglass cursor on all frames. */
730 static struct atimer *hourglass_atimer;
732 #endif /* HAVE_WINDOW_SYSTEM */
734 /* Default number of seconds to wait before displaying an hourglass
735 cursor. */
736 #define DEFAULT_HOURGLASS_DELAY 1
738 #ifdef HAVE_WINDOW_SYSTEM
740 /* Default pixel width of `thin-space' display method. */
741 #define THIN_SPACE_WIDTH 1
743 #endif /* HAVE_WINDOW_SYSTEM */
745 /* Function prototypes. */
747 static void setup_for_ellipsis (struct it *, int);
748 static void set_iterator_to_next (struct it *, int);
749 static void mark_window_display_accurate_1 (struct window *, int);
750 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
751 static int display_prop_string_p (Lisp_Object, Lisp_Object);
752 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
753 static int cursor_row_p (struct glyph_row *);
754 static int redisplay_mode_lines (Lisp_Object, bool);
755 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
757 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
759 static void handle_line_prefix (struct it *);
761 static void pint2str (char *, int, ptrdiff_t);
762 static void pint2hrstr (char *, int, ptrdiff_t);
763 static struct text_pos run_window_scroll_functions (Lisp_Object,
764 struct text_pos);
765 static int text_outside_line_unchanged_p (struct window *,
766 ptrdiff_t, ptrdiff_t);
767 static void store_mode_line_noprop_char (char);
768 static int store_mode_line_noprop (const char *, int, int);
769 static void handle_stop (struct it *);
770 static void handle_stop_backwards (struct it *, ptrdiff_t);
771 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
772 static void ensure_echo_area_buffers (void);
773 static void unwind_with_echo_area_buffer (Lisp_Object);
774 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
775 static int with_echo_area_buffer (struct window *, int,
776 int (*) (ptrdiff_t, Lisp_Object),
777 ptrdiff_t, Lisp_Object);
778 static void clear_garbaged_frames (void);
779 static int current_message_1 (ptrdiff_t, Lisp_Object);
780 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
781 static void set_message (Lisp_Object);
782 static int set_message_1 (ptrdiff_t, Lisp_Object);
783 static int display_echo_area (struct window *);
784 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
785 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
786 static void unwind_redisplay (void);
787 static int string_char_and_length (const unsigned char *, int *);
788 static struct text_pos display_prop_end (struct it *, Lisp_Object,
789 struct text_pos);
790 static int compute_window_start_on_continuation_line (struct window *);
791 static void insert_left_trunc_glyphs (struct it *);
792 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
793 Lisp_Object);
794 static void extend_face_to_end_of_line (struct it *);
795 static int append_space_for_newline (struct it *, int);
796 static int cursor_row_fully_visible_p (struct window *, int, int);
797 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
798 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
799 static int trailing_whitespace_p (ptrdiff_t);
800 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
801 static void push_it (struct it *, struct text_pos *);
802 static void iterate_out_of_display_property (struct it *);
803 static void pop_it (struct it *);
804 static void sync_frame_with_window_matrix_rows (struct window *);
805 static void redisplay_internal (void);
806 static bool echo_area_display (bool);
807 static void redisplay_windows (Lisp_Object);
808 static void redisplay_window (Lisp_Object, bool);
809 static Lisp_Object redisplay_window_error (Lisp_Object);
810 static Lisp_Object redisplay_window_0 (Lisp_Object);
811 static Lisp_Object redisplay_window_1 (Lisp_Object);
812 static int set_cursor_from_row (struct window *, struct glyph_row *,
813 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
814 int, int);
815 static int update_menu_bar (struct frame *, int, int);
816 static int try_window_reusing_current_matrix (struct window *);
817 static int try_window_id (struct window *);
818 static int display_line (struct it *);
819 static int display_mode_lines (struct window *);
820 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
821 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
822 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
823 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
824 static void display_menu_bar (struct window *);
825 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
826 ptrdiff_t *);
827 static int display_string (const char *, Lisp_Object, Lisp_Object,
828 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
829 static void compute_line_metrics (struct it *);
830 static void run_redisplay_end_trigger_hook (struct it *);
831 static int get_overlay_strings (struct it *, ptrdiff_t);
832 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
833 static void next_overlay_string (struct it *);
834 static void reseat (struct it *, struct text_pos, int);
835 static void reseat_1 (struct it *, struct text_pos, int);
836 static void back_to_previous_visible_line_start (struct it *);
837 static void reseat_at_next_visible_line_start (struct it *, int);
838 static int next_element_from_ellipsis (struct it *);
839 static int next_element_from_display_vector (struct it *);
840 static int next_element_from_string (struct it *);
841 static int next_element_from_c_string (struct it *);
842 static int next_element_from_buffer (struct it *);
843 static int next_element_from_composition (struct it *);
844 static int next_element_from_image (struct it *);
845 static int next_element_from_stretch (struct it *);
846 static void load_overlay_strings (struct it *, ptrdiff_t);
847 static int init_from_display_pos (struct it *, struct window *,
848 struct display_pos *);
849 static void reseat_to_string (struct it *, const char *,
850 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
851 static int get_next_display_element (struct it *);
852 static enum move_it_result
853 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
854 enum move_operation_enum);
855 static void get_visually_first_element (struct it *);
856 static void init_to_row_start (struct it *, struct window *,
857 struct glyph_row *);
858 static int init_to_row_end (struct it *, struct window *,
859 struct glyph_row *);
860 static void back_to_previous_line_start (struct it *);
861 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
862 static struct text_pos string_pos_nchars_ahead (struct text_pos,
863 Lisp_Object, ptrdiff_t);
864 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
865 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
866 static ptrdiff_t number_of_chars (const char *, bool);
867 static void compute_stop_pos (struct it *);
868 static void compute_string_pos (struct text_pos *, struct text_pos,
869 Lisp_Object);
870 static int face_before_or_after_it_pos (struct it *, int);
871 static ptrdiff_t next_overlay_change (ptrdiff_t);
872 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
873 Lisp_Object, struct text_pos *, ptrdiff_t, int);
874 static int handle_single_display_spec (struct it *, Lisp_Object,
875 Lisp_Object, Lisp_Object,
876 struct text_pos *, ptrdiff_t, int, int);
877 static int underlying_face_id (struct it *);
878 static int in_ellipses_for_invisible_text_p (struct display_pos *,
879 struct window *);
881 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
882 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
884 #ifdef HAVE_WINDOW_SYSTEM
886 static void x_consider_frame_title (Lisp_Object);
887 static void update_tool_bar (struct frame *, int);
888 static int redisplay_tool_bar (struct frame *);
889 static void x_draw_bottom_divider (struct window *w);
890 static void notice_overwritten_cursor (struct window *,
891 enum glyph_row_area,
892 int, int, int, int);
893 static void append_stretch_glyph (struct it *, Lisp_Object,
894 int, int, int);
897 #endif /* HAVE_WINDOW_SYSTEM */
899 static void produce_special_glyphs (struct it *, enum display_element_type);
900 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
901 static bool coords_in_mouse_face_p (struct window *, int, int);
905 /***********************************************************************
906 Window display dimensions
907 ***********************************************************************/
909 /* Return the bottom boundary y-position for text lines in window W.
910 This is the first y position at which a line cannot start.
911 It is relative to the top of the window.
913 This is the height of W minus the height of a mode line, if any. */
916 window_text_bottom_y (struct window *w)
918 int height = WINDOW_PIXEL_HEIGHT (w);
920 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
922 if (WINDOW_WANTS_MODELINE_P (w))
923 height -= CURRENT_MODE_LINE_HEIGHT (w);
925 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
927 return height;
930 /* Return the pixel width of display area AREA of window W.
931 ANY_AREA means return the total width of W, not including
932 fringes to the left and right of the window. */
935 window_box_width (struct window *w, enum glyph_row_area area)
937 int width = w->pixel_width;
939 if (!w->pseudo_window_p)
941 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
942 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
944 if (area == TEXT_AREA)
945 width -= (WINDOW_MARGINS_WIDTH (w)
946 + WINDOW_FRINGES_WIDTH (w));
947 else if (area == LEFT_MARGIN_AREA)
948 width = WINDOW_LEFT_MARGIN_WIDTH (w);
949 else if (area == RIGHT_MARGIN_AREA)
950 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
953 /* With wide margins, fringes, etc. we might end up with a negative
954 width, correct that here. */
955 return max (0, width);
959 /* Return the pixel height of the display area of window W, not
960 including mode lines of W, if any. */
963 window_box_height (struct window *w)
965 struct frame *f = XFRAME (w->frame);
966 int height = WINDOW_PIXEL_HEIGHT (w);
968 eassert (height >= 0);
970 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
971 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
973 /* Note: the code below that determines the mode-line/header-line
974 height is essentially the same as that contained in the macro
975 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
976 the appropriate glyph row has its `mode_line_p' flag set,
977 and if it doesn't, uses estimate_mode_line_height instead. */
979 if (WINDOW_WANTS_MODELINE_P (w))
981 struct glyph_row *ml_row
982 = (w->current_matrix && w->current_matrix->rows
983 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
984 : 0);
985 if (ml_row && ml_row->mode_line_p)
986 height -= ml_row->height;
987 else
988 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
991 if (WINDOW_WANTS_HEADER_LINE_P (w))
993 struct glyph_row *hl_row
994 = (w->current_matrix && w->current_matrix->rows
995 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
996 : 0);
997 if (hl_row && hl_row->mode_line_p)
998 height -= hl_row->height;
999 else
1000 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1003 /* With a very small font and a mode-line that's taller than
1004 default, we might end up with a negative height. */
1005 return max (0, height);
1008 /* Return the window-relative coordinate of the left edge of display
1009 area AREA of window W. ANY_AREA means return the left edge of the
1010 whole window, to the right of the left fringe of W. */
1013 window_box_left_offset (struct window *w, enum glyph_row_area area)
1015 int x;
1017 if (w->pseudo_window_p)
1018 return 0;
1020 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1022 if (area == TEXT_AREA)
1023 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1024 + window_box_width (w, LEFT_MARGIN_AREA));
1025 else if (area == RIGHT_MARGIN_AREA)
1026 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1027 + window_box_width (w, LEFT_MARGIN_AREA)
1028 + window_box_width (w, TEXT_AREA)
1029 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1031 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1032 else if (area == LEFT_MARGIN_AREA
1033 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1034 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1036 /* Don't return more than the window's pixel width. */
1037 return min (x, w->pixel_width);
1041 /* Return the window-relative coordinate of the right edge of display
1042 area AREA of window W. ANY_AREA means return the right edge of the
1043 whole window, to the left of the right fringe of W. */
1045 static int
1046 window_box_right_offset (struct window *w, enum glyph_row_area area)
1048 /* Don't return more than the window's pixel width. */
1049 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1050 w->pixel_width);
1053 /* Return the frame-relative coordinate of the left edge of display
1054 area AREA of window W. ANY_AREA means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1058 window_box_left (struct window *w, enum glyph_row_area area)
1060 struct frame *f = XFRAME (w->frame);
1061 int x;
1063 if (w->pseudo_window_p)
1064 return FRAME_INTERNAL_BORDER_WIDTH (f);
1066 x = (WINDOW_LEFT_EDGE_X (w)
1067 + window_box_left_offset (w, area));
1069 return x;
1073 /* Return the frame-relative coordinate of the right edge of display
1074 area AREA of window W. ANY_AREA means return the right edge of the
1075 whole window, to the left of the right fringe of W. */
1078 window_box_right (struct window *w, enum glyph_row_area area)
1080 return window_box_left (w, area) + window_box_width (w, area);
1083 /* Get the bounding box of the display area AREA of window W, without
1084 mode lines, in frame-relative coordinates. ANY_AREA means the
1085 whole window, not including the left and right fringes of
1086 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1087 coordinates of the upper-left corner of the box. Return in
1088 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1090 void
1091 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1092 int *box_y, int *box_width, int *box_height)
1094 if (box_width)
1095 *box_width = window_box_width (w, area);
1096 if (box_height)
1097 *box_height = window_box_height (w);
1098 if (box_x)
1099 *box_x = window_box_left (w, area);
1100 if (box_y)
1102 *box_y = WINDOW_TOP_EDGE_Y (w);
1103 if (WINDOW_WANTS_HEADER_LINE_P (w))
1104 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1108 #ifdef HAVE_WINDOW_SYSTEM
1110 /* Get the bounding box of the display area AREA of window W, without
1111 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1112 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1113 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1114 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1115 box. */
1117 static void
1118 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1119 int *bottom_right_x, int *bottom_right_y)
1121 window_box (w, ANY_AREA, top_left_x, top_left_y,
1122 bottom_right_x, bottom_right_y);
1123 *bottom_right_x += *top_left_x;
1124 *bottom_right_y += *top_left_y;
1127 #endif /* HAVE_WINDOW_SYSTEM */
1129 /***********************************************************************
1130 Utilities
1131 ***********************************************************************/
1133 /* Return the bottom y-position of the line the iterator IT is in.
1134 This can modify IT's settings. */
1137 line_bottom_y (struct it *it)
1139 int line_height = it->max_ascent + it->max_descent;
1140 int line_top_y = it->current_y;
1142 if (line_height == 0)
1144 if (last_height)
1145 line_height = last_height;
1146 else if (IT_CHARPOS (*it) < ZV)
1148 move_it_by_lines (it, 1);
1149 line_height = (it->max_ascent || it->max_descent
1150 ? it->max_ascent + it->max_descent
1151 : last_height);
1153 else
1155 struct glyph_row *row = it->glyph_row;
1157 /* Use the default character height. */
1158 it->glyph_row = NULL;
1159 it->what = IT_CHARACTER;
1160 it->c = ' ';
1161 it->len = 1;
1162 PRODUCE_GLYPHS (it);
1163 line_height = it->ascent + it->descent;
1164 it->glyph_row = row;
1168 return line_top_y + line_height;
1171 DEFUN ("line-pixel-height", Fline_pixel_height,
1172 Sline_pixel_height, 0, 0, 0,
1173 doc: /* Return height in pixels of text line in the selected window.
1175 Value is the height in pixels of the line at point. */)
1176 (void)
1178 struct it it;
1179 struct text_pos pt;
1180 struct window *w = XWINDOW (selected_window);
1181 struct buffer *old_buffer = NULL;
1182 Lisp_Object result;
1184 if (XBUFFER (w->contents) != current_buffer)
1186 old_buffer = current_buffer;
1187 set_buffer_internal_1 (XBUFFER (w->contents));
1189 SET_TEXT_POS (pt, PT, PT_BYTE);
1190 start_display (&it, w, pt);
1191 it.vpos = it.current_y = 0;
1192 last_height = 0;
1193 result = make_number (line_bottom_y (&it));
1194 if (old_buffer)
1195 set_buffer_internal_1 (old_buffer);
1197 return result;
1200 /* Return the default pixel height of text lines in window W. The
1201 value is the canonical height of the W frame's default font, plus
1202 any extra space required by the line-spacing variable or frame
1203 parameter.
1205 Implementation note: this ignores any line-spacing text properties
1206 put on the newline characters. This is because those properties
1207 only affect the _screen_ line ending in the newline (i.e., in a
1208 continued line, only the last screen line will be affected), which
1209 means only a small number of lines in a buffer can ever use this
1210 feature. Since this function is used to compute the default pixel
1211 equivalent of text lines in a window, we can safely ignore those
1212 few lines. For the same reasons, we ignore the line-height
1213 properties. */
1215 default_line_pixel_height (struct window *w)
1217 struct frame *f = WINDOW_XFRAME (w);
1218 int height = FRAME_LINE_HEIGHT (f);
1220 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1222 struct buffer *b = XBUFFER (w->contents);
1223 Lisp_Object val = BVAR (b, extra_line_spacing);
1225 if (NILP (val))
1226 val = BVAR (&buffer_defaults, extra_line_spacing);
1227 if (!NILP (val))
1229 if (RANGED_INTEGERP (0, val, INT_MAX))
1230 height += XFASTINT (val);
1231 else if (FLOATP (val))
1233 int addon = XFLOAT_DATA (val) * height + 0.5;
1235 if (addon >= 0)
1236 height += addon;
1239 else
1240 height += f->extra_line_spacing;
1243 return height;
1246 /* Subroutine of pos_visible_p below. Extracts a display string, if
1247 any, from the display spec given as its argument. */
1248 static Lisp_Object
1249 string_from_display_spec (Lisp_Object spec)
1251 if (CONSP (spec))
1253 while (CONSP (spec))
1255 if (STRINGP (XCAR (spec)))
1256 return XCAR (spec);
1257 spec = XCDR (spec);
1260 else if (VECTORP (spec))
1262 ptrdiff_t i;
1264 for (i = 0; i < ASIZE (spec); i++)
1266 if (STRINGP (AREF (spec, i)))
1267 return AREF (spec, i);
1269 return Qnil;
1272 return spec;
1276 /* Limit insanely large values of W->hscroll on frame F to the largest
1277 value that will still prevent first_visible_x and last_visible_x of
1278 'struct it' from overflowing an int. */
1279 static int
1280 window_hscroll_limited (struct window *w, struct frame *f)
1282 ptrdiff_t window_hscroll = w->hscroll;
1283 int window_text_width = window_box_width (w, TEXT_AREA);
1284 int colwidth = FRAME_COLUMN_WIDTH (f);
1286 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1287 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1289 return window_hscroll;
1292 /* Return 1 if position CHARPOS is visible in window W.
1293 CHARPOS < 0 means return info about WINDOW_END position.
1294 If visible, set *X and *Y to pixel coordinates of top left corner.
1295 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1296 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1299 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1300 int *rtop, int *rbot, int *rowh, int *vpos)
1302 struct it it;
1303 void *itdata = bidi_shelve_cache ();
1304 struct text_pos top;
1305 int visible_p = 0;
1306 struct buffer *old_buffer = NULL;
1307 bool r2l = false;
1309 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1310 return visible_p;
1312 if (XBUFFER (w->contents) != current_buffer)
1314 old_buffer = current_buffer;
1315 set_buffer_internal_1 (XBUFFER (w->contents));
1318 SET_TEXT_POS_FROM_MARKER (top, w->start);
1319 /* Scrolling a minibuffer window via scroll bar when the echo area
1320 shows long text sometimes resets the minibuffer contents behind
1321 our backs. */
1322 if (CHARPOS (top) > ZV)
1323 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1325 /* Compute exact mode line heights. */
1326 if (WINDOW_WANTS_MODELINE_P (w))
1327 w->mode_line_height
1328 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1329 BVAR (current_buffer, mode_line_format));
1331 if (WINDOW_WANTS_HEADER_LINE_P (w))
1332 w->header_line_height
1333 = display_mode_line (w, HEADER_LINE_FACE_ID,
1334 BVAR (current_buffer, header_line_format));
1336 start_display (&it, w, top);
1337 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1338 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1340 if (charpos >= 0
1341 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1342 && IT_CHARPOS (it) >= charpos)
1343 /* When scanning backwards under bidi iteration, move_it_to
1344 stops at or _before_ CHARPOS, because it stops at or to
1345 the _right_ of the character at CHARPOS. */
1346 || (it.bidi_p && it.bidi_it.scan_dir == -1
1347 && IT_CHARPOS (it) <= charpos)))
1349 /* We have reached CHARPOS, or passed it. How the call to
1350 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1351 or covered by a display property, move_it_to stops at the end
1352 of the invisible text, to the right of CHARPOS. (ii) If
1353 CHARPOS is in a display vector, move_it_to stops on its last
1354 glyph. */
1355 int top_x = it.current_x;
1356 int top_y = it.current_y;
1357 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1358 int bottom_y;
1359 struct it save_it;
1360 void *save_it_data = NULL;
1362 /* Calling line_bottom_y may change it.method, it.position, etc. */
1363 SAVE_IT (save_it, it, save_it_data);
1364 last_height = 0;
1365 bottom_y = line_bottom_y (&it);
1366 if (top_y < window_top_y)
1367 visible_p = bottom_y > window_top_y;
1368 else if (top_y < it.last_visible_y)
1369 visible_p = 1;
1370 if (bottom_y >= it.last_visible_y
1371 && it.bidi_p && it.bidi_it.scan_dir == -1
1372 && IT_CHARPOS (it) < charpos)
1374 /* When the last line of the window is scanned backwards
1375 under bidi iteration, we could be duped into thinking
1376 that we have passed CHARPOS, when in fact move_it_to
1377 simply stopped short of CHARPOS because it reached
1378 last_visible_y. To see if that's what happened, we call
1379 move_it_to again with a slightly larger vertical limit,
1380 and see if it actually moved vertically; if it did, we
1381 didn't really reach CHARPOS, which is beyond window end. */
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines = 10 * default_line_pixel_height (w);
1386 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1387 MOVE_TO_POS | MOVE_TO_Y);
1388 if (it.current_y > top_y)
1389 visible_p = 0;
1392 RESTORE_IT (&it, &save_it, save_it_data);
1393 if (visible_p)
1395 if (it.method == GET_FROM_DISPLAY_VECTOR)
1397 /* We stopped on the last glyph of a display vector.
1398 Try and recompute. Hack alert! */
1399 if (charpos < 2 || top.charpos >= charpos)
1400 top_x = it.glyph_row->x;
1401 else
1403 struct it it2, it2_prev;
1404 /* The idea is to get to the previous buffer
1405 position, consume the character there, and use
1406 the pixel coordinates we get after that. But if
1407 the previous buffer position is also displayed
1408 from a display vector, we need to consume all of
1409 the glyphs from that display vector. */
1410 start_display (&it2, w, top);
1411 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1412 /* If we didn't get to CHARPOS - 1, there's some
1413 replacing display property at that position, and
1414 we stopped after it. That is exactly the place
1415 whose coordinates we want. */
1416 if (IT_CHARPOS (it2) != charpos - 1)
1417 it2_prev = it2;
1418 else
1420 /* Iterate until we get out of the display
1421 vector that displays the character at
1422 CHARPOS - 1. */
1423 do {
1424 get_next_display_element (&it2);
1425 PRODUCE_GLYPHS (&it2);
1426 it2_prev = it2;
1427 set_iterator_to_next (&it2, 1);
1428 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1429 && IT_CHARPOS (it2) < charpos);
1431 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1432 || it2_prev.current_x > it2_prev.last_visible_x)
1433 top_x = it.glyph_row->x;
1434 else
1436 top_x = it2_prev.current_x;
1437 top_y = it2_prev.current_y;
1441 else if (IT_CHARPOS (it) != charpos)
1443 Lisp_Object cpos = make_number (charpos);
1444 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1445 Lisp_Object string = string_from_display_spec (spec);
1446 struct text_pos tpos;
1447 int replacing_spec_p;
1448 bool newline_in_string
1449 = (STRINGP (string)
1450 && memchr (SDATA (string), '\n', SBYTES (string)));
1452 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1453 replacing_spec_p
1454 = (!NILP (spec)
1455 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1456 charpos, FRAME_WINDOW_P (it.f)));
1457 /* The tricky code below is needed because there's a
1458 discrepancy between move_it_to and how we set cursor
1459 when PT is at the beginning of a portion of text
1460 covered by a display property or an overlay with a
1461 display property, or the display line ends in a
1462 newline from a display string. move_it_to will stop
1463 _after_ such display strings, whereas
1464 set_cursor_from_row conspires with cursor_row_p to
1465 place the cursor on the first glyph produced from the
1466 display string. */
1468 /* We have overshoot PT because it is covered by a
1469 display property that replaces the text it covers.
1470 If the string includes embedded newlines, we are also
1471 in the wrong display line. Backtrack to the correct
1472 line, where the display property begins. */
1473 if (replacing_spec_p)
1475 Lisp_Object startpos, endpos;
1476 EMACS_INT start, end;
1477 struct it it3;
1478 int it3_moved;
1480 /* Find the first and the last buffer positions
1481 covered by the display string. */
1482 endpos =
1483 Fnext_single_char_property_change (cpos, Qdisplay,
1484 Qnil, Qnil);
1485 startpos =
1486 Fprevious_single_char_property_change (endpos, Qdisplay,
1487 Qnil, Qnil);
1488 start = XFASTINT (startpos);
1489 end = XFASTINT (endpos);
1490 /* Move to the last buffer position before the
1491 display property. */
1492 start_display (&it3, w, top);
1493 if (start > CHARPOS (top))
1494 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1495 /* Move forward one more line if the position before
1496 the display string is a newline or if it is the
1497 rightmost character on a line that is
1498 continued or word-wrapped. */
1499 if (it3.method == GET_FROM_BUFFER
1500 && (it3.c == '\n'
1501 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1502 move_it_by_lines (&it3, 1);
1503 else if (move_it_in_display_line_to (&it3, -1,
1504 it3.current_x
1505 + it3.pixel_width,
1506 MOVE_TO_X)
1507 == MOVE_LINE_CONTINUED)
1509 move_it_by_lines (&it3, 1);
1510 /* When we are under word-wrap, the #$@%!
1511 move_it_by_lines moves 2 lines, so we need to
1512 fix that up. */
1513 if (it3.line_wrap == WORD_WRAP)
1514 move_it_by_lines (&it3, -1);
1517 /* Record the vertical coordinate of the display
1518 line where we wound up. */
1519 top_y = it3.current_y;
1520 if (it3.bidi_p)
1522 /* When characters are reordered for display,
1523 the character displayed to the left of the
1524 display string could be _after_ the display
1525 property in the logical order. Use the
1526 smallest vertical position of these two. */
1527 start_display (&it3, w, top);
1528 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1529 if (it3.current_y < top_y)
1530 top_y = it3.current_y;
1532 /* Move from the top of the window to the beginning
1533 of the display line where the display string
1534 begins. */
1535 start_display (&it3, w, top);
1536 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1537 /* If it3_moved stays zero after the 'while' loop
1538 below, that means we already were at a newline
1539 before the loop (e.g., the display string begins
1540 with a newline), so we don't need to (and cannot)
1541 inspect the glyphs of it3.glyph_row, because
1542 PRODUCE_GLYPHS will not produce anything for a
1543 newline, and thus it3.glyph_row stays at its
1544 stale content it got at top of the window. */
1545 it3_moved = 0;
1546 /* Finally, advance the iterator until we hit the
1547 first display element whose character position is
1548 CHARPOS, or until the first newline from the
1549 display string, which signals the end of the
1550 display line. */
1551 while (get_next_display_element (&it3))
1553 PRODUCE_GLYPHS (&it3);
1554 if (IT_CHARPOS (it3) == charpos
1555 || ITERATOR_AT_END_OF_LINE_P (&it3))
1556 break;
1557 it3_moved = 1;
1558 set_iterator_to_next (&it3, 0);
1560 top_x = it3.current_x - it3.pixel_width;
1561 /* Normally, we would exit the above loop because we
1562 found the display element whose character
1563 position is CHARPOS. For the contingency that we
1564 didn't, and stopped at the first newline from the
1565 display string, move back over the glyphs
1566 produced from the string, until we find the
1567 rightmost glyph not from the string. */
1568 if (it3_moved
1569 && newline_in_string
1570 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1572 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1573 + it3.glyph_row->used[TEXT_AREA];
1575 while (EQ ((g - 1)->object, string))
1577 --g;
1578 top_x -= g->pixel_width;
1580 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1581 + it3.glyph_row->used[TEXT_AREA]);
1586 *x = top_x;
1587 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1588 *rtop = max (0, window_top_y - top_y);
1589 *rbot = max (0, bottom_y - it.last_visible_y);
1590 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1591 - max (top_y, window_top_y)));
1592 *vpos = it.vpos;
1593 if (it.bidi_it.paragraph_dir == R2L)
1594 r2l = true;
1597 else
1599 /* Either we were asked to provide info about WINDOW_END, or
1600 CHARPOS is in the partially visible glyph row at end of
1601 window. */
1602 struct it it2;
1603 void *it2data = NULL;
1605 SAVE_IT (it2, it, it2data);
1606 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1607 move_it_by_lines (&it, 1);
1608 if (charpos < IT_CHARPOS (it)
1609 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1611 visible_p = true;
1612 RESTORE_IT (&it2, &it2, it2data);
1613 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1614 *x = it2.current_x;
1615 *y = it2.current_y + it2.max_ascent - it2.ascent;
1616 *rtop = max (0, -it2.current_y);
1617 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1618 - it.last_visible_y));
1619 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1620 it.last_visible_y)
1621 - max (it2.current_y,
1622 WINDOW_HEADER_LINE_HEIGHT (w))));
1623 *vpos = it2.vpos;
1624 if (it2.bidi_it.paragraph_dir == R2L)
1625 r2l = true;
1627 else
1628 bidi_unshelve_cache (it2data, 1);
1630 bidi_unshelve_cache (itdata, 0);
1632 if (old_buffer)
1633 set_buffer_internal_1 (old_buffer);
1635 if (visible_p)
1637 if (w->hscroll > 0)
1638 *x -=
1639 window_hscroll_limited (w, WINDOW_XFRAME (w))
1640 * WINDOW_FRAME_COLUMN_WIDTH (w);
1641 /* For lines in an R2L paragraph, we need to mirror the X pixel
1642 coordinate wrt the text area. For the reasons, see the
1643 commentary in buffer_posn_from_coords and the explanation of
1644 the geometry used by the move_it_* functions at the end of
1645 the large commentary near the beginning of this file. */
1646 if (r2l)
1647 *x = window_box_width (w, TEXT_AREA) - *x - 1;
1650 #if 0
1651 /* Debugging code. */
1652 if (visible_p)
1653 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1654 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1655 else
1656 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1657 #endif
1659 return visible_p;
1663 /* Return the next character from STR. Return in *LEN the length of
1664 the character. This is like STRING_CHAR_AND_LENGTH but never
1665 returns an invalid character. If we find one, we return a `?', but
1666 with the length of the invalid character. */
1668 static int
1669 string_char_and_length (const unsigned char *str, int *len)
1671 int c;
1673 c = STRING_CHAR_AND_LENGTH (str, *len);
1674 if (!CHAR_VALID_P (c))
1675 /* We may not change the length here because other places in Emacs
1676 don't use this function, i.e. they silently accept invalid
1677 characters. */
1678 c = '?';
1680 return c;
1685 /* Given a position POS containing a valid character and byte position
1686 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1688 static struct text_pos
1689 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1691 eassert (STRINGP (string) && nchars >= 0);
1693 if (STRING_MULTIBYTE (string))
1695 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1696 int len;
1698 while (nchars--)
1700 string_char_and_length (p, &len);
1701 p += len;
1702 CHARPOS (pos) += 1;
1703 BYTEPOS (pos) += len;
1706 else
1707 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1709 return pos;
1713 /* Value is the text position, i.e. character and byte position,
1714 for character position CHARPOS in STRING. */
1716 static struct text_pos
1717 string_pos (ptrdiff_t charpos, Lisp_Object string)
1719 struct text_pos pos;
1720 eassert (STRINGP (string));
1721 eassert (charpos >= 0);
1722 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1723 return pos;
1727 /* Value is a text position, i.e. character and byte position, for
1728 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1729 means recognize multibyte characters. */
1731 static struct text_pos
1732 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1734 struct text_pos pos;
1736 eassert (s != NULL);
1737 eassert (charpos >= 0);
1739 if (multibyte_p)
1741 int len;
1743 SET_TEXT_POS (pos, 0, 0);
1744 while (charpos--)
1746 string_char_and_length ((const unsigned char *) s, &len);
1747 s += len;
1748 CHARPOS (pos) += 1;
1749 BYTEPOS (pos) += len;
1752 else
1753 SET_TEXT_POS (pos, charpos, charpos);
1755 return pos;
1759 /* Value is the number of characters in C string S. MULTIBYTE_P
1760 non-zero means recognize multibyte characters. */
1762 static ptrdiff_t
1763 number_of_chars (const char *s, bool multibyte_p)
1765 ptrdiff_t nchars;
1767 if (multibyte_p)
1769 ptrdiff_t rest = strlen (s);
1770 int len;
1771 const unsigned char *p = (const unsigned char *) s;
1773 for (nchars = 0; rest > 0; ++nchars)
1775 string_char_and_length (p, &len);
1776 rest -= len, p += len;
1779 else
1780 nchars = strlen (s);
1782 return nchars;
1786 /* Compute byte position NEWPOS->bytepos corresponding to
1787 NEWPOS->charpos. POS is a known position in string STRING.
1788 NEWPOS->charpos must be >= POS.charpos. */
1790 static void
1791 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1793 eassert (STRINGP (string));
1794 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1796 if (STRING_MULTIBYTE (string))
1797 *newpos = string_pos_nchars_ahead (pos, string,
1798 CHARPOS (*newpos) - CHARPOS (pos));
1799 else
1800 BYTEPOS (*newpos) = CHARPOS (*newpos);
1803 /* EXPORT:
1804 Return an estimation of the pixel height of mode or header lines on
1805 frame F. FACE_ID specifies what line's height to estimate. */
1808 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1810 #ifdef HAVE_WINDOW_SYSTEM
1811 if (FRAME_WINDOW_P (f))
1813 int height = FONT_HEIGHT (FRAME_FONT (f));
1815 /* This function is called so early when Emacs starts that the face
1816 cache and mode line face are not yet initialized. */
1817 if (FRAME_FACE_CACHE (f))
1819 struct face *face = FACE_FROM_ID (f, face_id);
1820 if (face)
1822 if (face->font)
1823 height = FONT_HEIGHT (face->font);
1824 if (face->box_line_width > 0)
1825 height += 2 * face->box_line_width;
1829 return height;
1831 #endif
1833 return 1;
1836 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1837 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1838 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1839 not force the value into range. */
1841 void
1842 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1843 int *x, int *y, NativeRectangle *bounds, int noclip)
1846 #ifdef HAVE_WINDOW_SYSTEM
1847 if (FRAME_WINDOW_P (f))
1849 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1850 even for negative values. */
1851 if (pix_x < 0)
1852 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1853 if (pix_y < 0)
1854 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1856 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1857 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1859 if (bounds)
1860 STORE_NATIVE_RECT (*bounds,
1861 FRAME_COL_TO_PIXEL_X (f, pix_x),
1862 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1863 FRAME_COLUMN_WIDTH (f) - 1,
1864 FRAME_LINE_HEIGHT (f) - 1);
1866 /* PXW: Should we clip pixels before converting to columns/lines? */
1867 if (!noclip)
1869 if (pix_x < 0)
1870 pix_x = 0;
1871 else if (pix_x > FRAME_TOTAL_COLS (f))
1872 pix_x = FRAME_TOTAL_COLS (f);
1874 if (pix_y < 0)
1875 pix_y = 0;
1876 else if (pix_y > FRAME_TOTAL_LINES (f))
1877 pix_y = FRAME_TOTAL_LINES (f);
1880 #endif
1882 *x = pix_x;
1883 *y = pix_y;
1887 /* Find the glyph under window-relative coordinates X/Y in window W.
1888 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1889 strings. Return in *HPOS and *VPOS the row and column number of
1890 the glyph found. Return in *AREA the glyph area containing X.
1891 Value is a pointer to the glyph found or null if X/Y is not on
1892 text, or we can't tell because W's current matrix is not up to
1893 date. */
1895 static struct glyph *
1896 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1897 int *dx, int *dy, int *area)
1899 struct glyph *glyph, *end;
1900 struct glyph_row *row = NULL;
1901 int x0, i;
1903 /* Find row containing Y. Give up if some row is not enabled. */
1904 for (i = 0; i < w->current_matrix->nrows; ++i)
1906 row = MATRIX_ROW (w->current_matrix, i);
1907 if (!row->enabled_p)
1908 return NULL;
1909 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1910 break;
1913 *vpos = i;
1914 *hpos = 0;
1916 /* Give up if Y is not in the window. */
1917 if (i == w->current_matrix->nrows)
1918 return NULL;
1920 /* Get the glyph area containing X. */
1921 if (w->pseudo_window_p)
1923 *area = TEXT_AREA;
1924 x0 = 0;
1926 else
1928 if (x < window_box_left_offset (w, TEXT_AREA))
1930 *area = LEFT_MARGIN_AREA;
1931 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1933 else if (x < window_box_right_offset (w, TEXT_AREA))
1935 *area = TEXT_AREA;
1936 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1938 else
1940 *area = RIGHT_MARGIN_AREA;
1941 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1945 /* Find glyph containing X. */
1946 glyph = row->glyphs[*area];
1947 end = glyph + row->used[*area];
1948 x -= x0;
1949 while (glyph < end && x >= glyph->pixel_width)
1951 x -= glyph->pixel_width;
1952 ++glyph;
1955 if (glyph == end)
1956 return NULL;
1958 if (dx)
1960 *dx = x;
1961 *dy = y - (row->y + row->ascent - glyph->ascent);
1964 *hpos = glyph - row->glyphs[*area];
1965 return glyph;
1968 /* Convert frame-relative x/y to coordinates relative to window W.
1969 Takes pseudo-windows into account. */
1971 static void
1972 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1974 if (w->pseudo_window_p)
1976 /* A pseudo-window is always full-width, and starts at the
1977 left edge of the frame, plus a frame border. */
1978 struct frame *f = XFRAME (w->frame);
1979 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1980 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1982 else
1984 *x -= WINDOW_LEFT_EDGE_X (w);
1985 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1989 #ifdef HAVE_WINDOW_SYSTEM
1991 /* EXPORT:
1992 Return in RECTS[] at most N clipping rectangles for glyph string S.
1993 Return the number of stored rectangles. */
1996 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1998 XRectangle r;
2000 if (n <= 0)
2001 return 0;
2003 if (s->row->full_width_p)
2005 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2006 r.x = WINDOW_LEFT_EDGE_X (s->w);
2007 if (s->row->mode_line_p)
2008 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2009 else
2010 r.width = WINDOW_PIXEL_WIDTH (s->w);
2012 /* Unless displaying a mode or menu bar line, which are always
2013 fully visible, clip to the visible part of the row. */
2014 if (s->w->pseudo_window_p)
2015 r.height = s->row->visible_height;
2016 else
2017 r.height = s->height;
2019 else
2021 /* This is a text line that may be partially visible. */
2022 r.x = window_box_left (s->w, s->area);
2023 r.width = window_box_width (s->w, s->area);
2024 r.height = s->row->visible_height;
2027 if (s->clip_head)
2028 if (r.x < s->clip_head->x)
2030 if (r.width >= s->clip_head->x - r.x)
2031 r.width -= s->clip_head->x - r.x;
2032 else
2033 r.width = 0;
2034 r.x = s->clip_head->x;
2036 if (s->clip_tail)
2037 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2039 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2040 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2041 else
2042 r.width = 0;
2045 /* If S draws overlapping rows, it's sufficient to use the top and
2046 bottom of the window for clipping because this glyph string
2047 intentionally draws over other lines. */
2048 if (s->for_overlaps)
2050 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2051 r.height = window_text_bottom_y (s->w) - r.y;
2053 /* Alas, the above simple strategy does not work for the
2054 environments with anti-aliased text: if the same text is
2055 drawn onto the same place multiple times, it gets thicker.
2056 If the overlap we are processing is for the erased cursor, we
2057 take the intersection with the rectangle of the cursor. */
2058 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2060 XRectangle rc, r_save = r;
2062 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2063 rc.y = s->w->phys_cursor.y;
2064 rc.width = s->w->phys_cursor_width;
2065 rc.height = s->w->phys_cursor_height;
2067 x_intersect_rectangles (&r_save, &rc, &r);
2070 else
2072 /* Don't use S->y for clipping because it doesn't take partially
2073 visible lines into account. For example, it can be negative for
2074 partially visible lines at the top of a window. */
2075 if (!s->row->full_width_p
2076 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2077 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2078 else
2079 r.y = max (0, s->row->y);
2082 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2084 /* If drawing the cursor, don't let glyph draw outside its
2085 advertised boundaries. Cleartype does this under some circumstances. */
2086 if (s->hl == DRAW_CURSOR)
2088 struct glyph *glyph = s->first_glyph;
2089 int height, max_y;
2091 if (s->x > r.x)
2093 if (r.width >= s->x - r.x)
2094 r.width -= s->x - r.x;
2095 else /* R2L hscrolled row with cursor outside text area */
2096 r.width = 0;
2097 r.x = s->x;
2099 r.width = min (r.width, glyph->pixel_width);
2101 /* If r.y is below window bottom, ensure that we still see a cursor. */
2102 height = min (glyph->ascent + glyph->descent,
2103 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2104 max_y = window_text_bottom_y (s->w) - height;
2105 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2106 if (s->ybase - glyph->ascent > max_y)
2108 r.y = max_y;
2109 r.height = height;
2111 else
2113 /* Don't draw cursor glyph taller than our actual glyph. */
2114 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2115 if (height < r.height)
2117 max_y = r.y + r.height;
2118 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2119 r.height = min (max_y - r.y, height);
2124 if (s->row->clip)
2126 XRectangle r_save = r;
2128 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2129 r.width = 0;
2132 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2133 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2135 #ifdef CONVERT_FROM_XRECT
2136 CONVERT_FROM_XRECT (r, *rects);
2137 #else
2138 *rects = r;
2139 #endif
2140 return 1;
2142 else
2144 /* If we are processing overlapping and allowed to return
2145 multiple clipping rectangles, we exclude the row of the glyph
2146 string from the clipping rectangle. This is to avoid drawing
2147 the same text on the environment with anti-aliasing. */
2148 #ifdef CONVERT_FROM_XRECT
2149 XRectangle rs[2];
2150 #else
2151 XRectangle *rs = rects;
2152 #endif
2153 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2155 if (s->for_overlaps & OVERLAPS_PRED)
2157 rs[i] = r;
2158 if (r.y + r.height > row_y)
2160 if (r.y < row_y)
2161 rs[i].height = row_y - r.y;
2162 else
2163 rs[i].height = 0;
2165 i++;
2167 if (s->for_overlaps & OVERLAPS_SUCC)
2169 rs[i] = r;
2170 if (r.y < row_y + s->row->visible_height)
2172 if (r.y + r.height > row_y + s->row->visible_height)
2174 rs[i].y = row_y + s->row->visible_height;
2175 rs[i].height = r.y + r.height - rs[i].y;
2177 else
2178 rs[i].height = 0;
2180 i++;
2183 n = i;
2184 #ifdef CONVERT_FROM_XRECT
2185 for (i = 0; i < n; i++)
2186 CONVERT_FROM_XRECT (rs[i], rects[i]);
2187 #endif
2188 return n;
2192 /* EXPORT:
2193 Return in *NR the clipping rectangle for glyph string S. */
2195 void
2196 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2198 get_glyph_string_clip_rects (s, nr, 1);
2202 /* EXPORT:
2203 Return the position and height of the phys cursor in window W.
2204 Set w->phys_cursor_width to width of phys cursor.
2207 void
2208 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2209 struct glyph *glyph, int *xp, int *yp, int *heightp)
2211 struct frame *f = XFRAME (WINDOW_FRAME (w));
2212 int x, y, wd, h, h0, y0;
2214 /* Compute the width of the rectangle to draw. If on a stretch
2215 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2216 rectangle as wide as the glyph, but use a canonical character
2217 width instead. */
2218 wd = glyph->pixel_width;
2220 x = w->phys_cursor.x;
2221 if (x < 0)
2223 wd += x;
2224 x = 0;
2227 if (glyph->type == STRETCH_GLYPH
2228 && !x_stretch_cursor_p)
2229 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2230 w->phys_cursor_width = wd;
2232 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2234 /* If y is below window bottom, ensure that we still see a cursor. */
2235 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2237 h = max (h0, glyph->ascent + glyph->descent);
2238 h0 = min (h0, glyph->ascent + glyph->descent);
2240 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2241 if (y < y0)
2243 h = max (h - (y0 - y) + 1, h0);
2244 y = y0 - 1;
2246 else
2248 y0 = window_text_bottom_y (w) - h0;
2249 if (y > y0)
2251 h += y - y0;
2252 y = y0;
2256 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2257 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2258 *heightp = h;
2262 * Remember which glyph the mouse is over.
2265 void
2266 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2268 Lisp_Object window;
2269 struct window *w;
2270 struct glyph_row *r, *gr, *end_row;
2271 enum window_part part;
2272 enum glyph_row_area area;
2273 int x, y, width, height;
2275 /* Try to determine frame pixel position and size of the glyph under
2276 frame pixel coordinates X/Y on frame F. */
2278 if (window_resize_pixelwise)
2280 width = height = 1;
2281 goto virtual_glyph;
2283 else if (!f->glyphs_initialized_p
2284 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2285 NILP (window)))
2287 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2288 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2289 goto virtual_glyph;
2292 w = XWINDOW (window);
2293 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2294 height = WINDOW_FRAME_LINE_HEIGHT (w);
2296 x = window_relative_x_coord (w, part, gx);
2297 y = gy - WINDOW_TOP_EDGE_Y (w);
2299 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2300 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2302 if (w->pseudo_window_p)
2304 area = TEXT_AREA;
2305 part = ON_MODE_LINE; /* Don't adjust margin. */
2306 goto text_glyph;
2309 switch (part)
2311 case ON_LEFT_MARGIN:
2312 area = LEFT_MARGIN_AREA;
2313 goto text_glyph;
2315 case ON_RIGHT_MARGIN:
2316 area = RIGHT_MARGIN_AREA;
2317 goto text_glyph;
2319 case ON_HEADER_LINE:
2320 case ON_MODE_LINE:
2321 gr = (part == ON_HEADER_LINE
2322 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2323 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2324 gy = gr->y;
2325 area = TEXT_AREA;
2326 goto text_glyph_row_found;
2328 case ON_TEXT:
2329 area = TEXT_AREA;
2331 text_glyph:
2332 gr = 0; gy = 0;
2333 for (; r <= end_row && r->enabled_p; ++r)
2334 if (r->y + r->height > y)
2336 gr = r; gy = r->y;
2337 break;
2340 text_glyph_row_found:
2341 if (gr && gy <= y)
2343 struct glyph *g = gr->glyphs[area];
2344 struct glyph *end = g + gr->used[area];
2346 height = gr->height;
2347 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2348 if (gx + g->pixel_width > x)
2349 break;
2351 if (g < end)
2353 if (g->type == IMAGE_GLYPH)
2355 /* Don't remember when mouse is over image, as
2356 image may have hot-spots. */
2357 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2358 return;
2360 width = g->pixel_width;
2362 else
2364 /* Use nominal char spacing at end of line. */
2365 x -= gx;
2366 gx += (x / width) * width;
2369 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2371 gx += window_box_left_offset (w, area);
2372 /* Don't expand over the modeline to make sure the vertical
2373 drag cursor is shown early enough. */
2374 height = min (height,
2375 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2378 else
2380 /* Use nominal line height at end of window. */
2381 gx = (x / width) * width;
2382 y -= gy;
2383 gy += (y / height) * height;
2384 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2385 /* See comment above. */
2386 height = min (height,
2387 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2389 break;
2391 case ON_LEFT_FRINGE:
2392 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2393 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2394 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2395 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2396 goto row_glyph;
2398 case ON_RIGHT_FRINGE:
2399 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2400 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2401 : window_box_right_offset (w, TEXT_AREA));
2402 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2403 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2404 && !WINDOW_RIGHTMOST_P (w))
2405 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2406 /* Make sure the vertical border can get her own glyph to the
2407 right of the one we build here. */
2408 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2409 else
2410 width = WINDOW_PIXEL_WIDTH (w) - gx;
2411 else
2412 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2414 goto row_glyph;
2416 case ON_VERTICAL_BORDER:
2417 gx = WINDOW_PIXEL_WIDTH (w) - width;
2418 goto row_glyph;
2420 case ON_VERTICAL_SCROLL_BAR:
2421 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2423 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2424 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2425 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2426 : 0)));
2427 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2429 row_glyph:
2430 gr = 0, gy = 0;
2431 for (; r <= end_row && r->enabled_p; ++r)
2432 if (r->y + r->height > y)
2434 gr = r; gy = r->y;
2435 break;
2438 if (gr && gy <= y)
2439 height = gr->height;
2440 else
2442 /* Use nominal line height at end of window. */
2443 y -= gy;
2444 gy += (y / height) * height;
2446 break;
2448 case ON_RIGHT_DIVIDER:
2449 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2450 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2451 gy = 0;
2452 /* The bottom divider prevails. */
2453 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2454 goto add_edge;
2456 case ON_BOTTOM_DIVIDER:
2457 gx = 0;
2458 width = WINDOW_PIXEL_WIDTH (w);
2459 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2460 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2461 goto add_edge;
2463 default:
2465 virtual_glyph:
2466 /* If there is no glyph under the mouse, then we divide the screen
2467 into a grid of the smallest glyph in the frame, and use that
2468 as our "glyph". */
2470 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2471 round down even for negative values. */
2472 if (gx < 0)
2473 gx -= width - 1;
2474 if (gy < 0)
2475 gy -= height - 1;
2477 gx = (gx / width) * width;
2478 gy = (gy / height) * height;
2480 goto store_rect;
2483 add_edge:
2484 gx += WINDOW_LEFT_EDGE_X (w);
2485 gy += WINDOW_TOP_EDGE_Y (w);
2487 store_rect:
2488 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2490 /* Visible feedback for debugging. */
2491 #if 0
2492 #if HAVE_X_WINDOWS
2493 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2494 f->output_data.x->normal_gc,
2495 gx, gy, width, height);
2496 #endif
2497 #endif
2501 #endif /* HAVE_WINDOW_SYSTEM */
2503 static void
2504 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2506 eassert (w);
2507 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2508 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2509 w->window_end_vpos
2510 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2513 /***********************************************************************
2514 Lisp form evaluation
2515 ***********************************************************************/
2517 /* Error handler for safe_eval and safe_call. */
2519 static Lisp_Object
2520 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2522 add_to_log ("Error during redisplay: %S signaled %S",
2523 Flist (nargs, args), arg);
2524 return Qnil;
2527 /* Call function FUNC with the rest of NARGS - 1 arguments
2528 following. Return the result, or nil if something went
2529 wrong. Prevent redisplay during the evaluation. */
2531 static Lisp_Object
2532 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2534 Lisp_Object val;
2536 if (inhibit_eval_during_redisplay)
2537 val = Qnil;
2538 else
2540 ptrdiff_t i;
2541 ptrdiff_t count = SPECPDL_INDEX ();
2542 Lisp_Object *args;
2543 USE_SAFE_ALLOCA;
2544 SAFE_ALLOCA_LISP (args, nargs);
2546 args[0] = func;
2547 for (i = 1; i < nargs; i++)
2548 args[i] = va_arg (ap, Lisp_Object);
2550 specbind (Qinhibit_redisplay, Qt);
2551 if (inhibit_quit)
2552 specbind (Qinhibit_quit, Qt);
2553 /* Use Qt to ensure debugger does not run,
2554 so there is no possibility of wanting to redisplay. */
2555 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2556 safe_eval_handler);
2557 SAFE_FREE ();
2558 val = unbind_to (count, val);
2561 return val;
2564 Lisp_Object
2565 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2567 Lisp_Object retval;
2568 va_list ap;
2570 va_start (ap, func);
2571 retval = safe__call (false, nargs, func, ap);
2572 va_end (ap);
2573 return retval;
2576 /* Call function FN with one argument ARG.
2577 Return the result, or nil if something went wrong. */
2579 Lisp_Object
2580 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2582 return safe_call (2, fn, arg);
2585 static Lisp_Object
2586 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2588 Lisp_Object retval;
2589 va_list ap;
2591 va_start (ap, fn);
2592 retval = safe__call (inhibit_quit, 2, fn, ap);
2593 va_end (ap);
2594 return retval;
2597 Lisp_Object
2598 safe_eval (Lisp_Object sexpr)
2600 return safe__call1 (false, Qeval, sexpr);
2603 static Lisp_Object
2604 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2606 return safe__call1 (inhibit_quit, Qeval, sexpr);
2609 /* Call function FN with two arguments ARG1 and ARG2.
2610 Return the result, or nil if something went wrong. */
2612 Lisp_Object
2613 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2615 return safe_call (3, fn, arg1, arg2);
2620 /***********************************************************************
2621 Debugging
2622 ***********************************************************************/
2624 #if 0
2626 /* Define CHECK_IT to perform sanity checks on iterators.
2627 This is for debugging. It is too slow to do unconditionally. */
2629 static void
2630 check_it (struct it *it)
2632 if (it->method == GET_FROM_STRING)
2634 eassert (STRINGP (it->string));
2635 eassert (IT_STRING_CHARPOS (*it) >= 0);
2637 else
2639 eassert (IT_STRING_CHARPOS (*it) < 0);
2640 if (it->method == GET_FROM_BUFFER)
2642 /* Check that character and byte positions agree. */
2643 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2647 if (it->dpvec)
2648 eassert (it->current.dpvec_index >= 0);
2649 else
2650 eassert (it->current.dpvec_index < 0);
2653 #define CHECK_IT(IT) check_it ((IT))
2655 #else /* not 0 */
2657 #define CHECK_IT(IT) (void) 0
2659 #endif /* not 0 */
2662 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2664 /* Check that the window end of window W is what we expect it
2665 to be---the last row in the current matrix displaying text. */
2667 static void
2668 check_window_end (struct window *w)
2670 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2672 struct glyph_row *row;
2673 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2674 !row->enabled_p
2675 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2676 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2680 #define CHECK_WINDOW_END(W) check_window_end ((W))
2682 #else
2684 #define CHECK_WINDOW_END(W) (void) 0
2686 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2688 /***********************************************************************
2689 Iterator initialization
2690 ***********************************************************************/
2692 /* Initialize IT for displaying current_buffer in window W, starting
2693 at character position CHARPOS. CHARPOS < 0 means that no buffer
2694 position is specified which is useful when the iterator is assigned
2695 a position later. BYTEPOS is the byte position corresponding to
2696 CHARPOS.
2698 If ROW is not null, calls to produce_glyphs with IT as parameter
2699 will produce glyphs in that row.
2701 BASE_FACE_ID is the id of a base face to use. It must be one of
2702 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2703 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2704 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2706 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2707 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2708 will be initialized to use the corresponding mode line glyph row of
2709 the desired matrix of W. */
2711 void
2712 init_iterator (struct it *it, struct window *w,
2713 ptrdiff_t charpos, ptrdiff_t bytepos,
2714 struct glyph_row *row, enum face_id base_face_id)
2716 enum face_id remapped_base_face_id = base_face_id;
2718 /* Some precondition checks. */
2719 eassert (w != NULL && it != NULL);
2720 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2721 && charpos <= ZV));
2723 /* If face attributes have been changed since the last redisplay,
2724 free realized faces now because they depend on face definitions
2725 that might have changed. Don't free faces while there might be
2726 desired matrices pending which reference these faces. */
2727 if (face_change_count && !inhibit_free_realized_faces)
2729 face_change_count = 0;
2730 free_all_realized_faces (Qnil);
2733 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2734 if (! NILP (Vface_remapping_alist))
2735 remapped_base_face_id
2736 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2738 /* Use one of the mode line rows of W's desired matrix if
2739 appropriate. */
2740 if (row == NULL)
2742 if (base_face_id == MODE_LINE_FACE_ID
2743 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2744 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2745 else if (base_face_id == HEADER_LINE_FACE_ID)
2746 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2749 /* Clear IT. */
2750 memset (it, 0, sizeof *it);
2751 it->current.overlay_string_index = -1;
2752 it->current.dpvec_index = -1;
2753 it->base_face_id = remapped_base_face_id;
2754 it->string = Qnil;
2755 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2756 it->paragraph_embedding = L2R;
2757 it->bidi_it.string.lstring = Qnil;
2758 it->bidi_it.string.s = NULL;
2759 it->bidi_it.string.bufpos = 0;
2760 it->bidi_it.w = w;
2762 /* The window in which we iterate over current_buffer: */
2763 XSETWINDOW (it->window, w);
2764 it->w = w;
2765 it->f = XFRAME (w->frame);
2767 it->cmp_it.id = -1;
2769 /* Extra space between lines (on window systems only). */
2770 if (base_face_id == DEFAULT_FACE_ID
2771 && FRAME_WINDOW_P (it->f))
2773 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2774 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2775 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2776 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2777 * FRAME_LINE_HEIGHT (it->f));
2778 else if (it->f->extra_line_spacing > 0)
2779 it->extra_line_spacing = it->f->extra_line_spacing;
2780 it->max_extra_line_spacing = 0;
2783 /* If realized faces have been removed, e.g. because of face
2784 attribute changes of named faces, recompute them. When running
2785 in batch mode, the face cache of the initial frame is null. If
2786 we happen to get called, make a dummy face cache. */
2787 if (FRAME_FACE_CACHE (it->f) == NULL)
2788 init_frame_faces (it->f);
2789 if (FRAME_FACE_CACHE (it->f)->used == 0)
2790 recompute_basic_faces (it->f);
2792 /* Current value of the `slice', `space-width', and 'height' properties. */
2793 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2794 it->space_width = Qnil;
2795 it->font_height = Qnil;
2796 it->override_ascent = -1;
2798 /* Are control characters displayed as `^C'? */
2799 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2801 /* -1 means everything between a CR and the following line end
2802 is invisible. >0 means lines indented more than this value are
2803 invisible. */
2804 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2805 ? (clip_to_bounds
2806 (-1, XINT (BVAR (current_buffer, selective_display)),
2807 PTRDIFF_MAX))
2808 : (!NILP (BVAR (current_buffer, selective_display))
2809 ? -1 : 0));
2810 it->selective_display_ellipsis_p
2811 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2813 /* Display table to use. */
2814 it->dp = window_display_table (w);
2816 /* Are multibyte characters enabled in current_buffer? */
2817 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2819 /* Get the position at which the redisplay_end_trigger hook should
2820 be run, if it is to be run at all. */
2821 if (MARKERP (w->redisplay_end_trigger)
2822 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2823 it->redisplay_end_trigger_charpos
2824 = marker_position (w->redisplay_end_trigger);
2825 else if (INTEGERP (w->redisplay_end_trigger))
2826 it->redisplay_end_trigger_charpos
2827 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2828 PTRDIFF_MAX);
2830 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2832 /* Are lines in the display truncated? */
2833 if (base_face_id != DEFAULT_FACE_ID
2834 || it->w->hscroll
2835 || (! WINDOW_FULL_WIDTH_P (it->w)
2836 && ((!NILP (Vtruncate_partial_width_windows)
2837 && !INTEGERP (Vtruncate_partial_width_windows))
2838 || (INTEGERP (Vtruncate_partial_width_windows)
2839 /* PXW: Shall we do something about this? */
2840 && (WINDOW_TOTAL_COLS (it->w)
2841 < XINT (Vtruncate_partial_width_windows))))))
2842 it->line_wrap = TRUNCATE;
2843 else if (NILP (BVAR (current_buffer, truncate_lines)))
2844 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2845 ? WINDOW_WRAP : WORD_WRAP;
2846 else
2847 it->line_wrap = TRUNCATE;
2849 /* Get dimensions of truncation and continuation glyphs. These are
2850 displayed as fringe bitmaps under X, but we need them for such
2851 frames when the fringes are turned off. But leave the dimensions
2852 zero for tooltip frames, as these glyphs look ugly there and also
2853 sabotage calculations of tooltip dimensions in x-show-tip. */
2854 #ifdef HAVE_WINDOW_SYSTEM
2855 if (!(FRAME_WINDOW_P (it->f)
2856 && FRAMEP (tip_frame)
2857 && it->f == XFRAME (tip_frame)))
2858 #endif
2860 if (it->line_wrap == TRUNCATE)
2862 /* We will need the truncation glyph. */
2863 eassert (it->glyph_row == NULL);
2864 produce_special_glyphs (it, IT_TRUNCATION);
2865 it->truncation_pixel_width = it->pixel_width;
2867 else
2869 /* We will need the continuation glyph. */
2870 eassert (it->glyph_row == NULL);
2871 produce_special_glyphs (it, IT_CONTINUATION);
2872 it->continuation_pixel_width = it->pixel_width;
2876 /* Reset these values to zero because the produce_special_glyphs
2877 above has changed them. */
2878 it->pixel_width = it->ascent = it->descent = 0;
2879 it->phys_ascent = it->phys_descent = 0;
2881 /* Set this after getting the dimensions of truncation and
2882 continuation glyphs, so that we don't produce glyphs when calling
2883 produce_special_glyphs, above. */
2884 it->glyph_row = row;
2885 it->area = TEXT_AREA;
2887 /* Get the dimensions of the display area. The display area
2888 consists of the visible window area plus a horizontally scrolled
2889 part to the left of the window. All x-values are relative to the
2890 start of this total display area. */
2891 if (base_face_id != DEFAULT_FACE_ID)
2893 /* Mode lines, menu bar in terminal frames. */
2894 it->first_visible_x = 0;
2895 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2897 else
2899 it->first_visible_x
2900 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2901 it->last_visible_x = (it->first_visible_x
2902 + window_box_width (w, TEXT_AREA));
2904 /* If we truncate lines, leave room for the truncation glyph(s) at
2905 the right margin. Otherwise, leave room for the continuation
2906 glyph(s). Done only if the window has no right fringe. */
2907 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
2909 if (it->line_wrap == TRUNCATE)
2910 it->last_visible_x -= it->truncation_pixel_width;
2911 else
2912 it->last_visible_x -= it->continuation_pixel_width;
2915 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2916 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2919 /* Leave room for a border glyph. */
2920 if (!FRAME_WINDOW_P (it->f)
2921 && !WINDOW_RIGHTMOST_P (it->w))
2922 it->last_visible_x -= 1;
2924 it->last_visible_y = window_text_bottom_y (w);
2926 /* For mode lines and alike, arrange for the first glyph having a
2927 left box line if the face specifies a box. */
2928 if (base_face_id != DEFAULT_FACE_ID)
2930 struct face *face;
2932 it->face_id = remapped_base_face_id;
2934 /* If we have a boxed mode line, make the first character appear
2935 with a left box line. */
2936 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2937 if (face && face->box != FACE_NO_BOX)
2938 it->start_of_box_run_p = true;
2941 /* If a buffer position was specified, set the iterator there,
2942 getting overlays and face properties from that position. */
2943 if (charpos >= BUF_BEG (current_buffer))
2945 it->stop_charpos = charpos;
2946 it->end_charpos = ZV;
2947 eassert (charpos == BYTE_TO_CHAR (bytepos));
2948 IT_CHARPOS (*it) = charpos;
2949 IT_BYTEPOS (*it) = bytepos;
2951 /* We will rely on `reseat' to set this up properly, via
2952 handle_face_prop. */
2953 it->face_id = it->base_face_id;
2955 it->start = it->current;
2956 /* Do we need to reorder bidirectional text? Not if this is a
2957 unibyte buffer: by definition, none of the single-byte
2958 characters are strong R2L, so no reordering is needed. And
2959 bidi.c doesn't support unibyte buffers anyway. Also, don't
2960 reorder while we are loading loadup.el, since the tables of
2961 character properties needed for reordering are not yet
2962 available. */
2963 it->bidi_p =
2964 NILP (Vpurify_flag)
2965 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2966 && it->multibyte_p;
2968 /* If we are to reorder bidirectional text, init the bidi
2969 iterator. */
2970 if (it->bidi_p)
2972 /* Since we don't know at this point whether there will be
2973 any R2L lines in the window, we reserve space for
2974 truncation/continuation glyphs even if only the left
2975 fringe is absent. */
2976 if (base_face_id == DEFAULT_FACE_ID
2977 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
2978 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
2980 if (it->line_wrap == TRUNCATE)
2981 it->last_visible_x -= it->truncation_pixel_width;
2982 else
2983 it->last_visible_x -= it->continuation_pixel_width;
2985 /* Note the paragraph direction that this buffer wants to
2986 use. */
2987 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2988 Qleft_to_right))
2989 it->paragraph_embedding = L2R;
2990 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2991 Qright_to_left))
2992 it->paragraph_embedding = R2L;
2993 else
2994 it->paragraph_embedding = NEUTRAL_DIR;
2995 bidi_unshelve_cache (NULL, 0);
2996 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2997 &it->bidi_it);
3000 /* Compute faces etc. */
3001 reseat (it, it->current.pos, 1);
3004 CHECK_IT (it);
3008 /* Initialize IT for the display of window W with window start POS. */
3010 void
3011 start_display (struct it *it, struct window *w, struct text_pos pos)
3013 struct glyph_row *row;
3014 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3016 row = w->desired_matrix->rows + first_vpos;
3017 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3018 it->first_vpos = first_vpos;
3020 /* Don't reseat to previous visible line start if current start
3021 position is in a string or image. */
3022 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3024 int start_at_line_beg_p;
3025 int first_y = it->current_y;
3027 /* If window start is not at a line start, skip forward to POS to
3028 get the correct continuation lines width. */
3029 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3030 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3031 if (!start_at_line_beg_p)
3033 int new_x;
3035 reseat_at_previous_visible_line_start (it);
3036 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3038 new_x = it->current_x + it->pixel_width;
3040 /* If lines are continued, this line may end in the middle
3041 of a multi-glyph character (e.g. a control character
3042 displayed as \003, or in the middle of an overlay
3043 string). In this case move_it_to above will not have
3044 taken us to the start of the continuation line but to the
3045 end of the continued line. */
3046 if (it->current_x > 0
3047 && it->line_wrap != TRUNCATE /* Lines are continued. */
3048 && (/* And glyph doesn't fit on the line. */
3049 new_x > it->last_visible_x
3050 /* Or it fits exactly and we're on a window
3051 system frame. */
3052 || (new_x == it->last_visible_x
3053 && FRAME_WINDOW_P (it->f)
3054 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3055 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3056 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3058 if ((it->current.dpvec_index >= 0
3059 || it->current.overlay_string_index >= 0)
3060 /* If we are on a newline from a display vector or
3061 overlay string, then we are already at the end of
3062 a screen line; no need to go to the next line in
3063 that case, as this line is not really continued.
3064 (If we do go to the next line, C-e will not DTRT.) */
3065 && it->c != '\n')
3067 set_iterator_to_next (it, 1);
3068 move_it_in_display_line_to (it, -1, -1, 0);
3071 it->continuation_lines_width += it->current_x;
3073 /* If the character at POS is displayed via a display
3074 vector, move_it_to above stops at the final glyph of
3075 IT->dpvec. To make the caller redisplay that character
3076 again (a.k.a. start at POS), we need to reset the
3077 dpvec_index to the beginning of IT->dpvec. */
3078 else if (it->current.dpvec_index >= 0)
3079 it->current.dpvec_index = 0;
3081 /* We're starting a new display line, not affected by the
3082 height of the continued line, so clear the appropriate
3083 fields in the iterator structure. */
3084 it->max_ascent = it->max_descent = 0;
3085 it->max_phys_ascent = it->max_phys_descent = 0;
3087 it->current_y = first_y;
3088 it->vpos = 0;
3089 it->current_x = it->hpos = 0;
3095 /* Return 1 if POS is a position in ellipses displayed for invisible
3096 text. W is the window we display, for text property lookup. */
3098 static int
3099 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3101 Lisp_Object prop, window;
3102 int ellipses_p = 0;
3103 ptrdiff_t charpos = CHARPOS (pos->pos);
3105 /* If POS specifies a position in a display vector, this might
3106 be for an ellipsis displayed for invisible text. We won't
3107 get the iterator set up for delivering that ellipsis unless
3108 we make sure that it gets aware of the invisible text. */
3109 if (pos->dpvec_index >= 0
3110 && pos->overlay_string_index < 0
3111 && CHARPOS (pos->string_pos) < 0
3112 && charpos > BEGV
3113 && (XSETWINDOW (window, w),
3114 prop = Fget_char_property (make_number (charpos),
3115 Qinvisible, window),
3116 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3118 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3119 window);
3120 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3123 return ellipses_p;
3127 /* Initialize IT for stepping through current_buffer in window W,
3128 starting at position POS that includes overlay string and display
3129 vector/ control character translation position information. Value
3130 is zero if there are overlay strings with newlines at POS. */
3132 static int
3133 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3135 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3136 int i, overlay_strings_with_newlines = 0;
3138 /* If POS specifies a position in a display vector, this might
3139 be for an ellipsis displayed for invisible text. We won't
3140 get the iterator set up for delivering that ellipsis unless
3141 we make sure that it gets aware of the invisible text. */
3142 if (in_ellipses_for_invisible_text_p (pos, w))
3144 --charpos;
3145 bytepos = 0;
3148 /* Keep in mind: the call to reseat in init_iterator skips invisible
3149 text, so we might end up at a position different from POS. This
3150 is only a problem when POS is a row start after a newline and an
3151 overlay starts there with an after-string, and the overlay has an
3152 invisible property. Since we don't skip invisible text in
3153 display_line and elsewhere immediately after consuming the
3154 newline before the row start, such a POS will not be in a string,
3155 but the call to init_iterator below will move us to the
3156 after-string. */
3157 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3159 /* This only scans the current chunk -- it should scan all chunks.
3160 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3161 to 16 in 22.1 to make this a lesser problem. */
3162 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3164 const char *s = SSDATA (it->overlay_strings[i]);
3165 const char *e = s + SBYTES (it->overlay_strings[i]);
3167 while (s < e && *s != '\n')
3168 ++s;
3170 if (s < e)
3172 overlay_strings_with_newlines = 1;
3173 break;
3177 /* If position is within an overlay string, set up IT to the right
3178 overlay string. */
3179 if (pos->overlay_string_index >= 0)
3181 int relative_index;
3183 /* If the first overlay string happens to have a `display'
3184 property for an image, the iterator will be set up for that
3185 image, and we have to undo that setup first before we can
3186 correct the overlay string index. */
3187 if (it->method == GET_FROM_IMAGE)
3188 pop_it (it);
3190 /* We already have the first chunk of overlay strings in
3191 IT->overlay_strings. Load more until the one for
3192 pos->overlay_string_index is in IT->overlay_strings. */
3193 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3195 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3196 it->current.overlay_string_index = 0;
3197 while (n--)
3199 load_overlay_strings (it, 0);
3200 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3204 it->current.overlay_string_index = pos->overlay_string_index;
3205 relative_index = (it->current.overlay_string_index
3206 % OVERLAY_STRING_CHUNK_SIZE);
3207 it->string = it->overlay_strings[relative_index];
3208 eassert (STRINGP (it->string));
3209 it->current.string_pos = pos->string_pos;
3210 it->method = GET_FROM_STRING;
3211 it->end_charpos = SCHARS (it->string);
3212 /* Set up the bidi iterator for this overlay string. */
3213 if (it->bidi_p)
3215 it->bidi_it.string.lstring = it->string;
3216 it->bidi_it.string.s = NULL;
3217 it->bidi_it.string.schars = SCHARS (it->string);
3218 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3219 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3220 it->bidi_it.string.unibyte = !it->multibyte_p;
3221 it->bidi_it.w = it->w;
3222 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3223 FRAME_WINDOW_P (it->f), &it->bidi_it);
3225 /* Synchronize the state of the bidi iterator with
3226 pos->string_pos. For any string position other than
3227 zero, this will be done automagically when we resume
3228 iteration over the string and get_visually_first_element
3229 is called. But if string_pos is zero, and the string is
3230 to be reordered for display, we need to resync manually,
3231 since it could be that the iteration state recorded in
3232 pos ended at string_pos of 0 moving backwards in string. */
3233 if (CHARPOS (pos->string_pos) == 0)
3235 get_visually_first_element (it);
3236 if (IT_STRING_CHARPOS (*it) != 0)
3237 do {
3238 /* Paranoia. */
3239 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3240 bidi_move_to_visually_next (&it->bidi_it);
3241 } while (it->bidi_it.charpos != 0);
3243 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3244 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3248 if (CHARPOS (pos->string_pos) >= 0)
3250 /* Recorded position is not in an overlay string, but in another
3251 string. This can only be a string from a `display' property.
3252 IT should already be filled with that string. */
3253 it->current.string_pos = pos->string_pos;
3254 eassert (STRINGP (it->string));
3255 if (it->bidi_p)
3256 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3257 FRAME_WINDOW_P (it->f), &it->bidi_it);
3260 /* Restore position in display vector translations, control
3261 character translations or ellipses. */
3262 if (pos->dpvec_index >= 0)
3264 if (it->dpvec == NULL)
3265 get_next_display_element (it);
3266 eassert (it->dpvec && it->current.dpvec_index == 0);
3267 it->current.dpvec_index = pos->dpvec_index;
3270 CHECK_IT (it);
3271 return !overlay_strings_with_newlines;
3275 /* Initialize IT for stepping through current_buffer in window W
3276 starting at ROW->start. */
3278 static void
3279 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3281 init_from_display_pos (it, w, &row->start);
3282 it->start = row->start;
3283 it->continuation_lines_width = row->continuation_lines_width;
3284 CHECK_IT (it);
3288 /* Initialize IT for stepping through current_buffer in window W
3289 starting in the line following ROW, i.e. starting at ROW->end.
3290 Value is zero if there are overlay strings with newlines at ROW's
3291 end position. */
3293 static int
3294 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3296 int success = 0;
3298 if (init_from_display_pos (it, w, &row->end))
3300 if (row->continued_p)
3301 it->continuation_lines_width
3302 = row->continuation_lines_width + row->pixel_width;
3303 CHECK_IT (it);
3304 success = 1;
3307 return success;
3313 /***********************************************************************
3314 Text properties
3315 ***********************************************************************/
3317 /* Called when IT reaches IT->stop_charpos. Handle text property and
3318 overlay changes. Set IT->stop_charpos to the next position where
3319 to stop. */
3321 static void
3322 handle_stop (struct it *it)
3324 enum prop_handled handled;
3325 int handle_overlay_change_p;
3326 struct props *p;
3328 it->dpvec = NULL;
3329 it->current.dpvec_index = -1;
3330 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3331 it->ignore_overlay_strings_at_pos_p = 0;
3332 it->ellipsis_p = 0;
3334 /* Use face of preceding text for ellipsis (if invisible) */
3335 if (it->selective_display_ellipsis_p)
3336 it->saved_face_id = it->face_id;
3338 /* Here's the description of the semantics of, and the logic behind,
3339 the various HANDLED_* statuses:
3341 HANDLED_NORMALLY means the handler did its job, and the loop
3342 should proceed to calling the next handler in order.
3344 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3345 change in the properties and overlays at current position, so the
3346 loop should be restarted, to re-invoke the handlers that were
3347 already called. This happens when fontification-functions were
3348 called by handle_fontified_prop, and actually fontified
3349 something. Another case where HANDLED_RECOMPUTE_PROPS is
3350 returned is when we discover overlay strings that need to be
3351 displayed right away. The loop below will continue for as long
3352 as the status is HANDLED_RECOMPUTE_PROPS.
3354 HANDLED_RETURN means return immediately to the caller, to
3355 continue iteration without calling any further handlers. This is
3356 used when we need to act on some property right away, for example
3357 when we need to display the ellipsis or a replacing display
3358 property, such as display string or image.
3360 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3361 consumed, and the handler switched to the next overlay string.
3362 This signals the loop below to refrain from looking for more
3363 overlays before all the overlay strings of the current overlay
3364 are processed.
3366 Some of the handlers called by the loop push the iterator state
3367 onto the stack (see 'push_it'), and arrange for the iteration to
3368 continue with another object, such as an image, a display string,
3369 or an overlay string. In most such cases, it->stop_charpos is
3370 set to the first character of the string, so that when the
3371 iteration resumes, this function will immediately be called
3372 again, to examine the properties at the beginning of the string.
3374 When a display or overlay string is exhausted, the iterator state
3375 is popped (see 'pop_it'), and iteration continues with the
3376 previous object. Again, in many such cases this function is
3377 called again to find the next position where properties might
3378 change. */
3382 handled = HANDLED_NORMALLY;
3384 /* Call text property handlers. */
3385 for (p = it_props; p->handler; ++p)
3387 handled = p->handler (it);
3389 if (handled == HANDLED_RECOMPUTE_PROPS)
3390 break;
3391 else if (handled == HANDLED_RETURN)
3393 /* We still want to show before and after strings from
3394 overlays even if the actual buffer text is replaced. */
3395 if (!handle_overlay_change_p
3396 || it->sp > 1
3397 /* Don't call get_overlay_strings_1 if we already
3398 have overlay strings loaded, because doing so
3399 will load them again and push the iterator state
3400 onto the stack one more time, which is not
3401 expected by the rest of the code that processes
3402 overlay strings. */
3403 || (it->current.overlay_string_index < 0
3404 ? !get_overlay_strings_1 (it, 0, 0)
3405 : 0))
3407 if (it->ellipsis_p)
3408 setup_for_ellipsis (it, 0);
3409 /* When handling a display spec, we might load an
3410 empty string. In that case, discard it here. We
3411 used to discard it in handle_single_display_spec,
3412 but that causes get_overlay_strings_1, above, to
3413 ignore overlay strings that we must check. */
3414 if (STRINGP (it->string) && !SCHARS (it->string))
3415 pop_it (it);
3416 return;
3418 else if (STRINGP (it->string) && !SCHARS (it->string))
3419 pop_it (it);
3420 else
3422 it->ignore_overlay_strings_at_pos_p = true;
3423 it->string_from_display_prop_p = 0;
3424 it->from_disp_prop_p = 0;
3425 handle_overlay_change_p = 0;
3427 handled = HANDLED_RECOMPUTE_PROPS;
3428 break;
3430 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3431 handle_overlay_change_p = 0;
3434 if (handled != HANDLED_RECOMPUTE_PROPS)
3436 /* Don't check for overlay strings below when set to deliver
3437 characters from a display vector. */
3438 if (it->method == GET_FROM_DISPLAY_VECTOR)
3439 handle_overlay_change_p = 0;
3441 /* Handle overlay changes.
3442 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3443 if it finds overlays. */
3444 if (handle_overlay_change_p)
3445 handled = handle_overlay_change (it);
3448 if (it->ellipsis_p)
3450 setup_for_ellipsis (it, 0);
3451 break;
3454 while (handled == HANDLED_RECOMPUTE_PROPS);
3456 /* Determine where to stop next. */
3457 if (handled == HANDLED_NORMALLY)
3458 compute_stop_pos (it);
3462 /* Compute IT->stop_charpos from text property and overlay change
3463 information for IT's current position. */
3465 static void
3466 compute_stop_pos (struct it *it)
3468 register INTERVAL iv, next_iv;
3469 Lisp_Object object, limit, position;
3470 ptrdiff_t charpos, bytepos;
3472 if (STRINGP (it->string))
3474 /* Strings are usually short, so don't limit the search for
3475 properties. */
3476 it->stop_charpos = it->end_charpos;
3477 object = it->string;
3478 limit = Qnil;
3479 charpos = IT_STRING_CHARPOS (*it);
3480 bytepos = IT_STRING_BYTEPOS (*it);
3482 else
3484 ptrdiff_t pos;
3486 /* If end_charpos is out of range for some reason, such as a
3487 misbehaving display function, rationalize it (Bug#5984). */
3488 if (it->end_charpos > ZV)
3489 it->end_charpos = ZV;
3490 it->stop_charpos = it->end_charpos;
3492 /* If next overlay change is in front of the current stop pos
3493 (which is IT->end_charpos), stop there. Note: value of
3494 next_overlay_change is point-max if no overlay change
3495 follows. */
3496 charpos = IT_CHARPOS (*it);
3497 bytepos = IT_BYTEPOS (*it);
3498 pos = next_overlay_change (charpos);
3499 if (pos < it->stop_charpos)
3500 it->stop_charpos = pos;
3502 /* Set up variables for computing the stop position from text
3503 property changes. */
3504 XSETBUFFER (object, current_buffer);
3505 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3508 /* Get the interval containing IT's position. Value is a null
3509 interval if there isn't such an interval. */
3510 position = make_number (charpos);
3511 iv = validate_interval_range (object, &position, &position, 0);
3512 if (iv)
3514 Lisp_Object values_here[LAST_PROP_IDX];
3515 struct props *p;
3517 /* Get properties here. */
3518 for (p = it_props; p->handler; ++p)
3519 values_here[p->idx] = textget (iv->plist,
3520 builtin_lisp_symbol (p->name));
3522 /* Look for an interval following iv that has different
3523 properties. */
3524 for (next_iv = next_interval (iv);
3525 (next_iv
3526 && (NILP (limit)
3527 || XFASTINT (limit) > next_iv->position));
3528 next_iv = next_interval (next_iv))
3530 for (p = it_props; p->handler; ++p)
3532 Lisp_Object new_value = textget (next_iv->plist,
3533 builtin_lisp_symbol (p->name));
3534 if (!EQ (values_here[p->idx], new_value))
3535 break;
3538 if (p->handler)
3539 break;
3542 if (next_iv)
3544 if (INTEGERP (limit)
3545 && next_iv->position >= XFASTINT (limit))
3546 /* No text property change up to limit. */
3547 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3548 else
3549 /* Text properties change in next_iv. */
3550 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3554 if (it->cmp_it.id < 0)
3556 ptrdiff_t stoppos = it->end_charpos;
3558 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3559 stoppos = -1;
3560 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3561 stoppos, it->string);
3564 eassert (STRINGP (it->string)
3565 || (it->stop_charpos >= BEGV
3566 && it->stop_charpos >= IT_CHARPOS (*it)));
3570 /* Return the position of the next overlay change after POS in
3571 current_buffer. Value is point-max if no overlay change
3572 follows. This is like `next-overlay-change' but doesn't use
3573 xmalloc. */
3575 static ptrdiff_t
3576 next_overlay_change (ptrdiff_t pos)
3578 ptrdiff_t i, noverlays;
3579 ptrdiff_t endpos;
3580 Lisp_Object *overlays;
3581 USE_SAFE_ALLOCA;
3583 /* Get all overlays at the given position. */
3584 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3586 /* If any of these overlays ends before endpos,
3587 use its ending point instead. */
3588 for (i = 0; i < noverlays; ++i)
3590 Lisp_Object oend;
3591 ptrdiff_t oendpos;
3593 oend = OVERLAY_END (overlays[i]);
3594 oendpos = OVERLAY_POSITION (oend);
3595 endpos = min (endpos, oendpos);
3598 SAFE_FREE ();
3599 return endpos;
3602 /* How many characters forward to search for a display property or
3603 display string. Searching too far forward makes the bidi display
3604 sluggish, especially in small windows. */
3605 #define MAX_DISP_SCAN 250
3607 /* Return the character position of a display string at or after
3608 position specified by POSITION. If no display string exists at or
3609 after POSITION, return ZV. A display string is either an overlay
3610 with `display' property whose value is a string, or a `display'
3611 text property whose value is a string. STRING is data about the
3612 string to iterate; if STRING->lstring is nil, we are iterating a
3613 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3614 on a GUI frame. DISP_PROP is set to zero if we searched
3615 MAX_DISP_SCAN characters forward without finding any display
3616 strings, non-zero otherwise. It is set to 2 if the display string
3617 uses any kind of `(space ...)' spec that will produce a stretch of
3618 white space in the text area. */
3619 ptrdiff_t
3620 compute_display_string_pos (struct text_pos *position,
3621 struct bidi_string_data *string,
3622 struct window *w,
3623 int frame_window_p, int *disp_prop)
3625 /* OBJECT = nil means current buffer. */
3626 Lisp_Object object, object1;
3627 Lisp_Object pos, spec, limpos;
3628 int string_p = (string && (STRINGP (string->lstring) || string->s));
3629 ptrdiff_t eob = string_p ? string->schars : ZV;
3630 ptrdiff_t begb = string_p ? 0 : BEGV;
3631 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3632 ptrdiff_t lim =
3633 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3634 struct text_pos tpos;
3635 int rv = 0;
3637 if (string && STRINGP (string->lstring))
3638 object1 = object = string->lstring;
3639 else if (w && !string_p)
3641 XSETWINDOW (object, w);
3642 object1 = Qnil;
3644 else
3645 object1 = object = Qnil;
3647 *disp_prop = 1;
3649 if (charpos >= eob
3650 /* We don't support display properties whose values are strings
3651 that have display string properties. */
3652 || string->from_disp_str
3653 /* C strings cannot have display properties. */
3654 || (string->s && !STRINGP (object)))
3656 *disp_prop = 0;
3657 return eob;
3660 /* If the character at CHARPOS is where the display string begins,
3661 return CHARPOS. */
3662 pos = make_number (charpos);
3663 if (STRINGP (object))
3664 bufpos = string->bufpos;
3665 else
3666 bufpos = charpos;
3667 tpos = *position;
3668 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3669 && (charpos <= begb
3670 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3671 object),
3672 spec))
3673 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3674 frame_window_p)))
3676 if (rv == 2)
3677 *disp_prop = 2;
3678 return charpos;
3681 /* Look forward for the first character with a `display' property
3682 that will replace the underlying text when displayed. */
3683 limpos = make_number (lim);
3684 do {
3685 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3686 CHARPOS (tpos) = XFASTINT (pos);
3687 if (CHARPOS (tpos) >= lim)
3689 *disp_prop = 0;
3690 break;
3692 if (STRINGP (object))
3693 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3694 else
3695 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3696 spec = Fget_char_property (pos, Qdisplay, object);
3697 if (!STRINGP (object))
3698 bufpos = CHARPOS (tpos);
3699 } while (NILP (spec)
3700 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3701 bufpos, frame_window_p)));
3702 if (rv == 2)
3703 *disp_prop = 2;
3705 return CHARPOS (tpos);
3708 /* Return the character position of the end of the display string that
3709 started at CHARPOS. If there's no display string at CHARPOS,
3710 return -1. A display string is either an overlay with `display'
3711 property whose value is a string or a `display' text property whose
3712 value is a string. */
3713 ptrdiff_t
3714 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3716 /* OBJECT = nil means current buffer. */
3717 Lisp_Object object =
3718 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3719 Lisp_Object pos = make_number (charpos);
3720 ptrdiff_t eob =
3721 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3723 if (charpos >= eob || (string->s && !STRINGP (object)))
3724 return eob;
3726 /* It could happen that the display property or overlay was removed
3727 since we found it in compute_display_string_pos above. One way
3728 this can happen is if JIT font-lock was called (through
3729 handle_fontified_prop), and jit-lock-functions remove text
3730 properties or overlays from the portion of buffer that includes
3731 CHARPOS. Muse mode is known to do that, for example. In this
3732 case, we return -1 to the caller, to signal that no display
3733 string is actually present at CHARPOS. See bidi_fetch_char for
3734 how this is handled.
3736 An alternative would be to never look for display properties past
3737 it->stop_charpos. But neither compute_display_string_pos nor
3738 bidi_fetch_char that calls it know or care where the next
3739 stop_charpos is. */
3740 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3741 return -1;
3743 /* Look forward for the first character where the `display' property
3744 changes. */
3745 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3747 return XFASTINT (pos);
3752 /***********************************************************************
3753 Fontification
3754 ***********************************************************************/
3756 /* Handle changes in the `fontified' property of the current buffer by
3757 calling hook functions from Qfontification_functions to fontify
3758 regions of text. */
3760 static enum prop_handled
3761 handle_fontified_prop (struct it *it)
3763 Lisp_Object prop, pos;
3764 enum prop_handled handled = HANDLED_NORMALLY;
3766 if (!NILP (Vmemory_full))
3767 return handled;
3769 /* Get the value of the `fontified' property at IT's current buffer
3770 position. (The `fontified' property doesn't have a special
3771 meaning in strings.) If the value is nil, call functions from
3772 Qfontification_functions. */
3773 if (!STRINGP (it->string)
3774 && it->s == NULL
3775 && !NILP (Vfontification_functions)
3776 && !NILP (Vrun_hooks)
3777 && (pos = make_number (IT_CHARPOS (*it)),
3778 prop = Fget_char_property (pos, Qfontified, Qnil),
3779 /* Ignore the special cased nil value always present at EOB since
3780 no amount of fontifying will be able to change it. */
3781 NILP (prop) && IT_CHARPOS (*it) < Z))
3783 ptrdiff_t count = SPECPDL_INDEX ();
3784 Lisp_Object val;
3785 struct buffer *obuf = current_buffer;
3786 ptrdiff_t begv = BEGV, zv = ZV;
3787 bool old_clip_changed = current_buffer->clip_changed;
3789 val = Vfontification_functions;
3790 specbind (Qfontification_functions, Qnil);
3792 eassert (it->end_charpos == ZV);
3794 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3795 safe_call1 (val, pos);
3796 else
3798 Lisp_Object fns, fn;
3799 struct gcpro gcpro1, gcpro2;
3801 fns = Qnil;
3802 GCPRO2 (val, fns);
3804 for (; CONSP (val); val = XCDR (val))
3806 fn = XCAR (val);
3808 if (EQ (fn, Qt))
3810 /* A value of t indicates this hook has a local
3811 binding; it means to run the global binding too.
3812 In a global value, t should not occur. If it
3813 does, we must ignore it to avoid an endless
3814 loop. */
3815 for (fns = Fdefault_value (Qfontification_functions);
3816 CONSP (fns);
3817 fns = XCDR (fns))
3819 fn = XCAR (fns);
3820 if (!EQ (fn, Qt))
3821 safe_call1 (fn, pos);
3824 else
3825 safe_call1 (fn, pos);
3828 UNGCPRO;
3831 unbind_to (count, Qnil);
3833 /* Fontification functions routinely call `save-restriction'.
3834 Normally, this tags clip_changed, which can confuse redisplay
3835 (see discussion in Bug#6671). Since we don't perform any
3836 special handling of fontification changes in the case where
3837 `save-restriction' isn't called, there's no point doing so in
3838 this case either. So, if the buffer's restrictions are
3839 actually left unchanged, reset clip_changed. */
3840 if (obuf == current_buffer)
3842 if (begv == BEGV && zv == ZV)
3843 current_buffer->clip_changed = old_clip_changed;
3845 /* There isn't much we can reasonably do to protect against
3846 misbehaving fontification, but here's a fig leaf. */
3847 else if (BUFFER_LIVE_P (obuf))
3848 set_buffer_internal_1 (obuf);
3850 /* The fontification code may have added/removed text.
3851 It could do even a lot worse, but let's at least protect against
3852 the most obvious case where only the text past `pos' gets changed',
3853 as is/was done in grep.el where some escapes sequences are turned
3854 into face properties (bug#7876). */
3855 it->end_charpos = ZV;
3857 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3858 something. This avoids an endless loop if they failed to
3859 fontify the text for which reason ever. */
3860 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3861 handled = HANDLED_RECOMPUTE_PROPS;
3864 return handled;
3869 /***********************************************************************
3870 Faces
3871 ***********************************************************************/
3873 /* Set up iterator IT from face properties at its current position.
3874 Called from handle_stop. */
3876 static enum prop_handled
3877 handle_face_prop (struct it *it)
3879 int new_face_id;
3880 ptrdiff_t next_stop;
3882 if (!STRINGP (it->string))
3884 new_face_id
3885 = face_at_buffer_position (it->w,
3886 IT_CHARPOS (*it),
3887 &next_stop,
3888 (IT_CHARPOS (*it)
3889 + TEXT_PROP_DISTANCE_LIMIT),
3890 0, it->base_face_id);
3892 /* Is this a start of a run of characters with box face?
3893 Caveat: this can be called for a freshly initialized
3894 iterator; face_id is -1 in this case. We know that the new
3895 face will not change until limit, i.e. if the new face has a
3896 box, all characters up to limit will have one. But, as
3897 usual, we don't know whether limit is really the end. */
3898 if (new_face_id != it->face_id)
3900 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3901 /* If it->face_id is -1, old_face below will be NULL, see
3902 the definition of FACE_FROM_ID. This will happen if this
3903 is the initial call that gets the face. */
3904 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3906 /* If the value of face_id of the iterator is -1, we have to
3907 look in front of IT's position and see whether there is a
3908 face there that's different from new_face_id. */
3909 if (!old_face && IT_CHARPOS (*it) > BEG)
3911 int prev_face_id = face_before_it_pos (it);
3913 old_face = FACE_FROM_ID (it->f, prev_face_id);
3916 /* If the new face has a box, but the old face does not,
3917 this is the start of a run of characters with box face,
3918 i.e. this character has a shadow on the left side. */
3919 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3920 && (old_face == NULL || !old_face->box));
3921 it->face_box_p = new_face->box != FACE_NO_BOX;
3924 else
3926 int base_face_id;
3927 ptrdiff_t bufpos;
3928 int i;
3929 Lisp_Object from_overlay
3930 = (it->current.overlay_string_index >= 0
3931 ? it->string_overlays[it->current.overlay_string_index
3932 % OVERLAY_STRING_CHUNK_SIZE]
3933 : Qnil);
3935 /* See if we got to this string directly or indirectly from
3936 an overlay property. That includes the before-string or
3937 after-string of an overlay, strings in display properties
3938 provided by an overlay, their text properties, etc.
3940 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3941 if (! NILP (from_overlay))
3942 for (i = it->sp - 1; i >= 0; i--)
3944 if (it->stack[i].current.overlay_string_index >= 0)
3945 from_overlay
3946 = it->string_overlays[it->stack[i].current.overlay_string_index
3947 % OVERLAY_STRING_CHUNK_SIZE];
3948 else if (! NILP (it->stack[i].from_overlay))
3949 from_overlay = it->stack[i].from_overlay;
3951 if (!NILP (from_overlay))
3952 break;
3955 if (! NILP (from_overlay))
3957 bufpos = IT_CHARPOS (*it);
3958 /* For a string from an overlay, the base face depends
3959 only on text properties and ignores overlays. */
3960 base_face_id
3961 = face_for_overlay_string (it->w,
3962 IT_CHARPOS (*it),
3963 &next_stop,
3964 (IT_CHARPOS (*it)
3965 + TEXT_PROP_DISTANCE_LIMIT),
3967 from_overlay);
3969 else
3971 bufpos = 0;
3973 /* For strings from a `display' property, use the face at
3974 IT's current buffer position as the base face to merge
3975 with, so that overlay strings appear in the same face as
3976 surrounding text, unless they specify their own faces.
3977 For strings from wrap-prefix and line-prefix properties,
3978 use the default face, possibly remapped via
3979 Vface_remapping_alist. */
3980 /* Note that the fact that we use the face at _buffer_
3981 position means that a 'display' property on an overlay
3982 string will not inherit the face of that overlay string,
3983 but will instead revert to the face of buffer text
3984 covered by the overlay. This is visible, e.g., when the
3985 overlay specifies a box face, but neither the buffer nor
3986 the display string do. This sounds like a design bug,
3987 but Emacs always did that since v21.1, so changing that
3988 might be a big deal. */
3989 base_face_id = it->string_from_prefix_prop_p
3990 ? (!NILP (Vface_remapping_alist)
3991 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
3992 : DEFAULT_FACE_ID)
3993 : underlying_face_id (it);
3996 new_face_id = face_at_string_position (it->w,
3997 it->string,
3998 IT_STRING_CHARPOS (*it),
3999 bufpos,
4000 &next_stop,
4001 base_face_id, 0);
4003 /* Is this a start of a run of characters with box? Caveat:
4004 this can be called for a freshly allocated iterator; face_id
4005 is -1 is this case. We know that the new face will not
4006 change until the next check pos, i.e. if the new face has a
4007 box, all characters up to that position will have a
4008 box. But, as usual, we don't know whether that position
4009 is really the end. */
4010 if (new_face_id != it->face_id)
4012 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4013 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4015 /* If new face has a box but old face hasn't, this is the
4016 start of a run of characters with box, i.e. it has a
4017 shadow on the left side. */
4018 it->start_of_box_run_p
4019 = new_face->box && (old_face == NULL || !old_face->box);
4020 it->face_box_p = new_face->box != FACE_NO_BOX;
4024 it->face_id = new_face_id;
4025 return HANDLED_NORMALLY;
4029 /* Return the ID of the face ``underlying'' IT's current position,
4030 which is in a string. If the iterator is associated with a
4031 buffer, return the face at IT's current buffer position.
4032 Otherwise, use the iterator's base_face_id. */
4034 static int
4035 underlying_face_id (struct it *it)
4037 int face_id = it->base_face_id, i;
4039 eassert (STRINGP (it->string));
4041 for (i = it->sp - 1; i >= 0; --i)
4042 if (NILP (it->stack[i].string))
4043 face_id = it->stack[i].face_id;
4045 return face_id;
4049 /* Compute the face one character before or after the current position
4050 of IT, in the visual order. BEFORE_P non-zero means get the face
4051 in front (to the left in L2R paragraphs, to the right in R2L
4052 paragraphs) of IT's screen position. Value is the ID of the face. */
4054 static int
4055 face_before_or_after_it_pos (struct it *it, int before_p)
4057 int face_id, limit;
4058 ptrdiff_t next_check_charpos;
4059 struct it it_copy;
4060 void *it_copy_data = NULL;
4062 eassert (it->s == NULL);
4064 if (STRINGP (it->string))
4066 ptrdiff_t bufpos, charpos;
4067 int base_face_id;
4069 /* No face change past the end of the string (for the case
4070 we are padding with spaces). No face change before the
4071 string start. */
4072 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4073 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4074 return it->face_id;
4076 if (!it->bidi_p)
4078 /* Set charpos to the position before or after IT's current
4079 position, in the logical order, which in the non-bidi
4080 case is the same as the visual order. */
4081 if (before_p)
4082 charpos = IT_STRING_CHARPOS (*it) - 1;
4083 else if (it->what == IT_COMPOSITION)
4084 /* For composition, we must check the character after the
4085 composition. */
4086 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4087 else
4088 charpos = IT_STRING_CHARPOS (*it) + 1;
4090 else
4092 if (before_p)
4094 /* With bidi iteration, the character before the current
4095 in the visual order cannot be found by simple
4096 iteration, because "reverse" reordering is not
4097 supported. Instead, we need to use the move_it_*
4098 family of functions. */
4099 /* Ignore face changes before the first visible
4100 character on this display line. */
4101 if (it->current_x <= it->first_visible_x)
4102 return it->face_id;
4103 SAVE_IT (it_copy, *it, it_copy_data);
4104 /* Implementation note: Since move_it_in_display_line
4105 works in the iterator geometry, and thinks the first
4106 character is always the leftmost, even in R2L lines,
4107 we don't need to distinguish between the R2L and L2R
4108 cases here. */
4109 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4110 it_copy.current_x - 1, MOVE_TO_X);
4111 charpos = IT_STRING_CHARPOS (it_copy);
4112 RESTORE_IT (it, it, it_copy_data);
4114 else
4116 /* Set charpos to the string position of the character
4117 that comes after IT's current position in the visual
4118 order. */
4119 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4121 it_copy = *it;
4122 while (n--)
4123 bidi_move_to_visually_next (&it_copy.bidi_it);
4125 charpos = it_copy.bidi_it.charpos;
4128 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4130 if (it->current.overlay_string_index >= 0)
4131 bufpos = IT_CHARPOS (*it);
4132 else
4133 bufpos = 0;
4135 base_face_id = underlying_face_id (it);
4137 /* Get the face for ASCII, or unibyte. */
4138 face_id = face_at_string_position (it->w,
4139 it->string,
4140 charpos,
4141 bufpos,
4142 &next_check_charpos,
4143 base_face_id, 0);
4145 /* Correct the face for charsets different from ASCII. Do it
4146 for the multibyte case only. The face returned above is
4147 suitable for unibyte text if IT->string is unibyte. */
4148 if (STRING_MULTIBYTE (it->string))
4150 struct text_pos pos1 = string_pos (charpos, it->string);
4151 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4152 int c, len;
4153 struct face *face = FACE_FROM_ID (it->f, face_id);
4155 c = string_char_and_length (p, &len);
4156 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4159 else
4161 struct text_pos pos;
4163 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4164 || (IT_CHARPOS (*it) <= BEGV && before_p))
4165 return it->face_id;
4167 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4168 pos = it->current.pos;
4170 if (!it->bidi_p)
4172 if (before_p)
4173 DEC_TEXT_POS (pos, it->multibyte_p);
4174 else
4176 if (it->what == IT_COMPOSITION)
4178 /* For composition, we must check the position after
4179 the composition. */
4180 pos.charpos += it->cmp_it.nchars;
4181 pos.bytepos += it->len;
4183 else
4184 INC_TEXT_POS (pos, it->multibyte_p);
4187 else
4189 if (before_p)
4191 /* With bidi iteration, the character before the current
4192 in the visual order cannot be found by simple
4193 iteration, because "reverse" reordering is not
4194 supported. Instead, we need to use the move_it_*
4195 family of functions. */
4196 /* Ignore face changes before the first visible
4197 character on this display line. */
4198 if (it->current_x <= it->first_visible_x)
4199 return it->face_id;
4200 SAVE_IT (it_copy, *it, it_copy_data);
4201 /* Implementation note: Since move_it_in_display_line
4202 works in the iterator geometry, and thinks the first
4203 character is always the leftmost, even in R2L lines,
4204 we don't need to distinguish between the R2L and L2R
4205 cases here. */
4206 move_it_in_display_line (&it_copy, ZV,
4207 it_copy.current_x - 1, MOVE_TO_X);
4208 pos = it_copy.current.pos;
4209 RESTORE_IT (it, it, it_copy_data);
4211 else
4213 /* Set charpos to the buffer position of the character
4214 that comes after IT's current position in the visual
4215 order. */
4216 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4218 it_copy = *it;
4219 while (n--)
4220 bidi_move_to_visually_next (&it_copy.bidi_it);
4222 SET_TEXT_POS (pos,
4223 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4226 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4228 /* Determine face for CHARSET_ASCII, or unibyte. */
4229 face_id = face_at_buffer_position (it->w,
4230 CHARPOS (pos),
4231 &next_check_charpos,
4232 limit, 0, -1);
4234 /* Correct the face for charsets different from ASCII. Do it
4235 for the multibyte case only. The face returned above is
4236 suitable for unibyte text if current_buffer is unibyte. */
4237 if (it->multibyte_p)
4239 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4240 struct face *face = FACE_FROM_ID (it->f, face_id);
4241 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4245 return face_id;
4250 /***********************************************************************
4251 Invisible text
4252 ***********************************************************************/
4254 /* Set up iterator IT from invisible properties at its current
4255 position. Called from handle_stop. */
4257 static enum prop_handled
4258 handle_invisible_prop (struct it *it)
4260 enum prop_handled handled = HANDLED_NORMALLY;
4261 int invis_p;
4262 Lisp_Object prop;
4264 if (STRINGP (it->string))
4266 Lisp_Object end_charpos, limit, charpos;
4268 /* Get the value of the invisible text property at the
4269 current position. Value will be nil if there is no such
4270 property. */
4271 charpos = make_number (IT_STRING_CHARPOS (*it));
4272 prop = Fget_text_property (charpos, Qinvisible, it->string);
4273 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4275 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4277 /* Record whether we have to display an ellipsis for the
4278 invisible text. */
4279 int display_ellipsis_p = (invis_p == 2);
4280 ptrdiff_t len, endpos;
4282 handled = HANDLED_RECOMPUTE_PROPS;
4284 /* Get the position at which the next visible text can be
4285 found in IT->string, if any. */
4286 endpos = len = SCHARS (it->string);
4287 XSETINT (limit, len);
4290 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4291 it->string, limit);
4292 if (INTEGERP (end_charpos))
4294 endpos = XFASTINT (end_charpos);
4295 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4296 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4297 if (invis_p == 2)
4298 display_ellipsis_p = true;
4301 while (invis_p && endpos < len);
4303 if (display_ellipsis_p)
4304 it->ellipsis_p = true;
4306 if (endpos < len)
4308 /* Text at END_CHARPOS is visible. Move IT there. */
4309 struct text_pos old;
4310 ptrdiff_t oldpos;
4312 old = it->current.string_pos;
4313 oldpos = CHARPOS (old);
4314 if (it->bidi_p)
4316 if (it->bidi_it.first_elt
4317 && it->bidi_it.charpos < SCHARS (it->string))
4318 bidi_paragraph_init (it->paragraph_embedding,
4319 &it->bidi_it, 1);
4320 /* Bidi-iterate out of the invisible text. */
4323 bidi_move_to_visually_next (&it->bidi_it);
4325 while (oldpos <= it->bidi_it.charpos
4326 && it->bidi_it.charpos < endpos);
4328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4330 if (IT_CHARPOS (*it) >= endpos)
4331 it->prev_stop = endpos;
4333 else
4335 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4336 compute_string_pos (&it->current.string_pos, old, it->string);
4339 else
4341 /* The rest of the string is invisible. If this is an
4342 overlay string, proceed with the next overlay string
4343 or whatever comes and return a character from there. */
4344 if (it->current.overlay_string_index >= 0
4345 && !display_ellipsis_p)
4347 next_overlay_string (it);
4348 /* Don't check for overlay strings when we just
4349 finished processing them. */
4350 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4352 else
4354 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4355 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4360 else
4362 ptrdiff_t newpos, next_stop, start_charpos, tem;
4363 Lisp_Object pos, overlay;
4365 /* First of all, is there invisible text at this position? */
4366 tem = start_charpos = IT_CHARPOS (*it);
4367 pos = make_number (tem);
4368 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4369 &overlay);
4370 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4372 /* If we are on invisible text, skip over it. */
4373 if (invis_p && start_charpos < it->end_charpos)
4375 /* Record whether we have to display an ellipsis for the
4376 invisible text. */
4377 int display_ellipsis_p = invis_p == 2;
4379 handled = HANDLED_RECOMPUTE_PROPS;
4381 /* Loop skipping over invisible text. The loop is left at
4382 ZV or with IT on the first char being visible again. */
4385 /* Try to skip some invisible text. Return value is the
4386 position reached which can be equal to where we start
4387 if there is nothing invisible there. This skips both
4388 over invisible text properties and overlays with
4389 invisible property. */
4390 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4392 /* If we skipped nothing at all we weren't at invisible
4393 text in the first place. If everything to the end of
4394 the buffer was skipped, end the loop. */
4395 if (newpos == tem || newpos >= ZV)
4396 invis_p = 0;
4397 else
4399 /* We skipped some characters but not necessarily
4400 all there are. Check if we ended up on visible
4401 text. Fget_char_property returns the property of
4402 the char before the given position, i.e. if we
4403 get invis_p = 0, this means that the char at
4404 newpos is visible. */
4405 pos = make_number (newpos);
4406 prop = Fget_char_property (pos, Qinvisible, it->window);
4407 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4410 /* If we ended up on invisible text, proceed to
4411 skip starting with next_stop. */
4412 if (invis_p)
4413 tem = next_stop;
4415 /* If there are adjacent invisible texts, don't lose the
4416 second one's ellipsis. */
4417 if (invis_p == 2)
4418 display_ellipsis_p = true;
4420 while (invis_p);
4422 /* The position newpos is now either ZV or on visible text. */
4423 if (it->bidi_p)
4425 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4426 int on_newline
4427 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4428 int after_newline
4429 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4431 /* If the invisible text ends on a newline or on a
4432 character after a newline, we can avoid the costly,
4433 character by character, bidi iteration to NEWPOS, and
4434 instead simply reseat the iterator there. That's
4435 because all bidi reordering information is tossed at
4436 the newline. This is a big win for modes that hide
4437 complete lines, like Outline, Org, etc. */
4438 if (on_newline || after_newline)
4440 struct text_pos tpos;
4441 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4443 SET_TEXT_POS (tpos, newpos, bpos);
4444 reseat_1 (it, tpos, 0);
4445 /* If we reseat on a newline/ZV, we need to prep the
4446 bidi iterator for advancing to the next character
4447 after the newline/EOB, keeping the current paragraph
4448 direction (so that PRODUCE_GLYPHS does TRT wrt
4449 prepending/appending glyphs to a glyph row). */
4450 if (on_newline)
4452 it->bidi_it.first_elt = 0;
4453 it->bidi_it.paragraph_dir = pdir;
4454 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4455 it->bidi_it.nchars = 1;
4456 it->bidi_it.ch_len = 1;
4459 else /* Must use the slow method. */
4461 /* With bidi iteration, the region of invisible text
4462 could start and/or end in the middle of a
4463 non-base embedding level. Therefore, we need to
4464 skip invisible text using the bidi iterator,
4465 starting at IT's current position, until we find
4466 ourselves outside of the invisible text.
4467 Skipping invisible text _after_ bidi iteration
4468 avoids affecting the visual order of the
4469 displayed text when invisible properties are
4470 added or removed. */
4471 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4473 /* If we were `reseat'ed to a new paragraph,
4474 determine the paragraph base direction. We
4475 need to do it now because
4476 next_element_from_buffer may not have a
4477 chance to do it, if we are going to skip any
4478 text at the beginning, which resets the
4479 FIRST_ELT flag. */
4480 bidi_paragraph_init (it->paragraph_embedding,
4481 &it->bidi_it, 1);
4485 bidi_move_to_visually_next (&it->bidi_it);
4487 while (it->stop_charpos <= it->bidi_it.charpos
4488 && it->bidi_it.charpos < newpos);
4489 IT_CHARPOS (*it) = it->bidi_it.charpos;
4490 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4491 /* If we overstepped NEWPOS, record its position in
4492 the iterator, so that we skip invisible text if
4493 later the bidi iteration lands us in the
4494 invisible region again. */
4495 if (IT_CHARPOS (*it) >= newpos)
4496 it->prev_stop = newpos;
4499 else
4501 IT_CHARPOS (*it) = newpos;
4502 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4505 /* If there are before-strings at the start of invisible
4506 text, and the text is invisible because of a text
4507 property, arrange to show before-strings because 20.x did
4508 it that way. (If the text is invisible because of an
4509 overlay property instead of a text property, this is
4510 already handled in the overlay code.) */
4511 if (NILP (overlay)
4512 && get_overlay_strings (it, it->stop_charpos))
4514 handled = HANDLED_RECOMPUTE_PROPS;
4515 if (it->sp > 0)
4517 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4518 /* The call to get_overlay_strings above recomputes
4519 it->stop_charpos, but it only considers changes
4520 in properties and overlays beyond iterator's
4521 current position. This causes us to miss changes
4522 that happen exactly where the invisible property
4523 ended. So we play it safe here and force the
4524 iterator to check for potential stop positions
4525 immediately after the invisible text. Note that
4526 if get_overlay_strings returns non-zero, it
4527 normally also pushed the iterator stack, so we
4528 need to update the stop position in the slot
4529 below the current one. */
4530 it->stack[it->sp - 1].stop_charpos
4531 = CHARPOS (it->stack[it->sp - 1].current.pos);
4534 else if (display_ellipsis_p)
4536 /* Make sure that the glyphs of the ellipsis will get
4537 correct `charpos' values. If we would not update
4538 it->position here, the glyphs would belong to the
4539 last visible character _before_ the invisible
4540 text, which confuses `set_cursor_from_row'.
4542 We use the last invisible position instead of the
4543 first because this way the cursor is always drawn on
4544 the first "." of the ellipsis, whenever PT is inside
4545 the invisible text. Otherwise the cursor would be
4546 placed _after_ the ellipsis when the point is after the
4547 first invisible character. */
4548 if (!STRINGP (it->object))
4550 it->position.charpos = newpos - 1;
4551 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4553 it->ellipsis_p = true;
4554 /* Let the ellipsis display before
4555 considering any properties of the following char.
4556 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4557 handled = HANDLED_RETURN;
4562 return handled;
4566 /* Make iterator IT return `...' next.
4567 Replaces LEN characters from buffer. */
4569 static void
4570 setup_for_ellipsis (struct it *it, int len)
4572 /* Use the display table definition for `...'. Invalid glyphs
4573 will be handled by the method returning elements from dpvec. */
4574 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4576 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4577 it->dpvec = v->contents;
4578 it->dpend = v->contents + v->header.size;
4580 else
4582 /* Default `...'. */
4583 it->dpvec = default_invis_vector;
4584 it->dpend = default_invis_vector + 3;
4587 it->dpvec_char_len = len;
4588 it->current.dpvec_index = 0;
4589 it->dpvec_face_id = -1;
4591 /* Remember the current face id in case glyphs specify faces.
4592 IT's face is restored in set_iterator_to_next.
4593 saved_face_id was set to preceding char's face in handle_stop. */
4594 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4595 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4597 it->method = GET_FROM_DISPLAY_VECTOR;
4598 it->ellipsis_p = true;
4603 /***********************************************************************
4604 'display' property
4605 ***********************************************************************/
4607 /* Set up iterator IT from `display' property at its current position.
4608 Called from handle_stop.
4609 We return HANDLED_RETURN if some part of the display property
4610 overrides the display of the buffer text itself.
4611 Otherwise we return HANDLED_NORMALLY. */
4613 static enum prop_handled
4614 handle_display_prop (struct it *it)
4616 Lisp_Object propval, object, overlay;
4617 struct text_pos *position;
4618 ptrdiff_t bufpos;
4619 /* Nonzero if some property replaces the display of the text itself. */
4620 int display_replaced_p = 0;
4622 if (STRINGP (it->string))
4624 object = it->string;
4625 position = &it->current.string_pos;
4626 bufpos = CHARPOS (it->current.pos);
4628 else
4630 XSETWINDOW (object, it->w);
4631 position = &it->current.pos;
4632 bufpos = CHARPOS (*position);
4635 /* Reset those iterator values set from display property values. */
4636 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4637 it->space_width = Qnil;
4638 it->font_height = Qnil;
4639 it->voffset = 0;
4641 /* We don't support recursive `display' properties, i.e. string
4642 values that have a string `display' property, that have a string
4643 `display' property etc. */
4644 if (!it->string_from_display_prop_p)
4645 it->area = TEXT_AREA;
4647 propval = get_char_property_and_overlay (make_number (position->charpos),
4648 Qdisplay, object, &overlay);
4649 if (NILP (propval))
4650 return HANDLED_NORMALLY;
4651 /* Now OVERLAY is the overlay that gave us this property, or nil
4652 if it was a text property. */
4654 if (!STRINGP (it->string))
4655 object = it->w->contents;
4657 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4658 position, bufpos,
4659 FRAME_WINDOW_P (it->f));
4661 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4664 /* Subroutine of handle_display_prop. Returns non-zero if the display
4665 specification in SPEC is a replacing specification, i.e. it would
4666 replace the text covered by `display' property with something else,
4667 such as an image or a display string. If SPEC includes any kind or
4668 `(space ...) specification, the value is 2; this is used by
4669 compute_display_string_pos, which see.
4671 See handle_single_display_spec for documentation of arguments.
4672 frame_window_p is non-zero if the window being redisplayed is on a
4673 GUI frame; this argument is used only if IT is NULL, see below.
4675 IT can be NULL, if this is called by the bidi reordering code
4676 through compute_display_string_pos, which see. In that case, this
4677 function only examines SPEC, but does not otherwise "handle" it, in
4678 the sense that it doesn't set up members of IT from the display
4679 spec. */
4680 static int
4681 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4682 Lisp_Object overlay, struct text_pos *position,
4683 ptrdiff_t bufpos, int frame_window_p)
4685 int replacing_p = 0;
4686 int rv;
4688 if (CONSP (spec)
4689 /* Simple specifications. */
4690 && !EQ (XCAR (spec), Qimage)
4691 && !EQ (XCAR (spec), Qspace)
4692 && !EQ (XCAR (spec), Qwhen)
4693 && !EQ (XCAR (spec), Qslice)
4694 && !EQ (XCAR (spec), Qspace_width)
4695 && !EQ (XCAR (spec), Qheight)
4696 && !EQ (XCAR (spec), Qraise)
4697 /* Marginal area specifications. */
4698 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4699 && !EQ (XCAR (spec), Qleft_fringe)
4700 && !EQ (XCAR (spec), Qright_fringe)
4701 && !NILP (XCAR (spec)))
4703 for (; CONSP (spec); spec = XCDR (spec))
4705 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4706 overlay, position, bufpos,
4707 replacing_p, frame_window_p)))
4709 replacing_p = rv;
4710 /* If some text in a string is replaced, `position' no
4711 longer points to the position of `object'. */
4712 if (!it || STRINGP (object))
4713 break;
4717 else if (VECTORP (spec))
4719 ptrdiff_t i;
4720 for (i = 0; i < ASIZE (spec); ++i)
4721 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4722 overlay, position, bufpos,
4723 replacing_p, frame_window_p)))
4725 replacing_p = rv;
4726 /* If some text in a string is replaced, `position' no
4727 longer points to the position of `object'. */
4728 if (!it || STRINGP (object))
4729 break;
4732 else
4734 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4735 position, bufpos, 0,
4736 frame_window_p)))
4737 replacing_p = rv;
4740 return replacing_p;
4743 /* Value is the position of the end of the `display' property starting
4744 at START_POS in OBJECT. */
4746 static struct text_pos
4747 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4749 Lisp_Object end;
4750 struct text_pos end_pos;
4752 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4753 Qdisplay, object, Qnil);
4754 CHARPOS (end_pos) = XFASTINT (end);
4755 if (STRINGP (object))
4756 compute_string_pos (&end_pos, start_pos, it->string);
4757 else
4758 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4760 return end_pos;
4764 /* Set up IT from a single `display' property specification SPEC. OBJECT
4765 is the object in which the `display' property was found. *POSITION
4766 is the position in OBJECT at which the `display' property was found.
4767 BUFPOS is the buffer position of OBJECT (different from POSITION if
4768 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4769 previously saw a display specification which already replaced text
4770 display with something else, for example an image; we ignore such
4771 properties after the first one has been processed.
4773 OVERLAY is the overlay this `display' property came from,
4774 or nil if it was a text property.
4776 If SPEC is a `space' or `image' specification, and in some other
4777 cases too, set *POSITION to the position where the `display'
4778 property ends.
4780 If IT is NULL, only examine the property specification in SPEC, but
4781 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4782 is intended to be displayed in a window on a GUI frame.
4784 Value is non-zero if something was found which replaces the display
4785 of buffer or string text. */
4787 static int
4788 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4789 Lisp_Object overlay, struct text_pos *position,
4790 ptrdiff_t bufpos, int display_replaced_p,
4791 int frame_window_p)
4793 Lisp_Object form;
4794 Lisp_Object location, value;
4795 struct text_pos start_pos = *position;
4796 int valid_p;
4798 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4799 If the result is non-nil, use VALUE instead of SPEC. */
4800 form = Qt;
4801 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4803 spec = XCDR (spec);
4804 if (!CONSP (spec))
4805 return 0;
4806 form = XCAR (spec);
4807 spec = XCDR (spec);
4810 if (!NILP (form) && !EQ (form, Qt))
4812 ptrdiff_t count = SPECPDL_INDEX ();
4813 struct gcpro gcpro1;
4815 /* Bind `object' to the object having the `display' property, a
4816 buffer or string. Bind `position' to the position in the
4817 object where the property was found, and `buffer-position'
4818 to the current position in the buffer. */
4820 if (NILP (object))
4821 XSETBUFFER (object, current_buffer);
4822 specbind (Qobject, object);
4823 specbind (Qposition, make_number (CHARPOS (*position)));
4824 specbind (Qbuffer_position, make_number (bufpos));
4825 GCPRO1 (form);
4826 form = safe_eval (form);
4827 UNGCPRO;
4828 unbind_to (count, Qnil);
4831 if (NILP (form))
4832 return 0;
4834 /* Handle `(height HEIGHT)' specifications. */
4835 if (CONSP (spec)
4836 && EQ (XCAR (spec), Qheight)
4837 && CONSP (XCDR (spec)))
4839 if (it)
4841 if (!FRAME_WINDOW_P (it->f))
4842 return 0;
4844 it->font_height = XCAR (XCDR (spec));
4845 if (!NILP (it->font_height))
4847 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4848 int new_height = -1;
4850 if (CONSP (it->font_height)
4851 && (EQ (XCAR (it->font_height), Qplus)
4852 || EQ (XCAR (it->font_height), Qminus))
4853 && CONSP (XCDR (it->font_height))
4854 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4856 /* `(+ N)' or `(- N)' where N is an integer. */
4857 int steps = XINT (XCAR (XCDR (it->font_height)));
4858 if (EQ (XCAR (it->font_height), Qplus))
4859 steps = - steps;
4860 it->face_id = smaller_face (it->f, it->face_id, steps);
4862 else if (FUNCTIONP (it->font_height))
4864 /* Call function with current height as argument.
4865 Value is the new height. */
4866 Lisp_Object height;
4867 height = safe_call1 (it->font_height,
4868 face->lface[LFACE_HEIGHT_INDEX]);
4869 if (NUMBERP (height))
4870 new_height = XFLOATINT (height);
4872 else if (NUMBERP (it->font_height))
4874 /* Value is a multiple of the canonical char height. */
4875 struct face *f;
4877 f = FACE_FROM_ID (it->f,
4878 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4879 new_height = (XFLOATINT (it->font_height)
4880 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4882 else
4884 /* Evaluate IT->font_height with `height' bound to the
4885 current specified height to get the new height. */
4886 ptrdiff_t count = SPECPDL_INDEX ();
4888 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4889 value = safe_eval (it->font_height);
4890 unbind_to (count, Qnil);
4892 if (NUMBERP (value))
4893 new_height = XFLOATINT (value);
4896 if (new_height > 0)
4897 it->face_id = face_with_height (it->f, it->face_id, new_height);
4901 return 0;
4904 /* Handle `(space-width WIDTH)'. */
4905 if (CONSP (spec)
4906 && EQ (XCAR (spec), Qspace_width)
4907 && CONSP (XCDR (spec)))
4909 if (it)
4911 if (!FRAME_WINDOW_P (it->f))
4912 return 0;
4914 value = XCAR (XCDR (spec));
4915 if (NUMBERP (value) && XFLOATINT (value) > 0)
4916 it->space_width = value;
4919 return 0;
4922 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4923 if (CONSP (spec)
4924 && EQ (XCAR (spec), Qslice))
4926 Lisp_Object tem;
4928 if (it)
4930 if (!FRAME_WINDOW_P (it->f))
4931 return 0;
4933 if (tem = XCDR (spec), CONSP (tem))
4935 it->slice.x = XCAR (tem);
4936 if (tem = XCDR (tem), CONSP (tem))
4938 it->slice.y = XCAR (tem);
4939 if (tem = XCDR (tem), CONSP (tem))
4941 it->slice.width = XCAR (tem);
4942 if (tem = XCDR (tem), CONSP (tem))
4943 it->slice.height = XCAR (tem);
4949 return 0;
4952 /* Handle `(raise FACTOR)'. */
4953 if (CONSP (spec)
4954 && EQ (XCAR (spec), Qraise)
4955 && CONSP (XCDR (spec)))
4957 if (it)
4959 if (!FRAME_WINDOW_P (it->f))
4960 return 0;
4962 #ifdef HAVE_WINDOW_SYSTEM
4963 value = XCAR (XCDR (spec));
4964 if (NUMBERP (value))
4966 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4967 it->voffset = - (XFLOATINT (value)
4968 * (FONT_HEIGHT (face->font)));
4970 #endif /* HAVE_WINDOW_SYSTEM */
4973 return 0;
4976 /* Don't handle the other kinds of display specifications
4977 inside a string that we got from a `display' property. */
4978 if (it && it->string_from_display_prop_p)
4979 return 0;
4981 /* Characters having this form of property are not displayed, so
4982 we have to find the end of the property. */
4983 if (it)
4985 start_pos = *position;
4986 *position = display_prop_end (it, object, start_pos);
4988 value = Qnil;
4990 /* Stop the scan at that end position--we assume that all
4991 text properties change there. */
4992 if (it)
4993 it->stop_charpos = position->charpos;
4995 /* Handle `(left-fringe BITMAP [FACE])'
4996 and `(right-fringe BITMAP [FACE])'. */
4997 if (CONSP (spec)
4998 && (EQ (XCAR (spec), Qleft_fringe)
4999 || EQ (XCAR (spec), Qright_fringe))
5000 && CONSP (XCDR (spec)))
5002 int fringe_bitmap;
5004 if (it)
5006 if (!FRAME_WINDOW_P (it->f))
5007 /* If we return here, POSITION has been advanced
5008 across the text with this property. */
5010 /* Synchronize the bidi iterator with POSITION. This is
5011 needed because we are not going to push the iterator
5012 on behalf of this display property, so there will be
5013 no pop_it call to do this synchronization for us. */
5014 if (it->bidi_p)
5016 it->position = *position;
5017 iterate_out_of_display_property (it);
5018 *position = it->position;
5020 /* If we were to display this fringe bitmap,
5021 next_element_from_image would have reset this flag.
5022 Do the same, to avoid affecting overlays that
5023 follow. */
5024 it->ignore_overlay_strings_at_pos_p = 0;
5025 return 1;
5028 else if (!frame_window_p)
5029 return 1;
5031 #ifdef HAVE_WINDOW_SYSTEM
5032 value = XCAR (XCDR (spec));
5033 if (!SYMBOLP (value)
5034 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5035 /* If we return here, POSITION has been advanced
5036 across the text with this property. */
5038 if (it && it->bidi_p)
5040 it->position = *position;
5041 iterate_out_of_display_property (it);
5042 *position = it->position;
5044 if (it)
5045 /* Reset this flag like next_element_from_image would. */
5046 it->ignore_overlay_strings_at_pos_p = 0;
5047 return 1;
5050 if (it)
5052 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5054 if (CONSP (XCDR (XCDR (spec))))
5056 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5057 int face_id2 = lookup_derived_face (it->f, face_name,
5058 FRINGE_FACE_ID, 0);
5059 if (face_id2 >= 0)
5060 face_id = face_id2;
5063 /* Save current settings of IT so that we can restore them
5064 when we are finished with the glyph property value. */
5065 push_it (it, position);
5067 it->area = TEXT_AREA;
5068 it->what = IT_IMAGE;
5069 it->image_id = -1; /* no image */
5070 it->position = start_pos;
5071 it->object = NILP (object) ? it->w->contents : object;
5072 it->method = GET_FROM_IMAGE;
5073 it->from_overlay = Qnil;
5074 it->face_id = face_id;
5075 it->from_disp_prop_p = true;
5077 /* Say that we haven't consumed the characters with
5078 `display' property yet. The call to pop_it in
5079 set_iterator_to_next will clean this up. */
5080 *position = start_pos;
5082 if (EQ (XCAR (spec), Qleft_fringe))
5084 it->left_user_fringe_bitmap = fringe_bitmap;
5085 it->left_user_fringe_face_id = face_id;
5087 else
5089 it->right_user_fringe_bitmap = fringe_bitmap;
5090 it->right_user_fringe_face_id = face_id;
5093 #endif /* HAVE_WINDOW_SYSTEM */
5094 return 1;
5097 /* Prepare to handle `((margin left-margin) ...)',
5098 `((margin right-margin) ...)' and `((margin nil) ...)'
5099 prefixes for display specifications. */
5100 location = Qunbound;
5101 if (CONSP (spec) && CONSP (XCAR (spec)))
5103 Lisp_Object tem;
5105 value = XCDR (spec);
5106 if (CONSP (value))
5107 value = XCAR (value);
5109 tem = XCAR (spec);
5110 if (EQ (XCAR (tem), Qmargin)
5111 && (tem = XCDR (tem),
5112 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5113 (NILP (tem)
5114 || EQ (tem, Qleft_margin)
5115 || EQ (tem, Qright_margin))))
5116 location = tem;
5119 if (EQ (location, Qunbound))
5121 location = Qnil;
5122 value = spec;
5125 /* After this point, VALUE is the property after any
5126 margin prefix has been stripped. It must be a string,
5127 an image specification, or `(space ...)'.
5129 LOCATION specifies where to display: `left-margin',
5130 `right-margin' or nil. */
5132 valid_p = (STRINGP (value)
5133 #ifdef HAVE_WINDOW_SYSTEM
5134 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5135 && valid_image_p (value))
5136 #endif /* not HAVE_WINDOW_SYSTEM */
5137 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5139 if (valid_p && !display_replaced_p)
5141 int retval = 1;
5143 if (!it)
5145 /* Callers need to know whether the display spec is any kind
5146 of `(space ...)' spec that is about to affect text-area
5147 display. */
5148 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5149 retval = 2;
5150 return retval;
5153 /* Save current settings of IT so that we can restore them
5154 when we are finished with the glyph property value. */
5155 push_it (it, position);
5156 it->from_overlay = overlay;
5157 it->from_disp_prop_p = true;
5159 if (NILP (location))
5160 it->area = TEXT_AREA;
5161 else if (EQ (location, Qleft_margin))
5162 it->area = LEFT_MARGIN_AREA;
5163 else
5164 it->area = RIGHT_MARGIN_AREA;
5166 if (STRINGP (value))
5168 it->string = value;
5169 it->multibyte_p = STRING_MULTIBYTE (it->string);
5170 it->current.overlay_string_index = -1;
5171 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5172 it->end_charpos = it->string_nchars = SCHARS (it->string);
5173 it->method = GET_FROM_STRING;
5174 it->stop_charpos = 0;
5175 it->prev_stop = 0;
5176 it->base_level_stop = 0;
5177 it->string_from_display_prop_p = true;
5178 /* Say that we haven't consumed the characters with
5179 `display' property yet. The call to pop_it in
5180 set_iterator_to_next will clean this up. */
5181 if (BUFFERP (object))
5182 *position = start_pos;
5184 /* Force paragraph direction to be that of the parent
5185 object. If the parent object's paragraph direction is
5186 not yet determined, default to L2R. */
5187 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5188 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5189 else
5190 it->paragraph_embedding = L2R;
5192 /* Set up the bidi iterator for this display string. */
5193 if (it->bidi_p)
5195 it->bidi_it.string.lstring = it->string;
5196 it->bidi_it.string.s = NULL;
5197 it->bidi_it.string.schars = it->end_charpos;
5198 it->bidi_it.string.bufpos = bufpos;
5199 it->bidi_it.string.from_disp_str = 1;
5200 it->bidi_it.string.unibyte = !it->multibyte_p;
5201 it->bidi_it.w = it->w;
5202 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5205 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5207 it->method = GET_FROM_STRETCH;
5208 it->object = value;
5209 *position = it->position = start_pos;
5210 retval = 1 + (it->area == TEXT_AREA);
5212 #ifdef HAVE_WINDOW_SYSTEM
5213 else
5215 it->what = IT_IMAGE;
5216 it->image_id = lookup_image (it->f, value);
5217 it->position = start_pos;
5218 it->object = NILP (object) ? it->w->contents : object;
5219 it->method = GET_FROM_IMAGE;
5221 /* Say that we haven't consumed the characters with
5222 `display' property yet. The call to pop_it in
5223 set_iterator_to_next will clean this up. */
5224 *position = start_pos;
5226 #endif /* HAVE_WINDOW_SYSTEM */
5228 return retval;
5231 /* Invalid property or property not supported. Restore
5232 POSITION to what it was before. */
5233 *position = start_pos;
5234 return 0;
5237 /* Check if PROP is a display property value whose text should be
5238 treated as intangible. OVERLAY is the overlay from which PROP
5239 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5240 specify the buffer position covered by PROP. */
5243 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5244 ptrdiff_t charpos, ptrdiff_t bytepos)
5246 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5247 struct text_pos position;
5249 SET_TEXT_POS (position, charpos, bytepos);
5250 return handle_display_spec (NULL, prop, Qnil, overlay,
5251 &position, charpos, frame_window_p);
5255 /* Return 1 if PROP is a display sub-property value containing STRING.
5257 Implementation note: this and the following function are really
5258 special cases of handle_display_spec and
5259 handle_single_display_spec, and should ideally use the same code.
5260 Until they do, these two pairs must be consistent and must be
5261 modified in sync. */
5263 static int
5264 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5266 if (EQ (string, prop))
5267 return 1;
5269 /* Skip over `when FORM'. */
5270 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5272 prop = XCDR (prop);
5273 if (!CONSP (prop))
5274 return 0;
5275 /* Actually, the condition following `when' should be eval'ed,
5276 like handle_single_display_spec does, and we should return
5277 zero if it evaluates to nil. However, this function is
5278 called only when the buffer was already displayed and some
5279 glyph in the glyph matrix was found to come from a display
5280 string. Therefore, the condition was already evaluated, and
5281 the result was non-nil, otherwise the display string wouldn't
5282 have been displayed and we would have never been called for
5283 this property. Thus, we can skip the evaluation and assume
5284 its result is non-nil. */
5285 prop = XCDR (prop);
5288 if (CONSP (prop))
5289 /* Skip over `margin LOCATION'. */
5290 if (EQ (XCAR (prop), Qmargin))
5292 prop = XCDR (prop);
5293 if (!CONSP (prop))
5294 return 0;
5296 prop = XCDR (prop);
5297 if (!CONSP (prop))
5298 return 0;
5301 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5305 /* Return 1 if STRING appears in the `display' property PROP. */
5307 static int
5308 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5310 if (CONSP (prop)
5311 && !EQ (XCAR (prop), Qwhen)
5312 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5314 /* A list of sub-properties. */
5315 while (CONSP (prop))
5317 if (single_display_spec_string_p (XCAR (prop), string))
5318 return 1;
5319 prop = XCDR (prop);
5322 else if (VECTORP (prop))
5324 /* A vector of sub-properties. */
5325 ptrdiff_t i;
5326 for (i = 0; i < ASIZE (prop); ++i)
5327 if (single_display_spec_string_p (AREF (prop, i), string))
5328 return 1;
5330 else
5331 return single_display_spec_string_p (prop, string);
5333 return 0;
5336 /* Look for STRING in overlays and text properties in the current
5337 buffer, between character positions FROM and TO (excluding TO).
5338 BACK_P non-zero means look back (in this case, TO is supposed to be
5339 less than FROM).
5340 Value is the first character position where STRING was found, or
5341 zero if it wasn't found before hitting TO.
5343 This function may only use code that doesn't eval because it is
5344 called asynchronously from note_mouse_highlight. */
5346 static ptrdiff_t
5347 string_buffer_position_lim (Lisp_Object string,
5348 ptrdiff_t from, ptrdiff_t to, int back_p)
5350 Lisp_Object limit, prop, pos;
5351 int found = 0;
5353 pos = make_number (max (from, BEGV));
5355 if (!back_p) /* looking forward */
5357 limit = make_number (min (to, ZV));
5358 while (!found && !EQ (pos, limit))
5360 prop = Fget_char_property (pos, Qdisplay, Qnil);
5361 if (!NILP (prop) && display_prop_string_p (prop, string))
5362 found = 1;
5363 else
5364 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5365 limit);
5368 else /* looking back */
5370 limit = make_number (max (to, BEGV));
5371 while (!found && !EQ (pos, limit))
5373 prop = Fget_char_property (pos, Qdisplay, Qnil);
5374 if (!NILP (prop) && display_prop_string_p (prop, string))
5375 found = 1;
5376 else
5377 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5378 limit);
5382 return found ? XINT (pos) : 0;
5385 /* Determine which buffer position in current buffer STRING comes from.
5386 AROUND_CHARPOS is an approximate position where it could come from.
5387 Value is the buffer position or 0 if it couldn't be determined.
5389 This function is necessary because we don't record buffer positions
5390 in glyphs generated from strings (to keep struct glyph small).
5391 This function may only use code that doesn't eval because it is
5392 called asynchronously from note_mouse_highlight. */
5394 static ptrdiff_t
5395 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5397 const int MAX_DISTANCE = 1000;
5398 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5399 around_charpos + MAX_DISTANCE,
5402 if (!found)
5403 found = string_buffer_position_lim (string, around_charpos,
5404 around_charpos - MAX_DISTANCE, 1);
5405 return found;
5410 /***********************************************************************
5411 `composition' property
5412 ***********************************************************************/
5414 /* Set up iterator IT from `composition' property at its current
5415 position. Called from handle_stop. */
5417 static enum prop_handled
5418 handle_composition_prop (struct it *it)
5420 Lisp_Object prop, string;
5421 ptrdiff_t pos, pos_byte, start, end;
5423 if (STRINGP (it->string))
5425 unsigned char *s;
5427 pos = IT_STRING_CHARPOS (*it);
5428 pos_byte = IT_STRING_BYTEPOS (*it);
5429 string = it->string;
5430 s = SDATA (string) + pos_byte;
5431 it->c = STRING_CHAR (s);
5433 else
5435 pos = IT_CHARPOS (*it);
5436 pos_byte = IT_BYTEPOS (*it);
5437 string = Qnil;
5438 it->c = FETCH_CHAR (pos_byte);
5441 /* If there's a valid composition and point is not inside of the
5442 composition (in the case that the composition is from the current
5443 buffer), draw a glyph composed from the composition components. */
5444 if (find_composition (pos, -1, &start, &end, &prop, string)
5445 && composition_valid_p (start, end, prop)
5446 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5448 if (start < pos)
5449 /* As we can't handle this situation (perhaps font-lock added
5450 a new composition), we just return here hoping that next
5451 redisplay will detect this composition much earlier. */
5452 return HANDLED_NORMALLY;
5453 if (start != pos)
5455 if (STRINGP (it->string))
5456 pos_byte = string_char_to_byte (it->string, start);
5457 else
5458 pos_byte = CHAR_TO_BYTE (start);
5460 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5461 prop, string);
5463 if (it->cmp_it.id >= 0)
5465 it->cmp_it.ch = -1;
5466 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5467 it->cmp_it.nglyphs = -1;
5471 return HANDLED_NORMALLY;
5476 /***********************************************************************
5477 Overlay strings
5478 ***********************************************************************/
5480 /* The following structure is used to record overlay strings for
5481 later sorting in load_overlay_strings. */
5483 struct overlay_entry
5485 Lisp_Object overlay;
5486 Lisp_Object string;
5487 EMACS_INT priority;
5488 int after_string_p;
5492 /* Set up iterator IT from overlay strings at its current position.
5493 Called from handle_stop. */
5495 static enum prop_handled
5496 handle_overlay_change (struct it *it)
5498 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5499 return HANDLED_RECOMPUTE_PROPS;
5500 else
5501 return HANDLED_NORMALLY;
5505 /* Set up the next overlay string for delivery by IT, if there is an
5506 overlay string to deliver. Called by set_iterator_to_next when the
5507 end of the current overlay string is reached. If there are more
5508 overlay strings to display, IT->string and
5509 IT->current.overlay_string_index are set appropriately here.
5510 Otherwise IT->string is set to nil. */
5512 static void
5513 next_overlay_string (struct it *it)
5515 ++it->current.overlay_string_index;
5516 if (it->current.overlay_string_index == it->n_overlay_strings)
5518 /* No more overlay strings. Restore IT's settings to what
5519 they were before overlay strings were processed, and
5520 continue to deliver from current_buffer. */
5522 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5523 pop_it (it);
5524 eassert (it->sp > 0
5525 || (NILP (it->string)
5526 && it->method == GET_FROM_BUFFER
5527 && it->stop_charpos >= BEGV
5528 && it->stop_charpos <= it->end_charpos));
5529 it->current.overlay_string_index = -1;
5530 it->n_overlay_strings = 0;
5531 it->overlay_strings_charpos = -1;
5532 /* If there's an empty display string on the stack, pop the
5533 stack, to resync the bidi iterator with IT's position. Such
5534 empty strings are pushed onto the stack in
5535 get_overlay_strings_1. */
5536 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5537 pop_it (it);
5539 /* If we're at the end of the buffer, record that we have
5540 processed the overlay strings there already, so that
5541 next_element_from_buffer doesn't try it again. */
5542 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5543 it->overlay_strings_at_end_processed_p = true;
5545 else
5547 /* There are more overlay strings to process. If
5548 IT->current.overlay_string_index has advanced to a position
5549 where we must load IT->overlay_strings with more strings, do
5550 it. We must load at the IT->overlay_strings_charpos where
5551 IT->n_overlay_strings was originally computed; when invisible
5552 text is present, this might not be IT_CHARPOS (Bug#7016). */
5553 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5555 if (it->current.overlay_string_index && i == 0)
5556 load_overlay_strings (it, it->overlay_strings_charpos);
5558 /* Initialize IT to deliver display elements from the overlay
5559 string. */
5560 it->string = it->overlay_strings[i];
5561 it->multibyte_p = STRING_MULTIBYTE (it->string);
5562 SET_TEXT_POS (it->current.string_pos, 0, 0);
5563 it->method = GET_FROM_STRING;
5564 it->stop_charpos = 0;
5565 it->end_charpos = SCHARS (it->string);
5566 if (it->cmp_it.stop_pos >= 0)
5567 it->cmp_it.stop_pos = 0;
5568 it->prev_stop = 0;
5569 it->base_level_stop = 0;
5571 /* Set up the bidi iterator for this overlay string. */
5572 if (it->bidi_p)
5574 it->bidi_it.string.lstring = it->string;
5575 it->bidi_it.string.s = NULL;
5576 it->bidi_it.string.schars = SCHARS (it->string);
5577 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5578 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5579 it->bidi_it.string.unibyte = !it->multibyte_p;
5580 it->bidi_it.w = it->w;
5581 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5585 CHECK_IT (it);
5589 /* Compare two overlay_entry structures E1 and E2. Used as a
5590 comparison function for qsort in load_overlay_strings. Overlay
5591 strings for the same position are sorted so that
5593 1. All after-strings come in front of before-strings, except
5594 when they come from the same overlay.
5596 2. Within after-strings, strings are sorted so that overlay strings
5597 from overlays with higher priorities come first.
5599 2. Within before-strings, strings are sorted so that overlay
5600 strings from overlays with higher priorities come last.
5602 Value is analogous to strcmp. */
5605 static int
5606 compare_overlay_entries (const void *e1, const void *e2)
5608 struct overlay_entry const *entry1 = e1;
5609 struct overlay_entry const *entry2 = e2;
5610 int result;
5612 if (entry1->after_string_p != entry2->after_string_p)
5614 /* Let after-strings appear in front of before-strings if
5615 they come from different overlays. */
5616 if (EQ (entry1->overlay, entry2->overlay))
5617 result = entry1->after_string_p ? 1 : -1;
5618 else
5619 result = entry1->after_string_p ? -1 : 1;
5621 else if (entry1->priority != entry2->priority)
5623 if (entry1->after_string_p)
5624 /* After-strings sorted in order of decreasing priority. */
5625 result = entry2->priority < entry1->priority ? -1 : 1;
5626 else
5627 /* Before-strings sorted in order of increasing priority. */
5628 result = entry1->priority < entry2->priority ? -1 : 1;
5630 else
5631 result = 0;
5633 return result;
5637 /* Load the vector IT->overlay_strings with overlay strings from IT's
5638 current buffer position, or from CHARPOS if that is > 0. Set
5639 IT->n_overlays to the total number of overlay strings found.
5641 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5642 a time. On entry into load_overlay_strings,
5643 IT->current.overlay_string_index gives the number of overlay
5644 strings that have already been loaded by previous calls to this
5645 function.
5647 IT->add_overlay_start contains an additional overlay start
5648 position to consider for taking overlay strings from, if non-zero.
5649 This position comes into play when the overlay has an `invisible'
5650 property, and both before and after-strings. When we've skipped to
5651 the end of the overlay, because of its `invisible' property, we
5652 nevertheless want its before-string to appear.
5653 IT->add_overlay_start will contain the overlay start position
5654 in this case.
5656 Overlay strings are sorted so that after-string strings come in
5657 front of before-string strings. Within before and after-strings,
5658 strings are sorted by overlay priority. See also function
5659 compare_overlay_entries. */
5661 static void
5662 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5664 Lisp_Object overlay, window, str, invisible;
5665 struct Lisp_Overlay *ov;
5666 ptrdiff_t start, end;
5667 ptrdiff_t n = 0, i, j;
5668 int invis_p;
5669 struct overlay_entry entriesbuf[20];
5670 ptrdiff_t size = ARRAYELTS (entriesbuf);
5671 struct overlay_entry *entries = entriesbuf;
5672 USE_SAFE_ALLOCA;
5674 if (charpos <= 0)
5675 charpos = IT_CHARPOS (*it);
5677 /* Append the overlay string STRING of overlay OVERLAY to vector
5678 `entries' which has size `size' and currently contains `n'
5679 elements. AFTER_P non-zero means STRING is an after-string of
5680 OVERLAY. */
5681 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5682 do \
5684 Lisp_Object priority; \
5686 if (n == size) \
5688 struct overlay_entry *old = entries; \
5689 SAFE_NALLOCA (entries, 2, size); \
5690 memcpy (entries, old, size * sizeof *entries); \
5691 size *= 2; \
5694 entries[n].string = (STRING); \
5695 entries[n].overlay = (OVERLAY); \
5696 priority = Foverlay_get ((OVERLAY), Qpriority); \
5697 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5698 entries[n].after_string_p = (AFTER_P); \
5699 ++n; \
5701 while (0)
5703 /* Process overlay before the overlay center. */
5704 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5706 XSETMISC (overlay, ov);
5707 eassert (OVERLAYP (overlay));
5708 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5709 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5711 if (end < charpos)
5712 break;
5714 /* Skip this overlay if it doesn't start or end at IT's current
5715 position. */
5716 if (end != charpos && start != charpos)
5717 continue;
5719 /* Skip this overlay if it doesn't apply to IT->w. */
5720 window = Foverlay_get (overlay, Qwindow);
5721 if (WINDOWP (window) && XWINDOW (window) != it->w)
5722 continue;
5724 /* If the text ``under'' the overlay is invisible, both before-
5725 and after-strings from this overlay are visible; start and
5726 end position are indistinguishable. */
5727 invisible = Foverlay_get (overlay, Qinvisible);
5728 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5730 /* If overlay has a non-empty before-string, record it. */
5731 if ((start == charpos || (end == charpos && invis_p))
5732 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5733 && SCHARS (str))
5734 RECORD_OVERLAY_STRING (overlay, str, 0);
5736 /* If overlay has a non-empty after-string, record it. */
5737 if ((end == charpos || (start == charpos && invis_p))
5738 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5739 && SCHARS (str))
5740 RECORD_OVERLAY_STRING (overlay, str, 1);
5743 /* Process overlays after the overlay center. */
5744 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5746 XSETMISC (overlay, ov);
5747 eassert (OVERLAYP (overlay));
5748 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5749 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5751 if (start > charpos)
5752 break;
5754 /* Skip this overlay if it doesn't start or end at IT's current
5755 position. */
5756 if (end != charpos && start != charpos)
5757 continue;
5759 /* Skip this overlay if it doesn't apply to IT->w. */
5760 window = Foverlay_get (overlay, Qwindow);
5761 if (WINDOWP (window) && XWINDOW (window) != it->w)
5762 continue;
5764 /* If the text ``under'' the overlay is invisible, it has a zero
5765 dimension, and both before- and after-strings apply. */
5766 invisible = Foverlay_get (overlay, Qinvisible);
5767 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5769 /* If overlay has a non-empty before-string, record it. */
5770 if ((start == charpos || (end == charpos && invis_p))
5771 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5772 && SCHARS (str))
5773 RECORD_OVERLAY_STRING (overlay, str, 0);
5775 /* If overlay has a non-empty after-string, record it. */
5776 if ((end == charpos || (start == charpos && invis_p))
5777 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5778 && SCHARS (str))
5779 RECORD_OVERLAY_STRING (overlay, str, 1);
5782 #undef RECORD_OVERLAY_STRING
5784 /* Sort entries. */
5785 if (n > 1)
5786 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5788 /* Record number of overlay strings, and where we computed it. */
5789 it->n_overlay_strings = n;
5790 it->overlay_strings_charpos = charpos;
5792 /* IT->current.overlay_string_index is the number of overlay strings
5793 that have already been consumed by IT. Copy some of the
5794 remaining overlay strings to IT->overlay_strings. */
5795 i = 0;
5796 j = it->current.overlay_string_index;
5797 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5799 it->overlay_strings[i] = entries[j].string;
5800 it->string_overlays[i++] = entries[j++].overlay;
5803 CHECK_IT (it);
5804 SAFE_FREE ();
5808 /* Get the first chunk of overlay strings at IT's current buffer
5809 position, or at CHARPOS if that is > 0. Value is non-zero if at
5810 least one overlay string was found. */
5812 static int
5813 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5815 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5816 process. This fills IT->overlay_strings with strings, and sets
5817 IT->n_overlay_strings to the total number of strings to process.
5818 IT->pos.overlay_string_index has to be set temporarily to zero
5819 because load_overlay_strings needs this; it must be set to -1
5820 when no overlay strings are found because a zero value would
5821 indicate a position in the first overlay string. */
5822 it->current.overlay_string_index = 0;
5823 load_overlay_strings (it, charpos);
5825 /* If we found overlay strings, set up IT to deliver display
5826 elements from the first one. Otherwise set up IT to deliver
5827 from current_buffer. */
5828 if (it->n_overlay_strings)
5830 /* Make sure we know settings in current_buffer, so that we can
5831 restore meaningful values when we're done with the overlay
5832 strings. */
5833 if (compute_stop_p)
5834 compute_stop_pos (it);
5835 eassert (it->face_id >= 0);
5837 /* Save IT's settings. They are restored after all overlay
5838 strings have been processed. */
5839 eassert (!compute_stop_p || it->sp == 0);
5841 /* When called from handle_stop, there might be an empty display
5842 string loaded. In that case, don't bother saving it. But
5843 don't use this optimization with the bidi iterator, since we
5844 need the corresponding pop_it call to resync the bidi
5845 iterator's position with IT's position, after we are done
5846 with the overlay strings. (The corresponding call to pop_it
5847 in case of an empty display string is in
5848 next_overlay_string.) */
5849 if (!(!it->bidi_p
5850 && STRINGP (it->string) && !SCHARS (it->string)))
5851 push_it (it, NULL);
5853 /* Set up IT to deliver display elements from the first overlay
5854 string. */
5855 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5856 it->string = it->overlay_strings[0];
5857 it->from_overlay = Qnil;
5858 it->stop_charpos = 0;
5859 eassert (STRINGP (it->string));
5860 it->end_charpos = SCHARS (it->string);
5861 it->prev_stop = 0;
5862 it->base_level_stop = 0;
5863 it->multibyte_p = STRING_MULTIBYTE (it->string);
5864 it->method = GET_FROM_STRING;
5865 it->from_disp_prop_p = 0;
5867 /* Force paragraph direction to be that of the parent
5868 buffer. */
5869 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5870 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5871 else
5872 it->paragraph_embedding = L2R;
5874 /* Set up the bidi iterator for this overlay string. */
5875 if (it->bidi_p)
5877 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5879 it->bidi_it.string.lstring = it->string;
5880 it->bidi_it.string.s = NULL;
5881 it->bidi_it.string.schars = SCHARS (it->string);
5882 it->bidi_it.string.bufpos = pos;
5883 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5884 it->bidi_it.string.unibyte = !it->multibyte_p;
5885 it->bidi_it.w = it->w;
5886 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5888 return 1;
5891 it->current.overlay_string_index = -1;
5892 return 0;
5895 static int
5896 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5898 it->string = Qnil;
5899 it->method = GET_FROM_BUFFER;
5901 (void) get_overlay_strings_1 (it, charpos, 1);
5903 CHECK_IT (it);
5905 /* Value is non-zero if we found at least one overlay string. */
5906 return STRINGP (it->string);
5911 /***********************************************************************
5912 Saving and restoring state
5913 ***********************************************************************/
5915 /* Save current settings of IT on IT->stack. Called, for example,
5916 before setting up IT for an overlay string, to be able to restore
5917 IT's settings to what they were after the overlay string has been
5918 processed. If POSITION is non-NULL, it is the position to save on
5919 the stack instead of IT->position. */
5921 static void
5922 push_it (struct it *it, struct text_pos *position)
5924 struct iterator_stack_entry *p;
5926 eassert (it->sp < IT_STACK_SIZE);
5927 p = it->stack + it->sp;
5929 p->stop_charpos = it->stop_charpos;
5930 p->prev_stop = it->prev_stop;
5931 p->base_level_stop = it->base_level_stop;
5932 p->cmp_it = it->cmp_it;
5933 eassert (it->face_id >= 0);
5934 p->face_id = it->face_id;
5935 p->string = it->string;
5936 p->method = it->method;
5937 p->from_overlay = it->from_overlay;
5938 switch (p->method)
5940 case GET_FROM_IMAGE:
5941 p->u.image.object = it->object;
5942 p->u.image.image_id = it->image_id;
5943 p->u.image.slice = it->slice;
5944 break;
5945 case GET_FROM_STRETCH:
5946 p->u.stretch.object = it->object;
5947 break;
5949 p->position = position ? *position : it->position;
5950 p->current = it->current;
5951 p->end_charpos = it->end_charpos;
5952 p->string_nchars = it->string_nchars;
5953 p->area = it->area;
5954 p->multibyte_p = it->multibyte_p;
5955 p->avoid_cursor_p = it->avoid_cursor_p;
5956 p->space_width = it->space_width;
5957 p->font_height = it->font_height;
5958 p->voffset = it->voffset;
5959 p->string_from_display_prop_p = it->string_from_display_prop_p;
5960 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5961 p->display_ellipsis_p = 0;
5962 p->line_wrap = it->line_wrap;
5963 p->bidi_p = it->bidi_p;
5964 p->paragraph_embedding = it->paragraph_embedding;
5965 p->from_disp_prop_p = it->from_disp_prop_p;
5966 ++it->sp;
5968 /* Save the state of the bidi iterator as well. */
5969 if (it->bidi_p)
5970 bidi_push_it (&it->bidi_it);
5973 static void
5974 iterate_out_of_display_property (struct it *it)
5976 int buffer_p = !STRINGP (it->string);
5977 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5978 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5980 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5982 /* Maybe initialize paragraph direction. If we are at the beginning
5983 of a new paragraph, next_element_from_buffer may not have a
5984 chance to do that. */
5985 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5986 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5987 /* prev_stop can be zero, so check against BEGV as well. */
5988 while (it->bidi_it.charpos >= bob
5989 && it->prev_stop <= it->bidi_it.charpos
5990 && it->bidi_it.charpos < CHARPOS (it->position)
5991 && it->bidi_it.charpos < eob)
5992 bidi_move_to_visually_next (&it->bidi_it);
5993 /* Record the stop_pos we just crossed, for when we cross it
5994 back, maybe. */
5995 if (it->bidi_it.charpos > CHARPOS (it->position))
5996 it->prev_stop = CHARPOS (it->position);
5997 /* If we ended up not where pop_it put us, resync IT's
5998 positional members with the bidi iterator. */
5999 if (it->bidi_it.charpos != CHARPOS (it->position))
6000 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6001 if (buffer_p)
6002 it->current.pos = it->position;
6003 else
6004 it->current.string_pos = it->position;
6007 /* Restore IT's settings from IT->stack. Called, for example, when no
6008 more overlay strings must be processed, and we return to delivering
6009 display elements from a buffer, or when the end of a string from a
6010 `display' property is reached and we return to delivering display
6011 elements from an overlay string, or from a buffer. */
6013 static void
6014 pop_it (struct it *it)
6016 struct iterator_stack_entry *p;
6017 int from_display_prop = it->from_disp_prop_p;
6019 eassert (it->sp > 0);
6020 --it->sp;
6021 p = it->stack + it->sp;
6022 it->stop_charpos = p->stop_charpos;
6023 it->prev_stop = p->prev_stop;
6024 it->base_level_stop = p->base_level_stop;
6025 it->cmp_it = p->cmp_it;
6026 it->face_id = p->face_id;
6027 it->current = p->current;
6028 it->position = p->position;
6029 it->string = p->string;
6030 it->from_overlay = p->from_overlay;
6031 if (NILP (it->string))
6032 SET_TEXT_POS (it->current.string_pos, -1, -1);
6033 it->method = p->method;
6034 switch (it->method)
6036 case GET_FROM_IMAGE:
6037 it->image_id = p->u.image.image_id;
6038 it->object = p->u.image.object;
6039 it->slice = p->u.image.slice;
6040 break;
6041 case GET_FROM_STRETCH:
6042 it->object = p->u.stretch.object;
6043 break;
6044 case GET_FROM_BUFFER:
6045 it->object = it->w->contents;
6046 break;
6047 case GET_FROM_STRING:
6049 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6051 /* Restore the face_box_p flag, since it could have been
6052 overwritten by the face of the object that we just finished
6053 displaying. */
6054 if (face)
6055 it->face_box_p = face->box != FACE_NO_BOX;
6056 it->object = it->string;
6058 break;
6059 case GET_FROM_DISPLAY_VECTOR:
6060 if (it->s)
6061 it->method = GET_FROM_C_STRING;
6062 else if (STRINGP (it->string))
6063 it->method = GET_FROM_STRING;
6064 else
6066 it->method = GET_FROM_BUFFER;
6067 it->object = it->w->contents;
6070 it->end_charpos = p->end_charpos;
6071 it->string_nchars = p->string_nchars;
6072 it->area = p->area;
6073 it->multibyte_p = p->multibyte_p;
6074 it->avoid_cursor_p = p->avoid_cursor_p;
6075 it->space_width = p->space_width;
6076 it->font_height = p->font_height;
6077 it->voffset = p->voffset;
6078 it->string_from_display_prop_p = p->string_from_display_prop_p;
6079 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6080 it->line_wrap = p->line_wrap;
6081 it->bidi_p = p->bidi_p;
6082 it->paragraph_embedding = p->paragraph_embedding;
6083 it->from_disp_prop_p = p->from_disp_prop_p;
6084 if (it->bidi_p)
6086 bidi_pop_it (&it->bidi_it);
6087 /* Bidi-iterate until we get out of the portion of text, if any,
6088 covered by a `display' text property or by an overlay with
6089 `display' property. (We cannot just jump there, because the
6090 internal coherency of the bidi iterator state can not be
6091 preserved across such jumps.) We also must determine the
6092 paragraph base direction if the overlay we just processed is
6093 at the beginning of a new paragraph. */
6094 if (from_display_prop
6095 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6096 iterate_out_of_display_property (it);
6098 eassert ((BUFFERP (it->object)
6099 && IT_CHARPOS (*it) == it->bidi_it.charpos
6100 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6101 || (STRINGP (it->object)
6102 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6103 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6104 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6110 /***********************************************************************
6111 Moving over lines
6112 ***********************************************************************/
6114 /* Set IT's current position to the previous line start. */
6116 static void
6117 back_to_previous_line_start (struct it *it)
6119 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6121 DEC_BOTH (cp, bp);
6122 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6126 /* Move IT to the next line start.
6128 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6129 we skipped over part of the text (as opposed to moving the iterator
6130 continuously over the text). Otherwise, don't change the value
6131 of *SKIPPED_P.
6133 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6134 iterator on the newline, if it was found.
6136 Newlines may come from buffer text, overlay strings, or strings
6137 displayed via the `display' property. That's the reason we can't
6138 simply use find_newline_no_quit.
6140 Note that this function may not skip over invisible text that is so
6141 because of text properties and immediately follows a newline. If
6142 it would, function reseat_at_next_visible_line_start, when called
6143 from set_iterator_to_next, would effectively make invisible
6144 characters following a newline part of the wrong glyph row, which
6145 leads to wrong cursor motion. */
6147 static int
6148 forward_to_next_line_start (struct it *it, int *skipped_p,
6149 struct bidi_it *bidi_it_prev)
6151 ptrdiff_t old_selective;
6152 int newline_found_p, n;
6153 const int MAX_NEWLINE_DISTANCE = 500;
6155 /* If already on a newline, just consume it to avoid unintended
6156 skipping over invisible text below. */
6157 if (it->what == IT_CHARACTER
6158 && it->c == '\n'
6159 && CHARPOS (it->position) == IT_CHARPOS (*it))
6161 if (it->bidi_p && bidi_it_prev)
6162 *bidi_it_prev = it->bidi_it;
6163 set_iterator_to_next (it, 0);
6164 it->c = 0;
6165 return 1;
6168 /* Don't handle selective display in the following. It's (a)
6169 unnecessary because it's done by the caller, and (b) leads to an
6170 infinite recursion because next_element_from_ellipsis indirectly
6171 calls this function. */
6172 old_selective = it->selective;
6173 it->selective = 0;
6175 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6176 from buffer text. */
6177 for (n = newline_found_p = 0;
6178 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6179 n += STRINGP (it->string) ? 0 : 1)
6181 if (!get_next_display_element (it))
6182 return 0;
6183 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6184 if (newline_found_p && it->bidi_p && bidi_it_prev)
6185 *bidi_it_prev = it->bidi_it;
6186 set_iterator_to_next (it, 0);
6189 /* If we didn't find a newline near enough, see if we can use a
6190 short-cut. */
6191 if (!newline_found_p)
6193 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6194 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6195 1, &bytepos);
6196 Lisp_Object pos;
6198 eassert (!STRINGP (it->string));
6200 /* If there isn't any `display' property in sight, and no
6201 overlays, we can just use the position of the newline in
6202 buffer text. */
6203 if (it->stop_charpos >= limit
6204 || ((pos = Fnext_single_property_change (make_number (start),
6205 Qdisplay, Qnil,
6206 make_number (limit)),
6207 NILP (pos))
6208 && next_overlay_change (start) == ZV))
6210 if (!it->bidi_p)
6212 IT_CHARPOS (*it) = limit;
6213 IT_BYTEPOS (*it) = bytepos;
6215 else
6217 struct bidi_it bprev;
6219 /* Help bidi.c avoid expensive searches for display
6220 properties and overlays, by telling it that there are
6221 none up to `limit'. */
6222 if (it->bidi_it.disp_pos < limit)
6224 it->bidi_it.disp_pos = limit;
6225 it->bidi_it.disp_prop = 0;
6227 do {
6228 bprev = it->bidi_it;
6229 bidi_move_to_visually_next (&it->bidi_it);
6230 } while (it->bidi_it.charpos != limit);
6231 IT_CHARPOS (*it) = limit;
6232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6233 if (bidi_it_prev)
6234 *bidi_it_prev = bprev;
6236 *skipped_p = newline_found_p = true;
6238 else
6240 while (get_next_display_element (it)
6241 && !newline_found_p)
6243 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6244 if (newline_found_p && it->bidi_p && bidi_it_prev)
6245 *bidi_it_prev = it->bidi_it;
6246 set_iterator_to_next (it, 0);
6251 it->selective = old_selective;
6252 return newline_found_p;
6256 /* Set IT's current position to the previous visible line start. Skip
6257 invisible text that is so either due to text properties or due to
6258 selective display. Caution: this does not change IT->current_x and
6259 IT->hpos. */
6261 static void
6262 back_to_previous_visible_line_start (struct it *it)
6264 while (IT_CHARPOS (*it) > BEGV)
6266 back_to_previous_line_start (it);
6268 if (IT_CHARPOS (*it) <= BEGV)
6269 break;
6271 /* If selective > 0, then lines indented more than its value are
6272 invisible. */
6273 if (it->selective > 0
6274 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6275 it->selective))
6276 continue;
6278 /* Check the newline before point for invisibility. */
6280 Lisp_Object prop;
6281 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6282 Qinvisible, it->window);
6283 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6284 continue;
6287 if (IT_CHARPOS (*it) <= BEGV)
6288 break;
6291 struct it it2;
6292 void *it2data = NULL;
6293 ptrdiff_t pos;
6294 ptrdiff_t beg, end;
6295 Lisp_Object val, overlay;
6297 SAVE_IT (it2, *it, it2data);
6299 /* If newline is part of a composition, continue from start of composition */
6300 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6301 && beg < IT_CHARPOS (*it))
6302 goto replaced;
6304 /* If newline is replaced by a display property, find start of overlay
6305 or interval and continue search from that point. */
6306 pos = --IT_CHARPOS (it2);
6307 --IT_BYTEPOS (it2);
6308 it2.sp = 0;
6309 bidi_unshelve_cache (NULL, 0);
6310 it2.string_from_display_prop_p = 0;
6311 it2.from_disp_prop_p = 0;
6312 if (handle_display_prop (&it2) == HANDLED_RETURN
6313 && !NILP (val = get_char_property_and_overlay
6314 (make_number (pos), Qdisplay, Qnil, &overlay))
6315 && (OVERLAYP (overlay)
6316 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6317 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6319 RESTORE_IT (it, it, it2data);
6320 goto replaced;
6323 /* Newline is not replaced by anything -- so we are done. */
6324 RESTORE_IT (it, it, it2data);
6325 break;
6327 replaced:
6328 if (beg < BEGV)
6329 beg = BEGV;
6330 IT_CHARPOS (*it) = beg;
6331 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6335 it->continuation_lines_width = 0;
6337 eassert (IT_CHARPOS (*it) >= BEGV);
6338 eassert (IT_CHARPOS (*it) == BEGV
6339 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6340 CHECK_IT (it);
6344 /* Reseat iterator IT at the previous visible line start. Skip
6345 invisible text that is so either due to text properties or due to
6346 selective display. At the end, update IT's overlay information,
6347 face information etc. */
6349 void
6350 reseat_at_previous_visible_line_start (struct it *it)
6352 back_to_previous_visible_line_start (it);
6353 reseat (it, it->current.pos, 1);
6354 CHECK_IT (it);
6358 /* Reseat iterator IT on the next visible line start in the current
6359 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6360 preceding the line start. Skip over invisible text that is so
6361 because of selective display. Compute faces, overlays etc at the
6362 new position. Note that this function does not skip over text that
6363 is invisible because of text properties. */
6365 static void
6366 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6368 int newline_found_p, skipped_p = 0;
6369 struct bidi_it bidi_it_prev;
6371 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6373 /* Skip over lines that are invisible because they are indented
6374 more than the value of IT->selective. */
6375 if (it->selective > 0)
6376 while (IT_CHARPOS (*it) < ZV
6377 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6378 it->selective))
6380 eassert (IT_BYTEPOS (*it) == BEGV
6381 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6382 newline_found_p =
6383 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6386 /* Position on the newline if that's what's requested. */
6387 if (on_newline_p && newline_found_p)
6389 if (STRINGP (it->string))
6391 if (IT_STRING_CHARPOS (*it) > 0)
6393 if (!it->bidi_p)
6395 --IT_STRING_CHARPOS (*it);
6396 --IT_STRING_BYTEPOS (*it);
6398 else
6400 /* We need to restore the bidi iterator to the state
6401 it had on the newline, and resync the IT's
6402 position with that. */
6403 it->bidi_it = bidi_it_prev;
6404 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6405 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6409 else if (IT_CHARPOS (*it) > BEGV)
6411 if (!it->bidi_p)
6413 --IT_CHARPOS (*it);
6414 --IT_BYTEPOS (*it);
6416 else
6418 /* We need to restore the bidi iterator to the state it
6419 had on the newline and resync IT with that. */
6420 it->bidi_it = bidi_it_prev;
6421 IT_CHARPOS (*it) = it->bidi_it.charpos;
6422 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6424 reseat (it, it->current.pos, 0);
6427 else if (skipped_p)
6428 reseat (it, it->current.pos, 0);
6430 CHECK_IT (it);
6435 /***********************************************************************
6436 Changing an iterator's position
6437 ***********************************************************************/
6439 /* Change IT's current position to POS in current_buffer. If FORCE_P
6440 is non-zero, always check for text properties at the new position.
6441 Otherwise, text properties are only looked up if POS >=
6442 IT->check_charpos of a property. */
6444 static void
6445 reseat (struct it *it, struct text_pos pos, int force_p)
6447 ptrdiff_t original_pos = IT_CHARPOS (*it);
6449 reseat_1 (it, pos, 0);
6451 /* Determine where to check text properties. Avoid doing it
6452 where possible because text property lookup is very expensive. */
6453 if (force_p
6454 || CHARPOS (pos) > it->stop_charpos
6455 || CHARPOS (pos) < original_pos)
6457 if (it->bidi_p)
6459 /* For bidi iteration, we need to prime prev_stop and
6460 base_level_stop with our best estimations. */
6461 /* Implementation note: Of course, POS is not necessarily a
6462 stop position, so assigning prev_pos to it is a lie; we
6463 should have called compute_stop_backwards. However, if
6464 the current buffer does not include any R2L characters,
6465 that call would be a waste of cycles, because the
6466 iterator will never move back, and thus never cross this
6467 "fake" stop position. So we delay that backward search
6468 until the time we really need it, in next_element_from_buffer. */
6469 if (CHARPOS (pos) != it->prev_stop)
6470 it->prev_stop = CHARPOS (pos);
6471 if (CHARPOS (pos) < it->base_level_stop)
6472 it->base_level_stop = 0; /* meaning it's unknown */
6473 handle_stop (it);
6475 else
6477 handle_stop (it);
6478 it->prev_stop = it->base_level_stop = 0;
6483 CHECK_IT (it);
6487 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6488 IT->stop_pos to POS, also. */
6490 static void
6491 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6493 /* Don't call this function when scanning a C string. */
6494 eassert (it->s == NULL);
6496 /* POS must be a reasonable value. */
6497 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6499 it->current.pos = it->position = pos;
6500 it->end_charpos = ZV;
6501 it->dpvec = NULL;
6502 it->current.dpvec_index = -1;
6503 it->current.overlay_string_index = -1;
6504 IT_STRING_CHARPOS (*it) = -1;
6505 IT_STRING_BYTEPOS (*it) = -1;
6506 it->string = Qnil;
6507 it->method = GET_FROM_BUFFER;
6508 it->object = it->w->contents;
6509 it->area = TEXT_AREA;
6510 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6511 it->sp = 0;
6512 it->string_from_display_prop_p = 0;
6513 it->string_from_prefix_prop_p = 0;
6515 it->from_disp_prop_p = 0;
6516 it->face_before_selective_p = 0;
6517 if (it->bidi_p)
6519 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6520 &it->bidi_it);
6521 bidi_unshelve_cache (NULL, 0);
6522 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6523 it->bidi_it.string.s = NULL;
6524 it->bidi_it.string.lstring = Qnil;
6525 it->bidi_it.string.bufpos = 0;
6526 it->bidi_it.string.from_disp_str = 0;
6527 it->bidi_it.string.unibyte = 0;
6528 it->bidi_it.w = it->w;
6531 if (set_stop_p)
6533 it->stop_charpos = CHARPOS (pos);
6534 it->base_level_stop = CHARPOS (pos);
6536 /* This make the information stored in it->cmp_it invalidate. */
6537 it->cmp_it.id = -1;
6541 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6542 If S is non-null, it is a C string to iterate over. Otherwise,
6543 STRING gives a Lisp string to iterate over.
6545 If PRECISION > 0, don't return more then PRECISION number of
6546 characters from the string.
6548 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6549 characters have been returned. FIELD_WIDTH < 0 means an infinite
6550 field width.
6552 MULTIBYTE = 0 means disable processing of multibyte characters,
6553 MULTIBYTE > 0 means enable it,
6554 MULTIBYTE < 0 means use IT->multibyte_p.
6556 IT must be initialized via a prior call to init_iterator before
6557 calling this function. */
6559 static void
6560 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6561 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6562 int multibyte)
6564 /* No text property checks performed by default, but see below. */
6565 it->stop_charpos = -1;
6567 /* Set iterator position and end position. */
6568 memset (&it->current, 0, sizeof it->current);
6569 it->current.overlay_string_index = -1;
6570 it->current.dpvec_index = -1;
6571 eassert (charpos >= 0);
6573 /* If STRING is specified, use its multibyteness, otherwise use the
6574 setting of MULTIBYTE, if specified. */
6575 if (multibyte >= 0)
6576 it->multibyte_p = multibyte > 0;
6578 /* Bidirectional reordering of strings is controlled by the default
6579 value of bidi-display-reordering. Don't try to reorder while
6580 loading loadup.el, as the necessary character property tables are
6581 not yet available. */
6582 it->bidi_p =
6583 NILP (Vpurify_flag)
6584 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6586 if (s == NULL)
6588 eassert (STRINGP (string));
6589 it->string = string;
6590 it->s = NULL;
6591 it->end_charpos = it->string_nchars = SCHARS (string);
6592 it->method = GET_FROM_STRING;
6593 it->current.string_pos = string_pos (charpos, string);
6595 if (it->bidi_p)
6597 it->bidi_it.string.lstring = string;
6598 it->bidi_it.string.s = NULL;
6599 it->bidi_it.string.schars = it->end_charpos;
6600 it->bidi_it.string.bufpos = 0;
6601 it->bidi_it.string.from_disp_str = 0;
6602 it->bidi_it.string.unibyte = !it->multibyte_p;
6603 it->bidi_it.w = it->w;
6604 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6605 FRAME_WINDOW_P (it->f), &it->bidi_it);
6608 else
6610 it->s = (const unsigned char *) s;
6611 it->string = Qnil;
6613 /* Note that we use IT->current.pos, not it->current.string_pos,
6614 for displaying C strings. */
6615 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6616 if (it->multibyte_p)
6618 it->current.pos = c_string_pos (charpos, s, 1);
6619 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6621 else
6623 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6624 it->end_charpos = it->string_nchars = strlen (s);
6627 if (it->bidi_p)
6629 it->bidi_it.string.lstring = Qnil;
6630 it->bidi_it.string.s = (const unsigned char *) s;
6631 it->bidi_it.string.schars = it->end_charpos;
6632 it->bidi_it.string.bufpos = 0;
6633 it->bidi_it.string.from_disp_str = 0;
6634 it->bidi_it.string.unibyte = !it->multibyte_p;
6635 it->bidi_it.w = it->w;
6636 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6637 &it->bidi_it);
6639 it->method = GET_FROM_C_STRING;
6642 /* PRECISION > 0 means don't return more than PRECISION characters
6643 from the string. */
6644 if (precision > 0 && it->end_charpos - charpos > precision)
6646 it->end_charpos = it->string_nchars = charpos + precision;
6647 if (it->bidi_p)
6648 it->bidi_it.string.schars = it->end_charpos;
6651 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6652 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6653 FIELD_WIDTH < 0 means infinite field width. This is useful for
6654 padding with `-' at the end of a mode line. */
6655 if (field_width < 0)
6656 field_width = INFINITY;
6657 /* Implementation note: We deliberately don't enlarge
6658 it->bidi_it.string.schars here to fit it->end_charpos, because
6659 the bidi iterator cannot produce characters out of thin air. */
6660 if (field_width > it->end_charpos - charpos)
6661 it->end_charpos = charpos + field_width;
6663 /* Use the standard display table for displaying strings. */
6664 if (DISP_TABLE_P (Vstandard_display_table))
6665 it->dp = XCHAR_TABLE (Vstandard_display_table);
6667 it->stop_charpos = charpos;
6668 it->prev_stop = charpos;
6669 it->base_level_stop = 0;
6670 if (it->bidi_p)
6672 it->bidi_it.first_elt = 1;
6673 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6674 it->bidi_it.disp_pos = -1;
6676 if (s == NULL && it->multibyte_p)
6678 ptrdiff_t endpos = SCHARS (it->string);
6679 if (endpos > it->end_charpos)
6680 endpos = it->end_charpos;
6681 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6682 it->string);
6684 CHECK_IT (it);
6689 /***********************************************************************
6690 Iteration
6691 ***********************************************************************/
6693 /* Map enum it_method value to corresponding next_element_from_* function. */
6695 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6697 next_element_from_buffer,
6698 next_element_from_display_vector,
6699 next_element_from_string,
6700 next_element_from_c_string,
6701 next_element_from_image,
6702 next_element_from_stretch
6705 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6708 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6709 (possibly with the following characters). */
6711 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6712 ((IT)->cmp_it.id >= 0 \
6713 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6714 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6715 END_CHARPOS, (IT)->w, \
6716 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6717 (IT)->string)))
6720 /* Lookup the char-table Vglyphless_char_display for character C (-1
6721 if we want information for no-font case), and return the display
6722 method symbol. By side-effect, update it->what and
6723 it->glyphless_method. This function is called from
6724 get_next_display_element for each character element, and from
6725 x_produce_glyphs when no suitable font was found. */
6727 Lisp_Object
6728 lookup_glyphless_char_display (int c, struct it *it)
6730 Lisp_Object glyphless_method = Qnil;
6732 if (CHAR_TABLE_P (Vglyphless_char_display)
6733 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6735 if (c >= 0)
6737 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6738 if (CONSP (glyphless_method))
6739 glyphless_method = FRAME_WINDOW_P (it->f)
6740 ? XCAR (glyphless_method)
6741 : XCDR (glyphless_method);
6743 else
6744 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6747 retry:
6748 if (NILP (glyphless_method))
6750 if (c >= 0)
6751 /* The default is to display the character by a proper font. */
6752 return Qnil;
6753 /* The default for the no-font case is to display an empty box. */
6754 glyphless_method = Qempty_box;
6756 if (EQ (glyphless_method, Qzero_width))
6758 if (c >= 0)
6759 return glyphless_method;
6760 /* This method can't be used for the no-font case. */
6761 glyphless_method = Qempty_box;
6763 if (EQ (glyphless_method, Qthin_space))
6764 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6765 else if (EQ (glyphless_method, Qempty_box))
6766 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6767 else if (EQ (glyphless_method, Qhex_code))
6768 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6769 else if (STRINGP (glyphless_method))
6770 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6771 else
6773 /* Invalid value. We use the default method. */
6774 glyphless_method = Qnil;
6775 goto retry;
6777 it->what = IT_GLYPHLESS;
6778 return glyphless_method;
6781 /* Merge escape glyph face and cache the result. */
6783 static struct frame *last_escape_glyph_frame = NULL;
6784 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6785 static int last_escape_glyph_merged_face_id = 0;
6787 static int
6788 merge_escape_glyph_face (struct it *it)
6790 int face_id;
6792 if (it->f == last_escape_glyph_frame
6793 && it->face_id == last_escape_glyph_face_id)
6794 face_id = last_escape_glyph_merged_face_id;
6795 else
6797 /* Merge the `escape-glyph' face into the current face. */
6798 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6799 last_escape_glyph_frame = it->f;
6800 last_escape_glyph_face_id = it->face_id;
6801 last_escape_glyph_merged_face_id = face_id;
6803 return face_id;
6806 /* Likewise for glyphless glyph face. */
6808 static struct frame *last_glyphless_glyph_frame = NULL;
6809 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6810 static int last_glyphless_glyph_merged_face_id = 0;
6813 merge_glyphless_glyph_face (struct it *it)
6815 int face_id;
6817 if (it->f == last_glyphless_glyph_frame
6818 && it->face_id == last_glyphless_glyph_face_id)
6819 face_id = last_glyphless_glyph_merged_face_id;
6820 else
6822 /* Merge the `glyphless-char' face into the current face. */
6823 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6824 last_glyphless_glyph_frame = it->f;
6825 last_glyphless_glyph_face_id = it->face_id;
6826 last_glyphless_glyph_merged_face_id = face_id;
6828 return face_id;
6831 /* Load IT's display element fields with information about the next
6832 display element from the current position of IT. Value is zero if
6833 end of buffer (or C string) is reached. */
6835 static int
6836 get_next_display_element (struct it *it)
6838 /* Non-zero means that we found a display element. Zero means that
6839 we hit the end of what we iterate over. Performance note: the
6840 function pointer `method' used here turns out to be faster than
6841 using a sequence of if-statements. */
6842 int success_p;
6844 get_next:
6845 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6847 if (it->what == IT_CHARACTER)
6849 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6850 and only if (a) the resolved directionality of that character
6851 is R..." */
6852 /* FIXME: Do we need an exception for characters from display
6853 tables? */
6854 if (it->bidi_p && it->bidi_it.type == STRONG_R
6855 && !inhibit_bidi_mirroring)
6856 it->c = bidi_mirror_char (it->c);
6857 /* Map via display table or translate control characters.
6858 IT->c, IT->len etc. have been set to the next character by
6859 the function call above. If we have a display table, and it
6860 contains an entry for IT->c, translate it. Don't do this if
6861 IT->c itself comes from a display table, otherwise we could
6862 end up in an infinite recursion. (An alternative could be to
6863 count the recursion depth of this function and signal an
6864 error when a certain maximum depth is reached.) Is it worth
6865 it? */
6866 if (success_p && it->dpvec == NULL)
6868 Lisp_Object dv;
6869 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6870 int nonascii_space_p = 0;
6871 int nonascii_hyphen_p = 0;
6872 int c = it->c; /* This is the character to display. */
6874 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6876 eassert (SINGLE_BYTE_CHAR_P (c));
6877 if (unibyte_display_via_language_environment)
6879 c = DECODE_CHAR (unibyte, c);
6880 if (c < 0)
6881 c = BYTE8_TO_CHAR (it->c);
6883 else
6884 c = BYTE8_TO_CHAR (it->c);
6887 if (it->dp
6888 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6889 VECTORP (dv)))
6891 struct Lisp_Vector *v = XVECTOR (dv);
6893 /* Return the first character from the display table
6894 entry, if not empty. If empty, don't display the
6895 current character. */
6896 if (v->header.size)
6898 it->dpvec_char_len = it->len;
6899 it->dpvec = v->contents;
6900 it->dpend = v->contents + v->header.size;
6901 it->current.dpvec_index = 0;
6902 it->dpvec_face_id = -1;
6903 it->saved_face_id = it->face_id;
6904 it->method = GET_FROM_DISPLAY_VECTOR;
6905 it->ellipsis_p = 0;
6907 else
6909 set_iterator_to_next (it, 0);
6911 goto get_next;
6914 if (! NILP (lookup_glyphless_char_display (c, it)))
6916 if (it->what == IT_GLYPHLESS)
6917 goto done;
6918 /* Don't display this character. */
6919 set_iterator_to_next (it, 0);
6920 goto get_next;
6923 /* If `nobreak-char-display' is non-nil, we display
6924 non-ASCII spaces and hyphens specially. */
6925 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6927 if (c == 0xA0)
6928 nonascii_space_p = true;
6929 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6930 nonascii_hyphen_p = true;
6933 /* Translate control characters into `\003' or `^C' form.
6934 Control characters coming from a display table entry are
6935 currently not translated because we use IT->dpvec to hold
6936 the translation. This could easily be changed but I
6937 don't believe that it is worth doing.
6939 The characters handled by `nobreak-char-display' must be
6940 translated too.
6942 Non-printable characters and raw-byte characters are also
6943 translated to octal form. */
6944 if (((c < ' ' || c == 127) /* ASCII control chars. */
6945 ? (it->area != TEXT_AREA
6946 /* In mode line, treat \n, \t like other crl chars. */
6947 || (c != '\t'
6948 && it->glyph_row
6949 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6950 || (c != '\n' && c != '\t'))
6951 : (nonascii_space_p
6952 || nonascii_hyphen_p
6953 || CHAR_BYTE8_P (c)
6954 || ! CHAR_PRINTABLE_P (c))))
6956 /* C is a control character, non-ASCII space/hyphen,
6957 raw-byte, or a non-printable character which must be
6958 displayed either as '\003' or as `^C' where the '\\'
6959 and '^' can be defined in the display table. Fill
6960 IT->ctl_chars with glyphs for what we have to
6961 display. Then, set IT->dpvec to these glyphs. */
6962 Lisp_Object gc;
6963 int ctl_len;
6964 int face_id;
6965 int lface_id = 0;
6966 int escape_glyph;
6968 /* Handle control characters with ^. */
6970 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6972 int g;
6974 g = '^'; /* default glyph for Control */
6975 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6976 if (it->dp
6977 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6979 g = GLYPH_CODE_CHAR (gc);
6980 lface_id = GLYPH_CODE_FACE (gc);
6983 face_id = (lface_id
6984 ? merge_faces (it->f, Qt, lface_id, it->face_id)
6985 : merge_escape_glyph_face (it));
6987 XSETINT (it->ctl_chars[0], g);
6988 XSETINT (it->ctl_chars[1], c ^ 0100);
6989 ctl_len = 2;
6990 goto display_control;
6993 /* Handle non-ascii space in the mode where it only gets
6994 highlighting. */
6996 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6998 /* Merge `nobreak-space' into the current face. */
6999 face_id = merge_faces (it->f, Qnobreak_space, 0,
7000 it->face_id);
7001 XSETINT (it->ctl_chars[0], ' ');
7002 ctl_len = 1;
7003 goto display_control;
7006 /* Handle sequences that start with the "escape glyph". */
7008 /* the default escape glyph is \. */
7009 escape_glyph = '\\';
7011 if (it->dp
7012 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7014 escape_glyph = GLYPH_CODE_CHAR (gc);
7015 lface_id = GLYPH_CODE_FACE (gc);
7018 face_id = (lface_id
7019 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7020 : merge_escape_glyph_face (it));
7022 /* Draw non-ASCII hyphen with just highlighting: */
7024 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7026 XSETINT (it->ctl_chars[0], '-');
7027 ctl_len = 1;
7028 goto display_control;
7031 /* Draw non-ASCII space/hyphen with escape glyph: */
7033 if (nonascii_space_p || nonascii_hyphen_p)
7035 XSETINT (it->ctl_chars[0], escape_glyph);
7036 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7037 ctl_len = 2;
7038 goto display_control;
7042 char str[10];
7043 int len, i;
7045 if (CHAR_BYTE8_P (c))
7046 /* Display \200 instead of \17777600. */
7047 c = CHAR_TO_BYTE8 (c);
7048 len = sprintf (str, "%03o", c);
7050 XSETINT (it->ctl_chars[0], escape_glyph);
7051 for (i = 0; i < len; i++)
7052 XSETINT (it->ctl_chars[i + 1], str[i]);
7053 ctl_len = len + 1;
7056 display_control:
7057 /* Set up IT->dpvec and return first character from it. */
7058 it->dpvec_char_len = it->len;
7059 it->dpvec = it->ctl_chars;
7060 it->dpend = it->dpvec + ctl_len;
7061 it->current.dpvec_index = 0;
7062 it->dpvec_face_id = face_id;
7063 it->saved_face_id = it->face_id;
7064 it->method = GET_FROM_DISPLAY_VECTOR;
7065 it->ellipsis_p = 0;
7066 goto get_next;
7068 it->char_to_display = c;
7070 else if (success_p)
7072 it->char_to_display = it->c;
7076 #ifdef HAVE_WINDOW_SYSTEM
7077 /* Adjust face id for a multibyte character. There are no multibyte
7078 character in unibyte text. */
7079 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7080 && it->multibyte_p
7081 && success_p
7082 && FRAME_WINDOW_P (it->f))
7084 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7086 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7088 /* Automatic composition with glyph-string. */
7089 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7091 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7093 else
7095 ptrdiff_t pos = (it->s ? -1
7096 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7097 : IT_CHARPOS (*it));
7098 int c;
7100 if (it->what == IT_CHARACTER)
7101 c = it->char_to_display;
7102 else
7104 struct composition *cmp = composition_table[it->cmp_it.id];
7105 int i;
7107 c = ' ';
7108 for (i = 0; i < cmp->glyph_len; i++)
7109 /* TAB in a composition means display glyphs with
7110 padding space on the left or right. */
7111 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7112 break;
7114 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7117 #endif /* HAVE_WINDOW_SYSTEM */
7119 done:
7120 /* Is this character the last one of a run of characters with
7121 box? If yes, set IT->end_of_box_run_p to 1. */
7122 if (it->face_box_p
7123 && it->s == NULL)
7125 if (it->method == GET_FROM_STRING && it->sp)
7127 int face_id = underlying_face_id (it);
7128 struct face *face = FACE_FROM_ID (it->f, face_id);
7130 if (face)
7132 if (face->box == FACE_NO_BOX)
7134 /* If the box comes from face properties in a
7135 display string, check faces in that string. */
7136 int string_face_id = face_after_it_pos (it);
7137 it->end_of_box_run_p
7138 = (FACE_FROM_ID (it->f, string_face_id)->box
7139 == FACE_NO_BOX);
7141 /* Otherwise, the box comes from the underlying face.
7142 If this is the last string character displayed, check
7143 the next buffer location. */
7144 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7145 /* n_overlay_strings is unreliable unless
7146 overlay_string_index is non-negative. */
7147 && ((it->current.overlay_string_index >= 0
7148 && (it->current.overlay_string_index
7149 == it->n_overlay_strings - 1))
7150 /* A string from display property. */
7151 || it->from_disp_prop_p))
7153 ptrdiff_t ignore;
7154 int next_face_id;
7155 struct text_pos pos = it->current.pos;
7157 /* For a string from a display property, the next
7158 buffer position is stored in the 'position'
7159 member of the iteration stack slot below the
7160 current one, see handle_single_display_spec. By
7161 contrast, it->current.pos was is not yet updated
7162 to point to that buffer position; that will
7163 happen in pop_it, after we finish displaying the
7164 current string. Note that we already checked
7165 above that it->sp is positive, so subtracting one
7166 from it is safe. */
7167 if (it->from_disp_prop_p)
7168 pos = (it->stack + it->sp - 1)->position;
7169 else
7170 INC_TEXT_POS (pos, it->multibyte_p);
7172 if (CHARPOS (pos) >= ZV)
7173 it->end_of_box_run_p = true;
7174 else
7176 next_face_id = face_at_buffer_position
7177 (it->w, CHARPOS (pos), &ignore,
7178 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7179 it->end_of_box_run_p
7180 = (FACE_FROM_ID (it->f, next_face_id)->box
7181 == FACE_NO_BOX);
7186 /* next_element_from_display_vector sets this flag according to
7187 faces of the display vector glyphs, see there. */
7188 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7190 int face_id = face_after_it_pos (it);
7191 it->end_of_box_run_p
7192 = (face_id != it->face_id
7193 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7196 /* If we reached the end of the object we've been iterating (e.g., a
7197 display string or an overlay string), and there's something on
7198 IT->stack, proceed with what's on the stack. It doesn't make
7199 sense to return zero if there's unprocessed stuff on the stack,
7200 because otherwise that stuff will never be displayed. */
7201 if (!success_p && it->sp > 0)
7203 set_iterator_to_next (it, 0);
7204 success_p = get_next_display_element (it);
7207 /* Value is 0 if end of buffer or string reached. */
7208 return success_p;
7212 /* Move IT to the next display element.
7214 RESEAT_P non-zero means if called on a newline in buffer text,
7215 skip to the next visible line start.
7217 Functions get_next_display_element and set_iterator_to_next are
7218 separate because I find this arrangement easier to handle than a
7219 get_next_display_element function that also increments IT's
7220 position. The way it is we can first look at an iterator's current
7221 display element, decide whether it fits on a line, and if it does,
7222 increment the iterator position. The other way around we probably
7223 would either need a flag indicating whether the iterator has to be
7224 incremented the next time, or we would have to implement a
7225 decrement position function which would not be easy to write. */
7227 void
7228 set_iterator_to_next (struct it *it, int reseat_p)
7230 /* Reset flags indicating start and end of a sequence of characters
7231 with box. Reset them at the start of this function because
7232 moving the iterator to a new position might set them. */
7233 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7235 switch (it->method)
7237 case GET_FROM_BUFFER:
7238 /* The current display element of IT is a character from
7239 current_buffer. Advance in the buffer, and maybe skip over
7240 invisible lines that are so because of selective display. */
7241 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7242 reseat_at_next_visible_line_start (it, 0);
7243 else if (it->cmp_it.id >= 0)
7245 /* We are currently getting glyphs from a composition. */
7246 if (! it->bidi_p)
7248 IT_CHARPOS (*it) += it->cmp_it.nchars;
7249 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7251 else
7253 int i;
7255 /* Update IT's char/byte positions to point to the first
7256 character of the next grapheme cluster, or to the
7257 character visually after the current composition. */
7258 for (i = 0; i < it->cmp_it.nchars; i++)
7259 bidi_move_to_visually_next (&it->bidi_it);
7260 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7261 IT_CHARPOS (*it) = it->bidi_it.charpos;
7264 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7265 && it->cmp_it.to < it->cmp_it.nglyphs)
7267 /* Composition created while scanning forward. Proceed
7268 to the next grapheme cluster. */
7269 it->cmp_it.from = it->cmp_it.to;
7271 else if ((it->bidi_p && it->cmp_it.reversed_p)
7272 && it->cmp_it.from > 0)
7274 /* Composition created while scanning backward. Proceed
7275 to the previous grapheme cluster. */
7276 it->cmp_it.to = it->cmp_it.from;
7278 else
7280 /* No more grapheme clusters in this composition.
7281 Find the next stop position. */
7282 ptrdiff_t stop = it->end_charpos;
7284 if (it->bidi_it.scan_dir < 0)
7285 /* Now we are scanning backward and don't know
7286 where to stop. */
7287 stop = -1;
7288 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7289 IT_BYTEPOS (*it), stop, Qnil);
7292 else
7294 eassert (it->len != 0);
7296 if (!it->bidi_p)
7298 IT_BYTEPOS (*it) += it->len;
7299 IT_CHARPOS (*it) += 1;
7301 else
7303 int prev_scan_dir = it->bidi_it.scan_dir;
7304 /* If this is a new paragraph, determine its base
7305 direction (a.k.a. its base embedding level). */
7306 if (it->bidi_it.new_paragraph)
7307 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7308 bidi_move_to_visually_next (&it->bidi_it);
7309 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7310 IT_CHARPOS (*it) = it->bidi_it.charpos;
7311 if (prev_scan_dir != it->bidi_it.scan_dir)
7313 /* As the scan direction was changed, we must
7314 re-compute the stop position for composition. */
7315 ptrdiff_t stop = it->end_charpos;
7316 if (it->bidi_it.scan_dir < 0)
7317 stop = -1;
7318 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7319 IT_BYTEPOS (*it), stop, Qnil);
7322 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7324 break;
7326 case GET_FROM_C_STRING:
7327 /* Current display element of IT is from a C string. */
7328 if (!it->bidi_p
7329 /* If the string position is beyond string's end, it means
7330 next_element_from_c_string is padding the string with
7331 blanks, in which case we bypass the bidi iterator,
7332 because it cannot deal with such virtual characters. */
7333 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7335 IT_BYTEPOS (*it) += it->len;
7336 IT_CHARPOS (*it) += 1;
7338 else
7340 bidi_move_to_visually_next (&it->bidi_it);
7341 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7342 IT_CHARPOS (*it) = it->bidi_it.charpos;
7344 break;
7346 case GET_FROM_DISPLAY_VECTOR:
7347 /* Current display element of IT is from a display table entry.
7348 Advance in the display table definition. Reset it to null if
7349 end reached, and continue with characters from buffers/
7350 strings. */
7351 ++it->current.dpvec_index;
7353 /* Restore face of the iterator to what they were before the
7354 display vector entry (these entries may contain faces). */
7355 it->face_id = it->saved_face_id;
7357 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7359 int recheck_faces = it->ellipsis_p;
7361 if (it->s)
7362 it->method = GET_FROM_C_STRING;
7363 else if (STRINGP (it->string))
7364 it->method = GET_FROM_STRING;
7365 else
7367 it->method = GET_FROM_BUFFER;
7368 it->object = it->w->contents;
7371 it->dpvec = NULL;
7372 it->current.dpvec_index = -1;
7374 /* Skip over characters which were displayed via IT->dpvec. */
7375 if (it->dpvec_char_len < 0)
7376 reseat_at_next_visible_line_start (it, 1);
7377 else if (it->dpvec_char_len > 0)
7379 if (it->method == GET_FROM_STRING
7380 && it->current.overlay_string_index >= 0
7381 && it->n_overlay_strings > 0)
7382 it->ignore_overlay_strings_at_pos_p = true;
7383 it->len = it->dpvec_char_len;
7384 set_iterator_to_next (it, reseat_p);
7387 /* Maybe recheck faces after display vector. */
7388 if (recheck_faces)
7389 it->stop_charpos = IT_CHARPOS (*it);
7391 break;
7393 case GET_FROM_STRING:
7394 /* Current display element is a character from a Lisp string. */
7395 eassert (it->s == NULL && STRINGP (it->string));
7396 /* Don't advance past string end. These conditions are true
7397 when set_iterator_to_next is called at the end of
7398 get_next_display_element, in which case the Lisp string is
7399 already exhausted, and all we want is pop the iterator
7400 stack. */
7401 if (it->current.overlay_string_index >= 0)
7403 /* This is an overlay string, so there's no padding with
7404 spaces, and the number of characters in the string is
7405 where the string ends. */
7406 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7407 goto consider_string_end;
7409 else
7411 /* Not an overlay string. There could be padding, so test
7412 against it->end_charpos. */
7413 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7414 goto consider_string_end;
7416 if (it->cmp_it.id >= 0)
7418 /* We are delivering display elements from a composition.
7419 Update the string position past the grapheme cluster
7420 we've just processed. */
7421 if (! it->bidi_p)
7423 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7424 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7426 else
7428 int i;
7430 for (i = 0; i < it->cmp_it.nchars; i++)
7431 bidi_move_to_visually_next (&it->bidi_it);
7432 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7433 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7436 /* Did we exhaust all the grapheme clusters of this
7437 composition? */
7438 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7439 && (it->cmp_it.to < it->cmp_it.nglyphs))
7441 /* Not all the grapheme clusters were processed yet;
7442 advance to the next cluster. */
7443 it->cmp_it.from = it->cmp_it.to;
7445 else if ((it->bidi_p && it->cmp_it.reversed_p)
7446 && it->cmp_it.from > 0)
7448 /* Likewise: advance to the next cluster, but going in
7449 the reverse direction. */
7450 it->cmp_it.to = it->cmp_it.from;
7452 else
7454 /* This composition was fully processed; find the next
7455 candidate place for checking for composed
7456 characters. */
7457 /* Always limit string searches to the string length;
7458 any padding spaces are not part of the string, and
7459 there cannot be any compositions in that padding. */
7460 ptrdiff_t stop = SCHARS (it->string);
7462 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7463 stop = -1;
7464 else if (it->end_charpos < stop)
7466 /* Cf. PRECISION in reseat_to_string: we might be
7467 limited in how many of the string characters we
7468 need to deliver. */
7469 stop = it->end_charpos;
7471 composition_compute_stop_pos (&it->cmp_it,
7472 IT_STRING_CHARPOS (*it),
7473 IT_STRING_BYTEPOS (*it), stop,
7474 it->string);
7477 else
7479 if (!it->bidi_p
7480 /* If the string position is beyond string's end, it
7481 means next_element_from_string is padding the string
7482 with blanks, in which case we bypass the bidi
7483 iterator, because it cannot deal with such virtual
7484 characters. */
7485 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7487 IT_STRING_BYTEPOS (*it) += it->len;
7488 IT_STRING_CHARPOS (*it) += 1;
7490 else
7492 int prev_scan_dir = it->bidi_it.scan_dir;
7494 bidi_move_to_visually_next (&it->bidi_it);
7495 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7496 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7497 /* If the scan direction changes, we may need to update
7498 the place where to check for composed characters. */
7499 if (prev_scan_dir != it->bidi_it.scan_dir)
7501 ptrdiff_t stop = SCHARS (it->string);
7503 if (it->bidi_it.scan_dir < 0)
7504 stop = -1;
7505 else if (it->end_charpos < stop)
7506 stop = it->end_charpos;
7508 composition_compute_stop_pos (&it->cmp_it,
7509 IT_STRING_CHARPOS (*it),
7510 IT_STRING_BYTEPOS (*it), stop,
7511 it->string);
7516 consider_string_end:
7518 if (it->current.overlay_string_index >= 0)
7520 /* IT->string is an overlay string. Advance to the
7521 next, if there is one. */
7522 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7524 it->ellipsis_p = 0;
7525 next_overlay_string (it);
7526 if (it->ellipsis_p)
7527 setup_for_ellipsis (it, 0);
7530 else
7532 /* IT->string is not an overlay string. If we reached
7533 its end, and there is something on IT->stack, proceed
7534 with what is on the stack. This can be either another
7535 string, this time an overlay string, or a buffer. */
7536 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7537 && it->sp > 0)
7539 pop_it (it);
7540 if (it->method == GET_FROM_STRING)
7541 goto consider_string_end;
7544 break;
7546 case GET_FROM_IMAGE:
7547 case GET_FROM_STRETCH:
7548 /* The position etc with which we have to proceed are on
7549 the stack. The position may be at the end of a string,
7550 if the `display' property takes up the whole string. */
7551 eassert (it->sp > 0);
7552 pop_it (it);
7553 if (it->method == GET_FROM_STRING)
7554 goto consider_string_end;
7555 break;
7557 default:
7558 /* There are no other methods defined, so this should be a bug. */
7559 emacs_abort ();
7562 eassert (it->method != GET_FROM_STRING
7563 || (STRINGP (it->string)
7564 && IT_STRING_CHARPOS (*it) >= 0));
7567 /* Load IT's display element fields with information about the next
7568 display element which comes from a display table entry or from the
7569 result of translating a control character to one of the forms `^C'
7570 or `\003'.
7572 IT->dpvec holds the glyphs to return as characters.
7573 IT->saved_face_id holds the face id before the display vector--it
7574 is restored into IT->face_id in set_iterator_to_next. */
7576 static int
7577 next_element_from_display_vector (struct it *it)
7579 Lisp_Object gc;
7580 int prev_face_id = it->face_id;
7581 int next_face_id;
7583 /* Precondition. */
7584 eassert (it->dpvec && it->current.dpvec_index >= 0);
7586 it->face_id = it->saved_face_id;
7588 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7589 That seemed totally bogus - so I changed it... */
7590 gc = it->dpvec[it->current.dpvec_index];
7592 if (GLYPH_CODE_P (gc))
7594 struct face *this_face, *prev_face, *next_face;
7596 it->c = GLYPH_CODE_CHAR (gc);
7597 it->len = CHAR_BYTES (it->c);
7599 /* The entry may contain a face id to use. Such a face id is
7600 the id of a Lisp face, not a realized face. A face id of
7601 zero means no face is specified. */
7602 if (it->dpvec_face_id >= 0)
7603 it->face_id = it->dpvec_face_id;
7604 else
7606 int lface_id = GLYPH_CODE_FACE (gc);
7607 if (lface_id > 0)
7608 it->face_id = merge_faces (it->f, Qt, lface_id,
7609 it->saved_face_id);
7612 /* Glyphs in the display vector could have the box face, so we
7613 need to set the related flags in the iterator, as
7614 appropriate. */
7615 this_face = FACE_FROM_ID (it->f, it->face_id);
7616 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7618 /* Is this character the first character of a box-face run? */
7619 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7620 && (!prev_face
7621 || prev_face->box == FACE_NO_BOX));
7623 /* For the last character of the box-face run, we need to look
7624 either at the next glyph from the display vector, or at the
7625 face we saw before the display vector. */
7626 next_face_id = it->saved_face_id;
7627 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7629 if (it->dpvec_face_id >= 0)
7630 next_face_id = it->dpvec_face_id;
7631 else
7633 int lface_id =
7634 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7636 if (lface_id > 0)
7637 next_face_id = merge_faces (it->f, Qt, lface_id,
7638 it->saved_face_id);
7641 next_face = FACE_FROM_ID (it->f, next_face_id);
7642 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7643 && (!next_face
7644 || next_face->box == FACE_NO_BOX));
7645 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7647 else
7648 /* Display table entry is invalid. Return a space. */
7649 it->c = ' ', it->len = 1;
7651 /* Don't change position and object of the iterator here. They are
7652 still the values of the character that had this display table
7653 entry or was translated, and that's what we want. */
7654 it->what = IT_CHARACTER;
7655 return 1;
7658 /* Get the first element of string/buffer in the visual order, after
7659 being reseated to a new position in a string or a buffer. */
7660 static void
7661 get_visually_first_element (struct it *it)
7663 int string_p = STRINGP (it->string) || it->s;
7664 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7665 ptrdiff_t bob = (string_p ? 0 : BEGV);
7667 if (STRINGP (it->string))
7669 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7670 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7672 else
7674 it->bidi_it.charpos = IT_CHARPOS (*it);
7675 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7678 if (it->bidi_it.charpos == eob)
7680 /* Nothing to do, but reset the FIRST_ELT flag, like
7681 bidi_paragraph_init does, because we are not going to
7682 call it. */
7683 it->bidi_it.first_elt = 0;
7685 else if (it->bidi_it.charpos == bob
7686 || (!string_p
7687 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7688 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7690 /* If we are at the beginning of a line/string, we can produce
7691 the next element right away. */
7692 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7693 bidi_move_to_visually_next (&it->bidi_it);
7695 else
7697 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7699 /* We need to prime the bidi iterator starting at the line's or
7700 string's beginning, before we will be able to produce the
7701 next element. */
7702 if (string_p)
7703 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7704 else
7705 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7706 IT_BYTEPOS (*it), -1,
7707 &it->bidi_it.bytepos);
7708 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7711 /* Now return to buffer/string position where we were asked
7712 to get the next display element, and produce that. */
7713 bidi_move_to_visually_next (&it->bidi_it);
7715 while (it->bidi_it.bytepos != orig_bytepos
7716 && it->bidi_it.charpos < eob);
7719 /* Adjust IT's position information to where we ended up. */
7720 if (STRINGP (it->string))
7722 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7723 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7725 else
7727 IT_CHARPOS (*it) = it->bidi_it.charpos;
7728 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7731 if (STRINGP (it->string) || !it->s)
7733 ptrdiff_t stop, charpos, bytepos;
7735 if (STRINGP (it->string))
7737 eassert (!it->s);
7738 stop = SCHARS (it->string);
7739 if (stop > it->end_charpos)
7740 stop = it->end_charpos;
7741 charpos = IT_STRING_CHARPOS (*it);
7742 bytepos = IT_STRING_BYTEPOS (*it);
7744 else
7746 stop = it->end_charpos;
7747 charpos = IT_CHARPOS (*it);
7748 bytepos = IT_BYTEPOS (*it);
7750 if (it->bidi_it.scan_dir < 0)
7751 stop = -1;
7752 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7753 it->string);
7757 /* Load IT with the next display element from Lisp string IT->string.
7758 IT->current.string_pos is the current position within the string.
7759 If IT->current.overlay_string_index >= 0, the Lisp string is an
7760 overlay string. */
7762 static int
7763 next_element_from_string (struct it *it)
7765 struct text_pos position;
7767 eassert (STRINGP (it->string));
7768 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7769 eassert (IT_STRING_CHARPOS (*it) >= 0);
7770 position = it->current.string_pos;
7772 /* With bidi reordering, the character to display might not be the
7773 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7774 that we were reseat()ed to a new string, whose paragraph
7775 direction is not known. */
7776 if (it->bidi_p && it->bidi_it.first_elt)
7778 get_visually_first_element (it);
7779 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7782 /* Time to check for invisible text? */
7783 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7785 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7787 if (!(!it->bidi_p
7788 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7789 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7791 /* With bidi non-linear iteration, we could find
7792 ourselves far beyond the last computed stop_charpos,
7793 with several other stop positions in between that we
7794 missed. Scan them all now, in buffer's logical
7795 order, until we find and handle the last stop_charpos
7796 that precedes our current position. */
7797 handle_stop_backwards (it, it->stop_charpos);
7798 return GET_NEXT_DISPLAY_ELEMENT (it);
7800 else
7802 if (it->bidi_p)
7804 /* Take note of the stop position we just moved
7805 across, for when we will move back across it. */
7806 it->prev_stop = it->stop_charpos;
7807 /* If we are at base paragraph embedding level, take
7808 note of the last stop position seen at this
7809 level. */
7810 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7811 it->base_level_stop = it->stop_charpos;
7813 handle_stop (it);
7815 /* Since a handler may have changed IT->method, we must
7816 recurse here. */
7817 return GET_NEXT_DISPLAY_ELEMENT (it);
7820 else if (it->bidi_p
7821 /* If we are before prev_stop, we may have overstepped
7822 on our way backwards a stop_pos, and if so, we need
7823 to handle that stop_pos. */
7824 && IT_STRING_CHARPOS (*it) < it->prev_stop
7825 /* We can sometimes back up for reasons that have nothing
7826 to do with bidi reordering. E.g., compositions. The
7827 code below is only needed when we are above the base
7828 embedding level, so test for that explicitly. */
7829 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7831 /* If we lost track of base_level_stop, we have no better
7832 place for handle_stop_backwards to start from than string
7833 beginning. This happens, e.g., when we were reseated to
7834 the previous screenful of text by vertical-motion. */
7835 if (it->base_level_stop <= 0
7836 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7837 it->base_level_stop = 0;
7838 handle_stop_backwards (it, it->base_level_stop);
7839 return GET_NEXT_DISPLAY_ELEMENT (it);
7843 if (it->current.overlay_string_index >= 0)
7845 /* Get the next character from an overlay string. In overlay
7846 strings, there is no field width or padding with spaces to
7847 do. */
7848 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7850 it->what = IT_EOB;
7851 return 0;
7853 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7854 IT_STRING_BYTEPOS (*it),
7855 it->bidi_it.scan_dir < 0
7856 ? -1
7857 : SCHARS (it->string))
7858 && next_element_from_composition (it))
7860 return 1;
7862 else if (STRING_MULTIBYTE (it->string))
7864 const unsigned char *s = (SDATA (it->string)
7865 + IT_STRING_BYTEPOS (*it));
7866 it->c = string_char_and_length (s, &it->len);
7868 else
7870 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7871 it->len = 1;
7874 else
7876 /* Get the next character from a Lisp string that is not an
7877 overlay string. Such strings come from the mode line, for
7878 example. We may have to pad with spaces, or truncate the
7879 string. See also next_element_from_c_string. */
7880 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7882 it->what = IT_EOB;
7883 return 0;
7885 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7887 /* Pad with spaces. */
7888 it->c = ' ', it->len = 1;
7889 CHARPOS (position) = BYTEPOS (position) = -1;
7891 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7892 IT_STRING_BYTEPOS (*it),
7893 it->bidi_it.scan_dir < 0
7894 ? -1
7895 : it->string_nchars)
7896 && next_element_from_composition (it))
7898 return 1;
7900 else if (STRING_MULTIBYTE (it->string))
7902 const unsigned char *s = (SDATA (it->string)
7903 + IT_STRING_BYTEPOS (*it));
7904 it->c = string_char_and_length (s, &it->len);
7906 else
7908 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7909 it->len = 1;
7913 /* Record what we have and where it came from. */
7914 it->what = IT_CHARACTER;
7915 it->object = it->string;
7916 it->position = position;
7917 return 1;
7921 /* Load IT with next display element from C string IT->s.
7922 IT->string_nchars is the maximum number of characters to return
7923 from the string. IT->end_charpos may be greater than
7924 IT->string_nchars when this function is called, in which case we
7925 may have to return padding spaces. Value is zero if end of string
7926 reached, including padding spaces. */
7928 static int
7929 next_element_from_c_string (struct it *it)
7931 bool success_p = true;
7933 eassert (it->s);
7934 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7935 it->what = IT_CHARACTER;
7936 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7937 it->object = make_number (0);
7939 /* With bidi reordering, the character to display might not be the
7940 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7941 we were reseated to a new string, whose paragraph direction is
7942 not known. */
7943 if (it->bidi_p && it->bidi_it.first_elt)
7944 get_visually_first_element (it);
7946 /* IT's position can be greater than IT->string_nchars in case a
7947 field width or precision has been specified when the iterator was
7948 initialized. */
7949 if (IT_CHARPOS (*it) >= it->end_charpos)
7951 /* End of the game. */
7952 it->what = IT_EOB;
7953 success_p = 0;
7955 else if (IT_CHARPOS (*it) >= it->string_nchars)
7957 /* Pad with spaces. */
7958 it->c = ' ', it->len = 1;
7959 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7961 else if (it->multibyte_p)
7962 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7963 else
7964 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7966 return success_p;
7970 /* Set up IT to return characters from an ellipsis, if appropriate.
7971 The definition of the ellipsis glyphs may come from a display table
7972 entry. This function fills IT with the first glyph from the
7973 ellipsis if an ellipsis is to be displayed. */
7975 static int
7976 next_element_from_ellipsis (struct it *it)
7978 if (it->selective_display_ellipsis_p)
7979 setup_for_ellipsis (it, it->len);
7980 else
7982 /* The face at the current position may be different from the
7983 face we find after the invisible text. Remember what it
7984 was in IT->saved_face_id, and signal that it's there by
7985 setting face_before_selective_p. */
7986 it->saved_face_id = it->face_id;
7987 it->method = GET_FROM_BUFFER;
7988 it->object = it->w->contents;
7989 reseat_at_next_visible_line_start (it, 1);
7990 it->face_before_selective_p = true;
7993 return GET_NEXT_DISPLAY_ELEMENT (it);
7997 /* Deliver an image display element. The iterator IT is already
7998 filled with image information (done in handle_display_prop). Value
7999 is always 1. */
8002 static int
8003 next_element_from_image (struct it *it)
8005 it->what = IT_IMAGE;
8006 it->ignore_overlay_strings_at_pos_p = 0;
8007 return 1;
8011 /* Fill iterator IT with next display element from a stretch glyph
8012 property. IT->object is the value of the text property. Value is
8013 always 1. */
8015 static int
8016 next_element_from_stretch (struct it *it)
8018 it->what = IT_STRETCH;
8019 return 1;
8022 /* Scan backwards from IT's current position until we find a stop
8023 position, or until BEGV. This is called when we find ourself
8024 before both the last known prev_stop and base_level_stop while
8025 reordering bidirectional text. */
8027 static void
8028 compute_stop_pos_backwards (struct it *it)
8030 const int SCAN_BACK_LIMIT = 1000;
8031 struct text_pos pos;
8032 struct display_pos save_current = it->current;
8033 struct text_pos save_position = it->position;
8034 ptrdiff_t charpos = IT_CHARPOS (*it);
8035 ptrdiff_t where_we_are = charpos;
8036 ptrdiff_t save_stop_pos = it->stop_charpos;
8037 ptrdiff_t save_end_pos = it->end_charpos;
8039 eassert (NILP (it->string) && !it->s);
8040 eassert (it->bidi_p);
8041 it->bidi_p = 0;
8044 it->end_charpos = min (charpos + 1, ZV);
8045 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8046 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8047 reseat_1 (it, pos, 0);
8048 compute_stop_pos (it);
8049 /* We must advance forward, right? */
8050 if (it->stop_charpos <= charpos)
8051 emacs_abort ();
8053 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8055 if (it->stop_charpos <= where_we_are)
8056 it->prev_stop = it->stop_charpos;
8057 else
8058 it->prev_stop = BEGV;
8059 it->bidi_p = true;
8060 it->current = save_current;
8061 it->position = save_position;
8062 it->stop_charpos = save_stop_pos;
8063 it->end_charpos = save_end_pos;
8066 /* Scan forward from CHARPOS in the current buffer/string, until we
8067 find a stop position > current IT's position. Then handle the stop
8068 position before that. This is called when we bump into a stop
8069 position while reordering bidirectional text. CHARPOS should be
8070 the last previously processed stop_pos (or BEGV/0, if none were
8071 processed yet) whose position is less that IT's current
8072 position. */
8074 static void
8075 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8077 int bufp = !STRINGP (it->string);
8078 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8079 struct display_pos save_current = it->current;
8080 struct text_pos save_position = it->position;
8081 struct text_pos pos1;
8082 ptrdiff_t next_stop;
8084 /* Scan in strict logical order. */
8085 eassert (it->bidi_p);
8086 it->bidi_p = 0;
8089 it->prev_stop = charpos;
8090 if (bufp)
8092 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8093 reseat_1 (it, pos1, 0);
8095 else
8096 it->current.string_pos = string_pos (charpos, it->string);
8097 compute_stop_pos (it);
8098 /* We must advance forward, right? */
8099 if (it->stop_charpos <= it->prev_stop)
8100 emacs_abort ();
8101 charpos = it->stop_charpos;
8103 while (charpos <= where_we_are);
8105 it->bidi_p = true;
8106 it->current = save_current;
8107 it->position = save_position;
8108 next_stop = it->stop_charpos;
8109 it->stop_charpos = it->prev_stop;
8110 handle_stop (it);
8111 it->stop_charpos = next_stop;
8114 /* Load IT with the next display element from current_buffer. Value
8115 is zero if end of buffer reached. IT->stop_charpos is the next
8116 position at which to stop and check for text properties or buffer
8117 end. */
8119 static int
8120 next_element_from_buffer (struct it *it)
8122 bool success_p = true;
8124 eassert (IT_CHARPOS (*it) >= BEGV);
8125 eassert (NILP (it->string) && !it->s);
8126 eassert (!it->bidi_p
8127 || (EQ (it->bidi_it.string.lstring, Qnil)
8128 && it->bidi_it.string.s == NULL));
8130 /* With bidi reordering, the character to display might not be the
8131 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8132 we were reseat()ed to a new buffer position, which is potentially
8133 a different paragraph. */
8134 if (it->bidi_p && it->bidi_it.first_elt)
8136 get_visually_first_element (it);
8137 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8140 if (IT_CHARPOS (*it) >= it->stop_charpos)
8142 if (IT_CHARPOS (*it) >= it->end_charpos)
8144 int overlay_strings_follow_p;
8146 /* End of the game, except when overlay strings follow that
8147 haven't been returned yet. */
8148 if (it->overlay_strings_at_end_processed_p)
8149 overlay_strings_follow_p = 0;
8150 else
8152 it->overlay_strings_at_end_processed_p = true;
8153 overlay_strings_follow_p = get_overlay_strings (it, 0);
8156 if (overlay_strings_follow_p)
8157 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8158 else
8160 it->what = IT_EOB;
8161 it->position = it->current.pos;
8162 success_p = 0;
8165 else if (!(!it->bidi_p
8166 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8167 || IT_CHARPOS (*it) == it->stop_charpos))
8169 /* With bidi non-linear iteration, we could find ourselves
8170 far beyond the last computed stop_charpos, with several
8171 other stop positions in between that we missed. Scan
8172 them all now, in buffer's logical order, until we find
8173 and handle the last stop_charpos that precedes our
8174 current position. */
8175 handle_stop_backwards (it, it->stop_charpos);
8176 return GET_NEXT_DISPLAY_ELEMENT (it);
8178 else
8180 if (it->bidi_p)
8182 /* Take note of the stop position we just moved across,
8183 for when we will move back across it. */
8184 it->prev_stop = it->stop_charpos;
8185 /* If we are at base paragraph embedding level, take
8186 note of the last stop position seen at this
8187 level. */
8188 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8189 it->base_level_stop = it->stop_charpos;
8191 handle_stop (it);
8192 return GET_NEXT_DISPLAY_ELEMENT (it);
8195 else if (it->bidi_p
8196 /* If we are before prev_stop, we may have overstepped on
8197 our way backwards a stop_pos, and if so, we need to
8198 handle that stop_pos. */
8199 && IT_CHARPOS (*it) < it->prev_stop
8200 /* We can sometimes back up for reasons that have nothing
8201 to do with bidi reordering. E.g., compositions. The
8202 code below is only needed when we are above the base
8203 embedding level, so test for that explicitly. */
8204 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8206 if (it->base_level_stop <= 0
8207 || IT_CHARPOS (*it) < it->base_level_stop)
8209 /* If we lost track of base_level_stop, we need to find
8210 prev_stop by looking backwards. This happens, e.g., when
8211 we were reseated to the previous screenful of text by
8212 vertical-motion. */
8213 it->base_level_stop = BEGV;
8214 compute_stop_pos_backwards (it);
8215 handle_stop_backwards (it, it->prev_stop);
8217 else
8218 handle_stop_backwards (it, it->base_level_stop);
8219 return GET_NEXT_DISPLAY_ELEMENT (it);
8221 else
8223 /* No face changes, overlays etc. in sight, so just return a
8224 character from current_buffer. */
8225 unsigned char *p;
8226 ptrdiff_t stop;
8228 /* We moved to the next buffer position, so any info about
8229 previously seen overlays is no longer valid. */
8230 it->ignore_overlay_strings_at_pos_p = 0;
8232 /* Maybe run the redisplay end trigger hook. Performance note:
8233 This doesn't seem to cost measurable time. */
8234 if (it->redisplay_end_trigger_charpos
8235 && it->glyph_row
8236 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8237 run_redisplay_end_trigger_hook (it);
8239 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8240 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8241 stop)
8242 && next_element_from_composition (it))
8244 return 1;
8247 /* Get the next character, maybe multibyte. */
8248 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8249 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8250 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8251 else
8252 it->c = *p, it->len = 1;
8254 /* Record what we have and where it came from. */
8255 it->what = IT_CHARACTER;
8256 it->object = it->w->contents;
8257 it->position = it->current.pos;
8259 /* Normally we return the character found above, except when we
8260 really want to return an ellipsis for selective display. */
8261 if (it->selective)
8263 if (it->c == '\n')
8265 /* A value of selective > 0 means hide lines indented more
8266 than that number of columns. */
8267 if (it->selective > 0
8268 && IT_CHARPOS (*it) + 1 < ZV
8269 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8270 IT_BYTEPOS (*it) + 1,
8271 it->selective))
8273 success_p = next_element_from_ellipsis (it);
8274 it->dpvec_char_len = -1;
8277 else if (it->c == '\r' && it->selective == -1)
8279 /* A value of selective == -1 means that everything from the
8280 CR to the end of the line is invisible, with maybe an
8281 ellipsis displayed for it. */
8282 success_p = next_element_from_ellipsis (it);
8283 it->dpvec_char_len = -1;
8288 /* Value is zero if end of buffer reached. */
8289 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8290 return success_p;
8294 /* Run the redisplay end trigger hook for IT. */
8296 static void
8297 run_redisplay_end_trigger_hook (struct it *it)
8299 Lisp_Object args[3];
8301 /* IT->glyph_row should be non-null, i.e. we should be actually
8302 displaying something, or otherwise we should not run the hook. */
8303 eassert (it->glyph_row);
8305 /* Set up hook arguments. */
8306 args[0] = Qredisplay_end_trigger_functions;
8307 args[1] = it->window;
8308 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8309 it->redisplay_end_trigger_charpos = 0;
8311 /* Since we are *trying* to run these functions, don't try to run
8312 them again, even if they get an error. */
8313 wset_redisplay_end_trigger (it->w, Qnil);
8314 Frun_hook_with_args (3, args);
8316 /* Notice if it changed the face of the character we are on. */
8317 handle_face_prop (it);
8321 /* Deliver a composition display element. Unlike the other
8322 next_element_from_XXX, this function is not registered in the array
8323 get_next_element[]. It is called from next_element_from_buffer and
8324 next_element_from_string when necessary. */
8326 static int
8327 next_element_from_composition (struct it *it)
8329 it->what = IT_COMPOSITION;
8330 it->len = it->cmp_it.nbytes;
8331 if (STRINGP (it->string))
8333 if (it->c < 0)
8335 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8336 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8337 return 0;
8339 it->position = it->current.string_pos;
8340 it->object = it->string;
8341 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8342 IT_STRING_BYTEPOS (*it), it->string);
8344 else
8346 if (it->c < 0)
8348 IT_CHARPOS (*it) += it->cmp_it.nchars;
8349 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8350 if (it->bidi_p)
8352 if (it->bidi_it.new_paragraph)
8353 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8354 /* Resync the bidi iterator with IT's new position.
8355 FIXME: this doesn't support bidirectional text. */
8356 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8357 bidi_move_to_visually_next (&it->bidi_it);
8359 return 0;
8361 it->position = it->current.pos;
8362 it->object = it->w->contents;
8363 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8364 IT_BYTEPOS (*it), Qnil);
8366 return 1;
8371 /***********************************************************************
8372 Moving an iterator without producing glyphs
8373 ***********************************************************************/
8375 /* Check if iterator is at a position corresponding to a valid buffer
8376 position after some move_it_ call. */
8378 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8379 ((it)->method == GET_FROM_STRING \
8380 ? IT_STRING_CHARPOS (*it) == 0 \
8381 : 1)
8384 /* Move iterator IT to a specified buffer or X position within one
8385 line on the display without producing glyphs.
8387 OP should be a bit mask including some or all of these bits:
8388 MOVE_TO_X: Stop upon reaching x-position TO_X.
8389 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8390 Regardless of OP's value, stop upon reaching the end of the display line.
8392 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8393 This means, in particular, that TO_X includes window's horizontal
8394 scroll amount.
8396 The return value has several possible values that
8397 say what condition caused the scan to stop:
8399 MOVE_POS_MATCH_OR_ZV
8400 - when TO_POS or ZV was reached.
8402 MOVE_X_REACHED
8403 -when TO_X was reached before TO_POS or ZV were reached.
8405 MOVE_LINE_CONTINUED
8406 - when we reached the end of the display area and the line must
8407 be continued.
8409 MOVE_LINE_TRUNCATED
8410 - when we reached the end of the display area and the line is
8411 truncated.
8413 MOVE_NEWLINE_OR_CR
8414 - when we stopped at a line end, i.e. a newline or a CR and selective
8415 display is on. */
8417 static enum move_it_result
8418 move_it_in_display_line_to (struct it *it,
8419 ptrdiff_t to_charpos, int to_x,
8420 enum move_operation_enum op)
8422 enum move_it_result result = MOVE_UNDEFINED;
8423 struct glyph_row *saved_glyph_row;
8424 struct it wrap_it, atpos_it, atx_it, ppos_it;
8425 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8426 void *ppos_data = NULL;
8427 int may_wrap = 0;
8428 enum it_method prev_method = it->method;
8429 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8430 int saw_smaller_pos = prev_pos < to_charpos;
8432 /* Don't produce glyphs in produce_glyphs. */
8433 saved_glyph_row = it->glyph_row;
8434 it->glyph_row = NULL;
8436 /* Use wrap_it to save a copy of IT wherever a word wrap could
8437 occur. Use atpos_it to save a copy of IT at the desired buffer
8438 position, if found, so that we can scan ahead and check if the
8439 word later overshoots the window edge. Use atx_it similarly, for
8440 pixel positions. */
8441 wrap_it.sp = -1;
8442 atpos_it.sp = -1;
8443 atx_it.sp = -1;
8445 /* Use ppos_it under bidi reordering to save a copy of IT for the
8446 initial position. We restore that position in IT when we have
8447 scanned the entire display line without finding a match for
8448 TO_CHARPOS and all the character positions are greater than
8449 TO_CHARPOS. We then restart the scan from the initial position,
8450 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8451 the closest to TO_CHARPOS. */
8452 if (it->bidi_p)
8454 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8456 SAVE_IT (ppos_it, *it, ppos_data);
8457 closest_pos = IT_CHARPOS (*it);
8459 else
8460 closest_pos = ZV;
8463 #define BUFFER_POS_REACHED_P() \
8464 ((op & MOVE_TO_POS) != 0 \
8465 && BUFFERP (it->object) \
8466 && (IT_CHARPOS (*it) == to_charpos \
8467 || ((!it->bidi_p \
8468 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8469 && IT_CHARPOS (*it) > to_charpos) \
8470 || (it->what == IT_COMPOSITION \
8471 && ((IT_CHARPOS (*it) > to_charpos \
8472 && to_charpos >= it->cmp_it.charpos) \
8473 || (IT_CHARPOS (*it) < to_charpos \
8474 && to_charpos <= it->cmp_it.charpos)))) \
8475 && (it->method == GET_FROM_BUFFER \
8476 || (it->method == GET_FROM_DISPLAY_VECTOR \
8477 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8479 /* If there's a line-/wrap-prefix, handle it. */
8480 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8481 && it->current_y < it->last_visible_y)
8482 handle_line_prefix (it);
8484 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8485 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8487 while (1)
8489 int x, i, ascent = 0, descent = 0;
8491 /* Utility macro to reset an iterator with x, ascent, and descent. */
8492 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8493 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8494 (IT)->max_descent = descent)
8496 /* Stop if we move beyond TO_CHARPOS (after an image or a
8497 display string or stretch glyph). */
8498 if ((op & MOVE_TO_POS) != 0
8499 && BUFFERP (it->object)
8500 && it->method == GET_FROM_BUFFER
8501 && (((!it->bidi_p
8502 /* When the iterator is at base embedding level, we
8503 are guaranteed that characters are delivered for
8504 display in strictly increasing order of their
8505 buffer positions. */
8506 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8507 && IT_CHARPOS (*it) > to_charpos)
8508 || (it->bidi_p
8509 && (prev_method == GET_FROM_IMAGE
8510 || prev_method == GET_FROM_STRETCH
8511 || prev_method == GET_FROM_STRING)
8512 /* Passed TO_CHARPOS from left to right. */
8513 && ((prev_pos < to_charpos
8514 && IT_CHARPOS (*it) > to_charpos)
8515 /* Passed TO_CHARPOS from right to left. */
8516 || (prev_pos > to_charpos
8517 && IT_CHARPOS (*it) < to_charpos)))))
8519 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8521 result = MOVE_POS_MATCH_OR_ZV;
8522 break;
8524 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8525 /* If wrap_it is valid, the current position might be in a
8526 word that is wrapped. So, save the iterator in
8527 atpos_it and continue to see if wrapping happens. */
8528 SAVE_IT (atpos_it, *it, atpos_data);
8531 /* Stop when ZV reached.
8532 We used to stop here when TO_CHARPOS reached as well, but that is
8533 too soon if this glyph does not fit on this line. So we handle it
8534 explicitly below. */
8535 if (!get_next_display_element (it))
8537 result = MOVE_POS_MATCH_OR_ZV;
8538 break;
8541 if (it->line_wrap == TRUNCATE)
8543 if (BUFFER_POS_REACHED_P ())
8545 result = MOVE_POS_MATCH_OR_ZV;
8546 break;
8549 else
8551 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8553 if (IT_DISPLAYING_WHITESPACE (it))
8554 may_wrap = 1;
8555 else if (may_wrap)
8557 /* We have reached a glyph that follows one or more
8558 whitespace characters. If the position is
8559 already found, we are done. */
8560 if (atpos_it.sp >= 0)
8562 RESTORE_IT (it, &atpos_it, atpos_data);
8563 result = MOVE_POS_MATCH_OR_ZV;
8564 goto done;
8566 if (atx_it.sp >= 0)
8568 RESTORE_IT (it, &atx_it, atx_data);
8569 result = MOVE_X_REACHED;
8570 goto done;
8572 /* Otherwise, we can wrap here. */
8573 SAVE_IT (wrap_it, *it, wrap_data);
8574 may_wrap = 0;
8579 /* Remember the line height for the current line, in case
8580 the next element doesn't fit on the line. */
8581 ascent = it->max_ascent;
8582 descent = it->max_descent;
8584 /* The call to produce_glyphs will get the metrics of the
8585 display element IT is loaded with. Record the x-position
8586 before this display element, in case it doesn't fit on the
8587 line. */
8588 x = it->current_x;
8590 PRODUCE_GLYPHS (it);
8592 if (it->area != TEXT_AREA)
8594 prev_method = it->method;
8595 if (it->method == GET_FROM_BUFFER)
8596 prev_pos = IT_CHARPOS (*it);
8597 set_iterator_to_next (it, 1);
8598 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8599 SET_TEXT_POS (this_line_min_pos,
8600 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8601 if (it->bidi_p
8602 && (op & MOVE_TO_POS)
8603 && IT_CHARPOS (*it) > to_charpos
8604 && IT_CHARPOS (*it) < closest_pos)
8605 closest_pos = IT_CHARPOS (*it);
8606 continue;
8609 /* The number of glyphs we get back in IT->nglyphs will normally
8610 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8611 character on a terminal frame, or (iii) a line end. For the
8612 second case, IT->nglyphs - 1 padding glyphs will be present.
8613 (On X frames, there is only one glyph produced for a
8614 composite character.)
8616 The behavior implemented below means, for continuation lines,
8617 that as many spaces of a TAB as fit on the current line are
8618 displayed there. For terminal frames, as many glyphs of a
8619 multi-glyph character are displayed in the current line, too.
8620 This is what the old redisplay code did, and we keep it that
8621 way. Under X, the whole shape of a complex character must
8622 fit on the line or it will be completely displayed in the
8623 next line.
8625 Note that both for tabs and padding glyphs, all glyphs have
8626 the same width. */
8627 if (it->nglyphs)
8629 /* More than one glyph or glyph doesn't fit on line. All
8630 glyphs have the same width. */
8631 int single_glyph_width = it->pixel_width / it->nglyphs;
8632 int new_x;
8633 int x_before_this_char = x;
8634 int hpos_before_this_char = it->hpos;
8636 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8638 new_x = x + single_glyph_width;
8640 /* We want to leave anything reaching TO_X to the caller. */
8641 if ((op & MOVE_TO_X) && new_x > to_x)
8643 if (BUFFER_POS_REACHED_P ())
8645 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8646 goto buffer_pos_reached;
8647 if (atpos_it.sp < 0)
8649 SAVE_IT (atpos_it, *it, atpos_data);
8650 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8653 else
8655 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8657 it->current_x = x;
8658 result = MOVE_X_REACHED;
8659 break;
8661 if (atx_it.sp < 0)
8663 SAVE_IT (atx_it, *it, atx_data);
8664 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8669 if (/* Lines are continued. */
8670 it->line_wrap != TRUNCATE
8671 && (/* And glyph doesn't fit on the line. */
8672 new_x > it->last_visible_x
8673 /* Or it fits exactly and we're on a window
8674 system frame. */
8675 || (new_x == it->last_visible_x
8676 && FRAME_WINDOW_P (it->f)
8677 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8678 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8679 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8681 if (/* IT->hpos == 0 means the very first glyph
8682 doesn't fit on the line, e.g. a wide image. */
8683 it->hpos == 0
8684 || (new_x == it->last_visible_x
8685 && FRAME_WINDOW_P (it->f)))
8687 ++it->hpos;
8688 it->current_x = new_x;
8690 /* The character's last glyph just barely fits
8691 in this row. */
8692 if (i == it->nglyphs - 1)
8694 /* If this is the destination position,
8695 return a position *before* it in this row,
8696 now that we know it fits in this row. */
8697 if (BUFFER_POS_REACHED_P ())
8699 if (it->line_wrap != WORD_WRAP
8700 || wrap_it.sp < 0)
8702 it->hpos = hpos_before_this_char;
8703 it->current_x = x_before_this_char;
8704 result = MOVE_POS_MATCH_OR_ZV;
8705 break;
8707 if (it->line_wrap == WORD_WRAP
8708 && atpos_it.sp < 0)
8710 SAVE_IT (atpos_it, *it, atpos_data);
8711 atpos_it.current_x = x_before_this_char;
8712 atpos_it.hpos = hpos_before_this_char;
8716 prev_method = it->method;
8717 if (it->method == GET_FROM_BUFFER)
8718 prev_pos = IT_CHARPOS (*it);
8719 set_iterator_to_next (it, 1);
8720 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8721 SET_TEXT_POS (this_line_min_pos,
8722 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8723 /* On graphical terminals, newlines may
8724 "overflow" into the fringe if
8725 overflow-newline-into-fringe is non-nil.
8726 On text terminals, and on graphical
8727 terminals with no right margin, newlines
8728 may overflow into the last glyph on the
8729 display line.*/
8730 if (!FRAME_WINDOW_P (it->f)
8731 || ((it->bidi_p
8732 && it->bidi_it.paragraph_dir == R2L)
8733 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8734 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8735 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8737 if (!get_next_display_element (it))
8739 result = MOVE_POS_MATCH_OR_ZV;
8740 break;
8742 if (BUFFER_POS_REACHED_P ())
8744 if (ITERATOR_AT_END_OF_LINE_P (it))
8745 result = MOVE_POS_MATCH_OR_ZV;
8746 else
8747 result = MOVE_LINE_CONTINUED;
8748 break;
8750 if (ITERATOR_AT_END_OF_LINE_P (it)
8751 && (it->line_wrap != WORD_WRAP
8752 || wrap_it.sp < 0
8753 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8755 result = MOVE_NEWLINE_OR_CR;
8756 break;
8761 else
8762 IT_RESET_X_ASCENT_DESCENT (it);
8764 if (wrap_it.sp >= 0)
8766 RESTORE_IT (it, &wrap_it, wrap_data);
8767 atpos_it.sp = -1;
8768 atx_it.sp = -1;
8771 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8772 IT_CHARPOS (*it)));
8773 result = MOVE_LINE_CONTINUED;
8774 break;
8777 if (BUFFER_POS_REACHED_P ())
8779 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8780 goto buffer_pos_reached;
8781 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8783 SAVE_IT (atpos_it, *it, atpos_data);
8784 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8788 if (new_x > it->first_visible_x)
8790 /* Glyph is visible. Increment number of glyphs that
8791 would be displayed. */
8792 ++it->hpos;
8796 if (result != MOVE_UNDEFINED)
8797 break;
8799 else if (BUFFER_POS_REACHED_P ())
8801 buffer_pos_reached:
8802 IT_RESET_X_ASCENT_DESCENT (it);
8803 result = MOVE_POS_MATCH_OR_ZV;
8804 break;
8806 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8808 /* Stop when TO_X specified and reached. This check is
8809 necessary here because of lines consisting of a line end,
8810 only. The line end will not produce any glyphs and we
8811 would never get MOVE_X_REACHED. */
8812 eassert (it->nglyphs == 0);
8813 result = MOVE_X_REACHED;
8814 break;
8817 /* Is this a line end? If yes, we're done. */
8818 if (ITERATOR_AT_END_OF_LINE_P (it))
8820 /* If we are past TO_CHARPOS, but never saw any character
8821 positions smaller than TO_CHARPOS, return
8822 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8823 did. */
8824 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8826 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8828 if (closest_pos < ZV)
8830 RESTORE_IT (it, &ppos_it, ppos_data);
8831 /* Don't recurse if closest_pos is equal to
8832 to_charpos, since we have just tried that. */
8833 if (closest_pos != to_charpos)
8834 move_it_in_display_line_to (it, closest_pos, -1,
8835 MOVE_TO_POS);
8836 result = MOVE_POS_MATCH_OR_ZV;
8838 else
8839 goto buffer_pos_reached;
8841 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8842 && IT_CHARPOS (*it) > to_charpos)
8843 goto buffer_pos_reached;
8844 else
8845 result = MOVE_NEWLINE_OR_CR;
8847 else
8848 result = MOVE_NEWLINE_OR_CR;
8849 break;
8852 prev_method = it->method;
8853 if (it->method == GET_FROM_BUFFER)
8854 prev_pos = IT_CHARPOS (*it);
8855 /* The current display element has been consumed. Advance
8856 to the next. */
8857 set_iterator_to_next (it, 1);
8858 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8859 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8860 if (IT_CHARPOS (*it) < to_charpos)
8861 saw_smaller_pos = 1;
8862 if (it->bidi_p
8863 && (op & MOVE_TO_POS)
8864 && IT_CHARPOS (*it) >= to_charpos
8865 && IT_CHARPOS (*it) < closest_pos)
8866 closest_pos = IT_CHARPOS (*it);
8868 /* Stop if lines are truncated and IT's current x-position is
8869 past the right edge of the window now. */
8870 if (it->line_wrap == TRUNCATE
8871 && it->current_x >= it->last_visible_x)
8873 if (!FRAME_WINDOW_P (it->f)
8874 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8875 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8876 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8877 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8879 int at_eob_p = 0;
8881 if ((at_eob_p = !get_next_display_element (it))
8882 || BUFFER_POS_REACHED_P ()
8883 /* If we are past TO_CHARPOS, but never saw any
8884 character positions smaller than TO_CHARPOS,
8885 return MOVE_POS_MATCH_OR_ZV, like the
8886 unidirectional display did. */
8887 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8888 && !saw_smaller_pos
8889 && IT_CHARPOS (*it) > to_charpos))
8891 if (it->bidi_p
8892 && !BUFFER_POS_REACHED_P ()
8893 && !at_eob_p && closest_pos < ZV)
8895 RESTORE_IT (it, &ppos_it, ppos_data);
8896 if (closest_pos != to_charpos)
8897 move_it_in_display_line_to (it, closest_pos, -1,
8898 MOVE_TO_POS);
8900 result = MOVE_POS_MATCH_OR_ZV;
8901 break;
8903 if (ITERATOR_AT_END_OF_LINE_P (it))
8905 result = MOVE_NEWLINE_OR_CR;
8906 break;
8909 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8910 && !saw_smaller_pos
8911 && IT_CHARPOS (*it) > to_charpos)
8913 if (closest_pos < ZV)
8915 RESTORE_IT (it, &ppos_it, ppos_data);
8916 if (closest_pos != to_charpos)
8917 move_it_in_display_line_to (it, closest_pos, -1,
8918 MOVE_TO_POS);
8920 result = MOVE_POS_MATCH_OR_ZV;
8921 break;
8923 result = MOVE_LINE_TRUNCATED;
8924 break;
8926 #undef IT_RESET_X_ASCENT_DESCENT
8929 #undef BUFFER_POS_REACHED_P
8931 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8932 restore the saved iterator. */
8933 if (atpos_it.sp >= 0)
8934 RESTORE_IT (it, &atpos_it, atpos_data);
8935 else if (atx_it.sp >= 0)
8936 RESTORE_IT (it, &atx_it, atx_data);
8938 done:
8940 if (atpos_data)
8941 bidi_unshelve_cache (atpos_data, 1);
8942 if (atx_data)
8943 bidi_unshelve_cache (atx_data, 1);
8944 if (wrap_data)
8945 bidi_unshelve_cache (wrap_data, 1);
8946 if (ppos_data)
8947 bidi_unshelve_cache (ppos_data, 1);
8949 /* Restore the iterator settings altered at the beginning of this
8950 function. */
8951 it->glyph_row = saved_glyph_row;
8952 return result;
8955 /* For external use. */
8956 void
8957 move_it_in_display_line (struct it *it,
8958 ptrdiff_t to_charpos, int to_x,
8959 enum move_operation_enum op)
8961 if (it->line_wrap == WORD_WRAP
8962 && (op & MOVE_TO_X))
8964 struct it save_it;
8965 void *save_data = NULL;
8966 int skip;
8968 SAVE_IT (save_it, *it, save_data);
8969 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8970 /* When word-wrap is on, TO_X may lie past the end
8971 of a wrapped line. Then it->current is the
8972 character on the next line, so backtrack to the
8973 space before the wrap point. */
8974 if (skip == MOVE_LINE_CONTINUED)
8976 int prev_x = max (it->current_x - 1, 0);
8977 RESTORE_IT (it, &save_it, save_data);
8978 move_it_in_display_line_to
8979 (it, -1, prev_x, MOVE_TO_X);
8981 else
8982 bidi_unshelve_cache (save_data, 1);
8984 else
8985 move_it_in_display_line_to (it, to_charpos, to_x, op);
8989 /* Move IT forward until it satisfies one or more of the criteria in
8990 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8992 OP is a bit-mask that specifies where to stop, and in particular,
8993 which of those four position arguments makes a difference. See the
8994 description of enum move_operation_enum.
8996 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8997 screen line, this function will set IT to the next position that is
8998 displayed to the right of TO_CHARPOS on the screen.
9000 Return the maximum pixel length of any line scanned but never more
9001 than it.last_visible_x. */
9004 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9006 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9007 int line_height, line_start_x = 0, reached = 0;
9008 int max_current_x = 0;
9009 void *backup_data = NULL;
9011 for (;;)
9013 if (op & MOVE_TO_VPOS)
9015 /* If no TO_CHARPOS and no TO_X specified, stop at the
9016 start of the line TO_VPOS. */
9017 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9019 if (it->vpos == to_vpos)
9021 reached = 1;
9022 break;
9024 else
9025 skip = move_it_in_display_line_to (it, -1, -1, 0);
9027 else
9029 /* TO_VPOS >= 0 means stop at TO_X in the line at
9030 TO_VPOS, or at TO_POS, whichever comes first. */
9031 if (it->vpos == to_vpos)
9033 reached = 2;
9034 break;
9037 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9039 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9041 reached = 3;
9042 break;
9044 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9046 /* We have reached TO_X but not in the line we want. */
9047 skip = move_it_in_display_line_to (it, to_charpos,
9048 -1, MOVE_TO_POS);
9049 if (skip == MOVE_POS_MATCH_OR_ZV)
9051 reached = 4;
9052 break;
9057 else if (op & MOVE_TO_Y)
9059 struct it it_backup;
9061 if (it->line_wrap == WORD_WRAP)
9062 SAVE_IT (it_backup, *it, backup_data);
9064 /* TO_Y specified means stop at TO_X in the line containing
9065 TO_Y---or at TO_CHARPOS if this is reached first. The
9066 problem is that we can't really tell whether the line
9067 contains TO_Y before we have completely scanned it, and
9068 this may skip past TO_X. What we do is to first scan to
9069 TO_X.
9071 If TO_X is not specified, use a TO_X of zero. The reason
9072 is to make the outcome of this function more predictable.
9073 If we didn't use TO_X == 0, we would stop at the end of
9074 the line which is probably not what a caller would expect
9075 to happen. */
9076 skip = move_it_in_display_line_to
9077 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9078 (MOVE_TO_X | (op & MOVE_TO_POS)));
9080 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9081 if (skip == MOVE_POS_MATCH_OR_ZV)
9082 reached = 5;
9083 else if (skip == MOVE_X_REACHED)
9085 /* If TO_X was reached, we want to know whether TO_Y is
9086 in the line. We know this is the case if the already
9087 scanned glyphs make the line tall enough. Otherwise,
9088 we must check by scanning the rest of the line. */
9089 line_height = it->max_ascent + it->max_descent;
9090 if (to_y >= it->current_y
9091 && to_y < it->current_y + line_height)
9093 reached = 6;
9094 break;
9096 SAVE_IT (it_backup, *it, backup_data);
9097 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9098 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9099 op & MOVE_TO_POS);
9100 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9101 line_height = it->max_ascent + it->max_descent;
9102 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9104 if (to_y >= it->current_y
9105 && to_y < it->current_y + line_height)
9107 /* If TO_Y is in this line and TO_X was reached
9108 above, we scanned too far. We have to restore
9109 IT's settings to the ones before skipping. But
9110 keep the more accurate values of max_ascent and
9111 max_descent we've found while skipping the rest
9112 of the line, for the sake of callers, such as
9113 pos_visible_p, that need to know the line
9114 height. */
9115 int max_ascent = it->max_ascent;
9116 int max_descent = it->max_descent;
9118 RESTORE_IT (it, &it_backup, backup_data);
9119 it->max_ascent = max_ascent;
9120 it->max_descent = max_descent;
9121 reached = 6;
9123 else
9125 skip = skip2;
9126 if (skip == MOVE_POS_MATCH_OR_ZV)
9127 reached = 7;
9130 else
9132 /* Check whether TO_Y is in this line. */
9133 line_height = it->max_ascent + it->max_descent;
9134 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9136 if (to_y >= it->current_y
9137 && to_y < it->current_y + line_height)
9139 if (to_y > it->current_y)
9140 max_current_x = max (it->current_x, max_current_x);
9142 /* When word-wrap is on, TO_X may lie past the end
9143 of a wrapped line. Then it->current is the
9144 character on the next line, so backtrack to the
9145 space before the wrap point. */
9146 if (skip == MOVE_LINE_CONTINUED
9147 && it->line_wrap == WORD_WRAP)
9149 int prev_x = max (it->current_x - 1, 0);
9150 RESTORE_IT (it, &it_backup, backup_data);
9151 skip = move_it_in_display_line_to
9152 (it, -1, prev_x, MOVE_TO_X);
9155 reached = 6;
9159 if (reached)
9161 max_current_x = max (it->current_x, max_current_x);
9162 break;
9165 else if (BUFFERP (it->object)
9166 && (it->method == GET_FROM_BUFFER
9167 || it->method == GET_FROM_STRETCH)
9168 && IT_CHARPOS (*it) >= to_charpos
9169 /* Under bidi iteration, a call to set_iterator_to_next
9170 can scan far beyond to_charpos if the initial
9171 portion of the next line needs to be reordered. In
9172 that case, give move_it_in_display_line_to another
9173 chance below. */
9174 && !(it->bidi_p
9175 && it->bidi_it.scan_dir == -1))
9176 skip = MOVE_POS_MATCH_OR_ZV;
9177 else
9178 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9180 switch (skip)
9182 case MOVE_POS_MATCH_OR_ZV:
9183 max_current_x = max (it->current_x, max_current_x);
9184 reached = 8;
9185 goto out;
9187 case MOVE_NEWLINE_OR_CR:
9188 max_current_x = max (it->current_x, max_current_x);
9189 set_iterator_to_next (it, 1);
9190 it->continuation_lines_width = 0;
9191 break;
9193 case MOVE_LINE_TRUNCATED:
9194 max_current_x = it->last_visible_x;
9195 it->continuation_lines_width = 0;
9196 reseat_at_next_visible_line_start (it, 0);
9197 if ((op & MOVE_TO_POS) != 0
9198 && IT_CHARPOS (*it) > to_charpos)
9200 reached = 9;
9201 goto out;
9203 break;
9205 case MOVE_LINE_CONTINUED:
9206 max_current_x = it->last_visible_x;
9207 /* For continued lines ending in a tab, some of the glyphs
9208 associated with the tab are displayed on the current
9209 line. Since it->current_x does not include these glyphs,
9210 we use it->last_visible_x instead. */
9211 if (it->c == '\t')
9213 it->continuation_lines_width += it->last_visible_x;
9214 /* When moving by vpos, ensure that the iterator really
9215 advances to the next line (bug#847, bug#969). Fixme:
9216 do we need to do this in other circumstances? */
9217 if (it->current_x != it->last_visible_x
9218 && (op & MOVE_TO_VPOS)
9219 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9221 line_start_x = it->current_x + it->pixel_width
9222 - it->last_visible_x;
9223 if (FRAME_WINDOW_P (it->f))
9225 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9226 struct font *face_font = face->font;
9228 /* When display_line produces a continued line
9229 that ends in a TAB, it skips a tab stop that
9230 is closer than the font's space character
9231 width (see x_produce_glyphs where it produces
9232 the stretch glyph which represents a TAB).
9233 We need to reproduce the same logic here. */
9234 eassert (face_font);
9235 if (face_font)
9237 if (line_start_x < face_font->space_width)
9238 line_start_x
9239 += it->tab_width * face_font->space_width;
9242 set_iterator_to_next (it, 0);
9245 else
9246 it->continuation_lines_width += it->current_x;
9247 break;
9249 default:
9250 emacs_abort ();
9253 /* Reset/increment for the next run. */
9254 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9255 it->current_x = line_start_x;
9256 line_start_x = 0;
9257 it->hpos = 0;
9258 it->current_y += it->max_ascent + it->max_descent;
9259 ++it->vpos;
9260 last_height = it->max_ascent + it->max_descent;
9261 it->max_ascent = it->max_descent = 0;
9264 out:
9266 /* On text terminals, we may stop at the end of a line in the middle
9267 of a multi-character glyph. If the glyph itself is continued,
9268 i.e. it is actually displayed on the next line, don't treat this
9269 stopping point as valid; move to the next line instead (unless
9270 that brings us offscreen). */
9271 if (!FRAME_WINDOW_P (it->f)
9272 && op & MOVE_TO_POS
9273 && IT_CHARPOS (*it) == to_charpos
9274 && it->what == IT_CHARACTER
9275 && it->nglyphs > 1
9276 && it->line_wrap == WINDOW_WRAP
9277 && it->current_x == it->last_visible_x - 1
9278 && it->c != '\n'
9279 && it->c != '\t'
9280 && it->vpos < it->w->window_end_vpos)
9282 it->continuation_lines_width += it->current_x;
9283 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9284 it->current_y += it->max_ascent + it->max_descent;
9285 ++it->vpos;
9286 last_height = it->max_ascent + it->max_descent;
9289 if (backup_data)
9290 bidi_unshelve_cache (backup_data, 1);
9292 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9294 return max_current_x;
9298 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9300 If DY > 0, move IT backward at least that many pixels. DY = 0
9301 means move IT backward to the preceding line start or BEGV. This
9302 function may move over more than DY pixels if IT->current_y - DY
9303 ends up in the middle of a line; in this case IT->current_y will be
9304 set to the top of the line moved to. */
9306 void
9307 move_it_vertically_backward (struct it *it, int dy)
9309 int nlines, h;
9310 struct it it2, it3;
9311 void *it2data = NULL, *it3data = NULL;
9312 ptrdiff_t start_pos;
9313 int nchars_per_row
9314 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9315 ptrdiff_t pos_limit;
9317 move_further_back:
9318 eassert (dy >= 0);
9320 start_pos = IT_CHARPOS (*it);
9322 /* Estimate how many newlines we must move back. */
9323 nlines = max (1, dy / default_line_pixel_height (it->w));
9324 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9325 pos_limit = BEGV;
9326 else
9327 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9329 /* Set the iterator's position that many lines back. But don't go
9330 back more than NLINES full screen lines -- this wins a day with
9331 buffers which have very long lines. */
9332 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9333 back_to_previous_visible_line_start (it);
9335 /* Reseat the iterator here. When moving backward, we don't want
9336 reseat to skip forward over invisible text, set up the iterator
9337 to deliver from overlay strings at the new position etc. So,
9338 use reseat_1 here. */
9339 reseat_1 (it, it->current.pos, 1);
9341 /* We are now surely at a line start. */
9342 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9343 reordering is in effect. */
9344 it->continuation_lines_width = 0;
9346 /* Move forward and see what y-distance we moved. First move to the
9347 start of the next line so that we get its height. We need this
9348 height to be able to tell whether we reached the specified
9349 y-distance. */
9350 SAVE_IT (it2, *it, it2data);
9351 it2.max_ascent = it2.max_descent = 0;
9354 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9355 MOVE_TO_POS | MOVE_TO_VPOS);
9357 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9358 /* If we are in a display string which starts at START_POS,
9359 and that display string includes a newline, and we are
9360 right after that newline (i.e. at the beginning of a
9361 display line), exit the loop, because otherwise we will
9362 infloop, since move_it_to will see that it is already at
9363 START_POS and will not move. */
9364 || (it2.method == GET_FROM_STRING
9365 && IT_CHARPOS (it2) == start_pos
9366 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9367 eassert (IT_CHARPOS (*it) >= BEGV);
9368 SAVE_IT (it3, it2, it3data);
9370 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9371 eassert (IT_CHARPOS (*it) >= BEGV);
9372 /* H is the actual vertical distance from the position in *IT
9373 and the starting position. */
9374 h = it2.current_y - it->current_y;
9375 /* NLINES is the distance in number of lines. */
9376 nlines = it2.vpos - it->vpos;
9378 /* Correct IT's y and vpos position
9379 so that they are relative to the starting point. */
9380 it->vpos -= nlines;
9381 it->current_y -= h;
9383 if (dy == 0)
9385 /* DY == 0 means move to the start of the screen line. The
9386 value of nlines is > 0 if continuation lines were involved,
9387 or if the original IT position was at start of a line. */
9388 RESTORE_IT (it, it, it2data);
9389 if (nlines > 0)
9390 move_it_by_lines (it, nlines);
9391 /* The above code moves us to some position NLINES down,
9392 usually to its first glyph (leftmost in an L2R line), but
9393 that's not necessarily the start of the line, under bidi
9394 reordering. We want to get to the character position
9395 that is immediately after the newline of the previous
9396 line. */
9397 if (it->bidi_p
9398 && !it->continuation_lines_width
9399 && !STRINGP (it->string)
9400 && IT_CHARPOS (*it) > BEGV
9401 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9403 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9405 DEC_BOTH (cp, bp);
9406 cp = find_newline_no_quit (cp, bp, -1, NULL);
9407 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9409 bidi_unshelve_cache (it3data, 1);
9411 else
9413 /* The y-position we try to reach, relative to *IT.
9414 Note that H has been subtracted in front of the if-statement. */
9415 int target_y = it->current_y + h - dy;
9416 int y0 = it3.current_y;
9417 int y1;
9418 int line_height;
9420 RESTORE_IT (&it3, &it3, it3data);
9421 y1 = line_bottom_y (&it3);
9422 line_height = y1 - y0;
9423 RESTORE_IT (it, it, it2data);
9424 /* If we did not reach target_y, try to move further backward if
9425 we can. If we moved too far backward, try to move forward. */
9426 if (target_y < it->current_y
9427 /* This is heuristic. In a window that's 3 lines high, with
9428 a line height of 13 pixels each, recentering with point
9429 on the bottom line will try to move -39/2 = 19 pixels
9430 backward. Try to avoid moving into the first line. */
9431 && (it->current_y - target_y
9432 > min (window_box_height (it->w), line_height * 2 / 3))
9433 && IT_CHARPOS (*it) > BEGV)
9435 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9436 target_y - it->current_y));
9437 dy = it->current_y - target_y;
9438 goto move_further_back;
9440 else if (target_y >= it->current_y + line_height
9441 && IT_CHARPOS (*it) < ZV)
9443 /* Should move forward by at least one line, maybe more.
9445 Note: Calling move_it_by_lines can be expensive on
9446 terminal frames, where compute_motion is used (via
9447 vmotion) to do the job, when there are very long lines
9448 and truncate-lines is nil. That's the reason for
9449 treating terminal frames specially here. */
9451 if (!FRAME_WINDOW_P (it->f))
9452 move_it_vertically (it, target_y - (it->current_y + line_height));
9453 else
9457 move_it_by_lines (it, 1);
9459 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9466 /* Move IT by a specified amount of pixel lines DY. DY negative means
9467 move backwards. DY = 0 means move to start of screen line. At the
9468 end, IT will be on the start of a screen line. */
9470 void
9471 move_it_vertically (struct it *it, int dy)
9473 if (dy <= 0)
9474 move_it_vertically_backward (it, -dy);
9475 else
9477 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9478 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9479 MOVE_TO_POS | MOVE_TO_Y);
9480 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9482 /* If buffer ends in ZV without a newline, move to the start of
9483 the line to satisfy the post-condition. */
9484 if (IT_CHARPOS (*it) == ZV
9485 && ZV > BEGV
9486 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9487 move_it_by_lines (it, 0);
9492 /* Move iterator IT past the end of the text line it is in. */
9494 void
9495 move_it_past_eol (struct it *it)
9497 enum move_it_result rc;
9499 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9500 if (rc == MOVE_NEWLINE_OR_CR)
9501 set_iterator_to_next (it, 0);
9505 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9506 negative means move up. DVPOS == 0 means move to the start of the
9507 screen line.
9509 Optimization idea: If we would know that IT->f doesn't use
9510 a face with proportional font, we could be faster for
9511 truncate-lines nil. */
9513 void
9514 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9517 /* The commented-out optimization uses vmotion on terminals. This
9518 gives bad results, because elements like it->what, on which
9519 callers such as pos_visible_p rely, aren't updated. */
9520 /* struct position pos;
9521 if (!FRAME_WINDOW_P (it->f))
9523 struct text_pos textpos;
9525 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9526 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9527 reseat (it, textpos, 1);
9528 it->vpos += pos.vpos;
9529 it->current_y += pos.vpos;
9531 else */
9533 if (dvpos == 0)
9535 /* DVPOS == 0 means move to the start of the screen line. */
9536 move_it_vertically_backward (it, 0);
9537 /* Let next call to line_bottom_y calculate real line height. */
9538 last_height = 0;
9540 else if (dvpos > 0)
9542 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9543 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9545 /* Only move to the next buffer position if we ended up in a
9546 string from display property, not in an overlay string
9547 (before-string or after-string). That is because the
9548 latter don't conceal the underlying buffer position, so
9549 we can ask to move the iterator to the exact position we
9550 are interested in. Note that, even if we are already at
9551 IT_CHARPOS (*it), the call below is not a no-op, as it
9552 will detect that we are at the end of the string, pop the
9553 iterator, and compute it->current_x and it->hpos
9554 correctly. */
9555 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9556 -1, -1, -1, MOVE_TO_POS);
9559 else
9561 struct it it2;
9562 void *it2data = NULL;
9563 ptrdiff_t start_charpos, i;
9564 int nchars_per_row
9565 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9566 bool hit_pos_limit = false;
9567 ptrdiff_t pos_limit;
9569 /* Start at the beginning of the screen line containing IT's
9570 position. This may actually move vertically backwards,
9571 in case of overlays, so adjust dvpos accordingly. */
9572 dvpos += it->vpos;
9573 move_it_vertically_backward (it, 0);
9574 dvpos -= it->vpos;
9576 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9577 screen lines, and reseat the iterator there. */
9578 start_charpos = IT_CHARPOS (*it);
9579 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9580 pos_limit = BEGV;
9581 else
9582 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9584 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9585 back_to_previous_visible_line_start (it);
9586 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9587 hit_pos_limit = true;
9588 reseat (it, it->current.pos, 1);
9590 /* Move further back if we end up in a string or an image. */
9591 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9593 /* First try to move to start of display line. */
9594 dvpos += it->vpos;
9595 move_it_vertically_backward (it, 0);
9596 dvpos -= it->vpos;
9597 if (IT_POS_VALID_AFTER_MOVE_P (it))
9598 break;
9599 /* If start of line is still in string or image,
9600 move further back. */
9601 back_to_previous_visible_line_start (it);
9602 reseat (it, it->current.pos, 1);
9603 dvpos--;
9606 it->current_x = it->hpos = 0;
9608 /* Above call may have moved too far if continuation lines
9609 are involved. Scan forward and see if it did. */
9610 SAVE_IT (it2, *it, it2data);
9611 it2.vpos = it2.current_y = 0;
9612 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9613 it->vpos -= it2.vpos;
9614 it->current_y -= it2.current_y;
9615 it->current_x = it->hpos = 0;
9617 /* If we moved too far back, move IT some lines forward. */
9618 if (it2.vpos > -dvpos)
9620 int delta = it2.vpos + dvpos;
9622 RESTORE_IT (&it2, &it2, it2data);
9623 SAVE_IT (it2, *it, it2data);
9624 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9625 /* Move back again if we got too far ahead. */
9626 if (IT_CHARPOS (*it) >= start_charpos)
9627 RESTORE_IT (it, &it2, it2data);
9628 else
9629 bidi_unshelve_cache (it2data, 1);
9631 else if (hit_pos_limit && pos_limit > BEGV
9632 && dvpos < 0 && it2.vpos < -dvpos)
9634 /* If we hit the limit, but still didn't make it far enough
9635 back, that means there's a display string with a newline
9636 covering a large chunk of text, and that caused
9637 back_to_previous_visible_line_start try to go too far.
9638 Punish those who commit such atrocities by going back
9639 until we've reached DVPOS, after lifting the limit, which
9640 could make it slow for very long lines. "If it hurts,
9641 don't do that!" */
9642 dvpos += it2.vpos;
9643 RESTORE_IT (it, it, it2data);
9644 for (i = -dvpos; i > 0; --i)
9646 back_to_previous_visible_line_start (it);
9647 it->vpos--;
9649 reseat_1 (it, it->current.pos, 1);
9651 else
9652 RESTORE_IT (it, it, it2data);
9656 /* Return true if IT points into the middle of a display vector. */
9658 bool
9659 in_display_vector_p (struct it *it)
9661 return (it->method == GET_FROM_DISPLAY_VECTOR
9662 && it->current.dpvec_index > 0
9663 && it->dpvec + it->current.dpvec_index != it->dpend);
9666 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9667 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9668 WINDOW must be a live window and defaults to the selected one. The
9669 return value is a cons of the maximum pixel-width of any text line and
9670 the maximum pixel-height of all text lines.
9672 The optional argument FROM, if non-nil, specifies the first text
9673 position and defaults to the minimum accessible position of the buffer.
9674 If FROM is t, use the minimum accessible position that is not a newline
9675 character. TO, if non-nil, specifies the last text position and
9676 defaults to the maximum accessible position of the buffer. If TO is t,
9677 use the maximum accessible position that is not a newline character.
9679 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9680 width that can be returned. X-LIMIT nil or omitted, means to use the
9681 pixel-width of WINDOW's body; use this if you do not intend to change
9682 the width of WINDOW. Use the maximum width WINDOW may assume if you
9683 intend to change WINDOW's width. In any case, text whose x-coordinate
9684 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9685 can take some time, it's always a good idea to make this argument as
9686 small as possible; in particular, if the buffer contains long lines that
9687 shall be truncated anyway.
9689 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9690 height that can be returned. Text lines whose y-coordinate is beyond
9691 Y-LIMIT are ignored. Since calculating the text height of a large
9692 buffer can take some time, it makes sense to specify this argument if
9693 the size of the buffer is unknown.
9695 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9696 include the height of the mode- or header-line of WINDOW in the return
9697 value. If it is either the symbol `mode-line' or `header-line', include
9698 only the height of that line, if present, in the return value. If t,
9699 include the height of both, if present, in the return value. */)
9700 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9701 Lisp_Object mode_and_header_line)
9703 struct window *w = decode_live_window (window);
9704 Lisp_Object buf;
9705 struct buffer *b;
9706 struct it it;
9707 struct buffer *old_buffer = NULL;
9708 ptrdiff_t start, end, pos;
9709 struct text_pos startp;
9710 void *itdata = NULL;
9711 int c, max_y = -1, x = 0, y = 0;
9713 buf = w->contents;
9714 CHECK_BUFFER (buf);
9715 b = XBUFFER (buf);
9717 if (b != current_buffer)
9719 old_buffer = current_buffer;
9720 set_buffer_internal (b);
9723 if (NILP (from))
9724 start = BEGV;
9725 else if (EQ (from, Qt))
9727 start = pos = BEGV;
9728 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9729 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9730 start = pos;
9731 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9732 start = pos;
9734 else
9736 CHECK_NUMBER_COERCE_MARKER (from);
9737 start = min (max (XINT (from), BEGV), ZV);
9740 if (NILP (to))
9741 end = ZV;
9742 else if (EQ (to, Qt))
9744 end = pos = ZV;
9745 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9746 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9747 end = pos;
9748 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9749 end = pos;
9751 else
9753 CHECK_NUMBER_COERCE_MARKER (to);
9754 end = max (start, min (XINT (to), ZV));
9757 if (!NILP (y_limit))
9759 CHECK_NUMBER (y_limit);
9760 max_y = min (XINT (y_limit), INT_MAX);
9763 itdata = bidi_shelve_cache ();
9764 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9765 start_display (&it, w, startp);
9767 if (NILP (x_limit))
9768 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9769 else
9771 CHECK_NUMBER (x_limit);
9772 it.last_visible_x = min (XINT (x_limit), INFINITY);
9773 /* Actually, we never want move_it_to stop at to_x. But to make
9774 sure that move_it_in_display_line_to always moves far enough,
9775 we set it to INT_MAX and specify MOVE_TO_X. */
9776 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9777 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9780 y = it.current_y + it.max_ascent + it.max_descent;
9782 if (!EQ (mode_and_header_line, Qheader_line)
9783 && !EQ (mode_and_header_line, Qt))
9784 /* Do not count the header-line which was counted automatically by
9785 start_display. */
9786 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9788 if (EQ (mode_and_header_line, Qmode_line)
9789 || EQ (mode_and_header_line, Qt))
9790 /* Do count the mode-line which is not included automatically by
9791 start_display. */
9792 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9794 bidi_unshelve_cache (itdata, 0);
9796 if (old_buffer)
9797 set_buffer_internal (old_buffer);
9799 return Fcons (make_number (x), make_number (y));
9802 /***********************************************************************
9803 Messages
9804 ***********************************************************************/
9807 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9808 to *Messages*. */
9810 void
9811 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9813 Lisp_Object args[3];
9814 Lisp_Object msg, fmt;
9815 char *buffer;
9816 ptrdiff_t len;
9817 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9818 USE_SAFE_ALLOCA;
9820 fmt = msg = Qnil;
9821 GCPRO4 (fmt, msg, arg1, arg2);
9823 args[0] = fmt = build_string (format);
9824 args[1] = arg1;
9825 args[2] = arg2;
9826 msg = Fformat (3, args);
9828 len = SBYTES (msg) + 1;
9829 buffer = SAFE_ALLOCA (len);
9830 memcpy (buffer, SDATA (msg), len);
9832 message_dolog (buffer, len - 1, 1, 0);
9833 SAFE_FREE ();
9835 UNGCPRO;
9839 /* Output a newline in the *Messages* buffer if "needs" one. */
9841 void
9842 message_log_maybe_newline (void)
9844 if (message_log_need_newline)
9845 message_dolog ("", 0, 1, 0);
9849 /* Add a string M of length NBYTES to the message log, optionally
9850 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9851 true, means interpret the contents of M as multibyte. This
9852 function calls low-level routines in order to bypass text property
9853 hooks, etc. which might not be safe to run.
9855 This may GC (insert may run before/after change hooks),
9856 so the buffer M must NOT point to a Lisp string. */
9858 void
9859 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9861 const unsigned char *msg = (const unsigned char *) m;
9863 if (!NILP (Vmemory_full))
9864 return;
9866 if (!NILP (Vmessage_log_max))
9868 struct buffer *oldbuf;
9869 Lisp_Object oldpoint, oldbegv, oldzv;
9870 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9871 ptrdiff_t point_at_end = 0;
9872 ptrdiff_t zv_at_end = 0;
9873 Lisp_Object old_deactivate_mark;
9874 struct gcpro gcpro1;
9876 old_deactivate_mark = Vdeactivate_mark;
9877 oldbuf = current_buffer;
9879 /* Ensure the Messages buffer exists, and switch to it.
9880 If we created it, set the major-mode. */
9882 int newbuffer = 0;
9883 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9885 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9887 if (newbuffer
9888 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9889 call0 (intern ("messages-buffer-mode"));
9892 bset_undo_list (current_buffer, Qt);
9893 bset_cache_long_scans (current_buffer, Qnil);
9895 oldpoint = message_dolog_marker1;
9896 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9897 oldbegv = message_dolog_marker2;
9898 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9899 oldzv = message_dolog_marker3;
9900 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9901 GCPRO1 (old_deactivate_mark);
9903 if (PT == Z)
9904 point_at_end = 1;
9905 if (ZV == Z)
9906 zv_at_end = 1;
9908 BEGV = BEG;
9909 BEGV_BYTE = BEG_BYTE;
9910 ZV = Z;
9911 ZV_BYTE = Z_BYTE;
9912 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9914 /* Insert the string--maybe converting multibyte to single byte
9915 or vice versa, so that all the text fits the buffer. */
9916 if (multibyte
9917 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9919 ptrdiff_t i;
9920 int c, char_bytes;
9921 char work[1];
9923 /* Convert a multibyte string to single-byte
9924 for the *Message* buffer. */
9925 for (i = 0; i < nbytes; i += char_bytes)
9927 c = string_char_and_length (msg + i, &char_bytes);
9928 work[0] = CHAR_TO_BYTE8 (c);
9929 insert_1_both (work, 1, 1, 1, 0, 0);
9932 else if (! multibyte
9933 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9935 ptrdiff_t i;
9936 int c, char_bytes;
9937 unsigned char str[MAX_MULTIBYTE_LENGTH];
9938 /* Convert a single-byte string to multibyte
9939 for the *Message* buffer. */
9940 for (i = 0; i < nbytes; i++)
9942 c = msg[i];
9943 MAKE_CHAR_MULTIBYTE (c);
9944 char_bytes = CHAR_STRING (c, str);
9945 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9948 else if (nbytes)
9949 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9951 if (nlflag)
9953 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9954 printmax_t dups;
9956 insert_1_both ("\n", 1, 1, 1, 0, 0);
9958 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9959 this_bol = PT;
9960 this_bol_byte = PT_BYTE;
9962 /* See if this line duplicates the previous one.
9963 If so, combine duplicates. */
9964 if (this_bol > BEG)
9966 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9967 prev_bol = PT;
9968 prev_bol_byte = PT_BYTE;
9970 dups = message_log_check_duplicate (prev_bol_byte,
9971 this_bol_byte);
9972 if (dups)
9974 del_range_both (prev_bol, prev_bol_byte,
9975 this_bol, this_bol_byte, 0);
9976 if (dups > 1)
9978 char dupstr[sizeof " [ times]"
9979 + INT_STRLEN_BOUND (printmax_t)];
9981 /* If you change this format, don't forget to also
9982 change message_log_check_duplicate. */
9983 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9984 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9985 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9990 /* If we have more than the desired maximum number of lines
9991 in the *Messages* buffer now, delete the oldest ones.
9992 This is safe because we don't have undo in this buffer. */
9994 if (NATNUMP (Vmessage_log_max))
9996 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9997 -XFASTINT (Vmessage_log_max) - 1, 0);
9998 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
10001 BEGV = marker_position (oldbegv);
10002 BEGV_BYTE = marker_byte_position (oldbegv);
10004 if (zv_at_end)
10006 ZV = Z;
10007 ZV_BYTE = Z_BYTE;
10009 else
10011 ZV = marker_position (oldzv);
10012 ZV_BYTE = marker_byte_position (oldzv);
10015 if (point_at_end)
10016 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10017 else
10018 /* We can't do Fgoto_char (oldpoint) because it will run some
10019 Lisp code. */
10020 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10021 marker_byte_position (oldpoint));
10023 UNGCPRO;
10024 unchain_marker (XMARKER (oldpoint));
10025 unchain_marker (XMARKER (oldbegv));
10026 unchain_marker (XMARKER (oldzv));
10028 /* We called insert_1_both above with its 5th argument (PREPARE)
10029 zero, which prevents insert_1_both from calling
10030 prepare_to_modify_buffer, which in turns prevents us from
10031 incrementing windows_or_buffers_changed even if *Messages* is
10032 shown in some window. So we must manually set
10033 windows_or_buffers_changed here to make up for that. */
10034 windows_or_buffers_changed = old_windows_or_buffers_changed;
10035 bset_redisplay (current_buffer);
10037 set_buffer_internal (oldbuf);
10039 message_log_need_newline = !nlflag;
10040 Vdeactivate_mark = old_deactivate_mark;
10045 /* We are at the end of the buffer after just having inserted a newline.
10046 (Note: We depend on the fact we won't be crossing the gap.)
10047 Check to see if the most recent message looks a lot like the previous one.
10048 Return 0 if different, 1 if the new one should just replace it, or a
10049 value N > 1 if we should also append " [N times]". */
10051 static intmax_t
10052 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10054 ptrdiff_t i;
10055 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10056 int seen_dots = 0;
10057 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10058 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10060 for (i = 0; i < len; i++)
10062 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10063 seen_dots = 1;
10064 if (p1[i] != p2[i])
10065 return seen_dots;
10067 p1 += len;
10068 if (*p1 == '\n')
10069 return 2;
10070 if (*p1++ == ' ' && *p1++ == '[')
10072 char *pend;
10073 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10074 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10075 return n + 1;
10077 return 0;
10081 /* Display an echo area message M with a specified length of NBYTES
10082 bytes. The string may include null characters. If M is not a
10083 string, clear out any existing message, and let the mini-buffer
10084 text show through.
10086 This function cancels echoing. */
10088 void
10089 message3 (Lisp_Object m)
10091 struct gcpro gcpro1;
10093 GCPRO1 (m);
10094 clear_message (true, true);
10095 cancel_echoing ();
10097 /* First flush out any partial line written with print. */
10098 message_log_maybe_newline ();
10099 if (STRINGP (m))
10101 ptrdiff_t nbytes = SBYTES (m);
10102 bool multibyte = STRING_MULTIBYTE (m);
10103 char *buffer;
10104 USE_SAFE_ALLOCA;
10105 SAFE_ALLOCA_STRING (buffer, m);
10106 message_dolog (buffer, nbytes, 1, multibyte);
10107 SAFE_FREE ();
10109 message3_nolog (m);
10111 UNGCPRO;
10115 /* The non-logging version of message3.
10116 This does not cancel echoing, because it is used for echoing.
10117 Perhaps we need to make a separate function for echoing
10118 and make this cancel echoing. */
10120 void
10121 message3_nolog (Lisp_Object m)
10123 struct frame *sf = SELECTED_FRAME ();
10125 if (FRAME_INITIAL_P (sf))
10127 if (noninteractive_need_newline)
10128 putc ('\n', stderr);
10129 noninteractive_need_newline = 0;
10130 if (STRINGP (m))
10132 Lisp_Object s = ENCODE_SYSTEM (m);
10134 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10136 if (cursor_in_echo_area == 0)
10137 fprintf (stderr, "\n");
10138 fflush (stderr);
10140 /* Error messages get reported properly by cmd_error, so this must be just an
10141 informative message; if the frame hasn't really been initialized yet, just
10142 toss it. */
10143 else if (INTERACTIVE && sf->glyphs_initialized_p)
10145 /* Get the frame containing the mini-buffer
10146 that the selected frame is using. */
10147 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10148 Lisp_Object frame = XWINDOW (mini_window)->frame;
10149 struct frame *f = XFRAME (frame);
10151 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10152 Fmake_frame_visible (frame);
10154 if (STRINGP (m) && SCHARS (m) > 0)
10156 set_message (m);
10157 if (minibuffer_auto_raise)
10158 Fraise_frame (frame);
10159 /* Assume we are not echoing.
10160 (If we are, echo_now will override this.) */
10161 echo_message_buffer = Qnil;
10163 else
10164 clear_message (true, true);
10166 do_pending_window_change (false);
10167 echo_area_display (true);
10168 do_pending_window_change (false);
10169 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10170 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10175 /* Display a null-terminated echo area message M. If M is 0, clear
10176 out any existing message, and let the mini-buffer text show through.
10178 The buffer M must continue to exist until after the echo area gets
10179 cleared or some other message gets displayed there. Do not pass
10180 text that is stored in a Lisp string. Do not pass text in a buffer
10181 that was alloca'd. */
10183 void
10184 message1 (const char *m)
10186 message3 (m ? build_unibyte_string (m) : Qnil);
10190 /* The non-logging counterpart of message1. */
10192 void
10193 message1_nolog (const char *m)
10195 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10198 /* Display a message M which contains a single %s
10199 which gets replaced with STRING. */
10201 void
10202 message_with_string (const char *m, Lisp_Object string, int log)
10204 CHECK_STRING (string);
10206 if (noninteractive)
10208 if (m)
10210 /* ENCODE_SYSTEM below can GC and/or relocate the
10211 Lisp data, so make sure we don't use it here. */
10212 eassert (relocatable_string_data_p (m) != 1);
10214 if (noninteractive_need_newline)
10215 putc ('\n', stderr);
10216 noninteractive_need_newline = 0;
10217 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10218 if (!cursor_in_echo_area)
10219 fprintf (stderr, "\n");
10220 fflush (stderr);
10223 else if (INTERACTIVE)
10225 /* The frame whose minibuffer we're going to display the message on.
10226 It may be larger than the selected frame, so we need
10227 to use its buffer, not the selected frame's buffer. */
10228 Lisp_Object mini_window;
10229 struct frame *f, *sf = SELECTED_FRAME ();
10231 /* Get the frame containing the minibuffer
10232 that the selected frame is using. */
10233 mini_window = FRAME_MINIBUF_WINDOW (sf);
10234 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10236 /* Error messages get reported properly by cmd_error, so this must be
10237 just an informative message; if the frame hasn't really been
10238 initialized yet, just toss it. */
10239 if (f->glyphs_initialized_p)
10241 Lisp_Object args[2], msg;
10242 struct gcpro gcpro1, gcpro2;
10244 args[0] = build_string (m);
10245 args[1] = msg = string;
10246 GCPRO2 (args[0], msg);
10247 gcpro1.nvars = 2;
10249 msg = Fformat (2, args);
10251 if (log)
10252 message3 (msg);
10253 else
10254 message3_nolog (msg);
10256 UNGCPRO;
10258 /* Print should start at the beginning of the message
10259 buffer next time. */
10260 message_buf_print = 0;
10266 /* Dump an informative message to the minibuf. If M is 0, clear out
10267 any existing message, and let the mini-buffer text show through. */
10269 static void
10270 vmessage (const char *m, va_list ap)
10272 if (noninteractive)
10274 if (m)
10276 if (noninteractive_need_newline)
10277 putc ('\n', stderr);
10278 noninteractive_need_newline = 0;
10279 vfprintf (stderr, m, ap);
10280 if (cursor_in_echo_area == 0)
10281 fprintf (stderr, "\n");
10282 fflush (stderr);
10285 else if (INTERACTIVE)
10287 /* The frame whose mini-buffer we're going to display the message
10288 on. It may be larger than the selected frame, so we need to
10289 use its buffer, not the selected frame's buffer. */
10290 Lisp_Object mini_window;
10291 struct frame *f, *sf = SELECTED_FRAME ();
10293 /* Get the frame containing the mini-buffer
10294 that the selected frame is using. */
10295 mini_window = FRAME_MINIBUF_WINDOW (sf);
10296 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10298 /* Error messages get reported properly by cmd_error, so this must be
10299 just an informative message; if the frame hasn't really been
10300 initialized yet, just toss it. */
10301 if (f->glyphs_initialized_p)
10303 if (m)
10305 ptrdiff_t len;
10306 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10307 USE_SAFE_ALLOCA;
10308 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10310 len = doprnt (message_buf, maxsize, m, 0, ap);
10312 message3 (make_string (message_buf, len));
10313 SAFE_FREE ();
10315 else
10316 message1 (0);
10318 /* Print should start at the beginning of the message
10319 buffer next time. */
10320 message_buf_print = 0;
10325 void
10326 message (const char *m, ...)
10328 va_list ap;
10329 va_start (ap, m);
10330 vmessage (m, ap);
10331 va_end (ap);
10335 #if 0
10336 /* The non-logging version of message. */
10338 void
10339 message_nolog (const char *m, ...)
10341 Lisp_Object old_log_max;
10342 va_list ap;
10343 va_start (ap, m);
10344 old_log_max = Vmessage_log_max;
10345 Vmessage_log_max = Qnil;
10346 vmessage (m, ap);
10347 Vmessage_log_max = old_log_max;
10348 va_end (ap);
10350 #endif
10353 /* Display the current message in the current mini-buffer. This is
10354 only called from error handlers in process.c, and is not time
10355 critical. */
10357 void
10358 update_echo_area (void)
10360 if (!NILP (echo_area_buffer[0]))
10362 Lisp_Object string;
10363 string = Fcurrent_message ();
10364 message3 (string);
10369 /* Make sure echo area buffers in `echo_buffers' are live.
10370 If they aren't, make new ones. */
10372 static void
10373 ensure_echo_area_buffers (void)
10375 int i;
10377 for (i = 0; i < 2; ++i)
10378 if (!BUFFERP (echo_buffer[i])
10379 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10381 char name[30];
10382 Lisp_Object old_buffer;
10383 int j;
10385 old_buffer = echo_buffer[i];
10386 echo_buffer[i] = Fget_buffer_create
10387 (make_formatted_string (name, " *Echo Area %d*", i));
10388 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10389 /* to force word wrap in echo area -
10390 it was decided to postpone this*/
10391 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10393 for (j = 0; j < 2; ++j)
10394 if (EQ (old_buffer, echo_area_buffer[j]))
10395 echo_area_buffer[j] = echo_buffer[i];
10400 /* Call FN with args A1..A2 with either the current or last displayed
10401 echo_area_buffer as current buffer.
10403 WHICH zero means use the current message buffer
10404 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10405 from echo_buffer[] and clear it.
10407 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10408 suitable buffer from echo_buffer[] and clear it.
10410 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10411 that the current message becomes the last displayed one, make
10412 choose a suitable buffer for echo_area_buffer[0], and clear it.
10414 Value is what FN returns. */
10416 static int
10417 with_echo_area_buffer (struct window *w, int which,
10418 int (*fn) (ptrdiff_t, Lisp_Object),
10419 ptrdiff_t a1, Lisp_Object a2)
10421 Lisp_Object buffer;
10422 int this_one, the_other, clear_buffer_p, rc;
10423 ptrdiff_t count = SPECPDL_INDEX ();
10425 /* If buffers aren't live, make new ones. */
10426 ensure_echo_area_buffers ();
10428 clear_buffer_p = 0;
10430 if (which == 0)
10431 this_one = 0, the_other = 1;
10432 else if (which > 0)
10433 this_one = 1, the_other = 0;
10434 else
10436 this_one = 0, the_other = 1;
10437 clear_buffer_p = true;
10439 /* We need a fresh one in case the current echo buffer equals
10440 the one containing the last displayed echo area message. */
10441 if (!NILP (echo_area_buffer[this_one])
10442 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10443 echo_area_buffer[this_one] = Qnil;
10446 /* Choose a suitable buffer from echo_buffer[] is we don't
10447 have one. */
10448 if (NILP (echo_area_buffer[this_one]))
10450 echo_area_buffer[this_one]
10451 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10452 ? echo_buffer[the_other]
10453 : echo_buffer[this_one]);
10454 clear_buffer_p = true;
10457 buffer = echo_area_buffer[this_one];
10459 /* Don't get confused by reusing the buffer used for echoing
10460 for a different purpose. */
10461 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10462 cancel_echoing ();
10464 record_unwind_protect (unwind_with_echo_area_buffer,
10465 with_echo_area_buffer_unwind_data (w));
10467 /* Make the echo area buffer current. Note that for display
10468 purposes, it is not necessary that the displayed window's buffer
10469 == current_buffer, except for text property lookup. So, let's
10470 only set that buffer temporarily here without doing a full
10471 Fset_window_buffer. We must also change w->pointm, though,
10472 because otherwise an assertions in unshow_buffer fails, and Emacs
10473 aborts. */
10474 set_buffer_internal_1 (XBUFFER (buffer));
10475 if (w)
10477 wset_buffer (w, buffer);
10478 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10479 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10482 bset_undo_list (current_buffer, Qt);
10483 bset_read_only (current_buffer, Qnil);
10484 specbind (Qinhibit_read_only, Qt);
10485 specbind (Qinhibit_modification_hooks, Qt);
10487 if (clear_buffer_p && Z > BEG)
10488 del_range (BEG, Z);
10490 eassert (BEGV >= BEG);
10491 eassert (ZV <= Z && ZV >= BEGV);
10493 rc = fn (a1, a2);
10495 eassert (BEGV >= BEG);
10496 eassert (ZV <= Z && ZV >= BEGV);
10498 unbind_to (count, Qnil);
10499 return rc;
10503 /* Save state that should be preserved around the call to the function
10504 FN called in with_echo_area_buffer. */
10506 static Lisp_Object
10507 with_echo_area_buffer_unwind_data (struct window *w)
10509 int i = 0;
10510 Lisp_Object vector, tmp;
10512 /* Reduce consing by keeping one vector in
10513 Vwith_echo_area_save_vector. */
10514 vector = Vwith_echo_area_save_vector;
10515 Vwith_echo_area_save_vector = Qnil;
10517 if (NILP (vector))
10518 vector = Fmake_vector (make_number (11), Qnil);
10520 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10521 ASET (vector, i, Vdeactivate_mark); ++i;
10522 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10524 if (w)
10526 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10527 ASET (vector, i, w->contents); ++i;
10528 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10529 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10530 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10531 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10532 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10533 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10535 else
10537 int end = i + 8;
10538 for (; i < end; ++i)
10539 ASET (vector, i, Qnil);
10542 eassert (i == ASIZE (vector));
10543 return vector;
10547 /* Restore global state from VECTOR which was created by
10548 with_echo_area_buffer_unwind_data. */
10550 static void
10551 unwind_with_echo_area_buffer (Lisp_Object vector)
10553 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10554 Vdeactivate_mark = AREF (vector, 1);
10555 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10557 if (WINDOWP (AREF (vector, 3)))
10559 struct window *w;
10560 Lisp_Object buffer;
10562 w = XWINDOW (AREF (vector, 3));
10563 buffer = AREF (vector, 4);
10565 wset_buffer (w, buffer);
10566 set_marker_both (w->pointm, buffer,
10567 XFASTINT (AREF (vector, 5)),
10568 XFASTINT (AREF (vector, 6)));
10569 set_marker_both (w->old_pointm, buffer,
10570 XFASTINT (AREF (vector, 7)),
10571 XFASTINT (AREF (vector, 8)));
10572 set_marker_both (w->start, buffer,
10573 XFASTINT (AREF (vector, 9)),
10574 XFASTINT (AREF (vector, 10)));
10577 Vwith_echo_area_save_vector = vector;
10581 /* Set up the echo area for use by print functions. MULTIBYTE_P
10582 non-zero means we will print multibyte. */
10584 void
10585 setup_echo_area_for_printing (int multibyte_p)
10587 /* If we can't find an echo area any more, exit. */
10588 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10589 Fkill_emacs (Qnil);
10591 ensure_echo_area_buffers ();
10593 if (!message_buf_print)
10595 /* A message has been output since the last time we printed.
10596 Choose a fresh echo area buffer. */
10597 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10598 echo_area_buffer[0] = echo_buffer[1];
10599 else
10600 echo_area_buffer[0] = echo_buffer[0];
10602 /* Switch to that buffer and clear it. */
10603 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10604 bset_truncate_lines (current_buffer, Qnil);
10606 if (Z > BEG)
10608 ptrdiff_t count = SPECPDL_INDEX ();
10609 specbind (Qinhibit_read_only, Qt);
10610 /* Note that undo recording is always disabled. */
10611 del_range (BEG, Z);
10612 unbind_to (count, Qnil);
10614 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10616 /* Set up the buffer for the multibyteness we need. */
10617 if (multibyte_p
10618 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10619 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10621 /* Raise the frame containing the echo area. */
10622 if (minibuffer_auto_raise)
10624 struct frame *sf = SELECTED_FRAME ();
10625 Lisp_Object mini_window;
10626 mini_window = FRAME_MINIBUF_WINDOW (sf);
10627 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10630 message_log_maybe_newline ();
10631 message_buf_print = 1;
10633 else
10635 if (NILP (echo_area_buffer[0]))
10637 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10638 echo_area_buffer[0] = echo_buffer[1];
10639 else
10640 echo_area_buffer[0] = echo_buffer[0];
10643 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10645 /* Someone switched buffers between print requests. */
10646 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10647 bset_truncate_lines (current_buffer, Qnil);
10653 /* Display an echo area message in window W. Value is non-zero if W's
10654 height is changed. If display_last_displayed_message_p is
10655 non-zero, display the message that was last displayed, otherwise
10656 display the current message. */
10658 static int
10659 display_echo_area (struct window *w)
10661 int i, no_message_p, window_height_changed_p;
10663 /* Temporarily disable garbage collections while displaying the echo
10664 area. This is done because a GC can print a message itself.
10665 That message would modify the echo area buffer's contents while a
10666 redisplay of the buffer is going on, and seriously confuse
10667 redisplay. */
10668 ptrdiff_t count = inhibit_garbage_collection ();
10670 /* If there is no message, we must call display_echo_area_1
10671 nevertheless because it resizes the window. But we will have to
10672 reset the echo_area_buffer in question to nil at the end because
10673 with_echo_area_buffer will sets it to an empty buffer. */
10674 i = display_last_displayed_message_p ? 1 : 0;
10675 no_message_p = NILP (echo_area_buffer[i]);
10677 window_height_changed_p
10678 = with_echo_area_buffer (w, display_last_displayed_message_p,
10679 display_echo_area_1,
10680 (intptr_t) w, Qnil);
10682 if (no_message_p)
10683 echo_area_buffer[i] = Qnil;
10685 unbind_to (count, Qnil);
10686 return window_height_changed_p;
10690 /* Helper for display_echo_area. Display the current buffer which
10691 contains the current echo area message in window W, a mini-window,
10692 a pointer to which is passed in A1. A2..A4 are currently not used.
10693 Change the height of W so that all of the message is displayed.
10694 Value is non-zero if height of W was changed. */
10696 static int
10697 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10699 intptr_t i1 = a1;
10700 struct window *w = (struct window *) i1;
10701 Lisp_Object window;
10702 struct text_pos start;
10703 int window_height_changed_p = 0;
10705 /* Do this before displaying, so that we have a large enough glyph
10706 matrix for the display. If we can't get enough space for the
10707 whole text, display the last N lines. That works by setting w->start. */
10708 window_height_changed_p = resize_mini_window (w, 0);
10710 /* Use the starting position chosen by resize_mini_window. */
10711 SET_TEXT_POS_FROM_MARKER (start, w->start);
10713 /* Display. */
10714 clear_glyph_matrix (w->desired_matrix);
10715 XSETWINDOW (window, w);
10716 try_window (window, start, 0);
10718 return window_height_changed_p;
10722 /* Resize the echo area window to exactly the size needed for the
10723 currently displayed message, if there is one. If a mini-buffer
10724 is active, don't shrink it. */
10726 void
10727 resize_echo_area_exactly (void)
10729 if (BUFFERP (echo_area_buffer[0])
10730 && WINDOWP (echo_area_window))
10732 struct window *w = XWINDOW (echo_area_window);
10733 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10734 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10735 (intptr_t) w, resize_exactly);
10736 if (resized_p)
10738 windows_or_buffers_changed = 42;
10739 update_mode_lines = 30;
10740 redisplay_internal ();
10746 /* Callback function for with_echo_area_buffer, when used from
10747 resize_echo_area_exactly. A1 contains a pointer to the window to
10748 resize, EXACTLY non-nil means resize the mini-window exactly to the
10749 size of the text displayed. A3 and A4 are not used. Value is what
10750 resize_mini_window returns. */
10752 static int
10753 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10755 intptr_t i1 = a1;
10756 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10760 /* Resize mini-window W to fit the size of its contents. EXACT_P
10761 means size the window exactly to the size needed. Otherwise, it's
10762 only enlarged until W's buffer is empty.
10764 Set W->start to the right place to begin display. If the whole
10765 contents fit, start at the beginning. Otherwise, start so as
10766 to make the end of the contents appear. This is particularly
10767 important for y-or-n-p, but seems desirable generally.
10769 Value is non-zero if the window height has been changed. */
10772 resize_mini_window (struct window *w, int exact_p)
10774 struct frame *f = XFRAME (w->frame);
10775 int window_height_changed_p = 0;
10777 eassert (MINI_WINDOW_P (w));
10779 /* By default, start display at the beginning. */
10780 set_marker_both (w->start, w->contents,
10781 BUF_BEGV (XBUFFER (w->contents)),
10782 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10784 /* Don't resize windows while redisplaying a window; it would
10785 confuse redisplay functions when the size of the window they are
10786 displaying changes from under them. Such a resizing can happen,
10787 for instance, when which-func prints a long message while
10788 we are running fontification-functions. We're running these
10789 functions with safe_call which binds inhibit-redisplay to t. */
10790 if (!NILP (Vinhibit_redisplay))
10791 return 0;
10793 /* Nil means don't try to resize. */
10794 if (NILP (Vresize_mini_windows)
10795 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10796 return 0;
10798 if (!FRAME_MINIBUF_ONLY_P (f))
10800 struct it it;
10801 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10802 + WINDOW_PIXEL_HEIGHT (w));
10803 int unit = FRAME_LINE_HEIGHT (f);
10804 int height, max_height;
10805 struct text_pos start;
10806 struct buffer *old_current_buffer = NULL;
10808 if (current_buffer != XBUFFER (w->contents))
10810 old_current_buffer = current_buffer;
10811 set_buffer_internal (XBUFFER (w->contents));
10814 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10816 /* Compute the max. number of lines specified by the user. */
10817 if (FLOATP (Vmax_mini_window_height))
10818 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10819 else if (INTEGERP (Vmax_mini_window_height))
10820 max_height = XINT (Vmax_mini_window_height) * unit;
10821 else
10822 max_height = total_height / 4;
10824 /* Correct that max. height if it's bogus. */
10825 max_height = clip_to_bounds (unit, max_height, total_height);
10827 /* Find out the height of the text in the window. */
10828 if (it.line_wrap == TRUNCATE)
10829 height = unit;
10830 else
10832 last_height = 0;
10833 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10834 if (it.max_ascent == 0 && it.max_descent == 0)
10835 height = it.current_y + last_height;
10836 else
10837 height = it.current_y + it.max_ascent + it.max_descent;
10838 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10841 /* Compute a suitable window start. */
10842 if (height > max_height)
10844 height = (max_height / unit) * unit;
10845 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10846 move_it_vertically_backward (&it, height - unit);
10847 start = it.current.pos;
10849 else
10850 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10851 SET_MARKER_FROM_TEXT_POS (w->start, start);
10853 if (EQ (Vresize_mini_windows, Qgrow_only))
10855 /* Let it grow only, until we display an empty message, in which
10856 case the window shrinks again. */
10857 if (height > WINDOW_PIXEL_HEIGHT (w))
10859 int old_height = WINDOW_PIXEL_HEIGHT (w);
10861 FRAME_WINDOWS_FROZEN (f) = 1;
10862 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10863 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10865 else if (height < WINDOW_PIXEL_HEIGHT (w)
10866 && (exact_p || BEGV == ZV))
10868 int old_height = WINDOW_PIXEL_HEIGHT (w);
10870 FRAME_WINDOWS_FROZEN (f) = 0;
10871 shrink_mini_window (w, 1);
10872 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10875 else
10877 /* Always resize to exact size needed. */
10878 if (height > WINDOW_PIXEL_HEIGHT (w))
10880 int old_height = WINDOW_PIXEL_HEIGHT (w);
10882 FRAME_WINDOWS_FROZEN (f) = 1;
10883 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10884 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10886 else if (height < WINDOW_PIXEL_HEIGHT (w))
10888 int old_height = WINDOW_PIXEL_HEIGHT (w);
10890 FRAME_WINDOWS_FROZEN (f) = 0;
10891 shrink_mini_window (w, 1);
10893 if (height)
10895 FRAME_WINDOWS_FROZEN (f) = 1;
10896 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10899 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10903 if (old_current_buffer)
10904 set_buffer_internal (old_current_buffer);
10907 return window_height_changed_p;
10911 /* Value is the current message, a string, or nil if there is no
10912 current message. */
10914 Lisp_Object
10915 current_message (void)
10917 Lisp_Object msg;
10919 if (!BUFFERP (echo_area_buffer[0]))
10920 msg = Qnil;
10921 else
10923 with_echo_area_buffer (0, 0, current_message_1,
10924 (intptr_t) &msg, Qnil);
10925 if (NILP (msg))
10926 echo_area_buffer[0] = Qnil;
10929 return msg;
10933 static int
10934 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10936 intptr_t i1 = a1;
10937 Lisp_Object *msg = (Lisp_Object *) i1;
10939 if (Z > BEG)
10940 *msg = make_buffer_string (BEG, Z, 1);
10941 else
10942 *msg = Qnil;
10943 return 0;
10947 /* Push the current message on Vmessage_stack for later restoration
10948 by restore_message. Value is non-zero if the current message isn't
10949 empty. This is a relatively infrequent operation, so it's not
10950 worth optimizing. */
10952 bool
10953 push_message (void)
10955 Lisp_Object msg = current_message ();
10956 Vmessage_stack = Fcons (msg, Vmessage_stack);
10957 return STRINGP (msg);
10961 /* Restore message display from the top of Vmessage_stack. */
10963 void
10964 restore_message (void)
10966 eassert (CONSP (Vmessage_stack));
10967 message3_nolog (XCAR (Vmessage_stack));
10971 /* Handler for unwind-protect calling pop_message. */
10973 void
10974 pop_message_unwind (void)
10976 /* Pop the top-most entry off Vmessage_stack. */
10977 eassert (CONSP (Vmessage_stack));
10978 Vmessage_stack = XCDR (Vmessage_stack);
10982 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10983 exits. If the stack is not empty, we have a missing pop_message
10984 somewhere. */
10986 void
10987 check_message_stack (void)
10989 if (!NILP (Vmessage_stack))
10990 emacs_abort ();
10994 /* Truncate to NCHARS what will be displayed in the echo area the next
10995 time we display it---but don't redisplay it now. */
10997 void
10998 truncate_echo_area (ptrdiff_t nchars)
11000 if (nchars == 0)
11001 echo_area_buffer[0] = Qnil;
11002 else if (!noninteractive
11003 && INTERACTIVE
11004 && !NILP (echo_area_buffer[0]))
11006 struct frame *sf = SELECTED_FRAME ();
11007 /* Error messages get reported properly by cmd_error, so this must be
11008 just an informative message; if the frame hasn't really been
11009 initialized yet, just toss it. */
11010 if (sf->glyphs_initialized_p)
11011 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11016 /* Helper function for truncate_echo_area. Truncate the current
11017 message to at most NCHARS characters. */
11019 static int
11020 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11022 if (BEG + nchars < Z)
11023 del_range (BEG + nchars, Z);
11024 if (Z == BEG)
11025 echo_area_buffer[0] = Qnil;
11026 return 0;
11029 /* Set the current message to STRING. */
11031 static void
11032 set_message (Lisp_Object string)
11034 eassert (STRINGP (string));
11036 message_enable_multibyte = STRING_MULTIBYTE (string);
11038 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11039 message_buf_print = 0;
11040 help_echo_showing_p = 0;
11042 if (STRINGP (Vdebug_on_message)
11043 && STRINGP (string)
11044 && fast_string_match (Vdebug_on_message, string) >= 0)
11045 call_debugger (list2 (Qerror, string));
11049 /* Helper function for set_message. First argument is ignored and second
11050 argument has the same meaning as for set_message.
11051 This function is called with the echo area buffer being current. */
11053 static int
11054 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11056 eassert (STRINGP (string));
11058 /* Change multibyteness of the echo buffer appropriately. */
11059 if (message_enable_multibyte
11060 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11061 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11063 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11064 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11065 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11067 /* Insert new message at BEG. */
11068 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11070 /* This function takes care of single/multibyte conversion.
11071 We just have to ensure that the echo area buffer has the right
11072 setting of enable_multibyte_characters. */
11073 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11075 return 0;
11079 /* Clear messages. CURRENT_P non-zero means clear the current
11080 message. LAST_DISPLAYED_P non-zero means clear the message
11081 last displayed. */
11083 void
11084 clear_message (bool current_p, bool last_displayed_p)
11086 if (current_p)
11088 echo_area_buffer[0] = Qnil;
11089 message_cleared_p = true;
11092 if (last_displayed_p)
11093 echo_area_buffer[1] = Qnil;
11095 message_buf_print = 0;
11098 /* Clear garbaged frames.
11100 This function is used where the old redisplay called
11101 redraw_garbaged_frames which in turn called redraw_frame which in
11102 turn called clear_frame. The call to clear_frame was a source of
11103 flickering. I believe a clear_frame is not necessary. It should
11104 suffice in the new redisplay to invalidate all current matrices,
11105 and ensure a complete redisplay of all windows. */
11107 static void
11108 clear_garbaged_frames (void)
11110 if (frame_garbaged)
11112 Lisp_Object tail, frame;
11114 FOR_EACH_FRAME (tail, frame)
11116 struct frame *f = XFRAME (frame);
11118 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11120 if (f->resized_p)
11121 redraw_frame (f);
11122 else
11123 clear_current_matrices (f);
11124 fset_redisplay (f);
11125 f->garbaged = false;
11126 f->resized_p = false;
11130 frame_garbaged = false;
11135 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11136 is non-zero update selected_frame. Value is non-zero if the
11137 mini-windows height has been changed. */
11139 static bool
11140 echo_area_display (bool update_frame_p)
11142 Lisp_Object mini_window;
11143 struct window *w;
11144 struct frame *f;
11145 bool window_height_changed_p = false;
11146 struct frame *sf = SELECTED_FRAME ();
11148 mini_window = FRAME_MINIBUF_WINDOW (sf);
11149 w = XWINDOW (mini_window);
11150 f = XFRAME (WINDOW_FRAME (w));
11152 /* Don't display if frame is invisible or not yet initialized. */
11153 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11154 return 0;
11156 #ifdef HAVE_WINDOW_SYSTEM
11157 /* When Emacs starts, selected_frame may be the initial terminal
11158 frame. If we let this through, a message would be displayed on
11159 the terminal. */
11160 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11161 return 0;
11162 #endif /* HAVE_WINDOW_SYSTEM */
11164 /* Redraw garbaged frames. */
11165 clear_garbaged_frames ();
11167 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11169 echo_area_window = mini_window;
11170 window_height_changed_p = display_echo_area (w);
11171 w->must_be_updated_p = true;
11173 /* Update the display, unless called from redisplay_internal.
11174 Also don't update the screen during redisplay itself. The
11175 update will happen at the end of redisplay, and an update
11176 here could cause confusion. */
11177 if (update_frame_p && !redisplaying_p)
11179 int n = 0;
11181 /* If the display update has been interrupted by pending
11182 input, update mode lines in the frame. Due to the
11183 pending input, it might have been that redisplay hasn't
11184 been called, so that mode lines above the echo area are
11185 garbaged. This looks odd, so we prevent it here. */
11186 if (!display_completed)
11187 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11189 if (window_height_changed_p
11190 /* Don't do this if Emacs is shutting down. Redisplay
11191 needs to run hooks. */
11192 && !NILP (Vrun_hooks))
11194 /* Must update other windows. Likewise as in other
11195 cases, don't let this update be interrupted by
11196 pending input. */
11197 ptrdiff_t count = SPECPDL_INDEX ();
11198 specbind (Qredisplay_dont_pause, Qt);
11199 windows_or_buffers_changed = 44;
11200 redisplay_internal ();
11201 unbind_to (count, Qnil);
11203 else if (FRAME_WINDOW_P (f) && n == 0)
11205 /* Window configuration is the same as before.
11206 Can do with a display update of the echo area,
11207 unless we displayed some mode lines. */
11208 update_single_window (w);
11209 flush_frame (f);
11211 else
11212 update_frame (f, true, true);
11214 /* If cursor is in the echo area, make sure that the next
11215 redisplay displays the minibuffer, so that the cursor will
11216 be replaced with what the minibuffer wants. */
11217 if (cursor_in_echo_area)
11218 wset_redisplay (XWINDOW (mini_window));
11221 else if (!EQ (mini_window, selected_window))
11222 wset_redisplay (XWINDOW (mini_window));
11224 /* Last displayed message is now the current message. */
11225 echo_area_buffer[1] = echo_area_buffer[0];
11226 /* Inform read_char that we're not echoing. */
11227 echo_message_buffer = Qnil;
11229 /* Prevent redisplay optimization in redisplay_internal by resetting
11230 this_line_start_pos. This is done because the mini-buffer now
11231 displays the message instead of its buffer text. */
11232 if (EQ (mini_window, selected_window))
11233 CHARPOS (this_line_start_pos) = 0;
11235 return window_height_changed_p;
11238 /* Nonzero if W's buffer was changed but not saved. */
11240 static int
11241 window_buffer_changed (struct window *w)
11243 struct buffer *b = XBUFFER (w->contents);
11245 eassert (BUFFER_LIVE_P (b));
11247 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11250 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11252 static int
11253 mode_line_update_needed (struct window *w)
11255 return (w->column_number_displayed != -1
11256 && !(PT == w->last_point && !window_outdated (w))
11257 && (w->column_number_displayed != current_column ()));
11260 /* Nonzero if window start of W is frozen and may not be changed during
11261 redisplay. */
11263 static bool
11264 window_frozen_p (struct window *w)
11266 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11268 Lisp_Object window;
11270 XSETWINDOW (window, w);
11271 if (MINI_WINDOW_P (w))
11272 return 0;
11273 else if (EQ (window, selected_window))
11274 return 0;
11275 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11276 && EQ (window, Vminibuf_scroll_window))
11277 /* This special window can't be frozen too. */
11278 return 0;
11279 else
11280 return 1;
11282 return 0;
11285 /***********************************************************************
11286 Mode Lines and Frame Titles
11287 ***********************************************************************/
11289 /* A buffer for constructing non-propertized mode-line strings and
11290 frame titles in it; allocated from the heap in init_xdisp and
11291 resized as needed in store_mode_line_noprop_char. */
11293 static char *mode_line_noprop_buf;
11295 /* The buffer's end, and a current output position in it. */
11297 static char *mode_line_noprop_buf_end;
11298 static char *mode_line_noprop_ptr;
11300 #define MODE_LINE_NOPROP_LEN(start) \
11301 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11303 static enum {
11304 MODE_LINE_DISPLAY = 0,
11305 MODE_LINE_TITLE,
11306 MODE_LINE_NOPROP,
11307 MODE_LINE_STRING
11308 } mode_line_target;
11310 /* Alist that caches the results of :propertize.
11311 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11312 static Lisp_Object mode_line_proptrans_alist;
11314 /* List of strings making up the mode-line. */
11315 static Lisp_Object mode_line_string_list;
11317 /* Base face property when building propertized mode line string. */
11318 static Lisp_Object mode_line_string_face;
11319 static Lisp_Object mode_line_string_face_prop;
11322 /* Unwind data for mode line strings */
11324 static Lisp_Object Vmode_line_unwind_vector;
11326 static Lisp_Object
11327 format_mode_line_unwind_data (struct frame *target_frame,
11328 struct buffer *obuf,
11329 Lisp_Object owin,
11330 int save_proptrans)
11332 Lisp_Object vector, tmp;
11334 /* Reduce consing by keeping one vector in
11335 Vwith_echo_area_save_vector. */
11336 vector = Vmode_line_unwind_vector;
11337 Vmode_line_unwind_vector = Qnil;
11339 if (NILP (vector))
11340 vector = Fmake_vector (make_number (10), Qnil);
11342 ASET (vector, 0, make_number (mode_line_target));
11343 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11344 ASET (vector, 2, mode_line_string_list);
11345 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11346 ASET (vector, 4, mode_line_string_face);
11347 ASET (vector, 5, mode_line_string_face_prop);
11349 if (obuf)
11350 XSETBUFFER (tmp, obuf);
11351 else
11352 tmp = Qnil;
11353 ASET (vector, 6, tmp);
11354 ASET (vector, 7, owin);
11355 if (target_frame)
11357 /* Similarly to `with-selected-window', if the operation selects
11358 a window on another frame, we must restore that frame's
11359 selected window, and (for a tty) the top-frame. */
11360 ASET (vector, 8, target_frame->selected_window);
11361 if (FRAME_TERMCAP_P (target_frame))
11362 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11365 return vector;
11368 static void
11369 unwind_format_mode_line (Lisp_Object vector)
11371 Lisp_Object old_window = AREF (vector, 7);
11372 Lisp_Object target_frame_window = AREF (vector, 8);
11373 Lisp_Object old_top_frame = AREF (vector, 9);
11375 mode_line_target = XINT (AREF (vector, 0));
11376 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11377 mode_line_string_list = AREF (vector, 2);
11378 if (! EQ (AREF (vector, 3), Qt))
11379 mode_line_proptrans_alist = AREF (vector, 3);
11380 mode_line_string_face = AREF (vector, 4);
11381 mode_line_string_face_prop = AREF (vector, 5);
11383 /* Select window before buffer, since it may change the buffer. */
11384 if (!NILP (old_window))
11386 /* If the operation that we are unwinding had selected a window
11387 on a different frame, reset its frame-selected-window. For a
11388 text terminal, reset its top-frame if necessary. */
11389 if (!NILP (target_frame_window))
11391 Lisp_Object frame
11392 = WINDOW_FRAME (XWINDOW (target_frame_window));
11394 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11395 Fselect_window (target_frame_window, Qt);
11397 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11398 Fselect_frame (old_top_frame, Qt);
11401 Fselect_window (old_window, Qt);
11404 if (!NILP (AREF (vector, 6)))
11406 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11407 ASET (vector, 6, Qnil);
11410 Vmode_line_unwind_vector = vector;
11414 /* Store a single character C for the frame title in mode_line_noprop_buf.
11415 Re-allocate mode_line_noprop_buf if necessary. */
11417 static void
11418 store_mode_line_noprop_char (char c)
11420 /* If output position has reached the end of the allocated buffer,
11421 increase the buffer's size. */
11422 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11424 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11425 ptrdiff_t size = len;
11426 mode_line_noprop_buf =
11427 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11428 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11429 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11432 *mode_line_noprop_ptr++ = c;
11436 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11437 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11438 characters that yield more columns than PRECISION; PRECISION <= 0
11439 means copy the whole string. Pad with spaces until FIELD_WIDTH
11440 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11441 pad. Called from display_mode_element when it is used to build a
11442 frame title. */
11444 static int
11445 store_mode_line_noprop (const char *string, int field_width, int precision)
11447 const unsigned char *str = (const unsigned char *) string;
11448 int n = 0;
11449 ptrdiff_t dummy, nbytes;
11451 /* Copy at most PRECISION chars from STR. */
11452 nbytes = strlen (string);
11453 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11454 while (nbytes--)
11455 store_mode_line_noprop_char (*str++);
11457 /* Fill up with spaces until FIELD_WIDTH reached. */
11458 while (field_width > 0
11459 && n < field_width)
11461 store_mode_line_noprop_char (' ');
11462 ++n;
11465 return n;
11468 /***********************************************************************
11469 Frame Titles
11470 ***********************************************************************/
11472 #ifdef HAVE_WINDOW_SYSTEM
11474 /* Set the title of FRAME, if it has changed. The title format is
11475 Vicon_title_format if FRAME is iconified, otherwise it is
11476 frame_title_format. */
11478 static void
11479 x_consider_frame_title (Lisp_Object frame)
11481 struct frame *f = XFRAME (frame);
11483 if (FRAME_WINDOW_P (f)
11484 || FRAME_MINIBUF_ONLY_P (f)
11485 || f->explicit_name)
11487 /* Do we have more than one visible frame on this X display? */
11488 Lisp_Object tail, other_frame, fmt;
11489 ptrdiff_t title_start;
11490 char *title;
11491 ptrdiff_t len;
11492 struct it it;
11493 ptrdiff_t count = SPECPDL_INDEX ();
11495 FOR_EACH_FRAME (tail, other_frame)
11497 struct frame *tf = XFRAME (other_frame);
11499 if (tf != f
11500 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11501 && !FRAME_MINIBUF_ONLY_P (tf)
11502 && !EQ (other_frame, tip_frame)
11503 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11504 break;
11507 /* Set global variable indicating that multiple frames exist. */
11508 multiple_frames = CONSP (tail);
11510 /* Switch to the buffer of selected window of the frame. Set up
11511 mode_line_target so that display_mode_element will output into
11512 mode_line_noprop_buf; then display the title. */
11513 record_unwind_protect (unwind_format_mode_line,
11514 format_mode_line_unwind_data
11515 (f, current_buffer, selected_window, 0));
11517 Fselect_window (f->selected_window, Qt);
11518 set_buffer_internal_1
11519 (XBUFFER (XWINDOW (f->selected_window)->contents));
11520 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11522 mode_line_target = MODE_LINE_TITLE;
11523 title_start = MODE_LINE_NOPROP_LEN (0);
11524 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11525 NULL, DEFAULT_FACE_ID);
11526 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11527 len = MODE_LINE_NOPROP_LEN (title_start);
11528 title = mode_line_noprop_buf + title_start;
11529 unbind_to (count, Qnil);
11531 /* Set the title only if it's changed. This avoids consing in
11532 the common case where it hasn't. (If it turns out that we've
11533 already wasted too much time by walking through the list with
11534 display_mode_element, then we might need to optimize at a
11535 higher level than this.) */
11536 if (! STRINGP (f->name)
11537 || SBYTES (f->name) != len
11538 || memcmp (title, SDATA (f->name), len) != 0)
11539 x_implicitly_set_name (f, make_string (title, len), Qnil);
11543 #endif /* not HAVE_WINDOW_SYSTEM */
11546 /***********************************************************************
11547 Menu Bars
11548 ***********************************************************************/
11550 /* Non-zero if we will not redisplay all visible windows. */
11551 #define REDISPLAY_SOME_P() \
11552 ((windows_or_buffers_changed == 0 \
11553 || windows_or_buffers_changed == REDISPLAY_SOME) \
11554 && (update_mode_lines == 0 \
11555 || update_mode_lines == REDISPLAY_SOME))
11557 /* Prepare for redisplay by updating menu-bar item lists when
11558 appropriate. This can call eval. */
11560 static void
11561 prepare_menu_bars (void)
11563 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11564 bool some_windows = REDISPLAY_SOME_P ();
11565 struct gcpro gcpro1, gcpro2;
11566 Lisp_Object tooltip_frame;
11568 #ifdef HAVE_WINDOW_SYSTEM
11569 tooltip_frame = tip_frame;
11570 #else
11571 tooltip_frame = Qnil;
11572 #endif
11574 if (FUNCTIONP (Vpre_redisplay_function))
11576 Lisp_Object windows = all_windows ? Qt : Qnil;
11577 if (all_windows && some_windows)
11579 Lisp_Object ws = window_list ();
11580 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11582 Lisp_Object this = XCAR (ws);
11583 struct window *w = XWINDOW (this);
11584 if (w->redisplay
11585 || XFRAME (w->frame)->redisplay
11586 || XBUFFER (w->contents)->text->redisplay)
11588 windows = Fcons (this, windows);
11592 safe__call1 (true, Vpre_redisplay_function, windows);
11595 /* Update all frame titles based on their buffer names, etc. We do
11596 this before the menu bars so that the buffer-menu will show the
11597 up-to-date frame titles. */
11598 #ifdef HAVE_WINDOW_SYSTEM
11599 if (all_windows)
11601 Lisp_Object tail, frame;
11603 FOR_EACH_FRAME (tail, frame)
11605 struct frame *f = XFRAME (frame);
11606 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11607 if (some_windows
11608 && !f->redisplay
11609 && !w->redisplay
11610 && !XBUFFER (w->contents)->text->redisplay)
11611 continue;
11613 if (!EQ (frame, tooltip_frame)
11614 && (FRAME_ICONIFIED_P (f)
11615 || FRAME_VISIBLE_P (f) == 1
11616 /* Exclude TTY frames that are obscured because they
11617 are not the top frame on their console. This is
11618 because x_consider_frame_title actually switches
11619 to the frame, which for TTY frames means it is
11620 marked as garbaged, and will be completely
11621 redrawn on the next redisplay cycle. This causes
11622 TTY frames to be completely redrawn, when there
11623 are more than one of them, even though nothing
11624 should be changed on display. */
11625 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11626 x_consider_frame_title (frame);
11629 #endif /* HAVE_WINDOW_SYSTEM */
11631 /* Update the menu bar item lists, if appropriate. This has to be
11632 done before any actual redisplay or generation of display lines. */
11634 if (all_windows)
11636 Lisp_Object tail, frame;
11637 ptrdiff_t count = SPECPDL_INDEX ();
11638 /* 1 means that update_menu_bar has run its hooks
11639 so any further calls to update_menu_bar shouldn't do so again. */
11640 int menu_bar_hooks_run = 0;
11642 record_unwind_save_match_data ();
11644 FOR_EACH_FRAME (tail, frame)
11646 struct frame *f = XFRAME (frame);
11647 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11649 /* Ignore tooltip frame. */
11650 if (EQ (frame, tooltip_frame))
11651 continue;
11653 if (some_windows
11654 && !f->redisplay
11655 && !w->redisplay
11656 && !XBUFFER (w->contents)->text->redisplay)
11657 continue;
11659 /* If a window on this frame changed size, report that to
11660 the user and clear the size-change flag. */
11661 if (FRAME_WINDOW_SIZES_CHANGED (f))
11663 Lisp_Object functions;
11665 /* Clear flag first in case we get an error below. */
11666 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11667 functions = Vwindow_size_change_functions;
11668 GCPRO2 (tail, functions);
11670 while (CONSP (functions))
11672 if (!EQ (XCAR (functions), Qt))
11673 call1 (XCAR (functions), frame);
11674 functions = XCDR (functions);
11676 UNGCPRO;
11679 GCPRO1 (tail);
11680 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11681 #ifdef HAVE_WINDOW_SYSTEM
11682 update_tool_bar (f, 0);
11683 #endif
11684 UNGCPRO;
11687 unbind_to (count, Qnil);
11689 else
11691 struct frame *sf = SELECTED_FRAME ();
11692 update_menu_bar (sf, 1, 0);
11693 #ifdef HAVE_WINDOW_SYSTEM
11694 update_tool_bar (sf, 1);
11695 #endif
11700 /* Update the menu bar item list for frame F. This has to be done
11701 before we start to fill in any display lines, because it can call
11702 eval.
11704 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11706 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11707 already ran the menu bar hooks for this redisplay, so there
11708 is no need to run them again. The return value is the
11709 updated value of this flag, to pass to the next call. */
11711 static int
11712 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11714 Lisp_Object window;
11715 register struct window *w;
11717 /* If called recursively during a menu update, do nothing. This can
11718 happen when, for instance, an activate-menubar-hook causes a
11719 redisplay. */
11720 if (inhibit_menubar_update)
11721 return hooks_run;
11723 window = FRAME_SELECTED_WINDOW (f);
11724 w = XWINDOW (window);
11726 if (FRAME_WINDOW_P (f)
11728 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11729 || defined (HAVE_NS) || defined (USE_GTK)
11730 FRAME_EXTERNAL_MENU_BAR (f)
11731 #else
11732 FRAME_MENU_BAR_LINES (f) > 0
11733 #endif
11734 : FRAME_MENU_BAR_LINES (f) > 0)
11736 /* If the user has switched buffers or windows, we need to
11737 recompute to reflect the new bindings. But we'll
11738 recompute when update_mode_lines is set too; that means
11739 that people can use force-mode-line-update to request
11740 that the menu bar be recomputed. The adverse effect on
11741 the rest of the redisplay algorithm is about the same as
11742 windows_or_buffers_changed anyway. */
11743 if (windows_or_buffers_changed
11744 /* This used to test w->update_mode_line, but we believe
11745 there is no need to recompute the menu in that case. */
11746 || update_mode_lines
11747 || window_buffer_changed (w))
11749 struct buffer *prev = current_buffer;
11750 ptrdiff_t count = SPECPDL_INDEX ();
11752 specbind (Qinhibit_menubar_update, Qt);
11754 set_buffer_internal_1 (XBUFFER (w->contents));
11755 if (save_match_data)
11756 record_unwind_save_match_data ();
11757 if (NILP (Voverriding_local_map_menu_flag))
11759 specbind (Qoverriding_terminal_local_map, Qnil);
11760 specbind (Qoverriding_local_map, Qnil);
11763 if (!hooks_run)
11765 /* Run the Lucid hook. */
11766 safe_run_hooks (Qactivate_menubar_hook);
11768 /* If it has changed current-menubar from previous value,
11769 really recompute the menu-bar from the value. */
11770 if (! NILP (Vlucid_menu_bar_dirty_flag))
11771 call0 (Qrecompute_lucid_menubar);
11773 safe_run_hooks (Qmenu_bar_update_hook);
11775 hooks_run = 1;
11778 XSETFRAME (Vmenu_updating_frame, f);
11779 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11781 /* Redisplay the menu bar in case we changed it. */
11782 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11783 || defined (HAVE_NS) || defined (USE_GTK)
11784 if (FRAME_WINDOW_P (f))
11786 #if defined (HAVE_NS)
11787 /* All frames on Mac OS share the same menubar. So only
11788 the selected frame should be allowed to set it. */
11789 if (f == SELECTED_FRAME ())
11790 #endif
11791 set_frame_menubar (f, 0, 0);
11793 else
11794 /* On a terminal screen, the menu bar is an ordinary screen
11795 line, and this makes it get updated. */
11796 w->update_mode_line = 1;
11797 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11798 /* In the non-toolkit version, the menu bar is an ordinary screen
11799 line, and this makes it get updated. */
11800 w->update_mode_line = 1;
11801 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11803 unbind_to (count, Qnil);
11804 set_buffer_internal_1 (prev);
11808 return hooks_run;
11811 /***********************************************************************
11812 Tool-bars
11813 ***********************************************************************/
11815 #ifdef HAVE_WINDOW_SYSTEM
11817 /* Select `frame' temporarily without running all the code in
11818 do_switch_frame.
11819 FIXME: Maybe do_switch_frame should be trimmed down similarly
11820 when `norecord' is set. */
11821 static void
11822 fast_set_selected_frame (Lisp_Object frame)
11824 if (!EQ (selected_frame, frame))
11826 selected_frame = frame;
11827 selected_window = XFRAME (frame)->selected_window;
11831 /* Update the tool-bar item list for frame F. This has to be done
11832 before we start to fill in any display lines. Called from
11833 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11834 and restore it here. */
11836 static void
11837 update_tool_bar (struct frame *f, int save_match_data)
11839 #if defined (USE_GTK) || defined (HAVE_NS)
11840 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11841 #else
11842 int do_update = (WINDOWP (f->tool_bar_window)
11843 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11844 #endif
11846 if (do_update)
11848 Lisp_Object window;
11849 struct window *w;
11851 window = FRAME_SELECTED_WINDOW (f);
11852 w = XWINDOW (window);
11854 /* If the user has switched buffers or windows, we need to
11855 recompute to reflect the new bindings. But we'll
11856 recompute when update_mode_lines is set too; that means
11857 that people can use force-mode-line-update to request
11858 that the menu bar be recomputed. The adverse effect on
11859 the rest of the redisplay algorithm is about the same as
11860 windows_or_buffers_changed anyway. */
11861 if (windows_or_buffers_changed
11862 || w->update_mode_line
11863 || update_mode_lines
11864 || window_buffer_changed (w))
11866 struct buffer *prev = current_buffer;
11867 ptrdiff_t count = SPECPDL_INDEX ();
11868 Lisp_Object frame, new_tool_bar;
11869 int new_n_tool_bar;
11870 struct gcpro gcpro1;
11872 /* Set current_buffer to the buffer of the selected
11873 window of the frame, so that we get the right local
11874 keymaps. */
11875 set_buffer_internal_1 (XBUFFER (w->contents));
11877 /* Save match data, if we must. */
11878 if (save_match_data)
11879 record_unwind_save_match_data ();
11881 /* Make sure that we don't accidentally use bogus keymaps. */
11882 if (NILP (Voverriding_local_map_menu_flag))
11884 specbind (Qoverriding_terminal_local_map, Qnil);
11885 specbind (Qoverriding_local_map, Qnil);
11888 GCPRO1 (new_tool_bar);
11890 /* We must temporarily set the selected frame to this frame
11891 before calling tool_bar_items, because the calculation of
11892 the tool-bar keymap uses the selected frame (see
11893 `tool-bar-make-keymap' in tool-bar.el). */
11894 eassert (EQ (selected_window,
11895 /* Since we only explicitly preserve selected_frame,
11896 check that selected_window would be redundant. */
11897 XFRAME (selected_frame)->selected_window));
11898 record_unwind_protect (fast_set_selected_frame, selected_frame);
11899 XSETFRAME (frame, f);
11900 fast_set_selected_frame (frame);
11902 /* Build desired tool-bar items from keymaps. */
11903 new_tool_bar
11904 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11905 &new_n_tool_bar);
11907 /* Redisplay the tool-bar if we changed it. */
11908 if (new_n_tool_bar != f->n_tool_bar_items
11909 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11911 /* Redisplay that happens asynchronously due to an expose event
11912 may access f->tool_bar_items. Make sure we update both
11913 variables within BLOCK_INPUT so no such event interrupts. */
11914 block_input ();
11915 fset_tool_bar_items (f, new_tool_bar);
11916 f->n_tool_bar_items = new_n_tool_bar;
11917 w->update_mode_line = 1;
11918 unblock_input ();
11921 UNGCPRO;
11923 unbind_to (count, Qnil);
11924 set_buffer_internal_1 (prev);
11929 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
11931 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11932 F's desired tool-bar contents. F->tool_bar_items must have
11933 been set up previously by calling prepare_menu_bars. */
11935 static void
11936 build_desired_tool_bar_string (struct frame *f)
11938 int i, size, size_needed;
11939 struct gcpro gcpro1, gcpro2;
11940 Lisp_Object image, plist;
11942 image = plist = Qnil;
11943 GCPRO2 (image, plist);
11945 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11946 Otherwise, make a new string. */
11948 /* The size of the string we might be able to reuse. */
11949 size = (STRINGP (f->desired_tool_bar_string)
11950 ? SCHARS (f->desired_tool_bar_string)
11951 : 0);
11953 /* We need one space in the string for each image. */
11954 size_needed = f->n_tool_bar_items;
11956 /* Reuse f->desired_tool_bar_string, if possible. */
11957 if (size < size_needed || NILP (f->desired_tool_bar_string))
11958 fset_desired_tool_bar_string
11959 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11960 else
11962 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
11963 struct gcpro gcpro1;
11964 GCPRO1 (props);
11965 Fremove_text_properties (make_number (0), make_number (size),
11966 props, f->desired_tool_bar_string);
11967 UNGCPRO;
11970 /* Put a `display' property on the string for the images to display,
11971 put a `menu_item' property on tool-bar items with a value that
11972 is the index of the item in F's tool-bar item vector. */
11973 for (i = 0; i < f->n_tool_bar_items; ++i)
11975 #define PROP(IDX) \
11976 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11978 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11979 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11980 int hmargin, vmargin, relief, idx, end;
11982 /* If image is a vector, choose the image according to the
11983 button state. */
11984 image = PROP (TOOL_BAR_ITEM_IMAGES);
11985 if (VECTORP (image))
11987 if (enabled_p)
11988 idx = (selected_p
11989 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11990 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11991 else
11992 idx = (selected_p
11993 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11994 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11996 eassert (ASIZE (image) >= idx);
11997 image = AREF (image, idx);
11999 else
12000 idx = -1;
12002 /* Ignore invalid image specifications. */
12003 if (!valid_image_p (image))
12004 continue;
12006 /* Display the tool-bar button pressed, or depressed. */
12007 plist = Fcopy_sequence (XCDR (image));
12009 /* Compute margin and relief to draw. */
12010 relief = (tool_bar_button_relief >= 0
12011 ? tool_bar_button_relief
12012 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12013 hmargin = vmargin = relief;
12015 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12016 INT_MAX - max (hmargin, vmargin)))
12018 hmargin += XFASTINT (Vtool_bar_button_margin);
12019 vmargin += XFASTINT (Vtool_bar_button_margin);
12021 else if (CONSP (Vtool_bar_button_margin))
12023 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12024 INT_MAX - hmargin))
12025 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12027 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12028 INT_MAX - vmargin))
12029 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12032 if (auto_raise_tool_bar_buttons_p)
12034 /* Add a `:relief' property to the image spec if the item is
12035 selected. */
12036 if (selected_p)
12038 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12039 hmargin -= relief;
12040 vmargin -= relief;
12043 else
12045 /* If image is selected, display it pressed, i.e. with a
12046 negative relief. If it's not selected, display it with a
12047 raised relief. */
12048 plist = Fplist_put (plist, QCrelief,
12049 (selected_p
12050 ? make_number (-relief)
12051 : make_number (relief)));
12052 hmargin -= relief;
12053 vmargin -= relief;
12056 /* Put a margin around the image. */
12057 if (hmargin || vmargin)
12059 if (hmargin == vmargin)
12060 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12061 else
12062 plist = Fplist_put (plist, QCmargin,
12063 Fcons (make_number (hmargin),
12064 make_number (vmargin)));
12067 /* If button is not enabled, and we don't have special images
12068 for the disabled state, make the image appear disabled by
12069 applying an appropriate algorithm to it. */
12070 if (!enabled_p && idx < 0)
12071 plist = Fplist_put (plist, QCconversion, Qdisabled);
12073 /* Put a `display' text property on the string for the image to
12074 display. Put a `menu-item' property on the string that gives
12075 the start of this item's properties in the tool-bar items
12076 vector. */
12077 image = Fcons (Qimage, plist);
12078 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12079 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12080 struct gcpro gcpro1;
12081 GCPRO1 (props);
12083 /* Let the last image hide all remaining spaces in the tool bar
12084 string. The string can be longer than needed when we reuse a
12085 previous string. */
12086 if (i + 1 == f->n_tool_bar_items)
12087 end = SCHARS (f->desired_tool_bar_string);
12088 else
12089 end = i + 1;
12090 Fadd_text_properties (make_number (i), make_number (end),
12091 props, f->desired_tool_bar_string);
12092 UNGCPRO;
12093 #undef PROP
12096 UNGCPRO;
12100 /* Display one line of the tool-bar of frame IT->f.
12102 HEIGHT specifies the desired height of the tool-bar line.
12103 If the actual height of the glyph row is less than HEIGHT, the
12104 row's height is increased to HEIGHT, and the icons are centered
12105 vertically in the new height.
12107 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12108 count a final empty row in case the tool-bar width exactly matches
12109 the window width.
12112 static void
12113 display_tool_bar_line (struct it *it, int height)
12115 struct glyph_row *row = it->glyph_row;
12116 int max_x = it->last_visible_x;
12117 struct glyph *last;
12119 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12120 clear_glyph_row (row);
12121 row->enabled_p = true;
12122 row->y = it->current_y;
12124 /* Note that this isn't made use of if the face hasn't a box,
12125 so there's no need to check the face here. */
12126 it->start_of_box_run_p = 1;
12128 while (it->current_x < max_x)
12130 int x, n_glyphs_before, i, nglyphs;
12131 struct it it_before;
12133 /* Get the next display element. */
12134 if (!get_next_display_element (it))
12136 /* Don't count empty row if we are counting needed tool-bar lines. */
12137 if (height < 0 && !it->hpos)
12138 return;
12139 break;
12142 /* Produce glyphs. */
12143 n_glyphs_before = row->used[TEXT_AREA];
12144 it_before = *it;
12146 PRODUCE_GLYPHS (it);
12148 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12149 i = 0;
12150 x = it_before.current_x;
12151 while (i < nglyphs)
12153 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12155 if (x + glyph->pixel_width > max_x)
12157 /* Glyph doesn't fit on line. Backtrack. */
12158 row->used[TEXT_AREA] = n_glyphs_before;
12159 *it = it_before;
12160 /* If this is the only glyph on this line, it will never fit on the
12161 tool-bar, so skip it. But ensure there is at least one glyph,
12162 so we don't accidentally disable the tool-bar. */
12163 if (n_glyphs_before == 0
12164 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12165 break;
12166 goto out;
12169 ++it->hpos;
12170 x += glyph->pixel_width;
12171 ++i;
12174 /* Stop at line end. */
12175 if (ITERATOR_AT_END_OF_LINE_P (it))
12176 break;
12178 set_iterator_to_next (it, 1);
12181 out:;
12183 row->displays_text_p = row->used[TEXT_AREA] != 0;
12185 /* Use default face for the border below the tool bar.
12187 FIXME: When auto-resize-tool-bars is grow-only, there is
12188 no additional border below the possibly empty tool-bar lines.
12189 So to make the extra empty lines look "normal", we have to
12190 use the tool-bar face for the border too. */
12191 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12192 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12193 it->face_id = DEFAULT_FACE_ID;
12195 extend_face_to_end_of_line (it);
12196 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12197 last->right_box_line_p = 1;
12198 if (last == row->glyphs[TEXT_AREA])
12199 last->left_box_line_p = 1;
12201 /* Make line the desired height and center it vertically. */
12202 if ((height -= it->max_ascent + it->max_descent) > 0)
12204 /* Don't add more than one line height. */
12205 height %= FRAME_LINE_HEIGHT (it->f);
12206 it->max_ascent += height / 2;
12207 it->max_descent += (height + 1) / 2;
12210 compute_line_metrics (it);
12212 /* If line is empty, make it occupy the rest of the tool-bar. */
12213 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12215 row->height = row->phys_height = it->last_visible_y - row->y;
12216 row->visible_height = row->height;
12217 row->ascent = row->phys_ascent = 0;
12218 row->extra_line_spacing = 0;
12221 row->full_width_p = 1;
12222 row->continued_p = 0;
12223 row->truncated_on_left_p = 0;
12224 row->truncated_on_right_p = 0;
12226 it->current_x = it->hpos = 0;
12227 it->current_y += row->height;
12228 ++it->vpos;
12229 ++it->glyph_row;
12233 /* Value is the number of pixels needed to make all tool-bar items of
12234 frame F visible. The actual number of glyph rows needed is
12235 returned in *N_ROWS if non-NULL. */
12236 static int
12237 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12239 struct window *w = XWINDOW (f->tool_bar_window);
12240 struct it it;
12241 /* tool_bar_height is called from redisplay_tool_bar after building
12242 the desired matrix, so use (unused) mode-line row as temporary row to
12243 avoid destroying the first tool-bar row. */
12244 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12246 /* Initialize an iterator for iteration over
12247 F->desired_tool_bar_string in the tool-bar window of frame F. */
12248 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12249 temp_row->reversed_p = false;
12250 it.first_visible_x = 0;
12251 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12252 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12253 it.paragraph_embedding = L2R;
12255 while (!ITERATOR_AT_END_P (&it))
12257 clear_glyph_row (temp_row);
12258 it.glyph_row = temp_row;
12259 display_tool_bar_line (&it, -1);
12261 clear_glyph_row (temp_row);
12263 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12264 if (n_rows)
12265 *n_rows = it.vpos > 0 ? it.vpos : -1;
12267 if (pixelwise)
12268 return it.current_y;
12269 else
12270 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12273 #endif /* !USE_GTK && !HAVE_NS */
12275 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12276 0, 2, 0,
12277 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12278 If FRAME is nil or omitted, use the selected frame. Optional argument
12279 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12280 (Lisp_Object frame, Lisp_Object pixelwise)
12282 int height = 0;
12284 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12285 struct frame *f = decode_any_frame (frame);
12287 if (WINDOWP (f->tool_bar_window)
12288 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12290 update_tool_bar (f, 1);
12291 if (f->n_tool_bar_items)
12293 build_desired_tool_bar_string (f);
12294 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12297 #endif
12299 return make_number (height);
12303 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12304 height should be changed. */
12305 static int
12306 redisplay_tool_bar (struct frame *f)
12308 #if defined (USE_GTK) || defined (HAVE_NS)
12310 if (FRAME_EXTERNAL_TOOL_BAR (f))
12311 update_frame_tool_bar (f);
12312 return 0;
12314 #else /* !USE_GTK && !HAVE_NS */
12316 struct window *w;
12317 struct it it;
12318 struct glyph_row *row;
12320 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12321 do anything. This means you must start with tool-bar-lines
12322 non-zero to get the auto-sizing effect. Or in other words, you
12323 can turn off tool-bars by specifying tool-bar-lines zero. */
12324 if (!WINDOWP (f->tool_bar_window)
12325 || (w = XWINDOW (f->tool_bar_window),
12326 WINDOW_TOTAL_LINES (w) == 0))
12327 return 0;
12329 /* Set up an iterator for the tool-bar window. */
12330 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12331 it.first_visible_x = 0;
12332 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12333 row = it.glyph_row;
12334 row->reversed_p = false;
12336 /* Build a string that represents the contents of the tool-bar. */
12337 build_desired_tool_bar_string (f);
12338 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12339 /* FIXME: This should be controlled by a user option. But it
12340 doesn't make sense to have an R2L tool bar if the menu bar cannot
12341 be drawn also R2L, and making the menu bar R2L is tricky due
12342 toolkit-specific code that implements it. If an R2L tool bar is
12343 ever supported, display_tool_bar_line should also be augmented to
12344 call unproduce_glyphs like display_line and display_string
12345 do. */
12346 it.paragraph_embedding = L2R;
12348 if (f->n_tool_bar_rows == 0)
12350 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12352 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12354 x_change_tool_bar_height (f, new_height);
12355 frame_default_tool_bar_height = new_height;
12356 /* Always do that now. */
12357 clear_glyph_matrix (w->desired_matrix);
12358 f->fonts_changed = 1;
12359 return 1;
12363 /* Display as many lines as needed to display all tool-bar items. */
12365 if (f->n_tool_bar_rows > 0)
12367 int border, rows, height, extra;
12369 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12370 border = XINT (Vtool_bar_border);
12371 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12372 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12373 else if (EQ (Vtool_bar_border, Qborder_width))
12374 border = f->border_width;
12375 else
12376 border = 0;
12377 if (border < 0)
12378 border = 0;
12380 rows = f->n_tool_bar_rows;
12381 height = max (1, (it.last_visible_y - border) / rows);
12382 extra = it.last_visible_y - border - height * rows;
12384 while (it.current_y < it.last_visible_y)
12386 int h = 0;
12387 if (extra > 0 && rows-- > 0)
12389 h = (extra + rows - 1) / rows;
12390 extra -= h;
12392 display_tool_bar_line (&it, height + h);
12395 else
12397 while (it.current_y < it.last_visible_y)
12398 display_tool_bar_line (&it, 0);
12401 /* It doesn't make much sense to try scrolling in the tool-bar
12402 window, so don't do it. */
12403 w->desired_matrix->no_scrolling_p = 1;
12404 w->must_be_updated_p = 1;
12406 if (!NILP (Vauto_resize_tool_bars))
12408 int change_height_p = 0;
12410 /* If we couldn't display everything, change the tool-bar's
12411 height if there is room for more. */
12412 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12413 change_height_p = 1;
12415 /* We subtract 1 because display_tool_bar_line advances the
12416 glyph_row pointer before returning to its caller. We want to
12417 examine the last glyph row produced by
12418 display_tool_bar_line. */
12419 row = it.glyph_row - 1;
12421 /* If there are blank lines at the end, except for a partially
12422 visible blank line at the end that is smaller than
12423 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12424 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12425 && row->height >= FRAME_LINE_HEIGHT (f))
12426 change_height_p = 1;
12428 /* If row displays tool-bar items, but is partially visible,
12429 change the tool-bar's height. */
12430 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12431 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12432 change_height_p = 1;
12434 /* Resize windows as needed by changing the `tool-bar-lines'
12435 frame parameter. */
12436 if (change_height_p)
12438 int nrows;
12439 int new_height = tool_bar_height (f, &nrows, 1);
12441 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12442 && !f->minimize_tool_bar_window_p)
12443 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12444 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12445 f->minimize_tool_bar_window_p = 0;
12447 if (change_height_p)
12449 x_change_tool_bar_height (f, new_height);
12450 frame_default_tool_bar_height = new_height;
12451 clear_glyph_matrix (w->desired_matrix);
12452 f->n_tool_bar_rows = nrows;
12453 f->fonts_changed = 1;
12455 return 1;
12460 f->minimize_tool_bar_window_p = 0;
12461 return 0;
12463 #endif /* USE_GTK || HAVE_NS */
12466 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12468 /* Get information about the tool-bar item which is displayed in GLYPH
12469 on frame F. Return in *PROP_IDX the index where tool-bar item
12470 properties start in F->tool_bar_items. Value is zero if
12471 GLYPH doesn't display a tool-bar item. */
12473 static int
12474 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12476 Lisp_Object prop;
12477 int success_p;
12478 int charpos;
12480 /* This function can be called asynchronously, which means we must
12481 exclude any possibility that Fget_text_property signals an
12482 error. */
12483 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12484 charpos = max (0, charpos);
12486 /* Get the text property `menu-item' at pos. The value of that
12487 property is the start index of this item's properties in
12488 F->tool_bar_items. */
12489 prop = Fget_text_property (make_number (charpos),
12490 Qmenu_item, f->current_tool_bar_string);
12491 if (INTEGERP (prop))
12493 *prop_idx = XINT (prop);
12494 success_p = 1;
12496 else
12497 success_p = 0;
12499 return success_p;
12503 /* Get information about the tool-bar item at position X/Y on frame F.
12504 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12505 the current matrix of the tool-bar window of F, or NULL if not
12506 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12507 item in F->tool_bar_items. Value is
12509 -1 if X/Y is not on a tool-bar item
12510 0 if X/Y is on the same item that was highlighted before.
12511 1 otherwise. */
12513 static int
12514 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12515 int *hpos, int *vpos, int *prop_idx)
12517 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12518 struct window *w = XWINDOW (f->tool_bar_window);
12519 int area;
12521 /* Find the glyph under X/Y. */
12522 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12523 if (*glyph == NULL)
12524 return -1;
12526 /* Get the start of this tool-bar item's properties in
12527 f->tool_bar_items. */
12528 if (!tool_bar_item_info (f, *glyph, prop_idx))
12529 return -1;
12531 /* Is mouse on the highlighted item? */
12532 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12533 && *vpos >= hlinfo->mouse_face_beg_row
12534 && *vpos <= hlinfo->mouse_face_end_row
12535 && (*vpos > hlinfo->mouse_face_beg_row
12536 || *hpos >= hlinfo->mouse_face_beg_col)
12537 && (*vpos < hlinfo->mouse_face_end_row
12538 || *hpos < hlinfo->mouse_face_end_col
12539 || hlinfo->mouse_face_past_end))
12540 return 0;
12542 return 1;
12546 /* EXPORT:
12547 Handle mouse button event on the tool-bar of frame F, at
12548 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12549 0 for button release. MODIFIERS is event modifiers for button
12550 release. */
12552 void
12553 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12554 int modifiers)
12556 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12557 struct window *w = XWINDOW (f->tool_bar_window);
12558 int hpos, vpos, prop_idx;
12559 struct glyph *glyph;
12560 Lisp_Object enabled_p;
12561 int ts;
12563 /* If not on the highlighted tool-bar item, and mouse-highlight is
12564 non-nil, return. This is so we generate the tool-bar button
12565 click only when the mouse button is released on the same item as
12566 where it was pressed. However, when mouse-highlight is disabled,
12567 generate the click when the button is released regardless of the
12568 highlight, since tool-bar items are not highlighted in that
12569 case. */
12570 frame_to_window_pixel_xy (w, &x, &y);
12571 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12572 if (ts == -1
12573 || (ts != 0 && !NILP (Vmouse_highlight)))
12574 return;
12576 /* When mouse-highlight is off, generate the click for the item
12577 where the button was pressed, disregarding where it was
12578 released. */
12579 if (NILP (Vmouse_highlight) && !down_p)
12580 prop_idx = f->last_tool_bar_item;
12582 /* If item is disabled, do nothing. */
12583 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12584 if (NILP (enabled_p))
12585 return;
12587 if (down_p)
12589 /* Show item in pressed state. */
12590 if (!NILP (Vmouse_highlight))
12591 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12592 f->last_tool_bar_item = prop_idx;
12594 else
12596 Lisp_Object key, frame;
12597 struct input_event event;
12598 EVENT_INIT (event);
12600 /* Show item in released state. */
12601 if (!NILP (Vmouse_highlight))
12602 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12604 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12606 XSETFRAME (frame, f);
12607 event.kind = TOOL_BAR_EVENT;
12608 event.frame_or_window = frame;
12609 event.arg = frame;
12610 kbd_buffer_store_event (&event);
12612 event.kind = TOOL_BAR_EVENT;
12613 event.frame_or_window = frame;
12614 event.arg = key;
12615 event.modifiers = modifiers;
12616 kbd_buffer_store_event (&event);
12617 f->last_tool_bar_item = -1;
12622 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12623 tool-bar window-relative coordinates X/Y. Called from
12624 note_mouse_highlight. */
12626 static void
12627 note_tool_bar_highlight (struct frame *f, int x, int y)
12629 Lisp_Object window = f->tool_bar_window;
12630 struct window *w = XWINDOW (window);
12631 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12632 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12633 int hpos, vpos;
12634 struct glyph *glyph;
12635 struct glyph_row *row;
12636 int i;
12637 Lisp_Object enabled_p;
12638 int prop_idx;
12639 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12640 int mouse_down_p, rc;
12642 /* Function note_mouse_highlight is called with negative X/Y
12643 values when mouse moves outside of the frame. */
12644 if (x <= 0 || y <= 0)
12646 clear_mouse_face (hlinfo);
12647 return;
12650 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12651 if (rc < 0)
12653 /* Not on tool-bar item. */
12654 clear_mouse_face (hlinfo);
12655 return;
12657 else if (rc == 0)
12658 /* On same tool-bar item as before. */
12659 goto set_help_echo;
12661 clear_mouse_face (hlinfo);
12663 /* Mouse is down, but on different tool-bar item? */
12664 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12665 && f == dpyinfo->last_mouse_frame);
12667 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12668 return;
12670 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12672 /* If tool-bar item is not enabled, don't highlight it. */
12673 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12674 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12676 /* Compute the x-position of the glyph. In front and past the
12677 image is a space. We include this in the highlighted area. */
12678 row = MATRIX_ROW (w->current_matrix, vpos);
12679 for (i = x = 0; i < hpos; ++i)
12680 x += row->glyphs[TEXT_AREA][i].pixel_width;
12682 /* Record this as the current active region. */
12683 hlinfo->mouse_face_beg_col = hpos;
12684 hlinfo->mouse_face_beg_row = vpos;
12685 hlinfo->mouse_face_beg_x = x;
12686 hlinfo->mouse_face_past_end = 0;
12688 hlinfo->mouse_face_end_col = hpos + 1;
12689 hlinfo->mouse_face_end_row = vpos;
12690 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12691 hlinfo->mouse_face_window = window;
12692 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12694 /* Display it as active. */
12695 show_mouse_face (hlinfo, draw);
12698 set_help_echo:
12700 /* Set help_echo_string to a help string to display for this tool-bar item.
12701 XTread_socket does the rest. */
12702 help_echo_object = help_echo_window = Qnil;
12703 help_echo_pos = -1;
12704 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12705 if (NILP (help_echo_string))
12706 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12709 #endif /* !USE_GTK && !HAVE_NS */
12711 #endif /* HAVE_WINDOW_SYSTEM */
12715 /************************************************************************
12716 Horizontal scrolling
12717 ************************************************************************/
12719 static int hscroll_window_tree (Lisp_Object);
12720 static int hscroll_windows (Lisp_Object);
12722 /* For all leaf windows in the window tree rooted at WINDOW, set their
12723 hscroll value so that PT is (i) visible in the window, and (ii) so
12724 that it is not within a certain margin at the window's left and
12725 right border. Value is non-zero if any window's hscroll has been
12726 changed. */
12728 static int
12729 hscroll_window_tree (Lisp_Object window)
12731 int hscrolled_p = 0;
12732 int hscroll_relative_p = FLOATP (Vhscroll_step);
12733 int hscroll_step_abs = 0;
12734 double hscroll_step_rel = 0;
12736 if (hscroll_relative_p)
12738 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12739 if (hscroll_step_rel < 0)
12741 hscroll_relative_p = 0;
12742 hscroll_step_abs = 0;
12745 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12747 hscroll_step_abs = XINT (Vhscroll_step);
12748 if (hscroll_step_abs < 0)
12749 hscroll_step_abs = 0;
12751 else
12752 hscroll_step_abs = 0;
12754 while (WINDOWP (window))
12756 struct window *w = XWINDOW (window);
12758 if (WINDOWP (w->contents))
12759 hscrolled_p |= hscroll_window_tree (w->contents);
12760 else if (w->cursor.vpos >= 0)
12762 int h_margin;
12763 int text_area_width;
12764 struct glyph_row *cursor_row;
12765 struct glyph_row *bottom_row;
12766 int row_r2l_p;
12768 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12769 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12770 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12771 else
12772 cursor_row = bottom_row - 1;
12774 if (!cursor_row->enabled_p)
12776 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12777 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12778 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12779 else
12780 cursor_row = bottom_row - 1;
12782 row_r2l_p = cursor_row->reversed_p;
12784 text_area_width = window_box_width (w, TEXT_AREA);
12786 /* Scroll when cursor is inside this scroll margin. */
12787 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12789 /* If the position of this window's point has explicitly
12790 changed, no more suspend auto hscrolling. */
12791 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12792 w->suspend_auto_hscroll = 0;
12794 /* Remember window point. */
12795 Fset_marker (w->old_pointm,
12796 ((w == XWINDOW (selected_window))
12797 ? make_number (BUF_PT (XBUFFER (w->contents)))
12798 : Fmarker_position (w->pointm)),
12799 w->contents);
12801 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12802 && w->suspend_auto_hscroll == 0
12803 /* In some pathological cases, like restoring a window
12804 configuration into a frame that is much smaller than
12805 the one from which the configuration was saved, we
12806 get glyph rows whose start and end have zero buffer
12807 positions, which we cannot handle below. Just skip
12808 such windows. */
12809 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12810 /* For left-to-right rows, hscroll when cursor is either
12811 (i) inside the right hscroll margin, or (ii) if it is
12812 inside the left margin and the window is already
12813 hscrolled. */
12814 && ((!row_r2l_p
12815 && ((w->hscroll && w->cursor.x <= h_margin)
12816 || (cursor_row->enabled_p
12817 && cursor_row->truncated_on_right_p
12818 && (w->cursor.x >= text_area_width - h_margin))))
12819 /* For right-to-left rows, the logic is similar,
12820 except that rules for scrolling to left and right
12821 are reversed. E.g., if cursor.x <= h_margin, we
12822 need to hscroll "to the right" unconditionally,
12823 and that will scroll the screen to the left so as
12824 to reveal the next portion of the row. */
12825 || (row_r2l_p
12826 && ((cursor_row->enabled_p
12827 /* FIXME: It is confusing to set the
12828 truncated_on_right_p flag when R2L rows
12829 are actually truncated on the left. */
12830 && cursor_row->truncated_on_right_p
12831 && w->cursor.x <= h_margin)
12832 || (w->hscroll
12833 && (w->cursor.x >= text_area_width - h_margin))))))
12835 struct it it;
12836 ptrdiff_t hscroll;
12837 struct buffer *saved_current_buffer;
12838 ptrdiff_t pt;
12839 int wanted_x;
12841 /* Find point in a display of infinite width. */
12842 saved_current_buffer = current_buffer;
12843 current_buffer = XBUFFER (w->contents);
12845 if (w == XWINDOW (selected_window))
12846 pt = PT;
12847 else
12848 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12850 /* Move iterator to pt starting at cursor_row->start in
12851 a line with infinite width. */
12852 init_to_row_start (&it, w, cursor_row);
12853 it.last_visible_x = INFINITY;
12854 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12855 current_buffer = saved_current_buffer;
12857 /* Position cursor in window. */
12858 if (!hscroll_relative_p && hscroll_step_abs == 0)
12859 hscroll = max (0, (it.current_x
12860 - (ITERATOR_AT_END_OF_LINE_P (&it)
12861 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12862 : (text_area_width / 2))))
12863 / FRAME_COLUMN_WIDTH (it.f);
12864 else if ((!row_r2l_p
12865 && w->cursor.x >= text_area_width - h_margin)
12866 || (row_r2l_p && w->cursor.x <= h_margin))
12868 if (hscroll_relative_p)
12869 wanted_x = text_area_width * (1 - hscroll_step_rel)
12870 - h_margin;
12871 else
12872 wanted_x = text_area_width
12873 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12874 - h_margin;
12875 hscroll
12876 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12878 else
12880 if (hscroll_relative_p)
12881 wanted_x = text_area_width * hscroll_step_rel
12882 + h_margin;
12883 else
12884 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12885 + h_margin;
12886 hscroll
12887 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12889 hscroll = max (hscroll, w->min_hscroll);
12891 /* Don't prevent redisplay optimizations if hscroll
12892 hasn't changed, as it will unnecessarily slow down
12893 redisplay. */
12894 if (w->hscroll != hscroll)
12896 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12897 w->hscroll = hscroll;
12898 hscrolled_p = 1;
12903 window = w->next;
12906 /* Value is non-zero if hscroll of any leaf window has been changed. */
12907 return hscrolled_p;
12911 /* Set hscroll so that cursor is visible and not inside horizontal
12912 scroll margins for all windows in the tree rooted at WINDOW. See
12913 also hscroll_window_tree above. Value is non-zero if any window's
12914 hscroll has been changed. If it has, desired matrices on the frame
12915 of WINDOW are cleared. */
12917 static int
12918 hscroll_windows (Lisp_Object window)
12920 int hscrolled_p = hscroll_window_tree (window);
12921 if (hscrolled_p)
12922 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12923 return hscrolled_p;
12928 /************************************************************************
12929 Redisplay
12930 ************************************************************************/
12932 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12933 to a non-zero value. This is sometimes handy to have in a debugger
12934 session. */
12936 #ifdef GLYPH_DEBUG
12938 /* First and last unchanged row for try_window_id. */
12940 static int debug_first_unchanged_at_end_vpos;
12941 static int debug_last_unchanged_at_beg_vpos;
12943 /* Delta vpos and y. */
12945 static int debug_dvpos, debug_dy;
12947 /* Delta in characters and bytes for try_window_id. */
12949 static ptrdiff_t debug_delta, debug_delta_bytes;
12951 /* Values of window_end_pos and window_end_vpos at the end of
12952 try_window_id. */
12954 static ptrdiff_t debug_end_vpos;
12956 /* Append a string to W->desired_matrix->method. FMT is a printf
12957 format string. If trace_redisplay_p is true also printf the
12958 resulting string to stderr. */
12960 static void debug_method_add (struct window *, char const *, ...)
12961 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12963 static void
12964 debug_method_add (struct window *w, char const *fmt, ...)
12966 void *ptr = w;
12967 char *method = w->desired_matrix->method;
12968 int len = strlen (method);
12969 int size = sizeof w->desired_matrix->method;
12970 int remaining = size - len - 1;
12971 va_list ap;
12973 if (len && remaining)
12975 method[len] = '|';
12976 --remaining, ++len;
12979 va_start (ap, fmt);
12980 vsnprintf (method + len, remaining + 1, fmt, ap);
12981 va_end (ap);
12983 if (trace_redisplay_p)
12984 fprintf (stderr, "%p (%s): %s\n",
12985 ptr,
12986 ((BUFFERP (w->contents)
12987 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12988 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12989 : "no buffer"),
12990 method + len);
12993 #endif /* GLYPH_DEBUG */
12996 /* Value is non-zero if all changes in window W, which displays
12997 current_buffer, are in the text between START and END. START is a
12998 buffer position, END is given as a distance from Z. Used in
12999 redisplay_internal for display optimization. */
13001 static int
13002 text_outside_line_unchanged_p (struct window *w,
13003 ptrdiff_t start, ptrdiff_t end)
13005 int unchanged_p = 1;
13007 /* If text or overlays have changed, see where. */
13008 if (window_outdated (w))
13010 /* Gap in the line? */
13011 if (GPT < start || Z - GPT < end)
13012 unchanged_p = 0;
13014 /* Changes start in front of the line, or end after it? */
13015 if (unchanged_p
13016 && (BEG_UNCHANGED < start - 1
13017 || END_UNCHANGED < end))
13018 unchanged_p = 0;
13020 /* If selective display, can't optimize if changes start at the
13021 beginning of the line. */
13022 if (unchanged_p
13023 && INTEGERP (BVAR (current_buffer, selective_display))
13024 && XINT (BVAR (current_buffer, selective_display)) > 0
13025 && (BEG_UNCHANGED < start || GPT <= start))
13026 unchanged_p = 0;
13028 /* If there are overlays at the start or end of the line, these
13029 may have overlay strings with newlines in them. A change at
13030 START, for instance, may actually concern the display of such
13031 overlay strings as well, and they are displayed on different
13032 lines. So, quickly rule out this case. (For the future, it
13033 might be desirable to implement something more telling than
13034 just BEG/END_UNCHANGED.) */
13035 if (unchanged_p)
13037 if (BEG + BEG_UNCHANGED == start
13038 && overlay_touches_p (start))
13039 unchanged_p = 0;
13040 if (END_UNCHANGED == end
13041 && overlay_touches_p (Z - end))
13042 unchanged_p = 0;
13045 /* Under bidi reordering, adding or deleting a character in the
13046 beginning of a paragraph, before the first strong directional
13047 character, can change the base direction of the paragraph (unless
13048 the buffer specifies a fixed paragraph direction), which will
13049 require to redisplay the whole paragraph. It might be worthwhile
13050 to find the paragraph limits and widen the range of redisplayed
13051 lines to that, but for now just give up this optimization. */
13052 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13053 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13054 unchanged_p = 0;
13057 return unchanged_p;
13061 /* Do a frame update, taking possible shortcuts into account. This is
13062 the main external entry point for redisplay.
13064 If the last redisplay displayed an echo area message and that message
13065 is no longer requested, we clear the echo area or bring back the
13066 mini-buffer if that is in use. */
13068 void
13069 redisplay (void)
13071 redisplay_internal ();
13075 static Lisp_Object
13076 overlay_arrow_string_or_property (Lisp_Object var)
13078 Lisp_Object val;
13080 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13081 return val;
13083 return Voverlay_arrow_string;
13086 /* Return 1 if there are any overlay-arrows in current_buffer. */
13087 static int
13088 overlay_arrow_in_current_buffer_p (void)
13090 Lisp_Object vlist;
13092 for (vlist = Voverlay_arrow_variable_list;
13093 CONSP (vlist);
13094 vlist = XCDR (vlist))
13096 Lisp_Object var = XCAR (vlist);
13097 Lisp_Object val;
13099 if (!SYMBOLP (var))
13100 continue;
13101 val = find_symbol_value (var);
13102 if (MARKERP (val)
13103 && current_buffer == XMARKER (val)->buffer)
13104 return 1;
13106 return 0;
13110 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13111 has changed. */
13113 static int
13114 overlay_arrows_changed_p (void)
13116 Lisp_Object vlist;
13118 for (vlist = Voverlay_arrow_variable_list;
13119 CONSP (vlist);
13120 vlist = XCDR (vlist))
13122 Lisp_Object var = XCAR (vlist);
13123 Lisp_Object val, pstr;
13125 if (!SYMBOLP (var))
13126 continue;
13127 val = find_symbol_value (var);
13128 if (!MARKERP (val))
13129 continue;
13130 if (! EQ (COERCE_MARKER (val),
13131 Fget (var, Qlast_arrow_position))
13132 || ! (pstr = overlay_arrow_string_or_property (var),
13133 EQ (pstr, Fget (var, Qlast_arrow_string))))
13134 return 1;
13136 return 0;
13139 /* Mark overlay arrows to be updated on next redisplay. */
13141 static void
13142 update_overlay_arrows (int up_to_date)
13144 Lisp_Object vlist;
13146 for (vlist = Voverlay_arrow_variable_list;
13147 CONSP (vlist);
13148 vlist = XCDR (vlist))
13150 Lisp_Object var = XCAR (vlist);
13152 if (!SYMBOLP (var))
13153 continue;
13155 if (up_to_date > 0)
13157 Lisp_Object val = find_symbol_value (var);
13158 Fput (var, Qlast_arrow_position,
13159 COERCE_MARKER (val));
13160 Fput (var, Qlast_arrow_string,
13161 overlay_arrow_string_or_property (var));
13163 else if (up_to_date < 0
13164 || !NILP (Fget (var, Qlast_arrow_position)))
13166 Fput (var, Qlast_arrow_position, Qt);
13167 Fput (var, Qlast_arrow_string, Qt);
13173 /* Return overlay arrow string to display at row.
13174 Return integer (bitmap number) for arrow bitmap in left fringe.
13175 Return nil if no overlay arrow. */
13177 static Lisp_Object
13178 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13180 Lisp_Object vlist;
13182 for (vlist = Voverlay_arrow_variable_list;
13183 CONSP (vlist);
13184 vlist = XCDR (vlist))
13186 Lisp_Object var = XCAR (vlist);
13187 Lisp_Object val;
13189 if (!SYMBOLP (var))
13190 continue;
13192 val = find_symbol_value (var);
13194 if (MARKERP (val)
13195 && current_buffer == XMARKER (val)->buffer
13196 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13198 if (FRAME_WINDOW_P (it->f)
13199 /* FIXME: if ROW->reversed_p is set, this should test
13200 the right fringe, not the left one. */
13201 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13203 #ifdef HAVE_WINDOW_SYSTEM
13204 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13206 int fringe_bitmap;
13207 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13208 return make_number (fringe_bitmap);
13210 #endif
13211 return make_number (-1); /* Use default arrow bitmap. */
13213 return overlay_arrow_string_or_property (var);
13217 return Qnil;
13220 /* Return 1 if point moved out of or into a composition. Otherwise
13221 return 0. PREV_BUF and PREV_PT are the last point buffer and
13222 position. BUF and PT are the current point buffer and position. */
13224 static int
13225 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13226 struct buffer *buf, ptrdiff_t pt)
13228 ptrdiff_t start, end;
13229 Lisp_Object prop;
13230 Lisp_Object buffer;
13232 XSETBUFFER (buffer, buf);
13233 /* Check a composition at the last point if point moved within the
13234 same buffer. */
13235 if (prev_buf == buf)
13237 if (prev_pt == pt)
13238 /* Point didn't move. */
13239 return 0;
13241 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13242 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13243 && composition_valid_p (start, end, prop)
13244 && start < prev_pt && end > prev_pt)
13245 /* The last point was within the composition. Return 1 iff
13246 point moved out of the composition. */
13247 return (pt <= start || pt >= end);
13250 /* Check a composition at the current point. */
13251 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13252 && find_composition (pt, -1, &start, &end, &prop, buffer)
13253 && composition_valid_p (start, end, prop)
13254 && start < pt && end > pt);
13257 /* Reconsider the clip changes of buffer which is displayed in W. */
13259 static void
13260 reconsider_clip_changes (struct window *w)
13262 struct buffer *b = XBUFFER (w->contents);
13264 if (b->clip_changed
13265 && w->window_end_valid
13266 && w->current_matrix->buffer == b
13267 && w->current_matrix->zv == BUF_ZV (b)
13268 && w->current_matrix->begv == BUF_BEGV (b))
13269 b->clip_changed = 0;
13271 /* If display wasn't paused, and W is not a tool bar window, see if
13272 point has been moved into or out of a composition. In that case,
13273 we set b->clip_changed to 1 to force updating the screen. If
13274 b->clip_changed has already been set to 1, we can skip this
13275 check. */
13276 if (!b->clip_changed && w->window_end_valid)
13278 ptrdiff_t pt = (w == XWINDOW (selected_window)
13279 ? PT : marker_position (w->pointm));
13281 if ((w->current_matrix->buffer != b || pt != w->last_point)
13282 && check_point_in_composition (w->current_matrix->buffer,
13283 w->last_point, b, pt))
13284 b->clip_changed = 1;
13288 static void
13289 propagate_buffer_redisplay (void)
13290 { /* Resetting b->text->redisplay is problematic!
13291 We can't just reset it in the case that some window that displays
13292 it has not been redisplayed; and such a window can stay
13293 unredisplayed for a long time if it's currently invisible.
13294 But we do want to reset it at the end of redisplay otherwise
13295 its displayed windows will keep being redisplayed over and over
13296 again.
13297 So we copy all b->text->redisplay flags up to their windows here,
13298 such that mark_window_display_accurate can safely reset
13299 b->text->redisplay. */
13300 Lisp_Object ws = window_list ();
13301 for (; CONSP (ws); ws = XCDR (ws))
13303 struct window *thisw = XWINDOW (XCAR (ws));
13304 struct buffer *thisb = XBUFFER (thisw->contents);
13305 if (thisb->text->redisplay)
13306 thisw->redisplay = true;
13310 #define STOP_POLLING \
13311 do { if (! polling_stopped_here) stop_polling (); \
13312 polling_stopped_here = 1; } while (0)
13314 #define RESUME_POLLING \
13315 do { if (polling_stopped_here) start_polling (); \
13316 polling_stopped_here = 0; } while (0)
13319 /* Perhaps in the future avoid recentering windows if it
13320 is not necessary; currently that causes some problems. */
13322 static void
13323 redisplay_internal (void)
13325 struct window *w = XWINDOW (selected_window);
13326 struct window *sw;
13327 struct frame *fr;
13328 bool pending;
13329 bool must_finish = 0, match_p;
13330 struct text_pos tlbufpos, tlendpos;
13331 int number_of_visible_frames;
13332 ptrdiff_t count;
13333 struct frame *sf;
13334 int polling_stopped_here = 0;
13335 Lisp_Object tail, frame;
13337 /* True means redisplay has to consider all windows on all
13338 frames. False, only selected_window is considered. */
13339 bool consider_all_windows_p;
13341 /* True means redisplay has to redisplay the miniwindow. */
13342 bool update_miniwindow_p = false;
13344 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13346 /* No redisplay if running in batch mode or frame is not yet fully
13347 initialized, or redisplay is explicitly turned off by setting
13348 Vinhibit_redisplay. */
13349 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13350 || !NILP (Vinhibit_redisplay))
13351 return;
13353 /* Don't examine these until after testing Vinhibit_redisplay.
13354 When Emacs is shutting down, perhaps because its connection to
13355 X has dropped, we should not look at them at all. */
13356 fr = XFRAME (w->frame);
13357 sf = SELECTED_FRAME ();
13359 if (!fr->glyphs_initialized_p)
13360 return;
13362 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13363 if (popup_activated ())
13364 return;
13365 #endif
13367 /* I don't think this happens but let's be paranoid. */
13368 if (redisplaying_p)
13369 return;
13371 /* Record a function that clears redisplaying_p
13372 when we leave this function. */
13373 count = SPECPDL_INDEX ();
13374 record_unwind_protect_void (unwind_redisplay);
13375 redisplaying_p = 1;
13376 specbind (Qinhibit_free_realized_faces, Qnil);
13378 /* Record this function, so it appears on the profiler's backtraces. */
13379 record_in_backtrace (Qredisplay_internal, 0, 0);
13381 FOR_EACH_FRAME (tail, frame)
13382 XFRAME (frame)->already_hscrolled_p = 0;
13384 retry:
13385 /* Remember the currently selected window. */
13386 sw = w;
13388 pending = false;
13389 last_escape_glyph_frame = NULL;
13390 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13391 last_glyphless_glyph_frame = NULL;
13392 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13394 /* If face_change_count is non-zero, init_iterator will free all
13395 realized faces, which includes the faces referenced from current
13396 matrices. So, we can't reuse current matrices in this case. */
13397 if (face_change_count)
13398 windows_or_buffers_changed = 47;
13400 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13401 && FRAME_TTY (sf)->previous_frame != sf)
13403 /* Since frames on a single ASCII terminal share the same
13404 display area, displaying a different frame means redisplay
13405 the whole thing. */
13406 SET_FRAME_GARBAGED (sf);
13407 #ifndef DOS_NT
13408 set_tty_color_mode (FRAME_TTY (sf), sf);
13409 #endif
13410 FRAME_TTY (sf)->previous_frame = sf;
13413 /* Set the visible flags for all frames. Do this before checking for
13414 resized or garbaged frames; they want to know if their frames are
13415 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13416 number_of_visible_frames = 0;
13418 FOR_EACH_FRAME (tail, frame)
13420 struct frame *f = XFRAME (frame);
13422 if (FRAME_VISIBLE_P (f))
13424 ++number_of_visible_frames;
13425 /* Adjust matrices for visible frames only. */
13426 if (f->fonts_changed)
13428 adjust_frame_glyphs (f);
13429 f->fonts_changed = 0;
13431 /* If cursor type has been changed on the frame
13432 other than selected, consider all frames. */
13433 if (f != sf && f->cursor_type_changed)
13434 update_mode_lines = 31;
13436 clear_desired_matrices (f);
13439 /* Notice any pending interrupt request to change frame size. */
13440 do_pending_window_change (true);
13442 /* do_pending_window_change could change the selected_window due to
13443 frame resizing which makes the selected window too small. */
13444 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13445 sw = w;
13447 /* Clear frames marked as garbaged. */
13448 clear_garbaged_frames ();
13450 /* Build menubar and tool-bar items. */
13451 if (NILP (Vmemory_full))
13452 prepare_menu_bars ();
13454 reconsider_clip_changes (w);
13456 /* In most cases selected window displays current buffer. */
13457 match_p = XBUFFER (w->contents) == current_buffer;
13458 if (match_p)
13460 /* Detect case that we need to write or remove a star in the mode line. */
13461 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13462 w->update_mode_line = 1;
13464 if (mode_line_update_needed (w))
13465 w->update_mode_line = 1;
13467 /* If reconsider_clip_changes above decided that the narrowing
13468 in the current buffer changed, make sure all other windows
13469 showing that buffer will be redisplayed. */
13470 if (current_buffer->clip_changed)
13471 bset_update_mode_line (current_buffer);
13474 /* Normally the message* functions will have already displayed and
13475 updated the echo area, but the frame may have been trashed, or
13476 the update may have been preempted, so display the echo area
13477 again here. Checking message_cleared_p captures the case that
13478 the echo area should be cleared. */
13479 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13480 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13481 || (message_cleared_p
13482 && minibuf_level == 0
13483 /* If the mini-window is currently selected, this means the
13484 echo-area doesn't show through. */
13485 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13487 int window_height_changed_p = echo_area_display (false);
13489 if (message_cleared_p)
13490 update_miniwindow_p = true;
13492 must_finish = 1;
13494 /* If we don't display the current message, don't clear the
13495 message_cleared_p flag, because, if we did, we wouldn't clear
13496 the echo area in the next redisplay which doesn't preserve
13497 the echo area. */
13498 if (!display_last_displayed_message_p)
13499 message_cleared_p = 0;
13501 if (window_height_changed_p)
13503 windows_or_buffers_changed = 50;
13505 /* If window configuration was changed, frames may have been
13506 marked garbaged. Clear them or we will experience
13507 surprises wrt scrolling. */
13508 clear_garbaged_frames ();
13511 else if (EQ (selected_window, minibuf_window)
13512 && (current_buffer->clip_changed || window_outdated (w))
13513 && resize_mini_window (w, 0))
13515 /* Resized active mini-window to fit the size of what it is
13516 showing if its contents might have changed. */
13517 must_finish = 1;
13519 /* If window configuration was changed, frames may have been
13520 marked garbaged. Clear them or we will experience
13521 surprises wrt scrolling. */
13522 clear_garbaged_frames ();
13525 if (windows_or_buffers_changed && !update_mode_lines)
13526 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13527 only the windows's contents needs to be refreshed, or whether the
13528 mode-lines also need a refresh. */
13529 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13530 ? REDISPLAY_SOME : 32);
13532 /* If specs for an arrow have changed, do thorough redisplay
13533 to ensure we remove any arrow that should no longer exist. */
13534 if (overlay_arrows_changed_p ())
13535 /* Apparently, this is the only case where we update other windows,
13536 without updating other mode-lines. */
13537 windows_or_buffers_changed = 49;
13539 consider_all_windows_p = (update_mode_lines
13540 || windows_or_buffers_changed);
13542 #define AINC(a,i) \
13543 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13544 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13546 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13547 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13549 /* Optimize the case that only the line containing the cursor in the
13550 selected window has changed. Variables starting with this_ are
13551 set in display_line and record information about the line
13552 containing the cursor. */
13553 tlbufpos = this_line_start_pos;
13554 tlendpos = this_line_end_pos;
13555 if (!consider_all_windows_p
13556 && CHARPOS (tlbufpos) > 0
13557 && !w->update_mode_line
13558 && !current_buffer->clip_changed
13559 && !current_buffer->prevent_redisplay_optimizations_p
13560 && FRAME_VISIBLE_P (XFRAME (w->frame))
13561 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13562 && !XFRAME (w->frame)->cursor_type_changed
13563 /* Make sure recorded data applies to current buffer, etc. */
13564 && this_line_buffer == current_buffer
13565 && match_p
13566 && !w->force_start
13567 && !w->optional_new_start
13568 /* Point must be on the line that we have info recorded about. */
13569 && PT >= CHARPOS (tlbufpos)
13570 && PT <= Z - CHARPOS (tlendpos)
13571 /* All text outside that line, including its final newline,
13572 must be unchanged. */
13573 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13574 CHARPOS (tlendpos)))
13576 if (CHARPOS (tlbufpos) > BEGV
13577 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13578 && (CHARPOS (tlbufpos) == ZV
13579 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13580 /* Former continuation line has disappeared by becoming empty. */
13581 goto cancel;
13582 else if (window_outdated (w) || MINI_WINDOW_P (w))
13584 /* We have to handle the case of continuation around a
13585 wide-column character (see the comment in indent.c around
13586 line 1340).
13588 For instance, in the following case:
13590 -------- Insert --------
13591 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13592 J_I_ ==> J_I_ `^^' are cursors.
13593 ^^ ^^
13594 -------- --------
13596 As we have to redraw the line above, we cannot use this
13597 optimization. */
13599 struct it it;
13600 int line_height_before = this_line_pixel_height;
13602 /* Note that start_display will handle the case that the
13603 line starting at tlbufpos is a continuation line. */
13604 start_display (&it, w, tlbufpos);
13606 /* Implementation note: It this still necessary? */
13607 if (it.current_x != this_line_start_x)
13608 goto cancel;
13610 TRACE ((stderr, "trying display optimization 1\n"));
13611 w->cursor.vpos = -1;
13612 overlay_arrow_seen = 0;
13613 it.vpos = this_line_vpos;
13614 it.current_y = this_line_y;
13615 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13616 display_line (&it);
13618 /* If line contains point, is not continued,
13619 and ends at same distance from eob as before, we win. */
13620 if (w->cursor.vpos >= 0
13621 /* Line is not continued, otherwise this_line_start_pos
13622 would have been set to 0 in display_line. */
13623 && CHARPOS (this_line_start_pos)
13624 /* Line ends as before. */
13625 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13626 /* Line has same height as before. Otherwise other lines
13627 would have to be shifted up or down. */
13628 && this_line_pixel_height == line_height_before)
13630 /* If this is not the window's last line, we must adjust
13631 the charstarts of the lines below. */
13632 if (it.current_y < it.last_visible_y)
13634 struct glyph_row *row
13635 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13636 ptrdiff_t delta, delta_bytes;
13638 /* We used to distinguish between two cases here,
13639 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13640 when the line ends in a newline or the end of the
13641 buffer's accessible portion. But both cases did
13642 the same, so they were collapsed. */
13643 delta = (Z
13644 - CHARPOS (tlendpos)
13645 - MATRIX_ROW_START_CHARPOS (row));
13646 delta_bytes = (Z_BYTE
13647 - BYTEPOS (tlendpos)
13648 - MATRIX_ROW_START_BYTEPOS (row));
13650 increment_matrix_positions (w->current_matrix,
13651 this_line_vpos + 1,
13652 w->current_matrix->nrows,
13653 delta, delta_bytes);
13656 /* If this row displays text now but previously didn't,
13657 or vice versa, w->window_end_vpos may have to be
13658 adjusted. */
13659 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13661 if (w->window_end_vpos < this_line_vpos)
13662 w->window_end_vpos = this_line_vpos;
13664 else if (w->window_end_vpos == this_line_vpos
13665 && this_line_vpos > 0)
13666 w->window_end_vpos = this_line_vpos - 1;
13667 w->window_end_valid = 0;
13669 /* Update hint: No need to try to scroll in update_window. */
13670 w->desired_matrix->no_scrolling_p = 1;
13672 #ifdef GLYPH_DEBUG
13673 *w->desired_matrix->method = 0;
13674 debug_method_add (w, "optimization 1");
13675 #endif
13676 #ifdef HAVE_WINDOW_SYSTEM
13677 update_window_fringes (w, 0);
13678 #endif
13679 goto update;
13681 else
13682 goto cancel;
13684 else if (/* Cursor position hasn't changed. */
13685 PT == w->last_point
13686 /* Make sure the cursor was last displayed
13687 in this window. Otherwise we have to reposition it. */
13689 /* PXW: Must be converted to pixels, probably. */
13690 && 0 <= w->cursor.vpos
13691 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13693 if (!must_finish)
13695 do_pending_window_change (true);
13696 /* If selected_window changed, redisplay again. */
13697 if (WINDOWP (selected_window)
13698 && (w = XWINDOW (selected_window)) != sw)
13699 goto retry;
13701 /* We used to always goto end_of_redisplay here, but this
13702 isn't enough if we have a blinking cursor. */
13703 if (w->cursor_off_p == w->last_cursor_off_p)
13704 goto end_of_redisplay;
13706 goto update;
13708 /* If highlighting the region, or if the cursor is in the echo area,
13709 then we can't just move the cursor. */
13710 else if (NILP (Vshow_trailing_whitespace)
13711 && !cursor_in_echo_area)
13713 struct it it;
13714 struct glyph_row *row;
13716 /* Skip from tlbufpos to PT and see where it is. Note that
13717 PT may be in invisible text. If so, we will end at the
13718 next visible position. */
13719 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13720 NULL, DEFAULT_FACE_ID);
13721 it.current_x = this_line_start_x;
13722 it.current_y = this_line_y;
13723 it.vpos = this_line_vpos;
13725 /* The call to move_it_to stops in front of PT, but
13726 moves over before-strings. */
13727 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13729 if (it.vpos == this_line_vpos
13730 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13731 row->enabled_p))
13733 eassert (this_line_vpos == it.vpos);
13734 eassert (this_line_y == it.current_y);
13735 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13736 #ifdef GLYPH_DEBUG
13737 *w->desired_matrix->method = 0;
13738 debug_method_add (w, "optimization 3");
13739 #endif
13740 goto update;
13742 else
13743 goto cancel;
13746 cancel:
13747 /* Text changed drastically or point moved off of line. */
13748 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13751 CHARPOS (this_line_start_pos) = 0;
13752 ++clear_face_cache_count;
13753 #ifdef HAVE_WINDOW_SYSTEM
13754 ++clear_image_cache_count;
13755 #endif
13757 /* Build desired matrices, and update the display. If
13758 consider_all_windows_p is non-zero, do it for all windows on all
13759 frames. Otherwise do it for selected_window, only. */
13761 if (consider_all_windows_p)
13763 FOR_EACH_FRAME (tail, frame)
13764 XFRAME (frame)->updated_p = 0;
13766 propagate_buffer_redisplay ();
13768 FOR_EACH_FRAME (tail, frame)
13770 struct frame *f = XFRAME (frame);
13772 /* We don't have to do anything for unselected terminal
13773 frames. */
13774 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13775 && !EQ (FRAME_TTY (f)->top_frame, frame))
13776 continue;
13778 retry_frame:
13780 #if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
13781 /* Redisplay internal tool bar if this is the first time so we
13782 can adjust the frame height right now, if necessary. */
13783 if (!f->tool_bar_redisplayed_once)
13785 if (redisplay_tool_bar (f))
13786 adjust_frame_glyphs (f);
13787 f->tool_bar_redisplayed_once = true;
13789 #endif
13791 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13793 bool gcscrollbars
13794 /* Only GC scrollbars when we redisplay the whole frame. */
13795 = f->redisplay || !REDISPLAY_SOME_P ();
13796 /* Mark all the scroll bars to be removed; we'll redeem
13797 the ones we want when we redisplay their windows. */
13798 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13799 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13801 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13802 redisplay_windows (FRAME_ROOT_WINDOW (f));
13803 /* Remember that the invisible frames need to be redisplayed next
13804 time they're visible. */
13805 else if (!REDISPLAY_SOME_P ())
13806 f->redisplay = true;
13808 /* The X error handler may have deleted that frame. */
13809 if (!FRAME_LIVE_P (f))
13810 continue;
13812 /* Any scroll bars which redisplay_windows should have
13813 nuked should now go away. */
13814 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13815 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13817 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13819 /* If fonts changed on visible frame, display again. */
13820 if (f->fonts_changed)
13822 adjust_frame_glyphs (f);
13823 f->fonts_changed = false;
13824 goto retry_frame;
13827 /* See if we have to hscroll. */
13828 if (!f->already_hscrolled_p)
13830 f->already_hscrolled_p = true;
13831 if (hscroll_windows (f->root_window))
13832 goto retry_frame;
13835 /* Prevent various kinds of signals during display
13836 update. stdio is not robust about handling
13837 signals, which can cause an apparent I/O error. */
13838 if (interrupt_input)
13839 unrequest_sigio ();
13840 STOP_POLLING;
13842 pending |= update_frame (f, false, false);
13843 f->cursor_type_changed = false;
13844 f->updated_p = true;
13849 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13851 if (!pending)
13853 /* Do the mark_window_display_accurate after all windows have
13854 been redisplayed because this call resets flags in buffers
13855 which are needed for proper redisplay. */
13856 FOR_EACH_FRAME (tail, frame)
13858 struct frame *f = XFRAME (frame);
13859 if (f->updated_p)
13861 f->redisplay = false;
13862 mark_window_display_accurate (f->root_window, 1);
13863 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13864 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13869 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13871 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13872 struct frame *mini_frame;
13874 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13875 /* Use list_of_error, not Qerror, so that
13876 we catch only errors and don't run the debugger. */
13877 internal_condition_case_1 (redisplay_window_1, selected_window,
13878 list_of_error,
13879 redisplay_window_error);
13880 if (update_miniwindow_p)
13881 internal_condition_case_1 (redisplay_window_1, mini_window,
13882 list_of_error,
13883 redisplay_window_error);
13885 /* Compare desired and current matrices, perform output. */
13887 update:
13888 /* If fonts changed, display again. */
13889 if (sf->fonts_changed)
13890 goto retry;
13892 /* Prevent various kinds of signals during display update.
13893 stdio is not robust about handling signals,
13894 which can cause an apparent I/O error. */
13895 if (interrupt_input)
13896 unrequest_sigio ();
13897 STOP_POLLING;
13899 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13901 if (hscroll_windows (selected_window))
13902 goto retry;
13904 XWINDOW (selected_window)->must_be_updated_p = true;
13905 pending = update_frame (sf, false, false);
13906 sf->cursor_type_changed = false;
13909 /* We may have called echo_area_display at the top of this
13910 function. If the echo area is on another frame, that may
13911 have put text on a frame other than the selected one, so the
13912 above call to update_frame would not have caught it. Catch
13913 it here. */
13914 mini_window = FRAME_MINIBUF_WINDOW (sf);
13915 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13917 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13919 XWINDOW (mini_window)->must_be_updated_p = true;
13920 pending |= update_frame (mini_frame, false, false);
13921 mini_frame->cursor_type_changed = false;
13922 if (!pending && hscroll_windows (mini_window))
13923 goto retry;
13927 /* If display was paused because of pending input, make sure we do a
13928 thorough update the next time. */
13929 if (pending)
13931 /* Prevent the optimization at the beginning of
13932 redisplay_internal that tries a single-line update of the
13933 line containing the cursor in the selected window. */
13934 CHARPOS (this_line_start_pos) = 0;
13936 /* Let the overlay arrow be updated the next time. */
13937 update_overlay_arrows (0);
13939 /* If we pause after scrolling, some rows in the current
13940 matrices of some windows are not valid. */
13941 if (!WINDOW_FULL_WIDTH_P (w)
13942 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13943 update_mode_lines = 36;
13945 else
13947 if (!consider_all_windows_p)
13949 /* This has already been done above if
13950 consider_all_windows_p is set. */
13951 if (XBUFFER (w->contents)->text->redisplay
13952 && buffer_window_count (XBUFFER (w->contents)) > 1)
13953 /* This can happen if b->text->redisplay was set during
13954 jit-lock. */
13955 propagate_buffer_redisplay ();
13956 mark_window_display_accurate_1 (w, 1);
13958 /* Say overlay arrows are up to date. */
13959 update_overlay_arrows (1);
13961 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13962 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13965 update_mode_lines = 0;
13966 windows_or_buffers_changed = 0;
13969 /* Start SIGIO interrupts coming again. Having them off during the
13970 code above makes it less likely one will discard output, but not
13971 impossible, since there might be stuff in the system buffer here.
13972 But it is much hairier to try to do anything about that. */
13973 if (interrupt_input)
13974 request_sigio ();
13975 RESUME_POLLING;
13977 /* If a frame has become visible which was not before, redisplay
13978 again, so that we display it. Expose events for such a frame
13979 (which it gets when becoming visible) don't call the parts of
13980 redisplay constructing glyphs, so simply exposing a frame won't
13981 display anything in this case. So, we have to display these
13982 frames here explicitly. */
13983 if (!pending)
13985 int new_count = 0;
13987 FOR_EACH_FRAME (tail, frame)
13989 if (XFRAME (frame)->visible)
13990 new_count++;
13993 if (new_count != number_of_visible_frames)
13994 windows_or_buffers_changed = 52;
13997 /* Change frame size now if a change is pending. */
13998 do_pending_window_change (true);
14000 /* If we just did a pending size change, or have additional
14001 visible frames, or selected_window changed, redisplay again. */
14002 if ((windows_or_buffers_changed && !pending)
14003 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14004 goto retry;
14006 /* Clear the face and image caches.
14008 We used to do this only if consider_all_windows_p. But the cache
14009 needs to be cleared if a timer creates images in the current
14010 buffer (e.g. the test case in Bug#6230). */
14012 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14014 clear_face_cache (false);
14015 clear_face_cache_count = 0;
14018 #ifdef HAVE_WINDOW_SYSTEM
14019 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14021 clear_image_caches (Qnil);
14022 clear_image_cache_count = 0;
14024 #endif /* HAVE_WINDOW_SYSTEM */
14026 end_of_redisplay:
14027 #ifdef HAVE_NS
14028 ns_set_doc_edited ();
14029 #endif
14030 if (interrupt_input && interrupts_deferred)
14031 request_sigio ();
14033 unbind_to (count, Qnil);
14034 RESUME_POLLING;
14038 /* Redisplay, but leave alone any recent echo area message unless
14039 another message has been requested in its place.
14041 This is useful in situations where you need to redisplay but no
14042 user action has occurred, making it inappropriate for the message
14043 area to be cleared. See tracking_off and
14044 wait_reading_process_output for examples of these situations.
14046 FROM_WHERE is an integer saying from where this function was
14047 called. This is useful for debugging. */
14049 void
14050 redisplay_preserve_echo_area (int from_where)
14052 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14054 if (!NILP (echo_area_buffer[1]))
14056 /* We have a previously displayed message, but no current
14057 message. Redisplay the previous message. */
14058 display_last_displayed_message_p = true;
14059 redisplay_internal ();
14060 display_last_displayed_message_p = false;
14062 else
14063 redisplay_internal ();
14065 flush_frame (SELECTED_FRAME ());
14069 /* Function registered with record_unwind_protect in redisplay_internal. */
14071 static void
14072 unwind_redisplay (void)
14074 redisplaying_p = 0;
14078 /* Mark the display of leaf window W as accurate or inaccurate.
14079 If ACCURATE_P is non-zero mark display of W as accurate. If
14080 ACCURATE_P is zero, arrange for W to be redisplayed the next
14081 time redisplay_internal is called. */
14083 static void
14084 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14086 struct buffer *b = XBUFFER (w->contents);
14088 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14089 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14090 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14092 if (accurate_p)
14094 b->clip_changed = false;
14095 b->prevent_redisplay_optimizations_p = false;
14096 eassert (buffer_window_count (b) > 0);
14097 /* Resetting b->text->redisplay is problematic!
14098 In order to make it safer to do it here, redisplay_internal must
14099 have copied all b->text->redisplay to their respective windows. */
14100 b->text->redisplay = false;
14102 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14103 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14104 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14105 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14107 w->current_matrix->buffer = b;
14108 w->current_matrix->begv = BUF_BEGV (b);
14109 w->current_matrix->zv = BUF_ZV (b);
14111 w->last_cursor_vpos = w->cursor.vpos;
14112 w->last_cursor_off_p = w->cursor_off_p;
14114 if (w == XWINDOW (selected_window))
14115 w->last_point = BUF_PT (b);
14116 else
14117 w->last_point = marker_position (w->pointm);
14119 w->window_end_valid = true;
14120 w->update_mode_line = false;
14123 w->redisplay = !accurate_p;
14127 /* Mark the display of windows in the window tree rooted at WINDOW as
14128 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14129 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14130 be redisplayed the next time redisplay_internal is called. */
14132 void
14133 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14135 struct window *w;
14137 for (; !NILP (window); window = w->next)
14139 w = XWINDOW (window);
14140 if (WINDOWP (w->contents))
14141 mark_window_display_accurate (w->contents, accurate_p);
14142 else
14143 mark_window_display_accurate_1 (w, accurate_p);
14146 if (accurate_p)
14147 update_overlay_arrows (1);
14148 else
14149 /* Force a thorough redisplay the next time by setting
14150 last_arrow_position and last_arrow_string to t, which is
14151 unequal to any useful value of Voverlay_arrow_... */
14152 update_overlay_arrows (-1);
14156 /* Return value in display table DP (Lisp_Char_Table *) for character
14157 C. Since a display table doesn't have any parent, we don't have to
14158 follow parent. Do not call this function directly but use the
14159 macro DISP_CHAR_VECTOR. */
14161 Lisp_Object
14162 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14164 Lisp_Object val;
14166 if (ASCII_CHAR_P (c))
14168 val = dp->ascii;
14169 if (SUB_CHAR_TABLE_P (val))
14170 val = XSUB_CHAR_TABLE (val)->contents[c];
14172 else
14174 Lisp_Object table;
14176 XSETCHAR_TABLE (table, dp);
14177 val = char_table_ref (table, c);
14179 if (NILP (val))
14180 val = dp->defalt;
14181 return val;
14186 /***********************************************************************
14187 Window Redisplay
14188 ***********************************************************************/
14190 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14192 static void
14193 redisplay_windows (Lisp_Object window)
14195 while (!NILP (window))
14197 struct window *w = XWINDOW (window);
14199 if (WINDOWP (w->contents))
14200 redisplay_windows (w->contents);
14201 else if (BUFFERP (w->contents))
14203 displayed_buffer = XBUFFER (w->contents);
14204 /* Use list_of_error, not Qerror, so that
14205 we catch only errors and don't run the debugger. */
14206 internal_condition_case_1 (redisplay_window_0, window,
14207 list_of_error,
14208 redisplay_window_error);
14211 window = w->next;
14215 static Lisp_Object
14216 redisplay_window_error (Lisp_Object ignore)
14218 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14219 return Qnil;
14222 static Lisp_Object
14223 redisplay_window_0 (Lisp_Object window)
14225 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14226 redisplay_window (window, false);
14227 return Qnil;
14230 static Lisp_Object
14231 redisplay_window_1 (Lisp_Object window)
14233 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14234 redisplay_window (window, true);
14235 return Qnil;
14239 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14240 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14241 which positions recorded in ROW differ from current buffer
14242 positions.
14244 Return 0 if cursor is not on this row, 1 otherwise. */
14246 static int
14247 set_cursor_from_row (struct window *w, struct glyph_row *row,
14248 struct glyph_matrix *matrix,
14249 ptrdiff_t delta, ptrdiff_t delta_bytes,
14250 int dy, int dvpos)
14252 struct glyph *glyph = row->glyphs[TEXT_AREA];
14253 struct glyph *end = glyph + row->used[TEXT_AREA];
14254 struct glyph *cursor = NULL;
14255 /* The last known character position in row. */
14256 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14257 int x = row->x;
14258 ptrdiff_t pt_old = PT - delta;
14259 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14260 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14261 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14262 /* A glyph beyond the edge of TEXT_AREA which we should never
14263 touch. */
14264 struct glyph *glyphs_end = end;
14265 /* Non-zero means we've found a match for cursor position, but that
14266 glyph has the avoid_cursor_p flag set. */
14267 int match_with_avoid_cursor = 0;
14268 /* Non-zero means we've seen at least one glyph that came from a
14269 display string. */
14270 int string_seen = 0;
14271 /* Largest and smallest buffer positions seen so far during scan of
14272 glyph row. */
14273 ptrdiff_t bpos_max = pos_before;
14274 ptrdiff_t bpos_min = pos_after;
14275 /* Last buffer position covered by an overlay string with an integer
14276 `cursor' property. */
14277 ptrdiff_t bpos_covered = 0;
14278 /* Non-zero means the display string on which to display the cursor
14279 comes from a text property, not from an overlay. */
14280 int string_from_text_prop = 0;
14282 /* Don't even try doing anything if called for a mode-line or
14283 header-line row, since the rest of the code isn't prepared to
14284 deal with such calamities. */
14285 eassert (!row->mode_line_p);
14286 if (row->mode_line_p)
14287 return 0;
14289 /* Skip over glyphs not having an object at the start and the end of
14290 the row. These are special glyphs like truncation marks on
14291 terminal frames. */
14292 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14294 if (!row->reversed_p)
14296 while (glyph < end
14297 && NILP (glyph->object)
14298 && glyph->charpos < 0)
14300 x += glyph->pixel_width;
14301 ++glyph;
14303 while (end > glyph
14304 && NILP ((end - 1)->object)
14305 /* CHARPOS is zero for blanks and stretch glyphs
14306 inserted by extend_face_to_end_of_line. */
14307 && (end - 1)->charpos <= 0)
14308 --end;
14309 glyph_before = glyph - 1;
14310 glyph_after = end;
14312 else
14314 struct glyph *g;
14316 /* If the glyph row is reversed, we need to process it from back
14317 to front, so swap the edge pointers. */
14318 glyphs_end = end = glyph - 1;
14319 glyph += row->used[TEXT_AREA] - 1;
14321 while (glyph > end + 1
14322 && NILP (glyph->object)
14323 && glyph->charpos < 0)
14325 --glyph;
14326 x -= glyph->pixel_width;
14328 if (NILP (glyph->object) && glyph->charpos < 0)
14329 --glyph;
14330 /* By default, in reversed rows we put the cursor on the
14331 rightmost (first in the reading order) glyph. */
14332 for (g = end + 1; g < glyph; g++)
14333 x += g->pixel_width;
14334 while (end < glyph
14335 && NILP ((end + 1)->object)
14336 && (end + 1)->charpos <= 0)
14337 ++end;
14338 glyph_before = glyph + 1;
14339 glyph_after = end;
14342 else if (row->reversed_p)
14344 /* In R2L rows that don't display text, put the cursor on the
14345 rightmost glyph. Case in point: an empty last line that is
14346 part of an R2L paragraph. */
14347 cursor = end - 1;
14348 /* Avoid placing the cursor on the last glyph of the row, where
14349 on terminal frames we hold the vertical border between
14350 adjacent windows. */
14351 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14352 && !WINDOW_RIGHTMOST_P (w)
14353 && cursor == row->glyphs[LAST_AREA] - 1)
14354 cursor--;
14355 x = -1; /* will be computed below, at label compute_x */
14358 /* Step 1: Try to find the glyph whose character position
14359 corresponds to point. If that's not possible, find 2 glyphs
14360 whose character positions are the closest to point, one before
14361 point, the other after it. */
14362 if (!row->reversed_p)
14363 while (/* not marched to end of glyph row */
14364 glyph < end
14365 /* glyph was not inserted by redisplay for internal purposes */
14366 && !NILP (glyph->object))
14368 if (BUFFERP (glyph->object))
14370 ptrdiff_t dpos = glyph->charpos - pt_old;
14372 if (glyph->charpos > bpos_max)
14373 bpos_max = glyph->charpos;
14374 if (glyph->charpos < bpos_min)
14375 bpos_min = glyph->charpos;
14376 if (!glyph->avoid_cursor_p)
14378 /* If we hit point, we've found the glyph on which to
14379 display the cursor. */
14380 if (dpos == 0)
14382 match_with_avoid_cursor = 0;
14383 break;
14385 /* See if we've found a better approximation to
14386 POS_BEFORE or to POS_AFTER. */
14387 if (0 > dpos && dpos > pos_before - pt_old)
14389 pos_before = glyph->charpos;
14390 glyph_before = glyph;
14392 else if (0 < dpos && dpos < pos_after - pt_old)
14394 pos_after = glyph->charpos;
14395 glyph_after = glyph;
14398 else if (dpos == 0)
14399 match_with_avoid_cursor = 1;
14401 else if (STRINGP (glyph->object))
14403 Lisp_Object chprop;
14404 ptrdiff_t glyph_pos = glyph->charpos;
14406 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14407 glyph->object);
14408 if (!NILP (chprop))
14410 /* If the string came from a `display' text property,
14411 look up the buffer position of that property and
14412 use that position to update bpos_max, as if we
14413 actually saw such a position in one of the row's
14414 glyphs. This helps with supporting integer values
14415 of `cursor' property on the display string in
14416 situations where most or all of the row's buffer
14417 text is completely covered by display properties,
14418 so that no glyph with valid buffer positions is
14419 ever seen in the row. */
14420 ptrdiff_t prop_pos =
14421 string_buffer_position_lim (glyph->object, pos_before,
14422 pos_after, 0);
14424 if (prop_pos >= pos_before)
14425 bpos_max = prop_pos;
14427 if (INTEGERP (chprop))
14429 bpos_covered = bpos_max + XINT (chprop);
14430 /* If the `cursor' property covers buffer positions up
14431 to and including point, we should display cursor on
14432 this glyph. Note that, if a `cursor' property on one
14433 of the string's characters has an integer value, we
14434 will break out of the loop below _before_ we get to
14435 the position match above. IOW, integer values of
14436 the `cursor' property override the "exact match for
14437 point" strategy of positioning the cursor. */
14438 /* Implementation note: bpos_max == pt_old when, e.g.,
14439 we are in an empty line, where bpos_max is set to
14440 MATRIX_ROW_START_CHARPOS, see above. */
14441 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14443 cursor = glyph;
14444 break;
14448 string_seen = 1;
14450 x += glyph->pixel_width;
14451 ++glyph;
14453 else if (glyph > end) /* row is reversed */
14454 while (!NILP (glyph->object))
14456 if (BUFFERP (glyph->object))
14458 ptrdiff_t dpos = glyph->charpos - pt_old;
14460 if (glyph->charpos > bpos_max)
14461 bpos_max = glyph->charpos;
14462 if (glyph->charpos < bpos_min)
14463 bpos_min = glyph->charpos;
14464 if (!glyph->avoid_cursor_p)
14466 if (dpos == 0)
14468 match_with_avoid_cursor = 0;
14469 break;
14471 if (0 > dpos && dpos > pos_before - pt_old)
14473 pos_before = glyph->charpos;
14474 glyph_before = glyph;
14476 else if (0 < dpos && dpos < pos_after - pt_old)
14478 pos_after = glyph->charpos;
14479 glyph_after = glyph;
14482 else if (dpos == 0)
14483 match_with_avoid_cursor = 1;
14485 else if (STRINGP (glyph->object))
14487 Lisp_Object chprop;
14488 ptrdiff_t glyph_pos = glyph->charpos;
14490 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14491 glyph->object);
14492 if (!NILP (chprop))
14494 ptrdiff_t prop_pos =
14495 string_buffer_position_lim (glyph->object, pos_before,
14496 pos_after, 0);
14498 if (prop_pos >= pos_before)
14499 bpos_max = prop_pos;
14501 if (INTEGERP (chprop))
14503 bpos_covered = bpos_max + XINT (chprop);
14504 /* If the `cursor' property covers buffer positions up
14505 to and including point, we should display cursor on
14506 this glyph. */
14507 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14509 cursor = glyph;
14510 break;
14513 string_seen = 1;
14515 --glyph;
14516 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14518 x--; /* can't use any pixel_width */
14519 break;
14521 x -= glyph->pixel_width;
14524 /* Step 2: If we didn't find an exact match for point, we need to
14525 look for a proper place to put the cursor among glyphs between
14526 GLYPH_BEFORE and GLYPH_AFTER. */
14527 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14528 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14529 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14531 /* An empty line has a single glyph whose OBJECT is nil and
14532 whose CHARPOS is the position of a newline on that line.
14533 Note that on a TTY, there are more glyphs after that, which
14534 were produced by extend_face_to_end_of_line, but their
14535 CHARPOS is zero or negative. */
14536 int empty_line_p =
14537 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14538 && NILP (glyph->object) && glyph->charpos > 0
14539 /* On a TTY, continued and truncated rows also have a glyph at
14540 their end whose OBJECT is nil and whose CHARPOS is
14541 positive (the continuation and truncation glyphs), but such
14542 rows are obviously not "empty". */
14543 && !(row->continued_p || row->truncated_on_right_p);
14545 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14547 ptrdiff_t ellipsis_pos;
14549 /* Scan back over the ellipsis glyphs. */
14550 if (!row->reversed_p)
14552 ellipsis_pos = (glyph - 1)->charpos;
14553 while (glyph > row->glyphs[TEXT_AREA]
14554 && (glyph - 1)->charpos == ellipsis_pos)
14555 glyph--, x -= glyph->pixel_width;
14556 /* That loop always goes one position too far, including
14557 the glyph before the ellipsis. So scan forward over
14558 that one. */
14559 x += glyph->pixel_width;
14560 glyph++;
14562 else /* row is reversed */
14564 ellipsis_pos = (glyph + 1)->charpos;
14565 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14566 && (glyph + 1)->charpos == ellipsis_pos)
14567 glyph++, x += glyph->pixel_width;
14568 x -= glyph->pixel_width;
14569 glyph--;
14572 else if (match_with_avoid_cursor)
14574 cursor = glyph_after;
14575 x = -1;
14577 else if (string_seen)
14579 int incr = row->reversed_p ? -1 : +1;
14581 /* Need to find the glyph that came out of a string which is
14582 present at point. That glyph is somewhere between
14583 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14584 positioned between POS_BEFORE and POS_AFTER in the
14585 buffer. */
14586 struct glyph *start, *stop;
14587 ptrdiff_t pos = pos_before;
14589 x = -1;
14591 /* If the row ends in a newline from a display string,
14592 reordering could have moved the glyphs belonging to the
14593 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14594 in this case we extend the search to the last glyph in
14595 the row that was not inserted by redisplay. */
14596 if (row->ends_in_newline_from_string_p)
14598 glyph_after = end;
14599 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14602 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14603 correspond to POS_BEFORE and POS_AFTER, respectively. We
14604 need START and STOP in the order that corresponds to the
14605 row's direction as given by its reversed_p flag. If the
14606 directionality of characters between POS_BEFORE and
14607 POS_AFTER is the opposite of the row's base direction,
14608 these characters will have been reordered for display,
14609 and we need to reverse START and STOP. */
14610 if (!row->reversed_p)
14612 start = min (glyph_before, glyph_after);
14613 stop = max (glyph_before, glyph_after);
14615 else
14617 start = max (glyph_before, glyph_after);
14618 stop = min (glyph_before, glyph_after);
14620 for (glyph = start + incr;
14621 row->reversed_p ? glyph > stop : glyph < stop; )
14624 /* Any glyphs that come from the buffer are here because
14625 of bidi reordering. Skip them, and only pay
14626 attention to glyphs that came from some string. */
14627 if (STRINGP (glyph->object))
14629 Lisp_Object str;
14630 ptrdiff_t tem;
14631 /* If the display property covers the newline, we
14632 need to search for it one position farther. */
14633 ptrdiff_t lim = pos_after
14634 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14636 string_from_text_prop = 0;
14637 str = glyph->object;
14638 tem = string_buffer_position_lim (str, pos, lim, 0);
14639 if (tem == 0 /* from overlay */
14640 || pos <= tem)
14642 /* If the string from which this glyph came is
14643 found in the buffer at point, or at position
14644 that is closer to point than pos_after, then
14645 we've found the glyph we've been looking for.
14646 If it comes from an overlay (tem == 0), and
14647 it has the `cursor' property on one of its
14648 glyphs, record that glyph as a candidate for
14649 displaying the cursor. (As in the
14650 unidirectional version, we will display the
14651 cursor on the last candidate we find.) */
14652 if (tem == 0
14653 || tem == pt_old
14654 || (tem - pt_old > 0 && tem < pos_after))
14656 /* The glyphs from this string could have
14657 been reordered. Find the one with the
14658 smallest string position. Or there could
14659 be a character in the string with the
14660 `cursor' property, which means display
14661 cursor on that character's glyph. */
14662 ptrdiff_t strpos = glyph->charpos;
14664 if (tem)
14666 cursor = glyph;
14667 string_from_text_prop = 1;
14669 for ( ;
14670 (row->reversed_p ? glyph > stop : glyph < stop)
14671 && EQ (glyph->object, str);
14672 glyph += incr)
14674 Lisp_Object cprop;
14675 ptrdiff_t gpos = glyph->charpos;
14677 cprop = Fget_char_property (make_number (gpos),
14678 Qcursor,
14679 glyph->object);
14680 if (!NILP (cprop))
14682 cursor = glyph;
14683 break;
14685 if (tem && glyph->charpos < strpos)
14687 strpos = glyph->charpos;
14688 cursor = glyph;
14692 if (tem == pt_old
14693 || (tem - pt_old > 0 && tem < pos_after))
14694 goto compute_x;
14696 if (tem)
14697 pos = tem + 1; /* don't find previous instances */
14699 /* This string is not what we want; skip all of the
14700 glyphs that came from it. */
14701 while ((row->reversed_p ? glyph > stop : glyph < stop)
14702 && EQ (glyph->object, str))
14703 glyph += incr;
14705 else
14706 glyph += incr;
14709 /* If we reached the end of the line, and END was from a string,
14710 the cursor is not on this line. */
14711 if (cursor == NULL
14712 && (row->reversed_p ? glyph <= end : glyph >= end)
14713 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14714 && STRINGP (end->object)
14715 && row->continued_p)
14716 return 0;
14718 /* A truncated row may not include PT among its character positions.
14719 Setting the cursor inside the scroll margin will trigger
14720 recalculation of hscroll in hscroll_window_tree. But if a
14721 display string covers point, defer to the string-handling
14722 code below to figure this out. */
14723 else if (row->truncated_on_left_p && pt_old < bpos_min)
14725 cursor = glyph_before;
14726 x = -1;
14728 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14729 /* Zero-width characters produce no glyphs. */
14730 || (!empty_line_p
14731 && (row->reversed_p
14732 ? glyph_after > glyphs_end
14733 : glyph_after < glyphs_end)))
14735 cursor = glyph_after;
14736 x = -1;
14740 compute_x:
14741 if (cursor != NULL)
14742 glyph = cursor;
14743 else if (glyph == glyphs_end
14744 && pos_before == pos_after
14745 && STRINGP ((row->reversed_p
14746 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14747 : row->glyphs[TEXT_AREA])->object))
14749 /* If all the glyphs of this row came from strings, put the
14750 cursor on the first glyph of the row. This avoids having the
14751 cursor outside of the text area in this very rare and hard
14752 use case. */
14753 glyph =
14754 row->reversed_p
14755 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14756 : row->glyphs[TEXT_AREA];
14758 if (x < 0)
14760 struct glyph *g;
14762 /* Need to compute x that corresponds to GLYPH. */
14763 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14765 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14766 emacs_abort ();
14767 x += g->pixel_width;
14771 /* ROW could be part of a continued line, which, under bidi
14772 reordering, might have other rows whose start and end charpos
14773 occlude point. Only set w->cursor if we found a better
14774 approximation to the cursor position than we have from previously
14775 examined candidate rows belonging to the same continued line. */
14776 if (/* We already have a candidate row. */
14777 w->cursor.vpos >= 0
14778 /* That candidate is not the row we are processing. */
14779 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14780 /* Make sure cursor.vpos specifies a row whose start and end
14781 charpos occlude point, and it is valid candidate for being a
14782 cursor-row. This is because some callers of this function
14783 leave cursor.vpos at the row where the cursor was displayed
14784 during the last redisplay cycle. */
14785 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14786 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14787 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14789 struct glyph *g1
14790 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14792 /* Don't consider glyphs that are outside TEXT_AREA. */
14793 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14794 return 0;
14795 /* Keep the candidate whose buffer position is the closest to
14796 point or has the `cursor' property. */
14797 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14798 w->cursor.hpos >= 0
14799 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14800 && ((BUFFERP (g1->object)
14801 && (g1->charpos == pt_old /* An exact match always wins. */
14802 || (BUFFERP (glyph->object)
14803 && eabs (g1->charpos - pt_old)
14804 < eabs (glyph->charpos - pt_old))))
14805 /* Previous candidate is a glyph from a string that has
14806 a non-nil `cursor' property. */
14807 || (STRINGP (g1->object)
14808 && (!NILP (Fget_char_property (make_number (g1->charpos),
14809 Qcursor, g1->object))
14810 /* Previous candidate is from the same display
14811 string as this one, and the display string
14812 came from a text property. */
14813 || (EQ (g1->object, glyph->object)
14814 && string_from_text_prop)
14815 /* this candidate is from newline and its
14816 position is not an exact match */
14817 || (NILP (glyph->object)
14818 && glyph->charpos != pt_old)))))
14819 return 0;
14820 /* If this candidate gives an exact match, use that. */
14821 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14822 /* If this candidate is a glyph created for the
14823 terminating newline of a line, and point is on that
14824 newline, it wins because it's an exact match. */
14825 || (!row->continued_p
14826 && NILP (glyph->object)
14827 && glyph->charpos == 0
14828 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14829 /* Otherwise, keep the candidate that comes from a row
14830 spanning less buffer positions. This may win when one or
14831 both candidate positions are on glyphs that came from
14832 display strings, for which we cannot compare buffer
14833 positions. */
14834 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14835 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14836 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14837 return 0;
14839 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14840 w->cursor.x = x;
14841 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14842 w->cursor.y = row->y + dy;
14844 if (w == XWINDOW (selected_window))
14846 if (!row->continued_p
14847 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14848 && row->x == 0)
14850 this_line_buffer = XBUFFER (w->contents);
14852 CHARPOS (this_line_start_pos)
14853 = MATRIX_ROW_START_CHARPOS (row) + delta;
14854 BYTEPOS (this_line_start_pos)
14855 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14857 CHARPOS (this_line_end_pos)
14858 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14859 BYTEPOS (this_line_end_pos)
14860 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14862 this_line_y = w->cursor.y;
14863 this_line_pixel_height = row->height;
14864 this_line_vpos = w->cursor.vpos;
14865 this_line_start_x = row->x;
14867 else
14868 CHARPOS (this_line_start_pos) = 0;
14871 return 1;
14875 /* Run window scroll functions, if any, for WINDOW with new window
14876 start STARTP. Sets the window start of WINDOW to that position.
14878 We assume that the window's buffer is really current. */
14880 static struct text_pos
14881 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14883 struct window *w = XWINDOW (window);
14884 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14886 eassert (current_buffer == XBUFFER (w->contents));
14888 if (!NILP (Vwindow_scroll_functions))
14890 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14891 make_number (CHARPOS (startp)));
14892 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14893 /* In case the hook functions switch buffers. */
14894 set_buffer_internal (XBUFFER (w->contents));
14897 return startp;
14901 /* Make sure the line containing the cursor is fully visible.
14902 A value of 1 means there is nothing to be done.
14903 (Either the line is fully visible, or it cannot be made so,
14904 or we cannot tell.)
14906 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14907 is higher than window.
14909 If CURRENT_MATRIX_P is non-zero, use the information from the
14910 window's current glyph matrix; otherwise use the desired glyph
14911 matrix.
14913 A value of 0 means the caller should do scrolling
14914 as if point had gone off the screen. */
14916 static int
14917 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14919 struct glyph_matrix *matrix;
14920 struct glyph_row *row;
14921 int window_height;
14923 if (!make_cursor_line_fully_visible_p)
14924 return 1;
14926 /* It's not always possible to find the cursor, e.g, when a window
14927 is full of overlay strings. Don't do anything in that case. */
14928 if (w->cursor.vpos < 0)
14929 return 1;
14931 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14932 row = MATRIX_ROW (matrix, w->cursor.vpos);
14934 /* If the cursor row is not partially visible, there's nothing to do. */
14935 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14936 return 1;
14938 /* If the row the cursor is in is taller than the window's height,
14939 it's not clear what to do, so do nothing. */
14940 window_height = window_box_height (w);
14941 if (row->height >= window_height)
14943 if (!force_p || MINI_WINDOW_P (w)
14944 || w->vscroll || w->cursor.vpos == 0)
14945 return 1;
14947 return 0;
14951 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14952 non-zero means only WINDOW is redisplayed in redisplay_internal.
14953 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14954 in redisplay_window to bring a partially visible line into view in
14955 the case that only the cursor has moved.
14957 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14958 last screen line's vertical height extends past the end of the screen.
14960 Value is
14962 1 if scrolling succeeded
14964 0 if scrolling didn't find point.
14966 -1 if new fonts have been loaded so that we must interrupt
14967 redisplay, adjust glyph matrices, and try again. */
14969 enum
14971 SCROLLING_SUCCESS,
14972 SCROLLING_FAILED,
14973 SCROLLING_NEED_LARGER_MATRICES
14976 /* If scroll-conservatively is more than this, never recenter.
14978 If you change this, don't forget to update the doc string of
14979 `scroll-conservatively' and the Emacs manual. */
14980 #define SCROLL_LIMIT 100
14982 static int
14983 try_scrolling (Lisp_Object window, int just_this_one_p,
14984 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14985 int temp_scroll_step, int last_line_misfit)
14987 struct window *w = XWINDOW (window);
14988 struct frame *f = XFRAME (w->frame);
14989 struct text_pos pos, startp;
14990 struct it it;
14991 int this_scroll_margin, scroll_max, rc, height;
14992 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14993 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14994 Lisp_Object aggressive;
14995 /* We will never try scrolling more than this number of lines. */
14996 int scroll_limit = SCROLL_LIMIT;
14997 int frame_line_height = default_line_pixel_height (w);
14998 int window_total_lines
14999 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15001 #ifdef GLYPH_DEBUG
15002 debug_method_add (w, "try_scrolling");
15003 #endif
15005 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15007 /* Compute scroll margin height in pixels. We scroll when point is
15008 within this distance from the top or bottom of the window. */
15009 if (scroll_margin > 0)
15010 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15011 * frame_line_height;
15012 else
15013 this_scroll_margin = 0;
15015 /* Force arg_scroll_conservatively to have a reasonable value, to
15016 avoid scrolling too far away with slow move_it_* functions. Note
15017 that the user can supply scroll-conservatively equal to
15018 `most-positive-fixnum', which can be larger than INT_MAX. */
15019 if (arg_scroll_conservatively > scroll_limit)
15021 arg_scroll_conservatively = scroll_limit + 1;
15022 scroll_max = scroll_limit * frame_line_height;
15024 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15025 /* Compute how much we should try to scroll maximally to bring
15026 point into view. */
15027 scroll_max = (max (scroll_step,
15028 max (arg_scroll_conservatively, temp_scroll_step))
15029 * frame_line_height);
15030 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15031 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15032 /* We're trying to scroll because of aggressive scrolling but no
15033 scroll_step is set. Choose an arbitrary one. */
15034 scroll_max = 10 * frame_line_height;
15035 else
15036 scroll_max = 0;
15038 too_near_end:
15040 /* Decide whether to scroll down. */
15041 if (PT > CHARPOS (startp))
15043 int scroll_margin_y;
15045 /* Compute the pixel ypos of the scroll margin, then move IT to
15046 either that ypos or PT, whichever comes first. */
15047 start_display (&it, w, startp);
15048 scroll_margin_y = it.last_visible_y - this_scroll_margin
15049 - frame_line_height * extra_scroll_margin_lines;
15050 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15051 (MOVE_TO_POS | MOVE_TO_Y));
15053 if (PT > CHARPOS (it.current.pos))
15055 int y0 = line_bottom_y (&it);
15056 /* Compute how many pixels below window bottom to stop searching
15057 for PT. This avoids costly search for PT that is far away if
15058 the user limited scrolling by a small number of lines, but
15059 always finds PT if scroll_conservatively is set to a large
15060 number, such as most-positive-fixnum. */
15061 int slack = max (scroll_max, 10 * frame_line_height);
15062 int y_to_move = it.last_visible_y + slack;
15064 /* Compute the distance from the scroll margin to PT or to
15065 the scroll limit, whichever comes first. This should
15066 include the height of the cursor line, to make that line
15067 fully visible. */
15068 move_it_to (&it, PT, -1, y_to_move,
15069 -1, MOVE_TO_POS | MOVE_TO_Y);
15070 dy = line_bottom_y (&it) - y0;
15072 if (dy > scroll_max)
15073 return SCROLLING_FAILED;
15075 if (dy > 0)
15076 scroll_down_p = 1;
15080 if (scroll_down_p)
15082 /* Point is in or below the bottom scroll margin, so move the
15083 window start down. If scrolling conservatively, move it just
15084 enough down to make point visible. If scroll_step is set,
15085 move it down by scroll_step. */
15086 if (arg_scroll_conservatively)
15087 amount_to_scroll
15088 = min (max (dy, frame_line_height),
15089 frame_line_height * arg_scroll_conservatively);
15090 else if (scroll_step || temp_scroll_step)
15091 amount_to_scroll = scroll_max;
15092 else
15094 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15095 height = WINDOW_BOX_TEXT_HEIGHT (w);
15096 if (NUMBERP (aggressive))
15098 double float_amount = XFLOATINT (aggressive) * height;
15099 int aggressive_scroll = float_amount;
15100 if (aggressive_scroll == 0 && float_amount > 0)
15101 aggressive_scroll = 1;
15102 /* Don't let point enter the scroll margin near top of
15103 the window. This could happen if the value of
15104 scroll_up_aggressively is too large and there are
15105 non-zero margins, because scroll_up_aggressively
15106 means put point that fraction of window height
15107 _from_the_bottom_margin_. */
15108 if (aggressive_scroll + 2 * this_scroll_margin > height)
15109 aggressive_scroll = height - 2 * this_scroll_margin;
15110 amount_to_scroll = dy + aggressive_scroll;
15114 if (amount_to_scroll <= 0)
15115 return SCROLLING_FAILED;
15117 start_display (&it, w, startp);
15118 if (arg_scroll_conservatively <= scroll_limit)
15119 move_it_vertically (&it, amount_to_scroll);
15120 else
15122 /* Extra precision for users who set scroll-conservatively
15123 to a large number: make sure the amount we scroll
15124 the window start is never less than amount_to_scroll,
15125 which was computed as distance from window bottom to
15126 point. This matters when lines at window top and lines
15127 below window bottom have different height. */
15128 struct it it1;
15129 void *it1data = NULL;
15130 /* We use a temporary it1 because line_bottom_y can modify
15131 its argument, if it moves one line down; see there. */
15132 int start_y;
15134 SAVE_IT (it1, it, it1data);
15135 start_y = line_bottom_y (&it1);
15136 do {
15137 RESTORE_IT (&it, &it, it1data);
15138 move_it_by_lines (&it, 1);
15139 SAVE_IT (it1, it, it1data);
15140 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15143 /* If STARTP is unchanged, move it down another screen line. */
15144 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15145 move_it_by_lines (&it, 1);
15146 startp = it.current.pos;
15148 else
15150 struct text_pos scroll_margin_pos = startp;
15151 int y_offset = 0;
15153 /* See if point is inside the scroll margin at the top of the
15154 window. */
15155 if (this_scroll_margin)
15157 int y_start;
15159 start_display (&it, w, startp);
15160 y_start = it.current_y;
15161 move_it_vertically (&it, this_scroll_margin);
15162 scroll_margin_pos = it.current.pos;
15163 /* If we didn't move enough before hitting ZV, request
15164 additional amount of scroll, to move point out of the
15165 scroll margin. */
15166 if (IT_CHARPOS (it) == ZV
15167 && it.current_y - y_start < this_scroll_margin)
15168 y_offset = this_scroll_margin - (it.current_y - y_start);
15171 if (PT < CHARPOS (scroll_margin_pos))
15173 /* Point is in the scroll margin at the top of the window or
15174 above what is displayed in the window. */
15175 int y0, y_to_move;
15177 /* Compute the vertical distance from PT to the scroll
15178 margin position. Move as far as scroll_max allows, or
15179 one screenful, or 10 screen lines, whichever is largest.
15180 Give up if distance is greater than scroll_max or if we
15181 didn't reach the scroll margin position. */
15182 SET_TEXT_POS (pos, PT, PT_BYTE);
15183 start_display (&it, w, pos);
15184 y0 = it.current_y;
15185 y_to_move = max (it.last_visible_y,
15186 max (scroll_max, 10 * frame_line_height));
15187 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15188 y_to_move, -1,
15189 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15190 dy = it.current_y - y0;
15191 if (dy > scroll_max
15192 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15193 return SCROLLING_FAILED;
15195 /* Additional scroll for when ZV was too close to point. */
15196 dy += y_offset;
15198 /* Compute new window start. */
15199 start_display (&it, w, startp);
15201 if (arg_scroll_conservatively)
15202 amount_to_scroll = max (dy, frame_line_height
15203 * max (scroll_step, temp_scroll_step));
15204 else if (scroll_step || temp_scroll_step)
15205 amount_to_scroll = scroll_max;
15206 else
15208 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15209 height = WINDOW_BOX_TEXT_HEIGHT (w);
15210 if (NUMBERP (aggressive))
15212 double float_amount = XFLOATINT (aggressive) * height;
15213 int aggressive_scroll = float_amount;
15214 if (aggressive_scroll == 0 && float_amount > 0)
15215 aggressive_scroll = 1;
15216 /* Don't let point enter the scroll margin near
15217 bottom of the window, if the value of
15218 scroll_down_aggressively happens to be too
15219 large. */
15220 if (aggressive_scroll + 2 * this_scroll_margin > height)
15221 aggressive_scroll = height - 2 * this_scroll_margin;
15222 amount_to_scroll = dy + aggressive_scroll;
15226 if (amount_to_scroll <= 0)
15227 return SCROLLING_FAILED;
15229 move_it_vertically_backward (&it, amount_to_scroll);
15230 startp = it.current.pos;
15234 /* Run window scroll functions. */
15235 startp = run_window_scroll_functions (window, startp);
15237 /* Display the window. Give up if new fonts are loaded, or if point
15238 doesn't appear. */
15239 if (!try_window (window, startp, 0))
15240 rc = SCROLLING_NEED_LARGER_MATRICES;
15241 else if (w->cursor.vpos < 0)
15243 clear_glyph_matrix (w->desired_matrix);
15244 rc = SCROLLING_FAILED;
15246 else
15248 /* Maybe forget recorded base line for line number display. */
15249 if (!just_this_one_p
15250 || current_buffer->clip_changed
15251 || BEG_UNCHANGED < CHARPOS (startp))
15252 w->base_line_number = 0;
15254 /* If cursor ends up on a partially visible line,
15255 treat that as being off the bottom of the screen. */
15256 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15257 /* It's possible that the cursor is on the first line of the
15258 buffer, which is partially obscured due to a vscroll
15259 (Bug#7537). In that case, avoid looping forever. */
15260 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15262 clear_glyph_matrix (w->desired_matrix);
15263 ++extra_scroll_margin_lines;
15264 goto too_near_end;
15266 rc = SCROLLING_SUCCESS;
15269 return rc;
15273 /* Compute a suitable window start for window W if display of W starts
15274 on a continuation line. Value is non-zero if a new window start
15275 was computed.
15277 The new window start will be computed, based on W's width, starting
15278 from the start of the continued line. It is the start of the
15279 screen line with the minimum distance from the old start W->start. */
15281 static int
15282 compute_window_start_on_continuation_line (struct window *w)
15284 struct text_pos pos, start_pos;
15285 int window_start_changed_p = 0;
15287 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15289 /* If window start is on a continuation line... Window start may be
15290 < BEGV in case there's invisible text at the start of the
15291 buffer (M-x rmail, for example). */
15292 if (CHARPOS (start_pos) > BEGV
15293 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15295 struct it it;
15296 struct glyph_row *row;
15298 /* Handle the case that the window start is out of range. */
15299 if (CHARPOS (start_pos) < BEGV)
15300 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15301 else if (CHARPOS (start_pos) > ZV)
15302 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15304 /* Find the start of the continued line. This should be fast
15305 because find_newline is fast (newline cache). */
15306 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15307 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15308 row, DEFAULT_FACE_ID);
15309 reseat_at_previous_visible_line_start (&it);
15311 /* If the line start is "too far" away from the window start,
15312 say it takes too much time to compute a new window start. */
15313 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15314 /* PXW: Do we need upper bounds here? */
15315 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15317 int min_distance, distance;
15319 /* Move forward by display lines to find the new window
15320 start. If window width was enlarged, the new start can
15321 be expected to be > the old start. If window width was
15322 decreased, the new window start will be < the old start.
15323 So, we're looking for the display line start with the
15324 minimum distance from the old window start. */
15325 pos = it.current.pos;
15326 min_distance = INFINITY;
15327 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15328 distance < min_distance)
15330 min_distance = distance;
15331 pos = it.current.pos;
15332 if (it.line_wrap == WORD_WRAP)
15334 /* Under WORD_WRAP, move_it_by_lines is likely to
15335 overshoot and stop not at the first, but the
15336 second character from the left margin. So in
15337 that case, we need a more tight control on the X
15338 coordinate of the iterator than move_it_by_lines
15339 promises in its contract. The method is to first
15340 go to the last (rightmost) visible character of a
15341 line, then move to the leftmost character on the
15342 next line in a separate call. */
15343 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15344 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15345 move_it_to (&it, ZV, 0,
15346 it.current_y + it.max_ascent + it.max_descent, -1,
15347 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15349 else
15350 move_it_by_lines (&it, 1);
15353 /* Set the window start there. */
15354 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15355 window_start_changed_p = 1;
15359 return window_start_changed_p;
15363 /* Try cursor movement in case text has not changed in window WINDOW,
15364 with window start STARTP. Value is
15366 CURSOR_MOVEMENT_SUCCESS if successful
15368 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15370 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15371 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15372 we want to scroll as if scroll-step were set to 1. See the code.
15374 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15375 which case we have to abort this redisplay, and adjust matrices
15376 first. */
15378 enum
15380 CURSOR_MOVEMENT_SUCCESS,
15381 CURSOR_MOVEMENT_CANNOT_BE_USED,
15382 CURSOR_MOVEMENT_MUST_SCROLL,
15383 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15386 static int
15387 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15389 struct window *w = XWINDOW (window);
15390 struct frame *f = XFRAME (w->frame);
15391 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15393 #ifdef GLYPH_DEBUG
15394 if (inhibit_try_cursor_movement)
15395 return rc;
15396 #endif
15398 /* Previously, there was a check for Lisp integer in the
15399 if-statement below. Now, this field is converted to
15400 ptrdiff_t, thus zero means invalid position in a buffer. */
15401 eassert (w->last_point > 0);
15402 /* Likewise there was a check whether window_end_vpos is nil or larger
15403 than the window. Now window_end_vpos is int and so never nil, but
15404 let's leave eassert to check whether it fits in the window. */
15405 eassert (w->window_end_vpos < w->current_matrix->nrows);
15407 /* Handle case where text has not changed, only point, and it has
15408 not moved off the frame. */
15409 if (/* Point may be in this window. */
15410 PT >= CHARPOS (startp)
15411 /* Selective display hasn't changed. */
15412 && !current_buffer->clip_changed
15413 /* Function force-mode-line-update is used to force a thorough
15414 redisplay. It sets either windows_or_buffers_changed or
15415 update_mode_lines. So don't take a shortcut here for these
15416 cases. */
15417 && !update_mode_lines
15418 && !windows_or_buffers_changed
15419 && !f->cursor_type_changed
15420 && NILP (Vshow_trailing_whitespace)
15421 /* This code is not used for mini-buffer for the sake of the case
15422 of redisplaying to replace an echo area message; since in
15423 that case the mini-buffer contents per se are usually
15424 unchanged. This code is of no real use in the mini-buffer
15425 since the handling of this_line_start_pos, etc., in redisplay
15426 handles the same cases. */
15427 && !EQ (window, minibuf_window)
15428 && (FRAME_WINDOW_P (f)
15429 || !overlay_arrow_in_current_buffer_p ()))
15431 int this_scroll_margin, top_scroll_margin;
15432 struct glyph_row *row = NULL;
15433 int frame_line_height = default_line_pixel_height (w);
15434 int window_total_lines
15435 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15437 #ifdef GLYPH_DEBUG
15438 debug_method_add (w, "cursor movement");
15439 #endif
15441 /* Scroll if point within this distance from the top or bottom
15442 of the window. This is a pixel value. */
15443 if (scroll_margin > 0)
15445 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15446 this_scroll_margin *= frame_line_height;
15448 else
15449 this_scroll_margin = 0;
15451 top_scroll_margin = this_scroll_margin;
15452 if (WINDOW_WANTS_HEADER_LINE_P (w))
15453 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15455 /* Start with the row the cursor was displayed during the last
15456 not paused redisplay. Give up if that row is not valid. */
15457 if (w->last_cursor_vpos < 0
15458 || w->last_cursor_vpos >= w->current_matrix->nrows)
15459 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15460 else
15462 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15463 if (row->mode_line_p)
15464 ++row;
15465 if (!row->enabled_p)
15466 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15469 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15471 int scroll_p = 0, must_scroll = 0;
15472 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15474 if (PT > w->last_point)
15476 /* Point has moved forward. */
15477 while (MATRIX_ROW_END_CHARPOS (row) < PT
15478 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15480 eassert (row->enabled_p);
15481 ++row;
15484 /* If the end position of a row equals the start
15485 position of the next row, and PT is at that position,
15486 we would rather display cursor in the next line. */
15487 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15488 && MATRIX_ROW_END_CHARPOS (row) == PT
15489 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15490 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15491 && !cursor_row_p (row))
15492 ++row;
15494 /* If within the scroll margin, scroll. Note that
15495 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15496 the next line would be drawn, and that
15497 this_scroll_margin can be zero. */
15498 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15499 || PT > MATRIX_ROW_END_CHARPOS (row)
15500 /* Line is completely visible last line in window
15501 and PT is to be set in the next line. */
15502 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15503 && PT == MATRIX_ROW_END_CHARPOS (row)
15504 && !row->ends_at_zv_p
15505 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15506 scroll_p = 1;
15508 else if (PT < w->last_point)
15510 /* Cursor has to be moved backward. Note that PT >=
15511 CHARPOS (startp) because of the outer if-statement. */
15512 while (!row->mode_line_p
15513 && (MATRIX_ROW_START_CHARPOS (row) > PT
15514 || (MATRIX_ROW_START_CHARPOS (row) == PT
15515 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15516 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15517 row > w->current_matrix->rows
15518 && (row-1)->ends_in_newline_from_string_p))))
15519 && (row->y > top_scroll_margin
15520 || CHARPOS (startp) == BEGV))
15522 eassert (row->enabled_p);
15523 --row;
15526 /* Consider the following case: Window starts at BEGV,
15527 there is invisible, intangible text at BEGV, so that
15528 display starts at some point START > BEGV. It can
15529 happen that we are called with PT somewhere between
15530 BEGV and START. Try to handle that case. */
15531 if (row < w->current_matrix->rows
15532 || row->mode_line_p)
15534 row = w->current_matrix->rows;
15535 if (row->mode_line_p)
15536 ++row;
15539 /* Due to newlines in overlay strings, we may have to
15540 skip forward over overlay strings. */
15541 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15542 && MATRIX_ROW_END_CHARPOS (row) == PT
15543 && !cursor_row_p (row))
15544 ++row;
15546 /* If within the scroll margin, scroll. */
15547 if (row->y < top_scroll_margin
15548 && CHARPOS (startp) != BEGV)
15549 scroll_p = 1;
15551 else
15553 /* Cursor did not move. So don't scroll even if cursor line
15554 is partially visible, as it was so before. */
15555 rc = CURSOR_MOVEMENT_SUCCESS;
15558 if (PT < MATRIX_ROW_START_CHARPOS (row)
15559 || PT > MATRIX_ROW_END_CHARPOS (row))
15561 /* if PT is not in the glyph row, give up. */
15562 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15563 must_scroll = 1;
15565 else if (rc != CURSOR_MOVEMENT_SUCCESS
15566 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15568 struct glyph_row *row1;
15570 /* If rows are bidi-reordered and point moved, back up
15571 until we find a row that does not belong to a
15572 continuation line. This is because we must consider
15573 all rows of a continued line as candidates for the
15574 new cursor positioning, since row start and end
15575 positions change non-linearly with vertical position
15576 in such rows. */
15577 /* FIXME: Revisit this when glyph ``spilling'' in
15578 continuation lines' rows is implemented for
15579 bidi-reordered rows. */
15580 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15581 MATRIX_ROW_CONTINUATION_LINE_P (row);
15582 --row)
15584 /* If we hit the beginning of the displayed portion
15585 without finding the first row of a continued
15586 line, give up. */
15587 if (row <= row1)
15589 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15590 break;
15592 eassert (row->enabled_p);
15595 if (must_scroll)
15597 else if (rc != CURSOR_MOVEMENT_SUCCESS
15598 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15599 /* Make sure this isn't a header line by any chance, since
15600 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15601 && !row->mode_line_p
15602 && make_cursor_line_fully_visible_p)
15604 if (PT == MATRIX_ROW_END_CHARPOS (row)
15605 && !row->ends_at_zv_p
15606 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15607 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15608 else if (row->height > window_box_height (w))
15610 /* If we end up in a partially visible line, let's
15611 make it fully visible, except when it's taller
15612 than the window, in which case we can't do much
15613 about it. */
15614 *scroll_step = 1;
15615 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15617 else
15619 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15620 if (!cursor_row_fully_visible_p (w, 0, 1))
15621 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15622 else
15623 rc = CURSOR_MOVEMENT_SUCCESS;
15626 else if (scroll_p)
15627 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15628 else if (rc != CURSOR_MOVEMENT_SUCCESS
15629 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15631 /* With bidi-reordered rows, there could be more than
15632 one candidate row whose start and end positions
15633 occlude point. We need to let set_cursor_from_row
15634 find the best candidate. */
15635 /* FIXME: Revisit this when glyph ``spilling'' in
15636 continuation lines' rows is implemented for
15637 bidi-reordered rows. */
15638 int rv = 0;
15642 int at_zv_p = 0, exact_match_p = 0;
15644 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15645 && PT <= MATRIX_ROW_END_CHARPOS (row)
15646 && cursor_row_p (row))
15647 rv |= set_cursor_from_row (w, row, w->current_matrix,
15648 0, 0, 0, 0);
15649 /* As soon as we've found the exact match for point,
15650 or the first suitable row whose ends_at_zv_p flag
15651 is set, we are done. */
15652 if (rv)
15654 at_zv_p = MATRIX_ROW (w->current_matrix,
15655 w->cursor.vpos)->ends_at_zv_p;
15656 if (!at_zv_p
15657 && w->cursor.hpos >= 0
15658 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15659 w->cursor.vpos))
15661 struct glyph_row *candidate =
15662 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15663 struct glyph *g =
15664 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15665 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15667 exact_match_p =
15668 (BUFFERP (g->object) && g->charpos == PT)
15669 || (NILP (g->object)
15670 && (g->charpos == PT
15671 || (g->charpos == 0 && endpos - 1 == PT)));
15673 if (at_zv_p || exact_match_p)
15675 rc = CURSOR_MOVEMENT_SUCCESS;
15676 break;
15679 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15680 break;
15681 ++row;
15683 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15684 || row->continued_p)
15685 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15686 || (MATRIX_ROW_START_CHARPOS (row) == PT
15687 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15688 /* If we didn't find any candidate rows, or exited the
15689 loop before all the candidates were examined, signal
15690 to the caller that this method failed. */
15691 if (rc != CURSOR_MOVEMENT_SUCCESS
15692 && !(rv
15693 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15694 && !row->continued_p))
15695 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15696 else if (rv)
15697 rc = CURSOR_MOVEMENT_SUCCESS;
15699 else
15703 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15705 rc = CURSOR_MOVEMENT_SUCCESS;
15706 break;
15708 ++row;
15710 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15711 && MATRIX_ROW_START_CHARPOS (row) == PT
15712 && cursor_row_p (row));
15717 return rc;
15721 void
15722 set_vertical_scroll_bar (struct window *w)
15724 ptrdiff_t start, end, whole;
15726 /* Calculate the start and end positions for the current window.
15727 At some point, it would be nice to choose between scrollbars
15728 which reflect the whole buffer size, with special markers
15729 indicating narrowing, and scrollbars which reflect only the
15730 visible region.
15732 Note that mini-buffers sometimes aren't displaying any text. */
15733 if (!MINI_WINDOW_P (w)
15734 || (w == XWINDOW (minibuf_window)
15735 && NILP (echo_area_buffer[0])))
15737 struct buffer *buf = XBUFFER (w->contents);
15738 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15739 start = marker_position (w->start) - BUF_BEGV (buf);
15740 /* I don't think this is guaranteed to be right. For the
15741 moment, we'll pretend it is. */
15742 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15744 if (end < start)
15745 end = start;
15746 if (whole < (end - start))
15747 whole = end - start;
15749 else
15750 start = end = whole = 0;
15752 /* Indicate what this scroll bar ought to be displaying now. */
15753 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15754 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15755 (w, end - start, whole, start);
15759 void
15760 set_horizontal_scroll_bar (struct window *w)
15762 int start, end, whole, portion;
15764 if (!MINI_WINDOW_P (w)
15765 || (w == XWINDOW (minibuf_window)
15766 && NILP (echo_area_buffer[0])))
15768 struct buffer *b = XBUFFER (w->contents);
15769 struct buffer *old_buffer = NULL;
15770 struct it it;
15771 struct text_pos startp;
15773 if (b != current_buffer)
15775 old_buffer = current_buffer;
15776 set_buffer_internal (b);
15779 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15780 start_display (&it, w, startp);
15781 it.last_visible_x = INT_MAX;
15782 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15783 MOVE_TO_X | MOVE_TO_Y);
15784 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15785 window_box_height (w), -1,
15786 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15788 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15789 end = start + window_box_width (w, TEXT_AREA);
15790 portion = end - start;
15791 /* After enlarging a horizontally scrolled window such that it
15792 gets at least as wide as the text it contains, make sure that
15793 the thumb doesn't fill the entire scroll bar so we can still
15794 drag it back to see the entire text. */
15795 whole = max (whole, end);
15797 if (it.bidi_p)
15799 Lisp_Object pdir;
15801 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15802 if (EQ (pdir, Qright_to_left))
15804 start = whole - end;
15805 end = start + portion;
15809 if (old_buffer)
15810 set_buffer_internal (old_buffer);
15812 else
15813 start = end = whole = portion = 0;
15815 w->hscroll_whole = whole;
15817 /* Indicate what this scroll bar ought to be displaying now. */
15818 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15819 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15820 (w, portion, whole, start);
15824 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15825 selected_window is redisplayed.
15827 We can return without actually redisplaying the window if fonts has been
15828 changed on window's frame. In that case, redisplay_internal will retry.
15830 As one of the important parts of redisplaying a window, we need to
15831 decide whether the previous window-start position (stored in the
15832 window's w->start marker position) is still valid, and if it isn't,
15833 recompute it. Some details about that:
15835 . The previous window-start could be in a continuation line, in
15836 which case we need to recompute it when the window width
15837 changes. See compute_window_start_on_continuation_line and its
15838 call below.
15840 . The text that changed since last redisplay could include the
15841 previous window-start position. In that case, we try to salvage
15842 what we can from the current glyph matrix by calling
15843 try_scrolling, which see.
15845 . Some Emacs command could force us to use a specific window-start
15846 position by setting the window's force_start flag, or gently
15847 propose doing that by setting the window's optional_new_start
15848 flag. In these cases, we try using the specified start point if
15849 that succeeds (i.e. the window desired matrix is successfully
15850 recomputed, and point location is within the window). In case
15851 of optional_new_start, we first check if the specified start
15852 position is feasible, i.e. if it will allow point to be
15853 displayed in the window. If using the specified start point
15854 fails, e.g., if new fonts are needed to be loaded, we abort the
15855 redisplay cycle and leave it up to the next cycle to figure out
15856 things.
15858 . Note that the window's force_start flag is sometimes set by
15859 redisplay itself, when it decides that the previous window start
15860 point is fine and should be kept. Search for "goto force_start"
15861 below to see the details. Like the values of window-start
15862 specified outside of redisplay, these internally-deduced values
15863 are tested for feasibility, and ignored if found to be
15864 unfeasible.
15866 . Note that the function try_window, used to completely redisplay
15867 a window, accepts the window's start point as its argument.
15868 This is used several times in the redisplay code to control
15869 where the window start will be, according to user options such
15870 as scroll-conservatively, and also to ensure the screen line
15871 showing point will be fully (as opposed to partially) visible on
15872 display. */
15874 static void
15875 redisplay_window (Lisp_Object window, bool just_this_one_p)
15877 struct window *w = XWINDOW (window);
15878 struct frame *f = XFRAME (w->frame);
15879 struct buffer *buffer = XBUFFER (w->contents);
15880 struct buffer *old = current_buffer;
15881 struct text_pos lpoint, opoint, startp;
15882 int update_mode_line;
15883 int tem;
15884 struct it it;
15885 /* Record it now because it's overwritten. */
15886 bool current_matrix_up_to_date_p = false;
15887 bool used_current_matrix_p = false;
15888 /* This is less strict than current_matrix_up_to_date_p.
15889 It indicates that the buffer contents and narrowing are unchanged. */
15890 bool buffer_unchanged_p = false;
15891 int temp_scroll_step = 0;
15892 ptrdiff_t count = SPECPDL_INDEX ();
15893 int rc;
15894 int centering_position = -1;
15895 int last_line_misfit = 0;
15896 ptrdiff_t beg_unchanged, end_unchanged;
15897 int frame_line_height;
15899 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15900 opoint = lpoint;
15902 #ifdef GLYPH_DEBUG
15903 *w->desired_matrix->method = 0;
15904 #endif
15906 if (!just_this_one_p
15907 && REDISPLAY_SOME_P ()
15908 && !w->redisplay
15909 && !f->redisplay
15910 && !buffer->text->redisplay
15911 && BUF_PT (buffer) == w->last_point)
15912 return;
15914 /* Make sure that both W's markers are valid. */
15915 eassert (XMARKER (w->start)->buffer == buffer);
15916 eassert (XMARKER (w->pointm)->buffer == buffer);
15918 /* We come here again if we need to run window-text-change-functions
15919 below. */
15920 restart:
15921 reconsider_clip_changes (w);
15922 frame_line_height = default_line_pixel_height (w);
15924 /* Has the mode line to be updated? */
15925 update_mode_line = (w->update_mode_line
15926 || update_mode_lines
15927 || buffer->clip_changed
15928 || buffer->prevent_redisplay_optimizations_p);
15930 if (!just_this_one_p)
15931 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15932 cleverly elsewhere. */
15933 w->must_be_updated_p = true;
15935 if (MINI_WINDOW_P (w))
15937 if (w == XWINDOW (echo_area_window)
15938 && !NILP (echo_area_buffer[0]))
15940 if (update_mode_line)
15941 /* We may have to update a tty frame's menu bar or a
15942 tool-bar. Example `M-x C-h C-h C-g'. */
15943 goto finish_menu_bars;
15944 else
15945 /* We've already displayed the echo area glyphs in this window. */
15946 goto finish_scroll_bars;
15948 else if ((w != XWINDOW (minibuf_window)
15949 || minibuf_level == 0)
15950 /* When buffer is nonempty, redisplay window normally. */
15951 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15952 /* Quail displays non-mini buffers in minibuffer window.
15953 In that case, redisplay the window normally. */
15954 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15956 /* W is a mini-buffer window, but it's not active, so clear
15957 it. */
15958 int yb = window_text_bottom_y (w);
15959 struct glyph_row *row;
15960 int y;
15962 for (y = 0, row = w->desired_matrix->rows;
15963 y < yb;
15964 y += row->height, ++row)
15965 blank_row (w, row, y);
15966 goto finish_scroll_bars;
15969 clear_glyph_matrix (w->desired_matrix);
15972 /* Otherwise set up data on this window; select its buffer and point
15973 value. */
15974 /* Really select the buffer, for the sake of buffer-local
15975 variables. */
15976 set_buffer_internal_1 (XBUFFER (w->contents));
15978 current_matrix_up_to_date_p
15979 = (w->window_end_valid
15980 && !current_buffer->clip_changed
15981 && !current_buffer->prevent_redisplay_optimizations_p
15982 && !window_outdated (w));
15984 /* Run the window-text-change-functions
15985 if it is possible that the text on the screen has changed
15986 (either due to modification of the text, or any other reason). */
15987 if (!current_matrix_up_to_date_p
15988 && !NILP (Vwindow_text_change_functions))
15990 safe_run_hooks (Qwindow_text_change_functions);
15991 goto restart;
15994 beg_unchanged = BEG_UNCHANGED;
15995 end_unchanged = END_UNCHANGED;
15997 SET_TEXT_POS (opoint, PT, PT_BYTE);
15999 specbind (Qinhibit_point_motion_hooks, Qt);
16001 buffer_unchanged_p
16002 = (w->window_end_valid
16003 && !current_buffer->clip_changed
16004 && !window_outdated (w));
16006 /* When windows_or_buffers_changed is non-zero, we can't rely
16007 on the window end being valid, so set it to zero there. */
16008 if (windows_or_buffers_changed)
16010 /* If window starts on a continuation line, maybe adjust the
16011 window start in case the window's width changed. */
16012 if (XMARKER (w->start)->buffer == current_buffer)
16013 compute_window_start_on_continuation_line (w);
16015 w->window_end_valid = false;
16016 /* If so, we also can't rely on current matrix
16017 and should not fool try_cursor_movement below. */
16018 current_matrix_up_to_date_p = false;
16021 /* Some sanity checks. */
16022 CHECK_WINDOW_END (w);
16023 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16024 emacs_abort ();
16025 if (BYTEPOS (opoint) < CHARPOS (opoint))
16026 emacs_abort ();
16028 if (mode_line_update_needed (w))
16029 update_mode_line = 1;
16031 /* Point refers normally to the selected window. For any other
16032 window, set up appropriate value. */
16033 if (!EQ (window, selected_window))
16035 ptrdiff_t new_pt = marker_position (w->pointm);
16036 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16038 if (new_pt < BEGV)
16040 new_pt = BEGV;
16041 new_pt_byte = BEGV_BYTE;
16042 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16044 else if (new_pt > (ZV - 1))
16046 new_pt = ZV;
16047 new_pt_byte = ZV_BYTE;
16048 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16051 /* We don't use SET_PT so that the point-motion hooks don't run. */
16052 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16055 /* If any of the character widths specified in the display table
16056 have changed, invalidate the width run cache. It's true that
16057 this may be a bit late to catch such changes, but the rest of
16058 redisplay goes (non-fatally) haywire when the display table is
16059 changed, so why should we worry about doing any better? */
16060 if (current_buffer->width_run_cache
16061 || (current_buffer->base_buffer
16062 && current_buffer->base_buffer->width_run_cache))
16064 struct Lisp_Char_Table *disptab = buffer_display_table ();
16066 if (! disptab_matches_widthtab
16067 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16069 struct buffer *buf = current_buffer;
16071 if (buf->base_buffer)
16072 buf = buf->base_buffer;
16073 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16074 recompute_width_table (current_buffer, disptab);
16078 /* If window-start is screwed up, choose a new one. */
16079 if (XMARKER (w->start)->buffer != current_buffer)
16080 goto recenter;
16082 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16084 /* If someone specified a new starting point but did not insist,
16085 check whether it can be used. */
16086 if ((w->optional_new_start || window_frozen_p (w))
16087 && CHARPOS (startp) >= BEGV
16088 && CHARPOS (startp) <= ZV)
16090 ptrdiff_t it_charpos;
16092 w->optional_new_start = 0;
16093 start_display (&it, w, startp);
16094 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16095 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16096 /* Record IT's position now, since line_bottom_y might change
16097 that. */
16098 it_charpos = IT_CHARPOS (it);
16099 /* Make sure we set the force_start flag only if the cursor row
16100 will be fully visible. Otherwise, the code under force_start
16101 label below will try to move point back into view, which is
16102 not what the code which sets optional_new_start wants. */
16103 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16104 && !w->force_start)
16106 if (it_charpos == PT)
16107 w->force_start = 1;
16108 /* IT may overshoot PT if text at PT is invisible. */
16109 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16110 w->force_start = 1;
16111 #ifdef GLYPH_DEBUG
16112 if (w->force_start)
16114 if (window_frozen_p (w))
16115 debug_method_add (w, "set force_start from frozen window start");
16116 else
16117 debug_method_add (w, "set force_start from optional_new_start");
16119 #endif
16123 force_start:
16125 /* Handle case where place to start displaying has been specified,
16126 unless the specified location is outside the accessible range. */
16127 if (w->force_start)
16129 /* We set this later on if we have to adjust point. */
16130 int new_vpos = -1;
16132 w->force_start = 0;
16133 w->vscroll = 0;
16134 w->window_end_valid = 0;
16136 /* Forget any recorded base line for line number display. */
16137 if (!buffer_unchanged_p)
16138 w->base_line_number = 0;
16140 /* Redisplay the mode line. Select the buffer properly for that.
16141 Also, run the hook window-scroll-functions
16142 because we have scrolled. */
16143 /* Note, we do this after clearing force_start because
16144 if there's an error, it is better to forget about force_start
16145 than to get into an infinite loop calling the hook functions
16146 and having them get more errors. */
16147 if (!update_mode_line
16148 || ! NILP (Vwindow_scroll_functions))
16150 update_mode_line = 1;
16151 w->update_mode_line = 1;
16152 startp = run_window_scroll_functions (window, startp);
16155 if (CHARPOS (startp) < BEGV)
16156 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16157 else if (CHARPOS (startp) > ZV)
16158 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16160 /* Redisplay, then check if cursor has been set during the
16161 redisplay. Give up if new fonts were loaded. */
16162 /* We used to issue a CHECK_MARGINS argument to try_window here,
16163 but this causes scrolling to fail when point begins inside
16164 the scroll margin (bug#148) -- cyd */
16165 if (!try_window (window, startp, 0))
16167 w->force_start = 1;
16168 clear_glyph_matrix (w->desired_matrix);
16169 goto need_larger_matrices;
16172 if (w->cursor.vpos < 0)
16174 /* If point does not appear, try to move point so it does
16175 appear. The desired matrix has been built above, so we
16176 can use it here. */
16177 new_vpos = window_box_height (w) / 2;
16180 if (!cursor_row_fully_visible_p (w, 0, 0))
16182 /* Point does appear, but on a line partly visible at end of window.
16183 Move it back to a fully-visible line. */
16184 new_vpos = window_box_height (w);
16185 /* But if window_box_height suggests a Y coordinate that is
16186 not less than we already have, that line will clearly not
16187 be fully visible, so give up and scroll the display.
16188 This can happen when the default face uses a font whose
16189 dimensions are different from the frame's default
16190 font. */
16191 if (new_vpos >= w->cursor.y)
16193 w->cursor.vpos = -1;
16194 clear_glyph_matrix (w->desired_matrix);
16195 goto try_to_scroll;
16198 else if (w->cursor.vpos >= 0)
16200 /* Some people insist on not letting point enter the scroll
16201 margin, even though this part handles windows that didn't
16202 scroll at all. */
16203 int window_total_lines
16204 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16205 int margin = min (scroll_margin, window_total_lines / 4);
16206 int pixel_margin = margin * frame_line_height;
16207 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16209 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16210 below, which finds the row to move point to, advances by
16211 the Y coordinate of the _next_ row, see the definition of
16212 MATRIX_ROW_BOTTOM_Y. */
16213 if (w->cursor.vpos < margin + header_line)
16215 w->cursor.vpos = -1;
16216 clear_glyph_matrix (w->desired_matrix);
16217 goto try_to_scroll;
16219 else
16221 int window_height = window_box_height (w);
16223 if (header_line)
16224 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16225 if (w->cursor.y >= window_height - pixel_margin)
16227 w->cursor.vpos = -1;
16228 clear_glyph_matrix (w->desired_matrix);
16229 goto try_to_scroll;
16234 /* If we need to move point for either of the above reasons,
16235 now actually do it. */
16236 if (new_vpos >= 0)
16238 struct glyph_row *row;
16240 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16241 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16242 ++row;
16244 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16245 MATRIX_ROW_START_BYTEPOS (row));
16247 if (w != XWINDOW (selected_window))
16248 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16249 else if (current_buffer == old)
16250 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16252 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16254 /* Re-run pre-redisplay-function so it can update the region
16255 according to the new position of point. */
16256 /* Other than the cursor, w's redisplay is done so we can set its
16257 redisplay to false. Also the buffer's redisplay can be set to
16258 false, since propagate_buffer_redisplay should have already
16259 propagated its info to `w' anyway. */
16260 w->redisplay = false;
16261 XBUFFER (w->contents)->text->redisplay = false;
16262 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16264 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16266 /* pre-redisplay-function made changes (e.g. move the region)
16267 that require another round of redisplay. */
16268 clear_glyph_matrix (w->desired_matrix);
16269 if (!try_window (window, startp, 0))
16270 goto need_larger_matrices;
16273 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16275 clear_glyph_matrix (w->desired_matrix);
16276 goto try_to_scroll;
16279 #ifdef GLYPH_DEBUG
16280 debug_method_add (w, "forced window start");
16281 #endif
16282 goto done;
16285 /* Handle case where text has not changed, only point, and it has
16286 not moved off the frame, and we are not retrying after hscroll.
16287 (current_matrix_up_to_date_p is nonzero when retrying.) */
16288 if (current_matrix_up_to_date_p
16289 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16290 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16292 switch (rc)
16294 case CURSOR_MOVEMENT_SUCCESS:
16295 used_current_matrix_p = 1;
16296 goto done;
16298 case CURSOR_MOVEMENT_MUST_SCROLL:
16299 goto try_to_scroll;
16301 default:
16302 emacs_abort ();
16305 /* If current starting point was originally the beginning of a line
16306 but no longer is, find a new starting point. */
16307 else if (w->start_at_line_beg
16308 && !(CHARPOS (startp) <= BEGV
16309 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16311 #ifdef GLYPH_DEBUG
16312 debug_method_add (w, "recenter 1");
16313 #endif
16314 goto recenter;
16317 /* Try scrolling with try_window_id. Value is > 0 if update has
16318 been done, it is -1 if we know that the same window start will
16319 not work. It is 0 if unsuccessful for some other reason. */
16320 else if ((tem = try_window_id (w)) != 0)
16322 #ifdef GLYPH_DEBUG
16323 debug_method_add (w, "try_window_id %d", tem);
16324 #endif
16326 if (f->fonts_changed)
16327 goto need_larger_matrices;
16328 if (tem > 0)
16329 goto done;
16331 /* Otherwise try_window_id has returned -1 which means that we
16332 don't want the alternative below this comment to execute. */
16334 else if (CHARPOS (startp) >= BEGV
16335 && CHARPOS (startp) <= ZV
16336 && PT >= CHARPOS (startp)
16337 && (CHARPOS (startp) < ZV
16338 /* Avoid starting at end of buffer. */
16339 || CHARPOS (startp) == BEGV
16340 || !window_outdated (w)))
16342 int d1, d2, d5, d6;
16343 int rtop, rbot;
16345 /* If first window line is a continuation line, and window start
16346 is inside the modified region, but the first change is before
16347 current window start, we must select a new window start.
16349 However, if this is the result of a down-mouse event (e.g. by
16350 extending the mouse-drag-overlay), we don't want to select a
16351 new window start, since that would change the position under
16352 the mouse, resulting in an unwanted mouse-movement rather
16353 than a simple mouse-click. */
16354 if (!w->start_at_line_beg
16355 && NILP (do_mouse_tracking)
16356 && CHARPOS (startp) > BEGV
16357 && CHARPOS (startp) > BEG + beg_unchanged
16358 && CHARPOS (startp) <= Z - end_unchanged
16359 /* Even if w->start_at_line_beg is nil, a new window may
16360 start at a line_beg, since that's how set_buffer_window
16361 sets it. So, we need to check the return value of
16362 compute_window_start_on_continuation_line. (See also
16363 bug#197). */
16364 && XMARKER (w->start)->buffer == current_buffer
16365 && compute_window_start_on_continuation_line (w)
16366 /* It doesn't make sense to force the window start like we
16367 do at label force_start if it is already known that point
16368 will not be fully visible in the resulting window, because
16369 doing so will move point from its correct position
16370 instead of scrolling the window to bring point into view.
16371 See bug#9324. */
16372 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16373 /* A very tall row could need more than the window height,
16374 in which case we accept that it is partially visible. */
16375 && (rtop != 0) == (rbot != 0))
16377 w->force_start = 1;
16378 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16379 #ifdef GLYPH_DEBUG
16380 debug_method_add (w, "recomputed window start in continuation line");
16381 #endif
16382 goto force_start;
16385 #ifdef GLYPH_DEBUG
16386 debug_method_add (w, "same window start");
16387 #endif
16389 /* Try to redisplay starting at same place as before.
16390 If point has not moved off frame, accept the results. */
16391 if (!current_matrix_up_to_date_p
16392 /* Don't use try_window_reusing_current_matrix in this case
16393 because a window scroll function can have changed the
16394 buffer. */
16395 || !NILP (Vwindow_scroll_functions)
16396 || MINI_WINDOW_P (w)
16397 || !(used_current_matrix_p
16398 = try_window_reusing_current_matrix (w)))
16400 IF_DEBUG (debug_method_add (w, "1"));
16401 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16402 /* -1 means we need to scroll.
16403 0 means we need new matrices, but fonts_changed
16404 is set in that case, so we will detect it below. */
16405 goto try_to_scroll;
16408 if (f->fonts_changed)
16409 goto need_larger_matrices;
16411 if (w->cursor.vpos >= 0)
16413 if (!just_this_one_p
16414 || current_buffer->clip_changed
16415 || BEG_UNCHANGED < CHARPOS (startp))
16416 /* Forget any recorded base line for line number display. */
16417 w->base_line_number = 0;
16419 if (!cursor_row_fully_visible_p (w, 1, 0))
16421 clear_glyph_matrix (w->desired_matrix);
16422 last_line_misfit = 1;
16424 /* Drop through and scroll. */
16425 else
16426 goto done;
16428 else
16429 clear_glyph_matrix (w->desired_matrix);
16432 try_to_scroll:
16434 /* Redisplay the mode line. Select the buffer properly for that. */
16435 if (!update_mode_line)
16437 update_mode_line = 1;
16438 w->update_mode_line = 1;
16441 /* Try to scroll by specified few lines. */
16442 if ((scroll_conservatively
16443 || emacs_scroll_step
16444 || temp_scroll_step
16445 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16446 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16447 && CHARPOS (startp) >= BEGV
16448 && CHARPOS (startp) <= ZV)
16450 /* The function returns -1 if new fonts were loaded, 1 if
16451 successful, 0 if not successful. */
16452 int ss = try_scrolling (window, just_this_one_p,
16453 scroll_conservatively,
16454 emacs_scroll_step,
16455 temp_scroll_step, last_line_misfit);
16456 switch (ss)
16458 case SCROLLING_SUCCESS:
16459 goto done;
16461 case SCROLLING_NEED_LARGER_MATRICES:
16462 goto need_larger_matrices;
16464 case SCROLLING_FAILED:
16465 break;
16467 default:
16468 emacs_abort ();
16472 /* Finally, just choose a place to start which positions point
16473 according to user preferences. */
16475 recenter:
16477 #ifdef GLYPH_DEBUG
16478 debug_method_add (w, "recenter");
16479 #endif
16481 /* Forget any previously recorded base line for line number display. */
16482 if (!buffer_unchanged_p)
16483 w->base_line_number = 0;
16485 /* Determine the window start relative to point. */
16486 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16487 it.current_y = it.last_visible_y;
16488 if (centering_position < 0)
16490 int window_total_lines
16491 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16492 int margin
16493 = scroll_margin > 0
16494 ? min (scroll_margin, window_total_lines / 4)
16495 : 0;
16496 ptrdiff_t margin_pos = CHARPOS (startp);
16497 Lisp_Object aggressive;
16498 int scrolling_up;
16500 /* If there is a scroll margin at the top of the window, find
16501 its character position. */
16502 if (margin
16503 /* Cannot call start_display if startp is not in the
16504 accessible region of the buffer. This can happen when we
16505 have just switched to a different buffer and/or changed
16506 its restriction. In that case, startp is initialized to
16507 the character position 1 (BEGV) because we did not yet
16508 have chance to display the buffer even once. */
16509 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16511 struct it it1;
16512 void *it1data = NULL;
16514 SAVE_IT (it1, it, it1data);
16515 start_display (&it1, w, startp);
16516 move_it_vertically (&it1, margin * frame_line_height);
16517 margin_pos = IT_CHARPOS (it1);
16518 RESTORE_IT (&it, &it, it1data);
16520 scrolling_up = PT > margin_pos;
16521 aggressive =
16522 scrolling_up
16523 ? BVAR (current_buffer, scroll_up_aggressively)
16524 : BVAR (current_buffer, scroll_down_aggressively);
16526 if (!MINI_WINDOW_P (w)
16527 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16529 int pt_offset = 0;
16531 /* Setting scroll-conservatively overrides
16532 scroll-*-aggressively. */
16533 if (!scroll_conservatively && NUMBERP (aggressive))
16535 double float_amount = XFLOATINT (aggressive);
16537 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16538 if (pt_offset == 0 && float_amount > 0)
16539 pt_offset = 1;
16540 if (pt_offset && margin > 0)
16541 margin -= 1;
16543 /* Compute how much to move the window start backward from
16544 point so that point will be displayed where the user
16545 wants it. */
16546 if (scrolling_up)
16548 centering_position = it.last_visible_y;
16549 if (pt_offset)
16550 centering_position -= pt_offset;
16551 centering_position -=
16552 frame_line_height * (1 + margin + (last_line_misfit != 0))
16553 + WINDOW_HEADER_LINE_HEIGHT (w);
16554 /* Don't let point enter the scroll margin near top of
16555 the window. */
16556 if (centering_position < margin * frame_line_height)
16557 centering_position = margin * frame_line_height;
16559 else
16560 centering_position = margin * frame_line_height + pt_offset;
16562 else
16563 /* Set the window start half the height of the window backward
16564 from point. */
16565 centering_position = window_box_height (w) / 2;
16567 move_it_vertically_backward (&it, centering_position);
16569 eassert (IT_CHARPOS (it) >= BEGV);
16571 /* The function move_it_vertically_backward may move over more
16572 than the specified y-distance. If it->w is small, e.g. a
16573 mini-buffer window, we may end up in front of the window's
16574 display area. Start displaying at the start of the line
16575 containing PT in this case. */
16576 if (it.current_y <= 0)
16578 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16579 move_it_vertically_backward (&it, 0);
16580 it.current_y = 0;
16583 it.current_x = it.hpos = 0;
16585 /* Set the window start position here explicitly, to avoid an
16586 infinite loop in case the functions in window-scroll-functions
16587 get errors. */
16588 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16590 /* Run scroll hooks. */
16591 startp = run_window_scroll_functions (window, it.current.pos);
16593 /* Redisplay the window. */
16594 if (!current_matrix_up_to_date_p
16595 || windows_or_buffers_changed
16596 || f->cursor_type_changed
16597 /* Don't use try_window_reusing_current_matrix in this case
16598 because it can have changed the buffer. */
16599 || !NILP (Vwindow_scroll_functions)
16600 || !just_this_one_p
16601 || MINI_WINDOW_P (w)
16602 || !(used_current_matrix_p
16603 = try_window_reusing_current_matrix (w)))
16604 try_window (window, startp, 0);
16606 /* If new fonts have been loaded (due to fontsets), give up. We
16607 have to start a new redisplay since we need to re-adjust glyph
16608 matrices. */
16609 if (f->fonts_changed)
16610 goto need_larger_matrices;
16612 /* If cursor did not appear assume that the middle of the window is
16613 in the first line of the window. Do it again with the next line.
16614 (Imagine a window of height 100, displaying two lines of height
16615 60. Moving back 50 from it->last_visible_y will end in the first
16616 line.) */
16617 if (w->cursor.vpos < 0)
16619 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16621 clear_glyph_matrix (w->desired_matrix);
16622 move_it_by_lines (&it, 1);
16623 try_window (window, it.current.pos, 0);
16625 else if (PT < IT_CHARPOS (it))
16627 clear_glyph_matrix (w->desired_matrix);
16628 move_it_by_lines (&it, -1);
16629 try_window (window, it.current.pos, 0);
16631 else
16633 /* Not much we can do about it. */
16637 /* Consider the following case: Window starts at BEGV, there is
16638 invisible, intangible text at BEGV, so that display starts at
16639 some point START > BEGV. It can happen that we are called with
16640 PT somewhere between BEGV and START. Try to handle that case,
16641 and similar ones. */
16642 if (w->cursor.vpos < 0)
16644 /* First, try locating the proper glyph row for PT. */
16645 struct glyph_row *row =
16646 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16648 /* Sometimes point is at the beginning of invisible text that is
16649 before the 1st character displayed in the row. In that case,
16650 row_containing_pos fails to find the row, because no glyphs
16651 with appropriate buffer positions are present in the row.
16652 Therefore, we next try to find the row which shows the 1st
16653 position after the invisible text. */
16654 if (!row)
16656 Lisp_Object val =
16657 get_char_property_and_overlay (make_number (PT), Qinvisible,
16658 Qnil, NULL);
16660 if (TEXT_PROP_MEANS_INVISIBLE (val))
16662 ptrdiff_t alt_pos;
16663 Lisp_Object invis_end =
16664 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16665 Qnil, Qnil);
16667 if (NATNUMP (invis_end))
16668 alt_pos = XFASTINT (invis_end);
16669 else
16670 alt_pos = ZV;
16671 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16672 NULL, 0);
16675 /* Finally, fall back on the first row of the window after the
16676 header line (if any). This is slightly better than not
16677 displaying the cursor at all. */
16678 if (!row)
16680 row = w->current_matrix->rows;
16681 if (row->mode_line_p)
16682 ++row;
16684 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16687 if (!cursor_row_fully_visible_p (w, 0, 0))
16689 /* If vscroll is enabled, disable it and try again. */
16690 if (w->vscroll)
16692 w->vscroll = 0;
16693 clear_glyph_matrix (w->desired_matrix);
16694 goto recenter;
16697 /* Users who set scroll-conservatively to a large number want
16698 point just above/below the scroll margin. If we ended up
16699 with point's row partially visible, move the window start to
16700 make that row fully visible and out of the margin. */
16701 if (scroll_conservatively > SCROLL_LIMIT)
16703 int window_total_lines
16704 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16705 int margin =
16706 scroll_margin > 0
16707 ? min (scroll_margin, window_total_lines / 4)
16708 : 0;
16709 int move_down = w->cursor.vpos >= window_total_lines / 2;
16711 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16712 clear_glyph_matrix (w->desired_matrix);
16713 if (1 == try_window (window, it.current.pos,
16714 TRY_WINDOW_CHECK_MARGINS))
16715 goto done;
16718 /* If centering point failed to make the whole line visible,
16719 put point at the top instead. That has to make the whole line
16720 visible, if it can be done. */
16721 if (centering_position == 0)
16722 goto done;
16724 clear_glyph_matrix (w->desired_matrix);
16725 centering_position = 0;
16726 goto recenter;
16729 done:
16731 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16732 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16733 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16735 /* Display the mode line, if we must. */
16736 if ((update_mode_line
16737 /* If window not full width, must redo its mode line
16738 if (a) the window to its side is being redone and
16739 (b) we do a frame-based redisplay. This is a consequence
16740 of how inverted lines are drawn in frame-based redisplay. */
16741 || (!just_this_one_p
16742 && !FRAME_WINDOW_P (f)
16743 && !WINDOW_FULL_WIDTH_P (w))
16744 /* Line number to display. */
16745 || w->base_line_pos > 0
16746 /* Column number is displayed and different from the one displayed. */
16747 || (w->column_number_displayed != -1
16748 && (w->column_number_displayed != current_column ())))
16749 /* This means that the window has a mode line. */
16750 && (WINDOW_WANTS_MODELINE_P (w)
16751 || WINDOW_WANTS_HEADER_LINE_P (w)))
16754 display_mode_lines (w);
16756 /* If mode line height has changed, arrange for a thorough
16757 immediate redisplay using the correct mode line height. */
16758 if (WINDOW_WANTS_MODELINE_P (w)
16759 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16761 f->fonts_changed = 1;
16762 w->mode_line_height = -1;
16763 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16764 = DESIRED_MODE_LINE_HEIGHT (w);
16767 /* If header line height has changed, arrange for a thorough
16768 immediate redisplay using the correct header line height. */
16769 if (WINDOW_WANTS_HEADER_LINE_P (w)
16770 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16772 f->fonts_changed = 1;
16773 w->header_line_height = -1;
16774 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16775 = DESIRED_HEADER_LINE_HEIGHT (w);
16778 if (f->fonts_changed)
16779 goto need_larger_matrices;
16782 if (!line_number_displayed && w->base_line_pos != -1)
16784 w->base_line_pos = 0;
16785 w->base_line_number = 0;
16788 finish_menu_bars:
16790 /* When we reach a frame's selected window, redo the frame's menu bar. */
16791 if (update_mode_line
16792 && EQ (FRAME_SELECTED_WINDOW (f), window))
16794 int redisplay_menu_p = 0;
16796 if (FRAME_WINDOW_P (f))
16798 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16799 || defined (HAVE_NS) || defined (USE_GTK)
16800 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16801 #else
16802 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16803 #endif
16805 else
16806 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16808 if (redisplay_menu_p)
16809 display_menu_bar (w);
16811 #ifdef HAVE_WINDOW_SYSTEM
16812 if (FRAME_WINDOW_P (f))
16814 #if defined (USE_GTK) || defined (HAVE_NS)
16815 if (FRAME_EXTERNAL_TOOL_BAR (f))
16816 redisplay_tool_bar (f);
16817 #else
16818 if (WINDOWP (f->tool_bar_window)
16819 && (FRAME_TOOL_BAR_LINES (f) > 0
16820 || !NILP (Vauto_resize_tool_bars))
16821 && redisplay_tool_bar (f))
16822 ignore_mouse_drag_p = 1;
16823 #endif
16825 #endif
16828 #ifdef HAVE_WINDOW_SYSTEM
16829 if (FRAME_WINDOW_P (f)
16830 && update_window_fringes (w, (just_this_one_p
16831 || (!used_current_matrix_p && !overlay_arrow_seen)
16832 || w->pseudo_window_p)))
16834 update_begin (f);
16835 block_input ();
16836 if (draw_window_fringes (w, 1))
16838 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16839 x_draw_right_divider (w);
16840 else
16841 x_draw_vertical_border (w);
16843 unblock_input ();
16844 update_end (f);
16847 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16848 x_draw_bottom_divider (w);
16849 #endif /* HAVE_WINDOW_SYSTEM */
16851 /* We go to this label, with fonts_changed set, if it is
16852 necessary to try again using larger glyph matrices.
16853 We have to redeem the scroll bar even in this case,
16854 because the loop in redisplay_internal expects that. */
16855 need_larger_matrices:
16857 finish_scroll_bars:
16859 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16861 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16862 /* Set the thumb's position and size. */
16863 set_vertical_scroll_bar (w);
16865 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16866 /* Set the thumb's position and size. */
16867 set_horizontal_scroll_bar (w);
16869 /* Note that we actually used the scroll bar attached to this
16870 window, so it shouldn't be deleted at the end of redisplay. */
16871 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16872 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16875 /* Restore current_buffer and value of point in it. The window
16876 update may have changed the buffer, so first make sure `opoint'
16877 is still valid (Bug#6177). */
16878 if (CHARPOS (opoint) < BEGV)
16879 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16880 else if (CHARPOS (opoint) > ZV)
16881 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16882 else
16883 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16885 set_buffer_internal_1 (old);
16886 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16887 shorter. This can be caused by log truncation in *Messages*. */
16888 if (CHARPOS (lpoint) <= ZV)
16889 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16891 unbind_to (count, Qnil);
16895 /* Build the complete desired matrix of WINDOW with a window start
16896 buffer position POS.
16898 Value is 1 if successful. It is zero if fonts were loaded during
16899 redisplay which makes re-adjusting glyph matrices necessary, and -1
16900 if point would appear in the scroll margins.
16901 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16902 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16903 set in FLAGS.) */
16906 try_window (Lisp_Object window, struct text_pos pos, int flags)
16908 struct window *w = XWINDOW (window);
16909 struct it it;
16910 struct glyph_row *last_text_row = NULL;
16911 struct frame *f = XFRAME (w->frame);
16912 int frame_line_height = default_line_pixel_height (w);
16914 /* Make POS the new window start. */
16915 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16917 /* Mark cursor position as unknown. No overlay arrow seen. */
16918 w->cursor.vpos = -1;
16919 overlay_arrow_seen = 0;
16921 /* Initialize iterator and info to start at POS. */
16922 start_display (&it, w, pos);
16923 it.glyph_row->reversed_p = false;
16925 /* Display all lines of W. */
16926 while (it.current_y < it.last_visible_y)
16928 if (display_line (&it))
16929 last_text_row = it.glyph_row - 1;
16930 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16931 return 0;
16934 /* Don't let the cursor end in the scroll margins. */
16935 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16936 && !MINI_WINDOW_P (w))
16938 int this_scroll_margin;
16939 int window_total_lines
16940 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16942 if (scroll_margin > 0)
16944 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16945 this_scroll_margin *= frame_line_height;
16947 else
16948 this_scroll_margin = 0;
16950 if ((w->cursor.y >= 0 /* not vscrolled */
16951 && w->cursor.y < this_scroll_margin
16952 && CHARPOS (pos) > BEGV
16953 && IT_CHARPOS (it) < ZV)
16954 /* rms: considering make_cursor_line_fully_visible_p here
16955 seems to give wrong results. We don't want to recenter
16956 when the last line is partly visible, we want to allow
16957 that case to be handled in the usual way. */
16958 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16960 w->cursor.vpos = -1;
16961 clear_glyph_matrix (w->desired_matrix);
16962 return -1;
16966 /* If bottom moved off end of frame, change mode line percentage. */
16967 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16968 w->update_mode_line = 1;
16970 /* Set window_end_pos to the offset of the last character displayed
16971 on the window from the end of current_buffer. Set
16972 window_end_vpos to its row number. */
16973 if (last_text_row)
16975 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16976 adjust_window_ends (w, last_text_row, 0);
16977 eassert
16978 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16979 w->window_end_vpos)));
16981 else
16983 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16984 w->window_end_pos = Z - ZV;
16985 w->window_end_vpos = 0;
16988 /* But that is not valid info until redisplay finishes. */
16989 w->window_end_valid = 0;
16990 return 1;
16995 /************************************************************************
16996 Window redisplay reusing current matrix when buffer has not changed
16997 ************************************************************************/
16999 /* Try redisplay of window W showing an unchanged buffer with a
17000 different window start than the last time it was displayed by
17001 reusing its current matrix. Value is non-zero if successful.
17002 W->start is the new window start. */
17004 static int
17005 try_window_reusing_current_matrix (struct window *w)
17007 struct frame *f = XFRAME (w->frame);
17008 struct glyph_row *bottom_row;
17009 struct it it;
17010 struct run run;
17011 struct text_pos start, new_start;
17012 int nrows_scrolled, i;
17013 struct glyph_row *last_text_row;
17014 struct glyph_row *last_reused_text_row;
17015 struct glyph_row *start_row;
17016 int start_vpos, min_y, max_y;
17018 #ifdef GLYPH_DEBUG
17019 if (inhibit_try_window_reusing)
17020 return 0;
17021 #endif
17023 if (/* This function doesn't handle terminal frames. */
17024 !FRAME_WINDOW_P (f)
17025 /* Don't try to reuse the display if windows have been split
17026 or such. */
17027 || windows_or_buffers_changed
17028 || f->cursor_type_changed)
17029 return 0;
17031 /* Can't do this if showing trailing whitespace. */
17032 if (!NILP (Vshow_trailing_whitespace))
17033 return 0;
17035 /* If top-line visibility has changed, give up. */
17036 if (WINDOW_WANTS_HEADER_LINE_P (w)
17037 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17038 return 0;
17040 /* Give up if old or new display is scrolled vertically. We could
17041 make this function handle this, but right now it doesn't. */
17042 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17043 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17044 return 0;
17046 /* The variable new_start now holds the new window start. The old
17047 start `start' can be determined from the current matrix. */
17048 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17049 start = start_row->minpos;
17050 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17052 /* Clear the desired matrix for the display below. */
17053 clear_glyph_matrix (w->desired_matrix);
17055 if (CHARPOS (new_start) <= CHARPOS (start))
17057 /* Don't use this method if the display starts with an ellipsis
17058 displayed for invisible text. It's not easy to handle that case
17059 below, and it's certainly not worth the effort since this is
17060 not a frequent case. */
17061 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17062 return 0;
17064 IF_DEBUG (debug_method_add (w, "twu1"));
17066 /* Display up to a row that can be reused. The variable
17067 last_text_row is set to the last row displayed that displays
17068 text. Note that it.vpos == 0 if or if not there is a
17069 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17070 start_display (&it, w, new_start);
17071 w->cursor.vpos = -1;
17072 last_text_row = last_reused_text_row = NULL;
17074 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17076 /* If we have reached into the characters in the START row,
17077 that means the line boundaries have changed. So we
17078 can't start copying with the row START. Maybe it will
17079 work to start copying with the following row. */
17080 while (IT_CHARPOS (it) > CHARPOS (start))
17082 /* Advance to the next row as the "start". */
17083 start_row++;
17084 start = start_row->minpos;
17085 /* If there are no more rows to try, or just one, give up. */
17086 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17087 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17088 || CHARPOS (start) == ZV)
17090 clear_glyph_matrix (w->desired_matrix);
17091 return 0;
17094 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17096 /* If we have reached alignment, we can copy the rest of the
17097 rows. */
17098 if (IT_CHARPOS (it) == CHARPOS (start)
17099 /* Don't accept "alignment" inside a display vector,
17100 since start_row could have started in the middle of
17101 that same display vector (thus their character
17102 positions match), and we have no way of telling if
17103 that is the case. */
17104 && it.current.dpvec_index < 0)
17105 break;
17107 it.glyph_row->reversed_p = false;
17108 if (display_line (&it))
17109 last_text_row = it.glyph_row - 1;
17113 /* A value of current_y < last_visible_y means that we stopped
17114 at the previous window start, which in turn means that we
17115 have at least one reusable row. */
17116 if (it.current_y < it.last_visible_y)
17118 struct glyph_row *row;
17120 /* IT.vpos always starts from 0; it counts text lines. */
17121 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17123 /* Find PT if not already found in the lines displayed. */
17124 if (w->cursor.vpos < 0)
17126 int dy = it.current_y - start_row->y;
17128 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17129 row = row_containing_pos (w, PT, row, NULL, dy);
17130 if (row)
17131 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17132 dy, nrows_scrolled);
17133 else
17135 clear_glyph_matrix (w->desired_matrix);
17136 return 0;
17140 /* Scroll the display. Do it before the current matrix is
17141 changed. The problem here is that update has not yet
17142 run, i.e. part of the current matrix is not up to date.
17143 scroll_run_hook will clear the cursor, and use the
17144 current matrix to get the height of the row the cursor is
17145 in. */
17146 run.current_y = start_row->y;
17147 run.desired_y = it.current_y;
17148 run.height = it.last_visible_y - it.current_y;
17150 if (run.height > 0 && run.current_y != run.desired_y)
17152 update_begin (f);
17153 FRAME_RIF (f)->update_window_begin_hook (w);
17154 FRAME_RIF (f)->clear_window_mouse_face (w);
17155 FRAME_RIF (f)->scroll_run_hook (w, &run);
17156 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17157 update_end (f);
17160 /* Shift current matrix down by nrows_scrolled lines. */
17161 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17162 rotate_matrix (w->current_matrix,
17163 start_vpos,
17164 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17165 nrows_scrolled);
17167 /* Disable lines that must be updated. */
17168 for (i = 0; i < nrows_scrolled; ++i)
17169 (start_row + i)->enabled_p = false;
17171 /* Re-compute Y positions. */
17172 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17173 max_y = it.last_visible_y;
17174 for (row = start_row + nrows_scrolled;
17175 row < bottom_row;
17176 ++row)
17178 row->y = it.current_y;
17179 row->visible_height = row->height;
17181 if (row->y < min_y)
17182 row->visible_height -= min_y - row->y;
17183 if (row->y + row->height > max_y)
17184 row->visible_height -= row->y + row->height - max_y;
17185 if (row->fringe_bitmap_periodic_p)
17186 row->redraw_fringe_bitmaps_p = 1;
17188 it.current_y += row->height;
17190 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17191 last_reused_text_row = row;
17192 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17193 break;
17196 /* Disable lines in the current matrix which are now
17197 below the window. */
17198 for (++row; row < bottom_row; ++row)
17199 row->enabled_p = row->mode_line_p = 0;
17202 /* Update window_end_pos etc.; last_reused_text_row is the last
17203 reused row from the current matrix containing text, if any.
17204 The value of last_text_row is the last displayed line
17205 containing text. */
17206 if (last_reused_text_row)
17207 adjust_window_ends (w, last_reused_text_row, 1);
17208 else if (last_text_row)
17209 adjust_window_ends (w, last_text_row, 0);
17210 else
17212 /* This window must be completely empty. */
17213 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17214 w->window_end_pos = Z - ZV;
17215 w->window_end_vpos = 0;
17217 w->window_end_valid = 0;
17219 /* Update hint: don't try scrolling again in update_window. */
17220 w->desired_matrix->no_scrolling_p = 1;
17222 #ifdef GLYPH_DEBUG
17223 debug_method_add (w, "try_window_reusing_current_matrix 1");
17224 #endif
17225 return 1;
17227 else if (CHARPOS (new_start) > CHARPOS (start))
17229 struct glyph_row *pt_row, *row;
17230 struct glyph_row *first_reusable_row;
17231 struct glyph_row *first_row_to_display;
17232 int dy;
17233 int yb = window_text_bottom_y (w);
17235 /* Find the row starting at new_start, if there is one. Don't
17236 reuse a partially visible line at the end. */
17237 first_reusable_row = start_row;
17238 while (first_reusable_row->enabled_p
17239 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17240 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17241 < CHARPOS (new_start)))
17242 ++first_reusable_row;
17244 /* Give up if there is no row to reuse. */
17245 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17246 || !first_reusable_row->enabled_p
17247 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17248 != CHARPOS (new_start)))
17249 return 0;
17251 /* We can reuse fully visible rows beginning with
17252 first_reusable_row to the end of the window. Set
17253 first_row_to_display to the first row that cannot be reused.
17254 Set pt_row to the row containing point, if there is any. */
17255 pt_row = NULL;
17256 for (first_row_to_display = first_reusable_row;
17257 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17258 ++first_row_to_display)
17260 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17261 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17262 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17263 && first_row_to_display->ends_at_zv_p
17264 && pt_row == NULL)))
17265 pt_row = first_row_to_display;
17268 /* Start displaying at the start of first_row_to_display. */
17269 eassert (first_row_to_display->y < yb);
17270 init_to_row_start (&it, w, first_row_to_display);
17272 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17273 - start_vpos);
17274 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17275 - nrows_scrolled);
17276 it.current_y = (first_row_to_display->y - first_reusable_row->y
17277 + WINDOW_HEADER_LINE_HEIGHT (w));
17279 /* Display lines beginning with first_row_to_display in the
17280 desired matrix. Set last_text_row to the last row displayed
17281 that displays text. */
17282 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17283 if (pt_row == NULL)
17284 w->cursor.vpos = -1;
17285 last_text_row = NULL;
17286 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17287 if (display_line (&it))
17288 last_text_row = it.glyph_row - 1;
17290 /* If point is in a reused row, adjust y and vpos of the cursor
17291 position. */
17292 if (pt_row)
17294 w->cursor.vpos -= nrows_scrolled;
17295 w->cursor.y -= first_reusable_row->y - start_row->y;
17298 /* Give up if point isn't in a row displayed or reused. (This
17299 also handles the case where w->cursor.vpos < nrows_scrolled
17300 after the calls to display_line, which can happen with scroll
17301 margins. See bug#1295.) */
17302 if (w->cursor.vpos < 0)
17304 clear_glyph_matrix (w->desired_matrix);
17305 return 0;
17308 /* Scroll the display. */
17309 run.current_y = first_reusable_row->y;
17310 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17311 run.height = it.last_visible_y - run.current_y;
17312 dy = run.current_y - run.desired_y;
17314 if (run.height)
17316 update_begin (f);
17317 FRAME_RIF (f)->update_window_begin_hook (w);
17318 FRAME_RIF (f)->clear_window_mouse_face (w);
17319 FRAME_RIF (f)->scroll_run_hook (w, &run);
17320 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17321 update_end (f);
17324 /* Adjust Y positions of reused rows. */
17325 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17326 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17327 max_y = it.last_visible_y;
17328 for (row = first_reusable_row; row < first_row_to_display; ++row)
17330 row->y -= dy;
17331 row->visible_height = row->height;
17332 if (row->y < min_y)
17333 row->visible_height -= min_y - row->y;
17334 if (row->y + row->height > max_y)
17335 row->visible_height -= row->y + row->height - max_y;
17336 if (row->fringe_bitmap_periodic_p)
17337 row->redraw_fringe_bitmaps_p = 1;
17340 /* Scroll the current matrix. */
17341 eassert (nrows_scrolled > 0);
17342 rotate_matrix (w->current_matrix,
17343 start_vpos,
17344 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17345 -nrows_scrolled);
17347 /* Disable rows not reused. */
17348 for (row -= nrows_scrolled; row < bottom_row; ++row)
17349 row->enabled_p = false;
17351 /* Point may have moved to a different line, so we cannot assume that
17352 the previous cursor position is valid; locate the correct row. */
17353 if (pt_row)
17355 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17356 row < bottom_row
17357 && PT >= MATRIX_ROW_END_CHARPOS (row)
17358 && !row->ends_at_zv_p;
17359 row++)
17361 w->cursor.vpos++;
17362 w->cursor.y = row->y;
17364 if (row < bottom_row)
17366 /* Can't simply scan the row for point with
17367 bidi-reordered glyph rows. Let set_cursor_from_row
17368 figure out where to put the cursor, and if it fails,
17369 give up. */
17370 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17372 if (!set_cursor_from_row (w, row, w->current_matrix,
17373 0, 0, 0, 0))
17375 clear_glyph_matrix (w->desired_matrix);
17376 return 0;
17379 else
17381 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17382 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17384 for (; glyph < end
17385 && (!BUFFERP (glyph->object)
17386 || glyph->charpos < PT);
17387 glyph++)
17389 w->cursor.hpos++;
17390 w->cursor.x += glyph->pixel_width;
17396 /* Adjust window end. A null value of last_text_row means that
17397 the window end is in reused rows which in turn means that
17398 only its vpos can have changed. */
17399 if (last_text_row)
17400 adjust_window_ends (w, last_text_row, 0);
17401 else
17402 w->window_end_vpos -= nrows_scrolled;
17404 w->window_end_valid = 0;
17405 w->desired_matrix->no_scrolling_p = 1;
17407 #ifdef GLYPH_DEBUG
17408 debug_method_add (w, "try_window_reusing_current_matrix 2");
17409 #endif
17410 return 1;
17413 return 0;
17418 /************************************************************************
17419 Window redisplay reusing current matrix when buffer has changed
17420 ************************************************************************/
17422 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17423 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17424 ptrdiff_t *, ptrdiff_t *);
17425 static struct glyph_row *
17426 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17427 struct glyph_row *);
17430 /* Return the last row in MATRIX displaying text. If row START is
17431 non-null, start searching with that row. IT gives the dimensions
17432 of the display. Value is null if matrix is empty; otherwise it is
17433 a pointer to the row found. */
17435 static struct glyph_row *
17436 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17437 struct glyph_row *start)
17439 struct glyph_row *row, *row_found;
17441 /* Set row_found to the last row in IT->w's current matrix
17442 displaying text. The loop looks funny but think of partially
17443 visible lines. */
17444 row_found = NULL;
17445 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17446 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17448 eassert (row->enabled_p);
17449 row_found = row;
17450 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17451 break;
17452 ++row;
17455 return row_found;
17459 /* Return the last row in the current matrix of W that is not affected
17460 by changes at the start of current_buffer that occurred since W's
17461 current matrix was built. Value is null if no such row exists.
17463 BEG_UNCHANGED us the number of characters unchanged at the start of
17464 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17465 first changed character in current_buffer. Characters at positions <
17466 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17467 when the current matrix was built. */
17469 static struct glyph_row *
17470 find_last_unchanged_at_beg_row (struct window *w)
17472 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17473 struct glyph_row *row;
17474 struct glyph_row *row_found = NULL;
17475 int yb = window_text_bottom_y (w);
17477 /* Find the last row displaying unchanged text. */
17478 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17479 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17480 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17481 ++row)
17483 if (/* If row ends before first_changed_pos, it is unchanged,
17484 except in some case. */
17485 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17486 /* When row ends in ZV and we write at ZV it is not
17487 unchanged. */
17488 && !row->ends_at_zv_p
17489 /* When first_changed_pos is the end of a continued line,
17490 row is not unchanged because it may be no longer
17491 continued. */
17492 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17493 && (row->continued_p
17494 || row->exact_window_width_line_p))
17495 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17496 needs to be recomputed, so don't consider this row as
17497 unchanged. This happens when the last line was
17498 bidi-reordered and was killed immediately before this
17499 redisplay cycle. In that case, ROW->end stores the
17500 buffer position of the first visual-order character of
17501 the killed text, which is now beyond ZV. */
17502 && CHARPOS (row->end.pos) <= ZV)
17503 row_found = row;
17505 /* Stop if last visible row. */
17506 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17507 break;
17510 return row_found;
17514 /* Find the first glyph row in the current matrix of W that is not
17515 affected by changes at the end of current_buffer since the
17516 time W's current matrix was built.
17518 Return in *DELTA the number of chars by which buffer positions in
17519 unchanged text at the end of current_buffer must be adjusted.
17521 Return in *DELTA_BYTES the corresponding number of bytes.
17523 Value is null if no such row exists, i.e. all rows are affected by
17524 changes. */
17526 static struct glyph_row *
17527 find_first_unchanged_at_end_row (struct window *w,
17528 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17530 struct glyph_row *row;
17531 struct glyph_row *row_found = NULL;
17533 *delta = *delta_bytes = 0;
17535 /* Display must not have been paused, otherwise the current matrix
17536 is not up to date. */
17537 eassert (w->window_end_valid);
17539 /* A value of window_end_pos >= END_UNCHANGED means that the window
17540 end is in the range of changed text. If so, there is no
17541 unchanged row at the end of W's current matrix. */
17542 if (w->window_end_pos >= END_UNCHANGED)
17543 return NULL;
17545 /* Set row to the last row in W's current matrix displaying text. */
17546 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17548 /* If matrix is entirely empty, no unchanged row exists. */
17549 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17551 /* The value of row is the last glyph row in the matrix having a
17552 meaningful buffer position in it. The end position of row
17553 corresponds to window_end_pos. This allows us to translate
17554 buffer positions in the current matrix to current buffer
17555 positions for characters not in changed text. */
17556 ptrdiff_t Z_old =
17557 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17558 ptrdiff_t Z_BYTE_old =
17559 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17560 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17561 struct glyph_row *first_text_row
17562 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17564 *delta = Z - Z_old;
17565 *delta_bytes = Z_BYTE - Z_BYTE_old;
17567 /* Set last_unchanged_pos to the buffer position of the last
17568 character in the buffer that has not been changed. Z is the
17569 index + 1 of the last character in current_buffer, i.e. by
17570 subtracting END_UNCHANGED we get the index of the last
17571 unchanged character, and we have to add BEG to get its buffer
17572 position. */
17573 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17574 last_unchanged_pos_old = last_unchanged_pos - *delta;
17576 /* Search backward from ROW for a row displaying a line that
17577 starts at a minimum position >= last_unchanged_pos_old. */
17578 for (; row > first_text_row; --row)
17580 /* This used to abort, but it can happen.
17581 It is ok to just stop the search instead here. KFS. */
17582 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17583 break;
17585 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17586 row_found = row;
17590 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17592 return row_found;
17596 /* Make sure that glyph rows in the current matrix of window W
17597 reference the same glyph memory as corresponding rows in the
17598 frame's frame matrix. This function is called after scrolling W's
17599 current matrix on a terminal frame in try_window_id and
17600 try_window_reusing_current_matrix. */
17602 static void
17603 sync_frame_with_window_matrix_rows (struct window *w)
17605 struct frame *f = XFRAME (w->frame);
17606 struct glyph_row *window_row, *window_row_end, *frame_row;
17608 /* Preconditions: W must be a leaf window and full-width. Its frame
17609 must have a frame matrix. */
17610 eassert (BUFFERP (w->contents));
17611 eassert (WINDOW_FULL_WIDTH_P (w));
17612 eassert (!FRAME_WINDOW_P (f));
17614 /* If W is a full-width window, glyph pointers in W's current matrix
17615 have, by definition, to be the same as glyph pointers in the
17616 corresponding frame matrix. Note that frame matrices have no
17617 marginal areas (see build_frame_matrix). */
17618 window_row = w->current_matrix->rows;
17619 window_row_end = window_row + w->current_matrix->nrows;
17620 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17621 while (window_row < window_row_end)
17623 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17624 struct glyph *end = window_row->glyphs[LAST_AREA];
17626 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17627 frame_row->glyphs[TEXT_AREA] = start;
17628 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17629 frame_row->glyphs[LAST_AREA] = end;
17631 /* Disable frame rows whose corresponding window rows have
17632 been disabled in try_window_id. */
17633 if (!window_row->enabled_p)
17634 frame_row->enabled_p = false;
17636 ++window_row, ++frame_row;
17641 /* Find the glyph row in window W containing CHARPOS. Consider all
17642 rows between START and END (not inclusive). END null means search
17643 all rows to the end of the display area of W. Value is the row
17644 containing CHARPOS or null. */
17646 struct glyph_row *
17647 row_containing_pos (struct window *w, ptrdiff_t charpos,
17648 struct glyph_row *start, struct glyph_row *end, int dy)
17650 struct glyph_row *row = start;
17651 struct glyph_row *best_row = NULL;
17652 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17653 int last_y;
17655 /* If we happen to start on a header-line, skip that. */
17656 if (row->mode_line_p)
17657 ++row;
17659 if ((end && row >= end) || !row->enabled_p)
17660 return NULL;
17662 last_y = window_text_bottom_y (w) - dy;
17664 while (1)
17666 /* Give up if we have gone too far. */
17667 if (end && row >= end)
17668 return NULL;
17669 /* This formerly returned if they were equal.
17670 I think that both quantities are of a "last plus one" type;
17671 if so, when they are equal, the row is within the screen. -- rms. */
17672 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17673 return NULL;
17675 /* If it is in this row, return this row. */
17676 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17677 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17678 /* The end position of a row equals the start
17679 position of the next row. If CHARPOS is there, we
17680 would rather consider it displayed in the next
17681 line, except when this line ends in ZV. */
17682 && !row_for_charpos_p (row, charpos)))
17683 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17685 struct glyph *g;
17687 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17688 || (!best_row && !row->continued_p))
17689 return row;
17690 /* In bidi-reordered rows, there could be several rows whose
17691 edges surround CHARPOS, all of these rows belonging to
17692 the same continued line. We need to find the row which
17693 fits CHARPOS the best. */
17694 for (g = row->glyphs[TEXT_AREA];
17695 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17696 g++)
17698 if (!STRINGP (g->object))
17700 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17702 mindif = eabs (g->charpos - charpos);
17703 best_row = row;
17704 /* Exact match always wins. */
17705 if (mindif == 0)
17706 return best_row;
17711 else if (best_row && !row->continued_p)
17712 return best_row;
17713 ++row;
17718 /* Try to redisplay window W by reusing its existing display. W's
17719 current matrix must be up to date when this function is called,
17720 i.e. window_end_valid must be nonzero.
17722 Value is
17724 >= 1 if successful, i.e. display has been updated
17725 specifically:
17726 1 means the changes were in front of a newline that precedes
17727 the window start, and the whole current matrix was reused
17728 2 means the changes were after the last position displayed
17729 in the window, and the whole current matrix was reused
17730 3 means portions of the current matrix were reused, while
17731 some of the screen lines were redrawn
17732 -1 if redisplay with same window start is known not to succeed
17733 0 if otherwise unsuccessful
17735 The following steps are performed:
17737 1. Find the last row in the current matrix of W that is not
17738 affected by changes at the start of current_buffer. If no such row
17739 is found, give up.
17741 2. Find the first row in W's current matrix that is not affected by
17742 changes at the end of current_buffer. Maybe there is no such row.
17744 3. Display lines beginning with the row + 1 found in step 1 to the
17745 row found in step 2 or, if step 2 didn't find a row, to the end of
17746 the window.
17748 4. If cursor is not known to appear on the window, give up.
17750 5. If display stopped at the row found in step 2, scroll the
17751 display and current matrix as needed.
17753 6. Maybe display some lines at the end of W, if we must. This can
17754 happen under various circumstances, like a partially visible line
17755 becoming fully visible, or because newly displayed lines are displayed
17756 in smaller font sizes.
17758 7. Update W's window end information. */
17760 static int
17761 try_window_id (struct window *w)
17763 struct frame *f = XFRAME (w->frame);
17764 struct glyph_matrix *current_matrix = w->current_matrix;
17765 struct glyph_matrix *desired_matrix = w->desired_matrix;
17766 struct glyph_row *last_unchanged_at_beg_row;
17767 struct glyph_row *first_unchanged_at_end_row;
17768 struct glyph_row *row;
17769 struct glyph_row *bottom_row;
17770 int bottom_vpos;
17771 struct it it;
17772 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17773 int dvpos, dy;
17774 struct text_pos start_pos;
17775 struct run run;
17776 int first_unchanged_at_end_vpos = 0;
17777 struct glyph_row *last_text_row, *last_text_row_at_end;
17778 struct text_pos start;
17779 ptrdiff_t first_changed_charpos, last_changed_charpos;
17781 #ifdef GLYPH_DEBUG
17782 if (inhibit_try_window_id)
17783 return 0;
17784 #endif
17786 /* This is handy for debugging. */
17787 #if 0
17788 #define GIVE_UP(X) \
17789 do { \
17790 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17791 return 0; \
17792 } while (0)
17793 #else
17794 #define GIVE_UP(X) return 0
17795 #endif
17797 SET_TEXT_POS_FROM_MARKER (start, w->start);
17799 /* Don't use this for mini-windows because these can show
17800 messages and mini-buffers, and we don't handle that here. */
17801 if (MINI_WINDOW_P (w))
17802 GIVE_UP (1);
17804 /* This flag is used to prevent redisplay optimizations. */
17805 if (windows_or_buffers_changed || f->cursor_type_changed)
17806 GIVE_UP (2);
17808 /* This function's optimizations cannot be used if overlays have
17809 changed in the buffer displayed by the window, so give up if they
17810 have. */
17811 if (w->last_overlay_modified != OVERLAY_MODIFF)
17812 GIVE_UP (21);
17814 /* Verify that narrowing has not changed.
17815 Also verify that we were not told to prevent redisplay optimizations.
17816 It would be nice to further
17817 reduce the number of cases where this prevents try_window_id. */
17818 if (current_buffer->clip_changed
17819 || current_buffer->prevent_redisplay_optimizations_p)
17820 GIVE_UP (3);
17822 /* Window must either use window-based redisplay or be full width. */
17823 if (!FRAME_WINDOW_P (f)
17824 && (!FRAME_LINE_INS_DEL_OK (f)
17825 || !WINDOW_FULL_WIDTH_P (w)))
17826 GIVE_UP (4);
17828 /* Give up if point is known NOT to appear in W. */
17829 if (PT < CHARPOS (start))
17830 GIVE_UP (5);
17832 /* Another way to prevent redisplay optimizations. */
17833 if (w->last_modified == 0)
17834 GIVE_UP (6);
17836 /* Verify that window is not hscrolled. */
17837 if (w->hscroll != 0)
17838 GIVE_UP (7);
17840 /* Verify that display wasn't paused. */
17841 if (!w->window_end_valid)
17842 GIVE_UP (8);
17844 /* Likewise if highlighting trailing whitespace. */
17845 if (!NILP (Vshow_trailing_whitespace))
17846 GIVE_UP (11);
17848 /* Can't use this if overlay arrow position and/or string have
17849 changed. */
17850 if (overlay_arrows_changed_p ())
17851 GIVE_UP (12);
17853 /* When word-wrap is on, adding a space to the first word of a
17854 wrapped line can change the wrap position, altering the line
17855 above it. It might be worthwhile to handle this more
17856 intelligently, but for now just redisplay from scratch. */
17857 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17858 GIVE_UP (21);
17860 /* Under bidi reordering, adding or deleting a character in the
17861 beginning of a paragraph, before the first strong directional
17862 character, can change the base direction of the paragraph (unless
17863 the buffer specifies a fixed paragraph direction), which will
17864 require to redisplay the whole paragraph. It might be worthwhile
17865 to find the paragraph limits and widen the range of redisplayed
17866 lines to that, but for now just give up this optimization and
17867 redisplay from scratch. */
17868 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17869 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17870 GIVE_UP (22);
17872 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17873 only if buffer has really changed. The reason is that the gap is
17874 initially at Z for freshly visited files. The code below would
17875 set end_unchanged to 0 in that case. */
17876 if (MODIFF > SAVE_MODIFF
17877 /* This seems to happen sometimes after saving a buffer. */
17878 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17880 if (GPT - BEG < BEG_UNCHANGED)
17881 BEG_UNCHANGED = GPT - BEG;
17882 if (Z - GPT < END_UNCHANGED)
17883 END_UNCHANGED = Z - GPT;
17886 /* The position of the first and last character that has been changed. */
17887 first_changed_charpos = BEG + BEG_UNCHANGED;
17888 last_changed_charpos = Z - END_UNCHANGED;
17890 /* If window starts after a line end, and the last change is in
17891 front of that newline, then changes don't affect the display.
17892 This case happens with stealth-fontification. Note that although
17893 the display is unchanged, glyph positions in the matrix have to
17894 be adjusted, of course. */
17895 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17896 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17897 && ((last_changed_charpos < CHARPOS (start)
17898 && CHARPOS (start) == BEGV)
17899 || (last_changed_charpos < CHARPOS (start) - 1
17900 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17902 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17903 struct glyph_row *r0;
17905 /* Compute how many chars/bytes have been added to or removed
17906 from the buffer. */
17907 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17908 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17909 Z_delta = Z - Z_old;
17910 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17912 /* Give up if PT is not in the window. Note that it already has
17913 been checked at the start of try_window_id that PT is not in
17914 front of the window start. */
17915 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17916 GIVE_UP (13);
17918 /* If window start is unchanged, we can reuse the whole matrix
17919 as is, after adjusting glyph positions. No need to compute
17920 the window end again, since its offset from Z hasn't changed. */
17921 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17922 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17923 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17924 /* PT must not be in a partially visible line. */
17925 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17926 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17928 /* Adjust positions in the glyph matrix. */
17929 if (Z_delta || Z_delta_bytes)
17931 struct glyph_row *r1
17932 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17933 increment_matrix_positions (w->current_matrix,
17934 MATRIX_ROW_VPOS (r0, current_matrix),
17935 MATRIX_ROW_VPOS (r1, current_matrix),
17936 Z_delta, Z_delta_bytes);
17939 /* Set the cursor. */
17940 row = row_containing_pos (w, PT, r0, NULL, 0);
17941 if (row)
17942 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17943 return 1;
17947 /* Handle the case that changes are all below what is displayed in
17948 the window, and that PT is in the window. This shortcut cannot
17949 be taken if ZV is visible in the window, and text has been added
17950 there that is visible in the window. */
17951 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17952 /* ZV is not visible in the window, or there are no
17953 changes at ZV, actually. */
17954 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17955 || first_changed_charpos == last_changed_charpos))
17957 struct glyph_row *r0;
17959 /* Give up if PT is not in the window. Note that it already has
17960 been checked at the start of try_window_id that PT is not in
17961 front of the window start. */
17962 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17963 GIVE_UP (14);
17965 /* If window start is unchanged, we can reuse the whole matrix
17966 as is, without changing glyph positions since no text has
17967 been added/removed in front of the window end. */
17968 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17969 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17970 /* PT must not be in a partially visible line. */
17971 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17972 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17974 /* We have to compute the window end anew since text
17975 could have been added/removed after it. */
17976 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17977 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17979 /* Set the cursor. */
17980 row = row_containing_pos (w, PT, r0, NULL, 0);
17981 if (row)
17982 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17983 return 2;
17987 /* Give up if window start is in the changed area.
17989 The condition used to read
17991 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17993 but why that was tested escapes me at the moment. */
17994 if (CHARPOS (start) >= first_changed_charpos
17995 && CHARPOS (start) <= last_changed_charpos)
17996 GIVE_UP (15);
17998 /* Check that window start agrees with the start of the first glyph
17999 row in its current matrix. Check this after we know the window
18000 start is not in changed text, otherwise positions would not be
18001 comparable. */
18002 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18003 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18004 GIVE_UP (16);
18006 /* Give up if the window ends in strings. Overlay strings
18007 at the end are difficult to handle, so don't try. */
18008 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18009 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18010 GIVE_UP (20);
18012 /* Compute the position at which we have to start displaying new
18013 lines. Some of the lines at the top of the window might be
18014 reusable because they are not displaying changed text. Find the
18015 last row in W's current matrix not affected by changes at the
18016 start of current_buffer. Value is null if changes start in the
18017 first line of window. */
18018 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18019 if (last_unchanged_at_beg_row)
18021 /* Avoid starting to display in the middle of a character, a TAB
18022 for instance. This is easier than to set up the iterator
18023 exactly, and it's not a frequent case, so the additional
18024 effort wouldn't really pay off. */
18025 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18026 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18027 && last_unchanged_at_beg_row > w->current_matrix->rows)
18028 --last_unchanged_at_beg_row;
18030 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18031 GIVE_UP (17);
18033 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18034 GIVE_UP (18);
18035 start_pos = it.current.pos;
18037 /* Start displaying new lines in the desired matrix at the same
18038 vpos we would use in the current matrix, i.e. below
18039 last_unchanged_at_beg_row. */
18040 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18041 current_matrix);
18042 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18043 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18045 eassert (it.hpos == 0 && it.current_x == 0);
18047 else
18049 /* There are no reusable lines at the start of the window.
18050 Start displaying in the first text line. */
18051 start_display (&it, w, start);
18052 it.vpos = it.first_vpos;
18053 start_pos = it.current.pos;
18056 /* Find the first row that is not affected by changes at the end of
18057 the buffer. Value will be null if there is no unchanged row, in
18058 which case we must redisplay to the end of the window. delta
18059 will be set to the value by which buffer positions beginning with
18060 first_unchanged_at_end_row have to be adjusted due to text
18061 changes. */
18062 first_unchanged_at_end_row
18063 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18064 IF_DEBUG (debug_delta = delta);
18065 IF_DEBUG (debug_delta_bytes = delta_bytes);
18067 /* Set stop_pos to the buffer position up to which we will have to
18068 display new lines. If first_unchanged_at_end_row != NULL, this
18069 is the buffer position of the start of the line displayed in that
18070 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18071 that we don't stop at a buffer position. */
18072 stop_pos = 0;
18073 if (first_unchanged_at_end_row)
18075 eassert (last_unchanged_at_beg_row == NULL
18076 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18078 /* If this is a continuation line, move forward to the next one
18079 that isn't. Changes in lines above affect this line.
18080 Caution: this may move first_unchanged_at_end_row to a row
18081 not displaying text. */
18082 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18083 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18084 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18085 < it.last_visible_y))
18086 ++first_unchanged_at_end_row;
18088 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18089 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18090 >= it.last_visible_y))
18091 first_unchanged_at_end_row = NULL;
18092 else
18094 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18095 + delta);
18096 first_unchanged_at_end_vpos
18097 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18098 eassert (stop_pos >= Z - END_UNCHANGED);
18101 else if (last_unchanged_at_beg_row == NULL)
18102 GIVE_UP (19);
18105 #ifdef GLYPH_DEBUG
18107 /* Either there is no unchanged row at the end, or the one we have
18108 now displays text. This is a necessary condition for the window
18109 end pos calculation at the end of this function. */
18110 eassert (first_unchanged_at_end_row == NULL
18111 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18113 debug_last_unchanged_at_beg_vpos
18114 = (last_unchanged_at_beg_row
18115 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18116 : -1);
18117 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18119 #endif /* GLYPH_DEBUG */
18122 /* Display new lines. Set last_text_row to the last new line
18123 displayed which has text on it, i.e. might end up as being the
18124 line where the window_end_vpos is. */
18125 w->cursor.vpos = -1;
18126 last_text_row = NULL;
18127 overlay_arrow_seen = 0;
18128 if (it.current_y < it.last_visible_y
18129 && !f->fonts_changed
18130 && (first_unchanged_at_end_row == NULL
18131 || IT_CHARPOS (it) < stop_pos))
18132 it.glyph_row->reversed_p = false;
18133 while (it.current_y < it.last_visible_y
18134 && !f->fonts_changed
18135 && (first_unchanged_at_end_row == NULL
18136 || IT_CHARPOS (it) < stop_pos))
18138 if (display_line (&it))
18139 last_text_row = it.glyph_row - 1;
18142 if (f->fonts_changed)
18143 return -1;
18146 /* Compute differences in buffer positions, y-positions etc. for
18147 lines reused at the bottom of the window. Compute what we can
18148 scroll. */
18149 if (first_unchanged_at_end_row
18150 /* No lines reused because we displayed everything up to the
18151 bottom of the window. */
18152 && it.current_y < it.last_visible_y)
18154 dvpos = (it.vpos
18155 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18156 current_matrix));
18157 dy = it.current_y - first_unchanged_at_end_row->y;
18158 run.current_y = first_unchanged_at_end_row->y;
18159 run.desired_y = run.current_y + dy;
18160 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18162 else
18164 delta = delta_bytes = dvpos = dy
18165 = run.current_y = run.desired_y = run.height = 0;
18166 first_unchanged_at_end_row = NULL;
18168 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18171 /* Find the cursor if not already found. We have to decide whether
18172 PT will appear on this window (it sometimes doesn't, but this is
18173 not a very frequent case.) This decision has to be made before
18174 the current matrix is altered. A value of cursor.vpos < 0 means
18175 that PT is either in one of the lines beginning at
18176 first_unchanged_at_end_row or below the window. Don't care for
18177 lines that might be displayed later at the window end; as
18178 mentioned, this is not a frequent case. */
18179 if (w->cursor.vpos < 0)
18181 /* Cursor in unchanged rows at the top? */
18182 if (PT < CHARPOS (start_pos)
18183 && last_unchanged_at_beg_row)
18185 row = row_containing_pos (w, PT,
18186 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18187 last_unchanged_at_beg_row + 1, 0);
18188 if (row)
18189 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18192 /* Start from first_unchanged_at_end_row looking for PT. */
18193 else if (first_unchanged_at_end_row)
18195 row = row_containing_pos (w, PT - delta,
18196 first_unchanged_at_end_row, NULL, 0);
18197 if (row)
18198 set_cursor_from_row (w, row, w->current_matrix, delta,
18199 delta_bytes, dy, dvpos);
18202 /* Give up if cursor was not found. */
18203 if (w->cursor.vpos < 0)
18205 clear_glyph_matrix (w->desired_matrix);
18206 return -1;
18210 /* Don't let the cursor end in the scroll margins. */
18212 int this_scroll_margin, cursor_height;
18213 int frame_line_height = default_line_pixel_height (w);
18214 int window_total_lines
18215 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18217 this_scroll_margin =
18218 max (0, min (scroll_margin, window_total_lines / 4));
18219 this_scroll_margin *= frame_line_height;
18220 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18222 if ((w->cursor.y < this_scroll_margin
18223 && CHARPOS (start) > BEGV)
18224 /* Old redisplay didn't take scroll margin into account at the bottom,
18225 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18226 || (w->cursor.y + (make_cursor_line_fully_visible_p
18227 ? cursor_height + this_scroll_margin
18228 : 1)) > it.last_visible_y)
18230 w->cursor.vpos = -1;
18231 clear_glyph_matrix (w->desired_matrix);
18232 return -1;
18236 /* Scroll the display. Do it before changing the current matrix so
18237 that xterm.c doesn't get confused about where the cursor glyph is
18238 found. */
18239 if (dy && run.height)
18241 update_begin (f);
18243 if (FRAME_WINDOW_P (f))
18245 FRAME_RIF (f)->update_window_begin_hook (w);
18246 FRAME_RIF (f)->clear_window_mouse_face (w);
18247 FRAME_RIF (f)->scroll_run_hook (w, &run);
18248 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18250 else
18252 /* Terminal frame. In this case, dvpos gives the number of
18253 lines to scroll by; dvpos < 0 means scroll up. */
18254 int from_vpos
18255 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18256 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18257 int end = (WINDOW_TOP_EDGE_LINE (w)
18258 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18259 + window_internal_height (w));
18261 #if defined (HAVE_GPM) || defined (MSDOS)
18262 x_clear_window_mouse_face (w);
18263 #endif
18264 /* Perform the operation on the screen. */
18265 if (dvpos > 0)
18267 /* Scroll last_unchanged_at_beg_row to the end of the
18268 window down dvpos lines. */
18269 set_terminal_window (f, end);
18271 /* On dumb terminals delete dvpos lines at the end
18272 before inserting dvpos empty lines. */
18273 if (!FRAME_SCROLL_REGION_OK (f))
18274 ins_del_lines (f, end - dvpos, -dvpos);
18276 /* Insert dvpos empty lines in front of
18277 last_unchanged_at_beg_row. */
18278 ins_del_lines (f, from, dvpos);
18280 else if (dvpos < 0)
18282 /* Scroll up last_unchanged_at_beg_vpos to the end of
18283 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18284 set_terminal_window (f, end);
18286 /* Delete dvpos lines in front of
18287 last_unchanged_at_beg_vpos. ins_del_lines will set
18288 the cursor to the given vpos and emit |dvpos| delete
18289 line sequences. */
18290 ins_del_lines (f, from + dvpos, dvpos);
18292 /* On a dumb terminal insert dvpos empty lines at the
18293 end. */
18294 if (!FRAME_SCROLL_REGION_OK (f))
18295 ins_del_lines (f, end + dvpos, -dvpos);
18298 set_terminal_window (f, 0);
18301 update_end (f);
18304 /* Shift reused rows of the current matrix to the right position.
18305 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18306 text. */
18307 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18308 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18309 if (dvpos < 0)
18311 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18312 bottom_vpos, dvpos);
18313 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18314 bottom_vpos);
18316 else if (dvpos > 0)
18318 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18319 bottom_vpos, dvpos);
18320 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18321 first_unchanged_at_end_vpos + dvpos);
18324 /* For frame-based redisplay, make sure that current frame and window
18325 matrix are in sync with respect to glyph memory. */
18326 if (!FRAME_WINDOW_P (f))
18327 sync_frame_with_window_matrix_rows (w);
18329 /* Adjust buffer positions in reused rows. */
18330 if (delta || delta_bytes)
18331 increment_matrix_positions (current_matrix,
18332 first_unchanged_at_end_vpos + dvpos,
18333 bottom_vpos, delta, delta_bytes);
18335 /* Adjust Y positions. */
18336 if (dy)
18337 shift_glyph_matrix (w, current_matrix,
18338 first_unchanged_at_end_vpos + dvpos,
18339 bottom_vpos, dy);
18341 if (first_unchanged_at_end_row)
18343 first_unchanged_at_end_row += dvpos;
18344 if (first_unchanged_at_end_row->y >= it.last_visible_y
18345 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18346 first_unchanged_at_end_row = NULL;
18349 /* If scrolling up, there may be some lines to display at the end of
18350 the window. */
18351 last_text_row_at_end = NULL;
18352 if (dy < 0)
18354 /* Scrolling up can leave for example a partially visible line
18355 at the end of the window to be redisplayed. */
18356 /* Set last_row to the glyph row in the current matrix where the
18357 window end line is found. It has been moved up or down in
18358 the matrix by dvpos. */
18359 int last_vpos = w->window_end_vpos + dvpos;
18360 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18362 /* If last_row is the window end line, it should display text. */
18363 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18365 /* If window end line was partially visible before, begin
18366 displaying at that line. Otherwise begin displaying with the
18367 line following it. */
18368 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18370 init_to_row_start (&it, w, last_row);
18371 it.vpos = last_vpos;
18372 it.current_y = last_row->y;
18374 else
18376 init_to_row_end (&it, w, last_row);
18377 it.vpos = 1 + last_vpos;
18378 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18379 ++last_row;
18382 /* We may start in a continuation line. If so, we have to
18383 get the right continuation_lines_width and current_x. */
18384 it.continuation_lines_width = last_row->continuation_lines_width;
18385 it.hpos = it.current_x = 0;
18387 /* Display the rest of the lines at the window end. */
18388 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18389 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18391 /* Is it always sure that the display agrees with lines in
18392 the current matrix? I don't think so, so we mark rows
18393 displayed invalid in the current matrix by setting their
18394 enabled_p flag to zero. */
18395 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18396 if (display_line (&it))
18397 last_text_row_at_end = it.glyph_row - 1;
18401 /* Update window_end_pos and window_end_vpos. */
18402 if (first_unchanged_at_end_row && !last_text_row_at_end)
18404 /* Window end line if one of the preserved rows from the current
18405 matrix. Set row to the last row displaying text in current
18406 matrix starting at first_unchanged_at_end_row, after
18407 scrolling. */
18408 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18409 row = find_last_row_displaying_text (w->current_matrix, &it,
18410 first_unchanged_at_end_row);
18411 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18412 adjust_window_ends (w, row, 1);
18413 eassert (w->window_end_bytepos >= 0);
18414 IF_DEBUG (debug_method_add (w, "A"));
18416 else if (last_text_row_at_end)
18418 adjust_window_ends (w, last_text_row_at_end, 0);
18419 eassert (w->window_end_bytepos >= 0);
18420 IF_DEBUG (debug_method_add (w, "B"));
18422 else if (last_text_row)
18424 /* We have displayed either to the end of the window or at the
18425 end of the window, i.e. the last row with text is to be found
18426 in the desired matrix. */
18427 adjust_window_ends (w, last_text_row, 0);
18428 eassert (w->window_end_bytepos >= 0);
18430 else if (first_unchanged_at_end_row == NULL
18431 && last_text_row == NULL
18432 && last_text_row_at_end == NULL)
18434 /* Displayed to end of window, but no line containing text was
18435 displayed. Lines were deleted at the end of the window. */
18436 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18437 int vpos = w->window_end_vpos;
18438 struct glyph_row *current_row = current_matrix->rows + vpos;
18439 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18441 for (row = NULL;
18442 row == NULL && vpos >= first_vpos;
18443 --vpos, --current_row, --desired_row)
18445 if (desired_row->enabled_p)
18447 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18448 row = desired_row;
18450 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18451 row = current_row;
18454 eassert (row != NULL);
18455 w->window_end_vpos = vpos + 1;
18456 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18457 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18458 eassert (w->window_end_bytepos >= 0);
18459 IF_DEBUG (debug_method_add (w, "C"));
18461 else
18462 emacs_abort ();
18464 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18465 debug_end_vpos = w->window_end_vpos));
18467 /* Record that display has not been completed. */
18468 w->window_end_valid = 0;
18469 w->desired_matrix->no_scrolling_p = 1;
18470 return 3;
18472 #undef GIVE_UP
18477 /***********************************************************************
18478 More debugging support
18479 ***********************************************************************/
18481 #ifdef GLYPH_DEBUG
18483 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18484 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18485 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18488 /* Dump the contents of glyph matrix MATRIX on stderr.
18490 GLYPHS 0 means don't show glyph contents.
18491 GLYPHS 1 means show glyphs in short form
18492 GLYPHS > 1 means show glyphs in long form. */
18494 void
18495 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18497 int i;
18498 for (i = 0; i < matrix->nrows; ++i)
18499 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18503 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18504 the glyph row and area where the glyph comes from. */
18506 void
18507 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18509 if (glyph->type == CHAR_GLYPH
18510 || glyph->type == GLYPHLESS_GLYPH)
18512 fprintf (stderr,
18513 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18514 glyph - row->glyphs[TEXT_AREA],
18515 (glyph->type == CHAR_GLYPH
18516 ? 'C'
18517 : 'G'),
18518 glyph->charpos,
18519 (BUFFERP (glyph->object)
18520 ? 'B'
18521 : (STRINGP (glyph->object)
18522 ? 'S'
18523 : (NILP (glyph->object)
18524 ? '0'
18525 : '-'))),
18526 glyph->pixel_width,
18527 glyph->u.ch,
18528 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18529 ? glyph->u.ch
18530 : '.'),
18531 glyph->face_id,
18532 glyph->left_box_line_p,
18533 glyph->right_box_line_p);
18535 else if (glyph->type == STRETCH_GLYPH)
18537 fprintf (stderr,
18538 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18539 glyph - row->glyphs[TEXT_AREA],
18540 'S',
18541 glyph->charpos,
18542 (BUFFERP (glyph->object)
18543 ? 'B'
18544 : (STRINGP (glyph->object)
18545 ? 'S'
18546 : (NILP (glyph->object)
18547 ? '0'
18548 : '-'))),
18549 glyph->pixel_width,
18551 ' ',
18552 glyph->face_id,
18553 glyph->left_box_line_p,
18554 glyph->right_box_line_p);
18556 else if (glyph->type == IMAGE_GLYPH)
18558 fprintf (stderr,
18559 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18560 glyph - row->glyphs[TEXT_AREA],
18561 'I',
18562 glyph->charpos,
18563 (BUFFERP (glyph->object)
18564 ? 'B'
18565 : (STRINGP (glyph->object)
18566 ? 'S'
18567 : (NILP (glyph->object)
18568 ? '0'
18569 : '-'))),
18570 glyph->pixel_width,
18571 glyph->u.img_id,
18572 '.',
18573 glyph->face_id,
18574 glyph->left_box_line_p,
18575 glyph->right_box_line_p);
18577 else if (glyph->type == COMPOSITE_GLYPH)
18579 fprintf (stderr,
18580 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18581 glyph - row->glyphs[TEXT_AREA],
18582 '+',
18583 glyph->charpos,
18584 (BUFFERP (glyph->object)
18585 ? 'B'
18586 : (STRINGP (glyph->object)
18587 ? 'S'
18588 : (NILP (glyph->object)
18589 ? '0'
18590 : '-'))),
18591 glyph->pixel_width,
18592 glyph->u.cmp.id);
18593 if (glyph->u.cmp.automatic)
18594 fprintf (stderr,
18595 "[%d-%d]",
18596 glyph->slice.cmp.from, glyph->slice.cmp.to);
18597 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18598 glyph->face_id,
18599 glyph->left_box_line_p,
18600 glyph->right_box_line_p);
18605 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18606 GLYPHS 0 means don't show glyph contents.
18607 GLYPHS 1 means show glyphs in short form
18608 GLYPHS > 1 means show glyphs in long form. */
18610 void
18611 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18613 if (glyphs != 1)
18615 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18616 fprintf (stderr, "==============================================================================\n");
18618 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18619 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18620 vpos,
18621 MATRIX_ROW_START_CHARPOS (row),
18622 MATRIX_ROW_END_CHARPOS (row),
18623 row->used[TEXT_AREA],
18624 row->contains_overlapping_glyphs_p,
18625 row->enabled_p,
18626 row->truncated_on_left_p,
18627 row->truncated_on_right_p,
18628 row->continued_p,
18629 MATRIX_ROW_CONTINUATION_LINE_P (row),
18630 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18631 row->ends_at_zv_p,
18632 row->fill_line_p,
18633 row->ends_in_middle_of_char_p,
18634 row->starts_in_middle_of_char_p,
18635 row->mouse_face_p,
18636 row->x,
18637 row->y,
18638 row->pixel_width,
18639 row->height,
18640 row->visible_height,
18641 row->ascent,
18642 row->phys_ascent);
18643 /* The next 3 lines should align to "Start" in the header. */
18644 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18645 row->end.overlay_string_index,
18646 row->continuation_lines_width);
18647 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18648 CHARPOS (row->start.string_pos),
18649 CHARPOS (row->end.string_pos));
18650 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18651 row->end.dpvec_index);
18654 if (glyphs > 1)
18656 int area;
18658 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18660 struct glyph *glyph = row->glyphs[area];
18661 struct glyph *glyph_end = glyph + row->used[area];
18663 /* Glyph for a line end in text. */
18664 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18665 ++glyph_end;
18667 if (glyph < glyph_end)
18668 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18670 for (; glyph < glyph_end; ++glyph)
18671 dump_glyph (row, glyph, area);
18674 else if (glyphs == 1)
18676 int area;
18677 char s[SHRT_MAX + 4];
18679 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18681 int i;
18683 for (i = 0; i < row->used[area]; ++i)
18685 struct glyph *glyph = row->glyphs[area] + i;
18686 if (i == row->used[area] - 1
18687 && area == TEXT_AREA
18688 && NILP (glyph->object)
18689 && glyph->type == CHAR_GLYPH
18690 && glyph->u.ch == ' ')
18692 strcpy (&s[i], "[\\n]");
18693 i += 4;
18695 else if (glyph->type == CHAR_GLYPH
18696 && glyph->u.ch < 0x80
18697 && glyph->u.ch >= ' ')
18698 s[i] = glyph->u.ch;
18699 else
18700 s[i] = '.';
18703 s[i] = '\0';
18704 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18710 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18711 Sdump_glyph_matrix, 0, 1, "p",
18712 doc: /* Dump the current matrix of the selected window to stderr.
18713 Shows contents of glyph row structures. With non-nil
18714 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18715 glyphs in short form, otherwise show glyphs in long form.
18717 Interactively, no argument means show glyphs in short form;
18718 with numeric argument, its value is passed as the GLYPHS flag. */)
18719 (Lisp_Object glyphs)
18721 struct window *w = XWINDOW (selected_window);
18722 struct buffer *buffer = XBUFFER (w->contents);
18724 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18725 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18726 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18727 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18728 fprintf (stderr, "=============================================\n");
18729 dump_glyph_matrix (w->current_matrix,
18730 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18731 return Qnil;
18735 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18736 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18737 Only text-mode frames have frame glyph matrices. */)
18738 (void)
18740 struct frame *f = XFRAME (selected_frame);
18742 if (f->current_matrix)
18743 dump_glyph_matrix (f->current_matrix, 1);
18744 else
18745 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18746 return Qnil;
18750 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18751 doc: /* Dump glyph row ROW to stderr.
18752 GLYPH 0 means don't dump glyphs.
18753 GLYPH 1 means dump glyphs in short form.
18754 GLYPH > 1 or omitted means dump glyphs in long form. */)
18755 (Lisp_Object row, Lisp_Object glyphs)
18757 struct glyph_matrix *matrix;
18758 EMACS_INT vpos;
18760 CHECK_NUMBER (row);
18761 matrix = XWINDOW (selected_window)->current_matrix;
18762 vpos = XINT (row);
18763 if (vpos >= 0 && vpos < matrix->nrows)
18764 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18765 vpos,
18766 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18767 return Qnil;
18771 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18772 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18773 GLYPH 0 means don't dump glyphs.
18774 GLYPH 1 means dump glyphs in short form.
18775 GLYPH > 1 or omitted means dump glyphs in long form.
18777 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18778 do nothing. */)
18779 (Lisp_Object row, Lisp_Object glyphs)
18781 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18782 struct frame *sf = SELECTED_FRAME ();
18783 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18784 EMACS_INT vpos;
18786 CHECK_NUMBER (row);
18787 vpos = XINT (row);
18788 if (vpos >= 0 && vpos < m->nrows)
18789 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18790 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18791 #endif
18792 return Qnil;
18796 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18797 doc: /* Toggle tracing of redisplay.
18798 With ARG, turn tracing on if and only if ARG is positive. */)
18799 (Lisp_Object arg)
18801 if (NILP (arg))
18802 trace_redisplay_p = !trace_redisplay_p;
18803 else
18805 arg = Fprefix_numeric_value (arg);
18806 trace_redisplay_p = XINT (arg) > 0;
18809 return Qnil;
18813 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18814 doc: /* Like `format', but print result to stderr.
18815 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18816 (ptrdiff_t nargs, Lisp_Object *args)
18818 Lisp_Object s = Fformat (nargs, args);
18819 fprintf (stderr, "%s", SDATA (s));
18820 return Qnil;
18823 #endif /* GLYPH_DEBUG */
18827 /***********************************************************************
18828 Building Desired Matrix Rows
18829 ***********************************************************************/
18831 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18832 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18834 static struct glyph_row *
18835 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18837 struct frame *f = XFRAME (WINDOW_FRAME (w));
18838 struct buffer *buffer = XBUFFER (w->contents);
18839 struct buffer *old = current_buffer;
18840 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18841 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18842 const unsigned char *arrow_end = arrow_string + arrow_len;
18843 const unsigned char *p;
18844 struct it it;
18845 bool multibyte_p;
18846 int n_glyphs_before;
18848 set_buffer_temp (buffer);
18849 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18850 scratch_glyph_row.reversed_p = false;
18851 it.glyph_row->used[TEXT_AREA] = 0;
18852 SET_TEXT_POS (it.position, 0, 0);
18854 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18855 p = arrow_string;
18856 while (p < arrow_end)
18858 Lisp_Object face, ilisp;
18860 /* Get the next character. */
18861 if (multibyte_p)
18862 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18863 else
18865 it.c = it.char_to_display = *p, it.len = 1;
18866 if (! ASCII_CHAR_P (it.c))
18867 it.char_to_display = BYTE8_TO_CHAR (it.c);
18869 p += it.len;
18871 /* Get its face. */
18872 ilisp = make_number (p - arrow_string);
18873 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18874 it.face_id = compute_char_face (f, it.char_to_display, face);
18876 /* Compute its width, get its glyphs. */
18877 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18878 SET_TEXT_POS (it.position, -1, -1);
18879 PRODUCE_GLYPHS (&it);
18881 /* If this character doesn't fit any more in the line, we have
18882 to remove some glyphs. */
18883 if (it.current_x > it.last_visible_x)
18885 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18886 break;
18890 set_buffer_temp (old);
18891 return it.glyph_row;
18895 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18896 glyphs to insert is determined by produce_special_glyphs. */
18898 static void
18899 insert_left_trunc_glyphs (struct it *it)
18901 struct it truncate_it;
18902 struct glyph *from, *end, *to, *toend;
18904 eassert (!FRAME_WINDOW_P (it->f)
18905 || (!it->glyph_row->reversed_p
18906 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18907 || (it->glyph_row->reversed_p
18908 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18910 /* Get the truncation glyphs. */
18911 truncate_it = *it;
18912 truncate_it.current_x = 0;
18913 truncate_it.face_id = DEFAULT_FACE_ID;
18914 truncate_it.glyph_row = &scratch_glyph_row;
18915 truncate_it.area = TEXT_AREA;
18916 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18917 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18918 truncate_it.object = Qnil;
18919 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18921 /* Overwrite glyphs from IT with truncation glyphs. */
18922 if (!it->glyph_row->reversed_p)
18924 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18926 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18927 end = from + tused;
18928 to = it->glyph_row->glyphs[TEXT_AREA];
18929 toend = to + it->glyph_row->used[TEXT_AREA];
18930 if (FRAME_WINDOW_P (it->f))
18932 /* On GUI frames, when variable-size fonts are displayed,
18933 the truncation glyphs may need more pixels than the row's
18934 glyphs they overwrite. We overwrite more glyphs to free
18935 enough screen real estate, and enlarge the stretch glyph
18936 on the right (see display_line), if there is one, to
18937 preserve the screen position of the truncation glyphs on
18938 the right. */
18939 int w = 0;
18940 struct glyph *g = to;
18941 short used;
18943 /* The first glyph could be partially visible, in which case
18944 it->glyph_row->x will be negative. But we want the left
18945 truncation glyphs to be aligned at the left margin of the
18946 window, so we override the x coordinate at which the row
18947 will begin. */
18948 it->glyph_row->x = 0;
18949 while (g < toend && w < it->truncation_pixel_width)
18951 w += g->pixel_width;
18952 ++g;
18954 if (g - to - tused > 0)
18956 memmove (to + tused, g, (toend - g) * sizeof(*g));
18957 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18959 used = it->glyph_row->used[TEXT_AREA];
18960 if (it->glyph_row->truncated_on_right_p
18961 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18962 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18963 == STRETCH_GLYPH)
18965 int extra = w - it->truncation_pixel_width;
18967 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18971 while (from < end)
18972 *to++ = *from++;
18974 /* There may be padding glyphs left over. Overwrite them too. */
18975 if (!FRAME_WINDOW_P (it->f))
18977 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18979 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18980 while (from < end)
18981 *to++ = *from++;
18985 if (to > toend)
18986 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18988 else
18990 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18992 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18993 that back to front. */
18994 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18995 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18996 toend = it->glyph_row->glyphs[TEXT_AREA];
18997 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18998 if (FRAME_WINDOW_P (it->f))
19000 int w = 0;
19001 struct glyph *g = to;
19003 while (g >= toend && w < it->truncation_pixel_width)
19005 w += g->pixel_width;
19006 --g;
19008 if (to - g - tused > 0)
19009 to = g + tused;
19010 if (it->glyph_row->truncated_on_right_p
19011 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19012 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19014 int extra = w - it->truncation_pixel_width;
19016 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19020 while (from >= end && to >= toend)
19021 *to-- = *from--;
19022 if (!FRAME_WINDOW_P (it->f))
19024 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19026 from =
19027 truncate_it.glyph_row->glyphs[TEXT_AREA]
19028 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19029 while (from >= end && to >= toend)
19030 *to-- = *from--;
19033 if (from >= end)
19035 /* Need to free some room before prepending additional
19036 glyphs. */
19037 int move_by = from - end + 1;
19038 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19039 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19041 for ( ; g >= g0; g--)
19042 g[move_by] = *g;
19043 while (from >= end)
19044 *to-- = *from--;
19045 it->glyph_row->used[TEXT_AREA] += move_by;
19050 /* Compute the hash code for ROW. */
19051 unsigned
19052 row_hash (struct glyph_row *row)
19054 int area, k;
19055 unsigned hashval = 0;
19057 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19058 for (k = 0; k < row->used[area]; ++k)
19059 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19060 + row->glyphs[area][k].u.val
19061 + row->glyphs[area][k].face_id
19062 + row->glyphs[area][k].padding_p
19063 + (row->glyphs[area][k].type << 2));
19065 return hashval;
19068 /* Compute the pixel height and width of IT->glyph_row.
19070 Most of the time, ascent and height of a display line will be equal
19071 to the max_ascent and max_height values of the display iterator
19072 structure. This is not the case if
19074 1. We hit ZV without displaying anything. In this case, max_ascent
19075 and max_height will be zero.
19077 2. We have some glyphs that don't contribute to the line height.
19078 (The glyph row flag contributes_to_line_height_p is for future
19079 pixmap extensions).
19081 The first case is easily covered by using default values because in
19082 these cases, the line height does not really matter, except that it
19083 must not be zero. */
19085 static void
19086 compute_line_metrics (struct it *it)
19088 struct glyph_row *row = it->glyph_row;
19090 if (FRAME_WINDOW_P (it->f))
19092 int i, min_y, max_y;
19094 /* The line may consist of one space only, that was added to
19095 place the cursor on it. If so, the row's height hasn't been
19096 computed yet. */
19097 if (row->height == 0)
19099 if (it->max_ascent + it->max_descent == 0)
19100 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19101 row->ascent = it->max_ascent;
19102 row->height = it->max_ascent + it->max_descent;
19103 row->phys_ascent = it->max_phys_ascent;
19104 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19105 row->extra_line_spacing = it->max_extra_line_spacing;
19108 /* Compute the width of this line. */
19109 row->pixel_width = row->x;
19110 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19111 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19113 eassert (row->pixel_width >= 0);
19114 eassert (row->ascent >= 0 && row->height > 0);
19116 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19117 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19119 /* If first line's physical ascent is larger than its logical
19120 ascent, use the physical ascent, and make the row taller.
19121 This makes accented characters fully visible. */
19122 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19123 && row->phys_ascent > row->ascent)
19125 row->height += row->phys_ascent - row->ascent;
19126 row->ascent = row->phys_ascent;
19129 /* Compute how much of the line is visible. */
19130 row->visible_height = row->height;
19132 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19133 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19135 if (row->y < min_y)
19136 row->visible_height -= min_y - row->y;
19137 if (row->y + row->height > max_y)
19138 row->visible_height -= row->y + row->height - max_y;
19140 else
19142 row->pixel_width = row->used[TEXT_AREA];
19143 if (row->continued_p)
19144 row->pixel_width -= it->continuation_pixel_width;
19145 else if (row->truncated_on_right_p)
19146 row->pixel_width -= it->truncation_pixel_width;
19147 row->ascent = row->phys_ascent = 0;
19148 row->height = row->phys_height = row->visible_height = 1;
19149 row->extra_line_spacing = 0;
19152 /* Compute a hash code for this row. */
19153 row->hash = row_hash (row);
19155 it->max_ascent = it->max_descent = 0;
19156 it->max_phys_ascent = it->max_phys_descent = 0;
19160 /* Append one space to the glyph row of iterator IT if doing a
19161 window-based redisplay. The space has the same face as
19162 IT->face_id. Value is non-zero if a space was added.
19164 This function is called to make sure that there is always one glyph
19165 at the end of a glyph row that the cursor can be set on under
19166 window-systems. (If there weren't such a glyph we would not know
19167 how wide and tall a box cursor should be displayed).
19169 At the same time this space let's a nicely handle clearing to the
19170 end of the line if the row ends in italic text. */
19172 static int
19173 append_space_for_newline (struct it *it, int default_face_p)
19175 if (FRAME_WINDOW_P (it->f))
19177 int n = it->glyph_row->used[TEXT_AREA];
19179 if (it->glyph_row->glyphs[TEXT_AREA] + n
19180 < it->glyph_row->glyphs[1 + TEXT_AREA])
19182 /* Save some values that must not be changed.
19183 Must save IT->c and IT->len because otherwise
19184 ITERATOR_AT_END_P wouldn't work anymore after
19185 append_space_for_newline has been called. */
19186 enum display_element_type saved_what = it->what;
19187 int saved_c = it->c, saved_len = it->len;
19188 int saved_char_to_display = it->char_to_display;
19189 int saved_x = it->current_x;
19190 int saved_face_id = it->face_id;
19191 int saved_box_end = it->end_of_box_run_p;
19192 struct text_pos saved_pos;
19193 Lisp_Object saved_object;
19194 struct face *face;
19196 saved_object = it->object;
19197 saved_pos = it->position;
19199 it->what = IT_CHARACTER;
19200 memset (&it->position, 0, sizeof it->position);
19201 it->object = Qnil;
19202 it->c = it->char_to_display = ' ';
19203 it->len = 1;
19205 /* If the default face was remapped, be sure to use the
19206 remapped face for the appended newline. */
19207 if (default_face_p)
19208 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19209 else if (it->face_before_selective_p)
19210 it->face_id = it->saved_face_id;
19211 face = FACE_FROM_ID (it->f, it->face_id);
19212 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19213 /* In R2L rows, we will prepend a stretch glyph that will
19214 have the end_of_box_run_p flag set for it, so there's no
19215 need for the appended newline glyph to have that flag
19216 set. */
19217 if (it->glyph_row->reversed_p
19218 /* But if the appended newline glyph goes all the way to
19219 the end of the row, there will be no stretch glyph,
19220 so leave the box flag set. */
19221 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19222 it->end_of_box_run_p = 0;
19224 PRODUCE_GLYPHS (it);
19226 it->override_ascent = -1;
19227 it->constrain_row_ascent_descent_p = 0;
19228 it->current_x = saved_x;
19229 it->object = saved_object;
19230 it->position = saved_pos;
19231 it->what = saved_what;
19232 it->face_id = saved_face_id;
19233 it->len = saved_len;
19234 it->c = saved_c;
19235 it->char_to_display = saved_char_to_display;
19236 it->end_of_box_run_p = saved_box_end;
19237 return 1;
19241 return 0;
19245 /* Extend the face of the last glyph in the text area of IT->glyph_row
19246 to the end of the display line. Called from display_line. If the
19247 glyph row is empty, add a space glyph to it so that we know the
19248 face to draw. Set the glyph row flag fill_line_p. If the glyph
19249 row is R2L, prepend a stretch glyph to cover the empty space to the
19250 left of the leftmost glyph. */
19252 static void
19253 extend_face_to_end_of_line (struct it *it)
19255 struct face *face, *default_face;
19256 struct frame *f = it->f;
19258 /* If line is already filled, do nothing. Non window-system frames
19259 get a grace of one more ``pixel'' because their characters are
19260 1-``pixel'' wide, so they hit the equality too early. This grace
19261 is needed only for R2L rows that are not continued, to produce
19262 one extra blank where we could display the cursor. */
19263 if ((it->current_x >= it->last_visible_x
19264 + (!FRAME_WINDOW_P (f)
19265 && it->glyph_row->reversed_p
19266 && !it->glyph_row->continued_p))
19267 /* If the window has display margins, we will need to extend
19268 their face even if the text area is filled. */
19269 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19270 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19271 return;
19273 /* The default face, possibly remapped. */
19274 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19276 /* Face extension extends the background and box of IT->face_id
19277 to the end of the line. If the background equals the background
19278 of the frame, we don't have to do anything. */
19279 if (it->face_before_selective_p)
19280 face = FACE_FROM_ID (f, it->saved_face_id);
19281 else
19282 face = FACE_FROM_ID (f, it->face_id);
19284 if (FRAME_WINDOW_P (f)
19285 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19286 && face->box == FACE_NO_BOX
19287 && face->background == FRAME_BACKGROUND_PIXEL (f)
19288 #ifdef HAVE_WINDOW_SYSTEM
19289 && !face->stipple
19290 #endif
19291 && !it->glyph_row->reversed_p)
19292 return;
19294 /* Set the glyph row flag indicating that the face of the last glyph
19295 in the text area has to be drawn to the end of the text area. */
19296 it->glyph_row->fill_line_p = 1;
19298 /* If current character of IT is not ASCII, make sure we have the
19299 ASCII face. This will be automatically undone the next time
19300 get_next_display_element returns a multibyte character. Note
19301 that the character will always be single byte in unibyte
19302 text. */
19303 if (!ASCII_CHAR_P (it->c))
19305 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19308 if (FRAME_WINDOW_P (f))
19310 /* If the row is empty, add a space with the current face of IT,
19311 so that we know which face to draw. */
19312 if (it->glyph_row->used[TEXT_AREA] == 0)
19314 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19315 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19316 it->glyph_row->used[TEXT_AREA] = 1;
19318 /* Mode line and the header line don't have margins, and
19319 likewise the frame's tool-bar window, if there is any. */
19320 if (!(it->glyph_row->mode_line_p
19321 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19322 || (WINDOWP (f->tool_bar_window)
19323 && it->w == XWINDOW (f->tool_bar_window))
19324 #endif
19327 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19328 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19330 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19331 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19332 default_face->id;
19333 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19335 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19336 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19338 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19339 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19340 default_face->id;
19341 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19344 #ifdef HAVE_WINDOW_SYSTEM
19345 if (it->glyph_row->reversed_p)
19347 /* Prepend a stretch glyph to the row, such that the
19348 rightmost glyph will be drawn flushed all the way to the
19349 right margin of the window. The stretch glyph that will
19350 occupy the empty space, if any, to the left of the
19351 glyphs. */
19352 struct font *font = face->font ? face->font : FRAME_FONT (f);
19353 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19354 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19355 struct glyph *g;
19356 int row_width, stretch_ascent, stretch_width;
19357 struct text_pos saved_pos;
19358 int saved_face_id, saved_avoid_cursor, saved_box_start;
19360 for (row_width = 0, g = row_start; g < row_end; g++)
19361 row_width += g->pixel_width;
19363 /* FIXME: There are various minor display glitches in R2L
19364 rows when only one of the fringes is missing. The
19365 strange condition below produces the least bad effect. */
19366 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19367 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19368 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19369 stretch_width = window_box_width (it->w, TEXT_AREA);
19370 else
19371 stretch_width = it->last_visible_x - it->first_visible_x;
19372 stretch_width -= row_width;
19374 if (stretch_width > 0)
19376 stretch_ascent =
19377 (((it->ascent + it->descent)
19378 * FONT_BASE (font)) / FONT_HEIGHT (font));
19379 saved_pos = it->position;
19380 memset (&it->position, 0, sizeof it->position);
19381 saved_avoid_cursor = it->avoid_cursor_p;
19382 it->avoid_cursor_p = 1;
19383 saved_face_id = it->face_id;
19384 saved_box_start = it->start_of_box_run_p;
19385 /* The last row's stretch glyph should get the default
19386 face, to avoid painting the rest of the window with
19387 the region face, if the region ends at ZV. */
19388 if (it->glyph_row->ends_at_zv_p)
19389 it->face_id = default_face->id;
19390 else
19391 it->face_id = face->id;
19392 it->start_of_box_run_p = 0;
19393 append_stretch_glyph (it, Qnil, stretch_width,
19394 it->ascent + it->descent, stretch_ascent);
19395 it->position = saved_pos;
19396 it->avoid_cursor_p = saved_avoid_cursor;
19397 it->face_id = saved_face_id;
19398 it->start_of_box_run_p = saved_box_start;
19400 /* If stretch_width comes out negative, it means that the
19401 last glyph is only partially visible. In R2L rows, we
19402 want the leftmost glyph to be partially visible, so we
19403 need to give the row the corresponding left offset. */
19404 if (stretch_width < 0)
19405 it->glyph_row->x = stretch_width;
19407 #endif /* HAVE_WINDOW_SYSTEM */
19409 else
19411 /* Save some values that must not be changed. */
19412 int saved_x = it->current_x;
19413 struct text_pos saved_pos;
19414 Lisp_Object saved_object;
19415 enum display_element_type saved_what = it->what;
19416 int saved_face_id = it->face_id;
19418 saved_object = it->object;
19419 saved_pos = it->position;
19421 it->what = IT_CHARACTER;
19422 memset (&it->position, 0, sizeof it->position);
19423 it->object = Qnil;
19424 it->c = it->char_to_display = ' ';
19425 it->len = 1;
19427 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19428 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19429 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19430 && !it->glyph_row->mode_line_p
19431 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19433 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19434 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19436 for (it->current_x = 0; g < e; g++)
19437 it->current_x += g->pixel_width;
19439 it->area = LEFT_MARGIN_AREA;
19440 it->face_id = default_face->id;
19441 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19442 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19444 PRODUCE_GLYPHS (it);
19445 /* term.c:produce_glyphs advances it->current_x only for
19446 TEXT_AREA. */
19447 it->current_x += it->pixel_width;
19450 it->current_x = saved_x;
19451 it->area = TEXT_AREA;
19454 /* The last row's blank glyphs should get the default face, to
19455 avoid painting the rest of the window with the region face,
19456 if the region ends at ZV. */
19457 if (it->glyph_row->ends_at_zv_p)
19458 it->face_id = default_face->id;
19459 else
19460 it->face_id = face->id;
19461 PRODUCE_GLYPHS (it);
19463 while (it->current_x <= it->last_visible_x)
19464 PRODUCE_GLYPHS (it);
19466 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19467 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19468 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19469 && !it->glyph_row->mode_line_p
19470 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19472 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19473 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19475 for ( ; g < e; g++)
19476 it->current_x += g->pixel_width;
19478 it->area = RIGHT_MARGIN_AREA;
19479 it->face_id = default_face->id;
19480 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19481 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19483 PRODUCE_GLYPHS (it);
19484 it->current_x += it->pixel_width;
19487 it->area = TEXT_AREA;
19490 /* Don't count these blanks really. It would let us insert a left
19491 truncation glyph below and make us set the cursor on them, maybe. */
19492 it->current_x = saved_x;
19493 it->object = saved_object;
19494 it->position = saved_pos;
19495 it->what = saved_what;
19496 it->face_id = saved_face_id;
19501 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19502 trailing whitespace. */
19504 static int
19505 trailing_whitespace_p (ptrdiff_t charpos)
19507 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19508 int c = 0;
19510 while (bytepos < ZV_BYTE
19511 && (c = FETCH_CHAR (bytepos),
19512 c == ' ' || c == '\t'))
19513 ++bytepos;
19515 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19517 if (bytepos != PT_BYTE)
19518 return 1;
19520 return 0;
19524 /* Highlight trailing whitespace, if any, in ROW. */
19526 static void
19527 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19529 int used = row->used[TEXT_AREA];
19531 if (used)
19533 struct glyph *start = row->glyphs[TEXT_AREA];
19534 struct glyph *glyph = start + used - 1;
19536 if (row->reversed_p)
19538 /* Right-to-left rows need to be processed in the opposite
19539 direction, so swap the edge pointers. */
19540 glyph = start;
19541 start = row->glyphs[TEXT_AREA] + used - 1;
19544 /* Skip over glyphs inserted to display the cursor at the
19545 end of a line, for extending the face of the last glyph
19546 to the end of the line on terminals, and for truncation
19547 and continuation glyphs. */
19548 if (!row->reversed_p)
19550 while (glyph >= start
19551 && glyph->type == CHAR_GLYPH
19552 && NILP (glyph->object))
19553 --glyph;
19555 else
19557 while (glyph <= start
19558 && glyph->type == CHAR_GLYPH
19559 && NILP (glyph->object))
19560 ++glyph;
19563 /* If last glyph is a space or stretch, and it's trailing
19564 whitespace, set the face of all trailing whitespace glyphs in
19565 IT->glyph_row to `trailing-whitespace'. */
19566 if ((row->reversed_p ? glyph <= start : glyph >= start)
19567 && BUFFERP (glyph->object)
19568 && (glyph->type == STRETCH_GLYPH
19569 || (glyph->type == CHAR_GLYPH
19570 && glyph->u.ch == ' '))
19571 && trailing_whitespace_p (glyph->charpos))
19573 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19574 if (face_id < 0)
19575 return;
19577 if (!row->reversed_p)
19579 while (glyph >= start
19580 && BUFFERP (glyph->object)
19581 && (glyph->type == STRETCH_GLYPH
19582 || (glyph->type == CHAR_GLYPH
19583 && glyph->u.ch == ' ')))
19584 (glyph--)->face_id = face_id;
19586 else
19588 while (glyph <= start
19589 && BUFFERP (glyph->object)
19590 && (glyph->type == STRETCH_GLYPH
19591 || (glyph->type == CHAR_GLYPH
19592 && glyph->u.ch == ' ')))
19593 (glyph++)->face_id = face_id;
19600 /* Value is non-zero if glyph row ROW should be
19601 considered to hold the buffer position CHARPOS. */
19603 static int
19604 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19606 int result = 1;
19608 if (charpos == CHARPOS (row->end.pos)
19609 || charpos == MATRIX_ROW_END_CHARPOS (row))
19611 /* Suppose the row ends on a string.
19612 Unless the row is continued, that means it ends on a newline
19613 in the string. If it's anything other than a display string
19614 (e.g., a before-string from an overlay), we don't want the
19615 cursor there. (This heuristic seems to give the optimal
19616 behavior for the various types of multi-line strings.)
19617 One exception: if the string has `cursor' property on one of
19618 its characters, we _do_ want the cursor there. */
19619 if (CHARPOS (row->end.string_pos) >= 0)
19621 if (row->continued_p)
19622 result = 1;
19623 else
19625 /* Check for `display' property. */
19626 struct glyph *beg = row->glyphs[TEXT_AREA];
19627 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19628 struct glyph *glyph;
19630 result = 0;
19631 for (glyph = end; glyph >= beg; --glyph)
19632 if (STRINGP (glyph->object))
19634 Lisp_Object prop
19635 = Fget_char_property (make_number (charpos),
19636 Qdisplay, Qnil);
19637 result =
19638 (!NILP (prop)
19639 && display_prop_string_p (prop, glyph->object));
19640 /* If there's a `cursor' property on one of the
19641 string's characters, this row is a cursor row,
19642 even though this is not a display string. */
19643 if (!result)
19645 Lisp_Object s = glyph->object;
19647 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19649 ptrdiff_t gpos = glyph->charpos;
19651 if (!NILP (Fget_char_property (make_number (gpos),
19652 Qcursor, s)))
19654 result = 1;
19655 break;
19659 break;
19663 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19665 /* If the row ends in middle of a real character,
19666 and the line is continued, we want the cursor here.
19667 That's because CHARPOS (ROW->end.pos) would equal
19668 PT if PT is before the character. */
19669 if (!row->ends_in_ellipsis_p)
19670 result = row->continued_p;
19671 else
19672 /* If the row ends in an ellipsis, then
19673 CHARPOS (ROW->end.pos) will equal point after the
19674 invisible text. We want that position to be displayed
19675 after the ellipsis. */
19676 result = 0;
19678 /* If the row ends at ZV, display the cursor at the end of that
19679 row instead of at the start of the row below. */
19680 else if (row->ends_at_zv_p)
19681 result = 1;
19682 else
19683 result = 0;
19686 return result;
19689 /* Value is non-zero if glyph row ROW should be
19690 used to hold the cursor. */
19692 static int
19693 cursor_row_p (struct glyph_row *row)
19695 return row_for_charpos_p (row, PT);
19700 /* Push the property PROP so that it will be rendered at the current
19701 position in IT. Return 1 if PROP was successfully pushed, 0
19702 otherwise. Called from handle_line_prefix to handle the
19703 `line-prefix' and `wrap-prefix' properties. */
19705 static int
19706 push_prefix_prop (struct it *it, Lisp_Object prop)
19708 struct text_pos pos =
19709 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19711 eassert (it->method == GET_FROM_BUFFER
19712 || it->method == GET_FROM_DISPLAY_VECTOR
19713 || it->method == GET_FROM_STRING);
19715 /* We need to save the current buffer/string position, so it will be
19716 restored by pop_it, because iterate_out_of_display_property
19717 depends on that being set correctly, but some situations leave
19718 it->position not yet set when this function is called. */
19719 push_it (it, &pos);
19721 if (STRINGP (prop))
19723 if (SCHARS (prop) == 0)
19725 pop_it (it);
19726 return 0;
19729 it->string = prop;
19730 it->string_from_prefix_prop_p = 1;
19731 it->multibyte_p = STRING_MULTIBYTE (it->string);
19732 it->current.overlay_string_index = -1;
19733 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19734 it->end_charpos = it->string_nchars = SCHARS (it->string);
19735 it->method = GET_FROM_STRING;
19736 it->stop_charpos = 0;
19737 it->prev_stop = 0;
19738 it->base_level_stop = 0;
19740 /* Force paragraph direction to be that of the parent
19741 buffer/string. */
19742 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19743 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19744 else
19745 it->paragraph_embedding = L2R;
19747 /* Set up the bidi iterator for this display string. */
19748 if (it->bidi_p)
19750 it->bidi_it.string.lstring = it->string;
19751 it->bidi_it.string.s = NULL;
19752 it->bidi_it.string.schars = it->end_charpos;
19753 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19754 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19755 it->bidi_it.string.unibyte = !it->multibyte_p;
19756 it->bidi_it.w = it->w;
19757 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19760 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19762 it->method = GET_FROM_STRETCH;
19763 it->object = prop;
19765 #ifdef HAVE_WINDOW_SYSTEM
19766 else if (IMAGEP (prop))
19768 it->what = IT_IMAGE;
19769 it->image_id = lookup_image (it->f, prop);
19770 it->method = GET_FROM_IMAGE;
19772 #endif /* HAVE_WINDOW_SYSTEM */
19773 else
19775 pop_it (it); /* bogus display property, give up */
19776 return 0;
19779 return 1;
19782 /* Return the character-property PROP at the current position in IT. */
19784 static Lisp_Object
19785 get_it_property (struct it *it, Lisp_Object prop)
19787 Lisp_Object position, object = it->object;
19789 if (STRINGP (object))
19790 position = make_number (IT_STRING_CHARPOS (*it));
19791 else if (BUFFERP (object))
19793 position = make_number (IT_CHARPOS (*it));
19794 object = it->window;
19796 else
19797 return Qnil;
19799 return Fget_char_property (position, prop, object);
19802 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19804 static void
19805 handle_line_prefix (struct it *it)
19807 Lisp_Object prefix;
19809 if (it->continuation_lines_width > 0)
19811 prefix = get_it_property (it, Qwrap_prefix);
19812 if (NILP (prefix))
19813 prefix = Vwrap_prefix;
19815 else
19817 prefix = get_it_property (it, Qline_prefix);
19818 if (NILP (prefix))
19819 prefix = Vline_prefix;
19821 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19823 /* If the prefix is wider than the window, and we try to wrap
19824 it, it would acquire its own wrap prefix, and so on till the
19825 iterator stack overflows. So, don't wrap the prefix. */
19826 it->line_wrap = TRUNCATE;
19827 it->avoid_cursor_p = 1;
19833 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19834 only for R2L lines from display_line and display_string, when they
19835 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19836 the line/string needs to be continued on the next glyph row. */
19837 static void
19838 unproduce_glyphs (struct it *it, int n)
19840 struct glyph *glyph, *end;
19842 eassert (it->glyph_row);
19843 eassert (it->glyph_row->reversed_p);
19844 eassert (it->area == TEXT_AREA);
19845 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19847 if (n > it->glyph_row->used[TEXT_AREA])
19848 n = it->glyph_row->used[TEXT_AREA];
19849 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19850 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19851 for ( ; glyph < end; glyph++)
19852 glyph[-n] = *glyph;
19855 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19856 and ROW->maxpos. */
19857 static void
19858 find_row_edges (struct it *it, struct glyph_row *row,
19859 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19860 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19862 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19863 lines' rows is implemented for bidi-reordered rows. */
19865 /* ROW->minpos is the value of min_pos, the minimal buffer position
19866 we have in ROW, or ROW->start.pos if that is smaller. */
19867 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19868 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19869 else
19870 /* We didn't find buffer positions smaller than ROW->start, or
19871 didn't find _any_ valid buffer positions in any of the glyphs,
19872 so we must trust the iterator's computed positions. */
19873 row->minpos = row->start.pos;
19874 if (max_pos <= 0)
19876 max_pos = CHARPOS (it->current.pos);
19877 max_bpos = BYTEPOS (it->current.pos);
19880 /* Here are the various use-cases for ending the row, and the
19881 corresponding values for ROW->maxpos:
19883 Line ends in a newline from buffer eol_pos + 1
19884 Line is continued from buffer max_pos + 1
19885 Line is truncated on right it->current.pos
19886 Line ends in a newline from string max_pos + 1(*)
19887 (*) + 1 only when line ends in a forward scan
19888 Line is continued from string max_pos
19889 Line is continued from display vector max_pos
19890 Line is entirely from a string min_pos == max_pos
19891 Line is entirely from a display vector min_pos == max_pos
19892 Line that ends at ZV ZV
19894 If you discover other use-cases, please add them here as
19895 appropriate. */
19896 if (row->ends_at_zv_p)
19897 row->maxpos = it->current.pos;
19898 else if (row->used[TEXT_AREA])
19900 int seen_this_string = 0;
19901 struct glyph_row *r1 = row - 1;
19903 /* Did we see the same display string on the previous row? */
19904 if (STRINGP (it->object)
19905 /* this is not the first row */
19906 && row > it->w->desired_matrix->rows
19907 /* previous row is not the header line */
19908 && !r1->mode_line_p
19909 /* previous row also ends in a newline from a string */
19910 && r1->ends_in_newline_from_string_p)
19912 struct glyph *start, *end;
19914 /* Search for the last glyph of the previous row that came
19915 from buffer or string. Depending on whether the row is
19916 L2R or R2L, we need to process it front to back or the
19917 other way round. */
19918 if (!r1->reversed_p)
19920 start = r1->glyphs[TEXT_AREA];
19921 end = start + r1->used[TEXT_AREA];
19922 /* Glyphs inserted by redisplay have nil as their object. */
19923 while (end > start
19924 && NILP ((end - 1)->object)
19925 && (end - 1)->charpos <= 0)
19926 --end;
19927 if (end > start)
19929 if (EQ ((end - 1)->object, it->object))
19930 seen_this_string = 1;
19932 else
19933 /* If all the glyphs of the previous row were inserted
19934 by redisplay, it means the previous row was
19935 produced from a single newline, which is only
19936 possible if that newline came from the same string
19937 as the one which produced this ROW. */
19938 seen_this_string = 1;
19940 else
19942 end = r1->glyphs[TEXT_AREA] - 1;
19943 start = end + r1->used[TEXT_AREA];
19944 while (end < start
19945 && NILP ((end + 1)->object)
19946 && (end + 1)->charpos <= 0)
19947 ++end;
19948 if (end < start)
19950 if (EQ ((end + 1)->object, it->object))
19951 seen_this_string = 1;
19953 else
19954 seen_this_string = 1;
19957 /* Take note of each display string that covers a newline only
19958 once, the first time we see it. This is for when a display
19959 string includes more than one newline in it. */
19960 if (row->ends_in_newline_from_string_p && !seen_this_string)
19962 /* If we were scanning the buffer forward when we displayed
19963 the string, we want to account for at least one buffer
19964 position that belongs to this row (position covered by
19965 the display string), so that cursor positioning will
19966 consider this row as a candidate when point is at the end
19967 of the visual line represented by this row. This is not
19968 required when scanning back, because max_pos will already
19969 have a much larger value. */
19970 if (CHARPOS (row->end.pos) > max_pos)
19971 INC_BOTH (max_pos, max_bpos);
19972 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19974 else if (CHARPOS (it->eol_pos) > 0)
19975 SET_TEXT_POS (row->maxpos,
19976 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19977 else if (row->continued_p)
19979 /* If max_pos is different from IT's current position, it
19980 means IT->method does not belong to the display element
19981 at max_pos. However, it also means that the display
19982 element at max_pos was displayed in its entirety on this
19983 line, which is equivalent to saying that the next line
19984 starts at the next buffer position. */
19985 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19986 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19987 else
19989 INC_BOTH (max_pos, max_bpos);
19990 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19993 else if (row->truncated_on_right_p)
19994 /* display_line already called reseat_at_next_visible_line_start,
19995 which puts the iterator at the beginning of the next line, in
19996 the logical order. */
19997 row->maxpos = it->current.pos;
19998 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19999 /* A line that is entirely from a string/image/stretch... */
20000 row->maxpos = row->minpos;
20001 else
20002 emacs_abort ();
20004 else
20005 row->maxpos = it->current.pos;
20008 /* Construct the glyph row IT->glyph_row in the desired matrix of
20009 IT->w from text at the current position of IT. See dispextern.h
20010 for an overview of struct it. Value is non-zero if
20011 IT->glyph_row displays text, as opposed to a line displaying ZV
20012 only. */
20014 static int
20015 display_line (struct it *it)
20017 struct glyph_row *row = it->glyph_row;
20018 Lisp_Object overlay_arrow_string;
20019 struct it wrap_it;
20020 void *wrap_data = NULL;
20021 int may_wrap = 0, wrap_x IF_LINT (= 0);
20022 int wrap_row_used = -1;
20023 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20024 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20025 int wrap_row_extra_line_spacing IF_LINT (= 0);
20026 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20027 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20028 int cvpos;
20029 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20030 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20031 bool pending_handle_line_prefix = false;
20033 /* We always start displaying at hpos zero even if hscrolled. */
20034 eassert (it->hpos == 0 && it->current_x == 0);
20036 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20037 >= it->w->desired_matrix->nrows)
20039 it->w->nrows_scale_factor++;
20040 it->f->fonts_changed = 1;
20041 return 0;
20044 /* Clear the result glyph row and enable it. */
20045 prepare_desired_row (it->w, row, false);
20047 row->y = it->current_y;
20048 row->start = it->start;
20049 row->continuation_lines_width = it->continuation_lines_width;
20050 row->displays_text_p = 1;
20051 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20052 it->starts_in_middle_of_char_p = 0;
20054 /* Arrange the overlays nicely for our purposes. Usually, we call
20055 display_line on only one line at a time, in which case this
20056 can't really hurt too much, or we call it on lines which appear
20057 one after another in the buffer, in which case all calls to
20058 recenter_overlay_lists but the first will be pretty cheap. */
20059 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20061 /* Move over display elements that are not visible because we are
20062 hscrolled. This may stop at an x-position < IT->first_visible_x
20063 if the first glyph is partially visible or if we hit a line end. */
20064 if (it->current_x < it->first_visible_x)
20066 enum move_it_result move_result;
20068 this_line_min_pos = row->start.pos;
20069 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20070 MOVE_TO_POS | MOVE_TO_X);
20071 /* If we are under a large hscroll, move_it_in_display_line_to
20072 could hit the end of the line without reaching
20073 it->first_visible_x. Pretend that we did reach it. This is
20074 especially important on a TTY, where we will call
20075 extend_face_to_end_of_line, which needs to know how many
20076 blank glyphs to produce. */
20077 if (it->current_x < it->first_visible_x
20078 && (move_result == MOVE_NEWLINE_OR_CR
20079 || move_result == MOVE_POS_MATCH_OR_ZV))
20080 it->current_x = it->first_visible_x;
20082 /* Record the smallest positions seen while we moved over
20083 display elements that are not visible. This is needed by
20084 redisplay_internal for optimizing the case where the cursor
20085 stays inside the same line. The rest of this function only
20086 considers positions that are actually displayed, so
20087 RECORD_MAX_MIN_POS will not otherwise record positions that
20088 are hscrolled to the left of the left edge of the window. */
20089 min_pos = CHARPOS (this_line_min_pos);
20090 min_bpos = BYTEPOS (this_line_min_pos);
20092 else if (it->area == TEXT_AREA)
20094 /* We only do this when not calling move_it_in_display_line_to
20095 above, because that function calls itself handle_line_prefix. */
20096 handle_line_prefix (it);
20098 else
20100 /* Line-prefix and wrap-prefix are always displayed in the text
20101 area. But if this is the first call to display_line after
20102 init_iterator, the iterator might have been set up to write
20103 into a marginal area, e.g. if the line begins with some
20104 display property that writes to the margins. So we need to
20105 wait with the call to handle_line_prefix until whatever
20106 writes to the margin has done its job. */
20107 pending_handle_line_prefix = true;
20110 /* Get the initial row height. This is either the height of the
20111 text hscrolled, if there is any, or zero. */
20112 row->ascent = it->max_ascent;
20113 row->height = it->max_ascent + it->max_descent;
20114 row->phys_ascent = it->max_phys_ascent;
20115 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20116 row->extra_line_spacing = it->max_extra_line_spacing;
20118 /* Utility macro to record max and min buffer positions seen until now. */
20119 #define RECORD_MAX_MIN_POS(IT) \
20120 do \
20122 int composition_p = !STRINGP ((IT)->string) \
20123 && ((IT)->what == IT_COMPOSITION); \
20124 ptrdiff_t current_pos = \
20125 composition_p ? (IT)->cmp_it.charpos \
20126 : IT_CHARPOS (*(IT)); \
20127 ptrdiff_t current_bpos = \
20128 composition_p ? CHAR_TO_BYTE (current_pos) \
20129 : IT_BYTEPOS (*(IT)); \
20130 if (current_pos < min_pos) \
20132 min_pos = current_pos; \
20133 min_bpos = current_bpos; \
20135 if (IT_CHARPOS (*it) > max_pos) \
20137 max_pos = IT_CHARPOS (*it); \
20138 max_bpos = IT_BYTEPOS (*it); \
20141 while (0)
20143 /* Loop generating characters. The loop is left with IT on the next
20144 character to display. */
20145 while (1)
20147 int n_glyphs_before, hpos_before, x_before;
20148 int x, nglyphs;
20149 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20151 /* Retrieve the next thing to display. Value is zero if end of
20152 buffer reached. */
20153 if (!get_next_display_element (it))
20155 /* Maybe add a space at the end of this line that is used to
20156 display the cursor there under X. Set the charpos of the
20157 first glyph of blank lines not corresponding to any text
20158 to -1. */
20159 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20160 row->exact_window_width_line_p = 1;
20161 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20162 || row->used[TEXT_AREA] == 0)
20164 row->glyphs[TEXT_AREA]->charpos = -1;
20165 row->displays_text_p = 0;
20167 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20168 && (!MINI_WINDOW_P (it->w)
20169 || (minibuf_level && EQ (it->window, minibuf_window))))
20170 row->indicate_empty_line_p = 1;
20173 it->continuation_lines_width = 0;
20174 row->ends_at_zv_p = 1;
20175 /* A row that displays right-to-left text must always have
20176 its last face extended all the way to the end of line,
20177 even if this row ends in ZV, because we still write to
20178 the screen left to right. We also need to extend the
20179 last face if the default face is remapped to some
20180 different face, otherwise the functions that clear
20181 portions of the screen will clear with the default face's
20182 background color. */
20183 if (row->reversed_p
20184 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20185 extend_face_to_end_of_line (it);
20186 break;
20189 /* Now, get the metrics of what we want to display. This also
20190 generates glyphs in `row' (which is IT->glyph_row). */
20191 n_glyphs_before = row->used[TEXT_AREA];
20192 x = it->current_x;
20194 /* Remember the line height so far in case the next element doesn't
20195 fit on the line. */
20196 if (it->line_wrap != TRUNCATE)
20198 ascent = it->max_ascent;
20199 descent = it->max_descent;
20200 phys_ascent = it->max_phys_ascent;
20201 phys_descent = it->max_phys_descent;
20203 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20205 if (IT_DISPLAYING_WHITESPACE (it))
20206 may_wrap = 1;
20207 else if (may_wrap)
20209 SAVE_IT (wrap_it, *it, wrap_data);
20210 wrap_x = x;
20211 wrap_row_used = row->used[TEXT_AREA];
20212 wrap_row_ascent = row->ascent;
20213 wrap_row_height = row->height;
20214 wrap_row_phys_ascent = row->phys_ascent;
20215 wrap_row_phys_height = row->phys_height;
20216 wrap_row_extra_line_spacing = row->extra_line_spacing;
20217 wrap_row_min_pos = min_pos;
20218 wrap_row_min_bpos = min_bpos;
20219 wrap_row_max_pos = max_pos;
20220 wrap_row_max_bpos = max_bpos;
20221 may_wrap = 0;
20226 PRODUCE_GLYPHS (it);
20228 /* If this display element was in marginal areas, continue with
20229 the next one. */
20230 if (it->area != TEXT_AREA)
20232 row->ascent = max (row->ascent, it->max_ascent);
20233 row->height = max (row->height, it->max_ascent + it->max_descent);
20234 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20235 row->phys_height = max (row->phys_height,
20236 it->max_phys_ascent + it->max_phys_descent);
20237 row->extra_line_spacing = max (row->extra_line_spacing,
20238 it->max_extra_line_spacing);
20239 set_iterator_to_next (it, 1);
20240 /* If we didn't handle the line/wrap prefix above, and the
20241 call to set_iterator_to_next just switched to TEXT_AREA,
20242 process the prefix now. */
20243 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20245 pending_handle_line_prefix = false;
20246 handle_line_prefix (it);
20248 continue;
20251 /* Does the display element fit on the line? If we truncate
20252 lines, we should draw past the right edge of the window. If
20253 we don't truncate, we want to stop so that we can display the
20254 continuation glyph before the right margin. If lines are
20255 continued, there are two possible strategies for characters
20256 resulting in more than 1 glyph (e.g. tabs): Display as many
20257 glyphs as possible in this line and leave the rest for the
20258 continuation line, or display the whole element in the next
20259 line. Original redisplay did the former, so we do it also. */
20260 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20261 hpos_before = it->hpos;
20262 x_before = x;
20264 if (/* Not a newline. */
20265 nglyphs > 0
20266 /* Glyphs produced fit entirely in the line. */
20267 && it->current_x < it->last_visible_x)
20269 it->hpos += nglyphs;
20270 row->ascent = max (row->ascent, it->max_ascent);
20271 row->height = max (row->height, it->max_ascent + it->max_descent);
20272 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20273 row->phys_height = max (row->phys_height,
20274 it->max_phys_ascent + it->max_phys_descent);
20275 row->extra_line_spacing = max (row->extra_line_spacing,
20276 it->max_extra_line_spacing);
20277 if (it->current_x - it->pixel_width < it->first_visible_x
20278 /* In R2L rows, we arrange in extend_face_to_end_of_line
20279 to add a right offset to the line, by a suitable
20280 change to the stretch glyph that is the leftmost
20281 glyph of the line. */
20282 && !row->reversed_p)
20283 row->x = x - it->first_visible_x;
20284 /* Record the maximum and minimum buffer positions seen so
20285 far in glyphs that will be displayed by this row. */
20286 if (it->bidi_p)
20287 RECORD_MAX_MIN_POS (it);
20289 else
20291 int i, new_x;
20292 struct glyph *glyph;
20294 for (i = 0; i < nglyphs; ++i, x = new_x)
20296 /* Identify the glyphs added by the last call to
20297 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20298 the previous glyphs. */
20299 if (!row->reversed_p)
20300 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20301 else
20302 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20303 new_x = x + glyph->pixel_width;
20305 if (/* Lines are continued. */
20306 it->line_wrap != TRUNCATE
20307 && (/* Glyph doesn't fit on the line. */
20308 new_x > it->last_visible_x
20309 /* Or it fits exactly on a window system frame. */
20310 || (new_x == it->last_visible_x
20311 && FRAME_WINDOW_P (it->f)
20312 && (row->reversed_p
20313 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20314 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20316 /* End of a continued line. */
20318 if (it->hpos == 0
20319 || (new_x == it->last_visible_x
20320 && FRAME_WINDOW_P (it->f)
20321 && (row->reversed_p
20322 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20323 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20325 /* Current glyph is the only one on the line or
20326 fits exactly on the line. We must continue
20327 the line because we can't draw the cursor
20328 after the glyph. */
20329 row->continued_p = 1;
20330 it->current_x = new_x;
20331 it->continuation_lines_width += new_x;
20332 ++it->hpos;
20333 if (i == nglyphs - 1)
20335 /* If line-wrap is on, check if a previous
20336 wrap point was found. */
20337 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20338 && wrap_row_used > 0
20339 /* Even if there is a previous wrap
20340 point, continue the line here as
20341 usual, if (i) the previous character
20342 was a space or tab AND (ii) the
20343 current character is not. */
20344 && (!may_wrap
20345 || IT_DISPLAYING_WHITESPACE (it)))
20346 goto back_to_wrap;
20348 /* Record the maximum and minimum buffer
20349 positions seen so far in glyphs that will be
20350 displayed by this row. */
20351 if (it->bidi_p)
20352 RECORD_MAX_MIN_POS (it);
20353 set_iterator_to_next (it, 1);
20354 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20356 if (!get_next_display_element (it))
20358 row->exact_window_width_line_p = 1;
20359 it->continuation_lines_width = 0;
20360 row->continued_p = 0;
20361 row->ends_at_zv_p = 1;
20363 else if (ITERATOR_AT_END_OF_LINE_P (it))
20365 row->continued_p = 0;
20366 row->exact_window_width_line_p = 1;
20368 /* If line-wrap is on, check if a
20369 previous wrap point was found. */
20370 else if (wrap_row_used > 0
20371 /* Even if there is a previous wrap
20372 point, continue the line here as
20373 usual, if (i) the previous character
20374 was a space or tab AND (ii) the
20375 current character is not. */
20376 && (!may_wrap
20377 || IT_DISPLAYING_WHITESPACE (it)))
20378 goto back_to_wrap;
20382 else if (it->bidi_p)
20383 RECORD_MAX_MIN_POS (it);
20384 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20385 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20386 extend_face_to_end_of_line (it);
20388 else if (CHAR_GLYPH_PADDING_P (*glyph)
20389 && !FRAME_WINDOW_P (it->f))
20391 /* A padding glyph that doesn't fit on this line.
20392 This means the whole character doesn't fit
20393 on the line. */
20394 if (row->reversed_p)
20395 unproduce_glyphs (it, row->used[TEXT_AREA]
20396 - n_glyphs_before);
20397 row->used[TEXT_AREA] = n_glyphs_before;
20399 /* Fill the rest of the row with continuation
20400 glyphs like in 20.x. */
20401 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20402 < row->glyphs[1 + TEXT_AREA])
20403 produce_special_glyphs (it, IT_CONTINUATION);
20405 row->continued_p = 1;
20406 it->current_x = x_before;
20407 it->continuation_lines_width += x_before;
20409 /* Restore the height to what it was before the
20410 element not fitting on the line. */
20411 it->max_ascent = ascent;
20412 it->max_descent = descent;
20413 it->max_phys_ascent = phys_ascent;
20414 it->max_phys_descent = phys_descent;
20415 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20416 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20417 extend_face_to_end_of_line (it);
20419 else if (wrap_row_used > 0)
20421 back_to_wrap:
20422 if (row->reversed_p)
20423 unproduce_glyphs (it,
20424 row->used[TEXT_AREA] - wrap_row_used);
20425 RESTORE_IT (it, &wrap_it, wrap_data);
20426 it->continuation_lines_width += wrap_x;
20427 row->used[TEXT_AREA] = wrap_row_used;
20428 row->ascent = wrap_row_ascent;
20429 row->height = wrap_row_height;
20430 row->phys_ascent = wrap_row_phys_ascent;
20431 row->phys_height = wrap_row_phys_height;
20432 row->extra_line_spacing = wrap_row_extra_line_spacing;
20433 min_pos = wrap_row_min_pos;
20434 min_bpos = wrap_row_min_bpos;
20435 max_pos = wrap_row_max_pos;
20436 max_bpos = wrap_row_max_bpos;
20437 row->continued_p = 1;
20438 row->ends_at_zv_p = 0;
20439 row->exact_window_width_line_p = 0;
20440 it->continuation_lines_width += x;
20442 /* Make sure that a non-default face is extended
20443 up to the right margin of the window. */
20444 extend_face_to_end_of_line (it);
20446 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20448 /* A TAB that extends past the right edge of the
20449 window. This produces a single glyph on
20450 window system frames. We leave the glyph in
20451 this row and let it fill the row, but don't
20452 consume the TAB. */
20453 if ((row->reversed_p
20454 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20455 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20456 produce_special_glyphs (it, IT_CONTINUATION);
20457 it->continuation_lines_width += it->last_visible_x;
20458 row->ends_in_middle_of_char_p = 1;
20459 row->continued_p = 1;
20460 glyph->pixel_width = it->last_visible_x - x;
20461 it->starts_in_middle_of_char_p = 1;
20462 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20463 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20464 extend_face_to_end_of_line (it);
20466 else
20468 /* Something other than a TAB that draws past
20469 the right edge of the window. Restore
20470 positions to values before the element. */
20471 if (row->reversed_p)
20472 unproduce_glyphs (it, row->used[TEXT_AREA]
20473 - (n_glyphs_before + i));
20474 row->used[TEXT_AREA] = n_glyphs_before + i;
20476 /* Display continuation glyphs. */
20477 it->current_x = x_before;
20478 it->continuation_lines_width += x;
20479 if (!FRAME_WINDOW_P (it->f)
20480 || (row->reversed_p
20481 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20482 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20483 produce_special_glyphs (it, IT_CONTINUATION);
20484 row->continued_p = 1;
20486 extend_face_to_end_of_line (it);
20488 if (nglyphs > 1 && i > 0)
20490 row->ends_in_middle_of_char_p = 1;
20491 it->starts_in_middle_of_char_p = 1;
20494 /* Restore the height to what it was before the
20495 element not fitting on the line. */
20496 it->max_ascent = ascent;
20497 it->max_descent = descent;
20498 it->max_phys_ascent = phys_ascent;
20499 it->max_phys_descent = phys_descent;
20502 break;
20504 else if (new_x > it->first_visible_x)
20506 /* Increment number of glyphs actually displayed. */
20507 ++it->hpos;
20509 /* Record the maximum and minimum buffer positions
20510 seen so far in glyphs that will be displayed by
20511 this row. */
20512 if (it->bidi_p)
20513 RECORD_MAX_MIN_POS (it);
20515 if (x < it->first_visible_x && !row->reversed_p)
20516 /* Glyph is partially visible, i.e. row starts at
20517 negative X position. Don't do that in R2L
20518 rows, where we arrange to add a right offset to
20519 the line in extend_face_to_end_of_line, by a
20520 suitable change to the stretch glyph that is
20521 the leftmost glyph of the line. */
20522 row->x = x - it->first_visible_x;
20523 /* When the last glyph of an R2L row only fits
20524 partially on the line, we need to set row->x to a
20525 negative offset, so that the leftmost glyph is
20526 the one that is partially visible. But if we are
20527 going to produce the truncation glyph, this will
20528 be taken care of in produce_special_glyphs. */
20529 if (row->reversed_p
20530 && new_x > it->last_visible_x
20531 && !(it->line_wrap == TRUNCATE
20532 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20534 eassert (FRAME_WINDOW_P (it->f));
20535 row->x = it->last_visible_x - new_x;
20538 else
20540 /* Glyph is completely off the left margin of the
20541 window. This should not happen because of the
20542 move_it_in_display_line at the start of this
20543 function, unless the text display area of the
20544 window is empty. */
20545 eassert (it->first_visible_x <= it->last_visible_x);
20548 /* Even if this display element produced no glyphs at all,
20549 we want to record its position. */
20550 if (it->bidi_p && nglyphs == 0)
20551 RECORD_MAX_MIN_POS (it);
20553 row->ascent = max (row->ascent, it->max_ascent);
20554 row->height = max (row->height, it->max_ascent + it->max_descent);
20555 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20556 row->phys_height = max (row->phys_height,
20557 it->max_phys_ascent + it->max_phys_descent);
20558 row->extra_line_spacing = max (row->extra_line_spacing,
20559 it->max_extra_line_spacing);
20561 /* End of this display line if row is continued. */
20562 if (row->continued_p || row->ends_at_zv_p)
20563 break;
20566 at_end_of_line:
20567 /* Is this a line end? If yes, we're also done, after making
20568 sure that a non-default face is extended up to the right
20569 margin of the window. */
20570 if (ITERATOR_AT_END_OF_LINE_P (it))
20572 int used_before = row->used[TEXT_AREA];
20574 row->ends_in_newline_from_string_p = STRINGP (it->object);
20576 /* Add a space at the end of the line that is used to
20577 display the cursor there. */
20578 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20579 append_space_for_newline (it, 0);
20581 /* Extend the face to the end of the line. */
20582 extend_face_to_end_of_line (it);
20584 /* Make sure we have the position. */
20585 if (used_before == 0)
20586 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20588 /* Record the position of the newline, for use in
20589 find_row_edges. */
20590 it->eol_pos = it->current.pos;
20592 /* Consume the line end. This skips over invisible lines. */
20593 set_iterator_to_next (it, 1);
20594 it->continuation_lines_width = 0;
20595 break;
20598 /* Proceed with next display element. Note that this skips
20599 over lines invisible because of selective display. */
20600 set_iterator_to_next (it, 1);
20602 /* If we truncate lines, we are done when the last displayed
20603 glyphs reach past the right margin of the window. */
20604 if (it->line_wrap == TRUNCATE
20605 && ((FRAME_WINDOW_P (it->f)
20606 /* Images are preprocessed in produce_image_glyph such
20607 that they are cropped at the right edge of the
20608 window, so an image glyph will always end exactly at
20609 last_visible_x, even if there's no right fringe. */
20610 && ((row->reversed_p
20611 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20612 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20613 || it->what == IT_IMAGE))
20614 ? (it->current_x >= it->last_visible_x)
20615 : (it->current_x > it->last_visible_x)))
20617 /* Maybe add truncation glyphs. */
20618 if (!FRAME_WINDOW_P (it->f)
20619 || (row->reversed_p
20620 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20621 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20623 int i, n;
20625 if (!row->reversed_p)
20627 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20628 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20629 break;
20631 else
20633 for (i = 0; i < row->used[TEXT_AREA]; i++)
20634 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20635 break;
20636 /* Remove any padding glyphs at the front of ROW, to
20637 make room for the truncation glyphs we will be
20638 adding below. The loop below always inserts at
20639 least one truncation glyph, so also remove the
20640 last glyph added to ROW. */
20641 unproduce_glyphs (it, i + 1);
20642 /* Adjust i for the loop below. */
20643 i = row->used[TEXT_AREA] - (i + 1);
20646 /* produce_special_glyphs overwrites the last glyph, so
20647 we don't want that if we want to keep that last
20648 glyph, which means it's an image. */
20649 if (it->current_x > it->last_visible_x)
20651 it->current_x = x_before;
20652 if (!FRAME_WINDOW_P (it->f))
20654 for (n = row->used[TEXT_AREA]; i < n; ++i)
20656 row->used[TEXT_AREA] = i;
20657 produce_special_glyphs (it, IT_TRUNCATION);
20660 else
20662 row->used[TEXT_AREA] = i;
20663 produce_special_glyphs (it, IT_TRUNCATION);
20665 it->hpos = hpos_before;
20668 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20670 /* Don't truncate if we can overflow newline into fringe. */
20671 if (!get_next_display_element (it))
20673 it->continuation_lines_width = 0;
20674 row->ends_at_zv_p = 1;
20675 row->exact_window_width_line_p = 1;
20676 break;
20678 if (ITERATOR_AT_END_OF_LINE_P (it))
20680 row->exact_window_width_line_p = 1;
20681 goto at_end_of_line;
20683 it->current_x = x_before;
20684 it->hpos = hpos_before;
20687 row->truncated_on_right_p = 1;
20688 it->continuation_lines_width = 0;
20689 reseat_at_next_visible_line_start (it, 0);
20690 /* We insist below that IT's position be at ZV because in
20691 bidi-reordered lines the character at visible line start
20692 might not be the character that follows the newline in
20693 the logical order. */
20694 if (IT_BYTEPOS (*it) > BEG_BYTE)
20695 row->ends_at_zv_p =
20696 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20697 else
20698 row->ends_at_zv_p = false;
20699 break;
20703 if (wrap_data)
20704 bidi_unshelve_cache (wrap_data, 1);
20706 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20707 at the left window margin. */
20708 if (it->first_visible_x
20709 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20711 if (!FRAME_WINDOW_P (it->f)
20712 || (((row->reversed_p
20713 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20714 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20715 /* Don't let insert_left_trunc_glyphs overwrite the
20716 first glyph of the row if it is an image. */
20717 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20718 insert_left_trunc_glyphs (it);
20719 row->truncated_on_left_p = 1;
20722 /* Remember the position at which this line ends.
20724 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20725 cannot be before the call to find_row_edges below, since that is
20726 where these positions are determined. */
20727 row->end = it->current;
20728 if (!it->bidi_p)
20730 row->minpos = row->start.pos;
20731 row->maxpos = row->end.pos;
20733 else
20735 /* ROW->minpos and ROW->maxpos must be the smallest and
20736 `1 + the largest' buffer positions in ROW. But if ROW was
20737 bidi-reordered, these two positions can be anywhere in the
20738 row, so we must determine them now. */
20739 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20742 /* If the start of this line is the overlay arrow-position, then
20743 mark this glyph row as the one containing the overlay arrow.
20744 This is clearly a mess with variable size fonts. It would be
20745 better to let it be displayed like cursors under X. */
20746 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20747 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20748 !NILP (overlay_arrow_string)))
20750 /* Overlay arrow in window redisplay is a fringe bitmap. */
20751 if (STRINGP (overlay_arrow_string))
20753 struct glyph_row *arrow_row
20754 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20755 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20756 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20757 struct glyph *p = row->glyphs[TEXT_AREA];
20758 struct glyph *p2, *end;
20760 /* Copy the arrow glyphs. */
20761 while (glyph < arrow_end)
20762 *p++ = *glyph++;
20764 /* Throw away padding glyphs. */
20765 p2 = p;
20766 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20767 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20768 ++p2;
20769 if (p2 > p)
20771 while (p2 < end)
20772 *p++ = *p2++;
20773 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20776 else
20778 eassert (INTEGERP (overlay_arrow_string));
20779 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20781 overlay_arrow_seen = 1;
20784 /* Highlight trailing whitespace. */
20785 if (!NILP (Vshow_trailing_whitespace))
20786 highlight_trailing_whitespace (it->f, it->glyph_row);
20788 /* Compute pixel dimensions of this line. */
20789 compute_line_metrics (it);
20791 /* Implementation note: No changes in the glyphs of ROW or in their
20792 faces can be done past this point, because compute_line_metrics
20793 computes ROW's hash value and stores it within the glyph_row
20794 structure. */
20796 /* Record whether this row ends inside an ellipsis. */
20797 row->ends_in_ellipsis_p
20798 = (it->method == GET_FROM_DISPLAY_VECTOR
20799 && it->ellipsis_p);
20801 /* Save fringe bitmaps in this row. */
20802 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20803 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20804 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20805 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20807 it->left_user_fringe_bitmap = 0;
20808 it->left_user_fringe_face_id = 0;
20809 it->right_user_fringe_bitmap = 0;
20810 it->right_user_fringe_face_id = 0;
20812 /* Maybe set the cursor. */
20813 cvpos = it->w->cursor.vpos;
20814 if ((cvpos < 0
20815 /* In bidi-reordered rows, keep checking for proper cursor
20816 position even if one has been found already, because buffer
20817 positions in such rows change non-linearly with ROW->VPOS,
20818 when a line is continued. One exception: when we are at ZV,
20819 display cursor on the first suitable glyph row, since all
20820 the empty rows after that also have their position set to ZV. */
20821 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20822 lines' rows is implemented for bidi-reordered rows. */
20823 || (it->bidi_p
20824 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20825 && PT >= MATRIX_ROW_START_CHARPOS (row)
20826 && PT <= MATRIX_ROW_END_CHARPOS (row)
20827 && cursor_row_p (row))
20828 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20830 /* Prepare for the next line. This line starts horizontally at (X
20831 HPOS) = (0 0). Vertical positions are incremented. As a
20832 convenience for the caller, IT->glyph_row is set to the next
20833 row to be used. */
20834 it->current_x = it->hpos = 0;
20835 it->current_y += row->height;
20836 SET_TEXT_POS (it->eol_pos, 0, 0);
20837 ++it->vpos;
20838 ++it->glyph_row;
20839 /* The next row should by default use the same value of the
20840 reversed_p flag as this one. set_iterator_to_next decides when
20841 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20842 the flag accordingly. */
20843 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20844 it->glyph_row->reversed_p = row->reversed_p;
20845 it->start = row->end;
20846 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20848 #undef RECORD_MAX_MIN_POS
20851 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20852 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20853 doc: /* Return paragraph direction at point in BUFFER.
20854 Value is either `left-to-right' or `right-to-left'.
20855 If BUFFER is omitted or nil, it defaults to the current buffer.
20857 Paragraph direction determines how the text in the paragraph is displayed.
20858 In left-to-right paragraphs, text begins at the left margin of the window
20859 and the reading direction is generally left to right. In right-to-left
20860 paragraphs, text begins at the right margin and is read from right to left.
20862 See also `bidi-paragraph-direction'. */)
20863 (Lisp_Object buffer)
20865 struct buffer *buf = current_buffer;
20866 struct buffer *old = buf;
20868 if (! NILP (buffer))
20870 CHECK_BUFFER (buffer);
20871 buf = XBUFFER (buffer);
20874 if (NILP (BVAR (buf, bidi_display_reordering))
20875 || NILP (BVAR (buf, enable_multibyte_characters))
20876 /* When we are loading loadup.el, the character property tables
20877 needed for bidi iteration are not yet available. */
20878 || !NILP (Vpurify_flag))
20879 return Qleft_to_right;
20880 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20881 return BVAR (buf, bidi_paragraph_direction);
20882 else
20884 /* Determine the direction from buffer text. We could try to
20885 use current_matrix if it is up to date, but this seems fast
20886 enough as it is. */
20887 struct bidi_it itb;
20888 ptrdiff_t pos = BUF_PT (buf);
20889 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20890 int c;
20891 void *itb_data = bidi_shelve_cache ();
20893 set_buffer_temp (buf);
20894 /* bidi_paragraph_init finds the base direction of the paragraph
20895 by searching forward from paragraph start. We need the base
20896 direction of the current or _previous_ paragraph, so we need
20897 to make sure we are within that paragraph. To that end, find
20898 the previous non-empty line. */
20899 if (pos >= ZV && pos > BEGV)
20900 DEC_BOTH (pos, bytepos);
20901 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20902 if (fast_looking_at (trailing_white_space,
20903 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20905 while ((c = FETCH_BYTE (bytepos)) == '\n'
20906 || c == ' ' || c == '\t' || c == '\f')
20908 if (bytepos <= BEGV_BYTE)
20909 break;
20910 bytepos--;
20911 pos--;
20913 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20914 bytepos--;
20916 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20917 itb.paragraph_dir = NEUTRAL_DIR;
20918 itb.string.s = NULL;
20919 itb.string.lstring = Qnil;
20920 itb.string.bufpos = 0;
20921 itb.string.from_disp_str = 0;
20922 itb.string.unibyte = 0;
20923 /* We have no window to use here for ignoring window-specific
20924 overlays. Using NULL for window pointer will cause
20925 compute_display_string_pos to use the current buffer. */
20926 itb.w = NULL;
20927 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20928 bidi_unshelve_cache (itb_data, 0);
20929 set_buffer_temp (old);
20930 switch (itb.paragraph_dir)
20932 case L2R:
20933 return Qleft_to_right;
20934 break;
20935 case R2L:
20936 return Qright_to_left;
20937 break;
20938 default:
20939 emacs_abort ();
20944 DEFUN ("bidi-find-overridden-directionality",
20945 Fbidi_find_overridden_directionality,
20946 Sbidi_find_overridden_directionality, 2, 3, 0,
20947 doc: /* Return position between FROM and TO where directionality was overridden.
20949 This function returns the first character position in the specified
20950 region of OBJECT where there is a character whose `bidi-class' property
20951 is `L', but which was forced to display as `R' by a directional
20952 override, and likewise with characters whose `bidi-class' is `R'
20953 or `AL' that were forced to display as `L'.
20955 If no such character is found, the function returns nil.
20957 OBJECT is a Lisp string or buffer to search for overridden
20958 directionality, and defaults to the current buffer if nil or omitted.
20959 OBJECT can also be a window, in which case the function will search
20960 the buffer displayed in that window. Passing the window instead of
20961 a buffer is preferable when the buffer is displayed in some window,
20962 because this function will then be able to correctly account for
20963 window-specific overlays, which can affect the results.
20965 Strong directional characters `L', `R', and `AL' can have their
20966 intrinsic directionality overridden by directional override
20967 control characters RLO \(u+202e) and LRO \(u+202d). See the
20968 function `get-char-code-property' for a way to inquire about
20969 the `bidi-class' property of a character. */)
20970 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
20972 struct buffer *buf = current_buffer;
20973 struct buffer *old = buf;
20974 struct window *w = NULL;
20975 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
20976 struct bidi_it itb;
20977 ptrdiff_t from_pos, to_pos, from_bpos;
20978 void *itb_data;
20980 if (!NILP (object))
20982 if (BUFFERP (object))
20983 buf = XBUFFER (object);
20984 else if (WINDOWP (object))
20986 w = decode_live_window (object);
20987 buf = XBUFFER (w->contents);
20988 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
20990 else
20991 CHECK_STRING (object);
20994 if (STRINGP (object))
20996 /* Characters in unibyte strings are always treated by bidi.c as
20997 strong LTR. */
20998 if (!STRING_MULTIBYTE (object)
20999 /* When we are loading loadup.el, the character property
21000 tables needed for bidi iteration are not yet
21001 available. */
21002 || !NILP (Vpurify_flag))
21003 return Qnil;
21005 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21006 if (from_pos >= SCHARS (object))
21007 return Qnil;
21009 /* Set up the bidi iterator. */
21010 itb_data = bidi_shelve_cache ();
21011 itb.paragraph_dir = NEUTRAL_DIR;
21012 itb.string.lstring = object;
21013 itb.string.s = NULL;
21014 itb.string.schars = SCHARS (object);
21015 itb.string.bufpos = 0;
21016 itb.string.from_disp_str = 0;
21017 itb.string.unibyte = 0;
21018 itb.w = w;
21019 bidi_init_it (0, 0, frame_window_p, &itb);
21021 else
21023 /* Nothing this fancy can happen in unibyte buffers, or in a
21024 buffer that disabled reordering, or if FROM is at EOB. */
21025 if (NILP (BVAR (buf, bidi_display_reordering))
21026 || NILP (BVAR (buf, enable_multibyte_characters))
21027 /* When we are loading loadup.el, the character property
21028 tables needed for bidi iteration are not yet
21029 available. */
21030 || !NILP (Vpurify_flag))
21031 return Qnil;
21033 set_buffer_temp (buf);
21034 validate_region (&from, &to);
21035 from_pos = XINT (from);
21036 to_pos = XINT (to);
21037 if (from_pos >= ZV)
21038 return Qnil;
21040 /* Set up the bidi iterator. */
21041 itb_data = bidi_shelve_cache ();
21042 from_bpos = CHAR_TO_BYTE (from_pos);
21043 if (from_pos == BEGV)
21045 itb.charpos = BEGV;
21046 itb.bytepos = BEGV_BYTE;
21048 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21050 itb.charpos = from_pos;
21051 itb.bytepos = from_bpos;
21053 else
21054 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21055 -1, &itb.bytepos);
21056 itb.paragraph_dir = NEUTRAL_DIR;
21057 itb.string.s = NULL;
21058 itb.string.lstring = Qnil;
21059 itb.string.bufpos = 0;
21060 itb.string.from_disp_str = 0;
21061 itb.string.unibyte = 0;
21062 itb.w = w;
21063 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21066 ptrdiff_t found;
21067 do {
21068 /* For the purposes of this function, the actual base direction of
21069 the paragraph doesn't matter, so just set it to L2R. */
21070 bidi_paragraph_init (L2R, &itb, 0);
21071 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21073 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21075 bidi_unshelve_cache (itb_data, 0);
21076 set_buffer_temp (old);
21078 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21081 DEFUN ("move-point-visually", Fmove_point_visually,
21082 Smove_point_visually, 1, 1, 0,
21083 doc: /* Move point in the visual order in the specified DIRECTION.
21084 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21085 left.
21087 Value is the new character position of point. */)
21088 (Lisp_Object direction)
21090 struct window *w = XWINDOW (selected_window);
21091 struct buffer *b = XBUFFER (w->contents);
21092 struct glyph_row *row;
21093 int dir;
21094 Lisp_Object paragraph_dir;
21096 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21097 (!(ROW)->continued_p \
21098 && NILP ((GLYPH)->object) \
21099 && (GLYPH)->type == CHAR_GLYPH \
21100 && (GLYPH)->u.ch == ' ' \
21101 && (GLYPH)->charpos >= 0 \
21102 && !(GLYPH)->avoid_cursor_p)
21104 CHECK_NUMBER (direction);
21105 dir = XINT (direction);
21106 if (dir > 0)
21107 dir = 1;
21108 else
21109 dir = -1;
21111 /* If current matrix is up-to-date, we can use the information
21112 recorded in the glyphs, at least as long as the goal is on the
21113 screen. */
21114 if (w->window_end_valid
21115 && !windows_or_buffers_changed
21116 && b
21117 && !b->clip_changed
21118 && !b->prevent_redisplay_optimizations_p
21119 && !window_outdated (w)
21120 /* We rely below on the cursor coordinates to be up to date, but
21121 we cannot trust them if some command moved point since the
21122 last complete redisplay. */
21123 && w->last_point == BUF_PT (b)
21124 && w->cursor.vpos >= 0
21125 && w->cursor.vpos < w->current_matrix->nrows
21126 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21128 struct glyph *g = row->glyphs[TEXT_AREA];
21129 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21130 struct glyph *gpt = g + w->cursor.hpos;
21132 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21134 if (BUFFERP (g->object) && g->charpos != PT)
21136 SET_PT (g->charpos);
21137 w->cursor.vpos = -1;
21138 return make_number (PT);
21140 else if (!NILP (g->object) && !EQ (g->object, gpt->object))
21142 ptrdiff_t new_pos;
21144 if (BUFFERP (gpt->object))
21146 new_pos = PT;
21147 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21148 new_pos += (row->reversed_p ? -dir : dir);
21149 else
21150 new_pos -= (row->reversed_p ? -dir : dir);
21152 else if (BUFFERP (g->object))
21153 new_pos = g->charpos;
21154 else
21155 break;
21156 SET_PT (new_pos);
21157 w->cursor.vpos = -1;
21158 return make_number (PT);
21160 else if (ROW_GLYPH_NEWLINE_P (row, g))
21162 /* Glyphs inserted at the end of a non-empty line for
21163 positioning the cursor have zero charpos, so we must
21164 deduce the value of point by other means. */
21165 if (g->charpos > 0)
21166 SET_PT (g->charpos);
21167 else if (row->ends_at_zv_p && PT != ZV)
21168 SET_PT (ZV);
21169 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21170 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21171 else
21172 break;
21173 w->cursor.vpos = -1;
21174 return make_number (PT);
21177 if (g == e || NILP (g->object))
21179 if (row->truncated_on_left_p || row->truncated_on_right_p)
21180 goto simulate_display;
21181 if (!row->reversed_p)
21182 row += dir;
21183 else
21184 row -= dir;
21185 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21186 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21187 goto simulate_display;
21189 if (dir > 0)
21191 if (row->reversed_p && !row->continued_p)
21193 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21194 w->cursor.vpos = -1;
21195 return make_number (PT);
21197 g = row->glyphs[TEXT_AREA];
21198 e = g + row->used[TEXT_AREA];
21199 for ( ; g < e; g++)
21201 if (BUFFERP (g->object)
21202 /* Empty lines have only one glyph, which stands
21203 for the newline, and whose charpos is the
21204 buffer position of the newline. */
21205 || ROW_GLYPH_NEWLINE_P (row, g)
21206 /* When the buffer ends in a newline, the line at
21207 EOB also has one glyph, but its charpos is -1. */
21208 || (row->ends_at_zv_p
21209 && !row->reversed_p
21210 && NILP (g->object)
21211 && g->type == CHAR_GLYPH
21212 && g->u.ch == ' '))
21214 if (g->charpos > 0)
21215 SET_PT (g->charpos);
21216 else if (!row->reversed_p
21217 && row->ends_at_zv_p
21218 && PT != ZV)
21219 SET_PT (ZV);
21220 else
21221 continue;
21222 w->cursor.vpos = -1;
21223 return make_number (PT);
21227 else
21229 if (!row->reversed_p && !row->continued_p)
21231 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21232 w->cursor.vpos = -1;
21233 return make_number (PT);
21235 e = row->glyphs[TEXT_AREA];
21236 g = e + row->used[TEXT_AREA] - 1;
21237 for ( ; g >= e; g--)
21239 if (BUFFERP (g->object)
21240 || (ROW_GLYPH_NEWLINE_P (row, g)
21241 && g->charpos > 0)
21242 /* Empty R2L lines on GUI frames have the buffer
21243 position of the newline stored in the stretch
21244 glyph. */
21245 || g->type == STRETCH_GLYPH
21246 || (row->ends_at_zv_p
21247 && row->reversed_p
21248 && NILP (g->object)
21249 && g->type == CHAR_GLYPH
21250 && g->u.ch == ' '))
21252 if (g->charpos > 0)
21253 SET_PT (g->charpos);
21254 else if (row->reversed_p
21255 && row->ends_at_zv_p
21256 && PT != ZV)
21257 SET_PT (ZV);
21258 else
21259 continue;
21260 w->cursor.vpos = -1;
21261 return make_number (PT);
21268 simulate_display:
21270 /* If we wind up here, we failed to move by using the glyphs, so we
21271 need to simulate display instead. */
21273 if (b)
21274 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21275 else
21276 paragraph_dir = Qleft_to_right;
21277 if (EQ (paragraph_dir, Qright_to_left))
21278 dir = -dir;
21279 if (PT <= BEGV && dir < 0)
21280 xsignal0 (Qbeginning_of_buffer);
21281 else if (PT >= ZV && dir > 0)
21282 xsignal0 (Qend_of_buffer);
21283 else
21285 struct text_pos pt;
21286 struct it it;
21287 int pt_x, target_x, pixel_width, pt_vpos;
21288 bool at_eol_p;
21289 bool overshoot_expected = false;
21290 bool target_is_eol_p = false;
21292 /* Setup the arena. */
21293 SET_TEXT_POS (pt, PT, PT_BYTE);
21294 start_display (&it, w, pt);
21296 if (it.cmp_it.id < 0
21297 && it.method == GET_FROM_STRING
21298 && it.area == TEXT_AREA
21299 && it.string_from_display_prop_p
21300 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21301 overshoot_expected = true;
21303 /* Find the X coordinate of point. We start from the beginning
21304 of this or previous line to make sure we are before point in
21305 the logical order (since the move_it_* functions can only
21306 move forward). */
21307 reseat:
21308 reseat_at_previous_visible_line_start (&it);
21309 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21310 if (IT_CHARPOS (it) != PT)
21312 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21313 -1, -1, -1, MOVE_TO_POS);
21314 /* If we missed point because the character there is
21315 displayed out of a display vector that has more than one
21316 glyph, retry expecting overshoot. */
21317 if (it.method == GET_FROM_DISPLAY_VECTOR
21318 && it.current.dpvec_index > 0
21319 && !overshoot_expected)
21321 overshoot_expected = true;
21322 goto reseat;
21324 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21325 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21327 pt_x = it.current_x;
21328 pt_vpos = it.vpos;
21329 if (dir > 0 || overshoot_expected)
21331 struct glyph_row *row = it.glyph_row;
21333 /* When point is at beginning of line, we don't have
21334 information about the glyph there loaded into struct
21335 it. Calling get_next_display_element fixes that. */
21336 if (pt_x == 0)
21337 get_next_display_element (&it);
21338 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21339 it.glyph_row = NULL;
21340 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21341 it.glyph_row = row;
21342 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21343 it, lest it will become out of sync with it's buffer
21344 position. */
21345 it.current_x = pt_x;
21347 else
21348 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21349 pixel_width = it.pixel_width;
21350 if (overshoot_expected && at_eol_p)
21351 pixel_width = 0;
21352 else if (pixel_width <= 0)
21353 pixel_width = 1;
21355 /* If there's a display string (or something similar) at point,
21356 we are actually at the glyph to the left of point, so we need
21357 to correct the X coordinate. */
21358 if (overshoot_expected)
21360 if (it.bidi_p)
21361 pt_x += pixel_width * it.bidi_it.scan_dir;
21362 else
21363 pt_x += pixel_width;
21366 /* Compute target X coordinate, either to the left or to the
21367 right of point. On TTY frames, all characters have the same
21368 pixel width of 1, so we can use that. On GUI frames we don't
21369 have an easy way of getting at the pixel width of the
21370 character to the left of point, so we use a different method
21371 of getting to that place. */
21372 if (dir > 0)
21373 target_x = pt_x + pixel_width;
21374 else
21375 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21377 /* Target X coordinate could be one line above or below the line
21378 of point, in which case we need to adjust the target X
21379 coordinate. Also, if moving to the left, we need to begin at
21380 the left edge of the point's screen line. */
21381 if (dir < 0)
21383 if (pt_x > 0)
21385 start_display (&it, w, pt);
21386 reseat_at_previous_visible_line_start (&it);
21387 it.current_x = it.current_y = it.hpos = 0;
21388 if (pt_vpos != 0)
21389 move_it_by_lines (&it, pt_vpos);
21391 else
21393 move_it_by_lines (&it, -1);
21394 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21395 target_is_eol_p = true;
21396 /* Under word-wrap, we don't know the x coordinate of
21397 the last character displayed on the previous line,
21398 which immediately precedes the wrap point. To find
21399 out its x coordinate, we try moving to the right
21400 margin of the window, which will stop at the wrap
21401 point, and then reset target_x to point at the
21402 character that precedes the wrap point. This is not
21403 needed on GUI frames, because (see below) there we
21404 move from the left margin one grapheme cluster at a
21405 time, and stop when we hit the wrap point. */
21406 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21408 void *it_data = NULL;
21409 struct it it2;
21411 SAVE_IT (it2, it, it_data);
21412 move_it_in_display_line_to (&it, ZV, target_x,
21413 MOVE_TO_POS | MOVE_TO_X);
21414 /* If we arrived at target_x, that _is_ the last
21415 character on the previous line. */
21416 if (it.current_x != target_x)
21417 target_x = it.current_x - 1;
21418 RESTORE_IT (&it, &it2, it_data);
21422 else
21424 if (at_eol_p
21425 || (target_x >= it.last_visible_x
21426 && it.line_wrap != TRUNCATE))
21428 if (pt_x > 0)
21429 move_it_by_lines (&it, 0);
21430 move_it_by_lines (&it, 1);
21431 target_x = 0;
21435 /* Move to the target X coordinate. */
21436 #ifdef HAVE_WINDOW_SYSTEM
21437 /* On GUI frames, as we don't know the X coordinate of the
21438 character to the left of point, moving point to the left
21439 requires walking, one grapheme cluster at a time, until we
21440 find ourself at a place immediately to the left of the
21441 character at point. */
21442 if (FRAME_WINDOW_P (it.f) && dir < 0)
21444 struct text_pos new_pos;
21445 enum move_it_result rc = MOVE_X_REACHED;
21447 if (it.current_x == 0)
21448 get_next_display_element (&it);
21449 if (it.what == IT_COMPOSITION)
21451 new_pos.charpos = it.cmp_it.charpos;
21452 new_pos.bytepos = -1;
21454 else
21455 new_pos = it.current.pos;
21457 while (it.current_x + it.pixel_width <= target_x
21458 && (rc == MOVE_X_REACHED
21459 /* Under word-wrap, move_it_in_display_line_to
21460 stops at correct coordinates, but sometimes
21461 returns MOVE_POS_MATCH_OR_ZV. */
21462 || (it.line_wrap == WORD_WRAP
21463 && rc == MOVE_POS_MATCH_OR_ZV)))
21465 int new_x = it.current_x + it.pixel_width;
21467 /* For composed characters, we want the position of the
21468 first character in the grapheme cluster (usually, the
21469 composition's base character), whereas it.current
21470 might give us the position of the _last_ one, e.g. if
21471 the composition is rendered in reverse due to bidi
21472 reordering. */
21473 if (it.what == IT_COMPOSITION)
21475 new_pos.charpos = it.cmp_it.charpos;
21476 new_pos.bytepos = -1;
21478 else
21479 new_pos = it.current.pos;
21480 if (new_x == it.current_x)
21481 new_x++;
21482 rc = move_it_in_display_line_to (&it, ZV, new_x,
21483 MOVE_TO_POS | MOVE_TO_X);
21484 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21485 break;
21487 /* The previous position we saw in the loop is the one we
21488 want. */
21489 if (new_pos.bytepos == -1)
21490 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21491 it.current.pos = new_pos;
21493 else
21494 #endif
21495 if (it.current_x != target_x)
21496 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21498 /* When lines are truncated, the above loop will stop at the
21499 window edge. But we want to get to the end of line, even if
21500 it is beyond the window edge; automatic hscroll will then
21501 scroll the window to show point as appropriate. */
21502 if (target_is_eol_p && it.line_wrap == TRUNCATE
21503 && get_next_display_element (&it))
21505 struct text_pos new_pos = it.current.pos;
21507 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21509 set_iterator_to_next (&it, 0);
21510 if (it.method == GET_FROM_BUFFER)
21511 new_pos = it.current.pos;
21512 if (!get_next_display_element (&it))
21513 break;
21516 it.current.pos = new_pos;
21519 /* If we ended up in a display string that covers point, move to
21520 buffer position to the right in the visual order. */
21521 if (dir > 0)
21523 while (IT_CHARPOS (it) == PT)
21525 set_iterator_to_next (&it, 0);
21526 if (!get_next_display_element (&it))
21527 break;
21531 /* Move point to that position. */
21532 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21535 return make_number (PT);
21537 #undef ROW_GLYPH_NEWLINE_P
21540 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21541 Sbidi_resolved_levels, 0, 1, 0,
21542 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21544 The resolved levels are produced by the Emacs bidi reordering engine
21545 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21546 read the Unicode Standard Annex 9 (UAX#9) for background information
21547 about these levels.
21549 VPOS is the zero-based number of the current window's screen line
21550 for which to produce the resolved levels. If VPOS is nil or omitted,
21551 it defaults to the screen line of point. If the window displays a
21552 header line, VPOS of zero will report on the header line, and first
21553 line of text in the window will have VPOS of 1.
21555 Value is an array of resolved levels, indexed by glyph number.
21556 Glyphs are numbered from zero starting from the beginning of the
21557 screen line, i.e. the left edge of the window for left-to-right lines
21558 and from the right edge for right-to-left lines. The resolved levels
21559 are produced only for the window's text area; text in display margins
21560 is not included.
21562 If the selected window's display is not up-to-date, or if the specified
21563 screen line does not display text, this function returns nil. It is
21564 highly recommended to bind this function to some simple key, like F8,
21565 in order to avoid these problems.
21567 This function exists mainly for testing the correctness of the
21568 Emacs UBA implementation, in particular with the test suite. */)
21569 (Lisp_Object vpos)
21571 struct window *w = XWINDOW (selected_window);
21572 struct buffer *b = XBUFFER (w->contents);
21573 int nrow;
21574 struct glyph_row *row;
21576 if (NILP (vpos))
21578 int d1, d2, d3, d4, d5;
21580 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21582 else
21584 CHECK_NUMBER_COERCE_MARKER (vpos);
21585 nrow = XINT (vpos);
21588 /* We require up-to-date glyph matrix for this window. */
21589 if (w->window_end_valid
21590 && !windows_or_buffers_changed
21591 && b
21592 && !b->clip_changed
21593 && !b->prevent_redisplay_optimizations_p
21594 && !window_outdated (w)
21595 && nrow >= 0
21596 && nrow < w->current_matrix->nrows
21597 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21598 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21600 struct glyph *g, *e, *g1;
21601 int nglyphs, i;
21602 Lisp_Object levels;
21604 if (!row->reversed_p) /* Left-to-right glyph row. */
21606 g = g1 = row->glyphs[TEXT_AREA];
21607 e = g + row->used[TEXT_AREA];
21609 /* Skip over glyphs at the start of the row that was
21610 generated by redisplay for its own needs. */
21611 while (g < e
21612 && NILP (g->object)
21613 && g->charpos < 0)
21614 g++;
21615 g1 = g;
21617 /* Count the "interesting" glyphs in this row. */
21618 for (nglyphs = 0; g < e && !NILP (g->object); g++)
21619 nglyphs++;
21621 /* Create and fill the array. */
21622 levels = make_uninit_vector (nglyphs);
21623 for (i = 0; g1 < g; i++, g1++)
21624 ASET (levels, i, make_number (g1->resolved_level));
21626 else /* Right-to-left glyph row. */
21628 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21629 e = row->glyphs[TEXT_AREA] - 1;
21630 while (g > e
21631 && NILP (g->object)
21632 && g->charpos < 0)
21633 g--;
21634 g1 = g;
21635 for (nglyphs = 0; g > e && !NILP (g->object); g--)
21636 nglyphs++;
21637 levels = make_uninit_vector (nglyphs);
21638 for (i = 0; g1 > g; i++, g1--)
21639 ASET (levels, i, make_number (g1->resolved_level));
21641 return levels;
21643 else
21644 return Qnil;
21649 /***********************************************************************
21650 Menu Bar
21651 ***********************************************************************/
21653 /* Redisplay the menu bar in the frame for window W.
21655 The menu bar of X frames that don't have X toolkit support is
21656 displayed in a special window W->frame->menu_bar_window.
21658 The menu bar of terminal frames is treated specially as far as
21659 glyph matrices are concerned. Menu bar lines are not part of
21660 windows, so the update is done directly on the frame matrix rows
21661 for the menu bar. */
21663 static void
21664 display_menu_bar (struct window *w)
21666 struct frame *f = XFRAME (WINDOW_FRAME (w));
21667 struct it it;
21668 Lisp_Object items;
21669 int i;
21671 /* Don't do all this for graphical frames. */
21672 #ifdef HAVE_NTGUI
21673 if (FRAME_W32_P (f))
21674 return;
21675 #endif
21676 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21677 if (FRAME_X_P (f))
21678 return;
21679 #endif
21681 #ifdef HAVE_NS
21682 if (FRAME_NS_P (f))
21683 return;
21684 #endif /* HAVE_NS */
21686 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21687 eassert (!FRAME_WINDOW_P (f));
21688 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21689 it.first_visible_x = 0;
21690 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21691 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21692 if (FRAME_WINDOW_P (f))
21694 /* Menu bar lines are displayed in the desired matrix of the
21695 dummy window menu_bar_window. */
21696 struct window *menu_w;
21697 menu_w = XWINDOW (f->menu_bar_window);
21698 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21699 MENU_FACE_ID);
21700 it.first_visible_x = 0;
21701 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21703 else
21704 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21706 /* This is a TTY frame, i.e. character hpos/vpos are used as
21707 pixel x/y. */
21708 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21709 MENU_FACE_ID);
21710 it.first_visible_x = 0;
21711 it.last_visible_x = FRAME_COLS (f);
21714 /* FIXME: This should be controlled by a user option. See the
21715 comments in redisplay_tool_bar and display_mode_line about
21716 this. */
21717 it.paragraph_embedding = L2R;
21719 /* Clear all rows of the menu bar. */
21720 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21722 struct glyph_row *row = it.glyph_row + i;
21723 clear_glyph_row (row);
21724 row->enabled_p = true;
21725 row->full_width_p = 1;
21726 row->reversed_p = false;
21729 /* Display all items of the menu bar. */
21730 items = FRAME_MENU_BAR_ITEMS (it.f);
21731 for (i = 0; i < ASIZE (items); i += 4)
21733 Lisp_Object string;
21735 /* Stop at nil string. */
21736 string = AREF (items, i + 1);
21737 if (NILP (string))
21738 break;
21740 /* Remember where item was displayed. */
21741 ASET (items, i + 3, make_number (it.hpos));
21743 /* Display the item, pad with one space. */
21744 if (it.current_x < it.last_visible_x)
21745 display_string (NULL, string, Qnil, 0, 0, &it,
21746 SCHARS (string) + 1, 0, 0, -1);
21749 /* Fill out the line with spaces. */
21750 if (it.current_x < it.last_visible_x)
21751 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21753 /* Compute the total height of the lines. */
21754 compute_line_metrics (&it);
21757 /* Deep copy of a glyph row, including the glyphs. */
21758 static void
21759 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21761 struct glyph *pointers[1 + LAST_AREA];
21762 int to_used = to->used[TEXT_AREA];
21764 /* Save glyph pointers of TO. */
21765 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21767 /* Do a structure assignment. */
21768 *to = *from;
21770 /* Restore original glyph pointers of TO. */
21771 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21773 /* Copy the glyphs. */
21774 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21775 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21777 /* If we filled only part of the TO row, fill the rest with
21778 space_glyph (which will display as empty space). */
21779 if (to_used > from->used[TEXT_AREA])
21780 fill_up_frame_row_with_spaces (to, to_used);
21783 /* Display one menu item on a TTY, by overwriting the glyphs in the
21784 frame F's desired glyph matrix with glyphs produced from the menu
21785 item text. Called from term.c to display TTY drop-down menus one
21786 item at a time.
21788 ITEM_TEXT is the menu item text as a C string.
21790 FACE_ID is the face ID to be used for this menu item. FACE_ID
21791 could specify one of 3 faces: a face for an enabled item, a face
21792 for a disabled item, or a face for a selected item.
21794 X and Y are coordinates of the first glyph in the frame's desired
21795 matrix to be overwritten by the menu item. Since this is a TTY, Y
21796 is the zero-based number of the glyph row and X is the zero-based
21797 glyph number in the row, starting from left, where to start
21798 displaying the item.
21800 SUBMENU non-zero means this menu item drops down a submenu, which
21801 should be indicated by displaying a proper visual cue after the
21802 item text. */
21804 void
21805 display_tty_menu_item (const char *item_text, int width, int face_id,
21806 int x, int y, int submenu)
21808 struct it it;
21809 struct frame *f = SELECTED_FRAME ();
21810 struct window *w = XWINDOW (f->selected_window);
21811 int saved_used, saved_truncated, saved_width, saved_reversed;
21812 struct glyph_row *row;
21813 size_t item_len = strlen (item_text);
21815 eassert (FRAME_TERMCAP_P (f));
21817 /* Don't write beyond the matrix's last row. This can happen for
21818 TTY screens that are not high enough to show the entire menu.
21819 (This is actually a bit of defensive programming, as
21820 tty_menu_display already limits the number of menu items to one
21821 less than the number of screen lines.) */
21822 if (y >= f->desired_matrix->nrows)
21823 return;
21825 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21826 it.first_visible_x = 0;
21827 it.last_visible_x = FRAME_COLS (f) - 1;
21828 row = it.glyph_row;
21829 /* Start with the row contents from the current matrix. */
21830 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21831 saved_width = row->full_width_p;
21832 row->full_width_p = 1;
21833 saved_reversed = row->reversed_p;
21834 row->reversed_p = 0;
21835 row->enabled_p = true;
21837 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21838 desired face. */
21839 eassert (x < f->desired_matrix->matrix_w);
21840 it.current_x = it.hpos = x;
21841 it.current_y = it.vpos = y;
21842 saved_used = row->used[TEXT_AREA];
21843 saved_truncated = row->truncated_on_right_p;
21844 row->used[TEXT_AREA] = x;
21845 it.face_id = face_id;
21846 it.line_wrap = TRUNCATE;
21848 /* FIXME: This should be controlled by a user option. See the
21849 comments in redisplay_tool_bar and display_mode_line about this.
21850 Also, if paragraph_embedding could ever be R2L, changes will be
21851 needed to avoid shifting to the right the row characters in
21852 term.c:append_glyph. */
21853 it.paragraph_embedding = L2R;
21855 /* Pad with a space on the left. */
21856 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21857 width--;
21858 /* Display the menu item, pad with spaces to WIDTH. */
21859 if (submenu)
21861 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21862 item_len, 0, FRAME_COLS (f) - 1, -1);
21863 width -= item_len;
21864 /* Indicate with " >" that there's a submenu. */
21865 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21866 FRAME_COLS (f) - 1, -1);
21868 else
21869 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21870 width, 0, FRAME_COLS (f) - 1, -1);
21872 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21873 row->truncated_on_right_p = saved_truncated;
21874 row->hash = row_hash (row);
21875 row->full_width_p = saved_width;
21876 row->reversed_p = saved_reversed;
21879 /***********************************************************************
21880 Mode Line
21881 ***********************************************************************/
21883 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21884 FORCE is non-zero, redisplay mode lines unconditionally.
21885 Otherwise, redisplay only mode lines that are garbaged. Value is
21886 the number of windows whose mode lines were redisplayed. */
21888 static int
21889 redisplay_mode_lines (Lisp_Object window, bool force)
21891 int nwindows = 0;
21893 while (!NILP (window))
21895 struct window *w = XWINDOW (window);
21897 if (WINDOWP (w->contents))
21898 nwindows += redisplay_mode_lines (w->contents, force);
21899 else if (force
21900 || FRAME_GARBAGED_P (XFRAME (w->frame))
21901 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21903 struct text_pos lpoint;
21904 struct buffer *old = current_buffer;
21906 /* Set the window's buffer for the mode line display. */
21907 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21908 set_buffer_internal_1 (XBUFFER (w->contents));
21910 /* Point refers normally to the selected window. For any
21911 other window, set up appropriate value. */
21912 if (!EQ (window, selected_window))
21914 struct text_pos pt;
21916 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21917 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21920 /* Display mode lines. */
21921 clear_glyph_matrix (w->desired_matrix);
21922 if (display_mode_lines (w))
21923 ++nwindows;
21925 /* Restore old settings. */
21926 set_buffer_internal_1 (old);
21927 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21930 window = w->next;
21933 return nwindows;
21937 /* Display the mode and/or header line of window W. Value is the
21938 sum number of mode lines and header lines displayed. */
21940 static int
21941 display_mode_lines (struct window *w)
21943 Lisp_Object old_selected_window = selected_window;
21944 Lisp_Object old_selected_frame = selected_frame;
21945 Lisp_Object new_frame = w->frame;
21946 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21947 int n = 0;
21949 selected_frame = new_frame;
21950 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21951 or window's point, then we'd need select_window_1 here as well. */
21952 XSETWINDOW (selected_window, w);
21953 XFRAME (new_frame)->selected_window = selected_window;
21955 /* These will be set while the mode line specs are processed. */
21956 line_number_displayed = 0;
21957 w->column_number_displayed = -1;
21959 if (WINDOW_WANTS_MODELINE_P (w))
21961 struct window *sel_w = XWINDOW (old_selected_window);
21963 /* Select mode line face based on the real selected window. */
21964 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21965 BVAR (current_buffer, mode_line_format));
21966 ++n;
21969 if (WINDOW_WANTS_HEADER_LINE_P (w))
21971 display_mode_line (w, HEADER_LINE_FACE_ID,
21972 BVAR (current_buffer, header_line_format));
21973 ++n;
21976 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21977 selected_frame = old_selected_frame;
21978 selected_window = old_selected_window;
21979 if (n > 0)
21980 w->must_be_updated_p = true;
21981 return n;
21985 /* Display mode or header line of window W. FACE_ID specifies which
21986 line to display; it is either MODE_LINE_FACE_ID or
21987 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21988 display. Value is the pixel height of the mode/header line
21989 displayed. */
21991 static int
21992 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21994 struct it it;
21995 struct face *face;
21996 ptrdiff_t count = SPECPDL_INDEX ();
21998 init_iterator (&it, w, -1, -1, NULL, face_id);
21999 /* Don't extend on a previously drawn mode-line.
22000 This may happen if called from pos_visible_p. */
22001 it.glyph_row->enabled_p = false;
22002 prepare_desired_row (w, it.glyph_row, true);
22004 it.glyph_row->mode_line_p = 1;
22006 /* FIXME: This should be controlled by a user option. But
22007 supporting such an option is not trivial, since the mode line is
22008 made up of many separate strings. */
22009 it.paragraph_embedding = L2R;
22011 record_unwind_protect (unwind_format_mode_line,
22012 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
22014 mode_line_target = MODE_LINE_DISPLAY;
22016 /* Temporarily make frame's keyboard the current kboard so that
22017 kboard-local variables in the mode_line_format will get the right
22018 values. */
22019 push_kboard (FRAME_KBOARD (it.f));
22020 record_unwind_save_match_data ();
22021 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22022 pop_kboard ();
22024 unbind_to (count, Qnil);
22026 /* Fill up with spaces. */
22027 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22029 compute_line_metrics (&it);
22030 it.glyph_row->full_width_p = 1;
22031 it.glyph_row->continued_p = 0;
22032 it.glyph_row->truncated_on_left_p = 0;
22033 it.glyph_row->truncated_on_right_p = 0;
22035 /* Make a 3D mode-line have a shadow at its right end. */
22036 face = FACE_FROM_ID (it.f, face_id);
22037 extend_face_to_end_of_line (&it);
22038 if (face->box != FACE_NO_BOX)
22040 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22041 + it.glyph_row->used[TEXT_AREA] - 1);
22042 last->right_box_line_p = 1;
22045 return it.glyph_row->height;
22048 /* Move element ELT in LIST to the front of LIST.
22049 Return the updated list. */
22051 static Lisp_Object
22052 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22054 register Lisp_Object tail, prev;
22055 register Lisp_Object tem;
22057 tail = list;
22058 prev = Qnil;
22059 while (CONSP (tail))
22061 tem = XCAR (tail);
22063 if (EQ (elt, tem))
22065 /* Splice out the link TAIL. */
22066 if (NILP (prev))
22067 list = XCDR (tail);
22068 else
22069 Fsetcdr (prev, XCDR (tail));
22071 /* Now make it the first. */
22072 Fsetcdr (tail, list);
22073 return tail;
22075 else
22076 prev = tail;
22077 tail = XCDR (tail);
22078 QUIT;
22081 /* Not found--return unchanged LIST. */
22082 return list;
22085 /* Contribute ELT to the mode line for window IT->w. How it
22086 translates into text depends on its data type.
22088 IT describes the display environment in which we display, as usual.
22090 DEPTH is the depth in recursion. It is used to prevent
22091 infinite recursion here.
22093 FIELD_WIDTH is the number of characters the display of ELT should
22094 occupy in the mode line, and PRECISION is the maximum number of
22095 characters to display from ELT's representation. See
22096 display_string for details.
22098 Returns the hpos of the end of the text generated by ELT.
22100 PROPS is a property list to add to any string we encounter.
22102 If RISKY is nonzero, remove (disregard) any properties in any string
22103 we encounter, and ignore :eval and :propertize.
22105 The global variable `mode_line_target' determines whether the
22106 output is passed to `store_mode_line_noprop',
22107 `store_mode_line_string', or `display_string'. */
22109 static int
22110 display_mode_element (struct it *it, int depth, int field_width, int precision,
22111 Lisp_Object elt, Lisp_Object props, int risky)
22113 int n = 0, field, prec;
22114 int literal = 0;
22116 tail_recurse:
22117 if (depth > 100)
22118 elt = build_string ("*too-deep*");
22120 depth++;
22122 switch (XTYPE (elt))
22124 case Lisp_String:
22126 /* A string: output it and check for %-constructs within it. */
22127 unsigned char c;
22128 ptrdiff_t offset = 0;
22130 if (SCHARS (elt) > 0
22131 && (!NILP (props) || risky))
22133 Lisp_Object oprops, aelt;
22134 oprops = Ftext_properties_at (make_number (0), elt);
22136 /* If the starting string's properties are not what
22137 we want, translate the string. Also, if the string
22138 is risky, do that anyway. */
22140 if (NILP (Fequal (props, oprops)) || risky)
22142 /* If the starting string has properties,
22143 merge the specified ones onto the existing ones. */
22144 if (! NILP (oprops) && !risky)
22146 Lisp_Object tem;
22148 oprops = Fcopy_sequence (oprops);
22149 tem = props;
22150 while (CONSP (tem))
22152 oprops = Fplist_put (oprops, XCAR (tem),
22153 XCAR (XCDR (tem)));
22154 tem = XCDR (XCDR (tem));
22156 props = oprops;
22159 aelt = Fassoc (elt, mode_line_proptrans_alist);
22160 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22162 /* AELT is what we want. Move it to the front
22163 without consing. */
22164 elt = XCAR (aelt);
22165 mode_line_proptrans_alist
22166 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22168 else
22170 Lisp_Object tem;
22172 /* If AELT has the wrong props, it is useless.
22173 so get rid of it. */
22174 if (! NILP (aelt))
22175 mode_line_proptrans_alist
22176 = Fdelq (aelt, mode_line_proptrans_alist);
22178 elt = Fcopy_sequence (elt);
22179 Fset_text_properties (make_number (0), Flength (elt),
22180 props, elt);
22181 /* Add this item to mode_line_proptrans_alist. */
22182 mode_line_proptrans_alist
22183 = Fcons (Fcons (elt, props),
22184 mode_line_proptrans_alist);
22185 /* Truncate mode_line_proptrans_alist
22186 to at most 50 elements. */
22187 tem = Fnthcdr (make_number (50),
22188 mode_line_proptrans_alist);
22189 if (! NILP (tem))
22190 XSETCDR (tem, Qnil);
22195 offset = 0;
22197 if (literal)
22199 prec = precision - n;
22200 switch (mode_line_target)
22202 case MODE_LINE_NOPROP:
22203 case MODE_LINE_TITLE:
22204 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22205 break;
22206 case MODE_LINE_STRING:
22207 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22208 break;
22209 case MODE_LINE_DISPLAY:
22210 n += display_string (NULL, elt, Qnil, 0, 0, it,
22211 0, prec, 0, STRING_MULTIBYTE (elt));
22212 break;
22215 break;
22218 /* Handle the non-literal case. */
22220 while ((precision <= 0 || n < precision)
22221 && SREF (elt, offset) != 0
22222 && (mode_line_target != MODE_LINE_DISPLAY
22223 || it->current_x < it->last_visible_x))
22225 ptrdiff_t last_offset = offset;
22227 /* Advance to end of string or next format specifier. */
22228 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22231 if (offset - 1 != last_offset)
22233 ptrdiff_t nchars, nbytes;
22235 /* Output to end of string or up to '%'. Field width
22236 is length of string. Don't output more than
22237 PRECISION allows us. */
22238 offset--;
22240 prec = c_string_width (SDATA (elt) + last_offset,
22241 offset - last_offset, precision - n,
22242 &nchars, &nbytes);
22244 switch (mode_line_target)
22246 case MODE_LINE_NOPROP:
22247 case MODE_LINE_TITLE:
22248 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22249 break;
22250 case MODE_LINE_STRING:
22252 ptrdiff_t bytepos = last_offset;
22253 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22254 ptrdiff_t endpos = (precision <= 0
22255 ? string_byte_to_char (elt, offset)
22256 : charpos + nchars);
22258 n += store_mode_line_string (NULL,
22259 Fsubstring (elt, make_number (charpos),
22260 make_number (endpos)),
22261 0, 0, 0, Qnil);
22263 break;
22264 case MODE_LINE_DISPLAY:
22266 ptrdiff_t bytepos = last_offset;
22267 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22269 if (precision <= 0)
22270 nchars = string_byte_to_char (elt, offset) - charpos;
22271 n += display_string (NULL, elt, Qnil, 0, charpos,
22272 it, 0, nchars, 0,
22273 STRING_MULTIBYTE (elt));
22275 break;
22278 else /* c == '%' */
22280 ptrdiff_t percent_position = offset;
22282 /* Get the specified minimum width. Zero means
22283 don't pad. */
22284 field = 0;
22285 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22286 field = field * 10 + c - '0';
22288 /* Don't pad beyond the total padding allowed. */
22289 if (field_width - n > 0 && field > field_width - n)
22290 field = field_width - n;
22292 /* Note that either PRECISION <= 0 or N < PRECISION. */
22293 prec = precision - n;
22295 if (c == 'M')
22296 n += display_mode_element (it, depth, field, prec,
22297 Vglobal_mode_string, props,
22298 risky);
22299 else if (c != 0)
22301 bool multibyte;
22302 ptrdiff_t bytepos, charpos;
22303 const char *spec;
22304 Lisp_Object string;
22306 bytepos = percent_position;
22307 charpos = (STRING_MULTIBYTE (elt)
22308 ? string_byte_to_char (elt, bytepos)
22309 : bytepos);
22310 spec = decode_mode_spec (it->w, c, field, &string);
22311 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22313 switch (mode_line_target)
22315 case MODE_LINE_NOPROP:
22316 case MODE_LINE_TITLE:
22317 n += store_mode_line_noprop (spec, field, prec);
22318 break;
22319 case MODE_LINE_STRING:
22321 Lisp_Object tem = build_string (spec);
22322 props = Ftext_properties_at (make_number (charpos), elt);
22323 /* Should only keep face property in props */
22324 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22326 break;
22327 case MODE_LINE_DISPLAY:
22329 int nglyphs_before, nwritten;
22331 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22332 nwritten = display_string (spec, string, elt,
22333 charpos, 0, it,
22334 field, prec, 0,
22335 multibyte);
22337 /* Assign to the glyphs written above the
22338 string where the `%x' came from, position
22339 of the `%'. */
22340 if (nwritten > 0)
22342 struct glyph *glyph
22343 = (it->glyph_row->glyphs[TEXT_AREA]
22344 + nglyphs_before);
22345 int i;
22347 for (i = 0; i < nwritten; ++i)
22349 glyph[i].object = elt;
22350 glyph[i].charpos = charpos;
22353 n += nwritten;
22356 break;
22359 else /* c == 0 */
22360 break;
22364 break;
22366 case Lisp_Symbol:
22367 /* A symbol: process the value of the symbol recursively
22368 as if it appeared here directly. Avoid error if symbol void.
22369 Special case: if value of symbol is a string, output the string
22370 literally. */
22372 register Lisp_Object tem;
22374 /* If the variable is not marked as risky to set
22375 then its contents are risky to use. */
22376 if (NILP (Fget (elt, Qrisky_local_variable)))
22377 risky = 1;
22379 tem = Fboundp (elt);
22380 if (!NILP (tem))
22382 tem = Fsymbol_value (elt);
22383 /* If value is a string, output that string literally:
22384 don't check for % within it. */
22385 if (STRINGP (tem))
22386 literal = 1;
22388 if (!EQ (tem, elt))
22390 /* Give up right away for nil or t. */
22391 elt = tem;
22392 goto tail_recurse;
22396 break;
22398 case Lisp_Cons:
22400 register Lisp_Object car, tem;
22402 /* A cons cell: five distinct cases.
22403 If first element is :eval or :propertize, do something special.
22404 If first element is a string or a cons, process all the elements
22405 and effectively concatenate them.
22406 If first element is a negative number, truncate displaying cdr to
22407 at most that many characters. If positive, pad (with spaces)
22408 to at least that many characters.
22409 If first element is a symbol, process the cadr or caddr recursively
22410 according to whether the symbol's value is non-nil or nil. */
22411 car = XCAR (elt);
22412 if (EQ (car, QCeval))
22414 /* An element of the form (:eval FORM) means evaluate FORM
22415 and use the result as mode line elements. */
22417 if (risky)
22418 break;
22420 if (CONSP (XCDR (elt)))
22422 Lisp_Object spec;
22423 spec = safe__eval (true, XCAR (XCDR (elt)));
22424 n += display_mode_element (it, depth, field_width - n,
22425 precision - n, spec, props,
22426 risky);
22429 else if (EQ (car, QCpropertize))
22431 /* An element of the form (:propertize ELT PROPS...)
22432 means display ELT but applying properties PROPS. */
22434 if (risky)
22435 break;
22437 if (CONSP (XCDR (elt)))
22438 n += display_mode_element (it, depth, field_width - n,
22439 precision - n, XCAR (XCDR (elt)),
22440 XCDR (XCDR (elt)), risky);
22442 else if (SYMBOLP (car))
22444 tem = Fboundp (car);
22445 elt = XCDR (elt);
22446 if (!CONSP (elt))
22447 goto invalid;
22448 /* elt is now the cdr, and we know it is a cons cell.
22449 Use its car if CAR has a non-nil value. */
22450 if (!NILP (tem))
22452 tem = Fsymbol_value (car);
22453 if (!NILP (tem))
22455 elt = XCAR (elt);
22456 goto tail_recurse;
22459 /* Symbol's value is nil (or symbol is unbound)
22460 Get the cddr of the original list
22461 and if possible find the caddr and use that. */
22462 elt = XCDR (elt);
22463 if (NILP (elt))
22464 break;
22465 else if (!CONSP (elt))
22466 goto invalid;
22467 elt = XCAR (elt);
22468 goto tail_recurse;
22470 else if (INTEGERP (car))
22472 register int lim = XINT (car);
22473 elt = XCDR (elt);
22474 if (lim < 0)
22476 /* Negative int means reduce maximum width. */
22477 if (precision <= 0)
22478 precision = -lim;
22479 else
22480 precision = min (precision, -lim);
22482 else if (lim > 0)
22484 /* Padding specified. Don't let it be more than
22485 current maximum. */
22486 if (precision > 0)
22487 lim = min (precision, lim);
22489 /* If that's more padding than already wanted, queue it.
22490 But don't reduce padding already specified even if
22491 that is beyond the current truncation point. */
22492 field_width = max (lim, field_width);
22494 goto tail_recurse;
22496 else if (STRINGP (car) || CONSP (car))
22498 Lisp_Object halftail = elt;
22499 int len = 0;
22501 while (CONSP (elt)
22502 && (precision <= 0 || n < precision))
22504 n += display_mode_element (it, depth,
22505 /* Do padding only after the last
22506 element in the list. */
22507 (! CONSP (XCDR (elt))
22508 ? field_width - n
22509 : 0),
22510 precision - n, XCAR (elt),
22511 props, risky);
22512 elt = XCDR (elt);
22513 len++;
22514 if ((len & 1) == 0)
22515 halftail = XCDR (halftail);
22516 /* Check for cycle. */
22517 if (EQ (halftail, elt))
22518 break;
22522 break;
22524 default:
22525 invalid:
22526 elt = build_string ("*invalid*");
22527 goto tail_recurse;
22530 /* Pad to FIELD_WIDTH. */
22531 if (field_width > 0 && n < field_width)
22533 switch (mode_line_target)
22535 case MODE_LINE_NOPROP:
22536 case MODE_LINE_TITLE:
22537 n += store_mode_line_noprop ("", field_width - n, 0);
22538 break;
22539 case MODE_LINE_STRING:
22540 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22541 break;
22542 case MODE_LINE_DISPLAY:
22543 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22544 0, 0, 0);
22545 break;
22549 return n;
22552 /* Store a mode-line string element in mode_line_string_list.
22554 If STRING is non-null, display that C string. Otherwise, the Lisp
22555 string LISP_STRING is displayed.
22557 FIELD_WIDTH is the minimum number of output glyphs to produce.
22558 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22559 with spaces. FIELD_WIDTH <= 0 means don't pad.
22561 PRECISION is the maximum number of characters to output from
22562 STRING. PRECISION <= 0 means don't truncate the string.
22564 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22565 properties to the string.
22567 PROPS are the properties to add to the string.
22568 The mode_line_string_face face property is always added to the string.
22571 static int
22572 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22573 int field_width, int precision, Lisp_Object props)
22575 ptrdiff_t len;
22576 int n = 0;
22578 if (string != NULL)
22580 len = strlen (string);
22581 if (precision > 0 && len > precision)
22582 len = precision;
22583 lisp_string = make_string (string, len);
22584 if (NILP (props))
22585 props = mode_line_string_face_prop;
22586 else if (!NILP (mode_line_string_face))
22588 Lisp_Object face = Fplist_get (props, Qface);
22589 props = Fcopy_sequence (props);
22590 if (NILP (face))
22591 face = mode_line_string_face;
22592 else
22593 face = list2 (face, mode_line_string_face);
22594 props = Fplist_put (props, Qface, face);
22596 Fadd_text_properties (make_number (0), make_number (len),
22597 props, lisp_string);
22599 else
22601 len = XFASTINT (Flength (lisp_string));
22602 if (precision > 0 && len > precision)
22604 len = precision;
22605 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22606 precision = -1;
22608 if (!NILP (mode_line_string_face))
22610 Lisp_Object face;
22611 if (NILP (props))
22612 props = Ftext_properties_at (make_number (0), lisp_string);
22613 face = Fplist_get (props, Qface);
22614 if (NILP (face))
22615 face = mode_line_string_face;
22616 else
22617 face = list2 (face, mode_line_string_face);
22618 props = list2 (Qface, face);
22619 if (copy_string)
22620 lisp_string = Fcopy_sequence (lisp_string);
22622 if (!NILP (props))
22623 Fadd_text_properties (make_number (0), make_number (len),
22624 props, lisp_string);
22627 if (len > 0)
22629 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22630 n += len;
22633 if (field_width > len)
22635 field_width -= len;
22636 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22637 if (!NILP (props))
22638 Fadd_text_properties (make_number (0), make_number (field_width),
22639 props, lisp_string);
22640 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22641 n += field_width;
22644 return n;
22648 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22649 1, 4, 0,
22650 doc: /* Format a string out of a mode line format specification.
22651 First arg FORMAT specifies the mode line format (see `mode-line-format'
22652 for details) to use.
22654 By default, the format is evaluated for the currently selected window.
22656 Optional second arg FACE specifies the face property to put on all
22657 characters for which no face is specified. The value nil means the
22658 default face. The value t means whatever face the window's mode line
22659 currently uses (either `mode-line' or `mode-line-inactive',
22660 depending on whether the window is the selected window or not).
22661 An integer value means the value string has no text
22662 properties.
22664 Optional third and fourth args WINDOW and BUFFER specify the window
22665 and buffer to use as the context for the formatting (defaults
22666 are the selected window and the WINDOW's buffer). */)
22667 (Lisp_Object format, Lisp_Object face,
22668 Lisp_Object window, Lisp_Object buffer)
22670 struct it it;
22671 int len;
22672 struct window *w;
22673 struct buffer *old_buffer = NULL;
22674 int face_id;
22675 int no_props = INTEGERP (face);
22676 ptrdiff_t count = SPECPDL_INDEX ();
22677 Lisp_Object str;
22678 int string_start = 0;
22680 w = decode_any_window (window);
22681 XSETWINDOW (window, w);
22683 if (NILP (buffer))
22684 buffer = w->contents;
22685 CHECK_BUFFER (buffer);
22687 /* Make formatting the modeline a non-op when noninteractive, otherwise
22688 there will be problems later caused by a partially initialized frame. */
22689 if (NILP (format) || noninteractive)
22690 return empty_unibyte_string;
22692 if (no_props)
22693 face = Qnil;
22695 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22696 : EQ (face, Qt) ? (EQ (window, selected_window)
22697 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22698 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22699 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22700 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22701 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22702 : DEFAULT_FACE_ID;
22704 old_buffer = current_buffer;
22706 /* Save things including mode_line_proptrans_alist,
22707 and set that to nil so that we don't alter the outer value. */
22708 record_unwind_protect (unwind_format_mode_line,
22709 format_mode_line_unwind_data
22710 (XFRAME (WINDOW_FRAME (w)),
22711 old_buffer, selected_window, 1));
22712 mode_line_proptrans_alist = Qnil;
22714 Fselect_window (window, Qt);
22715 set_buffer_internal_1 (XBUFFER (buffer));
22717 init_iterator (&it, w, -1, -1, NULL, face_id);
22719 if (no_props)
22721 mode_line_target = MODE_LINE_NOPROP;
22722 mode_line_string_face_prop = Qnil;
22723 mode_line_string_list = Qnil;
22724 string_start = MODE_LINE_NOPROP_LEN (0);
22726 else
22728 mode_line_target = MODE_LINE_STRING;
22729 mode_line_string_list = Qnil;
22730 mode_line_string_face = face;
22731 mode_line_string_face_prop
22732 = NILP (face) ? Qnil : list2 (Qface, face);
22735 push_kboard (FRAME_KBOARD (it.f));
22736 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22737 pop_kboard ();
22739 if (no_props)
22741 len = MODE_LINE_NOPROP_LEN (string_start);
22742 str = make_string (mode_line_noprop_buf + string_start, len);
22744 else
22746 mode_line_string_list = Fnreverse (mode_line_string_list);
22747 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22748 empty_unibyte_string);
22751 unbind_to (count, Qnil);
22752 return str;
22755 /* Write a null-terminated, right justified decimal representation of
22756 the positive integer D to BUF using a minimal field width WIDTH. */
22758 static void
22759 pint2str (register char *buf, register int width, register ptrdiff_t d)
22761 register char *p = buf;
22763 if (d <= 0)
22764 *p++ = '0';
22765 else
22767 while (d > 0)
22769 *p++ = d % 10 + '0';
22770 d /= 10;
22774 for (width -= (int) (p - buf); width > 0; --width)
22775 *p++ = ' ';
22776 *p-- = '\0';
22777 while (p > buf)
22779 d = *buf;
22780 *buf++ = *p;
22781 *p-- = d;
22785 /* Write a null-terminated, right justified decimal and "human
22786 readable" representation of the nonnegative integer D to BUF using
22787 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22789 static const char power_letter[] =
22791 0, /* no letter */
22792 'k', /* kilo */
22793 'M', /* mega */
22794 'G', /* giga */
22795 'T', /* tera */
22796 'P', /* peta */
22797 'E', /* exa */
22798 'Z', /* zetta */
22799 'Y' /* yotta */
22802 static void
22803 pint2hrstr (char *buf, int width, ptrdiff_t d)
22805 /* We aim to represent the nonnegative integer D as
22806 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22807 ptrdiff_t quotient = d;
22808 int remainder = 0;
22809 /* -1 means: do not use TENTHS. */
22810 int tenths = -1;
22811 int exponent = 0;
22813 /* Length of QUOTIENT.TENTHS as a string. */
22814 int length;
22816 char * psuffix;
22817 char * p;
22819 if (quotient >= 1000)
22821 /* Scale to the appropriate EXPONENT. */
22824 remainder = quotient % 1000;
22825 quotient /= 1000;
22826 exponent++;
22828 while (quotient >= 1000);
22830 /* Round to nearest and decide whether to use TENTHS or not. */
22831 if (quotient <= 9)
22833 tenths = remainder / 100;
22834 if (remainder % 100 >= 50)
22836 if (tenths < 9)
22837 tenths++;
22838 else
22840 quotient++;
22841 if (quotient == 10)
22842 tenths = -1;
22843 else
22844 tenths = 0;
22848 else
22849 if (remainder >= 500)
22851 if (quotient < 999)
22852 quotient++;
22853 else
22855 quotient = 1;
22856 exponent++;
22857 tenths = 0;
22862 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22863 if (tenths == -1 && quotient <= 99)
22864 if (quotient <= 9)
22865 length = 1;
22866 else
22867 length = 2;
22868 else
22869 length = 3;
22870 p = psuffix = buf + max (width, length);
22872 /* Print EXPONENT. */
22873 *psuffix++ = power_letter[exponent];
22874 *psuffix = '\0';
22876 /* Print TENTHS. */
22877 if (tenths >= 0)
22879 *--p = '0' + tenths;
22880 *--p = '.';
22883 /* Print QUOTIENT. */
22886 int digit = quotient % 10;
22887 *--p = '0' + digit;
22889 while ((quotient /= 10) != 0);
22891 /* Print leading spaces. */
22892 while (buf < p)
22893 *--p = ' ';
22896 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22897 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22898 type of CODING_SYSTEM. Return updated pointer into BUF. */
22900 static unsigned char invalid_eol_type[] = "(*invalid*)";
22902 static char *
22903 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22905 Lisp_Object val;
22906 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22907 const unsigned char *eol_str;
22908 int eol_str_len;
22909 /* The EOL conversion we are using. */
22910 Lisp_Object eoltype;
22912 val = CODING_SYSTEM_SPEC (coding_system);
22913 eoltype = Qnil;
22915 if (!VECTORP (val)) /* Not yet decided. */
22917 *buf++ = multibyte ? '-' : ' ';
22918 if (eol_flag)
22919 eoltype = eol_mnemonic_undecided;
22920 /* Don't mention EOL conversion if it isn't decided. */
22922 else
22924 Lisp_Object attrs;
22925 Lisp_Object eolvalue;
22927 attrs = AREF (val, 0);
22928 eolvalue = AREF (val, 2);
22930 *buf++ = multibyte
22931 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22932 : ' ';
22934 if (eol_flag)
22936 /* The EOL conversion that is normal on this system. */
22938 if (NILP (eolvalue)) /* Not yet decided. */
22939 eoltype = eol_mnemonic_undecided;
22940 else if (VECTORP (eolvalue)) /* Not yet decided. */
22941 eoltype = eol_mnemonic_undecided;
22942 else /* eolvalue is Qunix, Qdos, or Qmac. */
22943 eoltype = (EQ (eolvalue, Qunix)
22944 ? eol_mnemonic_unix
22945 : (EQ (eolvalue, Qdos) == 1
22946 ? eol_mnemonic_dos : eol_mnemonic_mac));
22950 if (eol_flag)
22952 /* Mention the EOL conversion if it is not the usual one. */
22953 if (STRINGP (eoltype))
22955 eol_str = SDATA (eoltype);
22956 eol_str_len = SBYTES (eoltype);
22958 else if (CHARACTERP (eoltype))
22960 int c = XFASTINT (eoltype);
22961 return buf + CHAR_STRING (c, (unsigned char *) buf);
22963 else
22965 eol_str = invalid_eol_type;
22966 eol_str_len = sizeof (invalid_eol_type) - 1;
22968 memcpy (buf, eol_str, eol_str_len);
22969 buf += eol_str_len;
22972 return buf;
22975 /* Return a string for the output of a mode line %-spec for window W,
22976 generated by character C. FIELD_WIDTH > 0 means pad the string
22977 returned with spaces to that value. Return a Lisp string in
22978 *STRING if the resulting string is taken from that Lisp string.
22980 Note we operate on the current buffer for most purposes. */
22982 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22984 static const char *
22985 decode_mode_spec (struct window *w, register int c, int field_width,
22986 Lisp_Object *string)
22988 Lisp_Object obj;
22989 struct frame *f = XFRAME (WINDOW_FRAME (w));
22990 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22991 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22992 produce strings from numerical values, so limit preposterously
22993 large values of FIELD_WIDTH to avoid overrunning the buffer's
22994 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22995 bytes plus the terminating null. */
22996 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22997 struct buffer *b = current_buffer;
22999 obj = Qnil;
23000 *string = Qnil;
23002 switch (c)
23004 case '*':
23005 if (!NILP (BVAR (b, read_only)))
23006 return "%";
23007 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23008 return "*";
23009 return "-";
23011 case '+':
23012 /* This differs from %* only for a modified read-only buffer. */
23013 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23014 return "*";
23015 if (!NILP (BVAR (b, read_only)))
23016 return "%";
23017 return "-";
23019 case '&':
23020 /* This differs from %* in ignoring read-only-ness. */
23021 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23022 return "*";
23023 return "-";
23025 case '%':
23026 return "%";
23028 case '[':
23030 int i;
23031 char *p;
23033 if (command_loop_level > 5)
23034 return "[[[... ";
23035 p = decode_mode_spec_buf;
23036 for (i = 0; i < command_loop_level; i++)
23037 *p++ = '[';
23038 *p = 0;
23039 return decode_mode_spec_buf;
23042 case ']':
23044 int i;
23045 char *p;
23047 if (command_loop_level > 5)
23048 return " ...]]]";
23049 p = decode_mode_spec_buf;
23050 for (i = 0; i < command_loop_level; i++)
23051 *p++ = ']';
23052 *p = 0;
23053 return decode_mode_spec_buf;
23056 case '-':
23058 register int i;
23060 /* Let lots_of_dashes be a string of infinite length. */
23061 if (mode_line_target == MODE_LINE_NOPROP
23062 || mode_line_target == MODE_LINE_STRING)
23063 return "--";
23064 if (field_width <= 0
23065 || field_width > sizeof (lots_of_dashes))
23067 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23068 decode_mode_spec_buf[i] = '-';
23069 decode_mode_spec_buf[i] = '\0';
23070 return decode_mode_spec_buf;
23072 else
23073 return lots_of_dashes;
23076 case 'b':
23077 obj = BVAR (b, name);
23078 break;
23080 case 'c':
23081 /* %c and %l are ignored in `frame-title-format'.
23082 (In redisplay_internal, the frame title is drawn _before_ the
23083 windows are updated, so the stuff which depends on actual
23084 window contents (such as %l) may fail to render properly, or
23085 even crash emacs.) */
23086 if (mode_line_target == MODE_LINE_TITLE)
23087 return "";
23088 else
23090 ptrdiff_t col = current_column ();
23091 w->column_number_displayed = col;
23092 pint2str (decode_mode_spec_buf, width, col);
23093 return decode_mode_spec_buf;
23096 case 'e':
23097 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23099 if (NILP (Vmemory_full))
23100 return "";
23101 else
23102 return "!MEM FULL! ";
23104 #else
23105 return "";
23106 #endif
23108 case 'F':
23109 /* %F displays the frame name. */
23110 if (!NILP (f->title))
23111 return SSDATA (f->title);
23112 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23113 return SSDATA (f->name);
23114 return "Emacs";
23116 case 'f':
23117 obj = BVAR (b, filename);
23118 break;
23120 case 'i':
23122 ptrdiff_t size = ZV - BEGV;
23123 pint2str (decode_mode_spec_buf, width, size);
23124 return decode_mode_spec_buf;
23127 case 'I':
23129 ptrdiff_t size = ZV - BEGV;
23130 pint2hrstr (decode_mode_spec_buf, width, size);
23131 return decode_mode_spec_buf;
23134 case 'l':
23136 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23137 ptrdiff_t topline, nlines, height;
23138 ptrdiff_t junk;
23140 /* %c and %l are ignored in `frame-title-format'. */
23141 if (mode_line_target == MODE_LINE_TITLE)
23142 return "";
23144 startpos = marker_position (w->start);
23145 startpos_byte = marker_byte_position (w->start);
23146 height = WINDOW_TOTAL_LINES (w);
23148 /* If we decided that this buffer isn't suitable for line numbers,
23149 don't forget that too fast. */
23150 if (w->base_line_pos == -1)
23151 goto no_value;
23153 /* If the buffer is very big, don't waste time. */
23154 if (INTEGERP (Vline_number_display_limit)
23155 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23157 w->base_line_pos = 0;
23158 w->base_line_number = 0;
23159 goto no_value;
23162 if (w->base_line_number > 0
23163 && w->base_line_pos > 0
23164 && w->base_line_pos <= startpos)
23166 line = w->base_line_number;
23167 linepos = w->base_line_pos;
23168 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23170 else
23172 line = 1;
23173 linepos = BUF_BEGV (b);
23174 linepos_byte = BUF_BEGV_BYTE (b);
23177 /* Count lines from base line to window start position. */
23178 nlines = display_count_lines (linepos_byte,
23179 startpos_byte,
23180 startpos, &junk);
23182 topline = nlines + line;
23184 /* Determine a new base line, if the old one is too close
23185 or too far away, or if we did not have one.
23186 "Too close" means it's plausible a scroll-down would
23187 go back past it. */
23188 if (startpos == BUF_BEGV (b))
23190 w->base_line_number = topline;
23191 w->base_line_pos = BUF_BEGV (b);
23193 else if (nlines < height + 25 || nlines > height * 3 + 50
23194 || linepos == BUF_BEGV (b))
23196 ptrdiff_t limit = BUF_BEGV (b);
23197 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23198 ptrdiff_t position;
23199 ptrdiff_t distance =
23200 (height * 2 + 30) * line_number_display_limit_width;
23202 if (startpos - distance > limit)
23204 limit = startpos - distance;
23205 limit_byte = CHAR_TO_BYTE (limit);
23208 nlines = display_count_lines (startpos_byte,
23209 limit_byte,
23210 - (height * 2 + 30),
23211 &position);
23212 /* If we couldn't find the lines we wanted within
23213 line_number_display_limit_width chars per line,
23214 give up on line numbers for this window. */
23215 if (position == limit_byte && limit == startpos - distance)
23217 w->base_line_pos = -1;
23218 w->base_line_number = 0;
23219 goto no_value;
23222 w->base_line_number = topline - nlines;
23223 w->base_line_pos = BYTE_TO_CHAR (position);
23226 /* Now count lines from the start pos to point. */
23227 nlines = display_count_lines (startpos_byte,
23228 PT_BYTE, PT, &junk);
23230 /* Record that we did display the line number. */
23231 line_number_displayed = 1;
23233 /* Make the string to show. */
23234 pint2str (decode_mode_spec_buf, width, topline + nlines);
23235 return decode_mode_spec_buf;
23236 no_value:
23238 char *p = decode_mode_spec_buf;
23239 int pad = width - 2;
23240 while (pad-- > 0)
23241 *p++ = ' ';
23242 *p++ = '?';
23243 *p++ = '?';
23244 *p = '\0';
23245 return decode_mode_spec_buf;
23248 break;
23250 case 'm':
23251 obj = BVAR (b, mode_name);
23252 break;
23254 case 'n':
23255 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23256 return " Narrow";
23257 break;
23259 case 'p':
23261 ptrdiff_t pos = marker_position (w->start);
23262 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23264 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23266 if (pos <= BUF_BEGV (b))
23267 return "All";
23268 else
23269 return "Bottom";
23271 else if (pos <= BUF_BEGV (b))
23272 return "Top";
23273 else
23275 if (total > 1000000)
23276 /* Do it differently for a large value, to avoid overflow. */
23277 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23278 else
23279 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23280 /* We can't normally display a 3-digit number,
23281 so get us a 2-digit number that is close. */
23282 if (total == 100)
23283 total = 99;
23284 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23285 return decode_mode_spec_buf;
23289 /* Display percentage of size above the bottom of the screen. */
23290 case 'P':
23292 ptrdiff_t toppos = marker_position (w->start);
23293 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23294 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23296 if (botpos >= BUF_ZV (b))
23298 if (toppos <= BUF_BEGV (b))
23299 return "All";
23300 else
23301 return "Bottom";
23303 else
23305 if (total > 1000000)
23306 /* Do it differently for a large value, to avoid overflow. */
23307 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23308 else
23309 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23310 /* We can't normally display a 3-digit number,
23311 so get us a 2-digit number that is close. */
23312 if (total == 100)
23313 total = 99;
23314 if (toppos <= BUF_BEGV (b))
23315 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23316 else
23317 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23318 return decode_mode_spec_buf;
23322 case 's':
23323 /* status of process */
23324 obj = Fget_buffer_process (Fcurrent_buffer ());
23325 if (NILP (obj))
23326 return "no process";
23327 #ifndef MSDOS
23328 obj = Fsymbol_name (Fprocess_status (obj));
23329 #endif
23330 break;
23332 case '@':
23334 ptrdiff_t count = inhibit_garbage_collection ();
23335 Lisp_Object curdir = BVAR (current_buffer, directory);
23336 Lisp_Object val = Qnil;
23338 if (STRINGP (curdir))
23339 val = call1 (intern ("file-remote-p"), curdir);
23341 unbind_to (count, Qnil);
23343 if (NILP (val))
23344 return "-";
23345 else
23346 return "@";
23349 case 'z':
23350 /* coding-system (not including end-of-line format) */
23351 case 'Z':
23352 /* coding-system (including end-of-line type) */
23354 int eol_flag = (c == 'Z');
23355 char *p = decode_mode_spec_buf;
23357 if (! FRAME_WINDOW_P (f))
23359 /* No need to mention EOL here--the terminal never needs
23360 to do EOL conversion. */
23361 p = decode_mode_spec_coding (CODING_ID_NAME
23362 (FRAME_KEYBOARD_CODING (f)->id),
23363 p, 0);
23364 p = decode_mode_spec_coding (CODING_ID_NAME
23365 (FRAME_TERMINAL_CODING (f)->id),
23366 p, 0);
23368 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23369 p, eol_flag);
23371 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23372 #ifdef subprocesses
23373 obj = Fget_buffer_process (Fcurrent_buffer ());
23374 if (PROCESSP (obj))
23376 p = decode_mode_spec_coding
23377 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23378 p = decode_mode_spec_coding
23379 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23381 #endif /* subprocesses */
23382 #endif /* 0 */
23383 *p = 0;
23384 return decode_mode_spec_buf;
23388 if (STRINGP (obj))
23390 *string = obj;
23391 return SSDATA (obj);
23393 else
23394 return "";
23398 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23399 means count lines back from START_BYTE. But don't go beyond
23400 LIMIT_BYTE. Return the number of lines thus found (always
23401 nonnegative).
23403 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23404 either the position COUNT lines after/before START_BYTE, if we
23405 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23406 COUNT lines. */
23408 static ptrdiff_t
23409 display_count_lines (ptrdiff_t start_byte,
23410 ptrdiff_t limit_byte, ptrdiff_t count,
23411 ptrdiff_t *byte_pos_ptr)
23413 register unsigned char *cursor;
23414 unsigned char *base;
23416 register ptrdiff_t ceiling;
23417 register unsigned char *ceiling_addr;
23418 ptrdiff_t orig_count = count;
23420 /* If we are not in selective display mode,
23421 check only for newlines. */
23422 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23423 && !INTEGERP (BVAR (current_buffer, selective_display)));
23425 if (count > 0)
23427 while (start_byte < limit_byte)
23429 ceiling = BUFFER_CEILING_OF (start_byte);
23430 ceiling = min (limit_byte - 1, ceiling);
23431 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23432 base = (cursor = BYTE_POS_ADDR (start_byte));
23436 if (selective_display)
23438 while (*cursor != '\n' && *cursor != 015
23439 && ++cursor != ceiling_addr)
23440 continue;
23441 if (cursor == ceiling_addr)
23442 break;
23444 else
23446 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23447 if (! cursor)
23448 break;
23451 cursor++;
23453 if (--count == 0)
23455 start_byte += cursor - base;
23456 *byte_pos_ptr = start_byte;
23457 return orig_count;
23460 while (cursor < ceiling_addr);
23462 start_byte += ceiling_addr - base;
23465 else
23467 while (start_byte > limit_byte)
23469 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23470 ceiling = max (limit_byte, ceiling);
23471 ceiling_addr = BYTE_POS_ADDR (ceiling);
23472 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23473 while (1)
23475 if (selective_display)
23477 while (--cursor >= ceiling_addr
23478 && *cursor != '\n' && *cursor != 015)
23479 continue;
23480 if (cursor < ceiling_addr)
23481 break;
23483 else
23485 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23486 if (! cursor)
23487 break;
23490 if (++count == 0)
23492 start_byte += cursor - base + 1;
23493 *byte_pos_ptr = start_byte;
23494 /* When scanning backwards, we should
23495 not count the newline posterior to which we stop. */
23496 return - orig_count - 1;
23499 start_byte += ceiling_addr - base;
23503 *byte_pos_ptr = limit_byte;
23505 if (count < 0)
23506 return - orig_count + count;
23507 return orig_count - count;
23513 /***********************************************************************
23514 Displaying strings
23515 ***********************************************************************/
23517 /* Display a NUL-terminated string, starting with index START.
23519 If STRING is non-null, display that C string. Otherwise, the Lisp
23520 string LISP_STRING is displayed. There's a case that STRING is
23521 non-null and LISP_STRING is not nil. It means STRING is a string
23522 data of LISP_STRING. In that case, we display LISP_STRING while
23523 ignoring its text properties.
23525 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23526 FACE_STRING. Display STRING or LISP_STRING with the face at
23527 FACE_STRING_POS in FACE_STRING:
23529 Display the string in the environment given by IT, but use the
23530 standard display table, temporarily.
23532 FIELD_WIDTH is the minimum number of output glyphs to produce.
23533 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23534 with spaces. If STRING has more characters, more than FIELD_WIDTH
23535 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23537 PRECISION is the maximum number of characters to output from
23538 STRING. PRECISION < 0 means don't truncate the string.
23540 This is roughly equivalent to printf format specifiers:
23542 FIELD_WIDTH PRECISION PRINTF
23543 ----------------------------------------
23544 -1 -1 %s
23545 -1 10 %.10s
23546 10 -1 %10s
23547 20 10 %20.10s
23549 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23550 display them, and < 0 means obey the current buffer's value of
23551 enable_multibyte_characters.
23553 Value is the number of columns displayed. */
23555 static int
23556 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23557 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23558 int field_width, int precision, int max_x, int multibyte)
23560 int hpos_at_start = it->hpos;
23561 int saved_face_id = it->face_id;
23562 struct glyph_row *row = it->glyph_row;
23563 ptrdiff_t it_charpos;
23565 /* Initialize the iterator IT for iteration over STRING beginning
23566 with index START. */
23567 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23568 precision, field_width, multibyte);
23569 if (string && STRINGP (lisp_string))
23570 /* LISP_STRING is the one returned by decode_mode_spec. We should
23571 ignore its text properties. */
23572 it->stop_charpos = it->end_charpos;
23574 /* If displaying STRING, set up the face of the iterator from
23575 FACE_STRING, if that's given. */
23576 if (STRINGP (face_string))
23578 ptrdiff_t endptr;
23579 struct face *face;
23581 it->face_id
23582 = face_at_string_position (it->w, face_string, face_string_pos,
23583 0, &endptr, it->base_face_id, 0);
23584 face = FACE_FROM_ID (it->f, it->face_id);
23585 it->face_box_p = face->box != FACE_NO_BOX;
23588 /* Set max_x to the maximum allowed X position. Don't let it go
23589 beyond the right edge of the window. */
23590 if (max_x <= 0)
23591 max_x = it->last_visible_x;
23592 else
23593 max_x = min (max_x, it->last_visible_x);
23595 /* Skip over display elements that are not visible. because IT->w is
23596 hscrolled. */
23597 if (it->current_x < it->first_visible_x)
23598 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23599 MOVE_TO_POS | MOVE_TO_X);
23601 row->ascent = it->max_ascent;
23602 row->height = it->max_ascent + it->max_descent;
23603 row->phys_ascent = it->max_phys_ascent;
23604 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23605 row->extra_line_spacing = it->max_extra_line_spacing;
23607 if (STRINGP (it->string))
23608 it_charpos = IT_STRING_CHARPOS (*it);
23609 else
23610 it_charpos = IT_CHARPOS (*it);
23612 /* This condition is for the case that we are called with current_x
23613 past last_visible_x. */
23614 while (it->current_x < max_x)
23616 int x_before, x, n_glyphs_before, i, nglyphs;
23618 /* Get the next display element. */
23619 if (!get_next_display_element (it))
23620 break;
23622 /* Produce glyphs. */
23623 x_before = it->current_x;
23624 n_glyphs_before = row->used[TEXT_AREA];
23625 PRODUCE_GLYPHS (it);
23627 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23628 i = 0;
23629 x = x_before;
23630 while (i < nglyphs)
23632 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23634 if (it->line_wrap != TRUNCATE
23635 && x + glyph->pixel_width > max_x)
23637 /* End of continued line or max_x reached. */
23638 if (CHAR_GLYPH_PADDING_P (*glyph))
23640 /* A wide character is unbreakable. */
23641 if (row->reversed_p)
23642 unproduce_glyphs (it, row->used[TEXT_AREA]
23643 - n_glyphs_before);
23644 row->used[TEXT_AREA] = n_glyphs_before;
23645 it->current_x = x_before;
23647 else
23649 if (row->reversed_p)
23650 unproduce_glyphs (it, row->used[TEXT_AREA]
23651 - (n_glyphs_before + i));
23652 row->used[TEXT_AREA] = n_glyphs_before + i;
23653 it->current_x = x;
23655 break;
23657 else if (x + glyph->pixel_width >= it->first_visible_x)
23659 /* Glyph is at least partially visible. */
23660 ++it->hpos;
23661 if (x < it->first_visible_x)
23662 row->x = x - it->first_visible_x;
23664 else
23666 /* Glyph is off the left margin of the display area.
23667 Should not happen. */
23668 emacs_abort ();
23671 row->ascent = max (row->ascent, it->max_ascent);
23672 row->height = max (row->height, it->max_ascent + it->max_descent);
23673 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23674 row->phys_height = max (row->phys_height,
23675 it->max_phys_ascent + it->max_phys_descent);
23676 row->extra_line_spacing = max (row->extra_line_spacing,
23677 it->max_extra_line_spacing);
23678 x += glyph->pixel_width;
23679 ++i;
23682 /* Stop if max_x reached. */
23683 if (i < nglyphs)
23684 break;
23686 /* Stop at line ends. */
23687 if (ITERATOR_AT_END_OF_LINE_P (it))
23689 it->continuation_lines_width = 0;
23690 break;
23693 set_iterator_to_next (it, 1);
23694 if (STRINGP (it->string))
23695 it_charpos = IT_STRING_CHARPOS (*it);
23696 else
23697 it_charpos = IT_CHARPOS (*it);
23699 /* Stop if truncating at the right edge. */
23700 if (it->line_wrap == TRUNCATE
23701 && it->current_x >= it->last_visible_x)
23703 /* Add truncation mark, but don't do it if the line is
23704 truncated at a padding space. */
23705 if (it_charpos < it->string_nchars)
23707 if (!FRAME_WINDOW_P (it->f))
23709 int ii, n;
23711 if (it->current_x > it->last_visible_x)
23713 if (!row->reversed_p)
23715 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23716 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23717 break;
23719 else
23721 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23722 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23723 break;
23724 unproduce_glyphs (it, ii + 1);
23725 ii = row->used[TEXT_AREA] - (ii + 1);
23727 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23729 row->used[TEXT_AREA] = ii;
23730 produce_special_glyphs (it, IT_TRUNCATION);
23733 produce_special_glyphs (it, IT_TRUNCATION);
23735 row->truncated_on_right_p = 1;
23737 break;
23741 /* Maybe insert a truncation at the left. */
23742 if (it->first_visible_x
23743 && it_charpos > 0)
23745 if (!FRAME_WINDOW_P (it->f)
23746 || (row->reversed_p
23747 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23748 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23749 insert_left_trunc_glyphs (it);
23750 row->truncated_on_left_p = 1;
23753 it->face_id = saved_face_id;
23755 /* Value is number of columns displayed. */
23756 return it->hpos - hpos_at_start;
23761 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23762 appears as an element of LIST or as the car of an element of LIST.
23763 If PROPVAL is a list, compare each element against LIST in that
23764 way, and return 1/2 if any element of PROPVAL is found in LIST.
23765 Otherwise return 0. This function cannot quit.
23766 The return value is 2 if the text is invisible but with an ellipsis
23767 and 1 if it's invisible and without an ellipsis. */
23770 invisible_p (register Lisp_Object propval, Lisp_Object list)
23772 register Lisp_Object tail, proptail;
23774 for (tail = list; CONSP (tail); tail = XCDR (tail))
23776 register Lisp_Object tem;
23777 tem = XCAR (tail);
23778 if (EQ (propval, tem))
23779 return 1;
23780 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23781 return NILP (XCDR (tem)) ? 1 : 2;
23784 if (CONSP (propval))
23786 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23788 Lisp_Object propelt;
23789 propelt = XCAR (proptail);
23790 for (tail = list; CONSP (tail); tail = XCDR (tail))
23792 register Lisp_Object tem;
23793 tem = XCAR (tail);
23794 if (EQ (propelt, tem))
23795 return 1;
23796 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23797 return NILP (XCDR (tem)) ? 1 : 2;
23802 return 0;
23805 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23806 doc: /* Non-nil if the property makes the text invisible.
23807 POS-OR-PROP can be a marker or number, in which case it is taken to be
23808 a position in the current buffer and the value of the `invisible' property
23809 is checked; or it can be some other value, which is then presumed to be the
23810 value of the `invisible' property of the text of interest.
23811 The non-nil value returned can be t for truly invisible text or something
23812 else if the text is replaced by an ellipsis. */)
23813 (Lisp_Object pos_or_prop)
23815 Lisp_Object prop
23816 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23817 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23818 : pos_or_prop);
23819 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23820 return (invis == 0 ? Qnil
23821 : invis == 1 ? Qt
23822 : make_number (invis));
23825 /* Calculate a width or height in pixels from a specification using
23826 the following elements:
23828 SPEC ::=
23829 NUM - a (fractional) multiple of the default font width/height
23830 (NUM) - specifies exactly NUM pixels
23831 UNIT - a fixed number of pixels, see below.
23832 ELEMENT - size of a display element in pixels, see below.
23833 (NUM . SPEC) - equals NUM * SPEC
23834 (+ SPEC SPEC ...) - add pixel values
23835 (- SPEC SPEC ...) - subtract pixel values
23836 (- SPEC) - negate pixel value
23838 NUM ::=
23839 INT or FLOAT - a number constant
23840 SYMBOL - use symbol's (buffer local) variable binding.
23842 UNIT ::=
23843 in - pixels per inch *)
23844 mm - pixels per 1/1000 meter *)
23845 cm - pixels per 1/100 meter *)
23846 width - width of current font in pixels.
23847 height - height of current font in pixels.
23849 *) using the ratio(s) defined in display-pixels-per-inch.
23851 ELEMENT ::=
23853 left-fringe - left fringe width in pixels
23854 right-fringe - right fringe width in pixels
23856 left-margin - left margin width in pixels
23857 right-margin - right margin width in pixels
23859 scroll-bar - scroll-bar area width in pixels
23861 Examples:
23863 Pixels corresponding to 5 inches:
23864 (5 . in)
23866 Total width of non-text areas on left side of window (if scroll-bar is on left):
23867 '(space :width (+ left-fringe left-margin scroll-bar))
23869 Align to first text column (in header line):
23870 '(space :align-to 0)
23872 Align to middle of text area minus half the width of variable `my-image'
23873 containing a loaded image:
23874 '(space :align-to (0.5 . (- text my-image)))
23876 Width of left margin minus width of 1 character in the default font:
23877 '(space :width (- left-margin 1))
23879 Width of left margin minus width of 2 characters in the current font:
23880 '(space :width (- left-margin (2 . width)))
23882 Center 1 character over left-margin (in header line):
23883 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23885 Different ways to express width of left fringe plus left margin minus one pixel:
23886 '(space :width (- (+ left-fringe left-margin) (1)))
23887 '(space :width (+ left-fringe left-margin (- (1))))
23888 '(space :width (+ left-fringe left-margin (-1)))
23892 static int
23893 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23894 struct font *font, int width_p, int *align_to)
23896 double pixels;
23898 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23899 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23901 if (NILP (prop))
23902 return OK_PIXELS (0);
23904 eassert (FRAME_LIVE_P (it->f));
23906 if (SYMBOLP (prop))
23908 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23910 char *unit = SSDATA (SYMBOL_NAME (prop));
23912 if (unit[0] == 'i' && unit[1] == 'n')
23913 pixels = 1.0;
23914 else if (unit[0] == 'm' && unit[1] == 'm')
23915 pixels = 25.4;
23916 else if (unit[0] == 'c' && unit[1] == 'm')
23917 pixels = 2.54;
23918 else
23919 pixels = 0;
23920 if (pixels > 0)
23922 double ppi = (width_p ? FRAME_RES_X (it->f)
23923 : FRAME_RES_Y (it->f));
23925 if (ppi > 0)
23926 return OK_PIXELS (ppi / pixels);
23927 return 0;
23931 #ifdef HAVE_WINDOW_SYSTEM
23932 if (EQ (prop, Qheight))
23933 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23934 if (EQ (prop, Qwidth))
23935 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23936 #else
23937 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23938 return OK_PIXELS (1);
23939 #endif
23941 if (EQ (prop, Qtext))
23942 return OK_PIXELS (width_p
23943 ? window_box_width (it->w, TEXT_AREA)
23944 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23946 if (align_to && *align_to < 0)
23948 *res = 0;
23949 if (EQ (prop, Qleft))
23950 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23951 if (EQ (prop, Qright))
23952 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23953 if (EQ (prop, Qcenter))
23954 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23955 + window_box_width (it->w, TEXT_AREA) / 2);
23956 if (EQ (prop, Qleft_fringe))
23957 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23958 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23959 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23960 if (EQ (prop, Qright_fringe))
23961 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23962 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23963 : window_box_right_offset (it->w, TEXT_AREA));
23964 if (EQ (prop, Qleft_margin))
23965 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23966 if (EQ (prop, Qright_margin))
23967 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23968 if (EQ (prop, Qscroll_bar))
23969 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23971 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23972 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23973 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23974 : 0)));
23976 else
23978 if (EQ (prop, Qleft_fringe))
23979 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23980 if (EQ (prop, Qright_fringe))
23981 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23982 if (EQ (prop, Qleft_margin))
23983 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23984 if (EQ (prop, Qright_margin))
23985 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23986 if (EQ (prop, Qscroll_bar))
23987 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23990 prop = buffer_local_value (prop, it->w->contents);
23991 if (EQ (prop, Qunbound))
23992 prop = Qnil;
23995 if (INTEGERP (prop) || FLOATP (prop))
23997 int base_unit = (width_p
23998 ? FRAME_COLUMN_WIDTH (it->f)
23999 : FRAME_LINE_HEIGHT (it->f));
24000 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24003 if (CONSP (prop))
24005 Lisp_Object car = XCAR (prop);
24006 Lisp_Object cdr = XCDR (prop);
24008 if (SYMBOLP (car))
24010 #ifdef HAVE_WINDOW_SYSTEM
24011 if (FRAME_WINDOW_P (it->f)
24012 && valid_image_p (prop))
24014 ptrdiff_t id = lookup_image (it->f, prop);
24015 struct image *img = IMAGE_FROM_ID (it->f, id);
24017 return OK_PIXELS (width_p ? img->width : img->height);
24019 #endif
24020 if (EQ (car, Qplus) || EQ (car, Qminus))
24022 int first = 1;
24023 double px;
24025 pixels = 0;
24026 while (CONSP (cdr))
24028 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24029 font, width_p, align_to))
24030 return 0;
24031 if (first)
24032 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24033 else
24034 pixels += px;
24035 cdr = XCDR (cdr);
24037 if (EQ (car, Qminus))
24038 pixels = -pixels;
24039 return OK_PIXELS (pixels);
24042 car = buffer_local_value (car, it->w->contents);
24043 if (EQ (car, Qunbound))
24044 car = Qnil;
24047 if (INTEGERP (car) || FLOATP (car))
24049 double fact;
24050 pixels = XFLOATINT (car);
24051 if (NILP (cdr))
24052 return OK_PIXELS (pixels);
24053 if (calc_pixel_width_or_height (&fact, it, cdr,
24054 font, width_p, align_to))
24055 return OK_PIXELS (pixels * fact);
24056 return 0;
24059 return 0;
24062 return 0;
24066 /***********************************************************************
24067 Glyph Display
24068 ***********************************************************************/
24070 #ifdef HAVE_WINDOW_SYSTEM
24072 #ifdef GLYPH_DEBUG
24074 void
24075 dump_glyph_string (struct glyph_string *s)
24077 fprintf (stderr, "glyph string\n");
24078 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24079 s->x, s->y, s->width, s->height);
24080 fprintf (stderr, " ybase = %d\n", s->ybase);
24081 fprintf (stderr, " hl = %d\n", s->hl);
24082 fprintf (stderr, " left overhang = %d, right = %d\n",
24083 s->left_overhang, s->right_overhang);
24084 fprintf (stderr, " nchars = %d\n", s->nchars);
24085 fprintf (stderr, " extends to end of line = %d\n",
24086 s->extends_to_end_of_line_p);
24087 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24088 fprintf (stderr, " bg width = %d\n", s->background_width);
24091 #endif /* GLYPH_DEBUG */
24093 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24094 of XChar2b structures for S; it can't be allocated in
24095 init_glyph_string because it must be allocated via `alloca'. W
24096 is the window on which S is drawn. ROW and AREA are the glyph row
24097 and area within the row from which S is constructed. START is the
24098 index of the first glyph structure covered by S. HL is a
24099 face-override for drawing S. */
24101 #ifdef HAVE_NTGUI
24102 #define OPTIONAL_HDC(hdc) HDC hdc,
24103 #define DECLARE_HDC(hdc) HDC hdc;
24104 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24105 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24106 #endif
24108 #ifndef OPTIONAL_HDC
24109 #define OPTIONAL_HDC(hdc)
24110 #define DECLARE_HDC(hdc)
24111 #define ALLOCATE_HDC(hdc, f)
24112 #define RELEASE_HDC(hdc, f)
24113 #endif
24115 static void
24116 init_glyph_string (struct glyph_string *s,
24117 OPTIONAL_HDC (hdc)
24118 XChar2b *char2b, struct window *w, struct glyph_row *row,
24119 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24121 memset (s, 0, sizeof *s);
24122 s->w = w;
24123 s->f = XFRAME (w->frame);
24124 #ifdef HAVE_NTGUI
24125 s->hdc = hdc;
24126 #endif
24127 s->display = FRAME_X_DISPLAY (s->f);
24128 s->window = FRAME_X_WINDOW (s->f);
24129 s->char2b = char2b;
24130 s->hl = hl;
24131 s->row = row;
24132 s->area = area;
24133 s->first_glyph = row->glyphs[area] + start;
24134 s->height = row->height;
24135 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24136 s->ybase = s->y + row->ascent;
24140 /* Append the list of glyph strings with head H and tail T to the list
24141 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24143 static void
24144 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24145 struct glyph_string *h, struct glyph_string *t)
24147 if (h)
24149 if (*head)
24150 (*tail)->next = h;
24151 else
24152 *head = h;
24153 h->prev = *tail;
24154 *tail = t;
24159 /* Prepend the list of glyph strings with head H and tail T to the
24160 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24161 result. */
24163 static void
24164 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24165 struct glyph_string *h, struct glyph_string *t)
24167 if (h)
24169 if (*head)
24170 (*head)->prev = t;
24171 else
24172 *tail = t;
24173 t->next = *head;
24174 *head = h;
24179 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24180 Set *HEAD and *TAIL to the resulting list. */
24182 static void
24183 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24184 struct glyph_string *s)
24186 s->next = s->prev = NULL;
24187 append_glyph_string_lists (head, tail, s, s);
24191 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24192 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24193 make sure that X resources for the face returned are allocated.
24194 Value is a pointer to a realized face that is ready for display if
24195 DISPLAY_P is non-zero. */
24197 static struct face *
24198 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24199 XChar2b *char2b, int display_p)
24201 struct face *face = FACE_FROM_ID (f, face_id);
24202 unsigned code = 0;
24204 if (face->font)
24206 code = face->font->driver->encode_char (face->font, c);
24208 if (code == FONT_INVALID_CODE)
24209 code = 0;
24211 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24213 /* Make sure X resources of the face are allocated. */
24214 #ifdef HAVE_X_WINDOWS
24215 if (display_p)
24216 #endif
24218 eassert (face != NULL);
24219 prepare_face_for_display (f, face);
24222 return face;
24226 /* Get face and two-byte form of character glyph GLYPH on frame F.
24227 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24228 a pointer to a realized face that is ready for display. */
24230 static struct face *
24231 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24232 XChar2b *char2b, int *two_byte_p)
24234 struct face *face;
24235 unsigned code = 0;
24237 eassert (glyph->type == CHAR_GLYPH);
24238 face = FACE_FROM_ID (f, glyph->face_id);
24240 /* Make sure X resources of the face are allocated. */
24241 eassert (face != NULL);
24242 prepare_face_for_display (f, face);
24244 if (two_byte_p)
24245 *two_byte_p = 0;
24247 if (face->font)
24249 if (CHAR_BYTE8_P (glyph->u.ch))
24250 code = CHAR_TO_BYTE8 (glyph->u.ch);
24251 else
24252 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24254 if (code == FONT_INVALID_CODE)
24255 code = 0;
24258 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24259 return face;
24263 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24264 Return 1 if FONT has a glyph for C, otherwise return 0. */
24266 static int
24267 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24269 unsigned code;
24271 if (CHAR_BYTE8_P (c))
24272 code = CHAR_TO_BYTE8 (c);
24273 else
24274 code = font->driver->encode_char (font, c);
24276 if (code == FONT_INVALID_CODE)
24277 return 0;
24278 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24279 return 1;
24283 /* Fill glyph string S with composition components specified by S->cmp.
24285 BASE_FACE is the base face of the composition.
24286 S->cmp_from is the index of the first component for S.
24288 OVERLAPS non-zero means S should draw the foreground only, and use
24289 its physical height for clipping. See also draw_glyphs.
24291 Value is the index of a component not in S. */
24293 static int
24294 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24295 int overlaps)
24297 int i;
24298 /* For all glyphs of this composition, starting at the offset
24299 S->cmp_from, until we reach the end of the definition or encounter a
24300 glyph that requires the different face, add it to S. */
24301 struct face *face;
24303 eassert (s);
24305 s->for_overlaps = overlaps;
24306 s->face = NULL;
24307 s->font = NULL;
24308 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24310 int c = COMPOSITION_GLYPH (s->cmp, i);
24312 /* TAB in a composition means display glyphs with padding space
24313 on the left or right. */
24314 if (c != '\t')
24316 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24317 -1, Qnil);
24319 face = get_char_face_and_encoding (s->f, c, face_id,
24320 s->char2b + i, 1);
24321 if (face)
24323 if (! s->face)
24325 s->face = face;
24326 s->font = s->face->font;
24328 else if (s->face != face)
24329 break;
24332 ++s->nchars;
24334 s->cmp_to = i;
24336 if (s->face == NULL)
24338 s->face = base_face->ascii_face;
24339 s->font = s->face->font;
24342 /* All glyph strings for the same composition has the same width,
24343 i.e. the width set for the first component of the composition. */
24344 s->width = s->first_glyph->pixel_width;
24346 /* If the specified font could not be loaded, use the frame's
24347 default font, but record the fact that we couldn't load it in
24348 the glyph string so that we can draw rectangles for the
24349 characters of the glyph string. */
24350 if (s->font == NULL)
24352 s->font_not_found_p = 1;
24353 s->font = FRAME_FONT (s->f);
24356 /* Adjust base line for subscript/superscript text. */
24357 s->ybase += s->first_glyph->voffset;
24359 /* This glyph string must always be drawn with 16-bit functions. */
24360 s->two_byte_p = 1;
24362 return s->cmp_to;
24365 static int
24366 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24367 int start, int end, int overlaps)
24369 struct glyph *glyph, *last;
24370 Lisp_Object lgstring;
24371 int i;
24373 s->for_overlaps = overlaps;
24374 glyph = s->row->glyphs[s->area] + start;
24375 last = s->row->glyphs[s->area] + end;
24376 s->cmp_id = glyph->u.cmp.id;
24377 s->cmp_from = glyph->slice.cmp.from;
24378 s->cmp_to = glyph->slice.cmp.to + 1;
24379 s->face = FACE_FROM_ID (s->f, face_id);
24380 lgstring = composition_gstring_from_id (s->cmp_id);
24381 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24382 glyph++;
24383 while (glyph < last
24384 && glyph->u.cmp.automatic
24385 && glyph->u.cmp.id == s->cmp_id
24386 && s->cmp_to == glyph->slice.cmp.from)
24387 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24389 for (i = s->cmp_from; i < s->cmp_to; i++)
24391 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24392 unsigned code = LGLYPH_CODE (lglyph);
24394 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24396 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24397 return glyph - s->row->glyphs[s->area];
24401 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24402 See the comment of fill_glyph_string for arguments.
24403 Value is the index of the first glyph not in S. */
24406 static int
24407 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24408 int start, int end, int overlaps)
24410 struct glyph *glyph, *last;
24411 int voffset;
24413 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24414 s->for_overlaps = overlaps;
24415 glyph = s->row->glyphs[s->area] + start;
24416 last = s->row->glyphs[s->area] + end;
24417 voffset = glyph->voffset;
24418 s->face = FACE_FROM_ID (s->f, face_id);
24419 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24420 s->nchars = 1;
24421 s->width = glyph->pixel_width;
24422 glyph++;
24423 while (glyph < last
24424 && glyph->type == GLYPHLESS_GLYPH
24425 && glyph->voffset == voffset
24426 && glyph->face_id == face_id)
24428 s->nchars++;
24429 s->width += glyph->pixel_width;
24430 glyph++;
24432 s->ybase += voffset;
24433 return glyph - s->row->glyphs[s->area];
24437 /* Fill glyph string S from a sequence of character glyphs.
24439 FACE_ID is the face id of the string. START is the index of the
24440 first glyph to consider, END is the index of the last + 1.
24441 OVERLAPS non-zero means S should draw the foreground only, and use
24442 its physical height for clipping. See also draw_glyphs.
24444 Value is the index of the first glyph not in S. */
24446 static int
24447 fill_glyph_string (struct glyph_string *s, int face_id,
24448 int start, int end, int overlaps)
24450 struct glyph *glyph, *last;
24451 int voffset;
24452 int glyph_not_available_p;
24454 eassert (s->f == XFRAME (s->w->frame));
24455 eassert (s->nchars == 0);
24456 eassert (start >= 0 && end > start);
24458 s->for_overlaps = overlaps;
24459 glyph = s->row->glyphs[s->area] + start;
24460 last = s->row->glyphs[s->area] + end;
24461 voffset = glyph->voffset;
24462 s->padding_p = glyph->padding_p;
24463 glyph_not_available_p = glyph->glyph_not_available_p;
24465 while (glyph < last
24466 && glyph->type == CHAR_GLYPH
24467 && glyph->voffset == voffset
24468 /* Same face id implies same font, nowadays. */
24469 && glyph->face_id == face_id
24470 && glyph->glyph_not_available_p == glyph_not_available_p)
24472 int two_byte_p;
24474 s->face = get_glyph_face_and_encoding (s->f, glyph,
24475 s->char2b + s->nchars,
24476 &two_byte_p);
24477 s->two_byte_p = two_byte_p;
24478 ++s->nchars;
24479 eassert (s->nchars <= end - start);
24480 s->width += glyph->pixel_width;
24481 if (glyph++->padding_p != s->padding_p)
24482 break;
24485 s->font = s->face->font;
24487 /* If the specified font could not be loaded, use the frame's font,
24488 but record the fact that we couldn't load it in
24489 S->font_not_found_p so that we can draw rectangles for the
24490 characters of the glyph string. */
24491 if (s->font == NULL || glyph_not_available_p)
24493 s->font_not_found_p = 1;
24494 s->font = FRAME_FONT (s->f);
24497 /* Adjust base line for subscript/superscript text. */
24498 s->ybase += voffset;
24500 eassert (s->face && s->face->gc);
24501 return glyph - s->row->glyphs[s->area];
24505 /* Fill glyph string S from image glyph S->first_glyph. */
24507 static void
24508 fill_image_glyph_string (struct glyph_string *s)
24510 eassert (s->first_glyph->type == IMAGE_GLYPH);
24511 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24512 eassert (s->img);
24513 s->slice = s->first_glyph->slice.img;
24514 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24515 s->font = s->face->font;
24516 s->width = s->first_glyph->pixel_width;
24518 /* Adjust base line for subscript/superscript text. */
24519 s->ybase += s->first_glyph->voffset;
24523 /* Fill glyph string S from a sequence of stretch glyphs.
24525 START is the index of the first glyph to consider,
24526 END is the index of the last + 1.
24528 Value is the index of the first glyph not in S. */
24530 static int
24531 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24533 struct glyph *glyph, *last;
24534 int voffset, face_id;
24536 eassert (s->first_glyph->type == STRETCH_GLYPH);
24538 glyph = s->row->glyphs[s->area] + start;
24539 last = s->row->glyphs[s->area] + end;
24540 face_id = glyph->face_id;
24541 s->face = FACE_FROM_ID (s->f, face_id);
24542 s->font = s->face->font;
24543 s->width = glyph->pixel_width;
24544 s->nchars = 1;
24545 voffset = glyph->voffset;
24547 for (++glyph;
24548 (glyph < last
24549 && glyph->type == STRETCH_GLYPH
24550 && glyph->voffset == voffset
24551 && glyph->face_id == face_id);
24552 ++glyph)
24553 s->width += glyph->pixel_width;
24555 /* Adjust base line for subscript/superscript text. */
24556 s->ybase += voffset;
24558 /* The case that face->gc == 0 is handled when drawing the glyph
24559 string by calling prepare_face_for_display. */
24560 eassert (s->face);
24561 return glyph - s->row->glyphs[s->area];
24564 static struct font_metrics *
24565 get_per_char_metric (struct font *font, XChar2b *char2b)
24567 static struct font_metrics metrics;
24568 unsigned code;
24570 if (! font)
24571 return NULL;
24572 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24573 if (code == FONT_INVALID_CODE)
24574 return NULL;
24575 font->driver->text_extents (font, &code, 1, &metrics);
24576 return &metrics;
24579 /* EXPORT for RIF:
24580 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24581 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24582 assumed to be zero. */
24584 void
24585 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24587 *left = *right = 0;
24589 if (glyph->type == CHAR_GLYPH)
24591 struct face *face;
24592 XChar2b char2b;
24593 struct font_metrics *pcm;
24595 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24596 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24598 if (pcm->rbearing > pcm->width)
24599 *right = pcm->rbearing - pcm->width;
24600 if (pcm->lbearing < 0)
24601 *left = -pcm->lbearing;
24604 else if (glyph->type == COMPOSITE_GLYPH)
24606 if (! glyph->u.cmp.automatic)
24608 struct composition *cmp = composition_table[glyph->u.cmp.id];
24610 if (cmp->rbearing > cmp->pixel_width)
24611 *right = cmp->rbearing - cmp->pixel_width;
24612 if (cmp->lbearing < 0)
24613 *left = - cmp->lbearing;
24615 else
24617 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24618 struct font_metrics metrics;
24620 composition_gstring_width (gstring, glyph->slice.cmp.from,
24621 glyph->slice.cmp.to + 1, &metrics);
24622 if (metrics.rbearing > metrics.width)
24623 *right = metrics.rbearing - metrics.width;
24624 if (metrics.lbearing < 0)
24625 *left = - metrics.lbearing;
24631 /* Return the index of the first glyph preceding glyph string S that
24632 is overwritten by S because of S's left overhang. Value is -1
24633 if no glyphs are overwritten. */
24635 static int
24636 left_overwritten (struct glyph_string *s)
24638 int k;
24640 if (s->left_overhang)
24642 int x = 0, i;
24643 struct glyph *glyphs = s->row->glyphs[s->area];
24644 int first = s->first_glyph - glyphs;
24646 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24647 x -= glyphs[i].pixel_width;
24649 k = i + 1;
24651 else
24652 k = -1;
24654 return k;
24658 /* Return the index of the first glyph preceding glyph string S that
24659 is overwriting S because of its right overhang. Value is -1 if no
24660 glyph in front of S overwrites S. */
24662 static int
24663 left_overwriting (struct glyph_string *s)
24665 int i, k, x;
24666 struct glyph *glyphs = s->row->glyphs[s->area];
24667 int first = s->first_glyph - glyphs;
24669 k = -1;
24670 x = 0;
24671 for (i = first - 1; i >= 0; --i)
24673 int left, right;
24674 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24675 if (x + right > 0)
24676 k = i;
24677 x -= glyphs[i].pixel_width;
24680 return k;
24684 /* Return the index of the last glyph following glyph string S that is
24685 overwritten by S because of S's right overhang. Value is -1 if
24686 no such glyph is found. */
24688 static int
24689 right_overwritten (struct glyph_string *s)
24691 int k = -1;
24693 if (s->right_overhang)
24695 int x = 0, i;
24696 struct glyph *glyphs = s->row->glyphs[s->area];
24697 int first = (s->first_glyph - glyphs
24698 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24699 int end = s->row->used[s->area];
24701 for (i = first; i < end && s->right_overhang > x; ++i)
24702 x += glyphs[i].pixel_width;
24704 k = i;
24707 return k;
24711 /* Return the index of the last glyph following glyph string S that
24712 overwrites S because of its left overhang. Value is negative
24713 if no such glyph is found. */
24715 static int
24716 right_overwriting (struct glyph_string *s)
24718 int i, k, x;
24719 int end = s->row->used[s->area];
24720 struct glyph *glyphs = s->row->glyphs[s->area];
24721 int first = (s->first_glyph - glyphs
24722 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24724 k = -1;
24725 x = 0;
24726 for (i = first; i < end; ++i)
24728 int left, right;
24729 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24730 if (x - left < 0)
24731 k = i;
24732 x += glyphs[i].pixel_width;
24735 return k;
24739 /* Set background width of glyph string S. START is the index of the
24740 first glyph following S. LAST_X is the right-most x-position + 1
24741 in the drawing area. */
24743 static void
24744 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24746 /* If the face of this glyph string has to be drawn to the end of
24747 the drawing area, set S->extends_to_end_of_line_p. */
24749 if (start == s->row->used[s->area]
24750 && ((s->row->fill_line_p
24751 && (s->hl == DRAW_NORMAL_TEXT
24752 || s->hl == DRAW_IMAGE_RAISED
24753 || s->hl == DRAW_IMAGE_SUNKEN))
24754 || s->hl == DRAW_MOUSE_FACE))
24755 s->extends_to_end_of_line_p = 1;
24757 /* If S extends its face to the end of the line, set its
24758 background_width to the distance to the right edge of the drawing
24759 area. */
24760 if (s->extends_to_end_of_line_p)
24761 s->background_width = last_x - s->x + 1;
24762 else
24763 s->background_width = s->width;
24767 /* Compute overhangs and x-positions for glyph string S and its
24768 predecessors, or successors. X is the starting x-position for S.
24769 BACKWARD_P non-zero means process predecessors. */
24771 static void
24772 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24774 if (backward_p)
24776 while (s)
24778 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24779 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24780 x -= s->width;
24781 s->x = x;
24782 s = s->prev;
24785 else
24787 while (s)
24789 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24790 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24791 s->x = x;
24792 x += s->width;
24793 s = s->next;
24800 /* The following macros are only called from draw_glyphs below.
24801 They reference the following parameters of that function directly:
24802 `w', `row', `area', and `overlap_p'
24803 as well as the following local variables:
24804 `s', `f', and `hdc' (in W32) */
24806 #ifdef HAVE_NTGUI
24807 /* On W32, silently add local `hdc' variable to argument list of
24808 init_glyph_string. */
24809 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24810 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24811 #else
24812 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24813 init_glyph_string (s, char2b, w, row, area, start, hl)
24814 #endif
24816 /* Add a glyph string for a stretch glyph to the list of strings
24817 between HEAD and TAIL. START is the index of the stretch glyph in
24818 row area AREA of glyph row ROW. END is the index of the last glyph
24819 in that glyph row area. X is the current output position assigned
24820 to the new glyph string constructed. HL overrides that face of the
24821 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24822 is the right-most x-position of the drawing area. */
24824 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24825 and below -- keep them on one line. */
24826 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24827 do \
24829 s = alloca (sizeof *s); \
24830 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24831 START = fill_stretch_glyph_string (s, START, END); \
24832 append_glyph_string (&HEAD, &TAIL, s); \
24833 s->x = (X); \
24835 while (0)
24838 /* Add a glyph string for an image glyph to the list of strings
24839 between HEAD and TAIL. START is the index of the image glyph in
24840 row area AREA of glyph row ROW. END is the index of the last glyph
24841 in that glyph row area. X is the current output position assigned
24842 to the new glyph string constructed. HL overrides that face of the
24843 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24844 is the right-most x-position of the drawing area. */
24846 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24847 do \
24849 s = alloca (sizeof *s); \
24850 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24851 fill_image_glyph_string (s); \
24852 append_glyph_string (&HEAD, &TAIL, s); \
24853 ++START; \
24854 s->x = (X); \
24856 while (0)
24859 /* Add a glyph string for a sequence of character glyphs to the list
24860 of strings between HEAD and TAIL. START is the index of the first
24861 glyph in row area AREA of glyph row ROW that is part of the new
24862 glyph string. END is the index of the last glyph in that glyph row
24863 area. X is the current output position assigned to the new glyph
24864 string constructed. HL overrides that face of the glyph; e.g. it
24865 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24866 right-most x-position of the drawing area. */
24868 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24869 do \
24871 int face_id; \
24872 XChar2b *char2b; \
24874 face_id = (row)->glyphs[area][START].face_id; \
24876 s = alloca (sizeof *s); \
24877 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24878 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24879 append_glyph_string (&HEAD, &TAIL, s); \
24880 s->x = (X); \
24881 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24883 while (0)
24886 /* Add a glyph string for a composite sequence to the list of strings
24887 between HEAD and TAIL. START is the index of the first glyph in
24888 row area AREA of glyph row ROW that is part of the new glyph
24889 string. END is the index of the last glyph in that glyph row area.
24890 X is the current output position assigned to the new glyph string
24891 constructed. HL overrides that face of the glyph; e.g. it is
24892 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24893 x-position of the drawing area. */
24895 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24896 do { \
24897 int face_id = (row)->glyphs[area][START].face_id; \
24898 struct face *base_face = FACE_FROM_ID (f, face_id); \
24899 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24900 struct composition *cmp = composition_table[cmp_id]; \
24901 XChar2b *char2b; \
24902 struct glyph_string *first_s = NULL; \
24903 int n; \
24905 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24907 /* Make glyph_strings for each glyph sequence that is drawable by \
24908 the same face, and append them to HEAD/TAIL. */ \
24909 for (n = 0; n < cmp->glyph_len;) \
24911 s = alloca (sizeof *s); \
24912 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24913 append_glyph_string (&(HEAD), &(TAIL), s); \
24914 s->cmp = cmp; \
24915 s->cmp_from = n; \
24916 s->x = (X); \
24917 if (n == 0) \
24918 first_s = s; \
24919 n = fill_composite_glyph_string (s, base_face, overlaps); \
24922 ++START; \
24923 s = first_s; \
24924 } while (0)
24927 /* Add a glyph string for a glyph-string sequence to the list of strings
24928 between HEAD and TAIL. */
24930 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24931 do { \
24932 int face_id; \
24933 XChar2b *char2b; \
24934 Lisp_Object gstring; \
24936 face_id = (row)->glyphs[area][START].face_id; \
24937 gstring = (composition_gstring_from_id \
24938 ((row)->glyphs[area][START].u.cmp.id)); \
24939 s = alloca (sizeof *s); \
24940 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24941 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24942 append_glyph_string (&(HEAD), &(TAIL), s); \
24943 s->x = (X); \
24944 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24945 } while (0)
24948 /* Add a glyph string for a sequence of glyphless character's glyphs
24949 to the list of strings between HEAD and TAIL. The meanings of
24950 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24952 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24953 do \
24955 int face_id; \
24957 face_id = (row)->glyphs[area][START].face_id; \
24959 s = alloca (sizeof *s); \
24960 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24961 append_glyph_string (&HEAD, &TAIL, s); \
24962 s->x = (X); \
24963 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24964 overlaps); \
24966 while (0)
24969 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24970 of AREA of glyph row ROW on window W between indices START and END.
24971 HL overrides the face for drawing glyph strings, e.g. it is
24972 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24973 x-positions of the drawing area.
24975 This is an ugly monster macro construct because we must use alloca
24976 to allocate glyph strings (because draw_glyphs can be called
24977 asynchronously). */
24979 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24980 do \
24982 HEAD = TAIL = NULL; \
24983 while (START < END) \
24985 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24986 switch (first_glyph->type) \
24988 case CHAR_GLYPH: \
24989 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24990 HL, X, LAST_X); \
24991 break; \
24993 case COMPOSITE_GLYPH: \
24994 if (first_glyph->u.cmp.automatic) \
24995 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24996 HL, X, LAST_X); \
24997 else \
24998 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24999 HL, X, LAST_X); \
25000 break; \
25002 case STRETCH_GLYPH: \
25003 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25004 HL, X, LAST_X); \
25005 break; \
25007 case IMAGE_GLYPH: \
25008 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25009 HL, X, LAST_X); \
25010 break; \
25012 case GLYPHLESS_GLYPH: \
25013 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25014 HL, X, LAST_X); \
25015 break; \
25017 default: \
25018 emacs_abort (); \
25021 if (s) \
25023 set_glyph_string_background_width (s, START, LAST_X); \
25024 (X) += s->width; \
25027 } while (0)
25030 /* Draw glyphs between START and END in AREA of ROW on window W,
25031 starting at x-position X. X is relative to AREA in W. HL is a
25032 face-override with the following meaning:
25034 DRAW_NORMAL_TEXT draw normally
25035 DRAW_CURSOR draw in cursor face
25036 DRAW_MOUSE_FACE draw in mouse face.
25037 DRAW_INVERSE_VIDEO draw in mode line face
25038 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25039 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25041 If OVERLAPS is non-zero, draw only the foreground of characters and
25042 clip to the physical height of ROW. Non-zero value also defines
25043 the overlapping part to be drawn:
25045 OVERLAPS_PRED overlap with preceding rows
25046 OVERLAPS_SUCC overlap with succeeding rows
25047 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25048 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25050 Value is the x-position reached, relative to AREA of W. */
25052 static int
25053 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25054 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25055 enum draw_glyphs_face hl, int overlaps)
25057 struct glyph_string *head, *tail;
25058 struct glyph_string *s;
25059 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25060 int i, j, x_reached, last_x, area_left = 0;
25061 struct frame *f = XFRAME (WINDOW_FRAME (w));
25062 DECLARE_HDC (hdc);
25064 ALLOCATE_HDC (hdc, f);
25066 /* Let's rather be paranoid than getting a SEGV. */
25067 end = min (end, row->used[area]);
25068 start = clip_to_bounds (0, start, end);
25070 /* Translate X to frame coordinates. Set last_x to the right
25071 end of the drawing area. */
25072 if (row->full_width_p)
25074 /* X is relative to the left edge of W, without scroll bars
25075 or fringes. */
25076 area_left = WINDOW_LEFT_EDGE_X (w);
25077 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25078 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25080 else
25082 area_left = window_box_left (w, area);
25083 last_x = area_left + window_box_width (w, area);
25085 x += area_left;
25087 /* Build a doubly-linked list of glyph_string structures between
25088 head and tail from what we have to draw. Note that the macro
25089 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25090 the reason we use a separate variable `i'. */
25091 i = start;
25092 USE_SAFE_ALLOCA;
25093 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25094 if (tail)
25095 x_reached = tail->x + tail->background_width;
25096 else
25097 x_reached = x;
25099 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25100 the row, redraw some glyphs in front or following the glyph
25101 strings built above. */
25102 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25104 struct glyph_string *h, *t;
25105 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25106 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25107 int check_mouse_face = 0;
25108 int dummy_x = 0;
25110 /* If mouse highlighting is on, we may need to draw adjacent
25111 glyphs using mouse-face highlighting. */
25112 if (area == TEXT_AREA && row->mouse_face_p
25113 && hlinfo->mouse_face_beg_row >= 0
25114 && hlinfo->mouse_face_end_row >= 0)
25116 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25118 if (row_vpos >= hlinfo->mouse_face_beg_row
25119 && row_vpos <= hlinfo->mouse_face_end_row)
25121 check_mouse_face = 1;
25122 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25123 ? hlinfo->mouse_face_beg_col : 0;
25124 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25125 ? hlinfo->mouse_face_end_col
25126 : row->used[TEXT_AREA];
25130 /* Compute overhangs for all glyph strings. */
25131 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25132 for (s = head; s; s = s->next)
25133 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25135 /* Prepend glyph strings for glyphs in front of the first glyph
25136 string that are overwritten because of the first glyph
25137 string's left overhang. The background of all strings
25138 prepended must be drawn because the first glyph string
25139 draws over it. */
25140 i = left_overwritten (head);
25141 if (i >= 0)
25143 enum draw_glyphs_face overlap_hl;
25145 /* If this row contains mouse highlighting, attempt to draw
25146 the overlapped glyphs with the correct highlight. This
25147 code fails if the overlap encompasses more than one glyph
25148 and mouse-highlight spans only some of these glyphs.
25149 However, making it work perfectly involves a lot more
25150 code, and I don't know if the pathological case occurs in
25151 practice, so we'll stick to this for now. --- cyd */
25152 if (check_mouse_face
25153 && mouse_beg_col < start && mouse_end_col > i)
25154 overlap_hl = DRAW_MOUSE_FACE;
25155 else
25156 overlap_hl = DRAW_NORMAL_TEXT;
25158 if (hl != overlap_hl)
25159 clip_head = head;
25160 j = i;
25161 BUILD_GLYPH_STRINGS (j, start, h, t,
25162 overlap_hl, dummy_x, last_x);
25163 start = i;
25164 compute_overhangs_and_x (t, head->x, 1);
25165 prepend_glyph_string_lists (&head, &tail, h, t);
25166 if (clip_head == NULL)
25167 clip_head = head;
25170 /* Prepend glyph strings for glyphs in front of the first glyph
25171 string that overwrite that glyph string because of their
25172 right overhang. For these strings, only the foreground must
25173 be drawn, because it draws over the glyph string at `head'.
25174 The background must not be drawn because this would overwrite
25175 right overhangs of preceding glyphs for which no glyph
25176 strings exist. */
25177 i = left_overwriting (head);
25178 if (i >= 0)
25180 enum draw_glyphs_face overlap_hl;
25182 if (check_mouse_face
25183 && mouse_beg_col < start && mouse_end_col > i)
25184 overlap_hl = DRAW_MOUSE_FACE;
25185 else
25186 overlap_hl = DRAW_NORMAL_TEXT;
25188 if (hl == overlap_hl || clip_head == NULL)
25189 clip_head = head;
25190 BUILD_GLYPH_STRINGS (i, start, h, t,
25191 overlap_hl, dummy_x, last_x);
25192 for (s = h; s; s = s->next)
25193 s->background_filled_p = 1;
25194 compute_overhangs_and_x (t, head->x, 1);
25195 prepend_glyph_string_lists (&head, &tail, h, t);
25198 /* Append glyphs strings for glyphs following the last glyph
25199 string tail that are overwritten by tail. The background of
25200 these strings has to be drawn because tail's foreground draws
25201 over it. */
25202 i = right_overwritten (tail);
25203 if (i >= 0)
25205 enum draw_glyphs_face overlap_hl;
25207 if (check_mouse_face
25208 && mouse_beg_col < i && mouse_end_col > end)
25209 overlap_hl = DRAW_MOUSE_FACE;
25210 else
25211 overlap_hl = DRAW_NORMAL_TEXT;
25213 if (hl != overlap_hl)
25214 clip_tail = tail;
25215 BUILD_GLYPH_STRINGS (end, i, h, t,
25216 overlap_hl, x, last_x);
25217 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25218 we don't have `end = i;' here. */
25219 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25220 append_glyph_string_lists (&head, &tail, h, t);
25221 if (clip_tail == NULL)
25222 clip_tail = tail;
25225 /* Append glyph strings for glyphs following the last glyph
25226 string tail that overwrite tail. The foreground of such
25227 glyphs has to be drawn because it writes into the background
25228 of tail. The background must not be drawn because it could
25229 paint over the foreground of following glyphs. */
25230 i = right_overwriting (tail);
25231 if (i >= 0)
25233 enum draw_glyphs_face overlap_hl;
25234 if (check_mouse_face
25235 && mouse_beg_col < i && mouse_end_col > end)
25236 overlap_hl = DRAW_MOUSE_FACE;
25237 else
25238 overlap_hl = DRAW_NORMAL_TEXT;
25240 if (hl == overlap_hl || clip_tail == NULL)
25241 clip_tail = tail;
25242 i++; /* We must include the Ith glyph. */
25243 BUILD_GLYPH_STRINGS (end, i, h, t,
25244 overlap_hl, x, last_x);
25245 for (s = h; s; s = s->next)
25246 s->background_filled_p = 1;
25247 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25248 append_glyph_string_lists (&head, &tail, h, t);
25250 if (clip_head || clip_tail)
25251 for (s = head; s; s = s->next)
25253 s->clip_head = clip_head;
25254 s->clip_tail = clip_tail;
25258 /* Draw all strings. */
25259 for (s = head; s; s = s->next)
25260 FRAME_RIF (f)->draw_glyph_string (s);
25262 #ifndef HAVE_NS
25263 /* When focus a sole frame and move horizontally, this sets on_p to 0
25264 causing a failure to erase prev cursor position. */
25265 if (area == TEXT_AREA
25266 && !row->full_width_p
25267 /* When drawing overlapping rows, only the glyph strings'
25268 foreground is drawn, which doesn't erase a cursor
25269 completely. */
25270 && !overlaps)
25272 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25273 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25274 : (tail ? tail->x + tail->background_width : x));
25275 x0 -= area_left;
25276 x1 -= area_left;
25278 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25279 row->y, MATRIX_ROW_BOTTOM_Y (row));
25281 #endif
25283 /* Value is the x-position up to which drawn, relative to AREA of W.
25284 This doesn't include parts drawn because of overhangs. */
25285 if (row->full_width_p)
25286 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25287 else
25288 x_reached -= area_left;
25290 RELEASE_HDC (hdc, f);
25292 SAFE_FREE ();
25293 return x_reached;
25296 /* Expand row matrix if too narrow. Don't expand if area
25297 is not present. */
25299 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25301 if (!it->f->fonts_changed \
25302 && (it->glyph_row->glyphs[area] \
25303 < it->glyph_row->glyphs[area + 1])) \
25305 it->w->ncols_scale_factor++; \
25306 it->f->fonts_changed = 1; \
25310 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25311 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25313 static void
25314 append_glyph (struct it *it)
25316 struct glyph *glyph;
25317 enum glyph_row_area area = it->area;
25319 eassert (it->glyph_row);
25320 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25322 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25323 if (glyph < it->glyph_row->glyphs[area + 1])
25325 /* If the glyph row is reversed, we need to prepend the glyph
25326 rather than append it. */
25327 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25329 struct glyph *g;
25331 /* Make room for the additional glyph. */
25332 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25333 g[1] = *g;
25334 glyph = it->glyph_row->glyphs[area];
25336 glyph->charpos = CHARPOS (it->position);
25337 glyph->object = it->object;
25338 if (it->pixel_width > 0)
25340 glyph->pixel_width = it->pixel_width;
25341 glyph->padding_p = 0;
25343 else
25345 /* Assure at least 1-pixel width. Otherwise, cursor can't
25346 be displayed correctly. */
25347 glyph->pixel_width = 1;
25348 glyph->padding_p = 1;
25350 glyph->ascent = it->ascent;
25351 glyph->descent = it->descent;
25352 glyph->voffset = it->voffset;
25353 glyph->type = CHAR_GLYPH;
25354 glyph->avoid_cursor_p = it->avoid_cursor_p;
25355 glyph->multibyte_p = it->multibyte_p;
25356 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25358 /* In R2L rows, the left and the right box edges need to be
25359 drawn in reverse direction. */
25360 glyph->right_box_line_p = it->start_of_box_run_p;
25361 glyph->left_box_line_p = it->end_of_box_run_p;
25363 else
25365 glyph->left_box_line_p = it->start_of_box_run_p;
25366 glyph->right_box_line_p = it->end_of_box_run_p;
25368 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25369 || it->phys_descent > it->descent);
25370 glyph->glyph_not_available_p = it->glyph_not_available_p;
25371 glyph->face_id = it->face_id;
25372 glyph->u.ch = it->char_to_display;
25373 glyph->slice.img = null_glyph_slice;
25374 glyph->font_type = FONT_TYPE_UNKNOWN;
25375 if (it->bidi_p)
25377 glyph->resolved_level = it->bidi_it.resolved_level;
25378 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25379 glyph->bidi_type = it->bidi_it.type;
25381 else
25383 glyph->resolved_level = 0;
25384 glyph->bidi_type = UNKNOWN_BT;
25386 ++it->glyph_row->used[area];
25388 else
25389 IT_EXPAND_MATRIX_WIDTH (it, area);
25392 /* Store one glyph for the composition IT->cmp_it.id in
25393 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25394 non-null. */
25396 static void
25397 append_composite_glyph (struct it *it)
25399 struct glyph *glyph;
25400 enum glyph_row_area area = it->area;
25402 eassert (it->glyph_row);
25404 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25405 if (glyph < it->glyph_row->glyphs[area + 1])
25407 /* If the glyph row is reversed, we need to prepend the glyph
25408 rather than append it. */
25409 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25411 struct glyph *g;
25413 /* Make room for the new glyph. */
25414 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25415 g[1] = *g;
25416 glyph = it->glyph_row->glyphs[it->area];
25418 glyph->charpos = it->cmp_it.charpos;
25419 glyph->object = it->object;
25420 glyph->pixel_width = it->pixel_width;
25421 glyph->ascent = it->ascent;
25422 glyph->descent = it->descent;
25423 glyph->voffset = it->voffset;
25424 glyph->type = COMPOSITE_GLYPH;
25425 if (it->cmp_it.ch < 0)
25427 glyph->u.cmp.automatic = 0;
25428 glyph->u.cmp.id = it->cmp_it.id;
25429 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25431 else
25433 glyph->u.cmp.automatic = 1;
25434 glyph->u.cmp.id = it->cmp_it.id;
25435 glyph->slice.cmp.from = it->cmp_it.from;
25436 glyph->slice.cmp.to = it->cmp_it.to - 1;
25438 glyph->avoid_cursor_p = it->avoid_cursor_p;
25439 glyph->multibyte_p = it->multibyte_p;
25440 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25442 /* In R2L rows, the left and the right box edges need to be
25443 drawn in reverse direction. */
25444 glyph->right_box_line_p = it->start_of_box_run_p;
25445 glyph->left_box_line_p = it->end_of_box_run_p;
25447 else
25449 glyph->left_box_line_p = it->start_of_box_run_p;
25450 glyph->right_box_line_p = it->end_of_box_run_p;
25452 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25453 || it->phys_descent > it->descent);
25454 glyph->padding_p = 0;
25455 glyph->glyph_not_available_p = 0;
25456 glyph->face_id = it->face_id;
25457 glyph->font_type = FONT_TYPE_UNKNOWN;
25458 if (it->bidi_p)
25460 glyph->resolved_level = it->bidi_it.resolved_level;
25461 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25462 glyph->bidi_type = it->bidi_it.type;
25464 ++it->glyph_row->used[area];
25466 else
25467 IT_EXPAND_MATRIX_WIDTH (it, area);
25471 /* Change IT->ascent and IT->height according to the setting of
25472 IT->voffset. */
25474 static void
25475 take_vertical_position_into_account (struct it *it)
25477 if (it->voffset)
25479 if (it->voffset < 0)
25480 /* Increase the ascent so that we can display the text higher
25481 in the line. */
25482 it->ascent -= it->voffset;
25483 else
25484 /* Increase the descent so that we can display the text lower
25485 in the line. */
25486 it->descent += it->voffset;
25491 /* Produce glyphs/get display metrics for the image IT is loaded with.
25492 See the description of struct display_iterator in dispextern.h for
25493 an overview of struct display_iterator. */
25495 static void
25496 produce_image_glyph (struct it *it)
25498 struct image *img;
25499 struct face *face;
25500 int glyph_ascent, crop;
25501 struct glyph_slice slice;
25503 eassert (it->what == IT_IMAGE);
25505 face = FACE_FROM_ID (it->f, it->face_id);
25506 eassert (face);
25507 /* Make sure X resources of the face is loaded. */
25508 prepare_face_for_display (it->f, face);
25510 if (it->image_id < 0)
25512 /* Fringe bitmap. */
25513 it->ascent = it->phys_ascent = 0;
25514 it->descent = it->phys_descent = 0;
25515 it->pixel_width = 0;
25516 it->nglyphs = 0;
25517 return;
25520 img = IMAGE_FROM_ID (it->f, it->image_id);
25521 eassert (img);
25522 /* Make sure X resources of the image is loaded. */
25523 prepare_image_for_display (it->f, img);
25525 slice.x = slice.y = 0;
25526 slice.width = img->width;
25527 slice.height = img->height;
25529 if (INTEGERP (it->slice.x))
25530 slice.x = XINT (it->slice.x);
25531 else if (FLOATP (it->slice.x))
25532 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25534 if (INTEGERP (it->slice.y))
25535 slice.y = XINT (it->slice.y);
25536 else if (FLOATP (it->slice.y))
25537 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25539 if (INTEGERP (it->slice.width))
25540 slice.width = XINT (it->slice.width);
25541 else if (FLOATP (it->slice.width))
25542 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25544 if (INTEGERP (it->slice.height))
25545 slice.height = XINT (it->slice.height);
25546 else if (FLOATP (it->slice.height))
25547 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25549 if (slice.x >= img->width)
25550 slice.x = img->width;
25551 if (slice.y >= img->height)
25552 slice.y = img->height;
25553 if (slice.x + slice.width >= img->width)
25554 slice.width = img->width - slice.x;
25555 if (slice.y + slice.height > img->height)
25556 slice.height = img->height - slice.y;
25558 if (slice.width == 0 || slice.height == 0)
25559 return;
25561 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25563 it->descent = slice.height - glyph_ascent;
25564 if (slice.y == 0)
25565 it->descent += img->vmargin;
25566 if (slice.y + slice.height == img->height)
25567 it->descent += img->vmargin;
25568 it->phys_descent = it->descent;
25570 it->pixel_width = slice.width;
25571 if (slice.x == 0)
25572 it->pixel_width += img->hmargin;
25573 if (slice.x + slice.width == img->width)
25574 it->pixel_width += img->hmargin;
25576 /* It's quite possible for images to have an ascent greater than
25577 their height, so don't get confused in that case. */
25578 if (it->descent < 0)
25579 it->descent = 0;
25581 it->nglyphs = 1;
25583 if (face->box != FACE_NO_BOX)
25585 if (face->box_line_width > 0)
25587 if (slice.y == 0)
25588 it->ascent += face->box_line_width;
25589 if (slice.y + slice.height == img->height)
25590 it->descent += face->box_line_width;
25593 if (it->start_of_box_run_p && slice.x == 0)
25594 it->pixel_width += eabs (face->box_line_width);
25595 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25596 it->pixel_width += eabs (face->box_line_width);
25599 take_vertical_position_into_account (it);
25601 /* Automatically crop wide image glyphs at right edge so we can
25602 draw the cursor on same display row. */
25603 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25604 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25606 it->pixel_width -= crop;
25607 slice.width -= crop;
25610 if (it->glyph_row)
25612 struct glyph *glyph;
25613 enum glyph_row_area area = it->area;
25615 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25616 if (glyph < it->glyph_row->glyphs[area + 1])
25618 glyph->charpos = CHARPOS (it->position);
25619 glyph->object = it->object;
25620 glyph->pixel_width = it->pixel_width;
25621 glyph->ascent = glyph_ascent;
25622 glyph->descent = it->descent;
25623 glyph->voffset = it->voffset;
25624 glyph->type = IMAGE_GLYPH;
25625 glyph->avoid_cursor_p = it->avoid_cursor_p;
25626 glyph->multibyte_p = it->multibyte_p;
25627 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25629 /* In R2L rows, the left and the right box edges need to be
25630 drawn in reverse direction. */
25631 glyph->right_box_line_p = it->start_of_box_run_p;
25632 glyph->left_box_line_p = it->end_of_box_run_p;
25634 else
25636 glyph->left_box_line_p = it->start_of_box_run_p;
25637 glyph->right_box_line_p = it->end_of_box_run_p;
25639 glyph->overlaps_vertically_p = 0;
25640 glyph->padding_p = 0;
25641 glyph->glyph_not_available_p = 0;
25642 glyph->face_id = it->face_id;
25643 glyph->u.img_id = img->id;
25644 glyph->slice.img = slice;
25645 glyph->font_type = FONT_TYPE_UNKNOWN;
25646 if (it->bidi_p)
25648 glyph->resolved_level = it->bidi_it.resolved_level;
25649 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25650 glyph->bidi_type = it->bidi_it.type;
25652 ++it->glyph_row->used[area];
25654 else
25655 IT_EXPAND_MATRIX_WIDTH (it, area);
25660 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25661 of the glyph, WIDTH and HEIGHT are the width and height of the
25662 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25664 static void
25665 append_stretch_glyph (struct it *it, Lisp_Object object,
25666 int width, int height, int ascent)
25668 struct glyph *glyph;
25669 enum glyph_row_area area = it->area;
25671 eassert (ascent >= 0 && ascent <= height);
25673 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25674 if (glyph < it->glyph_row->glyphs[area + 1])
25676 /* If the glyph row is reversed, we need to prepend the glyph
25677 rather than append it. */
25678 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25680 struct glyph *g;
25682 /* Make room for the additional glyph. */
25683 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25684 g[1] = *g;
25685 glyph = it->glyph_row->glyphs[area];
25687 /* Decrease the width of the first glyph of the row that
25688 begins before first_visible_x (e.g., due to hscroll).
25689 This is so the overall width of the row becomes smaller
25690 by the scroll amount, and the stretch glyph appended by
25691 extend_face_to_end_of_line will be wider, to shift the
25692 row glyphs to the right. (In L2R rows, the corresponding
25693 left-shift effect is accomplished by setting row->x to a
25694 negative value, which won't work with R2L rows.)
25696 This must leave us with a positive value of WIDTH, since
25697 otherwise the call to move_it_in_display_line_to at the
25698 beginning of display_line would have got past the entire
25699 first glyph, and then it->current_x would have been
25700 greater or equal to it->first_visible_x. */
25701 if (it->current_x < it->first_visible_x)
25702 width -= it->first_visible_x - it->current_x;
25703 eassert (width > 0);
25705 glyph->charpos = CHARPOS (it->position);
25706 glyph->object = object;
25707 glyph->pixel_width = width;
25708 glyph->ascent = ascent;
25709 glyph->descent = height - ascent;
25710 glyph->voffset = it->voffset;
25711 glyph->type = STRETCH_GLYPH;
25712 glyph->avoid_cursor_p = it->avoid_cursor_p;
25713 glyph->multibyte_p = it->multibyte_p;
25714 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25716 /* In R2L rows, the left and the right box edges need to be
25717 drawn in reverse direction. */
25718 glyph->right_box_line_p = it->start_of_box_run_p;
25719 glyph->left_box_line_p = it->end_of_box_run_p;
25721 else
25723 glyph->left_box_line_p = it->start_of_box_run_p;
25724 glyph->right_box_line_p = it->end_of_box_run_p;
25726 glyph->overlaps_vertically_p = 0;
25727 glyph->padding_p = 0;
25728 glyph->glyph_not_available_p = 0;
25729 glyph->face_id = it->face_id;
25730 glyph->u.stretch.ascent = ascent;
25731 glyph->u.stretch.height = height;
25732 glyph->slice.img = null_glyph_slice;
25733 glyph->font_type = FONT_TYPE_UNKNOWN;
25734 if (it->bidi_p)
25736 glyph->resolved_level = it->bidi_it.resolved_level;
25737 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25738 glyph->bidi_type = it->bidi_it.type;
25740 else
25742 glyph->resolved_level = 0;
25743 glyph->bidi_type = UNKNOWN_BT;
25745 ++it->glyph_row->used[area];
25747 else
25748 IT_EXPAND_MATRIX_WIDTH (it, area);
25751 #endif /* HAVE_WINDOW_SYSTEM */
25753 /* Produce a stretch glyph for iterator IT. IT->object is the value
25754 of the glyph property displayed. The value must be a list
25755 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25756 being recognized:
25758 1. `:width WIDTH' specifies that the space should be WIDTH *
25759 canonical char width wide. WIDTH may be an integer or floating
25760 point number.
25762 2. `:relative-width FACTOR' specifies that the width of the stretch
25763 should be computed from the width of the first character having the
25764 `glyph' property, and should be FACTOR times that width.
25766 3. `:align-to HPOS' specifies that the space should be wide enough
25767 to reach HPOS, a value in canonical character units.
25769 Exactly one of the above pairs must be present.
25771 4. `:height HEIGHT' specifies that the height of the stretch produced
25772 should be HEIGHT, measured in canonical character units.
25774 5. `:relative-height FACTOR' specifies that the height of the
25775 stretch should be FACTOR times the height of the characters having
25776 the glyph property.
25778 Either none or exactly one of 4 or 5 must be present.
25780 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25781 of the stretch should be used for the ascent of the stretch.
25782 ASCENT must be in the range 0 <= ASCENT <= 100. */
25784 void
25785 produce_stretch_glyph (struct it *it)
25787 /* (space :width WIDTH :height HEIGHT ...) */
25788 Lisp_Object prop, plist;
25789 int width = 0, height = 0, align_to = -1;
25790 int zero_width_ok_p = 0;
25791 double tem;
25792 struct font *font = NULL;
25794 #ifdef HAVE_WINDOW_SYSTEM
25795 int ascent = 0;
25796 int zero_height_ok_p = 0;
25798 if (FRAME_WINDOW_P (it->f))
25800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25801 font = face->font ? face->font : FRAME_FONT (it->f);
25802 prepare_face_for_display (it->f, face);
25804 #endif
25806 /* List should start with `space'. */
25807 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25808 plist = XCDR (it->object);
25810 /* Compute the width of the stretch. */
25811 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25812 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25814 /* Absolute width `:width WIDTH' specified and valid. */
25815 zero_width_ok_p = 1;
25816 width = (int)tem;
25818 #ifdef HAVE_WINDOW_SYSTEM
25819 else if (FRAME_WINDOW_P (it->f)
25820 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25822 /* Relative width `:relative-width FACTOR' specified and valid.
25823 Compute the width of the characters having the `glyph'
25824 property. */
25825 struct it it2;
25826 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25828 it2 = *it;
25829 if (it->multibyte_p)
25830 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25831 else
25833 it2.c = it2.char_to_display = *p, it2.len = 1;
25834 if (! ASCII_CHAR_P (it2.c))
25835 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25838 it2.glyph_row = NULL;
25839 it2.what = IT_CHARACTER;
25840 x_produce_glyphs (&it2);
25841 width = NUMVAL (prop) * it2.pixel_width;
25843 #endif /* HAVE_WINDOW_SYSTEM */
25844 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25845 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25847 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25848 align_to = (align_to < 0
25850 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25851 else if (align_to < 0)
25852 align_to = window_box_left_offset (it->w, TEXT_AREA);
25853 width = max (0, (int)tem + align_to - it->current_x);
25854 zero_width_ok_p = 1;
25856 else
25857 /* Nothing specified -> width defaults to canonical char width. */
25858 width = FRAME_COLUMN_WIDTH (it->f);
25860 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25861 width = 1;
25863 #ifdef HAVE_WINDOW_SYSTEM
25864 /* Compute height. */
25865 if (FRAME_WINDOW_P (it->f))
25867 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25868 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25870 height = (int)tem;
25871 zero_height_ok_p = 1;
25873 else if (prop = Fplist_get (plist, QCrelative_height),
25874 NUMVAL (prop) > 0)
25875 height = FONT_HEIGHT (font) * NUMVAL (prop);
25876 else
25877 height = FONT_HEIGHT (font);
25879 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25880 height = 1;
25882 /* Compute percentage of height used for ascent. If
25883 `:ascent ASCENT' is present and valid, use that. Otherwise,
25884 derive the ascent from the font in use. */
25885 if (prop = Fplist_get (plist, QCascent),
25886 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25887 ascent = height * NUMVAL (prop) / 100.0;
25888 else if (!NILP (prop)
25889 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25890 ascent = min (max (0, (int)tem), height);
25891 else
25892 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25894 else
25895 #endif /* HAVE_WINDOW_SYSTEM */
25896 height = 1;
25898 if (width > 0 && it->line_wrap != TRUNCATE
25899 && it->current_x + width > it->last_visible_x)
25901 width = it->last_visible_x - it->current_x;
25902 #ifdef HAVE_WINDOW_SYSTEM
25903 /* Subtract one more pixel from the stretch width, but only on
25904 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25905 width -= FRAME_WINDOW_P (it->f);
25906 #endif
25909 if (width > 0 && height > 0 && it->glyph_row)
25911 Lisp_Object o_object = it->object;
25912 Lisp_Object object = it->stack[it->sp - 1].string;
25913 int n = width;
25915 if (!STRINGP (object))
25916 object = it->w->contents;
25917 #ifdef HAVE_WINDOW_SYSTEM
25918 if (FRAME_WINDOW_P (it->f))
25919 append_stretch_glyph (it, object, width, height, ascent);
25920 else
25921 #endif
25923 it->object = object;
25924 it->char_to_display = ' ';
25925 it->pixel_width = it->len = 1;
25926 while (n--)
25927 tty_append_glyph (it);
25928 it->object = o_object;
25932 it->pixel_width = width;
25933 #ifdef HAVE_WINDOW_SYSTEM
25934 if (FRAME_WINDOW_P (it->f))
25936 it->ascent = it->phys_ascent = ascent;
25937 it->descent = it->phys_descent = height - it->ascent;
25938 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25939 take_vertical_position_into_account (it);
25941 else
25942 #endif
25943 it->nglyphs = width;
25946 /* Get information about special display element WHAT in an
25947 environment described by IT. WHAT is one of IT_TRUNCATION or
25948 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25949 non-null glyph_row member. This function ensures that fields like
25950 face_id, c, len of IT are left untouched. */
25952 static void
25953 produce_special_glyphs (struct it *it, enum display_element_type what)
25955 struct it temp_it;
25956 Lisp_Object gc;
25957 GLYPH glyph;
25959 temp_it = *it;
25960 temp_it.object = Qnil;
25961 memset (&temp_it.current, 0, sizeof temp_it.current);
25963 if (what == IT_CONTINUATION)
25965 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25966 if (it->bidi_it.paragraph_dir == R2L)
25967 SET_GLYPH_FROM_CHAR (glyph, '/');
25968 else
25969 SET_GLYPH_FROM_CHAR (glyph, '\\');
25970 if (it->dp
25971 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25973 /* FIXME: Should we mirror GC for R2L lines? */
25974 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25975 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25978 else if (what == IT_TRUNCATION)
25980 /* Truncation glyph. */
25981 SET_GLYPH_FROM_CHAR (glyph, '$');
25982 if (it->dp
25983 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25985 /* FIXME: Should we mirror GC for R2L lines? */
25986 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25987 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25990 else
25991 emacs_abort ();
25993 #ifdef HAVE_WINDOW_SYSTEM
25994 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25995 is turned off, we precede the truncation/continuation glyphs by a
25996 stretch glyph whose width is computed such that these special
25997 glyphs are aligned at the window margin, even when very different
25998 fonts are used in different glyph rows. */
25999 if (FRAME_WINDOW_P (temp_it.f)
26000 /* init_iterator calls this with it->glyph_row == NULL, and it
26001 wants only the pixel width of the truncation/continuation
26002 glyphs. */
26003 && temp_it.glyph_row
26004 /* insert_left_trunc_glyphs calls us at the beginning of the
26005 row, and it has its own calculation of the stretch glyph
26006 width. */
26007 && temp_it.glyph_row->used[TEXT_AREA] > 0
26008 && (temp_it.glyph_row->reversed_p
26009 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26010 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26012 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26014 if (stretch_width > 0)
26016 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26017 struct font *font =
26018 face->font ? face->font : FRAME_FONT (temp_it.f);
26019 int stretch_ascent =
26020 (((temp_it.ascent + temp_it.descent)
26021 * FONT_BASE (font)) / FONT_HEIGHT (font));
26023 append_stretch_glyph (&temp_it, Qnil, stretch_width,
26024 temp_it.ascent + temp_it.descent,
26025 stretch_ascent);
26028 #endif
26030 temp_it.dp = NULL;
26031 temp_it.what = IT_CHARACTER;
26032 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26033 temp_it.face_id = GLYPH_FACE (glyph);
26034 temp_it.len = CHAR_BYTES (temp_it.c);
26036 PRODUCE_GLYPHS (&temp_it);
26037 it->pixel_width = temp_it.pixel_width;
26038 it->nglyphs = temp_it.nglyphs;
26041 #ifdef HAVE_WINDOW_SYSTEM
26043 /* Calculate line-height and line-spacing properties.
26044 An integer value specifies explicit pixel value.
26045 A float value specifies relative value to current face height.
26046 A cons (float . face-name) specifies relative value to
26047 height of specified face font.
26049 Returns height in pixels, or nil. */
26052 static Lisp_Object
26053 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26054 int boff, int override)
26056 Lisp_Object face_name = Qnil;
26057 int ascent, descent, height;
26059 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26060 return val;
26062 if (CONSP (val))
26064 face_name = XCAR (val);
26065 val = XCDR (val);
26066 if (!NUMBERP (val))
26067 val = make_number (1);
26068 if (NILP (face_name))
26070 height = it->ascent + it->descent;
26071 goto scale;
26075 if (NILP (face_name))
26077 font = FRAME_FONT (it->f);
26078 boff = FRAME_BASELINE_OFFSET (it->f);
26080 else if (EQ (face_name, Qt))
26082 override = 0;
26084 else
26086 int face_id;
26087 struct face *face;
26089 face_id = lookup_named_face (it->f, face_name, 0);
26090 if (face_id < 0)
26091 return make_number (-1);
26093 face = FACE_FROM_ID (it->f, face_id);
26094 font = face->font;
26095 if (font == NULL)
26096 return make_number (-1);
26097 boff = font->baseline_offset;
26098 if (font->vertical_centering)
26099 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26102 ascent = FONT_BASE (font) + boff;
26103 descent = FONT_DESCENT (font) - boff;
26105 if (override)
26107 it->override_ascent = ascent;
26108 it->override_descent = descent;
26109 it->override_boff = boff;
26112 height = ascent + descent;
26114 scale:
26115 if (FLOATP (val))
26116 height = (int)(XFLOAT_DATA (val) * height);
26117 else if (INTEGERP (val))
26118 height *= XINT (val);
26120 return make_number (height);
26124 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26125 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26126 and only if this is for a character for which no font was found.
26128 If the display method (it->glyphless_method) is
26129 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26130 length of the acronym or the hexadecimal string, UPPER_XOFF and
26131 UPPER_YOFF are pixel offsets for the upper part of the string,
26132 LOWER_XOFF and LOWER_YOFF are for the lower part.
26134 For the other display methods, LEN through LOWER_YOFF are zero. */
26136 static void
26137 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26138 short upper_xoff, short upper_yoff,
26139 short lower_xoff, short lower_yoff)
26141 struct glyph *glyph;
26142 enum glyph_row_area area = it->area;
26144 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26145 if (glyph < it->glyph_row->glyphs[area + 1])
26147 /* If the glyph row is reversed, we need to prepend the glyph
26148 rather than append it. */
26149 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26151 struct glyph *g;
26153 /* Make room for the additional glyph. */
26154 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26155 g[1] = *g;
26156 glyph = it->glyph_row->glyphs[area];
26158 glyph->charpos = CHARPOS (it->position);
26159 glyph->object = it->object;
26160 glyph->pixel_width = it->pixel_width;
26161 glyph->ascent = it->ascent;
26162 glyph->descent = it->descent;
26163 glyph->voffset = it->voffset;
26164 glyph->type = GLYPHLESS_GLYPH;
26165 glyph->u.glyphless.method = it->glyphless_method;
26166 glyph->u.glyphless.for_no_font = for_no_font;
26167 glyph->u.glyphless.len = len;
26168 glyph->u.glyphless.ch = it->c;
26169 glyph->slice.glyphless.upper_xoff = upper_xoff;
26170 glyph->slice.glyphless.upper_yoff = upper_yoff;
26171 glyph->slice.glyphless.lower_xoff = lower_xoff;
26172 glyph->slice.glyphless.lower_yoff = lower_yoff;
26173 glyph->avoid_cursor_p = it->avoid_cursor_p;
26174 glyph->multibyte_p = it->multibyte_p;
26175 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26177 /* In R2L rows, the left and the right box edges need to be
26178 drawn in reverse direction. */
26179 glyph->right_box_line_p = it->start_of_box_run_p;
26180 glyph->left_box_line_p = it->end_of_box_run_p;
26182 else
26184 glyph->left_box_line_p = it->start_of_box_run_p;
26185 glyph->right_box_line_p = it->end_of_box_run_p;
26187 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26188 || it->phys_descent > it->descent);
26189 glyph->padding_p = 0;
26190 glyph->glyph_not_available_p = 0;
26191 glyph->face_id = face_id;
26192 glyph->font_type = FONT_TYPE_UNKNOWN;
26193 if (it->bidi_p)
26195 glyph->resolved_level = it->bidi_it.resolved_level;
26196 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26197 glyph->bidi_type = it->bidi_it.type;
26199 ++it->glyph_row->used[area];
26201 else
26202 IT_EXPAND_MATRIX_WIDTH (it, area);
26206 /* Produce a glyph for a glyphless character for iterator IT.
26207 IT->glyphless_method specifies which method to use for displaying
26208 the character. See the description of enum
26209 glyphless_display_method in dispextern.h for the detail.
26211 FOR_NO_FONT is nonzero if and only if this is for a character for
26212 which no font was found. ACRONYM, if non-nil, is an acronym string
26213 for the character. */
26215 static void
26216 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26218 int face_id;
26219 struct face *face;
26220 struct font *font;
26221 int base_width, base_height, width, height;
26222 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26223 int len;
26225 /* Get the metrics of the base font. We always refer to the current
26226 ASCII face. */
26227 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26228 font = face->font ? face->font : FRAME_FONT (it->f);
26229 it->ascent = FONT_BASE (font) + font->baseline_offset;
26230 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26231 base_height = it->ascent + it->descent;
26232 base_width = font->average_width;
26234 face_id = merge_glyphless_glyph_face (it);
26236 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26238 it->pixel_width = THIN_SPACE_WIDTH;
26239 len = 0;
26240 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26242 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26244 width = CHAR_WIDTH (it->c);
26245 if (width == 0)
26246 width = 1;
26247 else if (width > 4)
26248 width = 4;
26249 it->pixel_width = base_width * width;
26250 len = 0;
26251 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26253 else
26255 char buf[7];
26256 const char *str;
26257 unsigned int code[6];
26258 int upper_len;
26259 int ascent, descent;
26260 struct font_metrics metrics_upper, metrics_lower;
26262 face = FACE_FROM_ID (it->f, face_id);
26263 font = face->font ? face->font : FRAME_FONT (it->f);
26264 prepare_face_for_display (it->f, face);
26266 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26268 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26269 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26270 if (CONSP (acronym))
26271 acronym = XCAR (acronym);
26272 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26274 else
26276 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26277 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26278 str = buf;
26280 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26281 code[len] = font->driver->encode_char (font, str[len]);
26282 upper_len = (len + 1) / 2;
26283 font->driver->text_extents (font, code, upper_len,
26284 &metrics_upper);
26285 font->driver->text_extents (font, code + upper_len, len - upper_len,
26286 &metrics_lower);
26290 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26291 width = max (metrics_upper.width, metrics_lower.width) + 4;
26292 upper_xoff = upper_yoff = 2; /* the typical case */
26293 if (base_width >= width)
26295 /* Align the upper to the left, the lower to the right. */
26296 it->pixel_width = base_width;
26297 lower_xoff = base_width - 2 - metrics_lower.width;
26299 else
26301 /* Center the shorter one. */
26302 it->pixel_width = width;
26303 if (metrics_upper.width >= metrics_lower.width)
26304 lower_xoff = (width - metrics_lower.width) / 2;
26305 else
26307 /* FIXME: This code doesn't look right. It formerly was
26308 missing the "lower_xoff = 0;", which couldn't have
26309 been right since it left lower_xoff uninitialized. */
26310 lower_xoff = 0;
26311 upper_xoff = (width - metrics_upper.width) / 2;
26315 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26316 top, bottom, and between upper and lower strings. */
26317 height = (metrics_upper.ascent + metrics_upper.descent
26318 + metrics_lower.ascent + metrics_lower.descent) + 5;
26319 /* Center vertically.
26320 H:base_height, D:base_descent
26321 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26323 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26324 descent = D - H/2 + h/2;
26325 lower_yoff = descent - 2 - ld;
26326 upper_yoff = lower_yoff - la - 1 - ud; */
26327 ascent = - (it->descent - (base_height + height + 1) / 2);
26328 descent = it->descent - (base_height - height) / 2;
26329 lower_yoff = descent - 2 - metrics_lower.descent;
26330 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26331 - metrics_upper.descent);
26332 /* Don't make the height shorter than the base height. */
26333 if (height > base_height)
26335 it->ascent = ascent;
26336 it->descent = descent;
26340 it->phys_ascent = it->ascent;
26341 it->phys_descent = it->descent;
26342 if (it->glyph_row)
26343 append_glyphless_glyph (it, face_id, for_no_font, len,
26344 upper_xoff, upper_yoff,
26345 lower_xoff, lower_yoff);
26346 it->nglyphs = 1;
26347 take_vertical_position_into_account (it);
26351 /* RIF:
26352 Produce glyphs/get display metrics for the display element IT is
26353 loaded with. See the description of struct it in dispextern.h
26354 for an overview of struct it. */
26356 void
26357 x_produce_glyphs (struct it *it)
26359 int extra_line_spacing = it->extra_line_spacing;
26361 it->glyph_not_available_p = 0;
26363 if (it->what == IT_CHARACTER)
26365 XChar2b char2b;
26366 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26367 struct font *font = face->font;
26368 struct font_metrics *pcm = NULL;
26369 int boff; /* Baseline offset. */
26371 if (font == NULL)
26373 /* When no suitable font is found, display this character by
26374 the method specified in the first extra slot of
26375 Vglyphless_char_display. */
26376 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26378 eassert (it->what == IT_GLYPHLESS);
26379 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26380 goto done;
26383 boff = font->baseline_offset;
26384 if (font->vertical_centering)
26385 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26387 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26389 int stretched_p;
26391 it->nglyphs = 1;
26393 if (it->override_ascent >= 0)
26395 it->ascent = it->override_ascent;
26396 it->descent = it->override_descent;
26397 boff = it->override_boff;
26399 else
26401 it->ascent = FONT_BASE (font) + boff;
26402 it->descent = FONT_DESCENT (font) - boff;
26405 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26407 pcm = get_per_char_metric (font, &char2b);
26408 if (pcm->width == 0
26409 && pcm->rbearing == 0 && pcm->lbearing == 0)
26410 pcm = NULL;
26413 if (pcm)
26415 it->phys_ascent = pcm->ascent + boff;
26416 it->phys_descent = pcm->descent - boff;
26417 it->pixel_width = pcm->width;
26419 else
26421 it->glyph_not_available_p = 1;
26422 it->phys_ascent = it->ascent;
26423 it->phys_descent = it->descent;
26424 it->pixel_width = font->space_width;
26427 if (it->constrain_row_ascent_descent_p)
26429 if (it->descent > it->max_descent)
26431 it->ascent += it->descent - it->max_descent;
26432 it->descent = it->max_descent;
26434 if (it->ascent > it->max_ascent)
26436 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26437 it->ascent = it->max_ascent;
26439 it->phys_ascent = min (it->phys_ascent, it->ascent);
26440 it->phys_descent = min (it->phys_descent, it->descent);
26441 extra_line_spacing = 0;
26444 /* If this is a space inside a region of text with
26445 `space-width' property, change its width. */
26446 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26447 if (stretched_p)
26448 it->pixel_width *= XFLOATINT (it->space_width);
26450 /* If face has a box, add the box thickness to the character
26451 height. If character has a box line to the left and/or
26452 right, add the box line width to the character's width. */
26453 if (face->box != FACE_NO_BOX)
26455 int thick = face->box_line_width;
26457 if (thick > 0)
26459 it->ascent += thick;
26460 it->descent += thick;
26462 else
26463 thick = -thick;
26465 if (it->start_of_box_run_p)
26466 it->pixel_width += thick;
26467 if (it->end_of_box_run_p)
26468 it->pixel_width += thick;
26471 /* If face has an overline, add the height of the overline
26472 (1 pixel) and a 1 pixel margin to the character height. */
26473 if (face->overline_p)
26474 it->ascent += overline_margin;
26476 if (it->constrain_row_ascent_descent_p)
26478 if (it->ascent > it->max_ascent)
26479 it->ascent = it->max_ascent;
26480 if (it->descent > it->max_descent)
26481 it->descent = it->max_descent;
26484 take_vertical_position_into_account (it);
26486 /* If we have to actually produce glyphs, do it. */
26487 if (it->glyph_row)
26489 if (stretched_p)
26491 /* Translate a space with a `space-width' property
26492 into a stretch glyph. */
26493 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26494 / FONT_HEIGHT (font));
26495 append_stretch_glyph (it, it->object, it->pixel_width,
26496 it->ascent + it->descent, ascent);
26498 else
26499 append_glyph (it);
26501 /* If characters with lbearing or rbearing are displayed
26502 in this line, record that fact in a flag of the
26503 glyph row. This is used to optimize X output code. */
26504 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26505 it->glyph_row->contains_overlapping_glyphs_p = 1;
26507 if (! stretched_p && it->pixel_width == 0)
26508 /* We assure that all visible glyphs have at least 1-pixel
26509 width. */
26510 it->pixel_width = 1;
26512 else if (it->char_to_display == '\n')
26514 /* A newline has no width, but we need the height of the
26515 line. But if previous part of the line sets a height,
26516 don't increase that height. */
26518 Lisp_Object height;
26519 Lisp_Object total_height = Qnil;
26521 it->override_ascent = -1;
26522 it->pixel_width = 0;
26523 it->nglyphs = 0;
26525 height = get_it_property (it, Qline_height);
26526 /* Split (line-height total-height) list. */
26527 if (CONSP (height)
26528 && CONSP (XCDR (height))
26529 && NILP (XCDR (XCDR (height))))
26531 total_height = XCAR (XCDR (height));
26532 height = XCAR (height);
26534 height = calc_line_height_property (it, height, font, boff, 1);
26536 if (it->override_ascent >= 0)
26538 it->ascent = it->override_ascent;
26539 it->descent = it->override_descent;
26540 boff = it->override_boff;
26542 else
26544 it->ascent = FONT_BASE (font) + boff;
26545 it->descent = FONT_DESCENT (font) - boff;
26548 if (EQ (height, Qt))
26550 if (it->descent > it->max_descent)
26552 it->ascent += it->descent - it->max_descent;
26553 it->descent = it->max_descent;
26555 if (it->ascent > it->max_ascent)
26557 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26558 it->ascent = it->max_ascent;
26560 it->phys_ascent = min (it->phys_ascent, it->ascent);
26561 it->phys_descent = min (it->phys_descent, it->descent);
26562 it->constrain_row_ascent_descent_p = 1;
26563 extra_line_spacing = 0;
26565 else
26567 Lisp_Object spacing;
26569 it->phys_ascent = it->ascent;
26570 it->phys_descent = it->descent;
26572 if ((it->max_ascent > 0 || it->max_descent > 0)
26573 && face->box != FACE_NO_BOX
26574 && face->box_line_width > 0)
26576 it->ascent += face->box_line_width;
26577 it->descent += face->box_line_width;
26579 if (!NILP (height)
26580 && XINT (height) > it->ascent + it->descent)
26581 it->ascent = XINT (height) - it->descent;
26583 if (!NILP (total_height))
26584 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26585 else
26587 spacing = get_it_property (it, Qline_spacing);
26588 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26590 if (INTEGERP (spacing))
26592 extra_line_spacing = XINT (spacing);
26593 if (!NILP (total_height))
26594 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26598 else /* i.e. (it->char_to_display == '\t') */
26600 if (font->space_width > 0)
26602 int tab_width = it->tab_width * font->space_width;
26603 int x = it->current_x + it->continuation_lines_width;
26604 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26606 /* If the distance from the current position to the next tab
26607 stop is less than a space character width, use the
26608 tab stop after that. */
26609 if (next_tab_x - x < font->space_width)
26610 next_tab_x += tab_width;
26612 it->pixel_width = next_tab_x - x;
26613 it->nglyphs = 1;
26614 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26615 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26617 if (it->glyph_row)
26619 append_stretch_glyph (it, it->object, it->pixel_width,
26620 it->ascent + it->descent, it->ascent);
26623 else
26625 it->pixel_width = 0;
26626 it->nglyphs = 1;
26630 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26632 /* A static composition.
26634 Note: A composition is represented as one glyph in the
26635 glyph matrix. There are no padding glyphs.
26637 Important note: pixel_width, ascent, and descent are the
26638 values of what is drawn by draw_glyphs (i.e. the values of
26639 the overall glyphs composed). */
26640 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26641 int boff; /* baseline offset */
26642 struct composition *cmp = composition_table[it->cmp_it.id];
26643 int glyph_len = cmp->glyph_len;
26644 struct font *font = face->font;
26646 it->nglyphs = 1;
26648 /* If we have not yet calculated pixel size data of glyphs of
26649 the composition for the current face font, calculate them
26650 now. Theoretically, we have to check all fonts for the
26651 glyphs, but that requires much time and memory space. So,
26652 here we check only the font of the first glyph. This may
26653 lead to incorrect display, but it's very rare, and C-l
26654 (recenter-top-bottom) can correct the display anyway. */
26655 if (! cmp->font || cmp->font != font)
26657 /* Ascent and descent of the font of the first character
26658 of this composition (adjusted by baseline offset).
26659 Ascent and descent of overall glyphs should not be less
26660 than these, respectively. */
26661 int font_ascent, font_descent, font_height;
26662 /* Bounding box of the overall glyphs. */
26663 int leftmost, rightmost, lowest, highest;
26664 int lbearing, rbearing;
26665 int i, width, ascent, descent;
26666 int left_padded = 0, right_padded = 0;
26667 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26668 XChar2b char2b;
26669 struct font_metrics *pcm;
26670 int font_not_found_p;
26671 ptrdiff_t pos;
26673 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26674 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26675 break;
26676 if (glyph_len < cmp->glyph_len)
26677 right_padded = 1;
26678 for (i = 0; i < glyph_len; i++)
26680 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26681 break;
26682 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26684 if (i > 0)
26685 left_padded = 1;
26687 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26688 : IT_CHARPOS (*it));
26689 /* If no suitable font is found, use the default font. */
26690 font_not_found_p = font == NULL;
26691 if (font_not_found_p)
26693 face = face->ascii_face;
26694 font = face->font;
26696 boff = font->baseline_offset;
26697 if (font->vertical_centering)
26698 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26699 font_ascent = FONT_BASE (font) + boff;
26700 font_descent = FONT_DESCENT (font) - boff;
26701 font_height = FONT_HEIGHT (font);
26703 cmp->font = font;
26705 pcm = NULL;
26706 if (! font_not_found_p)
26708 get_char_face_and_encoding (it->f, c, it->face_id,
26709 &char2b, 0);
26710 pcm = get_per_char_metric (font, &char2b);
26713 /* Initialize the bounding box. */
26714 if (pcm)
26716 width = cmp->glyph_len > 0 ? pcm->width : 0;
26717 ascent = pcm->ascent;
26718 descent = pcm->descent;
26719 lbearing = pcm->lbearing;
26720 rbearing = pcm->rbearing;
26722 else
26724 width = cmp->glyph_len > 0 ? font->space_width : 0;
26725 ascent = FONT_BASE (font);
26726 descent = FONT_DESCENT (font);
26727 lbearing = 0;
26728 rbearing = width;
26731 rightmost = width;
26732 leftmost = 0;
26733 lowest = - descent + boff;
26734 highest = ascent + boff;
26736 if (! font_not_found_p
26737 && font->default_ascent
26738 && CHAR_TABLE_P (Vuse_default_ascent)
26739 && !NILP (Faref (Vuse_default_ascent,
26740 make_number (it->char_to_display))))
26741 highest = font->default_ascent + boff;
26743 /* Draw the first glyph at the normal position. It may be
26744 shifted to right later if some other glyphs are drawn
26745 at the left. */
26746 cmp->offsets[i * 2] = 0;
26747 cmp->offsets[i * 2 + 1] = boff;
26748 cmp->lbearing = lbearing;
26749 cmp->rbearing = rbearing;
26751 /* Set cmp->offsets for the remaining glyphs. */
26752 for (i++; i < glyph_len; i++)
26754 int left, right, btm, top;
26755 int ch = COMPOSITION_GLYPH (cmp, i);
26756 int face_id;
26757 struct face *this_face;
26759 if (ch == '\t')
26760 ch = ' ';
26761 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26762 this_face = FACE_FROM_ID (it->f, face_id);
26763 font = this_face->font;
26765 if (font == NULL)
26766 pcm = NULL;
26767 else
26769 get_char_face_and_encoding (it->f, ch, face_id,
26770 &char2b, 0);
26771 pcm = get_per_char_metric (font, &char2b);
26773 if (! pcm)
26774 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26775 else
26777 width = pcm->width;
26778 ascent = pcm->ascent;
26779 descent = pcm->descent;
26780 lbearing = pcm->lbearing;
26781 rbearing = pcm->rbearing;
26782 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26784 /* Relative composition with or without
26785 alternate chars. */
26786 left = (leftmost + rightmost - width) / 2;
26787 btm = - descent + boff;
26788 if (font->relative_compose
26789 && (! CHAR_TABLE_P (Vignore_relative_composition)
26790 || NILP (Faref (Vignore_relative_composition,
26791 make_number (ch)))))
26794 if (- descent >= font->relative_compose)
26795 /* One extra pixel between two glyphs. */
26796 btm = highest + 1;
26797 else if (ascent <= 0)
26798 /* One extra pixel between two glyphs. */
26799 btm = lowest - 1 - ascent - descent;
26802 else
26804 /* A composition rule is specified by an integer
26805 value that encodes global and new reference
26806 points (GREF and NREF). GREF and NREF are
26807 specified by numbers as below:
26809 0---1---2 -- ascent
26813 9--10--11 -- center
26815 ---3---4---5--- baseline
26817 6---7---8 -- descent
26819 int rule = COMPOSITION_RULE (cmp, i);
26820 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26822 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26823 grefx = gref % 3, nrefx = nref % 3;
26824 grefy = gref / 3, nrefy = nref / 3;
26825 if (xoff)
26826 xoff = font_height * (xoff - 128) / 256;
26827 if (yoff)
26828 yoff = font_height * (yoff - 128) / 256;
26830 left = (leftmost
26831 + grefx * (rightmost - leftmost) / 2
26832 - nrefx * width / 2
26833 + xoff);
26835 btm = ((grefy == 0 ? highest
26836 : grefy == 1 ? 0
26837 : grefy == 2 ? lowest
26838 : (highest + lowest) / 2)
26839 - (nrefy == 0 ? ascent + descent
26840 : nrefy == 1 ? descent - boff
26841 : nrefy == 2 ? 0
26842 : (ascent + descent) / 2)
26843 + yoff);
26846 cmp->offsets[i * 2] = left;
26847 cmp->offsets[i * 2 + 1] = btm + descent;
26849 /* Update the bounding box of the overall glyphs. */
26850 if (width > 0)
26852 right = left + width;
26853 if (left < leftmost)
26854 leftmost = left;
26855 if (right > rightmost)
26856 rightmost = right;
26858 top = btm + descent + ascent;
26859 if (top > highest)
26860 highest = top;
26861 if (btm < lowest)
26862 lowest = btm;
26864 if (cmp->lbearing > left + lbearing)
26865 cmp->lbearing = left + lbearing;
26866 if (cmp->rbearing < left + rbearing)
26867 cmp->rbearing = left + rbearing;
26871 /* If there are glyphs whose x-offsets are negative,
26872 shift all glyphs to the right and make all x-offsets
26873 non-negative. */
26874 if (leftmost < 0)
26876 for (i = 0; i < cmp->glyph_len; i++)
26877 cmp->offsets[i * 2] -= leftmost;
26878 rightmost -= leftmost;
26879 cmp->lbearing -= leftmost;
26880 cmp->rbearing -= leftmost;
26883 if (left_padded && cmp->lbearing < 0)
26885 for (i = 0; i < cmp->glyph_len; i++)
26886 cmp->offsets[i * 2] -= cmp->lbearing;
26887 rightmost -= cmp->lbearing;
26888 cmp->rbearing -= cmp->lbearing;
26889 cmp->lbearing = 0;
26891 if (right_padded && rightmost < cmp->rbearing)
26893 rightmost = cmp->rbearing;
26896 cmp->pixel_width = rightmost;
26897 cmp->ascent = highest;
26898 cmp->descent = - lowest;
26899 if (cmp->ascent < font_ascent)
26900 cmp->ascent = font_ascent;
26901 if (cmp->descent < font_descent)
26902 cmp->descent = font_descent;
26905 if (it->glyph_row
26906 && (cmp->lbearing < 0
26907 || cmp->rbearing > cmp->pixel_width))
26908 it->glyph_row->contains_overlapping_glyphs_p = 1;
26910 it->pixel_width = cmp->pixel_width;
26911 it->ascent = it->phys_ascent = cmp->ascent;
26912 it->descent = it->phys_descent = cmp->descent;
26913 if (face->box != FACE_NO_BOX)
26915 int thick = face->box_line_width;
26917 if (thick > 0)
26919 it->ascent += thick;
26920 it->descent += thick;
26922 else
26923 thick = - thick;
26925 if (it->start_of_box_run_p)
26926 it->pixel_width += thick;
26927 if (it->end_of_box_run_p)
26928 it->pixel_width += thick;
26931 /* If face has an overline, add the height of the overline
26932 (1 pixel) and a 1 pixel margin to the character height. */
26933 if (face->overline_p)
26934 it->ascent += overline_margin;
26936 take_vertical_position_into_account (it);
26937 if (it->ascent < 0)
26938 it->ascent = 0;
26939 if (it->descent < 0)
26940 it->descent = 0;
26942 if (it->glyph_row && cmp->glyph_len > 0)
26943 append_composite_glyph (it);
26945 else if (it->what == IT_COMPOSITION)
26947 /* A dynamic (automatic) composition. */
26948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26949 Lisp_Object gstring;
26950 struct font_metrics metrics;
26952 it->nglyphs = 1;
26954 gstring = composition_gstring_from_id (it->cmp_it.id);
26955 it->pixel_width
26956 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26957 &metrics);
26958 if (it->glyph_row
26959 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26960 it->glyph_row->contains_overlapping_glyphs_p = 1;
26961 it->ascent = it->phys_ascent = metrics.ascent;
26962 it->descent = it->phys_descent = metrics.descent;
26963 if (face->box != FACE_NO_BOX)
26965 int thick = face->box_line_width;
26967 if (thick > 0)
26969 it->ascent += thick;
26970 it->descent += thick;
26972 else
26973 thick = - thick;
26975 if (it->start_of_box_run_p)
26976 it->pixel_width += thick;
26977 if (it->end_of_box_run_p)
26978 it->pixel_width += thick;
26980 /* If face has an overline, add the height of the overline
26981 (1 pixel) and a 1 pixel margin to the character height. */
26982 if (face->overline_p)
26983 it->ascent += overline_margin;
26984 take_vertical_position_into_account (it);
26985 if (it->ascent < 0)
26986 it->ascent = 0;
26987 if (it->descent < 0)
26988 it->descent = 0;
26990 if (it->glyph_row)
26991 append_composite_glyph (it);
26993 else if (it->what == IT_GLYPHLESS)
26994 produce_glyphless_glyph (it, 0, Qnil);
26995 else if (it->what == IT_IMAGE)
26996 produce_image_glyph (it);
26997 else if (it->what == IT_STRETCH)
26998 produce_stretch_glyph (it);
27000 done:
27001 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27002 because this isn't true for images with `:ascent 100'. */
27003 eassert (it->ascent >= 0 && it->descent >= 0);
27004 if (it->area == TEXT_AREA)
27005 it->current_x += it->pixel_width;
27007 if (extra_line_spacing > 0)
27009 it->descent += extra_line_spacing;
27010 if (extra_line_spacing > it->max_extra_line_spacing)
27011 it->max_extra_line_spacing = extra_line_spacing;
27014 it->max_ascent = max (it->max_ascent, it->ascent);
27015 it->max_descent = max (it->max_descent, it->descent);
27016 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27017 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27020 /* EXPORT for RIF:
27021 Output LEN glyphs starting at START at the nominal cursor position.
27022 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27023 being updated, and UPDATED_AREA is the area of that row being updated. */
27025 void
27026 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27027 struct glyph *start, enum glyph_row_area updated_area, int len)
27029 int x, hpos, chpos = w->phys_cursor.hpos;
27031 eassert (updated_row);
27032 /* When the window is hscrolled, cursor hpos can legitimately be out
27033 of bounds, but we draw the cursor at the corresponding window
27034 margin in that case. */
27035 if (!updated_row->reversed_p && chpos < 0)
27036 chpos = 0;
27037 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27038 chpos = updated_row->used[TEXT_AREA] - 1;
27040 block_input ();
27042 /* Write glyphs. */
27044 hpos = start - updated_row->glyphs[updated_area];
27045 x = draw_glyphs (w, w->output_cursor.x,
27046 updated_row, updated_area,
27047 hpos, hpos + len,
27048 DRAW_NORMAL_TEXT, 0);
27050 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27051 if (updated_area == TEXT_AREA
27052 && w->phys_cursor_on_p
27053 && w->phys_cursor.vpos == w->output_cursor.vpos
27054 && chpos >= hpos
27055 && chpos < hpos + len)
27056 w->phys_cursor_on_p = 0;
27058 unblock_input ();
27060 /* Advance the output cursor. */
27061 w->output_cursor.hpos += len;
27062 w->output_cursor.x = x;
27066 /* EXPORT for RIF:
27067 Insert LEN glyphs from START at the nominal cursor position. */
27069 void
27070 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27071 struct glyph *start, enum glyph_row_area updated_area, int len)
27073 struct frame *f;
27074 int line_height, shift_by_width, shifted_region_width;
27075 struct glyph_row *row;
27076 struct glyph *glyph;
27077 int frame_x, frame_y;
27078 ptrdiff_t hpos;
27080 eassert (updated_row);
27081 block_input ();
27082 f = XFRAME (WINDOW_FRAME (w));
27084 /* Get the height of the line we are in. */
27085 row = updated_row;
27086 line_height = row->height;
27088 /* Get the width of the glyphs to insert. */
27089 shift_by_width = 0;
27090 for (glyph = start; glyph < start + len; ++glyph)
27091 shift_by_width += glyph->pixel_width;
27093 /* Get the width of the region to shift right. */
27094 shifted_region_width = (window_box_width (w, updated_area)
27095 - w->output_cursor.x
27096 - shift_by_width);
27098 /* Shift right. */
27099 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27100 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27102 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27103 line_height, shift_by_width);
27105 /* Write the glyphs. */
27106 hpos = start - row->glyphs[updated_area];
27107 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27108 hpos, hpos + len,
27109 DRAW_NORMAL_TEXT, 0);
27111 /* Advance the output cursor. */
27112 w->output_cursor.hpos += len;
27113 w->output_cursor.x += shift_by_width;
27114 unblock_input ();
27118 /* EXPORT for RIF:
27119 Erase the current text line from the nominal cursor position
27120 (inclusive) to pixel column TO_X (exclusive). The idea is that
27121 everything from TO_X onward is already erased.
27123 TO_X is a pixel position relative to UPDATED_AREA of currently
27124 updated window W. TO_X == -1 means clear to the end of this area. */
27126 void
27127 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27128 enum glyph_row_area updated_area, int to_x)
27130 struct frame *f;
27131 int max_x, min_y, max_y;
27132 int from_x, from_y, to_y;
27134 eassert (updated_row);
27135 f = XFRAME (w->frame);
27137 if (updated_row->full_width_p)
27138 max_x = (WINDOW_PIXEL_WIDTH (w)
27139 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27140 else
27141 max_x = window_box_width (w, updated_area);
27142 max_y = window_text_bottom_y (w);
27144 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27145 of window. For TO_X > 0, truncate to end of drawing area. */
27146 if (to_x == 0)
27147 return;
27148 else if (to_x < 0)
27149 to_x = max_x;
27150 else
27151 to_x = min (to_x, max_x);
27153 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27155 /* Notice if the cursor will be cleared by this operation. */
27156 if (!updated_row->full_width_p)
27157 notice_overwritten_cursor (w, updated_area,
27158 w->output_cursor.x, -1,
27159 updated_row->y,
27160 MATRIX_ROW_BOTTOM_Y (updated_row));
27162 from_x = w->output_cursor.x;
27164 /* Translate to frame coordinates. */
27165 if (updated_row->full_width_p)
27167 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27168 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27170 else
27172 int area_left = window_box_left (w, updated_area);
27173 from_x += area_left;
27174 to_x += area_left;
27177 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27178 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27179 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27181 /* Prevent inadvertently clearing to end of the X window. */
27182 if (to_x > from_x && to_y > from_y)
27184 block_input ();
27185 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27186 to_x - from_x, to_y - from_y);
27187 unblock_input ();
27191 #endif /* HAVE_WINDOW_SYSTEM */
27195 /***********************************************************************
27196 Cursor types
27197 ***********************************************************************/
27199 /* Value is the internal representation of the specified cursor type
27200 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27201 of the bar cursor. */
27203 static enum text_cursor_kinds
27204 get_specified_cursor_type (Lisp_Object arg, int *width)
27206 enum text_cursor_kinds type;
27208 if (NILP (arg))
27209 return NO_CURSOR;
27211 if (EQ (arg, Qbox))
27212 return FILLED_BOX_CURSOR;
27214 if (EQ (arg, Qhollow))
27215 return HOLLOW_BOX_CURSOR;
27217 if (EQ (arg, Qbar))
27219 *width = 2;
27220 return BAR_CURSOR;
27223 if (CONSP (arg)
27224 && EQ (XCAR (arg), Qbar)
27225 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27227 *width = XINT (XCDR (arg));
27228 return BAR_CURSOR;
27231 if (EQ (arg, Qhbar))
27233 *width = 2;
27234 return HBAR_CURSOR;
27237 if (CONSP (arg)
27238 && EQ (XCAR (arg), Qhbar)
27239 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27241 *width = XINT (XCDR (arg));
27242 return HBAR_CURSOR;
27245 /* Treat anything unknown as "hollow box cursor".
27246 It was bad to signal an error; people have trouble fixing
27247 .Xdefaults with Emacs, when it has something bad in it. */
27248 type = HOLLOW_BOX_CURSOR;
27250 return type;
27253 /* Set the default cursor types for specified frame. */
27254 void
27255 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27257 int width = 1;
27258 Lisp_Object tem;
27260 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27261 FRAME_CURSOR_WIDTH (f) = width;
27263 /* By default, set up the blink-off state depending on the on-state. */
27265 tem = Fassoc (arg, Vblink_cursor_alist);
27266 if (!NILP (tem))
27268 FRAME_BLINK_OFF_CURSOR (f)
27269 = get_specified_cursor_type (XCDR (tem), &width);
27270 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27272 else
27273 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27275 /* Make sure the cursor gets redrawn. */
27276 f->cursor_type_changed = 1;
27280 #ifdef HAVE_WINDOW_SYSTEM
27282 /* Return the cursor we want to be displayed in window W. Return
27283 width of bar/hbar cursor through WIDTH arg. Return with
27284 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27285 (i.e. if the `system caret' should track this cursor).
27287 In a mini-buffer window, we want the cursor only to appear if we
27288 are reading input from this window. For the selected window, we
27289 want the cursor type given by the frame parameter or buffer local
27290 setting of cursor-type. If explicitly marked off, draw no cursor.
27291 In all other cases, we want a hollow box cursor. */
27293 static enum text_cursor_kinds
27294 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27295 int *active_cursor)
27297 struct frame *f = XFRAME (w->frame);
27298 struct buffer *b = XBUFFER (w->contents);
27299 int cursor_type = DEFAULT_CURSOR;
27300 Lisp_Object alt_cursor;
27301 int non_selected = 0;
27303 *active_cursor = 1;
27305 /* Echo area */
27306 if (cursor_in_echo_area
27307 && FRAME_HAS_MINIBUF_P (f)
27308 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27310 if (w == XWINDOW (echo_area_window))
27312 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27314 *width = FRAME_CURSOR_WIDTH (f);
27315 return FRAME_DESIRED_CURSOR (f);
27317 else
27318 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27321 *active_cursor = 0;
27322 non_selected = 1;
27325 /* Detect a nonselected window or nonselected frame. */
27326 else if (w != XWINDOW (f->selected_window)
27327 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27329 *active_cursor = 0;
27331 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27332 return NO_CURSOR;
27334 non_selected = 1;
27337 /* Never display a cursor in a window in which cursor-type is nil. */
27338 if (NILP (BVAR (b, cursor_type)))
27339 return NO_CURSOR;
27341 /* Get the normal cursor type for this window. */
27342 if (EQ (BVAR (b, cursor_type), Qt))
27344 cursor_type = FRAME_DESIRED_CURSOR (f);
27345 *width = FRAME_CURSOR_WIDTH (f);
27347 else
27348 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27350 /* Use cursor-in-non-selected-windows instead
27351 for non-selected window or frame. */
27352 if (non_selected)
27354 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27355 if (!EQ (Qt, alt_cursor))
27356 return get_specified_cursor_type (alt_cursor, width);
27357 /* t means modify the normal cursor type. */
27358 if (cursor_type == FILLED_BOX_CURSOR)
27359 cursor_type = HOLLOW_BOX_CURSOR;
27360 else if (cursor_type == BAR_CURSOR && *width > 1)
27361 --*width;
27362 return cursor_type;
27365 /* Use normal cursor if not blinked off. */
27366 if (!w->cursor_off_p)
27368 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27370 if (cursor_type == FILLED_BOX_CURSOR)
27372 /* Using a block cursor on large images can be very annoying.
27373 So use a hollow cursor for "large" images.
27374 If image is not transparent (no mask), also use hollow cursor. */
27375 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27376 if (img != NULL && IMAGEP (img->spec))
27378 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27379 where N = size of default frame font size.
27380 This should cover most of the "tiny" icons people may use. */
27381 if (!img->mask
27382 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27383 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27384 cursor_type = HOLLOW_BOX_CURSOR;
27387 else if (cursor_type != NO_CURSOR)
27389 /* Display current only supports BOX and HOLLOW cursors for images.
27390 So for now, unconditionally use a HOLLOW cursor when cursor is
27391 not a solid box cursor. */
27392 cursor_type = HOLLOW_BOX_CURSOR;
27395 return cursor_type;
27398 /* Cursor is blinked off, so determine how to "toggle" it. */
27400 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27401 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27402 return get_specified_cursor_type (XCDR (alt_cursor), width);
27404 /* Then see if frame has specified a specific blink off cursor type. */
27405 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27407 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27408 return FRAME_BLINK_OFF_CURSOR (f);
27411 #if 0
27412 /* Some people liked having a permanently visible blinking cursor,
27413 while others had very strong opinions against it. So it was
27414 decided to remove it. KFS 2003-09-03 */
27416 /* Finally perform built-in cursor blinking:
27417 filled box <-> hollow box
27418 wide [h]bar <-> narrow [h]bar
27419 narrow [h]bar <-> no cursor
27420 other type <-> no cursor */
27422 if (cursor_type == FILLED_BOX_CURSOR)
27423 return HOLLOW_BOX_CURSOR;
27425 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27427 *width = 1;
27428 return cursor_type;
27430 #endif
27432 return NO_CURSOR;
27436 /* Notice when the text cursor of window W has been completely
27437 overwritten by a drawing operation that outputs glyphs in AREA
27438 starting at X0 and ending at X1 in the line starting at Y0 and
27439 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27440 the rest of the line after X0 has been written. Y coordinates
27441 are window-relative. */
27443 static void
27444 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27445 int x0, int x1, int y0, int y1)
27447 int cx0, cx1, cy0, cy1;
27448 struct glyph_row *row;
27450 if (!w->phys_cursor_on_p)
27451 return;
27452 if (area != TEXT_AREA)
27453 return;
27455 if (w->phys_cursor.vpos < 0
27456 || w->phys_cursor.vpos >= w->current_matrix->nrows
27457 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27458 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27459 return;
27461 if (row->cursor_in_fringe_p)
27463 row->cursor_in_fringe_p = 0;
27464 draw_fringe_bitmap (w, row, row->reversed_p);
27465 w->phys_cursor_on_p = 0;
27466 return;
27469 cx0 = w->phys_cursor.x;
27470 cx1 = cx0 + w->phys_cursor_width;
27471 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27472 return;
27474 /* The cursor image will be completely removed from the
27475 screen if the output area intersects the cursor area in
27476 y-direction. When we draw in [y0 y1[, and some part of
27477 the cursor is at y < y0, that part must have been drawn
27478 before. When scrolling, the cursor is erased before
27479 actually scrolling, so we don't come here. When not
27480 scrolling, the rows above the old cursor row must have
27481 changed, and in this case these rows must have written
27482 over the cursor image.
27484 Likewise if part of the cursor is below y1, with the
27485 exception of the cursor being in the first blank row at
27486 the buffer and window end because update_text_area
27487 doesn't draw that row. (Except when it does, but
27488 that's handled in update_text_area.) */
27490 cy0 = w->phys_cursor.y;
27491 cy1 = cy0 + w->phys_cursor_height;
27492 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27493 return;
27495 w->phys_cursor_on_p = 0;
27498 #endif /* HAVE_WINDOW_SYSTEM */
27501 /************************************************************************
27502 Mouse Face
27503 ************************************************************************/
27505 #ifdef HAVE_WINDOW_SYSTEM
27507 /* EXPORT for RIF:
27508 Fix the display of area AREA of overlapping row ROW in window W
27509 with respect to the overlapping part OVERLAPS. */
27511 void
27512 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27513 enum glyph_row_area area, int overlaps)
27515 int i, x;
27517 block_input ();
27519 x = 0;
27520 for (i = 0; i < row->used[area];)
27522 if (row->glyphs[area][i].overlaps_vertically_p)
27524 int start = i, start_x = x;
27528 x += row->glyphs[area][i].pixel_width;
27529 ++i;
27531 while (i < row->used[area]
27532 && row->glyphs[area][i].overlaps_vertically_p);
27534 draw_glyphs (w, start_x, row, area,
27535 start, i,
27536 DRAW_NORMAL_TEXT, overlaps);
27538 else
27540 x += row->glyphs[area][i].pixel_width;
27541 ++i;
27545 unblock_input ();
27549 /* EXPORT:
27550 Draw the cursor glyph of window W in glyph row ROW. See the
27551 comment of draw_glyphs for the meaning of HL. */
27553 void
27554 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27555 enum draw_glyphs_face hl)
27557 /* If cursor hpos is out of bounds, don't draw garbage. This can
27558 happen in mini-buffer windows when switching between echo area
27559 glyphs and mini-buffer. */
27560 if ((row->reversed_p
27561 ? (w->phys_cursor.hpos >= 0)
27562 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27564 int on_p = w->phys_cursor_on_p;
27565 int x1;
27566 int hpos = w->phys_cursor.hpos;
27568 /* When the window is hscrolled, cursor hpos can legitimately be
27569 out of bounds, but we draw the cursor at the corresponding
27570 window margin in that case. */
27571 if (!row->reversed_p && hpos < 0)
27572 hpos = 0;
27573 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27574 hpos = row->used[TEXT_AREA] - 1;
27576 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27577 hl, 0);
27578 w->phys_cursor_on_p = on_p;
27580 if (hl == DRAW_CURSOR)
27581 w->phys_cursor_width = x1 - w->phys_cursor.x;
27582 /* When we erase the cursor, and ROW is overlapped by other
27583 rows, make sure that these overlapping parts of other rows
27584 are redrawn. */
27585 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27587 w->phys_cursor_width = x1 - w->phys_cursor.x;
27589 if (row > w->current_matrix->rows
27590 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27591 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27592 OVERLAPS_ERASED_CURSOR);
27594 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27595 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27596 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27597 OVERLAPS_ERASED_CURSOR);
27603 /* Erase the image of a cursor of window W from the screen. */
27605 void
27606 erase_phys_cursor (struct window *w)
27608 struct frame *f = XFRAME (w->frame);
27609 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27610 int hpos = w->phys_cursor.hpos;
27611 int vpos = w->phys_cursor.vpos;
27612 int mouse_face_here_p = 0;
27613 struct glyph_matrix *active_glyphs = w->current_matrix;
27614 struct glyph_row *cursor_row;
27615 struct glyph *cursor_glyph;
27616 enum draw_glyphs_face hl;
27618 /* No cursor displayed or row invalidated => nothing to do on the
27619 screen. */
27620 if (w->phys_cursor_type == NO_CURSOR)
27621 goto mark_cursor_off;
27623 /* VPOS >= active_glyphs->nrows means that window has been resized.
27624 Don't bother to erase the cursor. */
27625 if (vpos >= active_glyphs->nrows)
27626 goto mark_cursor_off;
27628 /* If row containing cursor is marked invalid, there is nothing we
27629 can do. */
27630 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27631 if (!cursor_row->enabled_p)
27632 goto mark_cursor_off;
27634 /* If line spacing is > 0, old cursor may only be partially visible in
27635 window after split-window. So adjust visible height. */
27636 cursor_row->visible_height = min (cursor_row->visible_height,
27637 window_text_bottom_y (w) - cursor_row->y);
27639 /* If row is completely invisible, don't attempt to delete a cursor which
27640 isn't there. This can happen if cursor is at top of a window, and
27641 we switch to a buffer with a header line in that window. */
27642 if (cursor_row->visible_height <= 0)
27643 goto mark_cursor_off;
27645 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27646 if (cursor_row->cursor_in_fringe_p)
27648 cursor_row->cursor_in_fringe_p = 0;
27649 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27650 goto mark_cursor_off;
27653 /* This can happen when the new row is shorter than the old one.
27654 In this case, either draw_glyphs or clear_end_of_line
27655 should have cleared the cursor. Note that we wouldn't be
27656 able to erase the cursor in this case because we don't have a
27657 cursor glyph at hand. */
27658 if ((cursor_row->reversed_p
27659 ? (w->phys_cursor.hpos < 0)
27660 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27661 goto mark_cursor_off;
27663 /* When the window is hscrolled, cursor hpos can legitimately be out
27664 of bounds, but we draw the cursor at the corresponding window
27665 margin in that case. */
27666 if (!cursor_row->reversed_p && hpos < 0)
27667 hpos = 0;
27668 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27669 hpos = cursor_row->used[TEXT_AREA] - 1;
27671 /* If the cursor is in the mouse face area, redisplay that when
27672 we clear the cursor. */
27673 if (! NILP (hlinfo->mouse_face_window)
27674 && coords_in_mouse_face_p (w, hpos, vpos)
27675 /* Don't redraw the cursor's spot in mouse face if it is at the
27676 end of a line (on a newline). The cursor appears there, but
27677 mouse highlighting does not. */
27678 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27679 mouse_face_here_p = 1;
27681 /* Maybe clear the display under the cursor. */
27682 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27684 int x, y;
27685 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27686 int width;
27688 cursor_glyph = get_phys_cursor_glyph (w);
27689 if (cursor_glyph == NULL)
27690 goto mark_cursor_off;
27692 width = cursor_glyph->pixel_width;
27693 x = w->phys_cursor.x;
27694 if (x < 0)
27696 width += x;
27697 x = 0;
27699 width = min (width, window_box_width (w, TEXT_AREA) - x);
27700 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27701 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27703 if (width > 0)
27704 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27707 /* Erase the cursor by redrawing the character underneath it. */
27708 if (mouse_face_here_p)
27709 hl = DRAW_MOUSE_FACE;
27710 else
27711 hl = DRAW_NORMAL_TEXT;
27712 draw_phys_cursor_glyph (w, cursor_row, hl);
27714 mark_cursor_off:
27715 w->phys_cursor_on_p = 0;
27716 w->phys_cursor_type = NO_CURSOR;
27720 /* EXPORT:
27721 Display or clear cursor of window W. If ON is zero, clear the
27722 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27723 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27725 void
27726 display_and_set_cursor (struct window *w, bool on,
27727 int hpos, int vpos, int x, int y)
27729 struct frame *f = XFRAME (w->frame);
27730 int new_cursor_type;
27731 int new_cursor_width;
27732 int active_cursor;
27733 struct glyph_row *glyph_row;
27734 struct glyph *glyph;
27736 /* This is pointless on invisible frames, and dangerous on garbaged
27737 windows and frames; in the latter case, the frame or window may
27738 be in the midst of changing its size, and x and y may be off the
27739 window. */
27740 if (! FRAME_VISIBLE_P (f)
27741 || FRAME_GARBAGED_P (f)
27742 || vpos >= w->current_matrix->nrows
27743 || hpos >= w->current_matrix->matrix_w)
27744 return;
27746 /* If cursor is off and we want it off, return quickly. */
27747 if (!on && !w->phys_cursor_on_p)
27748 return;
27750 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27751 /* If cursor row is not enabled, we don't really know where to
27752 display the cursor. */
27753 if (!glyph_row->enabled_p)
27755 w->phys_cursor_on_p = 0;
27756 return;
27759 glyph = NULL;
27760 if (!glyph_row->exact_window_width_line_p
27761 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27762 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27764 eassert (input_blocked_p ());
27766 /* Set new_cursor_type to the cursor we want to be displayed. */
27767 new_cursor_type = get_window_cursor_type (w, glyph,
27768 &new_cursor_width, &active_cursor);
27770 /* If cursor is currently being shown and we don't want it to be or
27771 it is in the wrong place, or the cursor type is not what we want,
27772 erase it. */
27773 if (w->phys_cursor_on_p
27774 && (!on
27775 || w->phys_cursor.x != x
27776 || w->phys_cursor.y != y
27777 /* HPOS can be negative in R2L rows whose
27778 exact_window_width_line_p flag is set (i.e. their newline
27779 would "overflow into the fringe"). */
27780 || hpos < 0
27781 || new_cursor_type != w->phys_cursor_type
27782 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27783 && new_cursor_width != w->phys_cursor_width)))
27784 erase_phys_cursor (w);
27786 /* Don't check phys_cursor_on_p here because that flag is only set
27787 to zero in some cases where we know that the cursor has been
27788 completely erased, to avoid the extra work of erasing the cursor
27789 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27790 still not be visible, or it has only been partly erased. */
27791 if (on)
27793 w->phys_cursor_ascent = glyph_row->ascent;
27794 w->phys_cursor_height = glyph_row->height;
27796 /* Set phys_cursor_.* before x_draw_.* is called because some
27797 of them may need the information. */
27798 w->phys_cursor.x = x;
27799 w->phys_cursor.y = glyph_row->y;
27800 w->phys_cursor.hpos = hpos;
27801 w->phys_cursor.vpos = vpos;
27804 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27805 new_cursor_type, new_cursor_width,
27806 on, active_cursor);
27810 /* Switch the display of W's cursor on or off, according to the value
27811 of ON. */
27813 static void
27814 update_window_cursor (struct window *w, bool on)
27816 /* Don't update cursor in windows whose frame is in the process
27817 of being deleted. */
27818 if (w->current_matrix)
27820 int hpos = w->phys_cursor.hpos;
27821 int vpos = w->phys_cursor.vpos;
27822 struct glyph_row *row;
27824 if (vpos >= w->current_matrix->nrows
27825 || hpos >= w->current_matrix->matrix_w)
27826 return;
27828 row = MATRIX_ROW (w->current_matrix, vpos);
27830 /* When the window is hscrolled, cursor hpos can legitimately be
27831 out of bounds, but we draw the cursor at the corresponding
27832 window margin in that case. */
27833 if (!row->reversed_p && hpos < 0)
27834 hpos = 0;
27835 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27836 hpos = row->used[TEXT_AREA] - 1;
27838 block_input ();
27839 display_and_set_cursor (w, on, hpos, vpos,
27840 w->phys_cursor.x, w->phys_cursor.y);
27841 unblock_input ();
27846 /* Call update_window_cursor with parameter ON_P on all leaf windows
27847 in the window tree rooted at W. */
27849 static void
27850 update_cursor_in_window_tree (struct window *w, bool on_p)
27852 while (w)
27854 if (WINDOWP (w->contents))
27855 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27856 else
27857 update_window_cursor (w, on_p);
27859 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27864 /* EXPORT:
27865 Display the cursor on window W, or clear it, according to ON_P.
27866 Don't change the cursor's position. */
27868 void
27869 x_update_cursor (struct frame *f, bool on_p)
27871 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27875 /* EXPORT:
27876 Clear the cursor of window W to background color, and mark the
27877 cursor as not shown. This is used when the text where the cursor
27878 is about to be rewritten. */
27880 void
27881 x_clear_cursor (struct window *w)
27883 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27884 update_window_cursor (w, 0);
27887 #endif /* HAVE_WINDOW_SYSTEM */
27889 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27890 and MSDOS. */
27891 static void
27892 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27893 int start_hpos, int end_hpos,
27894 enum draw_glyphs_face draw)
27896 #ifdef HAVE_WINDOW_SYSTEM
27897 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27899 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27900 return;
27902 #endif
27903 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27904 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27905 #endif
27908 /* Display the active region described by mouse_face_* according to DRAW. */
27910 static void
27911 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27913 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27914 struct frame *f = XFRAME (WINDOW_FRAME (w));
27916 if (/* If window is in the process of being destroyed, don't bother
27917 to do anything. */
27918 w->current_matrix != NULL
27919 /* Don't update mouse highlight if hidden. */
27920 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27921 /* Recognize when we are called to operate on rows that don't exist
27922 anymore. This can happen when a window is split. */
27923 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27925 int phys_cursor_on_p = w->phys_cursor_on_p;
27926 struct glyph_row *row, *first, *last;
27928 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27929 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27931 for (row = first; row <= last && row->enabled_p; ++row)
27933 int start_hpos, end_hpos, start_x;
27935 /* For all but the first row, the highlight starts at column 0. */
27936 if (row == first)
27938 /* R2L rows have BEG and END in reversed order, but the
27939 screen drawing geometry is always left to right. So
27940 we need to mirror the beginning and end of the
27941 highlighted area in R2L rows. */
27942 if (!row->reversed_p)
27944 start_hpos = hlinfo->mouse_face_beg_col;
27945 start_x = hlinfo->mouse_face_beg_x;
27947 else if (row == last)
27949 start_hpos = hlinfo->mouse_face_end_col;
27950 start_x = hlinfo->mouse_face_end_x;
27952 else
27954 start_hpos = 0;
27955 start_x = 0;
27958 else if (row->reversed_p && row == last)
27960 start_hpos = hlinfo->mouse_face_end_col;
27961 start_x = hlinfo->mouse_face_end_x;
27963 else
27965 start_hpos = 0;
27966 start_x = 0;
27969 if (row == last)
27971 if (!row->reversed_p)
27972 end_hpos = hlinfo->mouse_face_end_col;
27973 else if (row == first)
27974 end_hpos = hlinfo->mouse_face_beg_col;
27975 else
27977 end_hpos = row->used[TEXT_AREA];
27978 if (draw == DRAW_NORMAL_TEXT)
27979 row->fill_line_p = 1; /* Clear to end of line */
27982 else if (row->reversed_p && row == first)
27983 end_hpos = hlinfo->mouse_face_beg_col;
27984 else
27986 end_hpos = row->used[TEXT_AREA];
27987 if (draw == DRAW_NORMAL_TEXT)
27988 row->fill_line_p = 1; /* Clear to end of line */
27991 if (end_hpos > start_hpos)
27993 draw_row_with_mouse_face (w, start_x, row,
27994 start_hpos, end_hpos, draw);
27996 row->mouse_face_p
27997 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28001 #ifdef HAVE_WINDOW_SYSTEM
28002 /* When we've written over the cursor, arrange for it to
28003 be displayed again. */
28004 if (FRAME_WINDOW_P (f)
28005 && phys_cursor_on_p && !w->phys_cursor_on_p)
28007 int hpos = w->phys_cursor.hpos;
28009 /* When the window is hscrolled, cursor hpos can legitimately be
28010 out of bounds, but we draw the cursor at the corresponding
28011 window margin in that case. */
28012 if (!row->reversed_p && hpos < 0)
28013 hpos = 0;
28014 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28015 hpos = row->used[TEXT_AREA] - 1;
28017 block_input ();
28018 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28019 w->phys_cursor.x, w->phys_cursor.y);
28020 unblock_input ();
28022 #endif /* HAVE_WINDOW_SYSTEM */
28025 #ifdef HAVE_WINDOW_SYSTEM
28026 /* Change the mouse cursor. */
28027 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28029 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28030 if (draw == DRAW_NORMAL_TEXT
28031 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28032 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28033 else
28034 #endif
28035 if (draw == DRAW_MOUSE_FACE)
28036 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28037 else
28038 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28040 #endif /* HAVE_WINDOW_SYSTEM */
28043 /* EXPORT:
28044 Clear out the mouse-highlighted active region.
28045 Redraw it un-highlighted first. Value is non-zero if mouse
28046 face was actually drawn unhighlighted. */
28049 clear_mouse_face (Mouse_HLInfo *hlinfo)
28051 int cleared = 0;
28053 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28055 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28056 cleared = 1;
28059 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28060 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28061 hlinfo->mouse_face_window = Qnil;
28062 hlinfo->mouse_face_overlay = Qnil;
28063 return cleared;
28066 /* Return true if the coordinates HPOS and VPOS on windows W are
28067 within the mouse face on that window. */
28068 static bool
28069 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28071 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28073 /* Quickly resolve the easy cases. */
28074 if (!(WINDOWP (hlinfo->mouse_face_window)
28075 && XWINDOW (hlinfo->mouse_face_window) == w))
28076 return false;
28077 if (vpos < hlinfo->mouse_face_beg_row
28078 || vpos > hlinfo->mouse_face_end_row)
28079 return false;
28080 if (vpos > hlinfo->mouse_face_beg_row
28081 && vpos < hlinfo->mouse_face_end_row)
28082 return true;
28084 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28086 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28088 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28089 return true;
28091 else if ((vpos == hlinfo->mouse_face_beg_row
28092 && hpos >= hlinfo->mouse_face_beg_col)
28093 || (vpos == hlinfo->mouse_face_end_row
28094 && hpos < hlinfo->mouse_face_end_col))
28095 return true;
28097 else
28099 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28101 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28102 return true;
28104 else if ((vpos == hlinfo->mouse_face_beg_row
28105 && hpos <= hlinfo->mouse_face_beg_col)
28106 || (vpos == hlinfo->mouse_face_end_row
28107 && hpos > hlinfo->mouse_face_end_col))
28108 return true;
28110 return false;
28114 /* EXPORT:
28115 True if physical cursor of window W is within mouse face. */
28117 bool
28118 cursor_in_mouse_face_p (struct window *w)
28120 int hpos = w->phys_cursor.hpos;
28121 int vpos = w->phys_cursor.vpos;
28122 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28124 /* When the window is hscrolled, cursor hpos can legitimately be out
28125 of bounds, but we draw the cursor at the corresponding window
28126 margin in that case. */
28127 if (!row->reversed_p && hpos < 0)
28128 hpos = 0;
28129 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28130 hpos = row->used[TEXT_AREA] - 1;
28132 return coords_in_mouse_face_p (w, hpos, vpos);
28137 /* Find the glyph rows START_ROW and END_ROW of window W that display
28138 characters between buffer positions START_CHARPOS and END_CHARPOS
28139 (excluding END_CHARPOS). DISP_STRING is a display string that
28140 covers these buffer positions. This is similar to
28141 row_containing_pos, but is more accurate when bidi reordering makes
28142 buffer positions change non-linearly with glyph rows. */
28143 static void
28144 rows_from_pos_range (struct window *w,
28145 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28146 Lisp_Object disp_string,
28147 struct glyph_row **start, struct glyph_row **end)
28149 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28150 int last_y = window_text_bottom_y (w);
28151 struct glyph_row *row;
28153 *start = NULL;
28154 *end = NULL;
28156 while (!first->enabled_p
28157 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28158 first++;
28160 /* Find the START row. */
28161 for (row = first;
28162 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28163 row++)
28165 /* A row can potentially be the START row if the range of the
28166 characters it displays intersects the range
28167 [START_CHARPOS..END_CHARPOS). */
28168 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28169 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28170 /* See the commentary in row_containing_pos, for the
28171 explanation of the complicated way to check whether
28172 some position is beyond the end of the characters
28173 displayed by a row. */
28174 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28175 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28176 && !row->ends_at_zv_p
28177 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28178 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28179 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28180 && !row->ends_at_zv_p
28181 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28183 /* Found a candidate row. Now make sure at least one of the
28184 glyphs it displays has a charpos from the range
28185 [START_CHARPOS..END_CHARPOS).
28187 This is not obvious because bidi reordering could make
28188 buffer positions of a row be 1,2,3,102,101,100, and if we
28189 want to highlight characters in [50..60), we don't want
28190 this row, even though [50..60) does intersect [1..103),
28191 the range of character positions given by the row's start
28192 and end positions. */
28193 struct glyph *g = row->glyphs[TEXT_AREA];
28194 struct glyph *e = g + row->used[TEXT_AREA];
28196 while (g < e)
28198 if (((BUFFERP (g->object) || NILP (g->object))
28199 && start_charpos <= g->charpos && g->charpos < end_charpos)
28200 /* A glyph that comes from DISP_STRING is by
28201 definition to be highlighted. */
28202 || EQ (g->object, disp_string))
28203 *start = row;
28204 g++;
28206 if (*start)
28207 break;
28211 /* Find the END row. */
28212 if (!*start
28213 /* If the last row is partially visible, start looking for END
28214 from that row, instead of starting from FIRST. */
28215 && !(row->enabled_p
28216 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28217 row = first;
28218 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28220 struct glyph_row *next = row + 1;
28221 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28223 if (!next->enabled_p
28224 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28225 /* The first row >= START whose range of displayed characters
28226 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28227 is the row END + 1. */
28228 || (start_charpos < next_start
28229 && end_charpos < next_start)
28230 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28231 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28232 && !next->ends_at_zv_p
28233 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28234 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28235 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28236 && !next->ends_at_zv_p
28237 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28239 *end = row;
28240 break;
28242 else
28244 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28245 but none of the characters it displays are in the range, it is
28246 also END + 1. */
28247 struct glyph *g = next->glyphs[TEXT_AREA];
28248 struct glyph *s = g;
28249 struct glyph *e = g + next->used[TEXT_AREA];
28251 while (g < e)
28253 if (((BUFFERP (g->object) || NILP (g->object))
28254 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28255 /* If the buffer position of the first glyph in
28256 the row is equal to END_CHARPOS, it means
28257 the last character to be highlighted is the
28258 newline of ROW, and we must consider NEXT as
28259 END, not END+1. */
28260 || (((!next->reversed_p && g == s)
28261 || (next->reversed_p && g == e - 1))
28262 && (g->charpos == end_charpos
28263 /* Special case for when NEXT is an
28264 empty line at ZV. */
28265 || (g->charpos == -1
28266 && !row->ends_at_zv_p
28267 && next_start == end_charpos)))))
28268 /* A glyph that comes from DISP_STRING is by
28269 definition to be highlighted. */
28270 || EQ (g->object, disp_string))
28271 break;
28272 g++;
28274 if (g == e)
28276 *end = row;
28277 break;
28279 /* The first row that ends at ZV must be the last to be
28280 highlighted. */
28281 else if (next->ends_at_zv_p)
28283 *end = next;
28284 break;
28290 /* This function sets the mouse_face_* elements of HLINFO, assuming
28291 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28292 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28293 for the overlay or run of text properties specifying the mouse
28294 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28295 before-string and after-string that must also be highlighted.
28296 DISP_STRING, if non-nil, is a display string that may cover some
28297 or all of the highlighted text. */
28299 static void
28300 mouse_face_from_buffer_pos (Lisp_Object window,
28301 Mouse_HLInfo *hlinfo,
28302 ptrdiff_t mouse_charpos,
28303 ptrdiff_t start_charpos,
28304 ptrdiff_t end_charpos,
28305 Lisp_Object before_string,
28306 Lisp_Object after_string,
28307 Lisp_Object disp_string)
28309 struct window *w = XWINDOW (window);
28310 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28311 struct glyph_row *r1, *r2;
28312 struct glyph *glyph, *end;
28313 ptrdiff_t ignore, pos;
28314 int x;
28316 eassert (NILP (disp_string) || STRINGP (disp_string));
28317 eassert (NILP (before_string) || STRINGP (before_string));
28318 eassert (NILP (after_string) || STRINGP (after_string));
28320 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28321 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28322 if (r1 == NULL)
28323 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28324 /* If the before-string or display-string contains newlines,
28325 rows_from_pos_range skips to its last row. Move back. */
28326 if (!NILP (before_string) || !NILP (disp_string))
28328 struct glyph_row *prev;
28329 while ((prev = r1 - 1, prev >= first)
28330 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28331 && prev->used[TEXT_AREA] > 0)
28333 struct glyph *beg = prev->glyphs[TEXT_AREA];
28334 glyph = beg + prev->used[TEXT_AREA];
28335 while (--glyph >= beg && NILP (glyph->object));
28336 if (glyph < beg
28337 || !(EQ (glyph->object, before_string)
28338 || EQ (glyph->object, disp_string)))
28339 break;
28340 r1 = prev;
28343 if (r2 == NULL)
28345 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28346 hlinfo->mouse_face_past_end = 1;
28348 else if (!NILP (after_string))
28350 /* If the after-string has newlines, advance to its last row. */
28351 struct glyph_row *next;
28352 struct glyph_row *last
28353 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28355 for (next = r2 + 1;
28356 next <= last
28357 && next->used[TEXT_AREA] > 0
28358 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28359 ++next)
28360 r2 = next;
28362 /* The rest of the display engine assumes that mouse_face_beg_row is
28363 either above mouse_face_end_row or identical to it. But with
28364 bidi-reordered continued lines, the row for START_CHARPOS could
28365 be below the row for END_CHARPOS. If so, swap the rows and store
28366 them in correct order. */
28367 if (r1->y > r2->y)
28369 struct glyph_row *tem = r2;
28371 r2 = r1;
28372 r1 = tem;
28375 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28376 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28378 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28379 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28380 could be anywhere in the row and in any order. The strategy
28381 below is to find the leftmost and the rightmost glyph that
28382 belongs to either of these 3 strings, or whose position is
28383 between START_CHARPOS and END_CHARPOS, and highlight all the
28384 glyphs between those two. This may cover more than just the text
28385 between START_CHARPOS and END_CHARPOS if the range of characters
28386 strides the bidi level boundary, e.g. if the beginning is in R2L
28387 text while the end is in L2R text or vice versa. */
28388 if (!r1->reversed_p)
28390 /* This row is in a left to right paragraph. Scan it left to
28391 right. */
28392 glyph = r1->glyphs[TEXT_AREA];
28393 end = glyph + r1->used[TEXT_AREA];
28394 x = r1->x;
28396 /* Skip truncation glyphs at the start of the glyph row. */
28397 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28398 for (; glyph < end
28399 && NILP (glyph->object)
28400 && glyph->charpos < 0;
28401 ++glyph)
28402 x += glyph->pixel_width;
28404 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28405 or DISP_STRING, and the first glyph from buffer whose
28406 position is between START_CHARPOS and END_CHARPOS. */
28407 for (; glyph < end
28408 && !NILP (glyph->object)
28409 && !EQ (glyph->object, disp_string)
28410 && !(BUFFERP (glyph->object)
28411 && (glyph->charpos >= start_charpos
28412 && glyph->charpos < end_charpos));
28413 ++glyph)
28415 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28416 are present at buffer positions between START_CHARPOS and
28417 END_CHARPOS, or if they come from an overlay. */
28418 if (EQ (glyph->object, before_string))
28420 pos = string_buffer_position (before_string,
28421 start_charpos);
28422 /* If pos == 0, it means before_string came from an
28423 overlay, not from a buffer position. */
28424 if (!pos || (pos >= start_charpos && pos < end_charpos))
28425 break;
28427 else if (EQ (glyph->object, after_string))
28429 pos = string_buffer_position (after_string, end_charpos);
28430 if (!pos || (pos >= start_charpos && pos < end_charpos))
28431 break;
28433 x += glyph->pixel_width;
28435 hlinfo->mouse_face_beg_x = x;
28436 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28438 else
28440 /* This row is in a right to left paragraph. Scan it right to
28441 left. */
28442 struct glyph *g;
28444 end = r1->glyphs[TEXT_AREA] - 1;
28445 glyph = end + r1->used[TEXT_AREA];
28447 /* Skip truncation glyphs at the start of the glyph row. */
28448 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28449 for (; glyph > end
28450 && NILP (glyph->object)
28451 && glyph->charpos < 0;
28452 --glyph)
28455 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28456 or DISP_STRING, and the first glyph from buffer whose
28457 position is between START_CHARPOS and END_CHARPOS. */
28458 for (; glyph > end
28459 && !NILP (glyph->object)
28460 && !EQ (glyph->object, disp_string)
28461 && !(BUFFERP (glyph->object)
28462 && (glyph->charpos >= start_charpos
28463 && glyph->charpos < end_charpos));
28464 --glyph)
28466 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28467 are present at buffer positions between START_CHARPOS and
28468 END_CHARPOS, or if they come from an overlay. */
28469 if (EQ (glyph->object, before_string))
28471 pos = string_buffer_position (before_string, start_charpos);
28472 /* If pos == 0, it means before_string came from an
28473 overlay, not from a buffer position. */
28474 if (!pos || (pos >= start_charpos && pos < end_charpos))
28475 break;
28477 else if (EQ (glyph->object, after_string))
28479 pos = string_buffer_position (after_string, end_charpos);
28480 if (!pos || (pos >= start_charpos && pos < end_charpos))
28481 break;
28485 glyph++; /* first glyph to the right of the highlighted area */
28486 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28487 x += g->pixel_width;
28488 hlinfo->mouse_face_beg_x = x;
28489 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28492 /* If the highlight ends in a different row, compute GLYPH and END
28493 for the end row. Otherwise, reuse the values computed above for
28494 the row where the highlight begins. */
28495 if (r2 != r1)
28497 if (!r2->reversed_p)
28499 glyph = r2->glyphs[TEXT_AREA];
28500 end = glyph + r2->used[TEXT_AREA];
28501 x = r2->x;
28503 else
28505 end = r2->glyphs[TEXT_AREA] - 1;
28506 glyph = end + r2->used[TEXT_AREA];
28510 if (!r2->reversed_p)
28512 /* Skip truncation and continuation glyphs near the end of the
28513 row, and also blanks and stretch glyphs inserted by
28514 extend_face_to_end_of_line. */
28515 while (end > glyph
28516 && NILP ((end - 1)->object))
28517 --end;
28518 /* Scan the rest of the glyph row from the end, looking for the
28519 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28520 DISP_STRING, or whose position is between START_CHARPOS
28521 and END_CHARPOS */
28522 for (--end;
28523 end > glyph
28524 && !NILP (end->object)
28525 && !EQ (end->object, disp_string)
28526 && !(BUFFERP (end->object)
28527 && (end->charpos >= start_charpos
28528 && end->charpos < end_charpos));
28529 --end)
28531 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28532 are present at buffer positions between START_CHARPOS and
28533 END_CHARPOS, or if they come from an overlay. */
28534 if (EQ (end->object, before_string))
28536 pos = string_buffer_position (before_string, start_charpos);
28537 if (!pos || (pos >= start_charpos && pos < end_charpos))
28538 break;
28540 else if (EQ (end->object, after_string))
28542 pos = string_buffer_position (after_string, end_charpos);
28543 if (!pos || (pos >= start_charpos && pos < end_charpos))
28544 break;
28547 /* Find the X coordinate of the last glyph to be highlighted. */
28548 for (; glyph <= end; ++glyph)
28549 x += glyph->pixel_width;
28551 hlinfo->mouse_face_end_x = x;
28552 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28554 else
28556 /* Skip truncation and continuation glyphs near the end of the
28557 row, and also blanks and stretch glyphs inserted by
28558 extend_face_to_end_of_line. */
28559 x = r2->x;
28560 end++;
28561 while (end < glyph
28562 && NILP (end->object))
28564 x += end->pixel_width;
28565 ++end;
28567 /* Scan the rest of the glyph row from the end, looking for the
28568 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28569 DISP_STRING, or whose position is between START_CHARPOS
28570 and END_CHARPOS */
28571 for ( ;
28572 end < glyph
28573 && !NILP (end->object)
28574 && !EQ (end->object, disp_string)
28575 && !(BUFFERP (end->object)
28576 && (end->charpos >= start_charpos
28577 && end->charpos < end_charpos));
28578 ++end)
28580 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28581 are present at buffer positions between START_CHARPOS and
28582 END_CHARPOS, or if they come from an overlay. */
28583 if (EQ (end->object, before_string))
28585 pos = string_buffer_position (before_string, start_charpos);
28586 if (!pos || (pos >= start_charpos && pos < end_charpos))
28587 break;
28589 else if (EQ (end->object, after_string))
28591 pos = string_buffer_position (after_string, end_charpos);
28592 if (!pos || (pos >= start_charpos && pos < end_charpos))
28593 break;
28595 x += end->pixel_width;
28597 /* If we exited the above loop because we arrived at the last
28598 glyph of the row, and its buffer position is still not in
28599 range, it means the last character in range is the preceding
28600 newline. Bump the end column and x values to get past the
28601 last glyph. */
28602 if (end == glyph
28603 && BUFFERP (end->object)
28604 && (end->charpos < start_charpos
28605 || end->charpos >= end_charpos))
28607 x += end->pixel_width;
28608 ++end;
28610 hlinfo->mouse_face_end_x = x;
28611 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28614 hlinfo->mouse_face_window = window;
28615 hlinfo->mouse_face_face_id
28616 = face_at_buffer_position (w, mouse_charpos, &ignore,
28617 mouse_charpos + 1,
28618 !hlinfo->mouse_face_hidden, -1);
28619 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28622 /* The following function is not used anymore (replaced with
28623 mouse_face_from_string_pos), but I leave it here for the time
28624 being, in case someone would. */
28626 #if 0 /* not used */
28628 /* Find the position of the glyph for position POS in OBJECT in
28629 window W's current matrix, and return in *X, *Y the pixel
28630 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28632 RIGHT_P non-zero means return the position of the right edge of the
28633 glyph, RIGHT_P zero means return the left edge position.
28635 If no glyph for POS exists in the matrix, return the position of
28636 the glyph with the next smaller position that is in the matrix, if
28637 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28638 exists in the matrix, return the position of the glyph with the
28639 next larger position in OBJECT.
28641 Value is non-zero if a glyph was found. */
28643 static int
28644 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28645 int *hpos, int *vpos, int *x, int *y, int right_p)
28647 int yb = window_text_bottom_y (w);
28648 struct glyph_row *r;
28649 struct glyph *best_glyph = NULL;
28650 struct glyph_row *best_row = NULL;
28651 int best_x = 0;
28653 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28654 r->enabled_p && r->y < yb;
28655 ++r)
28657 struct glyph *g = r->glyphs[TEXT_AREA];
28658 struct glyph *e = g + r->used[TEXT_AREA];
28659 int gx;
28661 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28662 if (EQ (g->object, object))
28664 if (g->charpos == pos)
28666 best_glyph = g;
28667 best_x = gx;
28668 best_row = r;
28669 goto found;
28671 else if (best_glyph == NULL
28672 || ((eabs (g->charpos - pos)
28673 < eabs (best_glyph->charpos - pos))
28674 && (right_p
28675 ? g->charpos < pos
28676 : g->charpos > pos)))
28678 best_glyph = g;
28679 best_x = gx;
28680 best_row = r;
28685 found:
28687 if (best_glyph)
28689 *x = best_x;
28690 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28692 if (right_p)
28694 *x += best_glyph->pixel_width;
28695 ++*hpos;
28698 *y = best_row->y;
28699 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28702 return best_glyph != NULL;
28704 #endif /* not used */
28706 /* Find the positions of the first and the last glyphs in window W's
28707 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28708 (assumed to be a string), and return in HLINFO's mouse_face_*
28709 members the pixel and column/row coordinates of those glyphs. */
28711 static void
28712 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28713 Lisp_Object object,
28714 ptrdiff_t startpos, ptrdiff_t endpos)
28716 int yb = window_text_bottom_y (w);
28717 struct glyph_row *r;
28718 struct glyph *g, *e;
28719 int gx;
28720 int found = 0;
28722 /* Find the glyph row with at least one position in the range
28723 [STARTPOS..ENDPOS), and the first glyph in that row whose
28724 position belongs to that range. */
28725 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28726 r->enabled_p && r->y < yb;
28727 ++r)
28729 if (!r->reversed_p)
28731 g = r->glyphs[TEXT_AREA];
28732 e = g + r->used[TEXT_AREA];
28733 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28734 if (EQ (g->object, object)
28735 && startpos <= g->charpos && g->charpos < endpos)
28737 hlinfo->mouse_face_beg_row
28738 = MATRIX_ROW_VPOS (r, w->current_matrix);
28739 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28740 hlinfo->mouse_face_beg_x = gx;
28741 found = 1;
28742 break;
28745 else
28747 struct glyph *g1;
28749 e = r->glyphs[TEXT_AREA];
28750 g = e + r->used[TEXT_AREA];
28751 for ( ; g > e; --g)
28752 if (EQ ((g-1)->object, object)
28753 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28755 hlinfo->mouse_face_beg_row
28756 = MATRIX_ROW_VPOS (r, w->current_matrix);
28757 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28758 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28759 gx += g1->pixel_width;
28760 hlinfo->mouse_face_beg_x = gx;
28761 found = 1;
28762 break;
28765 if (found)
28766 break;
28769 if (!found)
28770 return;
28772 /* Starting with the next row, look for the first row which does NOT
28773 include any glyphs whose positions are in the range. */
28774 for (++r; r->enabled_p && r->y < yb; ++r)
28776 g = r->glyphs[TEXT_AREA];
28777 e = g + r->used[TEXT_AREA];
28778 found = 0;
28779 for ( ; g < e; ++g)
28780 if (EQ (g->object, object)
28781 && startpos <= g->charpos && g->charpos < endpos)
28783 found = 1;
28784 break;
28786 if (!found)
28787 break;
28790 /* The highlighted region ends on the previous row. */
28791 r--;
28793 /* Set the end row. */
28794 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28796 /* Compute and set the end column and the end column's horizontal
28797 pixel coordinate. */
28798 if (!r->reversed_p)
28800 g = r->glyphs[TEXT_AREA];
28801 e = g + r->used[TEXT_AREA];
28802 for ( ; e > g; --e)
28803 if (EQ ((e-1)->object, object)
28804 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28805 break;
28806 hlinfo->mouse_face_end_col = e - g;
28808 for (gx = r->x; g < e; ++g)
28809 gx += g->pixel_width;
28810 hlinfo->mouse_face_end_x = gx;
28812 else
28814 e = r->glyphs[TEXT_AREA];
28815 g = e + r->used[TEXT_AREA];
28816 for (gx = r->x ; e < g; ++e)
28818 if (EQ (e->object, object)
28819 && startpos <= e->charpos && e->charpos < endpos)
28820 break;
28821 gx += e->pixel_width;
28823 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28824 hlinfo->mouse_face_end_x = gx;
28828 #ifdef HAVE_WINDOW_SYSTEM
28830 /* See if position X, Y is within a hot-spot of an image. */
28832 static int
28833 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28835 if (!CONSP (hot_spot))
28836 return 0;
28838 if (EQ (XCAR (hot_spot), Qrect))
28840 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28841 Lisp_Object rect = XCDR (hot_spot);
28842 Lisp_Object tem;
28843 if (!CONSP (rect))
28844 return 0;
28845 if (!CONSP (XCAR (rect)))
28846 return 0;
28847 if (!CONSP (XCDR (rect)))
28848 return 0;
28849 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28850 return 0;
28851 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28852 return 0;
28853 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28854 return 0;
28855 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28856 return 0;
28857 return 1;
28859 else if (EQ (XCAR (hot_spot), Qcircle))
28861 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28862 Lisp_Object circ = XCDR (hot_spot);
28863 Lisp_Object lr, lx0, ly0;
28864 if (CONSP (circ)
28865 && CONSP (XCAR (circ))
28866 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28867 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28868 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28870 double r = XFLOATINT (lr);
28871 double dx = XINT (lx0) - x;
28872 double dy = XINT (ly0) - y;
28873 return (dx * dx + dy * dy <= r * r);
28876 else if (EQ (XCAR (hot_spot), Qpoly))
28878 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28879 if (VECTORP (XCDR (hot_spot)))
28881 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28882 Lisp_Object *poly = v->contents;
28883 ptrdiff_t n = v->header.size;
28884 ptrdiff_t i;
28885 int inside = 0;
28886 Lisp_Object lx, ly;
28887 int x0, y0;
28889 /* Need an even number of coordinates, and at least 3 edges. */
28890 if (n < 6 || n & 1)
28891 return 0;
28893 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28894 If count is odd, we are inside polygon. Pixels on edges
28895 may or may not be included depending on actual geometry of the
28896 polygon. */
28897 if ((lx = poly[n-2], !INTEGERP (lx))
28898 || (ly = poly[n-1], !INTEGERP (lx)))
28899 return 0;
28900 x0 = XINT (lx), y0 = XINT (ly);
28901 for (i = 0; i < n; i += 2)
28903 int x1 = x0, y1 = y0;
28904 if ((lx = poly[i], !INTEGERP (lx))
28905 || (ly = poly[i+1], !INTEGERP (ly)))
28906 return 0;
28907 x0 = XINT (lx), y0 = XINT (ly);
28909 /* Does this segment cross the X line? */
28910 if (x0 >= x)
28912 if (x1 >= x)
28913 continue;
28915 else if (x1 < x)
28916 continue;
28917 if (y > y0 && y > y1)
28918 continue;
28919 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28920 inside = !inside;
28922 return inside;
28925 return 0;
28928 Lisp_Object
28929 find_hot_spot (Lisp_Object map, int x, int y)
28931 while (CONSP (map))
28933 if (CONSP (XCAR (map))
28934 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28935 return XCAR (map);
28936 map = XCDR (map);
28939 return Qnil;
28942 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28943 3, 3, 0,
28944 doc: /* Lookup in image map MAP coordinates X and Y.
28945 An image map is an alist where each element has the format (AREA ID PLIST).
28946 An AREA is specified as either a rectangle, a circle, or a polygon:
28947 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28948 pixel coordinates of the upper left and bottom right corners.
28949 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28950 and the radius of the circle; r may be a float or integer.
28951 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28952 vector describes one corner in the polygon.
28953 Returns the alist element for the first matching AREA in MAP. */)
28954 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28956 if (NILP (map))
28957 return Qnil;
28959 CHECK_NUMBER (x);
28960 CHECK_NUMBER (y);
28962 return find_hot_spot (map,
28963 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28964 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28968 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28969 static void
28970 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28972 /* Do not change cursor shape while dragging mouse. */
28973 if (!NILP (do_mouse_tracking))
28974 return;
28976 if (!NILP (pointer))
28978 if (EQ (pointer, Qarrow))
28979 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28980 else if (EQ (pointer, Qhand))
28981 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28982 else if (EQ (pointer, Qtext))
28983 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28984 else if (EQ (pointer, intern ("hdrag")))
28985 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28986 else if (EQ (pointer, intern ("nhdrag")))
28987 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28988 #ifdef HAVE_X_WINDOWS
28989 else if (EQ (pointer, intern ("vdrag")))
28990 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28991 #endif
28992 else if (EQ (pointer, intern ("hourglass")))
28993 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28994 else if (EQ (pointer, Qmodeline))
28995 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28996 else
28997 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29000 if (cursor != No_Cursor)
29001 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29004 #endif /* HAVE_WINDOW_SYSTEM */
29006 /* Take proper action when mouse has moved to the mode or header line
29007 or marginal area AREA of window W, x-position X and y-position Y.
29008 X is relative to the start of the text display area of W, so the
29009 width of bitmap areas and scroll bars must be subtracted to get a
29010 position relative to the start of the mode line. */
29012 static void
29013 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29014 enum window_part area)
29016 struct window *w = XWINDOW (window);
29017 struct frame *f = XFRAME (w->frame);
29018 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29019 #ifdef HAVE_WINDOW_SYSTEM
29020 Display_Info *dpyinfo;
29021 #endif
29022 Cursor cursor = No_Cursor;
29023 Lisp_Object pointer = Qnil;
29024 int dx, dy, width, height;
29025 ptrdiff_t charpos;
29026 Lisp_Object string, object = Qnil;
29027 Lisp_Object pos IF_LINT (= Qnil), help;
29029 Lisp_Object mouse_face;
29030 int original_x_pixel = x;
29031 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29032 struct glyph_row *row IF_LINT (= 0);
29034 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29036 int x0;
29037 struct glyph *end;
29039 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29040 returns them in row/column units! */
29041 string = mode_line_string (w, area, &x, &y, &charpos,
29042 &object, &dx, &dy, &width, &height);
29044 row = (area == ON_MODE_LINE
29045 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29046 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29048 /* Find the glyph under the mouse pointer. */
29049 if (row->mode_line_p && row->enabled_p)
29051 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29052 end = glyph + row->used[TEXT_AREA];
29054 for (x0 = original_x_pixel;
29055 glyph < end && x0 >= glyph->pixel_width;
29056 ++glyph)
29057 x0 -= glyph->pixel_width;
29059 if (glyph >= end)
29060 glyph = NULL;
29063 else
29065 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29066 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29067 returns them in row/column units! */
29068 string = marginal_area_string (w, area, &x, &y, &charpos,
29069 &object, &dx, &dy, &width, &height);
29072 help = Qnil;
29074 #ifdef HAVE_WINDOW_SYSTEM
29075 if (IMAGEP (object))
29077 Lisp_Object image_map, hotspot;
29078 if ((image_map = Fplist_get (XCDR (object), QCmap),
29079 !NILP (image_map))
29080 && (hotspot = find_hot_spot (image_map, dx, dy),
29081 CONSP (hotspot))
29082 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29084 Lisp_Object plist;
29086 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29087 If so, we could look for mouse-enter, mouse-leave
29088 properties in PLIST (and do something...). */
29089 hotspot = XCDR (hotspot);
29090 if (CONSP (hotspot)
29091 && (plist = XCAR (hotspot), CONSP (plist)))
29093 pointer = Fplist_get (plist, Qpointer);
29094 if (NILP (pointer))
29095 pointer = Qhand;
29096 help = Fplist_get (plist, Qhelp_echo);
29097 if (!NILP (help))
29099 help_echo_string = help;
29100 XSETWINDOW (help_echo_window, w);
29101 help_echo_object = w->contents;
29102 help_echo_pos = charpos;
29106 if (NILP (pointer))
29107 pointer = Fplist_get (XCDR (object), QCpointer);
29109 #endif /* HAVE_WINDOW_SYSTEM */
29111 if (STRINGP (string))
29112 pos = make_number (charpos);
29114 /* Set the help text and mouse pointer. If the mouse is on a part
29115 of the mode line without any text (e.g. past the right edge of
29116 the mode line text), use the default help text and pointer. */
29117 if (STRINGP (string) || area == ON_MODE_LINE)
29119 /* Arrange to display the help by setting the global variables
29120 help_echo_string, help_echo_object, and help_echo_pos. */
29121 if (NILP (help))
29123 if (STRINGP (string))
29124 help = Fget_text_property (pos, Qhelp_echo, string);
29126 if (!NILP (help))
29128 help_echo_string = help;
29129 XSETWINDOW (help_echo_window, w);
29130 help_echo_object = string;
29131 help_echo_pos = charpos;
29133 else if (area == ON_MODE_LINE)
29135 Lisp_Object default_help
29136 = buffer_local_value (Qmode_line_default_help_echo,
29137 w->contents);
29139 if (STRINGP (default_help))
29141 help_echo_string = default_help;
29142 XSETWINDOW (help_echo_window, w);
29143 help_echo_object = Qnil;
29144 help_echo_pos = -1;
29149 #ifdef HAVE_WINDOW_SYSTEM
29150 /* Change the mouse pointer according to what is under it. */
29151 if (FRAME_WINDOW_P (f))
29153 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29154 || minibuf_level
29155 || NILP (Vresize_mini_windows));
29157 dpyinfo = FRAME_DISPLAY_INFO (f);
29158 if (STRINGP (string))
29160 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29162 if (NILP (pointer))
29163 pointer = Fget_text_property (pos, Qpointer, string);
29165 /* Change the mouse pointer according to what is under X/Y. */
29166 if (NILP (pointer)
29167 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29169 Lisp_Object map;
29170 map = Fget_text_property (pos, Qlocal_map, string);
29171 if (!KEYMAPP (map))
29172 map = Fget_text_property (pos, Qkeymap, string);
29173 if (!KEYMAPP (map) && draggable)
29174 cursor = dpyinfo->vertical_scroll_bar_cursor;
29177 else if (draggable)
29178 /* Default mode-line pointer. */
29179 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29181 #endif
29184 /* Change the mouse face according to what is under X/Y. */
29185 if (STRINGP (string))
29187 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29188 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29189 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29190 && glyph)
29192 Lisp_Object b, e;
29194 struct glyph * tmp_glyph;
29196 int gpos;
29197 int gseq_length;
29198 int total_pixel_width;
29199 ptrdiff_t begpos, endpos, ignore;
29201 int vpos, hpos;
29203 b = Fprevious_single_property_change (make_number (charpos + 1),
29204 Qmouse_face, string, Qnil);
29205 if (NILP (b))
29206 begpos = 0;
29207 else
29208 begpos = XINT (b);
29210 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29211 if (NILP (e))
29212 endpos = SCHARS (string);
29213 else
29214 endpos = XINT (e);
29216 /* Calculate the glyph position GPOS of GLYPH in the
29217 displayed string, relative to the beginning of the
29218 highlighted part of the string.
29220 Note: GPOS is different from CHARPOS. CHARPOS is the
29221 position of GLYPH in the internal string object. A mode
29222 line string format has structures which are converted to
29223 a flattened string by the Emacs Lisp interpreter. The
29224 internal string is an element of those structures. The
29225 displayed string is the flattened string. */
29226 tmp_glyph = row_start_glyph;
29227 while (tmp_glyph < glyph
29228 && (!(EQ (tmp_glyph->object, glyph->object)
29229 && begpos <= tmp_glyph->charpos
29230 && tmp_glyph->charpos < endpos)))
29231 tmp_glyph++;
29232 gpos = glyph - tmp_glyph;
29234 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29235 the highlighted part of the displayed string to which
29236 GLYPH belongs. Note: GSEQ_LENGTH is different from
29237 SCHARS (STRING), because the latter returns the length of
29238 the internal string. */
29239 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29240 tmp_glyph > glyph
29241 && (!(EQ (tmp_glyph->object, glyph->object)
29242 && begpos <= tmp_glyph->charpos
29243 && tmp_glyph->charpos < endpos));
29244 tmp_glyph--)
29246 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29248 /* Calculate the total pixel width of all the glyphs between
29249 the beginning of the highlighted area and GLYPH. */
29250 total_pixel_width = 0;
29251 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29252 total_pixel_width += tmp_glyph->pixel_width;
29254 /* Pre calculation of re-rendering position. Note: X is in
29255 column units here, after the call to mode_line_string or
29256 marginal_area_string. */
29257 hpos = x - gpos;
29258 vpos = (area == ON_MODE_LINE
29259 ? (w->current_matrix)->nrows - 1
29260 : 0);
29262 /* If GLYPH's position is included in the region that is
29263 already drawn in mouse face, we have nothing to do. */
29264 if ( EQ (window, hlinfo->mouse_face_window)
29265 && (!row->reversed_p
29266 ? (hlinfo->mouse_face_beg_col <= hpos
29267 && hpos < hlinfo->mouse_face_end_col)
29268 /* In R2L rows we swap BEG and END, see below. */
29269 : (hlinfo->mouse_face_end_col <= hpos
29270 && hpos < hlinfo->mouse_face_beg_col))
29271 && hlinfo->mouse_face_beg_row == vpos )
29272 return;
29274 if (clear_mouse_face (hlinfo))
29275 cursor = No_Cursor;
29277 if (!row->reversed_p)
29279 hlinfo->mouse_face_beg_col = hpos;
29280 hlinfo->mouse_face_beg_x = original_x_pixel
29281 - (total_pixel_width + dx);
29282 hlinfo->mouse_face_end_col = hpos + gseq_length;
29283 hlinfo->mouse_face_end_x = 0;
29285 else
29287 /* In R2L rows, show_mouse_face expects BEG and END
29288 coordinates to be swapped. */
29289 hlinfo->mouse_face_end_col = hpos;
29290 hlinfo->mouse_face_end_x = original_x_pixel
29291 - (total_pixel_width + dx);
29292 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29293 hlinfo->mouse_face_beg_x = 0;
29296 hlinfo->mouse_face_beg_row = vpos;
29297 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29298 hlinfo->mouse_face_past_end = 0;
29299 hlinfo->mouse_face_window = window;
29301 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29302 charpos,
29303 0, &ignore,
29304 glyph->face_id,
29306 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29308 if (NILP (pointer))
29309 pointer = Qhand;
29311 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29312 clear_mouse_face (hlinfo);
29314 #ifdef HAVE_WINDOW_SYSTEM
29315 if (FRAME_WINDOW_P (f))
29316 define_frame_cursor1 (f, cursor, pointer);
29317 #endif
29321 /* EXPORT:
29322 Take proper action when the mouse has moved to position X, Y on
29323 frame F with regards to highlighting portions of display that have
29324 mouse-face properties. Also de-highlight portions of display where
29325 the mouse was before, set the mouse pointer shape as appropriate
29326 for the mouse coordinates, and activate help echo (tooltips).
29327 X and Y can be negative or out of range. */
29329 void
29330 note_mouse_highlight (struct frame *f, int x, int y)
29332 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29333 enum window_part part = ON_NOTHING;
29334 Lisp_Object window;
29335 struct window *w;
29336 Cursor cursor = No_Cursor;
29337 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29338 struct buffer *b;
29340 /* When a menu is active, don't highlight because this looks odd. */
29341 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29342 if (popup_activated ())
29343 return;
29344 #endif
29346 if (!f->glyphs_initialized_p
29347 || f->pointer_invisible)
29348 return;
29350 hlinfo->mouse_face_mouse_x = x;
29351 hlinfo->mouse_face_mouse_y = y;
29352 hlinfo->mouse_face_mouse_frame = f;
29354 if (hlinfo->mouse_face_defer)
29355 return;
29357 /* Which window is that in? */
29358 window = window_from_coordinates (f, x, y, &part, 1);
29360 /* If displaying active text in another window, clear that. */
29361 if (! EQ (window, hlinfo->mouse_face_window)
29362 /* Also clear if we move out of text area in same window. */
29363 || (!NILP (hlinfo->mouse_face_window)
29364 && !NILP (window)
29365 && part != ON_TEXT
29366 && part != ON_MODE_LINE
29367 && part != ON_HEADER_LINE))
29368 clear_mouse_face (hlinfo);
29370 /* Not on a window -> return. */
29371 if (!WINDOWP (window))
29372 return;
29374 /* Reset help_echo_string. It will get recomputed below. */
29375 help_echo_string = Qnil;
29377 /* Convert to window-relative pixel coordinates. */
29378 w = XWINDOW (window);
29379 frame_to_window_pixel_xy (w, &x, &y);
29381 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29382 /* Handle tool-bar window differently since it doesn't display a
29383 buffer. */
29384 if (EQ (window, f->tool_bar_window))
29386 note_tool_bar_highlight (f, x, y);
29387 return;
29389 #endif
29391 /* Mouse is on the mode, header line or margin? */
29392 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29393 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29395 note_mode_line_or_margin_highlight (window, x, y, part);
29397 #ifdef HAVE_WINDOW_SYSTEM
29398 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29400 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29401 /* Show non-text cursor (Bug#16647). */
29402 goto set_cursor;
29404 else
29405 #endif
29406 return;
29409 #ifdef HAVE_WINDOW_SYSTEM
29410 if (part == ON_VERTICAL_BORDER)
29412 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29413 help_echo_string = build_string ("drag-mouse-1: resize");
29415 else if (part == ON_RIGHT_DIVIDER)
29417 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29418 help_echo_string = build_string ("drag-mouse-1: resize");
29420 else if (part == ON_BOTTOM_DIVIDER)
29421 if (! WINDOW_BOTTOMMOST_P (w)
29422 || minibuf_level
29423 || NILP (Vresize_mini_windows))
29425 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29426 help_echo_string = build_string ("drag-mouse-1: resize");
29428 else
29429 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29430 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29431 || part == ON_VERTICAL_SCROLL_BAR
29432 || part == ON_HORIZONTAL_SCROLL_BAR)
29433 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29434 else
29435 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29436 #endif
29438 /* Are we in a window whose display is up to date?
29439 And verify the buffer's text has not changed. */
29440 b = XBUFFER (w->contents);
29441 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29443 int hpos, vpos, dx, dy, area = LAST_AREA;
29444 ptrdiff_t pos;
29445 struct glyph *glyph;
29446 Lisp_Object object;
29447 Lisp_Object mouse_face = Qnil, position;
29448 Lisp_Object *overlay_vec = NULL;
29449 ptrdiff_t i, noverlays;
29450 struct buffer *obuf;
29451 ptrdiff_t obegv, ozv;
29452 int same_region;
29454 /* Find the glyph under X/Y. */
29455 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29457 #ifdef HAVE_WINDOW_SYSTEM
29458 /* Look for :pointer property on image. */
29459 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29461 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29462 if (img != NULL && IMAGEP (img->spec))
29464 Lisp_Object image_map, hotspot;
29465 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29466 !NILP (image_map))
29467 && (hotspot = find_hot_spot (image_map,
29468 glyph->slice.img.x + dx,
29469 glyph->slice.img.y + dy),
29470 CONSP (hotspot))
29471 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29473 Lisp_Object plist;
29475 /* Could check XCAR (hotspot) to see if we enter/leave
29476 this hot-spot.
29477 If so, we could look for mouse-enter, mouse-leave
29478 properties in PLIST (and do something...). */
29479 hotspot = XCDR (hotspot);
29480 if (CONSP (hotspot)
29481 && (plist = XCAR (hotspot), CONSP (plist)))
29483 pointer = Fplist_get (plist, Qpointer);
29484 if (NILP (pointer))
29485 pointer = Qhand;
29486 help_echo_string = Fplist_get (plist, Qhelp_echo);
29487 if (!NILP (help_echo_string))
29489 help_echo_window = window;
29490 help_echo_object = glyph->object;
29491 help_echo_pos = glyph->charpos;
29495 if (NILP (pointer))
29496 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29499 #endif /* HAVE_WINDOW_SYSTEM */
29501 /* Clear mouse face if X/Y not over text. */
29502 if (glyph == NULL
29503 || area != TEXT_AREA
29504 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29505 /* Glyph's OBJECT is nil for glyphs inserted by the
29506 display engine for its internal purposes, like truncation
29507 and continuation glyphs and blanks beyond the end of
29508 line's text on text terminals. If we are over such a
29509 glyph, we are not over any text. */
29510 || NILP (glyph->object)
29511 /* R2L rows have a stretch glyph at their front, which
29512 stands for no text, whereas L2R rows have no glyphs at
29513 all beyond the end of text. Treat such stretch glyphs
29514 like we do with NULL glyphs in L2R rows. */
29515 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29516 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29517 && glyph->type == STRETCH_GLYPH
29518 && glyph->avoid_cursor_p))
29520 if (clear_mouse_face (hlinfo))
29521 cursor = No_Cursor;
29522 #ifdef HAVE_WINDOW_SYSTEM
29523 if (FRAME_WINDOW_P (f) && NILP (pointer))
29525 if (area != TEXT_AREA)
29526 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29527 else
29528 pointer = Vvoid_text_area_pointer;
29530 #endif
29531 goto set_cursor;
29534 pos = glyph->charpos;
29535 object = glyph->object;
29536 if (!STRINGP (object) && !BUFFERP (object))
29537 goto set_cursor;
29539 /* If we get an out-of-range value, return now; avoid an error. */
29540 if (BUFFERP (object) && pos > BUF_Z (b))
29541 goto set_cursor;
29543 /* Make the window's buffer temporarily current for
29544 overlays_at and compute_char_face. */
29545 obuf = current_buffer;
29546 current_buffer = b;
29547 obegv = BEGV;
29548 ozv = ZV;
29549 BEGV = BEG;
29550 ZV = Z;
29552 /* Is this char mouse-active or does it have help-echo? */
29553 position = make_number (pos);
29555 USE_SAFE_ALLOCA;
29557 if (BUFFERP (object))
29559 /* Put all the overlays we want in a vector in overlay_vec. */
29560 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29561 /* Sort overlays into increasing priority order. */
29562 noverlays = sort_overlays (overlay_vec, noverlays, w);
29564 else
29565 noverlays = 0;
29567 if (NILP (Vmouse_highlight))
29569 clear_mouse_face (hlinfo);
29570 goto check_help_echo;
29573 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29575 if (same_region)
29576 cursor = No_Cursor;
29578 /* Check mouse-face highlighting. */
29579 if (! same_region
29580 /* If there exists an overlay with mouse-face overlapping
29581 the one we are currently highlighting, we have to
29582 check if we enter the overlapping overlay, and then
29583 highlight only that. */
29584 || (OVERLAYP (hlinfo->mouse_face_overlay)
29585 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29587 /* Find the highest priority overlay with a mouse-face. */
29588 Lisp_Object overlay = Qnil;
29589 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29591 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29592 if (!NILP (mouse_face))
29593 overlay = overlay_vec[i];
29596 /* If we're highlighting the same overlay as before, there's
29597 no need to do that again. */
29598 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29599 goto check_help_echo;
29600 hlinfo->mouse_face_overlay = overlay;
29602 /* Clear the display of the old active region, if any. */
29603 if (clear_mouse_face (hlinfo))
29604 cursor = No_Cursor;
29606 /* If no overlay applies, get a text property. */
29607 if (NILP (overlay))
29608 mouse_face = Fget_text_property (position, Qmouse_face, object);
29610 /* Next, compute the bounds of the mouse highlighting and
29611 display it. */
29612 if (!NILP (mouse_face) && STRINGP (object))
29614 /* The mouse-highlighting comes from a display string
29615 with a mouse-face. */
29616 Lisp_Object s, e;
29617 ptrdiff_t ignore;
29619 s = Fprevious_single_property_change
29620 (make_number (pos + 1), Qmouse_face, object, Qnil);
29621 e = Fnext_single_property_change
29622 (position, Qmouse_face, object, Qnil);
29623 if (NILP (s))
29624 s = make_number (0);
29625 if (NILP (e))
29626 e = make_number (SCHARS (object));
29627 mouse_face_from_string_pos (w, hlinfo, object,
29628 XINT (s), XINT (e));
29629 hlinfo->mouse_face_past_end = 0;
29630 hlinfo->mouse_face_window = window;
29631 hlinfo->mouse_face_face_id
29632 = face_at_string_position (w, object, pos, 0, &ignore,
29633 glyph->face_id, 1);
29634 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29635 cursor = No_Cursor;
29637 else
29639 /* The mouse-highlighting, if any, comes from an overlay
29640 or text property in the buffer. */
29641 Lisp_Object buffer IF_LINT (= Qnil);
29642 Lisp_Object disp_string IF_LINT (= Qnil);
29644 if (STRINGP (object))
29646 /* If we are on a display string with no mouse-face,
29647 check if the text under it has one. */
29648 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29649 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29650 pos = string_buffer_position (object, start);
29651 if (pos > 0)
29653 mouse_face = get_char_property_and_overlay
29654 (make_number (pos), Qmouse_face, w->contents, &overlay);
29655 buffer = w->contents;
29656 disp_string = object;
29659 else
29661 buffer = object;
29662 disp_string = Qnil;
29665 if (!NILP (mouse_face))
29667 Lisp_Object before, after;
29668 Lisp_Object before_string, after_string;
29669 /* To correctly find the limits of mouse highlight
29670 in a bidi-reordered buffer, we must not use the
29671 optimization of limiting the search in
29672 previous-single-property-change and
29673 next-single-property-change, because
29674 rows_from_pos_range needs the real start and end
29675 positions to DTRT in this case. That's because
29676 the first row visible in a window does not
29677 necessarily display the character whose position
29678 is the smallest. */
29679 Lisp_Object lim1
29680 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29681 ? Fmarker_position (w->start)
29682 : Qnil;
29683 Lisp_Object lim2
29684 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29685 ? make_number (BUF_Z (XBUFFER (buffer))
29686 - w->window_end_pos)
29687 : Qnil;
29689 if (NILP (overlay))
29691 /* Handle the text property case. */
29692 before = Fprevious_single_property_change
29693 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29694 after = Fnext_single_property_change
29695 (make_number (pos), Qmouse_face, buffer, lim2);
29696 before_string = after_string = Qnil;
29698 else
29700 /* Handle the overlay case. */
29701 before = Foverlay_start (overlay);
29702 after = Foverlay_end (overlay);
29703 before_string = Foverlay_get (overlay, Qbefore_string);
29704 after_string = Foverlay_get (overlay, Qafter_string);
29706 if (!STRINGP (before_string)) before_string = Qnil;
29707 if (!STRINGP (after_string)) after_string = Qnil;
29710 mouse_face_from_buffer_pos (window, hlinfo, pos,
29711 NILP (before)
29713 : XFASTINT (before),
29714 NILP (after)
29715 ? BUF_Z (XBUFFER (buffer))
29716 : XFASTINT (after),
29717 before_string, after_string,
29718 disp_string);
29719 cursor = No_Cursor;
29724 check_help_echo:
29726 /* Look for a `help-echo' property. */
29727 if (NILP (help_echo_string)) {
29728 Lisp_Object help, overlay;
29730 /* Check overlays first. */
29731 help = overlay = Qnil;
29732 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29734 overlay = overlay_vec[i];
29735 help = Foverlay_get (overlay, Qhelp_echo);
29738 if (!NILP (help))
29740 help_echo_string = help;
29741 help_echo_window = window;
29742 help_echo_object = overlay;
29743 help_echo_pos = pos;
29745 else
29747 Lisp_Object obj = glyph->object;
29748 ptrdiff_t charpos = glyph->charpos;
29750 /* Try text properties. */
29751 if (STRINGP (obj)
29752 && charpos >= 0
29753 && charpos < SCHARS (obj))
29755 help = Fget_text_property (make_number (charpos),
29756 Qhelp_echo, obj);
29757 if (NILP (help))
29759 /* If the string itself doesn't specify a help-echo,
29760 see if the buffer text ``under'' it does. */
29761 struct glyph_row *r
29762 = MATRIX_ROW (w->current_matrix, vpos);
29763 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29764 ptrdiff_t p = string_buffer_position (obj, start);
29765 if (p > 0)
29767 help = Fget_char_property (make_number (p),
29768 Qhelp_echo, w->contents);
29769 if (!NILP (help))
29771 charpos = p;
29772 obj = w->contents;
29777 else if (BUFFERP (obj)
29778 && charpos >= BEGV
29779 && charpos < ZV)
29780 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29781 obj);
29783 if (!NILP (help))
29785 help_echo_string = help;
29786 help_echo_window = window;
29787 help_echo_object = obj;
29788 help_echo_pos = charpos;
29793 #ifdef HAVE_WINDOW_SYSTEM
29794 /* Look for a `pointer' property. */
29795 if (FRAME_WINDOW_P (f) && NILP (pointer))
29797 /* Check overlays first. */
29798 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29799 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29801 if (NILP (pointer))
29803 Lisp_Object obj = glyph->object;
29804 ptrdiff_t charpos = glyph->charpos;
29806 /* Try text properties. */
29807 if (STRINGP (obj)
29808 && charpos >= 0
29809 && charpos < SCHARS (obj))
29811 pointer = Fget_text_property (make_number (charpos),
29812 Qpointer, obj);
29813 if (NILP (pointer))
29815 /* If the string itself doesn't specify a pointer,
29816 see if the buffer text ``under'' it does. */
29817 struct glyph_row *r
29818 = MATRIX_ROW (w->current_matrix, vpos);
29819 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29820 ptrdiff_t p = string_buffer_position (obj, start);
29821 if (p > 0)
29822 pointer = Fget_char_property (make_number (p),
29823 Qpointer, w->contents);
29826 else if (BUFFERP (obj)
29827 && charpos >= BEGV
29828 && charpos < ZV)
29829 pointer = Fget_text_property (make_number (charpos),
29830 Qpointer, obj);
29833 #endif /* HAVE_WINDOW_SYSTEM */
29835 BEGV = obegv;
29836 ZV = ozv;
29837 current_buffer = obuf;
29838 SAFE_FREE ();
29841 set_cursor:
29843 #ifdef HAVE_WINDOW_SYSTEM
29844 if (FRAME_WINDOW_P (f))
29845 define_frame_cursor1 (f, cursor, pointer);
29846 #else
29847 /* This is here to prevent a compiler error, about "label at end of
29848 compound statement". */
29849 return;
29850 #endif
29854 /* EXPORT for RIF:
29855 Clear any mouse-face on window W. This function is part of the
29856 redisplay interface, and is called from try_window_id and similar
29857 functions to ensure the mouse-highlight is off. */
29859 void
29860 x_clear_window_mouse_face (struct window *w)
29862 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29863 Lisp_Object window;
29865 block_input ();
29866 XSETWINDOW (window, w);
29867 if (EQ (window, hlinfo->mouse_face_window))
29868 clear_mouse_face (hlinfo);
29869 unblock_input ();
29873 /* EXPORT:
29874 Just discard the mouse face information for frame F, if any.
29875 This is used when the size of F is changed. */
29877 void
29878 cancel_mouse_face (struct frame *f)
29880 Lisp_Object window;
29881 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29883 window = hlinfo->mouse_face_window;
29884 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29885 reset_mouse_highlight (hlinfo);
29890 /***********************************************************************
29891 Exposure Events
29892 ***********************************************************************/
29894 #ifdef HAVE_WINDOW_SYSTEM
29896 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29897 which intersects rectangle R. R is in window-relative coordinates. */
29899 static void
29900 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29901 enum glyph_row_area area)
29903 struct glyph *first = row->glyphs[area];
29904 struct glyph *end = row->glyphs[area] + row->used[area];
29905 struct glyph *last;
29906 int first_x, start_x, x;
29908 if (area == TEXT_AREA && row->fill_line_p)
29909 /* If row extends face to end of line write the whole line. */
29910 draw_glyphs (w, 0, row, area,
29911 0, row->used[area],
29912 DRAW_NORMAL_TEXT, 0);
29913 else
29915 /* Set START_X to the window-relative start position for drawing glyphs of
29916 AREA. The first glyph of the text area can be partially visible.
29917 The first glyphs of other areas cannot. */
29918 start_x = window_box_left_offset (w, area);
29919 x = start_x;
29920 if (area == TEXT_AREA)
29921 x += row->x;
29923 /* Find the first glyph that must be redrawn. */
29924 while (first < end
29925 && x + first->pixel_width < r->x)
29927 x += first->pixel_width;
29928 ++first;
29931 /* Find the last one. */
29932 last = first;
29933 first_x = x;
29934 while (last < end
29935 && x < r->x + r->width)
29937 x += last->pixel_width;
29938 ++last;
29941 /* Repaint. */
29942 if (last > first)
29943 draw_glyphs (w, first_x - start_x, row, area,
29944 first - row->glyphs[area], last - row->glyphs[area],
29945 DRAW_NORMAL_TEXT, 0);
29950 /* Redraw the parts of the glyph row ROW on window W intersecting
29951 rectangle R. R is in window-relative coordinates. Value is
29952 non-zero if mouse-face was overwritten. */
29954 static int
29955 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29957 eassert (row->enabled_p);
29959 if (row->mode_line_p || w->pseudo_window_p)
29960 draw_glyphs (w, 0, row, TEXT_AREA,
29961 0, row->used[TEXT_AREA],
29962 DRAW_NORMAL_TEXT, 0);
29963 else
29965 if (row->used[LEFT_MARGIN_AREA])
29966 expose_area (w, row, r, LEFT_MARGIN_AREA);
29967 if (row->used[TEXT_AREA])
29968 expose_area (w, row, r, TEXT_AREA);
29969 if (row->used[RIGHT_MARGIN_AREA])
29970 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29971 draw_row_fringe_bitmaps (w, row);
29974 return row->mouse_face_p;
29978 /* Redraw those parts of glyphs rows during expose event handling that
29979 overlap other rows. Redrawing of an exposed line writes over parts
29980 of lines overlapping that exposed line; this function fixes that.
29982 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29983 row in W's current matrix that is exposed and overlaps other rows.
29984 LAST_OVERLAPPING_ROW is the last such row. */
29986 static void
29987 expose_overlaps (struct window *w,
29988 struct glyph_row *first_overlapping_row,
29989 struct glyph_row *last_overlapping_row,
29990 XRectangle *r)
29992 struct glyph_row *row;
29994 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29995 if (row->overlapping_p)
29997 eassert (row->enabled_p && !row->mode_line_p);
29999 row->clip = r;
30000 if (row->used[LEFT_MARGIN_AREA])
30001 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30003 if (row->used[TEXT_AREA])
30004 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30006 if (row->used[RIGHT_MARGIN_AREA])
30007 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30008 row->clip = NULL;
30013 /* Return non-zero if W's cursor intersects rectangle R. */
30015 static int
30016 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30018 XRectangle cr, result;
30019 struct glyph *cursor_glyph;
30020 struct glyph_row *row;
30022 if (w->phys_cursor.vpos >= 0
30023 && w->phys_cursor.vpos < w->current_matrix->nrows
30024 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30025 row->enabled_p)
30026 && row->cursor_in_fringe_p)
30028 /* Cursor is in the fringe. */
30029 cr.x = window_box_right_offset (w,
30030 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30031 ? RIGHT_MARGIN_AREA
30032 : TEXT_AREA));
30033 cr.y = row->y;
30034 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30035 cr.height = row->height;
30036 return x_intersect_rectangles (&cr, r, &result);
30039 cursor_glyph = get_phys_cursor_glyph (w);
30040 if (cursor_glyph)
30042 /* r is relative to W's box, but w->phys_cursor.x is relative
30043 to left edge of W's TEXT area. Adjust it. */
30044 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30045 cr.y = w->phys_cursor.y;
30046 cr.width = cursor_glyph->pixel_width;
30047 cr.height = w->phys_cursor_height;
30048 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30049 I assume the effect is the same -- and this is portable. */
30050 return x_intersect_rectangles (&cr, r, &result);
30052 /* If we don't understand the format, pretend we're not in the hot-spot. */
30053 return 0;
30057 /* EXPORT:
30058 Draw a vertical window border to the right of window W if W doesn't
30059 have vertical scroll bars. */
30061 void
30062 x_draw_vertical_border (struct window *w)
30064 struct frame *f = XFRAME (WINDOW_FRAME (w));
30066 /* We could do better, if we knew what type of scroll-bar the adjacent
30067 windows (on either side) have... But we don't :-(
30068 However, I think this works ok. ++KFS 2003-04-25 */
30070 /* Redraw borders between horizontally adjacent windows. Don't
30071 do it for frames with vertical scroll bars because either the
30072 right scroll bar of a window, or the left scroll bar of its
30073 neighbor will suffice as a border. */
30074 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30075 return;
30077 /* Note: It is necessary to redraw both the left and the right
30078 borders, for when only this single window W is being
30079 redisplayed. */
30080 if (!WINDOW_RIGHTMOST_P (w)
30081 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30083 int x0, x1, y0, y1;
30085 window_box_edges (w, &x0, &y0, &x1, &y1);
30086 y1 -= 1;
30088 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30089 x1 -= 1;
30091 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30094 if (!WINDOW_LEFTMOST_P (w)
30095 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30097 int x0, x1, y0, y1;
30099 window_box_edges (w, &x0, &y0, &x1, &y1);
30100 y1 -= 1;
30102 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30103 x0 -= 1;
30105 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30110 /* Draw window dividers for window W. */
30112 void
30113 x_draw_right_divider (struct window *w)
30115 struct frame *f = WINDOW_XFRAME (w);
30117 if (w->mini || w->pseudo_window_p)
30118 return;
30119 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30121 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30122 int x1 = WINDOW_RIGHT_EDGE_X (w);
30123 int y0 = WINDOW_TOP_EDGE_Y (w);
30124 /* The bottom divider prevails. */
30125 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30127 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30131 static void
30132 x_draw_bottom_divider (struct window *w)
30134 struct frame *f = XFRAME (WINDOW_FRAME (w));
30136 if (w->mini || w->pseudo_window_p)
30137 return;
30138 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30140 int x0 = WINDOW_LEFT_EDGE_X (w);
30141 int x1 = WINDOW_RIGHT_EDGE_X (w);
30142 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30143 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30145 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30149 /* Redraw the part of window W intersection rectangle FR. Pixel
30150 coordinates in FR are frame-relative. Call this function with
30151 input blocked. Value is non-zero if the exposure overwrites
30152 mouse-face. */
30154 static int
30155 expose_window (struct window *w, XRectangle *fr)
30157 struct frame *f = XFRAME (w->frame);
30158 XRectangle wr, r;
30159 int mouse_face_overwritten_p = 0;
30161 /* If window is not yet fully initialized, do nothing. This can
30162 happen when toolkit scroll bars are used and a window is split.
30163 Reconfiguring the scroll bar will generate an expose for a newly
30164 created window. */
30165 if (w->current_matrix == NULL)
30166 return 0;
30168 /* When we're currently updating the window, display and current
30169 matrix usually don't agree. Arrange for a thorough display
30170 later. */
30171 if (w->must_be_updated_p)
30173 SET_FRAME_GARBAGED (f);
30174 return 0;
30177 /* Frame-relative pixel rectangle of W. */
30178 wr.x = WINDOW_LEFT_EDGE_X (w);
30179 wr.y = WINDOW_TOP_EDGE_Y (w);
30180 wr.width = WINDOW_PIXEL_WIDTH (w);
30181 wr.height = WINDOW_PIXEL_HEIGHT (w);
30183 if (x_intersect_rectangles (fr, &wr, &r))
30185 int yb = window_text_bottom_y (w);
30186 struct glyph_row *row;
30187 int cursor_cleared_p, phys_cursor_on_p;
30188 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30190 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30191 r.x, r.y, r.width, r.height));
30193 /* Convert to window coordinates. */
30194 r.x -= WINDOW_LEFT_EDGE_X (w);
30195 r.y -= WINDOW_TOP_EDGE_Y (w);
30197 /* Turn off the cursor. */
30198 if (!w->pseudo_window_p
30199 && phys_cursor_in_rect_p (w, &r))
30201 x_clear_cursor (w);
30202 cursor_cleared_p = 1;
30204 else
30205 cursor_cleared_p = 0;
30207 /* If the row containing the cursor extends face to end of line,
30208 then expose_area might overwrite the cursor outside the
30209 rectangle and thus notice_overwritten_cursor might clear
30210 w->phys_cursor_on_p. We remember the original value and
30211 check later if it is changed. */
30212 phys_cursor_on_p = w->phys_cursor_on_p;
30214 /* Update lines intersecting rectangle R. */
30215 first_overlapping_row = last_overlapping_row = NULL;
30216 for (row = w->current_matrix->rows;
30217 row->enabled_p;
30218 ++row)
30220 int y0 = row->y;
30221 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30223 if ((y0 >= r.y && y0 < r.y + r.height)
30224 || (y1 > r.y && y1 < r.y + r.height)
30225 || (r.y >= y0 && r.y < y1)
30226 || (r.y + r.height > y0 && r.y + r.height < y1))
30228 /* A header line may be overlapping, but there is no need
30229 to fix overlapping areas for them. KFS 2005-02-12 */
30230 if (row->overlapping_p && !row->mode_line_p)
30232 if (first_overlapping_row == NULL)
30233 first_overlapping_row = row;
30234 last_overlapping_row = row;
30237 row->clip = fr;
30238 if (expose_line (w, row, &r))
30239 mouse_face_overwritten_p = 1;
30240 row->clip = NULL;
30242 else if (row->overlapping_p)
30244 /* We must redraw a row overlapping the exposed area. */
30245 if (y0 < r.y
30246 ? y0 + row->phys_height > r.y
30247 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30249 if (first_overlapping_row == NULL)
30250 first_overlapping_row = row;
30251 last_overlapping_row = row;
30255 if (y1 >= yb)
30256 break;
30259 /* Display the mode line if there is one. */
30260 if (WINDOW_WANTS_MODELINE_P (w)
30261 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30262 row->enabled_p)
30263 && row->y < r.y + r.height)
30265 if (expose_line (w, row, &r))
30266 mouse_face_overwritten_p = 1;
30269 if (!w->pseudo_window_p)
30271 /* Fix the display of overlapping rows. */
30272 if (first_overlapping_row)
30273 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30274 fr);
30276 /* Draw border between windows. */
30277 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30278 x_draw_right_divider (w);
30279 else
30280 x_draw_vertical_border (w);
30282 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30283 x_draw_bottom_divider (w);
30285 /* Turn the cursor on again. */
30286 if (cursor_cleared_p
30287 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30288 update_window_cursor (w, 1);
30292 return mouse_face_overwritten_p;
30297 /* Redraw (parts) of all windows in the window tree rooted at W that
30298 intersect R. R contains frame pixel coordinates. Value is
30299 non-zero if the exposure overwrites mouse-face. */
30301 static int
30302 expose_window_tree (struct window *w, XRectangle *r)
30304 struct frame *f = XFRAME (w->frame);
30305 int mouse_face_overwritten_p = 0;
30307 while (w && !FRAME_GARBAGED_P (f))
30309 if (WINDOWP (w->contents))
30310 mouse_face_overwritten_p
30311 |= expose_window_tree (XWINDOW (w->contents), r);
30312 else
30313 mouse_face_overwritten_p |= expose_window (w, r);
30315 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30318 return mouse_face_overwritten_p;
30322 /* EXPORT:
30323 Redisplay an exposed area of frame F. X and Y are the upper-left
30324 corner of the exposed rectangle. W and H are width and height of
30325 the exposed area. All are pixel values. W or H zero means redraw
30326 the entire frame. */
30328 void
30329 expose_frame (struct frame *f, int x, int y, int w, int h)
30331 XRectangle r;
30332 int mouse_face_overwritten_p = 0;
30334 TRACE ((stderr, "expose_frame "));
30336 /* No need to redraw if frame will be redrawn soon. */
30337 if (FRAME_GARBAGED_P (f))
30339 TRACE ((stderr, " garbaged\n"));
30340 return;
30343 /* If basic faces haven't been realized yet, there is no point in
30344 trying to redraw anything. This can happen when we get an expose
30345 event while Emacs is starting, e.g. by moving another window. */
30346 if (FRAME_FACE_CACHE (f) == NULL
30347 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30349 TRACE ((stderr, " no faces\n"));
30350 return;
30353 if (w == 0 || h == 0)
30355 r.x = r.y = 0;
30356 r.width = FRAME_TEXT_WIDTH (f);
30357 r.height = FRAME_TEXT_HEIGHT (f);
30359 else
30361 r.x = x;
30362 r.y = y;
30363 r.width = w;
30364 r.height = h;
30367 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30368 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30370 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30371 if (WINDOWP (f->tool_bar_window))
30372 mouse_face_overwritten_p
30373 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30374 #endif
30376 #ifdef HAVE_X_WINDOWS
30377 #ifndef MSDOS
30378 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30379 if (WINDOWP (f->menu_bar_window))
30380 mouse_face_overwritten_p
30381 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30382 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30383 #endif
30384 #endif
30386 /* Some window managers support a focus-follows-mouse style with
30387 delayed raising of frames. Imagine a partially obscured frame,
30388 and moving the mouse into partially obscured mouse-face on that
30389 frame. The visible part of the mouse-face will be highlighted,
30390 then the WM raises the obscured frame. With at least one WM, KDE
30391 2.1, Emacs is not getting any event for the raising of the frame
30392 (even tried with SubstructureRedirectMask), only Expose events.
30393 These expose events will draw text normally, i.e. not
30394 highlighted. Which means we must redo the highlight here.
30395 Subsume it under ``we love X''. --gerd 2001-08-15 */
30396 /* Included in Windows version because Windows most likely does not
30397 do the right thing if any third party tool offers
30398 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30399 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30401 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30402 if (f == hlinfo->mouse_face_mouse_frame)
30404 int mouse_x = hlinfo->mouse_face_mouse_x;
30405 int mouse_y = hlinfo->mouse_face_mouse_y;
30406 clear_mouse_face (hlinfo);
30407 note_mouse_highlight (f, mouse_x, mouse_y);
30413 /* EXPORT:
30414 Determine the intersection of two rectangles R1 and R2. Return
30415 the intersection in *RESULT. Value is non-zero if RESULT is not
30416 empty. */
30419 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30421 XRectangle *left, *right;
30422 XRectangle *upper, *lower;
30423 int intersection_p = 0;
30425 /* Rearrange so that R1 is the left-most rectangle. */
30426 if (r1->x < r2->x)
30427 left = r1, right = r2;
30428 else
30429 left = r2, right = r1;
30431 /* X0 of the intersection is right.x0, if this is inside R1,
30432 otherwise there is no intersection. */
30433 if (right->x <= left->x + left->width)
30435 result->x = right->x;
30437 /* The right end of the intersection is the minimum of
30438 the right ends of left and right. */
30439 result->width = (min (left->x + left->width, right->x + right->width)
30440 - result->x);
30442 /* Same game for Y. */
30443 if (r1->y < r2->y)
30444 upper = r1, lower = r2;
30445 else
30446 upper = r2, lower = r1;
30448 /* The upper end of the intersection is lower.y0, if this is inside
30449 of upper. Otherwise, there is no intersection. */
30450 if (lower->y <= upper->y + upper->height)
30452 result->y = lower->y;
30454 /* The lower end of the intersection is the minimum of the lower
30455 ends of upper and lower. */
30456 result->height = (min (lower->y + lower->height,
30457 upper->y + upper->height)
30458 - result->y);
30459 intersection_p = 1;
30463 return intersection_p;
30466 #endif /* HAVE_WINDOW_SYSTEM */
30469 /***********************************************************************
30470 Initialization
30471 ***********************************************************************/
30473 void
30474 syms_of_xdisp (void)
30476 Vwith_echo_area_save_vector = Qnil;
30477 staticpro (&Vwith_echo_area_save_vector);
30479 Vmessage_stack = Qnil;
30480 staticpro (&Vmessage_stack);
30482 /* Non-nil means don't actually do any redisplay. */
30483 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30485 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30487 message_dolog_marker1 = Fmake_marker ();
30488 staticpro (&message_dolog_marker1);
30489 message_dolog_marker2 = Fmake_marker ();
30490 staticpro (&message_dolog_marker2);
30491 message_dolog_marker3 = Fmake_marker ();
30492 staticpro (&message_dolog_marker3);
30494 #ifdef GLYPH_DEBUG
30495 defsubr (&Sdump_frame_glyph_matrix);
30496 defsubr (&Sdump_glyph_matrix);
30497 defsubr (&Sdump_glyph_row);
30498 defsubr (&Sdump_tool_bar_row);
30499 defsubr (&Strace_redisplay);
30500 defsubr (&Strace_to_stderr);
30501 #endif
30502 #ifdef HAVE_WINDOW_SYSTEM
30503 defsubr (&Stool_bar_height);
30504 defsubr (&Slookup_image_map);
30505 #endif
30506 defsubr (&Sline_pixel_height);
30507 defsubr (&Sformat_mode_line);
30508 defsubr (&Sinvisible_p);
30509 defsubr (&Scurrent_bidi_paragraph_direction);
30510 defsubr (&Swindow_text_pixel_size);
30511 defsubr (&Smove_point_visually);
30512 defsubr (&Sbidi_find_overridden_directionality);
30514 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30515 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30516 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30517 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30518 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30519 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30520 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30521 DEFSYM (Qeval, "eval");
30522 DEFSYM (QCdata, ":data");
30524 /* Names of text properties relevant for redisplay. */
30525 DEFSYM (Qdisplay, "display");
30526 DEFSYM (Qspace_width, "space-width");
30527 DEFSYM (Qraise, "raise");
30528 DEFSYM (Qslice, "slice");
30529 DEFSYM (Qspace, "space");
30530 DEFSYM (Qmargin, "margin");
30531 DEFSYM (Qpointer, "pointer");
30532 DEFSYM (Qleft_margin, "left-margin");
30533 DEFSYM (Qright_margin, "right-margin");
30534 DEFSYM (Qcenter, "center");
30535 DEFSYM (Qline_height, "line-height");
30536 DEFSYM (QCalign_to, ":align-to");
30537 DEFSYM (QCrelative_width, ":relative-width");
30538 DEFSYM (QCrelative_height, ":relative-height");
30539 DEFSYM (QCeval, ":eval");
30540 DEFSYM (QCpropertize, ":propertize");
30541 DEFSYM (QCfile, ":file");
30542 DEFSYM (Qfontified, "fontified");
30543 DEFSYM (Qfontification_functions, "fontification-functions");
30545 /* Name of the face used to highlight trailing whitespace. */
30546 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30548 /* Name and number of the face used to highlight escape glyphs. */
30549 DEFSYM (Qescape_glyph, "escape-glyph");
30551 /* Name and number of the face used to highlight non-breaking spaces. */
30552 DEFSYM (Qnobreak_space, "nobreak-space");
30554 /* The symbol 'image' which is the car of the lists used to represent
30555 images in Lisp. Also a tool bar style. */
30556 DEFSYM (Qimage, "image");
30558 /* Tool bar styles. */
30559 DEFSYM (Qtext, "text");
30560 DEFSYM (Qboth, "both");
30561 DEFSYM (Qboth_horiz, "both-horiz");
30562 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30564 /* The image map types. */
30565 DEFSYM (QCmap, ":map");
30566 DEFSYM (QCpointer, ":pointer");
30567 DEFSYM (Qrect, "rect");
30568 DEFSYM (Qcircle, "circle");
30569 DEFSYM (Qpoly, "poly");
30571 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
30572 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30573 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30575 DEFSYM (Qgrow_only, "grow-only");
30576 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30577 DEFSYM (Qposition, "position");
30578 DEFSYM (Qbuffer_position, "buffer-position");
30579 DEFSYM (Qobject, "object");
30581 /* Cursor shapes. */
30582 DEFSYM (Qbar, "bar");
30583 DEFSYM (Qhbar, "hbar");
30584 DEFSYM (Qbox, "box");
30585 DEFSYM (Qhollow, "hollow");
30587 /* Pointer shapes. */
30588 DEFSYM (Qhand, "hand");
30589 DEFSYM (Qarrow, "arrow");
30590 /* also Qtext */
30592 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30594 list_of_error = list1 (list2 (intern_c_string ("error"),
30595 intern_c_string ("void-variable")));
30596 staticpro (&list_of_error);
30598 /* Values of those variables at last redisplay are stored as
30599 properties on 'overlay-arrow-position' symbol. However, if
30600 Voverlay_arrow_position is a marker, last-arrow-position is its
30601 numerical position. */
30602 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30603 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30605 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
30606 properties on a symbol in overlay-arrow-variable-list. */
30607 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30608 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30610 echo_buffer[0] = echo_buffer[1] = Qnil;
30611 staticpro (&echo_buffer[0]);
30612 staticpro (&echo_buffer[1]);
30614 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30615 staticpro (&echo_area_buffer[0]);
30616 staticpro (&echo_area_buffer[1]);
30618 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30619 staticpro (&Vmessages_buffer_name);
30621 mode_line_proptrans_alist = Qnil;
30622 staticpro (&mode_line_proptrans_alist);
30623 mode_line_string_list = Qnil;
30624 staticpro (&mode_line_string_list);
30625 mode_line_string_face = Qnil;
30626 staticpro (&mode_line_string_face);
30627 mode_line_string_face_prop = Qnil;
30628 staticpro (&mode_line_string_face_prop);
30629 Vmode_line_unwind_vector = Qnil;
30630 staticpro (&Vmode_line_unwind_vector);
30632 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30634 help_echo_string = Qnil;
30635 staticpro (&help_echo_string);
30636 help_echo_object = Qnil;
30637 staticpro (&help_echo_object);
30638 help_echo_window = Qnil;
30639 staticpro (&help_echo_window);
30640 previous_help_echo_string = Qnil;
30641 staticpro (&previous_help_echo_string);
30642 help_echo_pos = -1;
30644 DEFSYM (Qright_to_left, "right-to-left");
30645 DEFSYM (Qleft_to_right, "left-to-right");
30646 defsubr (&Sbidi_resolved_levels);
30648 #ifdef HAVE_WINDOW_SYSTEM
30649 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30650 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30651 For example, if a block cursor is over a tab, it will be drawn as
30652 wide as that tab on the display. */);
30653 x_stretch_cursor_p = 0;
30654 #endif
30656 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30657 doc: /* Non-nil means highlight trailing whitespace.
30658 The face used for trailing whitespace is `trailing-whitespace'. */);
30659 Vshow_trailing_whitespace = Qnil;
30661 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30662 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30663 If the value is t, Emacs highlights non-ASCII chars which have the
30664 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30665 or `escape-glyph' face respectively.
30667 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30668 U+2011 (non-breaking hyphen) are affected.
30670 Any other non-nil value means to display these characters as a escape
30671 glyph followed by an ordinary space or hyphen.
30673 A value of nil means no special handling of these characters. */);
30674 Vnobreak_char_display = Qt;
30676 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30677 doc: /* The pointer shape to show in void text areas.
30678 A value of nil means to show the text pointer. Other options are
30679 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30680 `hourglass'. */);
30681 Vvoid_text_area_pointer = Qarrow;
30683 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30684 doc: /* Non-nil means don't actually do any redisplay.
30685 This is used for internal purposes. */);
30686 Vinhibit_redisplay = Qnil;
30688 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30689 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30690 Vglobal_mode_string = Qnil;
30692 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30693 doc: /* Marker for where to display an arrow on top of the buffer text.
30694 This must be the beginning of a line in order to work.
30695 See also `overlay-arrow-string'. */);
30696 Voverlay_arrow_position = Qnil;
30698 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30699 doc: /* String to display as an arrow in non-window frames.
30700 See also `overlay-arrow-position'. */);
30701 Voverlay_arrow_string = build_pure_c_string ("=>");
30703 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30704 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30705 The symbols on this list are examined during redisplay to determine
30706 where to display overlay arrows. */);
30707 Voverlay_arrow_variable_list
30708 = list1 (intern_c_string ("overlay-arrow-position"));
30710 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30711 doc: /* The number of lines to try scrolling a window by when point moves out.
30712 If that fails to bring point back on frame, point is centered instead.
30713 If this is zero, point is always centered after it moves off frame.
30714 If you want scrolling to always be a line at a time, you should set
30715 `scroll-conservatively' to a large value rather than set this to 1. */);
30717 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30718 doc: /* Scroll up to this many lines, to bring point back on screen.
30719 If point moves off-screen, redisplay will scroll by up to
30720 `scroll-conservatively' lines in order to bring point just barely
30721 onto the screen again. If that cannot be done, then redisplay
30722 recenters point as usual.
30724 If the value is greater than 100, redisplay will never recenter point,
30725 but will always scroll just enough text to bring point into view, even
30726 if you move far away.
30728 A value of zero means always recenter point if it moves off screen. */);
30729 scroll_conservatively = 0;
30731 DEFVAR_INT ("scroll-margin", scroll_margin,
30732 doc: /* Number of lines of margin at the top and bottom of a window.
30733 Recenter the window whenever point gets within this many lines
30734 of the top or bottom of the window. */);
30735 scroll_margin = 0;
30737 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30738 doc: /* Pixels per inch value for non-window system displays.
30739 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30740 Vdisplay_pixels_per_inch = make_float (72.0);
30742 #ifdef GLYPH_DEBUG
30743 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30744 #endif
30746 DEFVAR_LISP ("truncate-partial-width-windows",
30747 Vtruncate_partial_width_windows,
30748 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30749 For an integer value, truncate lines in each window narrower than the
30750 full frame width, provided the window width is less than that integer;
30751 otherwise, respect the value of `truncate-lines'.
30753 For any other non-nil value, truncate lines in all windows that do
30754 not span the full frame width.
30756 A value of nil means to respect the value of `truncate-lines'.
30758 If `word-wrap' is enabled, you might want to reduce this. */);
30759 Vtruncate_partial_width_windows = make_number (50);
30761 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30762 doc: /* Maximum buffer size for which line number should be displayed.
30763 If the buffer is bigger than this, the line number does not appear
30764 in the mode line. A value of nil means no limit. */);
30765 Vline_number_display_limit = Qnil;
30767 DEFVAR_INT ("line-number-display-limit-width",
30768 line_number_display_limit_width,
30769 doc: /* Maximum line width (in characters) for line number display.
30770 If the average length of the lines near point is bigger than this, then the
30771 line number may be omitted from the mode line. */);
30772 line_number_display_limit_width = 200;
30774 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30775 doc: /* Non-nil means highlight region even in nonselected windows. */);
30776 highlight_nonselected_windows = 0;
30778 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30779 doc: /* Non-nil if more than one frame is visible on this display.
30780 Minibuffer-only frames don't count, but iconified frames do.
30781 This variable is not guaranteed to be accurate except while processing
30782 `frame-title-format' and `icon-title-format'. */);
30784 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30785 doc: /* Template for displaying the title bar of visible frames.
30786 \(Assuming the window manager supports this feature.)
30788 This variable has the same structure as `mode-line-format', except that
30789 the %c and %l constructs are ignored. It is used only on frames for
30790 which no explicit name has been set \(see `modify-frame-parameters'). */);
30792 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30793 doc: /* Template for displaying the title bar of an iconified frame.
30794 \(Assuming the window manager supports this feature.)
30795 This variable has the same structure as `mode-line-format' (which see),
30796 and is used only on frames for which no explicit name has been set
30797 \(see `modify-frame-parameters'). */);
30798 Vicon_title_format
30799 = Vframe_title_format
30800 = listn (CONSTYPE_PURE, 3,
30801 intern_c_string ("multiple-frames"),
30802 build_pure_c_string ("%b"),
30803 listn (CONSTYPE_PURE, 4,
30804 empty_unibyte_string,
30805 intern_c_string ("invocation-name"),
30806 build_pure_c_string ("@"),
30807 intern_c_string ("system-name")));
30809 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30810 doc: /* Maximum number of lines to keep in the message log buffer.
30811 If nil, disable message logging. If t, log messages but don't truncate
30812 the buffer when it becomes large. */);
30813 Vmessage_log_max = make_number (1000);
30815 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30816 doc: /* Functions called before redisplay, if window sizes have changed.
30817 The value should be a list of functions that take one argument.
30818 Just before redisplay, for each frame, if any of its windows have changed
30819 size since the last redisplay, or have been split or deleted,
30820 all the functions in the list are called, with the frame as argument. */);
30821 Vwindow_size_change_functions = Qnil;
30823 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30824 doc: /* List of functions to call before redisplaying a window with scrolling.
30825 Each function is called with two arguments, the window and its new
30826 display-start position.
30827 These functions are called whenever the `window-start' marker is modified,
30828 either to point into another buffer (e.g. via `set-window-buffer') or another
30829 place in the same buffer.
30830 Note that the value of `window-end' is not valid when these functions are
30831 called.
30833 Warning: Do not use this feature to alter the way the window
30834 is scrolled. It is not designed for that, and such use probably won't
30835 work. */);
30836 Vwindow_scroll_functions = Qnil;
30838 DEFVAR_LISP ("window-text-change-functions",
30839 Vwindow_text_change_functions,
30840 doc: /* Functions to call in redisplay when text in the window might change. */);
30841 Vwindow_text_change_functions = Qnil;
30843 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30844 doc: /* Functions called when redisplay of a window reaches the end trigger.
30845 Each function is called with two arguments, the window and the end trigger value.
30846 See `set-window-redisplay-end-trigger'. */);
30847 Vredisplay_end_trigger_functions = Qnil;
30849 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30850 doc: /* Non-nil means autoselect window with mouse pointer.
30851 If nil, do not autoselect windows.
30852 A positive number means delay autoselection by that many seconds: a
30853 window is autoselected only after the mouse has remained in that
30854 window for the duration of the delay.
30855 A negative number has a similar effect, but causes windows to be
30856 autoselected only after the mouse has stopped moving. \(Because of
30857 the way Emacs compares mouse events, you will occasionally wait twice
30858 that time before the window gets selected.\)
30859 Any other value means to autoselect window instantaneously when the
30860 mouse pointer enters it.
30862 Autoselection selects the minibuffer only if it is active, and never
30863 unselects the minibuffer if it is active.
30865 When customizing this variable make sure that the actual value of
30866 `focus-follows-mouse' matches the behavior of your window manager. */);
30867 Vmouse_autoselect_window = Qnil;
30869 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30870 doc: /* Non-nil means automatically resize tool-bars.
30871 This dynamically changes the tool-bar's height to the minimum height
30872 that is needed to make all tool-bar items visible.
30873 If value is `grow-only', the tool-bar's height is only increased
30874 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30875 Vauto_resize_tool_bars = Qt;
30877 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30878 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30879 auto_raise_tool_bar_buttons_p = 1;
30881 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30882 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30883 make_cursor_line_fully_visible_p = 1;
30885 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30886 doc: /* Border below tool-bar in pixels.
30887 If an integer, use it as the height of the border.
30888 If it is one of `internal-border-width' or `border-width', use the
30889 value of the corresponding frame parameter.
30890 Otherwise, no border is added below the tool-bar. */);
30891 Vtool_bar_border = Qinternal_border_width;
30893 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30894 doc: /* Margin around tool-bar buttons in pixels.
30895 If an integer, use that for both horizontal and vertical margins.
30896 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30897 HORZ specifying the horizontal margin, and VERT specifying the
30898 vertical margin. */);
30899 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30901 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30902 doc: /* Relief thickness of tool-bar buttons. */);
30903 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30905 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30906 doc: /* Tool bar style to use.
30907 It can be one of
30908 image - show images only
30909 text - show text only
30910 both - show both, text below image
30911 both-horiz - show text to the right of the image
30912 text-image-horiz - show text to the left of the image
30913 any other - use system default or image if no system default.
30915 This variable only affects the GTK+ toolkit version of Emacs. */);
30916 Vtool_bar_style = Qnil;
30918 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30919 doc: /* Maximum number of characters a label can have to be shown.
30920 The tool bar style must also show labels for this to have any effect, see
30921 `tool-bar-style'. */);
30922 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30924 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30925 doc: /* List of functions to call to fontify regions of text.
30926 Each function is called with one argument POS. Functions must
30927 fontify a region starting at POS in the current buffer, and give
30928 fontified regions the property `fontified'. */);
30929 Vfontification_functions = Qnil;
30930 Fmake_variable_buffer_local (Qfontification_functions);
30932 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30933 unibyte_display_via_language_environment,
30934 doc: /* Non-nil means display unibyte text according to language environment.
30935 Specifically, this means that raw bytes in the range 160-255 decimal
30936 are displayed by converting them to the equivalent multibyte characters
30937 according to the current language environment. As a result, they are
30938 displayed according to the current fontset.
30940 Note that this variable affects only how these bytes are displayed,
30941 but does not change the fact they are interpreted as raw bytes. */);
30942 unibyte_display_via_language_environment = 0;
30944 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30945 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30946 If a float, it specifies a fraction of the mini-window frame's height.
30947 If an integer, it specifies a number of lines. */);
30948 Vmax_mini_window_height = make_float (0.25);
30950 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30951 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30952 A value of nil means don't automatically resize mini-windows.
30953 A value of t means resize them to fit the text displayed in them.
30954 A value of `grow-only', the default, means let mini-windows grow only;
30955 they return to their normal size when the minibuffer is closed, or the
30956 echo area becomes empty. */);
30957 Vresize_mini_windows = Qgrow_only;
30959 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30960 doc: /* Alist specifying how to blink the cursor off.
30961 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30962 `cursor-type' frame-parameter or variable equals ON-STATE,
30963 comparing using `equal', Emacs uses OFF-STATE to specify
30964 how to blink it off. ON-STATE and OFF-STATE are values for
30965 the `cursor-type' frame parameter.
30967 If a frame's ON-STATE has no entry in this list,
30968 the frame's other specifications determine how to blink the cursor off. */);
30969 Vblink_cursor_alist = Qnil;
30971 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30972 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30973 If non-nil, windows are automatically scrolled horizontally to make
30974 point visible. */);
30975 automatic_hscrolling_p = 1;
30976 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30978 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30979 doc: /* How many columns away from the window edge point is allowed to get
30980 before automatic hscrolling will horizontally scroll the window. */);
30981 hscroll_margin = 5;
30983 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30984 doc: /* How many columns to scroll the window when point gets too close to the edge.
30985 When point is less than `hscroll-margin' columns from the window
30986 edge, automatic hscrolling will scroll the window by the amount of columns
30987 determined by this variable. If its value is a positive integer, scroll that
30988 many columns. If it's a positive floating-point number, it specifies the
30989 fraction of the window's width to scroll. If it's nil or zero, point will be
30990 centered horizontally after the scroll. Any other value, including negative
30991 numbers, are treated as if the value were zero.
30993 Automatic hscrolling always moves point outside the scroll margin, so if
30994 point was more than scroll step columns inside the margin, the window will
30995 scroll more than the value given by the scroll step.
30997 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30998 and `scroll-right' overrides this variable's effect. */);
30999 Vhscroll_step = make_number (0);
31001 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31002 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31003 Bind this around calls to `message' to let it take effect. */);
31004 message_truncate_lines = 0;
31006 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31007 doc: /* Normal hook run to update the menu bar definitions.
31008 Redisplay runs this hook before it redisplays the menu bar.
31009 This is used to update menus such as Buffers, whose contents depend on
31010 various data. */);
31011 Vmenu_bar_update_hook = Qnil;
31013 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31014 doc: /* Frame for which we are updating a menu.
31015 The enable predicate for a menu binding should check this variable. */);
31016 Vmenu_updating_frame = Qnil;
31018 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31019 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31020 inhibit_menubar_update = 0;
31022 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31023 doc: /* Prefix prepended to all continuation lines at display time.
31024 The value may be a string, an image, or a stretch-glyph; it is
31025 interpreted in the same way as the value of a `display' text property.
31027 This variable is overridden by any `wrap-prefix' text or overlay
31028 property.
31030 To add a prefix to non-continuation lines, use `line-prefix'. */);
31031 Vwrap_prefix = Qnil;
31032 DEFSYM (Qwrap_prefix, "wrap-prefix");
31033 Fmake_variable_buffer_local (Qwrap_prefix);
31035 DEFVAR_LISP ("line-prefix", Vline_prefix,
31036 doc: /* Prefix prepended to all non-continuation lines at display time.
31037 The value may be a string, an image, or a stretch-glyph; it is
31038 interpreted in the same way as the value of a `display' text property.
31040 This variable is overridden by any `line-prefix' text or overlay
31041 property.
31043 To add a prefix to continuation lines, use `wrap-prefix'. */);
31044 Vline_prefix = Qnil;
31045 DEFSYM (Qline_prefix, "line-prefix");
31046 Fmake_variable_buffer_local (Qline_prefix);
31048 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31049 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31050 inhibit_eval_during_redisplay = 0;
31052 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31053 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31054 inhibit_free_realized_faces = 0;
31056 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31057 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31058 Intended for use during debugging and for testing bidi display;
31059 see biditest.el in the test suite. */);
31060 inhibit_bidi_mirroring = 0;
31062 #ifdef GLYPH_DEBUG
31063 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31064 doc: /* Inhibit try_window_id display optimization. */);
31065 inhibit_try_window_id = 0;
31067 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31068 doc: /* Inhibit try_window_reusing display optimization. */);
31069 inhibit_try_window_reusing = 0;
31071 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31072 doc: /* Inhibit try_cursor_movement display optimization. */);
31073 inhibit_try_cursor_movement = 0;
31074 #endif /* GLYPH_DEBUG */
31076 DEFVAR_INT ("overline-margin", overline_margin,
31077 doc: /* Space between overline and text, in pixels.
31078 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31079 margin to the character height. */);
31080 overline_margin = 2;
31082 DEFVAR_INT ("underline-minimum-offset",
31083 underline_minimum_offset,
31084 doc: /* Minimum distance between baseline and underline.
31085 This can improve legibility of underlined text at small font sizes,
31086 particularly when using variable `x-use-underline-position-properties'
31087 with fonts that specify an UNDERLINE_POSITION relatively close to the
31088 baseline. The default value is 1. */);
31089 underline_minimum_offset = 1;
31091 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31092 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31093 This feature only works when on a window system that can change
31094 cursor shapes. */);
31095 display_hourglass_p = 1;
31097 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31098 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31099 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31101 #ifdef HAVE_WINDOW_SYSTEM
31102 hourglass_atimer = NULL;
31103 hourglass_shown_p = 0;
31104 #endif /* HAVE_WINDOW_SYSTEM */
31106 /* Name of the face used to display glyphless characters. */
31107 DEFSYM (Qglyphless_char, "glyphless-char");
31109 /* Method symbols for Vglyphless_char_display. */
31110 DEFSYM (Qhex_code, "hex-code");
31111 DEFSYM (Qempty_box, "empty-box");
31112 DEFSYM (Qthin_space, "thin-space");
31113 DEFSYM (Qzero_width, "zero-width");
31115 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31116 doc: /* Function run just before redisplay.
31117 It is called with one argument, which is the set of windows that are to
31118 be redisplayed. This set can be nil (meaning, only the selected window),
31119 or t (meaning all windows). */);
31120 Vpre_redisplay_function = intern ("ignore");
31122 /* Symbol for the purpose of Vglyphless_char_display. */
31123 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31124 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31126 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31127 doc: /* Char-table defining glyphless characters.
31128 Each element, if non-nil, should be one of the following:
31129 an ASCII acronym string: display this string in a box
31130 `hex-code': display the hexadecimal code of a character in a box
31131 `empty-box': display as an empty box
31132 `thin-space': display as 1-pixel width space
31133 `zero-width': don't display
31134 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31135 display method for graphical terminals and text terminals respectively.
31136 GRAPHICAL and TEXT should each have one of the values listed above.
31138 The char-table has one extra slot to control the display of a character for
31139 which no font is found. This slot only takes effect on graphical terminals.
31140 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31141 `thin-space'. The default is `empty-box'.
31143 If a character has a non-nil entry in an active display table, the
31144 display table takes effect; in this case, Emacs does not consult
31145 `glyphless-char-display' at all. */);
31146 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31147 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31148 Qempty_box);
31150 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31151 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31152 Vdebug_on_message = Qnil;
31154 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31155 doc: /* */);
31156 Vredisplay__all_windows_cause
31157 = Fmake_vector (make_number (100), make_number (0));
31159 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31160 doc: /* */);
31161 Vredisplay__mode_lines_cause
31162 = Fmake_vector (make_number (100), make_number (0));
31166 /* Initialize this module when Emacs starts. */
31168 void
31169 init_xdisp (void)
31171 CHARPOS (this_line_start_pos) = 0;
31173 if (!noninteractive)
31175 struct window *m = XWINDOW (minibuf_window);
31176 Lisp_Object frame = m->frame;
31177 struct frame *f = XFRAME (frame);
31178 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31179 struct window *r = XWINDOW (root);
31180 int i;
31182 echo_area_window = minibuf_window;
31184 r->top_line = FRAME_TOP_MARGIN (f);
31185 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31186 r->total_cols = FRAME_COLS (f);
31187 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31188 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31189 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31191 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31192 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31193 m->total_cols = FRAME_COLS (f);
31194 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31195 m->total_lines = 1;
31196 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31198 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31199 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31200 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31202 /* The default ellipsis glyphs `...'. */
31203 for (i = 0; i < 3; ++i)
31204 default_invis_vector[i] = make_number ('.');
31208 /* Allocate the buffer for frame titles.
31209 Also used for `format-mode-line'. */
31210 int size = 100;
31211 mode_line_noprop_buf = xmalloc (size);
31212 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31213 mode_line_noprop_ptr = mode_line_noprop_buf;
31214 mode_line_target = MODE_LINE_DISPLAY;
31217 help_echo_showing_p = 0;
31220 #ifdef HAVE_WINDOW_SYSTEM
31222 /* Platform-independent portion of hourglass implementation. */
31224 /* Timer function of hourglass_atimer. */
31226 static void
31227 show_hourglass (struct atimer *timer)
31229 /* The timer implementation will cancel this timer automatically
31230 after this function has run. Set hourglass_atimer to null
31231 so that we know the timer doesn't have to be canceled. */
31232 hourglass_atimer = NULL;
31234 if (!hourglass_shown_p)
31236 Lisp_Object tail, frame;
31238 block_input ();
31240 FOR_EACH_FRAME (tail, frame)
31242 struct frame *f = XFRAME (frame);
31244 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31245 && FRAME_RIF (f)->show_hourglass)
31246 FRAME_RIF (f)->show_hourglass (f);
31249 hourglass_shown_p = 1;
31250 unblock_input ();
31254 /* Cancel a currently active hourglass timer, and start a new one. */
31256 void
31257 start_hourglass (void)
31259 struct timespec delay;
31261 cancel_hourglass ();
31263 if (INTEGERP (Vhourglass_delay)
31264 && XINT (Vhourglass_delay) > 0)
31265 delay = make_timespec (min (XINT (Vhourglass_delay),
31266 TYPE_MAXIMUM (time_t)),
31268 else if (FLOATP (Vhourglass_delay)
31269 && XFLOAT_DATA (Vhourglass_delay) > 0)
31270 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31271 else
31272 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31274 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31275 show_hourglass, NULL);
31278 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31279 shown. */
31281 void
31282 cancel_hourglass (void)
31284 if (hourglass_atimer)
31286 cancel_atimer (hourglass_atimer);
31287 hourglass_atimer = NULL;
31290 if (hourglass_shown_p)
31292 Lisp_Object tail, frame;
31294 block_input ();
31296 FOR_EACH_FRAME (tail, frame)
31298 struct frame *f = XFRAME (frame);
31300 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31301 && FRAME_RIF (f)->hide_hourglass)
31302 FRAME_RIF (f)->hide_hourglass (f);
31303 #ifdef HAVE_NTGUI
31304 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31305 else if (!FRAME_W32_P (f))
31306 w32_arrow_cursor ();
31307 #endif
31310 hourglass_shown_p = 0;
31311 unblock_input ();
31315 #endif /* HAVE_WINDOW_SYSTEM */