* lisp/progmodes/python.el (python-shell-prompt-detect): Remove redundant
[emacs.git] / src / xdisp.c
blob6ce8e3fff1834047a7918e4425b973d9cc6b5690
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2014 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 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
328 Lisp_Object Qwindow_scroll_functions;
329 static Lisp_Object Qwindow_text_change_functions;
330 static Lisp_Object Qredisplay_end_trigger_functions;
331 Lisp_Object Qinhibit_point_motion_hooks;
332 static Lisp_Object QCeval, QCpropertize;
333 Lisp_Object QCfile, QCdata;
334 static Lisp_Object Qfontified;
335 static Lisp_Object Qgrow_only;
336 static Lisp_Object Qinhibit_eval_during_redisplay;
337 static Lisp_Object Qbuffer_position, Qposition, Qobject;
338 static Lisp_Object Qright_to_left, Qleft_to_right;
340 /* Cursor shapes. */
341 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
343 /* Pointer shapes. */
344 static Lisp_Object Qarrow, Qhand;
345 Lisp_Object Qtext;
347 /* Holds the list (error). */
348 static Lisp_Object list_of_error;
350 static Lisp_Object Qfontification_functions;
352 static Lisp_Object Qwrap_prefix;
353 static Lisp_Object Qline_prefix;
354 static Lisp_Object Qredisplay_internal;
356 /* Non-nil means don't actually do any redisplay. */
358 Lisp_Object Qinhibit_redisplay;
360 /* Names of text properties relevant for redisplay. */
362 Lisp_Object Qdisplay;
364 Lisp_Object Qspace, QCalign_to;
365 static Lisp_Object QCrelative_width, QCrelative_height;
366 Lisp_Object Qleft_margin, Qright_margin;
367 static Lisp_Object Qspace_width, Qraise;
368 static Lisp_Object Qslice;
369 Lisp_Object Qcenter;
370 static Lisp_Object Qmargin, Qpointer;
371 static Lisp_Object Qline_height;
373 #ifdef HAVE_WINDOW_SYSTEM
375 /* Test if overflow newline into fringe. Called with iterator IT
376 at or past right window margin, and with IT->current_x set. */
378 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
379 (!NILP (Voverflow_newline_into_fringe) \
380 && FRAME_WINDOW_P ((IT)->f) \
381 && ((IT)->bidi_it.paragraph_dir == R2L \
382 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
383 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
384 && (IT)->current_x == (IT)->last_visible_x)
386 #else /* !HAVE_WINDOW_SYSTEM */
387 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
388 #endif /* HAVE_WINDOW_SYSTEM */
390 /* Test if the display element loaded in IT, or the underlying buffer
391 or string character, is a space or a TAB character. This is used
392 to determine where word wrapping can occur. */
394 #define IT_DISPLAYING_WHITESPACE(it) \
395 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
396 || ((STRINGP (it->string) \
397 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
398 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
399 || (it->s \
400 && (it->s[IT_BYTEPOS (*it)] == ' ' \
401 || it->s[IT_BYTEPOS (*it)] == '\t')) \
402 || (IT_BYTEPOS (*it) < ZV_BYTE \
403 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
404 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
406 /* Name of the face used to highlight trailing whitespace. */
408 static Lisp_Object Qtrailing_whitespace;
410 /* Name and number of the face used to highlight escape glyphs. */
412 static Lisp_Object Qescape_glyph;
414 /* Name and number of the face used to highlight non-breaking spaces. */
416 static Lisp_Object Qnobreak_space;
418 /* The symbol `image' which is the car of the lists used to represent
419 images in Lisp. Also a tool bar style. */
421 Lisp_Object Qimage;
423 /* The image map types. */
424 Lisp_Object QCmap;
425 static Lisp_Object QCpointer;
426 static Lisp_Object Qrect, Qcircle, Qpoly;
428 /* Tool bar styles */
429 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
431 /* Non-zero means print newline to stdout before next mini-buffer
432 message. */
434 bool noninteractive_need_newline;
436 /* Non-zero means print newline to message log before next message. */
438 static bool message_log_need_newline;
440 /* Three markers that message_dolog uses.
441 It could allocate them itself, but that causes trouble
442 in handling memory-full errors. */
443 static Lisp_Object message_dolog_marker1;
444 static Lisp_Object message_dolog_marker2;
445 static Lisp_Object message_dolog_marker3;
447 /* The buffer position of the first character appearing entirely or
448 partially on the line of the selected window which contains the
449 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
450 redisplay optimization in redisplay_internal. */
452 static struct text_pos this_line_start_pos;
454 /* Number of characters past the end of the line above, including the
455 terminating newline. */
457 static struct text_pos this_line_end_pos;
459 /* The vertical positions and the height of this line. */
461 static int this_line_vpos;
462 static int this_line_y;
463 static int this_line_pixel_height;
465 /* X position at which this display line starts. Usually zero;
466 negative if first character is partially visible. */
468 static int this_line_start_x;
470 /* The smallest character position seen by move_it_* functions as they
471 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
472 hscrolled lines, see display_line. */
474 static struct text_pos this_line_min_pos;
476 /* Buffer that this_line_.* variables are referring to. */
478 static struct buffer *this_line_buffer;
481 /* Values of those variables at last redisplay are stored as
482 properties on `overlay-arrow-position' symbol. However, if
483 Voverlay_arrow_position is a marker, last-arrow-position is its
484 numerical position. */
486 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
488 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
489 properties on a symbol in overlay-arrow-variable-list. */
491 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
493 Lisp_Object Qmenu_bar_update_hook;
495 /* Nonzero if an overlay arrow has been displayed in this window. */
497 static bool overlay_arrow_seen;
499 /* Vector containing glyphs for an ellipsis `...'. */
501 static Lisp_Object default_invis_vector[3];
503 /* This is the window where the echo area message was displayed. It
504 is always a mini-buffer window, but it may not be the same window
505 currently active as a mini-buffer. */
507 Lisp_Object echo_area_window;
509 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
510 pushes the current message and the value of
511 message_enable_multibyte on the stack, the function restore_message
512 pops the stack and displays MESSAGE again. */
514 static Lisp_Object Vmessage_stack;
516 /* Nonzero means multibyte characters were enabled when the echo area
517 message was specified. */
519 static bool message_enable_multibyte;
521 /* Nonzero if we should redraw the mode lines on the next redisplay.
522 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
523 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
524 (the number used is then only used to track down the cause for this
525 full-redisplay). */
527 int update_mode_lines;
529 /* Nonzero if window sizes or contents other than selected-window have changed
530 since last redisplay that finished.
531 If it has value REDISPLAY_SOME, then only redisplay the windows where
532 the `redisplay' bit has been set. Otherwise, redisplay all windows
533 (the number used is then only used to track down the cause for this
534 full-redisplay). */
536 int windows_or_buffers_changed;
538 /* Nonzero after display_mode_line if %l was used and it displayed a
539 line number. */
541 static bool line_number_displayed;
543 /* The name of the *Messages* buffer, a string. */
545 static Lisp_Object Vmessages_buffer_name;
547 /* Current, index 0, and last displayed echo area message. Either
548 buffers from echo_buffers, or nil to indicate no message. */
550 Lisp_Object echo_area_buffer[2];
552 /* The buffers referenced from echo_area_buffer. */
554 static Lisp_Object echo_buffer[2];
556 /* A vector saved used in with_area_buffer to reduce consing. */
558 static Lisp_Object Vwith_echo_area_save_vector;
560 /* Non-zero means display_echo_area should display the last echo area
561 message again. Set by redisplay_preserve_echo_area. */
563 static bool display_last_displayed_message_p;
565 /* Nonzero if echo area is being used by print; zero if being used by
566 message. */
568 static bool message_buf_print;
570 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
572 static Lisp_Object Qinhibit_menubar_update;
573 static Lisp_Object Qmessage_truncate_lines;
575 /* Set to 1 in clear_message to make redisplay_internal aware
576 of an emptied echo area. */
578 static bool message_cleared_p;
580 /* A scratch glyph row with contents used for generating truncation
581 glyphs. Also used in direct_output_for_insert. */
583 #define MAX_SCRATCH_GLYPHS 100
584 static struct glyph_row scratch_glyph_row;
585 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
587 /* Ascent and height of the last line processed by move_it_to. */
589 static int last_height;
591 /* Non-zero if there's a help-echo in the echo area. */
593 bool help_echo_showing_p;
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
601 #define TEXT_PROP_DISTANCE_LIMIT 100
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
626 /* Functions to mark elements as needing redisplay. */
627 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
629 void
630 redisplay_other_windows (void)
632 if (!windows_or_buffers_changed)
633 windows_or_buffers_changed = REDISPLAY_SOME;
636 void
637 wset_redisplay (struct window *w)
639 /* Beware: selected_window can be nil during early stages. */
640 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
641 redisplay_other_windows ();
642 w->redisplay = true;
645 void
646 fset_redisplay (struct frame *f)
648 redisplay_other_windows ();
649 f->redisplay = true;
652 void
653 bset_redisplay (struct buffer *b)
655 int count = buffer_window_count (b);
656 if (count > 0)
658 /* ... it's visible in other window than selected, */
659 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
660 redisplay_other_windows ();
661 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
662 so that if we later set windows_or_buffers_changed, this buffer will
663 not be omitted. */
664 b->text->redisplay = true;
668 void
669 bset_update_mode_line (struct buffer *b)
671 if (!update_mode_lines)
672 update_mode_lines = REDISPLAY_SOME;
673 b->text->redisplay = true;
676 #ifdef GLYPH_DEBUG
678 /* Non-zero means print traces of redisplay if compiled with
679 GLYPH_DEBUG defined. */
681 bool trace_redisplay_p;
683 #endif /* GLYPH_DEBUG */
685 #ifdef DEBUG_TRACE_MOVE
686 /* Non-zero means trace with TRACE_MOVE to stderr. */
687 int trace_move;
689 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
690 #else
691 #define TRACE_MOVE(x) (void) 0
692 #endif
694 static Lisp_Object Qauto_hscroll_mode;
696 /* Buffer being redisplayed -- for redisplay_window_error. */
698 static struct buffer *displayed_buffer;
700 /* Value returned from text property handlers (see below). */
702 enum prop_handled
704 HANDLED_NORMALLY,
705 HANDLED_RECOMPUTE_PROPS,
706 HANDLED_OVERLAY_STRING_CONSUMED,
707 HANDLED_RETURN
710 /* A description of text properties that redisplay is interested
711 in. */
713 struct props
715 /* The name of the property. */
716 Lisp_Object *name;
718 /* A unique index for the property. */
719 enum prop_idx idx;
721 /* A handler function called to set up iterator IT from the property
722 at IT's current position. Value is used to steer handle_stop. */
723 enum prop_handled (*handler) (struct it *it);
726 static enum prop_handled handle_face_prop (struct it *);
727 static enum prop_handled handle_invisible_prop (struct it *);
728 static enum prop_handled handle_display_prop (struct it *);
729 static enum prop_handled handle_composition_prop (struct it *);
730 static enum prop_handled handle_overlay_change (struct it *);
731 static enum prop_handled handle_fontified_prop (struct it *);
733 /* Properties handled by iterators. */
735 static struct props it_props[] =
737 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
738 /* Handle `face' before `display' because some sub-properties of
739 `display' need to know the face. */
740 {&Qface, FACE_PROP_IDX, handle_face_prop},
741 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
742 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
743 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
744 {NULL, 0, NULL}
747 /* Value is the position described by X. If X is a marker, value is
748 the marker_position of X. Otherwise, value is X. */
750 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
752 /* Enumeration returned by some move_it_.* functions internally. */
754 enum move_it_result
756 /* Not used. Undefined value. */
757 MOVE_UNDEFINED,
759 /* Move ended at the requested buffer position or ZV. */
760 MOVE_POS_MATCH_OR_ZV,
762 /* Move ended at the requested X pixel position. */
763 MOVE_X_REACHED,
765 /* Move within a line ended at the end of a line that must be
766 continued. */
767 MOVE_LINE_CONTINUED,
769 /* Move within a line ended at the end of a line that would
770 be displayed truncated. */
771 MOVE_LINE_TRUNCATED,
773 /* Move within a line ended at a line end. */
774 MOVE_NEWLINE_OR_CR
777 /* This counter is used to clear the face cache every once in a while
778 in redisplay_internal. It is incremented for each redisplay.
779 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
780 cleared. */
782 #define CLEAR_FACE_CACHE_COUNT 500
783 static int clear_face_cache_count;
785 /* Similarly for the image cache. */
787 #ifdef HAVE_WINDOW_SYSTEM
788 #define CLEAR_IMAGE_CACHE_COUNT 101
789 static int clear_image_cache_count;
791 /* Null glyph slice */
792 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
793 #endif
795 /* True while redisplay_internal is in progress. */
797 bool redisplaying_p;
799 static Lisp_Object Qinhibit_free_realized_faces;
800 static Lisp_Object Qmode_line_default_help_echo;
802 /* If a string, XTread_socket generates an event to display that string.
803 (The display is done in read_char.) */
805 Lisp_Object help_echo_string;
806 Lisp_Object help_echo_window;
807 Lisp_Object help_echo_object;
808 ptrdiff_t help_echo_pos;
810 /* Temporary variable for XTread_socket. */
812 Lisp_Object previous_help_echo_string;
814 /* Platform-independent portion of hourglass implementation. */
816 #ifdef HAVE_WINDOW_SYSTEM
818 /* Non-zero means an hourglass cursor is currently shown. */
819 bool hourglass_shown_p;
821 /* If non-null, an asynchronous timer that, when it expires, displays
822 an hourglass cursor on all frames. */
823 struct atimer *hourglass_atimer;
825 #endif /* HAVE_WINDOW_SYSTEM */
827 /* Name of the face used to display glyphless characters. */
828 static Lisp_Object Qglyphless_char;
830 /* Symbol for the purpose of Vglyphless_char_display. */
831 static Lisp_Object Qglyphless_char_display;
833 /* Method symbols for Vglyphless_char_display. */
834 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
836 /* Default number of seconds to wait before displaying an hourglass
837 cursor. */
838 #define DEFAULT_HOURGLASS_DELAY 1
840 #ifdef HAVE_WINDOW_SYSTEM
842 /* Default pixel width of `thin-space' display method. */
843 #define THIN_SPACE_WIDTH 1
845 #endif /* HAVE_WINDOW_SYSTEM */
847 /* Function prototypes. */
849 static void setup_for_ellipsis (struct it *, int);
850 static void set_iterator_to_next (struct it *, int);
851 static void mark_window_display_accurate_1 (struct window *, int);
852 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
853 static int display_prop_string_p (Lisp_Object, Lisp_Object);
854 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
855 static int cursor_row_p (struct glyph_row *);
856 static int redisplay_mode_lines (Lisp_Object, bool);
857 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
859 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
861 static void handle_line_prefix (struct it *);
863 static void pint2str (char *, int, ptrdiff_t);
864 static void pint2hrstr (char *, int, ptrdiff_t);
865 static struct text_pos run_window_scroll_functions (Lisp_Object,
866 struct text_pos);
867 static int text_outside_line_unchanged_p (struct window *,
868 ptrdiff_t, ptrdiff_t);
869 static void store_mode_line_noprop_char (char);
870 static int store_mode_line_noprop (const char *, int, int);
871 static void handle_stop (struct it *);
872 static void handle_stop_backwards (struct it *, ptrdiff_t);
873 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
874 static void ensure_echo_area_buffers (void);
875 static void unwind_with_echo_area_buffer (Lisp_Object);
876 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
877 static int with_echo_area_buffer (struct window *, int,
878 int (*) (ptrdiff_t, Lisp_Object),
879 ptrdiff_t, Lisp_Object);
880 static void clear_garbaged_frames (void);
881 static int current_message_1 (ptrdiff_t, Lisp_Object);
882 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
883 static void set_message (Lisp_Object);
884 static int set_message_1 (ptrdiff_t, Lisp_Object);
885 static int display_echo_area (struct window *);
886 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
887 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
888 static void unwind_redisplay (void);
889 static int string_char_and_length (const unsigned char *, int *);
890 static struct text_pos display_prop_end (struct it *, Lisp_Object,
891 struct text_pos);
892 static int compute_window_start_on_continuation_line (struct window *);
893 static void insert_left_trunc_glyphs (struct it *);
894 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
895 Lisp_Object);
896 static void extend_face_to_end_of_line (struct it *);
897 static int append_space_for_newline (struct it *, int);
898 static int cursor_row_fully_visible_p (struct window *, int, int);
899 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
900 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
901 static int trailing_whitespace_p (ptrdiff_t);
902 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
903 static void push_it (struct it *, struct text_pos *);
904 static void iterate_out_of_display_property (struct it *);
905 static void pop_it (struct it *);
906 static void sync_frame_with_window_matrix_rows (struct window *);
907 static void redisplay_internal (void);
908 static int echo_area_display (int);
909 static void redisplay_windows (Lisp_Object);
910 static void redisplay_window (Lisp_Object, bool);
911 static Lisp_Object redisplay_window_error (Lisp_Object);
912 static Lisp_Object redisplay_window_0 (Lisp_Object);
913 static Lisp_Object redisplay_window_1 (Lisp_Object);
914 static int set_cursor_from_row (struct window *, struct glyph_row *,
915 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
916 int, int);
917 static int update_menu_bar (struct frame *, int, int);
918 static int try_window_reusing_current_matrix (struct window *);
919 static int try_window_id (struct window *);
920 static int display_line (struct it *);
921 static int display_mode_lines (struct window *);
922 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
923 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
924 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
925 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
926 static void display_menu_bar (struct window *);
927 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
928 ptrdiff_t *);
929 static int display_string (const char *, Lisp_Object, Lisp_Object,
930 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
931 static void compute_line_metrics (struct it *);
932 static void run_redisplay_end_trigger_hook (struct it *);
933 static int get_overlay_strings (struct it *, ptrdiff_t);
934 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
935 static void next_overlay_string (struct it *);
936 static void reseat (struct it *, struct text_pos, int);
937 static void reseat_1 (struct it *, struct text_pos, int);
938 static void back_to_previous_visible_line_start (struct it *);
939 static void reseat_at_next_visible_line_start (struct it *, int);
940 static int next_element_from_ellipsis (struct it *);
941 static int next_element_from_display_vector (struct it *);
942 static int next_element_from_string (struct it *);
943 static int next_element_from_c_string (struct it *);
944 static int next_element_from_buffer (struct it *);
945 static int next_element_from_composition (struct it *);
946 static int next_element_from_image (struct it *);
947 static int next_element_from_stretch (struct it *);
948 static void load_overlay_strings (struct it *, ptrdiff_t);
949 static int init_from_display_pos (struct it *, struct window *,
950 struct display_pos *);
951 static void reseat_to_string (struct it *, const char *,
952 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
953 static int get_next_display_element (struct it *);
954 static enum move_it_result
955 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
956 enum move_operation_enum);
957 static void get_visually_first_element (struct it *);
958 static void init_to_row_start (struct it *, struct window *,
959 struct glyph_row *);
960 static int init_to_row_end (struct it *, struct window *,
961 struct glyph_row *);
962 static void back_to_previous_line_start (struct it *);
963 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
964 static struct text_pos string_pos_nchars_ahead (struct text_pos,
965 Lisp_Object, ptrdiff_t);
966 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
967 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
968 static ptrdiff_t number_of_chars (const char *, bool);
969 static void compute_stop_pos (struct it *);
970 static void compute_string_pos (struct text_pos *, struct text_pos,
971 Lisp_Object);
972 static int face_before_or_after_it_pos (struct it *, int);
973 static ptrdiff_t next_overlay_change (ptrdiff_t);
974 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
975 Lisp_Object, struct text_pos *, ptrdiff_t, int);
976 static int handle_single_display_spec (struct it *, Lisp_Object,
977 Lisp_Object, Lisp_Object,
978 struct text_pos *, ptrdiff_t, int, int);
979 static int underlying_face_id (struct it *);
980 static int in_ellipses_for_invisible_text_p (struct display_pos *,
981 struct window *);
983 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
984 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
986 #ifdef HAVE_WINDOW_SYSTEM
988 static void x_consider_frame_title (Lisp_Object);
989 static void update_tool_bar (struct frame *, int);
990 static int redisplay_tool_bar (struct frame *);
991 static void x_draw_bottom_divider (struct window *w);
992 static void notice_overwritten_cursor (struct window *,
993 enum glyph_row_area,
994 int, int, int, int);
995 static void append_stretch_glyph (struct it *, Lisp_Object,
996 int, int, int);
999 #endif /* HAVE_WINDOW_SYSTEM */
1001 static void produce_special_glyphs (struct it *, enum display_element_type);
1002 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
1003 static bool coords_in_mouse_face_p (struct window *, int, int);
1007 /***********************************************************************
1008 Window display dimensions
1009 ***********************************************************************/
1011 /* Return the bottom boundary y-position for text lines in window W.
1012 This is the first y position at which a line cannot start.
1013 It is relative to the top of the window.
1015 This is the height of W minus the height of a mode line, if any. */
1018 window_text_bottom_y (struct window *w)
1020 int height = WINDOW_PIXEL_HEIGHT (w);
1022 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 height -= CURRENT_MODE_LINE_HEIGHT (w);
1027 return height;
1030 /* Return the pixel width of display area AREA of window W.
1031 ANY_AREA means return the total width of W, not including
1032 fringes to the left and right of the window. */
1035 window_box_width (struct window *w, enum glyph_row_area area)
1037 int width = w->pixel_width;
1039 if (!w->pseudo_window_p)
1041 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1042 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1044 if (area == TEXT_AREA)
1045 width -= (WINDOW_MARGINS_WIDTH (w)
1046 + WINDOW_FRINGES_WIDTH (w));
1047 else if (area == LEFT_MARGIN_AREA)
1048 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1049 else if (area == RIGHT_MARGIN_AREA)
1050 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1053 /* With wide margins, fringes, etc. we might end up with a negative
1054 width, correct that here. */
1055 return max (0, width);
1059 /* Return the pixel height of the display area of window W, not
1060 including mode lines of W, if any. */
1063 window_box_height (struct window *w)
1065 struct frame *f = XFRAME (w->frame);
1066 int height = WINDOW_PIXEL_HEIGHT (w);
1068 eassert (height >= 0);
1070 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1072 /* Note: the code below that determines the mode-line/header-line
1073 height is essentially the same as that contained in the macro
1074 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1075 the appropriate glyph row has its `mode_line_p' flag set,
1076 and if it doesn't, uses estimate_mode_line_height instead. */
1078 if (WINDOW_WANTS_MODELINE_P (w))
1080 struct glyph_row *ml_row
1081 = (w->current_matrix && w->current_matrix->rows
1082 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1083 : 0);
1084 if (ml_row && ml_row->mode_line_p)
1085 height -= ml_row->height;
1086 else
1087 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1090 if (WINDOW_WANTS_HEADER_LINE_P (w))
1092 struct glyph_row *hl_row
1093 = (w->current_matrix && w->current_matrix->rows
1094 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1095 : 0);
1096 if (hl_row && hl_row->mode_line_p)
1097 height -= hl_row->height;
1098 else
1099 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1102 /* With a very small font and a mode-line that's taller than
1103 default, we might end up with a negative height. */
1104 return max (0, height);
1107 /* Return the window-relative coordinate of the left edge of display
1108 area AREA of window W. ANY_AREA means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1112 window_box_left_offset (struct window *w, enum glyph_row_area area)
1114 int x;
1116 if (w->pseudo_window_p)
1117 return 0;
1119 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1121 if (area == TEXT_AREA)
1122 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1123 + window_box_width (w, LEFT_MARGIN_AREA));
1124 else if (area == RIGHT_MARGIN_AREA)
1125 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1126 + window_box_width (w, LEFT_MARGIN_AREA)
1127 + window_box_width (w, TEXT_AREA)
1128 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1130 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1131 else if (area == LEFT_MARGIN_AREA
1132 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1133 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1135 /* Don't return more than the window's pixel width. */
1136 return min (x, w->pixel_width);
1140 /* Return the window-relative coordinate of the right edge of display
1141 area AREA of window W. ANY_AREA means return the right edge of the
1142 whole window, to the left of the right fringe of W. */
1145 window_box_right_offset (struct window *w, enum glyph_row_area area)
1147 /* Don't return more than the window's pixel width. */
1148 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1149 w->pixel_width);
1152 /* Return the frame-relative coordinate of the left edge of display
1153 area AREA of window W. ANY_AREA means return the left edge of the
1154 whole window, to the right of the left fringe of W. */
1157 window_box_left (struct window *w, enum glyph_row_area area)
1159 struct frame *f = XFRAME (w->frame);
1160 int x;
1162 if (w->pseudo_window_p)
1163 return FRAME_INTERNAL_BORDER_WIDTH (f);
1165 x = (WINDOW_LEFT_EDGE_X (w)
1166 + window_box_left_offset (w, area));
1168 return x;
1172 /* Return the frame-relative coordinate of the right edge of display
1173 area AREA of window W. ANY_AREA means return the right edge of the
1174 whole window, to the left of the right fringe of W. */
1177 window_box_right (struct window *w, enum glyph_row_area area)
1179 return window_box_left (w, area) + window_box_width (w, area);
1182 /* Get the bounding box of the display area AREA of window W, without
1183 mode lines, in frame-relative coordinates. ANY_AREA means the
1184 whole window, not including the left and right fringes of
1185 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1186 coordinates of the upper-left corner of the box. Return in
1187 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1189 void
1190 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1191 int *box_y, int *box_width, int *box_height)
1193 if (box_width)
1194 *box_width = window_box_width (w, area);
1195 if (box_height)
1196 *box_height = window_box_height (w);
1197 if (box_x)
1198 *box_x = window_box_left (w, area);
1199 if (box_y)
1201 *box_y = WINDOW_TOP_EDGE_Y (w);
1202 if (WINDOW_WANTS_HEADER_LINE_P (w))
1203 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1207 #ifdef HAVE_WINDOW_SYSTEM
1209 /* Get the bounding box of the display area AREA of window W, without
1210 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1211 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1212 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1213 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1214 box. */
1216 static void
1217 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1218 int *bottom_right_x, int *bottom_right_y)
1220 window_box (w, ANY_AREA, top_left_x, top_left_y,
1221 bottom_right_x, bottom_right_y);
1222 *bottom_right_x += *top_left_x;
1223 *bottom_right_y += *top_left_y;
1226 #endif /* HAVE_WINDOW_SYSTEM */
1228 /***********************************************************************
1229 Utilities
1230 ***********************************************************************/
1232 /* Return the bottom y-position of the line the iterator IT is in.
1233 This can modify IT's settings. */
1236 line_bottom_y (struct it *it)
1238 int line_height = it->max_ascent + it->max_descent;
1239 int line_top_y = it->current_y;
1241 if (line_height == 0)
1243 if (last_height)
1244 line_height = last_height;
1245 else if (IT_CHARPOS (*it) < ZV)
1247 move_it_by_lines (it, 1);
1248 line_height = (it->max_ascent || it->max_descent
1249 ? it->max_ascent + it->max_descent
1250 : last_height);
1252 else
1254 struct glyph_row *row = it->glyph_row;
1256 /* Use the default character height. */
1257 it->glyph_row = NULL;
1258 it->what = IT_CHARACTER;
1259 it->c = ' ';
1260 it->len = 1;
1261 PRODUCE_GLYPHS (it);
1262 line_height = it->ascent + it->descent;
1263 it->glyph_row = row;
1267 return line_top_y + line_height;
1270 DEFUN ("line-pixel-height", Fline_pixel_height,
1271 Sline_pixel_height, 0, 0, 0,
1272 doc: /* Return height in pixels of text line in the selected window.
1274 Value is the height in pixels of the line at point. */)
1275 (void)
1277 struct it it;
1278 struct text_pos pt;
1279 struct window *w = XWINDOW (selected_window);
1280 struct buffer *old_buffer = NULL;
1281 Lisp_Object result;
1283 if (XBUFFER (w->contents) != current_buffer)
1285 old_buffer = current_buffer;
1286 set_buffer_internal_1 (XBUFFER (w->contents));
1288 SET_TEXT_POS (pt, PT, PT_BYTE);
1289 start_display (&it, w, pt);
1290 it.vpos = it.current_y = 0;
1291 last_height = 0;
1292 result = make_number (line_bottom_y (&it));
1293 if (old_buffer)
1294 set_buffer_internal_1 (old_buffer);
1296 return result;
1299 /* Return the default pixel height of text lines in window W. The
1300 value is the canonical height of the W frame's default font, plus
1301 any extra space required by the line-spacing variable or frame
1302 parameter.
1304 Implementation note: this ignores any line-spacing text properties
1305 put on the newline characters. This is because those properties
1306 only affect the _screen_ line ending in the newline (i.e., in a
1307 continued line, only the last screen line will be affected), which
1308 means only a small number of lines in a buffer can ever use this
1309 feature. Since this function is used to compute the default pixel
1310 equivalent of text lines in a window, we can safely ignore those
1311 few lines. For the same reasons, we ignore the line-height
1312 properties. */
1314 default_line_pixel_height (struct window *w)
1316 struct frame *f = WINDOW_XFRAME (w);
1317 int height = FRAME_LINE_HEIGHT (f);
1319 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1321 struct buffer *b = XBUFFER (w->contents);
1322 Lisp_Object val = BVAR (b, extra_line_spacing);
1324 if (NILP (val))
1325 val = BVAR (&buffer_defaults, extra_line_spacing);
1326 if (!NILP (val))
1328 if (RANGED_INTEGERP (0, val, INT_MAX))
1329 height += XFASTINT (val);
1330 else if (FLOATP (val))
1332 int addon = XFLOAT_DATA (val) * height + 0.5;
1334 if (addon >= 0)
1335 height += addon;
1338 else
1339 height += f->extra_line_spacing;
1342 return height;
1345 /* Subroutine of pos_visible_p below. Extracts a display string, if
1346 any, from the display spec given as its argument. */
1347 static Lisp_Object
1348 string_from_display_spec (Lisp_Object spec)
1350 if (CONSP (spec))
1352 while (CONSP (spec))
1354 if (STRINGP (XCAR (spec)))
1355 return XCAR (spec);
1356 spec = XCDR (spec);
1359 else if (VECTORP (spec))
1361 ptrdiff_t i;
1363 for (i = 0; i < ASIZE (spec); i++)
1365 if (STRINGP (AREF (spec, i)))
1366 return AREF (spec, i);
1368 return Qnil;
1371 return spec;
1375 /* Limit insanely large values of W->hscroll on frame F to the largest
1376 value that will still prevent first_visible_x and last_visible_x of
1377 'struct it' from overflowing an int. */
1378 static int
1379 window_hscroll_limited (struct window *w, struct frame *f)
1381 ptrdiff_t window_hscroll = w->hscroll;
1382 int window_text_width = window_box_width (w, TEXT_AREA);
1383 int colwidth = FRAME_COLUMN_WIDTH (f);
1385 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1386 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1388 return window_hscroll;
1391 /* Return 1 if position CHARPOS is visible in window W.
1392 CHARPOS < 0 means return info about WINDOW_END position.
1393 If visible, set *X and *Y to pixel coordinates of top left corner.
1394 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1395 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1398 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1399 int *rtop, int *rbot, int *rowh, int *vpos)
1401 struct it it;
1402 void *itdata = bidi_shelve_cache ();
1403 struct text_pos top;
1404 int visible_p = 0;
1405 struct buffer *old_buffer = NULL;
1407 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1408 return visible_p;
1410 if (XBUFFER (w->contents) != current_buffer)
1412 old_buffer = current_buffer;
1413 set_buffer_internal_1 (XBUFFER (w->contents));
1416 SET_TEXT_POS_FROM_MARKER (top, w->start);
1417 /* Scrolling a minibuffer window via scroll bar when the echo area
1418 shows long text sometimes resets the minibuffer contents behind
1419 our backs. */
1420 if (CHARPOS (top) > ZV)
1421 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1423 /* Compute exact mode line heights. */
1424 if (WINDOW_WANTS_MODELINE_P (w))
1425 w->mode_line_height
1426 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1427 BVAR (current_buffer, mode_line_format));
1429 if (WINDOW_WANTS_HEADER_LINE_P (w))
1430 w->header_line_height
1431 = display_mode_line (w, HEADER_LINE_FACE_ID,
1432 BVAR (current_buffer, header_line_format));
1434 start_display (&it, w, top);
1435 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1436 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1438 if (charpos >= 0
1439 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1440 && IT_CHARPOS (it) >= charpos)
1441 /* When scanning backwards under bidi iteration, move_it_to
1442 stops at or _before_ CHARPOS, because it stops at or to
1443 the _right_ of the character at CHARPOS. */
1444 || (it.bidi_p && it.bidi_it.scan_dir == -1
1445 && IT_CHARPOS (it) <= charpos)))
1447 /* We have reached CHARPOS, or passed it. How the call to
1448 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1449 or covered by a display property, move_it_to stops at the end
1450 of the invisible text, to the right of CHARPOS. (ii) If
1451 CHARPOS is in a display vector, move_it_to stops on its last
1452 glyph. */
1453 int top_x = it.current_x;
1454 int top_y = it.current_y;
1455 /* Calling line_bottom_y may change it.method, it.position, etc. */
1456 enum it_method it_method = it.method;
1457 int bottom_y = (last_height = 0, line_bottom_y (&it));
1458 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1460 if (top_y < window_top_y)
1461 visible_p = bottom_y > window_top_y;
1462 else if (top_y < it.last_visible_y)
1463 visible_p = true;
1464 if (bottom_y >= it.last_visible_y
1465 && it.bidi_p && it.bidi_it.scan_dir == -1
1466 && IT_CHARPOS (it) < charpos)
1468 /* When the last line of the window is scanned backwards
1469 under bidi iteration, we could be duped into thinking
1470 that we have passed CHARPOS, when in fact move_it_to
1471 simply stopped short of CHARPOS because it reached
1472 last_visible_y. To see if that's what happened, we call
1473 move_it_to again with a slightly larger vertical limit,
1474 and see if it actually moved vertically; if it did, we
1475 didn't really reach CHARPOS, which is beyond window end. */
1476 struct it save_it = it;
1477 /* Why 10? because we don't know how many canonical lines
1478 will the height of the next line(s) be. So we guess. */
1479 int ten_more_lines = 10 * default_line_pixel_height (w);
1481 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1482 MOVE_TO_POS | MOVE_TO_Y);
1483 if (it.current_y > top_y)
1484 visible_p = 0;
1486 it = save_it;
1488 if (visible_p)
1490 if (it_method == GET_FROM_DISPLAY_VECTOR)
1492 /* We stopped on the last glyph of a display vector.
1493 Try and recompute. Hack alert! */
1494 if (charpos < 2 || top.charpos >= charpos)
1495 top_x = it.glyph_row->x;
1496 else
1498 struct it it2, it2_prev;
1499 /* The idea is to get to the previous buffer
1500 position, consume the character there, and use
1501 the pixel coordinates we get after that. But if
1502 the previous buffer position is also displayed
1503 from a display vector, we need to consume all of
1504 the glyphs from that display vector. */
1505 start_display (&it2, w, top);
1506 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1507 /* If we didn't get to CHARPOS - 1, there's some
1508 replacing display property at that position, and
1509 we stopped after it. That is exactly the place
1510 whose coordinates we want. */
1511 if (IT_CHARPOS (it2) != charpos - 1)
1512 it2_prev = it2;
1513 else
1515 /* Iterate until we get out of the display
1516 vector that displays the character at
1517 CHARPOS - 1. */
1518 do {
1519 get_next_display_element (&it2);
1520 PRODUCE_GLYPHS (&it2);
1521 it2_prev = it2;
1522 set_iterator_to_next (&it2, 1);
1523 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1524 && IT_CHARPOS (it2) < charpos);
1526 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1527 || it2_prev.current_x > it2_prev.last_visible_x)
1528 top_x = it.glyph_row->x;
1529 else
1531 top_x = it2_prev.current_x;
1532 top_y = it2_prev.current_y;
1536 else if (IT_CHARPOS (it) != charpos)
1538 Lisp_Object cpos = make_number (charpos);
1539 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1540 Lisp_Object string = string_from_display_spec (spec);
1541 struct text_pos tpos;
1542 int replacing_spec_p;
1543 bool newline_in_string
1544 = (STRINGP (string)
1545 && memchr (SDATA (string), '\n', SBYTES (string)));
1547 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1548 replacing_spec_p
1549 = (!NILP (spec)
1550 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1551 charpos, FRAME_WINDOW_P (it.f)));
1552 /* The tricky code below is needed because there's a
1553 discrepancy between move_it_to and how we set cursor
1554 when PT is at the beginning of a portion of text
1555 covered by a display property or an overlay with a
1556 display property, or the display line ends in a
1557 newline from a display string. move_it_to will stop
1558 _after_ such display strings, whereas
1559 set_cursor_from_row conspires with cursor_row_p to
1560 place the cursor on the first glyph produced from the
1561 display string. */
1563 /* We have overshoot PT because it is covered by a
1564 display property that replaces the text it covers.
1565 If the string includes embedded newlines, we are also
1566 in the wrong display line. Backtrack to the correct
1567 line, where the display property begins. */
1568 if (replacing_spec_p)
1570 Lisp_Object startpos, endpos;
1571 EMACS_INT start, end;
1572 struct it it3;
1573 int it3_moved;
1575 /* Find the first and the last buffer positions
1576 covered by the display string. */
1577 endpos =
1578 Fnext_single_char_property_change (cpos, Qdisplay,
1579 Qnil, Qnil);
1580 startpos =
1581 Fprevious_single_char_property_change (endpos, Qdisplay,
1582 Qnil, Qnil);
1583 start = XFASTINT (startpos);
1584 end = XFASTINT (endpos);
1585 /* Move to the last buffer position before the
1586 display property. */
1587 start_display (&it3, w, top);
1588 if (start > CHARPOS (top))
1589 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1590 /* Move forward one more line if the position before
1591 the display string is a newline or if it is the
1592 rightmost character on a line that is
1593 continued or word-wrapped. */
1594 if (it3.method == GET_FROM_BUFFER
1595 && (it3.c == '\n'
1596 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1597 move_it_by_lines (&it3, 1);
1598 else if (move_it_in_display_line_to (&it3, -1,
1599 it3.current_x
1600 + it3.pixel_width,
1601 MOVE_TO_X)
1602 == MOVE_LINE_CONTINUED)
1604 move_it_by_lines (&it3, 1);
1605 /* When we are under word-wrap, the #$@%!
1606 move_it_by_lines moves 2 lines, so we need to
1607 fix that up. */
1608 if (it3.line_wrap == WORD_WRAP)
1609 move_it_by_lines (&it3, -1);
1612 /* Record the vertical coordinate of the display
1613 line where we wound up. */
1614 top_y = it3.current_y;
1615 if (it3.bidi_p)
1617 /* When characters are reordered for display,
1618 the character displayed to the left of the
1619 display string could be _after_ the display
1620 property in the logical order. Use the
1621 smallest vertical position of these two. */
1622 start_display (&it3, w, top);
1623 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1624 if (it3.current_y < top_y)
1625 top_y = it3.current_y;
1627 /* Move from the top of the window to the beginning
1628 of the display line where the display string
1629 begins. */
1630 start_display (&it3, w, top);
1631 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1632 /* If it3_moved stays zero after the 'while' loop
1633 below, that means we already were at a newline
1634 before the loop (e.g., the display string begins
1635 with a newline), so we don't need to (and cannot)
1636 inspect the glyphs of it3.glyph_row, because
1637 PRODUCE_GLYPHS will not produce anything for a
1638 newline, and thus it3.glyph_row stays at its
1639 stale content it got at top of the window. */
1640 it3_moved = 0;
1641 /* Finally, advance the iterator until we hit the
1642 first display element whose character position is
1643 CHARPOS, or until the first newline from the
1644 display string, which signals the end of the
1645 display line. */
1646 while (get_next_display_element (&it3))
1648 PRODUCE_GLYPHS (&it3);
1649 if (IT_CHARPOS (it3) == charpos
1650 || ITERATOR_AT_END_OF_LINE_P (&it3))
1651 break;
1652 it3_moved = 1;
1653 set_iterator_to_next (&it3, 0);
1655 top_x = it3.current_x - it3.pixel_width;
1656 /* Normally, we would exit the above loop because we
1657 found the display element whose character
1658 position is CHARPOS. For the contingency that we
1659 didn't, and stopped at the first newline from the
1660 display string, move back over the glyphs
1661 produced from the string, until we find the
1662 rightmost glyph not from the string. */
1663 if (it3_moved
1664 && newline_in_string
1665 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1667 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1668 + it3.glyph_row->used[TEXT_AREA];
1670 while (EQ ((g - 1)->object, string))
1672 --g;
1673 top_x -= g->pixel_width;
1675 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1676 + it3.glyph_row->used[TEXT_AREA]);
1681 *x = top_x;
1682 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1683 *rtop = max (0, window_top_y - top_y);
1684 *rbot = max (0, bottom_y - it.last_visible_y);
1685 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1686 - max (top_y, window_top_y)));
1687 *vpos = it.vpos;
1690 else
1692 /* Either we were asked to provide info about WINDOW_END, or
1693 CHARPOS is in the partially visible glyph row at end of
1694 window. */
1695 struct it it2;
1696 void *it2data = NULL;
1698 SAVE_IT (it2, it, it2data);
1699 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1700 move_it_by_lines (&it, 1);
1701 if (charpos < IT_CHARPOS (it)
1702 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1704 visible_p = true;
1705 RESTORE_IT (&it2, &it2, it2data);
1706 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1707 *x = it2.current_x;
1708 *y = it2.current_y + it2.max_ascent - it2.ascent;
1709 *rtop = max (0, -it2.current_y);
1710 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1711 - it.last_visible_y));
1712 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1713 it.last_visible_y)
1714 - max (it2.current_y,
1715 WINDOW_HEADER_LINE_HEIGHT (w))));
1716 *vpos = it2.vpos;
1718 else
1719 bidi_unshelve_cache (it2data, 1);
1721 bidi_unshelve_cache (itdata, 0);
1723 if (old_buffer)
1724 set_buffer_internal_1 (old_buffer);
1726 if (visible_p && w->hscroll > 0)
1727 *x -=
1728 window_hscroll_limited (w, WINDOW_XFRAME (w))
1729 * WINDOW_FRAME_COLUMN_WIDTH (w);
1731 #if 0
1732 /* Debugging code. */
1733 if (visible_p)
1734 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1735 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1736 else
1737 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1738 #endif
1740 return visible_p;
1744 /* Return the next character from STR. Return in *LEN the length of
1745 the character. This is like STRING_CHAR_AND_LENGTH but never
1746 returns an invalid character. If we find one, we return a `?', but
1747 with the length of the invalid character. */
1749 static int
1750 string_char_and_length (const unsigned char *str, int *len)
1752 int c;
1754 c = STRING_CHAR_AND_LENGTH (str, *len);
1755 if (!CHAR_VALID_P (c))
1756 /* We may not change the length here because other places in Emacs
1757 don't use this function, i.e. they silently accept invalid
1758 characters. */
1759 c = '?';
1761 return c;
1766 /* Given a position POS containing a valid character and byte position
1767 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1769 static struct text_pos
1770 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1772 eassert (STRINGP (string) && nchars >= 0);
1774 if (STRING_MULTIBYTE (string))
1776 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1777 int len;
1779 while (nchars--)
1781 string_char_and_length (p, &len);
1782 p += len;
1783 CHARPOS (pos) += 1;
1784 BYTEPOS (pos) += len;
1787 else
1788 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1790 return pos;
1794 /* Value is the text position, i.e. character and byte position,
1795 for character position CHARPOS in STRING. */
1797 static struct text_pos
1798 string_pos (ptrdiff_t charpos, Lisp_Object string)
1800 struct text_pos pos;
1801 eassert (STRINGP (string));
1802 eassert (charpos >= 0);
1803 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1804 return pos;
1808 /* Value is a text position, i.e. character and byte position, for
1809 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1810 means recognize multibyte characters. */
1812 static struct text_pos
1813 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1815 struct text_pos pos;
1817 eassert (s != NULL);
1818 eassert (charpos >= 0);
1820 if (multibyte_p)
1822 int len;
1824 SET_TEXT_POS (pos, 0, 0);
1825 while (charpos--)
1827 string_char_and_length ((const unsigned char *) s, &len);
1828 s += len;
1829 CHARPOS (pos) += 1;
1830 BYTEPOS (pos) += len;
1833 else
1834 SET_TEXT_POS (pos, charpos, charpos);
1836 return pos;
1840 /* Value is the number of characters in C string S. MULTIBYTE_P
1841 non-zero means recognize multibyte characters. */
1843 static ptrdiff_t
1844 number_of_chars (const char *s, bool multibyte_p)
1846 ptrdiff_t nchars;
1848 if (multibyte_p)
1850 ptrdiff_t rest = strlen (s);
1851 int len;
1852 const unsigned char *p = (const unsigned char *) s;
1854 for (nchars = 0; rest > 0; ++nchars)
1856 string_char_and_length (p, &len);
1857 rest -= len, p += len;
1860 else
1861 nchars = strlen (s);
1863 return nchars;
1867 /* Compute byte position NEWPOS->bytepos corresponding to
1868 NEWPOS->charpos. POS is a known position in string STRING.
1869 NEWPOS->charpos must be >= POS.charpos. */
1871 static void
1872 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1874 eassert (STRINGP (string));
1875 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1877 if (STRING_MULTIBYTE (string))
1878 *newpos = string_pos_nchars_ahead (pos, string,
1879 CHARPOS (*newpos) - CHARPOS (pos));
1880 else
1881 BYTEPOS (*newpos) = CHARPOS (*newpos);
1884 /* EXPORT:
1885 Return an estimation of the pixel height of mode or header lines on
1886 frame F. FACE_ID specifies what line's height to estimate. */
1889 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1891 #ifdef HAVE_WINDOW_SYSTEM
1892 if (FRAME_WINDOW_P (f))
1894 int height = FONT_HEIGHT (FRAME_FONT (f));
1896 /* This function is called so early when Emacs starts that the face
1897 cache and mode line face are not yet initialized. */
1898 if (FRAME_FACE_CACHE (f))
1900 struct face *face = FACE_FROM_ID (f, face_id);
1901 if (face)
1903 if (face->font)
1904 height = FONT_HEIGHT (face->font);
1905 if (face->box_line_width > 0)
1906 height += 2 * face->box_line_width;
1910 return height;
1912 #endif
1914 return 1;
1917 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1918 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1919 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1920 not force the value into range. */
1922 void
1923 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1924 int *x, int *y, NativeRectangle *bounds, int noclip)
1927 #ifdef HAVE_WINDOW_SYSTEM
1928 if (FRAME_WINDOW_P (f))
1930 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1931 even for negative values. */
1932 if (pix_x < 0)
1933 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1934 if (pix_y < 0)
1935 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1937 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1938 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1940 if (bounds)
1941 STORE_NATIVE_RECT (*bounds,
1942 FRAME_COL_TO_PIXEL_X (f, pix_x),
1943 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1944 FRAME_COLUMN_WIDTH (f) - 1,
1945 FRAME_LINE_HEIGHT (f) - 1);
1947 /* PXW: Should we clip pixels before converting to columns/lines? */
1948 if (!noclip)
1950 if (pix_x < 0)
1951 pix_x = 0;
1952 else if (pix_x > FRAME_TOTAL_COLS (f))
1953 pix_x = FRAME_TOTAL_COLS (f);
1955 if (pix_y < 0)
1956 pix_y = 0;
1957 else if (pix_y > FRAME_LINES (f))
1958 pix_y = FRAME_LINES (f);
1961 #endif
1963 *x = pix_x;
1964 *y = pix_y;
1968 /* Find the glyph under window-relative coordinates X/Y in window W.
1969 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1970 strings. Return in *HPOS and *VPOS the row and column number of
1971 the glyph found. Return in *AREA the glyph area containing X.
1972 Value is a pointer to the glyph found or null if X/Y is not on
1973 text, or we can't tell because W's current matrix is not up to
1974 date. */
1976 static struct glyph *
1977 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1978 int *dx, int *dy, int *area)
1980 struct glyph *glyph, *end;
1981 struct glyph_row *row = NULL;
1982 int x0, i;
1984 /* Find row containing Y. Give up if some row is not enabled. */
1985 for (i = 0; i < w->current_matrix->nrows; ++i)
1987 row = MATRIX_ROW (w->current_matrix, i);
1988 if (!row->enabled_p)
1989 return NULL;
1990 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1991 break;
1994 *vpos = i;
1995 *hpos = 0;
1997 /* Give up if Y is not in the window. */
1998 if (i == w->current_matrix->nrows)
1999 return NULL;
2001 /* Get the glyph area containing X. */
2002 if (w->pseudo_window_p)
2004 *area = TEXT_AREA;
2005 x0 = 0;
2007 else
2009 if (x < window_box_left_offset (w, TEXT_AREA))
2011 *area = LEFT_MARGIN_AREA;
2012 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
2014 else if (x < window_box_right_offset (w, TEXT_AREA))
2016 *area = TEXT_AREA;
2017 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
2019 else
2021 *area = RIGHT_MARGIN_AREA;
2022 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
2026 /* Find glyph containing X. */
2027 glyph = row->glyphs[*area];
2028 end = glyph + row->used[*area];
2029 x -= x0;
2030 while (glyph < end && x >= glyph->pixel_width)
2032 x -= glyph->pixel_width;
2033 ++glyph;
2036 if (glyph == end)
2037 return NULL;
2039 if (dx)
2041 *dx = x;
2042 *dy = y - (row->y + row->ascent - glyph->ascent);
2045 *hpos = glyph - row->glyphs[*area];
2046 return glyph;
2049 /* Convert frame-relative x/y to coordinates relative to window W.
2050 Takes pseudo-windows into account. */
2052 static void
2053 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2055 if (w->pseudo_window_p)
2057 /* A pseudo-window is always full-width, and starts at the
2058 left edge of the frame, plus a frame border. */
2059 struct frame *f = XFRAME (w->frame);
2060 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2061 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2063 else
2065 *x -= WINDOW_LEFT_EDGE_X (w);
2066 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2070 #ifdef HAVE_WINDOW_SYSTEM
2072 /* EXPORT:
2073 Return in RECTS[] at most N clipping rectangles for glyph string S.
2074 Return the number of stored rectangles. */
2077 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2079 XRectangle r;
2081 if (n <= 0)
2082 return 0;
2084 if (s->row->full_width_p)
2086 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2087 r.x = WINDOW_LEFT_EDGE_X (s->w);
2088 if (s->row->mode_line_p)
2089 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2090 else
2091 r.width = WINDOW_PIXEL_WIDTH (s->w);
2093 /* Unless displaying a mode or menu bar line, which are always
2094 fully visible, clip to the visible part of the row. */
2095 if (s->w->pseudo_window_p)
2096 r.height = s->row->visible_height;
2097 else
2098 r.height = s->height;
2100 else
2102 /* This is a text line that may be partially visible. */
2103 r.x = window_box_left (s->w, s->area);
2104 r.width = window_box_width (s->w, s->area);
2105 r.height = s->row->visible_height;
2108 if (s->clip_head)
2109 if (r.x < s->clip_head->x)
2111 if (r.width >= s->clip_head->x - r.x)
2112 r.width -= s->clip_head->x - r.x;
2113 else
2114 r.width = 0;
2115 r.x = s->clip_head->x;
2117 if (s->clip_tail)
2118 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2120 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2121 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2122 else
2123 r.width = 0;
2126 /* If S draws overlapping rows, it's sufficient to use the top and
2127 bottom of the window for clipping because this glyph string
2128 intentionally draws over other lines. */
2129 if (s->for_overlaps)
2131 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2132 r.height = window_text_bottom_y (s->w) - r.y;
2134 /* Alas, the above simple strategy does not work for the
2135 environments with anti-aliased text: if the same text is
2136 drawn onto the same place multiple times, it gets thicker.
2137 If the overlap we are processing is for the erased cursor, we
2138 take the intersection with the rectangle of the cursor. */
2139 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2141 XRectangle rc, r_save = r;
2143 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2144 rc.y = s->w->phys_cursor.y;
2145 rc.width = s->w->phys_cursor_width;
2146 rc.height = s->w->phys_cursor_height;
2148 x_intersect_rectangles (&r_save, &rc, &r);
2151 else
2153 /* Don't use S->y for clipping because it doesn't take partially
2154 visible lines into account. For example, it can be negative for
2155 partially visible lines at the top of a window. */
2156 if (!s->row->full_width_p
2157 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2158 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2159 else
2160 r.y = max (0, s->row->y);
2163 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2165 /* If drawing the cursor, don't let glyph draw outside its
2166 advertised boundaries. Cleartype does this under some circumstances. */
2167 if (s->hl == DRAW_CURSOR)
2169 struct glyph *glyph = s->first_glyph;
2170 int height, max_y;
2172 if (s->x > r.x)
2174 r.width -= s->x - r.x;
2175 r.x = s->x;
2177 r.width = min (r.width, glyph->pixel_width);
2179 /* If r.y is below window bottom, ensure that we still see a cursor. */
2180 height = min (glyph->ascent + glyph->descent,
2181 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2182 max_y = window_text_bottom_y (s->w) - height;
2183 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2184 if (s->ybase - glyph->ascent > max_y)
2186 r.y = max_y;
2187 r.height = height;
2189 else
2191 /* Don't draw cursor glyph taller than our actual glyph. */
2192 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2193 if (height < r.height)
2195 max_y = r.y + r.height;
2196 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2197 r.height = min (max_y - r.y, height);
2202 if (s->row->clip)
2204 XRectangle r_save = r;
2206 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2207 r.width = 0;
2210 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2211 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2213 #ifdef CONVERT_FROM_XRECT
2214 CONVERT_FROM_XRECT (r, *rects);
2215 #else
2216 *rects = r;
2217 #endif
2218 return 1;
2220 else
2222 /* If we are processing overlapping and allowed to return
2223 multiple clipping rectangles, we exclude the row of the glyph
2224 string from the clipping rectangle. This is to avoid drawing
2225 the same text on the environment with anti-aliasing. */
2226 #ifdef CONVERT_FROM_XRECT
2227 XRectangle rs[2];
2228 #else
2229 XRectangle *rs = rects;
2230 #endif
2231 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2233 if (s->for_overlaps & OVERLAPS_PRED)
2235 rs[i] = r;
2236 if (r.y + r.height > row_y)
2238 if (r.y < row_y)
2239 rs[i].height = row_y - r.y;
2240 else
2241 rs[i].height = 0;
2243 i++;
2245 if (s->for_overlaps & OVERLAPS_SUCC)
2247 rs[i] = r;
2248 if (r.y < row_y + s->row->visible_height)
2250 if (r.y + r.height > row_y + s->row->visible_height)
2252 rs[i].y = row_y + s->row->visible_height;
2253 rs[i].height = r.y + r.height - rs[i].y;
2255 else
2256 rs[i].height = 0;
2258 i++;
2261 n = i;
2262 #ifdef CONVERT_FROM_XRECT
2263 for (i = 0; i < n; i++)
2264 CONVERT_FROM_XRECT (rs[i], rects[i]);
2265 #endif
2266 return n;
2270 /* EXPORT:
2271 Return in *NR the clipping rectangle for glyph string S. */
2273 void
2274 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2276 get_glyph_string_clip_rects (s, nr, 1);
2280 /* EXPORT:
2281 Return the position and height of the phys cursor in window W.
2282 Set w->phys_cursor_width to width of phys cursor.
2285 void
2286 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2287 struct glyph *glyph, int *xp, int *yp, int *heightp)
2289 struct frame *f = XFRAME (WINDOW_FRAME (w));
2290 int x, y, wd, h, h0, y0;
2292 /* Compute the width of the rectangle to draw. If on a stretch
2293 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2294 rectangle as wide as the glyph, but use a canonical character
2295 width instead. */
2296 wd = glyph->pixel_width - 1;
2297 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2298 wd++; /* Why? */
2299 #endif
2301 x = w->phys_cursor.x;
2302 if (x < 0)
2304 wd += x;
2305 x = 0;
2308 if (glyph->type == STRETCH_GLYPH
2309 && !x_stretch_cursor_p)
2310 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2311 w->phys_cursor_width = wd;
2313 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2315 /* If y is below window bottom, ensure that we still see a cursor. */
2316 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2318 h = max (h0, glyph->ascent + glyph->descent);
2319 h0 = min (h0, glyph->ascent + glyph->descent);
2321 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2322 if (y < y0)
2324 h = max (h - (y0 - y) + 1, h0);
2325 y = y0 - 1;
2327 else
2329 y0 = window_text_bottom_y (w) - h0;
2330 if (y > y0)
2332 h += y - y0;
2333 y = y0;
2337 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2338 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2339 *heightp = h;
2343 * Remember which glyph the mouse is over.
2346 void
2347 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2349 Lisp_Object window;
2350 struct window *w;
2351 struct glyph_row *r, *gr, *end_row;
2352 enum window_part part;
2353 enum glyph_row_area area;
2354 int x, y, width, height;
2356 /* Try to determine frame pixel position and size of the glyph under
2357 frame pixel coordinates X/Y on frame F. */
2359 if (window_resize_pixelwise)
2361 width = height = 1;
2362 goto virtual_glyph;
2364 else if (!f->glyphs_initialized_p
2365 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2366 NILP (window)))
2368 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2369 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2370 goto virtual_glyph;
2373 w = XWINDOW (window);
2374 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2375 height = WINDOW_FRAME_LINE_HEIGHT (w);
2377 x = window_relative_x_coord (w, part, gx);
2378 y = gy - WINDOW_TOP_EDGE_Y (w);
2380 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2381 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2383 if (w->pseudo_window_p)
2385 area = TEXT_AREA;
2386 part = ON_MODE_LINE; /* Don't adjust margin. */
2387 goto text_glyph;
2390 switch (part)
2392 case ON_LEFT_MARGIN:
2393 area = LEFT_MARGIN_AREA;
2394 goto text_glyph;
2396 case ON_RIGHT_MARGIN:
2397 area = RIGHT_MARGIN_AREA;
2398 goto text_glyph;
2400 case ON_HEADER_LINE:
2401 case ON_MODE_LINE:
2402 gr = (part == ON_HEADER_LINE
2403 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2404 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2405 gy = gr->y;
2406 area = TEXT_AREA;
2407 goto text_glyph_row_found;
2409 case ON_TEXT:
2410 area = TEXT_AREA;
2412 text_glyph:
2413 gr = 0; gy = 0;
2414 for (; r <= end_row && r->enabled_p; ++r)
2415 if (r->y + r->height > y)
2417 gr = r; gy = r->y;
2418 break;
2421 text_glyph_row_found:
2422 if (gr && gy <= y)
2424 struct glyph *g = gr->glyphs[area];
2425 struct glyph *end = g + gr->used[area];
2427 height = gr->height;
2428 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2429 if (gx + g->pixel_width > x)
2430 break;
2432 if (g < end)
2434 if (g->type == IMAGE_GLYPH)
2436 /* Don't remember when mouse is over image, as
2437 image may have hot-spots. */
2438 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2439 return;
2441 width = g->pixel_width;
2443 else
2445 /* Use nominal char spacing at end of line. */
2446 x -= gx;
2447 gx += (x / width) * width;
2450 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2452 gx += window_box_left_offset (w, area);
2453 /* Don't expand over the modeline to make sure the vertical
2454 drag cursor is shown early enough. */
2455 height = min (height,
2456 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2459 else
2461 /* Use nominal line height at end of window. */
2462 gx = (x / width) * width;
2463 y -= gy;
2464 gy += (y / height) * height;
2465 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2466 /* See comment above. */
2467 height = min (height,
2468 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2470 break;
2472 case ON_LEFT_FRINGE:
2473 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2474 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2475 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2476 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2477 goto row_glyph;
2479 case ON_RIGHT_FRINGE:
2480 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2481 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2482 : window_box_right_offset (w, TEXT_AREA));
2483 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2484 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2485 && !WINDOW_RIGHTMOST_P (w))
2486 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2487 /* Make sure the vertical border can get her own glyph to the
2488 right of the one we build here. */
2489 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2490 else
2491 width = WINDOW_PIXEL_WIDTH (w) - gx;
2492 else
2493 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2495 goto row_glyph;
2497 case ON_VERTICAL_BORDER:
2498 gx = WINDOW_PIXEL_WIDTH (w) - width;
2499 goto row_glyph;
2501 case ON_SCROLL_BAR:
2502 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2504 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2505 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2506 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2507 : 0)));
2508 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2510 row_glyph:
2511 gr = 0, gy = 0;
2512 for (; r <= end_row && r->enabled_p; ++r)
2513 if (r->y + r->height > y)
2515 gr = r; gy = r->y;
2516 break;
2519 if (gr && gy <= y)
2520 height = gr->height;
2521 else
2523 /* Use nominal line height at end of window. */
2524 y -= gy;
2525 gy += (y / height) * height;
2527 break;
2529 case ON_RIGHT_DIVIDER:
2530 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2531 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2532 gy = 0;
2533 /* The bottom divider prevails. */
2534 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2535 goto add_edge;;
2537 case ON_BOTTOM_DIVIDER:
2538 gx = 0;
2539 width = WINDOW_PIXEL_WIDTH (w);
2540 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2541 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2542 goto add_edge;
2544 default:
2546 virtual_glyph:
2547 /* If there is no glyph under the mouse, then we divide the screen
2548 into a grid of the smallest glyph in the frame, and use that
2549 as our "glyph". */
2551 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2552 round down even for negative values. */
2553 if (gx < 0)
2554 gx -= width - 1;
2555 if (gy < 0)
2556 gy -= height - 1;
2558 gx = (gx / width) * width;
2559 gy = (gy / height) * height;
2561 goto store_rect;
2564 add_edge:
2565 gx += WINDOW_LEFT_EDGE_X (w);
2566 gy += WINDOW_TOP_EDGE_Y (w);
2568 store_rect:
2569 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2571 /* Visible feedback for debugging. */
2572 #if 0
2573 #if HAVE_X_WINDOWS
2574 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2575 f->output_data.x->normal_gc,
2576 gx, gy, width, height);
2577 #endif
2578 #endif
2582 #endif /* HAVE_WINDOW_SYSTEM */
2584 static void
2585 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2587 eassert (w);
2588 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2589 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2590 w->window_end_vpos
2591 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2594 /***********************************************************************
2595 Lisp form evaluation
2596 ***********************************************************************/
2598 /* Error handler for safe_eval and safe_call. */
2600 static Lisp_Object
2601 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2603 add_to_log ("Error during redisplay: %S signaled %S",
2604 Flist (nargs, args), arg);
2605 return Qnil;
2608 /* Call function FUNC with the rest of NARGS - 1 arguments
2609 following. Return the result, or nil if something went
2610 wrong. Prevent redisplay during the evaluation. */
2612 static Lisp_Object
2613 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2615 Lisp_Object val;
2617 if (inhibit_eval_during_redisplay)
2618 val = Qnil;
2619 else
2621 ptrdiff_t i;
2622 ptrdiff_t count = SPECPDL_INDEX ();
2623 struct gcpro gcpro1;
2624 Lisp_Object *args = alloca (nargs * word_size);
2626 args[0] = func;
2627 for (i = 1; i < nargs; i++)
2628 args[i] = va_arg (ap, Lisp_Object);
2630 GCPRO1 (args[0]);
2631 gcpro1.nvars = nargs;
2632 specbind (Qinhibit_redisplay, Qt);
2633 if (inhibit_quit)
2634 specbind (Qinhibit_quit, Qt);
2635 /* Use Qt to ensure debugger does not run,
2636 so there is no possibility of wanting to redisplay. */
2637 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2638 safe_eval_handler);
2639 UNGCPRO;
2640 val = unbind_to (count, val);
2643 return val;
2646 Lisp_Object
2647 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2649 Lisp_Object retval;
2650 va_list ap;
2652 va_start (ap, func);
2653 retval = safe__call (false, nargs, func, ap);
2654 va_end (ap);
2655 return retval;
2658 /* Call function FN with one argument ARG.
2659 Return the result, or nil if something went wrong. */
2661 Lisp_Object
2662 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2664 return safe_call (2, fn, arg);
2667 static Lisp_Object
2668 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2670 Lisp_Object retval;
2671 va_list ap;
2673 va_start (ap, fn);
2674 retval = safe__call (inhibit_quit, 2, fn, ap);
2675 va_end (ap);
2676 return retval;
2679 static Lisp_Object Qeval;
2681 Lisp_Object
2682 safe_eval (Lisp_Object sexpr)
2684 return safe__call1 (false, Qeval, sexpr);
2687 static Lisp_Object
2688 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2690 return safe__call1 (inhibit_quit, Qeval, sexpr);
2693 /* Call function FN with two arguments ARG1 and ARG2.
2694 Return the result, or nil if something went wrong. */
2696 Lisp_Object
2697 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2699 return safe_call (3, fn, arg1, arg2);
2704 /***********************************************************************
2705 Debugging
2706 ***********************************************************************/
2708 #if 0
2710 /* Define CHECK_IT to perform sanity checks on iterators.
2711 This is for debugging. It is too slow to do unconditionally. */
2713 static void
2714 check_it (struct it *it)
2716 if (it->method == GET_FROM_STRING)
2718 eassert (STRINGP (it->string));
2719 eassert (IT_STRING_CHARPOS (*it) >= 0);
2721 else
2723 eassert (IT_STRING_CHARPOS (*it) < 0);
2724 if (it->method == GET_FROM_BUFFER)
2726 /* Check that character and byte positions agree. */
2727 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2731 if (it->dpvec)
2732 eassert (it->current.dpvec_index >= 0);
2733 else
2734 eassert (it->current.dpvec_index < 0);
2737 #define CHECK_IT(IT) check_it ((IT))
2739 #else /* not 0 */
2741 #define CHECK_IT(IT) (void) 0
2743 #endif /* not 0 */
2746 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2748 /* Check that the window end of window W is what we expect it
2749 to be---the last row in the current matrix displaying text. */
2751 static void
2752 check_window_end (struct window *w)
2754 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2756 struct glyph_row *row;
2757 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2758 !row->enabled_p
2759 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2760 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2764 #define CHECK_WINDOW_END(W) check_window_end ((W))
2766 #else
2768 #define CHECK_WINDOW_END(W) (void) 0
2770 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2772 /***********************************************************************
2773 Iterator initialization
2774 ***********************************************************************/
2776 /* Initialize IT for displaying current_buffer in window W, starting
2777 at character position CHARPOS. CHARPOS < 0 means that no buffer
2778 position is specified which is useful when the iterator is assigned
2779 a position later. BYTEPOS is the byte position corresponding to
2780 CHARPOS.
2782 If ROW is not null, calls to produce_glyphs with IT as parameter
2783 will produce glyphs in that row.
2785 BASE_FACE_ID is the id of a base face to use. It must be one of
2786 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2787 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2788 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2790 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2791 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2792 will be initialized to use the corresponding mode line glyph row of
2793 the desired matrix of W. */
2795 void
2796 init_iterator (struct it *it, struct window *w,
2797 ptrdiff_t charpos, ptrdiff_t bytepos,
2798 struct glyph_row *row, enum face_id base_face_id)
2800 enum face_id remapped_base_face_id = base_face_id;
2802 /* Some precondition checks. */
2803 eassert (w != NULL && it != NULL);
2804 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2805 && charpos <= ZV));
2807 /* If face attributes have been changed since the last redisplay,
2808 free realized faces now because they depend on face definitions
2809 that might have changed. Don't free faces while there might be
2810 desired matrices pending which reference these faces. */
2811 if (face_change_count && !inhibit_free_realized_faces)
2813 face_change_count = 0;
2814 free_all_realized_faces (Qnil);
2817 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2818 if (! NILP (Vface_remapping_alist))
2819 remapped_base_face_id
2820 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2822 /* Use one of the mode line rows of W's desired matrix if
2823 appropriate. */
2824 if (row == NULL)
2826 if (base_face_id == MODE_LINE_FACE_ID
2827 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2828 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2829 else if (base_face_id == HEADER_LINE_FACE_ID)
2830 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2833 /* Clear IT. */
2834 memset (it, 0, sizeof *it);
2835 it->current.overlay_string_index = -1;
2836 it->current.dpvec_index = -1;
2837 it->base_face_id = remapped_base_face_id;
2838 it->string = Qnil;
2839 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2840 it->paragraph_embedding = L2R;
2841 it->bidi_it.string.lstring = Qnil;
2842 it->bidi_it.string.s = NULL;
2843 it->bidi_it.string.bufpos = 0;
2844 it->bidi_it.w = w;
2846 /* The window in which we iterate over current_buffer: */
2847 XSETWINDOW (it->window, w);
2848 it->w = w;
2849 it->f = XFRAME (w->frame);
2851 it->cmp_it.id = -1;
2853 /* Extra space between lines (on window systems only). */
2854 if (base_face_id == DEFAULT_FACE_ID
2855 && FRAME_WINDOW_P (it->f))
2857 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2858 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2859 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2860 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2861 * FRAME_LINE_HEIGHT (it->f));
2862 else if (it->f->extra_line_spacing > 0)
2863 it->extra_line_spacing = it->f->extra_line_spacing;
2864 it->max_extra_line_spacing = 0;
2867 /* If realized faces have been removed, e.g. because of face
2868 attribute changes of named faces, recompute them. When running
2869 in batch mode, the face cache of the initial frame is null. If
2870 we happen to get called, make a dummy face cache. */
2871 if (FRAME_FACE_CACHE (it->f) == NULL)
2872 init_frame_faces (it->f);
2873 if (FRAME_FACE_CACHE (it->f)->used == 0)
2874 recompute_basic_faces (it->f);
2876 /* Current value of the `slice', `space-width', and 'height' properties. */
2877 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2878 it->space_width = Qnil;
2879 it->font_height = Qnil;
2880 it->override_ascent = -1;
2882 /* Are control characters displayed as `^C'? */
2883 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2885 /* -1 means everything between a CR and the following line end
2886 is invisible. >0 means lines indented more than this value are
2887 invisible. */
2888 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2889 ? (clip_to_bounds
2890 (-1, XINT (BVAR (current_buffer, selective_display)),
2891 PTRDIFF_MAX))
2892 : (!NILP (BVAR (current_buffer, selective_display))
2893 ? -1 : 0));
2894 it->selective_display_ellipsis_p
2895 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2897 /* Display table to use. */
2898 it->dp = window_display_table (w);
2900 /* Are multibyte characters enabled in current_buffer? */
2901 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2903 /* Get the position at which the redisplay_end_trigger hook should
2904 be run, if it is to be run at all. */
2905 if (MARKERP (w->redisplay_end_trigger)
2906 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2907 it->redisplay_end_trigger_charpos
2908 = marker_position (w->redisplay_end_trigger);
2909 else if (INTEGERP (w->redisplay_end_trigger))
2910 it->redisplay_end_trigger_charpos
2911 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2912 PTRDIFF_MAX);
2914 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2916 /* Are lines in the display truncated? */
2917 if (base_face_id != DEFAULT_FACE_ID
2918 || it->w->hscroll
2919 || (! WINDOW_FULL_WIDTH_P (it->w)
2920 && ((!NILP (Vtruncate_partial_width_windows)
2921 && !INTEGERP (Vtruncate_partial_width_windows))
2922 || (INTEGERP (Vtruncate_partial_width_windows)
2923 /* PXW: Shall we do something about this? */
2924 && (WINDOW_TOTAL_COLS (it->w)
2925 < XINT (Vtruncate_partial_width_windows))))))
2926 it->line_wrap = TRUNCATE;
2927 else if (NILP (BVAR (current_buffer, truncate_lines)))
2928 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2929 ? WINDOW_WRAP : WORD_WRAP;
2930 else
2931 it->line_wrap = TRUNCATE;
2933 /* Get dimensions of truncation and continuation glyphs. These are
2934 displayed as fringe bitmaps under X, but we need them for such
2935 frames when the fringes are turned off. But leave the dimensions
2936 zero for tooltip frames, as these glyphs look ugly there and also
2937 sabotage calculations of tooltip dimensions in x-show-tip. */
2938 #ifdef HAVE_WINDOW_SYSTEM
2939 if (!(FRAME_WINDOW_P (it->f)
2940 && FRAMEP (tip_frame)
2941 && it->f == XFRAME (tip_frame)))
2942 #endif
2944 if (it->line_wrap == TRUNCATE)
2946 /* We will need the truncation glyph. */
2947 eassert (it->glyph_row == NULL);
2948 produce_special_glyphs (it, IT_TRUNCATION);
2949 it->truncation_pixel_width = it->pixel_width;
2951 else
2953 /* We will need the continuation glyph. */
2954 eassert (it->glyph_row == NULL);
2955 produce_special_glyphs (it, IT_CONTINUATION);
2956 it->continuation_pixel_width = it->pixel_width;
2960 /* Reset these values to zero because the produce_special_glyphs
2961 above has changed them. */
2962 it->pixel_width = it->ascent = it->descent = 0;
2963 it->phys_ascent = it->phys_descent = 0;
2965 /* Set this after getting the dimensions of truncation and
2966 continuation glyphs, so that we don't produce glyphs when calling
2967 produce_special_glyphs, above. */
2968 it->glyph_row = row;
2969 it->area = TEXT_AREA;
2971 /* Forget any previous info about this row being reversed. */
2972 if (it->glyph_row)
2973 it->glyph_row->reversed_p = 0;
2975 /* Get the dimensions of the display area. The display area
2976 consists of the visible window area plus a horizontally scrolled
2977 part to the left of the window. All x-values are relative to the
2978 start of this total display area. */
2979 if (base_face_id != DEFAULT_FACE_ID)
2981 /* Mode lines, menu bar in terminal frames. */
2982 it->first_visible_x = 0;
2983 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2985 else
2987 it->first_visible_x
2988 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2989 it->last_visible_x = (it->first_visible_x
2990 + window_box_width (w, TEXT_AREA));
2992 /* If we truncate lines, leave room for the truncation glyph(s) at
2993 the right margin. Otherwise, leave room for the continuation
2994 glyph(s). Done only if the window has no fringes. Since we
2995 don't know at this point whether there will be any R2L lines in
2996 the window, we reserve space for truncation/continuation glyphs
2997 even if only one of the fringes is absent. */
2998 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2999 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
3001 if (it->line_wrap == TRUNCATE)
3002 it->last_visible_x -= it->truncation_pixel_width;
3003 else
3004 it->last_visible_x -= it->continuation_pixel_width;
3007 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
3008 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
3011 /* Leave room for a border glyph. */
3012 if (!FRAME_WINDOW_P (it->f)
3013 && !WINDOW_RIGHTMOST_P (it->w))
3014 it->last_visible_x -= 1;
3016 it->last_visible_y = window_text_bottom_y (w);
3018 /* For mode lines and alike, arrange for the first glyph having a
3019 left box line if the face specifies a box. */
3020 if (base_face_id != DEFAULT_FACE_ID)
3022 struct face *face;
3024 it->face_id = remapped_base_face_id;
3026 /* If we have a boxed mode line, make the first character appear
3027 with a left box line. */
3028 face = FACE_FROM_ID (it->f, remapped_base_face_id);
3029 if (face && face->box != FACE_NO_BOX)
3030 it->start_of_box_run_p = true;
3033 /* If a buffer position was specified, set the iterator there,
3034 getting overlays and face properties from that position. */
3035 if (charpos >= BUF_BEG (current_buffer))
3037 it->stop_charpos = charpos;
3038 it->end_charpos = ZV;
3039 eassert (charpos == BYTE_TO_CHAR (bytepos));
3040 IT_CHARPOS (*it) = charpos;
3041 IT_BYTEPOS (*it) = bytepos;
3043 /* We will rely on `reseat' to set this up properly, via
3044 handle_face_prop. */
3045 it->face_id = it->base_face_id;
3047 it->start = it->current;
3048 /* Do we need to reorder bidirectional text? Not if this is a
3049 unibyte buffer: by definition, none of the single-byte
3050 characters are strong R2L, so no reordering is needed. And
3051 bidi.c doesn't support unibyte buffers anyway. Also, don't
3052 reorder while we are loading loadup.el, since the tables of
3053 character properties needed for reordering are not yet
3054 available. */
3055 it->bidi_p =
3056 NILP (Vpurify_flag)
3057 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3058 && it->multibyte_p;
3060 /* If we are to reorder bidirectional text, init the bidi
3061 iterator. */
3062 if (it->bidi_p)
3064 /* Note the paragraph direction that this buffer wants to
3065 use. */
3066 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3067 Qleft_to_right))
3068 it->paragraph_embedding = L2R;
3069 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3070 Qright_to_left))
3071 it->paragraph_embedding = R2L;
3072 else
3073 it->paragraph_embedding = NEUTRAL_DIR;
3074 bidi_unshelve_cache (NULL, 0);
3075 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3076 &it->bidi_it);
3079 /* Compute faces etc. */
3080 reseat (it, it->current.pos, 1);
3083 CHECK_IT (it);
3087 /* Initialize IT for the display of window W with window start POS. */
3089 void
3090 start_display (struct it *it, struct window *w, struct text_pos pos)
3092 struct glyph_row *row;
3093 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3095 row = w->desired_matrix->rows + first_vpos;
3096 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3097 it->first_vpos = first_vpos;
3099 /* Don't reseat to previous visible line start if current start
3100 position is in a string or image. */
3101 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3103 int start_at_line_beg_p;
3104 int first_y = it->current_y;
3106 /* If window start is not at a line start, skip forward to POS to
3107 get the correct continuation lines width. */
3108 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3109 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3110 if (!start_at_line_beg_p)
3112 int new_x;
3114 reseat_at_previous_visible_line_start (it);
3115 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3117 new_x = it->current_x + it->pixel_width;
3119 /* If lines are continued, this line may end in the middle
3120 of a multi-glyph character (e.g. a control character
3121 displayed as \003, or in the middle of an overlay
3122 string). In this case move_it_to above will not have
3123 taken us to the start of the continuation line but to the
3124 end of the continued line. */
3125 if (it->current_x > 0
3126 && it->line_wrap != TRUNCATE /* Lines are continued. */
3127 && (/* And glyph doesn't fit on the line. */
3128 new_x > it->last_visible_x
3129 /* Or it fits exactly and we're on a window
3130 system frame. */
3131 || (new_x == it->last_visible_x
3132 && FRAME_WINDOW_P (it->f)
3133 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3134 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3135 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3137 if ((it->current.dpvec_index >= 0
3138 || it->current.overlay_string_index >= 0)
3139 /* If we are on a newline from a display vector or
3140 overlay string, then we are already at the end of
3141 a screen line; no need to go to the next line in
3142 that case, as this line is not really continued.
3143 (If we do go to the next line, C-e will not DTRT.) */
3144 && it->c != '\n')
3146 set_iterator_to_next (it, 1);
3147 move_it_in_display_line_to (it, -1, -1, 0);
3150 it->continuation_lines_width += it->current_x;
3152 /* If the character at POS is displayed via a display
3153 vector, move_it_to above stops at the final glyph of
3154 IT->dpvec. To make the caller redisplay that character
3155 again (a.k.a. start at POS), we need to reset the
3156 dpvec_index to the beginning of IT->dpvec. */
3157 else if (it->current.dpvec_index >= 0)
3158 it->current.dpvec_index = 0;
3160 /* We're starting a new display line, not affected by the
3161 height of the continued line, so clear the appropriate
3162 fields in the iterator structure. */
3163 it->max_ascent = it->max_descent = 0;
3164 it->max_phys_ascent = it->max_phys_descent = 0;
3166 it->current_y = first_y;
3167 it->vpos = 0;
3168 it->current_x = it->hpos = 0;
3174 /* Return 1 if POS is a position in ellipses displayed for invisible
3175 text. W is the window we display, for text property lookup. */
3177 static int
3178 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3180 Lisp_Object prop, window;
3181 int ellipses_p = 0;
3182 ptrdiff_t charpos = CHARPOS (pos->pos);
3184 /* If POS specifies a position in a display vector, this might
3185 be for an ellipsis displayed for invisible text. We won't
3186 get the iterator set up for delivering that ellipsis unless
3187 we make sure that it gets aware of the invisible text. */
3188 if (pos->dpvec_index >= 0
3189 && pos->overlay_string_index < 0
3190 && CHARPOS (pos->string_pos) < 0
3191 && charpos > BEGV
3192 && (XSETWINDOW (window, w),
3193 prop = Fget_char_property (make_number (charpos),
3194 Qinvisible, window),
3195 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3197 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3198 window);
3199 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3202 return ellipses_p;
3206 /* Initialize IT for stepping through current_buffer in window W,
3207 starting at position POS that includes overlay string and display
3208 vector/ control character translation position information. Value
3209 is zero if there are overlay strings with newlines at POS. */
3211 static int
3212 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3214 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3215 int i, overlay_strings_with_newlines = 0;
3217 /* If POS specifies a position in a display vector, this might
3218 be for an ellipsis displayed for invisible text. We won't
3219 get the iterator set up for delivering that ellipsis unless
3220 we make sure that it gets aware of the invisible text. */
3221 if (in_ellipses_for_invisible_text_p (pos, w))
3223 --charpos;
3224 bytepos = 0;
3227 /* Keep in mind: the call to reseat in init_iterator skips invisible
3228 text, so we might end up at a position different from POS. This
3229 is only a problem when POS is a row start after a newline and an
3230 overlay starts there with an after-string, and the overlay has an
3231 invisible property. Since we don't skip invisible text in
3232 display_line and elsewhere immediately after consuming the
3233 newline before the row start, such a POS will not be in a string,
3234 but the call to init_iterator below will move us to the
3235 after-string. */
3236 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3238 /* This only scans the current chunk -- it should scan all chunks.
3239 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3240 to 16 in 22.1 to make this a lesser problem. */
3241 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3243 const char *s = SSDATA (it->overlay_strings[i]);
3244 const char *e = s + SBYTES (it->overlay_strings[i]);
3246 while (s < e && *s != '\n')
3247 ++s;
3249 if (s < e)
3251 overlay_strings_with_newlines = 1;
3252 break;
3256 /* If position is within an overlay string, set up IT to the right
3257 overlay string. */
3258 if (pos->overlay_string_index >= 0)
3260 int relative_index;
3262 /* If the first overlay string happens to have a `display'
3263 property for an image, the iterator will be set up for that
3264 image, and we have to undo that setup first before we can
3265 correct the overlay string index. */
3266 if (it->method == GET_FROM_IMAGE)
3267 pop_it (it);
3269 /* We already have the first chunk of overlay strings in
3270 IT->overlay_strings. Load more until the one for
3271 pos->overlay_string_index is in IT->overlay_strings. */
3272 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3274 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3275 it->current.overlay_string_index = 0;
3276 while (n--)
3278 load_overlay_strings (it, 0);
3279 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3283 it->current.overlay_string_index = pos->overlay_string_index;
3284 relative_index = (it->current.overlay_string_index
3285 % OVERLAY_STRING_CHUNK_SIZE);
3286 it->string = it->overlay_strings[relative_index];
3287 eassert (STRINGP (it->string));
3288 it->current.string_pos = pos->string_pos;
3289 it->method = GET_FROM_STRING;
3290 it->end_charpos = SCHARS (it->string);
3291 /* Set up the bidi iterator for this overlay string. */
3292 if (it->bidi_p)
3294 it->bidi_it.string.lstring = it->string;
3295 it->bidi_it.string.s = NULL;
3296 it->bidi_it.string.schars = SCHARS (it->string);
3297 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3298 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3299 it->bidi_it.string.unibyte = !it->multibyte_p;
3300 it->bidi_it.w = it->w;
3301 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3302 FRAME_WINDOW_P (it->f), &it->bidi_it);
3304 /* Synchronize the state of the bidi iterator with
3305 pos->string_pos. For any string position other than
3306 zero, this will be done automagically when we resume
3307 iteration over the string and get_visually_first_element
3308 is called. But if string_pos is zero, and the string is
3309 to be reordered for display, we need to resync manually,
3310 since it could be that the iteration state recorded in
3311 pos ended at string_pos of 0 moving backwards in string. */
3312 if (CHARPOS (pos->string_pos) == 0)
3314 get_visually_first_element (it);
3315 if (IT_STRING_CHARPOS (*it) != 0)
3316 do {
3317 /* Paranoia. */
3318 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3319 bidi_move_to_visually_next (&it->bidi_it);
3320 } while (it->bidi_it.charpos != 0);
3322 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3323 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3327 if (CHARPOS (pos->string_pos) >= 0)
3329 /* Recorded position is not in an overlay string, but in another
3330 string. This can only be a string from a `display' property.
3331 IT should already be filled with that string. */
3332 it->current.string_pos = pos->string_pos;
3333 eassert (STRINGP (it->string));
3334 if (it->bidi_p)
3335 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3336 FRAME_WINDOW_P (it->f), &it->bidi_it);
3339 /* Restore position in display vector translations, control
3340 character translations or ellipses. */
3341 if (pos->dpvec_index >= 0)
3343 if (it->dpvec == NULL)
3344 get_next_display_element (it);
3345 eassert (it->dpvec && it->current.dpvec_index == 0);
3346 it->current.dpvec_index = pos->dpvec_index;
3349 CHECK_IT (it);
3350 return !overlay_strings_with_newlines;
3354 /* Initialize IT for stepping through current_buffer in window W
3355 starting at ROW->start. */
3357 static void
3358 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3360 init_from_display_pos (it, w, &row->start);
3361 it->start = row->start;
3362 it->continuation_lines_width = row->continuation_lines_width;
3363 CHECK_IT (it);
3367 /* Initialize IT for stepping through current_buffer in window W
3368 starting in the line following ROW, i.e. starting at ROW->end.
3369 Value is zero if there are overlay strings with newlines at ROW's
3370 end position. */
3372 static int
3373 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3375 int success = 0;
3377 if (init_from_display_pos (it, w, &row->end))
3379 if (row->continued_p)
3380 it->continuation_lines_width
3381 = row->continuation_lines_width + row->pixel_width;
3382 CHECK_IT (it);
3383 success = 1;
3386 return success;
3392 /***********************************************************************
3393 Text properties
3394 ***********************************************************************/
3396 /* Called when IT reaches IT->stop_charpos. Handle text property and
3397 overlay changes. Set IT->stop_charpos to the next position where
3398 to stop. */
3400 static void
3401 handle_stop (struct it *it)
3403 enum prop_handled handled;
3404 int handle_overlay_change_p;
3405 struct props *p;
3407 it->dpvec = NULL;
3408 it->current.dpvec_index = -1;
3409 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3410 it->ignore_overlay_strings_at_pos_p = 0;
3411 it->ellipsis_p = 0;
3413 /* Use face of preceding text for ellipsis (if invisible) */
3414 if (it->selective_display_ellipsis_p)
3415 it->saved_face_id = it->face_id;
3417 /* Here's the description of the semantics of, and the logic behind,
3418 the various HANDLED_* statuses:
3420 HANDLED_NORMALLY means the handler did its job, and the loop
3421 should proceed to calling the next handler in order.
3423 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3424 change in the properties and overlays at current position, so the
3425 loop should be restarted, to re-invoke the handlers that were
3426 already called. This happens when fontification-functions were
3427 called by handle_fontified_prop, and actually fontified
3428 something. Another case where HANDLED_RECOMPUTE_PROPS is
3429 returned is when we discover overlay strings that need to be
3430 displayed right away. The loop below will continue for as long
3431 as the status is HANDLED_RECOMPUTE_PROPS.
3433 HANDLED_RETURN means return immediately to the caller, to
3434 continue iteration without calling any further handlers. This is
3435 used when we need to act on some property right away, for example
3436 when we need to display the ellipsis or a replacing display
3437 property, such as display string or image.
3439 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3440 consumed, and the handler switched to the next overlay string.
3441 This signals the loop below to refrain from looking for more
3442 overlays before all the overlay strings of the current overlay
3443 are processed.
3445 Some of the handlers called by the loop push the iterator state
3446 onto the stack (see 'push_it'), and arrange for the iteration to
3447 continue with another object, such as an image, a display string,
3448 or an overlay string. In most such cases, it->stop_charpos is
3449 set to the first character of the string, so that when the
3450 iteration resumes, this function will immediately be called
3451 again, to examine the properties at the beginning of the string.
3453 When a display or overlay string is exhausted, the iterator state
3454 is popped (see 'pop_it'), and iteration continues with the
3455 previous object. Again, in many such cases this function is
3456 called again to find the next position where properties might
3457 change. */
3461 handled = HANDLED_NORMALLY;
3463 /* Call text property handlers. */
3464 for (p = it_props; p->handler; ++p)
3466 handled = p->handler (it);
3468 if (handled == HANDLED_RECOMPUTE_PROPS)
3469 break;
3470 else if (handled == HANDLED_RETURN)
3472 /* We still want to show before and after strings from
3473 overlays even if the actual buffer text is replaced. */
3474 if (!handle_overlay_change_p
3475 || it->sp > 1
3476 /* Don't call get_overlay_strings_1 if we already
3477 have overlay strings loaded, because doing so
3478 will load them again and push the iterator state
3479 onto the stack one more time, which is not
3480 expected by the rest of the code that processes
3481 overlay strings. */
3482 || (it->current.overlay_string_index < 0
3483 ? !get_overlay_strings_1 (it, 0, 0)
3484 : 0))
3486 if (it->ellipsis_p)
3487 setup_for_ellipsis (it, 0);
3488 /* When handling a display spec, we might load an
3489 empty string. In that case, discard it here. We
3490 used to discard it in handle_single_display_spec,
3491 but that causes get_overlay_strings_1, above, to
3492 ignore overlay strings that we must check. */
3493 if (STRINGP (it->string) && !SCHARS (it->string))
3494 pop_it (it);
3495 return;
3497 else if (STRINGP (it->string) && !SCHARS (it->string))
3498 pop_it (it);
3499 else
3501 it->ignore_overlay_strings_at_pos_p = true;
3502 it->string_from_display_prop_p = 0;
3503 it->from_disp_prop_p = 0;
3504 handle_overlay_change_p = 0;
3506 handled = HANDLED_RECOMPUTE_PROPS;
3507 break;
3509 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3510 handle_overlay_change_p = 0;
3513 if (handled != HANDLED_RECOMPUTE_PROPS)
3515 /* Don't check for overlay strings below when set to deliver
3516 characters from a display vector. */
3517 if (it->method == GET_FROM_DISPLAY_VECTOR)
3518 handle_overlay_change_p = 0;
3520 /* Handle overlay changes.
3521 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3522 if it finds overlays. */
3523 if (handle_overlay_change_p)
3524 handled = handle_overlay_change (it);
3527 if (it->ellipsis_p)
3529 setup_for_ellipsis (it, 0);
3530 break;
3533 while (handled == HANDLED_RECOMPUTE_PROPS);
3535 /* Determine where to stop next. */
3536 if (handled == HANDLED_NORMALLY)
3537 compute_stop_pos (it);
3541 /* Compute IT->stop_charpos from text property and overlay change
3542 information for IT's current position. */
3544 static void
3545 compute_stop_pos (struct it *it)
3547 register INTERVAL iv, next_iv;
3548 Lisp_Object object, limit, position;
3549 ptrdiff_t charpos, bytepos;
3551 if (STRINGP (it->string))
3553 /* Strings are usually short, so don't limit the search for
3554 properties. */
3555 it->stop_charpos = it->end_charpos;
3556 object = it->string;
3557 limit = Qnil;
3558 charpos = IT_STRING_CHARPOS (*it);
3559 bytepos = IT_STRING_BYTEPOS (*it);
3561 else
3563 ptrdiff_t pos;
3565 /* If end_charpos is out of range for some reason, such as a
3566 misbehaving display function, rationalize it (Bug#5984). */
3567 if (it->end_charpos > ZV)
3568 it->end_charpos = ZV;
3569 it->stop_charpos = it->end_charpos;
3571 /* If next overlay change is in front of the current stop pos
3572 (which is IT->end_charpos), stop there. Note: value of
3573 next_overlay_change is point-max if no overlay change
3574 follows. */
3575 charpos = IT_CHARPOS (*it);
3576 bytepos = IT_BYTEPOS (*it);
3577 pos = next_overlay_change (charpos);
3578 if (pos < it->stop_charpos)
3579 it->stop_charpos = pos;
3581 /* Set up variables for computing the stop position from text
3582 property changes. */
3583 XSETBUFFER (object, current_buffer);
3584 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3587 /* Get the interval containing IT's position. Value is a null
3588 interval if there isn't such an interval. */
3589 position = make_number (charpos);
3590 iv = validate_interval_range (object, &position, &position, 0);
3591 if (iv)
3593 Lisp_Object values_here[LAST_PROP_IDX];
3594 struct props *p;
3596 /* Get properties here. */
3597 for (p = it_props; p->handler; ++p)
3598 values_here[p->idx] = textget (iv->plist, *p->name);
3600 /* Look for an interval following iv that has different
3601 properties. */
3602 for (next_iv = next_interval (iv);
3603 (next_iv
3604 && (NILP (limit)
3605 || XFASTINT (limit) > next_iv->position));
3606 next_iv = next_interval (next_iv))
3608 for (p = it_props; p->handler; ++p)
3610 Lisp_Object new_value;
3612 new_value = textget (next_iv->plist, *p->name);
3613 if (!EQ (values_here[p->idx], new_value))
3614 break;
3617 if (p->handler)
3618 break;
3621 if (next_iv)
3623 if (INTEGERP (limit)
3624 && next_iv->position >= XFASTINT (limit))
3625 /* No text property change up to limit. */
3626 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3627 else
3628 /* Text properties change in next_iv. */
3629 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3633 if (it->cmp_it.id < 0)
3635 ptrdiff_t stoppos = it->end_charpos;
3637 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3638 stoppos = -1;
3639 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3640 stoppos, it->string);
3643 eassert (STRINGP (it->string)
3644 || (it->stop_charpos >= BEGV
3645 && it->stop_charpos >= IT_CHARPOS (*it)));
3649 /* Return the position of the next overlay change after POS in
3650 current_buffer. Value is point-max if no overlay change
3651 follows. This is like `next-overlay-change' but doesn't use
3652 xmalloc. */
3654 static ptrdiff_t
3655 next_overlay_change (ptrdiff_t pos)
3657 ptrdiff_t i, noverlays;
3658 ptrdiff_t endpos;
3659 Lisp_Object *overlays;
3661 /* Get all overlays at the given position. */
3662 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3664 /* If any of these overlays ends before endpos,
3665 use its ending point instead. */
3666 for (i = 0; i < noverlays; ++i)
3668 Lisp_Object oend;
3669 ptrdiff_t oendpos;
3671 oend = OVERLAY_END (overlays[i]);
3672 oendpos = OVERLAY_POSITION (oend);
3673 endpos = min (endpos, oendpos);
3676 return endpos;
3679 /* How many characters forward to search for a display property or
3680 display string. Searching too far forward makes the bidi display
3681 sluggish, especially in small windows. */
3682 #define MAX_DISP_SCAN 250
3684 /* Return the character position of a display string at or after
3685 position specified by POSITION. If no display string exists at or
3686 after POSITION, return ZV. A display string is either an overlay
3687 with `display' property whose value is a string, or a `display'
3688 text property whose value is a string. STRING is data about the
3689 string to iterate; if STRING->lstring is nil, we are iterating a
3690 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3691 on a GUI frame. DISP_PROP is set to zero if we searched
3692 MAX_DISP_SCAN characters forward without finding any display
3693 strings, non-zero otherwise. It is set to 2 if the display string
3694 uses any kind of `(space ...)' spec that will produce a stretch of
3695 white space in the text area. */
3696 ptrdiff_t
3697 compute_display_string_pos (struct text_pos *position,
3698 struct bidi_string_data *string,
3699 struct window *w,
3700 int frame_window_p, int *disp_prop)
3702 /* OBJECT = nil means current buffer. */
3703 Lisp_Object object, object1;
3704 Lisp_Object pos, spec, limpos;
3705 int string_p = (string && (STRINGP (string->lstring) || string->s));
3706 ptrdiff_t eob = string_p ? string->schars : ZV;
3707 ptrdiff_t begb = string_p ? 0 : BEGV;
3708 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3709 ptrdiff_t lim =
3710 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3711 struct text_pos tpos;
3712 int rv = 0;
3714 if (string && STRINGP (string->lstring))
3715 object1 = object = string->lstring;
3716 else if (w && !string_p)
3718 XSETWINDOW (object, w);
3719 object1 = Qnil;
3721 else
3722 object1 = object = Qnil;
3724 *disp_prop = 1;
3726 if (charpos >= eob
3727 /* We don't support display properties whose values are strings
3728 that have display string properties. */
3729 || string->from_disp_str
3730 /* C strings cannot have display properties. */
3731 || (string->s && !STRINGP (object)))
3733 *disp_prop = 0;
3734 return eob;
3737 /* If the character at CHARPOS is where the display string begins,
3738 return CHARPOS. */
3739 pos = make_number (charpos);
3740 if (STRINGP (object))
3741 bufpos = string->bufpos;
3742 else
3743 bufpos = charpos;
3744 tpos = *position;
3745 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3746 && (charpos <= begb
3747 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3748 object),
3749 spec))
3750 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3751 frame_window_p)))
3753 if (rv == 2)
3754 *disp_prop = 2;
3755 return charpos;
3758 /* Look forward for the first character with a `display' property
3759 that will replace the underlying text when displayed. */
3760 limpos = make_number (lim);
3761 do {
3762 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3763 CHARPOS (tpos) = XFASTINT (pos);
3764 if (CHARPOS (tpos) >= lim)
3766 *disp_prop = 0;
3767 break;
3769 if (STRINGP (object))
3770 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3771 else
3772 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3773 spec = Fget_char_property (pos, Qdisplay, object);
3774 if (!STRINGP (object))
3775 bufpos = CHARPOS (tpos);
3776 } while (NILP (spec)
3777 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3778 bufpos, frame_window_p)));
3779 if (rv == 2)
3780 *disp_prop = 2;
3782 return CHARPOS (tpos);
3785 /* Return the character position of the end of the display string that
3786 started at CHARPOS. If there's no display string at CHARPOS,
3787 return -1. A display string is either an overlay with `display'
3788 property whose value is a string or a `display' text property whose
3789 value is a string. */
3790 ptrdiff_t
3791 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3793 /* OBJECT = nil means current buffer. */
3794 Lisp_Object object =
3795 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3796 Lisp_Object pos = make_number (charpos);
3797 ptrdiff_t eob =
3798 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3800 if (charpos >= eob || (string->s && !STRINGP (object)))
3801 return eob;
3803 /* It could happen that the display property or overlay was removed
3804 since we found it in compute_display_string_pos above. One way
3805 this can happen is if JIT font-lock was called (through
3806 handle_fontified_prop), and jit-lock-functions remove text
3807 properties or overlays from the portion of buffer that includes
3808 CHARPOS. Muse mode is known to do that, for example. In this
3809 case, we return -1 to the caller, to signal that no display
3810 string is actually present at CHARPOS. See bidi_fetch_char for
3811 how this is handled.
3813 An alternative would be to never look for display properties past
3814 it->stop_charpos. But neither compute_display_string_pos nor
3815 bidi_fetch_char that calls it know or care where the next
3816 stop_charpos is. */
3817 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3818 return -1;
3820 /* Look forward for the first character where the `display' property
3821 changes. */
3822 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3824 return XFASTINT (pos);
3829 /***********************************************************************
3830 Fontification
3831 ***********************************************************************/
3833 /* Handle changes in the `fontified' property of the current buffer by
3834 calling hook functions from Qfontification_functions to fontify
3835 regions of text. */
3837 static enum prop_handled
3838 handle_fontified_prop (struct it *it)
3840 Lisp_Object prop, pos;
3841 enum prop_handled handled = HANDLED_NORMALLY;
3843 if (!NILP (Vmemory_full))
3844 return handled;
3846 /* Get the value of the `fontified' property at IT's current buffer
3847 position. (The `fontified' property doesn't have a special
3848 meaning in strings.) If the value is nil, call functions from
3849 Qfontification_functions. */
3850 if (!STRINGP (it->string)
3851 && it->s == NULL
3852 && !NILP (Vfontification_functions)
3853 && !NILP (Vrun_hooks)
3854 && (pos = make_number (IT_CHARPOS (*it)),
3855 prop = Fget_char_property (pos, Qfontified, Qnil),
3856 /* Ignore the special cased nil value always present at EOB since
3857 no amount of fontifying will be able to change it. */
3858 NILP (prop) && IT_CHARPOS (*it) < Z))
3860 ptrdiff_t count = SPECPDL_INDEX ();
3861 Lisp_Object val;
3862 struct buffer *obuf = current_buffer;
3863 ptrdiff_t begv = BEGV, zv = ZV;
3864 bool old_clip_changed = current_buffer->clip_changed;
3866 val = Vfontification_functions;
3867 specbind (Qfontification_functions, Qnil);
3869 eassert (it->end_charpos == ZV);
3871 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3872 safe_call1 (val, pos);
3873 else
3875 Lisp_Object fns, fn;
3876 struct gcpro gcpro1, gcpro2;
3878 fns = Qnil;
3879 GCPRO2 (val, fns);
3881 for (; CONSP (val); val = XCDR (val))
3883 fn = XCAR (val);
3885 if (EQ (fn, Qt))
3887 /* A value of t indicates this hook has a local
3888 binding; it means to run the global binding too.
3889 In a global value, t should not occur. If it
3890 does, we must ignore it to avoid an endless
3891 loop. */
3892 for (fns = Fdefault_value (Qfontification_functions);
3893 CONSP (fns);
3894 fns = XCDR (fns))
3896 fn = XCAR (fns);
3897 if (!EQ (fn, Qt))
3898 safe_call1 (fn, pos);
3901 else
3902 safe_call1 (fn, pos);
3905 UNGCPRO;
3908 unbind_to (count, Qnil);
3910 /* Fontification functions routinely call `save-restriction'.
3911 Normally, this tags clip_changed, which can confuse redisplay
3912 (see discussion in Bug#6671). Since we don't perform any
3913 special handling of fontification changes in the case where
3914 `save-restriction' isn't called, there's no point doing so in
3915 this case either. So, if the buffer's restrictions are
3916 actually left unchanged, reset clip_changed. */
3917 if (obuf == current_buffer)
3919 if (begv == BEGV && zv == ZV)
3920 current_buffer->clip_changed = old_clip_changed;
3922 /* There isn't much we can reasonably do to protect against
3923 misbehaving fontification, but here's a fig leaf. */
3924 else if (BUFFER_LIVE_P (obuf))
3925 set_buffer_internal_1 (obuf);
3927 /* The fontification code may have added/removed text.
3928 It could do even a lot worse, but let's at least protect against
3929 the most obvious case where only the text past `pos' gets changed',
3930 as is/was done in grep.el where some escapes sequences are turned
3931 into face properties (bug#7876). */
3932 it->end_charpos = ZV;
3934 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3935 something. This avoids an endless loop if they failed to
3936 fontify the text for which reason ever. */
3937 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3938 handled = HANDLED_RECOMPUTE_PROPS;
3941 return handled;
3946 /***********************************************************************
3947 Faces
3948 ***********************************************************************/
3950 /* Set up iterator IT from face properties at its current position.
3951 Called from handle_stop. */
3953 static enum prop_handled
3954 handle_face_prop (struct it *it)
3956 int new_face_id;
3957 ptrdiff_t next_stop;
3959 if (!STRINGP (it->string))
3961 new_face_id
3962 = face_at_buffer_position (it->w,
3963 IT_CHARPOS (*it),
3964 &next_stop,
3965 (IT_CHARPOS (*it)
3966 + TEXT_PROP_DISTANCE_LIMIT),
3967 0, it->base_face_id);
3969 /* Is this a start of a run of characters with box face?
3970 Caveat: this can be called for a freshly initialized
3971 iterator; face_id is -1 in this case. We know that the new
3972 face will not change until limit, i.e. if the new face has a
3973 box, all characters up to limit will have one. But, as
3974 usual, we don't know whether limit is really the end. */
3975 if (new_face_id != it->face_id)
3977 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3978 /* If it->face_id is -1, old_face below will be NULL, see
3979 the definition of FACE_FROM_ID. This will happen if this
3980 is the initial call that gets the face. */
3981 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3983 /* If the value of face_id of the iterator is -1, we have to
3984 look in front of IT's position and see whether there is a
3985 face there that's different from new_face_id. */
3986 if (!old_face && IT_CHARPOS (*it) > BEG)
3988 int prev_face_id = face_before_it_pos (it);
3990 old_face = FACE_FROM_ID (it->f, prev_face_id);
3993 /* If the new face has a box, but the old face does not,
3994 this is the start of a run of characters with box face,
3995 i.e. this character has a shadow on the left side. */
3996 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3997 && (old_face == NULL || !old_face->box));
3998 it->face_box_p = new_face->box != FACE_NO_BOX;
4001 else
4003 int base_face_id;
4004 ptrdiff_t bufpos;
4005 int i;
4006 Lisp_Object from_overlay
4007 = (it->current.overlay_string_index >= 0
4008 ? it->string_overlays[it->current.overlay_string_index
4009 % OVERLAY_STRING_CHUNK_SIZE]
4010 : Qnil);
4012 /* See if we got to this string directly or indirectly from
4013 an overlay property. That includes the before-string or
4014 after-string of an overlay, strings in display properties
4015 provided by an overlay, their text properties, etc.
4017 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
4018 if (! NILP (from_overlay))
4019 for (i = it->sp - 1; i >= 0; i--)
4021 if (it->stack[i].current.overlay_string_index >= 0)
4022 from_overlay
4023 = it->string_overlays[it->stack[i].current.overlay_string_index
4024 % OVERLAY_STRING_CHUNK_SIZE];
4025 else if (! NILP (it->stack[i].from_overlay))
4026 from_overlay = it->stack[i].from_overlay;
4028 if (!NILP (from_overlay))
4029 break;
4032 if (! NILP (from_overlay))
4034 bufpos = IT_CHARPOS (*it);
4035 /* For a string from an overlay, the base face depends
4036 only on text properties and ignores overlays. */
4037 base_face_id
4038 = face_for_overlay_string (it->w,
4039 IT_CHARPOS (*it),
4040 &next_stop,
4041 (IT_CHARPOS (*it)
4042 + TEXT_PROP_DISTANCE_LIMIT),
4044 from_overlay);
4046 else
4048 bufpos = 0;
4050 /* For strings from a `display' property, use the face at
4051 IT's current buffer position as the base face to merge
4052 with, so that overlay strings appear in the same face as
4053 surrounding text, unless they specify their own faces.
4054 For strings from wrap-prefix and line-prefix properties,
4055 use the default face, possibly remapped via
4056 Vface_remapping_alist. */
4057 /* Note that the fact that we use the face at _buffer_
4058 position means that a 'display' property on an overlay
4059 string will not inherit the face of that overlay string,
4060 but will instead revert to the face of buffer text
4061 covered by the overlay. This is visible, e.g., when the
4062 overlay specifies a box face, but neither the buffer nor
4063 the display string do. This sounds like a design bug,
4064 but Emacs always did that since v21.1, so changing that
4065 might be a big deal. */
4066 base_face_id = it->string_from_prefix_prop_p
4067 ? (!NILP (Vface_remapping_alist)
4068 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4069 : DEFAULT_FACE_ID)
4070 : underlying_face_id (it);
4073 new_face_id = face_at_string_position (it->w,
4074 it->string,
4075 IT_STRING_CHARPOS (*it),
4076 bufpos,
4077 &next_stop,
4078 base_face_id, 0);
4080 /* Is this a start of a run of characters with box? Caveat:
4081 this can be called for a freshly allocated iterator; face_id
4082 is -1 is this case. We know that the new face will not
4083 change until the next check pos, i.e. if the new face has a
4084 box, all characters up to that position will have a
4085 box. But, as usual, we don't know whether that position
4086 is really the end. */
4087 if (new_face_id != it->face_id)
4089 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4090 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4092 /* If new face has a box but old face hasn't, this is the
4093 start of a run of characters with box, i.e. it has a
4094 shadow on the left side. */
4095 it->start_of_box_run_p
4096 = new_face->box && (old_face == NULL || !old_face->box);
4097 it->face_box_p = new_face->box != FACE_NO_BOX;
4101 it->face_id = new_face_id;
4102 return HANDLED_NORMALLY;
4106 /* Return the ID of the face ``underlying'' IT's current position,
4107 which is in a string. If the iterator is associated with a
4108 buffer, return the face at IT's current buffer position.
4109 Otherwise, use the iterator's base_face_id. */
4111 static int
4112 underlying_face_id (struct it *it)
4114 int face_id = it->base_face_id, i;
4116 eassert (STRINGP (it->string));
4118 for (i = it->sp - 1; i >= 0; --i)
4119 if (NILP (it->stack[i].string))
4120 face_id = it->stack[i].face_id;
4122 return face_id;
4126 /* Compute the face one character before or after the current position
4127 of IT, in the visual order. BEFORE_P non-zero means get the face
4128 in front (to the left in L2R paragraphs, to the right in R2L
4129 paragraphs) of IT's screen position. Value is the ID of the face. */
4131 static int
4132 face_before_or_after_it_pos (struct it *it, int before_p)
4134 int face_id, limit;
4135 ptrdiff_t next_check_charpos;
4136 struct it it_copy;
4137 void *it_copy_data = NULL;
4139 eassert (it->s == NULL);
4141 if (STRINGP (it->string))
4143 ptrdiff_t bufpos, charpos;
4144 int base_face_id;
4146 /* No face change past the end of the string (for the case
4147 we are padding with spaces). No face change before the
4148 string start. */
4149 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4150 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4151 return it->face_id;
4153 if (!it->bidi_p)
4155 /* Set charpos to the position before or after IT's current
4156 position, in the logical order, which in the non-bidi
4157 case is the same as the visual order. */
4158 if (before_p)
4159 charpos = IT_STRING_CHARPOS (*it) - 1;
4160 else if (it->what == IT_COMPOSITION)
4161 /* For composition, we must check the character after the
4162 composition. */
4163 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4164 else
4165 charpos = IT_STRING_CHARPOS (*it) + 1;
4167 else
4169 if (before_p)
4171 /* With bidi iteration, the character before the current
4172 in the visual order cannot be found by simple
4173 iteration, because "reverse" reordering is not
4174 supported. Instead, we need to use the move_it_*
4175 family of functions. */
4176 /* Ignore face changes before the first visible
4177 character on this display line. */
4178 if (it->current_x <= it->first_visible_x)
4179 return it->face_id;
4180 SAVE_IT (it_copy, *it, it_copy_data);
4181 /* Implementation note: Since move_it_in_display_line
4182 works in the iterator geometry, and thinks the first
4183 character is always the leftmost, even in R2L lines,
4184 we don't need to distinguish between the R2L and L2R
4185 cases here. */
4186 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4187 it_copy.current_x - 1, MOVE_TO_X);
4188 charpos = IT_STRING_CHARPOS (it_copy);
4189 RESTORE_IT (it, it, it_copy_data);
4191 else
4193 /* Set charpos to the string position of the character
4194 that comes after IT's current position in the visual
4195 order. */
4196 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4198 it_copy = *it;
4199 while (n--)
4200 bidi_move_to_visually_next (&it_copy.bidi_it);
4202 charpos = it_copy.bidi_it.charpos;
4205 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4207 if (it->current.overlay_string_index >= 0)
4208 bufpos = IT_CHARPOS (*it);
4209 else
4210 bufpos = 0;
4212 base_face_id = underlying_face_id (it);
4214 /* Get the face for ASCII, or unibyte. */
4215 face_id = face_at_string_position (it->w,
4216 it->string,
4217 charpos,
4218 bufpos,
4219 &next_check_charpos,
4220 base_face_id, 0);
4222 /* Correct the face for charsets different from ASCII. Do it
4223 for the multibyte case only. The face returned above is
4224 suitable for unibyte text if IT->string is unibyte. */
4225 if (STRING_MULTIBYTE (it->string))
4227 struct text_pos pos1 = string_pos (charpos, it->string);
4228 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4229 int c, len;
4230 struct face *face = FACE_FROM_ID (it->f, face_id);
4232 c = string_char_and_length (p, &len);
4233 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4236 else
4238 struct text_pos pos;
4240 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4241 || (IT_CHARPOS (*it) <= BEGV && before_p))
4242 return it->face_id;
4244 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4245 pos = it->current.pos;
4247 if (!it->bidi_p)
4249 if (before_p)
4250 DEC_TEXT_POS (pos, it->multibyte_p);
4251 else
4253 if (it->what == IT_COMPOSITION)
4255 /* For composition, we must check the position after
4256 the composition. */
4257 pos.charpos += it->cmp_it.nchars;
4258 pos.bytepos += it->len;
4260 else
4261 INC_TEXT_POS (pos, it->multibyte_p);
4264 else
4266 if (before_p)
4268 /* With bidi iteration, the character before the current
4269 in the visual order cannot be found by simple
4270 iteration, because "reverse" reordering is not
4271 supported. Instead, we need to use the move_it_*
4272 family of functions. */
4273 /* Ignore face changes before the first visible
4274 character on this display line. */
4275 if (it->current_x <= it->first_visible_x)
4276 return it->face_id;
4277 SAVE_IT (it_copy, *it, it_copy_data);
4278 /* Implementation note: Since move_it_in_display_line
4279 works in the iterator geometry, and thinks the first
4280 character is always the leftmost, even in R2L lines,
4281 we don't need to distinguish between the R2L and L2R
4282 cases here. */
4283 move_it_in_display_line (&it_copy, ZV,
4284 it_copy.current_x - 1, MOVE_TO_X);
4285 pos = it_copy.current.pos;
4286 RESTORE_IT (it, it, it_copy_data);
4288 else
4290 /* Set charpos to the buffer position of the character
4291 that comes after IT's current position in the visual
4292 order. */
4293 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4295 it_copy = *it;
4296 while (n--)
4297 bidi_move_to_visually_next (&it_copy.bidi_it);
4299 SET_TEXT_POS (pos,
4300 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4303 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4305 /* Determine face for CHARSET_ASCII, or unibyte. */
4306 face_id = face_at_buffer_position (it->w,
4307 CHARPOS (pos),
4308 &next_check_charpos,
4309 limit, 0, -1);
4311 /* Correct the face for charsets different from ASCII. Do it
4312 for the multibyte case only. The face returned above is
4313 suitable for unibyte text if current_buffer is unibyte. */
4314 if (it->multibyte_p)
4316 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4317 struct face *face = FACE_FROM_ID (it->f, face_id);
4318 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4322 return face_id;
4327 /***********************************************************************
4328 Invisible text
4329 ***********************************************************************/
4331 /* Set up iterator IT from invisible properties at its current
4332 position. Called from handle_stop. */
4334 static enum prop_handled
4335 handle_invisible_prop (struct it *it)
4337 enum prop_handled handled = HANDLED_NORMALLY;
4338 int invis_p;
4339 Lisp_Object prop;
4341 if (STRINGP (it->string))
4343 Lisp_Object end_charpos, limit, charpos;
4345 /* Get the value of the invisible text property at the
4346 current position. Value will be nil if there is no such
4347 property. */
4348 charpos = make_number (IT_STRING_CHARPOS (*it));
4349 prop = Fget_text_property (charpos, Qinvisible, it->string);
4350 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4352 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4354 /* Record whether we have to display an ellipsis for the
4355 invisible text. */
4356 int display_ellipsis_p = (invis_p == 2);
4357 ptrdiff_t len, endpos;
4359 handled = HANDLED_RECOMPUTE_PROPS;
4361 /* Get the position at which the next visible text can be
4362 found in IT->string, if any. */
4363 endpos = len = SCHARS (it->string);
4364 XSETINT (limit, len);
4367 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4368 it->string, limit);
4369 if (INTEGERP (end_charpos))
4371 endpos = XFASTINT (end_charpos);
4372 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4373 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4374 if (invis_p == 2)
4375 display_ellipsis_p = true;
4378 while (invis_p && endpos < len);
4380 if (display_ellipsis_p)
4381 it->ellipsis_p = true;
4383 if (endpos < len)
4385 /* Text at END_CHARPOS is visible. Move IT there. */
4386 struct text_pos old;
4387 ptrdiff_t oldpos;
4389 old = it->current.string_pos;
4390 oldpos = CHARPOS (old);
4391 if (it->bidi_p)
4393 if (it->bidi_it.first_elt
4394 && it->bidi_it.charpos < SCHARS (it->string))
4395 bidi_paragraph_init (it->paragraph_embedding,
4396 &it->bidi_it, 1);
4397 /* Bidi-iterate out of the invisible text. */
4400 bidi_move_to_visually_next (&it->bidi_it);
4402 while (oldpos <= it->bidi_it.charpos
4403 && it->bidi_it.charpos < endpos);
4405 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4406 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4407 if (IT_CHARPOS (*it) >= endpos)
4408 it->prev_stop = endpos;
4410 else
4412 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4413 compute_string_pos (&it->current.string_pos, old, it->string);
4416 else
4418 /* The rest of the string is invisible. If this is an
4419 overlay string, proceed with the next overlay string
4420 or whatever comes and return a character from there. */
4421 if (it->current.overlay_string_index >= 0
4422 && !display_ellipsis_p)
4424 next_overlay_string (it);
4425 /* Don't check for overlay strings when we just
4426 finished processing them. */
4427 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4429 else
4431 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4432 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4437 else
4439 ptrdiff_t newpos, next_stop, start_charpos, tem;
4440 Lisp_Object pos, overlay;
4442 /* First of all, is there invisible text at this position? */
4443 tem = start_charpos = IT_CHARPOS (*it);
4444 pos = make_number (tem);
4445 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4446 &overlay);
4447 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4449 /* If we are on invisible text, skip over it. */
4450 if (invis_p && start_charpos < it->end_charpos)
4452 /* Record whether we have to display an ellipsis for the
4453 invisible text. */
4454 int display_ellipsis_p = invis_p == 2;
4456 handled = HANDLED_RECOMPUTE_PROPS;
4458 /* Loop skipping over invisible text. The loop is left at
4459 ZV or with IT on the first char being visible again. */
4462 /* Try to skip some invisible text. Return value is the
4463 position reached which can be equal to where we start
4464 if there is nothing invisible there. This skips both
4465 over invisible text properties and overlays with
4466 invisible property. */
4467 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4469 /* If we skipped nothing at all we weren't at invisible
4470 text in the first place. If everything to the end of
4471 the buffer was skipped, end the loop. */
4472 if (newpos == tem || newpos >= ZV)
4473 invis_p = 0;
4474 else
4476 /* We skipped some characters but not necessarily
4477 all there are. Check if we ended up on visible
4478 text. Fget_char_property returns the property of
4479 the char before the given position, i.e. if we
4480 get invis_p = 0, this means that the char at
4481 newpos is visible. */
4482 pos = make_number (newpos);
4483 prop = Fget_char_property (pos, Qinvisible, it->window);
4484 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4487 /* If we ended up on invisible text, proceed to
4488 skip starting with next_stop. */
4489 if (invis_p)
4490 tem = next_stop;
4492 /* If there are adjacent invisible texts, don't lose the
4493 second one's ellipsis. */
4494 if (invis_p == 2)
4495 display_ellipsis_p = true;
4497 while (invis_p);
4499 /* The position newpos is now either ZV or on visible text. */
4500 if (it->bidi_p)
4502 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4503 int on_newline
4504 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4505 int after_newline
4506 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4508 /* If the invisible text ends on a newline or on a
4509 character after a newline, we can avoid the costly,
4510 character by character, bidi iteration to NEWPOS, and
4511 instead simply reseat the iterator there. That's
4512 because all bidi reordering information is tossed at
4513 the newline. This is a big win for modes that hide
4514 complete lines, like Outline, Org, etc. */
4515 if (on_newline || after_newline)
4517 struct text_pos tpos;
4518 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4520 SET_TEXT_POS (tpos, newpos, bpos);
4521 reseat_1 (it, tpos, 0);
4522 /* If we reseat on a newline/ZV, we need to prep the
4523 bidi iterator for advancing to the next character
4524 after the newline/EOB, keeping the current paragraph
4525 direction (so that PRODUCE_GLYPHS does TRT wrt
4526 prepending/appending glyphs to a glyph row). */
4527 if (on_newline)
4529 it->bidi_it.first_elt = 0;
4530 it->bidi_it.paragraph_dir = pdir;
4531 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4532 it->bidi_it.nchars = 1;
4533 it->bidi_it.ch_len = 1;
4536 else /* Must use the slow method. */
4538 /* With bidi iteration, the region of invisible text
4539 could start and/or end in the middle of a
4540 non-base embedding level. Therefore, we need to
4541 skip invisible text using the bidi iterator,
4542 starting at IT's current position, until we find
4543 ourselves outside of the invisible text.
4544 Skipping invisible text _after_ bidi iteration
4545 avoids affecting the visual order of the
4546 displayed text when invisible properties are
4547 added or removed. */
4548 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4550 /* If we were `reseat'ed to a new paragraph,
4551 determine the paragraph base direction. We
4552 need to do it now because
4553 next_element_from_buffer may not have a
4554 chance to do it, if we are going to skip any
4555 text at the beginning, which resets the
4556 FIRST_ELT flag. */
4557 bidi_paragraph_init (it->paragraph_embedding,
4558 &it->bidi_it, 1);
4562 bidi_move_to_visually_next (&it->bidi_it);
4564 while (it->stop_charpos <= it->bidi_it.charpos
4565 && it->bidi_it.charpos < newpos);
4566 IT_CHARPOS (*it) = it->bidi_it.charpos;
4567 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4568 /* If we overstepped NEWPOS, record its position in
4569 the iterator, so that we skip invisible text if
4570 later the bidi iteration lands us in the
4571 invisible region again. */
4572 if (IT_CHARPOS (*it) >= newpos)
4573 it->prev_stop = newpos;
4576 else
4578 IT_CHARPOS (*it) = newpos;
4579 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4582 /* If there are before-strings at the start of invisible
4583 text, and the text is invisible because of a text
4584 property, arrange to show before-strings because 20.x did
4585 it that way. (If the text is invisible because of an
4586 overlay property instead of a text property, this is
4587 already handled in the overlay code.) */
4588 if (NILP (overlay)
4589 && get_overlay_strings (it, it->stop_charpos))
4591 handled = HANDLED_RECOMPUTE_PROPS;
4592 if (it->sp > 0)
4594 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4595 /* The call to get_overlay_strings above recomputes
4596 it->stop_charpos, but it only considers changes
4597 in properties and overlays beyond iterator's
4598 current position. This causes us to miss changes
4599 that happen exactly where the invisible property
4600 ended. So we play it safe here and force the
4601 iterator to check for potential stop positions
4602 immediately after the invisible text. Note that
4603 if get_overlay_strings returns non-zero, it
4604 normally also pushed the iterator stack, so we
4605 need to update the stop position in the slot
4606 below the current one. */
4607 it->stack[it->sp - 1].stop_charpos
4608 = CHARPOS (it->stack[it->sp - 1].current.pos);
4611 else if (display_ellipsis_p)
4613 /* Make sure that the glyphs of the ellipsis will get
4614 correct `charpos' values. If we would not update
4615 it->position here, the glyphs would belong to the
4616 last visible character _before_ the invisible
4617 text, which confuses `set_cursor_from_row'.
4619 We use the last invisible position instead of the
4620 first because this way the cursor is always drawn on
4621 the first "." of the ellipsis, whenever PT is inside
4622 the invisible text. Otherwise the cursor would be
4623 placed _after_ the ellipsis when the point is after the
4624 first invisible character. */
4625 if (!STRINGP (it->object))
4627 it->position.charpos = newpos - 1;
4628 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4630 it->ellipsis_p = true;
4631 /* Let the ellipsis display before
4632 considering any properties of the following char.
4633 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4634 handled = HANDLED_RETURN;
4639 return handled;
4643 /* Make iterator IT return `...' next.
4644 Replaces LEN characters from buffer. */
4646 static void
4647 setup_for_ellipsis (struct it *it, int len)
4649 /* Use the display table definition for `...'. Invalid glyphs
4650 will be handled by the method returning elements from dpvec. */
4651 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4653 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4654 it->dpvec = v->contents;
4655 it->dpend = v->contents + v->header.size;
4657 else
4659 /* Default `...'. */
4660 it->dpvec = default_invis_vector;
4661 it->dpend = default_invis_vector + 3;
4664 it->dpvec_char_len = len;
4665 it->current.dpvec_index = 0;
4666 it->dpvec_face_id = -1;
4668 /* Remember the current face id in case glyphs specify faces.
4669 IT's face is restored in set_iterator_to_next.
4670 saved_face_id was set to preceding char's face in handle_stop. */
4671 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4672 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4674 it->method = GET_FROM_DISPLAY_VECTOR;
4675 it->ellipsis_p = true;
4680 /***********************************************************************
4681 'display' property
4682 ***********************************************************************/
4684 /* Set up iterator IT from `display' property at its current position.
4685 Called from handle_stop.
4686 We return HANDLED_RETURN if some part of the display property
4687 overrides the display of the buffer text itself.
4688 Otherwise we return HANDLED_NORMALLY. */
4690 static enum prop_handled
4691 handle_display_prop (struct it *it)
4693 Lisp_Object propval, object, overlay;
4694 struct text_pos *position;
4695 ptrdiff_t bufpos;
4696 /* Nonzero if some property replaces the display of the text itself. */
4697 int display_replaced_p = 0;
4699 if (STRINGP (it->string))
4701 object = it->string;
4702 position = &it->current.string_pos;
4703 bufpos = CHARPOS (it->current.pos);
4705 else
4707 XSETWINDOW (object, it->w);
4708 position = &it->current.pos;
4709 bufpos = CHARPOS (*position);
4712 /* Reset those iterator values set from display property values. */
4713 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4714 it->space_width = Qnil;
4715 it->font_height = Qnil;
4716 it->voffset = 0;
4718 /* We don't support recursive `display' properties, i.e. string
4719 values that have a string `display' property, that have a string
4720 `display' property etc. */
4721 if (!it->string_from_display_prop_p)
4722 it->area = TEXT_AREA;
4724 propval = get_char_property_and_overlay (make_number (position->charpos),
4725 Qdisplay, object, &overlay);
4726 if (NILP (propval))
4727 return HANDLED_NORMALLY;
4728 /* Now OVERLAY is the overlay that gave us this property, or nil
4729 if it was a text property. */
4731 if (!STRINGP (it->string))
4732 object = it->w->contents;
4734 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4735 position, bufpos,
4736 FRAME_WINDOW_P (it->f));
4738 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4741 /* Subroutine of handle_display_prop. Returns non-zero if the display
4742 specification in SPEC is a replacing specification, i.e. it would
4743 replace the text covered by `display' property with something else,
4744 such as an image or a display string. If SPEC includes any kind or
4745 `(space ...) specification, the value is 2; this is used by
4746 compute_display_string_pos, which see.
4748 See handle_single_display_spec for documentation of arguments.
4749 frame_window_p is non-zero if the window being redisplayed is on a
4750 GUI frame; this argument is used only if IT is NULL, see below.
4752 IT can be NULL, if this is called by the bidi reordering code
4753 through compute_display_string_pos, which see. In that case, this
4754 function only examines SPEC, but does not otherwise "handle" it, in
4755 the sense that it doesn't set up members of IT from the display
4756 spec. */
4757 static int
4758 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4759 Lisp_Object overlay, struct text_pos *position,
4760 ptrdiff_t bufpos, int frame_window_p)
4762 int replacing_p = 0;
4763 int rv;
4765 if (CONSP (spec)
4766 /* Simple specifications. */
4767 && !EQ (XCAR (spec), Qimage)
4768 && !EQ (XCAR (spec), Qspace)
4769 && !EQ (XCAR (spec), Qwhen)
4770 && !EQ (XCAR (spec), Qslice)
4771 && !EQ (XCAR (spec), Qspace_width)
4772 && !EQ (XCAR (spec), Qheight)
4773 && !EQ (XCAR (spec), Qraise)
4774 /* Marginal area specifications. */
4775 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4776 && !EQ (XCAR (spec), Qleft_fringe)
4777 && !EQ (XCAR (spec), Qright_fringe)
4778 && !NILP (XCAR (spec)))
4780 for (; CONSP (spec); spec = XCDR (spec))
4782 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4783 overlay, position, bufpos,
4784 replacing_p, frame_window_p)))
4786 replacing_p = rv;
4787 /* If some text in a string is replaced, `position' no
4788 longer points to the position of `object'. */
4789 if (!it || STRINGP (object))
4790 break;
4794 else if (VECTORP (spec))
4796 ptrdiff_t i;
4797 for (i = 0; i < ASIZE (spec); ++i)
4798 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4799 overlay, position, bufpos,
4800 replacing_p, frame_window_p)))
4802 replacing_p = rv;
4803 /* If some text in a string is replaced, `position' no
4804 longer points to the position of `object'. */
4805 if (!it || STRINGP (object))
4806 break;
4809 else
4811 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4812 position, bufpos, 0,
4813 frame_window_p)))
4814 replacing_p = rv;
4817 return replacing_p;
4820 /* Value is the position of the end of the `display' property starting
4821 at START_POS in OBJECT. */
4823 static struct text_pos
4824 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4826 Lisp_Object end;
4827 struct text_pos end_pos;
4829 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4830 Qdisplay, object, Qnil);
4831 CHARPOS (end_pos) = XFASTINT (end);
4832 if (STRINGP (object))
4833 compute_string_pos (&end_pos, start_pos, it->string);
4834 else
4835 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4837 return end_pos;
4841 /* Set up IT from a single `display' property specification SPEC. OBJECT
4842 is the object in which the `display' property was found. *POSITION
4843 is the position in OBJECT at which the `display' property was found.
4844 BUFPOS is the buffer position of OBJECT (different from POSITION if
4845 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4846 previously saw a display specification which already replaced text
4847 display with something else, for example an image; we ignore such
4848 properties after the first one has been processed.
4850 OVERLAY is the overlay this `display' property came from,
4851 or nil if it was a text property.
4853 If SPEC is a `space' or `image' specification, and in some other
4854 cases too, set *POSITION to the position where the `display'
4855 property ends.
4857 If IT is NULL, only examine the property specification in SPEC, but
4858 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4859 is intended to be displayed in a window on a GUI frame.
4861 Value is non-zero if something was found which replaces the display
4862 of buffer or string text. */
4864 static int
4865 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4866 Lisp_Object overlay, struct text_pos *position,
4867 ptrdiff_t bufpos, int display_replaced_p,
4868 int frame_window_p)
4870 Lisp_Object form;
4871 Lisp_Object location, value;
4872 struct text_pos start_pos = *position;
4873 int valid_p;
4875 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4876 If the result is non-nil, use VALUE instead of SPEC. */
4877 form = Qt;
4878 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4880 spec = XCDR (spec);
4881 if (!CONSP (spec))
4882 return 0;
4883 form = XCAR (spec);
4884 spec = XCDR (spec);
4887 if (!NILP (form) && !EQ (form, Qt))
4889 ptrdiff_t count = SPECPDL_INDEX ();
4890 struct gcpro gcpro1;
4892 /* Bind `object' to the object having the `display' property, a
4893 buffer or string. Bind `position' to the position in the
4894 object where the property was found, and `buffer-position'
4895 to the current position in the buffer. */
4897 if (NILP (object))
4898 XSETBUFFER (object, current_buffer);
4899 specbind (Qobject, object);
4900 specbind (Qposition, make_number (CHARPOS (*position)));
4901 specbind (Qbuffer_position, make_number (bufpos));
4902 GCPRO1 (form);
4903 form = safe_eval (form);
4904 UNGCPRO;
4905 unbind_to (count, Qnil);
4908 if (NILP (form))
4909 return 0;
4911 /* Handle `(height HEIGHT)' specifications. */
4912 if (CONSP (spec)
4913 && EQ (XCAR (spec), Qheight)
4914 && CONSP (XCDR (spec)))
4916 if (it)
4918 if (!FRAME_WINDOW_P (it->f))
4919 return 0;
4921 it->font_height = XCAR (XCDR (spec));
4922 if (!NILP (it->font_height))
4924 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4925 int new_height = -1;
4927 if (CONSP (it->font_height)
4928 && (EQ (XCAR (it->font_height), Qplus)
4929 || EQ (XCAR (it->font_height), Qminus))
4930 && CONSP (XCDR (it->font_height))
4931 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4933 /* `(+ N)' or `(- N)' where N is an integer. */
4934 int steps = XINT (XCAR (XCDR (it->font_height)));
4935 if (EQ (XCAR (it->font_height), Qplus))
4936 steps = - steps;
4937 it->face_id = smaller_face (it->f, it->face_id, steps);
4939 else if (FUNCTIONP (it->font_height))
4941 /* Call function with current height as argument.
4942 Value is the new height. */
4943 Lisp_Object height;
4944 height = safe_call1 (it->font_height,
4945 face->lface[LFACE_HEIGHT_INDEX]);
4946 if (NUMBERP (height))
4947 new_height = XFLOATINT (height);
4949 else if (NUMBERP (it->font_height))
4951 /* Value is a multiple of the canonical char height. */
4952 struct face *f;
4954 f = FACE_FROM_ID (it->f,
4955 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4956 new_height = (XFLOATINT (it->font_height)
4957 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4959 else
4961 /* Evaluate IT->font_height with `height' bound to the
4962 current specified height to get the new height. */
4963 ptrdiff_t count = SPECPDL_INDEX ();
4965 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4966 value = safe_eval (it->font_height);
4967 unbind_to (count, Qnil);
4969 if (NUMBERP (value))
4970 new_height = XFLOATINT (value);
4973 if (new_height > 0)
4974 it->face_id = face_with_height (it->f, it->face_id, new_height);
4978 return 0;
4981 /* Handle `(space-width WIDTH)'. */
4982 if (CONSP (spec)
4983 && EQ (XCAR (spec), Qspace_width)
4984 && CONSP (XCDR (spec)))
4986 if (it)
4988 if (!FRAME_WINDOW_P (it->f))
4989 return 0;
4991 value = XCAR (XCDR (spec));
4992 if (NUMBERP (value) && XFLOATINT (value) > 0)
4993 it->space_width = value;
4996 return 0;
4999 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5000 if (CONSP (spec)
5001 && EQ (XCAR (spec), Qslice))
5003 Lisp_Object tem;
5005 if (it)
5007 if (!FRAME_WINDOW_P (it->f))
5008 return 0;
5010 if (tem = XCDR (spec), CONSP (tem))
5012 it->slice.x = XCAR (tem);
5013 if (tem = XCDR (tem), CONSP (tem))
5015 it->slice.y = XCAR (tem);
5016 if (tem = XCDR (tem), CONSP (tem))
5018 it->slice.width = XCAR (tem);
5019 if (tem = XCDR (tem), CONSP (tem))
5020 it->slice.height = XCAR (tem);
5026 return 0;
5029 /* Handle `(raise FACTOR)'. */
5030 if (CONSP (spec)
5031 && EQ (XCAR (spec), Qraise)
5032 && CONSP (XCDR (spec)))
5034 if (it)
5036 if (!FRAME_WINDOW_P (it->f))
5037 return 0;
5039 #ifdef HAVE_WINDOW_SYSTEM
5040 value = XCAR (XCDR (spec));
5041 if (NUMBERP (value))
5043 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5044 it->voffset = - (XFLOATINT (value)
5045 * (FONT_HEIGHT (face->font)));
5047 #endif /* HAVE_WINDOW_SYSTEM */
5050 return 0;
5053 /* Don't handle the other kinds of display specifications
5054 inside a string that we got from a `display' property. */
5055 if (it && it->string_from_display_prop_p)
5056 return 0;
5058 /* Characters having this form of property are not displayed, so
5059 we have to find the end of the property. */
5060 if (it)
5062 start_pos = *position;
5063 *position = display_prop_end (it, object, start_pos);
5065 value = Qnil;
5067 /* Stop the scan at that end position--we assume that all
5068 text properties change there. */
5069 if (it)
5070 it->stop_charpos = position->charpos;
5072 /* Handle `(left-fringe BITMAP [FACE])'
5073 and `(right-fringe BITMAP [FACE])'. */
5074 if (CONSP (spec)
5075 && (EQ (XCAR (spec), Qleft_fringe)
5076 || EQ (XCAR (spec), Qright_fringe))
5077 && CONSP (XCDR (spec)))
5079 int fringe_bitmap;
5081 if (it)
5083 if (!FRAME_WINDOW_P (it->f))
5084 /* If we return here, POSITION has been advanced
5085 across the text with this property. */
5087 /* Synchronize the bidi iterator with POSITION. This is
5088 needed because we are not going to push the iterator
5089 on behalf of this display property, so there will be
5090 no pop_it call to do this synchronization for us. */
5091 if (it->bidi_p)
5093 it->position = *position;
5094 iterate_out_of_display_property (it);
5095 *position = it->position;
5097 return 1;
5100 else if (!frame_window_p)
5101 return 1;
5103 #ifdef HAVE_WINDOW_SYSTEM
5104 value = XCAR (XCDR (spec));
5105 if (!SYMBOLP (value)
5106 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5107 /* If we return here, POSITION has been advanced
5108 across the text with this property. */
5110 if (it && it->bidi_p)
5112 it->position = *position;
5113 iterate_out_of_display_property (it);
5114 *position = it->position;
5116 return 1;
5119 if (it)
5121 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
5123 if (CONSP (XCDR (XCDR (spec))))
5125 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5126 int face_id2 = lookup_derived_face (it->f, face_name,
5127 FRINGE_FACE_ID, 0);
5128 if (face_id2 >= 0)
5129 face_id = face_id2;
5132 /* Save current settings of IT so that we can restore them
5133 when we are finished with the glyph property value. */
5134 push_it (it, position);
5136 it->area = TEXT_AREA;
5137 it->what = IT_IMAGE;
5138 it->image_id = -1; /* no image */
5139 it->position = start_pos;
5140 it->object = NILP (object) ? it->w->contents : object;
5141 it->method = GET_FROM_IMAGE;
5142 it->from_overlay = Qnil;
5143 it->face_id = face_id;
5144 it->from_disp_prop_p = true;
5146 /* Say that we haven't consumed the characters with
5147 `display' property yet. The call to pop_it in
5148 set_iterator_to_next will clean this up. */
5149 *position = start_pos;
5151 if (EQ (XCAR (spec), Qleft_fringe))
5153 it->left_user_fringe_bitmap = fringe_bitmap;
5154 it->left_user_fringe_face_id = face_id;
5156 else
5158 it->right_user_fringe_bitmap = fringe_bitmap;
5159 it->right_user_fringe_face_id = face_id;
5162 #endif /* HAVE_WINDOW_SYSTEM */
5163 return 1;
5166 /* Prepare to handle `((margin left-margin) ...)',
5167 `((margin right-margin) ...)' and `((margin nil) ...)'
5168 prefixes for display specifications. */
5169 location = Qunbound;
5170 if (CONSP (spec) && CONSP (XCAR (spec)))
5172 Lisp_Object tem;
5174 value = XCDR (spec);
5175 if (CONSP (value))
5176 value = XCAR (value);
5178 tem = XCAR (spec);
5179 if (EQ (XCAR (tem), Qmargin)
5180 && (tem = XCDR (tem),
5181 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5182 (NILP (tem)
5183 || EQ (tem, Qleft_margin)
5184 || EQ (tem, Qright_margin))))
5185 location = tem;
5188 if (EQ (location, Qunbound))
5190 location = Qnil;
5191 value = spec;
5194 /* After this point, VALUE is the property after any
5195 margin prefix has been stripped. It must be a string,
5196 an image specification, or `(space ...)'.
5198 LOCATION specifies where to display: `left-margin',
5199 `right-margin' or nil. */
5201 valid_p = (STRINGP (value)
5202 #ifdef HAVE_WINDOW_SYSTEM
5203 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5204 && valid_image_p (value))
5205 #endif /* not HAVE_WINDOW_SYSTEM */
5206 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5208 if (valid_p && !display_replaced_p)
5210 int retval = 1;
5212 if (!it)
5214 /* Callers need to know whether the display spec is any kind
5215 of `(space ...)' spec that is about to affect text-area
5216 display. */
5217 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5218 retval = 2;
5219 return retval;
5222 /* Save current settings of IT so that we can restore them
5223 when we are finished with the glyph property value. */
5224 push_it (it, position);
5225 it->from_overlay = overlay;
5226 it->from_disp_prop_p = true;
5228 if (NILP (location))
5229 it->area = TEXT_AREA;
5230 else if (EQ (location, Qleft_margin))
5231 it->area = LEFT_MARGIN_AREA;
5232 else
5233 it->area = RIGHT_MARGIN_AREA;
5235 if (STRINGP (value))
5237 it->string = value;
5238 it->multibyte_p = STRING_MULTIBYTE (it->string);
5239 it->current.overlay_string_index = -1;
5240 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5241 it->end_charpos = it->string_nchars = SCHARS (it->string);
5242 it->method = GET_FROM_STRING;
5243 it->stop_charpos = 0;
5244 it->prev_stop = 0;
5245 it->base_level_stop = 0;
5246 it->string_from_display_prop_p = true;
5247 /* Say that we haven't consumed the characters with
5248 `display' property yet. The call to pop_it in
5249 set_iterator_to_next will clean this up. */
5250 if (BUFFERP (object))
5251 *position = start_pos;
5253 /* Force paragraph direction to be that of the parent
5254 object. If the parent object's paragraph direction is
5255 not yet determined, default to L2R. */
5256 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5257 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5258 else
5259 it->paragraph_embedding = L2R;
5261 /* Set up the bidi iterator for this display string. */
5262 if (it->bidi_p)
5264 it->bidi_it.string.lstring = it->string;
5265 it->bidi_it.string.s = NULL;
5266 it->bidi_it.string.schars = it->end_charpos;
5267 it->bidi_it.string.bufpos = bufpos;
5268 it->bidi_it.string.from_disp_str = 1;
5269 it->bidi_it.string.unibyte = !it->multibyte_p;
5270 it->bidi_it.w = it->w;
5271 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5274 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5276 it->method = GET_FROM_STRETCH;
5277 it->object = value;
5278 *position = it->position = start_pos;
5279 retval = 1 + (it->area == TEXT_AREA);
5281 #ifdef HAVE_WINDOW_SYSTEM
5282 else
5284 it->what = IT_IMAGE;
5285 it->image_id = lookup_image (it->f, value);
5286 it->position = start_pos;
5287 it->object = NILP (object) ? it->w->contents : object;
5288 it->method = GET_FROM_IMAGE;
5290 /* Say that we haven't consumed the characters with
5291 `display' property yet. The call to pop_it in
5292 set_iterator_to_next will clean this up. */
5293 *position = start_pos;
5295 #endif /* HAVE_WINDOW_SYSTEM */
5297 return retval;
5300 /* Invalid property or property not supported. Restore
5301 POSITION to what it was before. */
5302 *position = start_pos;
5303 return 0;
5306 /* Check if PROP is a display property value whose text should be
5307 treated as intangible. OVERLAY is the overlay from which PROP
5308 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5309 specify the buffer position covered by PROP. */
5312 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5313 ptrdiff_t charpos, ptrdiff_t bytepos)
5315 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5316 struct text_pos position;
5318 SET_TEXT_POS (position, charpos, bytepos);
5319 return handle_display_spec (NULL, prop, Qnil, overlay,
5320 &position, charpos, frame_window_p);
5324 /* Return 1 if PROP is a display sub-property value containing STRING.
5326 Implementation note: this and the following function are really
5327 special cases of handle_display_spec and
5328 handle_single_display_spec, and should ideally use the same code.
5329 Until they do, these two pairs must be consistent and must be
5330 modified in sync. */
5332 static int
5333 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5335 if (EQ (string, prop))
5336 return 1;
5338 /* Skip over `when FORM'. */
5339 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5341 prop = XCDR (prop);
5342 if (!CONSP (prop))
5343 return 0;
5344 /* Actually, the condition following `when' should be eval'ed,
5345 like handle_single_display_spec does, and we should return
5346 zero if it evaluates to nil. However, this function is
5347 called only when the buffer was already displayed and some
5348 glyph in the glyph matrix was found to come from a display
5349 string. Therefore, the condition was already evaluated, and
5350 the result was non-nil, otherwise the display string wouldn't
5351 have been displayed and we would have never been called for
5352 this property. Thus, we can skip the evaluation and assume
5353 its result is non-nil. */
5354 prop = XCDR (prop);
5357 if (CONSP (prop))
5358 /* Skip over `margin LOCATION'. */
5359 if (EQ (XCAR (prop), Qmargin))
5361 prop = XCDR (prop);
5362 if (!CONSP (prop))
5363 return 0;
5365 prop = XCDR (prop);
5366 if (!CONSP (prop))
5367 return 0;
5370 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5374 /* Return 1 if STRING appears in the `display' property PROP. */
5376 static int
5377 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5379 if (CONSP (prop)
5380 && !EQ (XCAR (prop), Qwhen)
5381 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5383 /* A list of sub-properties. */
5384 while (CONSP (prop))
5386 if (single_display_spec_string_p (XCAR (prop), string))
5387 return 1;
5388 prop = XCDR (prop);
5391 else if (VECTORP (prop))
5393 /* A vector of sub-properties. */
5394 ptrdiff_t i;
5395 for (i = 0; i < ASIZE (prop); ++i)
5396 if (single_display_spec_string_p (AREF (prop, i), string))
5397 return 1;
5399 else
5400 return single_display_spec_string_p (prop, string);
5402 return 0;
5405 /* Look for STRING in overlays and text properties in the current
5406 buffer, between character positions FROM and TO (excluding TO).
5407 BACK_P non-zero means look back (in this case, TO is supposed to be
5408 less than FROM).
5409 Value is the first character position where STRING was found, or
5410 zero if it wasn't found before hitting TO.
5412 This function may only use code that doesn't eval because it is
5413 called asynchronously from note_mouse_highlight. */
5415 static ptrdiff_t
5416 string_buffer_position_lim (Lisp_Object string,
5417 ptrdiff_t from, ptrdiff_t to, int back_p)
5419 Lisp_Object limit, prop, pos;
5420 int found = 0;
5422 pos = make_number (max (from, BEGV));
5424 if (!back_p) /* looking forward */
5426 limit = make_number (min (to, ZV));
5427 while (!found && !EQ (pos, limit))
5429 prop = Fget_char_property (pos, Qdisplay, Qnil);
5430 if (!NILP (prop) && display_prop_string_p (prop, string))
5431 found = 1;
5432 else
5433 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5434 limit);
5437 else /* looking back */
5439 limit = make_number (max (to, BEGV));
5440 while (!found && !EQ (pos, limit))
5442 prop = Fget_char_property (pos, Qdisplay, Qnil);
5443 if (!NILP (prop) && display_prop_string_p (prop, string))
5444 found = 1;
5445 else
5446 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5447 limit);
5451 return found ? XINT (pos) : 0;
5454 /* Determine which buffer position in current buffer STRING comes from.
5455 AROUND_CHARPOS is an approximate position where it could come from.
5456 Value is the buffer position or 0 if it couldn't be determined.
5458 This function is necessary because we don't record buffer positions
5459 in glyphs generated from strings (to keep struct glyph small).
5460 This function may only use code that doesn't eval because it is
5461 called asynchronously from note_mouse_highlight. */
5463 static ptrdiff_t
5464 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5466 const int MAX_DISTANCE = 1000;
5467 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5468 around_charpos + MAX_DISTANCE,
5471 if (!found)
5472 found = string_buffer_position_lim (string, around_charpos,
5473 around_charpos - MAX_DISTANCE, 1);
5474 return found;
5479 /***********************************************************************
5480 `composition' property
5481 ***********************************************************************/
5483 /* Set up iterator IT from `composition' property at its current
5484 position. Called from handle_stop. */
5486 static enum prop_handled
5487 handle_composition_prop (struct it *it)
5489 Lisp_Object prop, string;
5490 ptrdiff_t pos, pos_byte, start, end;
5492 if (STRINGP (it->string))
5494 unsigned char *s;
5496 pos = IT_STRING_CHARPOS (*it);
5497 pos_byte = IT_STRING_BYTEPOS (*it);
5498 string = it->string;
5499 s = SDATA (string) + pos_byte;
5500 it->c = STRING_CHAR (s);
5502 else
5504 pos = IT_CHARPOS (*it);
5505 pos_byte = IT_BYTEPOS (*it);
5506 string = Qnil;
5507 it->c = FETCH_CHAR (pos_byte);
5510 /* If there's a valid composition and point is not inside of the
5511 composition (in the case that the composition is from the current
5512 buffer), draw a glyph composed from the composition components. */
5513 if (find_composition (pos, -1, &start, &end, &prop, string)
5514 && composition_valid_p (start, end, prop)
5515 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5517 if (start < pos)
5518 /* As we can't handle this situation (perhaps font-lock added
5519 a new composition), we just return here hoping that next
5520 redisplay will detect this composition much earlier. */
5521 return HANDLED_NORMALLY;
5522 if (start != pos)
5524 if (STRINGP (it->string))
5525 pos_byte = string_char_to_byte (it->string, start);
5526 else
5527 pos_byte = CHAR_TO_BYTE (start);
5529 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5530 prop, string);
5532 if (it->cmp_it.id >= 0)
5534 it->cmp_it.ch = -1;
5535 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5536 it->cmp_it.nglyphs = -1;
5540 return HANDLED_NORMALLY;
5545 /***********************************************************************
5546 Overlay strings
5547 ***********************************************************************/
5549 /* The following structure is used to record overlay strings for
5550 later sorting in load_overlay_strings. */
5552 struct overlay_entry
5554 Lisp_Object overlay;
5555 Lisp_Object string;
5556 EMACS_INT priority;
5557 int after_string_p;
5561 /* Set up iterator IT from overlay strings at its current position.
5562 Called from handle_stop. */
5564 static enum prop_handled
5565 handle_overlay_change (struct it *it)
5567 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5568 return HANDLED_RECOMPUTE_PROPS;
5569 else
5570 return HANDLED_NORMALLY;
5574 /* Set up the next overlay string for delivery by IT, if there is an
5575 overlay string to deliver. Called by set_iterator_to_next when the
5576 end of the current overlay string is reached. If there are more
5577 overlay strings to display, IT->string and
5578 IT->current.overlay_string_index are set appropriately here.
5579 Otherwise IT->string is set to nil. */
5581 static void
5582 next_overlay_string (struct it *it)
5584 ++it->current.overlay_string_index;
5585 if (it->current.overlay_string_index == it->n_overlay_strings)
5587 /* No more overlay strings. Restore IT's settings to what
5588 they were before overlay strings were processed, and
5589 continue to deliver from current_buffer. */
5591 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5592 pop_it (it);
5593 eassert (it->sp > 0
5594 || (NILP (it->string)
5595 && it->method == GET_FROM_BUFFER
5596 && it->stop_charpos >= BEGV
5597 && it->stop_charpos <= it->end_charpos));
5598 it->current.overlay_string_index = -1;
5599 it->n_overlay_strings = 0;
5600 it->overlay_strings_charpos = -1;
5601 /* If there's an empty display string on the stack, pop the
5602 stack, to resync the bidi iterator with IT's position. Such
5603 empty strings are pushed onto the stack in
5604 get_overlay_strings_1. */
5605 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5606 pop_it (it);
5608 /* If we're at the end of the buffer, record that we have
5609 processed the overlay strings there already, so that
5610 next_element_from_buffer doesn't try it again. */
5611 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5612 it->overlay_strings_at_end_processed_p = true;
5614 else
5616 /* There are more overlay strings to process. If
5617 IT->current.overlay_string_index has advanced to a position
5618 where we must load IT->overlay_strings with more strings, do
5619 it. We must load at the IT->overlay_strings_charpos where
5620 IT->n_overlay_strings was originally computed; when invisible
5621 text is present, this might not be IT_CHARPOS (Bug#7016). */
5622 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5624 if (it->current.overlay_string_index && i == 0)
5625 load_overlay_strings (it, it->overlay_strings_charpos);
5627 /* Initialize IT to deliver display elements from the overlay
5628 string. */
5629 it->string = it->overlay_strings[i];
5630 it->multibyte_p = STRING_MULTIBYTE (it->string);
5631 SET_TEXT_POS (it->current.string_pos, 0, 0);
5632 it->method = GET_FROM_STRING;
5633 it->stop_charpos = 0;
5634 it->end_charpos = SCHARS (it->string);
5635 if (it->cmp_it.stop_pos >= 0)
5636 it->cmp_it.stop_pos = 0;
5637 it->prev_stop = 0;
5638 it->base_level_stop = 0;
5640 /* Set up the bidi iterator for this overlay string. */
5641 if (it->bidi_p)
5643 it->bidi_it.string.lstring = it->string;
5644 it->bidi_it.string.s = NULL;
5645 it->bidi_it.string.schars = SCHARS (it->string);
5646 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5647 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5648 it->bidi_it.string.unibyte = !it->multibyte_p;
5649 it->bidi_it.w = it->w;
5650 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5654 CHECK_IT (it);
5658 /* Compare two overlay_entry structures E1 and E2. Used as a
5659 comparison function for qsort in load_overlay_strings. Overlay
5660 strings for the same position are sorted so that
5662 1. All after-strings come in front of before-strings, except
5663 when they come from the same overlay.
5665 2. Within after-strings, strings are sorted so that overlay strings
5666 from overlays with higher priorities come first.
5668 2. Within before-strings, strings are sorted so that overlay
5669 strings from overlays with higher priorities come last.
5671 Value is analogous to strcmp. */
5674 static int
5675 compare_overlay_entries (const void *e1, const void *e2)
5677 struct overlay_entry const *entry1 = e1;
5678 struct overlay_entry const *entry2 = e2;
5679 int result;
5681 if (entry1->after_string_p != entry2->after_string_p)
5683 /* Let after-strings appear in front of before-strings if
5684 they come from different overlays. */
5685 if (EQ (entry1->overlay, entry2->overlay))
5686 result = entry1->after_string_p ? 1 : -1;
5687 else
5688 result = entry1->after_string_p ? -1 : 1;
5690 else if (entry1->priority != entry2->priority)
5692 if (entry1->after_string_p)
5693 /* After-strings sorted in order of decreasing priority. */
5694 result = entry2->priority < entry1->priority ? -1 : 1;
5695 else
5696 /* Before-strings sorted in order of increasing priority. */
5697 result = entry1->priority < entry2->priority ? -1 : 1;
5699 else
5700 result = 0;
5702 return result;
5706 /* Load the vector IT->overlay_strings with overlay strings from IT's
5707 current buffer position, or from CHARPOS if that is > 0. Set
5708 IT->n_overlays to the total number of overlay strings found.
5710 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5711 a time. On entry into load_overlay_strings,
5712 IT->current.overlay_string_index gives the number of overlay
5713 strings that have already been loaded by previous calls to this
5714 function.
5716 IT->add_overlay_start contains an additional overlay start
5717 position to consider for taking overlay strings from, if non-zero.
5718 This position comes into play when the overlay has an `invisible'
5719 property, and both before and after-strings. When we've skipped to
5720 the end of the overlay, because of its `invisible' property, we
5721 nevertheless want its before-string to appear.
5722 IT->add_overlay_start will contain the overlay start position
5723 in this case.
5725 Overlay strings are sorted so that after-string strings come in
5726 front of before-string strings. Within before and after-strings,
5727 strings are sorted by overlay priority. See also function
5728 compare_overlay_entries. */
5730 static void
5731 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5733 Lisp_Object overlay, window, str, invisible;
5734 struct Lisp_Overlay *ov;
5735 ptrdiff_t start, end;
5736 ptrdiff_t size = 20;
5737 ptrdiff_t n = 0, i, j;
5738 int invis_p;
5739 struct overlay_entry *entries = alloca (size * sizeof *entries);
5740 USE_SAFE_ALLOCA;
5742 if (charpos <= 0)
5743 charpos = IT_CHARPOS (*it);
5745 /* Append the overlay string STRING of overlay OVERLAY to vector
5746 `entries' which has size `size' and currently contains `n'
5747 elements. AFTER_P non-zero means STRING is an after-string of
5748 OVERLAY. */
5749 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5750 do \
5752 Lisp_Object priority; \
5754 if (n == size) \
5756 struct overlay_entry *old = entries; \
5757 SAFE_NALLOCA (entries, 2, size); \
5758 memcpy (entries, old, size * sizeof *entries); \
5759 size *= 2; \
5762 entries[n].string = (STRING); \
5763 entries[n].overlay = (OVERLAY); \
5764 priority = Foverlay_get ((OVERLAY), Qpriority); \
5765 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5766 entries[n].after_string_p = (AFTER_P); \
5767 ++n; \
5769 while (0)
5771 /* Process overlay before the overlay center. */
5772 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5774 XSETMISC (overlay, ov);
5775 eassert (OVERLAYP (overlay));
5776 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5777 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5779 if (end < charpos)
5780 break;
5782 /* Skip this overlay if it doesn't start or end at IT's current
5783 position. */
5784 if (end != charpos && start != charpos)
5785 continue;
5787 /* Skip this overlay if it doesn't apply to IT->w. */
5788 window = Foverlay_get (overlay, Qwindow);
5789 if (WINDOWP (window) && XWINDOW (window) != it->w)
5790 continue;
5792 /* If the text ``under'' the overlay is invisible, both before-
5793 and after-strings from this overlay are visible; start and
5794 end position are indistinguishable. */
5795 invisible = Foverlay_get (overlay, Qinvisible);
5796 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5798 /* If overlay has a non-empty before-string, record it. */
5799 if ((start == charpos || (end == charpos && invis_p))
5800 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5801 && SCHARS (str))
5802 RECORD_OVERLAY_STRING (overlay, str, 0);
5804 /* If overlay has a non-empty after-string, record it. */
5805 if ((end == charpos || (start == charpos && invis_p))
5806 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5807 && SCHARS (str))
5808 RECORD_OVERLAY_STRING (overlay, str, 1);
5811 /* Process overlays after the overlay center. */
5812 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5814 XSETMISC (overlay, ov);
5815 eassert (OVERLAYP (overlay));
5816 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5817 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5819 if (start > charpos)
5820 break;
5822 /* Skip this overlay if it doesn't start or end at IT's current
5823 position. */
5824 if (end != charpos && start != charpos)
5825 continue;
5827 /* Skip this overlay if it doesn't apply to IT->w. */
5828 window = Foverlay_get (overlay, Qwindow);
5829 if (WINDOWP (window) && XWINDOW (window) != it->w)
5830 continue;
5832 /* If the text ``under'' the overlay is invisible, it has a zero
5833 dimension, and both before- and after-strings apply. */
5834 invisible = Foverlay_get (overlay, Qinvisible);
5835 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5837 /* If overlay has a non-empty before-string, record it. */
5838 if ((start == charpos || (end == charpos && invis_p))
5839 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5840 && SCHARS (str))
5841 RECORD_OVERLAY_STRING (overlay, str, 0);
5843 /* If overlay has a non-empty after-string, record it. */
5844 if ((end == charpos || (start == charpos && invis_p))
5845 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5846 && SCHARS (str))
5847 RECORD_OVERLAY_STRING (overlay, str, 1);
5850 #undef RECORD_OVERLAY_STRING
5852 /* Sort entries. */
5853 if (n > 1)
5854 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5856 /* Record number of overlay strings, and where we computed it. */
5857 it->n_overlay_strings = n;
5858 it->overlay_strings_charpos = charpos;
5860 /* IT->current.overlay_string_index is the number of overlay strings
5861 that have already been consumed by IT. Copy some of the
5862 remaining overlay strings to IT->overlay_strings. */
5863 i = 0;
5864 j = it->current.overlay_string_index;
5865 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5867 it->overlay_strings[i] = entries[j].string;
5868 it->string_overlays[i++] = entries[j++].overlay;
5871 CHECK_IT (it);
5872 SAFE_FREE ();
5876 /* Get the first chunk of overlay strings at IT's current buffer
5877 position, or at CHARPOS if that is > 0. Value is non-zero if at
5878 least one overlay string was found. */
5880 static int
5881 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5883 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5884 process. This fills IT->overlay_strings with strings, and sets
5885 IT->n_overlay_strings to the total number of strings to process.
5886 IT->pos.overlay_string_index has to be set temporarily to zero
5887 because load_overlay_strings needs this; it must be set to -1
5888 when no overlay strings are found because a zero value would
5889 indicate a position in the first overlay string. */
5890 it->current.overlay_string_index = 0;
5891 load_overlay_strings (it, charpos);
5893 /* If we found overlay strings, set up IT to deliver display
5894 elements from the first one. Otherwise set up IT to deliver
5895 from current_buffer. */
5896 if (it->n_overlay_strings)
5898 /* Make sure we know settings in current_buffer, so that we can
5899 restore meaningful values when we're done with the overlay
5900 strings. */
5901 if (compute_stop_p)
5902 compute_stop_pos (it);
5903 eassert (it->face_id >= 0);
5905 /* Save IT's settings. They are restored after all overlay
5906 strings have been processed. */
5907 eassert (!compute_stop_p || it->sp == 0);
5909 /* When called from handle_stop, there might be an empty display
5910 string loaded. In that case, don't bother saving it. But
5911 don't use this optimization with the bidi iterator, since we
5912 need the corresponding pop_it call to resync the bidi
5913 iterator's position with IT's position, after we are done
5914 with the overlay strings. (The corresponding call to pop_it
5915 in case of an empty display string is in
5916 next_overlay_string.) */
5917 if (!(!it->bidi_p
5918 && STRINGP (it->string) && !SCHARS (it->string)))
5919 push_it (it, NULL);
5921 /* Set up IT to deliver display elements from the first overlay
5922 string. */
5923 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5924 it->string = it->overlay_strings[0];
5925 it->from_overlay = Qnil;
5926 it->stop_charpos = 0;
5927 eassert (STRINGP (it->string));
5928 it->end_charpos = SCHARS (it->string);
5929 it->prev_stop = 0;
5930 it->base_level_stop = 0;
5931 it->multibyte_p = STRING_MULTIBYTE (it->string);
5932 it->method = GET_FROM_STRING;
5933 it->from_disp_prop_p = 0;
5935 /* Force paragraph direction to be that of the parent
5936 buffer. */
5937 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5938 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5939 else
5940 it->paragraph_embedding = L2R;
5942 /* Set up the bidi iterator for this overlay string. */
5943 if (it->bidi_p)
5945 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5947 it->bidi_it.string.lstring = it->string;
5948 it->bidi_it.string.s = NULL;
5949 it->bidi_it.string.schars = SCHARS (it->string);
5950 it->bidi_it.string.bufpos = pos;
5951 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5952 it->bidi_it.string.unibyte = !it->multibyte_p;
5953 it->bidi_it.w = it->w;
5954 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5956 return 1;
5959 it->current.overlay_string_index = -1;
5960 return 0;
5963 static int
5964 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5966 it->string = Qnil;
5967 it->method = GET_FROM_BUFFER;
5969 (void) get_overlay_strings_1 (it, charpos, 1);
5971 CHECK_IT (it);
5973 /* Value is non-zero if we found at least one overlay string. */
5974 return STRINGP (it->string);
5979 /***********************************************************************
5980 Saving and restoring state
5981 ***********************************************************************/
5983 /* Save current settings of IT on IT->stack. Called, for example,
5984 before setting up IT for an overlay string, to be able to restore
5985 IT's settings to what they were after the overlay string has been
5986 processed. If POSITION is non-NULL, it is the position to save on
5987 the stack instead of IT->position. */
5989 static void
5990 push_it (struct it *it, struct text_pos *position)
5992 struct iterator_stack_entry *p;
5994 eassert (it->sp < IT_STACK_SIZE);
5995 p = it->stack + it->sp;
5997 p->stop_charpos = it->stop_charpos;
5998 p->prev_stop = it->prev_stop;
5999 p->base_level_stop = it->base_level_stop;
6000 p->cmp_it = it->cmp_it;
6001 eassert (it->face_id >= 0);
6002 p->face_id = it->face_id;
6003 p->string = it->string;
6004 p->method = it->method;
6005 p->from_overlay = it->from_overlay;
6006 switch (p->method)
6008 case GET_FROM_IMAGE:
6009 p->u.image.object = it->object;
6010 p->u.image.image_id = it->image_id;
6011 p->u.image.slice = it->slice;
6012 break;
6013 case GET_FROM_STRETCH:
6014 p->u.stretch.object = it->object;
6015 break;
6017 p->position = position ? *position : it->position;
6018 p->current = it->current;
6019 p->end_charpos = it->end_charpos;
6020 p->string_nchars = it->string_nchars;
6021 p->area = it->area;
6022 p->multibyte_p = it->multibyte_p;
6023 p->avoid_cursor_p = it->avoid_cursor_p;
6024 p->space_width = it->space_width;
6025 p->font_height = it->font_height;
6026 p->voffset = it->voffset;
6027 p->string_from_display_prop_p = it->string_from_display_prop_p;
6028 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6029 p->display_ellipsis_p = 0;
6030 p->line_wrap = it->line_wrap;
6031 p->bidi_p = it->bidi_p;
6032 p->paragraph_embedding = it->paragraph_embedding;
6033 p->from_disp_prop_p = it->from_disp_prop_p;
6034 ++it->sp;
6036 /* Save the state of the bidi iterator as well. */
6037 if (it->bidi_p)
6038 bidi_push_it (&it->bidi_it);
6041 static void
6042 iterate_out_of_display_property (struct it *it)
6044 int buffer_p = !STRINGP (it->string);
6045 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6046 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6048 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6050 /* Maybe initialize paragraph direction. If we are at the beginning
6051 of a new paragraph, next_element_from_buffer may not have a
6052 chance to do that. */
6053 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6054 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6055 /* prev_stop can be zero, so check against BEGV as well. */
6056 while (it->bidi_it.charpos >= bob
6057 && it->prev_stop <= it->bidi_it.charpos
6058 && it->bidi_it.charpos < CHARPOS (it->position)
6059 && it->bidi_it.charpos < eob)
6060 bidi_move_to_visually_next (&it->bidi_it);
6061 /* Record the stop_pos we just crossed, for when we cross it
6062 back, maybe. */
6063 if (it->bidi_it.charpos > CHARPOS (it->position))
6064 it->prev_stop = CHARPOS (it->position);
6065 /* If we ended up not where pop_it put us, resync IT's
6066 positional members with the bidi iterator. */
6067 if (it->bidi_it.charpos != CHARPOS (it->position))
6068 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6069 if (buffer_p)
6070 it->current.pos = it->position;
6071 else
6072 it->current.string_pos = it->position;
6075 /* Restore IT's settings from IT->stack. Called, for example, when no
6076 more overlay strings must be processed, and we return to delivering
6077 display elements from a buffer, or when the end of a string from a
6078 `display' property is reached and we return to delivering display
6079 elements from an overlay string, or from a buffer. */
6081 static void
6082 pop_it (struct it *it)
6084 struct iterator_stack_entry *p;
6085 int from_display_prop = it->from_disp_prop_p;
6087 eassert (it->sp > 0);
6088 --it->sp;
6089 p = it->stack + it->sp;
6090 it->stop_charpos = p->stop_charpos;
6091 it->prev_stop = p->prev_stop;
6092 it->base_level_stop = p->base_level_stop;
6093 it->cmp_it = p->cmp_it;
6094 it->face_id = p->face_id;
6095 it->current = p->current;
6096 it->position = p->position;
6097 it->string = p->string;
6098 it->from_overlay = p->from_overlay;
6099 if (NILP (it->string))
6100 SET_TEXT_POS (it->current.string_pos, -1, -1);
6101 it->method = p->method;
6102 switch (it->method)
6104 case GET_FROM_IMAGE:
6105 it->image_id = p->u.image.image_id;
6106 it->object = p->u.image.object;
6107 it->slice = p->u.image.slice;
6108 break;
6109 case GET_FROM_STRETCH:
6110 it->object = p->u.stretch.object;
6111 break;
6112 case GET_FROM_BUFFER:
6113 it->object = it->w->contents;
6114 break;
6115 case GET_FROM_STRING:
6117 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6119 /* Restore the face_box_p flag, since it could have been
6120 overwritten by the face of the object that we just finished
6121 displaying. */
6122 if (face)
6123 it->face_box_p = face->box != FACE_NO_BOX;
6124 it->object = it->string;
6126 break;
6127 case GET_FROM_DISPLAY_VECTOR:
6128 if (it->s)
6129 it->method = GET_FROM_C_STRING;
6130 else if (STRINGP (it->string))
6131 it->method = GET_FROM_STRING;
6132 else
6134 it->method = GET_FROM_BUFFER;
6135 it->object = it->w->contents;
6138 it->end_charpos = p->end_charpos;
6139 it->string_nchars = p->string_nchars;
6140 it->area = p->area;
6141 it->multibyte_p = p->multibyte_p;
6142 it->avoid_cursor_p = p->avoid_cursor_p;
6143 it->space_width = p->space_width;
6144 it->font_height = p->font_height;
6145 it->voffset = p->voffset;
6146 it->string_from_display_prop_p = p->string_from_display_prop_p;
6147 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6148 it->line_wrap = p->line_wrap;
6149 it->bidi_p = p->bidi_p;
6150 it->paragraph_embedding = p->paragraph_embedding;
6151 it->from_disp_prop_p = p->from_disp_prop_p;
6152 if (it->bidi_p)
6154 bidi_pop_it (&it->bidi_it);
6155 /* Bidi-iterate until we get out of the portion of text, if any,
6156 covered by a `display' text property or by an overlay with
6157 `display' property. (We cannot just jump there, because the
6158 internal coherency of the bidi iterator state can not be
6159 preserved across such jumps.) We also must determine the
6160 paragraph base direction if the overlay we just processed is
6161 at the beginning of a new paragraph. */
6162 if (from_display_prop
6163 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6164 iterate_out_of_display_property (it);
6166 eassert ((BUFFERP (it->object)
6167 && IT_CHARPOS (*it) == it->bidi_it.charpos
6168 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6169 || (STRINGP (it->object)
6170 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6171 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6172 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6178 /***********************************************************************
6179 Moving over lines
6180 ***********************************************************************/
6182 /* Set IT's current position to the previous line start. */
6184 static void
6185 back_to_previous_line_start (struct it *it)
6187 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6189 DEC_BOTH (cp, bp);
6190 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6194 /* Move IT to the next line start.
6196 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6197 we skipped over part of the text (as opposed to moving the iterator
6198 continuously over the text). Otherwise, don't change the value
6199 of *SKIPPED_P.
6201 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6202 iterator on the newline, if it was found.
6204 Newlines may come from buffer text, overlay strings, or strings
6205 displayed via the `display' property. That's the reason we can't
6206 simply use find_newline_no_quit.
6208 Note that this function may not skip over invisible text that is so
6209 because of text properties and immediately follows a newline. If
6210 it would, function reseat_at_next_visible_line_start, when called
6211 from set_iterator_to_next, would effectively make invisible
6212 characters following a newline part of the wrong glyph row, which
6213 leads to wrong cursor motion. */
6215 static int
6216 forward_to_next_line_start (struct it *it, int *skipped_p,
6217 struct bidi_it *bidi_it_prev)
6219 ptrdiff_t old_selective;
6220 int newline_found_p, n;
6221 const int MAX_NEWLINE_DISTANCE = 500;
6223 /* If already on a newline, just consume it to avoid unintended
6224 skipping over invisible text below. */
6225 if (it->what == IT_CHARACTER
6226 && it->c == '\n'
6227 && CHARPOS (it->position) == IT_CHARPOS (*it))
6229 if (it->bidi_p && bidi_it_prev)
6230 *bidi_it_prev = it->bidi_it;
6231 set_iterator_to_next (it, 0);
6232 it->c = 0;
6233 return 1;
6236 /* Don't handle selective display in the following. It's (a)
6237 unnecessary because it's done by the caller, and (b) leads to an
6238 infinite recursion because next_element_from_ellipsis indirectly
6239 calls this function. */
6240 old_selective = it->selective;
6241 it->selective = 0;
6243 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6244 from buffer text. */
6245 for (n = newline_found_p = 0;
6246 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6247 n += STRINGP (it->string) ? 0 : 1)
6249 if (!get_next_display_element (it))
6250 return 0;
6251 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6252 if (newline_found_p && it->bidi_p && bidi_it_prev)
6253 *bidi_it_prev = it->bidi_it;
6254 set_iterator_to_next (it, 0);
6257 /* If we didn't find a newline near enough, see if we can use a
6258 short-cut. */
6259 if (!newline_found_p)
6261 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6262 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6263 1, &bytepos);
6264 Lisp_Object pos;
6266 eassert (!STRINGP (it->string));
6268 /* If there isn't any `display' property in sight, and no
6269 overlays, we can just use the position of the newline in
6270 buffer text. */
6271 if (it->stop_charpos >= limit
6272 || ((pos = Fnext_single_property_change (make_number (start),
6273 Qdisplay, Qnil,
6274 make_number (limit)),
6275 NILP (pos))
6276 && next_overlay_change (start) == ZV))
6278 if (!it->bidi_p)
6280 IT_CHARPOS (*it) = limit;
6281 IT_BYTEPOS (*it) = bytepos;
6283 else
6285 struct bidi_it bprev;
6287 /* Help bidi.c avoid expensive searches for display
6288 properties and overlays, by telling it that there are
6289 none up to `limit'. */
6290 if (it->bidi_it.disp_pos < limit)
6292 it->bidi_it.disp_pos = limit;
6293 it->bidi_it.disp_prop = 0;
6295 do {
6296 bprev = it->bidi_it;
6297 bidi_move_to_visually_next (&it->bidi_it);
6298 } while (it->bidi_it.charpos != limit);
6299 IT_CHARPOS (*it) = limit;
6300 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6301 if (bidi_it_prev)
6302 *bidi_it_prev = bprev;
6304 *skipped_p = newline_found_p = true;
6306 else
6308 while (get_next_display_element (it)
6309 && !newline_found_p)
6311 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6312 if (newline_found_p && it->bidi_p && bidi_it_prev)
6313 *bidi_it_prev = it->bidi_it;
6314 set_iterator_to_next (it, 0);
6319 it->selective = old_selective;
6320 return newline_found_p;
6324 /* Set IT's current position to the previous visible line start. Skip
6325 invisible text that is so either due to text properties or due to
6326 selective display. Caution: this does not change IT->current_x and
6327 IT->hpos. */
6329 static void
6330 back_to_previous_visible_line_start (struct it *it)
6332 while (IT_CHARPOS (*it) > BEGV)
6334 back_to_previous_line_start (it);
6336 if (IT_CHARPOS (*it) <= BEGV)
6337 break;
6339 /* If selective > 0, then lines indented more than its value are
6340 invisible. */
6341 if (it->selective > 0
6342 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6343 it->selective))
6344 continue;
6346 /* Check the newline before point for invisibility. */
6348 Lisp_Object prop;
6349 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6350 Qinvisible, it->window);
6351 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6352 continue;
6355 if (IT_CHARPOS (*it) <= BEGV)
6356 break;
6359 struct it it2;
6360 void *it2data = NULL;
6361 ptrdiff_t pos;
6362 ptrdiff_t beg, end;
6363 Lisp_Object val, overlay;
6365 SAVE_IT (it2, *it, it2data);
6367 /* If newline is part of a composition, continue from start of composition */
6368 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6369 && beg < IT_CHARPOS (*it))
6370 goto replaced;
6372 /* If newline is replaced by a display property, find start of overlay
6373 or interval and continue search from that point. */
6374 pos = --IT_CHARPOS (it2);
6375 --IT_BYTEPOS (it2);
6376 it2.sp = 0;
6377 bidi_unshelve_cache (NULL, 0);
6378 it2.string_from_display_prop_p = 0;
6379 it2.from_disp_prop_p = 0;
6380 if (handle_display_prop (&it2) == HANDLED_RETURN
6381 && !NILP (val = get_char_property_and_overlay
6382 (make_number (pos), Qdisplay, Qnil, &overlay))
6383 && (OVERLAYP (overlay)
6384 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6385 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6387 RESTORE_IT (it, it, it2data);
6388 goto replaced;
6391 /* Newline is not replaced by anything -- so we are done. */
6392 RESTORE_IT (it, it, it2data);
6393 break;
6395 replaced:
6396 if (beg < BEGV)
6397 beg = BEGV;
6398 IT_CHARPOS (*it) = beg;
6399 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6403 it->continuation_lines_width = 0;
6405 eassert (IT_CHARPOS (*it) >= BEGV);
6406 eassert (IT_CHARPOS (*it) == BEGV
6407 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6408 CHECK_IT (it);
6412 /* Reseat iterator IT at the previous visible line start. Skip
6413 invisible text that is so either due to text properties or due to
6414 selective display. At the end, update IT's overlay information,
6415 face information etc. */
6417 void
6418 reseat_at_previous_visible_line_start (struct it *it)
6420 back_to_previous_visible_line_start (it);
6421 reseat (it, it->current.pos, 1);
6422 CHECK_IT (it);
6426 /* Reseat iterator IT on the next visible line start in the current
6427 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6428 preceding the line start. Skip over invisible text that is so
6429 because of selective display. Compute faces, overlays etc at the
6430 new position. Note that this function does not skip over text that
6431 is invisible because of text properties. */
6433 static void
6434 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6436 int newline_found_p, skipped_p = 0;
6437 struct bidi_it bidi_it_prev;
6439 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6441 /* Skip over lines that are invisible because they are indented
6442 more than the value of IT->selective. */
6443 if (it->selective > 0)
6444 while (IT_CHARPOS (*it) < ZV
6445 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6446 it->selective))
6448 eassert (IT_BYTEPOS (*it) == BEGV
6449 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6450 newline_found_p =
6451 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6454 /* Position on the newline if that's what's requested. */
6455 if (on_newline_p && newline_found_p)
6457 if (STRINGP (it->string))
6459 if (IT_STRING_CHARPOS (*it) > 0)
6461 if (!it->bidi_p)
6463 --IT_STRING_CHARPOS (*it);
6464 --IT_STRING_BYTEPOS (*it);
6466 else
6468 /* We need to restore the bidi iterator to the state
6469 it had on the newline, and resync the IT's
6470 position with that. */
6471 it->bidi_it = bidi_it_prev;
6472 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6473 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6477 else if (IT_CHARPOS (*it) > BEGV)
6479 if (!it->bidi_p)
6481 --IT_CHARPOS (*it);
6482 --IT_BYTEPOS (*it);
6484 else
6486 /* We need to restore the bidi iterator to the state it
6487 had on the newline and resync IT with that. */
6488 it->bidi_it = bidi_it_prev;
6489 IT_CHARPOS (*it) = it->bidi_it.charpos;
6490 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6492 reseat (it, it->current.pos, 0);
6495 else if (skipped_p)
6496 reseat (it, it->current.pos, 0);
6498 CHECK_IT (it);
6503 /***********************************************************************
6504 Changing an iterator's position
6505 ***********************************************************************/
6507 /* Change IT's current position to POS in current_buffer. If FORCE_P
6508 is non-zero, always check for text properties at the new position.
6509 Otherwise, text properties are only looked up if POS >=
6510 IT->check_charpos of a property. */
6512 static void
6513 reseat (struct it *it, struct text_pos pos, int force_p)
6515 ptrdiff_t original_pos = IT_CHARPOS (*it);
6517 reseat_1 (it, pos, 0);
6519 /* Determine where to check text properties. Avoid doing it
6520 where possible because text property lookup is very expensive. */
6521 if (force_p
6522 || CHARPOS (pos) > it->stop_charpos
6523 || CHARPOS (pos) < original_pos)
6525 if (it->bidi_p)
6527 /* For bidi iteration, we need to prime prev_stop and
6528 base_level_stop with our best estimations. */
6529 /* Implementation note: Of course, POS is not necessarily a
6530 stop position, so assigning prev_pos to it is a lie; we
6531 should have called compute_stop_backwards. However, if
6532 the current buffer does not include any R2L characters,
6533 that call would be a waste of cycles, because the
6534 iterator will never move back, and thus never cross this
6535 "fake" stop position. So we delay that backward search
6536 until the time we really need it, in next_element_from_buffer. */
6537 if (CHARPOS (pos) != it->prev_stop)
6538 it->prev_stop = CHARPOS (pos);
6539 if (CHARPOS (pos) < it->base_level_stop)
6540 it->base_level_stop = 0; /* meaning it's unknown */
6541 handle_stop (it);
6543 else
6545 handle_stop (it);
6546 it->prev_stop = it->base_level_stop = 0;
6551 CHECK_IT (it);
6555 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6556 IT->stop_pos to POS, also. */
6558 static void
6559 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6561 /* Don't call this function when scanning a C string. */
6562 eassert (it->s == NULL);
6564 /* POS must be a reasonable value. */
6565 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6567 it->current.pos = it->position = pos;
6568 it->end_charpos = ZV;
6569 it->dpvec = NULL;
6570 it->current.dpvec_index = -1;
6571 it->current.overlay_string_index = -1;
6572 IT_STRING_CHARPOS (*it) = -1;
6573 IT_STRING_BYTEPOS (*it) = -1;
6574 it->string = Qnil;
6575 it->method = GET_FROM_BUFFER;
6576 it->object = it->w->contents;
6577 it->area = TEXT_AREA;
6578 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6579 it->sp = 0;
6580 it->string_from_display_prop_p = 0;
6581 it->string_from_prefix_prop_p = 0;
6583 it->from_disp_prop_p = 0;
6584 it->face_before_selective_p = 0;
6585 if (it->bidi_p)
6587 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6588 &it->bidi_it);
6589 bidi_unshelve_cache (NULL, 0);
6590 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6591 it->bidi_it.string.s = NULL;
6592 it->bidi_it.string.lstring = Qnil;
6593 it->bidi_it.string.bufpos = 0;
6594 it->bidi_it.string.from_disp_str = 0;
6595 it->bidi_it.string.unibyte = 0;
6596 it->bidi_it.w = it->w;
6599 if (set_stop_p)
6601 it->stop_charpos = CHARPOS (pos);
6602 it->base_level_stop = CHARPOS (pos);
6604 /* This make the information stored in it->cmp_it invalidate. */
6605 it->cmp_it.id = -1;
6609 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6610 If S is non-null, it is a C string to iterate over. Otherwise,
6611 STRING gives a Lisp string to iterate over.
6613 If PRECISION > 0, don't return more then PRECISION number of
6614 characters from the string.
6616 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6617 characters have been returned. FIELD_WIDTH < 0 means an infinite
6618 field width.
6620 MULTIBYTE = 0 means disable processing of multibyte characters,
6621 MULTIBYTE > 0 means enable it,
6622 MULTIBYTE < 0 means use IT->multibyte_p.
6624 IT must be initialized via a prior call to init_iterator before
6625 calling this function. */
6627 static void
6628 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6629 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6630 int multibyte)
6632 /* No text property checks performed by default, but see below. */
6633 it->stop_charpos = -1;
6635 /* Set iterator position and end position. */
6636 memset (&it->current, 0, sizeof it->current);
6637 it->current.overlay_string_index = -1;
6638 it->current.dpvec_index = -1;
6639 eassert (charpos >= 0);
6641 /* If STRING is specified, use its multibyteness, otherwise use the
6642 setting of MULTIBYTE, if specified. */
6643 if (multibyte >= 0)
6644 it->multibyte_p = multibyte > 0;
6646 /* Bidirectional reordering of strings is controlled by the default
6647 value of bidi-display-reordering. Don't try to reorder while
6648 loading loadup.el, as the necessary character property tables are
6649 not yet available. */
6650 it->bidi_p =
6651 NILP (Vpurify_flag)
6652 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6654 if (s == NULL)
6656 eassert (STRINGP (string));
6657 it->string = string;
6658 it->s = NULL;
6659 it->end_charpos = it->string_nchars = SCHARS (string);
6660 it->method = GET_FROM_STRING;
6661 it->current.string_pos = string_pos (charpos, string);
6663 if (it->bidi_p)
6665 it->bidi_it.string.lstring = string;
6666 it->bidi_it.string.s = NULL;
6667 it->bidi_it.string.schars = it->end_charpos;
6668 it->bidi_it.string.bufpos = 0;
6669 it->bidi_it.string.from_disp_str = 0;
6670 it->bidi_it.string.unibyte = !it->multibyte_p;
6671 it->bidi_it.w = it->w;
6672 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6673 FRAME_WINDOW_P (it->f), &it->bidi_it);
6676 else
6678 it->s = (const unsigned char *) s;
6679 it->string = Qnil;
6681 /* Note that we use IT->current.pos, not it->current.string_pos,
6682 for displaying C strings. */
6683 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6684 if (it->multibyte_p)
6686 it->current.pos = c_string_pos (charpos, s, 1);
6687 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6689 else
6691 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6692 it->end_charpos = it->string_nchars = strlen (s);
6695 if (it->bidi_p)
6697 it->bidi_it.string.lstring = Qnil;
6698 it->bidi_it.string.s = (const unsigned char *) s;
6699 it->bidi_it.string.schars = it->end_charpos;
6700 it->bidi_it.string.bufpos = 0;
6701 it->bidi_it.string.from_disp_str = 0;
6702 it->bidi_it.string.unibyte = !it->multibyte_p;
6703 it->bidi_it.w = it->w;
6704 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6705 &it->bidi_it);
6707 it->method = GET_FROM_C_STRING;
6710 /* PRECISION > 0 means don't return more than PRECISION characters
6711 from the string. */
6712 if (precision > 0 && it->end_charpos - charpos > precision)
6714 it->end_charpos = it->string_nchars = charpos + precision;
6715 if (it->bidi_p)
6716 it->bidi_it.string.schars = it->end_charpos;
6719 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6720 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6721 FIELD_WIDTH < 0 means infinite field width. This is useful for
6722 padding with `-' at the end of a mode line. */
6723 if (field_width < 0)
6724 field_width = INFINITY;
6725 /* Implementation note: We deliberately don't enlarge
6726 it->bidi_it.string.schars here to fit it->end_charpos, because
6727 the bidi iterator cannot produce characters out of thin air. */
6728 if (field_width > it->end_charpos - charpos)
6729 it->end_charpos = charpos + field_width;
6731 /* Use the standard display table for displaying strings. */
6732 if (DISP_TABLE_P (Vstandard_display_table))
6733 it->dp = XCHAR_TABLE (Vstandard_display_table);
6735 it->stop_charpos = charpos;
6736 it->prev_stop = charpos;
6737 it->base_level_stop = 0;
6738 if (it->bidi_p)
6740 it->bidi_it.first_elt = 1;
6741 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6742 it->bidi_it.disp_pos = -1;
6744 if (s == NULL && it->multibyte_p)
6746 ptrdiff_t endpos = SCHARS (it->string);
6747 if (endpos > it->end_charpos)
6748 endpos = it->end_charpos;
6749 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6750 it->string);
6752 CHECK_IT (it);
6757 /***********************************************************************
6758 Iteration
6759 ***********************************************************************/
6761 /* Map enum it_method value to corresponding next_element_from_* function. */
6763 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6765 next_element_from_buffer,
6766 next_element_from_display_vector,
6767 next_element_from_string,
6768 next_element_from_c_string,
6769 next_element_from_image,
6770 next_element_from_stretch
6773 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6776 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6777 (possibly with the following characters). */
6779 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6780 ((IT)->cmp_it.id >= 0 \
6781 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6782 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6783 END_CHARPOS, (IT)->w, \
6784 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6785 (IT)->string)))
6788 /* Lookup the char-table Vglyphless_char_display for character C (-1
6789 if we want information for no-font case), and return the display
6790 method symbol. By side-effect, update it->what and
6791 it->glyphless_method. This function is called from
6792 get_next_display_element for each character element, and from
6793 x_produce_glyphs when no suitable font was found. */
6795 Lisp_Object
6796 lookup_glyphless_char_display (int c, struct it *it)
6798 Lisp_Object glyphless_method = Qnil;
6800 if (CHAR_TABLE_P (Vglyphless_char_display)
6801 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6803 if (c >= 0)
6805 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6806 if (CONSP (glyphless_method))
6807 glyphless_method = FRAME_WINDOW_P (it->f)
6808 ? XCAR (glyphless_method)
6809 : XCDR (glyphless_method);
6811 else
6812 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6815 retry:
6816 if (NILP (glyphless_method))
6818 if (c >= 0)
6819 /* The default is to display the character by a proper font. */
6820 return Qnil;
6821 /* The default for the no-font case is to display an empty box. */
6822 glyphless_method = Qempty_box;
6824 if (EQ (glyphless_method, Qzero_width))
6826 if (c >= 0)
6827 return glyphless_method;
6828 /* This method can't be used for the no-font case. */
6829 glyphless_method = Qempty_box;
6831 if (EQ (glyphless_method, Qthin_space))
6832 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6833 else if (EQ (glyphless_method, Qempty_box))
6834 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6835 else if (EQ (glyphless_method, Qhex_code))
6836 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6837 else if (STRINGP (glyphless_method))
6838 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6839 else
6841 /* Invalid value. We use the default method. */
6842 glyphless_method = Qnil;
6843 goto retry;
6845 it->what = IT_GLYPHLESS;
6846 return glyphless_method;
6849 /* Merge escape glyph face and cache the result. */
6851 static struct frame *last_escape_glyph_frame = NULL;
6852 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6853 static int last_escape_glyph_merged_face_id = 0;
6855 static int
6856 merge_escape_glyph_face (struct it *it)
6858 int face_id;
6860 if (it->f == last_escape_glyph_frame
6861 && it->face_id == last_escape_glyph_face_id)
6862 face_id = last_escape_glyph_merged_face_id;
6863 else
6865 /* Merge the `escape-glyph' face into the current face. */
6866 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6867 last_escape_glyph_frame = it->f;
6868 last_escape_glyph_face_id = it->face_id;
6869 last_escape_glyph_merged_face_id = face_id;
6871 return face_id;
6874 /* Likewise for glyphless glyph face. */
6876 static struct frame *last_glyphless_glyph_frame = NULL;
6877 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6878 static int last_glyphless_glyph_merged_face_id = 0;
6881 merge_glyphless_glyph_face (struct it *it)
6883 int face_id;
6885 if (it->f == last_glyphless_glyph_frame
6886 && it->face_id == last_glyphless_glyph_face_id)
6887 face_id = last_glyphless_glyph_merged_face_id;
6888 else
6890 /* Merge the `glyphless-char' face into the current face. */
6891 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6892 last_glyphless_glyph_frame = it->f;
6893 last_glyphless_glyph_face_id = it->face_id;
6894 last_glyphless_glyph_merged_face_id = face_id;
6896 return face_id;
6899 /* Load IT's display element fields with information about the next
6900 display element from the current position of IT. Value is zero if
6901 end of buffer (or C string) is reached. */
6903 static int
6904 get_next_display_element (struct it *it)
6906 /* Non-zero means that we found a display element. Zero means that
6907 we hit the end of what we iterate over. Performance note: the
6908 function pointer `method' used here turns out to be faster than
6909 using a sequence of if-statements. */
6910 int success_p;
6912 get_next:
6913 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6915 if (it->what == IT_CHARACTER)
6917 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6918 and only if (a) the resolved directionality of that character
6919 is R..." */
6920 /* FIXME: Do we need an exception for characters from display
6921 tables? */
6922 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6923 it->c = bidi_mirror_char (it->c);
6924 /* Map via display table or translate control characters.
6925 IT->c, IT->len etc. have been set to the next character by
6926 the function call above. If we have a display table, and it
6927 contains an entry for IT->c, translate it. Don't do this if
6928 IT->c itself comes from a display table, otherwise we could
6929 end up in an infinite recursion. (An alternative could be to
6930 count the recursion depth of this function and signal an
6931 error when a certain maximum depth is reached.) Is it worth
6932 it? */
6933 if (success_p && it->dpvec == NULL)
6935 Lisp_Object dv;
6936 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6937 int nonascii_space_p = 0;
6938 int nonascii_hyphen_p = 0;
6939 int c = it->c; /* This is the character to display. */
6941 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6943 eassert (SINGLE_BYTE_CHAR_P (c));
6944 if (unibyte_display_via_language_environment)
6946 c = DECODE_CHAR (unibyte, c);
6947 if (c < 0)
6948 c = BYTE8_TO_CHAR (it->c);
6950 else
6951 c = BYTE8_TO_CHAR (it->c);
6954 if (it->dp
6955 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6956 VECTORP (dv)))
6958 struct Lisp_Vector *v = XVECTOR (dv);
6960 /* Return the first character from the display table
6961 entry, if not empty. If empty, don't display the
6962 current character. */
6963 if (v->header.size)
6965 it->dpvec_char_len = it->len;
6966 it->dpvec = v->contents;
6967 it->dpend = v->contents + v->header.size;
6968 it->current.dpvec_index = 0;
6969 it->dpvec_face_id = -1;
6970 it->saved_face_id = it->face_id;
6971 it->method = GET_FROM_DISPLAY_VECTOR;
6972 it->ellipsis_p = 0;
6974 else
6976 set_iterator_to_next (it, 0);
6978 goto get_next;
6981 if (! NILP (lookup_glyphless_char_display (c, it)))
6983 if (it->what == IT_GLYPHLESS)
6984 goto done;
6985 /* Don't display this character. */
6986 set_iterator_to_next (it, 0);
6987 goto get_next;
6990 /* If `nobreak-char-display' is non-nil, we display
6991 non-ASCII spaces and hyphens specially. */
6992 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6994 if (c == 0xA0)
6995 nonascii_space_p = true;
6996 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6997 nonascii_hyphen_p = true;
7000 /* Translate control characters into `\003' or `^C' form.
7001 Control characters coming from a display table entry are
7002 currently not translated because we use IT->dpvec to hold
7003 the translation. This could easily be changed but I
7004 don't believe that it is worth doing.
7006 The characters handled by `nobreak-char-display' must be
7007 translated too.
7009 Non-printable characters and raw-byte characters are also
7010 translated to octal form. */
7011 if (((c < ' ' || c == 127) /* ASCII control chars. */
7012 ? (it->area != TEXT_AREA
7013 /* In mode line, treat \n, \t like other crl chars. */
7014 || (c != '\t'
7015 && it->glyph_row
7016 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7017 || (c != '\n' && c != '\t'))
7018 : (nonascii_space_p
7019 || nonascii_hyphen_p
7020 || CHAR_BYTE8_P (c)
7021 || ! CHAR_PRINTABLE_P (c))))
7023 /* C is a control character, non-ASCII space/hyphen,
7024 raw-byte, or a non-printable character which must be
7025 displayed either as '\003' or as `^C' where the '\\'
7026 and '^' can be defined in the display table. Fill
7027 IT->ctl_chars with glyphs for what we have to
7028 display. Then, set IT->dpvec to these glyphs. */
7029 Lisp_Object gc;
7030 int ctl_len;
7031 int face_id;
7032 int lface_id = 0;
7033 int escape_glyph;
7035 /* Handle control characters with ^. */
7037 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7039 int g;
7041 g = '^'; /* default glyph for Control */
7042 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7043 if (it->dp
7044 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7046 g = GLYPH_CODE_CHAR (gc);
7047 lface_id = GLYPH_CODE_FACE (gc);
7050 face_id = (lface_id
7051 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7052 : merge_escape_glyph_face (it));
7054 XSETINT (it->ctl_chars[0], g);
7055 XSETINT (it->ctl_chars[1], c ^ 0100);
7056 ctl_len = 2;
7057 goto display_control;
7060 /* Handle non-ascii space in the mode where it only gets
7061 highlighting. */
7063 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7065 /* Merge `nobreak-space' into the current face. */
7066 face_id = merge_faces (it->f, Qnobreak_space, 0,
7067 it->face_id);
7068 XSETINT (it->ctl_chars[0], ' ');
7069 ctl_len = 1;
7070 goto display_control;
7073 /* Handle sequences that start with the "escape glyph". */
7075 /* the default escape glyph is \. */
7076 escape_glyph = '\\';
7078 if (it->dp
7079 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7081 escape_glyph = GLYPH_CODE_CHAR (gc);
7082 lface_id = GLYPH_CODE_FACE (gc);
7085 face_id = (lface_id
7086 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7087 : merge_escape_glyph_face (it));
7089 /* Draw non-ASCII hyphen with just highlighting: */
7091 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7093 XSETINT (it->ctl_chars[0], '-');
7094 ctl_len = 1;
7095 goto display_control;
7098 /* Draw non-ASCII space/hyphen with escape glyph: */
7100 if (nonascii_space_p || nonascii_hyphen_p)
7102 XSETINT (it->ctl_chars[0], escape_glyph);
7103 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7104 ctl_len = 2;
7105 goto display_control;
7109 char str[10];
7110 int len, i;
7112 if (CHAR_BYTE8_P (c))
7113 /* Display \200 instead of \17777600. */
7114 c = CHAR_TO_BYTE8 (c);
7115 len = sprintf (str, "%03o", c);
7117 XSETINT (it->ctl_chars[0], escape_glyph);
7118 for (i = 0; i < len; i++)
7119 XSETINT (it->ctl_chars[i + 1], str[i]);
7120 ctl_len = len + 1;
7123 display_control:
7124 /* Set up IT->dpvec and return first character from it. */
7125 it->dpvec_char_len = it->len;
7126 it->dpvec = it->ctl_chars;
7127 it->dpend = it->dpvec + ctl_len;
7128 it->current.dpvec_index = 0;
7129 it->dpvec_face_id = face_id;
7130 it->saved_face_id = it->face_id;
7131 it->method = GET_FROM_DISPLAY_VECTOR;
7132 it->ellipsis_p = 0;
7133 goto get_next;
7135 it->char_to_display = c;
7137 else if (success_p)
7139 it->char_to_display = it->c;
7143 #ifdef HAVE_WINDOW_SYSTEM
7144 /* Adjust face id for a multibyte character. There are no multibyte
7145 character in unibyte text. */
7146 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7147 && it->multibyte_p
7148 && success_p
7149 && FRAME_WINDOW_P (it->f))
7151 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7153 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7155 /* Automatic composition with glyph-string. */
7156 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7158 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7160 else
7162 ptrdiff_t pos = (it->s ? -1
7163 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7164 : IT_CHARPOS (*it));
7165 int c;
7167 if (it->what == IT_CHARACTER)
7168 c = it->char_to_display;
7169 else
7171 struct composition *cmp = composition_table[it->cmp_it.id];
7172 int i;
7174 c = ' ';
7175 for (i = 0; i < cmp->glyph_len; i++)
7176 /* TAB in a composition means display glyphs with
7177 padding space on the left or right. */
7178 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7179 break;
7181 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7184 #endif /* HAVE_WINDOW_SYSTEM */
7186 done:
7187 /* Is this character the last one of a run of characters with
7188 box? If yes, set IT->end_of_box_run_p to 1. */
7189 if (it->face_box_p
7190 && it->s == NULL)
7192 if (it->method == GET_FROM_STRING && it->sp)
7194 int face_id = underlying_face_id (it);
7195 struct face *face = FACE_FROM_ID (it->f, face_id);
7197 if (face)
7199 if (face->box == FACE_NO_BOX)
7201 /* If the box comes from face properties in a
7202 display string, check faces in that string. */
7203 int string_face_id = face_after_it_pos (it);
7204 it->end_of_box_run_p
7205 = (FACE_FROM_ID (it->f, string_face_id)->box
7206 == FACE_NO_BOX);
7208 /* Otherwise, the box comes from the underlying face.
7209 If this is the last string character displayed, check
7210 the next buffer location. */
7211 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7212 /* n_overlay_strings is unreliable unless
7213 overlay_string_index is non-negative. */
7214 && ((it->current.overlay_string_index >= 0
7215 && (it->current.overlay_string_index
7216 == it->n_overlay_strings - 1))
7217 /* A string from display property. */
7218 || it->from_disp_prop_p))
7220 ptrdiff_t ignore;
7221 int next_face_id;
7222 struct text_pos pos = it->current.pos;
7224 /* For a string from a display property, the next
7225 buffer position is stored in the 'position'
7226 member of the iteration stack slot below the
7227 current one, see handle_single_display_spec. By
7228 contrast, it->current.pos was is not yet updated
7229 to point to that buffer position; that will
7230 happen in pop_it, after we finish displaying the
7231 current string. Note that we already checked
7232 above that it->sp is positive, so subtracting one
7233 from it is safe. */
7234 if (it->from_disp_prop_p)
7235 pos = (it->stack + it->sp - 1)->position;
7236 else
7237 INC_TEXT_POS (pos, it->multibyte_p);
7239 if (CHARPOS (pos) >= ZV)
7240 it->end_of_box_run_p = true;
7241 else
7243 next_face_id = face_at_buffer_position
7244 (it->w, CHARPOS (pos), &ignore,
7245 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7246 it->end_of_box_run_p
7247 = (FACE_FROM_ID (it->f, next_face_id)->box
7248 == FACE_NO_BOX);
7253 /* next_element_from_display_vector sets this flag according to
7254 faces of the display vector glyphs, see there. */
7255 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7257 int face_id = face_after_it_pos (it);
7258 it->end_of_box_run_p
7259 = (face_id != it->face_id
7260 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7263 /* If we reached the end of the object we've been iterating (e.g., a
7264 display string or an overlay string), and there's something on
7265 IT->stack, proceed with what's on the stack. It doesn't make
7266 sense to return zero if there's unprocessed stuff on the stack,
7267 because otherwise that stuff will never be displayed. */
7268 if (!success_p && it->sp > 0)
7270 set_iterator_to_next (it, 0);
7271 success_p = get_next_display_element (it);
7274 /* Value is 0 if end of buffer or string reached. */
7275 return success_p;
7279 /* Move IT to the next display element.
7281 RESEAT_P non-zero means if called on a newline in buffer text,
7282 skip to the next visible line start.
7284 Functions get_next_display_element and set_iterator_to_next are
7285 separate because I find this arrangement easier to handle than a
7286 get_next_display_element function that also increments IT's
7287 position. The way it is we can first look at an iterator's current
7288 display element, decide whether it fits on a line, and if it does,
7289 increment the iterator position. The other way around we probably
7290 would either need a flag indicating whether the iterator has to be
7291 incremented the next time, or we would have to implement a
7292 decrement position function which would not be easy to write. */
7294 void
7295 set_iterator_to_next (struct it *it, int reseat_p)
7297 /* Reset flags indicating start and end of a sequence of characters
7298 with box. Reset them at the start of this function because
7299 moving the iterator to a new position might set them. */
7300 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7302 switch (it->method)
7304 case GET_FROM_BUFFER:
7305 /* The current display element of IT is a character from
7306 current_buffer. Advance in the buffer, and maybe skip over
7307 invisible lines that are so because of selective display. */
7308 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7309 reseat_at_next_visible_line_start (it, 0);
7310 else if (it->cmp_it.id >= 0)
7312 /* We are currently getting glyphs from a composition. */
7313 int i;
7315 if (! it->bidi_p)
7317 IT_CHARPOS (*it) += it->cmp_it.nchars;
7318 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7319 if (it->cmp_it.to < it->cmp_it.nglyphs)
7321 it->cmp_it.from = it->cmp_it.to;
7323 else
7325 it->cmp_it.id = -1;
7326 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7327 IT_BYTEPOS (*it),
7328 it->end_charpos, Qnil);
7331 else if (! it->cmp_it.reversed_p)
7333 /* Composition created while scanning forward. */
7334 /* Update IT's char/byte positions to point to the first
7335 character of the next grapheme cluster, or to the
7336 character visually after the current composition. */
7337 for (i = 0; i < it->cmp_it.nchars; i++)
7338 bidi_move_to_visually_next (&it->bidi_it);
7339 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7340 IT_CHARPOS (*it) = it->bidi_it.charpos;
7342 if (it->cmp_it.to < it->cmp_it.nglyphs)
7344 /* Proceed to the next grapheme cluster. */
7345 it->cmp_it.from = it->cmp_it.to;
7347 else
7349 /* No more grapheme clusters in this composition.
7350 Find the next stop position. */
7351 ptrdiff_t stop = it->end_charpos;
7352 if (it->bidi_it.scan_dir < 0)
7353 /* Now we are scanning backward and don't know
7354 where to stop. */
7355 stop = -1;
7356 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7357 IT_BYTEPOS (*it), stop, Qnil);
7360 else
7362 /* Composition created while scanning backward. */
7363 /* Update IT's char/byte positions to point to the last
7364 character of the previous grapheme cluster, or the
7365 character visually after the current composition. */
7366 for (i = 0; i < it->cmp_it.nchars; i++)
7367 bidi_move_to_visually_next (&it->bidi_it);
7368 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7369 IT_CHARPOS (*it) = it->bidi_it.charpos;
7370 if (it->cmp_it.from > 0)
7372 /* Proceed to the previous grapheme cluster. */
7373 it->cmp_it.to = it->cmp_it.from;
7375 else
7377 /* No more grapheme clusters in this composition.
7378 Find the next stop position. */
7379 ptrdiff_t stop = it->end_charpos;
7380 if (it->bidi_it.scan_dir < 0)
7381 /* Now we are scanning backward and don't know
7382 where to stop. */
7383 stop = -1;
7384 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7385 IT_BYTEPOS (*it), stop, Qnil);
7389 else
7391 eassert (it->len != 0);
7393 if (!it->bidi_p)
7395 IT_BYTEPOS (*it) += it->len;
7396 IT_CHARPOS (*it) += 1;
7398 else
7400 int prev_scan_dir = it->bidi_it.scan_dir;
7401 /* If this is a new paragraph, determine its base
7402 direction (a.k.a. its base embedding level). */
7403 if (it->bidi_it.new_paragraph)
7404 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7405 bidi_move_to_visually_next (&it->bidi_it);
7406 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7407 IT_CHARPOS (*it) = it->bidi_it.charpos;
7408 if (prev_scan_dir != it->bidi_it.scan_dir)
7410 /* As the scan direction was changed, we must
7411 re-compute the stop position for composition. */
7412 ptrdiff_t stop = it->end_charpos;
7413 if (it->bidi_it.scan_dir < 0)
7414 stop = -1;
7415 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7416 IT_BYTEPOS (*it), stop, Qnil);
7419 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7421 break;
7423 case GET_FROM_C_STRING:
7424 /* Current display element of IT is from a C string. */
7425 if (!it->bidi_p
7426 /* If the string position is beyond string's end, it means
7427 next_element_from_c_string is padding the string with
7428 blanks, in which case we bypass the bidi iterator,
7429 because it cannot deal with such virtual characters. */
7430 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7432 IT_BYTEPOS (*it) += it->len;
7433 IT_CHARPOS (*it) += 1;
7435 else
7437 bidi_move_to_visually_next (&it->bidi_it);
7438 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7439 IT_CHARPOS (*it) = it->bidi_it.charpos;
7441 break;
7443 case GET_FROM_DISPLAY_VECTOR:
7444 /* Current display element of IT is from a display table entry.
7445 Advance in the display table definition. Reset it to null if
7446 end reached, and continue with characters from buffers/
7447 strings. */
7448 ++it->current.dpvec_index;
7450 /* Restore face of the iterator to what they were before the
7451 display vector entry (these entries may contain faces). */
7452 it->face_id = it->saved_face_id;
7454 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7456 int recheck_faces = it->ellipsis_p;
7458 if (it->s)
7459 it->method = GET_FROM_C_STRING;
7460 else if (STRINGP (it->string))
7461 it->method = GET_FROM_STRING;
7462 else
7464 it->method = GET_FROM_BUFFER;
7465 it->object = it->w->contents;
7468 it->dpvec = NULL;
7469 it->current.dpvec_index = -1;
7471 /* Skip over characters which were displayed via IT->dpvec. */
7472 if (it->dpvec_char_len < 0)
7473 reseat_at_next_visible_line_start (it, 1);
7474 else if (it->dpvec_char_len > 0)
7476 if (it->method == GET_FROM_STRING
7477 && it->current.overlay_string_index >= 0
7478 && it->n_overlay_strings > 0)
7479 it->ignore_overlay_strings_at_pos_p = true;
7480 it->len = it->dpvec_char_len;
7481 set_iterator_to_next (it, reseat_p);
7484 /* Maybe recheck faces after display vector. */
7485 if (recheck_faces)
7486 it->stop_charpos = IT_CHARPOS (*it);
7488 break;
7490 case GET_FROM_STRING:
7491 /* Current display element is a character from a Lisp string. */
7492 eassert (it->s == NULL && STRINGP (it->string));
7493 /* Don't advance past string end. These conditions are true
7494 when set_iterator_to_next is called at the end of
7495 get_next_display_element, in which case the Lisp string is
7496 already exhausted, and all we want is pop the iterator
7497 stack. */
7498 if (it->current.overlay_string_index >= 0)
7500 /* This is an overlay string, so there's no padding with
7501 spaces, and the number of characters in the string is
7502 where the string ends. */
7503 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7504 goto consider_string_end;
7506 else
7508 /* Not an overlay string. There could be padding, so test
7509 against it->end_charpos. */
7510 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7511 goto consider_string_end;
7513 if (it->cmp_it.id >= 0)
7515 int i;
7517 if (! it->bidi_p)
7519 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7520 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7521 if (it->cmp_it.to < it->cmp_it.nglyphs)
7522 it->cmp_it.from = it->cmp_it.to;
7523 else
7525 it->cmp_it.id = -1;
7526 composition_compute_stop_pos (&it->cmp_it,
7527 IT_STRING_CHARPOS (*it),
7528 IT_STRING_BYTEPOS (*it),
7529 it->end_charpos, it->string);
7532 else if (! it->cmp_it.reversed_p)
7534 for (i = 0; i < it->cmp_it.nchars; i++)
7535 bidi_move_to_visually_next (&it->bidi_it);
7536 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7537 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7539 if (it->cmp_it.to < it->cmp_it.nglyphs)
7540 it->cmp_it.from = it->cmp_it.to;
7541 else
7543 ptrdiff_t stop = it->end_charpos;
7544 if (it->bidi_it.scan_dir < 0)
7545 stop = -1;
7546 composition_compute_stop_pos (&it->cmp_it,
7547 IT_STRING_CHARPOS (*it),
7548 IT_STRING_BYTEPOS (*it), stop,
7549 it->string);
7552 else
7554 for (i = 0; i < it->cmp_it.nchars; i++)
7555 bidi_move_to_visually_next (&it->bidi_it);
7556 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7557 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7558 if (it->cmp_it.from > 0)
7559 it->cmp_it.to = it->cmp_it.from;
7560 else
7562 ptrdiff_t stop = it->end_charpos;
7563 if (it->bidi_it.scan_dir < 0)
7564 stop = -1;
7565 composition_compute_stop_pos (&it->cmp_it,
7566 IT_STRING_CHARPOS (*it),
7567 IT_STRING_BYTEPOS (*it), stop,
7568 it->string);
7572 else
7574 if (!it->bidi_p
7575 /* If the string position is beyond string's end, it
7576 means next_element_from_string is padding the string
7577 with blanks, in which case we bypass the bidi
7578 iterator, because it cannot deal with such virtual
7579 characters. */
7580 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7582 IT_STRING_BYTEPOS (*it) += it->len;
7583 IT_STRING_CHARPOS (*it) += 1;
7585 else
7587 int prev_scan_dir = it->bidi_it.scan_dir;
7589 bidi_move_to_visually_next (&it->bidi_it);
7590 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7591 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7592 if (prev_scan_dir != it->bidi_it.scan_dir)
7594 ptrdiff_t stop = it->end_charpos;
7596 if (it->bidi_it.scan_dir < 0)
7597 stop = -1;
7598 composition_compute_stop_pos (&it->cmp_it,
7599 IT_STRING_CHARPOS (*it),
7600 IT_STRING_BYTEPOS (*it), stop,
7601 it->string);
7606 consider_string_end:
7608 if (it->current.overlay_string_index >= 0)
7610 /* IT->string is an overlay string. Advance to the
7611 next, if there is one. */
7612 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7614 it->ellipsis_p = 0;
7615 next_overlay_string (it);
7616 if (it->ellipsis_p)
7617 setup_for_ellipsis (it, 0);
7620 else
7622 /* IT->string is not an overlay string. If we reached
7623 its end, and there is something on IT->stack, proceed
7624 with what is on the stack. This can be either another
7625 string, this time an overlay string, or a buffer. */
7626 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7627 && it->sp > 0)
7629 pop_it (it);
7630 if (it->method == GET_FROM_STRING)
7631 goto consider_string_end;
7634 break;
7636 case GET_FROM_IMAGE:
7637 case GET_FROM_STRETCH:
7638 /* The position etc with which we have to proceed are on
7639 the stack. The position may be at the end of a string,
7640 if the `display' property takes up the whole string. */
7641 eassert (it->sp > 0);
7642 pop_it (it);
7643 if (it->method == GET_FROM_STRING)
7644 goto consider_string_end;
7645 break;
7647 default:
7648 /* There are no other methods defined, so this should be a bug. */
7649 emacs_abort ();
7652 eassert (it->method != GET_FROM_STRING
7653 || (STRINGP (it->string)
7654 && IT_STRING_CHARPOS (*it) >= 0));
7657 /* Load IT's display element fields with information about the next
7658 display element which comes from a display table entry or from the
7659 result of translating a control character to one of the forms `^C'
7660 or `\003'.
7662 IT->dpvec holds the glyphs to return as characters.
7663 IT->saved_face_id holds the face id before the display vector--it
7664 is restored into IT->face_id in set_iterator_to_next. */
7666 static int
7667 next_element_from_display_vector (struct it *it)
7669 Lisp_Object gc;
7670 int prev_face_id = it->face_id;
7671 int next_face_id;
7673 /* Precondition. */
7674 eassert (it->dpvec && it->current.dpvec_index >= 0);
7676 it->face_id = it->saved_face_id;
7678 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7679 That seemed totally bogus - so I changed it... */
7680 gc = it->dpvec[it->current.dpvec_index];
7682 if (GLYPH_CODE_P (gc))
7684 struct face *this_face, *prev_face, *next_face;
7686 it->c = GLYPH_CODE_CHAR (gc);
7687 it->len = CHAR_BYTES (it->c);
7689 /* The entry may contain a face id to use. Such a face id is
7690 the id of a Lisp face, not a realized face. A face id of
7691 zero means no face is specified. */
7692 if (it->dpvec_face_id >= 0)
7693 it->face_id = it->dpvec_face_id;
7694 else
7696 int lface_id = GLYPH_CODE_FACE (gc);
7697 if (lface_id > 0)
7698 it->face_id = merge_faces (it->f, Qt, lface_id,
7699 it->saved_face_id);
7702 /* Glyphs in the display vector could have the box face, so we
7703 need to set the related flags in the iterator, as
7704 appropriate. */
7705 this_face = FACE_FROM_ID (it->f, it->face_id);
7706 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7708 /* Is this character the first character of a box-face run? */
7709 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7710 && (!prev_face
7711 || prev_face->box == FACE_NO_BOX));
7713 /* For the last character of the box-face run, we need to look
7714 either at the next glyph from the display vector, or at the
7715 face we saw before the display vector. */
7716 next_face_id = it->saved_face_id;
7717 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7719 if (it->dpvec_face_id >= 0)
7720 next_face_id = it->dpvec_face_id;
7721 else
7723 int lface_id =
7724 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7726 if (lface_id > 0)
7727 next_face_id = merge_faces (it->f, Qt, lface_id,
7728 it->saved_face_id);
7731 next_face = FACE_FROM_ID (it->f, next_face_id);
7732 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7733 && (!next_face
7734 || next_face->box == FACE_NO_BOX));
7735 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7737 else
7738 /* Display table entry is invalid. Return a space. */
7739 it->c = ' ', it->len = 1;
7741 /* Don't change position and object of the iterator here. They are
7742 still the values of the character that had this display table
7743 entry or was translated, and that's what we want. */
7744 it->what = IT_CHARACTER;
7745 return 1;
7748 /* Get the first element of string/buffer in the visual order, after
7749 being reseated to a new position in a string or a buffer. */
7750 static void
7751 get_visually_first_element (struct it *it)
7753 int string_p = STRINGP (it->string) || it->s;
7754 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7755 ptrdiff_t bob = (string_p ? 0 : BEGV);
7757 if (STRINGP (it->string))
7759 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7760 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7762 else
7764 it->bidi_it.charpos = IT_CHARPOS (*it);
7765 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7768 if (it->bidi_it.charpos == eob)
7770 /* Nothing to do, but reset the FIRST_ELT flag, like
7771 bidi_paragraph_init does, because we are not going to
7772 call it. */
7773 it->bidi_it.first_elt = 0;
7775 else if (it->bidi_it.charpos == bob
7776 || (!string_p
7777 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7778 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7780 /* If we are at the beginning of a line/string, we can produce
7781 the next element right away. */
7782 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7783 bidi_move_to_visually_next (&it->bidi_it);
7785 else
7787 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7789 /* We need to prime the bidi iterator starting at the line's or
7790 string's beginning, before we will be able to produce the
7791 next element. */
7792 if (string_p)
7793 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7794 else
7795 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7796 IT_BYTEPOS (*it), -1,
7797 &it->bidi_it.bytepos);
7798 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7801 /* Now return to buffer/string position where we were asked
7802 to get the next display element, and produce that. */
7803 bidi_move_to_visually_next (&it->bidi_it);
7805 while (it->bidi_it.bytepos != orig_bytepos
7806 && it->bidi_it.charpos < eob);
7809 /* Adjust IT's position information to where we ended up. */
7810 if (STRINGP (it->string))
7812 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7813 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7815 else
7817 IT_CHARPOS (*it) = it->bidi_it.charpos;
7818 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7821 if (STRINGP (it->string) || !it->s)
7823 ptrdiff_t stop, charpos, bytepos;
7825 if (STRINGP (it->string))
7827 eassert (!it->s);
7828 stop = SCHARS (it->string);
7829 if (stop > it->end_charpos)
7830 stop = it->end_charpos;
7831 charpos = IT_STRING_CHARPOS (*it);
7832 bytepos = IT_STRING_BYTEPOS (*it);
7834 else
7836 stop = it->end_charpos;
7837 charpos = IT_CHARPOS (*it);
7838 bytepos = IT_BYTEPOS (*it);
7840 if (it->bidi_it.scan_dir < 0)
7841 stop = -1;
7842 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7843 it->string);
7847 /* Load IT with the next display element from Lisp string IT->string.
7848 IT->current.string_pos is the current position within the string.
7849 If IT->current.overlay_string_index >= 0, the Lisp string is an
7850 overlay string. */
7852 static int
7853 next_element_from_string (struct it *it)
7855 struct text_pos position;
7857 eassert (STRINGP (it->string));
7858 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7859 eassert (IT_STRING_CHARPOS (*it) >= 0);
7860 position = it->current.string_pos;
7862 /* With bidi reordering, the character to display might not be the
7863 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7864 that we were reseat()ed to a new string, whose paragraph
7865 direction is not known. */
7866 if (it->bidi_p && it->bidi_it.first_elt)
7868 get_visually_first_element (it);
7869 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7872 /* Time to check for invisible text? */
7873 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7875 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7877 if (!(!it->bidi_p
7878 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7879 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7881 /* With bidi non-linear iteration, we could find
7882 ourselves far beyond the last computed stop_charpos,
7883 with several other stop positions in between that we
7884 missed. Scan them all now, in buffer's logical
7885 order, until we find and handle the last stop_charpos
7886 that precedes our current position. */
7887 handle_stop_backwards (it, it->stop_charpos);
7888 return GET_NEXT_DISPLAY_ELEMENT (it);
7890 else
7892 if (it->bidi_p)
7894 /* Take note of the stop position we just moved
7895 across, for when we will move back across it. */
7896 it->prev_stop = it->stop_charpos;
7897 /* If we are at base paragraph embedding level, take
7898 note of the last stop position seen at this
7899 level. */
7900 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7901 it->base_level_stop = it->stop_charpos;
7903 handle_stop (it);
7905 /* Since a handler may have changed IT->method, we must
7906 recurse here. */
7907 return GET_NEXT_DISPLAY_ELEMENT (it);
7910 else if (it->bidi_p
7911 /* If we are before prev_stop, we may have overstepped
7912 on our way backwards a stop_pos, and if so, we need
7913 to handle that stop_pos. */
7914 && IT_STRING_CHARPOS (*it) < it->prev_stop
7915 /* We can sometimes back up for reasons that have nothing
7916 to do with bidi reordering. E.g., compositions. The
7917 code below is only needed when we are above the base
7918 embedding level, so test for that explicitly. */
7919 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7921 /* If we lost track of base_level_stop, we have no better
7922 place for handle_stop_backwards to start from than string
7923 beginning. This happens, e.g., when we were reseated to
7924 the previous screenful of text by vertical-motion. */
7925 if (it->base_level_stop <= 0
7926 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7927 it->base_level_stop = 0;
7928 handle_stop_backwards (it, it->base_level_stop);
7929 return GET_NEXT_DISPLAY_ELEMENT (it);
7933 if (it->current.overlay_string_index >= 0)
7935 /* Get the next character from an overlay string. In overlay
7936 strings, there is no field width or padding with spaces to
7937 do. */
7938 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7940 it->what = IT_EOB;
7941 return 0;
7943 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7944 IT_STRING_BYTEPOS (*it),
7945 it->bidi_it.scan_dir < 0
7946 ? -1
7947 : SCHARS (it->string))
7948 && next_element_from_composition (it))
7950 return 1;
7952 else if (STRING_MULTIBYTE (it->string))
7954 const unsigned char *s = (SDATA (it->string)
7955 + IT_STRING_BYTEPOS (*it));
7956 it->c = string_char_and_length (s, &it->len);
7958 else
7960 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7961 it->len = 1;
7964 else
7966 /* Get the next character from a Lisp string that is not an
7967 overlay string. Such strings come from the mode line, for
7968 example. We may have to pad with spaces, or truncate the
7969 string. See also next_element_from_c_string. */
7970 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7972 it->what = IT_EOB;
7973 return 0;
7975 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7977 /* Pad with spaces. */
7978 it->c = ' ', it->len = 1;
7979 CHARPOS (position) = BYTEPOS (position) = -1;
7981 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7982 IT_STRING_BYTEPOS (*it),
7983 it->bidi_it.scan_dir < 0
7984 ? -1
7985 : it->string_nchars)
7986 && next_element_from_composition (it))
7988 return 1;
7990 else if (STRING_MULTIBYTE (it->string))
7992 const unsigned char *s = (SDATA (it->string)
7993 + IT_STRING_BYTEPOS (*it));
7994 it->c = string_char_and_length (s, &it->len);
7996 else
7998 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7999 it->len = 1;
8003 /* Record what we have and where it came from. */
8004 it->what = IT_CHARACTER;
8005 it->object = it->string;
8006 it->position = position;
8007 return 1;
8011 /* Load IT with next display element from C string IT->s.
8012 IT->string_nchars is the maximum number of characters to return
8013 from the string. IT->end_charpos may be greater than
8014 IT->string_nchars when this function is called, in which case we
8015 may have to return padding spaces. Value is zero if end of string
8016 reached, including padding spaces. */
8018 static int
8019 next_element_from_c_string (struct it *it)
8021 bool success_p = true;
8023 eassert (it->s);
8024 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8025 it->what = IT_CHARACTER;
8026 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8027 it->object = Qnil;
8029 /* With bidi reordering, the character to display might not be the
8030 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8031 we were reseated to a new string, whose paragraph direction is
8032 not known. */
8033 if (it->bidi_p && it->bidi_it.first_elt)
8034 get_visually_first_element (it);
8036 /* IT's position can be greater than IT->string_nchars in case a
8037 field width or precision has been specified when the iterator was
8038 initialized. */
8039 if (IT_CHARPOS (*it) >= it->end_charpos)
8041 /* End of the game. */
8042 it->what = IT_EOB;
8043 success_p = 0;
8045 else if (IT_CHARPOS (*it) >= it->string_nchars)
8047 /* Pad with spaces. */
8048 it->c = ' ', it->len = 1;
8049 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8051 else if (it->multibyte_p)
8052 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8053 else
8054 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8056 return success_p;
8060 /* Set up IT to return characters from an ellipsis, if appropriate.
8061 The definition of the ellipsis glyphs may come from a display table
8062 entry. This function fills IT with the first glyph from the
8063 ellipsis if an ellipsis is to be displayed. */
8065 static int
8066 next_element_from_ellipsis (struct it *it)
8068 if (it->selective_display_ellipsis_p)
8069 setup_for_ellipsis (it, it->len);
8070 else
8072 /* The face at the current position may be different from the
8073 face we find after the invisible text. Remember what it
8074 was in IT->saved_face_id, and signal that it's there by
8075 setting face_before_selective_p. */
8076 it->saved_face_id = it->face_id;
8077 it->method = GET_FROM_BUFFER;
8078 it->object = it->w->contents;
8079 reseat_at_next_visible_line_start (it, 1);
8080 it->face_before_selective_p = true;
8083 return GET_NEXT_DISPLAY_ELEMENT (it);
8087 /* Deliver an image display element. The iterator IT is already
8088 filled with image information (done in handle_display_prop). Value
8089 is always 1. */
8092 static int
8093 next_element_from_image (struct it *it)
8095 it->what = IT_IMAGE;
8096 it->ignore_overlay_strings_at_pos_p = 0;
8097 return 1;
8101 /* Fill iterator IT with next display element from a stretch glyph
8102 property. IT->object is the value of the text property. Value is
8103 always 1. */
8105 static int
8106 next_element_from_stretch (struct it *it)
8108 it->what = IT_STRETCH;
8109 return 1;
8112 /* Scan backwards from IT's current position until we find a stop
8113 position, or until BEGV. This is called when we find ourself
8114 before both the last known prev_stop and base_level_stop while
8115 reordering bidirectional text. */
8117 static void
8118 compute_stop_pos_backwards (struct it *it)
8120 const int SCAN_BACK_LIMIT = 1000;
8121 struct text_pos pos;
8122 struct display_pos save_current = it->current;
8123 struct text_pos save_position = it->position;
8124 ptrdiff_t charpos = IT_CHARPOS (*it);
8125 ptrdiff_t where_we_are = charpos;
8126 ptrdiff_t save_stop_pos = it->stop_charpos;
8127 ptrdiff_t save_end_pos = it->end_charpos;
8129 eassert (NILP (it->string) && !it->s);
8130 eassert (it->bidi_p);
8131 it->bidi_p = 0;
8134 it->end_charpos = min (charpos + 1, ZV);
8135 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8136 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8137 reseat_1 (it, pos, 0);
8138 compute_stop_pos (it);
8139 /* We must advance forward, right? */
8140 if (it->stop_charpos <= charpos)
8141 emacs_abort ();
8143 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8145 if (it->stop_charpos <= where_we_are)
8146 it->prev_stop = it->stop_charpos;
8147 else
8148 it->prev_stop = BEGV;
8149 it->bidi_p = true;
8150 it->current = save_current;
8151 it->position = save_position;
8152 it->stop_charpos = save_stop_pos;
8153 it->end_charpos = save_end_pos;
8156 /* Scan forward from CHARPOS in the current buffer/string, until we
8157 find a stop position > current IT's position. Then handle the stop
8158 position before that. This is called when we bump into a stop
8159 position while reordering bidirectional text. CHARPOS should be
8160 the last previously processed stop_pos (or BEGV/0, if none were
8161 processed yet) whose position is less that IT's current
8162 position. */
8164 static void
8165 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8167 int bufp = !STRINGP (it->string);
8168 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8169 struct display_pos save_current = it->current;
8170 struct text_pos save_position = it->position;
8171 struct text_pos pos1;
8172 ptrdiff_t next_stop;
8174 /* Scan in strict logical order. */
8175 eassert (it->bidi_p);
8176 it->bidi_p = 0;
8179 it->prev_stop = charpos;
8180 if (bufp)
8182 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8183 reseat_1 (it, pos1, 0);
8185 else
8186 it->current.string_pos = string_pos (charpos, it->string);
8187 compute_stop_pos (it);
8188 /* We must advance forward, right? */
8189 if (it->stop_charpos <= it->prev_stop)
8190 emacs_abort ();
8191 charpos = it->stop_charpos;
8193 while (charpos <= where_we_are);
8195 it->bidi_p = true;
8196 it->current = save_current;
8197 it->position = save_position;
8198 next_stop = it->stop_charpos;
8199 it->stop_charpos = it->prev_stop;
8200 handle_stop (it);
8201 it->stop_charpos = next_stop;
8204 /* Load IT with the next display element from current_buffer. Value
8205 is zero if end of buffer reached. IT->stop_charpos is the next
8206 position at which to stop and check for text properties or buffer
8207 end. */
8209 static int
8210 next_element_from_buffer (struct it *it)
8212 bool success_p = true;
8214 eassert (IT_CHARPOS (*it) >= BEGV);
8215 eassert (NILP (it->string) && !it->s);
8216 eassert (!it->bidi_p
8217 || (EQ (it->bidi_it.string.lstring, Qnil)
8218 && it->bidi_it.string.s == NULL));
8220 /* With bidi reordering, the character to display might not be the
8221 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8222 we were reseat()ed to a new buffer position, which is potentially
8223 a different paragraph. */
8224 if (it->bidi_p && it->bidi_it.first_elt)
8226 get_visually_first_element (it);
8227 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8230 if (IT_CHARPOS (*it) >= it->stop_charpos)
8232 if (IT_CHARPOS (*it) >= it->end_charpos)
8234 int overlay_strings_follow_p;
8236 /* End of the game, except when overlay strings follow that
8237 haven't been returned yet. */
8238 if (it->overlay_strings_at_end_processed_p)
8239 overlay_strings_follow_p = 0;
8240 else
8242 it->overlay_strings_at_end_processed_p = true;
8243 overlay_strings_follow_p = get_overlay_strings (it, 0);
8246 if (overlay_strings_follow_p)
8247 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8248 else
8250 it->what = IT_EOB;
8251 it->position = it->current.pos;
8252 success_p = 0;
8255 else if (!(!it->bidi_p
8256 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8257 || IT_CHARPOS (*it) == it->stop_charpos))
8259 /* With bidi non-linear iteration, we could find ourselves
8260 far beyond the last computed stop_charpos, with several
8261 other stop positions in between that we missed. Scan
8262 them all now, in buffer's logical order, until we find
8263 and handle the last stop_charpos that precedes our
8264 current position. */
8265 handle_stop_backwards (it, it->stop_charpos);
8266 return GET_NEXT_DISPLAY_ELEMENT (it);
8268 else
8270 if (it->bidi_p)
8272 /* Take note of the stop position we just moved across,
8273 for when we will move back across it. */
8274 it->prev_stop = it->stop_charpos;
8275 /* If we are at base paragraph embedding level, take
8276 note of the last stop position seen at this
8277 level. */
8278 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8279 it->base_level_stop = it->stop_charpos;
8281 handle_stop (it);
8282 return GET_NEXT_DISPLAY_ELEMENT (it);
8285 else if (it->bidi_p
8286 /* If we are before prev_stop, we may have overstepped on
8287 our way backwards a stop_pos, and if so, we need to
8288 handle that stop_pos. */
8289 && IT_CHARPOS (*it) < it->prev_stop
8290 /* We can sometimes back up for reasons that have nothing
8291 to do with bidi reordering. E.g., compositions. The
8292 code below is only needed when we are above the base
8293 embedding level, so test for that explicitly. */
8294 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8296 if (it->base_level_stop <= 0
8297 || IT_CHARPOS (*it) < it->base_level_stop)
8299 /* If we lost track of base_level_stop, we need to find
8300 prev_stop by looking backwards. This happens, e.g., when
8301 we were reseated to the previous screenful of text by
8302 vertical-motion. */
8303 it->base_level_stop = BEGV;
8304 compute_stop_pos_backwards (it);
8305 handle_stop_backwards (it, it->prev_stop);
8307 else
8308 handle_stop_backwards (it, it->base_level_stop);
8309 return GET_NEXT_DISPLAY_ELEMENT (it);
8311 else
8313 /* No face changes, overlays etc. in sight, so just return a
8314 character from current_buffer. */
8315 unsigned char *p;
8316 ptrdiff_t stop;
8318 /* Maybe run the redisplay end trigger hook. Performance note:
8319 This doesn't seem to cost measurable time. */
8320 if (it->redisplay_end_trigger_charpos
8321 && it->glyph_row
8322 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8323 run_redisplay_end_trigger_hook (it);
8325 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8326 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8327 stop)
8328 && next_element_from_composition (it))
8330 return 1;
8333 /* Get the next character, maybe multibyte. */
8334 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8335 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8336 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8337 else
8338 it->c = *p, it->len = 1;
8340 /* Record what we have and where it came from. */
8341 it->what = IT_CHARACTER;
8342 it->object = it->w->contents;
8343 it->position = it->current.pos;
8345 /* Normally we return the character found above, except when we
8346 really want to return an ellipsis for selective display. */
8347 if (it->selective)
8349 if (it->c == '\n')
8351 /* A value of selective > 0 means hide lines indented more
8352 than that number of columns. */
8353 if (it->selective > 0
8354 && IT_CHARPOS (*it) + 1 < ZV
8355 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8356 IT_BYTEPOS (*it) + 1,
8357 it->selective))
8359 success_p = next_element_from_ellipsis (it);
8360 it->dpvec_char_len = -1;
8363 else if (it->c == '\r' && it->selective == -1)
8365 /* A value of selective == -1 means that everything from the
8366 CR to the end of the line is invisible, with maybe an
8367 ellipsis displayed for it. */
8368 success_p = next_element_from_ellipsis (it);
8369 it->dpvec_char_len = -1;
8374 /* Value is zero if end of buffer reached. */
8375 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8376 return success_p;
8380 /* Run the redisplay end trigger hook for IT. */
8382 static void
8383 run_redisplay_end_trigger_hook (struct it *it)
8385 Lisp_Object args[3];
8387 /* IT->glyph_row should be non-null, i.e. we should be actually
8388 displaying something, or otherwise we should not run the hook. */
8389 eassert (it->glyph_row);
8391 /* Set up hook arguments. */
8392 args[0] = Qredisplay_end_trigger_functions;
8393 args[1] = it->window;
8394 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8395 it->redisplay_end_trigger_charpos = 0;
8397 /* Since we are *trying* to run these functions, don't try to run
8398 them again, even if they get an error. */
8399 wset_redisplay_end_trigger (it->w, Qnil);
8400 Frun_hook_with_args (3, args);
8402 /* Notice if it changed the face of the character we are on. */
8403 handle_face_prop (it);
8407 /* Deliver a composition display element. Unlike the other
8408 next_element_from_XXX, this function is not registered in the array
8409 get_next_element[]. It is called from next_element_from_buffer and
8410 next_element_from_string when necessary. */
8412 static int
8413 next_element_from_composition (struct it *it)
8415 it->what = IT_COMPOSITION;
8416 it->len = it->cmp_it.nbytes;
8417 if (STRINGP (it->string))
8419 if (it->c < 0)
8421 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8422 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8423 return 0;
8425 it->position = it->current.string_pos;
8426 it->object = it->string;
8427 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8428 IT_STRING_BYTEPOS (*it), it->string);
8430 else
8432 if (it->c < 0)
8434 IT_CHARPOS (*it) += it->cmp_it.nchars;
8435 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8436 if (it->bidi_p)
8438 if (it->bidi_it.new_paragraph)
8439 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8440 /* Resync the bidi iterator with IT's new position.
8441 FIXME: this doesn't support bidirectional text. */
8442 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8443 bidi_move_to_visually_next (&it->bidi_it);
8445 return 0;
8447 it->position = it->current.pos;
8448 it->object = it->w->contents;
8449 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8450 IT_BYTEPOS (*it), Qnil);
8452 return 1;
8457 /***********************************************************************
8458 Moving an iterator without producing glyphs
8459 ***********************************************************************/
8461 /* Check if iterator is at a position corresponding to a valid buffer
8462 position after some move_it_ call. */
8464 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8465 ((it)->method == GET_FROM_STRING \
8466 ? IT_STRING_CHARPOS (*it) == 0 \
8467 : 1)
8470 /* Move iterator IT to a specified buffer or X position within one
8471 line on the display without producing glyphs.
8473 OP should be a bit mask including some or all of these bits:
8474 MOVE_TO_X: Stop upon reaching x-position TO_X.
8475 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8476 Regardless of OP's value, stop upon reaching the end of the display line.
8478 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8479 This means, in particular, that TO_X includes window's horizontal
8480 scroll amount.
8482 The return value has several possible values that
8483 say what condition caused the scan to stop:
8485 MOVE_POS_MATCH_OR_ZV
8486 - when TO_POS or ZV was reached.
8488 MOVE_X_REACHED
8489 -when TO_X was reached before TO_POS or ZV were reached.
8491 MOVE_LINE_CONTINUED
8492 - when we reached the end of the display area and the line must
8493 be continued.
8495 MOVE_LINE_TRUNCATED
8496 - when we reached the end of the display area and the line is
8497 truncated.
8499 MOVE_NEWLINE_OR_CR
8500 - when we stopped at a line end, i.e. a newline or a CR and selective
8501 display is on. */
8503 static enum move_it_result
8504 move_it_in_display_line_to (struct it *it,
8505 ptrdiff_t to_charpos, int to_x,
8506 enum move_operation_enum op)
8508 enum move_it_result result = MOVE_UNDEFINED;
8509 struct glyph_row *saved_glyph_row;
8510 struct it wrap_it, atpos_it, atx_it, ppos_it;
8511 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8512 void *ppos_data = NULL;
8513 int may_wrap = 0;
8514 enum it_method prev_method = it->method;
8515 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8516 int saw_smaller_pos = prev_pos < to_charpos;
8518 /* Don't produce glyphs in produce_glyphs. */
8519 saved_glyph_row = it->glyph_row;
8520 it->glyph_row = NULL;
8522 /* Use wrap_it to save a copy of IT wherever a word wrap could
8523 occur. Use atpos_it to save a copy of IT at the desired buffer
8524 position, if found, so that we can scan ahead and check if the
8525 word later overshoots the window edge. Use atx_it similarly, for
8526 pixel positions. */
8527 wrap_it.sp = -1;
8528 atpos_it.sp = -1;
8529 atx_it.sp = -1;
8531 /* Use ppos_it under bidi reordering to save a copy of IT for the
8532 initial position. We restore that position in IT when we have
8533 scanned the entire display line without finding a match for
8534 TO_CHARPOS and all the character positions are greater than
8535 TO_CHARPOS. We then restart the scan from the initial position,
8536 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8537 the closest to TO_CHARPOS. */
8538 if (it->bidi_p)
8540 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8542 SAVE_IT (ppos_it, *it, ppos_data);
8543 closest_pos = IT_CHARPOS (*it);
8545 else
8546 closest_pos = ZV;
8549 #define BUFFER_POS_REACHED_P() \
8550 ((op & MOVE_TO_POS) != 0 \
8551 && BUFFERP (it->object) \
8552 && (IT_CHARPOS (*it) == to_charpos \
8553 || ((!it->bidi_p \
8554 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8555 && IT_CHARPOS (*it) > to_charpos) \
8556 || (it->what == IT_COMPOSITION \
8557 && ((IT_CHARPOS (*it) > to_charpos \
8558 && to_charpos >= it->cmp_it.charpos) \
8559 || (IT_CHARPOS (*it) < to_charpos \
8560 && to_charpos <= it->cmp_it.charpos)))) \
8561 && (it->method == GET_FROM_BUFFER \
8562 || (it->method == GET_FROM_DISPLAY_VECTOR \
8563 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8565 /* If there's a line-/wrap-prefix, handle it. */
8566 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8567 && it->current_y < it->last_visible_y)
8568 handle_line_prefix (it);
8570 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8571 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8573 while (1)
8575 int x, i, ascent = 0, descent = 0;
8577 /* Utility macro to reset an iterator with x, ascent, and descent. */
8578 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8579 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8580 (IT)->max_descent = descent)
8582 /* Stop if we move beyond TO_CHARPOS (after an image or a
8583 display string or stretch glyph). */
8584 if ((op & MOVE_TO_POS) != 0
8585 && BUFFERP (it->object)
8586 && it->method == GET_FROM_BUFFER
8587 && (((!it->bidi_p
8588 /* When the iterator is at base embedding level, we
8589 are guaranteed that characters are delivered for
8590 display in strictly increasing order of their
8591 buffer positions. */
8592 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8593 && IT_CHARPOS (*it) > to_charpos)
8594 || (it->bidi_p
8595 && (prev_method == GET_FROM_IMAGE
8596 || prev_method == GET_FROM_STRETCH
8597 || prev_method == GET_FROM_STRING)
8598 /* Passed TO_CHARPOS from left to right. */
8599 && ((prev_pos < to_charpos
8600 && IT_CHARPOS (*it) > to_charpos)
8601 /* Passed TO_CHARPOS from right to left. */
8602 || (prev_pos > to_charpos
8603 && IT_CHARPOS (*it) < to_charpos)))))
8605 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8607 result = MOVE_POS_MATCH_OR_ZV;
8608 break;
8610 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8611 /* If wrap_it is valid, the current position might be in a
8612 word that is wrapped. So, save the iterator in
8613 atpos_it and continue to see if wrapping happens. */
8614 SAVE_IT (atpos_it, *it, atpos_data);
8617 /* Stop when ZV reached.
8618 We used to stop here when TO_CHARPOS reached as well, but that is
8619 too soon if this glyph does not fit on this line. So we handle it
8620 explicitly below. */
8621 if (!get_next_display_element (it))
8623 result = MOVE_POS_MATCH_OR_ZV;
8624 break;
8627 if (it->line_wrap == TRUNCATE)
8629 if (BUFFER_POS_REACHED_P ())
8631 result = MOVE_POS_MATCH_OR_ZV;
8632 break;
8635 else
8637 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8639 if (IT_DISPLAYING_WHITESPACE (it))
8640 may_wrap = 1;
8641 else if (may_wrap)
8643 /* We have reached a glyph that follows one or more
8644 whitespace characters. If the position is
8645 already found, we are done. */
8646 if (atpos_it.sp >= 0)
8648 RESTORE_IT (it, &atpos_it, atpos_data);
8649 result = MOVE_POS_MATCH_OR_ZV;
8650 goto done;
8652 if (atx_it.sp >= 0)
8654 RESTORE_IT (it, &atx_it, atx_data);
8655 result = MOVE_X_REACHED;
8656 goto done;
8658 /* Otherwise, we can wrap here. */
8659 SAVE_IT (wrap_it, *it, wrap_data);
8660 may_wrap = 0;
8665 /* Remember the line height for the current line, in case
8666 the next element doesn't fit on the line. */
8667 ascent = it->max_ascent;
8668 descent = it->max_descent;
8670 /* The call to produce_glyphs will get the metrics of the
8671 display element IT is loaded with. Record the x-position
8672 before this display element, in case it doesn't fit on the
8673 line. */
8674 x = it->current_x;
8676 PRODUCE_GLYPHS (it);
8678 if (it->area != TEXT_AREA)
8680 prev_method = it->method;
8681 if (it->method == GET_FROM_BUFFER)
8682 prev_pos = IT_CHARPOS (*it);
8683 set_iterator_to_next (it, 1);
8684 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8685 SET_TEXT_POS (this_line_min_pos,
8686 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8687 if (it->bidi_p
8688 && (op & MOVE_TO_POS)
8689 && IT_CHARPOS (*it) > to_charpos
8690 && IT_CHARPOS (*it) < closest_pos)
8691 closest_pos = IT_CHARPOS (*it);
8692 continue;
8695 /* The number of glyphs we get back in IT->nglyphs will normally
8696 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8697 character on a terminal frame, or (iii) a line end. For the
8698 second case, IT->nglyphs - 1 padding glyphs will be present.
8699 (On X frames, there is only one glyph produced for a
8700 composite character.)
8702 The behavior implemented below means, for continuation lines,
8703 that as many spaces of a TAB as fit on the current line are
8704 displayed there. For terminal frames, as many glyphs of a
8705 multi-glyph character are displayed in the current line, too.
8706 This is what the old redisplay code did, and we keep it that
8707 way. Under X, the whole shape of a complex character must
8708 fit on the line or it will be completely displayed in the
8709 next line.
8711 Note that both for tabs and padding glyphs, all glyphs have
8712 the same width. */
8713 if (it->nglyphs)
8715 /* More than one glyph or glyph doesn't fit on line. All
8716 glyphs have the same width. */
8717 int single_glyph_width = it->pixel_width / it->nglyphs;
8718 int new_x;
8719 int x_before_this_char = x;
8720 int hpos_before_this_char = it->hpos;
8722 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8724 new_x = x + single_glyph_width;
8726 /* We want to leave anything reaching TO_X to the caller. */
8727 if ((op & MOVE_TO_X) && new_x > to_x)
8729 if (BUFFER_POS_REACHED_P ())
8731 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8732 goto buffer_pos_reached;
8733 if (atpos_it.sp < 0)
8735 SAVE_IT (atpos_it, *it, atpos_data);
8736 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8739 else
8741 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8743 it->current_x = x;
8744 result = MOVE_X_REACHED;
8745 break;
8747 if (atx_it.sp < 0)
8749 SAVE_IT (atx_it, *it, atx_data);
8750 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8755 if (/* Lines are continued. */
8756 it->line_wrap != TRUNCATE
8757 && (/* And glyph doesn't fit on the line. */
8758 new_x > it->last_visible_x
8759 /* Or it fits exactly and we're on a window
8760 system frame. */
8761 || (new_x == it->last_visible_x
8762 && FRAME_WINDOW_P (it->f)
8763 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8764 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8765 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8767 if (/* IT->hpos == 0 means the very first glyph
8768 doesn't fit on the line, e.g. a wide image. */
8769 it->hpos == 0
8770 || (new_x == it->last_visible_x
8771 && FRAME_WINDOW_P (it->f)
8772 /* When word-wrap is ON and we have a valid
8773 wrap point, we don't allow the last glyph
8774 to "just barely fit" on the line. */
8775 && (it->line_wrap != WORD_WRAP
8776 || wrap_it.sp < 0)))
8778 ++it->hpos;
8779 it->current_x = new_x;
8781 /* The character's last glyph just barely fits
8782 in this row. */
8783 if (i == it->nglyphs - 1)
8785 /* If this is the destination position,
8786 return a position *before* it in this row,
8787 now that we know it fits in this row. */
8788 if (BUFFER_POS_REACHED_P ())
8790 if (it->line_wrap != WORD_WRAP
8791 || wrap_it.sp < 0)
8793 it->hpos = hpos_before_this_char;
8794 it->current_x = x_before_this_char;
8795 result = MOVE_POS_MATCH_OR_ZV;
8796 break;
8798 if (it->line_wrap == WORD_WRAP
8799 && atpos_it.sp < 0)
8801 SAVE_IT (atpos_it, *it, atpos_data);
8802 atpos_it.current_x = x_before_this_char;
8803 atpos_it.hpos = hpos_before_this_char;
8807 prev_method = it->method;
8808 if (it->method == GET_FROM_BUFFER)
8809 prev_pos = IT_CHARPOS (*it);
8810 set_iterator_to_next (it, 1);
8811 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8812 SET_TEXT_POS (this_line_min_pos,
8813 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8814 /* On graphical terminals, newlines may
8815 "overflow" into the fringe if
8816 overflow-newline-into-fringe is non-nil.
8817 On text terminals, and on graphical
8818 terminals with no right margin, newlines
8819 may overflow into the last glyph on the
8820 display line.*/
8821 if (!FRAME_WINDOW_P (it->f)
8822 || ((it->bidi_p
8823 && it->bidi_it.paragraph_dir == R2L)
8824 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8825 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8826 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8828 if (!get_next_display_element (it))
8830 result = MOVE_POS_MATCH_OR_ZV;
8831 break;
8833 if (BUFFER_POS_REACHED_P ())
8835 if (ITERATOR_AT_END_OF_LINE_P (it))
8836 result = MOVE_POS_MATCH_OR_ZV;
8837 else
8838 result = MOVE_LINE_CONTINUED;
8839 break;
8841 if (ITERATOR_AT_END_OF_LINE_P (it)
8842 && (it->line_wrap != WORD_WRAP
8843 || wrap_it.sp < 0))
8845 result = MOVE_NEWLINE_OR_CR;
8846 break;
8851 else
8852 IT_RESET_X_ASCENT_DESCENT (it);
8854 if (wrap_it.sp >= 0)
8856 RESTORE_IT (it, &wrap_it, wrap_data);
8857 atpos_it.sp = -1;
8858 atx_it.sp = -1;
8861 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8862 IT_CHARPOS (*it)));
8863 result = MOVE_LINE_CONTINUED;
8864 break;
8867 if (BUFFER_POS_REACHED_P ())
8869 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8870 goto buffer_pos_reached;
8871 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8873 SAVE_IT (atpos_it, *it, atpos_data);
8874 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8878 if (new_x > it->first_visible_x)
8880 /* Glyph is visible. Increment number of glyphs that
8881 would be displayed. */
8882 ++it->hpos;
8886 if (result != MOVE_UNDEFINED)
8887 break;
8889 else if (BUFFER_POS_REACHED_P ())
8891 buffer_pos_reached:
8892 IT_RESET_X_ASCENT_DESCENT (it);
8893 result = MOVE_POS_MATCH_OR_ZV;
8894 break;
8896 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8898 /* Stop when TO_X specified and reached. This check is
8899 necessary here because of lines consisting of a line end,
8900 only. The line end will not produce any glyphs and we
8901 would never get MOVE_X_REACHED. */
8902 eassert (it->nglyphs == 0);
8903 result = MOVE_X_REACHED;
8904 break;
8907 /* Is this a line end? If yes, we're done. */
8908 if (ITERATOR_AT_END_OF_LINE_P (it))
8910 /* If we are past TO_CHARPOS, but never saw any character
8911 positions smaller than TO_CHARPOS, return
8912 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8913 did. */
8914 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8916 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8918 if (closest_pos < ZV)
8920 RESTORE_IT (it, &ppos_it, ppos_data);
8921 /* Don't recurse if closest_pos is equal to
8922 to_charpos, since we have just tried that. */
8923 if (closest_pos != to_charpos)
8924 move_it_in_display_line_to (it, closest_pos, -1,
8925 MOVE_TO_POS);
8926 result = MOVE_POS_MATCH_OR_ZV;
8928 else
8929 goto buffer_pos_reached;
8931 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8932 && IT_CHARPOS (*it) > to_charpos)
8933 goto buffer_pos_reached;
8934 else
8935 result = MOVE_NEWLINE_OR_CR;
8937 else
8938 result = MOVE_NEWLINE_OR_CR;
8939 break;
8942 prev_method = it->method;
8943 if (it->method == GET_FROM_BUFFER)
8944 prev_pos = IT_CHARPOS (*it);
8945 /* The current display element has been consumed. Advance
8946 to the next. */
8947 set_iterator_to_next (it, 1);
8948 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8949 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8950 if (IT_CHARPOS (*it) < to_charpos)
8951 saw_smaller_pos = 1;
8952 if (it->bidi_p
8953 && (op & MOVE_TO_POS)
8954 && IT_CHARPOS (*it) >= to_charpos
8955 && IT_CHARPOS (*it) < closest_pos)
8956 closest_pos = IT_CHARPOS (*it);
8958 /* Stop if lines are truncated and IT's current x-position is
8959 past the right edge of the window now. */
8960 if (it->line_wrap == TRUNCATE
8961 && it->current_x >= it->last_visible_x)
8963 if (!FRAME_WINDOW_P (it->f)
8964 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8965 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8966 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8967 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8969 int at_eob_p = 0;
8971 if ((at_eob_p = !get_next_display_element (it))
8972 || BUFFER_POS_REACHED_P ()
8973 /* If we are past TO_CHARPOS, but never saw any
8974 character positions smaller than TO_CHARPOS,
8975 return MOVE_POS_MATCH_OR_ZV, like the
8976 unidirectional display did. */
8977 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8978 && !saw_smaller_pos
8979 && IT_CHARPOS (*it) > to_charpos))
8981 if (it->bidi_p
8982 && !BUFFER_POS_REACHED_P ()
8983 && !at_eob_p && closest_pos < ZV)
8985 RESTORE_IT (it, &ppos_it, ppos_data);
8986 if (closest_pos != to_charpos)
8987 move_it_in_display_line_to (it, closest_pos, -1,
8988 MOVE_TO_POS);
8990 result = MOVE_POS_MATCH_OR_ZV;
8991 break;
8993 if (ITERATOR_AT_END_OF_LINE_P (it))
8995 result = MOVE_NEWLINE_OR_CR;
8996 break;
8999 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9000 && !saw_smaller_pos
9001 && IT_CHARPOS (*it) > to_charpos)
9003 if (closest_pos < ZV)
9005 RESTORE_IT (it, &ppos_it, ppos_data);
9006 if (closest_pos != to_charpos)
9007 move_it_in_display_line_to (it, closest_pos, -1,
9008 MOVE_TO_POS);
9010 result = MOVE_POS_MATCH_OR_ZV;
9011 break;
9013 result = MOVE_LINE_TRUNCATED;
9014 break;
9016 #undef IT_RESET_X_ASCENT_DESCENT
9019 #undef BUFFER_POS_REACHED_P
9021 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9022 restore the saved iterator. */
9023 if (atpos_it.sp >= 0)
9024 RESTORE_IT (it, &atpos_it, atpos_data);
9025 else if (atx_it.sp >= 0)
9026 RESTORE_IT (it, &atx_it, atx_data);
9028 done:
9030 if (atpos_data)
9031 bidi_unshelve_cache (atpos_data, 1);
9032 if (atx_data)
9033 bidi_unshelve_cache (atx_data, 1);
9034 if (wrap_data)
9035 bidi_unshelve_cache (wrap_data, 1);
9036 if (ppos_data)
9037 bidi_unshelve_cache (ppos_data, 1);
9039 /* Restore the iterator settings altered at the beginning of this
9040 function. */
9041 it->glyph_row = saved_glyph_row;
9042 return result;
9045 /* For external use. */
9046 void
9047 move_it_in_display_line (struct it *it,
9048 ptrdiff_t to_charpos, int to_x,
9049 enum move_operation_enum op)
9051 if (it->line_wrap == WORD_WRAP
9052 && (op & MOVE_TO_X))
9054 struct it save_it;
9055 void *save_data = NULL;
9056 int skip;
9058 SAVE_IT (save_it, *it, save_data);
9059 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9060 /* When word-wrap is on, TO_X may lie past the end
9061 of a wrapped line. Then it->current is the
9062 character on the next line, so backtrack to the
9063 space before the wrap point. */
9064 if (skip == MOVE_LINE_CONTINUED)
9066 int prev_x = max (it->current_x - 1, 0);
9067 RESTORE_IT (it, &save_it, save_data);
9068 move_it_in_display_line_to
9069 (it, -1, prev_x, MOVE_TO_X);
9071 else
9072 bidi_unshelve_cache (save_data, 1);
9074 else
9075 move_it_in_display_line_to (it, to_charpos, to_x, op);
9079 /* Move IT forward until it satisfies one or more of the criteria in
9080 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9082 OP is a bit-mask that specifies where to stop, and in particular,
9083 which of those four position arguments makes a difference. See the
9084 description of enum move_operation_enum.
9086 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9087 screen line, this function will set IT to the next position that is
9088 displayed to the right of TO_CHARPOS on the screen.
9090 Return the maximum pixel length of any line scanned but never more
9091 than it.last_visible_x. */
9094 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9096 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9097 int line_height, line_start_x = 0, reached = 0;
9098 int max_current_x = 0;
9099 void *backup_data = NULL;
9101 for (;;)
9103 if (op & MOVE_TO_VPOS)
9105 /* If no TO_CHARPOS and no TO_X specified, stop at the
9106 start of the line TO_VPOS. */
9107 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9109 if (it->vpos == to_vpos)
9111 reached = 1;
9112 break;
9114 else
9115 skip = move_it_in_display_line_to (it, -1, -1, 0);
9117 else
9119 /* TO_VPOS >= 0 means stop at TO_X in the line at
9120 TO_VPOS, or at TO_POS, whichever comes first. */
9121 if (it->vpos == to_vpos)
9123 reached = 2;
9124 break;
9127 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9129 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9131 reached = 3;
9132 break;
9134 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9136 /* We have reached TO_X but not in the line we want. */
9137 skip = move_it_in_display_line_to (it, to_charpos,
9138 -1, MOVE_TO_POS);
9139 if (skip == MOVE_POS_MATCH_OR_ZV)
9141 reached = 4;
9142 break;
9147 else if (op & MOVE_TO_Y)
9149 struct it it_backup;
9151 if (it->line_wrap == WORD_WRAP)
9152 SAVE_IT (it_backup, *it, backup_data);
9154 /* TO_Y specified means stop at TO_X in the line containing
9155 TO_Y---or at TO_CHARPOS if this is reached first. The
9156 problem is that we can't really tell whether the line
9157 contains TO_Y before we have completely scanned it, and
9158 this may skip past TO_X. What we do is to first scan to
9159 TO_X.
9161 If TO_X is not specified, use a TO_X of zero. The reason
9162 is to make the outcome of this function more predictable.
9163 If we didn't use TO_X == 0, we would stop at the end of
9164 the line which is probably not what a caller would expect
9165 to happen. */
9166 skip = move_it_in_display_line_to
9167 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9168 (MOVE_TO_X | (op & MOVE_TO_POS)));
9170 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9171 if (skip == MOVE_POS_MATCH_OR_ZV)
9172 reached = 5;
9173 else if (skip == MOVE_X_REACHED)
9175 /* If TO_X was reached, we want to know whether TO_Y is
9176 in the line. We know this is the case if the already
9177 scanned glyphs make the line tall enough. Otherwise,
9178 we must check by scanning the rest of the line. */
9179 line_height = it->max_ascent + it->max_descent;
9180 if (to_y >= it->current_y
9181 && to_y < it->current_y + line_height)
9183 reached = 6;
9184 break;
9186 SAVE_IT (it_backup, *it, backup_data);
9187 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9188 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9189 op & MOVE_TO_POS);
9190 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9191 line_height = it->max_ascent + it->max_descent;
9192 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9194 if (to_y >= it->current_y
9195 && to_y < it->current_y + line_height)
9197 /* If TO_Y is in this line and TO_X was reached
9198 above, we scanned too far. We have to restore
9199 IT's settings to the ones before skipping. But
9200 keep the more accurate values of max_ascent and
9201 max_descent we've found while skipping the rest
9202 of the line, for the sake of callers, such as
9203 pos_visible_p, that need to know the line
9204 height. */
9205 int max_ascent = it->max_ascent;
9206 int max_descent = it->max_descent;
9208 RESTORE_IT (it, &it_backup, backup_data);
9209 it->max_ascent = max_ascent;
9210 it->max_descent = max_descent;
9211 reached = 6;
9213 else
9215 skip = skip2;
9216 if (skip == MOVE_POS_MATCH_OR_ZV)
9217 reached = 7;
9220 else
9222 /* Check whether TO_Y is in this line. */
9223 line_height = it->max_ascent + it->max_descent;
9224 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9226 if (to_y >= it->current_y
9227 && to_y < it->current_y + line_height)
9229 if (to_y > it->current_y)
9230 max_current_x = max (it->current_x, max_current_x);
9232 /* When word-wrap is on, TO_X may lie past the end
9233 of a wrapped line. Then it->current is the
9234 character on the next line, so backtrack to the
9235 space before the wrap point. */
9236 if (skip == MOVE_LINE_CONTINUED
9237 && it->line_wrap == WORD_WRAP)
9239 int prev_x = max (it->current_x - 1, 0);
9240 RESTORE_IT (it, &it_backup, backup_data);
9241 skip = move_it_in_display_line_to
9242 (it, -1, prev_x, MOVE_TO_X);
9245 reached = 6;
9249 if (reached)
9251 max_current_x = max (it->current_x, max_current_x);
9252 break;
9255 else if (BUFFERP (it->object)
9256 && (it->method == GET_FROM_BUFFER
9257 || it->method == GET_FROM_STRETCH)
9258 && IT_CHARPOS (*it) >= to_charpos
9259 /* Under bidi iteration, a call to set_iterator_to_next
9260 can scan far beyond to_charpos if the initial
9261 portion of the next line needs to be reordered. In
9262 that case, give move_it_in_display_line_to another
9263 chance below. */
9264 && !(it->bidi_p
9265 && it->bidi_it.scan_dir == -1))
9266 skip = MOVE_POS_MATCH_OR_ZV;
9267 else
9268 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9270 switch (skip)
9272 case MOVE_POS_MATCH_OR_ZV:
9273 max_current_x = max (it->current_x, max_current_x);
9274 reached = 8;
9275 goto out;
9277 case MOVE_NEWLINE_OR_CR:
9278 max_current_x = max (it->current_x, max_current_x);
9279 set_iterator_to_next (it, 1);
9280 it->continuation_lines_width = 0;
9281 break;
9283 case MOVE_LINE_TRUNCATED:
9284 max_current_x = it->last_visible_x;
9285 it->continuation_lines_width = 0;
9286 reseat_at_next_visible_line_start (it, 0);
9287 if ((op & MOVE_TO_POS) != 0
9288 && IT_CHARPOS (*it) > to_charpos)
9290 reached = 9;
9291 goto out;
9293 break;
9295 case MOVE_LINE_CONTINUED:
9296 max_current_x = it->last_visible_x;
9297 /* For continued lines ending in a tab, some of the glyphs
9298 associated with the tab are displayed on the current
9299 line. Since it->current_x does not include these glyphs,
9300 we use it->last_visible_x instead. */
9301 if (it->c == '\t')
9303 it->continuation_lines_width += it->last_visible_x;
9304 /* When moving by vpos, ensure that the iterator really
9305 advances to the next line (bug#847, bug#969). Fixme:
9306 do we need to do this in other circumstances? */
9307 if (it->current_x != it->last_visible_x
9308 && (op & MOVE_TO_VPOS)
9309 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9311 line_start_x = it->current_x + it->pixel_width
9312 - it->last_visible_x;
9313 if (FRAME_WINDOW_P (it->f))
9315 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9316 struct font *face_font = face->font;
9318 /* When display_line produces a continued line
9319 that ends in a TAB, it skips a tab stop that
9320 is closer than the font's space character
9321 width (see x_produce_glyphs where it produces
9322 the stretch glyph which represents a TAB).
9323 We need to reproduce the same logic here. */
9324 eassert (face_font);
9325 if (face_font)
9327 if (line_start_x < face_font->space_width)
9328 line_start_x
9329 += it->tab_width * face_font->space_width;
9332 set_iterator_to_next (it, 0);
9335 else
9336 it->continuation_lines_width += it->current_x;
9337 break;
9339 default:
9340 emacs_abort ();
9343 /* Reset/increment for the next run. */
9344 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9345 it->current_x = line_start_x;
9346 line_start_x = 0;
9347 it->hpos = 0;
9348 it->current_y += it->max_ascent + it->max_descent;
9349 ++it->vpos;
9350 last_height = it->max_ascent + it->max_descent;
9351 it->max_ascent = it->max_descent = 0;
9354 out:
9356 /* On text terminals, we may stop at the end of a line in the middle
9357 of a multi-character glyph. If the glyph itself is continued,
9358 i.e. it is actually displayed on the next line, don't treat this
9359 stopping point as valid; move to the next line instead (unless
9360 that brings us offscreen). */
9361 if (!FRAME_WINDOW_P (it->f)
9362 && op & MOVE_TO_POS
9363 && IT_CHARPOS (*it) == to_charpos
9364 && it->what == IT_CHARACTER
9365 && it->nglyphs > 1
9366 && it->line_wrap == WINDOW_WRAP
9367 && it->current_x == it->last_visible_x - 1
9368 && it->c != '\n'
9369 && it->c != '\t'
9370 && it->vpos < it->w->window_end_vpos)
9372 it->continuation_lines_width += it->current_x;
9373 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9374 it->current_y += it->max_ascent + it->max_descent;
9375 ++it->vpos;
9376 last_height = it->max_ascent + it->max_descent;
9379 if (backup_data)
9380 bidi_unshelve_cache (backup_data, 1);
9382 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9384 return max_current_x;
9388 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9390 If DY > 0, move IT backward at least that many pixels. DY = 0
9391 means move IT backward to the preceding line start or BEGV. This
9392 function may move over more than DY pixels if IT->current_y - DY
9393 ends up in the middle of a line; in this case IT->current_y will be
9394 set to the top of the line moved to. */
9396 void
9397 move_it_vertically_backward (struct it *it, int dy)
9399 int nlines, h;
9400 struct it it2, it3;
9401 void *it2data = NULL, *it3data = NULL;
9402 ptrdiff_t start_pos;
9403 int nchars_per_row
9404 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9405 ptrdiff_t pos_limit;
9407 move_further_back:
9408 eassert (dy >= 0);
9410 start_pos = IT_CHARPOS (*it);
9412 /* Estimate how many newlines we must move back. */
9413 nlines = max (1, dy / default_line_pixel_height (it->w));
9414 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9415 pos_limit = BEGV;
9416 else
9417 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9419 /* Set the iterator's position that many lines back. But don't go
9420 back more than NLINES full screen lines -- this wins a day with
9421 buffers which have very long lines. */
9422 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9423 back_to_previous_visible_line_start (it);
9425 /* Reseat the iterator here. When moving backward, we don't want
9426 reseat to skip forward over invisible text, set up the iterator
9427 to deliver from overlay strings at the new position etc. So,
9428 use reseat_1 here. */
9429 reseat_1 (it, it->current.pos, 1);
9431 /* We are now surely at a line start. */
9432 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9433 reordering is in effect. */
9434 it->continuation_lines_width = 0;
9436 /* Move forward and see what y-distance we moved. First move to the
9437 start of the next line so that we get its height. We need this
9438 height to be able to tell whether we reached the specified
9439 y-distance. */
9440 SAVE_IT (it2, *it, it2data);
9441 it2.max_ascent = it2.max_descent = 0;
9444 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9445 MOVE_TO_POS | MOVE_TO_VPOS);
9447 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9448 /* If we are in a display string which starts at START_POS,
9449 and that display string includes a newline, and we are
9450 right after that newline (i.e. at the beginning of a
9451 display line), exit the loop, because otherwise we will
9452 infloop, since move_it_to will see that it is already at
9453 START_POS and will not move. */
9454 || (it2.method == GET_FROM_STRING
9455 && IT_CHARPOS (it2) == start_pos
9456 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9457 eassert (IT_CHARPOS (*it) >= BEGV);
9458 SAVE_IT (it3, it2, it3data);
9460 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9461 eassert (IT_CHARPOS (*it) >= BEGV);
9462 /* H is the actual vertical distance from the position in *IT
9463 and the starting position. */
9464 h = it2.current_y - it->current_y;
9465 /* NLINES is the distance in number of lines. */
9466 nlines = it2.vpos - it->vpos;
9468 /* Correct IT's y and vpos position
9469 so that they are relative to the starting point. */
9470 it->vpos -= nlines;
9471 it->current_y -= h;
9473 if (dy == 0)
9475 /* DY == 0 means move to the start of the screen line. The
9476 value of nlines is > 0 if continuation lines were involved,
9477 or if the original IT position was at start of a line. */
9478 RESTORE_IT (it, it, it2data);
9479 if (nlines > 0)
9480 move_it_by_lines (it, nlines);
9481 /* The above code moves us to some position NLINES down,
9482 usually to its first glyph (leftmost in an L2R line), but
9483 that's not necessarily the start of the line, under bidi
9484 reordering. We want to get to the character position
9485 that is immediately after the newline of the previous
9486 line. */
9487 if (it->bidi_p
9488 && !it->continuation_lines_width
9489 && !STRINGP (it->string)
9490 && IT_CHARPOS (*it) > BEGV
9491 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9493 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9495 DEC_BOTH (cp, bp);
9496 cp = find_newline_no_quit (cp, bp, -1, NULL);
9497 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9499 bidi_unshelve_cache (it3data, 1);
9501 else
9503 /* The y-position we try to reach, relative to *IT.
9504 Note that H has been subtracted in front of the if-statement. */
9505 int target_y = it->current_y + h - dy;
9506 int y0 = it3.current_y;
9507 int y1;
9508 int line_height;
9510 RESTORE_IT (&it3, &it3, it3data);
9511 y1 = line_bottom_y (&it3);
9512 line_height = y1 - y0;
9513 RESTORE_IT (it, it, it2data);
9514 /* If we did not reach target_y, try to move further backward if
9515 we can. If we moved too far backward, try to move forward. */
9516 if (target_y < it->current_y
9517 /* This is heuristic. In a window that's 3 lines high, with
9518 a line height of 13 pixels each, recentering with point
9519 on the bottom line will try to move -39/2 = 19 pixels
9520 backward. Try to avoid moving into the first line. */
9521 && (it->current_y - target_y
9522 > min (window_box_height (it->w), line_height * 2 / 3))
9523 && IT_CHARPOS (*it) > BEGV)
9525 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9526 target_y - it->current_y));
9527 dy = it->current_y - target_y;
9528 goto move_further_back;
9530 else if (target_y >= it->current_y + line_height
9531 && IT_CHARPOS (*it) < ZV)
9533 /* Should move forward by at least one line, maybe more.
9535 Note: Calling move_it_by_lines can be expensive on
9536 terminal frames, where compute_motion is used (via
9537 vmotion) to do the job, when there are very long lines
9538 and truncate-lines is nil. That's the reason for
9539 treating terminal frames specially here. */
9541 if (!FRAME_WINDOW_P (it->f))
9542 move_it_vertically (it, target_y - (it->current_y + line_height));
9543 else
9547 move_it_by_lines (it, 1);
9549 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9556 /* Move IT by a specified amount of pixel lines DY. DY negative means
9557 move backwards. DY = 0 means move to start of screen line. At the
9558 end, IT will be on the start of a screen line. */
9560 void
9561 move_it_vertically (struct it *it, int dy)
9563 if (dy <= 0)
9564 move_it_vertically_backward (it, -dy);
9565 else
9567 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9568 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9569 MOVE_TO_POS | MOVE_TO_Y);
9570 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9572 /* If buffer ends in ZV without a newline, move to the start of
9573 the line to satisfy the post-condition. */
9574 if (IT_CHARPOS (*it) == ZV
9575 && ZV > BEGV
9576 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9577 move_it_by_lines (it, 0);
9582 /* Move iterator IT past the end of the text line it is in. */
9584 void
9585 move_it_past_eol (struct it *it)
9587 enum move_it_result rc;
9589 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9590 if (rc == MOVE_NEWLINE_OR_CR)
9591 set_iterator_to_next (it, 0);
9595 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9596 negative means move up. DVPOS == 0 means move to the start of the
9597 screen line.
9599 Optimization idea: If we would know that IT->f doesn't use
9600 a face with proportional font, we could be faster for
9601 truncate-lines nil. */
9603 void
9604 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9607 /* The commented-out optimization uses vmotion on terminals. This
9608 gives bad results, because elements like it->what, on which
9609 callers such as pos_visible_p rely, aren't updated. */
9610 /* struct position pos;
9611 if (!FRAME_WINDOW_P (it->f))
9613 struct text_pos textpos;
9615 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9616 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9617 reseat (it, textpos, 1);
9618 it->vpos += pos.vpos;
9619 it->current_y += pos.vpos;
9621 else */
9623 if (dvpos == 0)
9625 /* DVPOS == 0 means move to the start of the screen line. */
9626 move_it_vertically_backward (it, 0);
9627 /* Let next call to line_bottom_y calculate real line height. */
9628 last_height = 0;
9630 else if (dvpos > 0)
9632 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9633 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9635 /* Only move to the next buffer position if we ended up in a
9636 string from display property, not in an overlay string
9637 (before-string or after-string). That is because the
9638 latter don't conceal the underlying buffer position, so
9639 we can ask to move the iterator to the exact position we
9640 are interested in. Note that, even if we are already at
9641 IT_CHARPOS (*it), the call below is not a no-op, as it
9642 will detect that we are at the end of the string, pop the
9643 iterator, and compute it->current_x and it->hpos
9644 correctly. */
9645 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9646 -1, -1, -1, MOVE_TO_POS);
9649 else
9651 struct it it2;
9652 void *it2data = NULL;
9653 ptrdiff_t start_charpos, i;
9654 int nchars_per_row
9655 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9656 bool hit_pos_limit = false;
9657 ptrdiff_t pos_limit;
9659 /* Start at the beginning of the screen line containing IT's
9660 position. This may actually move vertically backwards,
9661 in case of overlays, so adjust dvpos accordingly. */
9662 dvpos += it->vpos;
9663 move_it_vertically_backward (it, 0);
9664 dvpos -= it->vpos;
9666 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9667 screen lines, and reseat the iterator there. */
9668 start_charpos = IT_CHARPOS (*it);
9669 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9670 pos_limit = BEGV;
9671 else
9672 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9674 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9675 back_to_previous_visible_line_start (it);
9676 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9677 hit_pos_limit = true;
9678 reseat (it, it->current.pos, 1);
9680 /* Move further back if we end up in a string or an image. */
9681 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9683 /* First try to move to start of display line. */
9684 dvpos += it->vpos;
9685 move_it_vertically_backward (it, 0);
9686 dvpos -= it->vpos;
9687 if (IT_POS_VALID_AFTER_MOVE_P (it))
9688 break;
9689 /* If start of line is still in string or image,
9690 move further back. */
9691 back_to_previous_visible_line_start (it);
9692 reseat (it, it->current.pos, 1);
9693 dvpos--;
9696 it->current_x = it->hpos = 0;
9698 /* Above call may have moved too far if continuation lines
9699 are involved. Scan forward and see if it did. */
9700 SAVE_IT (it2, *it, it2data);
9701 it2.vpos = it2.current_y = 0;
9702 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9703 it->vpos -= it2.vpos;
9704 it->current_y -= it2.current_y;
9705 it->current_x = it->hpos = 0;
9707 /* If we moved too far back, move IT some lines forward. */
9708 if (it2.vpos > -dvpos)
9710 int delta = it2.vpos + dvpos;
9712 RESTORE_IT (&it2, &it2, it2data);
9713 SAVE_IT (it2, *it, it2data);
9714 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9715 /* Move back again if we got too far ahead. */
9716 if (IT_CHARPOS (*it) >= start_charpos)
9717 RESTORE_IT (it, &it2, it2data);
9718 else
9719 bidi_unshelve_cache (it2data, 1);
9721 else if (hit_pos_limit && pos_limit > BEGV
9722 && dvpos < 0 && it2.vpos < -dvpos)
9724 /* If we hit the limit, but still didn't make it far enough
9725 back, that means there's a display string with a newline
9726 covering a large chunk of text, and that caused
9727 back_to_previous_visible_line_start try to go too far.
9728 Punish those who commit such atrocities by going back
9729 until we've reached DVPOS, after lifting the limit, which
9730 could make it slow for very long lines. "If it hurts,
9731 don't do that!" */
9732 dvpos += it2.vpos;
9733 RESTORE_IT (it, it, it2data);
9734 for (i = -dvpos; i > 0; --i)
9736 back_to_previous_visible_line_start (it);
9737 it->vpos--;
9740 else
9741 RESTORE_IT (it, it, it2data);
9745 /* Return true if IT points into the middle of a display vector. */
9747 bool
9748 in_display_vector_p (struct it *it)
9750 return (it->method == GET_FROM_DISPLAY_VECTOR
9751 && it->current.dpvec_index > 0
9752 && it->dpvec + it->current.dpvec_index != it->dpend);
9755 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9756 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9757 WINDOW must be a live window and defaults to the selected one. The
9758 return value is a cons of the maximum pixel-width of any text line and
9759 the maximum pixel-height of all text lines.
9761 The optional argument FROM, if non-nil, specifies the first text
9762 position and defaults to the minimum accessible position of the buffer.
9763 If FROM is t, use the minimum accessible position that is not a newline
9764 character. TO, if non-nil, specifies the last text position and
9765 defaults to the maximum accessible position of the buffer. If TO is t,
9766 use the maximum accessible position that is not a newline character.
9768 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9769 width that can be returned. X-LIMIT nil or omitted, means to use the
9770 pixel-width of WINDOW's body; use this if you do not intend to change
9771 the width of WINDOW. Use the maximum width WINDOW may assume if you
9772 intend to change WINDOW's width. In any case, text whose x-coordinate
9773 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9774 can take some time, it's always a good idea to make this argument as
9775 small as possible; in particular, if the buffer contains long lines that
9776 shall be truncated anyway.
9778 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9779 height that can be returned. Text lines whose y-coordinate is beyond
9780 Y-LIMIT are ignored. Since calculating the text height of a large
9781 buffer can take some time, it makes sense to specify this argument if
9782 the size of the buffer is unknown.
9784 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9785 include the height of the mode- or header-line of WINDOW in the return
9786 value. If it is either the symbol `mode-line' or `header-line', include
9787 only the height of that line, if present, in the return value. If t,
9788 include the height of both, if present, in the return value. */)
9789 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9790 Lisp_Object mode_and_header_line)
9792 struct window *w = decode_live_window (window);
9793 Lisp_Object buf;
9794 struct buffer *b;
9795 struct it it;
9796 struct buffer *old_buffer = NULL;
9797 ptrdiff_t start, end, pos;
9798 struct text_pos startp;
9799 void *itdata = NULL;
9800 int c, max_y = -1, x = 0, y = 0;
9802 buf = w->contents;
9803 CHECK_BUFFER (buf);
9804 b = XBUFFER (buf);
9806 if (b != current_buffer)
9808 old_buffer = current_buffer;
9809 set_buffer_internal (b);
9812 if (NILP (from))
9813 start = BEGV;
9814 else if (EQ (from, Qt))
9816 start = pos = BEGV;
9817 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9818 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9819 start = pos;
9820 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9821 start = pos;
9823 else
9825 CHECK_NUMBER_COERCE_MARKER (from);
9826 start = min (max (XINT (from), BEGV), ZV);
9829 if (NILP (to))
9830 end = ZV;
9831 else if (EQ (to, Qt))
9833 end = pos = ZV;
9834 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9835 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9836 end = pos;
9837 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9838 end = pos;
9840 else
9842 CHECK_NUMBER_COERCE_MARKER (to);
9843 end = max (start, min (XINT (to), ZV));
9846 if (!NILP (y_limit))
9848 CHECK_NUMBER (y_limit);
9849 max_y = min (XINT (y_limit), INT_MAX);
9852 itdata = bidi_shelve_cache ();
9853 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9854 start_display (&it, w, startp);
9856 if (NILP (x_limit))
9857 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9858 else
9860 CHECK_NUMBER (x_limit);
9861 it.last_visible_x = min (XINT (x_limit), INFINITY);
9862 /* Actually, we never want move_it_to stop at to_x. But to make
9863 sure that move_it_in_display_line_to always moves far enough,
9864 we set it to INT_MAX and specify MOVE_TO_X. */
9865 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9866 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9869 y = it.current_y + it.max_ascent + it.max_descent;
9871 if (!EQ (mode_and_header_line, Qheader_line)
9872 && !EQ (mode_and_header_line, Qt))
9873 /* Do not count the header-line which was counted automatically by
9874 start_display. */
9875 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9877 if (EQ (mode_and_header_line, Qmode_line)
9878 || EQ (mode_and_header_line, Qt))
9879 /* Do count the mode-line which is not included automatically by
9880 start_display. */
9881 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9883 bidi_unshelve_cache (itdata, 0);
9885 if (old_buffer)
9886 set_buffer_internal (old_buffer);
9888 return Fcons (make_number (x), make_number (y));
9891 /***********************************************************************
9892 Messages
9893 ***********************************************************************/
9896 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9897 to *Messages*. */
9899 void
9900 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9902 Lisp_Object args[3];
9903 Lisp_Object msg, fmt;
9904 char *buffer;
9905 ptrdiff_t len;
9906 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9907 USE_SAFE_ALLOCA;
9909 fmt = msg = Qnil;
9910 GCPRO4 (fmt, msg, arg1, arg2);
9912 args[0] = fmt = build_string (format);
9913 args[1] = arg1;
9914 args[2] = arg2;
9915 msg = Fformat (3, args);
9917 len = SBYTES (msg) + 1;
9918 buffer = SAFE_ALLOCA (len);
9919 memcpy (buffer, SDATA (msg), len);
9921 message_dolog (buffer, len - 1, 1, 0);
9922 SAFE_FREE ();
9924 UNGCPRO;
9928 /* Output a newline in the *Messages* buffer if "needs" one. */
9930 void
9931 message_log_maybe_newline (void)
9933 if (message_log_need_newline)
9934 message_dolog ("", 0, 1, 0);
9938 /* Add a string M of length NBYTES to the message log, optionally
9939 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9940 true, means interpret the contents of M as multibyte. This
9941 function calls low-level routines in order to bypass text property
9942 hooks, etc. which might not be safe to run.
9944 This may GC (insert may run before/after change hooks),
9945 so the buffer M must NOT point to a Lisp string. */
9947 void
9948 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9950 const unsigned char *msg = (const unsigned char *) m;
9952 if (!NILP (Vmemory_full))
9953 return;
9955 if (!NILP (Vmessage_log_max))
9957 struct buffer *oldbuf;
9958 Lisp_Object oldpoint, oldbegv, oldzv;
9959 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9960 ptrdiff_t point_at_end = 0;
9961 ptrdiff_t zv_at_end = 0;
9962 Lisp_Object old_deactivate_mark;
9963 struct gcpro gcpro1;
9965 old_deactivate_mark = Vdeactivate_mark;
9966 oldbuf = current_buffer;
9968 /* Ensure the Messages buffer exists, and switch to it.
9969 If we created it, set the major-mode. */
9971 int newbuffer = 0;
9972 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9974 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9976 if (newbuffer
9977 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9978 call0 (intern ("messages-buffer-mode"));
9981 bset_undo_list (current_buffer, Qt);
9982 bset_cache_long_scans (current_buffer, Qnil);
9984 oldpoint = message_dolog_marker1;
9985 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9986 oldbegv = message_dolog_marker2;
9987 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9988 oldzv = message_dolog_marker3;
9989 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9990 GCPRO1 (old_deactivate_mark);
9992 if (PT == Z)
9993 point_at_end = 1;
9994 if (ZV == Z)
9995 zv_at_end = 1;
9997 BEGV = BEG;
9998 BEGV_BYTE = BEG_BYTE;
9999 ZV = Z;
10000 ZV_BYTE = Z_BYTE;
10001 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10003 /* Insert the string--maybe converting multibyte to single byte
10004 or vice versa, so that all the text fits the buffer. */
10005 if (multibyte
10006 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10008 ptrdiff_t i;
10009 int c, char_bytes;
10010 char work[1];
10012 /* Convert a multibyte string to single-byte
10013 for the *Message* buffer. */
10014 for (i = 0; i < nbytes; i += char_bytes)
10016 c = string_char_and_length (msg + i, &char_bytes);
10017 work[0] = (ASCII_CHAR_P (c)
10019 : multibyte_char_to_unibyte (c));
10020 insert_1_both (work, 1, 1, 1, 0, 0);
10023 else if (! multibyte
10024 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10026 ptrdiff_t i;
10027 int c, char_bytes;
10028 unsigned char str[MAX_MULTIBYTE_LENGTH];
10029 /* Convert a single-byte string to multibyte
10030 for the *Message* buffer. */
10031 for (i = 0; i < nbytes; i++)
10033 c = msg[i];
10034 MAKE_CHAR_MULTIBYTE (c);
10035 char_bytes = CHAR_STRING (c, str);
10036 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
10039 else if (nbytes)
10040 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
10042 if (nlflag)
10044 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10045 printmax_t dups;
10047 insert_1_both ("\n", 1, 1, 1, 0, 0);
10049 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
10050 this_bol = PT;
10051 this_bol_byte = PT_BYTE;
10053 /* See if this line duplicates the previous one.
10054 If so, combine duplicates. */
10055 if (this_bol > BEG)
10057 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
10058 prev_bol = PT;
10059 prev_bol_byte = PT_BYTE;
10061 dups = message_log_check_duplicate (prev_bol_byte,
10062 this_bol_byte);
10063 if (dups)
10065 del_range_both (prev_bol, prev_bol_byte,
10066 this_bol, this_bol_byte, 0);
10067 if (dups > 1)
10069 char dupstr[sizeof " [ times]"
10070 + INT_STRLEN_BOUND (printmax_t)];
10072 /* If you change this format, don't forget to also
10073 change message_log_check_duplicate. */
10074 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10075 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10076 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
10081 /* If we have more than the desired maximum number of lines
10082 in the *Messages* buffer now, delete the oldest ones.
10083 This is safe because we don't have undo in this buffer. */
10085 if (NATNUMP (Vmessage_log_max))
10087 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10088 -XFASTINT (Vmessage_log_max) - 1, 0);
10089 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
10092 BEGV = marker_position (oldbegv);
10093 BEGV_BYTE = marker_byte_position (oldbegv);
10095 if (zv_at_end)
10097 ZV = Z;
10098 ZV_BYTE = Z_BYTE;
10100 else
10102 ZV = marker_position (oldzv);
10103 ZV_BYTE = marker_byte_position (oldzv);
10106 if (point_at_end)
10107 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10108 else
10109 /* We can't do Fgoto_char (oldpoint) because it will run some
10110 Lisp code. */
10111 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10112 marker_byte_position (oldpoint));
10114 UNGCPRO;
10115 unchain_marker (XMARKER (oldpoint));
10116 unchain_marker (XMARKER (oldbegv));
10117 unchain_marker (XMARKER (oldzv));
10119 /* We called insert_1_both above with its 5th argument (PREPARE)
10120 zero, which prevents insert_1_both from calling
10121 prepare_to_modify_buffer, which in turns prevents us from
10122 incrementing windows_or_buffers_changed even if *Messages* is
10123 shown in some window. So we must manually set
10124 windows_or_buffers_changed here to make up for that. */
10125 windows_or_buffers_changed = old_windows_or_buffers_changed;
10126 bset_redisplay (current_buffer);
10128 set_buffer_internal (oldbuf);
10130 message_log_need_newline = !nlflag;
10131 Vdeactivate_mark = old_deactivate_mark;
10136 /* We are at the end of the buffer after just having inserted a newline.
10137 (Note: We depend on the fact we won't be crossing the gap.)
10138 Check to see if the most recent message looks a lot like the previous one.
10139 Return 0 if different, 1 if the new one should just replace it, or a
10140 value N > 1 if we should also append " [N times]". */
10142 static intmax_t
10143 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10145 ptrdiff_t i;
10146 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10147 int seen_dots = 0;
10148 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10149 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10151 for (i = 0; i < len; i++)
10153 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10154 seen_dots = 1;
10155 if (p1[i] != p2[i])
10156 return seen_dots;
10158 p1 += len;
10159 if (*p1 == '\n')
10160 return 2;
10161 if (*p1++ == ' ' && *p1++ == '[')
10163 char *pend;
10164 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10165 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10166 return n + 1;
10168 return 0;
10172 /* Display an echo area message M with a specified length of NBYTES
10173 bytes. The string may include null characters. If M is not a
10174 string, clear out any existing message, and let the mini-buffer
10175 text show through.
10177 This function cancels echoing. */
10179 void
10180 message3 (Lisp_Object m)
10182 struct gcpro gcpro1;
10184 GCPRO1 (m);
10185 clear_message (true, true);
10186 cancel_echoing ();
10188 /* First flush out any partial line written with print. */
10189 message_log_maybe_newline ();
10190 if (STRINGP (m))
10192 ptrdiff_t nbytes = SBYTES (m);
10193 bool multibyte = STRING_MULTIBYTE (m);
10194 USE_SAFE_ALLOCA;
10195 char *buffer = SAFE_ALLOCA (nbytes);
10196 memcpy (buffer, SDATA (m), nbytes);
10197 message_dolog (buffer, nbytes, 1, multibyte);
10198 SAFE_FREE ();
10200 message3_nolog (m);
10202 UNGCPRO;
10206 /* The non-logging version of message3.
10207 This does not cancel echoing, because it is used for echoing.
10208 Perhaps we need to make a separate function for echoing
10209 and make this cancel echoing. */
10211 void
10212 message3_nolog (Lisp_Object m)
10214 struct frame *sf = SELECTED_FRAME ();
10216 if (FRAME_INITIAL_P (sf))
10218 if (noninteractive_need_newline)
10219 putc ('\n', stderr);
10220 noninteractive_need_newline = 0;
10221 if (STRINGP (m))
10223 Lisp_Object s = ENCODE_SYSTEM (m);
10225 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10227 if (cursor_in_echo_area == 0)
10228 fprintf (stderr, "\n");
10229 fflush (stderr);
10231 /* Error messages get reported properly by cmd_error, so this must be just an
10232 informative message; if the frame hasn't really been initialized yet, just
10233 toss it. */
10234 else if (INTERACTIVE && sf->glyphs_initialized_p)
10236 /* Get the frame containing the mini-buffer
10237 that the selected frame is using. */
10238 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10239 Lisp_Object frame = XWINDOW (mini_window)->frame;
10240 struct frame *f = XFRAME (frame);
10242 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10243 Fmake_frame_visible (frame);
10245 if (STRINGP (m) && SCHARS (m) > 0)
10247 set_message (m);
10248 if (minibuffer_auto_raise)
10249 Fraise_frame (frame);
10250 /* Assume we are not echoing.
10251 (If we are, echo_now will override this.) */
10252 echo_message_buffer = Qnil;
10254 else
10255 clear_message (true, true);
10257 do_pending_window_change (0);
10258 echo_area_display (1);
10259 do_pending_window_change (0);
10260 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10261 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10266 /* Display a null-terminated echo area message M. If M is 0, clear
10267 out any existing message, and let the mini-buffer text show through.
10269 The buffer M must continue to exist until after the echo area gets
10270 cleared or some other message gets displayed there. Do not pass
10271 text that is stored in a Lisp string. Do not pass text in a buffer
10272 that was alloca'd. */
10274 void
10275 message1 (const char *m)
10277 message3 (m ? build_unibyte_string (m) : Qnil);
10281 /* The non-logging counterpart of message1. */
10283 void
10284 message1_nolog (const char *m)
10286 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10289 /* Display a message M which contains a single %s
10290 which gets replaced with STRING. */
10292 void
10293 message_with_string (const char *m, Lisp_Object string, int log)
10295 CHECK_STRING (string);
10297 if (noninteractive)
10299 if (m)
10301 /* ENCODE_SYSTEM below can GC and/or relocate the Lisp
10302 String whose data pointer might be passed to us in M. So
10303 we use a local copy. */
10304 char *fmt = xstrdup (m);
10306 if (noninteractive_need_newline)
10307 putc ('\n', stderr);
10308 noninteractive_need_newline = 0;
10309 fprintf (stderr, fmt, SDATA (ENCODE_SYSTEM (string)));
10310 if (!cursor_in_echo_area)
10311 fprintf (stderr, "\n");
10312 fflush (stderr);
10313 xfree (fmt);
10316 else if (INTERACTIVE)
10318 /* The frame whose minibuffer we're going to display the message on.
10319 It may be larger than the selected frame, so we need
10320 to use its buffer, not the selected frame's buffer. */
10321 Lisp_Object mini_window;
10322 struct frame *f, *sf = SELECTED_FRAME ();
10324 /* Get the frame containing the minibuffer
10325 that the selected frame is using. */
10326 mini_window = FRAME_MINIBUF_WINDOW (sf);
10327 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10329 /* Error messages get reported properly by cmd_error, so this must be
10330 just an informative message; if the frame hasn't really been
10331 initialized yet, just toss it. */
10332 if (f->glyphs_initialized_p)
10334 Lisp_Object args[2], msg;
10335 struct gcpro gcpro1, gcpro2;
10337 args[0] = build_string (m);
10338 args[1] = msg = string;
10339 GCPRO2 (args[0], msg);
10340 gcpro1.nvars = 2;
10342 msg = Fformat (2, args);
10344 if (log)
10345 message3 (msg);
10346 else
10347 message3_nolog (msg);
10349 UNGCPRO;
10351 /* Print should start at the beginning of the message
10352 buffer next time. */
10353 message_buf_print = 0;
10359 /* Dump an informative message to the minibuf. If M is 0, clear out
10360 any existing message, and let the mini-buffer text show through. */
10362 static void
10363 vmessage (const char *m, va_list ap)
10365 if (noninteractive)
10367 if (m)
10369 if (noninteractive_need_newline)
10370 putc ('\n', stderr);
10371 noninteractive_need_newline = 0;
10372 vfprintf (stderr, m, ap);
10373 if (cursor_in_echo_area == 0)
10374 fprintf (stderr, "\n");
10375 fflush (stderr);
10378 else if (INTERACTIVE)
10380 /* The frame whose mini-buffer we're going to display the message
10381 on. It may be larger than the selected frame, so we need to
10382 use its buffer, not the selected frame's buffer. */
10383 Lisp_Object mini_window;
10384 struct frame *f, *sf = SELECTED_FRAME ();
10386 /* Get the frame containing the mini-buffer
10387 that the selected frame is using. */
10388 mini_window = FRAME_MINIBUF_WINDOW (sf);
10389 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10391 /* Error messages get reported properly by cmd_error, so this must be
10392 just an informative message; if the frame hasn't really been
10393 initialized yet, just toss it. */
10394 if (f->glyphs_initialized_p)
10396 if (m)
10398 ptrdiff_t len;
10399 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10400 char *message_buf = alloca (maxsize + 1);
10402 len = doprnt (message_buf, maxsize, m, 0, ap);
10404 message3 (make_string (message_buf, len));
10406 else
10407 message1 (0);
10409 /* Print should start at the beginning of the message
10410 buffer next time. */
10411 message_buf_print = 0;
10416 void
10417 message (const char *m, ...)
10419 va_list ap;
10420 va_start (ap, m);
10421 vmessage (m, ap);
10422 va_end (ap);
10426 #if 0
10427 /* The non-logging version of message. */
10429 void
10430 message_nolog (const char *m, ...)
10432 Lisp_Object old_log_max;
10433 va_list ap;
10434 va_start (ap, m);
10435 old_log_max = Vmessage_log_max;
10436 Vmessage_log_max = Qnil;
10437 vmessage (m, ap);
10438 Vmessage_log_max = old_log_max;
10439 va_end (ap);
10441 #endif
10444 /* Display the current message in the current mini-buffer. This is
10445 only called from error handlers in process.c, and is not time
10446 critical. */
10448 void
10449 update_echo_area (void)
10451 if (!NILP (echo_area_buffer[0]))
10453 Lisp_Object string;
10454 string = Fcurrent_message ();
10455 message3 (string);
10460 /* Make sure echo area buffers in `echo_buffers' are live.
10461 If they aren't, make new ones. */
10463 static void
10464 ensure_echo_area_buffers (void)
10466 int i;
10468 for (i = 0; i < 2; ++i)
10469 if (!BUFFERP (echo_buffer[i])
10470 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10472 char name[30];
10473 Lisp_Object old_buffer;
10474 int j;
10476 old_buffer = echo_buffer[i];
10477 echo_buffer[i] = Fget_buffer_create
10478 (make_formatted_string (name, " *Echo Area %d*", i));
10479 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10480 /* to force word wrap in echo area -
10481 it was decided to postpone this*/
10482 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10484 for (j = 0; j < 2; ++j)
10485 if (EQ (old_buffer, echo_area_buffer[j]))
10486 echo_area_buffer[j] = echo_buffer[i];
10491 /* Call FN with args A1..A2 with either the current or last displayed
10492 echo_area_buffer as current buffer.
10494 WHICH zero means use the current message buffer
10495 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10496 from echo_buffer[] and clear it.
10498 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10499 suitable buffer from echo_buffer[] and clear it.
10501 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10502 that the current message becomes the last displayed one, make
10503 choose a suitable buffer for echo_area_buffer[0], and clear it.
10505 Value is what FN returns. */
10507 static int
10508 with_echo_area_buffer (struct window *w, int which,
10509 int (*fn) (ptrdiff_t, Lisp_Object),
10510 ptrdiff_t a1, Lisp_Object a2)
10512 Lisp_Object buffer;
10513 int this_one, the_other, clear_buffer_p, rc;
10514 ptrdiff_t count = SPECPDL_INDEX ();
10516 /* If buffers aren't live, make new ones. */
10517 ensure_echo_area_buffers ();
10519 clear_buffer_p = 0;
10521 if (which == 0)
10522 this_one = 0, the_other = 1;
10523 else if (which > 0)
10524 this_one = 1, the_other = 0;
10525 else
10527 this_one = 0, the_other = 1;
10528 clear_buffer_p = true;
10530 /* We need a fresh one in case the current echo buffer equals
10531 the one containing the last displayed echo area message. */
10532 if (!NILP (echo_area_buffer[this_one])
10533 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10534 echo_area_buffer[this_one] = Qnil;
10537 /* Choose a suitable buffer from echo_buffer[] is we don't
10538 have one. */
10539 if (NILP (echo_area_buffer[this_one]))
10541 echo_area_buffer[this_one]
10542 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10543 ? echo_buffer[the_other]
10544 : echo_buffer[this_one]);
10545 clear_buffer_p = true;
10548 buffer = echo_area_buffer[this_one];
10550 /* Don't get confused by reusing the buffer used for echoing
10551 for a different purpose. */
10552 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10553 cancel_echoing ();
10555 record_unwind_protect (unwind_with_echo_area_buffer,
10556 with_echo_area_buffer_unwind_data (w));
10558 /* Make the echo area buffer current. Note that for display
10559 purposes, it is not necessary that the displayed window's buffer
10560 == current_buffer, except for text property lookup. So, let's
10561 only set that buffer temporarily here without doing a full
10562 Fset_window_buffer. We must also change w->pointm, though,
10563 because otherwise an assertions in unshow_buffer fails, and Emacs
10564 aborts. */
10565 set_buffer_internal_1 (XBUFFER (buffer));
10566 if (w)
10568 wset_buffer (w, buffer);
10569 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10572 bset_undo_list (current_buffer, Qt);
10573 bset_read_only (current_buffer, Qnil);
10574 specbind (Qinhibit_read_only, Qt);
10575 specbind (Qinhibit_modification_hooks, Qt);
10577 if (clear_buffer_p && Z > BEG)
10578 del_range (BEG, Z);
10580 eassert (BEGV >= BEG);
10581 eassert (ZV <= Z && ZV >= BEGV);
10583 rc = fn (a1, a2);
10585 eassert (BEGV >= BEG);
10586 eassert (ZV <= Z && ZV >= BEGV);
10588 unbind_to (count, Qnil);
10589 return rc;
10593 /* Save state that should be preserved around the call to the function
10594 FN called in with_echo_area_buffer. */
10596 static Lisp_Object
10597 with_echo_area_buffer_unwind_data (struct window *w)
10599 int i = 0;
10600 Lisp_Object vector, tmp;
10602 /* Reduce consing by keeping one vector in
10603 Vwith_echo_area_save_vector. */
10604 vector = Vwith_echo_area_save_vector;
10605 Vwith_echo_area_save_vector = Qnil;
10607 if (NILP (vector))
10608 vector = Fmake_vector (make_number (9), Qnil);
10610 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10611 ASET (vector, i, Vdeactivate_mark); ++i;
10612 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10614 if (w)
10616 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10617 ASET (vector, i, w->contents); ++i;
10618 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10619 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10620 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10621 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10623 else
10625 int end = i + 6;
10626 for (; i < end; ++i)
10627 ASET (vector, i, Qnil);
10630 eassert (i == ASIZE (vector));
10631 return vector;
10635 /* Restore global state from VECTOR which was created by
10636 with_echo_area_buffer_unwind_data. */
10638 static void
10639 unwind_with_echo_area_buffer (Lisp_Object vector)
10641 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10642 Vdeactivate_mark = AREF (vector, 1);
10643 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10645 if (WINDOWP (AREF (vector, 3)))
10647 struct window *w;
10648 Lisp_Object buffer;
10650 w = XWINDOW (AREF (vector, 3));
10651 buffer = AREF (vector, 4);
10653 wset_buffer (w, buffer);
10654 set_marker_both (w->pointm, buffer,
10655 XFASTINT (AREF (vector, 5)),
10656 XFASTINT (AREF (vector, 6)));
10657 set_marker_both (w->start, buffer,
10658 XFASTINT (AREF (vector, 7)),
10659 XFASTINT (AREF (vector, 8)));
10662 Vwith_echo_area_save_vector = vector;
10666 /* Set up the echo area for use by print functions. MULTIBYTE_P
10667 non-zero means we will print multibyte. */
10669 void
10670 setup_echo_area_for_printing (int multibyte_p)
10672 /* If we can't find an echo area any more, exit. */
10673 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10674 Fkill_emacs (Qnil);
10676 ensure_echo_area_buffers ();
10678 if (!message_buf_print)
10680 /* A message has been output since the last time we printed.
10681 Choose a fresh echo area buffer. */
10682 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10683 echo_area_buffer[0] = echo_buffer[1];
10684 else
10685 echo_area_buffer[0] = echo_buffer[0];
10687 /* Switch to that buffer and clear it. */
10688 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10689 bset_truncate_lines (current_buffer, Qnil);
10691 if (Z > BEG)
10693 ptrdiff_t count = SPECPDL_INDEX ();
10694 specbind (Qinhibit_read_only, Qt);
10695 /* Note that undo recording is always disabled. */
10696 del_range (BEG, Z);
10697 unbind_to (count, Qnil);
10699 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10701 /* Set up the buffer for the multibyteness we need. */
10702 if (multibyte_p
10703 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10704 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10706 /* Raise the frame containing the echo area. */
10707 if (minibuffer_auto_raise)
10709 struct frame *sf = SELECTED_FRAME ();
10710 Lisp_Object mini_window;
10711 mini_window = FRAME_MINIBUF_WINDOW (sf);
10712 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10715 message_log_maybe_newline ();
10716 message_buf_print = 1;
10718 else
10720 if (NILP (echo_area_buffer[0]))
10722 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10723 echo_area_buffer[0] = echo_buffer[1];
10724 else
10725 echo_area_buffer[0] = echo_buffer[0];
10728 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10730 /* Someone switched buffers between print requests. */
10731 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10732 bset_truncate_lines (current_buffer, Qnil);
10738 /* Display an echo area message in window W. Value is non-zero if W's
10739 height is changed. If display_last_displayed_message_p is
10740 non-zero, display the message that was last displayed, otherwise
10741 display the current message. */
10743 static int
10744 display_echo_area (struct window *w)
10746 int i, no_message_p, window_height_changed_p;
10748 /* Temporarily disable garbage collections while displaying the echo
10749 area. This is done because a GC can print a message itself.
10750 That message would modify the echo area buffer's contents while a
10751 redisplay of the buffer is going on, and seriously confuse
10752 redisplay. */
10753 ptrdiff_t count = inhibit_garbage_collection ();
10755 /* If there is no message, we must call display_echo_area_1
10756 nevertheless because it resizes the window. But we will have to
10757 reset the echo_area_buffer in question to nil at the end because
10758 with_echo_area_buffer will sets it to an empty buffer. */
10759 i = display_last_displayed_message_p ? 1 : 0;
10760 no_message_p = NILP (echo_area_buffer[i]);
10762 window_height_changed_p
10763 = with_echo_area_buffer (w, display_last_displayed_message_p,
10764 display_echo_area_1,
10765 (intptr_t) w, Qnil);
10767 if (no_message_p)
10768 echo_area_buffer[i] = Qnil;
10770 unbind_to (count, Qnil);
10771 return window_height_changed_p;
10775 /* Helper for display_echo_area. Display the current buffer which
10776 contains the current echo area message in window W, a mini-window,
10777 a pointer to which is passed in A1. A2..A4 are currently not used.
10778 Change the height of W so that all of the message is displayed.
10779 Value is non-zero if height of W was changed. */
10781 static int
10782 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10784 intptr_t i1 = a1;
10785 struct window *w = (struct window *) i1;
10786 Lisp_Object window;
10787 struct text_pos start;
10788 int window_height_changed_p = 0;
10790 /* Do this before displaying, so that we have a large enough glyph
10791 matrix for the display. If we can't get enough space for the
10792 whole text, display the last N lines. That works by setting w->start. */
10793 window_height_changed_p = resize_mini_window (w, 0);
10795 /* Use the starting position chosen by resize_mini_window. */
10796 SET_TEXT_POS_FROM_MARKER (start, w->start);
10798 /* Display. */
10799 clear_glyph_matrix (w->desired_matrix);
10800 XSETWINDOW (window, w);
10801 try_window (window, start, 0);
10803 return window_height_changed_p;
10807 /* Resize the echo area window to exactly the size needed for the
10808 currently displayed message, if there is one. If a mini-buffer
10809 is active, don't shrink it. */
10811 void
10812 resize_echo_area_exactly (void)
10814 if (BUFFERP (echo_area_buffer[0])
10815 && WINDOWP (echo_area_window))
10817 struct window *w = XWINDOW (echo_area_window);
10818 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10819 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10820 (intptr_t) w, resize_exactly);
10821 if (resized_p)
10823 windows_or_buffers_changed = 42;
10824 update_mode_lines = 30;
10825 redisplay_internal ();
10831 /* Callback function for with_echo_area_buffer, when used from
10832 resize_echo_area_exactly. A1 contains a pointer to the window to
10833 resize, EXACTLY non-nil means resize the mini-window exactly to the
10834 size of the text displayed. A3 and A4 are not used. Value is what
10835 resize_mini_window returns. */
10837 static int
10838 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10840 intptr_t i1 = a1;
10841 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10845 /* Resize mini-window W to fit the size of its contents. EXACT_P
10846 means size the window exactly to the size needed. Otherwise, it's
10847 only enlarged until W's buffer is empty.
10849 Set W->start to the right place to begin display. If the whole
10850 contents fit, start at the beginning. Otherwise, start so as
10851 to make the end of the contents appear. This is particularly
10852 important for y-or-n-p, but seems desirable generally.
10854 Value is non-zero if the window height has been changed. */
10857 resize_mini_window (struct window *w, int exact_p)
10859 struct frame *f = XFRAME (w->frame);
10860 int window_height_changed_p = 0;
10862 eassert (MINI_WINDOW_P (w));
10864 /* By default, start display at the beginning. */
10865 set_marker_both (w->start, w->contents,
10866 BUF_BEGV (XBUFFER (w->contents)),
10867 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10869 /* Don't resize windows while redisplaying a window; it would
10870 confuse redisplay functions when the size of the window they are
10871 displaying changes from under them. Such a resizing can happen,
10872 for instance, when which-func prints a long message while
10873 we are running fontification-functions. We're running these
10874 functions with safe_call which binds inhibit-redisplay to t. */
10875 if (!NILP (Vinhibit_redisplay))
10876 return 0;
10878 /* Nil means don't try to resize. */
10879 if (NILP (Vresize_mini_windows)
10880 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10881 return 0;
10883 if (!FRAME_MINIBUF_ONLY_P (f))
10885 struct it it;
10886 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10887 + WINDOW_PIXEL_HEIGHT (w));
10888 int unit = FRAME_LINE_HEIGHT (f);
10889 int height, max_height;
10890 struct text_pos start;
10891 struct buffer *old_current_buffer = NULL;
10893 if (current_buffer != XBUFFER (w->contents))
10895 old_current_buffer = current_buffer;
10896 set_buffer_internal (XBUFFER (w->contents));
10899 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10901 /* Compute the max. number of lines specified by the user. */
10902 if (FLOATP (Vmax_mini_window_height))
10903 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10904 else if (INTEGERP (Vmax_mini_window_height))
10905 max_height = XINT (Vmax_mini_window_height) * unit;
10906 else
10907 max_height = total_height / 4;
10909 /* Correct that max. height if it's bogus. */
10910 max_height = clip_to_bounds (unit, max_height, total_height);
10912 /* Find out the height of the text in the window. */
10913 if (it.line_wrap == TRUNCATE)
10914 height = unit;
10915 else
10917 last_height = 0;
10918 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10919 if (it.max_ascent == 0 && it.max_descent == 0)
10920 height = it.current_y + last_height;
10921 else
10922 height = it.current_y + it.max_ascent + it.max_descent;
10923 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10926 /* Compute a suitable window start. */
10927 if (height > max_height)
10929 height = (max_height / unit) * unit;
10930 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10931 move_it_vertically_backward (&it, height - unit);
10932 start = it.current.pos;
10934 else
10935 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10936 SET_MARKER_FROM_TEXT_POS (w->start, start);
10938 if (EQ (Vresize_mini_windows, Qgrow_only))
10940 /* Let it grow only, until we display an empty message, in which
10941 case the window shrinks again. */
10942 if (height > WINDOW_PIXEL_HEIGHT (w))
10944 int old_height = WINDOW_PIXEL_HEIGHT (w);
10946 FRAME_WINDOWS_FROZEN (f) = 1;
10947 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10948 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10950 else if (height < WINDOW_PIXEL_HEIGHT (w)
10951 && (exact_p || BEGV == ZV))
10953 int old_height = WINDOW_PIXEL_HEIGHT (w);
10955 FRAME_WINDOWS_FROZEN (f) = 0;
10956 shrink_mini_window (w, 1);
10957 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10960 else
10962 /* Always resize to exact size needed. */
10963 if (height > WINDOW_PIXEL_HEIGHT (w))
10965 int old_height = WINDOW_PIXEL_HEIGHT (w);
10967 FRAME_WINDOWS_FROZEN (f) = 1;
10968 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10969 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10971 else if (height < WINDOW_PIXEL_HEIGHT (w))
10973 int old_height = WINDOW_PIXEL_HEIGHT (w);
10975 FRAME_WINDOWS_FROZEN (f) = 0;
10976 shrink_mini_window (w, 1);
10978 if (height)
10980 FRAME_WINDOWS_FROZEN (f) = 1;
10981 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10984 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10988 if (old_current_buffer)
10989 set_buffer_internal (old_current_buffer);
10992 return window_height_changed_p;
10996 /* Value is the current message, a string, or nil if there is no
10997 current message. */
10999 Lisp_Object
11000 current_message (void)
11002 Lisp_Object msg;
11004 if (!BUFFERP (echo_area_buffer[0]))
11005 msg = Qnil;
11006 else
11008 with_echo_area_buffer (0, 0, current_message_1,
11009 (intptr_t) &msg, Qnil);
11010 if (NILP (msg))
11011 echo_area_buffer[0] = Qnil;
11014 return msg;
11018 static int
11019 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11021 intptr_t i1 = a1;
11022 Lisp_Object *msg = (Lisp_Object *) i1;
11024 if (Z > BEG)
11025 *msg = make_buffer_string (BEG, Z, 1);
11026 else
11027 *msg = Qnil;
11028 return 0;
11032 /* Push the current message on Vmessage_stack for later restoration
11033 by restore_message. Value is non-zero if the current message isn't
11034 empty. This is a relatively infrequent operation, so it's not
11035 worth optimizing. */
11037 bool
11038 push_message (void)
11040 Lisp_Object msg = current_message ();
11041 Vmessage_stack = Fcons (msg, Vmessage_stack);
11042 return STRINGP (msg);
11046 /* Restore message display from the top of Vmessage_stack. */
11048 void
11049 restore_message (void)
11051 eassert (CONSP (Vmessage_stack));
11052 message3_nolog (XCAR (Vmessage_stack));
11056 /* Handler for unwind-protect calling pop_message. */
11058 void
11059 pop_message_unwind (void)
11061 /* Pop the top-most entry off Vmessage_stack. */
11062 eassert (CONSP (Vmessage_stack));
11063 Vmessage_stack = XCDR (Vmessage_stack);
11067 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11068 exits. If the stack is not empty, we have a missing pop_message
11069 somewhere. */
11071 void
11072 check_message_stack (void)
11074 if (!NILP (Vmessage_stack))
11075 emacs_abort ();
11079 /* Truncate to NCHARS what will be displayed in the echo area the next
11080 time we display it---but don't redisplay it now. */
11082 void
11083 truncate_echo_area (ptrdiff_t nchars)
11085 if (nchars == 0)
11086 echo_area_buffer[0] = Qnil;
11087 else if (!noninteractive
11088 && INTERACTIVE
11089 && !NILP (echo_area_buffer[0]))
11091 struct frame *sf = SELECTED_FRAME ();
11092 /* Error messages get reported properly by cmd_error, so this must be
11093 just an informative message; if the frame hasn't really been
11094 initialized yet, just toss it. */
11095 if (sf->glyphs_initialized_p)
11096 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11101 /* Helper function for truncate_echo_area. Truncate the current
11102 message to at most NCHARS characters. */
11104 static int
11105 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11107 if (BEG + nchars < Z)
11108 del_range (BEG + nchars, Z);
11109 if (Z == BEG)
11110 echo_area_buffer[0] = Qnil;
11111 return 0;
11114 /* Set the current message to STRING. */
11116 static void
11117 set_message (Lisp_Object string)
11119 eassert (STRINGP (string));
11121 message_enable_multibyte = STRING_MULTIBYTE (string);
11123 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11124 message_buf_print = 0;
11125 help_echo_showing_p = 0;
11127 if (STRINGP (Vdebug_on_message)
11128 && STRINGP (string)
11129 && fast_string_match (Vdebug_on_message, string) >= 0)
11130 call_debugger (list2 (Qerror, string));
11134 /* Helper function for set_message. First argument is ignored and second
11135 argument has the same meaning as for set_message.
11136 This function is called with the echo area buffer being current. */
11138 static int
11139 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11141 eassert (STRINGP (string));
11143 /* Change multibyteness of the echo buffer appropriately. */
11144 if (message_enable_multibyte
11145 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11146 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11148 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11149 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11150 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11152 /* Insert new message at BEG. */
11153 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11155 /* This function takes care of single/multibyte conversion.
11156 We just have to ensure that the echo area buffer has the right
11157 setting of enable_multibyte_characters. */
11158 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11160 return 0;
11164 /* Clear messages. CURRENT_P non-zero means clear the current
11165 message. LAST_DISPLAYED_P non-zero means clear the message
11166 last displayed. */
11168 void
11169 clear_message (bool current_p, bool last_displayed_p)
11171 if (current_p)
11173 echo_area_buffer[0] = Qnil;
11174 message_cleared_p = true;
11177 if (last_displayed_p)
11178 echo_area_buffer[1] = Qnil;
11180 message_buf_print = 0;
11183 /* Clear garbaged frames.
11185 This function is used where the old redisplay called
11186 redraw_garbaged_frames which in turn called redraw_frame which in
11187 turn called clear_frame. The call to clear_frame was a source of
11188 flickering. I believe a clear_frame is not necessary. It should
11189 suffice in the new redisplay to invalidate all current matrices,
11190 and ensure a complete redisplay of all windows. */
11192 static void
11193 clear_garbaged_frames (void)
11195 if (frame_garbaged)
11197 Lisp_Object tail, frame;
11199 FOR_EACH_FRAME (tail, frame)
11201 struct frame *f = XFRAME (frame);
11203 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11205 if (f->resized_p)
11206 redraw_frame (f);
11207 else
11208 clear_current_matrices (f);
11209 fset_redisplay (f);
11210 f->garbaged = false;
11211 f->resized_p = false;
11215 frame_garbaged = false;
11220 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11221 is non-zero update selected_frame. Value is non-zero if the
11222 mini-windows height has been changed. */
11224 static int
11225 echo_area_display (int update_frame_p)
11227 Lisp_Object mini_window;
11228 struct window *w;
11229 struct frame *f;
11230 int window_height_changed_p = 0;
11231 struct frame *sf = SELECTED_FRAME ();
11233 mini_window = FRAME_MINIBUF_WINDOW (sf);
11234 w = XWINDOW (mini_window);
11235 f = XFRAME (WINDOW_FRAME (w));
11237 /* Don't display if frame is invisible or not yet initialized. */
11238 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11239 return 0;
11241 #ifdef HAVE_WINDOW_SYSTEM
11242 /* When Emacs starts, selected_frame may be the initial terminal
11243 frame. If we let this through, a message would be displayed on
11244 the terminal. */
11245 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11246 return 0;
11247 #endif /* HAVE_WINDOW_SYSTEM */
11249 /* Redraw garbaged frames. */
11250 clear_garbaged_frames ();
11252 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11254 echo_area_window = mini_window;
11255 window_height_changed_p = display_echo_area (w);
11256 w->must_be_updated_p = true;
11258 /* Update the display, unless called from redisplay_internal.
11259 Also don't update the screen during redisplay itself. The
11260 update will happen at the end of redisplay, and an update
11261 here could cause confusion. */
11262 if (update_frame_p && !redisplaying_p)
11264 int n = 0;
11266 /* If the display update has been interrupted by pending
11267 input, update mode lines in the frame. Due to the
11268 pending input, it might have been that redisplay hasn't
11269 been called, so that mode lines above the echo area are
11270 garbaged. This looks odd, so we prevent it here. */
11271 if (!display_completed)
11272 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11274 if (window_height_changed_p
11275 /* Don't do this if Emacs is shutting down. Redisplay
11276 needs to run hooks. */
11277 && !NILP (Vrun_hooks))
11279 /* Must update other windows. Likewise as in other
11280 cases, don't let this update be interrupted by
11281 pending input. */
11282 ptrdiff_t count = SPECPDL_INDEX ();
11283 specbind (Qredisplay_dont_pause, Qt);
11284 windows_or_buffers_changed = 44;
11285 redisplay_internal ();
11286 unbind_to (count, Qnil);
11288 else if (FRAME_WINDOW_P (f) && n == 0)
11290 /* Window configuration is the same as before.
11291 Can do with a display update of the echo area,
11292 unless we displayed some mode lines. */
11293 update_single_window (w, 1);
11294 flush_frame (f);
11296 else
11297 update_frame (f, 1, 1);
11299 /* If cursor is in the echo area, make sure that the next
11300 redisplay displays the minibuffer, so that the cursor will
11301 be replaced with what the minibuffer wants. */
11302 if (cursor_in_echo_area)
11303 wset_redisplay (XWINDOW (mini_window));
11306 else if (!EQ (mini_window, selected_window))
11307 wset_redisplay (XWINDOW (mini_window));
11309 /* Last displayed message is now the current message. */
11310 echo_area_buffer[1] = echo_area_buffer[0];
11311 /* Inform read_char that we're not echoing. */
11312 echo_message_buffer = Qnil;
11314 /* Prevent redisplay optimization in redisplay_internal by resetting
11315 this_line_start_pos. This is done because the mini-buffer now
11316 displays the message instead of its buffer text. */
11317 if (EQ (mini_window, selected_window))
11318 CHARPOS (this_line_start_pos) = 0;
11320 return window_height_changed_p;
11323 /* Nonzero if W's buffer was changed but not saved. */
11325 static int
11326 window_buffer_changed (struct window *w)
11328 struct buffer *b = XBUFFER (w->contents);
11330 eassert (BUFFER_LIVE_P (b));
11332 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11335 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11337 static int
11338 mode_line_update_needed (struct window *w)
11340 return (w->column_number_displayed != -1
11341 && !(PT == w->last_point && !window_outdated (w))
11342 && (w->column_number_displayed != current_column ()));
11345 /* Nonzero if window start of W is frozen and may not be changed during
11346 redisplay. */
11348 static bool
11349 window_frozen_p (struct window *w)
11351 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11353 Lisp_Object window;
11355 XSETWINDOW (window, w);
11356 if (MINI_WINDOW_P (w))
11357 return 0;
11358 else if (EQ (window, selected_window))
11359 return 0;
11360 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11361 && EQ (window, Vminibuf_scroll_window))
11362 /* This special window can't be frozen too. */
11363 return 0;
11364 else
11365 return 1;
11367 return 0;
11370 /***********************************************************************
11371 Mode Lines and Frame Titles
11372 ***********************************************************************/
11374 /* A buffer for constructing non-propertized mode-line strings and
11375 frame titles in it; allocated from the heap in init_xdisp and
11376 resized as needed in store_mode_line_noprop_char. */
11378 static char *mode_line_noprop_buf;
11380 /* The buffer's end, and a current output position in it. */
11382 static char *mode_line_noprop_buf_end;
11383 static char *mode_line_noprop_ptr;
11385 #define MODE_LINE_NOPROP_LEN(start) \
11386 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11388 static enum {
11389 MODE_LINE_DISPLAY = 0,
11390 MODE_LINE_TITLE,
11391 MODE_LINE_NOPROP,
11392 MODE_LINE_STRING
11393 } mode_line_target;
11395 /* Alist that caches the results of :propertize.
11396 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11397 static Lisp_Object mode_line_proptrans_alist;
11399 /* List of strings making up the mode-line. */
11400 static Lisp_Object mode_line_string_list;
11402 /* Base face property when building propertized mode line string. */
11403 static Lisp_Object mode_line_string_face;
11404 static Lisp_Object mode_line_string_face_prop;
11407 /* Unwind data for mode line strings */
11409 static Lisp_Object Vmode_line_unwind_vector;
11411 static Lisp_Object
11412 format_mode_line_unwind_data (struct frame *target_frame,
11413 struct buffer *obuf,
11414 Lisp_Object owin,
11415 int save_proptrans)
11417 Lisp_Object vector, tmp;
11419 /* Reduce consing by keeping one vector in
11420 Vwith_echo_area_save_vector. */
11421 vector = Vmode_line_unwind_vector;
11422 Vmode_line_unwind_vector = Qnil;
11424 if (NILP (vector))
11425 vector = Fmake_vector (make_number (10), Qnil);
11427 ASET (vector, 0, make_number (mode_line_target));
11428 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11429 ASET (vector, 2, mode_line_string_list);
11430 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11431 ASET (vector, 4, mode_line_string_face);
11432 ASET (vector, 5, mode_line_string_face_prop);
11434 if (obuf)
11435 XSETBUFFER (tmp, obuf);
11436 else
11437 tmp = Qnil;
11438 ASET (vector, 6, tmp);
11439 ASET (vector, 7, owin);
11440 if (target_frame)
11442 /* Similarly to `with-selected-window', if the operation selects
11443 a window on another frame, we must restore that frame's
11444 selected window, and (for a tty) the top-frame. */
11445 ASET (vector, 8, target_frame->selected_window);
11446 if (FRAME_TERMCAP_P (target_frame))
11447 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11450 return vector;
11453 static void
11454 unwind_format_mode_line (Lisp_Object vector)
11456 Lisp_Object old_window = AREF (vector, 7);
11457 Lisp_Object target_frame_window = AREF (vector, 8);
11458 Lisp_Object old_top_frame = AREF (vector, 9);
11460 mode_line_target = XINT (AREF (vector, 0));
11461 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11462 mode_line_string_list = AREF (vector, 2);
11463 if (! EQ (AREF (vector, 3), Qt))
11464 mode_line_proptrans_alist = AREF (vector, 3);
11465 mode_line_string_face = AREF (vector, 4);
11466 mode_line_string_face_prop = AREF (vector, 5);
11468 /* Select window before buffer, since it may change the buffer. */
11469 if (!NILP (old_window))
11471 /* If the operation that we are unwinding had selected a window
11472 on a different frame, reset its frame-selected-window. For a
11473 text terminal, reset its top-frame if necessary. */
11474 if (!NILP (target_frame_window))
11476 Lisp_Object frame
11477 = WINDOW_FRAME (XWINDOW (target_frame_window));
11479 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11480 Fselect_window (target_frame_window, Qt);
11482 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11483 Fselect_frame (old_top_frame, Qt);
11486 Fselect_window (old_window, Qt);
11489 if (!NILP (AREF (vector, 6)))
11491 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11492 ASET (vector, 6, Qnil);
11495 Vmode_line_unwind_vector = vector;
11499 /* Store a single character C for the frame title in mode_line_noprop_buf.
11500 Re-allocate mode_line_noprop_buf if necessary. */
11502 static void
11503 store_mode_line_noprop_char (char c)
11505 /* If output position has reached the end of the allocated buffer,
11506 increase the buffer's size. */
11507 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11509 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11510 ptrdiff_t size = len;
11511 mode_line_noprop_buf =
11512 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11513 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11514 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11517 *mode_line_noprop_ptr++ = c;
11521 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11522 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11523 characters that yield more columns than PRECISION; PRECISION <= 0
11524 means copy the whole string. Pad with spaces until FIELD_WIDTH
11525 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11526 pad. Called from display_mode_element when it is used to build a
11527 frame title. */
11529 static int
11530 store_mode_line_noprop (const char *string, int field_width, int precision)
11532 const unsigned char *str = (const unsigned char *) string;
11533 int n = 0;
11534 ptrdiff_t dummy, nbytes;
11536 /* Copy at most PRECISION chars from STR. */
11537 nbytes = strlen (string);
11538 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11539 while (nbytes--)
11540 store_mode_line_noprop_char (*str++);
11542 /* Fill up with spaces until FIELD_WIDTH reached. */
11543 while (field_width > 0
11544 && n < field_width)
11546 store_mode_line_noprop_char (' ');
11547 ++n;
11550 return n;
11553 /***********************************************************************
11554 Frame Titles
11555 ***********************************************************************/
11557 #ifdef HAVE_WINDOW_SYSTEM
11559 /* Set the title of FRAME, if it has changed. The title format is
11560 Vicon_title_format if FRAME is iconified, otherwise it is
11561 frame_title_format. */
11563 static void
11564 x_consider_frame_title (Lisp_Object frame)
11566 struct frame *f = XFRAME (frame);
11568 if (FRAME_WINDOW_P (f)
11569 || FRAME_MINIBUF_ONLY_P (f)
11570 || f->explicit_name)
11572 /* Do we have more than one visible frame on this X display? */
11573 Lisp_Object tail, other_frame, fmt;
11574 ptrdiff_t title_start;
11575 char *title;
11576 ptrdiff_t len;
11577 struct it it;
11578 ptrdiff_t count = SPECPDL_INDEX ();
11580 FOR_EACH_FRAME (tail, other_frame)
11582 struct frame *tf = XFRAME (other_frame);
11584 if (tf != f
11585 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11586 && !FRAME_MINIBUF_ONLY_P (tf)
11587 && !EQ (other_frame, tip_frame)
11588 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11589 break;
11592 /* Set global variable indicating that multiple frames exist. */
11593 multiple_frames = CONSP (tail);
11595 /* Switch to the buffer of selected window of the frame. Set up
11596 mode_line_target so that display_mode_element will output into
11597 mode_line_noprop_buf; then display the title. */
11598 record_unwind_protect (unwind_format_mode_line,
11599 format_mode_line_unwind_data
11600 (f, current_buffer, selected_window, 0));
11602 Fselect_window (f->selected_window, Qt);
11603 set_buffer_internal_1
11604 (XBUFFER (XWINDOW (f->selected_window)->contents));
11605 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11607 mode_line_target = MODE_LINE_TITLE;
11608 title_start = MODE_LINE_NOPROP_LEN (0);
11609 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11610 NULL, DEFAULT_FACE_ID);
11611 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11612 len = MODE_LINE_NOPROP_LEN (title_start);
11613 title = mode_line_noprop_buf + title_start;
11614 unbind_to (count, Qnil);
11616 /* Set the title only if it's changed. This avoids consing in
11617 the common case where it hasn't. (If it turns out that we've
11618 already wasted too much time by walking through the list with
11619 display_mode_element, then we might need to optimize at a
11620 higher level than this.) */
11621 if (! STRINGP (f->name)
11622 || SBYTES (f->name) != len
11623 || memcmp (title, SDATA (f->name), len) != 0)
11624 x_implicitly_set_name (f, make_string (title, len), Qnil);
11628 #endif /* not HAVE_WINDOW_SYSTEM */
11631 /***********************************************************************
11632 Menu Bars
11633 ***********************************************************************/
11635 /* Non-zero if we will not redisplay all visible windows. */
11636 #define REDISPLAY_SOME_P() \
11637 ((windows_or_buffers_changed == 0 \
11638 || windows_or_buffers_changed == REDISPLAY_SOME) \
11639 && (update_mode_lines == 0 \
11640 || update_mode_lines == REDISPLAY_SOME))
11642 /* Prepare for redisplay by updating menu-bar item lists when
11643 appropriate. This can call eval. */
11645 static void
11646 prepare_menu_bars (void)
11648 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11649 bool some_windows = REDISPLAY_SOME_P ();
11650 struct gcpro gcpro1, gcpro2;
11651 Lisp_Object tooltip_frame;
11653 #ifdef HAVE_WINDOW_SYSTEM
11654 tooltip_frame = tip_frame;
11655 #else
11656 tooltip_frame = Qnil;
11657 #endif
11659 if (FUNCTIONP (Vpre_redisplay_function))
11661 Lisp_Object windows = all_windows ? Qt : Qnil;
11662 if (all_windows && some_windows)
11664 Lisp_Object ws = window_list ();
11665 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11667 Lisp_Object this = XCAR (ws);
11668 struct window *w = XWINDOW (this);
11669 if (w->redisplay
11670 || XFRAME (w->frame)->redisplay
11671 || XBUFFER (w->contents)->text->redisplay)
11673 windows = Fcons (this, windows);
11677 safe__call1 (true, Vpre_redisplay_function, windows);
11680 /* Update all frame titles based on their buffer names, etc. We do
11681 this before the menu bars so that the buffer-menu will show the
11682 up-to-date frame titles. */
11683 #ifdef HAVE_WINDOW_SYSTEM
11684 if (all_windows)
11686 Lisp_Object tail, frame;
11688 FOR_EACH_FRAME (tail, frame)
11690 struct frame *f = XFRAME (frame);
11691 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11692 if (some_windows
11693 && !f->redisplay
11694 && !w->redisplay
11695 && !XBUFFER (w->contents)->text->redisplay)
11696 continue;
11698 if (!EQ (frame, tooltip_frame)
11699 && (FRAME_ICONIFIED_P (f)
11700 || FRAME_VISIBLE_P (f) == 1
11701 /* Exclude TTY frames that are obscured because they
11702 are not the top frame on their console. This is
11703 because x_consider_frame_title actually switches
11704 to the frame, which for TTY frames means it is
11705 marked as garbaged, and will be completely
11706 redrawn on the next redisplay cycle. This causes
11707 TTY frames to be completely redrawn, when there
11708 are more than one of them, even though nothing
11709 should be changed on display. */
11710 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11711 x_consider_frame_title (frame);
11714 #endif /* HAVE_WINDOW_SYSTEM */
11716 /* Update the menu bar item lists, if appropriate. This has to be
11717 done before any actual redisplay or generation of display lines. */
11719 if (all_windows)
11721 Lisp_Object tail, frame;
11722 ptrdiff_t count = SPECPDL_INDEX ();
11723 /* 1 means that update_menu_bar has run its hooks
11724 so any further calls to update_menu_bar shouldn't do so again. */
11725 int menu_bar_hooks_run = 0;
11727 record_unwind_save_match_data ();
11729 FOR_EACH_FRAME (tail, frame)
11731 struct frame *f = XFRAME (frame);
11732 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11734 /* Ignore tooltip frame. */
11735 if (EQ (frame, tooltip_frame))
11736 continue;
11738 if (some_windows
11739 && !f->redisplay
11740 && !w->redisplay
11741 && !XBUFFER (w->contents)->text->redisplay)
11742 continue;
11744 /* If a window on this frame changed size, report that to
11745 the user and clear the size-change flag. */
11746 if (FRAME_WINDOW_SIZES_CHANGED (f))
11748 Lisp_Object functions;
11750 /* Clear flag first in case we get an error below. */
11751 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11752 functions = Vwindow_size_change_functions;
11753 GCPRO2 (tail, functions);
11755 while (CONSP (functions))
11757 if (!EQ (XCAR (functions), Qt))
11758 call1 (XCAR (functions), frame);
11759 functions = XCDR (functions);
11761 UNGCPRO;
11764 GCPRO1 (tail);
11765 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11766 #ifdef HAVE_WINDOW_SYSTEM
11767 update_tool_bar (f, 0);
11768 #endif
11769 #ifdef HAVE_NS
11770 if (windows_or_buffers_changed
11771 && FRAME_NS_P (f))
11772 ns_set_doc_edited
11773 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11774 #endif
11775 UNGCPRO;
11778 unbind_to (count, Qnil);
11780 else
11782 struct frame *sf = SELECTED_FRAME ();
11783 update_menu_bar (sf, 1, 0);
11784 #ifdef HAVE_WINDOW_SYSTEM
11785 update_tool_bar (sf, 1);
11786 #endif
11791 /* Update the menu bar item list for frame F. This has to be done
11792 before we start to fill in any display lines, because it can call
11793 eval.
11795 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11797 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11798 already ran the menu bar hooks for this redisplay, so there
11799 is no need to run them again. The return value is the
11800 updated value of this flag, to pass to the next call. */
11802 static int
11803 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11805 Lisp_Object window;
11806 register struct window *w;
11808 /* If called recursively during a menu update, do nothing. This can
11809 happen when, for instance, an activate-menubar-hook causes a
11810 redisplay. */
11811 if (inhibit_menubar_update)
11812 return hooks_run;
11814 window = FRAME_SELECTED_WINDOW (f);
11815 w = XWINDOW (window);
11817 if (FRAME_WINDOW_P (f)
11819 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11820 || defined (HAVE_NS) || defined (USE_GTK)
11821 FRAME_EXTERNAL_MENU_BAR (f)
11822 #else
11823 FRAME_MENU_BAR_LINES (f) > 0
11824 #endif
11825 : FRAME_MENU_BAR_LINES (f) > 0)
11827 /* If the user has switched buffers or windows, we need to
11828 recompute to reflect the new bindings. But we'll
11829 recompute when update_mode_lines is set too; that means
11830 that people can use force-mode-line-update to request
11831 that the menu bar be recomputed. The adverse effect on
11832 the rest of the redisplay algorithm is about the same as
11833 windows_or_buffers_changed anyway. */
11834 if (windows_or_buffers_changed
11835 /* This used to test w->update_mode_line, but we believe
11836 there is no need to recompute the menu in that case. */
11837 || update_mode_lines
11838 || window_buffer_changed (w))
11840 struct buffer *prev = current_buffer;
11841 ptrdiff_t count = SPECPDL_INDEX ();
11843 specbind (Qinhibit_menubar_update, Qt);
11845 set_buffer_internal_1 (XBUFFER (w->contents));
11846 if (save_match_data)
11847 record_unwind_save_match_data ();
11848 if (NILP (Voverriding_local_map_menu_flag))
11850 specbind (Qoverriding_terminal_local_map, Qnil);
11851 specbind (Qoverriding_local_map, Qnil);
11854 if (!hooks_run)
11856 /* Run the Lucid hook. */
11857 safe_run_hooks (Qactivate_menubar_hook);
11859 /* If it has changed current-menubar from previous value,
11860 really recompute the menu-bar from the value. */
11861 if (! NILP (Vlucid_menu_bar_dirty_flag))
11862 call0 (Qrecompute_lucid_menubar);
11864 safe_run_hooks (Qmenu_bar_update_hook);
11866 hooks_run = 1;
11869 XSETFRAME (Vmenu_updating_frame, f);
11870 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11872 /* Redisplay the menu bar in case we changed it. */
11873 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11874 || defined (HAVE_NS) || defined (USE_GTK)
11875 if (FRAME_WINDOW_P (f))
11877 #if defined (HAVE_NS)
11878 /* All frames on Mac OS share the same menubar. So only
11879 the selected frame should be allowed to set it. */
11880 if (f == SELECTED_FRAME ())
11881 #endif
11882 set_frame_menubar (f, 0, 0);
11884 else
11885 /* On a terminal screen, the menu bar is an ordinary screen
11886 line, and this makes it get updated. */
11887 w->update_mode_line = 1;
11888 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11889 /* In the non-toolkit version, the menu bar is an ordinary screen
11890 line, and this makes it get updated. */
11891 w->update_mode_line = 1;
11892 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11894 unbind_to (count, Qnil);
11895 set_buffer_internal_1 (prev);
11899 return hooks_run;
11902 /***********************************************************************
11903 Tool-bars
11904 ***********************************************************************/
11906 #ifdef HAVE_WINDOW_SYSTEM
11908 /* Tool-bar item index of the item on which a mouse button was pressed
11909 or -1. */
11911 int last_tool_bar_item;
11913 /* Select `frame' temporarily without running all the code in
11914 do_switch_frame.
11915 FIXME: Maybe do_switch_frame should be trimmed down similarly
11916 when `norecord' is set. */
11917 static void
11918 fast_set_selected_frame (Lisp_Object frame)
11920 if (!EQ (selected_frame, frame))
11922 selected_frame = frame;
11923 selected_window = XFRAME (frame)->selected_window;
11927 /* Update the tool-bar item list for frame F. This has to be done
11928 before we start to fill in any display lines. Called from
11929 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11930 and restore it here. */
11932 static void
11933 update_tool_bar (struct frame *f, int save_match_data)
11935 #if defined (USE_GTK) || defined (HAVE_NS)
11936 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11937 #else
11938 int do_update = (WINDOWP (f->tool_bar_window)
11939 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0);
11940 #endif
11942 if (do_update)
11944 Lisp_Object window;
11945 struct window *w;
11947 window = FRAME_SELECTED_WINDOW (f);
11948 w = XWINDOW (window);
11950 /* If the user has switched buffers or windows, we need to
11951 recompute to reflect the new bindings. But we'll
11952 recompute when update_mode_lines is set too; that means
11953 that people can use force-mode-line-update to request
11954 that the menu bar be recomputed. The adverse effect on
11955 the rest of the redisplay algorithm is about the same as
11956 windows_or_buffers_changed anyway. */
11957 if (windows_or_buffers_changed
11958 || w->update_mode_line
11959 || update_mode_lines
11960 || window_buffer_changed (w))
11962 struct buffer *prev = current_buffer;
11963 ptrdiff_t count = SPECPDL_INDEX ();
11964 Lisp_Object frame, new_tool_bar;
11965 int new_n_tool_bar;
11966 struct gcpro gcpro1;
11968 /* Set current_buffer to the buffer of the selected
11969 window of the frame, so that we get the right local
11970 keymaps. */
11971 set_buffer_internal_1 (XBUFFER (w->contents));
11973 /* Save match data, if we must. */
11974 if (save_match_data)
11975 record_unwind_save_match_data ();
11977 /* Make sure that we don't accidentally use bogus keymaps. */
11978 if (NILP (Voverriding_local_map_menu_flag))
11980 specbind (Qoverriding_terminal_local_map, Qnil);
11981 specbind (Qoverriding_local_map, Qnil);
11984 GCPRO1 (new_tool_bar);
11986 /* We must temporarily set the selected frame to this frame
11987 before calling tool_bar_items, because the calculation of
11988 the tool-bar keymap uses the selected frame (see
11989 `tool-bar-make-keymap' in tool-bar.el). */
11990 eassert (EQ (selected_window,
11991 /* Since we only explicitly preserve selected_frame,
11992 check that selected_window would be redundant. */
11993 XFRAME (selected_frame)->selected_window));
11994 record_unwind_protect (fast_set_selected_frame, selected_frame);
11995 XSETFRAME (frame, f);
11996 fast_set_selected_frame (frame);
11998 /* Build desired tool-bar items from keymaps. */
11999 new_tool_bar
12000 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12001 &new_n_tool_bar);
12003 /* Redisplay the tool-bar if we changed it. */
12004 if (new_n_tool_bar != f->n_tool_bar_items
12005 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12007 /* Redisplay that happens asynchronously due to an expose event
12008 may access f->tool_bar_items. Make sure we update both
12009 variables within BLOCK_INPUT so no such event interrupts. */
12010 block_input ();
12011 fset_tool_bar_items (f, new_tool_bar);
12012 f->n_tool_bar_items = new_n_tool_bar;
12013 w->update_mode_line = 1;
12014 unblock_input ();
12017 UNGCPRO;
12019 unbind_to (count, Qnil);
12020 set_buffer_internal_1 (prev);
12025 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12027 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12028 F's desired tool-bar contents. F->tool_bar_items must have
12029 been set up previously by calling prepare_menu_bars. */
12031 static void
12032 build_desired_tool_bar_string (struct frame *f)
12034 int i, size, size_needed;
12035 struct gcpro gcpro1, gcpro2, gcpro3;
12036 Lisp_Object image, plist, props;
12038 image = plist = props = Qnil;
12039 GCPRO3 (image, plist, props);
12041 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12042 Otherwise, make a new string. */
12044 /* The size of the string we might be able to reuse. */
12045 size = (STRINGP (f->desired_tool_bar_string)
12046 ? SCHARS (f->desired_tool_bar_string)
12047 : 0);
12049 /* We need one space in the string for each image. */
12050 size_needed = f->n_tool_bar_items;
12052 /* Reuse f->desired_tool_bar_string, if possible. */
12053 if (size < size_needed || NILP (f->desired_tool_bar_string))
12054 fset_desired_tool_bar_string
12055 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12056 else
12058 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
12059 Fremove_text_properties (make_number (0), make_number (size),
12060 props, f->desired_tool_bar_string);
12063 /* Put a `display' property on the string for the images to display,
12064 put a `menu_item' property on tool-bar items with a value that
12065 is the index of the item in F's tool-bar item vector. */
12066 for (i = 0; i < f->n_tool_bar_items; ++i)
12068 #define PROP(IDX) \
12069 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12071 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12072 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12073 int hmargin, vmargin, relief, idx, end;
12075 /* If image is a vector, choose the image according to the
12076 button state. */
12077 image = PROP (TOOL_BAR_ITEM_IMAGES);
12078 if (VECTORP (image))
12080 if (enabled_p)
12081 idx = (selected_p
12082 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12083 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12084 else
12085 idx = (selected_p
12086 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12087 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12089 eassert (ASIZE (image) >= idx);
12090 image = AREF (image, idx);
12092 else
12093 idx = -1;
12095 /* Ignore invalid image specifications. */
12096 if (!valid_image_p (image))
12097 continue;
12099 /* Display the tool-bar button pressed, or depressed. */
12100 plist = Fcopy_sequence (XCDR (image));
12102 /* Compute margin and relief to draw. */
12103 relief = (tool_bar_button_relief >= 0
12104 ? tool_bar_button_relief
12105 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12106 hmargin = vmargin = relief;
12108 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12109 INT_MAX - max (hmargin, vmargin)))
12111 hmargin += XFASTINT (Vtool_bar_button_margin);
12112 vmargin += XFASTINT (Vtool_bar_button_margin);
12114 else if (CONSP (Vtool_bar_button_margin))
12116 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12117 INT_MAX - hmargin))
12118 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12120 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12121 INT_MAX - vmargin))
12122 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12125 if (auto_raise_tool_bar_buttons_p)
12127 /* Add a `:relief' property to the image spec if the item is
12128 selected. */
12129 if (selected_p)
12131 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12132 hmargin -= relief;
12133 vmargin -= relief;
12136 else
12138 /* If image is selected, display it pressed, i.e. with a
12139 negative relief. If it's not selected, display it with a
12140 raised relief. */
12141 plist = Fplist_put (plist, QCrelief,
12142 (selected_p
12143 ? make_number (-relief)
12144 : make_number (relief)));
12145 hmargin -= relief;
12146 vmargin -= relief;
12149 /* Put a margin around the image. */
12150 if (hmargin || vmargin)
12152 if (hmargin == vmargin)
12153 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12154 else
12155 plist = Fplist_put (plist, QCmargin,
12156 Fcons (make_number (hmargin),
12157 make_number (vmargin)));
12160 /* If button is not enabled, and we don't have special images
12161 for the disabled state, make the image appear disabled by
12162 applying an appropriate algorithm to it. */
12163 if (!enabled_p && idx < 0)
12164 plist = Fplist_put (plist, QCconversion, Qdisabled);
12166 /* Put a `display' text property on the string for the image to
12167 display. Put a `menu-item' property on the string that gives
12168 the start of this item's properties in the tool-bar items
12169 vector. */
12170 image = Fcons (Qimage, plist);
12171 props = list4 (Qdisplay, image,
12172 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
12174 /* Let the last image hide all remaining spaces in the tool bar
12175 string. The string can be longer than needed when we reuse a
12176 previous string. */
12177 if (i + 1 == f->n_tool_bar_items)
12178 end = SCHARS (f->desired_tool_bar_string);
12179 else
12180 end = i + 1;
12181 Fadd_text_properties (make_number (i), make_number (end),
12182 props, f->desired_tool_bar_string);
12183 #undef PROP
12186 UNGCPRO;
12190 /* Display one line of the tool-bar of frame IT->f.
12192 HEIGHT specifies the desired height of the tool-bar line.
12193 If the actual height of the glyph row is less than HEIGHT, the
12194 row's height is increased to HEIGHT, and the icons are centered
12195 vertically in the new height.
12197 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12198 count a final empty row in case the tool-bar width exactly matches
12199 the window width.
12202 static void
12203 display_tool_bar_line (struct it *it, int height)
12205 struct glyph_row *row = it->glyph_row;
12206 int max_x = it->last_visible_x;
12207 struct glyph *last;
12209 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12210 clear_glyph_row (row);
12211 row->enabled_p = true;
12212 row->y = it->current_y;
12214 /* Note that this isn't made use of if the face hasn't a box,
12215 so there's no need to check the face here. */
12216 it->start_of_box_run_p = 1;
12218 while (it->current_x < max_x)
12220 int x, n_glyphs_before, i, nglyphs;
12221 struct it it_before;
12223 /* Get the next display element. */
12224 if (!get_next_display_element (it))
12226 /* Don't count empty row if we are counting needed tool-bar lines. */
12227 if (height < 0 && !it->hpos)
12228 return;
12229 break;
12232 /* Produce glyphs. */
12233 n_glyphs_before = row->used[TEXT_AREA];
12234 it_before = *it;
12236 PRODUCE_GLYPHS (it);
12238 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12239 i = 0;
12240 x = it_before.current_x;
12241 while (i < nglyphs)
12243 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12245 if (x + glyph->pixel_width > max_x)
12247 /* Glyph doesn't fit on line. Backtrack. */
12248 row->used[TEXT_AREA] = n_glyphs_before;
12249 *it = it_before;
12250 /* If this is the only glyph on this line, it will never fit on the
12251 tool-bar, so skip it. But ensure there is at least one glyph,
12252 so we don't accidentally disable the tool-bar. */
12253 if (n_glyphs_before == 0
12254 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12255 break;
12256 goto out;
12259 ++it->hpos;
12260 x += glyph->pixel_width;
12261 ++i;
12264 /* Stop at line end. */
12265 if (ITERATOR_AT_END_OF_LINE_P (it))
12266 break;
12268 set_iterator_to_next (it, 1);
12271 out:;
12273 row->displays_text_p = row->used[TEXT_AREA] != 0;
12275 /* Use default face for the border below the tool bar.
12277 FIXME: When auto-resize-tool-bars is grow-only, there is
12278 no additional border below the possibly empty tool-bar lines.
12279 So to make the extra empty lines look "normal", we have to
12280 use the tool-bar face for the border too. */
12281 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12282 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12283 it->face_id = DEFAULT_FACE_ID;
12285 extend_face_to_end_of_line (it);
12286 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12287 last->right_box_line_p = 1;
12288 if (last == row->glyphs[TEXT_AREA])
12289 last->left_box_line_p = 1;
12291 /* Make line the desired height and center it vertically. */
12292 if ((height -= it->max_ascent + it->max_descent) > 0)
12294 /* Don't add more than one line height. */
12295 height %= FRAME_LINE_HEIGHT (it->f);
12296 it->max_ascent += height / 2;
12297 it->max_descent += (height + 1) / 2;
12300 compute_line_metrics (it);
12302 /* If line is empty, make it occupy the rest of the tool-bar. */
12303 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12305 row->height = row->phys_height = it->last_visible_y - row->y;
12306 row->visible_height = row->height;
12307 row->ascent = row->phys_ascent = 0;
12308 row->extra_line_spacing = 0;
12311 row->full_width_p = 1;
12312 row->continued_p = 0;
12313 row->truncated_on_left_p = 0;
12314 row->truncated_on_right_p = 0;
12316 it->current_x = it->hpos = 0;
12317 it->current_y += row->height;
12318 ++it->vpos;
12319 ++it->glyph_row;
12323 /* Max tool-bar height. Basically, this is what makes all other windows
12324 disappear when the frame gets too small. Rethink this! */
12326 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
12327 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
12329 /* Value is the number of pixels needed to make all tool-bar items of
12330 frame F visible. The actual number of glyph rows needed is
12331 returned in *N_ROWS if non-NULL. */
12333 static int
12334 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12336 struct window *w = XWINDOW (f->tool_bar_window);
12337 struct it it;
12338 /* tool_bar_height is called from redisplay_tool_bar after building
12339 the desired matrix, so use (unused) mode-line row as temporary row to
12340 avoid destroying the first tool-bar row. */
12341 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12343 /* Initialize an iterator for iteration over
12344 F->desired_tool_bar_string in the tool-bar window of frame F. */
12345 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12346 it.first_visible_x = 0;
12347 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12348 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12349 it.paragraph_embedding = L2R;
12351 while (!ITERATOR_AT_END_P (&it))
12353 clear_glyph_row (temp_row);
12354 it.glyph_row = temp_row;
12355 display_tool_bar_line (&it, -1);
12357 clear_glyph_row (temp_row);
12359 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12360 if (n_rows)
12361 *n_rows = it.vpos > 0 ? it.vpos : -1;
12363 if (pixelwise)
12364 return it.current_y;
12365 else
12366 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12369 #endif /* !USE_GTK && !HAVE_NS */
12371 #if defined USE_GTK || defined HAVE_NS
12372 EXFUN (Ftool_bar_height, 2) ATTRIBUTE_CONST;
12373 EXFUN (Ftool_bar_lines_needed, 1) ATTRIBUTE_CONST;
12374 #endif
12376 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12377 0, 2, 0,
12378 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12379 If FRAME is nil or omitted, use the selected frame. Optional argument
12380 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12381 (Lisp_Object frame, Lisp_Object pixelwise)
12383 int height = 0;
12385 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12386 struct frame *f = decode_any_frame (frame);
12388 if (WINDOWP (f->tool_bar_window)
12389 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12391 update_tool_bar (f, 1);
12392 if (f->n_tool_bar_items)
12394 build_desired_tool_bar_string (f);
12395 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12398 #endif
12400 return make_number (height);
12404 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12405 height should be changed. */
12407 static int
12408 redisplay_tool_bar (struct frame *f)
12410 #if defined (USE_GTK) || defined (HAVE_NS)
12412 if (FRAME_EXTERNAL_TOOL_BAR (f))
12413 update_frame_tool_bar (f);
12414 return 0;
12416 #else /* !USE_GTK && !HAVE_NS */
12418 struct window *w;
12419 struct it it;
12420 struct glyph_row *row;
12422 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12423 do anything. This means you must start with tool-bar-lines
12424 non-zero to get the auto-sizing effect. Or in other words, you
12425 can turn off tool-bars by specifying tool-bar-lines zero. */
12426 if (!WINDOWP (f->tool_bar_window)
12427 || (w = XWINDOW (f->tool_bar_window),
12428 WINDOW_PIXEL_HEIGHT (w) == 0))
12429 return 0;
12431 /* Set up an iterator for the tool-bar window. */
12432 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12433 it.first_visible_x = 0;
12434 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12435 row = it.glyph_row;
12437 /* Build a string that represents the contents of the tool-bar. */
12438 build_desired_tool_bar_string (f);
12439 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12440 /* FIXME: This should be controlled by a user option. But it
12441 doesn't make sense to have an R2L tool bar if the menu bar cannot
12442 be drawn also R2L, and making the menu bar R2L is tricky due
12443 toolkit-specific code that implements it. If an R2L tool bar is
12444 ever supported, display_tool_bar_line should also be augmented to
12445 call unproduce_glyphs like display_line and display_string
12446 do. */
12447 it.paragraph_embedding = L2R;
12449 if (f->n_tool_bar_rows == 0)
12451 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12453 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12455 Lisp_Object frame;
12456 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12457 / FRAME_LINE_HEIGHT (f));
12459 XSETFRAME (frame, f);
12460 Fmodify_frame_parameters (frame,
12461 list1 (Fcons (Qtool_bar_lines,
12462 make_number (new_lines))));
12463 /* Always do that now. */
12464 clear_glyph_matrix (w->desired_matrix);
12465 f->fonts_changed = 1;
12466 return 1;
12470 /* Display as many lines as needed to display all tool-bar items. */
12472 if (f->n_tool_bar_rows > 0)
12474 int border, rows, height, extra;
12476 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12477 border = XINT (Vtool_bar_border);
12478 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12479 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12480 else if (EQ (Vtool_bar_border, Qborder_width))
12481 border = f->border_width;
12482 else
12483 border = 0;
12484 if (border < 0)
12485 border = 0;
12487 rows = f->n_tool_bar_rows;
12488 height = max (1, (it.last_visible_y - border) / rows);
12489 extra = it.last_visible_y - border - height * rows;
12491 while (it.current_y < it.last_visible_y)
12493 int h = 0;
12494 if (extra > 0 && rows-- > 0)
12496 h = (extra + rows - 1) / rows;
12497 extra -= h;
12499 display_tool_bar_line (&it, height + h);
12502 else
12504 while (it.current_y < it.last_visible_y)
12505 display_tool_bar_line (&it, 0);
12508 /* It doesn't make much sense to try scrolling in the tool-bar
12509 window, so don't do it. */
12510 w->desired_matrix->no_scrolling_p = 1;
12511 w->must_be_updated_p = 1;
12513 if (!NILP (Vauto_resize_tool_bars))
12515 /* Do we really allow the toolbar to occupy the whole frame? */
12516 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12517 int change_height_p = 0;
12519 /* If we couldn't display everything, change the tool-bar's
12520 height if there is room for more. */
12521 if (IT_STRING_CHARPOS (it) < it.end_charpos
12522 && it.current_y < max_tool_bar_height)
12523 change_height_p = 1;
12525 /* We subtract 1 because display_tool_bar_line advances the
12526 glyph_row pointer before returning to its caller. We want to
12527 examine the last glyph row produced by
12528 display_tool_bar_line. */
12529 row = it.glyph_row - 1;
12531 /* If there are blank lines at the end, except for a partially
12532 visible blank line at the end that is smaller than
12533 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12534 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12535 && row->height >= FRAME_LINE_HEIGHT (f))
12536 change_height_p = 1;
12538 /* If row displays tool-bar items, but is partially visible,
12539 change the tool-bar's height. */
12540 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12541 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12542 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12543 change_height_p = 1;
12545 /* Resize windows as needed by changing the `tool-bar-lines'
12546 frame parameter. */
12547 if (change_height_p)
12549 Lisp_Object frame;
12550 int nrows;
12551 int new_height = tool_bar_height (f, &nrows, 1);
12553 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12554 && !f->minimize_tool_bar_window_p)
12555 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12556 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12557 f->minimize_tool_bar_window_p = 0;
12559 if (change_height_p)
12561 /* Current size of the tool-bar window in canonical line
12562 units. */
12563 int old_lines = WINDOW_TOTAL_LINES (w);
12564 /* Required size of the tool-bar window in canonical
12565 line units. */
12566 int new_lines = ((new_height + FRAME_LINE_HEIGHT (f) - 1)
12567 / FRAME_LINE_HEIGHT (f));
12568 /* Maximum size of the tool-bar window in canonical line
12569 units that this frame can allow. */
12570 int max_lines =
12571 WINDOW_TOTAL_LINES (XWINDOW (FRAME_ROOT_WINDOW (f))) - 1;
12573 /* Don't try to change the tool-bar window size and set
12574 the fonts_changed flag unless really necessary. That
12575 flag causes redisplay to give up and retry
12576 redisplaying the frame from scratch, so setting it
12577 unnecessarily can lead to nasty redisplay loops. */
12578 if (new_lines <= max_lines
12579 && eabs (new_lines - old_lines) >= 1)
12581 XSETFRAME (frame, f);
12582 Fmodify_frame_parameters (frame,
12583 list1 (Fcons (Qtool_bar_lines,
12584 make_number (new_lines))));
12585 clear_glyph_matrix (w->desired_matrix);
12586 f->n_tool_bar_rows = nrows;
12587 f->fonts_changed = 1;
12588 return 1;
12594 f->minimize_tool_bar_window_p = 0;
12595 return 0;
12597 #endif /* USE_GTK || HAVE_NS */
12600 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12602 /* Get information about the tool-bar item which is displayed in GLYPH
12603 on frame F. Return in *PROP_IDX the index where tool-bar item
12604 properties start in F->tool_bar_items. Value is zero if
12605 GLYPH doesn't display a tool-bar item. */
12607 static int
12608 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12610 Lisp_Object prop;
12611 int success_p;
12612 int charpos;
12614 /* This function can be called asynchronously, which means we must
12615 exclude any possibility that Fget_text_property signals an
12616 error. */
12617 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12618 charpos = max (0, charpos);
12620 /* Get the text property `menu-item' at pos. The value of that
12621 property is the start index of this item's properties in
12622 F->tool_bar_items. */
12623 prop = Fget_text_property (make_number (charpos),
12624 Qmenu_item, f->current_tool_bar_string);
12625 if (INTEGERP (prop))
12627 *prop_idx = XINT (prop);
12628 success_p = 1;
12630 else
12631 success_p = 0;
12633 return success_p;
12637 /* Get information about the tool-bar item at position X/Y on frame F.
12638 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12639 the current matrix of the tool-bar window of F, or NULL if not
12640 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12641 item in F->tool_bar_items. Value is
12643 -1 if X/Y is not on a tool-bar item
12644 0 if X/Y is on the same item that was highlighted before.
12645 1 otherwise. */
12647 static int
12648 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12649 int *hpos, int *vpos, int *prop_idx)
12651 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12652 struct window *w = XWINDOW (f->tool_bar_window);
12653 int area;
12655 /* Find the glyph under X/Y. */
12656 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12657 if (*glyph == NULL)
12658 return -1;
12660 /* Get the start of this tool-bar item's properties in
12661 f->tool_bar_items. */
12662 if (!tool_bar_item_info (f, *glyph, prop_idx))
12663 return -1;
12665 /* Is mouse on the highlighted item? */
12666 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12667 && *vpos >= hlinfo->mouse_face_beg_row
12668 && *vpos <= hlinfo->mouse_face_end_row
12669 && (*vpos > hlinfo->mouse_face_beg_row
12670 || *hpos >= hlinfo->mouse_face_beg_col)
12671 && (*vpos < hlinfo->mouse_face_end_row
12672 || *hpos < hlinfo->mouse_face_end_col
12673 || hlinfo->mouse_face_past_end))
12674 return 0;
12676 return 1;
12680 /* EXPORT:
12681 Handle mouse button event on the tool-bar of frame F, at
12682 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12683 0 for button release. MODIFIERS is event modifiers for button
12684 release. */
12686 void
12687 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12688 int modifiers)
12690 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12691 struct window *w = XWINDOW (f->tool_bar_window);
12692 int hpos, vpos, prop_idx;
12693 struct glyph *glyph;
12694 Lisp_Object enabled_p;
12695 int ts;
12697 /* If not on the highlighted tool-bar item, and mouse-highlight is
12698 non-nil, return. This is so we generate the tool-bar button
12699 click only when the mouse button is released on the same item as
12700 where it was pressed. However, when mouse-highlight is disabled,
12701 generate the click when the button is released regardless of the
12702 highlight, since tool-bar items are not highlighted in that
12703 case. */
12704 frame_to_window_pixel_xy (w, &x, &y);
12705 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12706 if (ts == -1
12707 || (ts != 0 && !NILP (Vmouse_highlight)))
12708 return;
12710 /* When mouse-highlight is off, generate the click for the item
12711 where the button was pressed, disregarding where it was
12712 released. */
12713 if (NILP (Vmouse_highlight) && !down_p)
12714 prop_idx = last_tool_bar_item;
12716 /* If item is disabled, do nothing. */
12717 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12718 if (NILP (enabled_p))
12719 return;
12721 if (down_p)
12723 /* Show item in pressed state. */
12724 if (!NILP (Vmouse_highlight))
12725 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12726 last_tool_bar_item = prop_idx;
12728 else
12730 Lisp_Object key, frame;
12731 struct input_event event;
12732 EVENT_INIT (event);
12734 /* Show item in released state. */
12735 if (!NILP (Vmouse_highlight))
12736 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12738 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12740 XSETFRAME (frame, f);
12741 event.kind = TOOL_BAR_EVENT;
12742 event.frame_or_window = frame;
12743 event.arg = frame;
12744 kbd_buffer_store_event (&event);
12746 event.kind = TOOL_BAR_EVENT;
12747 event.frame_or_window = frame;
12748 event.arg = key;
12749 event.modifiers = modifiers;
12750 kbd_buffer_store_event (&event);
12751 last_tool_bar_item = -1;
12756 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12757 tool-bar window-relative coordinates X/Y. Called from
12758 note_mouse_highlight. */
12760 static void
12761 note_tool_bar_highlight (struct frame *f, int x, int y)
12763 Lisp_Object window = f->tool_bar_window;
12764 struct window *w = XWINDOW (window);
12765 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12766 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12767 int hpos, vpos;
12768 struct glyph *glyph;
12769 struct glyph_row *row;
12770 int i;
12771 Lisp_Object enabled_p;
12772 int prop_idx;
12773 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12774 int mouse_down_p, rc;
12776 /* Function note_mouse_highlight is called with negative X/Y
12777 values when mouse moves outside of the frame. */
12778 if (x <= 0 || y <= 0)
12780 clear_mouse_face (hlinfo);
12781 return;
12784 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12785 if (rc < 0)
12787 /* Not on tool-bar item. */
12788 clear_mouse_face (hlinfo);
12789 return;
12791 else if (rc == 0)
12792 /* On same tool-bar item as before. */
12793 goto set_help_echo;
12795 clear_mouse_face (hlinfo);
12797 /* Mouse is down, but on different tool-bar item? */
12798 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12799 && f == dpyinfo->last_mouse_frame);
12801 if (mouse_down_p
12802 && last_tool_bar_item != prop_idx)
12803 return;
12805 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12807 /* If tool-bar item is not enabled, don't highlight it. */
12808 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12809 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12811 /* Compute the x-position of the glyph. In front and past the
12812 image is a space. We include this in the highlighted area. */
12813 row = MATRIX_ROW (w->current_matrix, vpos);
12814 for (i = x = 0; i < hpos; ++i)
12815 x += row->glyphs[TEXT_AREA][i].pixel_width;
12817 /* Record this as the current active region. */
12818 hlinfo->mouse_face_beg_col = hpos;
12819 hlinfo->mouse_face_beg_row = vpos;
12820 hlinfo->mouse_face_beg_x = x;
12821 hlinfo->mouse_face_past_end = 0;
12823 hlinfo->mouse_face_end_col = hpos + 1;
12824 hlinfo->mouse_face_end_row = vpos;
12825 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12826 hlinfo->mouse_face_window = window;
12827 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12829 /* Display it as active. */
12830 show_mouse_face (hlinfo, draw);
12833 set_help_echo:
12835 /* Set help_echo_string to a help string to display for this tool-bar item.
12836 XTread_socket does the rest. */
12837 help_echo_object = help_echo_window = Qnil;
12838 help_echo_pos = -1;
12839 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12840 if (NILP (help_echo_string))
12841 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12844 #endif /* !USE_GTK && !HAVE_NS */
12846 #endif /* HAVE_WINDOW_SYSTEM */
12850 /************************************************************************
12851 Horizontal scrolling
12852 ************************************************************************/
12854 static int hscroll_window_tree (Lisp_Object);
12855 static int hscroll_windows (Lisp_Object);
12857 /* For all leaf windows in the window tree rooted at WINDOW, set their
12858 hscroll value so that PT is (i) visible in the window, and (ii) so
12859 that it is not within a certain margin at the window's left and
12860 right border. Value is non-zero if any window's hscroll has been
12861 changed. */
12863 static int
12864 hscroll_window_tree (Lisp_Object window)
12866 int hscrolled_p = 0;
12867 int hscroll_relative_p = FLOATP (Vhscroll_step);
12868 int hscroll_step_abs = 0;
12869 double hscroll_step_rel = 0;
12871 if (hscroll_relative_p)
12873 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12874 if (hscroll_step_rel < 0)
12876 hscroll_relative_p = 0;
12877 hscroll_step_abs = 0;
12880 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12882 hscroll_step_abs = XINT (Vhscroll_step);
12883 if (hscroll_step_abs < 0)
12884 hscroll_step_abs = 0;
12886 else
12887 hscroll_step_abs = 0;
12889 while (WINDOWP (window))
12891 struct window *w = XWINDOW (window);
12893 if (WINDOWP (w->contents))
12894 hscrolled_p |= hscroll_window_tree (w->contents);
12895 else if (w->cursor.vpos >= 0)
12897 int h_margin;
12898 int text_area_width;
12899 struct glyph_row *cursor_row;
12900 struct glyph_row *bottom_row;
12901 int row_r2l_p;
12903 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12904 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12905 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12906 else
12907 cursor_row = bottom_row - 1;
12909 if (!cursor_row->enabled_p)
12911 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12912 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12913 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12914 else
12915 cursor_row = bottom_row - 1;
12917 row_r2l_p = cursor_row->reversed_p;
12919 text_area_width = window_box_width (w, TEXT_AREA);
12921 /* Scroll when cursor is inside this scroll margin. */
12922 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12924 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12925 /* In some pathological cases, like restoring a window
12926 configuration into a frame that is much smaller than
12927 the one from which the configuration was saved, we
12928 get glyph rows whose start and end have zero buffer
12929 positions, which we cannot handle below. Just skip
12930 such windows. */
12931 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12932 /* For left-to-right rows, hscroll when cursor is either
12933 (i) inside the right hscroll margin, or (ii) if it is
12934 inside the left margin and the window is already
12935 hscrolled. */
12936 && ((!row_r2l_p
12937 && ((w->hscroll
12938 && w->cursor.x <= h_margin)
12939 || (cursor_row->enabled_p
12940 && cursor_row->truncated_on_right_p
12941 && (w->cursor.x >= text_area_width - h_margin))))
12942 /* For right-to-left rows, the logic is similar,
12943 except that rules for scrolling to left and right
12944 are reversed. E.g., if cursor.x <= h_margin, we
12945 need to hscroll "to the right" unconditionally,
12946 and that will scroll the screen to the left so as
12947 to reveal the next portion of the row. */
12948 || (row_r2l_p
12949 && ((cursor_row->enabled_p
12950 /* FIXME: It is confusing to set the
12951 truncated_on_right_p flag when R2L rows
12952 are actually truncated on the left. */
12953 && cursor_row->truncated_on_right_p
12954 && w->cursor.x <= h_margin)
12955 || (w->hscroll
12956 && (w->cursor.x >= text_area_width - h_margin))))))
12958 struct it it;
12959 ptrdiff_t hscroll;
12960 struct buffer *saved_current_buffer;
12961 ptrdiff_t pt;
12962 int wanted_x;
12964 /* Find point in a display of infinite width. */
12965 saved_current_buffer = current_buffer;
12966 current_buffer = XBUFFER (w->contents);
12968 if (w == XWINDOW (selected_window))
12969 pt = PT;
12970 else
12971 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12973 /* Move iterator to pt starting at cursor_row->start in
12974 a line with infinite width. */
12975 init_to_row_start (&it, w, cursor_row);
12976 it.last_visible_x = INFINITY;
12977 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12978 current_buffer = saved_current_buffer;
12980 /* Position cursor in window. */
12981 if (!hscroll_relative_p && hscroll_step_abs == 0)
12982 hscroll = max (0, (it.current_x
12983 - (ITERATOR_AT_END_OF_LINE_P (&it)
12984 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12985 : (text_area_width / 2))))
12986 / FRAME_COLUMN_WIDTH (it.f);
12987 else if ((!row_r2l_p
12988 && w->cursor.x >= text_area_width - h_margin)
12989 || (row_r2l_p && w->cursor.x <= h_margin))
12991 if (hscroll_relative_p)
12992 wanted_x = text_area_width * (1 - hscroll_step_rel)
12993 - h_margin;
12994 else
12995 wanted_x = text_area_width
12996 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12997 - h_margin;
12998 hscroll
12999 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13001 else
13003 if (hscroll_relative_p)
13004 wanted_x = text_area_width * hscroll_step_rel
13005 + h_margin;
13006 else
13007 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13008 + h_margin;
13009 hscroll
13010 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13012 hscroll = max (hscroll, w->min_hscroll);
13014 /* Don't prevent redisplay optimizations if hscroll
13015 hasn't changed, as it will unnecessarily slow down
13016 redisplay. */
13017 if (w->hscroll != hscroll)
13019 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
13020 w->hscroll = hscroll;
13021 hscrolled_p = 1;
13026 window = w->next;
13029 /* Value is non-zero if hscroll of any leaf window has been changed. */
13030 return hscrolled_p;
13034 /* Set hscroll so that cursor is visible and not inside horizontal
13035 scroll margins for all windows in the tree rooted at WINDOW. See
13036 also hscroll_window_tree above. Value is non-zero if any window's
13037 hscroll has been changed. If it has, desired matrices on the frame
13038 of WINDOW are cleared. */
13040 static int
13041 hscroll_windows (Lisp_Object window)
13043 int hscrolled_p = hscroll_window_tree (window);
13044 if (hscrolled_p)
13045 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13046 return hscrolled_p;
13051 /************************************************************************
13052 Redisplay
13053 ************************************************************************/
13055 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
13056 to a non-zero value. This is sometimes handy to have in a debugger
13057 session. */
13059 #ifdef GLYPH_DEBUG
13061 /* First and last unchanged row for try_window_id. */
13063 static int debug_first_unchanged_at_end_vpos;
13064 static int debug_last_unchanged_at_beg_vpos;
13066 /* Delta vpos and y. */
13068 static int debug_dvpos, debug_dy;
13070 /* Delta in characters and bytes for try_window_id. */
13072 static ptrdiff_t debug_delta, debug_delta_bytes;
13074 /* Values of window_end_pos and window_end_vpos at the end of
13075 try_window_id. */
13077 static ptrdiff_t debug_end_vpos;
13079 /* Append a string to W->desired_matrix->method. FMT is a printf
13080 format string. If trace_redisplay_p is true also printf the
13081 resulting string to stderr. */
13083 static void debug_method_add (struct window *, char const *, ...)
13084 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13086 static void
13087 debug_method_add (struct window *w, char const *fmt, ...)
13089 void *ptr = w;
13090 char *method = w->desired_matrix->method;
13091 int len = strlen (method);
13092 int size = sizeof w->desired_matrix->method;
13093 int remaining = size - len - 1;
13094 va_list ap;
13096 if (len && remaining)
13098 method[len] = '|';
13099 --remaining, ++len;
13102 va_start (ap, fmt);
13103 vsnprintf (method + len, remaining + 1, fmt, ap);
13104 va_end (ap);
13106 if (trace_redisplay_p)
13107 fprintf (stderr, "%p (%s): %s\n",
13108 ptr,
13109 ((BUFFERP (w->contents)
13110 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13111 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13112 : "no buffer"),
13113 method + len);
13116 #endif /* GLYPH_DEBUG */
13119 /* Value is non-zero if all changes in window W, which displays
13120 current_buffer, are in the text between START and END. START is a
13121 buffer position, END is given as a distance from Z. Used in
13122 redisplay_internal for display optimization. */
13124 static int
13125 text_outside_line_unchanged_p (struct window *w,
13126 ptrdiff_t start, ptrdiff_t end)
13128 int unchanged_p = 1;
13130 /* If text or overlays have changed, see where. */
13131 if (window_outdated (w))
13133 /* Gap in the line? */
13134 if (GPT < start || Z - GPT < end)
13135 unchanged_p = 0;
13137 /* Changes start in front of the line, or end after it? */
13138 if (unchanged_p
13139 && (BEG_UNCHANGED < start - 1
13140 || END_UNCHANGED < end))
13141 unchanged_p = 0;
13143 /* If selective display, can't optimize if changes start at the
13144 beginning of the line. */
13145 if (unchanged_p
13146 && INTEGERP (BVAR (current_buffer, selective_display))
13147 && XINT (BVAR (current_buffer, selective_display)) > 0
13148 && (BEG_UNCHANGED < start || GPT <= start))
13149 unchanged_p = 0;
13151 /* If there are overlays at the start or end of the line, these
13152 may have overlay strings with newlines in them. A change at
13153 START, for instance, may actually concern the display of such
13154 overlay strings as well, and they are displayed on different
13155 lines. So, quickly rule out this case. (For the future, it
13156 might be desirable to implement something more telling than
13157 just BEG/END_UNCHANGED.) */
13158 if (unchanged_p)
13160 if (BEG + BEG_UNCHANGED == start
13161 && overlay_touches_p (start))
13162 unchanged_p = 0;
13163 if (END_UNCHANGED == end
13164 && overlay_touches_p (Z - end))
13165 unchanged_p = 0;
13168 /* Under bidi reordering, adding or deleting a character in the
13169 beginning of a paragraph, before the first strong directional
13170 character, can change the base direction of the paragraph (unless
13171 the buffer specifies a fixed paragraph direction), which will
13172 require to redisplay the whole paragraph. It might be worthwhile
13173 to find the paragraph limits and widen the range of redisplayed
13174 lines to that, but for now just give up this optimization. */
13175 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13176 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13177 unchanged_p = 0;
13180 return unchanged_p;
13184 /* Do a frame update, taking possible shortcuts into account. This is
13185 the main external entry point for redisplay.
13187 If the last redisplay displayed an echo area message and that message
13188 is no longer requested, we clear the echo area or bring back the
13189 mini-buffer if that is in use. */
13191 void
13192 redisplay (void)
13194 redisplay_internal ();
13198 static Lisp_Object
13199 overlay_arrow_string_or_property (Lisp_Object var)
13201 Lisp_Object val;
13203 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13204 return val;
13206 return Voverlay_arrow_string;
13209 /* Return 1 if there are any overlay-arrows in current_buffer. */
13210 static int
13211 overlay_arrow_in_current_buffer_p (void)
13213 Lisp_Object vlist;
13215 for (vlist = Voverlay_arrow_variable_list;
13216 CONSP (vlist);
13217 vlist = XCDR (vlist))
13219 Lisp_Object var = XCAR (vlist);
13220 Lisp_Object val;
13222 if (!SYMBOLP (var))
13223 continue;
13224 val = find_symbol_value (var);
13225 if (MARKERP (val)
13226 && current_buffer == XMARKER (val)->buffer)
13227 return 1;
13229 return 0;
13233 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13234 has changed. */
13236 static int
13237 overlay_arrows_changed_p (void)
13239 Lisp_Object vlist;
13241 for (vlist = Voverlay_arrow_variable_list;
13242 CONSP (vlist);
13243 vlist = XCDR (vlist))
13245 Lisp_Object var = XCAR (vlist);
13246 Lisp_Object val, pstr;
13248 if (!SYMBOLP (var))
13249 continue;
13250 val = find_symbol_value (var);
13251 if (!MARKERP (val))
13252 continue;
13253 if (! EQ (COERCE_MARKER (val),
13254 Fget (var, Qlast_arrow_position))
13255 || ! (pstr = overlay_arrow_string_or_property (var),
13256 EQ (pstr, Fget (var, Qlast_arrow_string))))
13257 return 1;
13259 return 0;
13262 /* Mark overlay arrows to be updated on next redisplay. */
13264 static void
13265 update_overlay_arrows (int up_to_date)
13267 Lisp_Object vlist;
13269 for (vlist = Voverlay_arrow_variable_list;
13270 CONSP (vlist);
13271 vlist = XCDR (vlist))
13273 Lisp_Object var = XCAR (vlist);
13275 if (!SYMBOLP (var))
13276 continue;
13278 if (up_to_date > 0)
13280 Lisp_Object val = find_symbol_value (var);
13281 Fput (var, Qlast_arrow_position,
13282 COERCE_MARKER (val));
13283 Fput (var, Qlast_arrow_string,
13284 overlay_arrow_string_or_property (var));
13286 else if (up_to_date < 0
13287 || !NILP (Fget (var, Qlast_arrow_position)))
13289 Fput (var, Qlast_arrow_position, Qt);
13290 Fput (var, Qlast_arrow_string, Qt);
13296 /* Return overlay arrow string to display at row.
13297 Return integer (bitmap number) for arrow bitmap in left fringe.
13298 Return nil if no overlay arrow. */
13300 static Lisp_Object
13301 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13303 Lisp_Object vlist;
13305 for (vlist = Voverlay_arrow_variable_list;
13306 CONSP (vlist);
13307 vlist = XCDR (vlist))
13309 Lisp_Object var = XCAR (vlist);
13310 Lisp_Object val;
13312 if (!SYMBOLP (var))
13313 continue;
13315 val = find_symbol_value (var);
13317 if (MARKERP (val)
13318 && current_buffer == XMARKER (val)->buffer
13319 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13321 if (FRAME_WINDOW_P (it->f)
13322 /* FIXME: if ROW->reversed_p is set, this should test
13323 the right fringe, not the left one. */
13324 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13326 #ifdef HAVE_WINDOW_SYSTEM
13327 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13329 int fringe_bitmap;
13330 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13331 return make_number (fringe_bitmap);
13333 #endif
13334 return make_number (-1); /* Use default arrow bitmap. */
13336 return overlay_arrow_string_or_property (var);
13340 return Qnil;
13343 /* Return 1 if point moved out of or into a composition. Otherwise
13344 return 0. PREV_BUF and PREV_PT are the last point buffer and
13345 position. BUF and PT are the current point buffer and position. */
13347 static int
13348 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13349 struct buffer *buf, ptrdiff_t pt)
13351 ptrdiff_t start, end;
13352 Lisp_Object prop;
13353 Lisp_Object buffer;
13355 XSETBUFFER (buffer, buf);
13356 /* Check a composition at the last point if point moved within the
13357 same buffer. */
13358 if (prev_buf == buf)
13360 if (prev_pt == pt)
13361 /* Point didn't move. */
13362 return 0;
13364 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13365 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13366 && composition_valid_p (start, end, prop)
13367 && start < prev_pt && end > prev_pt)
13368 /* The last point was within the composition. Return 1 iff
13369 point moved out of the composition. */
13370 return (pt <= start || pt >= end);
13373 /* Check a composition at the current point. */
13374 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13375 && find_composition (pt, -1, &start, &end, &prop, buffer)
13376 && composition_valid_p (start, end, prop)
13377 && start < pt && end > pt);
13380 /* Reconsider the clip changes of buffer which is displayed in W. */
13382 static void
13383 reconsider_clip_changes (struct window *w)
13385 struct buffer *b = XBUFFER (w->contents);
13387 if (b->clip_changed
13388 && w->window_end_valid
13389 && w->current_matrix->buffer == b
13390 && w->current_matrix->zv == BUF_ZV (b)
13391 && w->current_matrix->begv == BUF_BEGV (b))
13392 b->clip_changed = 0;
13394 /* If display wasn't paused, and W is not a tool bar window, see if
13395 point has been moved into or out of a composition. In that case,
13396 we set b->clip_changed to 1 to force updating the screen. If
13397 b->clip_changed has already been set to 1, we can skip this
13398 check. */
13399 if (!b->clip_changed && w->window_end_valid)
13401 ptrdiff_t pt = (w == XWINDOW (selected_window)
13402 ? PT : marker_position (w->pointm));
13404 if ((w->current_matrix->buffer != b || pt != w->last_point)
13405 && check_point_in_composition (w->current_matrix->buffer,
13406 w->last_point, b, pt))
13407 b->clip_changed = 1;
13411 static void
13412 propagate_buffer_redisplay (void)
13413 { /* Resetting b->text->redisplay is problematic!
13414 We can't just reset it in the case that some window that displays
13415 it has not been redisplayed; and such a window can stay
13416 unredisplayed for a long time if it's currently invisible.
13417 But we do want to reset it at the end of redisplay otherwise
13418 its displayed windows will keep being redisplayed over and over
13419 again.
13420 So we copy all b->text->redisplay flags up to their windows here,
13421 such that mark_window_display_accurate can safely reset
13422 b->text->redisplay. */
13423 Lisp_Object ws = window_list ();
13424 for (; CONSP (ws); ws = XCDR (ws))
13426 struct window *thisw = XWINDOW (XCAR (ws));
13427 struct buffer *thisb = XBUFFER (thisw->contents);
13428 if (thisb->text->redisplay)
13429 thisw->redisplay = true;
13433 #define STOP_POLLING \
13434 do { if (! polling_stopped_here) stop_polling (); \
13435 polling_stopped_here = 1; } while (0)
13437 #define RESUME_POLLING \
13438 do { if (polling_stopped_here) start_polling (); \
13439 polling_stopped_here = 0; } while (0)
13442 /* Perhaps in the future avoid recentering windows if it
13443 is not necessary; currently that causes some problems. */
13445 static void
13446 redisplay_internal (void)
13448 struct window *w = XWINDOW (selected_window);
13449 struct window *sw;
13450 struct frame *fr;
13451 int pending;
13452 bool must_finish = 0, match_p;
13453 struct text_pos tlbufpos, tlendpos;
13454 int number_of_visible_frames;
13455 ptrdiff_t count;
13456 struct frame *sf;
13457 int polling_stopped_here = 0;
13458 Lisp_Object tail, frame;
13460 /* True means redisplay has to consider all windows on all
13461 frames. False, only selected_window is considered. */
13462 bool consider_all_windows_p;
13464 /* True means redisplay has to redisplay the miniwindow. */
13465 bool update_miniwindow_p = false;
13467 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13469 /* No redisplay if running in batch mode or frame is not yet fully
13470 initialized, or redisplay is explicitly turned off by setting
13471 Vinhibit_redisplay. */
13472 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13473 || !NILP (Vinhibit_redisplay))
13474 return;
13476 /* Don't examine these until after testing Vinhibit_redisplay.
13477 When Emacs is shutting down, perhaps because its connection to
13478 X has dropped, we should not look at them at all. */
13479 fr = XFRAME (w->frame);
13480 sf = SELECTED_FRAME ();
13482 if (!fr->glyphs_initialized_p)
13483 return;
13485 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13486 if (popup_activated ())
13487 return;
13488 #endif
13490 /* I don't think this happens but let's be paranoid. */
13491 if (redisplaying_p)
13492 return;
13494 /* Record a function that clears redisplaying_p
13495 when we leave this function. */
13496 count = SPECPDL_INDEX ();
13497 record_unwind_protect_void (unwind_redisplay);
13498 redisplaying_p = 1;
13499 specbind (Qinhibit_free_realized_faces, Qnil);
13501 /* Record this function, so it appears on the profiler's backtraces. */
13502 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13504 FOR_EACH_FRAME (tail, frame)
13505 XFRAME (frame)->already_hscrolled_p = 0;
13507 retry:
13508 /* Remember the currently selected window. */
13509 sw = w;
13511 pending = 0;
13512 last_escape_glyph_frame = NULL;
13513 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13514 last_glyphless_glyph_frame = NULL;
13515 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13517 /* If face_change_count is non-zero, init_iterator will free all
13518 realized faces, which includes the faces referenced from current
13519 matrices. So, we can't reuse current matrices in this case. */
13520 if (face_change_count)
13521 windows_or_buffers_changed = 47;
13523 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13524 && FRAME_TTY (sf)->previous_frame != sf)
13526 /* Since frames on a single ASCII terminal share the same
13527 display area, displaying a different frame means redisplay
13528 the whole thing. */
13529 SET_FRAME_GARBAGED (sf);
13530 #ifndef DOS_NT
13531 set_tty_color_mode (FRAME_TTY (sf), sf);
13532 #endif
13533 FRAME_TTY (sf)->previous_frame = sf;
13536 /* Set the visible flags for all frames. Do this before checking for
13537 resized or garbaged frames; they want to know if their frames are
13538 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13539 number_of_visible_frames = 0;
13541 FOR_EACH_FRAME (tail, frame)
13543 struct frame *f = XFRAME (frame);
13545 if (FRAME_VISIBLE_P (f))
13547 ++number_of_visible_frames;
13548 /* Adjust matrices for visible frames only. */
13549 if (f->fonts_changed)
13551 adjust_frame_glyphs (f);
13552 f->fonts_changed = 0;
13554 /* If cursor type has been changed on the frame
13555 other than selected, consider all frames. */
13556 if (f != sf && f->cursor_type_changed)
13557 update_mode_lines = 31;
13559 clear_desired_matrices (f);
13562 /* Notice any pending interrupt request to change frame size. */
13563 do_pending_window_change (1);
13565 /* do_pending_window_change could change the selected_window due to
13566 frame resizing which makes the selected window too small. */
13567 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13568 sw = w;
13570 /* Clear frames marked as garbaged. */
13571 clear_garbaged_frames ();
13573 /* Build menubar and tool-bar items. */
13574 if (NILP (Vmemory_full))
13575 prepare_menu_bars ();
13577 reconsider_clip_changes (w);
13579 /* In most cases selected window displays current buffer. */
13580 match_p = XBUFFER (w->contents) == current_buffer;
13581 if (match_p)
13583 /* Detect case that we need to write or remove a star in the mode line. */
13584 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13585 w->update_mode_line = 1;
13587 if (mode_line_update_needed (w))
13588 w->update_mode_line = 1;
13591 /* Normally the message* functions will have already displayed and
13592 updated the echo area, but the frame may have been trashed, or
13593 the update may have been preempted, so display the echo area
13594 again here. Checking message_cleared_p captures the case that
13595 the echo area should be cleared. */
13596 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13597 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13598 || (message_cleared_p
13599 && minibuf_level == 0
13600 /* If the mini-window is currently selected, this means the
13601 echo-area doesn't show through. */
13602 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13604 int window_height_changed_p = echo_area_display (0);
13606 if (message_cleared_p)
13607 update_miniwindow_p = true;
13609 must_finish = 1;
13611 /* If we don't display the current message, don't clear the
13612 message_cleared_p flag, because, if we did, we wouldn't clear
13613 the echo area in the next redisplay which doesn't preserve
13614 the echo area. */
13615 if (!display_last_displayed_message_p)
13616 message_cleared_p = 0;
13618 if (window_height_changed_p)
13620 windows_or_buffers_changed = 50;
13622 /* If window configuration was changed, frames may have been
13623 marked garbaged. Clear them or we will experience
13624 surprises wrt scrolling. */
13625 clear_garbaged_frames ();
13628 else if (EQ (selected_window, minibuf_window)
13629 && (current_buffer->clip_changed || window_outdated (w))
13630 && resize_mini_window (w, 0))
13632 /* Resized active mini-window to fit the size of what it is
13633 showing if its contents might have changed. */
13634 must_finish = 1;
13636 /* If window configuration was changed, frames may have been
13637 marked garbaged. Clear them or we will experience
13638 surprises wrt scrolling. */
13639 clear_garbaged_frames ();
13642 if (windows_or_buffers_changed && !update_mode_lines)
13643 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13644 only the windows's contents needs to be refreshed, or whether the
13645 mode-lines also need a refresh. */
13646 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13647 ? REDISPLAY_SOME : 32);
13649 /* If specs for an arrow have changed, do thorough redisplay
13650 to ensure we remove any arrow that should no longer exist. */
13651 if (overlay_arrows_changed_p ())
13652 /* Apparently, this is the only case where we update other windows,
13653 without updating other mode-lines. */
13654 windows_or_buffers_changed = 49;
13656 consider_all_windows_p = (update_mode_lines
13657 || windows_or_buffers_changed);
13659 #define AINC(a,i) \
13660 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13661 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13663 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13664 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13666 /* Optimize the case that only the line containing the cursor in the
13667 selected window has changed. Variables starting with this_ are
13668 set in display_line and record information about the line
13669 containing the cursor. */
13670 tlbufpos = this_line_start_pos;
13671 tlendpos = this_line_end_pos;
13672 if (!consider_all_windows_p
13673 && CHARPOS (tlbufpos) > 0
13674 && !w->update_mode_line
13675 && !current_buffer->clip_changed
13676 && !current_buffer->prevent_redisplay_optimizations_p
13677 && FRAME_VISIBLE_P (XFRAME (w->frame))
13678 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13679 && !XFRAME (w->frame)->cursor_type_changed
13680 /* Make sure recorded data applies to current buffer, etc. */
13681 && this_line_buffer == current_buffer
13682 && match_p
13683 && !w->force_start
13684 && !w->optional_new_start
13685 /* Point must be on the line that we have info recorded about. */
13686 && PT >= CHARPOS (tlbufpos)
13687 && PT <= Z - CHARPOS (tlendpos)
13688 /* All text outside that line, including its final newline,
13689 must be unchanged. */
13690 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13691 CHARPOS (tlendpos)))
13693 if (CHARPOS (tlbufpos) > BEGV
13694 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13695 && (CHARPOS (tlbufpos) == ZV
13696 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13697 /* Former continuation line has disappeared by becoming empty. */
13698 goto cancel;
13699 else if (window_outdated (w) || MINI_WINDOW_P (w))
13701 /* We have to handle the case of continuation around a
13702 wide-column character (see the comment in indent.c around
13703 line 1340).
13705 For instance, in the following case:
13707 -------- Insert --------
13708 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13709 J_I_ ==> J_I_ `^^' are cursors.
13710 ^^ ^^
13711 -------- --------
13713 As we have to redraw the line above, we cannot use this
13714 optimization. */
13716 struct it it;
13717 int line_height_before = this_line_pixel_height;
13719 /* Note that start_display will handle the case that the
13720 line starting at tlbufpos is a continuation line. */
13721 start_display (&it, w, tlbufpos);
13723 /* Implementation note: It this still necessary? */
13724 if (it.current_x != this_line_start_x)
13725 goto cancel;
13727 TRACE ((stderr, "trying display optimization 1\n"));
13728 w->cursor.vpos = -1;
13729 overlay_arrow_seen = 0;
13730 it.vpos = this_line_vpos;
13731 it.current_y = this_line_y;
13732 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13733 display_line (&it);
13735 /* If line contains point, is not continued,
13736 and ends at same distance from eob as before, we win. */
13737 if (w->cursor.vpos >= 0
13738 /* Line is not continued, otherwise this_line_start_pos
13739 would have been set to 0 in display_line. */
13740 && CHARPOS (this_line_start_pos)
13741 /* Line ends as before. */
13742 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13743 /* Line has same height as before. Otherwise other lines
13744 would have to be shifted up or down. */
13745 && this_line_pixel_height == line_height_before)
13747 /* If this is not the window's last line, we must adjust
13748 the charstarts of the lines below. */
13749 if (it.current_y < it.last_visible_y)
13751 struct glyph_row *row
13752 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13753 ptrdiff_t delta, delta_bytes;
13755 /* We used to distinguish between two cases here,
13756 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13757 when the line ends in a newline or the end of the
13758 buffer's accessible portion. But both cases did
13759 the same, so they were collapsed. */
13760 delta = (Z
13761 - CHARPOS (tlendpos)
13762 - MATRIX_ROW_START_CHARPOS (row));
13763 delta_bytes = (Z_BYTE
13764 - BYTEPOS (tlendpos)
13765 - MATRIX_ROW_START_BYTEPOS (row));
13767 increment_matrix_positions (w->current_matrix,
13768 this_line_vpos + 1,
13769 w->current_matrix->nrows,
13770 delta, delta_bytes);
13773 /* If this row displays text now but previously didn't,
13774 or vice versa, w->window_end_vpos may have to be
13775 adjusted. */
13776 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13778 if (w->window_end_vpos < this_line_vpos)
13779 w->window_end_vpos = this_line_vpos;
13781 else if (w->window_end_vpos == this_line_vpos
13782 && this_line_vpos > 0)
13783 w->window_end_vpos = this_line_vpos - 1;
13784 w->window_end_valid = 0;
13786 /* Update hint: No need to try to scroll in update_window. */
13787 w->desired_matrix->no_scrolling_p = 1;
13789 #ifdef GLYPH_DEBUG
13790 *w->desired_matrix->method = 0;
13791 debug_method_add (w, "optimization 1");
13792 #endif
13793 #ifdef HAVE_WINDOW_SYSTEM
13794 update_window_fringes (w, 0);
13795 #endif
13796 goto update;
13798 else
13799 goto cancel;
13801 else if (/* Cursor position hasn't changed. */
13802 PT == w->last_point
13803 /* Make sure the cursor was last displayed
13804 in this window. Otherwise we have to reposition it. */
13806 /* PXW: Must be converted to pixels, probably. */
13807 && 0 <= w->cursor.vpos
13808 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13810 if (!must_finish)
13812 do_pending_window_change (1);
13813 /* If selected_window changed, redisplay again. */
13814 if (WINDOWP (selected_window)
13815 && (w = XWINDOW (selected_window)) != sw)
13816 goto retry;
13818 /* We used to always goto end_of_redisplay here, but this
13819 isn't enough if we have a blinking cursor. */
13820 if (w->cursor_off_p == w->last_cursor_off_p)
13821 goto end_of_redisplay;
13823 goto update;
13825 /* If highlighting the region, or if the cursor is in the echo area,
13826 then we can't just move the cursor. */
13827 else if (NILP (Vshow_trailing_whitespace)
13828 && !cursor_in_echo_area)
13830 struct it it;
13831 struct glyph_row *row;
13833 /* Skip from tlbufpos to PT and see where it is. Note that
13834 PT may be in invisible text. If so, we will end at the
13835 next visible position. */
13836 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13837 NULL, DEFAULT_FACE_ID);
13838 it.current_x = this_line_start_x;
13839 it.current_y = this_line_y;
13840 it.vpos = this_line_vpos;
13842 /* The call to move_it_to stops in front of PT, but
13843 moves over before-strings. */
13844 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13846 if (it.vpos == this_line_vpos
13847 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13848 row->enabled_p))
13850 eassert (this_line_vpos == it.vpos);
13851 eassert (this_line_y == it.current_y);
13852 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13853 #ifdef GLYPH_DEBUG
13854 *w->desired_matrix->method = 0;
13855 debug_method_add (w, "optimization 3");
13856 #endif
13857 goto update;
13859 else
13860 goto cancel;
13863 cancel:
13864 /* Text changed drastically or point moved off of line. */
13865 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13868 CHARPOS (this_line_start_pos) = 0;
13869 ++clear_face_cache_count;
13870 #ifdef HAVE_WINDOW_SYSTEM
13871 ++clear_image_cache_count;
13872 #endif
13874 /* Build desired matrices, and update the display. If
13875 consider_all_windows_p is non-zero, do it for all windows on all
13876 frames. Otherwise do it for selected_window, only. */
13878 if (consider_all_windows_p)
13880 FOR_EACH_FRAME (tail, frame)
13881 XFRAME (frame)->updated_p = 0;
13883 propagate_buffer_redisplay ();
13885 FOR_EACH_FRAME (tail, frame)
13887 struct frame *f = XFRAME (frame);
13889 /* We don't have to do anything for unselected terminal
13890 frames. */
13891 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13892 && !EQ (FRAME_TTY (f)->top_frame, frame))
13893 continue;
13895 retry_frame:
13897 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13899 bool gcscrollbars
13900 /* Only GC scrollbars when we redisplay the whole frame. */
13901 = f->redisplay || !REDISPLAY_SOME_P ();
13902 /* Mark all the scroll bars to be removed; we'll redeem
13903 the ones we want when we redisplay their windows. */
13904 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13905 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13907 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13908 redisplay_windows (FRAME_ROOT_WINDOW (f));
13909 /* Remember that the invisible frames need to be redisplayed next
13910 time they're visible. */
13911 else if (!REDISPLAY_SOME_P ())
13912 f->redisplay = true;
13914 /* The X error handler may have deleted that frame. */
13915 if (!FRAME_LIVE_P (f))
13916 continue;
13918 /* Any scroll bars which redisplay_windows should have
13919 nuked should now go away. */
13920 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13921 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13923 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13925 /* If fonts changed on visible frame, display again. */
13926 if (f->fonts_changed)
13928 adjust_frame_glyphs (f);
13929 f->fonts_changed = 0;
13930 goto retry_frame;
13933 /* See if we have to hscroll. */
13934 if (!f->already_hscrolled_p)
13936 f->already_hscrolled_p = 1;
13937 if (hscroll_windows (f->root_window))
13938 goto retry_frame;
13941 /* Prevent various kinds of signals during display
13942 update. stdio is not robust about handling
13943 signals, which can cause an apparent I/O error. */
13944 if (interrupt_input)
13945 unrequest_sigio ();
13946 STOP_POLLING;
13948 pending |= update_frame (f, 0, 0);
13949 f->cursor_type_changed = 0;
13950 f->updated_p = 1;
13955 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13957 if (!pending)
13959 /* Do the mark_window_display_accurate after all windows have
13960 been redisplayed because this call resets flags in buffers
13961 which are needed for proper redisplay. */
13962 FOR_EACH_FRAME (tail, frame)
13964 struct frame *f = XFRAME (frame);
13965 if (f->updated_p)
13967 f->redisplay = false;
13968 mark_window_display_accurate (f->root_window, 1);
13969 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13970 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13975 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13977 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13978 struct frame *mini_frame;
13980 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13981 /* Use list_of_error, not Qerror, so that
13982 we catch only errors and don't run the debugger. */
13983 internal_condition_case_1 (redisplay_window_1, selected_window,
13984 list_of_error,
13985 redisplay_window_error);
13986 if (update_miniwindow_p)
13987 internal_condition_case_1 (redisplay_window_1, mini_window,
13988 list_of_error,
13989 redisplay_window_error);
13991 /* Compare desired and current matrices, perform output. */
13993 update:
13994 /* If fonts changed, display again. */
13995 if (sf->fonts_changed)
13996 goto retry;
13998 /* Prevent various kinds of signals during display update.
13999 stdio is not robust about handling signals,
14000 which can cause an apparent I/O error. */
14001 if (interrupt_input)
14002 unrequest_sigio ();
14003 STOP_POLLING;
14005 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14007 if (hscroll_windows (selected_window))
14008 goto retry;
14010 XWINDOW (selected_window)->must_be_updated_p = true;
14011 pending = update_frame (sf, 0, 0);
14012 sf->cursor_type_changed = 0;
14015 /* We may have called echo_area_display at the top of this
14016 function. If the echo area is on another frame, that may
14017 have put text on a frame other than the selected one, so the
14018 above call to update_frame would not have caught it. Catch
14019 it here. */
14020 mini_window = FRAME_MINIBUF_WINDOW (sf);
14021 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14023 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14025 XWINDOW (mini_window)->must_be_updated_p = true;
14026 pending |= update_frame (mini_frame, 0, 0);
14027 mini_frame->cursor_type_changed = 0;
14028 if (!pending && hscroll_windows (mini_window))
14029 goto retry;
14033 /* If display was paused because of pending input, make sure we do a
14034 thorough update the next time. */
14035 if (pending)
14037 /* Prevent the optimization at the beginning of
14038 redisplay_internal that tries a single-line update of the
14039 line containing the cursor in the selected window. */
14040 CHARPOS (this_line_start_pos) = 0;
14042 /* Let the overlay arrow be updated the next time. */
14043 update_overlay_arrows (0);
14045 /* If we pause after scrolling, some rows in the current
14046 matrices of some windows are not valid. */
14047 if (!WINDOW_FULL_WIDTH_P (w)
14048 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14049 update_mode_lines = 36;
14051 else
14053 if (!consider_all_windows_p)
14055 /* This has already been done above if
14056 consider_all_windows_p is set. */
14057 if (XBUFFER (w->contents)->text->redisplay
14058 && buffer_window_count (XBUFFER (w->contents)) > 1)
14059 /* This can happen if b->text->redisplay was set during
14060 jit-lock. */
14061 propagate_buffer_redisplay ();
14062 mark_window_display_accurate_1 (w, 1);
14064 /* Say overlay arrows are up to date. */
14065 update_overlay_arrows (1);
14067 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14068 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14071 update_mode_lines = 0;
14072 windows_or_buffers_changed = 0;
14075 /* Start SIGIO interrupts coming again. Having them off during the
14076 code above makes it less likely one will discard output, but not
14077 impossible, since there might be stuff in the system buffer here.
14078 But it is much hairier to try to do anything about that. */
14079 if (interrupt_input)
14080 request_sigio ();
14081 RESUME_POLLING;
14083 /* If a frame has become visible which was not before, redisplay
14084 again, so that we display it. Expose events for such a frame
14085 (which it gets when becoming visible) don't call the parts of
14086 redisplay constructing glyphs, so simply exposing a frame won't
14087 display anything in this case. So, we have to display these
14088 frames here explicitly. */
14089 if (!pending)
14091 int new_count = 0;
14093 FOR_EACH_FRAME (tail, frame)
14095 if (XFRAME (frame)->visible)
14096 new_count++;
14099 if (new_count != number_of_visible_frames)
14100 windows_or_buffers_changed = 52;
14103 /* Change frame size now if a change is pending. */
14104 do_pending_window_change (1);
14106 /* If we just did a pending size change, or have additional
14107 visible frames, or selected_window changed, redisplay again. */
14108 if ((windows_or_buffers_changed && !pending)
14109 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14110 goto retry;
14112 /* Clear the face and image caches.
14114 We used to do this only if consider_all_windows_p. But the cache
14115 needs to be cleared if a timer creates images in the current
14116 buffer (e.g. the test case in Bug#6230). */
14118 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14120 clear_face_cache (0);
14121 clear_face_cache_count = 0;
14124 #ifdef HAVE_WINDOW_SYSTEM
14125 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14127 clear_image_caches (Qnil);
14128 clear_image_cache_count = 0;
14130 #endif /* HAVE_WINDOW_SYSTEM */
14132 end_of_redisplay:
14133 if (interrupt_input && interrupts_deferred)
14134 request_sigio ();
14136 unbind_to (count, Qnil);
14137 RESUME_POLLING;
14141 /* Redisplay, but leave alone any recent echo area message unless
14142 another message has been requested in its place.
14144 This is useful in situations where you need to redisplay but no
14145 user action has occurred, making it inappropriate for the message
14146 area to be cleared. See tracking_off and
14147 wait_reading_process_output for examples of these situations.
14149 FROM_WHERE is an integer saying from where this function was
14150 called. This is useful for debugging. */
14152 void
14153 redisplay_preserve_echo_area (int from_where)
14155 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14157 if (!NILP (echo_area_buffer[1]))
14159 /* We have a previously displayed message, but no current
14160 message. Redisplay the previous message. */
14161 display_last_displayed_message_p = 1;
14162 redisplay_internal ();
14163 display_last_displayed_message_p = 0;
14165 else
14166 redisplay_internal ();
14168 flush_frame (SELECTED_FRAME ());
14172 /* Function registered with record_unwind_protect in redisplay_internal. */
14174 static void
14175 unwind_redisplay (void)
14177 redisplaying_p = 0;
14181 /* Mark the display of leaf window W as accurate or inaccurate.
14182 If ACCURATE_P is non-zero mark display of W as accurate. If
14183 ACCURATE_P is zero, arrange for W to be redisplayed the next
14184 time redisplay_internal is called. */
14186 static void
14187 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14189 struct buffer *b = XBUFFER (w->contents);
14191 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14192 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14193 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14195 if (accurate_p)
14197 b->clip_changed = false;
14198 b->prevent_redisplay_optimizations_p = false;
14199 eassert (buffer_window_count (b) > 0);
14200 /* Resetting b->text->redisplay is problematic!
14201 In order to make it safer to do it here, redisplay_internal must
14202 have copied all b->text->redisplay to their respective windows. */
14203 b->text->redisplay = false;
14205 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14206 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14207 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14208 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14210 w->current_matrix->buffer = b;
14211 w->current_matrix->begv = BUF_BEGV (b);
14212 w->current_matrix->zv = BUF_ZV (b);
14214 w->last_cursor_vpos = w->cursor.vpos;
14215 w->last_cursor_off_p = w->cursor_off_p;
14217 if (w == XWINDOW (selected_window))
14218 w->last_point = BUF_PT (b);
14219 else
14220 w->last_point = marker_position (w->pointm);
14222 w->window_end_valid = true;
14223 w->update_mode_line = false;
14226 w->redisplay = !accurate_p;
14230 /* Mark the display of windows in the window tree rooted at WINDOW as
14231 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14232 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14233 be redisplayed the next time redisplay_internal is called. */
14235 void
14236 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14238 struct window *w;
14240 for (; !NILP (window); window = w->next)
14242 w = XWINDOW (window);
14243 if (WINDOWP (w->contents))
14244 mark_window_display_accurate (w->contents, accurate_p);
14245 else
14246 mark_window_display_accurate_1 (w, accurate_p);
14249 if (accurate_p)
14250 update_overlay_arrows (1);
14251 else
14252 /* Force a thorough redisplay the next time by setting
14253 last_arrow_position and last_arrow_string to t, which is
14254 unequal to any useful value of Voverlay_arrow_... */
14255 update_overlay_arrows (-1);
14259 /* Return value in display table DP (Lisp_Char_Table *) for character
14260 C. Since a display table doesn't have any parent, we don't have to
14261 follow parent. Do not call this function directly but use the
14262 macro DISP_CHAR_VECTOR. */
14264 Lisp_Object
14265 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14267 Lisp_Object val;
14269 if (ASCII_CHAR_P (c))
14271 val = dp->ascii;
14272 if (SUB_CHAR_TABLE_P (val))
14273 val = XSUB_CHAR_TABLE (val)->contents[c];
14275 else
14277 Lisp_Object table;
14279 XSETCHAR_TABLE (table, dp);
14280 val = char_table_ref (table, c);
14282 if (NILP (val))
14283 val = dp->defalt;
14284 return val;
14289 /***********************************************************************
14290 Window Redisplay
14291 ***********************************************************************/
14293 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14295 static void
14296 redisplay_windows (Lisp_Object window)
14298 while (!NILP (window))
14300 struct window *w = XWINDOW (window);
14302 if (WINDOWP (w->contents))
14303 redisplay_windows (w->contents);
14304 else if (BUFFERP (w->contents))
14306 displayed_buffer = XBUFFER (w->contents);
14307 /* Use list_of_error, not Qerror, so that
14308 we catch only errors and don't run the debugger. */
14309 internal_condition_case_1 (redisplay_window_0, window,
14310 list_of_error,
14311 redisplay_window_error);
14314 window = w->next;
14318 static Lisp_Object
14319 redisplay_window_error (Lisp_Object ignore)
14321 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14322 return Qnil;
14325 static Lisp_Object
14326 redisplay_window_0 (Lisp_Object window)
14328 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14329 redisplay_window (window, false);
14330 return Qnil;
14333 static Lisp_Object
14334 redisplay_window_1 (Lisp_Object window)
14336 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14337 redisplay_window (window, true);
14338 return Qnil;
14342 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14343 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14344 which positions recorded in ROW differ from current buffer
14345 positions.
14347 Return 0 if cursor is not on this row, 1 otherwise. */
14349 static int
14350 set_cursor_from_row (struct window *w, struct glyph_row *row,
14351 struct glyph_matrix *matrix,
14352 ptrdiff_t delta, ptrdiff_t delta_bytes,
14353 int dy, int dvpos)
14355 struct glyph *glyph = row->glyphs[TEXT_AREA];
14356 struct glyph *end = glyph + row->used[TEXT_AREA];
14357 struct glyph *cursor = NULL;
14358 /* The last known character position in row. */
14359 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14360 int x = row->x;
14361 ptrdiff_t pt_old = PT - delta;
14362 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14363 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14364 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14365 /* A glyph beyond the edge of TEXT_AREA which we should never
14366 touch. */
14367 struct glyph *glyphs_end = end;
14368 /* Non-zero means we've found a match for cursor position, but that
14369 glyph has the avoid_cursor_p flag set. */
14370 int match_with_avoid_cursor = 0;
14371 /* Non-zero means we've seen at least one glyph that came from a
14372 display string. */
14373 int string_seen = 0;
14374 /* Largest and smallest buffer positions seen so far during scan of
14375 glyph row. */
14376 ptrdiff_t bpos_max = pos_before;
14377 ptrdiff_t bpos_min = pos_after;
14378 /* Last buffer position covered by an overlay string with an integer
14379 `cursor' property. */
14380 ptrdiff_t bpos_covered = 0;
14381 /* Non-zero means the display string on which to display the cursor
14382 comes from a text property, not from an overlay. */
14383 int string_from_text_prop = 0;
14385 /* Don't even try doing anything if called for a mode-line or
14386 header-line row, since the rest of the code isn't prepared to
14387 deal with such calamities. */
14388 eassert (!row->mode_line_p);
14389 if (row->mode_line_p)
14390 return 0;
14392 /* Skip over glyphs not having an object at the start and the end of
14393 the row. These are special glyphs like truncation marks on
14394 terminal frames. */
14395 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14397 if (!row->reversed_p)
14399 while (glyph < end
14400 && INTEGERP (glyph->object)
14401 && glyph->charpos < 0)
14403 x += glyph->pixel_width;
14404 ++glyph;
14406 while (end > glyph
14407 && INTEGERP ((end - 1)->object)
14408 /* CHARPOS is zero for blanks and stretch glyphs
14409 inserted by extend_face_to_end_of_line. */
14410 && (end - 1)->charpos <= 0)
14411 --end;
14412 glyph_before = glyph - 1;
14413 glyph_after = end;
14415 else
14417 struct glyph *g;
14419 /* If the glyph row is reversed, we need to process it from back
14420 to front, so swap the edge pointers. */
14421 glyphs_end = end = glyph - 1;
14422 glyph += row->used[TEXT_AREA] - 1;
14424 while (glyph > end + 1
14425 && INTEGERP (glyph->object)
14426 && glyph->charpos < 0)
14428 --glyph;
14429 x -= glyph->pixel_width;
14431 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14432 --glyph;
14433 /* By default, in reversed rows we put the cursor on the
14434 rightmost (first in the reading order) glyph. */
14435 for (g = end + 1; g < glyph; g++)
14436 x += g->pixel_width;
14437 while (end < glyph
14438 && INTEGERP ((end + 1)->object)
14439 && (end + 1)->charpos <= 0)
14440 ++end;
14441 glyph_before = glyph + 1;
14442 glyph_after = end;
14445 else if (row->reversed_p)
14447 /* In R2L rows that don't display text, put the cursor on the
14448 rightmost glyph. Case in point: an empty last line that is
14449 part of an R2L paragraph. */
14450 cursor = end - 1;
14451 /* Avoid placing the cursor on the last glyph of the row, where
14452 on terminal frames we hold the vertical border between
14453 adjacent windows. */
14454 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14455 && !WINDOW_RIGHTMOST_P (w)
14456 && cursor == row->glyphs[LAST_AREA] - 1)
14457 cursor--;
14458 x = -1; /* will be computed below, at label compute_x */
14461 /* Step 1: Try to find the glyph whose character position
14462 corresponds to point. If that's not possible, find 2 glyphs
14463 whose character positions are the closest to point, one before
14464 point, the other after it. */
14465 if (!row->reversed_p)
14466 while (/* not marched to end of glyph row */
14467 glyph < end
14468 /* glyph was not inserted by redisplay for internal purposes */
14469 && !INTEGERP (glyph->object))
14471 if (BUFFERP (glyph->object))
14473 ptrdiff_t dpos = glyph->charpos - pt_old;
14475 if (glyph->charpos > bpos_max)
14476 bpos_max = glyph->charpos;
14477 if (glyph->charpos < bpos_min)
14478 bpos_min = glyph->charpos;
14479 if (!glyph->avoid_cursor_p)
14481 /* If we hit point, we've found the glyph on which to
14482 display the cursor. */
14483 if (dpos == 0)
14485 match_with_avoid_cursor = 0;
14486 break;
14488 /* See if we've found a better approximation to
14489 POS_BEFORE or to POS_AFTER. */
14490 if (0 > dpos && dpos > pos_before - pt_old)
14492 pos_before = glyph->charpos;
14493 glyph_before = glyph;
14495 else if (0 < dpos && dpos < pos_after - pt_old)
14497 pos_after = glyph->charpos;
14498 glyph_after = glyph;
14501 else if (dpos == 0)
14502 match_with_avoid_cursor = 1;
14504 else if (STRINGP (glyph->object))
14506 Lisp_Object chprop;
14507 ptrdiff_t glyph_pos = glyph->charpos;
14509 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14510 glyph->object);
14511 if (!NILP (chprop))
14513 /* If the string came from a `display' text property,
14514 look up the buffer position of that property and
14515 use that position to update bpos_max, as if we
14516 actually saw such a position in one of the row's
14517 glyphs. This helps with supporting integer values
14518 of `cursor' property on the display string in
14519 situations where most or all of the row's buffer
14520 text is completely covered by display properties,
14521 so that no glyph with valid buffer positions is
14522 ever seen in the row. */
14523 ptrdiff_t prop_pos =
14524 string_buffer_position_lim (glyph->object, pos_before,
14525 pos_after, 0);
14527 if (prop_pos >= pos_before)
14528 bpos_max = prop_pos;
14530 if (INTEGERP (chprop))
14532 bpos_covered = bpos_max + XINT (chprop);
14533 /* If the `cursor' property covers buffer positions up
14534 to and including point, we should display cursor on
14535 this glyph. Note that, if a `cursor' property on one
14536 of the string's characters has an integer value, we
14537 will break out of the loop below _before_ we get to
14538 the position match above. IOW, integer values of
14539 the `cursor' property override the "exact match for
14540 point" strategy of positioning the cursor. */
14541 /* Implementation note: bpos_max == pt_old when, e.g.,
14542 we are in an empty line, where bpos_max is set to
14543 MATRIX_ROW_START_CHARPOS, see above. */
14544 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14546 cursor = glyph;
14547 break;
14551 string_seen = 1;
14553 x += glyph->pixel_width;
14554 ++glyph;
14556 else if (glyph > end) /* row is reversed */
14557 while (!INTEGERP (glyph->object))
14559 if (BUFFERP (glyph->object))
14561 ptrdiff_t dpos = glyph->charpos - pt_old;
14563 if (glyph->charpos > bpos_max)
14564 bpos_max = glyph->charpos;
14565 if (glyph->charpos < bpos_min)
14566 bpos_min = glyph->charpos;
14567 if (!glyph->avoid_cursor_p)
14569 if (dpos == 0)
14571 match_with_avoid_cursor = 0;
14572 break;
14574 if (0 > dpos && dpos > pos_before - pt_old)
14576 pos_before = glyph->charpos;
14577 glyph_before = glyph;
14579 else if (0 < dpos && dpos < pos_after - pt_old)
14581 pos_after = glyph->charpos;
14582 glyph_after = glyph;
14585 else if (dpos == 0)
14586 match_with_avoid_cursor = 1;
14588 else if (STRINGP (glyph->object))
14590 Lisp_Object chprop;
14591 ptrdiff_t glyph_pos = glyph->charpos;
14593 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14594 glyph->object);
14595 if (!NILP (chprop))
14597 ptrdiff_t prop_pos =
14598 string_buffer_position_lim (glyph->object, pos_before,
14599 pos_after, 0);
14601 if (prop_pos >= pos_before)
14602 bpos_max = prop_pos;
14604 if (INTEGERP (chprop))
14606 bpos_covered = bpos_max + XINT (chprop);
14607 /* If the `cursor' property covers buffer positions up
14608 to and including point, we should display cursor on
14609 this glyph. */
14610 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14612 cursor = glyph;
14613 break;
14616 string_seen = 1;
14618 --glyph;
14619 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14621 x--; /* can't use any pixel_width */
14622 break;
14624 x -= glyph->pixel_width;
14627 /* Step 2: If we didn't find an exact match for point, we need to
14628 look for a proper place to put the cursor among glyphs between
14629 GLYPH_BEFORE and GLYPH_AFTER. */
14630 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14631 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14632 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14634 /* An empty line has a single glyph whose OBJECT is zero and
14635 whose CHARPOS is the position of a newline on that line.
14636 Note that on a TTY, there are more glyphs after that, which
14637 were produced by extend_face_to_end_of_line, but their
14638 CHARPOS is zero or negative. */
14639 int empty_line_p =
14640 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14641 && INTEGERP (glyph->object) && glyph->charpos > 0
14642 /* On a TTY, continued and truncated rows also have a glyph at
14643 their end whose OBJECT is zero and whose CHARPOS is
14644 positive (the continuation and truncation glyphs), but such
14645 rows are obviously not "empty". */
14646 && !(row->continued_p || row->truncated_on_right_p);
14648 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14650 ptrdiff_t ellipsis_pos;
14652 /* Scan back over the ellipsis glyphs. */
14653 if (!row->reversed_p)
14655 ellipsis_pos = (glyph - 1)->charpos;
14656 while (glyph > row->glyphs[TEXT_AREA]
14657 && (glyph - 1)->charpos == ellipsis_pos)
14658 glyph--, x -= glyph->pixel_width;
14659 /* That loop always goes one position too far, including
14660 the glyph before the ellipsis. So scan forward over
14661 that one. */
14662 x += glyph->pixel_width;
14663 glyph++;
14665 else /* row is reversed */
14667 ellipsis_pos = (glyph + 1)->charpos;
14668 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14669 && (glyph + 1)->charpos == ellipsis_pos)
14670 glyph++, x += glyph->pixel_width;
14671 x -= glyph->pixel_width;
14672 glyph--;
14675 else if (match_with_avoid_cursor)
14677 cursor = glyph_after;
14678 x = -1;
14680 else if (string_seen)
14682 int incr = row->reversed_p ? -1 : +1;
14684 /* Need to find the glyph that came out of a string which is
14685 present at point. That glyph is somewhere between
14686 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14687 positioned between POS_BEFORE and POS_AFTER in the
14688 buffer. */
14689 struct glyph *start, *stop;
14690 ptrdiff_t pos = pos_before;
14692 x = -1;
14694 /* If the row ends in a newline from a display string,
14695 reordering could have moved the glyphs belonging to the
14696 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14697 in this case we extend the search to the last glyph in
14698 the row that was not inserted by redisplay. */
14699 if (row->ends_in_newline_from_string_p)
14701 glyph_after = end;
14702 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14705 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14706 correspond to POS_BEFORE and POS_AFTER, respectively. We
14707 need START and STOP in the order that corresponds to the
14708 row's direction as given by its reversed_p flag. If the
14709 directionality of characters between POS_BEFORE and
14710 POS_AFTER is the opposite of the row's base direction,
14711 these characters will have been reordered for display,
14712 and we need to reverse START and STOP. */
14713 if (!row->reversed_p)
14715 start = min (glyph_before, glyph_after);
14716 stop = max (glyph_before, glyph_after);
14718 else
14720 start = max (glyph_before, glyph_after);
14721 stop = min (glyph_before, glyph_after);
14723 for (glyph = start + incr;
14724 row->reversed_p ? glyph > stop : glyph < stop; )
14727 /* Any glyphs that come from the buffer are here because
14728 of bidi reordering. Skip them, and only pay
14729 attention to glyphs that came from some string. */
14730 if (STRINGP (glyph->object))
14732 Lisp_Object str;
14733 ptrdiff_t tem;
14734 /* If the display property covers the newline, we
14735 need to search for it one position farther. */
14736 ptrdiff_t lim = pos_after
14737 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14739 string_from_text_prop = 0;
14740 str = glyph->object;
14741 tem = string_buffer_position_lim (str, pos, lim, 0);
14742 if (tem == 0 /* from overlay */
14743 || pos <= tem)
14745 /* If the string from which this glyph came is
14746 found in the buffer at point, or at position
14747 that is closer to point than pos_after, then
14748 we've found the glyph we've been looking for.
14749 If it comes from an overlay (tem == 0), and
14750 it has the `cursor' property on one of its
14751 glyphs, record that glyph as a candidate for
14752 displaying the cursor. (As in the
14753 unidirectional version, we will display the
14754 cursor on the last candidate we find.) */
14755 if (tem == 0
14756 || tem == pt_old
14757 || (tem - pt_old > 0 && tem < pos_after))
14759 /* The glyphs from this string could have
14760 been reordered. Find the one with the
14761 smallest string position. Or there could
14762 be a character in the string with the
14763 `cursor' property, which means display
14764 cursor on that character's glyph. */
14765 ptrdiff_t strpos = glyph->charpos;
14767 if (tem)
14769 cursor = glyph;
14770 string_from_text_prop = 1;
14772 for ( ;
14773 (row->reversed_p ? glyph > stop : glyph < stop)
14774 && EQ (glyph->object, str);
14775 glyph += incr)
14777 Lisp_Object cprop;
14778 ptrdiff_t gpos = glyph->charpos;
14780 cprop = Fget_char_property (make_number (gpos),
14781 Qcursor,
14782 glyph->object);
14783 if (!NILP (cprop))
14785 cursor = glyph;
14786 break;
14788 if (tem && glyph->charpos < strpos)
14790 strpos = glyph->charpos;
14791 cursor = glyph;
14795 if (tem == pt_old
14796 || (tem - pt_old > 0 && tem < pos_after))
14797 goto compute_x;
14799 if (tem)
14800 pos = tem + 1; /* don't find previous instances */
14802 /* This string is not what we want; skip all of the
14803 glyphs that came from it. */
14804 while ((row->reversed_p ? glyph > stop : glyph < stop)
14805 && EQ (glyph->object, str))
14806 glyph += incr;
14808 else
14809 glyph += incr;
14812 /* If we reached the end of the line, and END was from a string,
14813 the cursor is not on this line. */
14814 if (cursor == NULL
14815 && (row->reversed_p ? glyph <= end : glyph >= end)
14816 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14817 && STRINGP (end->object)
14818 && row->continued_p)
14819 return 0;
14821 /* A truncated row may not include PT among its character positions.
14822 Setting the cursor inside the scroll margin will trigger
14823 recalculation of hscroll in hscroll_window_tree. But if a
14824 display string covers point, defer to the string-handling
14825 code below to figure this out. */
14826 else if (row->truncated_on_left_p && pt_old < bpos_min)
14828 cursor = glyph_before;
14829 x = -1;
14831 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14832 /* Zero-width characters produce no glyphs. */
14833 || (!empty_line_p
14834 && (row->reversed_p
14835 ? glyph_after > glyphs_end
14836 : glyph_after < glyphs_end)))
14838 cursor = glyph_after;
14839 x = -1;
14843 compute_x:
14844 if (cursor != NULL)
14845 glyph = cursor;
14846 else if (glyph == glyphs_end
14847 && pos_before == pos_after
14848 && STRINGP ((row->reversed_p
14849 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14850 : row->glyphs[TEXT_AREA])->object))
14852 /* If all the glyphs of this row came from strings, put the
14853 cursor on the first glyph of the row. This avoids having the
14854 cursor outside of the text area in this very rare and hard
14855 use case. */
14856 glyph =
14857 row->reversed_p
14858 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14859 : row->glyphs[TEXT_AREA];
14861 if (x < 0)
14863 struct glyph *g;
14865 /* Need to compute x that corresponds to GLYPH. */
14866 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14868 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14869 emacs_abort ();
14870 x += g->pixel_width;
14874 /* ROW could be part of a continued line, which, under bidi
14875 reordering, might have other rows whose start and end charpos
14876 occlude point. Only set w->cursor if we found a better
14877 approximation to the cursor position than we have from previously
14878 examined candidate rows belonging to the same continued line. */
14879 if (/* We already have a candidate row. */
14880 w->cursor.vpos >= 0
14881 /* That candidate is not the row we are processing. */
14882 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14883 /* Make sure cursor.vpos specifies a row whose start and end
14884 charpos occlude point, and it is valid candidate for being a
14885 cursor-row. This is because some callers of this function
14886 leave cursor.vpos at the row where the cursor was displayed
14887 during the last redisplay cycle. */
14888 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14889 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14890 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14892 struct glyph *g1
14893 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14895 /* Don't consider glyphs that are outside TEXT_AREA. */
14896 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14897 return 0;
14898 /* Keep the candidate whose buffer position is the closest to
14899 point or has the `cursor' property. */
14900 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14901 w->cursor.hpos >= 0
14902 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14903 && ((BUFFERP (g1->object)
14904 && (g1->charpos == pt_old /* An exact match always wins. */
14905 || (BUFFERP (glyph->object)
14906 && eabs (g1->charpos - pt_old)
14907 < eabs (glyph->charpos - pt_old))))
14908 /* Previous candidate is a glyph from a string that has
14909 a non-nil `cursor' property. */
14910 || (STRINGP (g1->object)
14911 && (!NILP (Fget_char_property (make_number (g1->charpos),
14912 Qcursor, g1->object))
14913 /* Previous candidate is from the same display
14914 string as this one, and the display string
14915 came from a text property. */
14916 || (EQ (g1->object, glyph->object)
14917 && string_from_text_prop)
14918 /* this candidate is from newline and its
14919 position is not an exact match */
14920 || (INTEGERP (glyph->object)
14921 && glyph->charpos != pt_old)))))
14922 return 0;
14923 /* If this candidate gives an exact match, use that. */
14924 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14925 /* If this candidate is a glyph created for the
14926 terminating newline of a line, and point is on that
14927 newline, it wins because it's an exact match. */
14928 || (!row->continued_p
14929 && INTEGERP (glyph->object)
14930 && glyph->charpos == 0
14931 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14932 /* Otherwise, keep the candidate that comes from a row
14933 spanning less buffer positions. This may win when one or
14934 both candidate positions are on glyphs that came from
14935 display strings, for which we cannot compare buffer
14936 positions. */
14937 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14938 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14939 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14940 return 0;
14942 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14943 w->cursor.x = x;
14944 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14945 w->cursor.y = row->y + dy;
14947 if (w == XWINDOW (selected_window))
14949 if (!row->continued_p
14950 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14951 && row->x == 0)
14953 this_line_buffer = XBUFFER (w->contents);
14955 CHARPOS (this_line_start_pos)
14956 = MATRIX_ROW_START_CHARPOS (row) + delta;
14957 BYTEPOS (this_line_start_pos)
14958 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14960 CHARPOS (this_line_end_pos)
14961 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14962 BYTEPOS (this_line_end_pos)
14963 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14965 this_line_y = w->cursor.y;
14966 this_line_pixel_height = row->height;
14967 this_line_vpos = w->cursor.vpos;
14968 this_line_start_x = row->x;
14970 else
14971 CHARPOS (this_line_start_pos) = 0;
14974 return 1;
14978 /* Run window scroll functions, if any, for WINDOW with new window
14979 start STARTP. Sets the window start of WINDOW to that position.
14981 We assume that the window's buffer is really current. */
14983 static struct text_pos
14984 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14986 struct window *w = XWINDOW (window);
14987 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14989 eassert (current_buffer == XBUFFER (w->contents));
14991 if (!NILP (Vwindow_scroll_functions))
14993 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14994 make_number (CHARPOS (startp)));
14995 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14996 /* In case the hook functions switch buffers. */
14997 set_buffer_internal (XBUFFER (w->contents));
15000 return startp;
15004 /* Make sure the line containing the cursor is fully visible.
15005 A value of 1 means there is nothing to be done.
15006 (Either the line is fully visible, or it cannot be made so,
15007 or we cannot tell.)
15009 If FORCE_P is non-zero, return 0 even if partial visible cursor row
15010 is higher than window.
15012 A value of 0 means the caller should do scrolling
15013 as if point had gone off the screen. */
15015 static int
15016 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
15018 struct glyph_matrix *matrix;
15019 struct glyph_row *row;
15020 int window_height;
15022 if (!make_cursor_line_fully_visible_p)
15023 return 1;
15025 /* It's not always possible to find the cursor, e.g, when a window
15026 is full of overlay strings. Don't do anything in that case. */
15027 if (w->cursor.vpos < 0)
15028 return 1;
15030 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15031 row = MATRIX_ROW (matrix, w->cursor.vpos);
15033 /* If the cursor row is not partially visible, there's nothing to do. */
15034 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15035 return 1;
15037 /* If the row the cursor is in is taller than the window's height,
15038 it's not clear what to do, so do nothing. */
15039 window_height = window_box_height (w);
15040 if (row->height >= window_height)
15042 if (!force_p || MINI_WINDOW_P (w)
15043 || w->vscroll || w->cursor.vpos == 0)
15044 return 1;
15046 return 0;
15050 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15051 non-zero means only WINDOW is redisplayed in redisplay_internal.
15052 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15053 in redisplay_window to bring a partially visible line into view in
15054 the case that only the cursor has moved.
15056 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
15057 last screen line's vertical height extends past the end of the screen.
15059 Value is
15061 1 if scrolling succeeded
15063 0 if scrolling didn't find point.
15065 -1 if new fonts have been loaded so that we must interrupt
15066 redisplay, adjust glyph matrices, and try again. */
15068 enum
15070 SCROLLING_SUCCESS,
15071 SCROLLING_FAILED,
15072 SCROLLING_NEED_LARGER_MATRICES
15075 /* If scroll-conservatively is more than this, never recenter.
15077 If you change this, don't forget to update the doc string of
15078 `scroll-conservatively' and the Emacs manual. */
15079 #define SCROLL_LIMIT 100
15081 static int
15082 try_scrolling (Lisp_Object window, int just_this_one_p,
15083 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15084 int temp_scroll_step, int last_line_misfit)
15086 struct window *w = XWINDOW (window);
15087 struct frame *f = XFRAME (w->frame);
15088 struct text_pos pos, startp;
15089 struct it it;
15090 int this_scroll_margin, scroll_max, rc, height;
15091 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
15092 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
15093 Lisp_Object aggressive;
15094 /* We will never try scrolling more than this number of lines. */
15095 int scroll_limit = SCROLL_LIMIT;
15096 int frame_line_height = default_line_pixel_height (w);
15097 int window_total_lines
15098 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15100 #ifdef GLYPH_DEBUG
15101 debug_method_add (w, "try_scrolling");
15102 #endif
15104 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15106 /* Compute scroll margin height in pixels. We scroll when point is
15107 within this distance from the top or bottom of the window. */
15108 if (scroll_margin > 0)
15109 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15110 * frame_line_height;
15111 else
15112 this_scroll_margin = 0;
15114 /* Force arg_scroll_conservatively to have a reasonable value, to
15115 avoid scrolling too far away with slow move_it_* functions. Note
15116 that the user can supply scroll-conservatively equal to
15117 `most-positive-fixnum', which can be larger than INT_MAX. */
15118 if (arg_scroll_conservatively > scroll_limit)
15120 arg_scroll_conservatively = scroll_limit + 1;
15121 scroll_max = scroll_limit * frame_line_height;
15123 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15124 /* Compute how much we should try to scroll maximally to bring
15125 point into view. */
15126 scroll_max = (max (scroll_step,
15127 max (arg_scroll_conservatively, temp_scroll_step))
15128 * frame_line_height);
15129 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15130 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15131 /* We're trying to scroll because of aggressive scrolling but no
15132 scroll_step is set. Choose an arbitrary one. */
15133 scroll_max = 10 * frame_line_height;
15134 else
15135 scroll_max = 0;
15137 too_near_end:
15139 /* Decide whether to scroll down. */
15140 if (PT > CHARPOS (startp))
15142 int scroll_margin_y;
15144 /* Compute the pixel ypos of the scroll margin, then move IT to
15145 either that ypos or PT, whichever comes first. */
15146 start_display (&it, w, startp);
15147 scroll_margin_y = it.last_visible_y - this_scroll_margin
15148 - frame_line_height * extra_scroll_margin_lines;
15149 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15150 (MOVE_TO_POS | MOVE_TO_Y));
15152 if (PT > CHARPOS (it.current.pos))
15154 int y0 = line_bottom_y (&it);
15155 /* Compute how many pixels below window bottom to stop searching
15156 for PT. This avoids costly search for PT that is far away if
15157 the user limited scrolling by a small number of lines, but
15158 always finds PT if scroll_conservatively is set to a large
15159 number, such as most-positive-fixnum. */
15160 int slack = max (scroll_max, 10 * frame_line_height);
15161 int y_to_move = it.last_visible_y + slack;
15163 /* Compute the distance from the scroll margin to PT or to
15164 the scroll limit, whichever comes first. This should
15165 include the height of the cursor line, to make that line
15166 fully visible. */
15167 move_it_to (&it, PT, -1, y_to_move,
15168 -1, MOVE_TO_POS | MOVE_TO_Y);
15169 dy = line_bottom_y (&it) - y0;
15171 if (dy > scroll_max)
15172 return SCROLLING_FAILED;
15174 if (dy > 0)
15175 scroll_down_p = 1;
15179 if (scroll_down_p)
15181 /* Point is in or below the bottom scroll margin, so move the
15182 window start down. If scrolling conservatively, move it just
15183 enough down to make point visible. If scroll_step is set,
15184 move it down by scroll_step. */
15185 if (arg_scroll_conservatively)
15186 amount_to_scroll
15187 = min (max (dy, frame_line_height),
15188 frame_line_height * arg_scroll_conservatively);
15189 else if (scroll_step || temp_scroll_step)
15190 amount_to_scroll = scroll_max;
15191 else
15193 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15194 height = WINDOW_BOX_TEXT_HEIGHT (w);
15195 if (NUMBERP (aggressive))
15197 double float_amount = XFLOATINT (aggressive) * height;
15198 int aggressive_scroll = float_amount;
15199 if (aggressive_scroll == 0 && float_amount > 0)
15200 aggressive_scroll = 1;
15201 /* Don't let point enter the scroll margin near top of
15202 the window. This could happen if the value of
15203 scroll_up_aggressively is too large and there are
15204 non-zero margins, because scroll_up_aggressively
15205 means put point that fraction of window height
15206 _from_the_bottom_margin_. */
15207 if (aggressive_scroll + 2*this_scroll_margin > height)
15208 aggressive_scroll = height - 2*this_scroll_margin;
15209 amount_to_scroll = dy + aggressive_scroll;
15213 if (amount_to_scroll <= 0)
15214 return SCROLLING_FAILED;
15216 start_display (&it, w, startp);
15217 if (arg_scroll_conservatively <= scroll_limit)
15218 move_it_vertically (&it, amount_to_scroll);
15219 else
15221 /* Extra precision for users who set scroll-conservatively
15222 to a large number: make sure the amount we scroll
15223 the window start is never less than amount_to_scroll,
15224 which was computed as distance from window bottom to
15225 point. This matters when lines at window top and lines
15226 below window bottom have different height. */
15227 struct it it1;
15228 void *it1data = NULL;
15229 /* We use a temporary it1 because line_bottom_y can modify
15230 its argument, if it moves one line down; see there. */
15231 int start_y;
15233 SAVE_IT (it1, it, it1data);
15234 start_y = line_bottom_y (&it1);
15235 do {
15236 RESTORE_IT (&it, &it, it1data);
15237 move_it_by_lines (&it, 1);
15238 SAVE_IT (it1, it, it1data);
15239 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15242 /* If STARTP is unchanged, move it down another screen line. */
15243 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15244 move_it_by_lines (&it, 1);
15245 startp = it.current.pos;
15247 else
15249 struct text_pos scroll_margin_pos = startp;
15250 int y_offset = 0;
15252 /* See if point is inside the scroll margin at the top of the
15253 window. */
15254 if (this_scroll_margin)
15256 int y_start;
15258 start_display (&it, w, startp);
15259 y_start = it.current_y;
15260 move_it_vertically (&it, this_scroll_margin);
15261 scroll_margin_pos = it.current.pos;
15262 /* If we didn't move enough before hitting ZV, request
15263 additional amount of scroll, to move point out of the
15264 scroll margin. */
15265 if (IT_CHARPOS (it) == ZV
15266 && it.current_y - y_start < this_scroll_margin)
15267 y_offset = this_scroll_margin - (it.current_y - y_start);
15270 if (PT < CHARPOS (scroll_margin_pos))
15272 /* Point is in the scroll margin at the top of the window or
15273 above what is displayed in the window. */
15274 int y0, y_to_move;
15276 /* Compute the vertical distance from PT to the scroll
15277 margin position. Move as far as scroll_max allows, or
15278 one screenful, or 10 screen lines, whichever is largest.
15279 Give up if distance is greater than scroll_max or if we
15280 didn't reach the scroll margin position. */
15281 SET_TEXT_POS (pos, PT, PT_BYTE);
15282 start_display (&it, w, pos);
15283 y0 = it.current_y;
15284 y_to_move = max (it.last_visible_y,
15285 max (scroll_max, 10 * frame_line_height));
15286 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15287 y_to_move, -1,
15288 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15289 dy = it.current_y - y0;
15290 if (dy > scroll_max
15291 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15292 return SCROLLING_FAILED;
15294 /* Additional scroll for when ZV was too close to point. */
15295 dy += y_offset;
15297 /* Compute new window start. */
15298 start_display (&it, w, startp);
15300 if (arg_scroll_conservatively)
15301 amount_to_scroll = max (dy, frame_line_height *
15302 max (scroll_step, temp_scroll_step));
15303 else if (scroll_step || temp_scroll_step)
15304 amount_to_scroll = scroll_max;
15305 else
15307 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15308 height = WINDOW_BOX_TEXT_HEIGHT (w);
15309 if (NUMBERP (aggressive))
15311 double float_amount = XFLOATINT (aggressive) * height;
15312 int aggressive_scroll = float_amount;
15313 if (aggressive_scroll == 0 && float_amount > 0)
15314 aggressive_scroll = 1;
15315 /* Don't let point enter the scroll margin near
15316 bottom of the window, if the value of
15317 scroll_down_aggressively happens to be too
15318 large. */
15319 if (aggressive_scroll + 2*this_scroll_margin > height)
15320 aggressive_scroll = height - 2*this_scroll_margin;
15321 amount_to_scroll = dy + aggressive_scroll;
15325 if (amount_to_scroll <= 0)
15326 return SCROLLING_FAILED;
15328 move_it_vertically_backward (&it, amount_to_scroll);
15329 startp = it.current.pos;
15333 /* Run window scroll functions. */
15334 startp = run_window_scroll_functions (window, startp);
15336 /* Display the window. Give up if new fonts are loaded, or if point
15337 doesn't appear. */
15338 if (!try_window (window, startp, 0))
15339 rc = SCROLLING_NEED_LARGER_MATRICES;
15340 else if (w->cursor.vpos < 0)
15342 clear_glyph_matrix (w->desired_matrix);
15343 rc = SCROLLING_FAILED;
15345 else
15347 /* Maybe forget recorded base line for line number display. */
15348 if (!just_this_one_p
15349 || current_buffer->clip_changed
15350 || BEG_UNCHANGED < CHARPOS (startp))
15351 w->base_line_number = 0;
15353 /* If cursor ends up on a partially visible line,
15354 treat that as being off the bottom of the screen. */
15355 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15356 /* It's possible that the cursor is on the first line of the
15357 buffer, which is partially obscured due to a vscroll
15358 (Bug#7537). In that case, avoid looping forever. */
15359 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15361 clear_glyph_matrix (w->desired_matrix);
15362 ++extra_scroll_margin_lines;
15363 goto too_near_end;
15365 rc = SCROLLING_SUCCESS;
15368 return rc;
15372 /* Compute a suitable window start for window W if display of W starts
15373 on a continuation line. Value is non-zero if a new window start
15374 was computed.
15376 The new window start will be computed, based on W's width, starting
15377 from the start of the continued line. It is the start of the
15378 screen line with the minimum distance from the old start W->start. */
15380 static int
15381 compute_window_start_on_continuation_line (struct window *w)
15383 struct text_pos pos, start_pos;
15384 int window_start_changed_p = 0;
15386 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15388 /* If window start is on a continuation line... Window start may be
15389 < BEGV in case there's invisible text at the start of the
15390 buffer (M-x rmail, for example). */
15391 if (CHARPOS (start_pos) > BEGV
15392 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15394 struct it it;
15395 struct glyph_row *row;
15397 /* Handle the case that the window start is out of range. */
15398 if (CHARPOS (start_pos) < BEGV)
15399 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15400 else if (CHARPOS (start_pos) > ZV)
15401 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15403 /* Find the start of the continued line. This should be fast
15404 because find_newline is fast (newline cache). */
15405 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15406 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15407 row, DEFAULT_FACE_ID);
15408 reseat_at_previous_visible_line_start (&it);
15410 /* If the line start is "too far" away from the window start,
15411 say it takes too much time to compute a new window start. */
15412 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15413 /* PXW: Do we need upper bounds here? */
15414 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15416 int min_distance, distance;
15418 /* Move forward by display lines to find the new window
15419 start. If window width was enlarged, the new start can
15420 be expected to be > the old start. If window width was
15421 decreased, the new window start will be < the old start.
15422 So, we're looking for the display line start with the
15423 minimum distance from the old window start. */
15424 pos = it.current.pos;
15425 min_distance = INFINITY;
15426 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15427 distance < min_distance)
15429 min_distance = distance;
15430 pos = it.current.pos;
15431 if (it.line_wrap == WORD_WRAP)
15433 /* Under WORD_WRAP, move_it_by_lines is likely to
15434 overshoot and stop not at the first, but the
15435 second character from the left margin. So in
15436 that case, we need a more tight control on the X
15437 coordinate of the iterator than move_it_by_lines
15438 promises in its contract. The method is to first
15439 go to the last (rightmost) visible character of a
15440 line, then move to the leftmost character on the
15441 next line in a separate call. */
15442 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15443 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15444 move_it_to (&it, ZV, 0,
15445 it.current_y + it.max_ascent + it.max_descent, -1,
15446 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15448 else
15449 move_it_by_lines (&it, 1);
15452 /* Set the window start there. */
15453 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15454 window_start_changed_p = 1;
15458 return window_start_changed_p;
15462 /* Try cursor movement in case text has not changed in window WINDOW,
15463 with window start STARTP. Value is
15465 CURSOR_MOVEMENT_SUCCESS if successful
15467 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15469 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15470 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15471 we want to scroll as if scroll-step were set to 1. See the code.
15473 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15474 which case we have to abort this redisplay, and adjust matrices
15475 first. */
15477 enum
15479 CURSOR_MOVEMENT_SUCCESS,
15480 CURSOR_MOVEMENT_CANNOT_BE_USED,
15481 CURSOR_MOVEMENT_MUST_SCROLL,
15482 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15485 static int
15486 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15488 struct window *w = XWINDOW (window);
15489 struct frame *f = XFRAME (w->frame);
15490 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15492 #ifdef GLYPH_DEBUG
15493 if (inhibit_try_cursor_movement)
15494 return rc;
15495 #endif
15497 /* Previously, there was a check for Lisp integer in the
15498 if-statement below. Now, this field is converted to
15499 ptrdiff_t, thus zero means invalid position in a buffer. */
15500 eassert (w->last_point > 0);
15501 /* Likewise there was a check whether window_end_vpos is nil or larger
15502 than the window. Now window_end_vpos is int and so never nil, but
15503 let's leave eassert to check whether it fits in the window. */
15504 eassert (w->window_end_vpos < w->current_matrix->nrows);
15506 /* Handle case where text has not changed, only point, and it has
15507 not moved off the frame. */
15508 if (/* Point may be in this window. */
15509 PT >= CHARPOS (startp)
15510 /* Selective display hasn't changed. */
15511 && !current_buffer->clip_changed
15512 /* Function force-mode-line-update is used to force a thorough
15513 redisplay. It sets either windows_or_buffers_changed or
15514 update_mode_lines. So don't take a shortcut here for these
15515 cases. */
15516 && !update_mode_lines
15517 && !windows_or_buffers_changed
15518 && !f->cursor_type_changed
15519 && NILP (Vshow_trailing_whitespace)
15520 /* This code is not used for mini-buffer for the sake of the case
15521 of redisplaying to replace an echo area message; since in
15522 that case the mini-buffer contents per se are usually
15523 unchanged. This code is of no real use in the mini-buffer
15524 since the handling of this_line_start_pos, etc., in redisplay
15525 handles the same cases. */
15526 && !EQ (window, minibuf_window)
15527 && (FRAME_WINDOW_P (f)
15528 || !overlay_arrow_in_current_buffer_p ()))
15530 int this_scroll_margin, top_scroll_margin;
15531 struct glyph_row *row = NULL;
15532 int frame_line_height = default_line_pixel_height (w);
15533 int window_total_lines
15534 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15536 #ifdef GLYPH_DEBUG
15537 debug_method_add (w, "cursor movement");
15538 #endif
15540 /* Scroll if point within this distance from the top or bottom
15541 of the window. This is a pixel value. */
15542 if (scroll_margin > 0)
15544 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15545 this_scroll_margin *= frame_line_height;
15547 else
15548 this_scroll_margin = 0;
15550 top_scroll_margin = this_scroll_margin;
15551 if (WINDOW_WANTS_HEADER_LINE_P (w))
15552 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15554 /* Start with the row the cursor was displayed during the last
15555 not paused redisplay. Give up if that row is not valid. */
15556 if (w->last_cursor_vpos < 0
15557 || w->last_cursor_vpos >= w->current_matrix->nrows)
15558 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15559 else
15561 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15562 if (row->mode_line_p)
15563 ++row;
15564 if (!row->enabled_p)
15565 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15568 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15570 int scroll_p = 0, must_scroll = 0;
15571 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15573 if (PT > w->last_point)
15575 /* Point has moved forward. */
15576 while (MATRIX_ROW_END_CHARPOS (row) < PT
15577 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15579 eassert (row->enabled_p);
15580 ++row;
15583 /* If the end position of a row equals the start
15584 position of the next row, and PT is at that position,
15585 we would rather display cursor in the next line. */
15586 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15587 && MATRIX_ROW_END_CHARPOS (row) == PT
15588 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15589 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15590 && !cursor_row_p (row))
15591 ++row;
15593 /* If within the scroll margin, scroll. Note that
15594 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15595 the next line would be drawn, and that
15596 this_scroll_margin can be zero. */
15597 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15598 || PT > MATRIX_ROW_END_CHARPOS (row)
15599 /* Line is completely visible last line in window
15600 and PT is to be set in the next line. */
15601 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15602 && PT == MATRIX_ROW_END_CHARPOS (row)
15603 && !row->ends_at_zv_p
15604 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15605 scroll_p = 1;
15607 else if (PT < w->last_point)
15609 /* Cursor has to be moved backward. Note that PT >=
15610 CHARPOS (startp) because of the outer if-statement. */
15611 while (!row->mode_line_p
15612 && (MATRIX_ROW_START_CHARPOS (row) > PT
15613 || (MATRIX_ROW_START_CHARPOS (row) == PT
15614 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15615 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15616 row > w->current_matrix->rows
15617 && (row-1)->ends_in_newline_from_string_p))))
15618 && (row->y > top_scroll_margin
15619 || CHARPOS (startp) == BEGV))
15621 eassert (row->enabled_p);
15622 --row;
15625 /* Consider the following case: Window starts at BEGV,
15626 there is invisible, intangible text at BEGV, so that
15627 display starts at some point START > BEGV. It can
15628 happen that we are called with PT somewhere between
15629 BEGV and START. Try to handle that case. */
15630 if (row < w->current_matrix->rows
15631 || row->mode_line_p)
15633 row = w->current_matrix->rows;
15634 if (row->mode_line_p)
15635 ++row;
15638 /* Due to newlines in overlay strings, we may have to
15639 skip forward over overlay strings. */
15640 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15641 && MATRIX_ROW_END_CHARPOS (row) == PT
15642 && !cursor_row_p (row))
15643 ++row;
15645 /* If within the scroll margin, scroll. */
15646 if (row->y < top_scroll_margin
15647 && CHARPOS (startp) != BEGV)
15648 scroll_p = 1;
15650 else
15652 /* Cursor did not move. So don't scroll even if cursor line
15653 is partially visible, as it was so before. */
15654 rc = CURSOR_MOVEMENT_SUCCESS;
15657 if (PT < MATRIX_ROW_START_CHARPOS (row)
15658 || PT > MATRIX_ROW_END_CHARPOS (row))
15660 /* if PT is not in the glyph row, give up. */
15661 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15662 must_scroll = 1;
15664 else if (rc != CURSOR_MOVEMENT_SUCCESS
15665 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15667 struct glyph_row *row1;
15669 /* If rows are bidi-reordered and point moved, back up
15670 until we find a row that does not belong to a
15671 continuation line. This is because we must consider
15672 all rows of a continued line as candidates for the
15673 new cursor positioning, since row start and end
15674 positions change non-linearly with vertical position
15675 in such rows. */
15676 /* FIXME: Revisit this when glyph ``spilling'' in
15677 continuation lines' rows is implemented for
15678 bidi-reordered rows. */
15679 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15680 MATRIX_ROW_CONTINUATION_LINE_P (row);
15681 --row)
15683 /* If we hit the beginning of the displayed portion
15684 without finding the first row of a continued
15685 line, give up. */
15686 if (row <= row1)
15688 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15689 break;
15691 eassert (row->enabled_p);
15694 if (must_scroll)
15696 else if (rc != CURSOR_MOVEMENT_SUCCESS
15697 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15698 /* Make sure this isn't a header line by any chance, since
15699 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15700 && !row->mode_line_p
15701 && make_cursor_line_fully_visible_p)
15703 if (PT == MATRIX_ROW_END_CHARPOS (row)
15704 && !row->ends_at_zv_p
15705 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15706 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15707 else if (row->height > window_box_height (w))
15709 /* If we end up in a partially visible line, let's
15710 make it fully visible, except when it's taller
15711 than the window, in which case we can't do much
15712 about it. */
15713 *scroll_step = 1;
15714 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15716 else
15718 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15719 if (!cursor_row_fully_visible_p (w, 0, 1))
15720 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15721 else
15722 rc = CURSOR_MOVEMENT_SUCCESS;
15725 else if (scroll_p)
15726 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15727 else if (rc != CURSOR_MOVEMENT_SUCCESS
15728 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15730 /* With bidi-reordered rows, there could be more than
15731 one candidate row whose start and end positions
15732 occlude point. We need to let set_cursor_from_row
15733 find the best candidate. */
15734 /* FIXME: Revisit this when glyph ``spilling'' in
15735 continuation lines' rows is implemented for
15736 bidi-reordered rows. */
15737 int rv = 0;
15741 int at_zv_p = 0, exact_match_p = 0;
15743 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15744 && PT <= MATRIX_ROW_END_CHARPOS (row)
15745 && cursor_row_p (row))
15746 rv |= set_cursor_from_row (w, row, w->current_matrix,
15747 0, 0, 0, 0);
15748 /* As soon as we've found the exact match for point,
15749 or the first suitable row whose ends_at_zv_p flag
15750 is set, we are done. */
15751 if (rv)
15753 at_zv_p = MATRIX_ROW (w->current_matrix,
15754 w->cursor.vpos)->ends_at_zv_p;
15755 if (!at_zv_p
15756 && w->cursor.hpos >= 0
15757 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15758 w->cursor.vpos))
15760 struct glyph_row *candidate =
15761 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15762 struct glyph *g =
15763 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15764 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15766 exact_match_p =
15767 (BUFFERP (g->object) && g->charpos == PT)
15768 || (INTEGERP (g->object)
15769 && (g->charpos == PT
15770 || (g->charpos == 0 && endpos - 1 == PT)));
15772 if (at_zv_p || exact_match_p)
15774 rc = CURSOR_MOVEMENT_SUCCESS;
15775 break;
15778 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15779 break;
15780 ++row;
15782 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15783 || row->continued_p)
15784 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15785 || (MATRIX_ROW_START_CHARPOS (row) == PT
15786 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15787 /* If we didn't find any candidate rows, or exited the
15788 loop before all the candidates were examined, signal
15789 to the caller that this method failed. */
15790 if (rc != CURSOR_MOVEMENT_SUCCESS
15791 && !(rv
15792 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15793 && !row->continued_p))
15794 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15795 else if (rv)
15796 rc = CURSOR_MOVEMENT_SUCCESS;
15798 else
15802 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15804 rc = CURSOR_MOVEMENT_SUCCESS;
15805 break;
15807 ++row;
15809 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15810 && MATRIX_ROW_START_CHARPOS (row) == PT
15811 && cursor_row_p (row));
15816 return rc;
15819 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15820 static
15821 #endif
15822 void
15823 set_vertical_scroll_bar (struct window *w)
15825 ptrdiff_t start, end, whole;
15827 /* Calculate the start and end positions for the current window.
15828 At some point, it would be nice to choose between scrollbars
15829 which reflect the whole buffer size, with special markers
15830 indicating narrowing, and scrollbars which reflect only the
15831 visible region.
15833 Note that mini-buffers sometimes aren't displaying any text. */
15834 if (!MINI_WINDOW_P (w)
15835 || (w == XWINDOW (minibuf_window)
15836 && NILP (echo_area_buffer[0])))
15838 struct buffer *buf = XBUFFER (w->contents);
15839 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15840 start = marker_position (w->start) - BUF_BEGV (buf);
15841 /* I don't think this is guaranteed to be right. For the
15842 moment, we'll pretend it is. */
15843 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15845 if (end < start)
15846 end = start;
15847 if (whole < (end - start))
15848 whole = end - start;
15850 else
15851 start = end = whole = 0;
15853 /* Indicate what this scroll bar ought to be displaying now. */
15854 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15855 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15856 (w, end - start, whole, start);
15860 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15861 selected_window is redisplayed.
15863 We can return without actually redisplaying the window if fonts has been
15864 changed on window's frame. In that case, redisplay_internal will retry.
15866 As one of the important parts of redisplaying a window, we need to
15867 decide whether the previous window-start position (stored in the
15868 window's w->start marker position) is still valid, and if it isn't,
15869 recompute it. Some details about that:
15871 . The previous window-start could be in a continuation line, in
15872 which case we need to recompute it when the window width
15873 changes. See compute_window_start_on_continuation_line and its
15874 call below.
15876 . The text that changed since last redisplay could include the
15877 previous window-start position. In that case, we try to salvage
15878 what we can from the current glyph matrix by calling
15879 try_scrolling, which see.
15881 . Some Emacs command could force us to use a specific window-start
15882 position by setting the window's force_start flag, or gently
15883 propose doing that by setting the window's optional_new_start
15884 flag. In these cases, we try using the specified start point if
15885 that succeeds (i.e. the window desired matrix is successfully
15886 recomputed, and point location is within the window). In case
15887 of optional_new_start, we first check if the specified start
15888 position is feasible, i.e. if it will allow point to be
15889 displayed in the window. If using the specified start point
15890 fails, e.g., if new fonts are needed to be loaded, we abort the
15891 redisplay cycle and leave it up to the next cycle to figure out
15892 things.
15894 . Note that the window's force_start flag is sometimes set by
15895 redisplay itself, when it decides that the previous window start
15896 point is fine and should be kept. Search for "goto force_start"
15897 below to see the details. Like the values of window-start
15898 specified outside of redisplay, these internally-deduced values
15899 are tested for feasibility, and ignored if found to be
15900 unfeasible.
15902 . Note that the function try_window, used to completely redisplay
15903 a window, accepts the window's start point as its argument.
15904 This is used several times in the redisplay code to control
15905 where the window start will be, according to user options such
15906 as scroll-conservatively, and also to ensure the screen line
15907 showing point will be fully (as opposed to partially) visible on
15908 display. */
15910 static void
15911 redisplay_window (Lisp_Object window, bool just_this_one_p)
15913 struct window *w = XWINDOW (window);
15914 struct frame *f = XFRAME (w->frame);
15915 struct buffer *buffer = XBUFFER (w->contents);
15916 struct buffer *old = current_buffer;
15917 struct text_pos lpoint, opoint, startp;
15918 int update_mode_line;
15919 int tem;
15920 struct it it;
15921 /* Record it now because it's overwritten. */
15922 bool current_matrix_up_to_date_p = false;
15923 bool used_current_matrix_p = false;
15924 /* This is less strict than current_matrix_up_to_date_p.
15925 It indicates that the buffer contents and narrowing are unchanged. */
15926 bool buffer_unchanged_p = false;
15927 int temp_scroll_step = 0;
15928 ptrdiff_t count = SPECPDL_INDEX ();
15929 int rc;
15930 int centering_position = -1;
15931 int last_line_misfit = 0;
15932 ptrdiff_t beg_unchanged, end_unchanged;
15933 int frame_line_height;
15935 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15936 opoint = lpoint;
15938 #ifdef GLYPH_DEBUG
15939 *w->desired_matrix->method = 0;
15940 #endif
15942 if (!just_this_one_p
15943 && REDISPLAY_SOME_P ()
15944 && !w->redisplay
15945 && !f->redisplay
15946 && !buffer->text->redisplay
15947 && BUF_PT (buffer) == w->last_point)
15948 return;
15950 /* Make sure that both W's markers are valid. */
15951 eassert (XMARKER (w->start)->buffer == buffer);
15952 eassert (XMARKER (w->pointm)->buffer == buffer);
15954 /* We come here again if we need to run window-text-change-functions
15955 below. */
15956 restart:
15957 reconsider_clip_changes (w);
15958 frame_line_height = default_line_pixel_height (w);
15960 /* Has the mode line to be updated? */
15961 update_mode_line = (w->update_mode_line
15962 || update_mode_lines
15963 || buffer->clip_changed
15964 || buffer->prevent_redisplay_optimizations_p);
15966 if (!just_this_one_p)
15967 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
15968 cleverly elsewhere. */
15969 w->must_be_updated_p = true;
15971 if (MINI_WINDOW_P (w))
15973 if (w == XWINDOW (echo_area_window)
15974 && !NILP (echo_area_buffer[0]))
15976 if (update_mode_line)
15977 /* We may have to update a tty frame's menu bar or a
15978 tool-bar. Example `M-x C-h C-h C-g'. */
15979 goto finish_menu_bars;
15980 else
15981 /* We've already displayed the echo area glyphs in this window. */
15982 goto finish_scroll_bars;
15984 else if ((w != XWINDOW (minibuf_window)
15985 || minibuf_level == 0)
15986 /* When buffer is nonempty, redisplay window normally. */
15987 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15988 /* Quail displays non-mini buffers in minibuffer window.
15989 In that case, redisplay the window normally. */
15990 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15992 /* W is a mini-buffer window, but it's not active, so clear
15993 it. */
15994 int yb = window_text_bottom_y (w);
15995 struct glyph_row *row;
15996 int y;
15998 for (y = 0, row = w->desired_matrix->rows;
15999 y < yb;
16000 y += row->height, ++row)
16001 blank_row (w, row, y);
16002 goto finish_scroll_bars;
16005 clear_glyph_matrix (w->desired_matrix);
16008 /* Otherwise set up data on this window; select its buffer and point
16009 value. */
16010 /* Really select the buffer, for the sake of buffer-local
16011 variables. */
16012 set_buffer_internal_1 (XBUFFER (w->contents));
16014 current_matrix_up_to_date_p
16015 = (w->window_end_valid
16016 && !current_buffer->clip_changed
16017 && !current_buffer->prevent_redisplay_optimizations_p
16018 && !window_outdated (w));
16020 /* Run the window-text-change-functions
16021 if it is possible that the text on the screen has changed
16022 (either due to modification of the text, or any other reason). */
16023 if (!current_matrix_up_to_date_p
16024 && !NILP (Vwindow_text_change_functions))
16026 safe_run_hooks (Qwindow_text_change_functions);
16027 goto restart;
16030 beg_unchanged = BEG_UNCHANGED;
16031 end_unchanged = END_UNCHANGED;
16033 SET_TEXT_POS (opoint, PT, PT_BYTE);
16035 specbind (Qinhibit_point_motion_hooks, Qt);
16037 buffer_unchanged_p
16038 = (w->window_end_valid
16039 && !current_buffer->clip_changed
16040 && !window_outdated (w));
16042 /* When windows_or_buffers_changed is non-zero, we can't rely
16043 on the window end being valid, so set it to zero there. */
16044 if (windows_or_buffers_changed)
16046 /* If window starts on a continuation line, maybe adjust the
16047 window start in case the window's width changed. */
16048 if (XMARKER (w->start)->buffer == current_buffer)
16049 compute_window_start_on_continuation_line (w);
16051 w->window_end_valid = false;
16052 /* If so, we also can't rely on current matrix
16053 and should not fool try_cursor_movement below. */
16054 current_matrix_up_to_date_p = false;
16057 /* Some sanity checks. */
16058 CHECK_WINDOW_END (w);
16059 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16060 emacs_abort ();
16061 if (BYTEPOS (opoint) < CHARPOS (opoint))
16062 emacs_abort ();
16064 if (mode_line_update_needed (w))
16065 update_mode_line = 1;
16067 /* Point refers normally to the selected window. For any other
16068 window, set up appropriate value. */
16069 if (!EQ (window, selected_window))
16071 ptrdiff_t new_pt = marker_position (w->pointm);
16072 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16073 if (new_pt < BEGV)
16075 new_pt = BEGV;
16076 new_pt_byte = BEGV_BYTE;
16077 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16079 else if (new_pt > (ZV - 1))
16081 new_pt = ZV;
16082 new_pt_byte = ZV_BYTE;
16083 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16086 /* We don't use SET_PT so that the point-motion hooks don't run. */
16087 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16090 /* If any of the character widths specified in the display table
16091 have changed, invalidate the width run cache. It's true that
16092 this may be a bit late to catch such changes, but the rest of
16093 redisplay goes (non-fatally) haywire when the display table is
16094 changed, so why should we worry about doing any better? */
16095 if (current_buffer->width_run_cache
16096 || (current_buffer->base_buffer
16097 && current_buffer->base_buffer->width_run_cache))
16099 struct Lisp_Char_Table *disptab = buffer_display_table ();
16101 if (! disptab_matches_widthtab
16102 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16104 struct buffer *buf = current_buffer;
16106 if (buf->base_buffer)
16107 buf = buf->base_buffer;
16108 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16109 recompute_width_table (current_buffer, disptab);
16113 /* If window-start is screwed up, choose a new one. */
16114 if (XMARKER (w->start)->buffer != current_buffer)
16115 goto recenter;
16117 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16119 /* If someone specified a new starting point but did not insist,
16120 check whether it can be used. */
16121 if (w->optional_new_start
16122 && CHARPOS (startp) >= BEGV
16123 && CHARPOS (startp) <= ZV)
16125 w->optional_new_start = 0;
16126 start_display (&it, w, startp);
16127 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16128 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16129 if (IT_CHARPOS (it) == PT)
16130 w->force_start = 1;
16131 /* IT may overshoot PT if text at PT is invisible. */
16132 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
16133 w->force_start = 1;
16136 force_start:
16138 /* Handle case where place to start displaying has been specified,
16139 unless the specified location is outside the accessible range. */
16140 if (w->force_start || window_frozen_p (w))
16142 /* We set this later on if we have to adjust point. */
16143 int new_vpos = -1;
16145 w->force_start = 0;
16146 w->vscroll = 0;
16147 w->window_end_valid = 0;
16149 /* Forget any recorded base line for line number display. */
16150 if (!buffer_unchanged_p)
16151 w->base_line_number = 0;
16153 /* Redisplay the mode line. Select the buffer properly for that.
16154 Also, run the hook window-scroll-functions
16155 because we have scrolled. */
16156 /* Note, we do this after clearing force_start because
16157 if there's an error, it is better to forget about force_start
16158 than to get into an infinite loop calling the hook functions
16159 and having them get more errors. */
16160 if (!update_mode_line
16161 || ! NILP (Vwindow_scroll_functions))
16163 update_mode_line = 1;
16164 w->update_mode_line = 1;
16165 startp = run_window_scroll_functions (window, startp);
16168 if (CHARPOS (startp) < BEGV)
16169 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16170 else if (CHARPOS (startp) > ZV)
16171 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16173 /* Redisplay, then check if cursor has been set during the
16174 redisplay. Give up if new fonts were loaded. */
16175 /* We used to issue a CHECK_MARGINS argument to try_window here,
16176 but this causes scrolling to fail when point begins inside
16177 the scroll margin (bug#148) -- cyd */
16178 if (!try_window (window, startp, 0))
16180 w->force_start = 1;
16181 clear_glyph_matrix (w->desired_matrix);
16182 goto need_larger_matrices;
16185 if (w->cursor.vpos < 0 && !window_frozen_p (w))
16187 /* If point does not appear, try to move point so it does
16188 appear. The desired matrix has been built above, so we
16189 can use it here. */
16190 new_vpos = window_box_height (w) / 2;
16193 if (!cursor_row_fully_visible_p (w, 0, 0))
16195 /* Point does appear, but on a line partly visible at end of window.
16196 Move it back to a fully-visible line. */
16197 new_vpos = window_box_height (w);
16198 /* But if window_box_height suggests a Y coordinate that is
16199 not less than we already have, that line will clearly not
16200 be fully visible, so give up and scroll the display.
16201 This can happen when the default face uses a font whose
16202 dimensions are different from the frame's default
16203 font. */
16204 if (new_vpos >= w->cursor.y)
16206 w->cursor.vpos = -1;
16207 clear_glyph_matrix (w->desired_matrix);
16208 goto try_to_scroll;
16211 else if (w->cursor.vpos >= 0)
16213 /* Some people insist on not letting point enter the scroll
16214 margin, even though this part handles windows that didn't
16215 scroll at all. */
16216 int window_total_lines
16217 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16218 int margin = min (scroll_margin, window_total_lines / 4);
16219 int pixel_margin = margin * frame_line_height;
16220 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16222 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16223 below, which finds the row to move point to, advances by
16224 the Y coordinate of the _next_ row, see the definition of
16225 MATRIX_ROW_BOTTOM_Y. */
16226 if (w->cursor.vpos < margin + header_line)
16228 w->cursor.vpos = -1;
16229 clear_glyph_matrix (w->desired_matrix);
16230 goto try_to_scroll;
16232 else
16234 int window_height = window_box_height (w);
16236 if (header_line)
16237 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16238 if (w->cursor.y >= window_height - pixel_margin)
16240 w->cursor.vpos = -1;
16241 clear_glyph_matrix (w->desired_matrix);
16242 goto try_to_scroll;
16247 /* If we need to move point for either of the above reasons,
16248 now actually do it. */
16249 if (new_vpos >= 0)
16251 struct glyph_row *row;
16253 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16254 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16255 ++row;
16257 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16258 MATRIX_ROW_START_BYTEPOS (row));
16260 if (w != XWINDOW (selected_window))
16261 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16262 else if (current_buffer == old)
16263 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16265 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16267 /* If we are highlighting the region, then we just changed
16268 the region, so redisplay to show it. */
16269 /* FIXME: We need to (re)run pre-redisplay-function! */
16270 /* if (markpos_of_region () >= 0)
16272 clear_glyph_matrix (w->desired_matrix);
16273 if (!try_window (window, startp, 0))
16274 goto need_larger_matrices;
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, d3, d4, d5, d6;
16344 /* If first window line is a continuation line, and window start
16345 is inside the modified region, but the first change is before
16346 current window start, we must select a new window start.
16348 However, if this is the result of a down-mouse event (e.g. by
16349 extending the mouse-drag-overlay), we don't want to select a
16350 new window start, since that would change the position under
16351 the mouse, resulting in an unwanted mouse-movement rather
16352 than a simple mouse-click. */
16353 if (!w->start_at_line_beg
16354 && NILP (do_mouse_tracking)
16355 && CHARPOS (startp) > BEGV
16356 && CHARPOS (startp) > BEG + beg_unchanged
16357 && CHARPOS (startp) <= Z - end_unchanged
16358 /* Even if w->start_at_line_beg is nil, a new window may
16359 start at a line_beg, since that's how set_buffer_window
16360 sets it. So, we need to check the return value of
16361 compute_window_start_on_continuation_line. (See also
16362 bug#197). */
16363 && XMARKER (w->start)->buffer == current_buffer
16364 && compute_window_start_on_continuation_line (w)
16365 /* It doesn't make sense to force the window start like we
16366 do at label force_start if it is already known that point
16367 will not be visible in the resulting window, because
16368 doing so will move point from its correct position
16369 instead of scrolling the window to bring point into view.
16370 See bug#9324. */
16371 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
16373 w->force_start = 1;
16374 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16375 goto force_start;
16378 #ifdef GLYPH_DEBUG
16379 debug_method_add (w, "same window start");
16380 #endif
16382 /* Try to redisplay starting at same place as before.
16383 If point has not moved off frame, accept the results. */
16384 if (!current_matrix_up_to_date_p
16385 /* Don't use try_window_reusing_current_matrix in this case
16386 because a window scroll function can have changed the
16387 buffer. */
16388 || !NILP (Vwindow_scroll_functions)
16389 || MINI_WINDOW_P (w)
16390 || !(used_current_matrix_p
16391 = try_window_reusing_current_matrix (w)))
16393 IF_DEBUG (debug_method_add (w, "1"));
16394 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16395 /* -1 means we need to scroll.
16396 0 means we need new matrices, but fonts_changed
16397 is set in that case, so we will detect it below. */
16398 goto try_to_scroll;
16401 if (f->fonts_changed)
16402 goto need_larger_matrices;
16404 if (w->cursor.vpos >= 0)
16406 if (!just_this_one_p
16407 || current_buffer->clip_changed
16408 || BEG_UNCHANGED < CHARPOS (startp))
16409 /* Forget any recorded base line for line number display. */
16410 w->base_line_number = 0;
16412 if (!cursor_row_fully_visible_p (w, 1, 0))
16414 clear_glyph_matrix (w->desired_matrix);
16415 last_line_misfit = 1;
16417 /* Drop through and scroll. */
16418 else
16419 goto done;
16421 else
16422 clear_glyph_matrix (w->desired_matrix);
16425 try_to_scroll:
16427 /* Redisplay the mode line. Select the buffer properly for that. */
16428 if (!update_mode_line)
16430 update_mode_line = 1;
16431 w->update_mode_line = 1;
16434 /* Try to scroll by specified few lines. */
16435 if ((scroll_conservatively
16436 || emacs_scroll_step
16437 || temp_scroll_step
16438 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16439 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16440 && CHARPOS (startp) >= BEGV
16441 && CHARPOS (startp) <= ZV)
16443 /* The function returns -1 if new fonts were loaded, 1 if
16444 successful, 0 if not successful. */
16445 int ss = try_scrolling (window, just_this_one_p,
16446 scroll_conservatively,
16447 emacs_scroll_step,
16448 temp_scroll_step, last_line_misfit);
16449 switch (ss)
16451 case SCROLLING_SUCCESS:
16452 goto done;
16454 case SCROLLING_NEED_LARGER_MATRICES:
16455 goto need_larger_matrices;
16457 case SCROLLING_FAILED:
16458 break;
16460 default:
16461 emacs_abort ();
16465 /* Finally, just choose a place to start which positions point
16466 according to user preferences. */
16468 recenter:
16470 #ifdef GLYPH_DEBUG
16471 debug_method_add (w, "recenter");
16472 #endif
16474 /* Forget any previously recorded base line for line number display. */
16475 if (!buffer_unchanged_p)
16476 w->base_line_number = 0;
16478 /* Determine the window start relative to point. */
16479 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16480 it.current_y = it.last_visible_y;
16481 if (centering_position < 0)
16483 int window_total_lines
16484 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16485 int margin =
16486 scroll_margin > 0
16487 ? min (scroll_margin, window_total_lines / 4)
16488 : 0;
16489 ptrdiff_t margin_pos = CHARPOS (startp);
16490 Lisp_Object aggressive;
16491 int scrolling_up;
16493 /* If there is a scroll margin at the top of the window, find
16494 its character position. */
16495 if (margin
16496 /* Cannot call start_display if startp is not in the
16497 accessible region of the buffer. This can happen when we
16498 have just switched to a different buffer and/or changed
16499 its restriction. In that case, startp is initialized to
16500 the character position 1 (BEGV) because we did not yet
16501 have chance to display the buffer even once. */
16502 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16504 struct it it1;
16505 void *it1data = NULL;
16507 SAVE_IT (it1, it, it1data);
16508 start_display (&it1, w, startp);
16509 move_it_vertically (&it1, margin * frame_line_height);
16510 margin_pos = IT_CHARPOS (it1);
16511 RESTORE_IT (&it, &it, it1data);
16513 scrolling_up = PT > margin_pos;
16514 aggressive =
16515 scrolling_up
16516 ? BVAR (current_buffer, scroll_up_aggressively)
16517 : BVAR (current_buffer, scroll_down_aggressively);
16519 if (!MINI_WINDOW_P (w)
16520 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16522 int pt_offset = 0;
16524 /* Setting scroll-conservatively overrides
16525 scroll-*-aggressively. */
16526 if (!scroll_conservatively && NUMBERP (aggressive))
16528 double float_amount = XFLOATINT (aggressive);
16530 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16531 if (pt_offset == 0 && float_amount > 0)
16532 pt_offset = 1;
16533 if (pt_offset && margin > 0)
16534 margin -= 1;
16536 /* Compute how much to move the window start backward from
16537 point so that point will be displayed where the user
16538 wants it. */
16539 if (scrolling_up)
16541 centering_position = it.last_visible_y;
16542 if (pt_offset)
16543 centering_position -= pt_offset;
16544 centering_position -=
16545 frame_line_height * (1 + margin + (last_line_misfit != 0))
16546 + WINDOW_HEADER_LINE_HEIGHT (w);
16547 /* Don't let point enter the scroll margin near top of
16548 the window. */
16549 if (centering_position < margin * frame_line_height)
16550 centering_position = margin * frame_line_height;
16552 else
16553 centering_position = margin * frame_line_height + pt_offset;
16555 else
16556 /* Set the window start half the height of the window backward
16557 from point. */
16558 centering_position = window_box_height (w) / 2;
16560 move_it_vertically_backward (&it, centering_position);
16562 eassert (IT_CHARPOS (it) >= BEGV);
16564 /* The function move_it_vertically_backward may move over more
16565 than the specified y-distance. If it->w is small, e.g. a
16566 mini-buffer window, we may end up in front of the window's
16567 display area. Start displaying at the start of the line
16568 containing PT in this case. */
16569 if (it.current_y <= 0)
16571 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16572 move_it_vertically_backward (&it, 0);
16573 it.current_y = 0;
16576 it.current_x = it.hpos = 0;
16578 /* Set the window start position here explicitly, to avoid an
16579 infinite loop in case the functions in window-scroll-functions
16580 get errors. */
16581 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16583 /* Run scroll hooks. */
16584 startp = run_window_scroll_functions (window, it.current.pos);
16586 /* Redisplay the window. */
16587 if (!current_matrix_up_to_date_p
16588 || windows_or_buffers_changed
16589 || f->cursor_type_changed
16590 /* Don't use try_window_reusing_current_matrix in this case
16591 because it can have changed the buffer. */
16592 || !NILP (Vwindow_scroll_functions)
16593 || !just_this_one_p
16594 || MINI_WINDOW_P (w)
16595 || !(used_current_matrix_p
16596 = try_window_reusing_current_matrix (w)))
16597 try_window (window, startp, 0);
16599 /* If new fonts have been loaded (due to fontsets), give up. We
16600 have to start a new redisplay since we need to re-adjust glyph
16601 matrices. */
16602 if (f->fonts_changed)
16603 goto need_larger_matrices;
16605 /* If cursor did not appear assume that the middle of the window is
16606 in the first line of the window. Do it again with the next line.
16607 (Imagine a window of height 100, displaying two lines of height
16608 60. Moving back 50 from it->last_visible_y will end in the first
16609 line.) */
16610 if (w->cursor.vpos < 0)
16612 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16614 clear_glyph_matrix (w->desired_matrix);
16615 move_it_by_lines (&it, 1);
16616 try_window (window, it.current.pos, 0);
16618 else if (PT < IT_CHARPOS (it))
16620 clear_glyph_matrix (w->desired_matrix);
16621 move_it_by_lines (&it, -1);
16622 try_window (window, it.current.pos, 0);
16624 else
16626 /* Not much we can do about it. */
16630 /* Consider the following case: Window starts at BEGV, there is
16631 invisible, intangible text at BEGV, so that display starts at
16632 some point START > BEGV. It can happen that we are called with
16633 PT somewhere between BEGV and START. Try to handle that case,
16634 and similar ones. */
16635 if (w->cursor.vpos < 0)
16637 /* First, try locating the proper glyph row for PT. */
16638 struct glyph_row *row =
16639 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16641 /* Sometimes point is at the beginning of invisible text that is
16642 before the 1st character displayed in the row. In that case,
16643 row_containing_pos fails to find the row, because no glyphs
16644 with appropriate buffer positions are present in the row.
16645 Therefore, we next try to find the row which shows the 1st
16646 position after the invisible text. */
16647 if (!row)
16649 Lisp_Object val =
16650 get_char_property_and_overlay (make_number (PT), Qinvisible,
16651 Qnil, NULL);
16653 if (TEXT_PROP_MEANS_INVISIBLE (val))
16655 ptrdiff_t alt_pos;
16656 Lisp_Object invis_end =
16657 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16658 Qnil, Qnil);
16660 if (NATNUMP (invis_end))
16661 alt_pos = XFASTINT (invis_end);
16662 else
16663 alt_pos = ZV;
16664 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16665 NULL, 0);
16668 /* Finally, fall back on the first row of the window after the
16669 header line (if any). This is slightly better than not
16670 displaying the cursor at all. */
16671 if (!row)
16673 row = w->current_matrix->rows;
16674 if (row->mode_line_p)
16675 ++row;
16677 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16680 if (!cursor_row_fully_visible_p (w, 0, 0))
16682 /* If vscroll is enabled, disable it and try again. */
16683 if (w->vscroll)
16685 w->vscroll = 0;
16686 clear_glyph_matrix (w->desired_matrix);
16687 goto recenter;
16690 /* Users who set scroll-conservatively to a large number want
16691 point just above/below the scroll margin. If we ended up
16692 with point's row partially visible, move the window start to
16693 make that row fully visible and out of the margin. */
16694 if (scroll_conservatively > SCROLL_LIMIT)
16696 int window_total_lines
16697 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16698 int margin =
16699 scroll_margin > 0
16700 ? min (scroll_margin, window_total_lines / 4)
16701 : 0;
16702 int move_down = w->cursor.vpos >= window_total_lines / 2;
16704 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16705 clear_glyph_matrix (w->desired_matrix);
16706 if (1 == try_window (window, it.current.pos,
16707 TRY_WINDOW_CHECK_MARGINS))
16708 goto done;
16711 /* If centering point failed to make the whole line visible,
16712 put point at the top instead. That has to make the whole line
16713 visible, if it can be done. */
16714 if (centering_position == 0)
16715 goto done;
16717 clear_glyph_matrix (w->desired_matrix);
16718 centering_position = 0;
16719 goto recenter;
16722 done:
16724 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16725 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16726 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16728 /* Display the mode line, if we must. */
16729 if ((update_mode_line
16730 /* If window not full width, must redo its mode line
16731 if (a) the window to its side is being redone and
16732 (b) we do a frame-based redisplay. This is a consequence
16733 of how inverted lines are drawn in frame-based redisplay. */
16734 || (!just_this_one_p
16735 && !FRAME_WINDOW_P (f)
16736 && !WINDOW_FULL_WIDTH_P (w))
16737 /* Line number to display. */
16738 || w->base_line_pos > 0
16739 /* Column number is displayed and different from the one displayed. */
16740 || (w->column_number_displayed != -1
16741 && (w->column_number_displayed != current_column ())))
16742 /* This means that the window has a mode line. */
16743 && (WINDOW_WANTS_MODELINE_P (w)
16744 || WINDOW_WANTS_HEADER_LINE_P (w)))
16747 display_mode_lines (w);
16749 /* If mode line height has changed, arrange for a thorough
16750 immediate redisplay using the correct mode line height. */
16751 if (WINDOW_WANTS_MODELINE_P (w)
16752 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16754 f->fonts_changed = 1;
16755 w->mode_line_height = -1;
16756 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16757 = DESIRED_MODE_LINE_HEIGHT (w);
16760 /* If header line height has changed, arrange for a thorough
16761 immediate redisplay using the correct header line height. */
16762 if (WINDOW_WANTS_HEADER_LINE_P (w)
16763 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16765 f->fonts_changed = 1;
16766 w->header_line_height = -1;
16767 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16768 = DESIRED_HEADER_LINE_HEIGHT (w);
16771 if (f->fonts_changed)
16772 goto need_larger_matrices;
16775 if (!line_number_displayed && w->base_line_pos != -1)
16777 w->base_line_pos = 0;
16778 w->base_line_number = 0;
16781 finish_menu_bars:
16783 /* When we reach a frame's selected window, redo the frame's menu bar. */
16784 if (update_mode_line
16785 && EQ (FRAME_SELECTED_WINDOW (f), window))
16787 int redisplay_menu_p = 0;
16789 if (FRAME_WINDOW_P (f))
16791 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16792 || defined (HAVE_NS) || defined (USE_GTK)
16793 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16794 #else
16795 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16796 #endif
16798 else
16799 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16801 if (redisplay_menu_p)
16802 display_menu_bar (w);
16804 #ifdef HAVE_WINDOW_SYSTEM
16805 if (FRAME_WINDOW_P (f))
16807 #if defined (USE_GTK) || defined (HAVE_NS)
16808 if (FRAME_EXTERNAL_TOOL_BAR (f))
16809 redisplay_tool_bar (f);
16810 #else
16811 if (WINDOWP (f->tool_bar_window)
16812 && (FRAME_TOOL_BAR_HEIGHT (f) > 0
16813 || !NILP (Vauto_resize_tool_bars))
16814 && redisplay_tool_bar (f))
16815 ignore_mouse_drag_p = 1;
16816 #endif
16818 #endif
16821 #ifdef HAVE_WINDOW_SYSTEM
16822 if (FRAME_WINDOW_P (f)
16823 && update_window_fringes (w, (just_this_one_p
16824 || (!used_current_matrix_p && !overlay_arrow_seen)
16825 || w->pseudo_window_p)))
16827 update_begin (f);
16828 block_input ();
16829 if (draw_window_fringes (w, 1))
16831 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16832 x_draw_right_divider (w);
16833 else
16834 x_draw_vertical_border (w);
16836 unblock_input ();
16837 update_end (f);
16840 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16841 x_draw_bottom_divider (w);
16842 #endif /* HAVE_WINDOW_SYSTEM */
16844 /* We go to this label, with fonts_changed set, if it is
16845 necessary to try again using larger glyph matrices.
16846 We have to redeem the scroll bar even in this case,
16847 because the loop in redisplay_internal expects that. */
16848 need_larger_matrices:
16850 finish_scroll_bars:
16852 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16854 /* Set the thumb's position and size. */
16855 set_vertical_scroll_bar (w);
16857 /* Note that we actually used the scroll bar attached to this
16858 window, so it shouldn't be deleted at the end of redisplay. */
16859 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16860 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16863 /* Restore current_buffer and value of point in it. The window
16864 update may have changed the buffer, so first make sure `opoint'
16865 is still valid (Bug#6177). */
16866 if (CHARPOS (opoint) < BEGV)
16867 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16868 else if (CHARPOS (opoint) > ZV)
16869 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16870 else
16871 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16873 set_buffer_internal_1 (old);
16874 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16875 shorter. This can be caused by log truncation in *Messages*. */
16876 if (CHARPOS (lpoint) <= ZV)
16877 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16879 unbind_to (count, Qnil);
16883 /* Build the complete desired matrix of WINDOW with a window start
16884 buffer position POS.
16886 Value is 1 if successful. It is zero if fonts were loaded during
16887 redisplay which makes re-adjusting glyph matrices necessary, and -1
16888 if point would appear in the scroll margins.
16889 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16890 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16891 set in FLAGS.) */
16894 try_window (Lisp_Object window, struct text_pos pos, int flags)
16896 struct window *w = XWINDOW (window);
16897 struct it it;
16898 struct glyph_row *last_text_row = NULL;
16899 struct frame *f = XFRAME (w->frame);
16900 int frame_line_height = default_line_pixel_height (w);
16902 /* Make POS the new window start. */
16903 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16905 /* Mark cursor position as unknown. No overlay arrow seen. */
16906 w->cursor.vpos = -1;
16907 overlay_arrow_seen = 0;
16909 /* Initialize iterator and info to start at POS. */
16910 start_display (&it, w, pos);
16912 /* Display all lines of W. */
16913 while (it.current_y < it.last_visible_y)
16915 if (display_line (&it))
16916 last_text_row = it.glyph_row - 1;
16917 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16918 return 0;
16921 /* Don't let the cursor end in the scroll margins. */
16922 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16923 && !MINI_WINDOW_P (w))
16925 int this_scroll_margin;
16926 int window_total_lines
16927 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16929 if (scroll_margin > 0)
16931 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16932 this_scroll_margin *= frame_line_height;
16934 else
16935 this_scroll_margin = 0;
16937 if ((w->cursor.y >= 0 /* not vscrolled */
16938 && w->cursor.y < this_scroll_margin
16939 && CHARPOS (pos) > BEGV
16940 && IT_CHARPOS (it) < ZV)
16941 /* rms: considering make_cursor_line_fully_visible_p here
16942 seems to give wrong results. We don't want to recenter
16943 when the last line is partly visible, we want to allow
16944 that case to be handled in the usual way. */
16945 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16947 w->cursor.vpos = -1;
16948 clear_glyph_matrix (w->desired_matrix);
16949 return -1;
16953 /* If bottom moved off end of frame, change mode line percentage. */
16954 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
16955 w->update_mode_line = 1;
16957 /* Set window_end_pos to the offset of the last character displayed
16958 on the window from the end of current_buffer. Set
16959 window_end_vpos to its row number. */
16960 if (last_text_row)
16962 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16963 adjust_window_ends (w, last_text_row, 0);
16964 eassert
16965 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16966 w->window_end_vpos)));
16968 else
16970 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16971 w->window_end_pos = Z - ZV;
16972 w->window_end_vpos = 0;
16975 /* But that is not valid info until redisplay finishes. */
16976 w->window_end_valid = 0;
16977 return 1;
16982 /************************************************************************
16983 Window redisplay reusing current matrix when buffer has not changed
16984 ************************************************************************/
16986 /* Try redisplay of window W showing an unchanged buffer with a
16987 different window start than the last time it was displayed by
16988 reusing its current matrix. Value is non-zero if successful.
16989 W->start is the new window start. */
16991 static int
16992 try_window_reusing_current_matrix (struct window *w)
16994 struct frame *f = XFRAME (w->frame);
16995 struct glyph_row *bottom_row;
16996 struct it it;
16997 struct run run;
16998 struct text_pos start, new_start;
16999 int nrows_scrolled, i;
17000 struct glyph_row *last_text_row;
17001 struct glyph_row *last_reused_text_row;
17002 struct glyph_row *start_row;
17003 int start_vpos, min_y, max_y;
17005 #ifdef GLYPH_DEBUG
17006 if (inhibit_try_window_reusing)
17007 return 0;
17008 #endif
17010 if (/* This function doesn't handle terminal frames. */
17011 !FRAME_WINDOW_P (f)
17012 /* Don't try to reuse the display if windows have been split
17013 or such. */
17014 || windows_or_buffers_changed
17015 || f->cursor_type_changed)
17016 return 0;
17018 /* Can't do this if showing trailing whitespace. */
17019 if (!NILP (Vshow_trailing_whitespace))
17020 return 0;
17022 /* If top-line visibility has changed, give up. */
17023 if (WINDOW_WANTS_HEADER_LINE_P (w)
17024 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17025 return 0;
17027 /* Give up if old or new display is scrolled vertically. We could
17028 make this function handle this, but right now it doesn't. */
17029 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17030 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17031 return 0;
17033 /* The variable new_start now holds the new window start. The old
17034 start `start' can be determined from the current matrix. */
17035 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17036 start = start_row->minpos;
17037 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17039 /* Clear the desired matrix for the display below. */
17040 clear_glyph_matrix (w->desired_matrix);
17042 if (CHARPOS (new_start) <= CHARPOS (start))
17044 /* Don't use this method if the display starts with an ellipsis
17045 displayed for invisible text. It's not easy to handle that case
17046 below, and it's certainly not worth the effort since this is
17047 not a frequent case. */
17048 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17049 return 0;
17051 IF_DEBUG (debug_method_add (w, "twu1"));
17053 /* Display up to a row that can be reused. The variable
17054 last_text_row is set to the last row displayed that displays
17055 text. Note that it.vpos == 0 if or if not there is a
17056 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17057 start_display (&it, w, new_start);
17058 w->cursor.vpos = -1;
17059 last_text_row = last_reused_text_row = NULL;
17061 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17063 /* If we have reached into the characters in the START row,
17064 that means the line boundaries have changed. So we
17065 can't start copying with the row START. Maybe it will
17066 work to start copying with the following row. */
17067 while (IT_CHARPOS (it) > CHARPOS (start))
17069 /* Advance to the next row as the "start". */
17070 start_row++;
17071 start = start_row->minpos;
17072 /* If there are no more rows to try, or just one, give up. */
17073 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17074 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17075 || CHARPOS (start) == ZV)
17077 clear_glyph_matrix (w->desired_matrix);
17078 return 0;
17081 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17083 /* If we have reached alignment, we can copy the rest of the
17084 rows. */
17085 if (IT_CHARPOS (it) == CHARPOS (start)
17086 /* Don't accept "alignment" inside a display vector,
17087 since start_row could have started in the middle of
17088 that same display vector (thus their character
17089 positions match), and we have no way of telling if
17090 that is the case. */
17091 && it.current.dpvec_index < 0)
17092 break;
17094 if (display_line (&it))
17095 last_text_row = it.glyph_row - 1;
17099 /* A value of current_y < last_visible_y means that we stopped
17100 at the previous window start, which in turn means that we
17101 have at least one reusable row. */
17102 if (it.current_y < it.last_visible_y)
17104 struct glyph_row *row;
17106 /* IT.vpos always starts from 0; it counts text lines. */
17107 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17109 /* Find PT if not already found in the lines displayed. */
17110 if (w->cursor.vpos < 0)
17112 int dy = it.current_y - start_row->y;
17114 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17115 row = row_containing_pos (w, PT, row, NULL, dy);
17116 if (row)
17117 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17118 dy, nrows_scrolled);
17119 else
17121 clear_glyph_matrix (w->desired_matrix);
17122 return 0;
17126 /* Scroll the display. Do it before the current matrix is
17127 changed. The problem here is that update has not yet
17128 run, i.e. part of the current matrix is not up to date.
17129 scroll_run_hook will clear the cursor, and use the
17130 current matrix to get the height of the row the cursor is
17131 in. */
17132 run.current_y = start_row->y;
17133 run.desired_y = it.current_y;
17134 run.height = it.last_visible_y - it.current_y;
17136 if (run.height > 0 && run.current_y != run.desired_y)
17138 update_begin (f);
17139 FRAME_RIF (f)->update_window_begin_hook (w);
17140 FRAME_RIF (f)->clear_window_mouse_face (w);
17141 FRAME_RIF (f)->scroll_run_hook (w, &run);
17142 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17143 update_end (f);
17146 /* Shift current matrix down by nrows_scrolled lines. */
17147 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17148 rotate_matrix (w->current_matrix,
17149 start_vpos,
17150 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17151 nrows_scrolled);
17153 /* Disable lines that must be updated. */
17154 for (i = 0; i < nrows_scrolled; ++i)
17155 (start_row + i)->enabled_p = false;
17157 /* Re-compute Y positions. */
17158 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17159 max_y = it.last_visible_y;
17160 for (row = start_row + nrows_scrolled;
17161 row < bottom_row;
17162 ++row)
17164 row->y = it.current_y;
17165 row->visible_height = row->height;
17167 if (row->y < min_y)
17168 row->visible_height -= min_y - row->y;
17169 if (row->y + row->height > max_y)
17170 row->visible_height -= row->y + row->height - max_y;
17171 if (row->fringe_bitmap_periodic_p)
17172 row->redraw_fringe_bitmaps_p = 1;
17174 it.current_y += row->height;
17176 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17177 last_reused_text_row = row;
17178 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17179 break;
17182 /* Disable lines in the current matrix which are now
17183 below the window. */
17184 for (++row; row < bottom_row; ++row)
17185 row->enabled_p = row->mode_line_p = 0;
17188 /* Update window_end_pos etc.; last_reused_text_row is the last
17189 reused row from the current matrix containing text, if any.
17190 The value of last_text_row is the last displayed line
17191 containing text. */
17192 if (last_reused_text_row)
17193 adjust_window_ends (w, last_reused_text_row, 1);
17194 else if (last_text_row)
17195 adjust_window_ends (w, last_text_row, 0);
17196 else
17198 /* This window must be completely empty. */
17199 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17200 w->window_end_pos = Z - ZV;
17201 w->window_end_vpos = 0;
17203 w->window_end_valid = 0;
17205 /* Update hint: don't try scrolling again in update_window. */
17206 w->desired_matrix->no_scrolling_p = 1;
17208 #ifdef GLYPH_DEBUG
17209 debug_method_add (w, "try_window_reusing_current_matrix 1");
17210 #endif
17211 return 1;
17213 else if (CHARPOS (new_start) > CHARPOS (start))
17215 struct glyph_row *pt_row, *row;
17216 struct glyph_row *first_reusable_row;
17217 struct glyph_row *first_row_to_display;
17218 int dy;
17219 int yb = window_text_bottom_y (w);
17221 /* Find the row starting at new_start, if there is one. Don't
17222 reuse a partially visible line at the end. */
17223 first_reusable_row = start_row;
17224 while (first_reusable_row->enabled_p
17225 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17226 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17227 < CHARPOS (new_start)))
17228 ++first_reusable_row;
17230 /* Give up if there is no row to reuse. */
17231 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17232 || !first_reusable_row->enabled_p
17233 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17234 != CHARPOS (new_start)))
17235 return 0;
17237 /* We can reuse fully visible rows beginning with
17238 first_reusable_row to the end of the window. Set
17239 first_row_to_display to the first row that cannot be reused.
17240 Set pt_row to the row containing point, if there is any. */
17241 pt_row = NULL;
17242 for (first_row_to_display = first_reusable_row;
17243 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17244 ++first_row_to_display)
17246 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17247 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17248 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17249 && first_row_to_display->ends_at_zv_p
17250 && pt_row == NULL)))
17251 pt_row = first_row_to_display;
17254 /* Start displaying at the start of first_row_to_display. */
17255 eassert (first_row_to_display->y < yb);
17256 init_to_row_start (&it, w, first_row_to_display);
17258 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17259 - start_vpos);
17260 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17261 - nrows_scrolled);
17262 it.current_y = (first_row_to_display->y - first_reusable_row->y
17263 + WINDOW_HEADER_LINE_HEIGHT (w));
17265 /* Display lines beginning with first_row_to_display in the
17266 desired matrix. Set last_text_row to the last row displayed
17267 that displays text. */
17268 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17269 if (pt_row == NULL)
17270 w->cursor.vpos = -1;
17271 last_text_row = NULL;
17272 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17273 if (display_line (&it))
17274 last_text_row = it.glyph_row - 1;
17276 /* If point is in a reused row, adjust y and vpos of the cursor
17277 position. */
17278 if (pt_row)
17280 w->cursor.vpos -= nrows_scrolled;
17281 w->cursor.y -= first_reusable_row->y - start_row->y;
17284 /* Give up if point isn't in a row displayed or reused. (This
17285 also handles the case where w->cursor.vpos < nrows_scrolled
17286 after the calls to display_line, which can happen with scroll
17287 margins. See bug#1295.) */
17288 if (w->cursor.vpos < 0)
17290 clear_glyph_matrix (w->desired_matrix);
17291 return 0;
17294 /* Scroll the display. */
17295 run.current_y = first_reusable_row->y;
17296 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17297 run.height = it.last_visible_y - run.current_y;
17298 dy = run.current_y - run.desired_y;
17300 if (run.height)
17302 update_begin (f);
17303 FRAME_RIF (f)->update_window_begin_hook (w);
17304 FRAME_RIF (f)->clear_window_mouse_face (w);
17305 FRAME_RIF (f)->scroll_run_hook (w, &run);
17306 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17307 update_end (f);
17310 /* Adjust Y positions of reused rows. */
17311 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17312 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17313 max_y = it.last_visible_y;
17314 for (row = first_reusable_row; row < first_row_to_display; ++row)
17316 row->y -= dy;
17317 row->visible_height = row->height;
17318 if (row->y < min_y)
17319 row->visible_height -= min_y - row->y;
17320 if (row->y + row->height > max_y)
17321 row->visible_height -= row->y + row->height - max_y;
17322 if (row->fringe_bitmap_periodic_p)
17323 row->redraw_fringe_bitmaps_p = 1;
17326 /* Scroll the current matrix. */
17327 eassert (nrows_scrolled > 0);
17328 rotate_matrix (w->current_matrix,
17329 start_vpos,
17330 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17331 -nrows_scrolled);
17333 /* Disable rows not reused. */
17334 for (row -= nrows_scrolled; row < bottom_row; ++row)
17335 row->enabled_p = false;
17337 /* Point may have moved to a different line, so we cannot assume that
17338 the previous cursor position is valid; locate the correct row. */
17339 if (pt_row)
17341 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17342 row < bottom_row
17343 && PT >= MATRIX_ROW_END_CHARPOS (row)
17344 && !row->ends_at_zv_p;
17345 row++)
17347 w->cursor.vpos++;
17348 w->cursor.y = row->y;
17350 if (row < bottom_row)
17352 /* Can't simply scan the row for point with
17353 bidi-reordered glyph rows. Let set_cursor_from_row
17354 figure out where to put the cursor, and if it fails,
17355 give up. */
17356 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17358 if (!set_cursor_from_row (w, row, w->current_matrix,
17359 0, 0, 0, 0))
17361 clear_glyph_matrix (w->desired_matrix);
17362 return 0;
17365 else
17367 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17368 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17370 for (; glyph < end
17371 && (!BUFFERP (glyph->object)
17372 || glyph->charpos < PT);
17373 glyph++)
17375 w->cursor.hpos++;
17376 w->cursor.x += glyph->pixel_width;
17382 /* Adjust window end. A null value of last_text_row means that
17383 the window end is in reused rows which in turn means that
17384 only its vpos can have changed. */
17385 if (last_text_row)
17386 adjust_window_ends (w, last_text_row, 0);
17387 else
17388 w->window_end_vpos -= nrows_scrolled;
17390 w->window_end_valid = 0;
17391 w->desired_matrix->no_scrolling_p = 1;
17393 #ifdef GLYPH_DEBUG
17394 debug_method_add (w, "try_window_reusing_current_matrix 2");
17395 #endif
17396 return 1;
17399 return 0;
17404 /************************************************************************
17405 Window redisplay reusing current matrix when buffer has changed
17406 ************************************************************************/
17408 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17409 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17410 ptrdiff_t *, ptrdiff_t *);
17411 static struct glyph_row *
17412 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17413 struct glyph_row *);
17416 /* Return the last row in MATRIX displaying text. If row START is
17417 non-null, start searching with that row. IT gives the dimensions
17418 of the display. Value is null if matrix is empty; otherwise it is
17419 a pointer to the row found. */
17421 static struct glyph_row *
17422 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17423 struct glyph_row *start)
17425 struct glyph_row *row, *row_found;
17427 /* Set row_found to the last row in IT->w's current matrix
17428 displaying text. The loop looks funny but think of partially
17429 visible lines. */
17430 row_found = NULL;
17431 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17432 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17434 eassert (row->enabled_p);
17435 row_found = row;
17436 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17437 break;
17438 ++row;
17441 return row_found;
17445 /* Return the last row in the current matrix of W that is not affected
17446 by changes at the start of current_buffer that occurred since W's
17447 current matrix was built. Value is null if no such row exists.
17449 BEG_UNCHANGED us the number of characters unchanged at the start of
17450 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17451 first changed character in current_buffer. Characters at positions <
17452 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17453 when the current matrix was built. */
17455 static struct glyph_row *
17456 find_last_unchanged_at_beg_row (struct window *w)
17458 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17459 struct glyph_row *row;
17460 struct glyph_row *row_found = NULL;
17461 int yb = window_text_bottom_y (w);
17463 /* Find the last row displaying unchanged text. */
17464 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17465 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17466 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17467 ++row)
17469 if (/* If row ends before first_changed_pos, it is unchanged,
17470 except in some case. */
17471 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17472 /* When row ends in ZV and we write at ZV it is not
17473 unchanged. */
17474 && !row->ends_at_zv_p
17475 /* When first_changed_pos is the end of a continued line,
17476 row is not unchanged because it may be no longer
17477 continued. */
17478 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17479 && (row->continued_p
17480 || row->exact_window_width_line_p))
17481 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17482 needs to be recomputed, so don't consider this row as
17483 unchanged. This happens when the last line was
17484 bidi-reordered and was killed immediately before this
17485 redisplay cycle. In that case, ROW->end stores the
17486 buffer position of the first visual-order character of
17487 the killed text, which is now beyond ZV. */
17488 && CHARPOS (row->end.pos) <= ZV)
17489 row_found = row;
17491 /* Stop if last visible row. */
17492 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17493 break;
17496 return row_found;
17500 /* Find the first glyph row in the current matrix of W that is not
17501 affected by changes at the end of current_buffer since the
17502 time W's current matrix was built.
17504 Return in *DELTA the number of chars by which buffer positions in
17505 unchanged text at the end of current_buffer must be adjusted.
17507 Return in *DELTA_BYTES the corresponding number of bytes.
17509 Value is null if no such row exists, i.e. all rows are affected by
17510 changes. */
17512 static struct glyph_row *
17513 find_first_unchanged_at_end_row (struct window *w,
17514 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17516 struct glyph_row *row;
17517 struct glyph_row *row_found = NULL;
17519 *delta = *delta_bytes = 0;
17521 /* Display must not have been paused, otherwise the current matrix
17522 is not up to date. */
17523 eassert (w->window_end_valid);
17525 /* A value of window_end_pos >= END_UNCHANGED means that the window
17526 end is in the range of changed text. If so, there is no
17527 unchanged row at the end of W's current matrix. */
17528 if (w->window_end_pos >= END_UNCHANGED)
17529 return NULL;
17531 /* Set row to the last row in W's current matrix displaying text. */
17532 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17534 /* If matrix is entirely empty, no unchanged row exists. */
17535 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17537 /* The value of row is the last glyph row in the matrix having a
17538 meaningful buffer position in it. The end position of row
17539 corresponds to window_end_pos. This allows us to translate
17540 buffer positions in the current matrix to current buffer
17541 positions for characters not in changed text. */
17542 ptrdiff_t Z_old =
17543 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17544 ptrdiff_t Z_BYTE_old =
17545 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17546 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17547 struct glyph_row *first_text_row
17548 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17550 *delta = Z - Z_old;
17551 *delta_bytes = Z_BYTE - Z_BYTE_old;
17553 /* Set last_unchanged_pos to the buffer position of the last
17554 character in the buffer that has not been changed. Z is the
17555 index + 1 of the last character in current_buffer, i.e. by
17556 subtracting END_UNCHANGED we get the index of the last
17557 unchanged character, and we have to add BEG to get its buffer
17558 position. */
17559 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17560 last_unchanged_pos_old = last_unchanged_pos - *delta;
17562 /* Search backward from ROW for a row displaying a line that
17563 starts at a minimum position >= last_unchanged_pos_old. */
17564 for (; row > first_text_row; --row)
17566 /* This used to abort, but it can happen.
17567 It is ok to just stop the search instead here. KFS. */
17568 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17569 break;
17571 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17572 row_found = row;
17576 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17578 return row_found;
17582 /* Make sure that glyph rows in the current matrix of window W
17583 reference the same glyph memory as corresponding rows in the
17584 frame's frame matrix. This function is called after scrolling W's
17585 current matrix on a terminal frame in try_window_id and
17586 try_window_reusing_current_matrix. */
17588 static void
17589 sync_frame_with_window_matrix_rows (struct window *w)
17591 struct frame *f = XFRAME (w->frame);
17592 struct glyph_row *window_row, *window_row_end, *frame_row;
17594 /* Preconditions: W must be a leaf window and full-width. Its frame
17595 must have a frame matrix. */
17596 eassert (BUFFERP (w->contents));
17597 eassert (WINDOW_FULL_WIDTH_P (w));
17598 eassert (!FRAME_WINDOW_P (f));
17600 /* If W is a full-width window, glyph pointers in W's current matrix
17601 have, by definition, to be the same as glyph pointers in the
17602 corresponding frame matrix. Note that frame matrices have no
17603 marginal areas (see build_frame_matrix). */
17604 window_row = w->current_matrix->rows;
17605 window_row_end = window_row + w->current_matrix->nrows;
17606 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17607 while (window_row < window_row_end)
17609 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17610 struct glyph *end = window_row->glyphs[LAST_AREA];
17612 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17613 frame_row->glyphs[TEXT_AREA] = start;
17614 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17615 frame_row->glyphs[LAST_AREA] = end;
17617 /* Disable frame rows whose corresponding window rows have
17618 been disabled in try_window_id. */
17619 if (!window_row->enabled_p)
17620 frame_row->enabled_p = false;
17622 ++window_row, ++frame_row;
17627 /* Find the glyph row in window W containing CHARPOS. Consider all
17628 rows between START and END (not inclusive). END null means search
17629 all rows to the end of the display area of W. Value is the row
17630 containing CHARPOS or null. */
17632 struct glyph_row *
17633 row_containing_pos (struct window *w, ptrdiff_t charpos,
17634 struct glyph_row *start, struct glyph_row *end, int dy)
17636 struct glyph_row *row = start;
17637 struct glyph_row *best_row = NULL;
17638 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17639 int last_y;
17641 /* If we happen to start on a header-line, skip that. */
17642 if (row->mode_line_p)
17643 ++row;
17645 if ((end && row >= end) || !row->enabled_p)
17646 return NULL;
17648 last_y = window_text_bottom_y (w) - dy;
17650 while (1)
17652 /* Give up if we have gone too far. */
17653 if (end && row >= end)
17654 return NULL;
17655 /* This formerly returned if they were equal.
17656 I think that both quantities are of a "last plus one" type;
17657 if so, when they are equal, the row is within the screen. -- rms. */
17658 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17659 return NULL;
17661 /* If it is in this row, return this row. */
17662 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17663 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17664 /* The end position of a row equals the start
17665 position of the next row. If CHARPOS is there, we
17666 would rather consider it displayed in the next
17667 line, except when this line ends in ZV. */
17668 && !row_for_charpos_p (row, charpos)))
17669 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17671 struct glyph *g;
17673 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17674 || (!best_row && !row->continued_p))
17675 return row;
17676 /* In bidi-reordered rows, there could be several rows whose
17677 edges surround CHARPOS, all of these rows belonging to
17678 the same continued line. We need to find the row which
17679 fits CHARPOS the best. */
17680 for (g = row->glyphs[TEXT_AREA];
17681 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17682 g++)
17684 if (!STRINGP (g->object))
17686 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17688 mindif = eabs (g->charpos - charpos);
17689 best_row = row;
17690 /* Exact match always wins. */
17691 if (mindif == 0)
17692 return best_row;
17697 else if (best_row && !row->continued_p)
17698 return best_row;
17699 ++row;
17704 /* Try to redisplay window W by reusing its existing display. W's
17705 current matrix must be up to date when this function is called,
17706 i.e. window_end_valid must be nonzero.
17708 Value is
17710 >= 1 if successful, i.e. display has been updated
17711 specifically:
17712 1 means the changes were in front of a newline that precedes
17713 the window start, and the whole current matrix was reused
17714 2 means the changes were after the last position displayed
17715 in the window, and the whole current matrix was reused
17716 3 means portions of the current matrix were reused, while
17717 some of the screen lines were redrawn
17718 -1 if redisplay with same window start is known not to succeed
17719 0 if otherwise unsuccessful
17721 The following steps are performed:
17723 1. Find the last row in the current matrix of W that is not
17724 affected by changes at the start of current_buffer. If no such row
17725 is found, give up.
17727 2. Find the first row in W's current matrix that is not affected by
17728 changes at the end of current_buffer. Maybe there is no such row.
17730 3. Display lines beginning with the row + 1 found in step 1 to the
17731 row found in step 2 or, if step 2 didn't find a row, to the end of
17732 the window.
17734 4. If cursor is not known to appear on the window, give up.
17736 5. If display stopped at the row found in step 2, scroll the
17737 display and current matrix as needed.
17739 6. Maybe display some lines at the end of W, if we must. This can
17740 happen under various circumstances, like a partially visible line
17741 becoming fully visible, or because newly displayed lines are displayed
17742 in smaller font sizes.
17744 7. Update W's window end information. */
17746 static int
17747 try_window_id (struct window *w)
17749 struct frame *f = XFRAME (w->frame);
17750 struct glyph_matrix *current_matrix = w->current_matrix;
17751 struct glyph_matrix *desired_matrix = w->desired_matrix;
17752 struct glyph_row *last_unchanged_at_beg_row;
17753 struct glyph_row *first_unchanged_at_end_row;
17754 struct glyph_row *row;
17755 struct glyph_row *bottom_row;
17756 int bottom_vpos;
17757 struct it it;
17758 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17759 int dvpos, dy;
17760 struct text_pos start_pos;
17761 struct run run;
17762 int first_unchanged_at_end_vpos = 0;
17763 struct glyph_row *last_text_row, *last_text_row_at_end;
17764 struct text_pos start;
17765 ptrdiff_t first_changed_charpos, last_changed_charpos;
17767 #ifdef GLYPH_DEBUG
17768 if (inhibit_try_window_id)
17769 return 0;
17770 #endif
17772 /* This is handy for debugging. */
17773 #if 0
17774 #define GIVE_UP(X) \
17775 do { \
17776 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17777 return 0; \
17778 } while (0)
17779 #else
17780 #define GIVE_UP(X) return 0
17781 #endif
17783 SET_TEXT_POS_FROM_MARKER (start, w->start);
17785 /* Don't use this for mini-windows because these can show
17786 messages and mini-buffers, and we don't handle that here. */
17787 if (MINI_WINDOW_P (w))
17788 GIVE_UP (1);
17790 /* This flag is used to prevent redisplay optimizations. */
17791 if (windows_or_buffers_changed || f->cursor_type_changed)
17792 GIVE_UP (2);
17794 /* This function's optimizations cannot be used if overlays have
17795 changed in the buffer displayed by the window, so give up if they
17796 have. */
17797 if (w->last_overlay_modified != OVERLAY_MODIFF)
17798 GIVE_UP (21);
17800 /* Verify that narrowing has not changed.
17801 Also verify that we were not told to prevent redisplay optimizations.
17802 It would be nice to further
17803 reduce the number of cases where this prevents try_window_id. */
17804 if (current_buffer->clip_changed
17805 || current_buffer->prevent_redisplay_optimizations_p)
17806 GIVE_UP (3);
17808 /* Window must either use window-based redisplay or be full width. */
17809 if (!FRAME_WINDOW_P (f)
17810 && (!FRAME_LINE_INS_DEL_OK (f)
17811 || !WINDOW_FULL_WIDTH_P (w)))
17812 GIVE_UP (4);
17814 /* Give up if point is known NOT to appear in W. */
17815 if (PT < CHARPOS (start))
17816 GIVE_UP (5);
17818 /* Another way to prevent redisplay optimizations. */
17819 if (w->last_modified == 0)
17820 GIVE_UP (6);
17822 /* Verify that window is not hscrolled. */
17823 if (w->hscroll != 0)
17824 GIVE_UP (7);
17826 /* Verify that display wasn't paused. */
17827 if (!w->window_end_valid)
17828 GIVE_UP (8);
17830 /* Likewise if highlighting trailing whitespace. */
17831 if (!NILP (Vshow_trailing_whitespace))
17832 GIVE_UP (11);
17834 /* Can't use this if overlay arrow position and/or string have
17835 changed. */
17836 if (overlay_arrows_changed_p ())
17837 GIVE_UP (12);
17839 /* When word-wrap is on, adding a space to the first word of a
17840 wrapped line can change the wrap position, altering the line
17841 above it. It might be worthwhile to handle this more
17842 intelligently, but for now just redisplay from scratch. */
17843 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17844 GIVE_UP (21);
17846 /* Under bidi reordering, adding or deleting a character in the
17847 beginning of a paragraph, before the first strong directional
17848 character, can change the base direction of the paragraph (unless
17849 the buffer specifies a fixed paragraph direction), which will
17850 require to redisplay the whole paragraph. It might be worthwhile
17851 to find the paragraph limits and widen the range of redisplayed
17852 lines to that, but for now just give up this optimization and
17853 redisplay from scratch. */
17854 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17855 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17856 GIVE_UP (22);
17858 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17859 only if buffer has really changed. The reason is that the gap is
17860 initially at Z for freshly visited files. The code below would
17861 set end_unchanged to 0 in that case. */
17862 if (MODIFF > SAVE_MODIFF
17863 /* This seems to happen sometimes after saving a buffer. */
17864 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17866 if (GPT - BEG < BEG_UNCHANGED)
17867 BEG_UNCHANGED = GPT - BEG;
17868 if (Z - GPT < END_UNCHANGED)
17869 END_UNCHANGED = Z - GPT;
17872 /* The position of the first and last character that has been changed. */
17873 first_changed_charpos = BEG + BEG_UNCHANGED;
17874 last_changed_charpos = Z - END_UNCHANGED;
17876 /* If window starts after a line end, and the last change is in
17877 front of that newline, then changes don't affect the display.
17878 This case happens with stealth-fontification. Note that although
17879 the display is unchanged, glyph positions in the matrix have to
17880 be adjusted, of course. */
17881 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17882 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17883 && ((last_changed_charpos < CHARPOS (start)
17884 && CHARPOS (start) == BEGV)
17885 || (last_changed_charpos < CHARPOS (start) - 1
17886 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17888 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17889 struct glyph_row *r0;
17891 /* Compute how many chars/bytes have been added to or removed
17892 from the buffer. */
17893 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17894 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17895 Z_delta = Z - Z_old;
17896 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17898 /* Give up if PT is not in the window. Note that it already has
17899 been checked at the start of try_window_id that PT is not in
17900 front of the window start. */
17901 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17902 GIVE_UP (13);
17904 /* If window start is unchanged, we can reuse the whole matrix
17905 as is, after adjusting glyph positions. No need to compute
17906 the window end again, since its offset from Z hasn't changed. */
17907 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17908 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17909 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17910 /* PT must not be in a partially visible line. */
17911 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17912 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17914 /* Adjust positions in the glyph matrix. */
17915 if (Z_delta || Z_delta_bytes)
17917 struct glyph_row *r1
17918 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17919 increment_matrix_positions (w->current_matrix,
17920 MATRIX_ROW_VPOS (r0, current_matrix),
17921 MATRIX_ROW_VPOS (r1, current_matrix),
17922 Z_delta, Z_delta_bytes);
17925 /* Set the cursor. */
17926 row = row_containing_pos (w, PT, r0, NULL, 0);
17927 if (row)
17928 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17929 return 1;
17933 /* Handle the case that changes are all below what is displayed in
17934 the window, and that PT is in the window. This shortcut cannot
17935 be taken if ZV is visible in the window, and text has been added
17936 there that is visible in the window. */
17937 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17938 /* ZV is not visible in the window, or there are no
17939 changes at ZV, actually. */
17940 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17941 || first_changed_charpos == last_changed_charpos))
17943 struct glyph_row *r0;
17945 /* Give up if PT is not in the window. Note that it already has
17946 been checked at the start of try_window_id that PT is not in
17947 front of the window start. */
17948 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17949 GIVE_UP (14);
17951 /* If window start is unchanged, we can reuse the whole matrix
17952 as is, without changing glyph positions since no text has
17953 been added/removed in front of the window end. */
17954 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17955 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17956 /* PT must not be in a partially visible line. */
17957 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17958 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17960 /* We have to compute the window end anew since text
17961 could have been added/removed after it. */
17962 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
17963 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17965 /* Set the cursor. */
17966 row = row_containing_pos (w, PT, r0, NULL, 0);
17967 if (row)
17968 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17969 return 2;
17973 /* Give up if window start is in the changed area.
17975 The condition used to read
17977 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17979 but why that was tested escapes me at the moment. */
17980 if (CHARPOS (start) >= first_changed_charpos
17981 && CHARPOS (start) <= last_changed_charpos)
17982 GIVE_UP (15);
17984 /* Check that window start agrees with the start of the first glyph
17985 row in its current matrix. Check this after we know the window
17986 start is not in changed text, otherwise positions would not be
17987 comparable. */
17988 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17989 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17990 GIVE_UP (16);
17992 /* Give up if the window ends in strings. Overlay strings
17993 at the end are difficult to handle, so don't try. */
17994 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
17995 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17996 GIVE_UP (20);
17998 /* Compute the position at which we have to start displaying new
17999 lines. Some of the lines at the top of the window might be
18000 reusable because they are not displaying changed text. Find the
18001 last row in W's current matrix not affected by changes at the
18002 start of current_buffer. Value is null if changes start in the
18003 first line of window. */
18004 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18005 if (last_unchanged_at_beg_row)
18007 /* Avoid starting to display in the middle of a character, a TAB
18008 for instance. This is easier than to set up the iterator
18009 exactly, and it's not a frequent case, so the additional
18010 effort wouldn't really pay off. */
18011 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18012 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18013 && last_unchanged_at_beg_row > w->current_matrix->rows)
18014 --last_unchanged_at_beg_row;
18016 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18017 GIVE_UP (17);
18019 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18020 GIVE_UP (18);
18021 start_pos = it.current.pos;
18023 /* Start displaying new lines in the desired matrix at the same
18024 vpos we would use in the current matrix, i.e. below
18025 last_unchanged_at_beg_row. */
18026 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18027 current_matrix);
18028 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18029 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18031 eassert (it.hpos == 0 && it.current_x == 0);
18033 else
18035 /* There are no reusable lines at the start of the window.
18036 Start displaying in the first text line. */
18037 start_display (&it, w, start);
18038 it.vpos = it.first_vpos;
18039 start_pos = it.current.pos;
18042 /* Find the first row that is not affected by changes at the end of
18043 the buffer. Value will be null if there is no unchanged row, in
18044 which case we must redisplay to the end of the window. delta
18045 will be set to the value by which buffer positions beginning with
18046 first_unchanged_at_end_row have to be adjusted due to text
18047 changes. */
18048 first_unchanged_at_end_row
18049 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18050 IF_DEBUG (debug_delta = delta);
18051 IF_DEBUG (debug_delta_bytes = delta_bytes);
18053 /* Set stop_pos to the buffer position up to which we will have to
18054 display new lines. If first_unchanged_at_end_row != NULL, this
18055 is the buffer position of the start of the line displayed in that
18056 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18057 that we don't stop at a buffer position. */
18058 stop_pos = 0;
18059 if (first_unchanged_at_end_row)
18061 eassert (last_unchanged_at_beg_row == NULL
18062 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18064 /* If this is a continuation line, move forward to the next one
18065 that isn't. Changes in lines above affect this line.
18066 Caution: this may move first_unchanged_at_end_row to a row
18067 not displaying text. */
18068 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18069 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18070 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18071 < it.last_visible_y))
18072 ++first_unchanged_at_end_row;
18074 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18075 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18076 >= it.last_visible_y))
18077 first_unchanged_at_end_row = NULL;
18078 else
18080 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18081 + delta);
18082 first_unchanged_at_end_vpos
18083 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18084 eassert (stop_pos >= Z - END_UNCHANGED);
18087 else if (last_unchanged_at_beg_row == NULL)
18088 GIVE_UP (19);
18091 #ifdef GLYPH_DEBUG
18093 /* Either there is no unchanged row at the end, or the one we have
18094 now displays text. This is a necessary condition for the window
18095 end pos calculation at the end of this function. */
18096 eassert (first_unchanged_at_end_row == NULL
18097 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18099 debug_last_unchanged_at_beg_vpos
18100 = (last_unchanged_at_beg_row
18101 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18102 : -1);
18103 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18105 #endif /* GLYPH_DEBUG */
18108 /* Display new lines. Set last_text_row to the last new line
18109 displayed which has text on it, i.e. might end up as being the
18110 line where the window_end_vpos is. */
18111 w->cursor.vpos = -1;
18112 last_text_row = NULL;
18113 overlay_arrow_seen = 0;
18114 while (it.current_y < it.last_visible_y
18115 && !f->fonts_changed
18116 && (first_unchanged_at_end_row == NULL
18117 || IT_CHARPOS (it) < stop_pos))
18119 if (display_line (&it))
18120 last_text_row = it.glyph_row - 1;
18123 if (f->fonts_changed)
18124 return -1;
18127 /* Compute differences in buffer positions, y-positions etc. for
18128 lines reused at the bottom of the window. Compute what we can
18129 scroll. */
18130 if (first_unchanged_at_end_row
18131 /* No lines reused because we displayed everything up to the
18132 bottom of the window. */
18133 && it.current_y < it.last_visible_y)
18135 dvpos = (it.vpos
18136 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18137 current_matrix));
18138 dy = it.current_y - first_unchanged_at_end_row->y;
18139 run.current_y = first_unchanged_at_end_row->y;
18140 run.desired_y = run.current_y + dy;
18141 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18143 else
18145 delta = delta_bytes = dvpos = dy
18146 = run.current_y = run.desired_y = run.height = 0;
18147 first_unchanged_at_end_row = NULL;
18149 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18152 /* Find the cursor if not already found. We have to decide whether
18153 PT will appear on this window (it sometimes doesn't, but this is
18154 not a very frequent case.) This decision has to be made before
18155 the current matrix is altered. A value of cursor.vpos < 0 means
18156 that PT is either in one of the lines beginning at
18157 first_unchanged_at_end_row or below the window. Don't care for
18158 lines that might be displayed later at the window end; as
18159 mentioned, this is not a frequent case. */
18160 if (w->cursor.vpos < 0)
18162 /* Cursor in unchanged rows at the top? */
18163 if (PT < CHARPOS (start_pos)
18164 && last_unchanged_at_beg_row)
18166 row = row_containing_pos (w, PT,
18167 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18168 last_unchanged_at_beg_row + 1, 0);
18169 if (row)
18170 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18173 /* Start from first_unchanged_at_end_row looking for PT. */
18174 else if (first_unchanged_at_end_row)
18176 row = row_containing_pos (w, PT - delta,
18177 first_unchanged_at_end_row, NULL, 0);
18178 if (row)
18179 set_cursor_from_row (w, row, w->current_matrix, delta,
18180 delta_bytes, dy, dvpos);
18183 /* Give up if cursor was not found. */
18184 if (w->cursor.vpos < 0)
18186 clear_glyph_matrix (w->desired_matrix);
18187 return -1;
18191 /* Don't let the cursor end in the scroll margins. */
18193 int this_scroll_margin, cursor_height;
18194 int frame_line_height = default_line_pixel_height (w);
18195 int window_total_lines
18196 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18198 this_scroll_margin =
18199 max (0, min (scroll_margin, window_total_lines / 4));
18200 this_scroll_margin *= frame_line_height;
18201 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18203 if ((w->cursor.y < this_scroll_margin
18204 && CHARPOS (start) > BEGV)
18205 /* Old redisplay didn't take scroll margin into account at the bottom,
18206 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18207 || (w->cursor.y + (make_cursor_line_fully_visible_p
18208 ? cursor_height + this_scroll_margin
18209 : 1)) > it.last_visible_y)
18211 w->cursor.vpos = -1;
18212 clear_glyph_matrix (w->desired_matrix);
18213 return -1;
18217 /* Scroll the display. Do it before changing the current matrix so
18218 that xterm.c doesn't get confused about where the cursor glyph is
18219 found. */
18220 if (dy && run.height)
18222 update_begin (f);
18224 if (FRAME_WINDOW_P (f))
18226 FRAME_RIF (f)->update_window_begin_hook (w);
18227 FRAME_RIF (f)->clear_window_mouse_face (w);
18228 FRAME_RIF (f)->scroll_run_hook (w, &run);
18229 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18231 else
18233 /* Terminal frame. In this case, dvpos gives the number of
18234 lines to scroll by; dvpos < 0 means scroll up. */
18235 int from_vpos
18236 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18237 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18238 int end = (WINDOW_TOP_EDGE_LINE (w)
18239 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18240 + window_internal_height (w));
18242 #if defined (HAVE_GPM) || defined (MSDOS)
18243 x_clear_window_mouse_face (w);
18244 #endif
18245 /* Perform the operation on the screen. */
18246 if (dvpos > 0)
18248 /* Scroll last_unchanged_at_beg_row to the end of the
18249 window down dvpos lines. */
18250 set_terminal_window (f, end);
18252 /* On dumb terminals delete dvpos lines at the end
18253 before inserting dvpos empty lines. */
18254 if (!FRAME_SCROLL_REGION_OK (f))
18255 ins_del_lines (f, end - dvpos, -dvpos);
18257 /* Insert dvpos empty lines in front of
18258 last_unchanged_at_beg_row. */
18259 ins_del_lines (f, from, dvpos);
18261 else if (dvpos < 0)
18263 /* Scroll up last_unchanged_at_beg_vpos to the end of
18264 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18265 set_terminal_window (f, end);
18267 /* Delete dvpos lines in front of
18268 last_unchanged_at_beg_vpos. ins_del_lines will set
18269 the cursor to the given vpos and emit |dvpos| delete
18270 line sequences. */
18271 ins_del_lines (f, from + dvpos, dvpos);
18273 /* On a dumb terminal insert dvpos empty lines at the
18274 end. */
18275 if (!FRAME_SCROLL_REGION_OK (f))
18276 ins_del_lines (f, end + dvpos, -dvpos);
18279 set_terminal_window (f, 0);
18282 update_end (f);
18285 /* Shift reused rows of the current matrix to the right position.
18286 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18287 text. */
18288 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18289 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18290 if (dvpos < 0)
18292 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18293 bottom_vpos, dvpos);
18294 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18295 bottom_vpos);
18297 else if (dvpos > 0)
18299 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18300 bottom_vpos, dvpos);
18301 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18302 first_unchanged_at_end_vpos + dvpos);
18305 /* For frame-based redisplay, make sure that current frame and window
18306 matrix are in sync with respect to glyph memory. */
18307 if (!FRAME_WINDOW_P (f))
18308 sync_frame_with_window_matrix_rows (w);
18310 /* Adjust buffer positions in reused rows. */
18311 if (delta || delta_bytes)
18312 increment_matrix_positions (current_matrix,
18313 first_unchanged_at_end_vpos + dvpos,
18314 bottom_vpos, delta, delta_bytes);
18316 /* Adjust Y positions. */
18317 if (dy)
18318 shift_glyph_matrix (w, current_matrix,
18319 first_unchanged_at_end_vpos + dvpos,
18320 bottom_vpos, dy);
18322 if (first_unchanged_at_end_row)
18324 first_unchanged_at_end_row += dvpos;
18325 if (first_unchanged_at_end_row->y >= it.last_visible_y
18326 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18327 first_unchanged_at_end_row = NULL;
18330 /* If scrolling up, there may be some lines to display at the end of
18331 the window. */
18332 last_text_row_at_end = NULL;
18333 if (dy < 0)
18335 /* Scrolling up can leave for example a partially visible line
18336 at the end of the window to be redisplayed. */
18337 /* Set last_row to the glyph row in the current matrix where the
18338 window end line is found. It has been moved up or down in
18339 the matrix by dvpos. */
18340 int last_vpos = w->window_end_vpos + dvpos;
18341 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18343 /* If last_row is the window end line, it should display text. */
18344 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18346 /* If window end line was partially visible before, begin
18347 displaying at that line. Otherwise begin displaying with the
18348 line following it. */
18349 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18351 init_to_row_start (&it, w, last_row);
18352 it.vpos = last_vpos;
18353 it.current_y = last_row->y;
18355 else
18357 init_to_row_end (&it, w, last_row);
18358 it.vpos = 1 + last_vpos;
18359 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18360 ++last_row;
18363 /* We may start in a continuation line. If so, we have to
18364 get the right continuation_lines_width and current_x. */
18365 it.continuation_lines_width = last_row->continuation_lines_width;
18366 it.hpos = it.current_x = 0;
18368 /* Display the rest of the lines at the window end. */
18369 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18370 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18372 /* Is it always sure that the display agrees with lines in
18373 the current matrix? I don't think so, so we mark rows
18374 displayed invalid in the current matrix by setting their
18375 enabled_p flag to zero. */
18376 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18377 if (display_line (&it))
18378 last_text_row_at_end = it.glyph_row - 1;
18382 /* Update window_end_pos and window_end_vpos. */
18383 if (first_unchanged_at_end_row && !last_text_row_at_end)
18385 /* Window end line if one of the preserved rows from the current
18386 matrix. Set row to the last row displaying text in current
18387 matrix starting at first_unchanged_at_end_row, after
18388 scrolling. */
18389 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18390 row = find_last_row_displaying_text (w->current_matrix, &it,
18391 first_unchanged_at_end_row);
18392 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18393 adjust_window_ends (w, row, 1);
18394 eassert (w->window_end_bytepos >= 0);
18395 IF_DEBUG (debug_method_add (w, "A"));
18397 else if (last_text_row_at_end)
18399 adjust_window_ends (w, last_text_row_at_end, 0);
18400 eassert (w->window_end_bytepos >= 0);
18401 IF_DEBUG (debug_method_add (w, "B"));
18403 else if (last_text_row)
18405 /* We have displayed either to the end of the window or at the
18406 end of the window, i.e. the last row with text is to be found
18407 in the desired matrix. */
18408 adjust_window_ends (w, last_text_row, 0);
18409 eassert (w->window_end_bytepos >= 0);
18411 else if (first_unchanged_at_end_row == NULL
18412 && last_text_row == NULL
18413 && last_text_row_at_end == NULL)
18415 /* Displayed to end of window, but no line containing text was
18416 displayed. Lines were deleted at the end of the window. */
18417 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18418 int vpos = w->window_end_vpos;
18419 struct glyph_row *current_row = current_matrix->rows + vpos;
18420 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18422 for (row = NULL;
18423 row == NULL && vpos >= first_vpos;
18424 --vpos, --current_row, --desired_row)
18426 if (desired_row->enabled_p)
18428 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18429 row = desired_row;
18431 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18432 row = current_row;
18435 eassert (row != NULL);
18436 w->window_end_vpos = vpos + 1;
18437 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18438 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18439 eassert (w->window_end_bytepos >= 0);
18440 IF_DEBUG (debug_method_add (w, "C"));
18442 else
18443 emacs_abort ();
18445 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18446 debug_end_vpos = w->window_end_vpos));
18448 /* Record that display has not been completed. */
18449 w->window_end_valid = 0;
18450 w->desired_matrix->no_scrolling_p = 1;
18451 return 3;
18453 #undef GIVE_UP
18458 /***********************************************************************
18459 More debugging support
18460 ***********************************************************************/
18462 #ifdef GLYPH_DEBUG
18464 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18465 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18466 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18469 /* Dump the contents of glyph matrix MATRIX on stderr.
18471 GLYPHS 0 means don't show glyph contents.
18472 GLYPHS 1 means show glyphs in short form
18473 GLYPHS > 1 means show glyphs in long form. */
18475 void
18476 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18478 int i;
18479 for (i = 0; i < matrix->nrows; ++i)
18480 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18484 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18485 the glyph row and area where the glyph comes from. */
18487 void
18488 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18490 if (glyph->type == CHAR_GLYPH
18491 || glyph->type == GLYPHLESS_GLYPH)
18493 fprintf (stderr,
18494 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18495 glyph - row->glyphs[TEXT_AREA],
18496 (glyph->type == CHAR_GLYPH
18497 ? 'C'
18498 : 'G'),
18499 glyph->charpos,
18500 (BUFFERP (glyph->object)
18501 ? 'B'
18502 : (STRINGP (glyph->object)
18503 ? 'S'
18504 : (INTEGERP (glyph->object)
18505 ? '0'
18506 : '-'))),
18507 glyph->pixel_width,
18508 glyph->u.ch,
18509 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18510 ? glyph->u.ch
18511 : '.'),
18512 glyph->face_id,
18513 glyph->left_box_line_p,
18514 glyph->right_box_line_p);
18516 else if (glyph->type == STRETCH_GLYPH)
18518 fprintf (stderr,
18519 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18520 glyph - row->glyphs[TEXT_AREA],
18521 'S',
18522 glyph->charpos,
18523 (BUFFERP (glyph->object)
18524 ? 'B'
18525 : (STRINGP (glyph->object)
18526 ? 'S'
18527 : (INTEGERP (glyph->object)
18528 ? '0'
18529 : '-'))),
18530 glyph->pixel_width,
18532 ' ',
18533 glyph->face_id,
18534 glyph->left_box_line_p,
18535 glyph->right_box_line_p);
18537 else if (glyph->type == IMAGE_GLYPH)
18539 fprintf (stderr,
18540 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18541 glyph - row->glyphs[TEXT_AREA],
18542 'I',
18543 glyph->charpos,
18544 (BUFFERP (glyph->object)
18545 ? 'B'
18546 : (STRINGP (glyph->object)
18547 ? 'S'
18548 : (INTEGERP (glyph->object)
18549 ? '0'
18550 : '-'))),
18551 glyph->pixel_width,
18552 glyph->u.img_id,
18553 '.',
18554 glyph->face_id,
18555 glyph->left_box_line_p,
18556 glyph->right_box_line_p);
18558 else if (glyph->type == COMPOSITE_GLYPH)
18560 fprintf (stderr,
18561 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18562 glyph - row->glyphs[TEXT_AREA],
18563 '+',
18564 glyph->charpos,
18565 (BUFFERP (glyph->object)
18566 ? 'B'
18567 : (STRINGP (glyph->object)
18568 ? 'S'
18569 : (INTEGERP (glyph->object)
18570 ? '0'
18571 : '-'))),
18572 glyph->pixel_width,
18573 glyph->u.cmp.id);
18574 if (glyph->u.cmp.automatic)
18575 fprintf (stderr,
18576 "[%d-%d]",
18577 glyph->slice.cmp.from, glyph->slice.cmp.to);
18578 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18579 glyph->face_id,
18580 glyph->left_box_line_p,
18581 glyph->right_box_line_p);
18586 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18587 GLYPHS 0 means don't show glyph contents.
18588 GLYPHS 1 means show glyphs in short form
18589 GLYPHS > 1 means show glyphs in long form. */
18591 void
18592 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18594 if (glyphs != 1)
18596 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18597 fprintf (stderr, "==============================================================================\n");
18599 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18600 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18601 vpos,
18602 MATRIX_ROW_START_CHARPOS (row),
18603 MATRIX_ROW_END_CHARPOS (row),
18604 row->used[TEXT_AREA],
18605 row->contains_overlapping_glyphs_p,
18606 row->enabled_p,
18607 row->truncated_on_left_p,
18608 row->truncated_on_right_p,
18609 row->continued_p,
18610 MATRIX_ROW_CONTINUATION_LINE_P (row),
18611 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18612 row->ends_at_zv_p,
18613 row->fill_line_p,
18614 row->ends_in_middle_of_char_p,
18615 row->starts_in_middle_of_char_p,
18616 row->mouse_face_p,
18617 row->x,
18618 row->y,
18619 row->pixel_width,
18620 row->height,
18621 row->visible_height,
18622 row->ascent,
18623 row->phys_ascent);
18624 /* The next 3 lines should align to "Start" in the header. */
18625 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18626 row->end.overlay_string_index,
18627 row->continuation_lines_width);
18628 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18629 CHARPOS (row->start.string_pos),
18630 CHARPOS (row->end.string_pos));
18631 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18632 row->end.dpvec_index);
18635 if (glyphs > 1)
18637 int area;
18639 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18641 struct glyph *glyph = row->glyphs[area];
18642 struct glyph *glyph_end = glyph + row->used[area];
18644 /* Glyph for a line end in text. */
18645 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18646 ++glyph_end;
18648 if (glyph < glyph_end)
18649 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18651 for (; glyph < glyph_end; ++glyph)
18652 dump_glyph (row, glyph, area);
18655 else if (glyphs == 1)
18657 int area;
18659 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18661 char *s = alloca (row->used[area] + 4);
18662 int i;
18664 for (i = 0; i < row->used[area]; ++i)
18666 struct glyph *glyph = row->glyphs[area] + i;
18667 if (i == row->used[area] - 1
18668 && area == TEXT_AREA
18669 && INTEGERP (glyph->object)
18670 && glyph->type == CHAR_GLYPH
18671 && glyph->u.ch == ' ')
18673 strcpy (&s[i], "[\\n]");
18674 i += 4;
18676 else if (glyph->type == CHAR_GLYPH
18677 && glyph->u.ch < 0x80
18678 && glyph->u.ch >= ' ')
18679 s[i] = glyph->u.ch;
18680 else
18681 s[i] = '.';
18684 s[i] = '\0';
18685 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18691 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18692 Sdump_glyph_matrix, 0, 1, "p",
18693 doc: /* Dump the current matrix of the selected window to stderr.
18694 Shows contents of glyph row structures. With non-nil
18695 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18696 glyphs in short form, otherwise show glyphs in long form. */)
18697 (Lisp_Object glyphs)
18699 struct window *w = XWINDOW (selected_window);
18700 struct buffer *buffer = XBUFFER (w->contents);
18702 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18703 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18704 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18705 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18706 fprintf (stderr, "=============================================\n");
18707 dump_glyph_matrix (w->current_matrix,
18708 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18709 return Qnil;
18713 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18714 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18715 (void)
18717 struct frame *f = XFRAME (selected_frame);
18718 dump_glyph_matrix (f->current_matrix, 1);
18719 return Qnil;
18723 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18724 doc: /* Dump glyph row ROW to stderr.
18725 GLYPH 0 means don't dump glyphs.
18726 GLYPH 1 means dump glyphs in short form.
18727 GLYPH > 1 or omitted means dump glyphs in long form. */)
18728 (Lisp_Object row, Lisp_Object glyphs)
18730 struct glyph_matrix *matrix;
18731 EMACS_INT vpos;
18733 CHECK_NUMBER (row);
18734 matrix = XWINDOW (selected_window)->current_matrix;
18735 vpos = XINT (row);
18736 if (vpos >= 0 && vpos < matrix->nrows)
18737 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18738 vpos,
18739 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18740 return Qnil;
18744 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18745 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18746 GLYPH 0 means don't dump glyphs.
18747 GLYPH 1 means dump glyphs in short form.
18748 GLYPH > 1 or omitted means dump glyphs in long form.
18750 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18751 do nothing. */)
18752 (Lisp_Object row, Lisp_Object glyphs)
18754 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18755 struct frame *sf = SELECTED_FRAME ();
18756 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18757 EMACS_INT vpos;
18759 CHECK_NUMBER (row);
18760 vpos = XINT (row);
18761 if (vpos >= 0 && vpos < m->nrows)
18762 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18763 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18764 #endif
18765 return Qnil;
18769 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18770 doc: /* Toggle tracing of redisplay.
18771 With ARG, turn tracing on if and only if ARG is positive. */)
18772 (Lisp_Object arg)
18774 if (NILP (arg))
18775 trace_redisplay_p = !trace_redisplay_p;
18776 else
18778 arg = Fprefix_numeric_value (arg);
18779 trace_redisplay_p = XINT (arg) > 0;
18782 return Qnil;
18786 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18787 doc: /* Like `format', but print result to stderr.
18788 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18789 (ptrdiff_t nargs, Lisp_Object *args)
18791 Lisp_Object s = Fformat (nargs, args);
18792 fprintf (stderr, "%s", SDATA (s));
18793 return Qnil;
18796 #endif /* GLYPH_DEBUG */
18800 /***********************************************************************
18801 Building Desired Matrix Rows
18802 ***********************************************************************/
18804 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18805 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18807 static struct glyph_row *
18808 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18810 struct frame *f = XFRAME (WINDOW_FRAME (w));
18811 struct buffer *buffer = XBUFFER (w->contents);
18812 struct buffer *old = current_buffer;
18813 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18814 int arrow_len = SCHARS (overlay_arrow_string);
18815 const unsigned char *arrow_end = arrow_string + arrow_len;
18816 const unsigned char *p;
18817 struct it it;
18818 bool multibyte_p;
18819 int n_glyphs_before;
18821 set_buffer_temp (buffer);
18822 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18823 it.glyph_row->used[TEXT_AREA] = 0;
18824 SET_TEXT_POS (it.position, 0, 0);
18826 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18827 p = arrow_string;
18828 while (p < arrow_end)
18830 Lisp_Object face, ilisp;
18832 /* Get the next character. */
18833 if (multibyte_p)
18834 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18835 else
18837 it.c = it.char_to_display = *p, it.len = 1;
18838 if (! ASCII_CHAR_P (it.c))
18839 it.char_to_display = BYTE8_TO_CHAR (it.c);
18841 p += it.len;
18843 /* Get its face. */
18844 ilisp = make_number (p - arrow_string);
18845 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18846 it.face_id = compute_char_face (f, it.char_to_display, face);
18848 /* Compute its width, get its glyphs. */
18849 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18850 SET_TEXT_POS (it.position, -1, -1);
18851 PRODUCE_GLYPHS (&it);
18853 /* If this character doesn't fit any more in the line, we have
18854 to remove some glyphs. */
18855 if (it.current_x > it.last_visible_x)
18857 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18858 break;
18862 set_buffer_temp (old);
18863 return it.glyph_row;
18867 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18868 glyphs to insert is determined by produce_special_glyphs. */
18870 static void
18871 insert_left_trunc_glyphs (struct it *it)
18873 struct it truncate_it;
18874 struct glyph *from, *end, *to, *toend;
18876 eassert (!FRAME_WINDOW_P (it->f)
18877 || (!it->glyph_row->reversed_p
18878 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18879 || (it->glyph_row->reversed_p
18880 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18882 /* Get the truncation glyphs. */
18883 truncate_it = *it;
18884 truncate_it.current_x = 0;
18885 truncate_it.face_id = DEFAULT_FACE_ID;
18886 truncate_it.glyph_row = &scratch_glyph_row;
18887 truncate_it.area = TEXT_AREA;
18888 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18889 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18890 truncate_it.object = make_number (0);
18891 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18893 /* Overwrite glyphs from IT with truncation glyphs. */
18894 if (!it->glyph_row->reversed_p)
18896 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18898 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18899 end = from + tused;
18900 to = it->glyph_row->glyphs[TEXT_AREA];
18901 toend = to + it->glyph_row->used[TEXT_AREA];
18902 if (FRAME_WINDOW_P (it->f))
18904 /* On GUI frames, when variable-size fonts are displayed,
18905 the truncation glyphs may need more pixels than the row's
18906 glyphs they overwrite. We overwrite more glyphs to free
18907 enough screen real estate, and enlarge the stretch glyph
18908 on the right (see display_line), if there is one, to
18909 preserve the screen position of the truncation glyphs on
18910 the right. */
18911 int w = 0;
18912 struct glyph *g = to;
18913 short used;
18915 /* The first glyph could be partially visible, in which case
18916 it->glyph_row->x will be negative. But we want the left
18917 truncation glyphs to be aligned at the left margin of the
18918 window, so we override the x coordinate at which the row
18919 will begin. */
18920 it->glyph_row->x = 0;
18921 while (g < toend && w < it->truncation_pixel_width)
18923 w += g->pixel_width;
18924 ++g;
18926 if (g - to - tused > 0)
18928 memmove (to + tused, g, (toend - g) * sizeof(*g));
18929 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18931 used = it->glyph_row->used[TEXT_AREA];
18932 if (it->glyph_row->truncated_on_right_p
18933 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18934 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18935 == STRETCH_GLYPH)
18937 int extra = w - it->truncation_pixel_width;
18939 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18943 while (from < end)
18944 *to++ = *from++;
18946 /* There may be padding glyphs left over. Overwrite them too. */
18947 if (!FRAME_WINDOW_P (it->f))
18949 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18951 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18952 while (from < end)
18953 *to++ = *from++;
18957 if (to > toend)
18958 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18960 else
18962 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18964 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18965 that back to front. */
18966 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18967 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18968 toend = it->glyph_row->glyphs[TEXT_AREA];
18969 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18970 if (FRAME_WINDOW_P (it->f))
18972 int w = 0;
18973 struct glyph *g = to;
18975 while (g >= toend && w < it->truncation_pixel_width)
18977 w += g->pixel_width;
18978 --g;
18980 if (to - g - tused > 0)
18981 to = g + tused;
18982 if (it->glyph_row->truncated_on_right_p
18983 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18984 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18986 int extra = w - it->truncation_pixel_width;
18988 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18992 while (from >= end && to >= toend)
18993 *to-- = *from--;
18994 if (!FRAME_WINDOW_P (it->f))
18996 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18998 from =
18999 truncate_it.glyph_row->glyphs[TEXT_AREA]
19000 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19001 while (from >= end && to >= toend)
19002 *to-- = *from--;
19005 if (from >= end)
19007 /* Need to free some room before prepending additional
19008 glyphs. */
19009 int move_by = from - end + 1;
19010 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19011 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19013 for ( ; g >= g0; g--)
19014 g[move_by] = *g;
19015 while (from >= end)
19016 *to-- = *from--;
19017 it->glyph_row->used[TEXT_AREA] += move_by;
19022 /* Compute the hash code for ROW. */
19023 unsigned
19024 row_hash (struct glyph_row *row)
19026 int area, k;
19027 unsigned hashval = 0;
19029 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19030 for (k = 0; k < row->used[area]; ++k)
19031 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19032 + row->glyphs[area][k].u.val
19033 + row->glyphs[area][k].face_id
19034 + row->glyphs[area][k].padding_p
19035 + (row->glyphs[area][k].type << 2));
19037 return hashval;
19040 /* Compute the pixel height and width of IT->glyph_row.
19042 Most of the time, ascent and height of a display line will be equal
19043 to the max_ascent and max_height values of the display iterator
19044 structure. This is not the case if
19046 1. We hit ZV without displaying anything. In this case, max_ascent
19047 and max_height will be zero.
19049 2. We have some glyphs that don't contribute to the line height.
19050 (The glyph row flag contributes_to_line_height_p is for future
19051 pixmap extensions).
19053 The first case is easily covered by using default values because in
19054 these cases, the line height does not really matter, except that it
19055 must not be zero. */
19057 static void
19058 compute_line_metrics (struct it *it)
19060 struct glyph_row *row = it->glyph_row;
19062 if (FRAME_WINDOW_P (it->f))
19064 int i, min_y, max_y;
19066 /* The line may consist of one space only, that was added to
19067 place the cursor on it. If so, the row's height hasn't been
19068 computed yet. */
19069 if (row->height == 0)
19071 if (it->max_ascent + it->max_descent == 0)
19072 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19073 row->ascent = it->max_ascent;
19074 row->height = it->max_ascent + it->max_descent;
19075 row->phys_ascent = it->max_phys_ascent;
19076 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19077 row->extra_line_spacing = it->max_extra_line_spacing;
19080 /* Compute the width of this line. */
19081 row->pixel_width = row->x;
19082 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19083 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19085 eassert (row->pixel_width >= 0);
19086 eassert (row->ascent >= 0 && row->height > 0);
19088 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19089 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19091 /* If first line's physical ascent is larger than its logical
19092 ascent, use the physical ascent, and make the row taller.
19093 This makes accented characters fully visible. */
19094 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19095 && row->phys_ascent > row->ascent)
19097 row->height += row->phys_ascent - row->ascent;
19098 row->ascent = row->phys_ascent;
19101 /* Compute how much of the line is visible. */
19102 row->visible_height = row->height;
19104 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19105 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19107 if (row->y < min_y)
19108 row->visible_height -= min_y - row->y;
19109 if (row->y + row->height > max_y)
19110 row->visible_height -= row->y + row->height - max_y;
19112 else
19114 row->pixel_width = row->used[TEXT_AREA];
19115 if (row->continued_p)
19116 row->pixel_width -= it->continuation_pixel_width;
19117 else if (row->truncated_on_right_p)
19118 row->pixel_width -= it->truncation_pixel_width;
19119 row->ascent = row->phys_ascent = 0;
19120 row->height = row->phys_height = row->visible_height = 1;
19121 row->extra_line_spacing = 0;
19124 /* Compute a hash code for this row. */
19125 row->hash = row_hash (row);
19127 it->max_ascent = it->max_descent = 0;
19128 it->max_phys_ascent = it->max_phys_descent = 0;
19132 /* Append one space to the glyph row of iterator IT if doing a
19133 window-based redisplay. The space has the same face as
19134 IT->face_id. Value is non-zero if a space was added.
19136 This function is called to make sure that there is always one glyph
19137 at the end of a glyph row that the cursor can be set on under
19138 window-systems. (If there weren't such a glyph we would not know
19139 how wide and tall a box cursor should be displayed).
19141 At the same time this space let's a nicely handle clearing to the
19142 end of the line if the row ends in italic text. */
19144 static int
19145 append_space_for_newline (struct it *it, int default_face_p)
19147 if (FRAME_WINDOW_P (it->f))
19149 int n = it->glyph_row->used[TEXT_AREA];
19151 if (it->glyph_row->glyphs[TEXT_AREA] + n
19152 < it->glyph_row->glyphs[1 + TEXT_AREA])
19154 /* Save some values that must not be changed.
19155 Must save IT->c and IT->len because otherwise
19156 ITERATOR_AT_END_P wouldn't work anymore after
19157 append_space_for_newline has been called. */
19158 enum display_element_type saved_what = it->what;
19159 int saved_c = it->c, saved_len = it->len;
19160 int saved_char_to_display = it->char_to_display;
19161 int saved_x = it->current_x;
19162 int saved_face_id = it->face_id;
19163 int saved_box_end = it->end_of_box_run_p;
19164 struct text_pos saved_pos;
19165 Lisp_Object saved_object;
19166 struct face *face;
19168 saved_object = it->object;
19169 saved_pos = it->position;
19171 it->what = IT_CHARACTER;
19172 memset (&it->position, 0, sizeof it->position);
19173 it->object = make_number (0);
19174 it->c = it->char_to_display = ' ';
19175 it->len = 1;
19177 /* If the default face was remapped, be sure to use the
19178 remapped face for the appended newline. */
19179 if (default_face_p)
19180 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19181 else if (it->face_before_selective_p)
19182 it->face_id = it->saved_face_id;
19183 face = FACE_FROM_ID (it->f, it->face_id);
19184 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19185 /* In R2L rows, we will prepend a stretch glyph that will
19186 have the end_of_box_run_p flag set for it, so there's no
19187 need for the appended newline glyph to have that flag
19188 set. */
19189 if (it->glyph_row->reversed_p
19190 /* But if the appended newline glyph goes all the way to
19191 the end of the row, there will be no stretch glyph,
19192 so leave the box flag set. */
19193 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19194 it->end_of_box_run_p = 0;
19196 PRODUCE_GLYPHS (it);
19198 it->override_ascent = -1;
19199 it->constrain_row_ascent_descent_p = 0;
19200 it->current_x = saved_x;
19201 it->object = saved_object;
19202 it->position = saved_pos;
19203 it->what = saved_what;
19204 it->face_id = saved_face_id;
19205 it->len = saved_len;
19206 it->c = saved_c;
19207 it->char_to_display = saved_char_to_display;
19208 it->end_of_box_run_p = saved_box_end;
19209 return 1;
19213 return 0;
19217 /* Extend the face of the last glyph in the text area of IT->glyph_row
19218 to the end of the display line. Called from display_line. If the
19219 glyph row is empty, add a space glyph to it so that we know the
19220 face to draw. Set the glyph row flag fill_line_p. If the glyph
19221 row is R2L, prepend a stretch glyph to cover the empty space to the
19222 left of the leftmost glyph. */
19224 static void
19225 extend_face_to_end_of_line (struct it *it)
19227 struct face *face, *default_face;
19228 struct frame *f = it->f;
19230 /* If line is already filled, do nothing. Non window-system frames
19231 get a grace of one more ``pixel'' because their characters are
19232 1-``pixel'' wide, so they hit the equality too early. This grace
19233 is needed only for R2L rows that are not continued, to produce
19234 one extra blank where we could display the cursor. */
19235 if ((it->current_x >= it->last_visible_x
19236 + (!FRAME_WINDOW_P (f)
19237 && it->glyph_row->reversed_p
19238 && !it->glyph_row->continued_p))
19239 /* If the window has display margins, we will need to extend
19240 their face even if the text area is filled. */
19241 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19242 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19243 return;
19245 /* The default face, possibly remapped. */
19246 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19248 /* Face extension extends the background and box of IT->face_id
19249 to the end of the line. If the background equals the background
19250 of the frame, we don't have to do anything. */
19251 if (it->face_before_selective_p)
19252 face = FACE_FROM_ID (f, it->saved_face_id);
19253 else
19254 face = FACE_FROM_ID (f, it->face_id);
19256 if (FRAME_WINDOW_P (f)
19257 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19258 && face->box == FACE_NO_BOX
19259 && face->background == FRAME_BACKGROUND_PIXEL (f)
19260 #ifdef HAVE_WINDOW_SYSTEM
19261 && !face->stipple
19262 #endif
19263 && !it->glyph_row->reversed_p)
19264 return;
19266 /* Set the glyph row flag indicating that the face of the last glyph
19267 in the text area has to be drawn to the end of the text area. */
19268 it->glyph_row->fill_line_p = 1;
19270 /* If current character of IT is not ASCII, make sure we have the
19271 ASCII face. This will be automatically undone the next time
19272 get_next_display_element returns a multibyte character. Note
19273 that the character will always be single byte in unibyte
19274 text. */
19275 if (!ASCII_CHAR_P (it->c))
19277 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19280 if (FRAME_WINDOW_P (f))
19282 /* If the row is empty, add a space with the current face of IT,
19283 so that we know which face to draw. */
19284 if (it->glyph_row->used[TEXT_AREA] == 0)
19286 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19287 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19288 it->glyph_row->used[TEXT_AREA] = 1;
19290 /* Mode line and the header line don't have margins, and
19291 likewise the frame's tool-bar window, if there is any. */
19292 if (!(it->glyph_row->mode_line_p
19293 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19294 || (WINDOWP (f->tool_bar_window)
19295 && it->w == XWINDOW (f->tool_bar_window))
19296 #endif
19299 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19300 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19302 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19303 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19304 default_face->id;
19305 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19307 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19308 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19310 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19311 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19312 default_face->id;
19313 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19316 #ifdef HAVE_WINDOW_SYSTEM
19317 if (it->glyph_row->reversed_p)
19319 /* Prepend a stretch glyph to the row, such that the
19320 rightmost glyph will be drawn flushed all the way to the
19321 right margin of the window. The stretch glyph that will
19322 occupy the empty space, if any, to the left of the
19323 glyphs. */
19324 struct font *font = face->font ? face->font : FRAME_FONT (f);
19325 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19326 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19327 struct glyph *g;
19328 int row_width, stretch_ascent, stretch_width;
19329 struct text_pos saved_pos;
19330 int saved_face_id, saved_avoid_cursor, saved_box_start;
19332 for (row_width = 0, g = row_start; g < row_end; g++)
19333 row_width += g->pixel_width;
19334 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
19335 if (stretch_width > 0)
19337 stretch_ascent =
19338 (((it->ascent + it->descent)
19339 * FONT_BASE (font)) / FONT_HEIGHT (font));
19340 saved_pos = it->position;
19341 memset (&it->position, 0, sizeof it->position);
19342 saved_avoid_cursor = it->avoid_cursor_p;
19343 it->avoid_cursor_p = 1;
19344 saved_face_id = it->face_id;
19345 saved_box_start = it->start_of_box_run_p;
19346 /* The last row's stretch glyph should get the default
19347 face, to avoid painting the rest of the window with
19348 the region face, if the region ends at ZV. */
19349 if (it->glyph_row->ends_at_zv_p)
19350 it->face_id = default_face->id;
19351 else
19352 it->face_id = face->id;
19353 it->start_of_box_run_p = 0;
19354 append_stretch_glyph (it, make_number (0), stretch_width,
19355 it->ascent + it->descent, stretch_ascent);
19356 it->position = saved_pos;
19357 it->avoid_cursor_p = saved_avoid_cursor;
19358 it->face_id = saved_face_id;
19359 it->start_of_box_run_p = saved_box_start;
19361 /* If stretch_width comes out negative, it means that the
19362 last glyph is only partially visible. In R2L rows, we
19363 want the leftmost glyph to be partially visible, so we
19364 need to give the row the corresponding left offset. */
19365 if (stretch_width < 0)
19366 it->glyph_row->x = stretch_width;
19368 #endif /* HAVE_WINDOW_SYSTEM */
19370 else
19372 /* Save some values that must not be changed. */
19373 int saved_x = it->current_x;
19374 struct text_pos saved_pos;
19375 Lisp_Object saved_object;
19376 enum display_element_type saved_what = it->what;
19377 int saved_face_id = it->face_id;
19379 saved_object = it->object;
19380 saved_pos = it->position;
19382 it->what = IT_CHARACTER;
19383 memset (&it->position, 0, sizeof it->position);
19384 it->object = make_number (0);
19385 it->c = it->char_to_display = ' ';
19386 it->len = 1;
19388 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19389 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19390 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19391 && !it->glyph_row->mode_line_p
19392 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19394 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19395 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19397 for (it->current_x = 0; g < e; g++)
19398 it->current_x += g->pixel_width;
19400 it->area = LEFT_MARGIN_AREA;
19401 it->face_id = default_face->id;
19402 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19403 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19405 PRODUCE_GLYPHS (it);
19406 /* term.c:produce_glyphs advances it->current_x only for
19407 TEXT_AREA. */
19408 it->current_x += it->pixel_width;
19411 it->current_x = saved_x;
19412 it->area = TEXT_AREA;
19415 /* The last row's blank glyphs should get the default face, to
19416 avoid painting the rest of the window with the region face,
19417 if the region ends at ZV. */
19418 if (it->glyph_row->ends_at_zv_p)
19419 it->face_id = default_face->id;
19420 else
19421 it->face_id = face->id;
19422 PRODUCE_GLYPHS (it);
19424 while (it->current_x <= it->last_visible_x)
19425 PRODUCE_GLYPHS (it);
19427 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19428 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19429 < WINDOW_RIGHT_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[RIGHT_MARGIN_AREA];
19434 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19436 for ( ; g < e; g++)
19437 it->current_x += g->pixel_width;
19439 it->area = RIGHT_MARGIN_AREA;
19440 it->face_id = default_face->id;
19441 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19442 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19444 PRODUCE_GLYPHS (it);
19445 it->current_x += it->pixel_width;
19448 it->area = TEXT_AREA;
19451 /* Don't count these blanks really. It would let us insert a left
19452 truncation glyph below and make us set the cursor on them, maybe. */
19453 it->current_x = saved_x;
19454 it->object = saved_object;
19455 it->position = saved_pos;
19456 it->what = saved_what;
19457 it->face_id = saved_face_id;
19462 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19463 trailing whitespace. */
19465 static int
19466 trailing_whitespace_p (ptrdiff_t charpos)
19468 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19469 int c = 0;
19471 while (bytepos < ZV_BYTE
19472 && (c = FETCH_CHAR (bytepos),
19473 c == ' ' || c == '\t'))
19474 ++bytepos;
19476 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19478 if (bytepos != PT_BYTE)
19479 return 1;
19481 return 0;
19485 /* Highlight trailing whitespace, if any, in ROW. */
19487 static void
19488 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19490 int used = row->used[TEXT_AREA];
19492 if (used)
19494 struct glyph *start = row->glyphs[TEXT_AREA];
19495 struct glyph *glyph = start + used - 1;
19497 if (row->reversed_p)
19499 /* Right-to-left rows need to be processed in the opposite
19500 direction, so swap the edge pointers. */
19501 glyph = start;
19502 start = row->glyphs[TEXT_AREA] + used - 1;
19505 /* Skip over glyphs inserted to display the cursor at the
19506 end of a line, for extending the face of the last glyph
19507 to the end of the line on terminals, and for truncation
19508 and continuation glyphs. */
19509 if (!row->reversed_p)
19511 while (glyph >= start
19512 && glyph->type == CHAR_GLYPH
19513 && INTEGERP (glyph->object))
19514 --glyph;
19516 else
19518 while (glyph <= start
19519 && glyph->type == CHAR_GLYPH
19520 && INTEGERP (glyph->object))
19521 ++glyph;
19524 /* If last glyph is a space or stretch, and it's trailing
19525 whitespace, set the face of all trailing whitespace glyphs in
19526 IT->glyph_row to `trailing-whitespace'. */
19527 if ((row->reversed_p ? glyph <= start : glyph >= start)
19528 && BUFFERP (glyph->object)
19529 && (glyph->type == STRETCH_GLYPH
19530 || (glyph->type == CHAR_GLYPH
19531 && glyph->u.ch == ' '))
19532 && trailing_whitespace_p (glyph->charpos))
19534 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19535 if (face_id < 0)
19536 return;
19538 if (!row->reversed_p)
19540 while (glyph >= start
19541 && BUFFERP (glyph->object)
19542 && (glyph->type == STRETCH_GLYPH
19543 || (glyph->type == CHAR_GLYPH
19544 && glyph->u.ch == ' ')))
19545 (glyph--)->face_id = face_id;
19547 else
19549 while (glyph <= start
19550 && BUFFERP (glyph->object)
19551 && (glyph->type == STRETCH_GLYPH
19552 || (glyph->type == CHAR_GLYPH
19553 && glyph->u.ch == ' ')))
19554 (glyph++)->face_id = face_id;
19561 /* Value is non-zero if glyph row ROW should be
19562 considered to hold the buffer position CHARPOS. */
19564 static int
19565 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19567 int result = 1;
19569 if (charpos == CHARPOS (row->end.pos)
19570 || charpos == MATRIX_ROW_END_CHARPOS (row))
19572 /* Suppose the row ends on a string.
19573 Unless the row is continued, that means it ends on a newline
19574 in the string. If it's anything other than a display string
19575 (e.g., a before-string from an overlay), we don't want the
19576 cursor there. (This heuristic seems to give the optimal
19577 behavior for the various types of multi-line strings.)
19578 One exception: if the string has `cursor' property on one of
19579 its characters, we _do_ want the cursor there. */
19580 if (CHARPOS (row->end.string_pos) >= 0)
19582 if (row->continued_p)
19583 result = 1;
19584 else
19586 /* Check for `display' property. */
19587 struct glyph *beg = row->glyphs[TEXT_AREA];
19588 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19589 struct glyph *glyph;
19591 result = 0;
19592 for (glyph = end; glyph >= beg; --glyph)
19593 if (STRINGP (glyph->object))
19595 Lisp_Object prop
19596 = Fget_char_property (make_number (charpos),
19597 Qdisplay, Qnil);
19598 result =
19599 (!NILP (prop)
19600 && display_prop_string_p (prop, glyph->object));
19601 /* If there's a `cursor' property on one of the
19602 string's characters, this row is a cursor row,
19603 even though this is not a display string. */
19604 if (!result)
19606 Lisp_Object s = glyph->object;
19608 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19610 ptrdiff_t gpos = glyph->charpos;
19612 if (!NILP (Fget_char_property (make_number (gpos),
19613 Qcursor, s)))
19615 result = 1;
19616 break;
19620 break;
19624 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19626 /* If the row ends in middle of a real character,
19627 and the line is continued, we want the cursor here.
19628 That's because CHARPOS (ROW->end.pos) would equal
19629 PT if PT is before the character. */
19630 if (!row->ends_in_ellipsis_p)
19631 result = row->continued_p;
19632 else
19633 /* If the row ends in an ellipsis, then
19634 CHARPOS (ROW->end.pos) will equal point after the
19635 invisible text. We want that position to be displayed
19636 after the ellipsis. */
19637 result = 0;
19639 /* If the row ends at ZV, display the cursor at the end of that
19640 row instead of at the start of the row below. */
19641 else if (row->ends_at_zv_p)
19642 result = 1;
19643 else
19644 result = 0;
19647 return result;
19650 /* Value is non-zero if glyph row ROW should be
19651 used to hold the cursor. */
19653 static int
19654 cursor_row_p (struct glyph_row *row)
19656 return row_for_charpos_p (row, PT);
19661 /* Push the property PROP so that it will be rendered at the current
19662 position in IT. Return 1 if PROP was successfully pushed, 0
19663 otherwise. Called from handle_line_prefix to handle the
19664 `line-prefix' and `wrap-prefix' properties. */
19666 static int
19667 push_prefix_prop (struct it *it, Lisp_Object prop)
19669 struct text_pos pos =
19670 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19672 eassert (it->method == GET_FROM_BUFFER
19673 || it->method == GET_FROM_DISPLAY_VECTOR
19674 || it->method == GET_FROM_STRING);
19676 /* We need to save the current buffer/string position, so it will be
19677 restored by pop_it, because iterate_out_of_display_property
19678 depends on that being set correctly, but some situations leave
19679 it->position not yet set when this function is called. */
19680 push_it (it, &pos);
19682 if (STRINGP (prop))
19684 if (SCHARS (prop) == 0)
19686 pop_it (it);
19687 return 0;
19690 it->string = prop;
19691 it->string_from_prefix_prop_p = 1;
19692 it->multibyte_p = STRING_MULTIBYTE (it->string);
19693 it->current.overlay_string_index = -1;
19694 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19695 it->end_charpos = it->string_nchars = SCHARS (it->string);
19696 it->method = GET_FROM_STRING;
19697 it->stop_charpos = 0;
19698 it->prev_stop = 0;
19699 it->base_level_stop = 0;
19701 /* Force paragraph direction to be that of the parent
19702 buffer/string. */
19703 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19704 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19705 else
19706 it->paragraph_embedding = L2R;
19708 /* Set up the bidi iterator for this display string. */
19709 if (it->bidi_p)
19711 it->bidi_it.string.lstring = it->string;
19712 it->bidi_it.string.s = NULL;
19713 it->bidi_it.string.schars = it->end_charpos;
19714 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19715 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19716 it->bidi_it.string.unibyte = !it->multibyte_p;
19717 it->bidi_it.w = it->w;
19718 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19721 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19723 it->method = GET_FROM_STRETCH;
19724 it->object = prop;
19726 #ifdef HAVE_WINDOW_SYSTEM
19727 else if (IMAGEP (prop))
19729 it->what = IT_IMAGE;
19730 it->image_id = lookup_image (it->f, prop);
19731 it->method = GET_FROM_IMAGE;
19733 #endif /* HAVE_WINDOW_SYSTEM */
19734 else
19736 pop_it (it); /* bogus display property, give up */
19737 return 0;
19740 return 1;
19743 /* Return the character-property PROP at the current position in IT. */
19745 static Lisp_Object
19746 get_it_property (struct it *it, Lisp_Object prop)
19748 Lisp_Object position, object = it->object;
19750 if (STRINGP (object))
19751 position = make_number (IT_STRING_CHARPOS (*it));
19752 else if (BUFFERP (object))
19754 position = make_number (IT_CHARPOS (*it));
19755 object = it->window;
19757 else
19758 return Qnil;
19760 return Fget_char_property (position, prop, object);
19763 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19765 static void
19766 handle_line_prefix (struct it *it)
19768 Lisp_Object prefix;
19770 if (it->continuation_lines_width > 0)
19772 prefix = get_it_property (it, Qwrap_prefix);
19773 if (NILP (prefix))
19774 prefix = Vwrap_prefix;
19776 else
19778 prefix = get_it_property (it, Qline_prefix);
19779 if (NILP (prefix))
19780 prefix = Vline_prefix;
19782 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19784 /* If the prefix is wider than the window, and we try to wrap
19785 it, it would acquire its own wrap prefix, and so on till the
19786 iterator stack overflows. So, don't wrap the prefix. */
19787 it->line_wrap = TRUNCATE;
19788 it->avoid_cursor_p = 1;
19794 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19795 only for R2L lines from display_line and display_string, when they
19796 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19797 the line/string needs to be continued on the next glyph row. */
19798 static void
19799 unproduce_glyphs (struct it *it, int n)
19801 struct glyph *glyph, *end;
19803 eassert (it->glyph_row);
19804 eassert (it->glyph_row->reversed_p);
19805 eassert (it->area == TEXT_AREA);
19806 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19808 if (n > it->glyph_row->used[TEXT_AREA])
19809 n = it->glyph_row->used[TEXT_AREA];
19810 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19811 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19812 for ( ; glyph < end; glyph++)
19813 glyph[-n] = *glyph;
19816 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19817 and ROW->maxpos. */
19818 static void
19819 find_row_edges (struct it *it, struct glyph_row *row,
19820 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19821 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19823 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19824 lines' rows is implemented for bidi-reordered rows. */
19826 /* ROW->minpos is the value of min_pos, the minimal buffer position
19827 we have in ROW, or ROW->start.pos if that is smaller. */
19828 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19829 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19830 else
19831 /* We didn't find buffer positions smaller than ROW->start, or
19832 didn't find _any_ valid buffer positions in any of the glyphs,
19833 so we must trust the iterator's computed positions. */
19834 row->minpos = row->start.pos;
19835 if (max_pos <= 0)
19837 max_pos = CHARPOS (it->current.pos);
19838 max_bpos = BYTEPOS (it->current.pos);
19841 /* Here are the various use-cases for ending the row, and the
19842 corresponding values for ROW->maxpos:
19844 Line ends in a newline from buffer eol_pos + 1
19845 Line is continued from buffer max_pos + 1
19846 Line is truncated on right it->current.pos
19847 Line ends in a newline from string max_pos + 1(*)
19848 (*) + 1 only when line ends in a forward scan
19849 Line is continued from string max_pos
19850 Line is continued from display vector max_pos
19851 Line is entirely from a string min_pos == max_pos
19852 Line is entirely from a display vector min_pos == max_pos
19853 Line that ends at ZV ZV
19855 If you discover other use-cases, please add them here as
19856 appropriate. */
19857 if (row->ends_at_zv_p)
19858 row->maxpos = it->current.pos;
19859 else if (row->used[TEXT_AREA])
19861 int seen_this_string = 0;
19862 struct glyph_row *r1 = row - 1;
19864 /* Did we see the same display string on the previous row? */
19865 if (STRINGP (it->object)
19866 /* this is not the first row */
19867 && row > it->w->desired_matrix->rows
19868 /* previous row is not the header line */
19869 && !r1->mode_line_p
19870 /* previous row also ends in a newline from a string */
19871 && r1->ends_in_newline_from_string_p)
19873 struct glyph *start, *end;
19875 /* Search for the last glyph of the previous row that came
19876 from buffer or string. Depending on whether the row is
19877 L2R or R2L, we need to process it front to back or the
19878 other way round. */
19879 if (!r1->reversed_p)
19881 start = r1->glyphs[TEXT_AREA];
19882 end = start + r1->used[TEXT_AREA];
19883 /* Glyphs inserted by redisplay have an integer (zero)
19884 as their object. */
19885 while (end > start
19886 && INTEGERP ((end - 1)->object)
19887 && (end - 1)->charpos <= 0)
19888 --end;
19889 if (end > start)
19891 if (EQ ((end - 1)->object, it->object))
19892 seen_this_string = 1;
19894 else
19895 /* If all the glyphs of the previous row were inserted
19896 by redisplay, it means the previous row was
19897 produced from a single newline, which is only
19898 possible if that newline came from the same string
19899 as the one which produced this ROW. */
19900 seen_this_string = 1;
19902 else
19904 end = r1->glyphs[TEXT_AREA] - 1;
19905 start = end + r1->used[TEXT_AREA];
19906 while (end < start
19907 && INTEGERP ((end + 1)->object)
19908 && (end + 1)->charpos <= 0)
19909 ++end;
19910 if (end < start)
19912 if (EQ ((end + 1)->object, it->object))
19913 seen_this_string = 1;
19915 else
19916 seen_this_string = 1;
19919 /* Take note of each display string that covers a newline only
19920 once, the first time we see it. This is for when a display
19921 string includes more than one newline in it. */
19922 if (row->ends_in_newline_from_string_p && !seen_this_string)
19924 /* If we were scanning the buffer forward when we displayed
19925 the string, we want to account for at least one buffer
19926 position that belongs to this row (position covered by
19927 the display string), so that cursor positioning will
19928 consider this row as a candidate when point is at the end
19929 of the visual line represented by this row. This is not
19930 required when scanning back, because max_pos will already
19931 have a much larger value. */
19932 if (CHARPOS (row->end.pos) > max_pos)
19933 INC_BOTH (max_pos, max_bpos);
19934 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19936 else if (CHARPOS (it->eol_pos) > 0)
19937 SET_TEXT_POS (row->maxpos,
19938 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19939 else if (row->continued_p)
19941 /* If max_pos is different from IT's current position, it
19942 means IT->method does not belong to the display element
19943 at max_pos. However, it also means that the display
19944 element at max_pos was displayed in its entirety on this
19945 line, which is equivalent to saying that the next line
19946 starts at the next buffer position. */
19947 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19948 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19949 else
19951 INC_BOTH (max_pos, max_bpos);
19952 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19955 else if (row->truncated_on_right_p)
19956 /* display_line already called reseat_at_next_visible_line_start,
19957 which puts the iterator at the beginning of the next line, in
19958 the logical order. */
19959 row->maxpos = it->current.pos;
19960 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19961 /* A line that is entirely from a string/image/stretch... */
19962 row->maxpos = row->minpos;
19963 else
19964 emacs_abort ();
19966 else
19967 row->maxpos = it->current.pos;
19970 /* Construct the glyph row IT->glyph_row in the desired matrix of
19971 IT->w from text at the current position of IT. See dispextern.h
19972 for an overview of struct it. Value is non-zero if
19973 IT->glyph_row displays text, as opposed to a line displaying ZV
19974 only. */
19976 static int
19977 display_line (struct it *it)
19979 struct glyph_row *row = it->glyph_row;
19980 Lisp_Object overlay_arrow_string;
19981 struct it wrap_it;
19982 void *wrap_data = NULL;
19983 int may_wrap = 0, wrap_x IF_LINT (= 0);
19984 int wrap_row_used = -1;
19985 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19986 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19987 int wrap_row_extra_line_spacing IF_LINT (= 0);
19988 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19989 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19990 int cvpos;
19991 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19992 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19993 bool pending_handle_line_prefix = false;
19995 /* We always start displaying at hpos zero even if hscrolled. */
19996 eassert (it->hpos == 0 && it->current_x == 0);
19998 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19999 >= it->w->desired_matrix->nrows)
20001 it->w->nrows_scale_factor++;
20002 it->f->fonts_changed = 1;
20003 return 0;
20006 /* Clear the result glyph row and enable it. */
20007 prepare_desired_row (it->w, row, false);
20009 row->y = it->current_y;
20010 row->start = it->start;
20011 row->continuation_lines_width = it->continuation_lines_width;
20012 row->displays_text_p = 1;
20013 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20014 it->starts_in_middle_of_char_p = 0;
20016 /* Arrange the overlays nicely for our purposes. Usually, we call
20017 display_line on only one line at a time, in which case this
20018 can't really hurt too much, or we call it on lines which appear
20019 one after another in the buffer, in which case all calls to
20020 recenter_overlay_lists but the first will be pretty cheap. */
20021 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20023 /* Move over display elements that are not visible because we are
20024 hscrolled. This may stop at an x-position < IT->first_visible_x
20025 if the first glyph is partially visible or if we hit a line end. */
20026 if (it->current_x < it->first_visible_x)
20028 enum move_it_result move_result;
20030 this_line_min_pos = row->start.pos;
20031 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20032 MOVE_TO_POS | MOVE_TO_X);
20033 /* If we are under a large hscroll, move_it_in_display_line_to
20034 could hit the end of the line without reaching
20035 it->first_visible_x. Pretend that we did reach it. This is
20036 especially important on a TTY, where we will call
20037 extend_face_to_end_of_line, which needs to know how many
20038 blank glyphs to produce. */
20039 if (it->current_x < it->first_visible_x
20040 && (move_result == MOVE_NEWLINE_OR_CR
20041 || move_result == MOVE_POS_MATCH_OR_ZV))
20042 it->current_x = it->first_visible_x;
20044 /* Record the smallest positions seen while we moved over
20045 display elements that are not visible. This is needed by
20046 redisplay_internal for optimizing the case where the cursor
20047 stays inside the same line. The rest of this function only
20048 considers positions that are actually displayed, so
20049 RECORD_MAX_MIN_POS will not otherwise record positions that
20050 are hscrolled to the left of the left edge of the window. */
20051 min_pos = CHARPOS (this_line_min_pos);
20052 min_bpos = BYTEPOS (this_line_min_pos);
20054 else if (it->area == TEXT_AREA)
20056 /* We only do this when not calling move_it_in_display_line_to
20057 above, because that function calls itself handle_line_prefix. */
20058 handle_line_prefix (it);
20060 else
20062 /* Line-prefix and wrap-prefix are always displayed in the text
20063 area. But if this is the first call to display_line after
20064 init_iterator, the iterator might have been set up to write
20065 into a marginal area, e.g. if the line begins with some
20066 display property that writes to the margins. So we need to
20067 wait with the call to handle_line_prefix until whatever
20068 writes to the margin has done its job. */
20069 pending_handle_line_prefix = true;
20072 /* Get the initial row height. This is either the height of the
20073 text hscrolled, if there is any, or zero. */
20074 row->ascent = it->max_ascent;
20075 row->height = it->max_ascent + it->max_descent;
20076 row->phys_ascent = it->max_phys_ascent;
20077 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20078 row->extra_line_spacing = it->max_extra_line_spacing;
20080 /* Utility macro to record max and min buffer positions seen until now. */
20081 #define RECORD_MAX_MIN_POS(IT) \
20082 do \
20084 int composition_p = !STRINGP ((IT)->string) \
20085 && ((IT)->what == IT_COMPOSITION); \
20086 ptrdiff_t current_pos = \
20087 composition_p ? (IT)->cmp_it.charpos \
20088 : IT_CHARPOS (*(IT)); \
20089 ptrdiff_t current_bpos = \
20090 composition_p ? CHAR_TO_BYTE (current_pos) \
20091 : IT_BYTEPOS (*(IT)); \
20092 if (current_pos < min_pos) \
20094 min_pos = current_pos; \
20095 min_bpos = current_bpos; \
20097 if (IT_CHARPOS (*it) > max_pos) \
20099 max_pos = IT_CHARPOS (*it); \
20100 max_bpos = IT_BYTEPOS (*it); \
20103 while (0)
20105 /* Loop generating characters. The loop is left with IT on the next
20106 character to display. */
20107 while (1)
20109 int n_glyphs_before, hpos_before, x_before;
20110 int x, nglyphs;
20111 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20113 /* Retrieve the next thing to display. Value is zero if end of
20114 buffer reached. */
20115 if (!get_next_display_element (it))
20117 /* Maybe add a space at the end of this line that is used to
20118 display the cursor there under X. Set the charpos of the
20119 first glyph of blank lines not corresponding to any text
20120 to -1. */
20121 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20122 row->exact_window_width_line_p = 1;
20123 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20124 || row->used[TEXT_AREA] == 0)
20126 row->glyphs[TEXT_AREA]->charpos = -1;
20127 row->displays_text_p = 0;
20129 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20130 && (!MINI_WINDOW_P (it->w)
20131 || (minibuf_level && EQ (it->window, minibuf_window))))
20132 row->indicate_empty_line_p = 1;
20135 it->continuation_lines_width = 0;
20136 row->ends_at_zv_p = 1;
20137 /* A row that displays right-to-left text must always have
20138 its last face extended all the way to the end of line,
20139 even if this row ends in ZV, because we still write to
20140 the screen left to right. We also need to extend the
20141 last face if the default face is remapped to some
20142 different face, otherwise the functions that clear
20143 portions of the screen will clear with the default face's
20144 background color. */
20145 if (row->reversed_p
20146 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20147 extend_face_to_end_of_line (it);
20148 break;
20151 /* Now, get the metrics of what we want to display. This also
20152 generates glyphs in `row' (which is IT->glyph_row). */
20153 n_glyphs_before = row->used[TEXT_AREA];
20154 x = it->current_x;
20156 /* Remember the line height so far in case the next element doesn't
20157 fit on the line. */
20158 if (it->line_wrap != TRUNCATE)
20160 ascent = it->max_ascent;
20161 descent = it->max_descent;
20162 phys_ascent = it->max_phys_ascent;
20163 phys_descent = it->max_phys_descent;
20165 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20167 if (IT_DISPLAYING_WHITESPACE (it))
20168 may_wrap = 1;
20169 else if (may_wrap)
20171 SAVE_IT (wrap_it, *it, wrap_data);
20172 wrap_x = x;
20173 wrap_row_used = row->used[TEXT_AREA];
20174 wrap_row_ascent = row->ascent;
20175 wrap_row_height = row->height;
20176 wrap_row_phys_ascent = row->phys_ascent;
20177 wrap_row_phys_height = row->phys_height;
20178 wrap_row_extra_line_spacing = row->extra_line_spacing;
20179 wrap_row_min_pos = min_pos;
20180 wrap_row_min_bpos = min_bpos;
20181 wrap_row_max_pos = max_pos;
20182 wrap_row_max_bpos = max_bpos;
20183 may_wrap = 0;
20188 PRODUCE_GLYPHS (it);
20190 /* If this display element was in marginal areas, continue with
20191 the next one. */
20192 if (it->area != TEXT_AREA)
20194 row->ascent = max (row->ascent, it->max_ascent);
20195 row->height = max (row->height, it->max_ascent + it->max_descent);
20196 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20197 row->phys_height = max (row->phys_height,
20198 it->max_phys_ascent + it->max_phys_descent);
20199 row->extra_line_spacing = max (row->extra_line_spacing,
20200 it->max_extra_line_spacing);
20201 set_iterator_to_next (it, 1);
20202 /* If we didn't handle the line/wrap prefix above, and the
20203 call to set_iterator_to_next just switched to TEXT_AREA,
20204 process the prefix now. */
20205 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20207 pending_handle_line_prefix = false;
20208 handle_line_prefix (it);
20210 continue;
20213 /* Does the display element fit on the line? If we truncate
20214 lines, we should draw past the right edge of the window. If
20215 we don't truncate, we want to stop so that we can display the
20216 continuation glyph before the right margin. If lines are
20217 continued, there are two possible strategies for characters
20218 resulting in more than 1 glyph (e.g. tabs): Display as many
20219 glyphs as possible in this line and leave the rest for the
20220 continuation line, or display the whole element in the next
20221 line. Original redisplay did the former, so we do it also. */
20222 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20223 hpos_before = it->hpos;
20224 x_before = x;
20226 if (/* Not a newline. */
20227 nglyphs > 0
20228 /* Glyphs produced fit entirely in the line. */
20229 && it->current_x < it->last_visible_x)
20231 it->hpos += nglyphs;
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 if (it->current_x - it->pixel_width < it->first_visible_x
20240 /* In R2L rows, we arrange in extend_face_to_end_of_line
20241 to add a right offset to the line, by a suitable
20242 change to the stretch glyph that is the leftmost
20243 glyph of the line. */
20244 && !row->reversed_p)
20245 row->x = x - it->first_visible_x;
20246 /* Record the maximum and minimum buffer positions seen so
20247 far in glyphs that will be displayed by this row. */
20248 if (it->bidi_p)
20249 RECORD_MAX_MIN_POS (it);
20251 else
20253 int i, new_x;
20254 struct glyph *glyph;
20256 for (i = 0; i < nglyphs; ++i, x = new_x)
20258 /* Identify the glyphs added by the last call to
20259 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20260 the previous glyphs. */
20261 if (!row->reversed_p)
20262 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20263 else
20264 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20265 new_x = x + glyph->pixel_width;
20267 if (/* Lines are continued. */
20268 it->line_wrap != TRUNCATE
20269 && (/* Glyph doesn't fit on the line. */
20270 new_x > it->last_visible_x
20271 /* Or it fits exactly on a window system frame. */
20272 || (new_x == it->last_visible_x
20273 && FRAME_WINDOW_P (it->f)
20274 && (row->reversed_p
20275 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20276 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20278 /* End of a continued line. */
20280 if (it->hpos == 0
20281 || (new_x == it->last_visible_x
20282 && FRAME_WINDOW_P (it->f)
20283 && (row->reversed_p
20284 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20285 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20287 /* Current glyph is the only one on the line or
20288 fits exactly on the line. We must continue
20289 the line because we can't draw the cursor
20290 after the glyph. */
20291 row->continued_p = 1;
20292 it->current_x = new_x;
20293 it->continuation_lines_width += new_x;
20294 ++it->hpos;
20295 if (i == nglyphs - 1)
20297 /* If line-wrap is on, check if a previous
20298 wrap point was found. */
20299 if (wrap_row_used > 0
20300 /* Even if there is a previous wrap
20301 point, continue the line here as
20302 usual, if (i) the previous character
20303 was a space or tab AND (ii) the
20304 current character is not. */
20305 && (!may_wrap
20306 || IT_DISPLAYING_WHITESPACE (it)))
20307 goto back_to_wrap;
20309 /* Record the maximum and minimum buffer
20310 positions seen so far in glyphs that will be
20311 displayed by this row. */
20312 if (it->bidi_p)
20313 RECORD_MAX_MIN_POS (it);
20314 set_iterator_to_next (it, 1);
20315 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20317 if (!get_next_display_element (it))
20319 row->exact_window_width_line_p = 1;
20320 it->continuation_lines_width = 0;
20321 row->continued_p = 0;
20322 row->ends_at_zv_p = 1;
20324 else if (ITERATOR_AT_END_OF_LINE_P (it))
20326 row->continued_p = 0;
20327 row->exact_window_width_line_p = 1;
20331 else if (it->bidi_p)
20332 RECORD_MAX_MIN_POS (it);
20333 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20334 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20335 extend_face_to_end_of_line (it);
20337 else if (CHAR_GLYPH_PADDING_P (*glyph)
20338 && !FRAME_WINDOW_P (it->f))
20340 /* A padding glyph that doesn't fit on this line.
20341 This means the whole character doesn't fit
20342 on the line. */
20343 if (row->reversed_p)
20344 unproduce_glyphs (it, row->used[TEXT_AREA]
20345 - n_glyphs_before);
20346 row->used[TEXT_AREA] = n_glyphs_before;
20348 /* Fill the rest of the row with continuation
20349 glyphs like in 20.x. */
20350 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20351 < row->glyphs[1 + TEXT_AREA])
20352 produce_special_glyphs (it, IT_CONTINUATION);
20354 row->continued_p = 1;
20355 it->current_x = x_before;
20356 it->continuation_lines_width += x_before;
20358 /* Restore the height to what it was before the
20359 element not fitting on the line. */
20360 it->max_ascent = ascent;
20361 it->max_descent = descent;
20362 it->max_phys_ascent = phys_ascent;
20363 it->max_phys_descent = phys_descent;
20364 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20365 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20366 extend_face_to_end_of_line (it);
20368 else if (wrap_row_used > 0)
20370 back_to_wrap:
20371 if (row->reversed_p)
20372 unproduce_glyphs (it,
20373 row->used[TEXT_AREA] - wrap_row_used);
20374 RESTORE_IT (it, &wrap_it, wrap_data);
20375 it->continuation_lines_width += wrap_x;
20376 row->used[TEXT_AREA] = wrap_row_used;
20377 row->ascent = wrap_row_ascent;
20378 row->height = wrap_row_height;
20379 row->phys_ascent = wrap_row_phys_ascent;
20380 row->phys_height = wrap_row_phys_height;
20381 row->extra_line_spacing = wrap_row_extra_line_spacing;
20382 min_pos = wrap_row_min_pos;
20383 min_bpos = wrap_row_min_bpos;
20384 max_pos = wrap_row_max_pos;
20385 max_bpos = wrap_row_max_bpos;
20386 row->continued_p = 1;
20387 row->ends_at_zv_p = 0;
20388 row->exact_window_width_line_p = 0;
20389 it->continuation_lines_width += x;
20391 /* Make sure that a non-default face is extended
20392 up to the right margin of the window. */
20393 extend_face_to_end_of_line (it);
20395 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20397 /* A TAB that extends past the right edge of the
20398 window. This produces a single glyph on
20399 window system frames. We leave the glyph in
20400 this row and let it fill the row, but don't
20401 consume the TAB. */
20402 if ((row->reversed_p
20403 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20404 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20405 produce_special_glyphs (it, IT_CONTINUATION);
20406 it->continuation_lines_width += it->last_visible_x;
20407 row->ends_in_middle_of_char_p = 1;
20408 row->continued_p = 1;
20409 glyph->pixel_width = it->last_visible_x - x;
20410 it->starts_in_middle_of_char_p = 1;
20411 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20412 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20413 extend_face_to_end_of_line (it);
20415 else
20417 /* Something other than a TAB that draws past
20418 the right edge of the window. Restore
20419 positions to values before the element. */
20420 if (row->reversed_p)
20421 unproduce_glyphs (it, row->used[TEXT_AREA]
20422 - (n_glyphs_before + i));
20423 row->used[TEXT_AREA] = n_glyphs_before + i;
20425 /* Display continuation glyphs. */
20426 it->current_x = x_before;
20427 it->continuation_lines_width += x;
20428 if (!FRAME_WINDOW_P (it->f)
20429 || (row->reversed_p
20430 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20431 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20432 produce_special_glyphs (it, IT_CONTINUATION);
20433 row->continued_p = 1;
20435 extend_face_to_end_of_line (it);
20437 if (nglyphs > 1 && i > 0)
20439 row->ends_in_middle_of_char_p = 1;
20440 it->starts_in_middle_of_char_p = 1;
20443 /* Restore the height to what it was before the
20444 element not fitting on the line. */
20445 it->max_ascent = ascent;
20446 it->max_descent = descent;
20447 it->max_phys_ascent = phys_ascent;
20448 it->max_phys_descent = phys_descent;
20451 break;
20453 else if (new_x > it->first_visible_x)
20455 /* Increment number of glyphs actually displayed. */
20456 ++it->hpos;
20458 /* Record the maximum and minimum buffer positions
20459 seen so far in glyphs that will be displayed by
20460 this row. */
20461 if (it->bidi_p)
20462 RECORD_MAX_MIN_POS (it);
20464 if (x < it->first_visible_x && !row->reversed_p)
20465 /* Glyph is partially visible, i.e. row starts at
20466 negative X position. Don't do that in R2L
20467 rows, where we arrange to add a right offset to
20468 the line in extend_face_to_end_of_line, by a
20469 suitable change to the stretch glyph that is
20470 the leftmost glyph of the line. */
20471 row->x = x - it->first_visible_x;
20472 /* When the last glyph of an R2L row only fits
20473 partially on the line, we need to set row->x to a
20474 negative offset, so that the leftmost glyph is
20475 the one that is partially visible. */
20476 if (row->reversed_p && new_x > it->last_visible_x)
20477 row->x = it->last_visible_x - new_x;
20479 else
20481 /* Glyph is completely off the left margin of the
20482 window. This should not happen because of the
20483 move_it_in_display_line at the start of this
20484 function, unless the text display area of the
20485 window is empty. */
20486 eassert (it->first_visible_x <= it->last_visible_x);
20489 /* Even if this display element produced no glyphs at all,
20490 we want to record its position. */
20491 if (it->bidi_p && nglyphs == 0)
20492 RECORD_MAX_MIN_POS (it);
20494 row->ascent = max (row->ascent, it->max_ascent);
20495 row->height = max (row->height, it->max_ascent + it->max_descent);
20496 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20497 row->phys_height = max (row->phys_height,
20498 it->max_phys_ascent + it->max_phys_descent);
20499 row->extra_line_spacing = max (row->extra_line_spacing,
20500 it->max_extra_line_spacing);
20502 /* End of this display line if row is continued. */
20503 if (row->continued_p || row->ends_at_zv_p)
20504 break;
20507 at_end_of_line:
20508 /* Is this a line end? If yes, we're also done, after making
20509 sure that a non-default face is extended up to the right
20510 margin of the window. */
20511 if (ITERATOR_AT_END_OF_LINE_P (it))
20513 int used_before = row->used[TEXT_AREA];
20515 row->ends_in_newline_from_string_p = STRINGP (it->object);
20517 /* Add a space at the end of the line that is used to
20518 display the cursor there. */
20519 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20520 append_space_for_newline (it, 0);
20522 /* Extend the face to the end of the line. */
20523 extend_face_to_end_of_line (it);
20525 /* Make sure we have the position. */
20526 if (used_before == 0)
20527 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20529 /* Record the position of the newline, for use in
20530 find_row_edges. */
20531 it->eol_pos = it->current.pos;
20533 /* Consume the line end. This skips over invisible lines. */
20534 set_iterator_to_next (it, 1);
20535 it->continuation_lines_width = 0;
20536 break;
20539 /* Proceed with next display element. Note that this skips
20540 over lines invisible because of selective display. */
20541 set_iterator_to_next (it, 1);
20543 /* If we truncate lines, we are done when the last displayed
20544 glyphs reach past the right margin of the window. */
20545 if (it->line_wrap == TRUNCATE
20546 && ((FRAME_WINDOW_P (it->f)
20547 /* Images are preprocessed in produce_image_glyph such
20548 that they are cropped at the right edge of the
20549 window, so an image glyph will always end exactly at
20550 last_visible_x, even if there's no right fringe. */
20551 && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
20552 ? (it->current_x >= it->last_visible_x)
20553 : (it->current_x > it->last_visible_x)))
20555 /* Maybe add truncation glyphs. */
20556 if (!FRAME_WINDOW_P (it->f)
20557 || (row->reversed_p
20558 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20559 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20561 int i, n;
20563 if (!row->reversed_p)
20565 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20566 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20567 break;
20569 else
20571 for (i = 0; i < row->used[TEXT_AREA]; i++)
20572 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20573 break;
20574 /* Remove any padding glyphs at the front of ROW, to
20575 make room for the truncation glyphs we will be
20576 adding below. The loop below always inserts at
20577 least one truncation glyph, so also remove the
20578 last glyph added to ROW. */
20579 unproduce_glyphs (it, i + 1);
20580 /* Adjust i for the loop below. */
20581 i = row->used[TEXT_AREA] - (i + 1);
20584 /* produce_special_glyphs overwrites the last glyph, so
20585 we don't want that if we want to keep that last
20586 glyph, which means it's an image. */
20587 if (it->current_x > it->last_visible_x)
20589 it->current_x = x_before;
20590 if (!FRAME_WINDOW_P (it->f))
20592 for (n = row->used[TEXT_AREA]; i < n; ++i)
20594 row->used[TEXT_AREA] = i;
20595 produce_special_glyphs (it, IT_TRUNCATION);
20598 else
20600 row->used[TEXT_AREA] = i;
20601 produce_special_glyphs (it, IT_TRUNCATION);
20603 it->hpos = hpos_before;
20606 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20608 /* Don't truncate if we can overflow newline into fringe. */
20609 if (!get_next_display_element (it))
20611 it->continuation_lines_width = 0;
20612 row->ends_at_zv_p = 1;
20613 row->exact_window_width_line_p = 1;
20614 break;
20616 if (ITERATOR_AT_END_OF_LINE_P (it))
20618 row->exact_window_width_line_p = 1;
20619 goto at_end_of_line;
20621 it->current_x = x_before;
20622 it->hpos = hpos_before;
20625 row->truncated_on_right_p = 1;
20626 it->continuation_lines_width = 0;
20627 reseat_at_next_visible_line_start (it, 0);
20628 /* We insist below that IT's position be at ZV because in
20629 bidi-reordered lines the character at visible line start
20630 might not be the character that follows the newline in
20631 the logical order. */
20632 if (IT_BYTEPOS (*it) > BEG_BYTE)
20633 row->ends_at_zv_p =
20634 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20635 else
20636 row->ends_at_zv_p = false;
20637 break;
20641 if (wrap_data)
20642 bidi_unshelve_cache (wrap_data, 1);
20644 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20645 at the left window margin. */
20646 if (it->first_visible_x
20647 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20649 if (!FRAME_WINDOW_P (it->f)
20650 || (((row->reversed_p
20651 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20652 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20653 /* Don't let insert_left_trunc_glyphs overwrite the
20654 first glyph of the row if it is an image. */
20655 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20656 insert_left_trunc_glyphs (it);
20657 row->truncated_on_left_p = 1;
20660 /* Remember the position at which this line ends.
20662 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20663 cannot be before the call to find_row_edges below, since that is
20664 where these positions are determined. */
20665 row->end = it->current;
20666 if (!it->bidi_p)
20668 row->minpos = row->start.pos;
20669 row->maxpos = row->end.pos;
20671 else
20673 /* ROW->minpos and ROW->maxpos must be the smallest and
20674 `1 + the largest' buffer positions in ROW. But if ROW was
20675 bidi-reordered, these two positions can be anywhere in the
20676 row, so we must determine them now. */
20677 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20680 /* If the start of this line is the overlay arrow-position, then
20681 mark this glyph row as the one containing the overlay arrow.
20682 This is clearly a mess with variable size fonts. It would be
20683 better to let it be displayed like cursors under X. */
20684 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20685 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20686 !NILP (overlay_arrow_string)))
20688 /* Overlay arrow in window redisplay is a fringe bitmap. */
20689 if (STRINGP (overlay_arrow_string))
20691 struct glyph_row *arrow_row
20692 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20693 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20694 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20695 struct glyph *p = row->glyphs[TEXT_AREA];
20696 struct glyph *p2, *end;
20698 /* Copy the arrow glyphs. */
20699 while (glyph < arrow_end)
20700 *p++ = *glyph++;
20702 /* Throw away padding glyphs. */
20703 p2 = p;
20704 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20705 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20706 ++p2;
20707 if (p2 > p)
20709 while (p2 < end)
20710 *p++ = *p2++;
20711 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20714 else
20716 eassert (INTEGERP (overlay_arrow_string));
20717 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20719 overlay_arrow_seen = 1;
20722 /* Highlight trailing whitespace. */
20723 if (!NILP (Vshow_trailing_whitespace))
20724 highlight_trailing_whitespace (it->f, it->glyph_row);
20726 /* Compute pixel dimensions of this line. */
20727 compute_line_metrics (it);
20729 /* Implementation note: No changes in the glyphs of ROW or in their
20730 faces can be done past this point, because compute_line_metrics
20731 computes ROW's hash value and stores it within the glyph_row
20732 structure. */
20734 /* Record whether this row ends inside an ellipsis. */
20735 row->ends_in_ellipsis_p
20736 = (it->method == GET_FROM_DISPLAY_VECTOR
20737 && it->ellipsis_p);
20739 /* Save fringe bitmaps in this row. */
20740 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20741 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20742 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20743 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20745 it->left_user_fringe_bitmap = 0;
20746 it->left_user_fringe_face_id = 0;
20747 it->right_user_fringe_bitmap = 0;
20748 it->right_user_fringe_face_id = 0;
20750 /* Maybe set the cursor. */
20751 cvpos = it->w->cursor.vpos;
20752 if ((cvpos < 0
20753 /* In bidi-reordered rows, keep checking for proper cursor
20754 position even if one has been found already, because buffer
20755 positions in such rows change non-linearly with ROW->VPOS,
20756 when a line is continued. One exception: when we are at ZV,
20757 display cursor on the first suitable glyph row, since all
20758 the empty rows after that also have their position set to ZV. */
20759 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20760 lines' rows is implemented for bidi-reordered rows. */
20761 || (it->bidi_p
20762 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20763 && PT >= MATRIX_ROW_START_CHARPOS (row)
20764 && PT <= MATRIX_ROW_END_CHARPOS (row)
20765 && cursor_row_p (row))
20766 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20768 /* Prepare for the next line. This line starts horizontally at (X
20769 HPOS) = (0 0). Vertical positions are incremented. As a
20770 convenience for the caller, IT->glyph_row is set to the next
20771 row to be used. */
20772 it->current_x = it->hpos = 0;
20773 it->current_y += row->height;
20774 SET_TEXT_POS (it->eol_pos, 0, 0);
20775 ++it->vpos;
20776 ++it->glyph_row;
20777 /* The next row should by default use the same value of the
20778 reversed_p flag as this one. set_iterator_to_next decides when
20779 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20780 the flag accordingly. */
20781 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20782 it->glyph_row->reversed_p = row->reversed_p;
20783 it->start = row->end;
20784 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20786 #undef RECORD_MAX_MIN_POS
20789 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20790 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20791 doc: /* Return paragraph direction at point in BUFFER.
20792 Value is either `left-to-right' or `right-to-left'.
20793 If BUFFER is omitted or nil, it defaults to the current buffer.
20795 Paragraph direction determines how the text in the paragraph is displayed.
20796 In left-to-right paragraphs, text begins at the left margin of the window
20797 and the reading direction is generally left to right. In right-to-left
20798 paragraphs, text begins at the right margin and is read from right to left.
20800 See also `bidi-paragraph-direction'. */)
20801 (Lisp_Object buffer)
20803 struct buffer *buf = current_buffer;
20804 struct buffer *old = buf;
20806 if (! NILP (buffer))
20808 CHECK_BUFFER (buffer);
20809 buf = XBUFFER (buffer);
20812 if (NILP (BVAR (buf, bidi_display_reordering))
20813 || NILP (BVAR (buf, enable_multibyte_characters))
20814 /* When we are loading loadup.el, the character property tables
20815 needed for bidi iteration are not yet available. */
20816 || !NILP (Vpurify_flag))
20817 return Qleft_to_right;
20818 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20819 return BVAR (buf, bidi_paragraph_direction);
20820 else
20822 /* Determine the direction from buffer text. We could try to
20823 use current_matrix if it is up to date, but this seems fast
20824 enough as it is. */
20825 struct bidi_it itb;
20826 ptrdiff_t pos = BUF_PT (buf);
20827 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20828 int c;
20829 void *itb_data = bidi_shelve_cache ();
20831 set_buffer_temp (buf);
20832 /* bidi_paragraph_init finds the base direction of the paragraph
20833 by searching forward from paragraph start. We need the base
20834 direction of the current or _previous_ paragraph, so we need
20835 to make sure we are within that paragraph. To that end, find
20836 the previous non-empty line. */
20837 if (pos >= ZV && pos > BEGV)
20838 DEC_BOTH (pos, bytepos);
20839 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20840 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20842 while ((c = FETCH_BYTE (bytepos)) == '\n'
20843 || c == ' ' || c == '\t' || c == '\f')
20845 if (bytepos <= BEGV_BYTE)
20846 break;
20847 bytepos--;
20848 pos--;
20850 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20851 bytepos--;
20853 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20854 itb.paragraph_dir = NEUTRAL_DIR;
20855 itb.string.s = NULL;
20856 itb.string.lstring = Qnil;
20857 itb.string.bufpos = 0;
20858 itb.string.from_disp_str = 0;
20859 itb.string.unibyte = 0;
20860 /* We have no window to use here for ignoring window-specific
20861 overlays. Using NULL for window pointer will cause
20862 compute_display_string_pos to use the current buffer. */
20863 itb.w = NULL;
20864 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20865 bidi_unshelve_cache (itb_data, 0);
20866 set_buffer_temp (old);
20867 switch (itb.paragraph_dir)
20869 case L2R:
20870 return Qleft_to_right;
20871 break;
20872 case R2L:
20873 return Qright_to_left;
20874 break;
20875 default:
20876 emacs_abort ();
20881 DEFUN ("move-point-visually", Fmove_point_visually,
20882 Smove_point_visually, 1, 1, 0,
20883 doc: /* Move point in the visual order in the specified DIRECTION.
20884 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20885 left.
20887 Value is the new character position of point. */)
20888 (Lisp_Object direction)
20890 struct window *w = XWINDOW (selected_window);
20891 struct buffer *b = XBUFFER (w->contents);
20892 struct glyph_row *row;
20893 int dir;
20894 Lisp_Object paragraph_dir;
20896 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20897 (!(ROW)->continued_p \
20898 && INTEGERP ((GLYPH)->object) \
20899 && (GLYPH)->type == CHAR_GLYPH \
20900 && (GLYPH)->u.ch == ' ' \
20901 && (GLYPH)->charpos >= 0 \
20902 && !(GLYPH)->avoid_cursor_p)
20904 CHECK_NUMBER (direction);
20905 dir = XINT (direction);
20906 if (dir > 0)
20907 dir = 1;
20908 else
20909 dir = -1;
20911 /* If current matrix is up-to-date, we can use the information
20912 recorded in the glyphs, at least as long as the goal is on the
20913 screen. */
20914 if (w->window_end_valid
20915 && !windows_or_buffers_changed
20916 && b
20917 && !b->clip_changed
20918 && !b->prevent_redisplay_optimizations_p
20919 && !window_outdated (w)
20920 /* We rely below on the cursor coordinates to be up to date, but
20921 we cannot trust them if some command moved point since the
20922 last complete redisplay. */
20923 && w->last_point == BUF_PT (b)
20924 && w->cursor.vpos >= 0
20925 && w->cursor.vpos < w->current_matrix->nrows
20926 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20928 struct glyph *g = row->glyphs[TEXT_AREA];
20929 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20930 struct glyph *gpt = g + w->cursor.hpos;
20932 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20934 if (BUFFERP (g->object) && g->charpos != PT)
20936 SET_PT (g->charpos);
20937 w->cursor.vpos = -1;
20938 return make_number (PT);
20940 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20942 ptrdiff_t new_pos;
20944 if (BUFFERP (gpt->object))
20946 new_pos = PT;
20947 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20948 new_pos += (row->reversed_p ? -dir : dir);
20949 else
20950 new_pos -= (row->reversed_p ? -dir : dir);;
20952 else if (BUFFERP (g->object))
20953 new_pos = g->charpos;
20954 else
20955 break;
20956 SET_PT (new_pos);
20957 w->cursor.vpos = -1;
20958 return make_number (PT);
20960 else if (ROW_GLYPH_NEWLINE_P (row, g))
20962 /* Glyphs inserted at the end of a non-empty line for
20963 positioning the cursor have zero charpos, so we must
20964 deduce the value of point by other means. */
20965 if (g->charpos > 0)
20966 SET_PT (g->charpos);
20967 else if (row->ends_at_zv_p && PT != ZV)
20968 SET_PT (ZV);
20969 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20970 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20971 else
20972 break;
20973 w->cursor.vpos = -1;
20974 return make_number (PT);
20977 if (g == e || INTEGERP (g->object))
20979 if (row->truncated_on_left_p || row->truncated_on_right_p)
20980 goto simulate_display;
20981 if (!row->reversed_p)
20982 row += dir;
20983 else
20984 row -= dir;
20985 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20986 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20987 goto simulate_display;
20989 if (dir > 0)
20991 if (row->reversed_p && !row->continued_p)
20993 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20994 w->cursor.vpos = -1;
20995 return make_number (PT);
20997 g = row->glyphs[TEXT_AREA];
20998 e = g + row->used[TEXT_AREA];
20999 for ( ; g < e; g++)
21001 if (BUFFERP (g->object)
21002 /* Empty lines have only one glyph, which stands
21003 for the newline, and whose charpos is the
21004 buffer position of the newline. */
21005 || ROW_GLYPH_NEWLINE_P (row, g)
21006 /* When the buffer ends in a newline, the line at
21007 EOB also has one glyph, but its charpos is -1. */
21008 || (row->ends_at_zv_p
21009 && !row->reversed_p
21010 && INTEGERP (g->object)
21011 && g->type == CHAR_GLYPH
21012 && g->u.ch == ' '))
21014 if (g->charpos > 0)
21015 SET_PT (g->charpos);
21016 else if (!row->reversed_p
21017 && row->ends_at_zv_p
21018 && PT != ZV)
21019 SET_PT (ZV);
21020 else
21021 continue;
21022 w->cursor.vpos = -1;
21023 return make_number (PT);
21027 else
21029 if (!row->reversed_p && !row->continued_p)
21031 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21032 w->cursor.vpos = -1;
21033 return make_number (PT);
21035 e = row->glyphs[TEXT_AREA];
21036 g = e + row->used[TEXT_AREA] - 1;
21037 for ( ; g >= e; g--)
21039 if (BUFFERP (g->object)
21040 || (ROW_GLYPH_NEWLINE_P (row, g)
21041 && g->charpos > 0)
21042 /* Empty R2L lines on GUI frames have the buffer
21043 position of the newline stored in the stretch
21044 glyph. */
21045 || g->type == STRETCH_GLYPH
21046 || (row->ends_at_zv_p
21047 && row->reversed_p
21048 && INTEGERP (g->object)
21049 && g->type == CHAR_GLYPH
21050 && g->u.ch == ' '))
21052 if (g->charpos > 0)
21053 SET_PT (g->charpos);
21054 else if (row->reversed_p
21055 && row->ends_at_zv_p
21056 && PT != ZV)
21057 SET_PT (ZV);
21058 else
21059 continue;
21060 w->cursor.vpos = -1;
21061 return make_number (PT);
21068 simulate_display:
21070 /* If we wind up here, we failed to move by using the glyphs, so we
21071 need to simulate display instead. */
21073 if (b)
21074 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21075 else
21076 paragraph_dir = Qleft_to_right;
21077 if (EQ (paragraph_dir, Qright_to_left))
21078 dir = -dir;
21079 if (PT <= BEGV && dir < 0)
21080 xsignal0 (Qbeginning_of_buffer);
21081 else if (PT >= ZV && dir > 0)
21082 xsignal0 (Qend_of_buffer);
21083 else
21085 struct text_pos pt;
21086 struct it it;
21087 int pt_x, target_x, pixel_width, pt_vpos;
21088 bool at_eol_p;
21089 bool overshoot_expected = false;
21090 bool target_is_eol_p = false;
21092 /* Setup the arena. */
21093 SET_TEXT_POS (pt, PT, PT_BYTE);
21094 start_display (&it, w, pt);
21096 if (it.cmp_it.id < 0
21097 && it.method == GET_FROM_STRING
21098 && it.area == TEXT_AREA
21099 && it.string_from_display_prop_p
21100 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21101 overshoot_expected = true;
21103 /* Find the X coordinate of point. We start from the beginning
21104 of this or previous line to make sure we are before point in
21105 the logical order (since the move_it_* functions can only
21106 move forward). */
21107 reseat:
21108 reseat_at_previous_visible_line_start (&it);
21109 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21110 if (IT_CHARPOS (it) != PT)
21112 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21113 -1, -1, -1, MOVE_TO_POS);
21114 /* If we missed point because the character there is
21115 displayed out of a display vector that has more than one
21116 glyph, retry expecting overshoot. */
21117 if (it.method == GET_FROM_DISPLAY_VECTOR
21118 && it.current.dpvec_index > 0
21119 && !overshoot_expected)
21121 overshoot_expected = true;
21122 goto reseat;
21124 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21125 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21127 pt_x = it.current_x;
21128 pt_vpos = it.vpos;
21129 if (dir > 0 || overshoot_expected)
21131 struct glyph_row *row = it.glyph_row;
21133 /* When point is at beginning of line, we don't have
21134 information about the glyph there loaded into struct
21135 it. Calling get_next_display_element fixes that. */
21136 if (pt_x == 0)
21137 get_next_display_element (&it);
21138 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21139 it.glyph_row = NULL;
21140 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21141 it.glyph_row = row;
21142 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21143 it, lest it will become out of sync with it's buffer
21144 position. */
21145 it.current_x = pt_x;
21147 else
21148 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21149 pixel_width = it.pixel_width;
21150 if (overshoot_expected && at_eol_p)
21151 pixel_width = 0;
21152 else if (pixel_width <= 0)
21153 pixel_width = 1;
21155 /* If there's a display string (or something similar) at point,
21156 we are actually at the glyph to the left of point, so we need
21157 to correct the X coordinate. */
21158 if (overshoot_expected)
21160 if (it.bidi_p)
21161 pt_x += pixel_width * it.bidi_it.scan_dir;
21162 else
21163 pt_x += pixel_width;
21166 /* Compute target X coordinate, either to the left or to the
21167 right of point. On TTY frames, all characters have the same
21168 pixel width of 1, so we can use that. On GUI frames we don't
21169 have an easy way of getting at the pixel width of the
21170 character to the left of point, so we use a different method
21171 of getting to that place. */
21172 if (dir > 0)
21173 target_x = pt_x + pixel_width;
21174 else
21175 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21177 /* Target X coordinate could be one line above or below the line
21178 of point, in which case we need to adjust the target X
21179 coordinate. Also, if moving to the left, we need to begin at
21180 the left edge of the point's screen line. */
21181 if (dir < 0)
21183 if (pt_x > 0)
21185 start_display (&it, w, pt);
21186 reseat_at_previous_visible_line_start (&it);
21187 it.current_x = it.current_y = it.hpos = 0;
21188 if (pt_vpos != 0)
21189 move_it_by_lines (&it, pt_vpos);
21191 else
21193 move_it_by_lines (&it, -1);
21194 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21195 target_is_eol_p = true;
21196 /* Under word-wrap, we don't know the x coordinate of
21197 the last character displayed on the previous line,
21198 which immediately precedes the wrap point. To find
21199 out its x coordinate, we try moving to the right
21200 margin of the window, which will stop at the wrap
21201 point, and then reset target_x to point at the
21202 character that precedes the wrap point. This is not
21203 needed on GUI frames, because (see below) there we
21204 move from the left margin one grapheme cluster at a
21205 time, and stop when we hit the wrap point. */
21206 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21208 void *it_data = NULL;
21209 struct it it2;
21211 SAVE_IT (it2, it, it_data);
21212 move_it_in_display_line_to (&it, ZV, target_x,
21213 MOVE_TO_POS | MOVE_TO_X);
21214 /* If we arrived at target_x, that _is_ the last
21215 character on the previous line. */
21216 if (it.current_x != target_x)
21217 target_x = it.current_x - 1;
21218 RESTORE_IT (&it, &it2, it_data);
21222 else
21224 if (at_eol_p
21225 || (target_x >= it.last_visible_x
21226 && it.line_wrap != TRUNCATE))
21228 if (pt_x > 0)
21229 move_it_by_lines (&it, 0);
21230 move_it_by_lines (&it, 1);
21231 target_x = 0;
21235 /* Move to the target X coordinate. */
21236 #ifdef HAVE_WINDOW_SYSTEM
21237 /* On GUI frames, as we don't know the X coordinate of the
21238 character to the left of point, moving point to the left
21239 requires walking, one grapheme cluster at a time, until we
21240 find ourself at a place immediately to the left of the
21241 character at point. */
21242 if (FRAME_WINDOW_P (it.f) && dir < 0)
21244 struct text_pos new_pos;
21245 enum move_it_result rc = MOVE_X_REACHED;
21247 if (it.current_x == 0)
21248 get_next_display_element (&it);
21249 if (it.what == IT_COMPOSITION)
21251 new_pos.charpos = it.cmp_it.charpos;
21252 new_pos.bytepos = -1;
21254 else
21255 new_pos = it.current.pos;
21257 while (it.current_x + it.pixel_width <= target_x
21258 && (rc == MOVE_X_REACHED
21259 /* Under word-wrap, move_it_in_display_line_to
21260 stops at correct coordinates, but sometimes
21261 returns MOVE_POS_MATCH_OR_ZV. */
21262 || (it.line_wrap == WORD_WRAP
21263 && rc == MOVE_POS_MATCH_OR_ZV)))
21265 int new_x = it.current_x + it.pixel_width;
21267 /* For composed characters, we want the position of the
21268 first character in the grapheme cluster (usually, the
21269 composition's base character), whereas it.current
21270 might give us the position of the _last_ one, e.g. if
21271 the composition is rendered in reverse due to bidi
21272 reordering. */
21273 if (it.what == IT_COMPOSITION)
21275 new_pos.charpos = it.cmp_it.charpos;
21276 new_pos.bytepos = -1;
21278 else
21279 new_pos = it.current.pos;
21280 if (new_x == it.current_x)
21281 new_x++;
21282 rc = move_it_in_display_line_to (&it, ZV, new_x,
21283 MOVE_TO_POS | MOVE_TO_X);
21284 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21285 break;
21287 /* The previous position we saw in the loop is the one we
21288 want. */
21289 if (new_pos.bytepos == -1)
21290 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21291 it.current.pos = new_pos;
21293 else
21294 #endif
21295 if (it.current_x != target_x)
21296 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21298 /* When lines are truncated, the above loop will stop at the
21299 window edge. But we want to get to the end of line, even if
21300 it is beyond the window edge; automatic hscroll will then
21301 scroll the window to show point as appropriate. */
21302 if (target_is_eol_p && it.line_wrap == TRUNCATE
21303 && get_next_display_element (&it))
21305 struct text_pos new_pos = it.current.pos;
21307 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21309 set_iterator_to_next (&it, 0);
21310 if (it.method == GET_FROM_BUFFER)
21311 new_pos = it.current.pos;
21312 if (!get_next_display_element (&it))
21313 break;
21316 it.current.pos = new_pos;
21319 /* If we ended up in a display string that covers point, move to
21320 buffer position to the right in the visual order. */
21321 if (dir > 0)
21323 while (IT_CHARPOS (it) == PT)
21325 set_iterator_to_next (&it, 0);
21326 if (!get_next_display_element (&it))
21327 break;
21331 /* Move point to that position. */
21332 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21335 return make_number (PT);
21337 #undef ROW_GLYPH_NEWLINE_P
21341 /***********************************************************************
21342 Menu Bar
21343 ***********************************************************************/
21345 /* Redisplay the menu bar in the frame for window W.
21347 The menu bar of X frames that don't have X toolkit support is
21348 displayed in a special window W->frame->menu_bar_window.
21350 The menu bar of terminal frames is treated specially as far as
21351 glyph matrices are concerned. Menu bar lines are not part of
21352 windows, so the update is done directly on the frame matrix rows
21353 for the menu bar. */
21355 static void
21356 display_menu_bar (struct window *w)
21358 struct frame *f = XFRAME (WINDOW_FRAME (w));
21359 struct it it;
21360 Lisp_Object items;
21361 int i;
21363 /* Don't do all this for graphical frames. */
21364 #ifdef HAVE_NTGUI
21365 if (FRAME_W32_P (f))
21366 return;
21367 #endif
21368 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21369 if (FRAME_X_P (f))
21370 return;
21371 #endif
21373 #ifdef HAVE_NS
21374 if (FRAME_NS_P (f))
21375 return;
21376 #endif /* HAVE_NS */
21378 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21379 eassert (!FRAME_WINDOW_P (f));
21380 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21381 it.first_visible_x = 0;
21382 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21383 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21384 if (FRAME_WINDOW_P (f))
21386 /* Menu bar lines are displayed in the desired matrix of the
21387 dummy window menu_bar_window. */
21388 struct window *menu_w;
21389 menu_w = XWINDOW (f->menu_bar_window);
21390 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21391 MENU_FACE_ID);
21392 it.first_visible_x = 0;
21393 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21395 else
21396 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21398 /* This is a TTY frame, i.e. character hpos/vpos are used as
21399 pixel x/y. */
21400 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21401 MENU_FACE_ID);
21402 it.first_visible_x = 0;
21403 it.last_visible_x = FRAME_COLS (f);
21406 /* FIXME: This should be controlled by a user option. See the
21407 comments in redisplay_tool_bar and display_mode_line about
21408 this. */
21409 it.paragraph_embedding = L2R;
21411 /* Clear all rows of the menu bar. */
21412 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21414 struct glyph_row *row = it.glyph_row + i;
21415 clear_glyph_row (row);
21416 row->enabled_p = true;
21417 row->full_width_p = 1;
21420 /* Display all items of the menu bar. */
21421 items = FRAME_MENU_BAR_ITEMS (it.f);
21422 for (i = 0; i < ASIZE (items); i += 4)
21424 Lisp_Object string;
21426 /* Stop at nil string. */
21427 string = AREF (items, i + 1);
21428 if (NILP (string))
21429 break;
21431 /* Remember where item was displayed. */
21432 ASET (items, i + 3, make_number (it.hpos));
21434 /* Display the item, pad with one space. */
21435 if (it.current_x < it.last_visible_x)
21436 display_string (NULL, string, Qnil, 0, 0, &it,
21437 SCHARS (string) + 1, 0, 0, -1);
21440 /* Fill out the line with spaces. */
21441 if (it.current_x < it.last_visible_x)
21442 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21444 /* Compute the total height of the lines. */
21445 compute_line_metrics (&it);
21448 /* Deep copy of a glyph row, including the glyphs. */
21449 static void
21450 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21452 struct glyph *pointers[1 + LAST_AREA];
21453 int to_used = to->used[TEXT_AREA];
21455 /* Save glyph pointers of TO. */
21456 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21458 /* Do a structure assignment. */
21459 *to = *from;
21461 /* Restore original glyph pointers of TO. */
21462 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21464 /* Copy the glyphs. */
21465 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21466 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21468 /* If we filled only part of the TO row, fill the rest with
21469 space_glyph (which will display as empty space). */
21470 if (to_used > from->used[TEXT_AREA])
21471 fill_up_frame_row_with_spaces (to, to_used);
21474 /* Display one menu item on a TTY, by overwriting the glyphs in the
21475 frame F's desired glyph matrix with glyphs produced from the menu
21476 item text. Called from term.c to display TTY drop-down menus one
21477 item at a time.
21479 ITEM_TEXT is the menu item text as a C string.
21481 FACE_ID is the face ID to be used for this menu item. FACE_ID
21482 could specify one of 3 faces: a face for an enabled item, a face
21483 for a disabled item, or a face for a selected item.
21485 X and Y are coordinates of the first glyph in the frame's desired
21486 matrix to be overwritten by the menu item. Since this is a TTY, Y
21487 is the zero-based number of the glyph row and X is the zero-based
21488 glyph number in the row, starting from left, where to start
21489 displaying the item.
21491 SUBMENU non-zero means this menu item drops down a submenu, which
21492 should be indicated by displaying a proper visual cue after the
21493 item text. */
21495 void
21496 display_tty_menu_item (const char *item_text, int width, int face_id,
21497 int x, int y, int submenu)
21499 struct it it;
21500 struct frame *f = SELECTED_FRAME ();
21501 struct window *w = XWINDOW (f->selected_window);
21502 int saved_used, saved_truncated, saved_width, saved_reversed;
21503 struct glyph_row *row;
21504 size_t item_len = strlen (item_text);
21506 eassert (FRAME_TERMCAP_P (f));
21508 /* Don't write beyond the matrix's last row. This can happen for
21509 TTY screens that are not high enough to show the entire menu.
21510 (This is actually a bit of defensive programming, as
21511 tty_menu_display already limits the number of menu items to one
21512 less than the number of screen lines.) */
21513 if (y >= f->desired_matrix->nrows)
21514 return;
21516 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21517 it.first_visible_x = 0;
21518 it.last_visible_x = FRAME_COLS (f) - 1;
21519 row = it.glyph_row;
21520 /* Start with the row contents from the current matrix. */
21521 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21522 saved_width = row->full_width_p;
21523 row->full_width_p = 1;
21524 saved_reversed = row->reversed_p;
21525 row->reversed_p = 0;
21526 row->enabled_p = true;
21528 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21529 desired face. */
21530 eassert (x < f->desired_matrix->matrix_w);
21531 it.current_x = it.hpos = x;
21532 it.current_y = it.vpos = y;
21533 saved_used = row->used[TEXT_AREA];
21534 saved_truncated = row->truncated_on_right_p;
21535 row->used[TEXT_AREA] = x;
21536 it.face_id = face_id;
21537 it.line_wrap = TRUNCATE;
21539 /* FIXME: This should be controlled by a user option. See the
21540 comments in redisplay_tool_bar and display_mode_line about this.
21541 Also, if paragraph_embedding could ever be R2L, changes will be
21542 needed to avoid shifting to the right the row characters in
21543 term.c:append_glyph. */
21544 it.paragraph_embedding = L2R;
21546 /* Pad with a space on the left. */
21547 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21548 width--;
21549 /* Display the menu item, pad with spaces to WIDTH. */
21550 if (submenu)
21552 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21553 item_len, 0, FRAME_COLS (f) - 1, -1);
21554 width -= item_len;
21555 /* Indicate with " >" that there's a submenu. */
21556 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21557 FRAME_COLS (f) - 1, -1);
21559 else
21560 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21561 width, 0, FRAME_COLS (f) - 1, -1);
21563 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21564 row->truncated_on_right_p = saved_truncated;
21565 row->hash = row_hash (row);
21566 row->full_width_p = saved_width;
21567 row->reversed_p = saved_reversed;
21570 /***********************************************************************
21571 Mode Line
21572 ***********************************************************************/
21574 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21575 FORCE is non-zero, redisplay mode lines unconditionally.
21576 Otherwise, redisplay only mode lines that are garbaged. Value is
21577 the number of windows whose mode lines were redisplayed. */
21579 static int
21580 redisplay_mode_lines (Lisp_Object window, bool force)
21582 int nwindows = 0;
21584 while (!NILP (window))
21586 struct window *w = XWINDOW (window);
21588 if (WINDOWP (w->contents))
21589 nwindows += redisplay_mode_lines (w->contents, force);
21590 else if (force
21591 || FRAME_GARBAGED_P (XFRAME (w->frame))
21592 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21594 struct text_pos lpoint;
21595 struct buffer *old = current_buffer;
21597 /* Set the window's buffer for the mode line display. */
21598 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21599 set_buffer_internal_1 (XBUFFER (w->contents));
21601 /* Point refers normally to the selected window. For any
21602 other window, set up appropriate value. */
21603 if (!EQ (window, selected_window))
21605 struct text_pos pt;
21607 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21608 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21611 /* Display mode lines. */
21612 clear_glyph_matrix (w->desired_matrix);
21613 if (display_mode_lines (w))
21614 ++nwindows;
21616 /* Restore old settings. */
21617 set_buffer_internal_1 (old);
21618 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21621 window = w->next;
21624 return nwindows;
21628 /* Display the mode and/or header line of window W. Value is the
21629 sum number of mode lines and header lines displayed. */
21631 static int
21632 display_mode_lines (struct window *w)
21634 Lisp_Object old_selected_window = selected_window;
21635 Lisp_Object old_selected_frame = selected_frame;
21636 Lisp_Object new_frame = w->frame;
21637 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21638 int n = 0;
21640 selected_frame = new_frame;
21641 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21642 or window's point, then we'd need select_window_1 here as well. */
21643 XSETWINDOW (selected_window, w);
21644 XFRAME (new_frame)->selected_window = selected_window;
21646 /* These will be set while the mode line specs are processed. */
21647 line_number_displayed = 0;
21648 w->column_number_displayed = -1;
21650 if (WINDOW_WANTS_MODELINE_P (w))
21652 struct window *sel_w = XWINDOW (old_selected_window);
21654 /* Select mode line face based on the real selected window. */
21655 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21656 BVAR (current_buffer, mode_line_format));
21657 ++n;
21660 if (WINDOW_WANTS_HEADER_LINE_P (w))
21662 display_mode_line (w, HEADER_LINE_FACE_ID,
21663 BVAR (current_buffer, header_line_format));
21664 ++n;
21667 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21668 selected_frame = old_selected_frame;
21669 selected_window = old_selected_window;
21670 if (n > 0)
21671 w->must_be_updated_p = true;
21672 return n;
21676 /* Display mode or header line of window W. FACE_ID specifies which
21677 line to display; it is either MODE_LINE_FACE_ID or
21678 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21679 display. Value is the pixel height of the mode/header line
21680 displayed. */
21682 static int
21683 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21685 struct it it;
21686 struct face *face;
21687 ptrdiff_t count = SPECPDL_INDEX ();
21689 init_iterator (&it, w, -1, -1, NULL, face_id);
21690 /* Don't extend on a previously drawn mode-line.
21691 This may happen if called from pos_visible_p. */
21692 it.glyph_row->enabled_p = false;
21693 prepare_desired_row (w, it.glyph_row, true);
21695 it.glyph_row->mode_line_p = 1;
21697 /* FIXME: This should be controlled by a user option. But
21698 supporting such an option is not trivial, since the mode line is
21699 made up of many separate strings. */
21700 it.paragraph_embedding = L2R;
21702 record_unwind_protect (unwind_format_mode_line,
21703 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21705 mode_line_target = MODE_LINE_DISPLAY;
21707 /* Temporarily make frame's keyboard the current kboard so that
21708 kboard-local variables in the mode_line_format will get the right
21709 values. */
21710 push_kboard (FRAME_KBOARD (it.f));
21711 record_unwind_save_match_data ();
21712 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21713 pop_kboard ();
21715 unbind_to (count, Qnil);
21717 /* Fill up with spaces. */
21718 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21720 compute_line_metrics (&it);
21721 it.glyph_row->full_width_p = 1;
21722 it.glyph_row->continued_p = 0;
21723 it.glyph_row->truncated_on_left_p = 0;
21724 it.glyph_row->truncated_on_right_p = 0;
21726 /* Make a 3D mode-line have a shadow at its right end. */
21727 face = FACE_FROM_ID (it.f, face_id);
21728 extend_face_to_end_of_line (&it);
21729 if (face->box != FACE_NO_BOX)
21731 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21732 + it.glyph_row->used[TEXT_AREA] - 1);
21733 last->right_box_line_p = 1;
21736 return it.glyph_row->height;
21739 /* Move element ELT in LIST to the front of LIST.
21740 Return the updated list. */
21742 static Lisp_Object
21743 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21745 register Lisp_Object tail, prev;
21746 register Lisp_Object tem;
21748 tail = list;
21749 prev = Qnil;
21750 while (CONSP (tail))
21752 tem = XCAR (tail);
21754 if (EQ (elt, tem))
21756 /* Splice out the link TAIL. */
21757 if (NILP (prev))
21758 list = XCDR (tail);
21759 else
21760 Fsetcdr (prev, XCDR (tail));
21762 /* Now make it the first. */
21763 Fsetcdr (tail, list);
21764 return tail;
21766 else
21767 prev = tail;
21768 tail = XCDR (tail);
21769 QUIT;
21772 /* Not found--return unchanged LIST. */
21773 return list;
21776 /* Contribute ELT to the mode line for window IT->w. How it
21777 translates into text depends on its data type.
21779 IT describes the display environment in which we display, as usual.
21781 DEPTH is the depth in recursion. It is used to prevent
21782 infinite recursion here.
21784 FIELD_WIDTH is the number of characters the display of ELT should
21785 occupy in the mode line, and PRECISION is the maximum number of
21786 characters to display from ELT's representation. See
21787 display_string for details.
21789 Returns the hpos of the end of the text generated by ELT.
21791 PROPS is a property list to add to any string we encounter.
21793 If RISKY is nonzero, remove (disregard) any properties in any string
21794 we encounter, and ignore :eval and :propertize.
21796 The global variable `mode_line_target' determines whether the
21797 output is passed to `store_mode_line_noprop',
21798 `store_mode_line_string', or `display_string'. */
21800 static int
21801 display_mode_element (struct it *it, int depth, int field_width, int precision,
21802 Lisp_Object elt, Lisp_Object props, int risky)
21804 int n = 0, field, prec;
21805 int literal = 0;
21807 tail_recurse:
21808 if (depth > 100)
21809 elt = build_string ("*too-deep*");
21811 depth++;
21813 switch (XTYPE (elt))
21815 case Lisp_String:
21817 /* A string: output it and check for %-constructs within it. */
21818 unsigned char c;
21819 ptrdiff_t offset = 0;
21821 if (SCHARS (elt) > 0
21822 && (!NILP (props) || risky))
21824 Lisp_Object oprops, aelt;
21825 oprops = Ftext_properties_at (make_number (0), elt);
21827 /* If the starting string's properties are not what
21828 we want, translate the string. Also, if the string
21829 is risky, do that anyway. */
21831 if (NILP (Fequal (props, oprops)) || risky)
21833 /* If the starting string has properties,
21834 merge the specified ones onto the existing ones. */
21835 if (! NILP (oprops) && !risky)
21837 Lisp_Object tem;
21839 oprops = Fcopy_sequence (oprops);
21840 tem = props;
21841 while (CONSP (tem))
21843 oprops = Fplist_put (oprops, XCAR (tem),
21844 XCAR (XCDR (tem)));
21845 tem = XCDR (XCDR (tem));
21847 props = oprops;
21850 aelt = Fassoc (elt, mode_line_proptrans_alist);
21851 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21853 /* AELT is what we want. Move it to the front
21854 without consing. */
21855 elt = XCAR (aelt);
21856 mode_line_proptrans_alist
21857 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21859 else
21861 Lisp_Object tem;
21863 /* If AELT has the wrong props, it is useless.
21864 so get rid of it. */
21865 if (! NILP (aelt))
21866 mode_line_proptrans_alist
21867 = Fdelq (aelt, mode_line_proptrans_alist);
21869 elt = Fcopy_sequence (elt);
21870 Fset_text_properties (make_number (0), Flength (elt),
21871 props, elt);
21872 /* Add this item to mode_line_proptrans_alist. */
21873 mode_line_proptrans_alist
21874 = Fcons (Fcons (elt, props),
21875 mode_line_proptrans_alist);
21876 /* Truncate mode_line_proptrans_alist
21877 to at most 50 elements. */
21878 tem = Fnthcdr (make_number (50),
21879 mode_line_proptrans_alist);
21880 if (! NILP (tem))
21881 XSETCDR (tem, Qnil);
21886 offset = 0;
21888 if (literal)
21890 prec = precision - n;
21891 switch (mode_line_target)
21893 case MODE_LINE_NOPROP:
21894 case MODE_LINE_TITLE:
21895 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
21896 break;
21897 case MODE_LINE_STRING:
21898 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
21899 break;
21900 case MODE_LINE_DISPLAY:
21901 n += display_string (NULL, elt, Qnil, 0, 0, it,
21902 0, prec, 0, STRING_MULTIBYTE (elt));
21903 break;
21906 break;
21909 /* Handle the non-literal case. */
21911 while ((precision <= 0 || n < precision)
21912 && SREF (elt, offset) != 0
21913 && (mode_line_target != MODE_LINE_DISPLAY
21914 || it->current_x < it->last_visible_x))
21916 ptrdiff_t last_offset = offset;
21918 /* Advance to end of string or next format specifier. */
21919 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
21922 if (offset - 1 != last_offset)
21924 ptrdiff_t nchars, nbytes;
21926 /* Output to end of string or up to '%'. Field width
21927 is length of string. Don't output more than
21928 PRECISION allows us. */
21929 offset--;
21931 prec = c_string_width (SDATA (elt) + last_offset,
21932 offset - last_offset, precision - n,
21933 &nchars, &nbytes);
21935 switch (mode_line_target)
21937 case MODE_LINE_NOPROP:
21938 case MODE_LINE_TITLE:
21939 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
21940 break;
21941 case MODE_LINE_STRING:
21943 ptrdiff_t bytepos = last_offset;
21944 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21945 ptrdiff_t endpos = (precision <= 0
21946 ? string_byte_to_char (elt, offset)
21947 : charpos + nchars);
21949 n += store_mode_line_string (NULL,
21950 Fsubstring (elt, make_number (charpos),
21951 make_number (endpos)),
21952 0, 0, 0, Qnil);
21954 break;
21955 case MODE_LINE_DISPLAY:
21957 ptrdiff_t bytepos = last_offset;
21958 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21960 if (precision <= 0)
21961 nchars = string_byte_to_char (elt, offset) - charpos;
21962 n += display_string (NULL, elt, Qnil, 0, charpos,
21963 it, 0, nchars, 0,
21964 STRING_MULTIBYTE (elt));
21966 break;
21969 else /* c == '%' */
21971 ptrdiff_t percent_position = offset;
21973 /* Get the specified minimum width. Zero means
21974 don't pad. */
21975 field = 0;
21976 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21977 field = field * 10 + c - '0';
21979 /* Don't pad beyond the total padding allowed. */
21980 if (field_width - n > 0 && field > field_width - n)
21981 field = field_width - n;
21983 /* Note that either PRECISION <= 0 or N < PRECISION. */
21984 prec = precision - n;
21986 if (c == 'M')
21987 n += display_mode_element (it, depth, field, prec,
21988 Vglobal_mode_string, props,
21989 risky);
21990 else if (c != 0)
21992 bool multibyte;
21993 ptrdiff_t bytepos, charpos;
21994 const char *spec;
21995 Lisp_Object string;
21997 bytepos = percent_position;
21998 charpos = (STRING_MULTIBYTE (elt)
21999 ? string_byte_to_char (elt, bytepos)
22000 : bytepos);
22001 spec = decode_mode_spec (it->w, c, field, &string);
22002 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22004 switch (mode_line_target)
22006 case MODE_LINE_NOPROP:
22007 case MODE_LINE_TITLE:
22008 n += store_mode_line_noprop (spec, field, prec);
22009 break;
22010 case MODE_LINE_STRING:
22012 Lisp_Object tem = build_string (spec);
22013 props = Ftext_properties_at (make_number (charpos), elt);
22014 /* Should only keep face property in props */
22015 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22017 break;
22018 case MODE_LINE_DISPLAY:
22020 int nglyphs_before, nwritten;
22022 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22023 nwritten = display_string (spec, string, elt,
22024 charpos, 0, it,
22025 field, prec, 0,
22026 multibyte);
22028 /* Assign to the glyphs written above the
22029 string where the `%x' came from, position
22030 of the `%'. */
22031 if (nwritten > 0)
22033 struct glyph *glyph
22034 = (it->glyph_row->glyphs[TEXT_AREA]
22035 + nglyphs_before);
22036 int i;
22038 for (i = 0; i < nwritten; ++i)
22040 glyph[i].object = elt;
22041 glyph[i].charpos = charpos;
22044 n += nwritten;
22047 break;
22050 else /* c == 0 */
22051 break;
22055 break;
22057 case Lisp_Symbol:
22058 /* A symbol: process the value of the symbol recursively
22059 as if it appeared here directly. Avoid error if symbol void.
22060 Special case: if value of symbol is a string, output the string
22061 literally. */
22063 register Lisp_Object tem;
22065 /* If the variable is not marked as risky to set
22066 then its contents are risky to use. */
22067 if (NILP (Fget (elt, Qrisky_local_variable)))
22068 risky = 1;
22070 tem = Fboundp (elt);
22071 if (!NILP (tem))
22073 tem = Fsymbol_value (elt);
22074 /* If value is a string, output that string literally:
22075 don't check for % within it. */
22076 if (STRINGP (tem))
22077 literal = 1;
22079 if (!EQ (tem, elt))
22081 /* Give up right away for nil or t. */
22082 elt = tem;
22083 goto tail_recurse;
22087 break;
22089 case Lisp_Cons:
22091 register Lisp_Object car, tem;
22093 /* A cons cell: five distinct cases.
22094 If first element is :eval or :propertize, do something special.
22095 If first element is a string or a cons, process all the elements
22096 and effectively concatenate them.
22097 If first element is a negative number, truncate displaying cdr to
22098 at most that many characters. If positive, pad (with spaces)
22099 to at least that many characters.
22100 If first element is a symbol, process the cadr or caddr recursively
22101 according to whether the symbol's value is non-nil or nil. */
22102 car = XCAR (elt);
22103 if (EQ (car, QCeval))
22105 /* An element of the form (:eval FORM) means evaluate FORM
22106 and use the result as mode line elements. */
22108 if (risky)
22109 break;
22111 if (CONSP (XCDR (elt)))
22113 Lisp_Object spec;
22114 spec = safe__eval (true, XCAR (XCDR (elt)));
22115 n += display_mode_element (it, depth, field_width - n,
22116 precision - n, spec, props,
22117 risky);
22120 else if (EQ (car, QCpropertize))
22122 /* An element of the form (:propertize ELT PROPS...)
22123 means display ELT but applying properties PROPS. */
22125 if (risky)
22126 break;
22128 if (CONSP (XCDR (elt)))
22129 n += display_mode_element (it, depth, field_width - n,
22130 precision - n, XCAR (XCDR (elt)),
22131 XCDR (XCDR (elt)), risky);
22133 else if (SYMBOLP (car))
22135 tem = Fboundp (car);
22136 elt = XCDR (elt);
22137 if (!CONSP (elt))
22138 goto invalid;
22139 /* elt is now the cdr, and we know it is a cons cell.
22140 Use its car if CAR has a non-nil value. */
22141 if (!NILP (tem))
22143 tem = Fsymbol_value (car);
22144 if (!NILP (tem))
22146 elt = XCAR (elt);
22147 goto tail_recurse;
22150 /* Symbol's value is nil (or symbol is unbound)
22151 Get the cddr of the original list
22152 and if possible find the caddr and use that. */
22153 elt = XCDR (elt);
22154 if (NILP (elt))
22155 break;
22156 else if (!CONSP (elt))
22157 goto invalid;
22158 elt = XCAR (elt);
22159 goto tail_recurse;
22161 else if (INTEGERP (car))
22163 register int lim = XINT (car);
22164 elt = XCDR (elt);
22165 if (lim < 0)
22167 /* Negative int means reduce maximum width. */
22168 if (precision <= 0)
22169 precision = -lim;
22170 else
22171 precision = min (precision, -lim);
22173 else if (lim > 0)
22175 /* Padding specified. Don't let it be more than
22176 current maximum. */
22177 if (precision > 0)
22178 lim = min (precision, lim);
22180 /* If that's more padding than already wanted, queue it.
22181 But don't reduce padding already specified even if
22182 that is beyond the current truncation point. */
22183 field_width = max (lim, field_width);
22185 goto tail_recurse;
22187 else if (STRINGP (car) || CONSP (car))
22189 Lisp_Object halftail = elt;
22190 int len = 0;
22192 while (CONSP (elt)
22193 && (precision <= 0 || n < precision))
22195 n += display_mode_element (it, depth,
22196 /* Do padding only after the last
22197 element in the list. */
22198 (! CONSP (XCDR (elt))
22199 ? field_width - n
22200 : 0),
22201 precision - n, XCAR (elt),
22202 props, risky);
22203 elt = XCDR (elt);
22204 len++;
22205 if ((len & 1) == 0)
22206 halftail = XCDR (halftail);
22207 /* Check for cycle. */
22208 if (EQ (halftail, elt))
22209 break;
22213 break;
22215 default:
22216 invalid:
22217 elt = build_string ("*invalid*");
22218 goto tail_recurse;
22221 /* Pad to FIELD_WIDTH. */
22222 if (field_width > 0 && n < field_width)
22224 switch (mode_line_target)
22226 case MODE_LINE_NOPROP:
22227 case MODE_LINE_TITLE:
22228 n += store_mode_line_noprop ("", field_width - n, 0);
22229 break;
22230 case MODE_LINE_STRING:
22231 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22232 break;
22233 case MODE_LINE_DISPLAY:
22234 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22235 0, 0, 0);
22236 break;
22240 return n;
22243 /* Store a mode-line string element in mode_line_string_list.
22245 If STRING is non-null, display that C string. Otherwise, the Lisp
22246 string LISP_STRING is displayed.
22248 FIELD_WIDTH is the minimum number of output glyphs to produce.
22249 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22250 with spaces. FIELD_WIDTH <= 0 means don't pad.
22252 PRECISION is the maximum number of characters to output from
22253 STRING. PRECISION <= 0 means don't truncate the string.
22255 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22256 properties to the string.
22258 PROPS are the properties to add to the string.
22259 The mode_line_string_face face property is always added to the string.
22262 static int
22263 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22264 int field_width, int precision, Lisp_Object props)
22266 ptrdiff_t len;
22267 int n = 0;
22269 if (string != NULL)
22271 len = strlen (string);
22272 if (precision > 0 && len > precision)
22273 len = precision;
22274 lisp_string = make_string (string, len);
22275 if (NILP (props))
22276 props = mode_line_string_face_prop;
22277 else if (!NILP (mode_line_string_face))
22279 Lisp_Object face = Fplist_get (props, Qface);
22280 props = Fcopy_sequence (props);
22281 if (NILP (face))
22282 face = mode_line_string_face;
22283 else
22284 face = list2 (face, mode_line_string_face);
22285 props = Fplist_put (props, Qface, face);
22287 Fadd_text_properties (make_number (0), make_number (len),
22288 props, lisp_string);
22290 else
22292 len = XFASTINT (Flength (lisp_string));
22293 if (precision > 0 && len > precision)
22295 len = precision;
22296 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22297 precision = -1;
22299 if (!NILP (mode_line_string_face))
22301 Lisp_Object face;
22302 if (NILP (props))
22303 props = Ftext_properties_at (make_number (0), lisp_string);
22304 face = Fplist_get (props, Qface);
22305 if (NILP (face))
22306 face = mode_line_string_face;
22307 else
22308 face = list2 (face, mode_line_string_face);
22309 props = list2 (Qface, face);
22310 if (copy_string)
22311 lisp_string = Fcopy_sequence (lisp_string);
22313 if (!NILP (props))
22314 Fadd_text_properties (make_number (0), make_number (len),
22315 props, lisp_string);
22318 if (len > 0)
22320 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22321 n += len;
22324 if (field_width > len)
22326 field_width -= len;
22327 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22328 if (!NILP (props))
22329 Fadd_text_properties (make_number (0), make_number (field_width),
22330 props, lisp_string);
22331 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22332 n += field_width;
22335 return n;
22339 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22340 1, 4, 0,
22341 doc: /* Format a string out of a mode line format specification.
22342 First arg FORMAT specifies the mode line format (see `mode-line-format'
22343 for details) to use.
22345 By default, the format is evaluated for the currently selected window.
22347 Optional second arg FACE specifies the face property to put on all
22348 characters for which no face is specified. The value nil means the
22349 default face. The value t means whatever face the window's mode line
22350 currently uses (either `mode-line' or `mode-line-inactive',
22351 depending on whether the window is the selected window or not).
22352 An integer value means the value string has no text
22353 properties.
22355 Optional third and fourth args WINDOW and BUFFER specify the window
22356 and buffer to use as the context for the formatting (defaults
22357 are the selected window and the WINDOW's buffer). */)
22358 (Lisp_Object format, Lisp_Object face,
22359 Lisp_Object window, Lisp_Object buffer)
22361 struct it it;
22362 int len;
22363 struct window *w;
22364 struct buffer *old_buffer = NULL;
22365 int face_id;
22366 int no_props = INTEGERP (face);
22367 ptrdiff_t count = SPECPDL_INDEX ();
22368 Lisp_Object str;
22369 int string_start = 0;
22371 w = decode_any_window (window);
22372 XSETWINDOW (window, w);
22374 if (NILP (buffer))
22375 buffer = w->contents;
22376 CHECK_BUFFER (buffer);
22378 /* Make formatting the modeline a non-op when noninteractive, otherwise
22379 there will be problems later caused by a partially initialized frame. */
22380 if (NILP (format) || noninteractive)
22381 return empty_unibyte_string;
22383 if (no_props)
22384 face = Qnil;
22386 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22387 : EQ (face, Qt) ? (EQ (window, selected_window)
22388 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22389 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22390 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22391 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22392 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22393 : DEFAULT_FACE_ID;
22395 old_buffer = current_buffer;
22397 /* Save things including mode_line_proptrans_alist,
22398 and set that to nil so that we don't alter the outer value. */
22399 record_unwind_protect (unwind_format_mode_line,
22400 format_mode_line_unwind_data
22401 (XFRAME (WINDOW_FRAME (w)),
22402 old_buffer, selected_window, 1));
22403 mode_line_proptrans_alist = Qnil;
22405 Fselect_window (window, Qt);
22406 set_buffer_internal_1 (XBUFFER (buffer));
22408 init_iterator (&it, w, -1, -1, NULL, face_id);
22410 if (no_props)
22412 mode_line_target = MODE_LINE_NOPROP;
22413 mode_line_string_face_prop = Qnil;
22414 mode_line_string_list = Qnil;
22415 string_start = MODE_LINE_NOPROP_LEN (0);
22417 else
22419 mode_line_target = MODE_LINE_STRING;
22420 mode_line_string_list = Qnil;
22421 mode_line_string_face = face;
22422 mode_line_string_face_prop
22423 = NILP (face) ? Qnil : list2 (Qface, face);
22426 push_kboard (FRAME_KBOARD (it.f));
22427 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22428 pop_kboard ();
22430 if (no_props)
22432 len = MODE_LINE_NOPROP_LEN (string_start);
22433 str = make_string (mode_line_noprop_buf + string_start, len);
22435 else
22437 mode_line_string_list = Fnreverse (mode_line_string_list);
22438 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22439 empty_unibyte_string);
22442 unbind_to (count, Qnil);
22443 return str;
22446 /* Write a null-terminated, right justified decimal representation of
22447 the positive integer D to BUF using a minimal field width WIDTH. */
22449 static void
22450 pint2str (register char *buf, register int width, register ptrdiff_t d)
22452 register char *p = buf;
22454 if (d <= 0)
22455 *p++ = '0';
22456 else
22458 while (d > 0)
22460 *p++ = d % 10 + '0';
22461 d /= 10;
22465 for (width -= (int) (p - buf); width > 0; --width)
22466 *p++ = ' ';
22467 *p-- = '\0';
22468 while (p > buf)
22470 d = *buf;
22471 *buf++ = *p;
22472 *p-- = d;
22476 /* Write a null-terminated, right justified decimal and "human
22477 readable" representation of the nonnegative integer D to BUF using
22478 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22480 static const char power_letter[] =
22482 0, /* no letter */
22483 'k', /* kilo */
22484 'M', /* mega */
22485 'G', /* giga */
22486 'T', /* tera */
22487 'P', /* peta */
22488 'E', /* exa */
22489 'Z', /* zetta */
22490 'Y' /* yotta */
22493 static void
22494 pint2hrstr (char *buf, int width, ptrdiff_t d)
22496 /* We aim to represent the nonnegative integer D as
22497 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22498 ptrdiff_t quotient = d;
22499 int remainder = 0;
22500 /* -1 means: do not use TENTHS. */
22501 int tenths = -1;
22502 int exponent = 0;
22504 /* Length of QUOTIENT.TENTHS as a string. */
22505 int length;
22507 char * psuffix;
22508 char * p;
22510 if (quotient >= 1000)
22512 /* Scale to the appropriate EXPONENT. */
22515 remainder = quotient % 1000;
22516 quotient /= 1000;
22517 exponent++;
22519 while (quotient >= 1000);
22521 /* Round to nearest and decide whether to use TENTHS or not. */
22522 if (quotient <= 9)
22524 tenths = remainder / 100;
22525 if (remainder % 100 >= 50)
22527 if (tenths < 9)
22528 tenths++;
22529 else
22531 quotient++;
22532 if (quotient == 10)
22533 tenths = -1;
22534 else
22535 tenths = 0;
22539 else
22540 if (remainder >= 500)
22542 if (quotient < 999)
22543 quotient++;
22544 else
22546 quotient = 1;
22547 exponent++;
22548 tenths = 0;
22553 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22554 if (tenths == -1 && quotient <= 99)
22555 if (quotient <= 9)
22556 length = 1;
22557 else
22558 length = 2;
22559 else
22560 length = 3;
22561 p = psuffix = buf + max (width, length);
22563 /* Print EXPONENT. */
22564 *psuffix++ = power_letter[exponent];
22565 *psuffix = '\0';
22567 /* Print TENTHS. */
22568 if (tenths >= 0)
22570 *--p = '0' + tenths;
22571 *--p = '.';
22574 /* Print QUOTIENT. */
22577 int digit = quotient % 10;
22578 *--p = '0' + digit;
22580 while ((quotient /= 10) != 0);
22582 /* Print leading spaces. */
22583 while (buf < p)
22584 *--p = ' ';
22587 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22588 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22589 type of CODING_SYSTEM. Return updated pointer into BUF. */
22591 static unsigned char invalid_eol_type[] = "(*invalid*)";
22593 static char *
22594 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22596 Lisp_Object val;
22597 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22598 const unsigned char *eol_str;
22599 int eol_str_len;
22600 /* The EOL conversion we are using. */
22601 Lisp_Object eoltype;
22603 val = CODING_SYSTEM_SPEC (coding_system);
22604 eoltype = Qnil;
22606 if (!VECTORP (val)) /* Not yet decided. */
22608 *buf++ = multibyte ? '-' : ' ';
22609 if (eol_flag)
22610 eoltype = eol_mnemonic_undecided;
22611 /* Don't mention EOL conversion if it isn't decided. */
22613 else
22615 Lisp_Object attrs;
22616 Lisp_Object eolvalue;
22618 attrs = AREF (val, 0);
22619 eolvalue = AREF (val, 2);
22621 *buf++ = multibyte
22622 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22623 : ' ';
22625 if (eol_flag)
22627 /* The EOL conversion that is normal on this system. */
22629 if (NILP (eolvalue)) /* Not yet decided. */
22630 eoltype = eol_mnemonic_undecided;
22631 else if (VECTORP (eolvalue)) /* Not yet decided. */
22632 eoltype = eol_mnemonic_undecided;
22633 else /* eolvalue is Qunix, Qdos, or Qmac. */
22634 eoltype = (EQ (eolvalue, Qunix)
22635 ? eol_mnemonic_unix
22636 : (EQ (eolvalue, Qdos) == 1
22637 ? eol_mnemonic_dos : eol_mnemonic_mac));
22641 if (eol_flag)
22643 /* Mention the EOL conversion if it is not the usual one. */
22644 if (STRINGP (eoltype))
22646 eol_str = SDATA (eoltype);
22647 eol_str_len = SBYTES (eoltype);
22649 else if (CHARACTERP (eoltype))
22651 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
22652 int c = XFASTINT (eoltype);
22653 eol_str_len = CHAR_STRING (c, tmp);
22654 eol_str = tmp;
22656 else
22658 eol_str = invalid_eol_type;
22659 eol_str_len = sizeof (invalid_eol_type) - 1;
22661 memcpy (buf, eol_str, eol_str_len);
22662 buf += eol_str_len;
22665 return buf;
22668 /* Return a string for the output of a mode line %-spec for window W,
22669 generated by character C. FIELD_WIDTH > 0 means pad the string
22670 returned with spaces to that value. Return a Lisp string in
22671 *STRING if the resulting string is taken from that Lisp string.
22673 Note we operate on the current buffer for most purposes. */
22675 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22677 static const char *
22678 decode_mode_spec (struct window *w, register int c, int field_width,
22679 Lisp_Object *string)
22681 Lisp_Object obj;
22682 struct frame *f = XFRAME (WINDOW_FRAME (w));
22683 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22684 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22685 produce strings from numerical values, so limit preposterously
22686 large values of FIELD_WIDTH to avoid overrunning the buffer's
22687 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22688 bytes plus the terminating null. */
22689 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22690 struct buffer *b = current_buffer;
22692 obj = Qnil;
22693 *string = Qnil;
22695 switch (c)
22697 case '*':
22698 if (!NILP (BVAR (b, read_only)))
22699 return "%";
22700 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22701 return "*";
22702 return "-";
22704 case '+':
22705 /* This differs from %* only for a modified read-only buffer. */
22706 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22707 return "*";
22708 if (!NILP (BVAR (b, read_only)))
22709 return "%";
22710 return "-";
22712 case '&':
22713 /* This differs from %* in ignoring read-only-ness. */
22714 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22715 return "*";
22716 return "-";
22718 case '%':
22719 return "%";
22721 case '[':
22723 int i;
22724 char *p;
22726 if (command_loop_level > 5)
22727 return "[[[... ";
22728 p = decode_mode_spec_buf;
22729 for (i = 0; i < command_loop_level; i++)
22730 *p++ = '[';
22731 *p = 0;
22732 return decode_mode_spec_buf;
22735 case ']':
22737 int i;
22738 char *p;
22740 if (command_loop_level > 5)
22741 return " ...]]]";
22742 p = decode_mode_spec_buf;
22743 for (i = 0; i < command_loop_level; i++)
22744 *p++ = ']';
22745 *p = 0;
22746 return decode_mode_spec_buf;
22749 case '-':
22751 register int i;
22753 /* Let lots_of_dashes be a string of infinite length. */
22754 if (mode_line_target == MODE_LINE_NOPROP
22755 || mode_line_target == MODE_LINE_STRING)
22756 return "--";
22757 if (field_width <= 0
22758 || field_width > sizeof (lots_of_dashes))
22760 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22761 decode_mode_spec_buf[i] = '-';
22762 decode_mode_spec_buf[i] = '\0';
22763 return decode_mode_spec_buf;
22765 else
22766 return lots_of_dashes;
22769 case 'b':
22770 obj = BVAR (b, name);
22771 break;
22773 case 'c':
22774 /* %c and %l are ignored in `frame-title-format'.
22775 (In redisplay_internal, the frame title is drawn _before_ the
22776 windows are updated, so the stuff which depends on actual
22777 window contents (such as %l) may fail to render properly, or
22778 even crash emacs.) */
22779 if (mode_line_target == MODE_LINE_TITLE)
22780 return "";
22781 else
22783 ptrdiff_t col = current_column ();
22784 w->column_number_displayed = col;
22785 pint2str (decode_mode_spec_buf, width, col);
22786 return decode_mode_spec_buf;
22789 case 'e':
22790 #ifndef SYSTEM_MALLOC
22792 if (NILP (Vmemory_full))
22793 return "";
22794 else
22795 return "!MEM FULL! ";
22797 #else
22798 return "";
22799 #endif
22801 case 'F':
22802 /* %F displays the frame name. */
22803 if (!NILP (f->title))
22804 return SSDATA (f->title);
22805 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22806 return SSDATA (f->name);
22807 return "Emacs";
22809 case 'f':
22810 obj = BVAR (b, filename);
22811 break;
22813 case 'i':
22815 ptrdiff_t size = ZV - BEGV;
22816 pint2str (decode_mode_spec_buf, width, size);
22817 return decode_mode_spec_buf;
22820 case 'I':
22822 ptrdiff_t size = ZV - BEGV;
22823 pint2hrstr (decode_mode_spec_buf, width, size);
22824 return decode_mode_spec_buf;
22827 case 'l':
22829 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22830 ptrdiff_t topline, nlines, height;
22831 ptrdiff_t junk;
22833 /* %c and %l are ignored in `frame-title-format'. */
22834 if (mode_line_target == MODE_LINE_TITLE)
22835 return "";
22837 startpos = marker_position (w->start);
22838 startpos_byte = marker_byte_position (w->start);
22839 height = WINDOW_TOTAL_LINES (w);
22841 /* If we decided that this buffer isn't suitable for line numbers,
22842 don't forget that too fast. */
22843 if (w->base_line_pos == -1)
22844 goto no_value;
22846 /* If the buffer is very big, don't waste time. */
22847 if (INTEGERP (Vline_number_display_limit)
22848 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22850 w->base_line_pos = 0;
22851 w->base_line_number = 0;
22852 goto no_value;
22855 if (w->base_line_number > 0
22856 && w->base_line_pos > 0
22857 && w->base_line_pos <= startpos)
22859 line = w->base_line_number;
22860 linepos = w->base_line_pos;
22861 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22863 else
22865 line = 1;
22866 linepos = BUF_BEGV (b);
22867 linepos_byte = BUF_BEGV_BYTE (b);
22870 /* Count lines from base line to window start position. */
22871 nlines = display_count_lines (linepos_byte,
22872 startpos_byte,
22873 startpos, &junk);
22875 topline = nlines + line;
22877 /* Determine a new base line, if the old one is too close
22878 or too far away, or if we did not have one.
22879 "Too close" means it's plausible a scroll-down would
22880 go back past it. */
22881 if (startpos == BUF_BEGV (b))
22883 w->base_line_number = topline;
22884 w->base_line_pos = BUF_BEGV (b);
22886 else if (nlines < height + 25 || nlines > height * 3 + 50
22887 || linepos == BUF_BEGV (b))
22889 ptrdiff_t limit = BUF_BEGV (b);
22890 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
22891 ptrdiff_t position;
22892 ptrdiff_t distance =
22893 (height * 2 + 30) * line_number_display_limit_width;
22895 if (startpos - distance > limit)
22897 limit = startpos - distance;
22898 limit_byte = CHAR_TO_BYTE (limit);
22901 nlines = display_count_lines (startpos_byte,
22902 limit_byte,
22903 - (height * 2 + 30),
22904 &position);
22905 /* If we couldn't find the lines we wanted within
22906 line_number_display_limit_width chars per line,
22907 give up on line numbers for this window. */
22908 if (position == limit_byte && limit == startpos - distance)
22910 w->base_line_pos = -1;
22911 w->base_line_number = 0;
22912 goto no_value;
22915 w->base_line_number = topline - nlines;
22916 w->base_line_pos = BYTE_TO_CHAR (position);
22919 /* Now count lines from the start pos to point. */
22920 nlines = display_count_lines (startpos_byte,
22921 PT_BYTE, PT, &junk);
22923 /* Record that we did display the line number. */
22924 line_number_displayed = 1;
22926 /* Make the string to show. */
22927 pint2str (decode_mode_spec_buf, width, topline + nlines);
22928 return decode_mode_spec_buf;
22929 no_value:
22931 char* p = decode_mode_spec_buf;
22932 int pad = width - 2;
22933 while (pad-- > 0)
22934 *p++ = ' ';
22935 *p++ = '?';
22936 *p++ = '?';
22937 *p = '\0';
22938 return decode_mode_spec_buf;
22941 break;
22943 case 'm':
22944 obj = BVAR (b, mode_name);
22945 break;
22947 case 'n':
22948 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22949 return " Narrow";
22950 break;
22952 case 'p':
22954 ptrdiff_t pos = marker_position (w->start);
22955 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22957 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
22959 if (pos <= BUF_BEGV (b))
22960 return "All";
22961 else
22962 return "Bottom";
22964 else if (pos <= BUF_BEGV (b))
22965 return "Top";
22966 else
22968 if (total > 1000000)
22969 /* Do it differently for a large value, to avoid overflow. */
22970 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22971 else
22972 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22973 /* We can't normally display a 3-digit number,
22974 so get us a 2-digit number that is close. */
22975 if (total == 100)
22976 total = 99;
22977 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22978 return decode_mode_spec_buf;
22982 /* Display percentage of size above the bottom of the screen. */
22983 case 'P':
22985 ptrdiff_t toppos = marker_position (w->start);
22986 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
22987 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22989 if (botpos >= BUF_ZV (b))
22991 if (toppos <= BUF_BEGV (b))
22992 return "All";
22993 else
22994 return "Bottom";
22996 else
22998 if (total > 1000000)
22999 /* Do it differently for a large value, to avoid overflow. */
23000 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23001 else
23002 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23003 /* We can't normally display a 3-digit number,
23004 so get us a 2-digit number that is close. */
23005 if (total == 100)
23006 total = 99;
23007 if (toppos <= BUF_BEGV (b))
23008 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23009 else
23010 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23011 return decode_mode_spec_buf;
23015 case 's':
23016 /* status of process */
23017 obj = Fget_buffer_process (Fcurrent_buffer ());
23018 if (NILP (obj))
23019 return "no process";
23020 #ifndef MSDOS
23021 obj = Fsymbol_name (Fprocess_status (obj));
23022 #endif
23023 break;
23025 case '@':
23027 ptrdiff_t count = inhibit_garbage_collection ();
23028 Lisp_Object curdir = BVAR (current_buffer, directory);
23029 Lisp_Object val = Qnil;
23031 if (STRINGP (curdir))
23032 val = call1 (intern ("file-remote-p"), curdir);
23034 unbind_to (count, Qnil);
23036 if (NILP (val))
23037 return "-";
23038 else
23039 return "@";
23042 case 'z':
23043 /* coding-system (not including end-of-line format) */
23044 case 'Z':
23045 /* coding-system (including end-of-line type) */
23047 int eol_flag = (c == 'Z');
23048 char *p = decode_mode_spec_buf;
23050 if (! FRAME_WINDOW_P (f))
23052 /* No need to mention EOL here--the terminal never needs
23053 to do EOL conversion. */
23054 p = decode_mode_spec_coding (CODING_ID_NAME
23055 (FRAME_KEYBOARD_CODING (f)->id),
23056 p, 0);
23057 p = decode_mode_spec_coding (CODING_ID_NAME
23058 (FRAME_TERMINAL_CODING (f)->id),
23059 p, 0);
23061 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23062 p, eol_flag);
23064 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23065 #ifdef subprocesses
23066 obj = Fget_buffer_process (Fcurrent_buffer ());
23067 if (PROCESSP (obj))
23069 p = decode_mode_spec_coding
23070 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23071 p = decode_mode_spec_coding
23072 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23074 #endif /* subprocesses */
23075 #endif /* 0 */
23076 *p = 0;
23077 return decode_mode_spec_buf;
23081 if (STRINGP (obj))
23083 *string = obj;
23084 return SSDATA (obj);
23086 else
23087 return "";
23091 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23092 means count lines back from START_BYTE. But don't go beyond
23093 LIMIT_BYTE. Return the number of lines thus found (always
23094 nonnegative).
23096 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23097 either the position COUNT lines after/before START_BYTE, if we
23098 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23099 COUNT lines. */
23101 static ptrdiff_t
23102 display_count_lines (ptrdiff_t start_byte,
23103 ptrdiff_t limit_byte, ptrdiff_t count,
23104 ptrdiff_t *byte_pos_ptr)
23106 register unsigned char *cursor;
23107 unsigned char *base;
23109 register ptrdiff_t ceiling;
23110 register unsigned char *ceiling_addr;
23111 ptrdiff_t orig_count = count;
23113 /* If we are not in selective display mode,
23114 check only for newlines. */
23115 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23116 && !INTEGERP (BVAR (current_buffer, selective_display)));
23118 if (count > 0)
23120 while (start_byte < limit_byte)
23122 ceiling = BUFFER_CEILING_OF (start_byte);
23123 ceiling = min (limit_byte - 1, ceiling);
23124 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23125 base = (cursor = BYTE_POS_ADDR (start_byte));
23129 if (selective_display)
23131 while (*cursor != '\n' && *cursor != 015
23132 && ++cursor != ceiling_addr)
23133 continue;
23134 if (cursor == ceiling_addr)
23135 break;
23137 else
23139 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23140 if (! cursor)
23141 break;
23144 cursor++;
23146 if (--count == 0)
23148 start_byte += cursor - base;
23149 *byte_pos_ptr = start_byte;
23150 return orig_count;
23153 while (cursor < ceiling_addr);
23155 start_byte += ceiling_addr - base;
23158 else
23160 while (start_byte > limit_byte)
23162 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23163 ceiling = max (limit_byte, ceiling);
23164 ceiling_addr = BYTE_POS_ADDR (ceiling);
23165 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23166 while (1)
23168 if (selective_display)
23170 while (--cursor >= ceiling_addr
23171 && *cursor != '\n' && *cursor != 015)
23172 continue;
23173 if (cursor < ceiling_addr)
23174 break;
23176 else
23178 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23179 if (! cursor)
23180 break;
23183 if (++count == 0)
23185 start_byte += cursor - base + 1;
23186 *byte_pos_ptr = start_byte;
23187 /* When scanning backwards, we should
23188 not count the newline posterior to which we stop. */
23189 return - orig_count - 1;
23192 start_byte += ceiling_addr - base;
23196 *byte_pos_ptr = limit_byte;
23198 if (count < 0)
23199 return - orig_count + count;
23200 return orig_count - count;
23206 /***********************************************************************
23207 Displaying strings
23208 ***********************************************************************/
23210 /* Display a NUL-terminated string, starting with index START.
23212 If STRING is non-null, display that C string. Otherwise, the Lisp
23213 string LISP_STRING is displayed. There's a case that STRING is
23214 non-null and LISP_STRING is not nil. It means STRING is a string
23215 data of LISP_STRING. In that case, we display LISP_STRING while
23216 ignoring its text properties.
23218 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23219 FACE_STRING. Display STRING or LISP_STRING with the face at
23220 FACE_STRING_POS in FACE_STRING:
23222 Display the string in the environment given by IT, but use the
23223 standard display table, temporarily.
23225 FIELD_WIDTH is the minimum number of output glyphs to produce.
23226 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23227 with spaces. If STRING has more characters, more than FIELD_WIDTH
23228 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23230 PRECISION is the maximum number of characters to output from
23231 STRING. PRECISION < 0 means don't truncate the string.
23233 This is roughly equivalent to printf format specifiers:
23235 FIELD_WIDTH PRECISION PRINTF
23236 ----------------------------------------
23237 -1 -1 %s
23238 -1 10 %.10s
23239 10 -1 %10s
23240 20 10 %20.10s
23242 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23243 display them, and < 0 means obey the current buffer's value of
23244 enable_multibyte_characters.
23246 Value is the number of columns displayed. */
23248 static int
23249 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23250 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23251 int field_width, int precision, int max_x, int multibyte)
23253 int hpos_at_start = it->hpos;
23254 int saved_face_id = it->face_id;
23255 struct glyph_row *row = it->glyph_row;
23256 ptrdiff_t it_charpos;
23258 /* Initialize the iterator IT for iteration over STRING beginning
23259 with index START. */
23260 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23261 precision, field_width, multibyte);
23262 if (string && STRINGP (lisp_string))
23263 /* LISP_STRING is the one returned by decode_mode_spec. We should
23264 ignore its text properties. */
23265 it->stop_charpos = it->end_charpos;
23267 /* If displaying STRING, set up the face of the iterator from
23268 FACE_STRING, if that's given. */
23269 if (STRINGP (face_string))
23271 ptrdiff_t endptr;
23272 struct face *face;
23274 it->face_id
23275 = face_at_string_position (it->w, face_string, face_string_pos,
23276 0, &endptr, it->base_face_id, 0);
23277 face = FACE_FROM_ID (it->f, it->face_id);
23278 it->face_box_p = face->box != FACE_NO_BOX;
23281 /* Set max_x to the maximum allowed X position. Don't let it go
23282 beyond the right edge of the window. */
23283 if (max_x <= 0)
23284 max_x = it->last_visible_x;
23285 else
23286 max_x = min (max_x, it->last_visible_x);
23288 /* Skip over display elements that are not visible. because IT->w is
23289 hscrolled. */
23290 if (it->current_x < it->first_visible_x)
23291 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23292 MOVE_TO_POS | MOVE_TO_X);
23294 row->ascent = it->max_ascent;
23295 row->height = it->max_ascent + it->max_descent;
23296 row->phys_ascent = it->max_phys_ascent;
23297 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23298 row->extra_line_spacing = it->max_extra_line_spacing;
23300 if (STRINGP (it->string))
23301 it_charpos = IT_STRING_CHARPOS (*it);
23302 else
23303 it_charpos = IT_CHARPOS (*it);
23305 /* This condition is for the case that we are called with current_x
23306 past last_visible_x. */
23307 while (it->current_x < max_x)
23309 int x_before, x, n_glyphs_before, i, nglyphs;
23311 /* Get the next display element. */
23312 if (!get_next_display_element (it))
23313 break;
23315 /* Produce glyphs. */
23316 x_before = it->current_x;
23317 n_glyphs_before = row->used[TEXT_AREA];
23318 PRODUCE_GLYPHS (it);
23320 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23321 i = 0;
23322 x = x_before;
23323 while (i < nglyphs)
23325 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23327 if (it->line_wrap != TRUNCATE
23328 && x + glyph->pixel_width > max_x)
23330 /* End of continued line or max_x reached. */
23331 if (CHAR_GLYPH_PADDING_P (*glyph))
23333 /* A wide character is unbreakable. */
23334 if (row->reversed_p)
23335 unproduce_glyphs (it, row->used[TEXT_AREA]
23336 - n_glyphs_before);
23337 row->used[TEXT_AREA] = n_glyphs_before;
23338 it->current_x = x_before;
23340 else
23342 if (row->reversed_p)
23343 unproduce_glyphs (it, row->used[TEXT_AREA]
23344 - (n_glyphs_before + i));
23345 row->used[TEXT_AREA] = n_glyphs_before + i;
23346 it->current_x = x;
23348 break;
23350 else if (x + glyph->pixel_width >= it->first_visible_x)
23352 /* Glyph is at least partially visible. */
23353 ++it->hpos;
23354 if (x < it->first_visible_x)
23355 row->x = x - it->first_visible_x;
23357 else
23359 /* Glyph is off the left margin of the display area.
23360 Should not happen. */
23361 emacs_abort ();
23364 row->ascent = max (row->ascent, it->max_ascent);
23365 row->height = max (row->height, it->max_ascent + it->max_descent);
23366 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23367 row->phys_height = max (row->phys_height,
23368 it->max_phys_ascent + it->max_phys_descent);
23369 row->extra_line_spacing = max (row->extra_line_spacing,
23370 it->max_extra_line_spacing);
23371 x += glyph->pixel_width;
23372 ++i;
23375 /* Stop if max_x reached. */
23376 if (i < nglyphs)
23377 break;
23379 /* Stop at line ends. */
23380 if (ITERATOR_AT_END_OF_LINE_P (it))
23382 it->continuation_lines_width = 0;
23383 break;
23386 set_iterator_to_next (it, 1);
23387 if (STRINGP (it->string))
23388 it_charpos = IT_STRING_CHARPOS (*it);
23389 else
23390 it_charpos = IT_CHARPOS (*it);
23392 /* Stop if truncating at the right edge. */
23393 if (it->line_wrap == TRUNCATE
23394 && it->current_x >= it->last_visible_x)
23396 /* Add truncation mark, but don't do it if the line is
23397 truncated at a padding space. */
23398 if (it_charpos < it->string_nchars)
23400 if (!FRAME_WINDOW_P (it->f))
23402 int ii, n;
23404 if (it->current_x > it->last_visible_x)
23406 if (!row->reversed_p)
23408 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23409 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23410 break;
23412 else
23414 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23415 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23416 break;
23417 unproduce_glyphs (it, ii + 1);
23418 ii = row->used[TEXT_AREA] - (ii + 1);
23420 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23422 row->used[TEXT_AREA] = ii;
23423 produce_special_glyphs (it, IT_TRUNCATION);
23426 produce_special_glyphs (it, IT_TRUNCATION);
23428 row->truncated_on_right_p = 1;
23430 break;
23434 /* Maybe insert a truncation at the left. */
23435 if (it->first_visible_x
23436 && it_charpos > 0)
23438 if (!FRAME_WINDOW_P (it->f)
23439 || (row->reversed_p
23440 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23441 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23442 insert_left_trunc_glyphs (it);
23443 row->truncated_on_left_p = 1;
23446 it->face_id = saved_face_id;
23448 /* Value is number of columns displayed. */
23449 return it->hpos - hpos_at_start;
23454 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23455 appears as an element of LIST or as the car of an element of LIST.
23456 If PROPVAL is a list, compare each element against LIST in that
23457 way, and return 1/2 if any element of PROPVAL is found in LIST.
23458 Otherwise return 0. This function cannot quit.
23459 The return value is 2 if the text is invisible but with an ellipsis
23460 and 1 if it's invisible and without an ellipsis. */
23463 invisible_p (register Lisp_Object propval, Lisp_Object list)
23465 register Lisp_Object tail, proptail;
23467 for (tail = list; CONSP (tail); tail = XCDR (tail))
23469 register Lisp_Object tem;
23470 tem = XCAR (tail);
23471 if (EQ (propval, tem))
23472 return 1;
23473 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23474 return NILP (XCDR (tem)) ? 1 : 2;
23477 if (CONSP (propval))
23479 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23481 Lisp_Object propelt;
23482 propelt = XCAR (proptail);
23483 for (tail = list; CONSP (tail); tail = XCDR (tail))
23485 register Lisp_Object tem;
23486 tem = XCAR (tail);
23487 if (EQ (propelt, tem))
23488 return 1;
23489 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23490 return NILP (XCDR (tem)) ? 1 : 2;
23495 return 0;
23498 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23499 doc: /* Non-nil if the property makes the text invisible.
23500 POS-OR-PROP can be a marker or number, in which case it is taken to be
23501 a position in the current buffer and the value of the `invisible' property
23502 is checked; or it can be some other value, which is then presumed to be the
23503 value of the `invisible' property of the text of interest.
23504 The non-nil value returned can be t for truly invisible text or something
23505 else if the text is replaced by an ellipsis. */)
23506 (Lisp_Object pos_or_prop)
23508 Lisp_Object prop
23509 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23510 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23511 : pos_or_prop);
23512 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23513 return (invis == 0 ? Qnil
23514 : invis == 1 ? Qt
23515 : make_number (invis));
23518 /* Calculate a width or height in pixels from a specification using
23519 the following elements:
23521 SPEC ::=
23522 NUM - a (fractional) multiple of the default font width/height
23523 (NUM) - specifies exactly NUM pixels
23524 UNIT - a fixed number of pixels, see below.
23525 ELEMENT - size of a display element in pixels, see below.
23526 (NUM . SPEC) - equals NUM * SPEC
23527 (+ SPEC SPEC ...) - add pixel values
23528 (- SPEC SPEC ...) - subtract pixel values
23529 (- SPEC) - negate pixel value
23531 NUM ::=
23532 INT or FLOAT - a number constant
23533 SYMBOL - use symbol's (buffer local) variable binding.
23535 UNIT ::=
23536 in - pixels per inch *)
23537 mm - pixels per 1/1000 meter *)
23538 cm - pixels per 1/100 meter *)
23539 width - width of current font in pixels.
23540 height - height of current font in pixels.
23542 *) using the ratio(s) defined in display-pixels-per-inch.
23544 ELEMENT ::=
23546 left-fringe - left fringe width in pixels
23547 right-fringe - right fringe width in pixels
23549 left-margin - left margin width in pixels
23550 right-margin - right margin width in pixels
23552 scroll-bar - scroll-bar area width in pixels
23554 Examples:
23556 Pixels corresponding to 5 inches:
23557 (5 . in)
23559 Total width of non-text areas on left side of window (if scroll-bar is on left):
23560 '(space :width (+ left-fringe left-margin scroll-bar))
23562 Align to first text column (in header line):
23563 '(space :align-to 0)
23565 Align to middle of text area minus half the width of variable `my-image'
23566 containing a loaded image:
23567 '(space :align-to (0.5 . (- text my-image)))
23569 Width of left margin minus width of 1 character in the default font:
23570 '(space :width (- left-margin 1))
23572 Width of left margin minus width of 2 characters in the current font:
23573 '(space :width (- left-margin (2 . width)))
23575 Center 1 character over left-margin (in header line):
23576 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23578 Different ways to express width of left fringe plus left margin minus one pixel:
23579 '(space :width (- (+ left-fringe left-margin) (1)))
23580 '(space :width (+ left-fringe left-margin (- (1))))
23581 '(space :width (+ left-fringe left-margin (-1)))
23585 static int
23586 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23587 struct font *font, int width_p, int *align_to)
23589 double pixels;
23591 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23592 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23594 if (NILP (prop))
23595 return OK_PIXELS (0);
23597 eassert (FRAME_LIVE_P (it->f));
23599 if (SYMBOLP (prop))
23601 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23603 char *unit = SSDATA (SYMBOL_NAME (prop));
23605 if (unit[0] == 'i' && unit[1] == 'n')
23606 pixels = 1.0;
23607 else if (unit[0] == 'm' && unit[1] == 'm')
23608 pixels = 25.4;
23609 else if (unit[0] == 'c' && unit[1] == 'm')
23610 pixels = 2.54;
23611 else
23612 pixels = 0;
23613 if (pixels > 0)
23615 double ppi = (width_p ? FRAME_RES_X (it->f)
23616 : FRAME_RES_Y (it->f));
23618 if (ppi > 0)
23619 return OK_PIXELS (ppi / pixels);
23620 return 0;
23624 #ifdef HAVE_WINDOW_SYSTEM
23625 if (EQ (prop, Qheight))
23626 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23627 if (EQ (prop, Qwidth))
23628 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23629 #else
23630 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23631 return OK_PIXELS (1);
23632 #endif
23634 if (EQ (prop, Qtext))
23635 return OK_PIXELS (width_p
23636 ? window_box_width (it->w, TEXT_AREA)
23637 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23639 if (align_to && *align_to < 0)
23641 *res = 0;
23642 if (EQ (prop, Qleft))
23643 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23644 if (EQ (prop, Qright))
23645 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23646 if (EQ (prop, Qcenter))
23647 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23648 + window_box_width (it->w, TEXT_AREA) / 2);
23649 if (EQ (prop, Qleft_fringe))
23650 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23651 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23652 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23653 if (EQ (prop, Qright_fringe))
23654 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23655 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23656 : window_box_right_offset (it->w, TEXT_AREA));
23657 if (EQ (prop, Qleft_margin))
23658 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23659 if (EQ (prop, Qright_margin))
23660 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23661 if (EQ (prop, Qscroll_bar))
23662 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23664 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23665 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23666 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23667 : 0)));
23669 else
23671 if (EQ (prop, Qleft_fringe))
23672 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23673 if (EQ (prop, Qright_fringe))
23674 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23675 if (EQ (prop, Qleft_margin))
23676 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23677 if (EQ (prop, Qright_margin))
23678 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23679 if (EQ (prop, Qscroll_bar))
23680 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23683 prop = buffer_local_value_1 (prop, it->w->contents);
23684 if (EQ (prop, Qunbound))
23685 prop = Qnil;
23688 if (INTEGERP (prop) || FLOATP (prop))
23690 int base_unit = (width_p
23691 ? FRAME_COLUMN_WIDTH (it->f)
23692 : FRAME_LINE_HEIGHT (it->f));
23693 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23696 if (CONSP (prop))
23698 Lisp_Object car = XCAR (prop);
23699 Lisp_Object cdr = XCDR (prop);
23701 if (SYMBOLP (car))
23703 #ifdef HAVE_WINDOW_SYSTEM
23704 if (FRAME_WINDOW_P (it->f)
23705 && valid_image_p (prop))
23707 ptrdiff_t id = lookup_image (it->f, prop);
23708 struct image *img = IMAGE_FROM_ID (it->f, id);
23710 return OK_PIXELS (width_p ? img->width : img->height);
23712 #endif
23713 if (EQ (car, Qplus) || EQ (car, Qminus))
23715 int first = 1;
23716 double px;
23718 pixels = 0;
23719 while (CONSP (cdr))
23721 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23722 font, width_p, align_to))
23723 return 0;
23724 if (first)
23725 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23726 else
23727 pixels += px;
23728 cdr = XCDR (cdr);
23730 if (EQ (car, Qminus))
23731 pixels = -pixels;
23732 return OK_PIXELS (pixels);
23735 car = buffer_local_value_1 (car, it->w->contents);
23736 if (EQ (car, Qunbound))
23737 car = Qnil;
23740 if (INTEGERP (car) || FLOATP (car))
23742 double fact;
23743 pixels = XFLOATINT (car);
23744 if (NILP (cdr))
23745 return OK_PIXELS (pixels);
23746 if (calc_pixel_width_or_height (&fact, it, cdr,
23747 font, width_p, align_to))
23748 return OK_PIXELS (pixels * fact);
23749 return 0;
23752 return 0;
23755 return 0;
23759 /***********************************************************************
23760 Glyph Display
23761 ***********************************************************************/
23763 #ifdef HAVE_WINDOW_SYSTEM
23765 #ifdef GLYPH_DEBUG
23767 void
23768 dump_glyph_string (struct glyph_string *s)
23770 fprintf (stderr, "glyph string\n");
23771 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23772 s->x, s->y, s->width, s->height);
23773 fprintf (stderr, " ybase = %d\n", s->ybase);
23774 fprintf (stderr, " hl = %d\n", s->hl);
23775 fprintf (stderr, " left overhang = %d, right = %d\n",
23776 s->left_overhang, s->right_overhang);
23777 fprintf (stderr, " nchars = %d\n", s->nchars);
23778 fprintf (stderr, " extends to end of line = %d\n",
23779 s->extends_to_end_of_line_p);
23780 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23781 fprintf (stderr, " bg width = %d\n", s->background_width);
23784 #endif /* GLYPH_DEBUG */
23786 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23787 of XChar2b structures for S; it can't be allocated in
23788 init_glyph_string because it must be allocated via `alloca'. W
23789 is the window on which S is drawn. ROW and AREA are the glyph row
23790 and area within the row from which S is constructed. START is the
23791 index of the first glyph structure covered by S. HL is a
23792 face-override for drawing S. */
23794 #ifdef HAVE_NTGUI
23795 #define OPTIONAL_HDC(hdc) HDC hdc,
23796 #define DECLARE_HDC(hdc) HDC hdc;
23797 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23798 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23799 #endif
23801 #ifndef OPTIONAL_HDC
23802 #define OPTIONAL_HDC(hdc)
23803 #define DECLARE_HDC(hdc)
23804 #define ALLOCATE_HDC(hdc, f)
23805 #define RELEASE_HDC(hdc, f)
23806 #endif
23808 static void
23809 init_glyph_string (struct glyph_string *s,
23810 OPTIONAL_HDC (hdc)
23811 XChar2b *char2b, struct window *w, struct glyph_row *row,
23812 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23814 memset (s, 0, sizeof *s);
23815 s->w = w;
23816 s->f = XFRAME (w->frame);
23817 #ifdef HAVE_NTGUI
23818 s->hdc = hdc;
23819 #endif
23820 s->display = FRAME_X_DISPLAY (s->f);
23821 s->window = FRAME_X_WINDOW (s->f);
23822 s->char2b = char2b;
23823 s->hl = hl;
23824 s->row = row;
23825 s->area = area;
23826 s->first_glyph = row->glyphs[area] + start;
23827 s->height = row->height;
23828 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23829 s->ybase = s->y + row->ascent;
23833 /* Append the list of glyph strings with head H and tail T to the list
23834 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23836 static void
23837 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23838 struct glyph_string *h, struct glyph_string *t)
23840 if (h)
23842 if (*head)
23843 (*tail)->next = h;
23844 else
23845 *head = h;
23846 h->prev = *tail;
23847 *tail = t;
23852 /* Prepend the list of glyph strings with head H and tail T to the
23853 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23854 result. */
23856 static void
23857 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23858 struct glyph_string *h, struct glyph_string *t)
23860 if (h)
23862 if (*head)
23863 (*head)->prev = t;
23864 else
23865 *tail = t;
23866 t->next = *head;
23867 *head = h;
23872 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
23873 Set *HEAD and *TAIL to the resulting list. */
23875 static void
23876 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
23877 struct glyph_string *s)
23879 s->next = s->prev = NULL;
23880 append_glyph_string_lists (head, tail, s, s);
23884 /* Get face and two-byte form of character C in face FACE_ID on frame F.
23885 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
23886 make sure that X resources for the face returned are allocated.
23887 Value is a pointer to a realized face that is ready for display if
23888 DISPLAY_P is non-zero. */
23890 static struct face *
23891 get_char_face_and_encoding (struct frame *f, int c, int face_id,
23892 XChar2b *char2b, int display_p)
23894 struct face *face = FACE_FROM_ID (f, face_id);
23895 unsigned code = 0;
23897 if (face->font)
23899 code = face->font->driver->encode_char (face->font, c);
23901 if (code == FONT_INVALID_CODE)
23902 code = 0;
23904 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23906 /* Make sure X resources of the face are allocated. */
23907 #ifdef HAVE_X_WINDOWS
23908 if (display_p)
23909 #endif
23911 eassert (face != NULL);
23912 PREPARE_FACE_FOR_DISPLAY (f, face);
23915 return face;
23919 /* Get face and two-byte form of character glyph GLYPH on frame F.
23920 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
23921 a pointer to a realized face that is ready for display. */
23923 static struct face *
23924 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
23925 XChar2b *char2b, int *two_byte_p)
23927 struct face *face;
23928 unsigned code = 0;
23930 eassert (glyph->type == CHAR_GLYPH);
23931 face = FACE_FROM_ID (f, glyph->face_id);
23933 /* Make sure X resources of the face are allocated. */
23934 eassert (face != NULL);
23935 PREPARE_FACE_FOR_DISPLAY (f, face);
23937 if (two_byte_p)
23938 *two_byte_p = 0;
23940 if (face->font)
23942 if (CHAR_BYTE8_P (glyph->u.ch))
23943 code = CHAR_TO_BYTE8 (glyph->u.ch);
23944 else
23945 code = face->font->driver->encode_char (face->font, glyph->u.ch);
23947 if (code == FONT_INVALID_CODE)
23948 code = 0;
23951 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23952 return face;
23956 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23957 Return 1 if FONT has a glyph for C, otherwise return 0. */
23959 static int
23960 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23962 unsigned code;
23964 if (CHAR_BYTE8_P (c))
23965 code = CHAR_TO_BYTE8 (c);
23966 else
23967 code = font->driver->encode_char (font, c);
23969 if (code == FONT_INVALID_CODE)
23970 return 0;
23971 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23972 return 1;
23976 /* Fill glyph string S with composition components specified by S->cmp.
23978 BASE_FACE is the base face of the composition.
23979 S->cmp_from is the index of the first component for S.
23981 OVERLAPS non-zero means S should draw the foreground only, and use
23982 its physical height for clipping. See also draw_glyphs.
23984 Value is the index of a component not in S. */
23986 static int
23987 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23988 int overlaps)
23990 int i;
23991 /* For all glyphs of this composition, starting at the offset
23992 S->cmp_from, until we reach the end of the definition or encounter a
23993 glyph that requires the different face, add it to S. */
23994 struct face *face;
23996 eassert (s);
23998 s->for_overlaps = overlaps;
23999 s->face = NULL;
24000 s->font = NULL;
24001 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24003 int c = COMPOSITION_GLYPH (s->cmp, i);
24005 /* TAB in a composition means display glyphs with padding space
24006 on the left or right. */
24007 if (c != '\t')
24009 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24010 -1, Qnil);
24012 face = get_char_face_and_encoding (s->f, c, face_id,
24013 s->char2b + i, 1);
24014 if (face)
24016 if (! s->face)
24018 s->face = face;
24019 s->font = s->face->font;
24021 else if (s->face != face)
24022 break;
24025 ++s->nchars;
24027 s->cmp_to = i;
24029 if (s->face == NULL)
24031 s->face = base_face->ascii_face;
24032 s->font = s->face->font;
24035 /* All glyph strings for the same composition has the same width,
24036 i.e. the width set for the first component of the composition. */
24037 s->width = s->first_glyph->pixel_width;
24039 /* If the specified font could not be loaded, use the frame's
24040 default font, but record the fact that we couldn't load it in
24041 the glyph string so that we can draw rectangles for the
24042 characters of the glyph string. */
24043 if (s->font == NULL)
24045 s->font_not_found_p = 1;
24046 s->font = FRAME_FONT (s->f);
24049 /* Adjust base line for subscript/superscript text. */
24050 s->ybase += s->first_glyph->voffset;
24052 /* This glyph string must always be drawn with 16-bit functions. */
24053 s->two_byte_p = 1;
24055 return s->cmp_to;
24058 static int
24059 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24060 int start, int end, int overlaps)
24062 struct glyph *glyph, *last;
24063 Lisp_Object lgstring;
24064 int i;
24066 s->for_overlaps = overlaps;
24067 glyph = s->row->glyphs[s->area] + start;
24068 last = s->row->glyphs[s->area] + end;
24069 s->cmp_id = glyph->u.cmp.id;
24070 s->cmp_from = glyph->slice.cmp.from;
24071 s->cmp_to = glyph->slice.cmp.to + 1;
24072 s->face = FACE_FROM_ID (s->f, face_id);
24073 lgstring = composition_gstring_from_id (s->cmp_id);
24074 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24075 glyph++;
24076 while (glyph < last
24077 && glyph->u.cmp.automatic
24078 && glyph->u.cmp.id == s->cmp_id
24079 && s->cmp_to == glyph->slice.cmp.from)
24080 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24082 for (i = s->cmp_from; i < s->cmp_to; i++)
24084 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24085 unsigned code = LGLYPH_CODE (lglyph);
24087 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24089 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24090 return glyph - s->row->glyphs[s->area];
24094 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24095 See the comment of fill_glyph_string for arguments.
24096 Value is the index of the first glyph not in S. */
24099 static int
24100 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24101 int start, int end, int overlaps)
24103 struct glyph *glyph, *last;
24104 int voffset;
24106 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24107 s->for_overlaps = overlaps;
24108 glyph = s->row->glyphs[s->area] + start;
24109 last = s->row->glyphs[s->area] + end;
24110 voffset = glyph->voffset;
24111 s->face = FACE_FROM_ID (s->f, face_id);
24112 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24113 s->nchars = 1;
24114 s->width = glyph->pixel_width;
24115 glyph++;
24116 while (glyph < last
24117 && glyph->type == GLYPHLESS_GLYPH
24118 && glyph->voffset == voffset
24119 && glyph->face_id == face_id)
24121 s->nchars++;
24122 s->width += glyph->pixel_width;
24123 glyph++;
24125 s->ybase += voffset;
24126 return glyph - s->row->glyphs[s->area];
24130 /* Fill glyph string S from a sequence of character glyphs.
24132 FACE_ID is the face id of the string. START is the index of the
24133 first glyph to consider, END is the index of the last + 1.
24134 OVERLAPS non-zero means S should draw the foreground only, and use
24135 its physical height for clipping. See also draw_glyphs.
24137 Value is the index of the first glyph not in S. */
24139 static int
24140 fill_glyph_string (struct glyph_string *s, int face_id,
24141 int start, int end, int overlaps)
24143 struct glyph *glyph, *last;
24144 int voffset;
24145 int glyph_not_available_p;
24147 eassert (s->f == XFRAME (s->w->frame));
24148 eassert (s->nchars == 0);
24149 eassert (start >= 0 && end > start);
24151 s->for_overlaps = overlaps;
24152 glyph = s->row->glyphs[s->area] + start;
24153 last = s->row->glyphs[s->area] + end;
24154 voffset = glyph->voffset;
24155 s->padding_p = glyph->padding_p;
24156 glyph_not_available_p = glyph->glyph_not_available_p;
24158 while (glyph < last
24159 && glyph->type == CHAR_GLYPH
24160 && glyph->voffset == voffset
24161 /* Same face id implies same font, nowadays. */
24162 && glyph->face_id == face_id
24163 && glyph->glyph_not_available_p == glyph_not_available_p)
24165 int two_byte_p;
24167 s->face = get_glyph_face_and_encoding (s->f, glyph,
24168 s->char2b + s->nchars,
24169 &two_byte_p);
24170 s->two_byte_p = two_byte_p;
24171 ++s->nchars;
24172 eassert (s->nchars <= end - start);
24173 s->width += glyph->pixel_width;
24174 if (glyph++->padding_p != s->padding_p)
24175 break;
24178 s->font = s->face->font;
24180 /* If the specified font could not be loaded, use the frame's font,
24181 but record the fact that we couldn't load it in
24182 S->font_not_found_p so that we can draw rectangles for the
24183 characters of the glyph string. */
24184 if (s->font == NULL || glyph_not_available_p)
24186 s->font_not_found_p = 1;
24187 s->font = FRAME_FONT (s->f);
24190 /* Adjust base line for subscript/superscript text. */
24191 s->ybase += voffset;
24193 eassert (s->face && s->face->gc);
24194 return glyph - s->row->glyphs[s->area];
24198 /* Fill glyph string S from image glyph S->first_glyph. */
24200 static void
24201 fill_image_glyph_string (struct glyph_string *s)
24203 eassert (s->first_glyph->type == IMAGE_GLYPH);
24204 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24205 eassert (s->img);
24206 s->slice = s->first_glyph->slice.img;
24207 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24208 s->font = s->face->font;
24209 s->width = s->first_glyph->pixel_width;
24211 /* Adjust base line for subscript/superscript text. */
24212 s->ybase += s->first_glyph->voffset;
24216 /* Fill glyph string S from a sequence of stretch glyphs.
24218 START is the index of the first glyph to consider,
24219 END is the index of the last + 1.
24221 Value is the index of the first glyph not in S. */
24223 static int
24224 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24226 struct glyph *glyph, *last;
24227 int voffset, face_id;
24229 eassert (s->first_glyph->type == STRETCH_GLYPH);
24231 glyph = s->row->glyphs[s->area] + start;
24232 last = s->row->glyphs[s->area] + end;
24233 face_id = glyph->face_id;
24234 s->face = FACE_FROM_ID (s->f, face_id);
24235 s->font = s->face->font;
24236 s->width = glyph->pixel_width;
24237 s->nchars = 1;
24238 voffset = glyph->voffset;
24240 for (++glyph;
24241 (glyph < last
24242 && glyph->type == STRETCH_GLYPH
24243 && glyph->voffset == voffset
24244 && glyph->face_id == face_id);
24245 ++glyph)
24246 s->width += glyph->pixel_width;
24248 /* Adjust base line for subscript/superscript text. */
24249 s->ybase += voffset;
24251 /* The case that face->gc == 0 is handled when drawing the glyph
24252 string by calling PREPARE_FACE_FOR_DISPLAY. */
24253 eassert (s->face);
24254 return glyph - s->row->glyphs[s->area];
24257 static struct font_metrics *
24258 get_per_char_metric (struct font *font, XChar2b *char2b)
24260 static struct font_metrics metrics;
24261 unsigned code;
24263 if (! font)
24264 return NULL;
24265 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24266 if (code == FONT_INVALID_CODE)
24267 return NULL;
24268 font->driver->text_extents (font, &code, 1, &metrics);
24269 return &metrics;
24272 /* EXPORT for RIF:
24273 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24274 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24275 assumed to be zero. */
24277 void
24278 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24280 *left = *right = 0;
24282 if (glyph->type == CHAR_GLYPH)
24284 struct face *face;
24285 XChar2b char2b;
24286 struct font_metrics *pcm;
24288 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24289 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24291 if (pcm->rbearing > pcm->width)
24292 *right = pcm->rbearing - pcm->width;
24293 if (pcm->lbearing < 0)
24294 *left = -pcm->lbearing;
24297 else if (glyph->type == COMPOSITE_GLYPH)
24299 if (! glyph->u.cmp.automatic)
24301 struct composition *cmp = composition_table[glyph->u.cmp.id];
24303 if (cmp->rbearing > cmp->pixel_width)
24304 *right = cmp->rbearing - cmp->pixel_width;
24305 if (cmp->lbearing < 0)
24306 *left = - cmp->lbearing;
24308 else
24310 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24311 struct font_metrics metrics;
24313 composition_gstring_width (gstring, glyph->slice.cmp.from,
24314 glyph->slice.cmp.to + 1, &metrics);
24315 if (metrics.rbearing > metrics.width)
24316 *right = metrics.rbearing - metrics.width;
24317 if (metrics.lbearing < 0)
24318 *left = - metrics.lbearing;
24324 /* Return the index of the first glyph preceding glyph string S that
24325 is overwritten by S because of S's left overhang. Value is -1
24326 if no glyphs are overwritten. */
24328 static int
24329 left_overwritten (struct glyph_string *s)
24331 int k;
24333 if (s->left_overhang)
24335 int x = 0, i;
24336 struct glyph *glyphs = s->row->glyphs[s->area];
24337 int first = s->first_glyph - glyphs;
24339 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24340 x -= glyphs[i].pixel_width;
24342 k = i + 1;
24344 else
24345 k = -1;
24347 return k;
24351 /* Return the index of the first glyph preceding glyph string S that
24352 is overwriting S because of its right overhang. Value is -1 if no
24353 glyph in front of S overwrites S. */
24355 static int
24356 left_overwriting (struct glyph_string *s)
24358 int i, k, x;
24359 struct glyph *glyphs = s->row->glyphs[s->area];
24360 int first = s->first_glyph - glyphs;
24362 k = -1;
24363 x = 0;
24364 for (i = first - 1; i >= 0; --i)
24366 int left, right;
24367 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24368 if (x + right > 0)
24369 k = i;
24370 x -= glyphs[i].pixel_width;
24373 return k;
24377 /* Return the index of the last glyph following glyph string S that is
24378 overwritten by S because of S's right overhang. Value is -1 if
24379 no such glyph is found. */
24381 static int
24382 right_overwritten (struct glyph_string *s)
24384 int k = -1;
24386 if (s->right_overhang)
24388 int x = 0, i;
24389 struct glyph *glyphs = s->row->glyphs[s->area];
24390 int first = (s->first_glyph - glyphs
24391 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24392 int end = s->row->used[s->area];
24394 for (i = first; i < end && s->right_overhang > x; ++i)
24395 x += glyphs[i].pixel_width;
24397 k = i;
24400 return k;
24404 /* Return the index of the last glyph following glyph string S that
24405 overwrites S because of its left overhang. Value is negative
24406 if no such glyph is found. */
24408 static int
24409 right_overwriting (struct glyph_string *s)
24411 int i, k, x;
24412 int end = s->row->used[s->area];
24413 struct glyph *glyphs = s->row->glyphs[s->area];
24414 int first = (s->first_glyph - glyphs
24415 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24417 k = -1;
24418 x = 0;
24419 for (i = first; i < end; ++i)
24421 int left, right;
24422 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24423 if (x - left < 0)
24424 k = i;
24425 x += glyphs[i].pixel_width;
24428 return k;
24432 /* Set background width of glyph string S. START is the index of the
24433 first glyph following S. LAST_X is the right-most x-position + 1
24434 in the drawing area. */
24436 static void
24437 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24439 /* If the face of this glyph string has to be drawn to the end of
24440 the drawing area, set S->extends_to_end_of_line_p. */
24442 if (start == s->row->used[s->area]
24443 && ((s->row->fill_line_p
24444 && (s->hl == DRAW_NORMAL_TEXT
24445 || s->hl == DRAW_IMAGE_RAISED
24446 || s->hl == DRAW_IMAGE_SUNKEN))
24447 || s->hl == DRAW_MOUSE_FACE))
24448 s->extends_to_end_of_line_p = 1;
24450 /* If S extends its face to the end of the line, set its
24451 background_width to the distance to the right edge of the drawing
24452 area. */
24453 if (s->extends_to_end_of_line_p)
24454 s->background_width = last_x - s->x + 1;
24455 else
24456 s->background_width = s->width;
24460 /* Compute overhangs and x-positions for glyph string S and its
24461 predecessors, or successors. X is the starting x-position for S.
24462 BACKWARD_P non-zero means process predecessors. */
24464 static void
24465 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24467 if (backward_p)
24469 while (s)
24471 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24472 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24473 x -= s->width;
24474 s->x = x;
24475 s = s->prev;
24478 else
24480 while (s)
24482 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24483 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24484 s->x = x;
24485 x += s->width;
24486 s = s->next;
24493 /* The following macros are only called from draw_glyphs below.
24494 They reference the following parameters of that function directly:
24495 `w', `row', `area', and `overlap_p'
24496 as well as the following local variables:
24497 `s', `f', and `hdc' (in W32) */
24499 #ifdef HAVE_NTGUI
24500 /* On W32, silently add local `hdc' variable to argument list of
24501 init_glyph_string. */
24502 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24503 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24504 #else
24505 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24506 init_glyph_string (s, char2b, w, row, area, start, hl)
24507 #endif
24509 /* Add a glyph string for a stretch glyph to the list of strings
24510 between HEAD and TAIL. START is the index of the stretch glyph in
24511 row area AREA of glyph row ROW. END is the index of the last glyph
24512 in that glyph row area. X is the current output position assigned
24513 to the new glyph string constructed. HL overrides that face of the
24514 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24515 is the right-most x-position of the drawing area. */
24517 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24518 and below -- keep them on one line. */
24519 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24520 do \
24522 s = alloca (sizeof *s); \
24523 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24524 START = fill_stretch_glyph_string (s, START, END); \
24525 append_glyph_string (&HEAD, &TAIL, s); \
24526 s->x = (X); \
24528 while (0)
24531 /* Add a glyph string for an image glyph to the list of strings
24532 between HEAD and TAIL. START is the index of the image glyph in
24533 row area AREA of glyph row ROW. END is the index of the last glyph
24534 in that glyph row area. X is the current output position assigned
24535 to the new glyph string constructed. HL overrides that face of the
24536 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24537 is the right-most x-position of the drawing area. */
24539 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24540 do \
24542 s = alloca (sizeof *s); \
24543 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24544 fill_image_glyph_string (s); \
24545 append_glyph_string (&HEAD, &TAIL, s); \
24546 ++START; \
24547 s->x = (X); \
24549 while (0)
24552 /* Add a glyph string for a sequence of character glyphs to the list
24553 of strings between HEAD and TAIL. START is the index of the first
24554 glyph in row area AREA of glyph row ROW that is part of the new
24555 glyph string. END is the index of the last glyph in that glyph row
24556 area. X is the current output position assigned to the new glyph
24557 string constructed. HL overrides that face of the glyph; e.g. it
24558 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24559 right-most x-position of the drawing area. */
24561 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24562 do \
24564 int face_id; \
24565 XChar2b *char2b; \
24567 face_id = (row)->glyphs[area][START].face_id; \
24569 s = alloca (sizeof *s); \
24570 char2b = alloca ((END - START) * sizeof *char2b); \
24571 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24572 append_glyph_string (&HEAD, &TAIL, s); \
24573 s->x = (X); \
24574 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24576 while (0)
24579 /* Add a glyph string for a composite sequence to the list of strings
24580 between HEAD and TAIL. START is the index of the first glyph in
24581 row area AREA of glyph row ROW that is part of the new glyph
24582 string. END is the index of the last glyph in that glyph row area.
24583 X is the current output position assigned to the new glyph string
24584 constructed. HL overrides that face of the glyph; e.g. it is
24585 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24586 x-position of the drawing area. */
24588 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24589 do { \
24590 int face_id = (row)->glyphs[area][START].face_id; \
24591 struct face *base_face = FACE_FROM_ID (f, face_id); \
24592 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24593 struct composition *cmp = composition_table[cmp_id]; \
24594 XChar2b *char2b; \
24595 struct glyph_string *first_s = NULL; \
24596 int n; \
24598 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
24600 /* Make glyph_strings for each glyph sequence that is drawable by \
24601 the same face, and append them to HEAD/TAIL. */ \
24602 for (n = 0; n < cmp->glyph_len;) \
24604 s = alloca (sizeof *s); \
24605 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24606 append_glyph_string (&(HEAD), &(TAIL), s); \
24607 s->cmp = cmp; \
24608 s->cmp_from = n; \
24609 s->x = (X); \
24610 if (n == 0) \
24611 first_s = s; \
24612 n = fill_composite_glyph_string (s, base_face, overlaps); \
24615 ++START; \
24616 s = first_s; \
24617 } while (0)
24620 /* Add a glyph string for a glyph-string sequence to the list of strings
24621 between HEAD and TAIL. */
24623 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24624 do { \
24625 int face_id; \
24626 XChar2b *char2b; \
24627 Lisp_Object gstring; \
24629 face_id = (row)->glyphs[area][START].face_id; \
24630 gstring = (composition_gstring_from_id \
24631 ((row)->glyphs[area][START].u.cmp.id)); \
24632 s = alloca (sizeof *s); \
24633 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
24634 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24635 append_glyph_string (&(HEAD), &(TAIL), s); \
24636 s->x = (X); \
24637 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24638 } while (0)
24641 /* Add a glyph string for a sequence of glyphless character's glyphs
24642 to the list of strings between HEAD and TAIL. The meanings of
24643 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24645 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24646 do \
24648 int face_id; \
24650 face_id = (row)->glyphs[area][START].face_id; \
24652 s = alloca (sizeof *s); \
24653 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24654 append_glyph_string (&HEAD, &TAIL, s); \
24655 s->x = (X); \
24656 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24657 overlaps); \
24659 while (0)
24662 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24663 of AREA of glyph row ROW on window W between indices START and END.
24664 HL overrides the face for drawing glyph strings, e.g. it is
24665 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24666 x-positions of the drawing area.
24668 This is an ugly monster macro construct because we must use alloca
24669 to allocate glyph strings (because draw_glyphs can be called
24670 asynchronously). */
24672 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24673 do \
24675 HEAD = TAIL = NULL; \
24676 while (START < END) \
24678 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24679 switch (first_glyph->type) \
24681 case CHAR_GLYPH: \
24682 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24683 HL, X, LAST_X); \
24684 break; \
24686 case COMPOSITE_GLYPH: \
24687 if (first_glyph->u.cmp.automatic) \
24688 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24689 HL, X, LAST_X); \
24690 else \
24691 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24692 HL, X, LAST_X); \
24693 break; \
24695 case STRETCH_GLYPH: \
24696 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24697 HL, X, LAST_X); \
24698 break; \
24700 case IMAGE_GLYPH: \
24701 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24702 HL, X, LAST_X); \
24703 break; \
24705 case GLYPHLESS_GLYPH: \
24706 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24707 HL, X, LAST_X); \
24708 break; \
24710 default: \
24711 emacs_abort (); \
24714 if (s) \
24716 set_glyph_string_background_width (s, START, LAST_X); \
24717 (X) += s->width; \
24720 } while (0)
24723 /* Draw glyphs between START and END in AREA of ROW on window W,
24724 starting at x-position X. X is relative to AREA in W. HL is a
24725 face-override with the following meaning:
24727 DRAW_NORMAL_TEXT draw normally
24728 DRAW_CURSOR draw in cursor face
24729 DRAW_MOUSE_FACE draw in mouse face.
24730 DRAW_INVERSE_VIDEO draw in mode line face
24731 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24732 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24734 If OVERLAPS is non-zero, draw only the foreground of characters and
24735 clip to the physical height of ROW. Non-zero value also defines
24736 the overlapping part to be drawn:
24738 OVERLAPS_PRED overlap with preceding rows
24739 OVERLAPS_SUCC overlap with succeeding rows
24740 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24741 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24743 Value is the x-position reached, relative to AREA of W. */
24745 static int
24746 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24747 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24748 enum draw_glyphs_face hl, int overlaps)
24750 struct glyph_string *head, *tail;
24751 struct glyph_string *s;
24752 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24753 int i, j, x_reached, last_x, area_left = 0;
24754 struct frame *f = XFRAME (WINDOW_FRAME (w));
24755 DECLARE_HDC (hdc);
24757 ALLOCATE_HDC (hdc, f);
24759 /* Let's rather be paranoid than getting a SEGV. */
24760 end = min (end, row->used[area]);
24761 start = clip_to_bounds (0, start, end);
24763 /* Translate X to frame coordinates. Set last_x to the right
24764 end of the drawing area. */
24765 if (row->full_width_p)
24767 /* X is relative to the left edge of W, without scroll bars
24768 or fringes. */
24769 area_left = WINDOW_LEFT_EDGE_X (w);
24770 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24771 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24773 else
24775 area_left = window_box_left (w, area);
24776 last_x = area_left + window_box_width (w, area);
24778 x += area_left;
24780 /* Build a doubly-linked list of glyph_string structures between
24781 head and tail from what we have to draw. Note that the macro
24782 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24783 the reason we use a separate variable `i'. */
24784 i = start;
24785 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24786 if (tail)
24787 x_reached = tail->x + tail->background_width;
24788 else
24789 x_reached = x;
24791 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24792 the row, redraw some glyphs in front or following the glyph
24793 strings built above. */
24794 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24796 struct glyph_string *h, *t;
24797 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24798 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24799 int check_mouse_face = 0;
24800 int dummy_x = 0;
24802 /* If mouse highlighting is on, we may need to draw adjacent
24803 glyphs using mouse-face highlighting. */
24804 if (area == TEXT_AREA && row->mouse_face_p
24805 && hlinfo->mouse_face_beg_row >= 0
24806 && hlinfo->mouse_face_end_row >= 0)
24808 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24810 if (row_vpos >= hlinfo->mouse_face_beg_row
24811 && row_vpos <= hlinfo->mouse_face_end_row)
24813 check_mouse_face = 1;
24814 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24815 ? hlinfo->mouse_face_beg_col : 0;
24816 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24817 ? hlinfo->mouse_face_end_col
24818 : row->used[TEXT_AREA];
24822 /* Compute overhangs for all glyph strings. */
24823 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24824 for (s = head; s; s = s->next)
24825 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24827 /* Prepend glyph strings for glyphs in front of the first glyph
24828 string that are overwritten because of the first glyph
24829 string's left overhang. The background of all strings
24830 prepended must be drawn because the first glyph string
24831 draws over it. */
24832 i = left_overwritten (head);
24833 if (i >= 0)
24835 enum draw_glyphs_face overlap_hl;
24837 /* If this row contains mouse highlighting, attempt to draw
24838 the overlapped glyphs with the correct highlight. This
24839 code fails if the overlap encompasses more than one glyph
24840 and mouse-highlight spans only some of these glyphs.
24841 However, making it work perfectly involves a lot more
24842 code, and I don't know if the pathological case occurs in
24843 practice, so we'll stick to this for now. --- cyd */
24844 if (check_mouse_face
24845 && mouse_beg_col < start && mouse_end_col > i)
24846 overlap_hl = DRAW_MOUSE_FACE;
24847 else
24848 overlap_hl = DRAW_NORMAL_TEXT;
24850 j = i;
24851 BUILD_GLYPH_STRINGS (j, start, h, t,
24852 overlap_hl, dummy_x, last_x);
24853 start = i;
24854 compute_overhangs_and_x (t, head->x, 1);
24855 prepend_glyph_string_lists (&head, &tail, h, t);
24856 clip_head = head;
24859 /* Prepend glyph strings for glyphs in front of the first glyph
24860 string that overwrite that glyph string because of their
24861 right overhang. For these strings, only the foreground must
24862 be drawn, because it draws over the glyph string at `head'.
24863 The background must not be drawn because this would overwrite
24864 right overhangs of preceding glyphs for which no glyph
24865 strings exist. */
24866 i = left_overwriting (head);
24867 if (i >= 0)
24869 enum draw_glyphs_face overlap_hl;
24871 if (check_mouse_face
24872 && mouse_beg_col < start && mouse_end_col > i)
24873 overlap_hl = DRAW_MOUSE_FACE;
24874 else
24875 overlap_hl = DRAW_NORMAL_TEXT;
24877 clip_head = head;
24878 BUILD_GLYPH_STRINGS (i, start, h, t,
24879 overlap_hl, dummy_x, last_x);
24880 for (s = h; s; s = s->next)
24881 s->background_filled_p = 1;
24882 compute_overhangs_and_x (t, head->x, 1);
24883 prepend_glyph_string_lists (&head, &tail, h, t);
24886 /* Append glyphs strings for glyphs following the last glyph
24887 string tail that are overwritten by tail. The background of
24888 these strings has to be drawn because tail's foreground draws
24889 over it. */
24890 i = right_overwritten (tail);
24891 if (i >= 0)
24893 enum draw_glyphs_face overlap_hl;
24895 if (check_mouse_face
24896 && mouse_beg_col < i && mouse_end_col > end)
24897 overlap_hl = DRAW_MOUSE_FACE;
24898 else
24899 overlap_hl = DRAW_NORMAL_TEXT;
24901 BUILD_GLYPH_STRINGS (end, i, h, t,
24902 overlap_hl, x, last_x);
24903 /* Because BUILD_GLYPH_STRINGS updates the first argument,
24904 we don't have `end = i;' here. */
24905 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24906 append_glyph_string_lists (&head, &tail, h, t);
24907 clip_tail = tail;
24910 /* Append glyph strings for glyphs following the last glyph
24911 string tail that overwrite tail. The foreground of such
24912 glyphs has to be drawn because it writes into the background
24913 of tail. The background must not be drawn because it could
24914 paint over the foreground of following glyphs. */
24915 i = right_overwriting (tail);
24916 if (i >= 0)
24918 enum draw_glyphs_face overlap_hl;
24919 if (check_mouse_face
24920 && mouse_beg_col < i && mouse_end_col > end)
24921 overlap_hl = DRAW_MOUSE_FACE;
24922 else
24923 overlap_hl = DRAW_NORMAL_TEXT;
24925 clip_tail = tail;
24926 i++; /* We must include the Ith glyph. */
24927 BUILD_GLYPH_STRINGS (end, i, h, t,
24928 overlap_hl, x, last_x);
24929 for (s = h; s; s = s->next)
24930 s->background_filled_p = 1;
24931 compute_overhangs_and_x (h, tail->x + tail->width, 0);
24932 append_glyph_string_lists (&head, &tail, h, t);
24934 if (clip_head || clip_tail)
24935 for (s = head; s; s = s->next)
24937 s->clip_head = clip_head;
24938 s->clip_tail = clip_tail;
24942 /* Draw all strings. */
24943 for (s = head; s; s = s->next)
24944 FRAME_RIF (f)->draw_glyph_string (s);
24946 #ifndef HAVE_NS
24947 /* When focus a sole frame and move horizontally, this sets on_p to 0
24948 causing a failure to erase prev cursor position. */
24949 if (area == TEXT_AREA
24950 && !row->full_width_p
24951 /* When drawing overlapping rows, only the glyph strings'
24952 foreground is drawn, which doesn't erase a cursor
24953 completely. */
24954 && !overlaps)
24956 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24957 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24958 : (tail ? tail->x + tail->background_width : x));
24959 x0 -= area_left;
24960 x1 -= area_left;
24962 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24963 row->y, MATRIX_ROW_BOTTOM_Y (row));
24965 #endif
24967 /* Value is the x-position up to which drawn, relative to AREA of W.
24968 This doesn't include parts drawn because of overhangs. */
24969 if (row->full_width_p)
24970 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24971 else
24972 x_reached -= area_left;
24974 RELEASE_HDC (hdc, f);
24976 return x_reached;
24979 /* Expand row matrix if too narrow. Don't expand if area
24980 is not present. */
24982 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24984 if (!it->f->fonts_changed \
24985 && (it->glyph_row->glyphs[area] \
24986 < it->glyph_row->glyphs[area + 1])) \
24988 it->w->ncols_scale_factor++; \
24989 it->f->fonts_changed = 1; \
24993 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24994 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24996 static void
24997 append_glyph (struct it *it)
24999 struct glyph *glyph;
25000 enum glyph_row_area area = it->area;
25002 eassert (it->glyph_row);
25003 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25005 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25006 if (glyph < it->glyph_row->glyphs[area + 1])
25008 /* If the glyph row is reversed, we need to prepend the glyph
25009 rather than append it. */
25010 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25012 struct glyph *g;
25014 /* Make room for the additional glyph. */
25015 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25016 g[1] = *g;
25017 glyph = it->glyph_row->glyphs[area];
25019 glyph->charpos = CHARPOS (it->position);
25020 glyph->object = it->object;
25021 if (it->pixel_width > 0)
25023 glyph->pixel_width = it->pixel_width;
25024 glyph->padding_p = 0;
25026 else
25028 /* Assure at least 1-pixel width. Otherwise, cursor can't
25029 be displayed correctly. */
25030 glyph->pixel_width = 1;
25031 glyph->padding_p = 1;
25033 glyph->ascent = it->ascent;
25034 glyph->descent = it->descent;
25035 glyph->voffset = it->voffset;
25036 glyph->type = CHAR_GLYPH;
25037 glyph->avoid_cursor_p = it->avoid_cursor_p;
25038 glyph->multibyte_p = it->multibyte_p;
25039 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25041 /* In R2L rows, the left and the right box edges need to be
25042 drawn in reverse direction. */
25043 glyph->right_box_line_p = it->start_of_box_run_p;
25044 glyph->left_box_line_p = it->end_of_box_run_p;
25046 else
25048 glyph->left_box_line_p = it->start_of_box_run_p;
25049 glyph->right_box_line_p = it->end_of_box_run_p;
25051 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25052 || it->phys_descent > it->descent);
25053 glyph->glyph_not_available_p = it->glyph_not_available_p;
25054 glyph->face_id = it->face_id;
25055 glyph->u.ch = it->char_to_display;
25056 glyph->slice.img = null_glyph_slice;
25057 glyph->font_type = FONT_TYPE_UNKNOWN;
25058 if (it->bidi_p)
25060 glyph->resolved_level = it->bidi_it.resolved_level;
25061 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25062 emacs_abort ();
25063 glyph->bidi_type = it->bidi_it.type;
25065 else
25067 glyph->resolved_level = 0;
25068 glyph->bidi_type = UNKNOWN_BT;
25070 ++it->glyph_row->used[area];
25072 else
25073 IT_EXPAND_MATRIX_WIDTH (it, area);
25076 /* Store one glyph for the composition IT->cmp_it.id in
25077 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25078 non-null. */
25080 static void
25081 append_composite_glyph (struct it *it)
25083 struct glyph *glyph;
25084 enum glyph_row_area area = it->area;
25086 eassert (it->glyph_row);
25088 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25089 if (glyph < it->glyph_row->glyphs[area + 1])
25091 /* If the glyph row is reversed, we need to prepend the glyph
25092 rather than append it. */
25093 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25095 struct glyph *g;
25097 /* Make room for the new glyph. */
25098 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25099 g[1] = *g;
25100 glyph = it->glyph_row->glyphs[it->area];
25102 glyph->charpos = it->cmp_it.charpos;
25103 glyph->object = it->object;
25104 glyph->pixel_width = it->pixel_width;
25105 glyph->ascent = it->ascent;
25106 glyph->descent = it->descent;
25107 glyph->voffset = it->voffset;
25108 glyph->type = COMPOSITE_GLYPH;
25109 if (it->cmp_it.ch < 0)
25111 glyph->u.cmp.automatic = 0;
25112 glyph->u.cmp.id = it->cmp_it.id;
25113 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25115 else
25117 glyph->u.cmp.automatic = 1;
25118 glyph->u.cmp.id = it->cmp_it.id;
25119 glyph->slice.cmp.from = it->cmp_it.from;
25120 glyph->slice.cmp.to = it->cmp_it.to - 1;
25122 glyph->avoid_cursor_p = it->avoid_cursor_p;
25123 glyph->multibyte_p = it->multibyte_p;
25124 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25126 /* In R2L rows, the left and the right box edges need to be
25127 drawn in reverse direction. */
25128 glyph->right_box_line_p = it->start_of_box_run_p;
25129 glyph->left_box_line_p = it->end_of_box_run_p;
25131 else
25133 glyph->left_box_line_p = it->start_of_box_run_p;
25134 glyph->right_box_line_p = it->end_of_box_run_p;
25136 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25137 || it->phys_descent > it->descent);
25138 glyph->padding_p = 0;
25139 glyph->glyph_not_available_p = 0;
25140 glyph->face_id = it->face_id;
25141 glyph->font_type = FONT_TYPE_UNKNOWN;
25142 if (it->bidi_p)
25144 glyph->resolved_level = it->bidi_it.resolved_level;
25145 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25146 emacs_abort ();
25147 glyph->bidi_type = it->bidi_it.type;
25149 ++it->glyph_row->used[area];
25151 else
25152 IT_EXPAND_MATRIX_WIDTH (it, area);
25156 /* Change IT->ascent and IT->height according to the setting of
25157 IT->voffset. */
25159 static void
25160 take_vertical_position_into_account (struct it *it)
25162 if (it->voffset)
25164 if (it->voffset < 0)
25165 /* Increase the ascent so that we can display the text higher
25166 in the line. */
25167 it->ascent -= it->voffset;
25168 else
25169 /* Increase the descent so that we can display the text lower
25170 in the line. */
25171 it->descent += it->voffset;
25176 /* Produce glyphs/get display metrics for the image IT is loaded with.
25177 See the description of struct display_iterator in dispextern.h for
25178 an overview of struct display_iterator. */
25180 static void
25181 produce_image_glyph (struct it *it)
25183 struct image *img;
25184 struct face *face;
25185 int glyph_ascent, crop;
25186 struct glyph_slice slice;
25188 eassert (it->what == IT_IMAGE);
25190 face = FACE_FROM_ID (it->f, it->face_id);
25191 eassert (face);
25192 /* Make sure X resources of the face is loaded. */
25193 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25195 if (it->image_id < 0)
25197 /* Fringe bitmap. */
25198 it->ascent = it->phys_ascent = 0;
25199 it->descent = it->phys_descent = 0;
25200 it->pixel_width = 0;
25201 it->nglyphs = 0;
25202 return;
25205 img = IMAGE_FROM_ID (it->f, it->image_id);
25206 eassert (img);
25207 /* Make sure X resources of the image is loaded. */
25208 prepare_image_for_display (it->f, img);
25210 slice.x = slice.y = 0;
25211 slice.width = img->width;
25212 slice.height = img->height;
25214 if (INTEGERP (it->slice.x))
25215 slice.x = XINT (it->slice.x);
25216 else if (FLOATP (it->slice.x))
25217 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25219 if (INTEGERP (it->slice.y))
25220 slice.y = XINT (it->slice.y);
25221 else if (FLOATP (it->slice.y))
25222 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25224 if (INTEGERP (it->slice.width))
25225 slice.width = XINT (it->slice.width);
25226 else if (FLOATP (it->slice.width))
25227 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25229 if (INTEGERP (it->slice.height))
25230 slice.height = XINT (it->slice.height);
25231 else if (FLOATP (it->slice.height))
25232 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25234 if (slice.x >= img->width)
25235 slice.x = img->width;
25236 if (slice.y >= img->height)
25237 slice.y = img->height;
25238 if (slice.x + slice.width >= img->width)
25239 slice.width = img->width - slice.x;
25240 if (slice.y + slice.height > img->height)
25241 slice.height = img->height - slice.y;
25243 if (slice.width == 0 || slice.height == 0)
25244 return;
25246 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25248 it->descent = slice.height - glyph_ascent;
25249 if (slice.y == 0)
25250 it->descent += img->vmargin;
25251 if (slice.y + slice.height == img->height)
25252 it->descent += img->vmargin;
25253 it->phys_descent = it->descent;
25255 it->pixel_width = slice.width;
25256 if (slice.x == 0)
25257 it->pixel_width += img->hmargin;
25258 if (slice.x + slice.width == img->width)
25259 it->pixel_width += img->hmargin;
25261 /* It's quite possible for images to have an ascent greater than
25262 their height, so don't get confused in that case. */
25263 if (it->descent < 0)
25264 it->descent = 0;
25266 it->nglyphs = 1;
25268 if (face->box != FACE_NO_BOX)
25270 if (face->box_line_width > 0)
25272 if (slice.y == 0)
25273 it->ascent += face->box_line_width;
25274 if (slice.y + slice.height == img->height)
25275 it->descent += face->box_line_width;
25278 if (it->start_of_box_run_p && slice.x == 0)
25279 it->pixel_width += eabs (face->box_line_width);
25280 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25281 it->pixel_width += eabs (face->box_line_width);
25284 take_vertical_position_into_account (it);
25286 /* Automatically crop wide image glyphs at right edge so we can
25287 draw the cursor on same display row. */
25288 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25289 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25291 it->pixel_width -= crop;
25292 slice.width -= crop;
25295 if (it->glyph_row)
25297 struct glyph *glyph;
25298 enum glyph_row_area area = it->area;
25300 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25301 if (glyph < it->glyph_row->glyphs[area + 1])
25303 glyph->charpos = CHARPOS (it->position);
25304 glyph->object = it->object;
25305 glyph->pixel_width = it->pixel_width;
25306 glyph->ascent = glyph_ascent;
25307 glyph->descent = it->descent;
25308 glyph->voffset = it->voffset;
25309 glyph->type = IMAGE_GLYPH;
25310 glyph->avoid_cursor_p = it->avoid_cursor_p;
25311 glyph->multibyte_p = it->multibyte_p;
25312 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25314 /* In R2L rows, the left and the right box edges need to be
25315 drawn in reverse direction. */
25316 glyph->right_box_line_p = it->start_of_box_run_p;
25317 glyph->left_box_line_p = it->end_of_box_run_p;
25319 else
25321 glyph->left_box_line_p = it->start_of_box_run_p;
25322 glyph->right_box_line_p = it->end_of_box_run_p;
25324 glyph->overlaps_vertically_p = 0;
25325 glyph->padding_p = 0;
25326 glyph->glyph_not_available_p = 0;
25327 glyph->face_id = it->face_id;
25328 glyph->u.img_id = img->id;
25329 glyph->slice.img = slice;
25330 glyph->font_type = FONT_TYPE_UNKNOWN;
25331 if (it->bidi_p)
25333 glyph->resolved_level = it->bidi_it.resolved_level;
25334 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25335 emacs_abort ();
25336 glyph->bidi_type = it->bidi_it.type;
25338 ++it->glyph_row->used[area];
25340 else
25341 IT_EXPAND_MATRIX_WIDTH (it, area);
25346 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25347 of the glyph, WIDTH and HEIGHT are the width and height of the
25348 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25350 static void
25351 append_stretch_glyph (struct it *it, Lisp_Object object,
25352 int width, int height, int ascent)
25354 struct glyph *glyph;
25355 enum glyph_row_area area = it->area;
25357 eassert (ascent >= 0 && ascent <= height);
25359 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25360 if (glyph < it->glyph_row->glyphs[area + 1])
25362 /* If the glyph row is reversed, we need to prepend the glyph
25363 rather than append it. */
25364 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25366 struct glyph *g;
25368 /* Make room for the additional glyph. */
25369 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25370 g[1] = *g;
25371 glyph = it->glyph_row->glyphs[area];
25373 /* Decrease the width of the first glyph of the row that
25374 begins before first_visible_x (e.g., due to hscroll).
25375 This is so the overall width of the row becomes smaller
25376 by the scroll amount, and the stretch glyph appended by
25377 extend_face_to_end_of_line will be wider, to shift the
25378 row glyphs to the right. (In L2R rows, the corresponding
25379 left-shift effect is accomplished by setting row->x to a
25380 negative value, which won't work with R2L rows.)
25382 This must leave us with a positive value of WIDTH, since
25383 otherwise the call to move_it_in_display_line_to at the
25384 beginning of display_line would have got past the entire
25385 first glyph, and then it->current_x would have been
25386 greater or equal to it->first_visible_x. */
25387 if (it->current_x < it->first_visible_x)
25388 width -= it->first_visible_x - it->current_x;
25389 eassert (width > 0);
25391 glyph->charpos = CHARPOS (it->position);
25392 glyph->object = object;
25393 glyph->pixel_width = width;
25394 glyph->ascent = ascent;
25395 glyph->descent = height - ascent;
25396 glyph->voffset = it->voffset;
25397 glyph->type = STRETCH_GLYPH;
25398 glyph->avoid_cursor_p = it->avoid_cursor_p;
25399 glyph->multibyte_p = it->multibyte_p;
25400 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25402 /* In R2L rows, the left and the right box edges need to be
25403 drawn in reverse direction. */
25404 glyph->right_box_line_p = it->start_of_box_run_p;
25405 glyph->left_box_line_p = it->end_of_box_run_p;
25407 else
25409 glyph->left_box_line_p = it->start_of_box_run_p;
25410 glyph->right_box_line_p = it->end_of_box_run_p;
25412 glyph->overlaps_vertically_p = 0;
25413 glyph->padding_p = 0;
25414 glyph->glyph_not_available_p = 0;
25415 glyph->face_id = it->face_id;
25416 glyph->u.stretch.ascent = ascent;
25417 glyph->u.stretch.height = height;
25418 glyph->slice.img = null_glyph_slice;
25419 glyph->font_type = FONT_TYPE_UNKNOWN;
25420 if (it->bidi_p)
25422 glyph->resolved_level = it->bidi_it.resolved_level;
25423 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25424 emacs_abort ();
25425 glyph->bidi_type = it->bidi_it.type;
25427 else
25429 glyph->resolved_level = 0;
25430 glyph->bidi_type = UNKNOWN_BT;
25432 ++it->glyph_row->used[area];
25434 else
25435 IT_EXPAND_MATRIX_WIDTH (it, area);
25438 #endif /* HAVE_WINDOW_SYSTEM */
25440 /* Produce a stretch glyph for iterator IT. IT->object is the value
25441 of the glyph property displayed. The value must be a list
25442 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25443 being recognized:
25445 1. `:width WIDTH' specifies that the space should be WIDTH *
25446 canonical char width wide. WIDTH may be an integer or floating
25447 point number.
25449 2. `:relative-width FACTOR' specifies that the width of the stretch
25450 should be computed from the width of the first character having the
25451 `glyph' property, and should be FACTOR times that width.
25453 3. `:align-to HPOS' specifies that the space should be wide enough
25454 to reach HPOS, a value in canonical character units.
25456 Exactly one of the above pairs must be present.
25458 4. `:height HEIGHT' specifies that the height of the stretch produced
25459 should be HEIGHT, measured in canonical character units.
25461 5. `:relative-height FACTOR' specifies that the height of the
25462 stretch should be FACTOR times the height of the characters having
25463 the glyph property.
25465 Either none or exactly one of 4 or 5 must be present.
25467 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25468 of the stretch should be used for the ascent of the stretch.
25469 ASCENT must be in the range 0 <= ASCENT <= 100. */
25471 void
25472 produce_stretch_glyph (struct it *it)
25474 /* (space :width WIDTH :height HEIGHT ...) */
25475 Lisp_Object prop, plist;
25476 int width = 0, height = 0, align_to = -1;
25477 int zero_width_ok_p = 0;
25478 double tem;
25479 struct font *font = NULL;
25481 #ifdef HAVE_WINDOW_SYSTEM
25482 int ascent = 0;
25483 int zero_height_ok_p = 0;
25485 if (FRAME_WINDOW_P (it->f))
25487 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25488 font = face->font ? face->font : FRAME_FONT (it->f);
25489 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25491 #endif
25493 /* List should start with `space'. */
25494 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25495 plist = XCDR (it->object);
25497 /* Compute the width of the stretch. */
25498 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25499 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25501 /* Absolute width `:width WIDTH' specified and valid. */
25502 zero_width_ok_p = 1;
25503 width = (int)tem;
25505 #ifdef HAVE_WINDOW_SYSTEM
25506 else if (FRAME_WINDOW_P (it->f)
25507 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25509 /* Relative width `:relative-width FACTOR' specified and valid.
25510 Compute the width of the characters having the `glyph'
25511 property. */
25512 struct it it2;
25513 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25515 it2 = *it;
25516 if (it->multibyte_p)
25517 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25518 else
25520 it2.c = it2.char_to_display = *p, it2.len = 1;
25521 if (! ASCII_CHAR_P (it2.c))
25522 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25525 it2.glyph_row = NULL;
25526 it2.what = IT_CHARACTER;
25527 x_produce_glyphs (&it2);
25528 width = NUMVAL (prop) * it2.pixel_width;
25530 #endif /* HAVE_WINDOW_SYSTEM */
25531 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25532 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25534 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25535 align_to = (align_to < 0
25537 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25538 else if (align_to < 0)
25539 align_to = window_box_left_offset (it->w, TEXT_AREA);
25540 width = max (0, (int)tem + align_to - it->current_x);
25541 zero_width_ok_p = 1;
25543 else
25544 /* Nothing specified -> width defaults to canonical char width. */
25545 width = FRAME_COLUMN_WIDTH (it->f);
25547 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25548 width = 1;
25550 #ifdef HAVE_WINDOW_SYSTEM
25551 /* Compute height. */
25552 if (FRAME_WINDOW_P (it->f))
25554 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25555 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25557 height = (int)tem;
25558 zero_height_ok_p = 1;
25560 else if (prop = Fplist_get (plist, QCrelative_height),
25561 NUMVAL (prop) > 0)
25562 height = FONT_HEIGHT (font) * NUMVAL (prop);
25563 else
25564 height = FONT_HEIGHT (font);
25566 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25567 height = 1;
25569 /* Compute percentage of height used for ascent. If
25570 `:ascent ASCENT' is present and valid, use that. Otherwise,
25571 derive the ascent from the font in use. */
25572 if (prop = Fplist_get (plist, QCascent),
25573 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25574 ascent = height * NUMVAL (prop) / 100.0;
25575 else if (!NILP (prop)
25576 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25577 ascent = min (max (0, (int)tem), height);
25578 else
25579 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25581 else
25582 #endif /* HAVE_WINDOW_SYSTEM */
25583 height = 1;
25585 if (width > 0 && it->line_wrap != TRUNCATE
25586 && it->current_x + width > it->last_visible_x)
25588 width = it->last_visible_x - it->current_x;
25589 #ifdef HAVE_WINDOW_SYSTEM
25590 /* Subtract one more pixel from the stretch width, but only on
25591 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25592 width -= FRAME_WINDOW_P (it->f);
25593 #endif
25596 if (width > 0 && height > 0 && it->glyph_row)
25598 Lisp_Object o_object = it->object;
25599 Lisp_Object object = it->stack[it->sp - 1].string;
25600 int n = width;
25602 if (!STRINGP (object))
25603 object = it->w->contents;
25604 #ifdef HAVE_WINDOW_SYSTEM
25605 if (FRAME_WINDOW_P (it->f))
25606 append_stretch_glyph (it, object, width, height, ascent);
25607 else
25608 #endif
25610 it->object = object;
25611 it->char_to_display = ' ';
25612 it->pixel_width = it->len = 1;
25613 while (n--)
25614 tty_append_glyph (it);
25615 it->object = o_object;
25619 it->pixel_width = width;
25620 #ifdef HAVE_WINDOW_SYSTEM
25621 if (FRAME_WINDOW_P (it->f))
25623 it->ascent = it->phys_ascent = ascent;
25624 it->descent = it->phys_descent = height - it->ascent;
25625 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25626 take_vertical_position_into_account (it);
25628 else
25629 #endif
25630 it->nglyphs = width;
25633 /* Get information about special display element WHAT in an
25634 environment described by IT. WHAT is one of IT_TRUNCATION or
25635 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25636 non-null glyph_row member. This function ensures that fields like
25637 face_id, c, len of IT are left untouched. */
25639 static void
25640 produce_special_glyphs (struct it *it, enum display_element_type what)
25642 struct it temp_it;
25643 Lisp_Object gc;
25644 GLYPH glyph;
25646 temp_it = *it;
25647 temp_it.object = make_number (0);
25648 memset (&temp_it.current, 0, sizeof temp_it.current);
25650 if (what == IT_CONTINUATION)
25652 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25653 if (it->bidi_it.paragraph_dir == R2L)
25654 SET_GLYPH_FROM_CHAR (glyph, '/');
25655 else
25656 SET_GLYPH_FROM_CHAR (glyph, '\\');
25657 if (it->dp
25658 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25660 /* FIXME: Should we mirror GC for R2L lines? */
25661 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25662 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25665 else if (what == IT_TRUNCATION)
25667 /* Truncation glyph. */
25668 SET_GLYPH_FROM_CHAR (glyph, '$');
25669 if (it->dp
25670 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25672 /* FIXME: Should we mirror GC for R2L lines? */
25673 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25674 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25677 else
25678 emacs_abort ();
25680 #ifdef HAVE_WINDOW_SYSTEM
25681 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25682 is turned off, we precede the truncation/continuation glyphs by a
25683 stretch glyph whose width is computed such that these special
25684 glyphs are aligned at the window margin, even when very different
25685 fonts are used in different glyph rows. */
25686 if (FRAME_WINDOW_P (temp_it.f)
25687 /* init_iterator calls this with it->glyph_row == NULL, and it
25688 wants only the pixel width of the truncation/continuation
25689 glyphs. */
25690 && temp_it.glyph_row
25691 /* insert_left_trunc_glyphs calls us at the beginning of the
25692 row, and it has its own calculation of the stretch glyph
25693 width. */
25694 && temp_it.glyph_row->used[TEXT_AREA] > 0
25695 && (temp_it.glyph_row->reversed_p
25696 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25697 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25699 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25701 if (stretch_width > 0)
25703 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25704 struct font *font =
25705 face->font ? face->font : FRAME_FONT (temp_it.f);
25706 int stretch_ascent =
25707 (((temp_it.ascent + temp_it.descent)
25708 * FONT_BASE (font)) / FONT_HEIGHT (font));
25710 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25711 temp_it.ascent + temp_it.descent,
25712 stretch_ascent);
25715 #endif
25717 temp_it.dp = NULL;
25718 temp_it.what = IT_CHARACTER;
25719 temp_it.len = 1;
25720 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25721 temp_it.face_id = GLYPH_FACE (glyph);
25722 temp_it.len = CHAR_BYTES (temp_it.c);
25724 PRODUCE_GLYPHS (&temp_it);
25725 it->pixel_width = temp_it.pixel_width;
25726 it->nglyphs = temp_it.pixel_width;
25729 #ifdef HAVE_WINDOW_SYSTEM
25731 /* Calculate line-height and line-spacing properties.
25732 An integer value specifies explicit pixel value.
25733 A float value specifies relative value to current face height.
25734 A cons (float . face-name) specifies relative value to
25735 height of specified face font.
25737 Returns height in pixels, or nil. */
25740 static Lisp_Object
25741 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25742 int boff, int override)
25744 Lisp_Object face_name = Qnil;
25745 int ascent, descent, height;
25747 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25748 return val;
25750 if (CONSP (val))
25752 face_name = XCAR (val);
25753 val = XCDR (val);
25754 if (!NUMBERP (val))
25755 val = make_number (1);
25756 if (NILP (face_name))
25758 height = it->ascent + it->descent;
25759 goto scale;
25763 if (NILP (face_name))
25765 font = FRAME_FONT (it->f);
25766 boff = FRAME_BASELINE_OFFSET (it->f);
25768 else if (EQ (face_name, Qt))
25770 override = 0;
25772 else
25774 int face_id;
25775 struct face *face;
25777 face_id = lookup_named_face (it->f, face_name, 0);
25778 if (face_id < 0)
25779 return make_number (-1);
25781 face = FACE_FROM_ID (it->f, face_id);
25782 font = face->font;
25783 if (font == NULL)
25784 return make_number (-1);
25785 boff = font->baseline_offset;
25786 if (font->vertical_centering)
25787 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25790 ascent = FONT_BASE (font) + boff;
25791 descent = FONT_DESCENT (font) - boff;
25793 if (override)
25795 it->override_ascent = ascent;
25796 it->override_descent = descent;
25797 it->override_boff = boff;
25800 height = ascent + descent;
25802 scale:
25803 if (FLOATP (val))
25804 height = (int)(XFLOAT_DATA (val) * height);
25805 else if (INTEGERP (val))
25806 height *= XINT (val);
25808 return make_number (height);
25812 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25813 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25814 and only if this is for a character for which no font was found.
25816 If the display method (it->glyphless_method) is
25817 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25818 length of the acronym or the hexadecimal string, UPPER_XOFF and
25819 UPPER_YOFF are pixel offsets for the upper part of the string,
25820 LOWER_XOFF and LOWER_YOFF are for the lower part.
25822 For the other display methods, LEN through LOWER_YOFF are zero. */
25824 static void
25825 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25826 short upper_xoff, short upper_yoff,
25827 short lower_xoff, short lower_yoff)
25829 struct glyph *glyph;
25830 enum glyph_row_area area = it->area;
25832 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25833 if (glyph < it->glyph_row->glyphs[area + 1])
25835 /* If the glyph row is reversed, we need to prepend the glyph
25836 rather than append it. */
25837 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25839 struct glyph *g;
25841 /* Make room for the additional glyph. */
25842 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25843 g[1] = *g;
25844 glyph = it->glyph_row->glyphs[area];
25846 glyph->charpos = CHARPOS (it->position);
25847 glyph->object = it->object;
25848 glyph->pixel_width = it->pixel_width;
25849 glyph->ascent = it->ascent;
25850 glyph->descent = it->descent;
25851 glyph->voffset = it->voffset;
25852 glyph->type = GLYPHLESS_GLYPH;
25853 glyph->u.glyphless.method = it->glyphless_method;
25854 glyph->u.glyphless.for_no_font = for_no_font;
25855 glyph->u.glyphless.len = len;
25856 glyph->u.glyphless.ch = it->c;
25857 glyph->slice.glyphless.upper_xoff = upper_xoff;
25858 glyph->slice.glyphless.upper_yoff = upper_yoff;
25859 glyph->slice.glyphless.lower_xoff = lower_xoff;
25860 glyph->slice.glyphless.lower_yoff = lower_yoff;
25861 glyph->avoid_cursor_p = it->avoid_cursor_p;
25862 glyph->multibyte_p = it->multibyte_p;
25863 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25865 /* In R2L rows, the left and the right box edges need to be
25866 drawn in reverse direction. */
25867 glyph->right_box_line_p = it->start_of_box_run_p;
25868 glyph->left_box_line_p = it->end_of_box_run_p;
25870 else
25872 glyph->left_box_line_p = it->start_of_box_run_p;
25873 glyph->right_box_line_p = it->end_of_box_run_p;
25875 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25876 || it->phys_descent > it->descent);
25877 glyph->padding_p = 0;
25878 glyph->glyph_not_available_p = 0;
25879 glyph->face_id = face_id;
25880 glyph->font_type = FONT_TYPE_UNKNOWN;
25881 if (it->bidi_p)
25883 glyph->resolved_level = it->bidi_it.resolved_level;
25884 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25885 emacs_abort ();
25886 glyph->bidi_type = it->bidi_it.type;
25888 ++it->glyph_row->used[area];
25890 else
25891 IT_EXPAND_MATRIX_WIDTH (it, area);
25895 /* Produce a glyph for a glyphless character for iterator IT.
25896 IT->glyphless_method specifies which method to use for displaying
25897 the character. See the description of enum
25898 glyphless_display_method in dispextern.h for the detail.
25900 FOR_NO_FONT is nonzero if and only if this is for a character for
25901 which no font was found. ACRONYM, if non-nil, is an acronym string
25902 for the character. */
25904 static void
25905 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
25907 int face_id;
25908 struct face *face;
25909 struct font *font;
25910 int base_width, base_height, width, height;
25911 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
25912 int len;
25914 /* Get the metrics of the base font. We always refer to the current
25915 ASCII face. */
25916 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
25917 font = face->font ? face->font : FRAME_FONT (it->f);
25918 it->ascent = FONT_BASE (font) + font->baseline_offset;
25919 it->descent = FONT_DESCENT (font) - font->baseline_offset;
25920 base_height = it->ascent + it->descent;
25921 base_width = font->average_width;
25923 face_id = merge_glyphless_glyph_face (it);
25925 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
25927 it->pixel_width = THIN_SPACE_WIDTH;
25928 len = 0;
25929 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25931 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
25933 width = CHAR_WIDTH (it->c);
25934 if (width == 0)
25935 width = 1;
25936 else if (width > 4)
25937 width = 4;
25938 it->pixel_width = base_width * width;
25939 len = 0;
25940 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
25942 else
25944 char buf[7];
25945 const char *str;
25946 unsigned int code[6];
25947 int upper_len;
25948 int ascent, descent;
25949 struct font_metrics metrics_upper, metrics_lower;
25951 face = FACE_FROM_ID (it->f, face_id);
25952 font = face->font ? face->font : FRAME_FONT (it->f);
25953 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25955 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25957 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25958 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25959 if (CONSP (acronym))
25960 acronym = XCAR (acronym);
25961 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25963 else
25965 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25966 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25967 str = buf;
25969 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25970 code[len] = font->driver->encode_char (font, str[len]);
25971 upper_len = (len + 1) / 2;
25972 font->driver->text_extents (font, code, upper_len,
25973 &metrics_upper);
25974 font->driver->text_extents (font, code + upper_len, len - upper_len,
25975 &metrics_lower);
25979 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25980 width = max (metrics_upper.width, metrics_lower.width) + 4;
25981 upper_xoff = upper_yoff = 2; /* the typical case */
25982 if (base_width >= width)
25984 /* Align the upper to the left, the lower to the right. */
25985 it->pixel_width = base_width;
25986 lower_xoff = base_width - 2 - metrics_lower.width;
25988 else
25990 /* Center the shorter one. */
25991 it->pixel_width = width;
25992 if (metrics_upper.width >= metrics_lower.width)
25993 lower_xoff = (width - metrics_lower.width) / 2;
25994 else
25996 /* FIXME: This code doesn't look right. It formerly was
25997 missing the "lower_xoff = 0;", which couldn't have
25998 been right since it left lower_xoff uninitialized. */
25999 lower_xoff = 0;
26000 upper_xoff = (width - metrics_upper.width) / 2;
26004 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26005 top, bottom, and between upper and lower strings. */
26006 height = (metrics_upper.ascent + metrics_upper.descent
26007 + metrics_lower.ascent + metrics_lower.descent) + 5;
26008 /* Center vertically.
26009 H:base_height, D:base_descent
26010 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26012 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26013 descent = D - H/2 + h/2;
26014 lower_yoff = descent - 2 - ld;
26015 upper_yoff = lower_yoff - la - 1 - ud; */
26016 ascent = - (it->descent - (base_height + height + 1) / 2);
26017 descent = it->descent - (base_height - height) / 2;
26018 lower_yoff = descent - 2 - metrics_lower.descent;
26019 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26020 - metrics_upper.descent);
26021 /* Don't make the height shorter than the base height. */
26022 if (height > base_height)
26024 it->ascent = ascent;
26025 it->descent = descent;
26029 it->phys_ascent = it->ascent;
26030 it->phys_descent = it->descent;
26031 if (it->glyph_row)
26032 append_glyphless_glyph (it, face_id, for_no_font, len,
26033 upper_xoff, upper_yoff,
26034 lower_xoff, lower_yoff);
26035 it->nglyphs = 1;
26036 take_vertical_position_into_account (it);
26040 /* RIF:
26041 Produce glyphs/get display metrics for the display element IT is
26042 loaded with. See the description of struct it in dispextern.h
26043 for an overview of struct it. */
26045 void
26046 x_produce_glyphs (struct it *it)
26048 int extra_line_spacing = it->extra_line_spacing;
26050 it->glyph_not_available_p = 0;
26052 if (it->what == IT_CHARACTER)
26054 XChar2b char2b;
26055 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26056 struct font *font = face->font;
26057 struct font_metrics *pcm = NULL;
26058 int boff; /* Baseline offset. */
26060 if (font == NULL)
26062 /* When no suitable font is found, display this character by
26063 the method specified in the first extra slot of
26064 Vglyphless_char_display. */
26065 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26067 eassert (it->what == IT_GLYPHLESS);
26068 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26069 goto done;
26072 boff = font->baseline_offset;
26073 if (font->vertical_centering)
26074 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26076 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26078 int stretched_p;
26080 it->nglyphs = 1;
26082 if (it->override_ascent >= 0)
26084 it->ascent = it->override_ascent;
26085 it->descent = it->override_descent;
26086 boff = it->override_boff;
26088 else
26090 it->ascent = FONT_BASE (font) + boff;
26091 it->descent = FONT_DESCENT (font) - boff;
26094 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26096 pcm = get_per_char_metric (font, &char2b);
26097 if (pcm->width == 0
26098 && pcm->rbearing == 0 && pcm->lbearing == 0)
26099 pcm = NULL;
26102 if (pcm)
26104 it->phys_ascent = pcm->ascent + boff;
26105 it->phys_descent = pcm->descent - boff;
26106 it->pixel_width = pcm->width;
26108 else
26110 it->glyph_not_available_p = 1;
26111 it->phys_ascent = it->ascent;
26112 it->phys_descent = it->descent;
26113 it->pixel_width = font->space_width;
26116 if (it->constrain_row_ascent_descent_p)
26118 if (it->descent > it->max_descent)
26120 it->ascent += it->descent - it->max_descent;
26121 it->descent = it->max_descent;
26123 if (it->ascent > it->max_ascent)
26125 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26126 it->ascent = it->max_ascent;
26128 it->phys_ascent = min (it->phys_ascent, it->ascent);
26129 it->phys_descent = min (it->phys_descent, it->descent);
26130 extra_line_spacing = 0;
26133 /* If this is a space inside a region of text with
26134 `space-width' property, change its width. */
26135 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26136 if (stretched_p)
26137 it->pixel_width *= XFLOATINT (it->space_width);
26139 /* If face has a box, add the box thickness to the character
26140 height. If character has a box line to the left and/or
26141 right, add the box line width to the character's width. */
26142 if (face->box != FACE_NO_BOX)
26144 int thick = face->box_line_width;
26146 if (thick > 0)
26148 it->ascent += thick;
26149 it->descent += thick;
26151 else
26152 thick = -thick;
26154 if (it->start_of_box_run_p)
26155 it->pixel_width += thick;
26156 if (it->end_of_box_run_p)
26157 it->pixel_width += thick;
26160 /* If face has an overline, add the height of the overline
26161 (1 pixel) and a 1 pixel margin to the character height. */
26162 if (face->overline_p)
26163 it->ascent += overline_margin;
26165 if (it->constrain_row_ascent_descent_p)
26167 if (it->ascent > it->max_ascent)
26168 it->ascent = it->max_ascent;
26169 if (it->descent > it->max_descent)
26170 it->descent = it->max_descent;
26173 take_vertical_position_into_account (it);
26175 /* If we have to actually produce glyphs, do it. */
26176 if (it->glyph_row)
26178 if (stretched_p)
26180 /* Translate a space with a `space-width' property
26181 into a stretch glyph. */
26182 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26183 / FONT_HEIGHT (font));
26184 append_stretch_glyph (it, it->object, it->pixel_width,
26185 it->ascent + it->descent, ascent);
26187 else
26188 append_glyph (it);
26190 /* If characters with lbearing or rbearing are displayed
26191 in this line, record that fact in a flag of the
26192 glyph row. This is used to optimize X output code. */
26193 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26194 it->glyph_row->contains_overlapping_glyphs_p = 1;
26196 if (! stretched_p && it->pixel_width == 0)
26197 /* We assure that all visible glyphs have at least 1-pixel
26198 width. */
26199 it->pixel_width = 1;
26201 else if (it->char_to_display == '\n')
26203 /* A newline has no width, but we need the height of the
26204 line. But if previous part of the line sets a height,
26205 don't increase that height. */
26207 Lisp_Object height;
26208 Lisp_Object total_height = Qnil;
26210 it->override_ascent = -1;
26211 it->pixel_width = 0;
26212 it->nglyphs = 0;
26214 height = get_it_property (it, Qline_height);
26215 /* Split (line-height total-height) list. */
26216 if (CONSP (height)
26217 && CONSP (XCDR (height))
26218 && NILP (XCDR (XCDR (height))))
26220 total_height = XCAR (XCDR (height));
26221 height = XCAR (height);
26223 height = calc_line_height_property (it, height, font, boff, 1);
26225 if (it->override_ascent >= 0)
26227 it->ascent = it->override_ascent;
26228 it->descent = it->override_descent;
26229 boff = it->override_boff;
26231 else
26233 it->ascent = FONT_BASE (font) + boff;
26234 it->descent = FONT_DESCENT (font) - boff;
26237 if (EQ (height, Qt))
26239 if (it->descent > it->max_descent)
26241 it->ascent += it->descent - it->max_descent;
26242 it->descent = it->max_descent;
26244 if (it->ascent > it->max_ascent)
26246 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26247 it->ascent = it->max_ascent;
26249 it->phys_ascent = min (it->phys_ascent, it->ascent);
26250 it->phys_descent = min (it->phys_descent, it->descent);
26251 it->constrain_row_ascent_descent_p = 1;
26252 extra_line_spacing = 0;
26254 else
26256 Lisp_Object spacing;
26258 it->phys_ascent = it->ascent;
26259 it->phys_descent = it->descent;
26261 if ((it->max_ascent > 0 || it->max_descent > 0)
26262 && face->box != FACE_NO_BOX
26263 && face->box_line_width > 0)
26265 it->ascent += face->box_line_width;
26266 it->descent += face->box_line_width;
26268 if (!NILP (height)
26269 && XINT (height) > it->ascent + it->descent)
26270 it->ascent = XINT (height) - it->descent;
26272 if (!NILP (total_height))
26273 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26274 else
26276 spacing = get_it_property (it, Qline_spacing);
26277 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26279 if (INTEGERP (spacing))
26281 extra_line_spacing = XINT (spacing);
26282 if (!NILP (total_height))
26283 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26287 else /* i.e. (it->char_to_display == '\t') */
26289 if (font->space_width > 0)
26291 int tab_width = it->tab_width * font->space_width;
26292 int x = it->current_x + it->continuation_lines_width;
26293 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26295 /* If the distance from the current position to the next tab
26296 stop is less than a space character width, use the
26297 tab stop after that. */
26298 if (next_tab_x - x < font->space_width)
26299 next_tab_x += tab_width;
26301 it->pixel_width = next_tab_x - x;
26302 it->nglyphs = 1;
26303 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26304 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26306 if (it->glyph_row)
26308 append_stretch_glyph (it, it->object, it->pixel_width,
26309 it->ascent + it->descent, it->ascent);
26312 else
26314 it->pixel_width = 0;
26315 it->nglyphs = 1;
26319 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26321 /* A static composition.
26323 Note: A composition is represented as one glyph in the
26324 glyph matrix. There are no padding glyphs.
26326 Important note: pixel_width, ascent, and descent are the
26327 values of what is drawn by draw_glyphs (i.e. the values of
26328 the overall glyphs composed). */
26329 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26330 int boff; /* baseline offset */
26331 struct composition *cmp = composition_table[it->cmp_it.id];
26332 int glyph_len = cmp->glyph_len;
26333 struct font *font = face->font;
26335 it->nglyphs = 1;
26337 /* If we have not yet calculated pixel size data of glyphs of
26338 the composition for the current face font, calculate them
26339 now. Theoretically, we have to check all fonts for the
26340 glyphs, but that requires much time and memory space. So,
26341 here we check only the font of the first glyph. This may
26342 lead to incorrect display, but it's very rare, and C-l
26343 (recenter-top-bottom) can correct the display anyway. */
26344 if (! cmp->font || cmp->font != font)
26346 /* Ascent and descent of the font of the first character
26347 of this composition (adjusted by baseline offset).
26348 Ascent and descent of overall glyphs should not be less
26349 than these, respectively. */
26350 int font_ascent, font_descent, font_height;
26351 /* Bounding box of the overall glyphs. */
26352 int leftmost, rightmost, lowest, highest;
26353 int lbearing, rbearing;
26354 int i, width, ascent, descent;
26355 int left_padded = 0, right_padded = 0;
26356 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26357 XChar2b char2b;
26358 struct font_metrics *pcm;
26359 int font_not_found_p;
26360 ptrdiff_t pos;
26362 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26363 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26364 break;
26365 if (glyph_len < cmp->glyph_len)
26366 right_padded = 1;
26367 for (i = 0; i < glyph_len; i++)
26369 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26370 break;
26371 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26373 if (i > 0)
26374 left_padded = 1;
26376 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26377 : IT_CHARPOS (*it));
26378 /* If no suitable font is found, use the default font. */
26379 font_not_found_p = font == NULL;
26380 if (font_not_found_p)
26382 face = face->ascii_face;
26383 font = face->font;
26385 boff = font->baseline_offset;
26386 if (font->vertical_centering)
26387 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26388 font_ascent = FONT_BASE (font) + boff;
26389 font_descent = FONT_DESCENT (font) - boff;
26390 font_height = FONT_HEIGHT (font);
26392 cmp->font = font;
26394 pcm = NULL;
26395 if (! font_not_found_p)
26397 get_char_face_and_encoding (it->f, c, it->face_id,
26398 &char2b, 0);
26399 pcm = get_per_char_metric (font, &char2b);
26402 /* Initialize the bounding box. */
26403 if (pcm)
26405 width = cmp->glyph_len > 0 ? pcm->width : 0;
26406 ascent = pcm->ascent;
26407 descent = pcm->descent;
26408 lbearing = pcm->lbearing;
26409 rbearing = pcm->rbearing;
26411 else
26413 width = cmp->glyph_len > 0 ? font->space_width : 0;
26414 ascent = FONT_BASE (font);
26415 descent = FONT_DESCENT (font);
26416 lbearing = 0;
26417 rbearing = width;
26420 rightmost = width;
26421 leftmost = 0;
26422 lowest = - descent + boff;
26423 highest = ascent + boff;
26425 if (! font_not_found_p
26426 && font->default_ascent
26427 && CHAR_TABLE_P (Vuse_default_ascent)
26428 && !NILP (Faref (Vuse_default_ascent,
26429 make_number (it->char_to_display))))
26430 highest = font->default_ascent + boff;
26432 /* Draw the first glyph at the normal position. It may be
26433 shifted to right later if some other glyphs are drawn
26434 at the left. */
26435 cmp->offsets[i * 2] = 0;
26436 cmp->offsets[i * 2 + 1] = boff;
26437 cmp->lbearing = lbearing;
26438 cmp->rbearing = rbearing;
26440 /* Set cmp->offsets for the remaining glyphs. */
26441 for (i++; i < glyph_len; i++)
26443 int left, right, btm, top;
26444 int ch = COMPOSITION_GLYPH (cmp, i);
26445 int face_id;
26446 struct face *this_face;
26448 if (ch == '\t')
26449 ch = ' ';
26450 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26451 this_face = FACE_FROM_ID (it->f, face_id);
26452 font = this_face->font;
26454 if (font == NULL)
26455 pcm = NULL;
26456 else
26458 get_char_face_and_encoding (it->f, ch, face_id,
26459 &char2b, 0);
26460 pcm = get_per_char_metric (font, &char2b);
26462 if (! pcm)
26463 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26464 else
26466 width = pcm->width;
26467 ascent = pcm->ascent;
26468 descent = pcm->descent;
26469 lbearing = pcm->lbearing;
26470 rbearing = pcm->rbearing;
26471 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26473 /* Relative composition with or without
26474 alternate chars. */
26475 left = (leftmost + rightmost - width) / 2;
26476 btm = - descent + boff;
26477 if (font->relative_compose
26478 && (! CHAR_TABLE_P (Vignore_relative_composition)
26479 || NILP (Faref (Vignore_relative_composition,
26480 make_number (ch)))))
26483 if (- descent >= font->relative_compose)
26484 /* One extra pixel between two glyphs. */
26485 btm = highest + 1;
26486 else if (ascent <= 0)
26487 /* One extra pixel between two glyphs. */
26488 btm = lowest - 1 - ascent - descent;
26491 else
26493 /* A composition rule is specified by an integer
26494 value that encodes global and new reference
26495 points (GREF and NREF). GREF and NREF are
26496 specified by numbers as below:
26498 0---1---2 -- ascent
26502 9--10--11 -- center
26504 ---3---4---5--- baseline
26506 6---7---8 -- descent
26508 int rule = COMPOSITION_RULE (cmp, i);
26509 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26511 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26512 grefx = gref % 3, nrefx = nref % 3;
26513 grefy = gref / 3, nrefy = nref / 3;
26514 if (xoff)
26515 xoff = font_height * (xoff - 128) / 256;
26516 if (yoff)
26517 yoff = font_height * (yoff - 128) / 256;
26519 left = (leftmost
26520 + grefx * (rightmost - leftmost) / 2
26521 - nrefx * width / 2
26522 + xoff);
26524 btm = ((grefy == 0 ? highest
26525 : grefy == 1 ? 0
26526 : grefy == 2 ? lowest
26527 : (highest + lowest) / 2)
26528 - (nrefy == 0 ? ascent + descent
26529 : nrefy == 1 ? descent - boff
26530 : nrefy == 2 ? 0
26531 : (ascent + descent) / 2)
26532 + yoff);
26535 cmp->offsets[i * 2] = left;
26536 cmp->offsets[i * 2 + 1] = btm + descent;
26538 /* Update the bounding box of the overall glyphs. */
26539 if (width > 0)
26541 right = left + width;
26542 if (left < leftmost)
26543 leftmost = left;
26544 if (right > rightmost)
26545 rightmost = right;
26547 top = btm + descent + ascent;
26548 if (top > highest)
26549 highest = top;
26550 if (btm < lowest)
26551 lowest = btm;
26553 if (cmp->lbearing > left + lbearing)
26554 cmp->lbearing = left + lbearing;
26555 if (cmp->rbearing < left + rbearing)
26556 cmp->rbearing = left + rbearing;
26560 /* If there are glyphs whose x-offsets are negative,
26561 shift all glyphs to the right and make all x-offsets
26562 non-negative. */
26563 if (leftmost < 0)
26565 for (i = 0; i < cmp->glyph_len; i++)
26566 cmp->offsets[i * 2] -= leftmost;
26567 rightmost -= leftmost;
26568 cmp->lbearing -= leftmost;
26569 cmp->rbearing -= leftmost;
26572 if (left_padded && cmp->lbearing < 0)
26574 for (i = 0; i < cmp->glyph_len; i++)
26575 cmp->offsets[i * 2] -= cmp->lbearing;
26576 rightmost -= cmp->lbearing;
26577 cmp->rbearing -= cmp->lbearing;
26578 cmp->lbearing = 0;
26580 if (right_padded && rightmost < cmp->rbearing)
26582 rightmost = cmp->rbearing;
26585 cmp->pixel_width = rightmost;
26586 cmp->ascent = highest;
26587 cmp->descent = - lowest;
26588 if (cmp->ascent < font_ascent)
26589 cmp->ascent = font_ascent;
26590 if (cmp->descent < font_descent)
26591 cmp->descent = font_descent;
26594 if (it->glyph_row
26595 && (cmp->lbearing < 0
26596 || cmp->rbearing > cmp->pixel_width))
26597 it->glyph_row->contains_overlapping_glyphs_p = 1;
26599 it->pixel_width = cmp->pixel_width;
26600 it->ascent = it->phys_ascent = cmp->ascent;
26601 it->descent = it->phys_descent = cmp->descent;
26602 if (face->box != FACE_NO_BOX)
26604 int thick = face->box_line_width;
26606 if (thick > 0)
26608 it->ascent += thick;
26609 it->descent += thick;
26611 else
26612 thick = - thick;
26614 if (it->start_of_box_run_p)
26615 it->pixel_width += thick;
26616 if (it->end_of_box_run_p)
26617 it->pixel_width += thick;
26620 /* If face has an overline, add the height of the overline
26621 (1 pixel) and a 1 pixel margin to the character height. */
26622 if (face->overline_p)
26623 it->ascent += overline_margin;
26625 take_vertical_position_into_account (it);
26626 if (it->ascent < 0)
26627 it->ascent = 0;
26628 if (it->descent < 0)
26629 it->descent = 0;
26631 if (it->glyph_row && cmp->glyph_len > 0)
26632 append_composite_glyph (it);
26634 else if (it->what == IT_COMPOSITION)
26636 /* A dynamic (automatic) composition. */
26637 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26638 Lisp_Object gstring;
26639 struct font_metrics metrics;
26641 it->nglyphs = 1;
26643 gstring = composition_gstring_from_id (it->cmp_it.id);
26644 it->pixel_width
26645 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26646 &metrics);
26647 if (it->glyph_row
26648 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26649 it->glyph_row->contains_overlapping_glyphs_p = 1;
26650 it->ascent = it->phys_ascent = metrics.ascent;
26651 it->descent = it->phys_descent = metrics.descent;
26652 if (face->box != FACE_NO_BOX)
26654 int thick = face->box_line_width;
26656 if (thick > 0)
26658 it->ascent += thick;
26659 it->descent += thick;
26661 else
26662 thick = - thick;
26664 if (it->start_of_box_run_p)
26665 it->pixel_width += thick;
26666 if (it->end_of_box_run_p)
26667 it->pixel_width += thick;
26669 /* If face has an overline, add the height of the overline
26670 (1 pixel) and a 1 pixel margin to the character height. */
26671 if (face->overline_p)
26672 it->ascent += overline_margin;
26673 take_vertical_position_into_account (it);
26674 if (it->ascent < 0)
26675 it->ascent = 0;
26676 if (it->descent < 0)
26677 it->descent = 0;
26679 if (it->glyph_row)
26680 append_composite_glyph (it);
26682 else if (it->what == IT_GLYPHLESS)
26683 produce_glyphless_glyph (it, 0, Qnil);
26684 else if (it->what == IT_IMAGE)
26685 produce_image_glyph (it);
26686 else if (it->what == IT_STRETCH)
26687 produce_stretch_glyph (it);
26689 done:
26690 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26691 because this isn't true for images with `:ascent 100'. */
26692 eassert (it->ascent >= 0 && it->descent >= 0);
26693 if (it->area == TEXT_AREA)
26694 it->current_x += it->pixel_width;
26696 if (extra_line_spacing > 0)
26698 it->descent += extra_line_spacing;
26699 if (extra_line_spacing > it->max_extra_line_spacing)
26700 it->max_extra_line_spacing = extra_line_spacing;
26703 it->max_ascent = max (it->max_ascent, it->ascent);
26704 it->max_descent = max (it->max_descent, it->descent);
26705 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26706 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26709 /* EXPORT for RIF:
26710 Output LEN glyphs starting at START at the nominal cursor position.
26711 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26712 being updated, and UPDATED_AREA is the area of that row being updated. */
26714 void
26715 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26716 struct glyph *start, enum glyph_row_area updated_area, int len)
26718 int x, hpos, chpos = w->phys_cursor.hpos;
26720 eassert (updated_row);
26721 /* When the window is hscrolled, cursor hpos can legitimately be out
26722 of bounds, but we draw the cursor at the corresponding window
26723 margin in that case. */
26724 if (!updated_row->reversed_p && chpos < 0)
26725 chpos = 0;
26726 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26727 chpos = updated_row->used[TEXT_AREA] - 1;
26729 block_input ();
26731 /* Write glyphs. */
26733 hpos = start - updated_row->glyphs[updated_area];
26734 x = draw_glyphs (w, w->output_cursor.x,
26735 updated_row, updated_area,
26736 hpos, hpos + len,
26737 DRAW_NORMAL_TEXT, 0);
26739 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26740 if (updated_area == TEXT_AREA
26741 && w->phys_cursor_on_p
26742 && w->phys_cursor.vpos == w->output_cursor.vpos
26743 && chpos >= hpos
26744 && chpos < hpos + len)
26745 w->phys_cursor_on_p = 0;
26747 unblock_input ();
26749 /* Advance the output cursor. */
26750 w->output_cursor.hpos += len;
26751 w->output_cursor.x = x;
26755 /* EXPORT for RIF:
26756 Insert LEN glyphs from START at the nominal cursor position. */
26758 void
26759 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26760 struct glyph *start, enum glyph_row_area updated_area, int len)
26762 struct frame *f;
26763 int line_height, shift_by_width, shifted_region_width;
26764 struct glyph_row *row;
26765 struct glyph *glyph;
26766 int frame_x, frame_y;
26767 ptrdiff_t hpos;
26769 eassert (updated_row);
26770 block_input ();
26771 f = XFRAME (WINDOW_FRAME (w));
26773 /* Get the height of the line we are in. */
26774 row = updated_row;
26775 line_height = row->height;
26777 /* Get the width of the glyphs to insert. */
26778 shift_by_width = 0;
26779 for (glyph = start; glyph < start + len; ++glyph)
26780 shift_by_width += glyph->pixel_width;
26782 /* Get the width of the region to shift right. */
26783 shifted_region_width = (window_box_width (w, updated_area)
26784 - w->output_cursor.x
26785 - shift_by_width);
26787 /* Shift right. */
26788 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26789 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26791 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26792 line_height, shift_by_width);
26794 /* Write the glyphs. */
26795 hpos = start - row->glyphs[updated_area];
26796 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26797 hpos, hpos + len,
26798 DRAW_NORMAL_TEXT, 0);
26800 /* Advance the output cursor. */
26801 w->output_cursor.hpos += len;
26802 w->output_cursor.x += shift_by_width;
26803 unblock_input ();
26807 /* EXPORT for RIF:
26808 Erase the current text line from the nominal cursor position
26809 (inclusive) to pixel column TO_X (exclusive). The idea is that
26810 everything from TO_X onward is already erased.
26812 TO_X is a pixel position relative to UPDATED_AREA of currently
26813 updated window W. TO_X == -1 means clear to the end of this area. */
26815 void
26816 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26817 enum glyph_row_area updated_area, int to_x)
26819 struct frame *f;
26820 int max_x, min_y, max_y;
26821 int from_x, from_y, to_y;
26823 eassert (updated_row);
26824 f = XFRAME (w->frame);
26826 if (updated_row->full_width_p)
26827 max_x = (WINDOW_PIXEL_WIDTH (w)
26828 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26829 else
26830 max_x = window_box_width (w, updated_area);
26831 max_y = window_text_bottom_y (w);
26833 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26834 of window. For TO_X > 0, truncate to end of drawing area. */
26835 if (to_x == 0)
26836 return;
26837 else if (to_x < 0)
26838 to_x = max_x;
26839 else
26840 to_x = min (to_x, max_x);
26842 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26844 /* Notice if the cursor will be cleared by this operation. */
26845 if (!updated_row->full_width_p)
26846 notice_overwritten_cursor (w, updated_area,
26847 w->output_cursor.x, -1,
26848 updated_row->y,
26849 MATRIX_ROW_BOTTOM_Y (updated_row));
26851 from_x = w->output_cursor.x;
26853 /* Translate to frame coordinates. */
26854 if (updated_row->full_width_p)
26856 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26857 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26859 else
26861 int area_left = window_box_left (w, updated_area);
26862 from_x += area_left;
26863 to_x += area_left;
26866 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
26867 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
26868 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
26870 /* Prevent inadvertently clearing to end of the X window. */
26871 if (to_x > from_x && to_y > from_y)
26873 block_input ();
26874 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
26875 to_x - from_x, to_y - from_y);
26876 unblock_input ();
26880 #endif /* HAVE_WINDOW_SYSTEM */
26884 /***********************************************************************
26885 Cursor types
26886 ***********************************************************************/
26888 /* Value is the internal representation of the specified cursor type
26889 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
26890 of the bar cursor. */
26892 static enum text_cursor_kinds
26893 get_specified_cursor_type (Lisp_Object arg, int *width)
26895 enum text_cursor_kinds type;
26897 if (NILP (arg))
26898 return NO_CURSOR;
26900 if (EQ (arg, Qbox))
26901 return FILLED_BOX_CURSOR;
26903 if (EQ (arg, Qhollow))
26904 return HOLLOW_BOX_CURSOR;
26906 if (EQ (arg, Qbar))
26908 *width = 2;
26909 return BAR_CURSOR;
26912 if (CONSP (arg)
26913 && EQ (XCAR (arg), Qbar)
26914 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26916 *width = XINT (XCDR (arg));
26917 return BAR_CURSOR;
26920 if (EQ (arg, Qhbar))
26922 *width = 2;
26923 return HBAR_CURSOR;
26926 if (CONSP (arg)
26927 && EQ (XCAR (arg), Qhbar)
26928 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
26930 *width = XINT (XCDR (arg));
26931 return HBAR_CURSOR;
26934 /* Treat anything unknown as "hollow box cursor".
26935 It was bad to signal an error; people have trouble fixing
26936 .Xdefaults with Emacs, when it has something bad in it. */
26937 type = HOLLOW_BOX_CURSOR;
26939 return type;
26942 /* Set the default cursor types for specified frame. */
26943 void
26944 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
26946 int width = 1;
26947 Lisp_Object tem;
26949 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26950 FRAME_CURSOR_WIDTH (f) = width;
26952 /* By default, set up the blink-off state depending on the on-state. */
26954 tem = Fassoc (arg, Vblink_cursor_alist);
26955 if (!NILP (tem))
26957 FRAME_BLINK_OFF_CURSOR (f)
26958 = get_specified_cursor_type (XCDR (tem), &width);
26959 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26961 else
26962 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26964 /* Make sure the cursor gets redrawn. */
26965 f->cursor_type_changed = 1;
26969 #ifdef HAVE_WINDOW_SYSTEM
26971 /* Return the cursor we want to be displayed in window W. Return
26972 width of bar/hbar cursor through WIDTH arg. Return with
26973 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26974 (i.e. if the `system caret' should track this cursor).
26976 In a mini-buffer window, we want the cursor only to appear if we
26977 are reading input from this window. For the selected window, we
26978 want the cursor type given by the frame parameter or buffer local
26979 setting of cursor-type. If explicitly marked off, draw no cursor.
26980 In all other cases, we want a hollow box cursor. */
26982 static enum text_cursor_kinds
26983 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26984 int *active_cursor)
26986 struct frame *f = XFRAME (w->frame);
26987 struct buffer *b = XBUFFER (w->contents);
26988 int cursor_type = DEFAULT_CURSOR;
26989 Lisp_Object alt_cursor;
26990 int non_selected = 0;
26992 *active_cursor = 1;
26994 /* Echo area */
26995 if (cursor_in_echo_area
26996 && FRAME_HAS_MINIBUF_P (f)
26997 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26999 if (w == XWINDOW (echo_area_window))
27001 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27003 *width = FRAME_CURSOR_WIDTH (f);
27004 return FRAME_DESIRED_CURSOR (f);
27006 else
27007 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27010 *active_cursor = 0;
27011 non_selected = 1;
27014 /* Detect a nonselected window or nonselected frame. */
27015 else if (w != XWINDOW (f->selected_window)
27016 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27018 *active_cursor = 0;
27020 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27021 return NO_CURSOR;
27023 non_selected = 1;
27026 /* Never display a cursor in a window in which cursor-type is nil. */
27027 if (NILP (BVAR (b, cursor_type)))
27028 return NO_CURSOR;
27030 /* Get the normal cursor type for this window. */
27031 if (EQ (BVAR (b, cursor_type), Qt))
27033 cursor_type = FRAME_DESIRED_CURSOR (f);
27034 *width = FRAME_CURSOR_WIDTH (f);
27036 else
27037 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27039 /* Use cursor-in-non-selected-windows instead
27040 for non-selected window or frame. */
27041 if (non_selected)
27043 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27044 if (!EQ (Qt, alt_cursor))
27045 return get_specified_cursor_type (alt_cursor, width);
27046 /* t means modify the normal cursor type. */
27047 if (cursor_type == FILLED_BOX_CURSOR)
27048 cursor_type = HOLLOW_BOX_CURSOR;
27049 else if (cursor_type == BAR_CURSOR && *width > 1)
27050 --*width;
27051 return cursor_type;
27054 /* Use normal cursor if not blinked off. */
27055 if (!w->cursor_off_p)
27057 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27059 if (cursor_type == FILLED_BOX_CURSOR)
27061 /* Using a block cursor on large images can be very annoying.
27062 So use a hollow cursor for "large" images.
27063 If image is not transparent (no mask), also use hollow cursor. */
27064 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27065 if (img != NULL && IMAGEP (img->spec))
27067 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27068 where N = size of default frame font size.
27069 This should cover most of the "tiny" icons people may use. */
27070 if (!img->mask
27071 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27072 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27073 cursor_type = HOLLOW_BOX_CURSOR;
27076 else if (cursor_type != NO_CURSOR)
27078 /* Display current only supports BOX and HOLLOW cursors for images.
27079 So for now, unconditionally use a HOLLOW cursor when cursor is
27080 not a solid box cursor. */
27081 cursor_type = HOLLOW_BOX_CURSOR;
27084 return cursor_type;
27087 /* Cursor is blinked off, so determine how to "toggle" it. */
27089 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27090 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27091 return get_specified_cursor_type (XCDR (alt_cursor), width);
27093 /* Then see if frame has specified a specific blink off cursor type. */
27094 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27096 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27097 return FRAME_BLINK_OFF_CURSOR (f);
27100 #if 0
27101 /* Some people liked having a permanently visible blinking cursor,
27102 while others had very strong opinions against it. So it was
27103 decided to remove it. KFS 2003-09-03 */
27105 /* Finally perform built-in cursor blinking:
27106 filled box <-> hollow box
27107 wide [h]bar <-> narrow [h]bar
27108 narrow [h]bar <-> no cursor
27109 other type <-> no cursor */
27111 if (cursor_type == FILLED_BOX_CURSOR)
27112 return HOLLOW_BOX_CURSOR;
27114 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27116 *width = 1;
27117 return cursor_type;
27119 #endif
27121 return NO_CURSOR;
27125 /* Notice when the text cursor of window W has been completely
27126 overwritten by a drawing operation that outputs glyphs in AREA
27127 starting at X0 and ending at X1 in the line starting at Y0 and
27128 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27129 the rest of the line after X0 has been written. Y coordinates
27130 are window-relative. */
27132 static void
27133 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27134 int x0, int x1, int y0, int y1)
27136 int cx0, cx1, cy0, cy1;
27137 struct glyph_row *row;
27139 if (!w->phys_cursor_on_p)
27140 return;
27141 if (area != TEXT_AREA)
27142 return;
27144 if (w->phys_cursor.vpos < 0
27145 || w->phys_cursor.vpos >= w->current_matrix->nrows
27146 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27147 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27148 return;
27150 if (row->cursor_in_fringe_p)
27152 row->cursor_in_fringe_p = 0;
27153 draw_fringe_bitmap (w, row, row->reversed_p);
27154 w->phys_cursor_on_p = 0;
27155 return;
27158 cx0 = w->phys_cursor.x;
27159 cx1 = cx0 + w->phys_cursor_width;
27160 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27161 return;
27163 /* The cursor image will be completely removed from the
27164 screen if the output area intersects the cursor area in
27165 y-direction. When we draw in [y0 y1[, and some part of
27166 the cursor is at y < y0, that part must have been drawn
27167 before. When scrolling, the cursor is erased before
27168 actually scrolling, so we don't come here. When not
27169 scrolling, the rows above the old cursor row must have
27170 changed, and in this case these rows must have written
27171 over the cursor image.
27173 Likewise if part of the cursor is below y1, with the
27174 exception of the cursor being in the first blank row at
27175 the buffer and window end because update_text_area
27176 doesn't draw that row. (Except when it does, but
27177 that's handled in update_text_area.) */
27179 cy0 = w->phys_cursor.y;
27180 cy1 = cy0 + w->phys_cursor_height;
27181 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27182 return;
27184 w->phys_cursor_on_p = 0;
27187 #endif /* HAVE_WINDOW_SYSTEM */
27190 /************************************************************************
27191 Mouse Face
27192 ************************************************************************/
27194 #ifdef HAVE_WINDOW_SYSTEM
27196 /* EXPORT for RIF:
27197 Fix the display of area AREA of overlapping row ROW in window W
27198 with respect to the overlapping part OVERLAPS. */
27200 void
27201 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27202 enum glyph_row_area area, int overlaps)
27204 int i, x;
27206 block_input ();
27208 x = 0;
27209 for (i = 0; i < row->used[area];)
27211 if (row->glyphs[area][i].overlaps_vertically_p)
27213 int start = i, start_x = x;
27217 x += row->glyphs[area][i].pixel_width;
27218 ++i;
27220 while (i < row->used[area]
27221 && row->glyphs[area][i].overlaps_vertically_p);
27223 draw_glyphs (w, start_x, row, area,
27224 start, i,
27225 DRAW_NORMAL_TEXT, overlaps);
27227 else
27229 x += row->glyphs[area][i].pixel_width;
27230 ++i;
27234 unblock_input ();
27238 /* EXPORT:
27239 Draw the cursor glyph of window W in glyph row ROW. See the
27240 comment of draw_glyphs for the meaning of HL. */
27242 void
27243 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27244 enum draw_glyphs_face hl)
27246 /* If cursor hpos is out of bounds, don't draw garbage. This can
27247 happen in mini-buffer windows when switching between echo area
27248 glyphs and mini-buffer. */
27249 if ((row->reversed_p
27250 ? (w->phys_cursor.hpos >= 0)
27251 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27253 int on_p = w->phys_cursor_on_p;
27254 int x1;
27255 int hpos = w->phys_cursor.hpos;
27257 /* When the window is hscrolled, cursor hpos can legitimately be
27258 out of bounds, but we draw the cursor at the corresponding
27259 window margin in that case. */
27260 if (!row->reversed_p && hpos < 0)
27261 hpos = 0;
27262 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27263 hpos = row->used[TEXT_AREA] - 1;
27265 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27266 hl, 0);
27267 w->phys_cursor_on_p = on_p;
27269 if (hl == DRAW_CURSOR)
27270 w->phys_cursor_width = x1 - w->phys_cursor.x;
27271 /* When we erase the cursor, and ROW is overlapped by other
27272 rows, make sure that these overlapping parts of other rows
27273 are redrawn. */
27274 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27276 w->phys_cursor_width = x1 - w->phys_cursor.x;
27278 if (row > w->current_matrix->rows
27279 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27280 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27281 OVERLAPS_ERASED_CURSOR);
27283 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27284 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27285 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27286 OVERLAPS_ERASED_CURSOR);
27292 /* Erase the image of a cursor of window W from the screen. */
27294 #ifndef HAVE_NTGUI
27295 static
27296 #endif
27297 void
27298 erase_phys_cursor (struct window *w)
27300 struct frame *f = XFRAME (w->frame);
27301 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27302 int hpos = w->phys_cursor.hpos;
27303 int vpos = w->phys_cursor.vpos;
27304 int mouse_face_here_p = 0;
27305 struct glyph_matrix *active_glyphs = w->current_matrix;
27306 struct glyph_row *cursor_row;
27307 struct glyph *cursor_glyph;
27308 enum draw_glyphs_face hl;
27310 /* No cursor displayed or row invalidated => nothing to do on the
27311 screen. */
27312 if (w->phys_cursor_type == NO_CURSOR)
27313 goto mark_cursor_off;
27315 /* VPOS >= active_glyphs->nrows means that window has been resized.
27316 Don't bother to erase the cursor. */
27317 if (vpos >= active_glyphs->nrows)
27318 goto mark_cursor_off;
27320 /* If row containing cursor is marked invalid, there is nothing we
27321 can do. */
27322 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27323 if (!cursor_row->enabled_p)
27324 goto mark_cursor_off;
27326 /* If line spacing is > 0, old cursor may only be partially visible in
27327 window after split-window. So adjust visible height. */
27328 cursor_row->visible_height = min (cursor_row->visible_height,
27329 window_text_bottom_y (w) - cursor_row->y);
27331 /* If row is completely invisible, don't attempt to delete a cursor which
27332 isn't there. This can happen if cursor is at top of a window, and
27333 we switch to a buffer with a header line in that window. */
27334 if (cursor_row->visible_height <= 0)
27335 goto mark_cursor_off;
27337 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27338 if (cursor_row->cursor_in_fringe_p)
27340 cursor_row->cursor_in_fringe_p = 0;
27341 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27342 goto mark_cursor_off;
27345 /* This can happen when the new row is shorter than the old one.
27346 In this case, either draw_glyphs or clear_end_of_line
27347 should have cleared the cursor. Note that we wouldn't be
27348 able to erase the cursor in this case because we don't have a
27349 cursor glyph at hand. */
27350 if ((cursor_row->reversed_p
27351 ? (w->phys_cursor.hpos < 0)
27352 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27353 goto mark_cursor_off;
27355 /* When the window is hscrolled, cursor hpos can legitimately be out
27356 of bounds, but we draw the cursor at the corresponding window
27357 margin in that case. */
27358 if (!cursor_row->reversed_p && hpos < 0)
27359 hpos = 0;
27360 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27361 hpos = cursor_row->used[TEXT_AREA] - 1;
27363 /* If the cursor is in the mouse face area, redisplay that when
27364 we clear the cursor. */
27365 if (! NILP (hlinfo->mouse_face_window)
27366 && coords_in_mouse_face_p (w, hpos, vpos)
27367 /* Don't redraw the cursor's spot in mouse face if it is at the
27368 end of a line (on a newline). The cursor appears there, but
27369 mouse highlighting does not. */
27370 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27371 mouse_face_here_p = 1;
27373 /* Maybe clear the display under the cursor. */
27374 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27376 int x, y, left_x;
27377 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27378 int width;
27380 cursor_glyph = get_phys_cursor_glyph (w);
27381 if (cursor_glyph == NULL)
27382 goto mark_cursor_off;
27384 width = cursor_glyph->pixel_width;
27385 left_x = window_box_left_offset (w, TEXT_AREA);
27386 x = w->phys_cursor.x;
27387 if (x < left_x)
27388 width -= left_x - x;
27389 width = min (width, window_box_width (w, TEXT_AREA) - x);
27390 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27391 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
27393 if (width > 0)
27394 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27397 /* Erase the cursor by redrawing the character underneath it. */
27398 if (mouse_face_here_p)
27399 hl = DRAW_MOUSE_FACE;
27400 else
27401 hl = DRAW_NORMAL_TEXT;
27402 draw_phys_cursor_glyph (w, cursor_row, hl);
27404 mark_cursor_off:
27405 w->phys_cursor_on_p = 0;
27406 w->phys_cursor_type = NO_CURSOR;
27410 /* EXPORT:
27411 Display or clear cursor of window W. If ON is zero, clear the
27412 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27413 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27415 void
27416 display_and_set_cursor (struct window *w, bool on,
27417 int hpos, int vpos, int x, int y)
27419 struct frame *f = XFRAME (w->frame);
27420 int new_cursor_type;
27421 int new_cursor_width;
27422 int active_cursor;
27423 struct glyph_row *glyph_row;
27424 struct glyph *glyph;
27426 /* This is pointless on invisible frames, and dangerous on garbaged
27427 windows and frames; in the latter case, the frame or window may
27428 be in the midst of changing its size, and x and y may be off the
27429 window. */
27430 if (! FRAME_VISIBLE_P (f)
27431 || FRAME_GARBAGED_P (f)
27432 || vpos >= w->current_matrix->nrows
27433 || hpos >= w->current_matrix->matrix_w)
27434 return;
27436 /* If cursor is off and we want it off, return quickly. */
27437 if (!on && !w->phys_cursor_on_p)
27438 return;
27440 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27441 /* If cursor row is not enabled, we don't really know where to
27442 display the cursor. */
27443 if (!glyph_row->enabled_p)
27445 w->phys_cursor_on_p = 0;
27446 return;
27449 glyph = NULL;
27450 if (!glyph_row->exact_window_width_line_p
27451 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27452 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27454 eassert (input_blocked_p ());
27456 /* Set new_cursor_type to the cursor we want to be displayed. */
27457 new_cursor_type = get_window_cursor_type (w, glyph,
27458 &new_cursor_width, &active_cursor);
27460 /* If cursor is currently being shown and we don't want it to be or
27461 it is in the wrong place, or the cursor type is not what we want,
27462 erase it. */
27463 if (w->phys_cursor_on_p
27464 && (!on
27465 || w->phys_cursor.x != x
27466 || w->phys_cursor.y != y
27467 || new_cursor_type != w->phys_cursor_type
27468 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27469 && new_cursor_width != w->phys_cursor_width)))
27470 erase_phys_cursor (w);
27472 /* Don't check phys_cursor_on_p here because that flag is only set
27473 to zero in some cases where we know that the cursor has been
27474 completely erased, to avoid the extra work of erasing the cursor
27475 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27476 still not be visible, or it has only been partly erased. */
27477 if (on)
27479 w->phys_cursor_ascent = glyph_row->ascent;
27480 w->phys_cursor_height = glyph_row->height;
27482 /* Set phys_cursor_.* before x_draw_.* is called because some
27483 of them may need the information. */
27484 w->phys_cursor.x = x;
27485 w->phys_cursor.y = glyph_row->y;
27486 w->phys_cursor.hpos = hpos;
27487 w->phys_cursor.vpos = vpos;
27490 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27491 new_cursor_type, new_cursor_width,
27492 on, active_cursor);
27496 /* Switch the display of W's cursor on or off, according to the value
27497 of ON. */
27499 static void
27500 update_window_cursor (struct window *w, bool on)
27502 /* Don't update cursor in windows whose frame is in the process
27503 of being deleted. */
27504 if (w->current_matrix)
27506 int hpos = w->phys_cursor.hpos;
27507 int vpos = w->phys_cursor.vpos;
27508 struct glyph_row *row;
27510 if (vpos >= w->current_matrix->nrows
27511 || hpos >= w->current_matrix->matrix_w)
27512 return;
27514 row = MATRIX_ROW (w->current_matrix, vpos);
27516 /* When the window is hscrolled, cursor hpos can legitimately be
27517 out of bounds, but we draw the cursor at the corresponding
27518 window margin in that case. */
27519 if (!row->reversed_p && hpos < 0)
27520 hpos = 0;
27521 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27522 hpos = row->used[TEXT_AREA] - 1;
27524 block_input ();
27525 display_and_set_cursor (w, on, hpos, vpos,
27526 w->phys_cursor.x, w->phys_cursor.y);
27527 unblock_input ();
27532 /* Call update_window_cursor with parameter ON_P on all leaf windows
27533 in the window tree rooted at W. */
27535 static void
27536 update_cursor_in_window_tree (struct window *w, bool on_p)
27538 while (w)
27540 if (WINDOWP (w->contents))
27541 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27542 else
27543 update_window_cursor (w, on_p);
27545 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27550 /* EXPORT:
27551 Display the cursor on window W, or clear it, according to ON_P.
27552 Don't change the cursor's position. */
27554 void
27555 x_update_cursor (struct frame *f, bool on_p)
27557 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27561 /* EXPORT:
27562 Clear the cursor of window W to background color, and mark the
27563 cursor as not shown. This is used when the text where the cursor
27564 is about to be rewritten. */
27566 void
27567 x_clear_cursor (struct window *w)
27569 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27570 update_window_cursor (w, 0);
27573 #endif /* HAVE_WINDOW_SYSTEM */
27575 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27576 and MSDOS. */
27577 static void
27578 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27579 int start_hpos, int end_hpos,
27580 enum draw_glyphs_face draw)
27582 #ifdef HAVE_WINDOW_SYSTEM
27583 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27585 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27586 return;
27588 #endif
27589 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27590 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27591 #endif
27594 /* Display the active region described by mouse_face_* according to DRAW. */
27596 static void
27597 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27599 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27600 struct frame *f = XFRAME (WINDOW_FRAME (w));
27602 if (/* If window is in the process of being destroyed, don't bother
27603 to do anything. */
27604 w->current_matrix != NULL
27605 /* Don't update mouse highlight if hidden. */
27606 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27607 /* Recognize when we are called to operate on rows that don't exist
27608 anymore. This can happen when a window is split. */
27609 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27611 int phys_cursor_on_p = w->phys_cursor_on_p;
27612 struct glyph_row *row, *first, *last;
27614 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27615 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27617 for (row = first; row <= last && row->enabled_p; ++row)
27619 int start_hpos, end_hpos, start_x;
27621 /* For all but the first row, the highlight starts at column 0. */
27622 if (row == first)
27624 /* R2L rows have BEG and END in reversed order, but the
27625 screen drawing geometry is always left to right. So
27626 we need to mirror the beginning and end of the
27627 highlighted area in R2L rows. */
27628 if (!row->reversed_p)
27630 start_hpos = hlinfo->mouse_face_beg_col;
27631 start_x = hlinfo->mouse_face_beg_x;
27633 else if (row == last)
27635 start_hpos = hlinfo->mouse_face_end_col;
27636 start_x = hlinfo->mouse_face_end_x;
27638 else
27640 start_hpos = 0;
27641 start_x = 0;
27644 else if (row->reversed_p && row == last)
27646 start_hpos = hlinfo->mouse_face_end_col;
27647 start_x = hlinfo->mouse_face_end_x;
27649 else
27651 start_hpos = 0;
27652 start_x = 0;
27655 if (row == last)
27657 if (!row->reversed_p)
27658 end_hpos = hlinfo->mouse_face_end_col;
27659 else if (row == first)
27660 end_hpos = hlinfo->mouse_face_beg_col;
27661 else
27663 end_hpos = row->used[TEXT_AREA];
27664 if (draw == DRAW_NORMAL_TEXT)
27665 row->fill_line_p = 1; /* Clear to end of line */
27668 else if (row->reversed_p && row == first)
27669 end_hpos = hlinfo->mouse_face_beg_col;
27670 else
27672 end_hpos = row->used[TEXT_AREA];
27673 if (draw == DRAW_NORMAL_TEXT)
27674 row->fill_line_p = 1; /* Clear to end of line */
27677 if (end_hpos > start_hpos)
27679 draw_row_with_mouse_face (w, start_x, row,
27680 start_hpos, end_hpos, draw);
27682 row->mouse_face_p
27683 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27687 #ifdef HAVE_WINDOW_SYSTEM
27688 /* When we've written over the cursor, arrange for it to
27689 be displayed again. */
27690 if (FRAME_WINDOW_P (f)
27691 && phys_cursor_on_p && !w->phys_cursor_on_p)
27693 int hpos = w->phys_cursor.hpos;
27695 /* When the window is hscrolled, cursor hpos can legitimately be
27696 out of bounds, but we draw the cursor at the corresponding
27697 window margin in that case. */
27698 if (!row->reversed_p && hpos < 0)
27699 hpos = 0;
27700 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27701 hpos = row->used[TEXT_AREA] - 1;
27703 block_input ();
27704 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27705 w->phys_cursor.x, w->phys_cursor.y);
27706 unblock_input ();
27708 #endif /* HAVE_WINDOW_SYSTEM */
27711 #ifdef HAVE_WINDOW_SYSTEM
27712 /* Change the mouse cursor. */
27713 if (FRAME_WINDOW_P (f))
27715 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27716 if (draw == DRAW_NORMAL_TEXT
27717 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27718 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27719 else
27720 #endif
27721 if (draw == DRAW_MOUSE_FACE)
27722 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27723 else
27724 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27726 #endif /* HAVE_WINDOW_SYSTEM */
27729 /* EXPORT:
27730 Clear out the mouse-highlighted active region.
27731 Redraw it un-highlighted first. Value is non-zero if mouse
27732 face was actually drawn unhighlighted. */
27735 clear_mouse_face (Mouse_HLInfo *hlinfo)
27737 int cleared = 0;
27739 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27741 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27742 cleared = 1;
27745 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27746 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27747 hlinfo->mouse_face_window = Qnil;
27748 hlinfo->mouse_face_overlay = Qnil;
27749 return cleared;
27752 /* Return true if the coordinates HPOS and VPOS on windows W are
27753 within the mouse face on that window. */
27754 static bool
27755 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27757 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27759 /* Quickly resolve the easy cases. */
27760 if (!(WINDOWP (hlinfo->mouse_face_window)
27761 && XWINDOW (hlinfo->mouse_face_window) == w))
27762 return false;
27763 if (vpos < hlinfo->mouse_face_beg_row
27764 || vpos > hlinfo->mouse_face_end_row)
27765 return false;
27766 if (vpos > hlinfo->mouse_face_beg_row
27767 && vpos < hlinfo->mouse_face_end_row)
27768 return true;
27770 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27772 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27774 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27775 return true;
27777 else if ((vpos == hlinfo->mouse_face_beg_row
27778 && hpos >= hlinfo->mouse_face_beg_col)
27779 || (vpos == hlinfo->mouse_face_end_row
27780 && hpos < hlinfo->mouse_face_end_col))
27781 return true;
27783 else
27785 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27787 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27788 return true;
27790 else if ((vpos == hlinfo->mouse_face_beg_row
27791 && hpos <= hlinfo->mouse_face_beg_col)
27792 || (vpos == hlinfo->mouse_face_end_row
27793 && hpos > hlinfo->mouse_face_end_col))
27794 return true;
27796 return false;
27800 /* EXPORT:
27801 True if physical cursor of window W is within mouse face. */
27803 bool
27804 cursor_in_mouse_face_p (struct window *w)
27806 int hpos = w->phys_cursor.hpos;
27807 int vpos = w->phys_cursor.vpos;
27808 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27810 /* When the window is hscrolled, cursor hpos can legitimately be out
27811 of bounds, but we draw the cursor at the corresponding window
27812 margin in that case. */
27813 if (!row->reversed_p && hpos < 0)
27814 hpos = 0;
27815 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27816 hpos = row->used[TEXT_AREA] - 1;
27818 return coords_in_mouse_face_p (w, hpos, vpos);
27823 /* Find the glyph rows START_ROW and END_ROW of window W that display
27824 characters between buffer positions START_CHARPOS and END_CHARPOS
27825 (excluding END_CHARPOS). DISP_STRING is a display string that
27826 covers these buffer positions. This is similar to
27827 row_containing_pos, but is more accurate when bidi reordering makes
27828 buffer positions change non-linearly with glyph rows. */
27829 static void
27830 rows_from_pos_range (struct window *w,
27831 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27832 Lisp_Object disp_string,
27833 struct glyph_row **start, struct glyph_row **end)
27835 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27836 int last_y = window_text_bottom_y (w);
27837 struct glyph_row *row;
27839 *start = NULL;
27840 *end = NULL;
27842 while (!first->enabled_p
27843 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27844 first++;
27846 /* Find the START row. */
27847 for (row = first;
27848 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27849 row++)
27851 /* A row can potentially be the START row if the range of the
27852 characters it displays intersects the range
27853 [START_CHARPOS..END_CHARPOS). */
27854 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27855 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27856 /* See the commentary in row_containing_pos, for the
27857 explanation of the complicated way to check whether
27858 some position is beyond the end of the characters
27859 displayed by a row. */
27860 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
27861 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
27862 && !row->ends_at_zv_p
27863 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
27864 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
27865 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
27866 && !row->ends_at_zv_p
27867 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
27869 /* Found a candidate row. Now make sure at least one of the
27870 glyphs it displays has a charpos from the range
27871 [START_CHARPOS..END_CHARPOS).
27873 This is not obvious because bidi reordering could make
27874 buffer positions of a row be 1,2,3,102,101,100, and if we
27875 want to highlight characters in [50..60), we don't want
27876 this row, even though [50..60) does intersect [1..103),
27877 the range of character positions given by the row's start
27878 and end positions. */
27879 struct glyph *g = row->glyphs[TEXT_AREA];
27880 struct glyph *e = g + row->used[TEXT_AREA];
27882 while (g < e)
27884 if (((BUFFERP (g->object) || INTEGERP (g->object))
27885 && start_charpos <= g->charpos && g->charpos < end_charpos)
27886 /* A glyph that comes from DISP_STRING is by
27887 definition to be highlighted. */
27888 || EQ (g->object, disp_string))
27889 *start = row;
27890 g++;
27892 if (*start)
27893 break;
27897 /* Find the END row. */
27898 if (!*start
27899 /* If the last row is partially visible, start looking for END
27900 from that row, instead of starting from FIRST. */
27901 && !(row->enabled_p
27902 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
27903 row = first;
27904 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
27906 struct glyph_row *next = row + 1;
27907 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
27909 if (!next->enabled_p
27910 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
27911 /* The first row >= START whose range of displayed characters
27912 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
27913 is the row END + 1. */
27914 || (start_charpos < next_start
27915 && end_charpos < next_start)
27916 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
27917 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
27918 && !next->ends_at_zv_p
27919 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
27920 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
27921 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
27922 && !next->ends_at_zv_p
27923 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
27925 *end = row;
27926 break;
27928 else
27930 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
27931 but none of the characters it displays are in the range, it is
27932 also END + 1. */
27933 struct glyph *g = next->glyphs[TEXT_AREA];
27934 struct glyph *s = g;
27935 struct glyph *e = g + next->used[TEXT_AREA];
27937 while (g < e)
27939 if (((BUFFERP (g->object) || INTEGERP (g->object))
27940 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
27941 /* If the buffer position of the first glyph in
27942 the row is equal to END_CHARPOS, it means
27943 the last character to be highlighted is the
27944 newline of ROW, and we must consider NEXT as
27945 END, not END+1. */
27946 || (((!next->reversed_p && g == s)
27947 || (next->reversed_p && g == e - 1))
27948 && (g->charpos == end_charpos
27949 /* Special case for when NEXT is an
27950 empty line at ZV. */
27951 || (g->charpos == -1
27952 && !row->ends_at_zv_p
27953 && next_start == end_charpos)))))
27954 /* A glyph that comes from DISP_STRING is by
27955 definition to be highlighted. */
27956 || EQ (g->object, disp_string))
27957 break;
27958 g++;
27960 if (g == e)
27962 *end = row;
27963 break;
27965 /* The first row that ends at ZV must be the last to be
27966 highlighted. */
27967 else if (next->ends_at_zv_p)
27969 *end = next;
27970 break;
27976 /* This function sets the mouse_face_* elements of HLINFO, assuming
27977 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27978 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27979 for the overlay or run of text properties specifying the mouse
27980 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27981 before-string and after-string that must also be highlighted.
27982 DISP_STRING, if non-nil, is a display string that may cover some
27983 or all of the highlighted text. */
27985 static void
27986 mouse_face_from_buffer_pos (Lisp_Object window,
27987 Mouse_HLInfo *hlinfo,
27988 ptrdiff_t mouse_charpos,
27989 ptrdiff_t start_charpos,
27990 ptrdiff_t end_charpos,
27991 Lisp_Object before_string,
27992 Lisp_Object after_string,
27993 Lisp_Object disp_string)
27995 struct window *w = XWINDOW (window);
27996 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27997 struct glyph_row *r1, *r2;
27998 struct glyph *glyph, *end;
27999 ptrdiff_t ignore, pos;
28000 int x;
28002 eassert (NILP (disp_string) || STRINGP (disp_string));
28003 eassert (NILP (before_string) || STRINGP (before_string));
28004 eassert (NILP (after_string) || STRINGP (after_string));
28006 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28007 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28008 if (r1 == NULL)
28009 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28010 /* If the before-string or display-string contains newlines,
28011 rows_from_pos_range skips to its last row. Move back. */
28012 if (!NILP (before_string) || !NILP (disp_string))
28014 struct glyph_row *prev;
28015 while ((prev = r1 - 1, prev >= first)
28016 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28017 && prev->used[TEXT_AREA] > 0)
28019 struct glyph *beg = prev->glyphs[TEXT_AREA];
28020 glyph = beg + prev->used[TEXT_AREA];
28021 while (--glyph >= beg && INTEGERP (glyph->object));
28022 if (glyph < beg
28023 || !(EQ (glyph->object, before_string)
28024 || EQ (glyph->object, disp_string)))
28025 break;
28026 r1 = prev;
28029 if (r2 == NULL)
28031 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28032 hlinfo->mouse_face_past_end = 1;
28034 else if (!NILP (after_string))
28036 /* If the after-string has newlines, advance to its last row. */
28037 struct glyph_row *next;
28038 struct glyph_row *last
28039 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28041 for (next = r2 + 1;
28042 next <= last
28043 && next->used[TEXT_AREA] > 0
28044 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28045 ++next)
28046 r2 = next;
28048 /* The rest of the display engine assumes that mouse_face_beg_row is
28049 either above mouse_face_end_row or identical to it. But with
28050 bidi-reordered continued lines, the row for START_CHARPOS could
28051 be below the row for END_CHARPOS. If so, swap the rows and store
28052 them in correct order. */
28053 if (r1->y > r2->y)
28055 struct glyph_row *tem = r2;
28057 r2 = r1;
28058 r1 = tem;
28061 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28062 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28064 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28065 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28066 could be anywhere in the row and in any order. The strategy
28067 below is to find the leftmost and the rightmost glyph that
28068 belongs to either of these 3 strings, or whose position is
28069 between START_CHARPOS and END_CHARPOS, and highlight all the
28070 glyphs between those two. This may cover more than just the text
28071 between START_CHARPOS and END_CHARPOS if the range of characters
28072 strides the bidi level boundary, e.g. if the beginning is in R2L
28073 text while the end is in L2R text or vice versa. */
28074 if (!r1->reversed_p)
28076 /* This row is in a left to right paragraph. Scan it left to
28077 right. */
28078 glyph = r1->glyphs[TEXT_AREA];
28079 end = glyph + r1->used[TEXT_AREA];
28080 x = r1->x;
28082 /* Skip truncation glyphs at the start of the glyph row. */
28083 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28084 for (; glyph < end
28085 && INTEGERP (glyph->object)
28086 && glyph->charpos < 0;
28087 ++glyph)
28088 x += glyph->pixel_width;
28090 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28091 or DISP_STRING, and the first glyph from buffer whose
28092 position is between START_CHARPOS and END_CHARPOS. */
28093 for (; glyph < end
28094 && !INTEGERP (glyph->object)
28095 && !EQ (glyph->object, disp_string)
28096 && !(BUFFERP (glyph->object)
28097 && (glyph->charpos >= start_charpos
28098 && glyph->charpos < end_charpos));
28099 ++glyph)
28101 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28102 are present at buffer positions between START_CHARPOS and
28103 END_CHARPOS, or if they come from an overlay. */
28104 if (EQ (glyph->object, before_string))
28106 pos = string_buffer_position (before_string,
28107 start_charpos);
28108 /* If pos == 0, it means before_string came from an
28109 overlay, not from a buffer position. */
28110 if (!pos || (pos >= start_charpos && pos < end_charpos))
28111 break;
28113 else if (EQ (glyph->object, after_string))
28115 pos = string_buffer_position (after_string, end_charpos);
28116 if (!pos || (pos >= start_charpos && pos < end_charpos))
28117 break;
28119 x += glyph->pixel_width;
28121 hlinfo->mouse_face_beg_x = x;
28122 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28124 else
28126 /* This row is in a right to left paragraph. Scan it right to
28127 left. */
28128 struct glyph *g;
28130 end = r1->glyphs[TEXT_AREA] - 1;
28131 glyph = end + r1->used[TEXT_AREA];
28133 /* Skip truncation glyphs at the start of the glyph row. */
28134 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28135 for (; glyph > end
28136 && INTEGERP (glyph->object)
28137 && glyph->charpos < 0;
28138 --glyph)
28141 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28142 or DISP_STRING, and the first glyph from buffer whose
28143 position is between START_CHARPOS and END_CHARPOS. */
28144 for (; glyph > end
28145 && !INTEGERP (glyph->object)
28146 && !EQ (glyph->object, disp_string)
28147 && !(BUFFERP (glyph->object)
28148 && (glyph->charpos >= start_charpos
28149 && glyph->charpos < end_charpos));
28150 --glyph)
28152 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28153 are present at buffer positions between START_CHARPOS and
28154 END_CHARPOS, or if they come from an overlay. */
28155 if (EQ (glyph->object, before_string))
28157 pos = string_buffer_position (before_string, start_charpos);
28158 /* If pos == 0, it means before_string came from an
28159 overlay, not from a buffer position. */
28160 if (!pos || (pos >= start_charpos && pos < end_charpos))
28161 break;
28163 else if (EQ (glyph->object, after_string))
28165 pos = string_buffer_position (after_string, end_charpos);
28166 if (!pos || (pos >= start_charpos && pos < end_charpos))
28167 break;
28171 glyph++; /* first glyph to the right of the highlighted area */
28172 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28173 x += g->pixel_width;
28174 hlinfo->mouse_face_beg_x = x;
28175 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28178 /* If the highlight ends in a different row, compute GLYPH and END
28179 for the end row. Otherwise, reuse the values computed above for
28180 the row where the highlight begins. */
28181 if (r2 != r1)
28183 if (!r2->reversed_p)
28185 glyph = r2->glyphs[TEXT_AREA];
28186 end = glyph + r2->used[TEXT_AREA];
28187 x = r2->x;
28189 else
28191 end = r2->glyphs[TEXT_AREA] - 1;
28192 glyph = end + r2->used[TEXT_AREA];
28196 if (!r2->reversed_p)
28198 /* Skip truncation and continuation glyphs near the end of the
28199 row, and also blanks and stretch glyphs inserted by
28200 extend_face_to_end_of_line. */
28201 while (end > glyph
28202 && INTEGERP ((end - 1)->object))
28203 --end;
28204 /* Scan the rest of the glyph row from the end, looking for the
28205 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28206 DISP_STRING, or whose position is between START_CHARPOS
28207 and END_CHARPOS */
28208 for (--end;
28209 end > glyph
28210 && !INTEGERP (end->object)
28211 && !EQ (end->object, disp_string)
28212 && !(BUFFERP (end->object)
28213 && (end->charpos >= start_charpos
28214 && end->charpos < end_charpos));
28215 --end)
28217 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28218 are present at buffer positions between START_CHARPOS and
28219 END_CHARPOS, or if they come from an overlay. */
28220 if (EQ (end->object, before_string))
28222 pos = string_buffer_position (before_string, start_charpos);
28223 if (!pos || (pos >= start_charpos && pos < end_charpos))
28224 break;
28226 else if (EQ (end->object, after_string))
28228 pos = string_buffer_position (after_string, end_charpos);
28229 if (!pos || (pos >= start_charpos && pos < end_charpos))
28230 break;
28233 /* Find the X coordinate of the last glyph to be highlighted. */
28234 for (; glyph <= end; ++glyph)
28235 x += glyph->pixel_width;
28237 hlinfo->mouse_face_end_x = x;
28238 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28240 else
28242 /* Skip truncation and continuation glyphs near the end of the
28243 row, and also blanks and stretch glyphs inserted by
28244 extend_face_to_end_of_line. */
28245 x = r2->x;
28246 end++;
28247 while (end < glyph
28248 && INTEGERP (end->object))
28250 x += end->pixel_width;
28251 ++end;
28253 /* Scan the rest of the glyph row from the end, looking for the
28254 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28255 DISP_STRING, or whose position is between START_CHARPOS
28256 and END_CHARPOS */
28257 for ( ;
28258 end < glyph
28259 && !INTEGERP (end->object)
28260 && !EQ (end->object, disp_string)
28261 && !(BUFFERP (end->object)
28262 && (end->charpos >= start_charpos
28263 && end->charpos < end_charpos));
28264 ++end)
28266 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28267 are present at buffer positions between START_CHARPOS and
28268 END_CHARPOS, or if they come from an overlay. */
28269 if (EQ (end->object, before_string))
28271 pos = string_buffer_position (before_string, start_charpos);
28272 if (!pos || (pos >= start_charpos && pos < end_charpos))
28273 break;
28275 else if (EQ (end->object, after_string))
28277 pos = string_buffer_position (after_string, end_charpos);
28278 if (!pos || (pos >= start_charpos && pos < end_charpos))
28279 break;
28281 x += end->pixel_width;
28283 /* If we exited the above loop because we arrived at the last
28284 glyph of the row, and its buffer position is still not in
28285 range, it means the last character in range is the preceding
28286 newline. Bump the end column and x values to get past the
28287 last glyph. */
28288 if (end == glyph
28289 && BUFFERP (end->object)
28290 && (end->charpos < start_charpos
28291 || end->charpos >= end_charpos))
28293 x += end->pixel_width;
28294 ++end;
28296 hlinfo->mouse_face_end_x = x;
28297 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28300 hlinfo->mouse_face_window = window;
28301 hlinfo->mouse_face_face_id
28302 = face_at_buffer_position (w, mouse_charpos, &ignore,
28303 mouse_charpos + 1,
28304 !hlinfo->mouse_face_hidden, -1);
28305 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28308 /* The following function is not used anymore (replaced with
28309 mouse_face_from_string_pos), but I leave it here for the time
28310 being, in case someone would. */
28312 #if 0 /* not used */
28314 /* Find the position of the glyph for position POS in OBJECT in
28315 window W's current matrix, and return in *X, *Y the pixel
28316 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28318 RIGHT_P non-zero means return the position of the right edge of the
28319 glyph, RIGHT_P zero means return the left edge position.
28321 If no glyph for POS exists in the matrix, return the position of
28322 the glyph with the next smaller position that is in the matrix, if
28323 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28324 exists in the matrix, return the position of the glyph with the
28325 next larger position in OBJECT.
28327 Value is non-zero if a glyph was found. */
28329 static int
28330 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28331 int *hpos, int *vpos, int *x, int *y, int right_p)
28333 int yb = window_text_bottom_y (w);
28334 struct glyph_row *r;
28335 struct glyph *best_glyph = NULL;
28336 struct glyph_row *best_row = NULL;
28337 int best_x = 0;
28339 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28340 r->enabled_p && r->y < yb;
28341 ++r)
28343 struct glyph *g = r->glyphs[TEXT_AREA];
28344 struct glyph *e = g + r->used[TEXT_AREA];
28345 int gx;
28347 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28348 if (EQ (g->object, object))
28350 if (g->charpos == pos)
28352 best_glyph = g;
28353 best_x = gx;
28354 best_row = r;
28355 goto found;
28357 else if (best_glyph == NULL
28358 || ((eabs (g->charpos - pos)
28359 < eabs (best_glyph->charpos - pos))
28360 && (right_p
28361 ? g->charpos < pos
28362 : g->charpos > pos)))
28364 best_glyph = g;
28365 best_x = gx;
28366 best_row = r;
28371 found:
28373 if (best_glyph)
28375 *x = best_x;
28376 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28378 if (right_p)
28380 *x += best_glyph->pixel_width;
28381 ++*hpos;
28384 *y = best_row->y;
28385 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28388 return best_glyph != NULL;
28390 #endif /* not used */
28392 /* Find the positions of the first and the last glyphs in window W's
28393 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28394 (assumed to be a string), and return in HLINFO's mouse_face_*
28395 members the pixel and column/row coordinates of those glyphs. */
28397 static void
28398 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28399 Lisp_Object object,
28400 ptrdiff_t startpos, ptrdiff_t endpos)
28402 int yb = window_text_bottom_y (w);
28403 struct glyph_row *r;
28404 struct glyph *g, *e;
28405 int gx;
28406 int found = 0;
28408 /* Find the glyph row with at least one position in the range
28409 [STARTPOS..ENDPOS), and the first glyph in that row whose
28410 position belongs to that range. */
28411 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28412 r->enabled_p && r->y < yb;
28413 ++r)
28415 if (!r->reversed_p)
28417 g = r->glyphs[TEXT_AREA];
28418 e = g + r->used[TEXT_AREA];
28419 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28420 if (EQ (g->object, object)
28421 && startpos <= g->charpos && g->charpos < endpos)
28423 hlinfo->mouse_face_beg_row
28424 = MATRIX_ROW_VPOS (r, w->current_matrix);
28425 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28426 hlinfo->mouse_face_beg_x = gx;
28427 found = 1;
28428 break;
28431 else
28433 struct glyph *g1;
28435 e = r->glyphs[TEXT_AREA];
28436 g = e + r->used[TEXT_AREA];
28437 for ( ; g > e; --g)
28438 if (EQ ((g-1)->object, object)
28439 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28441 hlinfo->mouse_face_beg_row
28442 = MATRIX_ROW_VPOS (r, w->current_matrix);
28443 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28444 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28445 gx += g1->pixel_width;
28446 hlinfo->mouse_face_beg_x = gx;
28447 found = 1;
28448 break;
28451 if (found)
28452 break;
28455 if (!found)
28456 return;
28458 /* Starting with the next row, look for the first row which does NOT
28459 include any glyphs whose positions are in the range. */
28460 for (++r; r->enabled_p && r->y < yb; ++r)
28462 g = r->glyphs[TEXT_AREA];
28463 e = g + r->used[TEXT_AREA];
28464 found = 0;
28465 for ( ; g < e; ++g)
28466 if (EQ (g->object, object)
28467 && startpos <= g->charpos && g->charpos < endpos)
28469 found = 1;
28470 break;
28472 if (!found)
28473 break;
28476 /* The highlighted region ends on the previous row. */
28477 r--;
28479 /* Set the end row. */
28480 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28482 /* Compute and set the end column and the end column's horizontal
28483 pixel coordinate. */
28484 if (!r->reversed_p)
28486 g = r->glyphs[TEXT_AREA];
28487 e = g + r->used[TEXT_AREA];
28488 for ( ; e > g; --e)
28489 if (EQ ((e-1)->object, object)
28490 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28491 break;
28492 hlinfo->mouse_face_end_col = e - g;
28494 for (gx = r->x; g < e; ++g)
28495 gx += g->pixel_width;
28496 hlinfo->mouse_face_end_x = gx;
28498 else
28500 e = r->glyphs[TEXT_AREA];
28501 g = e + r->used[TEXT_AREA];
28502 for (gx = r->x ; e < g; ++e)
28504 if (EQ (e->object, object)
28505 && startpos <= e->charpos && e->charpos < endpos)
28506 break;
28507 gx += e->pixel_width;
28509 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28510 hlinfo->mouse_face_end_x = gx;
28514 #ifdef HAVE_WINDOW_SYSTEM
28516 /* See if position X, Y is within a hot-spot of an image. */
28518 static int
28519 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28521 if (!CONSP (hot_spot))
28522 return 0;
28524 if (EQ (XCAR (hot_spot), Qrect))
28526 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28527 Lisp_Object rect = XCDR (hot_spot);
28528 Lisp_Object tem;
28529 if (!CONSP (rect))
28530 return 0;
28531 if (!CONSP (XCAR (rect)))
28532 return 0;
28533 if (!CONSP (XCDR (rect)))
28534 return 0;
28535 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28536 return 0;
28537 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28538 return 0;
28539 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28540 return 0;
28541 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28542 return 0;
28543 return 1;
28545 else if (EQ (XCAR (hot_spot), Qcircle))
28547 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28548 Lisp_Object circ = XCDR (hot_spot);
28549 Lisp_Object lr, lx0, ly0;
28550 if (CONSP (circ)
28551 && CONSP (XCAR (circ))
28552 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28553 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28554 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28556 double r = XFLOATINT (lr);
28557 double dx = XINT (lx0) - x;
28558 double dy = XINT (ly0) - y;
28559 return (dx * dx + dy * dy <= r * r);
28562 else if (EQ (XCAR (hot_spot), Qpoly))
28564 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28565 if (VECTORP (XCDR (hot_spot)))
28567 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28568 Lisp_Object *poly = v->contents;
28569 ptrdiff_t n = v->header.size;
28570 ptrdiff_t i;
28571 int inside = 0;
28572 Lisp_Object lx, ly;
28573 int x0, y0;
28575 /* Need an even number of coordinates, and at least 3 edges. */
28576 if (n < 6 || n & 1)
28577 return 0;
28579 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28580 If count is odd, we are inside polygon. Pixels on edges
28581 may or may not be included depending on actual geometry of the
28582 polygon. */
28583 if ((lx = poly[n-2], !INTEGERP (lx))
28584 || (ly = poly[n-1], !INTEGERP (lx)))
28585 return 0;
28586 x0 = XINT (lx), y0 = XINT (ly);
28587 for (i = 0; i < n; i += 2)
28589 int x1 = x0, y1 = y0;
28590 if ((lx = poly[i], !INTEGERP (lx))
28591 || (ly = poly[i+1], !INTEGERP (ly)))
28592 return 0;
28593 x0 = XINT (lx), y0 = XINT (ly);
28595 /* Does this segment cross the X line? */
28596 if (x0 >= x)
28598 if (x1 >= x)
28599 continue;
28601 else if (x1 < x)
28602 continue;
28603 if (y > y0 && y > y1)
28604 continue;
28605 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28606 inside = !inside;
28608 return inside;
28611 return 0;
28614 Lisp_Object
28615 find_hot_spot (Lisp_Object map, int x, int y)
28617 while (CONSP (map))
28619 if (CONSP (XCAR (map))
28620 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28621 return XCAR (map);
28622 map = XCDR (map);
28625 return Qnil;
28628 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28629 3, 3, 0,
28630 doc: /* Lookup in image map MAP coordinates X and Y.
28631 An image map is an alist where each element has the format (AREA ID PLIST).
28632 An AREA is specified as either a rectangle, a circle, or a polygon:
28633 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28634 pixel coordinates of the upper left and bottom right corners.
28635 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28636 and the radius of the circle; r may be a float or integer.
28637 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28638 vector describes one corner in the polygon.
28639 Returns the alist element for the first matching AREA in MAP. */)
28640 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28642 if (NILP (map))
28643 return Qnil;
28645 CHECK_NUMBER (x);
28646 CHECK_NUMBER (y);
28648 return find_hot_spot (map,
28649 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28650 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28654 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28655 static void
28656 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28658 /* Do not change cursor shape while dragging mouse. */
28659 if (!NILP (do_mouse_tracking))
28660 return;
28662 if (!NILP (pointer))
28664 if (EQ (pointer, Qarrow))
28665 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28666 else if (EQ (pointer, Qhand))
28667 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28668 else if (EQ (pointer, Qtext))
28669 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28670 else if (EQ (pointer, intern ("hdrag")))
28671 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28672 else if (EQ (pointer, intern ("nhdrag")))
28673 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28674 #ifdef HAVE_X_WINDOWS
28675 else if (EQ (pointer, intern ("vdrag")))
28676 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28677 #endif
28678 else if (EQ (pointer, intern ("hourglass")))
28679 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28680 else if (EQ (pointer, Qmodeline))
28681 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28682 else
28683 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28686 if (cursor != No_Cursor)
28687 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28690 #endif /* HAVE_WINDOW_SYSTEM */
28692 /* Take proper action when mouse has moved to the mode or header line
28693 or marginal area AREA of window W, x-position X and y-position Y.
28694 X is relative to the start of the text display area of W, so the
28695 width of bitmap areas and scroll bars must be subtracted to get a
28696 position relative to the start of the mode line. */
28698 static void
28699 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28700 enum window_part area)
28702 struct window *w = XWINDOW (window);
28703 struct frame *f = XFRAME (w->frame);
28704 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28705 #ifdef HAVE_WINDOW_SYSTEM
28706 Display_Info *dpyinfo;
28707 #endif
28708 Cursor cursor = No_Cursor;
28709 Lisp_Object pointer = Qnil;
28710 int dx, dy, width, height;
28711 ptrdiff_t charpos;
28712 Lisp_Object string, object = Qnil;
28713 Lisp_Object pos IF_LINT (= Qnil), help;
28715 Lisp_Object mouse_face;
28716 int original_x_pixel = x;
28717 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28718 struct glyph_row *row IF_LINT (= 0);
28720 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28722 int x0;
28723 struct glyph *end;
28725 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28726 returns them in row/column units! */
28727 string = mode_line_string (w, area, &x, &y, &charpos,
28728 &object, &dx, &dy, &width, &height);
28730 row = (area == ON_MODE_LINE
28731 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28732 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28734 /* Find the glyph under the mouse pointer. */
28735 if (row->mode_line_p && row->enabled_p)
28737 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28738 end = glyph + row->used[TEXT_AREA];
28740 for (x0 = original_x_pixel;
28741 glyph < end && x0 >= glyph->pixel_width;
28742 ++glyph)
28743 x0 -= glyph->pixel_width;
28745 if (glyph >= end)
28746 glyph = NULL;
28749 else
28751 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28752 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28753 returns them in row/column units! */
28754 string = marginal_area_string (w, area, &x, &y, &charpos,
28755 &object, &dx, &dy, &width, &height);
28758 help = Qnil;
28760 #ifdef HAVE_WINDOW_SYSTEM
28761 if (IMAGEP (object))
28763 Lisp_Object image_map, hotspot;
28764 if ((image_map = Fplist_get (XCDR (object), QCmap),
28765 !NILP (image_map))
28766 && (hotspot = find_hot_spot (image_map, dx, dy),
28767 CONSP (hotspot))
28768 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28770 Lisp_Object plist;
28772 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28773 If so, we could look for mouse-enter, mouse-leave
28774 properties in PLIST (and do something...). */
28775 hotspot = XCDR (hotspot);
28776 if (CONSP (hotspot)
28777 && (plist = XCAR (hotspot), CONSP (plist)))
28779 pointer = Fplist_get (plist, Qpointer);
28780 if (NILP (pointer))
28781 pointer = Qhand;
28782 help = Fplist_get (plist, Qhelp_echo);
28783 if (!NILP (help))
28785 help_echo_string = help;
28786 XSETWINDOW (help_echo_window, w);
28787 help_echo_object = w->contents;
28788 help_echo_pos = charpos;
28792 if (NILP (pointer))
28793 pointer = Fplist_get (XCDR (object), QCpointer);
28795 #endif /* HAVE_WINDOW_SYSTEM */
28797 if (STRINGP (string))
28798 pos = make_number (charpos);
28800 /* Set the help text and mouse pointer. If the mouse is on a part
28801 of the mode line without any text (e.g. past the right edge of
28802 the mode line text), use the default help text and pointer. */
28803 if (STRINGP (string) || area == ON_MODE_LINE)
28805 /* Arrange to display the help by setting the global variables
28806 help_echo_string, help_echo_object, and help_echo_pos. */
28807 if (NILP (help))
28809 if (STRINGP (string))
28810 help = Fget_text_property (pos, Qhelp_echo, string);
28812 if (!NILP (help))
28814 help_echo_string = help;
28815 XSETWINDOW (help_echo_window, w);
28816 help_echo_object = string;
28817 help_echo_pos = charpos;
28819 else if (area == ON_MODE_LINE)
28821 Lisp_Object default_help
28822 = buffer_local_value_1 (Qmode_line_default_help_echo,
28823 w->contents);
28825 if (STRINGP (default_help))
28827 help_echo_string = default_help;
28828 XSETWINDOW (help_echo_window, w);
28829 help_echo_object = Qnil;
28830 help_echo_pos = -1;
28835 #ifdef HAVE_WINDOW_SYSTEM
28836 /* Change the mouse pointer according to what is under it. */
28837 if (FRAME_WINDOW_P (f))
28839 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28840 || minibuf_level
28841 || NILP (Vresize_mini_windows));
28843 dpyinfo = FRAME_DISPLAY_INFO (f);
28844 if (STRINGP (string))
28846 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28848 if (NILP (pointer))
28849 pointer = Fget_text_property (pos, Qpointer, string);
28851 /* Change the mouse pointer according to what is under X/Y. */
28852 if (NILP (pointer)
28853 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28855 Lisp_Object map;
28856 map = Fget_text_property (pos, Qlocal_map, string);
28857 if (!KEYMAPP (map))
28858 map = Fget_text_property (pos, Qkeymap, string);
28859 if (!KEYMAPP (map) && draggable)
28860 cursor = dpyinfo->vertical_scroll_bar_cursor;
28863 else if (draggable)
28864 /* Default mode-line pointer. */
28865 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28867 #endif
28870 /* Change the mouse face according to what is under X/Y. */
28871 if (STRINGP (string))
28873 mouse_face = Fget_text_property (pos, Qmouse_face, string);
28874 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
28875 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28876 && glyph)
28878 Lisp_Object b, e;
28880 struct glyph * tmp_glyph;
28882 int gpos;
28883 int gseq_length;
28884 int total_pixel_width;
28885 ptrdiff_t begpos, endpos, ignore;
28887 int vpos, hpos;
28889 b = Fprevious_single_property_change (make_number (charpos + 1),
28890 Qmouse_face, string, Qnil);
28891 if (NILP (b))
28892 begpos = 0;
28893 else
28894 begpos = XINT (b);
28896 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
28897 if (NILP (e))
28898 endpos = SCHARS (string);
28899 else
28900 endpos = XINT (e);
28902 /* Calculate the glyph position GPOS of GLYPH in the
28903 displayed string, relative to the beginning of the
28904 highlighted part of the string.
28906 Note: GPOS is different from CHARPOS. CHARPOS is the
28907 position of GLYPH in the internal string object. A mode
28908 line string format has structures which are converted to
28909 a flattened string by the Emacs Lisp interpreter. The
28910 internal string is an element of those structures. The
28911 displayed string is the flattened string. */
28912 tmp_glyph = row_start_glyph;
28913 while (tmp_glyph < glyph
28914 && (!(EQ (tmp_glyph->object, glyph->object)
28915 && begpos <= tmp_glyph->charpos
28916 && tmp_glyph->charpos < endpos)))
28917 tmp_glyph++;
28918 gpos = glyph - tmp_glyph;
28920 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
28921 the highlighted part of the displayed string to which
28922 GLYPH belongs. Note: GSEQ_LENGTH is different from
28923 SCHARS (STRING), because the latter returns the length of
28924 the internal string. */
28925 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
28926 tmp_glyph > glyph
28927 && (!(EQ (tmp_glyph->object, glyph->object)
28928 && begpos <= tmp_glyph->charpos
28929 && tmp_glyph->charpos < endpos));
28930 tmp_glyph--)
28932 gseq_length = gpos + (tmp_glyph - glyph) + 1;
28934 /* Calculate the total pixel width of all the glyphs between
28935 the beginning of the highlighted area and GLYPH. */
28936 total_pixel_width = 0;
28937 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
28938 total_pixel_width += tmp_glyph->pixel_width;
28940 /* Pre calculation of re-rendering position. Note: X is in
28941 column units here, after the call to mode_line_string or
28942 marginal_area_string. */
28943 hpos = x - gpos;
28944 vpos = (area == ON_MODE_LINE
28945 ? (w->current_matrix)->nrows - 1
28946 : 0);
28948 /* If GLYPH's position is included in the region that is
28949 already drawn in mouse face, we have nothing to do. */
28950 if ( EQ (window, hlinfo->mouse_face_window)
28951 && (!row->reversed_p
28952 ? (hlinfo->mouse_face_beg_col <= hpos
28953 && hpos < hlinfo->mouse_face_end_col)
28954 /* In R2L rows we swap BEG and END, see below. */
28955 : (hlinfo->mouse_face_end_col <= hpos
28956 && hpos < hlinfo->mouse_face_beg_col))
28957 && hlinfo->mouse_face_beg_row == vpos )
28958 return;
28960 if (clear_mouse_face (hlinfo))
28961 cursor = No_Cursor;
28963 if (!row->reversed_p)
28965 hlinfo->mouse_face_beg_col = hpos;
28966 hlinfo->mouse_face_beg_x = original_x_pixel
28967 - (total_pixel_width + dx);
28968 hlinfo->mouse_face_end_col = hpos + gseq_length;
28969 hlinfo->mouse_face_end_x = 0;
28971 else
28973 /* In R2L rows, show_mouse_face expects BEG and END
28974 coordinates to be swapped. */
28975 hlinfo->mouse_face_end_col = hpos;
28976 hlinfo->mouse_face_end_x = original_x_pixel
28977 - (total_pixel_width + dx);
28978 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28979 hlinfo->mouse_face_beg_x = 0;
28982 hlinfo->mouse_face_beg_row = vpos;
28983 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28984 hlinfo->mouse_face_past_end = 0;
28985 hlinfo->mouse_face_window = window;
28987 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28988 charpos,
28989 0, &ignore,
28990 glyph->face_id,
28992 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28994 if (NILP (pointer))
28995 pointer = Qhand;
28997 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28998 clear_mouse_face (hlinfo);
29000 #ifdef HAVE_WINDOW_SYSTEM
29001 if (FRAME_WINDOW_P (f))
29002 define_frame_cursor1 (f, cursor, pointer);
29003 #endif
29007 /* EXPORT:
29008 Take proper action when the mouse has moved to position X, Y on
29009 frame F with regards to highlighting portions of display that have
29010 mouse-face properties. Also de-highlight portions of display where
29011 the mouse was before, set the mouse pointer shape as appropriate
29012 for the mouse coordinates, and activate help echo (tooltips).
29013 X and Y can be negative or out of range. */
29015 void
29016 note_mouse_highlight (struct frame *f, int x, int y)
29018 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29019 enum window_part part = ON_NOTHING;
29020 Lisp_Object window;
29021 struct window *w;
29022 Cursor cursor = No_Cursor;
29023 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29024 struct buffer *b;
29026 /* When a menu is active, don't highlight because this looks odd. */
29027 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29028 if (popup_activated ())
29029 return;
29030 #endif
29032 if (!f->glyphs_initialized_p
29033 || f->pointer_invisible)
29034 return;
29036 hlinfo->mouse_face_mouse_x = x;
29037 hlinfo->mouse_face_mouse_y = y;
29038 hlinfo->mouse_face_mouse_frame = f;
29040 if (hlinfo->mouse_face_defer)
29041 return;
29043 /* Which window is that in? */
29044 window = window_from_coordinates (f, x, y, &part, 1);
29046 /* If displaying active text in another window, clear that. */
29047 if (! EQ (window, hlinfo->mouse_face_window)
29048 /* Also clear if we move out of text area in same window. */
29049 || (!NILP (hlinfo->mouse_face_window)
29050 && !NILP (window)
29051 && part != ON_TEXT
29052 && part != ON_MODE_LINE
29053 && part != ON_HEADER_LINE))
29054 clear_mouse_face (hlinfo);
29056 /* Not on a window -> return. */
29057 if (!WINDOWP (window))
29058 return;
29060 /* Reset help_echo_string. It will get recomputed below. */
29061 help_echo_string = Qnil;
29063 /* Convert to window-relative pixel coordinates. */
29064 w = XWINDOW (window);
29065 frame_to_window_pixel_xy (w, &x, &y);
29067 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29068 /* Handle tool-bar window differently since it doesn't display a
29069 buffer. */
29070 if (EQ (window, f->tool_bar_window))
29072 note_tool_bar_highlight (f, x, y);
29073 return;
29075 #endif
29077 /* Mouse is on the mode, header line or margin? */
29078 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29079 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29081 note_mode_line_or_margin_highlight (window, x, y, part);
29083 #ifdef HAVE_WINDOW_SYSTEM
29084 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29086 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29087 /* Show non-text cursor (Bug#16647). */
29088 goto set_cursor;
29090 else
29091 #endif
29092 return;
29095 #ifdef HAVE_WINDOW_SYSTEM
29096 if (part == ON_VERTICAL_BORDER)
29098 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29099 help_echo_string = build_string ("drag-mouse-1: resize");
29101 else if (part == ON_RIGHT_DIVIDER)
29103 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29104 help_echo_string = build_string ("drag-mouse-1: resize");
29106 else if (part == ON_BOTTOM_DIVIDER)
29107 if (! WINDOW_BOTTOMMOST_P (w)
29108 || minibuf_level
29109 || NILP (Vresize_mini_windows))
29111 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29112 help_echo_string = build_string ("drag-mouse-1: resize");
29114 else
29115 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29116 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29117 || part == ON_SCROLL_BAR)
29118 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29119 else
29120 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29121 #endif
29123 /* Are we in a window whose display is up to date?
29124 And verify the buffer's text has not changed. */
29125 b = XBUFFER (w->contents);
29126 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29128 int hpos, vpos, dx, dy, area = LAST_AREA;
29129 ptrdiff_t pos;
29130 struct glyph *glyph;
29131 Lisp_Object object;
29132 Lisp_Object mouse_face = Qnil, position;
29133 Lisp_Object *overlay_vec = NULL;
29134 ptrdiff_t i, noverlays;
29135 struct buffer *obuf;
29136 ptrdiff_t obegv, ozv;
29137 int same_region;
29139 /* Find the glyph under X/Y. */
29140 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29142 #ifdef HAVE_WINDOW_SYSTEM
29143 /* Look for :pointer property on image. */
29144 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29146 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29147 if (img != NULL && IMAGEP (img->spec))
29149 Lisp_Object image_map, hotspot;
29150 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29151 !NILP (image_map))
29152 && (hotspot = find_hot_spot (image_map,
29153 glyph->slice.img.x + dx,
29154 glyph->slice.img.y + dy),
29155 CONSP (hotspot))
29156 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29158 Lisp_Object plist;
29160 /* Could check XCAR (hotspot) to see if we enter/leave
29161 this hot-spot.
29162 If so, we could look for mouse-enter, mouse-leave
29163 properties in PLIST (and do something...). */
29164 hotspot = XCDR (hotspot);
29165 if (CONSP (hotspot)
29166 && (plist = XCAR (hotspot), CONSP (plist)))
29168 pointer = Fplist_get (plist, Qpointer);
29169 if (NILP (pointer))
29170 pointer = Qhand;
29171 help_echo_string = Fplist_get (plist, Qhelp_echo);
29172 if (!NILP (help_echo_string))
29174 help_echo_window = window;
29175 help_echo_object = glyph->object;
29176 help_echo_pos = glyph->charpos;
29180 if (NILP (pointer))
29181 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29184 #endif /* HAVE_WINDOW_SYSTEM */
29186 /* Clear mouse face if X/Y not over text. */
29187 if (glyph == NULL
29188 || area != TEXT_AREA
29189 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29190 /* Glyph's OBJECT is an integer for glyphs inserted by the
29191 display engine for its internal purposes, like truncation
29192 and continuation glyphs and blanks beyond the end of
29193 line's text on text terminals. If we are over such a
29194 glyph, we are not over any text. */
29195 || INTEGERP (glyph->object)
29196 /* R2L rows have a stretch glyph at their front, which
29197 stands for no text, whereas L2R rows have no glyphs at
29198 all beyond the end of text. Treat such stretch glyphs
29199 like we do with NULL glyphs in L2R rows. */
29200 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29201 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29202 && glyph->type == STRETCH_GLYPH
29203 && glyph->avoid_cursor_p))
29205 if (clear_mouse_face (hlinfo))
29206 cursor = No_Cursor;
29207 #ifdef HAVE_WINDOW_SYSTEM
29208 if (FRAME_WINDOW_P (f) && NILP (pointer))
29210 if (area != TEXT_AREA)
29211 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29212 else
29213 pointer = Vvoid_text_area_pointer;
29215 #endif
29216 goto set_cursor;
29219 pos = glyph->charpos;
29220 object = glyph->object;
29221 if (!STRINGP (object) && !BUFFERP (object))
29222 goto set_cursor;
29224 /* If we get an out-of-range value, return now; avoid an error. */
29225 if (BUFFERP (object) && pos > BUF_Z (b))
29226 goto set_cursor;
29228 /* Make the window's buffer temporarily current for
29229 overlays_at and compute_char_face. */
29230 obuf = current_buffer;
29231 current_buffer = b;
29232 obegv = BEGV;
29233 ozv = ZV;
29234 BEGV = BEG;
29235 ZV = Z;
29237 /* Is this char mouse-active or does it have help-echo? */
29238 position = make_number (pos);
29240 if (BUFFERP (object))
29242 /* Put all the overlays we want in a vector in overlay_vec. */
29243 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29244 /* Sort overlays into increasing priority order. */
29245 noverlays = sort_overlays (overlay_vec, noverlays, w);
29247 else
29248 noverlays = 0;
29250 if (NILP (Vmouse_highlight))
29252 clear_mouse_face (hlinfo);
29253 goto check_help_echo;
29256 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29258 if (same_region)
29259 cursor = No_Cursor;
29261 /* Check mouse-face highlighting. */
29262 if (! same_region
29263 /* If there exists an overlay with mouse-face overlapping
29264 the one we are currently highlighting, we have to
29265 check if we enter the overlapping overlay, and then
29266 highlight only that. */
29267 || (OVERLAYP (hlinfo->mouse_face_overlay)
29268 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29270 /* Find the highest priority overlay with a mouse-face. */
29271 Lisp_Object overlay = Qnil;
29272 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29274 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29275 if (!NILP (mouse_face))
29276 overlay = overlay_vec[i];
29279 /* If we're highlighting the same overlay as before, there's
29280 no need to do that again. */
29281 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29282 goto check_help_echo;
29283 hlinfo->mouse_face_overlay = overlay;
29285 /* Clear the display of the old active region, if any. */
29286 if (clear_mouse_face (hlinfo))
29287 cursor = No_Cursor;
29289 /* If no overlay applies, get a text property. */
29290 if (NILP (overlay))
29291 mouse_face = Fget_text_property (position, Qmouse_face, object);
29293 /* Next, compute the bounds of the mouse highlighting and
29294 display it. */
29295 if (!NILP (mouse_face) && STRINGP (object))
29297 /* The mouse-highlighting comes from a display string
29298 with a mouse-face. */
29299 Lisp_Object s, e;
29300 ptrdiff_t ignore;
29302 s = Fprevious_single_property_change
29303 (make_number (pos + 1), Qmouse_face, object, Qnil);
29304 e = Fnext_single_property_change
29305 (position, Qmouse_face, object, Qnil);
29306 if (NILP (s))
29307 s = make_number (0);
29308 if (NILP (e))
29309 e = make_number (SCHARS (object));
29310 mouse_face_from_string_pos (w, hlinfo, object,
29311 XINT (s), XINT (e));
29312 hlinfo->mouse_face_past_end = 0;
29313 hlinfo->mouse_face_window = window;
29314 hlinfo->mouse_face_face_id
29315 = face_at_string_position (w, object, pos, 0, &ignore,
29316 glyph->face_id, 1);
29317 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29318 cursor = No_Cursor;
29320 else
29322 /* The mouse-highlighting, if any, comes from an overlay
29323 or text property in the buffer. */
29324 Lisp_Object buffer IF_LINT (= Qnil);
29325 Lisp_Object disp_string IF_LINT (= Qnil);
29327 if (STRINGP (object))
29329 /* If we are on a display string with no mouse-face,
29330 check if the text under it has one. */
29331 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29332 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29333 pos = string_buffer_position (object, start);
29334 if (pos > 0)
29336 mouse_face = get_char_property_and_overlay
29337 (make_number (pos), Qmouse_face, w->contents, &overlay);
29338 buffer = w->contents;
29339 disp_string = object;
29342 else
29344 buffer = object;
29345 disp_string = Qnil;
29348 if (!NILP (mouse_face))
29350 Lisp_Object before, after;
29351 Lisp_Object before_string, after_string;
29352 /* To correctly find the limits of mouse highlight
29353 in a bidi-reordered buffer, we must not use the
29354 optimization of limiting the search in
29355 previous-single-property-change and
29356 next-single-property-change, because
29357 rows_from_pos_range needs the real start and end
29358 positions to DTRT in this case. That's because
29359 the first row visible in a window does not
29360 necessarily display the character whose position
29361 is the smallest. */
29362 Lisp_Object lim1
29363 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29364 ? Fmarker_position (w->start)
29365 : Qnil;
29366 Lisp_Object lim2
29367 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29368 ? make_number (BUF_Z (XBUFFER (buffer))
29369 - w->window_end_pos)
29370 : Qnil;
29372 if (NILP (overlay))
29374 /* Handle the text property case. */
29375 before = Fprevious_single_property_change
29376 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29377 after = Fnext_single_property_change
29378 (make_number (pos), Qmouse_face, buffer, lim2);
29379 before_string = after_string = Qnil;
29381 else
29383 /* Handle the overlay case. */
29384 before = Foverlay_start (overlay);
29385 after = Foverlay_end (overlay);
29386 before_string = Foverlay_get (overlay, Qbefore_string);
29387 after_string = Foverlay_get (overlay, Qafter_string);
29389 if (!STRINGP (before_string)) before_string = Qnil;
29390 if (!STRINGP (after_string)) after_string = Qnil;
29393 mouse_face_from_buffer_pos (window, hlinfo, pos,
29394 NILP (before)
29396 : XFASTINT (before),
29397 NILP (after)
29398 ? BUF_Z (XBUFFER (buffer))
29399 : XFASTINT (after),
29400 before_string, after_string,
29401 disp_string);
29402 cursor = No_Cursor;
29407 check_help_echo:
29409 /* Look for a `help-echo' property. */
29410 if (NILP (help_echo_string)) {
29411 Lisp_Object help, overlay;
29413 /* Check overlays first. */
29414 help = overlay = Qnil;
29415 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29417 overlay = overlay_vec[i];
29418 help = Foverlay_get (overlay, Qhelp_echo);
29421 if (!NILP (help))
29423 help_echo_string = help;
29424 help_echo_window = window;
29425 help_echo_object = overlay;
29426 help_echo_pos = pos;
29428 else
29430 Lisp_Object obj = glyph->object;
29431 ptrdiff_t charpos = glyph->charpos;
29433 /* Try text properties. */
29434 if (STRINGP (obj)
29435 && charpos >= 0
29436 && charpos < SCHARS (obj))
29438 help = Fget_text_property (make_number (charpos),
29439 Qhelp_echo, obj);
29440 if (NILP (help))
29442 /* If the string itself doesn't specify a help-echo,
29443 see if the buffer text ``under'' it does. */
29444 struct glyph_row *r
29445 = MATRIX_ROW (w->current_matrix, vpos);
29446 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29447 ptrdiff_t p = string_buffer_position (obj, start);
29448 if (p > 0)
29450 help = Fget_char_property (make_number (p),
29451 Qhelp_echo, w->contents);
29452 if (!NILP (help))
29454 charpos = p;
29455 obj = w->contents;
29460 else if (BUFFERP (obj)
29461 && charpos >= BEGV
29462 && charpos < ZV)
29463 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29464 obj);
29466 if (!NILP (help))
29468 help_echo_string = help;
29469 help_echo_window = window;
29470 help_echo_object = obj;
29471 help_echo_pos = charpos;
29476 #ifdef HAVE_WINDOW_SYSTEM
29477 /* Look for a `pointer' property. */
29478 if (FRAME_WINDOW_P (f) && NILP (pointer))
29480 /* Check overlays first. */
29481 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29482 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29484 if (NILP (pointer))
29486 Lisp_Object obj = glyph->object;
29487 ptrdiff_t charpos = glyph->charpos;
29489 /* Try text properties. */
29490 if (STRINGP (obj)
29491 && charpos >= 0
29492 && charpos < SCHARS (obj))
29494 pointer = Fget_text_property (make_number (charpos),
29495 Qpointer, obj);
29496 if (NILP (pointer))
29498 /* If the string itself doesn't specify a pointer,
29499 see if the buffer text ``under'' it does. */
29500 struct glyph_row *r
29501 = MATRIX_ROW (w->current_matrix, vpos);
29502 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29503 ptrdiff_t p = string_buffer_position (obj, start);
29504 if (p > 0)
29505 pointer = Fget_char_property (make_number (p),
29506 Qpointer, w->contents);
29509 else if (BUFFERP (obj)
29510 && charpos >= BEGV
29511 && charpos < ZV)
29512 pointer = Fget_text_property (make_number (charpos),
29513 Qpointer, obj);
29516 #endif /* HAVE_WINDOW_SYSTEM */
29518 BEGV = obegv;
29519 ZV = ozv;
29520 current_buffer = obuf;
29523 set_cursor:
29525 #ifdef HAVE_WINDOW_SYSTEM
29526 if (FRAME_WINDOW_P (f))
29527 define_frame_cursor1 (f, cursor, pointer);
29528 #else
29529 /* This is here to prevent a compiler error, about "label at end of
29530 compound statement". */
29531 return;
29532 #endif
29536 /* EXPORT for RIF:
29537 Clear any mouse-face on window W. This function is part of the
29538 redisplay interface, and is called from try_window_id and similar
29539 functions to ensure the mouse-highlight is off. */
29541 void
29542 x_clear_window_mouse_face (struct window *w)
29544 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29545 Lisp_Object window;
29547 block_input ();
29548 XSETWINDOW (window, w);
29549 if (EQ (window, hlinfo->mouse_face_window))
29550 clear_mouse_face (hlinfo);
29551 unblock_input ();
29555 /* EXPORT:
29556 Just discard the mouse face information for frame F, if any.
29557 This is used when the size of F is changed. */
29559 void
29560 cancel_mouse_face (struct frame *f)
29562 Lisp_Object window;
29563 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29565 window = hlinfo->mouse_face_window;
29566 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29567 reset_mouse_highlight (hlinfo);
29572 /***********************************************************************
29573 Exposure Events
29574 ***********************************************************************/
29576 #ifdef HAVE_WINDOW_SYSTEM
29578 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29579 which intersects rectangle R. R is in window-relative coordinates. */
29581 static void
29582 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29583 enum glyph_row_area area)
29585 struct glyph *first = row->glyphs[area];
29586 struct glyph *end = row->glyphs[area] + row->used[area];
29587 struct glyph *last;
29588 int first_x, start_x, x;
29590 if (area == TEXT_AREA && row->fill_line_p)
29591 /* If row extends face to end of line write the whole line. */
29592 draw_glyphs (w, 0, row, area,
29593 0, row->used[area],
29594 DRAW_NORMAL_TEXT, 0);
29595 else
29597 /* Set START_X to the window-relative start position for drawing glyphs of
29598 AREA. The first glyph of the text area can be partially visible.
29599 The first glyphs of other areas cannot. */
29600 start_x = window_box_left_offset (w, area);
29601 x = start_x;
29602 if (area == TEXT_AREA)
29603 x += row->x;
29605 /* Find the first glyph that must be redrawn. */
29606 while (first < end
29607 && x + first->pixel_width < r->x)
29609 x += first->pixel_width;
29610 ++first;
29613 /* Find the last one. */
29614 last = first;
29615 first_x = x;
29616 while (last < end
29617 && x < r->x + r->width)
29619 x += last->pixel_width;
29620 ++last;
29623 /* Repaint. */
29624 if (last > first)
29625 draw_glyphs (w, first_x - start_x, row, area,
29626 first - row->glyphs[area], last - row->glyphs[area],
29627 DRAW_NORMAL_TEXT, 0);
29632 /* Redraw the parts of the glyph row ROW on window W intersecting
29633 rectangle R. R is in window-relative coordinates. Value is
29634 non-zero if mouse-face was overwritten. */
29636 static int
29637 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29639 eassert (row->enabled_p);
29641 if (row->mode_line_p || w->pseudo_window_p)
29642 draw_glyphs (w, 0, row, TEXT_AREA,
29643 0, row->used[TEXT_AREA],
29644 DRAW_NORMAL_TEXT, 0);
29645 else
29647 if (row->used[LEFT_MARGIN_AREA])
29648 expose_area (w, row, r, LEFT_MARGIN_AREA);
29649 if (row->used[TEXT_AREA])
29650 expose_area (w, row, r, TEXT_AREA);
29651 if (row->used[RIGHT_MARGIN_AREA])
29652 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29653 draw_row_fringe_bitmaps (w, row);
29656 return row->mouse_face_p;
29660 /* Redraw those parts of glyphs rows during expose event handling that
29661 overlap other rows. Redrawing of an exposed line writes over parts
29662 of lines overlapping that exposed line; this function fixes that.
29664 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29665 row in W's current matrix that is exposed and overlaps other rows.
29666 LAST_OVERLAPPING_ROW is the last such row. */
29668 static void
29669 expose_overlaps (struct window *w,
29670 struct glyph_row *first_overlapping_row,
29671 struct glyph_row *last_overlapping_row,
29672 XRectangle *r)
29674 struct glyph_row *row;
29676 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29677 if (row->overlapping_p)
29679 eassert (row->enabled_p && !row->mode_line_p);
29681 row->clip = r;
29682 if (row->used[LEFT_MARGIN_AREA])
29683 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29685 if (row->used[TEXT_AREA])
29686 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29688 if (row->used[RIGHT_MARGIN_AREA])
29689 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29690 row->clip = NULL;
29695 /* Return non-zero if W's cursor intersects rectangle R. */
29697 static int
29698 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29700 XRectangle cr, result;
29701 struct glyph *cursor_glyph;
29702 struct glyph_row *row;
29704 if (w->phys_cursor.vpos >= 0
29705 && w->phys_cursor.vpos < w->current_matrix->nrows
29706 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29707 row->enabled_p)
29708 && row->cursor_in_fringe_p)
29710 /* Cursor is in the fringe. */
29711 cr.x = window_box_right_offset (w,
29712 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29713 ? RIGHT_MARGIN_AREA
29714 : TEXT_AREA));
29715 cr.y = row->y;
29716 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29717 cr.height = row->height;
29718 return x_intersect_rectangles (&cr, r, &result);
29721 cursor_glyph = get_phys_cursor_glyph (w);
29722 if (cursor_glyph)
29724 /* r is relative to W's box, but w->phys_cursor.x is relative
29725 to left edge of W's TEXT area. Adjust it. */
29726 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29727 cr.y = w->phys_cursor.y;
29728 cr.width = cursor_glyph->pixel_width;
29729 cr.height = w->phys_cursor_height;
29730 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29731 I assume the effect is the same -- and this is portable. */
29732 return x_intersect_rectangles (&cr, r, &result);
29734 /* If we don't understand the format, pretend we're not in the hot-spot. */
29735 return 0;
29739 /* EXPORT:
29740 Draw a vertical window border to the right of window W if W doesn't
29741 have vertical scroll bars. */
29743 void
29744 x_draw_vertical_border (struct window *w)
29746 struct frame *f = XFRAME (WINDOW_FRAME (w));
29748 /* We could do better, if we knew what type of scroll-bar the adjacent
29749 windows (on either side) have... But we don't :-(
29750 However, I think this works ok. ++KFS 2003-04-25 */
29752 /* Redraw borders between horizontally adjacent windows. Don't
29753 do it for frames with vertical scroll bars because either the
29754 right scroll bar of a window, or the left scroll bar of its
29755 neighbor will suffice as a border. */
29756 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29757 return;
29759 /* Note: It is necessary to redraw both the left and the right
29760 borders, for when only this single window W is being
29761 redisplayed. */
29762 if (!WINDOW_RIGHTMOST_P (w)
29763 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29765 int x0, x1, y0, y1;
29767 window_box_edges (w, &x0, &y0, &x1, &y1);
29768 y1 -= 1;
29770 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29771 x1 -= 1;
29773 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29776 if (!WINDOW_LEFTMOST_P (w)
29777 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29779 int x0, x1, y0, y1;
29781 window_box_edges (w, &x0, &y0, &x1, &y1);
29782 y1 -= 1;
29784 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29785 x0 -= 1;
29787 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29792 /* Draw window dividers for window W. */
29794 void
29795 x_draw_right_divider (struct window *w)
29797 struct frame *f = WINDOW_XFRAME (w);
29799 if (w->mini || w->pseudo_window_p)
29800 return;
29801 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29803 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29804 int x1 = WINDOW_RIGHT_EDGE_X (w);
29805 int y0 = WINDOW_TOP_EDGE_Y (w);
29806 /* The bottom divider prevails. */
29807 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29809 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29813 static void
29814 x_draw_bottom_divider (struct window *w)
29816 struct frame *f = XFRAME (WINDOW_FRAME (w));
29818 if (w->mini || w->pseudo_window_p)
29819 return;
29820 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29822 int x0 = WINDOW_LEFT_EDGE_X (w);
29823 int x1 = WINDOW_RIGHT_EDGE_X (w);
29824 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29825 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29827 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29831 /* Redraw the part of window W intersection rectangle FR. Pixel
29832 coordinates in FR are frame-relative. Call this function with
29833 input blocked. Value is non-zero if the exposure overwrites
29834 mouse-face. */
29836 static int
29837 expose_window (struct window *w, XRectangle *fr)
29839 struct frame *f = XFRAME (w->frame);
29840 XRectangle wr, r;
29841 int mouse_face_overwritten_p = 0;
29843 /* If window is not yet fully initialized, do nothing. This can
29844 happen when toolkit scroll bars are used and a window is split.
29845 Reconfiguring the scroll bar will generate an expose for a newly
29846 created window. */
29847 if (w->current_matrix == NULL)
29848 return 0;
29850 /* When we're currently updating the window, display and current
29851 matrix usually don't agree. Arrange for a thorough display
29852 later. */
29853 if (w->must_be_updated_p)
29855 SET_FRAME_GARBAGED (f);
29856 return 0;
29859 /* Frame-relative pixel rectangle of W. */
29860 wr.x = WINDOW_LEFT_EDGE_X (w);
29861 wr.y = WINDOW_TOP_EDGE_Y (w);
29862 wr.width = WINDOW_PIXEL_WIDTH (w);
29863 wr.height = WINDOW_PIXEL_HEIGHT (w);
29865 if (x_intersect_rectangles (fr, &wr, &r))
29867 int yb = window_text_bottom_y (w);
29868 struct glyph_row *row;
29869 int cursor_cleared_p, phys_cursor_on_p;
29870 struct glyph_row *first_overlapping_row, *last_overlapping_row;
29872 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
29873 r.x, r.y, r.width, r.height));
29875 /* Convert to window coordinates. */
29876 r.x -= WINDOW_LEFT_EDGE_X (w);
29877 r.y -= WINDOW_TOP_EDGE_Y (w);
29879 /* Turn off the cursor. */
29880 if (!w->pseudo_window_p
29881 && phys_cursor_in_rect_p (w, &r))
29883 x_clear_cursor (w);
29884 cursor_cleared_p = 1;
29886 else
29887 cursor_cleared_p = 0;
29889 /* If the row containing the cursor extends face to end of line,
29890 then expose_area might overwrite the cursor outside the
29891 rectangle and thus notice_overwritten_cursor might clear
29892 w->phys_cursor_on_p. We remember the original value and
29893 check later if it is changed. */
29894 phys_cursor_on_p = w->phys_cursor_on_p;
29896 /* Update lines intersecting rectangle R. */
29897 first_overlapping_row = last_overlapping_row = NULL;
29898 for (row = w->current_matrix->rows;
29899 row->enabled_p;
29900 ++row)
29902 int y0 = row->y;
29903 int y1 = MATRIX_ROW_BOTTOM_Y (row);
29905 if ((y0 >= r.y && y0 < r.y + r.height)
29906 || (y1 > r.y && y1 < r.y + r.height)
29907 || (r.y >= y0 && r.y < y1)
29908 || (r.y + r.height > y0 && r.y + r.height < y1))
29910 /* A header line may be overlapping, but there is no need
29911 to fix overlapping areas for them. KFS 2005-02-12 */
29912 if (row->overlapping_p && !row->mode_line_p)
29914 if (first_overlapping_row == NULL)
29915 first_overlapping_row = row;
29916 last_overlapping_row = row;
29919 row->clip = fr;
29920 if (expose_line (w, row, &r))
29921 mouse_face_overwritten_p = 1;
29922 row->clip = NULL;
29924 else if (row->overlapping_p)
29926 /* We must redraw a row overlapping the exposed area. */
29927 if (y0 < r.y
29928 ? y0 + row->phys_height > r.y
29929 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
29931 if (first_overlapping_row == NULL)
29932 first_overlapping_row = row;
29933 last_overlapping_row = row;
29937 if (y1 >= yb)
29938 break;
29941 /* Display the mode line if there is one. */
29942 if (WINDOW_WANTS_MODELINE_P (w)
29943 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
29944 row->enabled_p)
29945 && row->y < r.y + r.height)
29947 if (expose_line (w, row, &r))
29948 mouse_face_overwritten_p = 1;
29951 if (!w->pseudo_window_p)
29953 /* Fix the display of overlapping rows. */
29954 if (first_overlapping_row)
29955 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
29956 fr);
29958 /* Draw border between windows. */
29959 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29960 x_draw_right_divider (w);
29961 else
29962 x_draw_vertical_border (w);
29964 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29965 x_draw_bottom_divider (w);
29967 /* Turn the cursor on again. */
29968 if (cursor_cleared_p
29969 || (phys_cursor_on_p && !w->phys_cursor_on_p))
29970 update_window_cursor (w, 1);
29974 return mouse_face_overwritten_p;
29979 /* Redraw (parts) of all windows in the window tree rooted at W that
29980 intersect R. R contains frame pixel coordinates. Value is
29981 non-zero if the exposure overwrites mouse-face. */
29983 static int
29984 expose_window_tree (struct window *w, XRectangle *r)
29986 struct frame *f = XFRAME (w->frame);
29987 int mouse_face_overwritten_p = 0;
29989 while (w && !FRAME_GARBAGED_P (f))
29991 if (WINDOWP (w->contents))
29992 mouse_face_overwritten_p
29993 |= expose_window_tree (XWINDOW (w->contents), r);
29994 else
29995 mouse_face_overwritten_p |= expose_window (w, r);
29997 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30000 return mouse_face_overwritten_p;
30004 /* EXPORT:
30005 Redisplay an exposed area of frame F. X and Y are the upper-left
30006 corner of the exposed rectangle. W and H are width and height of
30007 the exposed area. All are pixel values. W or H zero means redraw
30008 the entire frame. */
30010 void
30011 expose_frame (struct frame *f, int x, int y, int w, int h)
30013 XRectangle r;
30014 int mouse_face_overwritten_p = 0;
30016 TRACE ((stderr, "expose_frame "));
30018 /* No need to redraw if frame will be redrawn soon. */
30019 if (FRAME_GARBAGED_P (f))
30021 TRACE ((stderr, " garbaged\n"));
30022 return;
30025 /* If basic faces haven't been realized yet, there is no point in
30026 trying to redraw anything. This can happen when we get an expose
30027 event while Emacs is starting, e.g. by moving another window. */
30028 if (FRAME_FACE_CACHE (f) == NULL
30029 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30031 TRACE ((stderr, " no faces\n"));
30032 return;
30035 if (w == 0 || h == 0)
30037 r.x = r.y = 0;
30038 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
30039 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
30041 else
30043 r.x = x;
30044 r.y = y;
30045 r.width = w;
30046 r.height = h;
30049 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30050 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30052 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30053 if (WINDOWP (f->tool_bar_window))
30054 mouse_face_overwritten_p
30055 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30056 #endif
30058 #ifdef HAVE_X_WINDOWS
30059 #ifndef MSDOS
30060 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30061 if (WINDOWP (f->menu_bar_window))
30062 mouse_face_overwritten_p
30063 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30064 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30065 #endif
30066 #endif
30068 /* Some window managers support a focus-follows-mouse style with
30069 delayed raising of frames. Imagine a partially obscured frame,
30070 and moving the mouse into partially obscured mouse-face on that
30071 frame. The visible part of the mouse-face will be highlighted,
30072 then the WM raises the obscured frame. With at least one WM, KDE
30073 2.1, Emacs is not getting any event for the raising of the frame
30074 (even tried with SubstructureRedirectMask), only Expose events.
30075 These expose events will draw text normally, i.e. not
30076 highlighted. Which means we must redo the highlight here.
30077 Subsume it under ``we love X''. --gerd 2001-08-15 */
30078 /* Included in Windows version because Windows most likely does not
30079 do the right thing if any third party tool offers
30080 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30081 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30083 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30084 if (f == hlinfo->mouse_face_mouse_frame)
30086 int mouse_x = hlinfo->mouse_face_mouse_x;
30087 int mouse_y = hlinfo->mouse_face_mouse_y;
30088 clear_mouse_face (hlinfo);
30089 note_mouse_highlight (f, mouse_x, mouse_y);
30095 /* EXPORT:
30096 Determine the intersection of two rectangles R1 and R2. Return
30097 the intersection in *RESULT. Value is non-zero if RESULT is not
30098 empty. */
30101 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30103 XRectangle *left, *right;
30104 XRectangle *upper, *lower;
30105 int intersection_p = 0;
30107 /* Rearrange so that R1 is the left-most rectangle. */
30108 if (r1->x < r2->x)
30109 left = r1, right = r2;
30110 else
30111 left = r2, right = r1;
30113 /* X0 of the intersection is right.x0, if this is inside R1,
30114 otherwise there is no intersection. */
30115 if (right->x <= left->x + left->width)
30117 result->x = right->x;
30119 /* The right end of the intersection is the minimum of
30120 the right ends of left and right. */
30121 result->width = (min (left->x + left->width, right->x + right->width)
30122 - result->x);
30124 /* Same game for Y. */
30125 if (r1->y < r2->y)
30126 upper = r1, lower = r2;
30127 else
30128 upper = r2, lower = r1;
30130 /* The upper end of the intersection is lower.y0, if this is inside
30131 of upper. Otherwise, there is no intersection. */
30132 if (lower->y <= upper->y + upper->height)
30134 result->y = lower->y;
30136 /* The lower end of the intersection is the minimum of the lower
30137 ends of upper and lower. */
30138 result->height = (min (lower->y + lower->height,
30139 upper->y + upper->height)
30140 - result->y);
30141 intersection_p = 1;
30145 return intersection_p;
30148 #endif /* HAVE_WINDOW_SYSTEM */
30151 /***********************************************************************
30152 Initialization
30153 ***********************************************************************/
30155 void
30156 syms_of_xdisp (void)
30158 Vwith_echo_area_save_vector = Qnil;
30159 staticpro (&Vwith_echo_area_save_vector);
30161 Vmessage_stack = Qnil;
30162 staticpro (&Vmessage_stack);
30164 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30165 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30167 message_dolog_marker1 = Fmake_marker ();
30168 staticpro (&message_dolog_marker1);
30169 message_dolog_marker2 = Fmake_marker ();
30170 staticpro (&message_dolog_marker2);
30171 message_dolog_marker3 = Fmake_marker ();
30172 staticpro (&message_dolog_marker3);
30174 #ifdef GLYPH_DEBUG
30175 defsubr (&Sdump_frame_glyph_matrix);
30176 defsubr (&Sdump_glyph_matrix);
30177 defsubr (&Sdump_glyph_row);
30178 defsubr (&Sdump_tool_bar_row);
30179 defsubr (&Strace_redisplay);
30180 defsubr (&Strace_to_stderr);
30181 #endif
30182 #ifdef HAVE_WINDOW_SYSTEM
30183 defsubr (&Stool_bar_height);
30184 defsubr (&Slookup_image_map);
30185 #endif
30186 defsubr (&Sline_pixel_height);
30187 defsubr (&Sformat_mode_line);
30188 defsubr (&Sinvisible_p);
30189 defsubr (&Scurrent_bidi_paragraph_direction);
30190 defsubr (&Swindow_text_pixel_size);
30191 defsubr (&Smove_point_visually);
30193 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30194 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30195 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30196 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30197 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30198 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30199 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30200 DEFSYM (Qeval, "eval");
30201 DEFSYM (QCdata, ":data");
30202 DEFSYM (Qdisplay, "display");
30203 DEFSYM (Qspace_width, "space-width");
30204 DEFSYM (Qraise, "raise");
30205 DEFSYM (Qslice, "slice");
30206 DEFSYM (Qspace, "space");
30207 DEFSYM (Qmargin, "margin");
30208 DEFSYM (Qpointer, "pointer");
30209 DEFSYM (Qleft_margin, "left-margin");
30210 DEFSYM (Qright_margin, "right-margin");
30211 DEFSYM (Qcenter, "center");
30212 DEFSYM (Qline_height, "line-height");
30213 DEFSYM (QCalign_to, ":align-to");
30214 DEFSYM (QCrelative_width, ":relative-width");
30215 DEFSYM (QCrelative_height, ":relative-height");
30216 DEFSYM (QCeval, ":eval");
30217 DEFSYM (QCpropertize, ":propertize");
30218 DEFSYM (QCfile, ":file");
30219 DEFSYM (Qfontified, "fontified");
30220 DEFSYM (Qfontification_functions, "fontification-functions");
30221 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30222 DEFSYM (Qescape_glyph, "escape-glyph");
30223 DEFSYM (Qnobreak_space, "nobreak-space");
30224 DEFSYM (Qimage, "image");
30225 DEFSYM (Qtext, "text");
30226 DEFSYM (Qboth, "both");
30227 DEFSYM (Qboth_horiz, "both-horiz");
30228 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30229 DEFSYM (QCmap, ":map");
30230 DEFSYM (QCpointer, ":pointer");
30231 DEFSYM (Qrect, "rect");
30232 DEFSYM (Qcircle, "circle");
30233 DEFSYM (Qpoly, "poly");
30234 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30235 DEFSYM (Qgrow_only, "grow-only");
30236 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30237 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30238 DEFSYM (Qposition, "position");
30239 DEFSYM (Qbuffer_position, "buffer-position");
30240 DEFSYM (Qobject, "object");
30241 DEFSYM (Qbar, "bar");
30242 DEFSYM (Qhbar, "hbar");
30243 DEFSYM (Qbox, "box");
30244 DEFSYM (Qhollow, "hollow");
30245 DEFSYM (Qhand, "hand");
30246 DEFSYM (Qarrow, "arrow");
30247 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30249 list_of_error = list1 (list2 (intern_c_string ("error"),
30250 intern_c_string ("void-variable")));
30251 staticpro (&list_of_error);
30253 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30254 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30255 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30256 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30258 echo_buffer[0] = echo_buffer[1] = Qnil;
30259 staticpro (&echo_buffer[0]);
30260 staticpro (&echo_buffer[1]);
30262 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30263 staticpro (&echo_area_buffer[0]);
30264 staticpro (&echo_area_buffer[1]);
30266 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30267 staticpro (&Vmessages_buffer_name);
30269 mode_line_proptrans_alist = Qnil;
30270 staticpro (&mode_line_proptrans_alist);
30271 mode_line_string_list = Qnil;
30272 staticpro (&mode_line_string_list);
30273 mode_line_string_face = Qnil;
30274 staticpro (&mode_line_string_face);
30275 mode_line_string_face_prop = Qnil;
30276 staticpro (&mode_line_string_face_prop);
30277 Vmode_line_unwind_vector = Qnil;
30278 staticpro (&Vmode_line_unwind_vector);
30280 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30282 help_echo_string = Qnil;
30283 staticpro (&help_echo_string);
30284 help_echo_object = Qnil;
30285 staticpro (&help_echo_object);
30286 help_echo_window = Qnil;
30287 staticpro (&help_echo_window);
30288 previous_help_echo_string = Qnil;
30289 staticpro (&previous_help_echo_string);
30290 help_echo_pos = -1;
30292 DEFSYM (Qright_to_left, "right-to-left");
30293 DEFSYM (Qleft_to_right, "left-to-right");
30295 #ifdef HAVE_WINDOW_SYSTEM
30296 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30297 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30298 For example, if a block cursor is over a tab, it will be drawn as
30299 wide as that tab on the display. */);
30300 x_stretch_cursor_p = 0;
30301 #endif
30303 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30304 doc: /* Non-nil means highlight trailing whitespace.
30305 The face used for trailing whitespace is `trailing-whitespace'. */);
30306 Vshow_trailing_whitespace = Qnil;
30308 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30309 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30310 If the value is t, Emacs highlights non-ASCII chars which have the
30311 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30312 or `escape-glyph' face respectively.
30314 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30315 U+2011 (non-breaking hyphen) are affected.
30317 Any other non-nil value means to display these characters as a escape
30318 glyph followed by an ordinary space or hyphen.
30320 A value of nil means no special handling of these characters. */);
30321 Vnobreak_char_display = Qt;
30323 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30324 doc: /* The pointer shape to show in void text areas.
30325 A value of nil means to show the text pointer. Other options are `arrow',
30326 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
30327 Vvoid_text_area_pointer = Qarrow;
30329 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30330 doc: /* Non-nil means don't actually do any redisplay.
30331 This is used for internal purposes. */);
30332 Vinhibit_redisplay = Qnil;
30334 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30335 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30336 Vglobal_mode_string = Qnil;
30338 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30339 doc: /* Marker for where to display an arrow on top of the buffer text.
30340 This must be the beginning of a line in order to work.
30341 See also `overlay-arrow-string'. */);
30342 Voverlay_arrow_position = Qnil;
30344 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30345 doc: /* String to display as an arrow in non-window frames.
30346 See also `overlay-arrow-position'. */);
30347 Voverlay_arrow_string = build_pure_c_string ("=>");
30349 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30350 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30351 The symbols on this list are examined during redisplay to determine
30352 where to display overlay arrows. */);
30353 Voverlay_arrow_variable_list
30354 = list1 (intern_c_string ("overlay-arrow-position"));
30356 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30357 doc: /* The number of lines to try scrolling a window by when point moves out.
30358 If that fails to bring point back on frame, point is centered instead.
30359 If this is zero, point is always centered after it moves off frame.
30360 If you want scrolling to always be a line at a time, you should set
30361 `scroll-conservatively' to a large value rather than set this to 1. */);
30363 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30364 doc: /* Scroll up to this many lines, to bring point back on screen.
30365 If point moves off-screen, redisplay will scroll by up to
30366 `scroll-conservatively' lines in order to bring point just barely
30367 onto the screen again. If that cannot be done, then redisplay
30368 recenters point as usual.
30370 If the value is greater than 100, redisplay will never recenter point,
30371 but will always scroll just enough text to bring point into view, even
30372 if you move far away.
30374 A value of zero means always recenter point if it moves off screen. */);
30375 scroll_conservatively = 0;
30377 DEFVAR_INT ("scroll-margin", scroll_margin,
30378 doc: /* Number of lines of margin at the top and bottom of a window.
30379 Recenter the window whenever point gets within this many lines
30380 of the top or bottom of the window. */);
30381 scroll_margin = 0;
30383 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30384 doc: /* Pixels per inch value for non-window system displays.
30385 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30386 Vdisplay_pixels_per_inch = make_float (72.0);
30388 #ifdef GLYPH_DEBUG
30389 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30390 #endif
30392 DEFVAR_LISP ("truncate-partial-width-windows",
30393 Vtruncate_partial_width_windows,
30394 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30395 For an integer value, truncate lines in each window narrower than the
30396 full frame width, provided the window width is less than that integer;
30397 otherwise, respect the value of `truncate-lines'.
30399 For any other non-nil value, truncate lines in all windows that do
30400 not span the full frame width.
30402 A value of nil means to respect the value of `truncate-lines'.
30404 If `word-wrap' is enabled, you might want to reduce this. */);
30405 Vtruncate_partial_width_windows = make_number (50);
30407 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30408 doc: /* Maximum buffer size for which line number should be displayed.
30409 If the buffer is bigger than this, the line number does not appear
30410 in the mode line. A value of nil means no limit. */);
30411 Vline_number_display_limit = Qnil;
30413 DEFVAR_INT ("line-number-display-limit-width",
30414 line_number_display_limit_width,
30415 doc: /* Maximum line width (in characters) for line number display.
30416 If the average length of the lines near point is bigger than this, then the
30417 line number may be omitted from the mode line. */);
30418 line_number_display_limit_width = 200;
30420 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30421 doc: /* Non-nil means highlight region even in nonselected windows. */);
30422 highlight_nonselected_windows = 0;
30424 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30425 doc: /* Non-nil if more than one frame is visible on this display.
30426 Minibuffer-only frames don't count, but iconified frames do.
30427 This variable is not guaranteed to be accurate except while processing
30428 `frame-title-format' and `icon-title-format'. */);
30430 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30431 doc: /* Template for displaying the title bar of visible frames.
30432 \(Assuming the window manager supports this feature.)
30434 This variable has the same structure as `mode-line-format', except that
30435 the %c and %l constructs are ignored. It is used only on frames for
30436 which no explicit name has been set \(see `modify-frame-parameters'). */);
30438 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30439 doc: /* Template for displaying the title bar of an iconified frame.
30440 \(Assuming the window manager supports this feature.)
30441 This variable has the same structure as `mode-line-format' (which see),
30442 and is used only on frames for which no explicit name has been set
30443 \(see `modify-frame-parameters'). */);
30444 Vicon_title_format
30445 = Vframe_title_format
30446 = listn (CONSTYPE_PURE, 3,
30447 intern_c_string ("multiple-frames"),
30448 build_pure_c_string ("%b"),
30449 listn (CONSTYPE_PURE, 4,
30450 empty_unibyte_string,
30451 intern_c_string ("invocation-name"),
30452 build_pure_c_string ("@"),
30453 intern_c_string ("system-name")));
30455 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30456 doc: /* Maximum number of lines to keep in the message log buffer.
30457 If nil, disable message logging. If t, log messages but don't truncate
30458 the buffer when it becomes large. */);
30459 Vmessage_log_max = make_number (1000);
30461 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30462 doc: /* Functions called before redisplay, if window sizes have changed.
30463 The value should be a list of functions that take one argument.
30464 Just before redisplay, for each frame, if any of its windows have changed
30465 size since the last redisplay, or have been split or deleted,
30466 all the functions in the list are called, with the frame as argument. */);
30467 Vwindow_size_change_functions = Qnil;
30469 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30470 doc: /* List of functions to call before redisplaying a window with scrolling.
30471 Each function is called with two arguments, the window and its new
30472 display-start position. Note that these functions are also called by
30473 `set-window-buffer'. Also note that the value of `window-end' is not
30474 valid when these functions are called.
30476 Warning: Do not use this feature to alter the way the window
30477 is scrolled. It is not designed for that, and such use probably won't
30478 work. */);
30479 Vwindow_scroll_functions = Qnil;
30481 DEFVAR_LISP ("window-text-change-functions",
30482 Vwindow_text_change_functions,
30483 doc: /* Functions to call in redisplay when text in the window might change. */);
30484 Vwindow_text_change_functions = Qnil;
30486 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30487 doc: /* Functions called when redisplay of a window reaches the end trigger.
30488 Each function is called with two arguments, the window and the end trigger value.
30489 See `set-window-redisplay-end-trigger'. */);
30490 Vredisplay_end_trigger_functions = Qnil;
30492 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30493 doc: /* Non-nil means autoselect window with mouse pointer.
30494 If nil, do not autoselect windows.
30495 A positive number means delay autoselection by that many seconds: a
30496 window is autoselected only after the mouse has remained in that
30497 window for the duration of the delay.
30498 A negative number has a similar effect, but causes windows to be
30499 autoselected only after the mouse has stopped moving. \(Because of
30500 the way Emacs compares mouse events, you will occasionally wait twice
30501 that time before the window gets selected.\)
30502 Any other value means to autoselect window instantaneously when the
30503 mouse pointer enters it.
30505 Autoselection selects the minibuffer only if it is active, and never
30506 unselects the minibuffer if it is active.
30508 When customizing this variable make sure that the actual value of
30509 `focus-follows-mouse' matches the behavior of your window manager. */);
30510 Vmouse_autoselect_window = Qnil;
30512 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30513 doc: /* Non-nil means automatically resize tool-bars.
30514 This dynamically changes the tool-bar's height to the minimum height
30515 that is needed to make all tool-bar items visible.
30516 If value is `grow-only', the tool-bar's height is only increased
30517 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30518 Vauto_resize_tool_bars = Qt;
30520 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30521 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30522 auto_raise_tool_bar_buttons_p = 1;
30524 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30525 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30526 make_cursor_line_fully_visible_p = 1;
30528 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30529 doc: /* Border below tool-bar in pixels.
30530 If an integer, use it as the height of the border.
30531 If it is one of `internal-border-width' or `border-width', use the
30532 value of the corresponding frame parameter.
30533 Otherwise, no border is added below the tool-bar. */);
30534 Vtool_bar_border = Qinternal_border_width;
30536 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30537 doc: /* Margin around tool-bar buttons in pixels.
30538 If an integer, use that for both horizontal and vertical margins.
30539 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30540 HORZ specifying the horizontal margin, and VERT specifying the
30541 vertical margin. */);
30542 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30544 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30545 doc: /* Relief thickness of tool-bar buttons. */);
30546 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30548 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30549 doc: /* Tool bar style to use.
30550 It can be one of
30551 image - show images only
30552 text - show text only
30553 both - show both, text below image
30554 both-horiz - show text to the right of the image
30555 text-image-horiz - show text to the left of the image
30556 any other - use system default or image if no system default.
30558 This variable only affects the GTK+ toolkit version of Emacs. */);
30559 Vtool_bar_style = Qnil;
30561 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30562 doc: /* Maximum number of characters a label can have to be shown.
30563 The tool bar style must also show labels for this to have any effect, see
30564 `tool-bar-style'. */);
30565 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30567 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30568 doc: /* List of functions to call to fontify regions of text.
30569 Each function is called with one argument POS. Functions must
30570 fontify a region starting at POS in the current buffer, and give
30571 fontified regions the property `fontified'. */);
30572 Vfontification_functions = Qnil;
30573 Fmake_variable_buffer_local (Qfontification_functions);
30575 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30576 unibyte_display_via_language_environment,
30577 doc: /* Non-nil means display unibyte text according to language environment.
30578 Specifically, this means that raw bytes in the range 160-255 decimal
30579 are displayed by converting them to the equivalent multibyte characters
30580 according to the current language environment. As a result, they are
30581 displayed according to the current fontset.
30583 Note that this variable affects only how these bytes are displayed,
30584 but does not change the fact they are interpreted as raw bytes. */);
30585 unibyte_display_via_language_environment = 0;
30587 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30588 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30589 If a float, it specifies a fraction of the mini-window frame's height.
30590 If an integer, it specifies a number of lines. */);
30591 Vmax_mini_window_height = make_float (0.25);
30593 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30594 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30595 A value of nil means don't automatically resize mini-windows.
30596 A value of t means resize them to fit the text displayed in them.
30597 A value of `grow-only', the default, means let mini-windows grow only;
30598 they return to their normal size when the minibuffer is closed, or the
30599 echo area becomes empty. */);
30600 Vresize_mini_windows = Qgrow_only;
30602 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30603 doc: /* Alist specifying how to blink the cursor off.
30604 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30605 `cursor-type' frame-parameter or variable equals ON-STATE,
30606 comparing using `equal', Emacs uses OFF-STATE to specify
30607 how to blink it off. ON-STATE and OFF-STATE are values for
30608 the `cursor-type' frame parameter.
30610 If a frame's ON-STATE has no entry in this list,
30611 the frame's other specifications determine how to blink the cursor off. */);
30612 Vblink_cursor_alist = Qnil;
30614 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30615 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30616 If non-nil, windows are automatically scrolled horizontally to make
30617 point visible. */);
30618 automatic_hscrolling_p = 1;
30619 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30621 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30622 doc: /* How many columns away from the window edge point is allowed to get
30623 before automatic hscrolling will horizontally scroll the window. */);
30624 hscroll_margin = 5;
30626 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30627 doc: /* How many columns to scroll the window when point gets too close to the edge.
30628 When point is less than `hscroll-margin' columns from the window
30629 edge, automatic hscrolling will scroll the window by the amount of columns
30630 determined by this variable. If its value is a positive integer, scroll that
30631 many columns. If it's a positive floating-point number, it specifies the
30632 fraction of the window's width to scroll. If it's nil or zero, point will be
30633 centered horizontally after the scroll. Any other value, including negative
30634 numbers, are treated as if the value were zero.
30636 Automatic hscrolling always moves point outside the scroll margin, so if
30637 point was more than scroll step columns inside the margin, the window will
30638 scroll more than the value given by the scroll step.
30640 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30641 and `scroll-right' overrides this variable's effect. */);
30642 Vhscroll_step = make_number (0);
30644 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30645 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30646 Bind this around calls to `message' to let it take effect. */);
30647 message_truncate_lines = 0;
30649 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30650 doc: /* Normal hook run to update the menu bar definitions.
30651 Redisplay runs this hook before it redisplays the menu bar.
30652 This is used to update menus such as Buffers, whose contents depend on
30653 various data. */);
30654 Vmenu_bar_update_hook = Qnil;
30656 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30657 doc: /* Frame for which we are updating a menu.
30658 The enable predicate for a menu binding should check this variable. */);
30659 Vmenu_updating_frame = Qnil;
30661 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30662 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30663 inhibit_menubar_update = 0;
30665 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30666 doc: /* Prefix prepended to all continuation lines at display time.
30667 The value may be a string, an image, or a stretch-glyph; it is
30668 interpreted in the same way as the value of a `display' text property.
30670 This variable is overridden by any `wrap-prefix' text or overlay
30671 property.
30673 To add a prefix to non-continuation lines, use `line-prefix'. */);
30674 Vwrap_prefix = Qnil;
30675 DEFSYM (Qwrap_prefix, "wrap-prefix");
30676 Fmake_variable_buffer_local (Qwrap_prefix);
30678 DEFVAR_LISP ("line-prefix", Vline_prefix,
30679 doc: /* Prefix prepended to all non-continuation lines at display time.
30680 The value may be a string, an image, or a stretch-glyph; it is
30681 interpreted in the same way as the value of a `display' text property.
30683 This variable is overridden by any `line-prefix' text or overlay
30684 property.
30686 To add a prefix to continuation lines, use `wrap-prefix'. */);
30687 Vline_prefix = Qnil;
30688 DEFSYM (Qline_prefix, "line-prefix");
30689 Fmake_variable_buffer_local (Qline_prefix);
30691 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30692 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30693 inhibit_eval_during_redisplay = 0;
30695 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30696 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30697 inhibit_free_realized_faces = 0;
30699 #ifdef GLYPH_DEBUG
30700 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30701 doc: /* Inhibit try_window_id display optimization. */);
30702 inhibit_try_window_id = 0;
30704 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30705 doc: /* Inhibit try_window_reusing display optimization. */);
30706 inhibit_try_window_reusing = 0;
30708 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30709 doc: /* Inhibit try_cursor_movement display optimization. */);
30710 inhibit_try_cursor_movement = 0;
30711 #endif /* GLYPH_DEBUG */
30713 DEFVAR_INT ("overline-margin", overline_margin,
30714 doc: /* Space between overline and text, in pixels.
30715 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30716 margin to the character height. */);
30717 overline_margin = 2;
30719 DEFVAR_INT ("underline-minimum-offset",
30720 underline_minimum_offset,
30721 doc: /* Minimum distance between baseline and underline.
30722 This can improve legibility of underlined text at small font sizes,
30723 particularly when using variable `x-use-underline-position-properties'
30724 with fonts that specify an UNDERLINE_POSITION relatively close to the
30725 baseline. The default value is 1. */);
30726 underline_minimum_offset = 1;
30728 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30729 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30730 This feature only works when on a window system that can change
30731 cursor shapes. */);
30732 display_hourglass_p = 1;
30734 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30735 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30736 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30738 #ifdef HAVE_WINDOW_SYSTEM
30739 hourglass_atimer = NULL;
30740 hourglass_shown_p = 0;
30741 #endif /* HAVE_WINDOW_SYSTEM */
30743 DEFSYM (Qglyphless_char, "glyphless-char");
30744 DEFSYM (Qhex_code, "hex-code");
30745 DEFSYM (Qempty_box, "empty-box");
30746 DEFSYM (Qthin_space, "thin-space");
30747 DEFSYM (Qzero_width, "zero-width");
30749 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30750 doc: /* Function run just before redisplay.
30751 It is called with one argument, which is the set of windows that are to
30752 be redisplayed. This set can be nil (meaning, only the selected window),
30753 or t (meaning all windows). */);
30754 Vpre_redisplay_function = intern ("ignore");
30756 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30757 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30759 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30760 doc: /* Char-table defining glyphless characters.
30761 Each element, if non-nil, should be one of the following:
30762 an ASCII acronym string: display this string in a box
30763 `hex-code': display the hexadecimal code of a character in a box
30764 `empty-box': display as an empty box
30765 `thin-space': display as 1-pixel width space
30766 `zero-width': don't display
30767 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30768 display method for graphical terminals and text terminals respectively.
30769 GRAPHICAL and TEXT should each have one of the values listed above.
30771 The char-table has one extra slot to control the display of a character for
30772 which no font is found. This slot only takes effect on graphical terminals.
30773 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30774 `thin-space'. The default is `empty-box'.
30776 If a character has a non-nil entry in an active display table, the
30777 display table takes effect; in this case, Emacs does not consult
30778 `glyphless-char-display' at all. */);
30779 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30780 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30781 Qempty_box);
30783 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30784 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30785 Vdebug_on_message = Qnil;
30787 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30788 doc: /* */);
30789 Vredisplay__all_windows_cause
30790 = Fmake_vector (make_number (100), make_number (0));
30792 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30793 doc: /* */);
30794 Vredisplay__mode_lines_cause
30795 = Fmake_vector (make_number (100), make_number (0));
30799 /* Initialize this module when Emacs starts. */
30801 void
30802 init_xdisp (void)
30804 CHARPOS (this_line_start_pos) = 0;
30806 if (!noninteractive)
30808 struct window *m = XWINDOW (minibuf_window);
30809 Lisp_Object frame = m->frame;
30810 struct frame *f = XFRAME (frame);
30811 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30812 struct window *r = XWINDOW (root);
30813 int i;
30815 echo_area_window = minibuf_window;
30817 r->top_line = FRAME_TOP_MARGIN (f);
30818 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30819 r->total_cols = FRAME_COLS (f);
30820 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30821 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30822 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30824 m->top_line = FRAME_LINES (f) - 1;
30825 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30826 m->total_cols = FRAME_COLS (f);
30827 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30828 m->total_lines = 1;
30829 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30831 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30832 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30833 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30835 /* The default ellipsis glyphs `...'. */
30836 for (i = 0; i < 3; ++i)
30837 default_invis_vector[i] = make_number ('.');
30841 /* Allocate the buffer for frame titles.
30842 Also used for `format-mode-line'. */
30843 int size = 100;
30844 mode_line_noprop_buf = xmalloc (size);
30845 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30846 mode_line_noprop_ptr = mode_line_noprop_buf;
30847 mode_line_target = MODE_LINE_DISPLAY;
30850 help_echo_showing_p = 0;
30853 #ifdef HAVE_WINDOW_SYSTEM
30855 /* Platform-independent portion of hourglass implementation. */
30857 /* Cancel a currently active hourglass timer, and start a new one. */
30858 void
30859 start_hourglass (void)
30861 struct timespec delay;
30863 cancel_hourglass ();
30865 if (INTEGERP (Vhourglass_delay)
30866 && XINT (Vhourglass_delay) > 0)
30867 delay = make_timespec (min (XINT (Vhourglass_delay),
30868 TYPE_MAXIMUM (time_t)),
30870 else if (FLOATP (Vhourglass_delay)
30871 && XFLOAT_DATA (Vhourglass_delay) > 0)
30872 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
30873 else
30874 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
30876 #ifdef HAVE_NTGUI
30878 extern void w32_note_current_window (void);
30879 w32_note_current_window ();
30881 #endif /* HAVE_NTGUI */
30883 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
30884 show_hourglass, NULL);
30888 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
30889 shown. */
30890 void
30891 cancel_hourglass (void)
30893 if (hourglass_atimer)
30895 cancel_atimer (hourglass_atimer);
30896 hourglass_atimer = NULL;
30899 if (hourglass_shown_p)
30900 hide_hourglass ();
30903 #endif /* HAVE_WINDOW_SYSTEM */