Merge from emacs-24; up to 2014-07-16T17:06:12Z!rgm@gnu.org
[emacs.git] / src / xdisp.c
blobbf73de69669e8e9b3990174f58f278d52bda2872
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 static bool hourglass_shown_p;
821 /* If non-null, an asynchronous timer that, when it expires, displays
822 an hourglass cursor on all frames. */
823 static 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 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
1029 return height;
1032 /* Return the pixel width of display area AREA of window W.
1033 ANY_AREA means return the total width of W, not including
1034 fringes to the left and right of the window. */
1037 window_box_width (struct window *w, enum glyph_row_area area)
1039 int width = w->pixel_width;
1041 if (!w->pseudo_window_p)
1043 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1044 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1046 if (area == TEXT_AREA)
1047 width -= (WINDOW_MARGINS_WIDTH (w)
1048 + WINDOW_FRINGES_WIDTH (w));
1049 else if (area == LEFT_MARGIN_AREA)
1050 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1051 else if (area == RIGHT_MARGIN_AREA)
1052 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1055 /* With wide margins, fringes, etc. we might end up with a negative
1056 width, correct that here. */
1057 return max (0, width);
1061 /* Return the pixel height of the display area of window W, not
1062 including mode lines of W, if any. */
1065 window_box_height (struct window *w)
1067 struct frame *f = XFRAME (w->frame);
1068 int height = WINDOW_PIXEL_HEIGHT (w);
1070 eassert (height >= 0);
1072 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1073 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
1075 /* Note: the code below that determines the mode-line/header-line
1076 height is essentially the same as that contained in the macro
1077 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1078 the appropriate glyph row has its `mode_line_p' flag set,
1079 and if it doesn't, uses estimate_mode_line_height instead. */
1081 if (WINDOW_WANTS_MODELINE_P (w))
1083 struct glyph_row *ml_row
1084 = (w->current_matrix && w->current_matrix->rows
1085 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1086 : 0);
1087 if (ml_row && ml_row->mode_line_p)
1088 height -= ml_row->height;
1089 else
1090 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1093 if (WINDOW_WANTS_HEADER_LINE_P (w))
1095 struct glyph_row *hl_row
1096 = (w->current_matrix && w->current_matrix->rows
1097 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1098 : 0);
1099 if (hl_row && hl_row->mode_line_p)
1100 height -= hl_row->height;
1101 else
1102 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1105 /* With a very small font and a mode-line that's taller than
1106 default, we might end up with a negative height. */
1107 return max (0, height);
1110 /* Return the window-relative coordinate of the left edge of display
1111 area AREA of window W. ANY_AREA means return the left edge of the
1112 whole window, to the right of the left fringe of W. */
1115 window_box_left_offset (struct window *w, enum glyph_row_area area)
1117 int x;
1119 if (w->pseudo_window_p)
1120 return 0;
1122 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1124 if (area == TEXT_AREA)
1125 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1126 + window_box_width (w, LEFT_MARGIN_AREA));
1127 else if (area == RIGHT_MARGIN_AREA)
1128 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1129 + window_box_width (w, LEFT_MARGIN_AREA)
1130 + window_box_width (w, TEXT_AREA)
1131 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1133 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1134 else if (area == LEFT_MARGIN_AREA
1135 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1136 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1138 /* Don't return more than the window's pixel width. */
1139 return min (x, w->pixel_width);
1143 /* Return the window-relative coordinate of the right edge of display
1144 area AREA of window W. ANY_AREA means return the right edge of the
1145 whole window, to the left of the right fringe of W. */
1147 static int
1148 window_box_right_offset (struct window *w, enum glyph_row_area area)
1150 /* Don't return more than the window's pixel width. */
1151 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1152 w->pixel_width);
1155 /* Return the frame-relative coordinate of the left edge of display
1156 area AREA of window W. ANY_AREA means return the left edge of the
1157 whole window, to the right of the left fringe of W. */
1160 window_box_left (struct window *w, enum glyph_row_area area)
1162 struct frame *f = XFRAME (w->frame);
1163 int x;
1165 if (w->pseudo_window_p)
1166 return FRAME_INTERNAL_BORDER_WIDTH (f);
1168 x = (WINDOW_LEFT_EDGE_X (w)
1169 + window_box_left_offset (w, area));
1171 return x;
1175 /* Return the frame-relative coordinate of the right edge of display
1176 area AREA of window W. ANY_AREA means return the right edge of the
1177 whole window, to the left of the right fringe of W. */
1180 window_box_right (struct window *w, enum glyph_row_area area)
1182 return window_box_left (w, area) + window_box_width (w, area);
1185 /* Get the bounding box of the display area AREA of window W, without
1186 mode lines, in frame-relative coordinates. ANY_AREA means the
1187 whole window, not including the left and right fringes of
1188 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1189 coordinates of the upper-left corner of the box. Return in
1190 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1192 void
1193 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1194 int *box_y, int *box_width, int *box_height)
1196 if (box_width)
1197 *box_width = window_box_width (w, area);
1198 if (box_height)
1199 *box_height = window_box_height (w);
1200 if (box_x)
1201 *box_x = window_box_left (w, area);
1202 if (box_y)
1204 *box_y = WINDOW_TOP_EDGE_Y (w);
1205 if (WINDOW_WANTS_HEADER_LINE_P (w))
1206 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1210 #ifdef HAVE_WINDOW_SYSTEM
1212 /* Get the bounding box of the display area AREA of window W, without
1213 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1214 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1215 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1216 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1217 box. */
1219 static void
1220 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1221 int *bottom_right_x, int *bottom_right_y)
1223 window_box (w, ANY_AREA, top_left_x, top_left_y,
1224 bottom_right_x, bottom_right_y);
1225 *bottom_right_x += *top_left_x;
1226 *bottom_right_y += *top_left_y;
1229 #endif /* HAVE_WINDOW_SYSTEM */
1231 /***********************************************************************
1232 Utilities
1233 ***********************************************************************/
1235 /* Return the bottom y-position of the line the iterator IT is in.
1236 This can modify IT's settings. */
1239 line_bottom_y (struct it *it)
1241 int line_height = it->max_ascent + it->max_descent;
1242 int line_top_y = it->current_y;
1244 if (line_height == 0)
1246 if (last_height)
1247 line_height = last_height;
1248 else if (IT_CHARPOS (*it) < ZV)
1250 move_it_by_lines (it, 1);
1251 line_height = (it->max_ascent || it->max_descent
1252 ? it->max_ascent + it->max_descent
1253 : last_height);
1255 else
1257 struct glyph_row *row = it->glyph_row;
1259 /* Use the default character height. */
1260 it->glyph_row = NULL;
1261 it->what = IT_CHARACTER;
1262 it->c = ' ';
1263 it->len = 1;
1264 PRODUCE_GLYPHS (it);
1265 line_height = it->ascent + it->descent;
1266 it->glyph_row = row;
1270 return line_top_y + line_height;
1273 DEFUN ("line-pixel-height", Fline_pixel_height,
1274 Sline_pixel_height, 0, 0, 0,
1275 doc: /* Return height in pixels of text line in the selected window.
1277 Value is the height in pixels of the line at point. */)
1278 (void)
1280 struct it it;
1281 struct text_pos pt;
1282 struct window *w = XWINDOW (selected_window);
1283 struct buffer *old_buffer = NULL;
1284 Lisp_Object result;
1286 if (XBUFFER (w->contents) != current_buffer)
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->contents));
1291 SET_TEXT_POS (pt, PT, PT_BYTE);
1292 start_display (&it, w, pt);
1293 it.vpos = it.current_y = 0;
1294 last_height = 0;
1295 result = make_number (line_bottom_y (&it));
1296 if (old_buffer)
1297 set_buffer_internal_1 (old_buffer);
1299 return result;
1302 /* Return the default pixel height of text lines in window W. The
1303 value is the canonical height of the W frame's default font, plus
1304 any extra space required by the line-spacing variable or frame
1305 parameter.
1307 Implementation note: this ignores any line-spacing text properties
1308 put on the newline characters. This is because those properties
1309 only affect the _screen_ line ending in the newline (i.e., in a
1310 continued line, only the last screen line will be affected), which
1311 means only a small number of lines in a buffer can ever use this
1312 feature. Since this function is used to compute the default pixel
1313 equivalent of text lines in a window, we can safely ignore those
1314 few lines. For the same reasons, we ignore the line-height
1315 properties. */
1317 default_line_pixel_height (struct window *w)
1319 struct frame *f = WINDOW_XFRAME (w);
1320 int height = FRAME_LINE_HEIGHT (f);
1322 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1324 struct buffer *b = XBUFFER (w->contents);
1325 Lisp_Object val = BVAR (b, extra_line_spacing);
1327 if (NILP (val))
1328 val = BVAR (&buffer_defaults, extra_line_spacing);
1329 if (!NILP (val))
1331 if (RANGED_INTEGERP (0, val, INT_MAX))
1332 height += XFASTINT (val);
1333 else if (FLOATP (val))
1335 int addon = XFLOAT_DATA (val) * height + 0.5;
1337 if (addon >= 0)
1338 height += addon;
1341 else
1342 height += f->extra_line_spacing;
1345 return height;
1348 /* Subroutine of pos_visible_p below. Extracts a display string, if
1349 any, from the display spec given as its argument. */
1350 static Lisp_Object
1351 string_from_display_spec (Lisp_Object spec)
1353 if (CONSP (spec))
1355 while (CONSP (spec))
1357 if (STRINGP (XCAR (spec)))
1358 return XCAR (spec);
1359 spec = XCDR (spec);
1362 else if (VECTORP (spec))
1364 ptrdiff_t i;
1366 for (i = 0; i < ASIZE (spec); i++)
1368 if (STRINGP (AREF (spec, i)))
1369 return AREF (spec, i);
1371 return Qnil;
1374 return spec;
1378 /* Limit insanely large values of W->hscroll on frame F to the largest
1379 value that will still prevent first_visible_x and last_visible_x of
1380 'struct it' from overflowing an int. */
1381 static int
1382 window_hscroll_limited (struct window *w, struct frame *f)
1384 ptrdiff_t window_hscroll = w->hscroll;
1385 int window_text_width = window_box_width (w, TEXT_AREA);
1386 int colwidth = FRAME_COLUMN_WIDTH (f);
1388 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1389 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1391 return window_hscroll;
1394 /* Return 1 if position CHARPOS is visible in window W.
1395 CHARPOS < 0 means return info about WINDOW_END position.
1396 If visible, set *X and *Y to pixel coordinates of top left corner.
1397 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1398 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1401 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1402 int *rtop, int *rbot, int *rowh, int *vpos)
1404 struct it it;
1405 void *itdata = bidi_shelve_cache ();
1406 struct text_pos top;
1407 int visible_p = 0;
1408 struct buffer *old_buffer = NULL;
1410 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1411 return visible_p;
1413 if (XBUFFER (w->contents) != current_buffer)
1415 old_buffer = current_buffer;
1416 set_buffer_internal_1 (XBUFFER (w->contents));
1419 SET_TEXT_POS_FROM_MARKER (top, w->start);
1420 /* Scrolling a minibuffer window via scroll bar when the echo area
1421 shows long text sometimes resets the minibuffer contents behind
1422 our backs. */
1423 if (CHARPOS (top) > ZV)
1424 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1426 /* Compute exact mode line heights. */
1427 if (WINDOW_WANTS_MODELINE_P (w))
1428 w->mode_line_height
1429 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1430 BVAR (current_buffer, mode_line_format));
1432 if (WINDOW_WANTS_HEADER_LINE_P (w))
1433 w->header_line_height
1434 = display_mode_line (w, HEADER_LINE_FACE_ID,
1435 BVAR (current_buffer, header_line_format));
1437 start_display (&it, w, top);
1438 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1439 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1441 if (charpos >= 0
1442 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1443 && IT_CHARPOS (it) >= charpos)
1444 /* When scanning backwards under bidi iteration, move_it_to
1445 stops at or _before_ CHARPOS, because it stops at or to
1446 the _right_ of the character at CHARPOS. */
1447 || (it.bidi_p && it.bidi_it.scan_dir == -1
1448 && IT_CHARPOS (it) <= charpos)))
1450 /* We have reached CHARPOS, or passed it. How the call to
1451 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1452 or covered by a display property, move_it_to stops at the end
1453 of the invisible text, to the right of CHARPOS. (ii) If
1454 CHARPOS is in a display vector, move_it_to stops on its last
1455 glyph. */
1456 int top_x = it.current_x;
1457 int top_y = it.current_y;
1458 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1459 int bottom_y;
1460 struct it save_it;
1461 void *save_it_data = NULL;
1463 /* Calling line_bottom_y may change it.method, it.position, etc. */
1464 SAVE_IT (save_it, it, save_it_data);
1465 last_height = 0;
1466 bottom_y = line_bottom_y (&it);
1467 if (top_y < window_top_y)
1468 visible_p = bottom_y > window_top_y;
1469 else if (top_y < it.last_visible_y)
1470 visible_p = 1;
1471 if (bottom_y >= it.last_visible_y
1472 && it.bidi_p && it.bidi_it.scan_dir == -1
1473 && IT_CHARPOS (it) < charpos)
1475 /* When the last line of the window is scanned backwards
1476 under bidi iteration, we could be duped into thinking
1477 that we have passed CHARPOS, when in fact move_it_to
1478 simply stopped short of CHARPOS because it reached
1479 last_visible_y. To see if that's what happened, we call
1480 move_it_to again with a slightly larger vertical limit,
1481 and see if it actually moved vertically; if it did, we
1482 didn't really reach CHARPOS, which is beyond window end. */
1483 /* Why 10? because we don't know how many canonical lines
1484 will the height of the next line(s) be. So we guess. */
1485 int ten_more_lines = 10 * default_line_pixel_height (w);
1487 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1488 MOVE_TO_POS | MOVE_TO_Y);
1489 if (it.current_y > top_y)
1490 visible_p = 0;
1493 RESTORE_IT (&it, &save_it, save_it_data);
1494 if (visible_p)
1496 if (it.method == GET_FROM_DISPLAY_VECTOR)
1498 /* We stopped on the last glyph of a display vector.
1499 Try and recompute. Hack alert! */
1500 if (charpos < 2 || top.charpos >= charpos)
1501 top_x = it.glyph_row->x;
1502 else
1504 struct it it2, it2_prev;
1505 /* The idea is to get to the previous buffer
1506 position, consume the character there, and use
1507 the pixel coordinates we get after that. But if
1508 the previous buffer position is also displayed
1509 from a display vector, we need to consume all of
1510 the glyphs from that display vector. */
1511 start_display (&it2, w, top);
1512 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1513 /* If we didn't get to CHARPOS - 1, there's some
1514 replacing display property at that position, and
1515 we stopped after it. That is exactly the place
1516 whose coordinates we want. */
1517 if (IT_CHARPOS (it2) != charpos - 1)
1518 it2_prev = it2;
1519 else
1521 /* Iterate until we get out of the display
1522 vector that displays the character at
1523 CHARPOS - 1. */
1524 do {
1525 get_next_display_element (&it2);
1526 PRODUCE_GLYPHS (&it2);
1527 it2_prev = it2;
1528 set_iterator_to_next (&it2, 1);
1529 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1530 && IT_CHARPOS (it2) < charpos);
1532 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1533 || it2_prev.current_x > it2_prev.last_visible_x)
1534 top_x = it.glyph_row->x;
1535 else
1537 top_x = it2_prev.current_x;
1538 top_y = it2_prev.current_y;
1542 else if (IT_CHARPOS (it) != charpos)
1544 Lisp_Object cpos = make_number (charpos);
1545 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1546 Lisp_Object string = string_from_display_spec (spec);
1547 struct text_pos tpos;
1548 int replacing_spec_p;
1549 bool newline_in_string
1550 = (STRINGP (string)
1551 && memchr (SDATA (string), '\n', SBYTES (string)));
1553 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1554 replacing_spec_p
1555 = (!NILP (spec)
1556 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1557 charpos, FRAME_WINDOW_P (it.f)));
1558 /* The tricky code below is needed because there's a
1559 discrepancy between move_it_to and how we set cursor
1560 when PT is at the beginning of a portion of text
1561 covered by a display property or an overlay with a
1562 display property, or the display line ends in a
1563 newline from a display string. move_it_to will stop
1564 _after_ such display strings, whereas
1565 set_cursor_from_row conspires with cursor_row_p to
1566 place the cursor on the first glyph produced from the
1567 display string. */
1569 /* We have overshoot PT because it is covered by a
1570 display property that replaces the text it covers.
1571 If the string includes embedded newlines, we are also
1572 in the wrong display line. Backtrack to the correct
1573 line, where the display property begins. */
1574 if (replacing_spec_p)
1576 Lisp_Object startpos, endpos;
1577 EMACS_INT start, end;
1578 struct it it3;
1579 int it3_moved;
1581 /* Find the first and the last buffer positions
1582 covered by the display string. */
1583 endpos =
1584 Fnext_single_char_property_change (cpos, Qdisplay,
1585 Qnil, Qnil);
1586 startpos =
1587 Fprevious_single_char_property_change (endpos, Qdisplay,
1588 Qnil, Qnil);
1589 start = XFASTINT (startpos);
1590 end = XFASTINT (endpos);
1591 /* Move to the last buffer position before the
1592 display property. */
1593 start_display (&it3, w, top);
1594 if (start > CHARPOS (top))
1595 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1596 /* Move forward one more line if the position before
1597 the display string is a newline or if it is the
1598 rightmost character on a line that is
1599 continued or word-wrapped. */
1600 if (it3.method == GET_FROM_BUFFER
1601 && (it3.c == '\n'
1602 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1603 move_it_by_lines (&it3, 1);
1604 else if (move_it_in_display_line_to (&it3, -1,
1605 it3.current_x
1606 + it3.pixel_width,
1607 MOVE_TO_X)
1608 == MOVE_LINE_CONTINUED)
1610 move_it_by_lines (&it3, 1);
1611 /* When we are under word-wrap, the #$@%!
1612 move_it_by_lines moves 2 lines, so we need to
1613 fix that up. */
1614 if (it3.line_wrap == WORD_WRAP)
1615 move_it_by_lines (&it3, -1);
1618 /* Record the vertical coordinate of the display
1619 line where we wound up. */
1620 top_y = it3.current_y;
1621 if (it3.bidi_p)
1623 /* When characters are reordered for display,
1624 the character displayed to the left of the
1625 display string could be _after_ the display
1626 property in the logical order. Use the
1627 smallest vertical position of these two. */
1628 start_display (&it3, w, top);
1629 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1630 if (it3.current_y < top_y)
1631 top_y = it3.current_y;
1633 /* Move from the top of the window to the beginning
1634 of the display line where the display string
1635 begins. */
1636 start_display (&it3, w, top);
1637 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1638 /* If it3_moved stays zero after the 'while' loop
1639 below, that means we already were at a newline
1640 before the loop (e.g., the display string begins
1641 with a newline), so we don't need to (and cannot)
1642 inspect the glyphs of it3.glyph_row, because
1643 PRODUCE_GLYPHS will not produce anything for a
1644 newline, and thus it3.glyph_row stays at its
1645 stale content it got at top of the window. */
1646 it3_moved = 0;
1647 /* Finally, advance the iterator until we hit the
1648 first display element whose character position is
1649 CHARPOS, or until the first newline from the
1650 display string, which signals the end of the
1651 display line. */
1652 while (get_next_display_element (&it3))
1654 PRODUCE_GLYPHS (&it3);
1655 if (IT_CHARPOS (it3) == charpos
1656 || ITERATOR_AT_END_OF_LINE_P (&it3))
1657 break;
1658 it3_moved = 1;
1659 set_iterator_to_next (&it3, 0);
1661 top_x = it3.current_x - it3.pixel_width;
1662 /* Normally, we would exit the above loop because we
1663 found the display element whose character
1664 position is CHARPOS. For the contingency that we
1665 didn't, and stopped at the first newline from the
1666 display string, move back over the glyphs
1667 produced from the string, until we find the
1668 rightmost glyph not from the string. */
1669 if (it3_moved
1670 && newline_in_string
1671 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1673 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1674 + it3.glyph_row->used[TEXT_AREA];
1676 while (EQ ((g - 1)->object, string))
1678 --g;
1679 top_x -= g->pixel_width;
1681 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1682 + it3.glyph_row->used[TEXT_AREA]);
1687 *x = top_x;
1688 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1689 *rtop = max (0, window_top_y - top_y);
1690 *rbot = max (0, bottom_y - it.last_visible_y);
1691 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1692 - max (top_y, window_top_y)));
1693 *vpos = it.vpos;
1696 else
1698 /* Either we were asked to provide info about WINDOW_END, or
1699 CHARPOS is in the partially visible glyph row at end of
1700 window. */
1701 struct it it2;
1702 void *it2data = NULL;
1704 SAVE_IT (it2, it, it2data);
1705 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1706 move_it_by_lines (&it, 1);
1707 if (charpos < IT_CHARPOS (it)
1708 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1710 visible_p = true;
1711 RESTORE_IT (&it2, &it2, it2data);
1712 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1713 *x = it2.current_x;
1714 *y = it2.current_y + it2.max_ascent - it2.ascent;
1715 *rtop = max (0, -it2.current_y);
1716 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1717 - it.last_visible_y));
1718 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1719 it.last_visible_y)
1720 - max (it2.current_y,
1721 WINDOW_HEADER_LINE_HEIGHT (w))));
1722 *vpos = it2.vpos;
1724 else
1725 bidi_unshelve_cache (it2data, 1);
1727 bidi_unshelve_cache (itdata, 0);
1729 if (old_buffer)
1730 set_buffer_internal_1 (old_buffer);
1732 if (visible_p && w->hscroll > 0)
1733 *x -=
1734 window_hscroll_limited (w, WINDOW_XFRAME (w))
1735 * WINDOW_FRAME_COLUMN_WIDTH (w);
1737 #if 0
1738 /* Debugging code. */
1739 if (visible_p)
1740 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1741 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1742 else
1743 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1744 #endif
1746 return visible_p;
1750 /* Return the next character from STR. Return in *LEN the length of
1751 the character. This is like STRING_CHAR_AND_LENGTH but never
1752 returns an invalid character. If we find one, we return a `?', but
1753 with the length of the invalid character. */
1755 static int
1756 string_char_and_length (const unsigned char *str, int *len)
1758 int c;
1760 c = STRING_CHAR_AND_LENGTH (str, *len);
1761 if (!CHAR_VALID_P (c))
1762 /* We may not change the length here because other places in Emacs
1763 don't use this function, i.e. they silently accept invalid
1764 characters. */
1765 c = '?';
1767 return c;
1772 /* Given a position POS containing a valid character and byte position
1773 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1775 static struct text_pos
1776 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1778 eassert (STRINGP (string) && nchars >= 0);
1780 if (STRING_MULTIBYTE (string))
1782 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1783 int len;
1785 while (nchars--)
1787 string_char_and_length (p, &len);
1788 p += len;
1789 CHARPOS (pos) += 1;
1790 BYTEPOS (pos) += len;
1793 else
1794 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1796 return pos;
1800 /* Value is the text position, i.e. character and byte position,
1801 for character position CHARPOS in STRING. */
1803 static struct text_pos
1804 string_pos (ptrdiff_t charpos, Lisp_Object string)
1806 struct text_pos pos;
1807 eassert (STRINGP (string));
1808 eassert (charpos >= 0);
1809 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1810 return pos;
1814 /* Value is a text position, i.e. character and byte position, for
1815 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1816 means recognize multibyte characters. */
1818 static struct text_pos
1819 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1821 struct text_pos pos;
1823 eassert (s != NULL);
1824 eassert (charpos >= 0);
1826 if (multibyte_p)
1828 int len;
1830 SET_TEXT_POS (pos, 0, 0);
1831 while (charpos--)
1833 string_char_and_length ((const unsigned char *) s, &len);
1834 s += len;
1835 CHARPOS (pos) += 1;
1836 BYTEPOS (pos) += len;
1839 else
1840 SET_TEXT_POS (pos, charpos, charpos);
1842 return pos;
1846 /* Value is the number of characters in C string S. MULTIBYTE_P
1847 non-zero means recognize multibyte characters. */
1849 static ptrdiff_t
1850 number_of_chars (const char *s, bool multibyte_p)
1852 ptrdiff_t nchars;
1854 if (multibyte_p)
1856 ptrdiff_t rest = strlen (s);
1857 int len;
1858 const unsigned char *p = (const unsigned char *) s;
1860 for (nchars = 0; rest > 0; ++nchars)
1862 string_char_and_length (p, &len);
1863 rest -= len, p += len;
1866 else
1867 nchars = strlen (s);
1869 return nchars;
1873 /* Compute byte position NEWPOS->bytepos corresponding to
1874 NEWPOS->charpos. POS is a known position in string STRING.
1875 NEWPOS->charpos must be >= POS.charpos. */
1877 static void
1878 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1880 eassert (STRINGP (string));
1881 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1883 if (STRING_MULTIBYTE (string))
1884 *newpos = string_pos_nchars_ahead (pos, string,
1885 CHARPOS (*newpos) - CHARPOS (pos));
1886 else
1887 BYTEPOS (*newpos) = CHARPOS (*newpos);
1890 /* EXPORT:
1891 Return an estimation of the pixel height of mode or header lines on
1892 frame F. FACE_ID specifies what line's height to estimate. */
1895 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1897 #ifdef HAVE_WINDOW_SYSTEM
1898 if (FRAME_WINDOW_P (f))
1900 int height = FONT_HEIGHT (FRAME_FONT (f));
1902 /* This function is called so early when Emacs starts that the face
1903 cache and mode line face are not yet initialized. */
1904 if (FRAME_FACE_CACHE (f))
1906 struct face *face = FACE_FROM_ID (f, face_id);
1907 if (face)
1909 if (face->font)
1910 height = FONT_HEIGHT (face->font);
1911 if (face->box_line_width > 0)
1912 height += 2 * face->box_line_width;
1916 return height;
1918 #endif
1920 return 1;
1923 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1924 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1925 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1926 not force the value into range. */
1928 void
1929 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1930 int *x, int *y, NativeRectangle *bounds, int noclip)
1933 #ifdef HAVE_WINDOW_SYSTEM
1934 if (FRAME_WINDOW_P (f))
1936 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1937 even for negative values. */
1938 if (pix_x < 0)
1939 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1940 if (pix_y < 0)
1941 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1943 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1944 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1946 if (bounds)
1947 STORE_NATIVE_RECT (*bounds,
1948 FRAME_COL_TO_PIXEL_X (f, pix_x),
1949 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1950 FRAME_COLUMN_WIDTH (f) - 1,
1951 FRAME_LINE_HEIGHT (f) - 1);
1953 /* PXW: Should we clip pixels before converting to columns/lines? */
1954 if (!noclip)
1956 if (pix_x < 0)
1957 pix_x = 0;
1958 else if (pix_x > FRAME_TOTAL_COLS (f))
1959 pix_x = FRAME_TOTAL_COLS (f);
1961 if (pix_y < 0)
1962 pix_y = 0;
1963 else if (pix_y > FRAME_TOTAL_LINES (f))
1964 pix_y = FRAME_TOTAL_LINES (f);
1967 #endif
1969 *x = pix_x;
1970 *y = pix_y;
1974 /* Find the glyph under window-relative coordinates X/Y in window W.
1975 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1976 strings. Return in *HPOS and *VPOS the row and column number of
1977 the glyph found. Return in *AREA the glyph area containing X.
1978 Value is a pointer to the glyph found or null if X/Y is not on
1979 text, or we can't tell because W's current matrix is not up to
1980 date. */
1982 static struct glyph *
1983 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1984 int *dx, int *dy, int *area)
1986 struct glyph *glyph, *end;
1987 struct glyph_row *row = NULL;
1988 int x0, i;
1990 /* Find row containing Y. Give up if some row is not enabled. */
1991 for (i = 0; i < w->current_matrix->nrows; ++i)
1993 row = MATRIX_ROW (w->current_matrix, i);
1994 if (!row->enabled_p)
1995 return NULL;
1996 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1997 break;
2000 *vpos = i;
2001 *hpos = 0;
2003 /* Give up if Y is not in the window. */
2004 if (i == w->current_matrix->nrows)
2005 return NULL;
2007 /* Get the glyph area containing X. */
2008 if (w->pseudo_window_p)
2010 *area = TEXT_AREA;
2011 x0 = 0;
2013 else
2015 if (x < window_box_left_offset (w, TEXT_AREA))
2017 *area = LEFT_MARGIN_AREA;
2018 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
2020 else if (x < window_box_right_offset (w, TEXT_AREA))
2022 *area = TEXT_AREA;
2023 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
2025 else
2027 *area = RIGHT_MARGIN_AREA;
2028 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
2032 /* Find glyph containing X. */
2033 glyph = row->glyphs[*area];
2034 end = glyph + row->used[*area];
2035 x -= x0;
2036 while (glyph < end && x >= glyph->pixel_width)
2038 x -= glyph->pixel_width;
2039 ++glyph;
2042 if (glyph == end)
2043 return NULL;
2045 if (dx)
2047 *dx = x;
2048 *dy = y - (row->y + row->ascent - glyph->ascent);
2051 *hpos = glyph - row->glyphs[*area];
2052 return glyph;
2055 /* Convert frame-relative x/y to coordinates relative to window W.
2056 Takes pseudo-windows into account. */
2058 static void
2059 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2061 if (w->pseudo_window_p)
2063 /* A pseudo-window is always full-width, and starts at the
2064 left edge of the frame, plus a frame border. */
2065 struct frame *f = XFRAME (w->frame);
2066 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2067 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2069 else
2071 *x -= WINDOW_LEFT_EDGE_X (w);
2072 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2076 #ifdef HAVE_WINDOW_SYSTEM
2078 /* EXPORT:
2079 Return in RECTS[] at most N clipping rectangles for glyph string S.
2080 Return the number of stored rectangles. */
2083 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2085 XRectangle r;
2087 if (n <= 0)
2088 return 0;
2090 if (s->row->full_width_p)
2092 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2093 r.x = WINDOW_LEFT_EDGE_X (s->w);
2094 if (s->row->mode_line_p)
2095 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2096 else
2097 r.width = WINDOW_PIXEL_WIDTH (s->w);
2099 /* Unless displaying a mode or menu bar line, which are always
2100 fully visible, clip to the visible part of the row. */
2101 if (s->w->pseudo_window_p)
2102 r.height = s->row->visible_height;
2103 else
2104 r.height = s->height;
2106 else
2108 /* This is a text line that may be partially visible. */
2109 r.x = window_box_left (s->w, s->area);
2110 r.width = window_box_width (s->w, s->area);
2111 r.height = s->row->visible_height;
2114 if (s->clip_head)
2115 if (r.x < s->clip_head->x)
2117 if (r.width >= s->clip_head->x - r.x)
2118 r.width -= s->clip_head->x - r.x;
2119 else
2120 r.width = 0;
2121 r.x = s->clip_head->x;
2123 if (s->clip_tail)
2124 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2126 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2127 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2128 else
2129 r.width = 0;
2132 /* If S draws overlapping rows, it's sufficient to use the top and
2133 bottom of the window for clipping because this glyph string
2134 intentionally draws over other lines. */
2135 if (s->for_overlaps)
2137 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2138 r.height = window_text_bottom_y (s->w) - r.y;
2140 /* Alas, the above simple strategy does not work for the
2141 environments with anti-aliased text: if the same text is
2142 drawn onto the same place multiple times, it gets thicker.
2143 If the overlap we are processing is for the erased cursor, we
2144 take the intersection with the rectangle of the cursor. */
2145 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2147 XRectangle rc, r_save = r;
2149 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2150 rc.y = s->w->phys_cursor.y;
2151 rc.width = s->w->phys_cursor_width;
2152 rc.height = s->w->phys_cursor_height;
2154 x_intersect_rectangles (&r_save, &rc, &r);
2157 else
2159 /* Don't use S->y for clipping because it doesn't take partially
2160 visible lines into account. For example, it can be negative for
2161 partially visible lines at the top of a window. */
2162 if (!s->row->full_width_p
2163 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2164 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2165 else
2166 r.y = max (0, s->row->y);
2169 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2171 /* If drawing the cursor, don't let glyph draw outside its
2172 advertised boundaries. Cleartype does this under some circumstances. */
2173 if (s->hl == DRAW_CURSOR)
2175 struct glyph *glyph = s->first_glyph;
2176 int height, max_y;
2178 if (s->x > r.x)
2180 if (r.width >= s->x - r.x)
2181 r.width -= s->x - r.x;
2182 else /* R2L hscrolled row with cursor outside text area */
2183 r.width = 0;
2184 r.x = s->x;
2186 r.width = min (r.width, glyph->pixel_width);
2188 /* If r.y is below window bottom, ensure that we still see a cursor. */
2189 height = min (glyph->ascent + glyph->descent,
2190 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2191 max_y = window_text_bottom_y (s->w) - height;
2192 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2193 if (s->ybase - glyph->ascent > max_y)
2195 r.y = max_y;
2196 r.height = height;
2198 else
2200 /* Don't draw cursor glyph taller than our actual glyph. */
2201 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2202 if (height < r.height)
2204 max_y = r.y + r.height;
2205 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2206 r.height = min (max_y - r.y, height);
2211 if (s->row->clip)
2213 XRectangle r_save = r;
2215 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2216 r.width = 0;
2219 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2220 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2222 #ifdef CONVERT_FROM_XRECT
2223 CONVERT_FROM_XRECT (r, *rects);
2224 #else
2225 *rects = r;
2226 #endif
2227 return 1;
2229 else
2231 /* If we are processing overlapping and allowed to return
2232 multiple clipping rectangles, we exclude the row of the glyph
2233 string from the clipping rectangle. This is to avoid drawing
2234 the same text on the environment with anti-aliasing. */
2235 #ifdef CONVERT_FROM_XRECT
2236 XRectangle rs[2];
2237 #else
2238 XRectangle *rs = rects;
2239 #endif
2240 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2242 if (s->for_overlaps & OVERLAPS_PRED)
2244 rs[i] = r;
2245 if (r.y + r.height > row_y)
2247 if (r.y < row_y)
2248 rs[i].height = row_y - r.y;
2249 else
2250 rs[i].height = 0;
2252 i++;
2254 if (s->for_overlaps & OVERLAPS_SUCC)
2256 rs[i] = r;
2257 if (r.y < row_y + s->row->visible_height)
2259 if (r.y + r.height > row_y + s->row->visible_height)
2261 rs[i].y = row_y + s->row->visible_height;
2262 rs[i].height = r.y + r.height - rs[i].y;
2264 else
2265 rs[i].height = 0;
2267 i++;
2270 n = i;
2271 #ifdef CONVERT_FROM_XRECT
2272 for (i = 0; i < n; i++)
2273 CONVERT_FROM_XRECT (rs[i], rects[i]);
2274 #endif
2275 return n;
2279 /* EXPORT:
2280 Return in *NR the clipping rectangle for glyph string S. */
2282 void
2283 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2285 get_glyph_string_clip_rects (s, nr, 1);
2289 /* EXPORT:
2290 Return the position and height of the phys cursor in window W.
2291 Set w->phys_cursor_width to width of phys cursor.
2294 void
2295 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2296 struct glyph *glyph, int *xp, int *yp, int *heightp)
2298 struct frame *f = XFRAME (WINDOW_FRAME (w));
2299 int x, y, wd, h, h0, y0;
2301 /* Compute the width of the rectangle to draw. If on a stretch
2302 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2303 rectangle as wide as the glyph, but use a canonical character
2304 width instead. */
2305 wd = glyph->pixel_width - 1;
2306 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2307 wd++; /* Why? */
2308 #endif
2310 x = w->phys_cursor.x;
2311 if (x < 0)
2313 wd += x;
2314 x = 0;
2317 if (glyph->type == STRETCH_GLYPH
2318 && !x_stretch_cursor_p)
2319 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2320 w->phys_cursor_width = wd;
2322 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2324 /* If y is below window bottom, ensure that we still see a cursor. */
2325 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2327 h = max (h0, glyph->ascent + glyph->descent);
2328 h0 = min (h0, glyph->ascent + glyph->descent);
2330 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2331 if (y < y0)
2333 h = max (h - (y0 - y) + 1, h0);
2334 y = y0 - 1;
2336 else
2338 y0 = window_text_bottom_y (w) - h0;
2339 if (y > y0)
2341 h += y - y0;
2342 y = y0;
2346 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2347 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2348 *heightp = h;
2352 * Remember which glyph the mouse is over.
2355 void
2356 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2358 Lisp_Object window;
2359 struct window *w;
2360 struct glyph_row *r, *gr, *end_row;
2361 enum window_part part;
2362 enum glyph_row_area area;
2363 int x, y, width, height;
2365 /* Try to determine frame pixel position and size of the glyph under
2366 frame pixel coordinates X/Y on frame F. */
2368 if (window_resize_pixelwise)
2370 width = height = 1;
2371 goto virtual_glyph;
2373 else if (!f->glyphs_initialized_p
2374 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2375 NILP (window)))
2377 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2378 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2379 goto virtual_glyph;
2382 w = XWINDOW (window);
2383 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2384 height = WINDOW_FRAME_LINE_HEIGHT (w);
2386 x = window_relative_x_coord (w, part, gx);
2387 y = gy - WINDOW_TOP_EDGE_Y (w);
2389 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2390 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2392 if (w->pseudo_window_p)
2394 area = TEXT_AREA;
2395 part = ON_MODE_LINE; /* Don't adjust margin. */
2396 goto text_glyph;
2399 switch (part)
2401 case ON_LEFT_MARGIN:
2402 area = LEFT_MARGIN_AREA;
2403 goto text_glyph;
2405 case ON_RIGHT_MARGIN:
2406 area = RIGHT_MARGIN_AREA;
2407 goto text_glyph;
2409 case ON_HEADER_LINE:
2410 case ON_MODE_LINE:
2411 gr = (part == ON_HEADER_LINE
2412 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2413 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2414 gy = gr->y;
2415 area = TEXT_AREA;
2416 goto text_glyph_row_found;
2418 case ON_TEXT:
2419 area = TEXT_AREA;
2421 text_glyph:
2422 gr = 0; gy = 0;
2423 for (; r <= end_row && r->enabled_p; ++r)
2424 if (r->y + r->height > y)
2426 gr = r; gy = r->y;
2427 break;
2430 text_glyph_row_found:
2431 if (gr && gy <= y)
2433 struct glyph *g = gr->glyphs[area];
2434 struct glyph *end = g + gr->used[area];
2436 height = gr->height;
2437 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2438 if (gx + g->pixel_width > x)
2439 break;
2441 if (g < end)
2443 if (g->type == IMAGE_GLYPH)
2445 /* Don't remember when mouse is over image, as
2446 image may have hot-spots. */
2447 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2448 return;
2450 width = g->pixel_width;
2452 else
2454 /* Use nominal char spacing at end of line. */
2455 x -= gx;
2456 gx += (x / width) * width;
2459 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2461 gx += window_box_left_offset (w, area);
2462 /* Don't expand over the modeline to make sure the vertical
2463 drag cursor is shown early enough. */
2464 height = min (height,
2465 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2468 else
2470 /* Use nominal line height at end of window. */
2471 gx = (x / width) * width;
2472 y -= gy;
2473 gy += (y / height) * height;
2474 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2475 /* See comment above. */
2476 height = min (height,
2477 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2479 break;
2481 case ON_LEFT_FRINGE:
2482 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2483 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2484 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2485 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2486 goto row_glyph;
2488 case ON_RIGHT_FRINGE:
2489 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2490 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2491 : window_box_right_offset (w, TEXT_AREA));
2492 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2493 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2494 && !WINDOW_RIGHTMOST_P (w))
2495 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2496 /* Make sure the vertical border can get her own glyph to the
2497 right of the one we build here. */
2498 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2499 else
2500 width = WINDOW_PIXEL_WIDTH (w) - gx;
2501 else
2502 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2504 goto row_glyph;
2506 case ON_VERTICAL_BORDER:
2507 gx = WINDOW_PIXEL_WIDTH (w) - width;
2508 goto row_glyph;
2510 case ON_VERTICAL_SCROLL_BAR:
2511 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2513 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2514 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2515 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2516 : 0)));
2517 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2519 row_glyph:
2520 gr = 0, gy = 0;
2521 for (; r <= end_row && r->enabled_p; ++r)
2522 if (r->y + r->height > y)
2524 gr = r; gy = r->y;
2525 break;
2528 if (gr && gy <= y)
2529 height = gr->height;
2530 else
2532 /* Use nominal line height at end of window. */
2533 y -= gy;
2534 gy += (y / height) * height;
2536 break;
2538 case ON_RIGHT_DIVIDER:
2539 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2540 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2541 gy = 0;
2542 /* The bottom divider prevails. */
2543 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2544 goto add_edge;
2546 case ON_BOTTOM_DIVIDER:
2547 gx = 0;
2548 width = WINDOW_PIXEL_WIDTH (w);
2549 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2550 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2551 goto add_edge;
2553 default:
2555 virtual_glyph:
2556 /* If there is no glyph under the mouse, then we divide the screen
2557 into a grid of the smallest glyph in the frame, and use that
2558 as our "glyph". */
2560 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2561 round down even for negative values. */
2562 if (gx < 0)
2563 gx -= width - 1;
2564 if (gy < 0)
2565 gy -= height - 1;
2567 gx = (gx / width) * width;
2568 gy = (gy / height) * height;
2570 goto store_rect;
2573 add_edge:
2574 gx += WINDOW_LEFT_EDGE_X (w);
2575 gy += WINDOW_TOP_EDGE_Y (w);
2577 store_rect:
2578 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2580 /* Visible feedback for debugging. */
2581 #if 0
2582 #if HAVE_X_WINDOWS
2583 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2584 f->output_data.x->normal_gc,
2585 gx, gy, width, height);
2586 #endif
2587 #endif
2591 #endif /* HAVE_WINDOW_SYSTEM */
2593 static void
2594 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2596 eassert (w);
2597 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2598 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2599 w->window_end_vpos
2600 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2603 /***********************************************************************
2604 Lisp form evaluation
2605 ***********************************************************************/
2607 /* Error handler for safe_eval and safe_call. */
2609 static Lisp_Object
2610 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2612 add_to_log ("Error during redisplay: %S signaled %S",
2613 Flist (nargs, args), arg);
2614 return Qnil;
2617 /* Call function FUNC with the rest of NARGS - 1 arguments
2618 following. Return the result, or nil if something went
2619 wrong. Prevent redisplay during the evaluation. */
2621 static Lisp_Object
2622 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2624 Lisp_Object val;
2626 if (inhibit_eval_during_redisplay)
2627 val = Qnil;
2628 else
2630 ptrdiff_t i;
2631 ptrdiff_t count = SPECPDL_INDEX ();
2632 Lisp_Object *args;
2633 USE_SAFE_ALLOCA;
2634 SAFE_ALLOCA_LISP (args, nargs);
2636 args[0] = func;
2637 for (i = 1; i < nargs; i++)
2638 args[i] = va_arg (ap, Lisp_Object);
2640 specbind (Qinhibit_redisplay, Qt);
2641 if (inhibit_quit)
2642 specbind (Qinhibit_quit, Qt);
2643 /* Use Qt to ensure debugger does not run,
2644 so there is no possibility of wanting to redisplay. */
2645 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2646 safe_eval_handler);
2647 SAFE_FREE ();
2648 val = unbind_to (count, val);
2651 return val;
2654 Lisp_Object
2655 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2657 Lisp_Object retval;
2658 va_list ap;
2660 va_start (ap, func);
2661 retval = safe__call (false, nargs, func, ap);
2662 va_end (ap);
2663 return retval;
2666 /* Call function FN with one argument ARG.
2667 Return the result, or nil if something went wrong. */
2669 Lisp_Object
2670 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2672 return safe_call (2, fn, arg);
2675 static Lisp_Object
2676 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2678 Lisp_Object retval;
2679 va_list ap;
2681 va_start (ap, fn);
2682 retval = safe__call (inhibit_quit, 2, fn, ap);
2683 va_end (ap);
2684 return retval;
2687 static Lisp_Object Qeval;
2689 Lisp_Object
2690 safe_eval (Lisp_Object sexpr)
2692 return safe__call1 (false, Qeval, sexpr);
2695 static Lisp_Object
2696 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2698 return safe__call1 (inhibit_quit, Qeval, sexpr);
2701 /* Call function FN with two arguments ARG1 and ARG2.
2702 Return the result, or nil if something went wrong. */
2704 Lisp_Object
2705 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2707 return safe_call (3, fn, arg1, arg2);
2712 /***********************************************************************
2713 Debugging
2714 ***********************************************************************/
2716 #if 0
2718 /* Define CHECK_IT to perform sanity checks on iterators.
2719 This is for debugging. It is too slow to do unconditionally. */
2721 static void
2722 check_it (struct it *it)
2724 if (it->method == GET_FROM_STRING)
2726 eassert (STRINGP (it->string));
2727 eassert (IT_STRING_CHARPOS (*it) >= 0);
2729 else
2731 eassert (IT_STRING_CHARPOS (*it) < 0);
2732 if (it->method == GET_FROM_BUFFER)
2734 /* Check that character and byte positions agree. */
2735 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2739 if (it->dpvec)
2740 eassert (it->current.dpvec_index >= 0);
2741 else
2742 eassert (it->current.dpvec_index < 0);
2745 #define CHECK_IT(IT) check_it ((IT))
2747 #else /* not 0 */
2749 #define CHECK_IT(IT) (void) 0
2751 #endif /* not 0 */
2754 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2756 /* Check that the window end of window W is what we expect it
2757 to be---the last row in the current matrix displaying text. */
2759 static void
2760 check_window_end (struct window *w)
2762 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2764 struct glyph_row *row;
2765 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2766 !row->enabled_p
2767 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2768 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2772 #define CHECK_WINDOW_END(W) check_window_end ((W))
2774 #else
2776 #define CHECK_WINDOW_END(W) (void) 0
2778 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2780 /***********************************************************************
2781 Iterator initialization
2782 ***********************************************************************/
2784 /* Initialize IT for displaying current_buffer in window W, starting
2785 at character position CHARPOS. CHARPOS < 0 means that no buffer
2786 position is specified which is useful when the iterator is assigned
2787 a position later. BYTEPOS is the byte position corresponding to
2788 CHARPOS.
2790 If ROW is not null, calls to produce_glyphs with IT as parameter
2791 will produce glyphs in that row.
2793 BASE_FACE_ID is the id of a base face to use. It must be one of
2794 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2795 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2796 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2798 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2799 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2800 will be initialized to use the corresponding mode line glyph row of
2801 the desired matrix of W. */
2803 void
2804 init_iterator (struct it *it, struct window *w,
2805 ptrdiff_t charpos, ptrdiff_t bytepos,
2806 struct glyph_row *row, enum face_id base_face_id)
2808 enum face_id remapped_base_face_id = base_face_id;
2810 /* Some precondition checks. */
2811 eassert (w != NULL && it != NULL);
2812 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2813 && charpos <= ZV));
2815 /* If face attributes have been changed since the last redisplay,
2816 free realized faces now because they depend on face definitions
2817 that might have changed. Don't free faces while there might be
2818 desired matrices pending which reference these faces. */
2819 if (face_change_count && !inhibit_free_realized_faces)
2821 face_change_count = 0;
2822 free_all_realized_faces (Qnil);
2825 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2826 if (! NILP (Vface_remapping_alist))
2827 remapped_base_face_id
2828 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2830 /* Use one of the mode line rows of W's desired matrix if
2831 appropriate. */
2832 if (row == NULL)
2834 if (base_face_id == MODE_LINE_FACE_ID
2835 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2836 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2837 else if (base_face_id == HEADER_LINE_FACE_ID)
2838 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2841 /* Clear IT. */
2842 memset (it, 0, sizeof *it);
2843 it->current.overlay_string_index = -1;
2844 it->current.dpvec_index = -1;
2845 it->base_face_id = remapped_base_face_id;
2846 it->string = Qnil;
2847 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2848 it->paragraph_embedding = L2R;
2849 it->bidi_it.string.lstring = Qnil;
2850 it->bidi_it.string.s = NULL;
2851 it->bidi_it.string.bufpos = 0;
2852 it->bidi_it.w = w;
2854 /* The window in which we iterate over current_buffer: */
2855 XSETWINDOW (it->window, w);
2856 it->w = w;
2857 it->f = XFRAME (w->frame);
2859 it->cmp_it.id = -1;
2861 /* Extra space between lines (on window systems only). */
2862 if (base_face_id == DEFAULT_FACE_ID
2863 && FRAME_WINDOW_P (it->f))
2865 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2866 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2867 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2868 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2869 * FRAME_LINE_HEIGHT (it->f));
2870 else if (it->f->extra_line_spacing > 0)
2871 it->extra_line_spacing = it->f->extra_line_spacing;
2872 it->max_extra_line_spacing = 0;
2875 /* If realized faces have been removed, e.g. because of face
2876 attribute changes of named faces, recompute them. When running
2877 in batch mode, the face cache of the initial frame is null. If
2878 we happen to get called, make a dummy face cache. */
2879 if (FRAME_FACE_CACHE (it->f) == NULL)
2880 init_frame_faces (it->f);
2881 if (FRAME_FACE_CACHE (it->f)->used == 0)
2882 recompute_basic_faces (it->f);
2884 /* Current value of the `slice', `space-width', and 'height' properties. */
2885 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2886 it->space_width = Qnil;
2887 it->font_height = Qnil;
2888 it->override_ascent = -1;
2890 /* Are control characters displayed as `^C'? */
2891 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2893 /* -1 means everything between a CR and the following line end
2894 is invisible. >0 means lines indented more than this value are
2895 invisible. */
2896 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2897 ? (clip_to_bounds
2898 (-1, XINT (BVAR (current_buffer, selective_display)),
2899 PTRDIFF_MAX))
2900 : (!NILP (BVAR (current_buffer, selective_display))
2901 ? -1 : 0));
2902 it->selective_display_ellipsis_p
2903 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2905 /* Display table to use. */
2906 it->dp = window_display_table (w);
2908 /* Are multibyte characters enabled in current_buffer? */
2909 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2911 /* Get the position at which the redisplay_end_trigger hook should
2912 be run, if it is to be run at all. */
2913 if (MARKERP (w->redisplay_end_trigger)
2914 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2915 it->redisplay_end_trigger_charpos
2916 = marker_position (w->redisplay_end_trigger);
2917 else if (INTEGERP (w->redisplay_end_trigger))
2918 it->redisplay_end_trigger_charpos
2919 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2920 PTRDIFF_MAX);
2922 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2924 /* Are lines in the display truncated? */
2925 if (base_face_id != DEFAULT_FACE_ID
2926 || it->w->hscroll
2927 || (! WINDOW_FULL_WIDTH_P (it->w)
2928 && ((!NILP (Vtruncate_partial_width_windows)
2929 && !INTEGERP (Vtruncate_partial_width_windows))
2930 || (INTEGERP (Vtruncate_partial_width_windows)
2931 /* PXW: Shall we do something about this? */
2932 && (WINDOW_TOTAL_COLS (it->w)
2933 < XINT (Vtruncate_partial_width_windows))))))
2934 it->line_wrap = TRUNCATE;
2935 else if (NILP (BVAR (current_buffer, truncate_lines)))
2936 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2937 ? WINDOW_WRAP : WORD_WRAP;
2938 else
2939 it->line_wrap = TRUNCATE;
2941 /* Get dimensions of truncation and continuation glyphs. These are
2942 displayed as fringe bitmaps under X, but we need them for such
2943 frames when the fringes are turned off. But leave the dimensions
2944 zero for tooltip frames, as these glyphs look ugly there and also
2945 sabotage calculations of tooltip dimensions in x-show-tip. */
2946 #ifdef HAVE_WINDOW_SYSTEM
2947 if (!(FRAME_WINDOW_P (it->f)
2948 && FRAMEP (tip_frame)
2949 && it->f == XFRAME (tip_frame)))
2950 #endif
2952 if (it->line_wrap == TRUNCATE)
2954 /* We will need the truncation glyph. */
2955 eassert (it->glyph_row == NULL);
2956 produce_special_glyphs (it, IT_TRUNCATION);
2957 it->truncation_pixel_width = it->pixel_width;
2959 else
2961 /* We will need the continuation glyph. */
2962 eassert (it->glyph_row == NULL);
2963 produce_special_glyphs (it, IT_CONTINUATION);
2964 it->continuation_pixel_width = it->pixel_width;
2968 /* Reset these values to zero because the produce_special_glyphs
2969 above has changed them. */
2970 it->pixel_width = it->ascent = it->descent = 0;
2971 it->phys_ascent = it->phys_descent = 0;
2973 /* Set this after getting the dimensions of truncation and
2974 continuation glyphs, so that we don't produce glyphs when calling
2975 produce_special_glyphs, above. */
2976 it->glyph_row = row;
2977 it->area = TEXT_AREA;
2979 /* Get the dimensions of the display area. The display area
2980 consists of the visible window area plus a horizontally scrolled
2981 part to the left of the window. All x-values are relative to the
2982 start of this total display area. */
2983 if (base_face_id != DEFAULT_FACE_ID)
2985 /* Mode lines, menu bar in terminal frames. */
2986 it->first_visible_x = 0;
2987 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2989 else
2991 it->first_visible_x
2992 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2993 it->last_visible_x = (it->first_visible_x
2994 + window_box_width (w, TEXT_AREA));
2996 /* If we truncate lines, leave room for the truncation glyph(s) at
2997 the right margin. Otherwise, leave room for the continuation
2998 glyph(s). Done only if the window has no right fringe. */
2999 if (WINDOW_RIGHT_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 /* Since we don't know at this point whether there will be
3065 any R2L lines in the window, we reserve space for
3066 truncation/continuation glyphs even if only the left
3067 fringe is absent. */
3068 if (base_face_id == DEFAULT_FACE_ID
3069 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3070 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3072 if (it->line_wrap == TRUNCATE)
3073 it->last_visible_x -= it->truncation_pixel_width;
3074 else
3075 it->last_visible_x -= it->continuation_pixel_width;
3077 /* Note the paragraph direction that this buffer wants to
3078 use. */
3079 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3080 Qleft_to_right))
3081 it->paragraph_embedding = L2R;
3082 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3083 Qright_to_left))
3084 it->paragraph_embedding = R2L;
3085 else
3086 it->paragraph_embedding = NEUTRAL_DIR;
3087 bidi_unshelve_cache (NULL, 0);
3088 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3089 &it->bidi_it);
3092 /* Compute faces etc. */
3093 reseat (it, it->current.pos, 1);
3096 CHECK_IT (it);
3100 /* Initialize IT for the display of window W with window start POS. */
3102 void
3103 start_display (struct it *it, struct window *w, struct text_pos pos)
3105 struct glyph_row *row;
3106 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3108 row = w->desired_matrix->rows + first_vpos;
3109 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3110 it->first_vpos = first_vpos;
3112 /* Don't reseat to previous visible line start if current start
3113 position is in a string or image. */
3114 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3116 int start_at_line_beg_p;
3117 int first_y = it->current_y;
3119 /* If window start is not at a line start, skip forward to POS to
3120 get the correct continuation lines width. */
3121 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3122 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3123 if (!start_at_line_beg_p)
3125 int new_x;
3127 reseat_at_previous_visible_line_start (it);
3128 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3130 new_x = it->current_x + it->pixel_width;
3132 /* If lines are continued, this line may end in the middle
3133 of a multi-glyph character (e.g. a control character
3134 displayed as \003, or in the middle of an overlay
3135 string). In this case move_it_to above will not have
3136 taken us to the start of the continuation line but to the
3137 end of the continued line. */
3138 if (it->current_x > 0
3139 && it->line_wrap != TRUNCATE /* Lines are continued. */
3140 && (/* And glyph doesn't fit on the line. */
3141 new_x > it->last_visible_x
3142 /* Or it fits exactly and we're on a window
3143 system frame. */
3144 || (new_x == it->last_visible_x
3145 && FRAME_WINDOW_P (it->f)
3146 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3147 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3148 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3150 if ((it->current.dpvec_index >= 0
3151 || it->current.overlay_string_index >= 0)
3152 /* If we are on a newline from a display vector or
3153 overlay string, then we are already at the end of
3154 a screen line; no need to go to the next line in
3155 that case, as this line is not really continued.
3156 (If we do go to the next line, C-e will not DTRT.) */
3157 && it->c != '\n')
3159 set_iterator_to_next (it, 1);
3160 move_it_in_display_line_to (it, -1, -1, 0);
3163 it->continuation_lines_width += it->current_x;
3165 /* If the character at POS is displayed via a display
3166 vector, move_it_to above stops at the final glyph of
3167 IT->dpvec. To make the caller redisplay that character
3168 again (a.k.a. start at POS), we need to reset the
3169 dpvec_index to the beginning of IT->dpvec. */
3170 else if (it->current.dpvec_index >= 0)
3171 it->current.dpvec_index = 0;
3173 /* We're starting a new display line, not affected by the
3174 height of the continued line, so clear the appropriate
3175 fields in the iterator structure. */
3176 it->max_ascent = it->max_descent = 0;
3177 it->max_phys_ascent = it->max_phys_descent = 0;
3179 it->current_y = first_y;
3180 it->vpos = 0;
3181 it->current_x = it->hpos = 0;
3187 /* Return 1 if POS is a position in ellipses displayed for invisible
3188 text. W is the window we display, for text property lookup. */
3190 static int
3191 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3193 Lisp_Object prop, window;
3194 int ellipses_p = 0;
3195 ptrdiff_t charpos = CHARPOS (pos->pos);
3197 /* If POS specifies a position in a display vector, this might
3198 be for an ellipsis displayed for invisible text. We won't
3199 get the iterator set up for delivering that ellipsis unless
3200 we make sure that it gets aware of the invisible text. */
3201 if (pos->dpvec_index >= 0
3202 && pos->overlay_string_index < 0
3203 && CHARPOS (pos->string_pos) < 0
3204 && charpos > BEGV
3205 && (XSETWINDOW (window, w),
3206 prop = Fget_char_property (make_number (charpos),
3207 Qinvisible, window),
3208 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3210 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3211 window);
3212 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3215 return ellipses_p;
3219 /* Initialize IT for stepping through current_buffer in window W,
3220 starting at position POS that includes overlay string and display
3221 vector/ control character translation position information. Value
3222 is zero if there are overlay strings with newlines at POS. */
3224 static int
3225 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3227 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3228 int i, overlay_strings_with_newlines = 0;
3230 /* If POS specifies a position in a display vector, this might
3231 be for an ellipsis displayed for invisible text. We won't
3232 get the iterator set up for delivering that ellipsis unless
3233 we make sure that it gets aware of the invisible text. */
3234 if (in_ellipses_for_invisible_text_p (pos, w))
3236 --charpos;
3237 bytepos = 0;
3240 /* Keep in mind: the call to reseat in init_iterator skips invisible
3241 text, so we might end up at a position different from POS. This
3242 is only a problem when POS is a row start after a newline and an
3243 overlay starts there with an after-string, and the overlay has an
3244 invisible property. Since we don't skip invisible text in
3245 display_line and elsewhere immediately after consuming the
3246 newline before the row start, such a POS will not be in a string,
3247 but the call to init_iterator below will move us to the
3248 after-string. */
3249 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3251 /* This only scans the current chunk -- it should scan all chunks.
3252 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3253 to 16 in 22.1 to make this a lesser problem. */
3254 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3256 const char *s = SSDATA (it->overlay_strings[i]);
3257 const char *e = s + SBYTES (it->overlay_strings[i]);
3259 while (s < e && *s != '\n')
3260 ++s;
3262 if (s < e)
3264 overlay_strings_with_newlines = 1;
3265 break;
3269 /* If position is within an overlay string, set up IT to the right
3270 overlay string. */
3271 if (pos->overlay_string_index >= 0)
3273 int relative_index;
3275 /* If the first overlay string happens to have a `display'
3276 property for an image, the iterator will be set up for that
3277 image, and we have to undo that setup first before we can
3278 correct the overlay string index. */
3279 if (it->method == GET_FROM_IMAGE)
3280 pop_it (it);
3282 /* We already have the first chunk of overlay strings in
3283 IT->overlay_strings. Load more until the one for
3284 pos->overlay_string_index is in IT->overlay_strings. */
3285 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3287 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3288 it->current.overlay_string_index = 0;
3289 while (n--)
3291 load_overlay_strings (it, 0);
3292 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3296 it->current.overlay_string_index = pos->overlay_string_index;
3297 relative_index = (it->current.overlay_string_index
3298 % OVERLAY_STRING_CHUNK_SIZE);
3299 it->string = it->overlay_strings[relative_index];
3300 eassert (STRINGP (it->string));
3301 it->current.string_pos = pos->string_pos;
3302 it->method = GET_FROM_STRING;
3303 it->end_charpos = SCHARS (it->string);
3304 /* Set up the bidi iterator for this overlay string. */
3305 if (it->bidi_p)
3307 it->bidi_it.string.lstring = it->string;
3308 it->bidi_it.string.s = NULL;
3309 it->bidi_it.string.schars = SCHARS (it->string);
3310 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3311 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3312 it->bidi_it.string.unibyte = !it->multibyte_p;
3313 it->bidi_it.w = it->w;
3314 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3315 FRAME_WINDOW_P (it->f), &it->bidi_it);
3317 /* Synchronize the state of the bidi iterator with
3318 pos->string_pos. For any string position other than
3319 zero, this will be done automagically when we resume
3320 iteration over the string and get_visually_first_element
3321 is called. But if string_pos is zero, and the string is
3322 to be reordered for display, we need to resync manually,
3323 since it could be that the iteration state recorded in
3324 pos ended at string_pos of 0 moving backwards in string. */
3325 if (CHARPOS (pos->string_pos) == 0)
3327 get_visually_first_element (it);
3328 if (IT_STRING_CHARPOS (*it) != 0)
3329 do {
3330 /* Paranoia. */
3331 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3332 bidi_move_to_visually_next (&it->bidi_it);
3333 } while (it->bidi_it.charpos != 0);
3335 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3336 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3340 if (CHARPOS (pos->string_pos) >= 0)
3342 /* Recorded position is not in an overlay string, but in another
3343 string. This can only be a string from a `display' property.
3344 IT should already be filled with that string. */
3345 it->current.string_pos = pos->string_pos;
3346 eassert (STRINGP (it->string));
3347 if (it->bidi_p)
3348 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3349 FRAME_WINDOW_P (it->f), &it->bidi_it);
3352 /* Restore position in display vector translations, control
3353 character translations or ellipses. */
3354 if (pos->dpvec_index >= 0)
3356 if (it->dpvec == NULL)
3357 get_next_display_element (it);
3358 eassert (it->dpvec && it->current.dpvec_index == 0);
3359 it->current.dpvec_index = pos->dpvec_index;
3362 CHECK_IT (it);
3363 return !overlay_strings_with_newlines;
3367 /* Initialize IT for stepping through current_buffer in window W
3368 starting at ROW->start. */
3370 static void
3371 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3373 init_from_display_pos (it, w, &row->start);
3374 it->start = row->start;
3375 it->continuation_lines_width = row->continuation_lines_width;
3376 CHECK_IT (it);
3380 /* Initialize IT for stepping through current_buffer in window W
3381 starting in the line following ROW, i.e. starting at ROW->end.
3382 Value is zero if there are overlay strings with newlines at ROW's
3383 end position. */
3385 static int
3386 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3388 int success = 0;
3390 if (init_from_display_pos (it, w, &row->end))
3392 if (row->continued_p)
3393 it->continuation_lines_width
3394 = row->continuation_lines_width + row->pixel_width;
3395 CHECK_IT (it);
3396 success = 1;
3399 return success;
3405 /***********************************************************************
3406 Text properties
3407 ***********************************************************************/
3409 /* Called when IT reaches IT->stop_charpos. Handle text property and
3410 overlay changes. Set IT->stop_charpos to the next position where
3411 to stop. */
3413 static void
3414 handle_stop (struct it *it)
3416 enum prop_handled handled;
3417 int handle_overlay_change_p;
3418 struct props *p;
3420 it->dpvec = NULL;
3421 it->current.dpvec_index = -1;
3422 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3423 it->ignore_overlay_strings_at_pos_p = 0;
3424 it->ellipsis_p = 0;
3426 /* Use face of preceding text for ellipsis (if invisible) */
3427 if (it->selective_display_ellipsis_p)
3428 it->saved_face_id = it->face_id;
3430 /* Here's the description of the semantics of, and the logic behind,
3431 the various HANDLED_* statuses:
3433 HANDLED_NORMALLY means the handler did its job, and the loop
3434 should proceed to calling the next handler in order.
3436 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3437 change in the properties and overlays at current position, so the
3438 loop should be restarted, to re-invoke the handlers that were
3439 already called. This happens when fontification-functions were
3440 called by handle_fontified_prop, and actually fontified
3441 something. Another case where HANDLED_RECOMPUTE_PROPS is
3442 returned is when we discover overlay strings that need to be
3443 displayed right away. The loop below will continue for as long
3444 as the status is HANDLED_RECOMPUTE_PROPS.
3446 HANDLED_RETURN means return immediately to the caller, to
3447 continue iteration without calling any further handlers. This is
3448 used when we need to act on some property right away, for example
3449 when we need to display the ellipsis or a replacing display
3450 property, such as display string or image.
3452 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3453 consumed, and the handler switched to the next overlay string.
3454 This signals the loop below to refrain from looking for more
3455 overlays before all the overlay strings of the current overlay
3456 are processed.
3458 Some of the handlers called by the loop push the iterator state
3459 onto the stack (see 'push_it'), and arrange for the iteration to
3460 continue with another object, such as an image, a display string,
3461 or an overlay string. In most such cases, it->stop_charpos is
3462 set to the first character of the string, so that when the
3463 iteration resumes, this function will immediately be called
3464 again, to examine the properties at the beginning of the string.
3466 When a display or overlay string is exhausted, the iterator state
3467 is popped (see 'pop_it'), and iteration continues with the
3468 previous object. Again, in many such cases this function is
3469 called again to find the next position where properties might
3470 change. */
3474 handled = HANDLED_NORMALLY;
3476 /* Call text property handlers. */
3477 for (p = it_props; p->handler; ++p)
3479 handled = p->handler (it);
3481 if (handled == HANDLED_RECOMPUTE_PROPS)
3482 break;
3483 else if (handled == HANDLED_RETURN)
3485 /* We still want to show before and after strings from
3486 overlays even if the actual buffer text is replaced. */
3487 if (!handle_overlay_change_p
3488 || it->sp > 1
3489 /* Don't call get_overlay_strings_1 if we already
3490 have overlay strings loaded, because doing so
3491 will load them again and push the iterator state
3492 onto the stack one more time, which is not
3493 expected by the rest of the code that processes
3494 overlay strings. */
3495 || (it->current.overlay_string_index < 0
3496 ? !get_overlay_strings_1 (it, 0, 0)
3497 : 0))
3499 if (it->ellipsis_p)
3500 setup_for_ellipsis (it, 0);
3501 /* When handling a display spec, we might load an
3502 empty string. In that case, discard it here. We
3503 used to discard it in handle_single_display_spec,
3504 but that causes get_overlay_strings_1, above, to
3505 ignore overlay strings that we must check. */
3506 if (STRINGP (it->string) && !SCHARS (it->string))
3507 pop_it (it);
3508 return;
3510 else if (STRINGP (it->string) && !SCHARS (it->string))
3511 pop_it (it);
3512 else
3514 it->ignore_overlay_strings_at_pos_p = true;
3515 it->string_from_display_prop_p = 0;
3516 it->from_disp_prop_p = 0;
3517 handle_overlay_change_p = 0;
3519 handled = HANDLED_RECOMPUTE_PROPS;
3520 break;
3522 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3523 handle_overlay_change_p = 0;
3526 if (handled != HANDLED_RECOMPUTE_PROPS)
3528 /* Don't check for overlay strings below when set to deliver
3529 characters from a display vector. */
3530 if (it->method == GET_FROM_DISPLAY_VECTOR)
3531 handle_overlay_change_p = 0;
3533 /* Handle overlay changes.
3534 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3535 if it finds overlays. */
3536 if (handle_overlay_change_p)
3537 handled = handle_overlay_change (it);
3540 if (it->ellipsis_p)
3542 setup_for_ellipsis (it, 0);
3543 break;
3546 while (handled == HANDLED_RECOMPUTE_PROPS);
3548 /* Determine where to stop next. */
3549 if (handled == HANDLED_NORMALLY)
3550 compute_stop_pos (it);
3554 /* Compute IT->stop_charpos from text property and overlay change
3555 information for IT's current position. */
3557 static void
3558 compute_stop_pos (struct it *it)
3560 register INTERVAL iv, next_iv;
3561 Lisp_Object object, limit, position;
3562 ptrdiff_t charpos, bytepos;
3564 if (STRINGP (it->string))
3566 /* Strings are usually short, so don't limit the search for
3567 properties. */
3568 it->stop_charpos = it->end_charpos;
3569 object = it->string;
3570 limit = Qnil;
3571 charpos = IT_STRING_CHARPOS (*it);
3572 bytepos = IT_STRING_BYTEPOS (*it);
3574 else
3576 ptrdiff_t pos;
3578 /* If end_charpos is out of range for some reason, such as a
3579 misbehaving display function, rationalize it (Bug#5984). */
3580 if (it->end_charpos > ZV)
3581 it->end_charpos = ZV;
3582 it->stop_charpos = it->end_charpos;
3584 /* If next overlay change is in front of the current stop pos
3585 (which is IT->end_charpos), stop there. Note: value of
3586 next_overlay_change is point-max if no overlay change
3587 follows. */
3588 charpos = IT_CHARPOS (*it);
3589 bytepos = IT_BYTEPOS (*it);
3590 pos = next_overlay_change (charpos);
3591 if (pos < it->stop_charpos)
3592 it->stop_charpos = pos;
3594 /* Set up variables for computing the stop position from text
3595 property changes. */
3596 XSETBUFFER (object, current_buffer);
3597 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3600 /* Get the interval containing IT's position. Value is a null
3601 interval if there isn't such an interval. */
3602 position = make_number (charpos);
3603 iv = validate_interval_range (object, &position, &position, 0);
3604 if (iv)
3606 Lisp_Object values_here[LAST_PROP_IDX];
3607 struct props *p;
3609 /* Get properties here. */
3610 for (p = it_props; p->handler; ++p)
3611 values_here[p->idx] = textget (iv->plist, *p->name);
3613 /* Look for an interval following iv that has different
3614 properties. */
3615 for (next_iv = next_interval (iv);
3616 (next_iv
3617 && (NILP (limit)
3618 || XFASTINT (limit) > next_iv->position));
3619 next_iv = next_interval (next_iv))
3621 for (p = it_props; p->handler; ++p)
3623 Lisp_Object new_value;
3625 new_value = textget (next_iv->plist, *p->name);
3626 if (!EQ (values_here[p->idx], new_value))
3627 break;
3630 if (p->handler)
3631 break;
3634 if (next_iv)
3636 if (INTEGERP (limit)
3637 && next_iv->position >= XFASTINT (limit))
3638 /* No text property change up to limit. */
3639 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3640 else
3641 /* Text properties change in next_iv. */
3642 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3646 if (it->cmp_it.id < 0)
3648 ptrdiff_t stoppos = it->end_charpos;
3650 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3651 stoppos = -1;
3652 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3653 stoppos, it->string);
3656 eassert (STRINGP (it->string)
3657 || (it->stop_charpos >= BEGV
3658 && it->stop_charpos >= IT_CHARPOS (*it)));
3662 /* Return the position of the next overlay change after POS in
3663 current_buffer. Value is point-max if no overlay change
3664 follows. This is like `next-overlay-change' but doesn't use
3665 xmalloc. */
3667 static ptrdiff_t
3668 next_overlay_change (ptrdiff_t pos)
3670 ptrdiff_t i, noverlays;
3671 ptrdiff_t endpos;
3672 Lisp_Object *overlays;
3673 USE_SAFE_ALLOCA;
3675 /* Get all overlays at the given position. */
3676 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3678 /* If any of these overlays ends before endpos,
3679 use its ending point instead. */
3680 for (i = 0; i < noverlays; ++i)
3682 Lisp_Object oend;
3683 ptrdiff_t oendpos;
3685 oend = OVERLAY_END (overlays[i]);
3686 oendpos = OVERLAY_POSITION (oend);
3687 endpos = min (endpos, oendpos);
3690 SAFE_FREE ();
3691 return endpos;
3694 /* How many characters forward to search for a display property or
3695 display string. Searching too far forward makes the bidi display
3696 sluggish, especially in small windows. */
3697 #define MAX_DISP_SCAN 250
3699 /* Return the character position of a display string at or after
3700 position specified by POSITION. If no display string exists at or
3701 after POSITION, return ZV. A display string is either an overlay
3702 with `display' property whose value is a string, or a `display'
3703 text property whose value is a string. STRING is data about the
3704 string to iterate; if STRING->lstring is nil, we are iterating a
3705 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3706 on a GUI frame. DISP_PROP is set to zero if we searched
3707 MAX_DISP_SCAN characters forward without finding any display
3708 strings, non-zero otherwise. It is set to 2 if the display string
3709 uses any kind of `(space ...)' spec that will produce a stretch of
3710 white space in the text area. */
3711 ptrdiff_t
3712 compute_display_string_pos (struct text_pos *position,
3713 struct bidi_string_data *string,
3714 struct window *w,
3715 int frame_window_p, int *disp_prop)
3717 /* OBJECT = nil means current buffer. */
3718 Lisp_Object object, object1;
3719 Lisp_Object pos, spec, limpos;
3720 int string_p = (string && (STRINGP (string->lstring) || string->s));
3721 ptrdiff_t eob = string_p ? string->schars : ZV;
3722 ptrdiff_t begb = string_p ? 0 : BEGV;
3723 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3724 ptrdiff_t lim =
3725 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3726 struct text_pos tpos;
3727 int rv = 0;
3729 if (string && STRINGP (string->lstring))
3730 object1 = object = string->lstring;
3731 else if (w && !string_p)
3733 XSETWINDOW (object, w);
3734 object1 = Qnil;
3736 else
3737 object1 = object = Qnil;
3739 *disp_prop = 1;
3741 if (charpos >= eob
3742 /* We don't support display properties whose values are strings
3743 that have display string properties. */
3744 || string->from_disp_str
3745 /* C strings cannot have display properties. */
3746 || (string->s && !STRINGP (object)))
3748 *disp_prop = 0;
3749 return eob;
3752 /* If the character at CHARPOS is where the display string begins,
3753 return CHARPOS. */
3754 pos = make_number (charpos);
3755 if (STRINGP (object))
3756 bufpos = string->bufpos;
3757 else
3758 bufpos = charpos;
3759 tpos = *position;
3760 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3761 && (charpos <= begb
3762 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3763 object),
3764 spec))
3765 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3766 frame_window_p)))
3768 if (rv == 2)
3769 *disp_prop = 2;
3770 return charpos;
3773 /* Look forward for the first character with a `display' property
3774 that will replace the underlying text when displayed. */
3775 limpos = make_number (lim);
3776 do {
3777 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3778 CHARPOS (tpos) = XFASTINT (pos);
3779 if (CHARPOS (tpos) >= lim)
3781 *disp_prop = 0;
3782 break;
3784 if (STRINGP (object))
3785 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3786 else
3787 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3788 spec = Fget_char_property (pos, Qdisplay, object);
3789 if (!STRINGP (object))
3790 bufpos = CHARPOS (tpos);
3791 } while (NILP (spec)
3792 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3793 bufpos, frame_window_p)));
3794 if (rv == 2)
3795 *disp_prop = 2;
3797 return CHARPOS (tpos);
3800 /* Return the character position of the end of the display string that
3801 started at CHARPOS. If there's no display string at CHARPOS,
3802 return -1. A display string is either an overlay with `display'
3803 property whose value is a string or a `display' text property whose
3804 value is a string. */
3805 ptrdiff_t
3806 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3808 /* OBJECT = nil means current buffer. */
3809 Lisp_Object object =
3810 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3811 Lisp_Object pos = make_number (charpos);
3812 ptrdiff_t eob =
3813 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3815 if (charpos >= eob || (string->s && !STRINGP (object)))
3816 return eob;
3818 /* It could happen that the display property or overlay was removed
3819 since we found it in compute_display_string_pos above. One way
3820 this can happen is if JIT font-lock was called (through
3821 handle_fontified_prop), and jit-lock-functions remove text
3822 properties or overlays from the portion of buffer that includes
3823 CHARPOS. Muse mode is known to do that, for example. In this
3824 case, we return -1 to the caller, to signal that no display
3825 string is actually present at CHARPOS. See bidi_fetch_char for
3826 how this is handled.
3828 An alternative would be to never look for display properties past
3829 it->stop_charpos. But neither compute_display_string_pos nor
3830 bidi_fetch_char that calls it know or care where the next
3831 stop_charpos is. */
3832 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3833 return -1;
3835 /* Look forward for the first character where the `display' property
3836 changes. */
3837 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3839 return XFASTINT (pos);
3844 /***********************************************************************
3845 Fontification
3846 ***********************************************************************/
3848 /* Handle changes in the `fontified' property of the current buffer by
3849 calling hook functions from Qfontification_functions to fontify
3850 regions of text. */
3852 static enum prop_handled
3853 handle_fontified_prop (struct it *it)
3855 Lisp_Object prop, pos;
3856 enum prop_handled handled = HANDLED_NORMALLY;
3858 if (!NILP (Vmemory_full))
3859 return handled;
3861 /* Get the value of the `fontified' property at IT's current buffer
3862 position. (The `fontified' property doesn't have a special
3863 meaning in strings.) If the value is nil, call functions from
3864 Qfontification_functions. */
3865 if (!STRINGP (it->string)
3866 && it->s == NULL
3867 && !NILP (Vfontification_functions)
3868 && !NILP (Vrun_hooks)
3869 && (pos = make_number (IT_CHARPOS (*it)),
3870 prop = Fget_char_property (pos, Qfontified, Qnil),
3871 /* Ignore the special cased nil value always present at EOB since
3872 no amount of fontifying will be able to change it. */
3873 NILP (prop) && IT_CHARPOS (*it) < Z))
3875 ptrdiff_t count = SPECPDL_INDEX ();
3876 Lisp_Object val;
3877 struct buffer *obuf = current_buffer;
3878 ptrdiff_t begv = BEGV, zv = ZV;
3879 bool old_clip_changed = current_buffer->clip_changed;
3881 val = Vfontification_functions;
3882 specbind (Qfontification_functions, Qnil);
3884 eassert (it->end_charpos == ZV);
3886 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3887 safe_call1 (val, pos);
3888 else
3890 Lisp_Object fns, fn;
3891 struct gcpro gcpro1, gcpro2;
3893 fns = Qnil;
3894 GCPRO2 (val, fns);
3896 for (; CONSP (val); val = XCDR (val))
3898 fn = XCAR (val);
3900 if (EQ (fn, Qt))
3902 /* A value of t indicates this hook has a local
3903 binding; it means to run the global binding too.
3904 In a global value, t should not occur. If it
3905 does, we must ignore it to avoid an endless
3906 loop. */
3907 for (fns = Fdefault_value (Qfontification_functions);
3908 CONSP (fns);
3909 fns = XCDR (fns))
3911 fn = XCAR (fns);
3912 if (!EQ (fn, Qt))
3913 safe_call1 (fn, pos);
3916 else
3917 safe_call1 (fn, pos);
3920 UNGCPRO;
3923 unbind_to (count, Qnil);
3925 /* Fontification functions routinely call `save-restriction'.
3926 Normally, this tags clip_changed, which can confuse redisplay
3927 (see discussion in Bug#6671). Since we don't perform any
3928 special handling of fontification changes in the case where
3929 `save-restriction' isn't called, there's no point doing so in
3930 this case either. So, if the buffer's restrictions are
3931 actually left unchanged, reset clip_changed. */
3932 if (obuf == current_buffer)
3934 if (begv == BEGV && zv == ZV)
3935 current_buffer->clip_changed = old_clip_changed;
3937 /* There isn't much we can reasonably do to protect against
3938 misbehaving fontification, but here's a fig leaf. */
3939 else if (BUFFER_LIVE_P (obuf))
3940 set_buffer_internal_1 (obuf);
3942 /* The fontification code may have added/removed text.
3943 It could do even a lot worse, but let's at least protect against
3944 the most obvious case where only the text past `pos' gets changed',
3945 as is/was done in grep.el where some escapes sequences are turned
3946 into face properties (bug#7876). */
3947 it->end_charpos = ZV;
3949 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3950 something. This avoids an endless loop if they failed to
3951 fontify the text for which reason ever. */
3952 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3953 handled = HANDLED_RECOMPUTE_PROPS;
3956 return handled;
3961 /***********************************************************************
3962 Faces
3963 ***********************************************************************/
3965 /* Set up iterator IT from face properties at its current position.
3966 Called from handle_stop. */
3968 static enum prop_handled
3969 handle_face_prop (struct it *it)
3971 int new_face_id;
3972 ptrdiff_t next_stop;
3974 if (!STRINGP (it->string))
3976 new_face_id
3977 = face_at_buffer_position (it->w,
3978 IT_CHARPOS (*it),
3979 &next_stop,
3980 (IT_CHARPOS (*it)
3981 + TEXT_PROP_DISTANCE_LIMIT),
3982 0, it->base_face_id);
3984 /* Is this a start of a run of characters with box face?
3985 Caveat: this can be called for a freshly initialized
3986 iterator; face_id is -1 in this case. We know that the new
3987 face will not change until limit, i.e. if the new face has a
3988 box, all characters up to limit will have one. But, as
3989 usual, we don't know whether limit is really the end. */
3990 if (new_face_id != it->face_id)
3992 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3993 /* If it->face_id is -1, old_face below will be NULL, see
3994 the definition of FACE_FROM_ID. This will happen if this
3995 is the initial call that gets the face. */
3996 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3998 /* If the value of face_id of the iterator is -1, we have to
3999 look in front of IT's position and see whether there is a
4000 face there that's different from new_face_id. */
4001 if (!old_face && IT_CHARPOS (*it) > BEG)
4003 int prev_face_id = face_before_it_pos (it);
4005 old_face = FACE_FROM_ID (it->f, prev_face_id);
4008 /* If the new face has a box, but the old face does not,
4009 this is the start of a run of characters with box face,
4010 i.e. this character has a shadow on the left side. */
4011 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
4012 && (old_face == NULL || !old_face->box));
4013 it->face_box_p = new_face->box != FACE_NO_BOX;
4016 else
4018 int base_face_id;
4019 ptrdiff_t bufpos;
4020 int i;
4021 Lisp_Object from_overlay
4022 = (it->current.overlay_string_index >= 0
4023 ? it->string_overlays[it->current.overlay_string_index
4024 % OVERLAY_STRING_CHUNK_SIZE]
4025 : Qnil);
4027 /* See if we got to this string directly or indirectly from
4028 an overlay property. That includes the before-string or
4029 after-string of an overlay, strings in display properties
4030 provided by an overlay, their text properties, etc.
4032 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
4033 if (! NILP (from_overlay))
4034 for (i = it->sp - 1; i >= 0; i--)
4036 if (it->stack[i].current.overlay_string_index >= 0)
4037 from_overlay
4038 = it->string_overlays[it->stack[i].current.overlay_string_index
4039 % OVERLAY_STRING_CHUNK_SIZE];
4040 else if (! NILP (it->stack[i].from_overlay))
4041 from_overlay = it->stack[i].from_overlay;
4043 if (!NILP (from_overlay))
4044 break;
4047 if (! NILP (from_overlay))
4049 bufpos = IT_CHARPOS (*it);
4050 /* For a string from an overlay, the base face depends
4051 only on text properties and ignores overlays. */
4052 base_face_id
4053 = face_for_overlay_string (it->w,
4054 IT_CHARPOS (*it),
4055 &next_stop,
4056 (IT_CHARPOS (*it)
4057 + TEXT_PROP_DISTANCE_LIMIT),
4059 from_overlay);
4061 else
4063 bufpos = 0;
4065 /* For strings from a `display' property, use the face at
4066 IT's current buffer position as the base face to merge
4067 with, so that overlay strings appear in the same face as
4068 surrounding text, unless they specify their own faces.
4069 For strings from wrap-prefix and line-prefix properties,
4070 use the default face, possibly remapped via
4071 Vface_remapping_alist. */
4072 /* Note that the fact that we use the face at _buffer_
4073 position means that a 'display' property on an overlay
4074 string will not inherit the face of that overlay string,
4075 but will instead revert to the face of buffer text
4076 covered by the overlay. This is visible, e.g., when the
4077 overlay specifies a box face, but neither the buffer nor
4078 the display string do. This sounds like a design bug,
4079 but Emacs always did that since v21.1, so changing that
4080 might be a big deal. */
4081 base_face_id = it->string_from_prefix_prop_p
4082 ? (!NILP (Vface_remapping_alist)
4083 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4084 : DEFAULT_FACE_ID)
4085 : underlying_face_id (it);
4088 new_face_id = face_at_string_position (it->w,
4089 it->string,
4090 IT_STRING_CHARPOS (*it),
4091 bufpos,
4092 &next_stop,
4093 base_face_id, 0);
4095 /* Is this a start of a run of characters with box? Caveat:
4096 this can be called for a freshly allocated iterator; face_id
4097 is -1 is this case. We know that the new face will not
4098 change until the next check pos, i.e. if the new face has a
4099 box, all characters up to that position will have a
4100 box. But, as usual, we don't know whether that position
4101 is really the end. */
4102 if (new_face_id != it->face_id)
4104 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4105 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4107 /* If new face has a box but old face hasn't, this is the
4108 start of a run of characters with box, i.e. it has a
4109 shadow on the left side. */
4110 it->start_of_box_run_p
4111 = new_face->box && (old_face == NULL || !old_face->box);
4112 it->face_box_p = new_face->box != FACE_NO_BOX;
4116 it->face_id = new_face_id;
4117 return HANDLED_NORMALLY;
4121 /* Return the ID of the face ``underlying'' IT's current position,
4122 which is in a string. If the iterator is associated with a
4123 buffer, return the face at IT's current buffer position.
4124 Otherwise, use the iterator's base_face_id. */
4126 static int
4127 underlying_face_id (struct it *it)
4129 int face_id = it->base_face_id, i;
4131 eassert (STRINGP (it->string));
4133 for (i = it->sp - 1; i >= 0; --i)
4134 if (NILP (it->stack[i].string))
4135 face_id = it->stack[i].face_id;
4137 return face_id;
4141 /* Compute the face one character before or after the current position
4142 of IT, in the visual order. BEFORE_P non-zero means get the face
4143 in front (to the left in L2R paragraphs, to the right in R2L
4144 paragraphs) of IT's screen position. Value is the ID of the face. */
4146 static int
4147 face_before_or_after_it_pos (struct it *it, int before_p)
4149 int face_id, limit;
4150 ptrdiff_t next_check_charpos;
4151 struct it it_copy;
4152 void *it_copy_data = NULL;
4154 eassert (it->s == NULL);
4156 if (STRINGP (it->string))
4158 ptrdiff_t bufpos, charpos;
4159 int base_face_id;
4161 /* No face change past the end of the string (for the case
4162 we are padding with spaces). No face change before the
4163 string start. */
4164 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4165 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4166 return it->face_id;
4168 if (!it->bidi_p)
4170 /* Set charpos to the position before or after IT's current
4171 position, in the logical order, which in the non-bidi
4172 case is the same as the visual order. */
4173 if (before_p)
4174 charpos = IT_STRING_CHARPOS (*it) - 1;
4175 else if (it->what == IT_COMPOSITION)
4176 /* For composition, we must check the character after the
4177 composition. */
4178 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4179 else
4180 charpos = IT_STRING_CHARPOS (*it) + 1;
4182 else
4184 if (before_p)
4186 /* With bidi iteration, the character before the current
4187 in the visual order cannot be found by simple
4188 iteration, because "reverse" reordering is not
4189 supported. Instead, we need to use the move_it_*
4190 family of functions. */
4191 /* Ignore face changes before the first visible
4192 character on this display line. */
4193 if (it->current_x <= it->first_visible_x)
4194 return it->face_id;
4195 SAVE_IT (it_copy, *it, it_copy_data);
4196 /* Implementation note: Since move_it_in_display_line
4197 works in the iterator geometry, and thinks the first
4198 character is always the leftmost, even in R2L lines,
4199 we don't need to distinguish between the R2L and L2R
4200 cases here. */
4201 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4202 it_copy.current_x - 1, MOVE_TO_X);
4203 charpos = IT_STRING_CHARPOS (it_copy);
4204 RESTORE_IT (it, it, it_copy_data);
4206 else
4208 /* Set charpos to the string position of the character
4209 that comes after IT's current position in the visual
4210 order. */
4211 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4213 it_copy = *it;
4214 while (n--)
4215 bidi_move_to_visually_next (&it_copy.bidi_it);
4217 charpos = it_copy.bidi_it.charpos;
4220 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4222 if (it->current.overlay_string_index >= 0)
4223 bufpos = IT_CHARPOS (*it);
4224 else
4225 bufpos = 0;
4227 base_face_id = underlying_face_id (it);
4229 /* Get the face for ASCII, or unibyte. */
4230 face_id = face_at_string_position (it->w,
4231 it->string,
4232 charpos,
4233 bufpos,
4234 &next_check_charpos,
4235 base_face_id, 0);
4237 /* Correct the face for charsets different from ASCII. Do it
4238 for the multibyte case only. The face returned above is
4239 suitable for unibyte text if IT->string is unibyte. */
4240 if (STRING_MULTIBYTE (it->string))
4242 struct text_pos pos1 = string_pos (charpos, it->string);
4243 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4244 int c, len;
4245 struct face *face = FACE_FROM_ID (it->f, face_id);
4247 c = string_char_and_length (p, &len);
4248 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4251 else
4253 struct text_pos pos;
4255 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4256 || (IT_CHARPOS (*it) <= BEGV && before_p))
4257 return it->face_id;
4259 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4260 pos = it->current.pos;
4262 if (!it->bidi_p)
4264 if (before_p)
4265 DEC_TEXT_POS (pos, it->multibyte_p);
4266 else
4268 if (it->what == IT_COMPOSITION)
4270 /* For composition, we must check the position after
4271 the composition. */
4272 pos.charpos += it->cmp_it.nchars;
4273 pos.bytepos += it->len;
4275 else
4276 INC_TEXT_POS (pos, it->multibyte_p);
4279 else
4281 if (before_p)
4283 /* With bidi iteration, the character before the current
4284 in the visual order cannot be found by simple
4285 iteration, because "reverse" reordering is not
4286 supported. Instead, we need to use the move_it_*
4287 family of functions. */
4288 /* Ignore face changes before the first visible
4289 character on this display line. */
4290 if (it->current_x <= it->first_visible_x)
4291 return it->face_id;
4292 SAVE_IT (it_copy, *it, it_copy_data);
4293 /* Implementation note: Since move_it_in_display_line
4294 works in the iterator geometry, and thinks the first
4295 character is always the leftmost, even in R2L lines,
4296 we don't need to distinguish between the R2L and L2R
4297 cases here. */
4298 move_it_in_display_line (&it_copy, ZV,
4299 it_copy.current_x - 1, MOVE_TO_X);
4300 pos = it_copy.current.pos;
4301 RESTORE_IT (it, it, it_copy_data);
4303 else
4305 /* Set charpos to the buffer position of the character
4306 that comes after IT's current position in the visual
4307 order. */
4308 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4310 it_copy = *it;
4311 while (n--)
4312 bidi_move_to_visually_next (&it_copy.bidi_it);
4314 SET_TEXT_POS (pos,
4315 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4318 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4320 /* Determine face for CHARSET_ASCII, or unibyte. */
4321 face_id = face_at_buffer_position (it->w,
4322 CHARPOS (pos),
4323 &next_check_charpos,
4324 limit, 0, -1);
4326 /* Correct the face for charsets different from ASCII. Do it
4327 for the multibyte case only. The face returned above is
4328 suitable for unibyte text if current_buffer is unibyte. */
4329 if (it->multibyte_p)
4331 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4332 struct face *face = FACE_FROM_ID (it->f, face_id);
4333 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4337 return face_id;
4342 /***********************************************************************
4343 Invisible text
4344 ***********************************************************************/
4346 /* Set up iterator IT from invisible properties at its current
4347 position. Called from handle_stop. */
4349 static enum prop_handled
4350 handle_invisible_prop (struct it *it)
4352 enum prop_handled handled = HANDLED_NORMALLY;
4353 int invis_p;
4354 Lisp_Object prop;
4356 if (STRINGP (it->string))
4358 Lisp_Object end_charpos, limit, charpos;
4360 /* Get the value of the invisible text property at the
4361 current position. Value will be nil if there is no such
4362 property. */
4363 charpos = make_number (IT_STRING_CHARPOS (*it));
4364 prop = Fget_text_property (charpos, Qinvisible, it->string);
4365 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4367 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4369 /* Record whether we have to display an ellipsis for the
4370 invisible text. */
4371 int display_ellipsis_p = (invis_p == 2);
4372 ptrdiff_t len, endpos;
4374 handled = HANDLED_RECOMPUTE_PROPS;
4376 /* Get the position at which the next visible text can be
4377 found in IT->string, if any. */
4378 endpos = len = SCHARS (it->string);
4379 XSETINT (limit, len);
4382 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4383 it->string, limit);
4384 if (INTEGERP (end_charpos))
4386 endpos = XFASTINT (end_charpos);
4387 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4388 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4389 if (invis_p == 2)
4390 display_ellipsis_p = true;
4393 while (invis_p && endpos < len);
4395 if (display_ellipsis_p)
4396 it->ellipsis_p = true;
4398 if (endpos < len)
4400 /* Text at END_CHARPOS is visible. Move IT there. */
4401 struct text_pos old;
4402 ptrdiff_t oldpos;
4404 old = it->current.string_pos;
4405 oldpos = CHARPOS (old);
4406 if (it->bidi_p)
4408 if (it->bidi_it.first_elt
4409 && it->bidi_it.charpos < SCHARS (it->string))
4410 bidi_paragraph_init (it->paragraph_embedding,
4411 &it->bidi_it, 1);
4412 /* Bidi-iterate out of the invisible text. */
4415 bidi_move_to_visually_next (&it->bidi_it);
4417 while (oldpos <= it->bidi_it.charpos
4418 && it->bidi_it.charpos < endpos);
4420 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4421 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4422 if (IT_CHARPOS (*it) >= endpos)
4423 it->prev_stop = endpos;
4425 else
4427 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4428 compute_string_pos (&it->current.string_pos, old, it->string);
4431 else
4433 /* The rest of the string is invisible. If this is an
4434 overlay string, proceed with the next overlay string
4435 or whatever comes and return a character from there. */
4436 if (it->current.overlay_string_index >= 0
4437 && !display_ellipsis_p)
4439 next_overlay_string (it);
4440 /* Don't check for overlay strings when we just
4441 finished processing them. */
4442 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4444 else
4446 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4447 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4452 else
4454 ptrdiff_t newpos, next_stop, start_charpos, tem;
4455 Lisp_Object pos, overlay;
4457 /* First of all, is there invisible text at this position? */
4458 tem = start_charpos = IT_CHARPOS (*it);
4459 pos = make_number (tem);
4460 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4461 &overlay);
4462 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4464 /* If we are on invisible text, skip over it. */
4465 if (invis_p && start_charpos < it->end_charpos)
4467 /* Record whether we have to display an ellipsis for the
4468 invisible text. */
4469 int display_ellipsis_p = invis_p == 2;
4471 handled = HANDLED_RECOMPUTE_PROPS;
4473 /* Loop skipping over invisible text. The loop is left at
4474 ZV or with IT on the first char being visible again. */
4477 /* Try to skip some invisible text. Return value is the
4478 position reached which can be equal to where we start
4479 if there is nothing invisible there. This skips both
4480 over invisible text properties and overlays with
4481 invisible property. */
4482 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4484 /* If we skipped nothing at all we weren't at invisible
4485 text in the first place. If everything to the end of
4486 the buffer was skipped, end the loop. */
4487 if (newpos == tem || newpos >= ZV)
4488 invis_p = 0;
4489 else
4491 /* We skipped some characters but not necessarily
4492 all there are. Check if we ended up on visible
4493 text. Fget_char_property returns the property of
4494 the char before the given position, i.e. if we
4495 get invis_p = 0, this means that the char at
4496 newpos is visible. */
4497 pos = make_number (newpos);
4498 prop = Fget_char_property (pos, Qinvisible, it->window);
4499 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4502 /* If we ended up on invisible text, proceed to
4503 skip starting with next_stop. */
4504 if (invis_p)
4505 tem = next_stop;
4507 /* If there are adjacent invisible texts, don't lose the
4508 second one's ellipsis. */
4509 if (invis_p == 2)
4510 display_ellipsis_p = true;
4512 while (invis_p);
4514 /* The position newpos is now either ZV or on visible text. */
4515 if (it->bidi_p)
4517 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4518 int on_newline
4519 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4520 int after_newline
4521 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4523 /* If the invisible text ends on a newline or on a
4524 character after a newline, we can avoid the costly,
4525 character by character, bidi iteration to NEWPOS, and
4526 instead simply reseat the iterator there. That's
4527 because all bidi reordering information is tossed at
4528 the newline. This is a big win for modes that hide
4529 complete lines, like Outline, Org, etc. */
4530 if (on_newline || after_newline)
4532 struct text_pos tpos;
4533 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4535 SET_TEXT_POS (tpos, newpos, bpos);
4536 reseat_1 (it, tpos, 0);
4537 /* If we reseat on a newline/ZV, we need to prep the
4538 bidi iterator for advancing to the next character
4539 after the newline/EOB, keeping the current paragraph
4540 direction (so that PRODUCE_GLYPHS does TRT wrt
4541 prepending/appending glyphs to a glyph row). */
4542 if (on_newline)
4544 it->bidi_it.first_elt = 0;
4545 it->bidi_it.paragraph_dir = pdir;
4546 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4547 it->bidi_it.nchars = 1;
4548 it->bidi_it.ch_len = 1;
4551 else /* Must use the slow method. */
4553 /* With bidi iteration, the region of invisible text
4554 could start and/or end in the middle of a
4555 non-base embedding level. Therefore, we need to
4556 skip invisible text using the bidi iterator,
4557 starting at IT's current position, until we find
4558 ourselves outside of the invisible text.
4559 Skipping invisible text _after_ bidi iteration
4560 avoids affecting the visual order of the
4561 displayed text when invisible properties are
4562 added or removed. */
4563 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4565 /* If we were `reseat'ed to a new paragraph,
4566 determine the paragraph base direction. We
4567 need to do it now because
4568 next_element_from_buffer may not have a
4569 chance to do it, if we are going to skip any
4570 text at the beginning, which resets the
4571 FIRST_ELT flag. */
4572 bidi_paragraph_init (it->paragraph_embedding,
4573 &it->bidi_it, 1);
4577 bidi_move_to_visually_next (&it->bidi_it);
4579 while (it->stop_charpos <= it->bidi_it.charpos
4580 && it->bidi_it.charpos < newpos);
4581 IT_CHARPOS (*it) = it->bidi_it.charpos;
4582 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4583 /* If we overstepped NEWPOS, record its position in
4584 the iterator, so that we skip invisible text if
4585 later the bidi iteration lands us in the
4586 invisible region again. */
4587 if (IT_CHARPOS (*it) >= newpos)
4588 it->prev_stop = newpos;
4591 else
4593 IT_CHARPOS (*it) = newpos;
4594 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4597 /* If there are before-strings at the start of invisible
4598 text, and the text is invisible because of a text
4599 property, arrange to show before-strings because 20.x did
4600 it that way. (If the text is invisible because of an
4601 overlay property instead of a text property, this is
4602 already handled in the overlay code.) */
4603 if (NILP (overlay)
4604 && get_overlay_strings (it, it->stop_charpos))
4606 handled = HANDLED_RECOMPUTE_PROPS;
4607 if (it->sp > 0)
4609 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4610 /* The call to get_overlay_strings above recomputes
4611 it->stop_charpos, but it only considers changes
4612 in properties and overlays beyond iterator's
4613 current position. This causes us to miss changes
4614 that happen exactly where the invisible property
4615 ended. So we play it safe here and force the
4616 iterator to check for potential stop positions
4617 immediately after the invisible text. Note that
4618 if get_overlay_strings returns non-zero, it
4619 normally also pushed the iterator stack, so we
4620 need to update the stop position in the slot
4621 below the current one. */
4622 it->stack[it->sp - 1].stop_charpos
4623 = CHARPOS (it->stack[it->sp - 1].current.pos);
4626 else if (display_ellipsis_p)
4628 /* Make sure that the glyphs of the ellipsis will get
4629 correct `charpos' values. If we would not update
4630 it->position here, the glyphs would belong to the
4631 last visible character _before_ the invisible
4632 text, which confuses `set_cursor_from_row'.
4634 We use the last invisible position instead of the
4635 first because this way the cursor is always drawn on
4636 the first "." of the ellipsis, whenever PT is inside
4637 the invisible text. Otherwise the cursor would be
4638 placed _after_ the ellipsis when the point is after the
4639 first invisible character. */
4640 if (!STRINGP (it->object))
4642 it->position.charpos = newpos - 1;
4643 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4645 it->ellipsis_p = true;
4646 /* Let the ellipsis display before
4647 considering any properties of the following char.
4648 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4649 handled = HANDLED_RETURN;
4654 return handled;
4658 /* Make iterator IT return `...' next.
4659 Replaces LEN characters from buffer. */
4661 static void
4662 setup_for_ellipsis (struct it *it, int len)
4664 /* Use the display table definition for `...'. Invalid glyphs
4665 will be handled by the method returning elements from dpvec. */
4666 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4668 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4669 it->dpvec = v->contents;
4670 it->dpend = v->contents + v->header.size;
4672 else
4674 /* Default `...'. */
4675 it->dpvec = default_invis_vector;
4676 it->dpend = default_invis_vector + 3;
4679 it->dpvec_char_len = len;
4680 it->current.dpvec_index = 0;
4681 it->dpvec_face_id = -1;
4683 /* Remember the current face id in case glyphs specify faces.
4684 IT's face is restored in set_iterator_to_next.
4685 saved_face_id was set to preceding char's face in handle_stop. */
4686 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4687 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4689 it->method = GET_FROM_DISPLAY_VECTOR;
4690 it->ellipsis_p = true;
4695 /***********************************************************************
4696 'display' property
4697 ***********************************************************************/
4699 /* Set up iterator IT from `display' property at its current position.
4700 Called from handle_stop.
4701 We return HANDLED_RETURN if some part of the display property
4702 overrides the display of the buffer text itself.
4703 Otherwise we return HANDLED_NORMALLY. */
4705 static enum prop_handled
4706 handle_display_prop (struct it *it)
4708 Lisp_Object propval, object, overlay;
4709 struct text_pos *position;
4710 ptrdiff_t bufpos;
4711 /* Nonzero if some property replaces the display of the text itself. */
4712 int display_replaced_p = 0;
4714 if (STRINGP (it->string))
4716 object = it->string;
4717 position = &it->current.string_pos;
4718 bufpos = CHARPOS (it->current.pos);
4720 else
4722 XSETWINDOW (object, it->w);
4723 position = &it->current.pos;
4724 bufpos = CHARPOS (*position);
4727 /* Reset those iterator values set from display property values. */
4728 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4729 it->space_width = Qnil;
4730 it->font_height = Qnil;
4731 it->voffset = 0;
4733 /* We don't support recursive `display' properties, i.e. string
4734 values that have a string `display' property, that have a string
4735 `display' property etc. */
4736 if (!it->string_from_display_prop_p)
4737 it->area = TEXT_AREA;
4739 propval = get_char_property_and_overlay (make_number (position->charpos),
4740 Qdisplay, object, &overlay);
4741 if (NILP (propval))
4742 return HANDLED_NORMALLY;
4743 /* Now OVERLAY is the overlay that gave us this property, or nil
4744 if it was a text property. */
4746 if (!STRINGP (it->string))
4747 object = it->w->contents;
4749 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4750 position, bufpos,
4751 FRAME_WINDOW_P (it->f));
4753 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4756 /* Subroutine of handle_display_prop. Returns non-zero if the display
4757 specification in SPEC is a replacing specification, i.e. it would
4758 replace the text covered by `display' property with something else,
4759 such as an image or a display string. If SPEC includes any kind or
4760 `(space ...) specification, the value is 2; this is used by
4761 compute_display_string_pos, which see.
4763 See handle_single_display_spec for documentation of arguments.
4764 frame_window_p is non-zero if the window being redisplayed is on a
4765 GUI frame; this argument is used only if IT is NULL, see below.
4767 IT can be NULL, if this is called by the bidi reordering code
4768 through compute_display_string_pos, which see. In that case, this
4769 function only examines SPEC, but does not otherwise "handle" it, in
4770 the sense that it doesn't set up members of IT from the display
4771 spec. */
4772 static int
4773 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4774 Lisp_Object overlay, struct text_pos *position,
4775 ptrdiff_t bufpos, int frame_window_p)
4777 int replacing_p = 0;
4778 int rv;
4780 if (CONSP (spec)
4781 /* Simple specifications. */
4782 && !EQ (XCAR (spec), Qimage)
4783 && !EQ (XCAR (spec), Qspace)
4784 && !EQ (XCAR (spec), Qwhen)
4785 && !EQ (XCAR (spec), Qslice)
4786 && !EQ (XCAR (spec), Qspace_width)
4787 && !EQ (XCAR (spec), Qheight)
4788 && !EQ (XCAR (spec), Qraise)
4789 /* Marginal area specifications. */
4790 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4791 && !EQ (XCAR (spec), Qleft_fringe)
4792 && !EQ (XCAR (spec), Qright_fringe)
4793 && !NILP (XCAR (spec)))
4795 for (; CONSP (spec); spec = XCDR (spec))
4797 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4798 overlay, position, bufpos,
4799 replacing_p, frame_window_p)))
4801 replacing_p = rv;
4802 /* If some text in a string is replaced, `position' no
4803 longer points to the position of `object'. */
4804 if (!it || STRINGP (object))
4805 break;
4809 else if (VECTORP (spec))
4811 ptrdiff_t i;
4812 for (i = 0; i < ASIZE (spec); ++i)
4813 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4814 overlay, position, bufpos,
4815 replacing_p, frame_window_p)))
4817 replacing_p = rv;
4818 /* If some text in a string is replaced, `position' no
4819 longer points to the position of `object'. */
4820 if (!it || STRINGP (object))
4821 break;
4824 else
4826 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4827 position, bufpos, 0,
4828 frame_window_p)))
4829 replacing_p = rv;
4832 return replacing_p;
4835 /* Value is the position of the end of the `display' property starting
4836 at START_POS in OBJECT. */
4838 static struct text_pos
4839 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4841 Lisp_Object end;
4842 struct text_pos end_pos;
4844 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4845 Qdisplay, object, Qnil);
4846 CHARPOS (end_pos) = XFASTINT (end);
4847 if (STRINGP (object))
4848 compute_string_pos (&end_pos, start_pos, it->string);
4849 else
4850 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4852 return end_pos;
4856 /* Set up IT from a single `display' property specification SPEC. OBJECT
4857 is the object in which the `display' property was found. *POSITION
4858 is the position in OBJECT at which the `display' property was found.
4859 BUFPOS is the buffer position of OBJECT (different from POSITION if
4860 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4861 previously saw a display specification which already replaced text
4862 display with something else, for example an image; we ignore such
4863 properties after the first one has been processed.
4865 OVERLAY is the overlay this `display' property came from,
4866 or nil if it was a text property.
4868 If SPEC is a `space' or `image' specification, and in some other
4869 cases too, set *POSITION to the position where the `display'
4870 property ends.
4872 If IT is NULL, only examine the property specification in SPEC, but
4873 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4874 is intended to be displayed in a window on a GUI frame.
4876 Value is non-zero if something was found which replaces the display
4877 of buffer or string text. */
4879 static int
4880 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4881 Lisp_Object overlay, struct text_pos *position,
4882 ptrdiff_t bufpos, int display_replaced_p,
4883 int frame_window_p)
4885 Lisp_Object form;
4886 Lisp_Object location, value;
4887 struct text_pos start_pos = *position;
4888 int valid_p;
4890 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4891 If the result is non-nil, use VALUE instead of SPEC. */
4892 form = Qt;
4893 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4895 spec = XCDR (spec);
4896 if (!CONSP (spec))
4897 return 0;
4898 form = XCAR (spec);
4899 spec = XCDR (spec);
4902 if (!NILP (form) && !EQ (form, Qt))
4904 ptrdiff_t count = SPECPDL_INDEX ();
4905 struct gcpro gcpro1;
4907 /* Bind `object' to the object having the `display' property, a
4908 buffer or string. Bind `position' to the position in the
4909 object where the property was found, and `buffer-position'
4910 to the current position in the buffer. */
4912 if (NILP (object))
4913 XSETBUFFER (object, current_buffer);
4914 specbind (Qobject, object);
4915 specbind (Qposition, make_number (CHARPOS (*position)));
4916 specbind (Qbuffer_position, make_number (bufpos));
4917 GCPRO1 (form);
4918 form = safe_eval (form);
4919 UNGCPRO;
4920 unbind_to (count, Qnil);
4923 if (NILP (form))
4924 return 0;
4926 /* Handle `(height HEIGHT)' specifications. */
4927 if (CONSP (spec)
4928 && EQ (XCAR (spec), Qheight)
4929 && CONSP (XCDR (spec)))
4931 if (it)
4933 if (!FRAME_WINDOW_P (it->f))
4934 return 0;
4936 it->font_height = XCAR (XCDR (spec));
4937 if (!NILP (it->font_height))
4939 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4940 int new_height = -1;
4942 if (CONSP (it->font_height)
4943 && (EQ (XCAR (it->font_height), Qplus)
4944 || EQ (XCAR (it->font_height), Qminus))
4945 && CONSP (XCDR (it->font_height))
4946 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4948 /* `(+ N)' or `(- N)' where N is an integer. */
4949 int steps = XINT (XCAR (XCDR (it->font_height)));
4950 if (EQ (XCAR (it->font_height), Qplus))
4951 steps = - steps;
4952 it->face_id = smaller_face (it->f, it->face_id, steps);
4954 else if (FUNCTIONP (it->font_height))
4956 /* Call function with current height as argument.
4957 Value is the new height. */
4958 Lisp_Object height;
4959 height = safe_call1 (it->font_height,
4960 face->lface[LFACE_HEIGHT_INDEX]);
4961 if (NUMBERP (height))
4962 new_height = XFLOATINT (height);
4964 else if (NUMBERP (it->font_height))
4966 /* Value is a multiple of the canonical char height. */
4967 struct face *f;
4969 f = FACE_FROM_ID (it->f,
4970 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4971 new_height = (XFLOATINT (it->font_height)
4972 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4974 else
4976 /* Evaluate IT->font_height with `height' bound to the
4977 current specified height to get the new height. */
4978 ptrdiff_t count = SPECPDL_INDEX ();
4980 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4981 value = safe_eval (it->font_height);
4982 unbind_to (count, Qnil);
4984 if (NUMBERP (value))
4985 new_height = XFLOATINT (value);
4988 if (new_height > 0)
4989 it->face_id = face_with_height (it->f, it->face_id, new_height);
4993 return 0;
4996 /* Handle `(space-width WIDTH)'. */
4997 if (CONSP (spec)
4998 && EQ (XCAR (spec), Qspace_width)
4999 && CONSP (XCDR (spec)))
5001 if (it)
5003 if (!FRAME_WINDOW_P (it->f))
5004 return 0;
5006 value = XCAR (XCDR (spec));
5007 if (NUMBERP (value) && XFLOATINT (value) > 0)
5008 it->space_width = value;
5011 return 0;
5014 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5015 if (CONSP (spec)
5016 && EQ (XCAR (spec), Qslice))
5018 Lisp_Object tem;
5020 if (it)
5022 if (!FRAME_WINDOW_P (it->f))
5023 return 0;
5025 if (tem = XCDR (spec), CONSP (tem))
5027 it->slice.x = XCAR (tem);
5028 if (tem = XCDR (tem), CONSP (tem))
5030 it->slice.y = XCAR (tem);
5031 if (tem = XCDR (tem), CONSP (tem))
5033 it->slice.width = XCAR (tem);
5034 if (tem = XCDR (tem), CONSP (tem))
5035 it->slice.height = XCAR (tem);
5041 return 0;
5044 /* Handle `(raise FACTOR)'. */
5045 if (CONSP (spec)
5046 && EQ (XCAR (spec), Qraise)
5047 && CONSP (XCDR (spec)))
5049 if (it)
5051 if (!FRAME_WINDOW_P (it->f))
5052 return 0;
5054 #ifdef HAVE_WINDOW_SYSTEM
5055 value = XCAR (XCDR (spec));
5056 if (NUMBERP (value))
5058 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5059 it->voffset = - (XFLOATINT (value)
5060 * (FONT_HEIGHT (face->font)));
5062 #endif /* HAVE_WINDOW_SYSTEM */
5065 return 0;
5068 /* Don't handle the other kinds of display specifications
5069 inside a string that we got from a `display' property. */
5070 if (it && it->string_from_display_prop_p)
5071 return 0;
5073 /* Characters having this form of property are not displayed, so
5074 we have to find the end of the property. */
5075 if (it)
5077 start_pos = *position;
5078 *position = display_prop_end (it, object, start_pos);
5080 value = Qnil;
5082 /* Stop the scan at that end position--we assume that all
5083 text properties change there. */
5084 if (it)
5085 it->stop_charpos = position->charpos;
5087 /* Handle `(left-fringe BITMAP [FACE])'
5088 and `(right-fringe BITMAP [FACE])'. */
5089 if (CONSP (spec)
5090 && (EQ (XCAR (spec), Qleft_fringe)
5091 || EQ (XCAR (spec), Qright_fringe))
5092 && CONSP (XCDR (spec)))
5094 int fringe_bitmap;
5096 if (it)
5098 if (!FRAME_WINDOW_P (it->f))
5099 /* If we return here, POSITION has been advanced
5100 across the text with this property. */
5102 /* Synchronize the bidi iterator with POSITION. This is
5103 needed because we are not going to push the iterator
5104 on behalf of this display property, so there will be
5105 no pop_it call to do this synchronization for us. */
5106 if (it->bidi_p)
5108 it->position = *position;
5109 iterate_out_of_display_property (it);
5110 *position = it->position;
5112 return 1;
5115 else if (!frame_window_p)
5116 return 1;
5118 #ifdef HAVE_WINDOW_SYSTEM
5119 value = XCAR (XCDR (spec));
5120 if (!SYMBOLP (value)
5121 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5122 /* If we return here, POSITION has been advanced
5123 across the text with this property. */
5125 if (it && it->bidi_p)
5127 it->position = *position;
5128 iterate_out_of_display_property (it);
5129 *position = it->position;
5131 return 1;
5134 if (it)
5136 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5138 if (CONSP (XCDR (XCDR (spec))))
5140 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5141 int face_id2 = lookup_derived_face (it->f, face_name,
5142 FRINGE_FACE_ID, 0);
5143 if (face_id2 >= 0)
5144 face_id = face_id2;
5147 /* Save current settings of IT so that we can restore them
5148 when we are finished with the glyph property value. */
5149 push_it (it, position);
5151 it->area = TEXT_AREA;
5152 it->what = IT_IMAGE;
5153 it->image_id = -1; /* no image */
5154 it->position = start_pos;
5155 it->object = NILP (object) ? it->w->contents : object;
5156 it->method = GET_FROM_IMAGE;
5157 it->from_overlay = Qnil;
5158 it->face_id = face_id;
5159 it->from_disp_prop_p = true;
5161 /* Say that we haven't consumed the characters with
5162 `display' property yet. The call to pop_it in
5163 set_iterator_to_next will clean this up. */
5164 *position = start_pos;
5166 if (EQ (XCAR (spec), Qleft_fringe))
5168 it->left_user_fringe_bitmap = fringe_bitmap;
5169 it->left_user_fringe_face_id = face_id;
5171 else
5173 it->right_user_fringe_bitmap = fringe_bitmap;
5174 it->right_user_fringe_face_id = face_id;
5177 #endif /* HAVE_WINDOW_SYSTEM */
5178 return 1;
5181 /* Prepare to handle `((margin left-margin) ...)',
5182 `((margin right-margin) ...)' and `((margin nil) ...)'
5183 prefixes for display specifications. */
5184 location = Qunbound;
5185 if (CONSP (spec) && CONSP (XCAR (spec)))
5187 Lisp_Object tem;
5189 value = XCDR (spec);
5190 if (CONSP (value))
5191 value = XCAR (value);
5193 tem = XCAR (spec);
5194 if (EQ (XCAR (tem), Qmargin)
5195 && (tem = XCDR (tem),
5196 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5197 (NILP (tem)
5198 || EQ (tem, Qleft_margin)
5199 || EQ (tem, Qright_margin))))
5200 location = tem;
5203 if (EQ (location, Qunbound))
5205 location = Qnil;
5206 value = spec;
5209 /* After this point, VALUE is the property after any
5210 margin prefix has been stripped. It must be a string,
5211 an image specification, or `(space ...)'.
5213 LOCATION specifies where to display: `left-margin',
5214 `right-margin' or nil. */
5216 valid_p = (STRINGP (value)
5217 #ifdef HAVE_WINDOW_SYSTEM
5218 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5219 && valid_image_p (value))
5220 #endif /* not HAVE_WINDOW_SYSTEM */
5221 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5223 if (valid_p && !display_replaced_p)
5225 int retval = 1;
5227 if (!it)
5229 /* Callers need to know whether the display spec is any kind
5230 of `(space ...)' spec that is about to affect text-area
5231 display. */
5232 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5233 retval = 2;
5234 return retval;
5237 /* Save current settings of IT so that we can restore them
5238 when we are finished with the glyph property value. */
5239 push_it (it, position);
5240 it->from_overlay = overlay;
5241 it->from_disp_prop_p = true;
5243 if (NILP (location))
5244 it->area = TEXT_AREA;
5245 else if (EQ (location, Qleft_margin))
5246 it->area = LEFT_MARGIN_AREA;
5247 else
5248 it->area = RIGHT_MARGIN_AREA;
5250 if (STRINGP (value))
5252 it->string = value;
5253 it->multibyte_p = STRING_MULTIBYTE (it->string);
5254 it->current.overlay_string_index = -1;
5255 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5256 it->end_charpos = it->string_nchars = SCHARS (it->string);
5257 it->method = GET_FROM_STRING;
5258 it->stop_charpos = 0;
5259 it->prev_stop = 0;
5260 it->base_level_stop = 0;
5261 it->string_from_display_prop_p = true;
5262 /* Say that we haven't consumed the characters with
5263 `display' property yet. The call to pop_it in
5264 set_iterator_to_next will clean this up. */
5265 if (BUFFERP (object))
5266 *position = start_pos;
5268 /* Force paragraph direction to be that of the parent
5269 object. If the parent object's paragraph direction is
5270 not yet determined, default to L2R. */
5271 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5272 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5273 else
5274 it->paragraph_embedding = L2R;
5276 /* Set up the bidi iterator for this display string. */
5277 if (it->bidi_p)
5279 it->bidi_it.string.lstring = it->string;
5280 it->bidi_it.string.s = NULL;
5281 it->bidi_it.string.schars = it->end_charpos;
5282 it->bidi_it.string.bufpos = bufpos;
5283 it->bidi_it.string.from_disp_str = 1;
5284 it->bidi_it.string.unibyte = !it->multibyte_p;
5285 it->bidi_it.w = it->w;
5286 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5289 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5291 it->method = GET_FROM_STRETCH;
5292 it->object = value;
5293 *position = it->position = start_pos;
5294 retval = 1 + (it->area == TEXT_AREA);
5296 #ifdef HAVE_WINDOW_SYSTEM
5297 else
5299 it->what = IT_IMAGE;
5300 it->image_id = lookup_image (it->f, value);
5301 it->position = start_pos;
5302 it->object = NILP (object) ? it->w->contents : object;
5303 it->method = GET_FROM_IMAGE;
5305 /* Say that we haven't consumed the characters with
5306 `display' property yet. The call to pop_it in
5307 set_iterator_to_next will clean this up. */
5308 *position = start_pos;
5310 #endif /* HAVE_WINDOW_SYSTEM */
5312 return retval;
5315 /* Invalid property or property not supported. Restore
5316 POSITION to what it was before. */
5317 *position = start_pos;
5318 return 0;
5321 /* Check if PROP is a display property value whose text should be
5322 treated as intangible. OVERLAY is the overlay from which PROP
5323 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5324 specify the buffer position covered by PROP. */
5327 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5328 ptrdiff_t charpos, ptrdiff_t bytepos)
5330 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5331 struct text_pos position;
5333 SET_TEXT_POS (position, charpos, bytepos);
5334 return handle_display_spec (NULL, prop, Qnil, overlay,
5335 &position, charpos, frame_window_p);
5339 /* Return 1 if PROP is a display sub-property value containing STRING.
5341 Implementation note: this and the following function are really
5342 special cases of handle_display_spec and
5343 handle_single_display_spec, and should ideally use the same code.
5344 Until they do, these two pairs must be consistent and must be
5345 modified in sync. */
5347 static int
5348 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5350 if (EQ (string, prop))
5351 return 1;
5353 /* Skip over `when FORM'. */
5354 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5356 prop = XCDR (prop);
5357 if (!CONSP (prop))
5358 return 0;
5359 /* Actually, the condition following `when' should be eval'ed,
5360 like handle_single_display_spec does, and we should return
5361 zero if it evaluates to nil. However, this function is
5362 called only when the buffer was already displayed and some
5363 glyph in the glyph matrix was found to come from a display
5364 string. Therefore, the condition was already evaluated, and
5365 the result was non-nil, otherwise the display string wouldn't
5366 have been displayed and we would have never been called for
5367 this property. Thus, we can skip the evaluation and assume
5368 its result is non-nil. */
5369 prop = XCDR (prop);
5372 if (CONSP (prop))
5373 /* Skip over `margin LOCATION'. */
5374 if (EQ (XCAR (prop), Qmargin))
5376 prop = XCDR (prop);
5377 if (!CONSP (prop))
5378 return 0;
5380 prop = XCDR (prop);
5381 if (!CONSP (prop))
5382 return 0;
5385 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5389 /* Return 1 if STRING appears in the `display' property PROP. */
5391 static int
5392 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5394 if (CONSP (prop)
5395 && !EQ (XCAR (prop), Qwhen)
5396 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5398 /* A list of sub-properties. */
5399 while (CONSP (prop))
5401 if (single_display_spec_string_p (XCAR (prop), string))
5402 return 1;
5403 prop = XCDR (prop);
5406 else if (VECTORP (prop))
5408 /* A vector of sub-properties. */
5409 ptrdiff_t i;
5410 for (i = 0; i < ASIZE (prop); ++i)
5411 if (single_display_spec_string_p (AREF (prop, i), string))
5412 return 1;
5414 else
5415 return single_display_spec_string_p (prop, string);
5417 return 0;
5420 /* Look for STRING in overlays and text properties in the current
5421 buffer, between character positions FROM and TO (excluding TO).
5422 BACK_P non-zero means look back (in this case, TO is supposed to be
5423 less than FROM).
5424 Value is the first character position where STRING was found, or
5425 zero if it wasn't found before hitting TO.
5427 This function may only use code that doesn't eval because it is
5428 called asynchronously from note_mouse_highlight. */
5430 static ptrdiff_t
5431 string_buffer_position_lim (Lisp_Object string,
5432 ptrdiff_t from, ptrdiff_t to, int back_p)
5434 Lisp_Object limit, prop, pos;
5435 int found = 0;
5437 pos = make_number (max (from, BEGV));
5439 if (!back_p) /* looking forward */
5441 limit = make_number (min (to, ZV));
5442 while (!found && !EQ (pos, limit))
5444 prop = Fget_char_property (pos, Qdisplay, Qnil);
5445 if (!NILP (prop) && display_prop_string_p (prop, string))
5446 found = 1;
5447 else
5448 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5449 limit);
5452 else /* looking back */
5454 limit = make_number (max (to, BEGV));
5455 while (!found && !EQ (pos, limit))
5457 prop = Fget_char_property (pos, Qdisplay, Qnil);
5458 if (!NILP (prop) && display_prop_string_p (prop, string))
5459 found = 1;
5460 else
5461 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5462 limit);
5466 return found ? XINT (pos) : 0;
5469 /* Determine which buffer position in current buffer STRING comes from.
5470 AROUND_CHARPOS is an approximate position where it could come from.
5471 Value is the buffer position or 0 if it couldn't be determined.
5473 This function is necessary because we don't record buffer positions
5474 in glyphs generated from strings (to keep struct glyph small).
5475 This function may only use code that doesn't eval because it is
5476 called asynchronously from note_mouse_highlight. */
5478 static ptrdiff_t
5479 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5481 const int MAX_DISTANCE = 1000;
5482 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5483 around_charpos + MAX_DISTANCE,
5486 if (!found)
5487 found = string_buffer_position_lim (string, around_charpos,
5488 around_charpos - MAX_DISTANCE, 1);
5489 return found;
5494 /***********************************************************************
5495 `composition' property
5496 ***********************************************************************/
5498 /* Set up iterator IT from `composition' property at its current
5499 position. Called from handle_stop. */
5501 static enum prop_handled
5502 handle_composition_prop (struct it *it)
5504 Lisp_Object prop, string;
5505 ptrdiff_t pos, pos_byte, start, end;
5507 if (STRINGP (it->string))
5509 unsigned char *s;
5511 pos = IT_STRING_CHARPOS (*it);
5512 pos_byte = IT_STRING_BYTEPOS (*it);
5513 string = it->string;
5514 s = SDATA (string) + pos_byte;
5515 it->c = STRING_CHAR (s);
5517 else
5519 pos = IT_CHARPOS (*it);
5520 pos_byte = IT_BYTEPOS (*it);
5521 string = Qnil;
5522 it->c = FETCH_CHAR (pos_byte);
5525 /* If there's a valid composition and point is not inside of the
5526 composition (in the case that the composition is from the current
5527 buffer), draw a glyph composed from the composition components. */
5528 if (find_composition (pos, -1, &start, &end, &prop, string)
5529 && composition_valid_p (start, end, prop)
5530 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5532 if (start < pos)
5533 /* As we can't handle this situation (perhaps font-lock added
5534 a new composition), we just return here hoping that next
5535 redisplay will detect this composition much earlier. */
5536 return HANDLED_NORMALLY;
5537 if (start != pos)
5539 if (STRINGP (it->string))
5540 pos_byte = string_char_to_byte (it->string, start);
5541 else
5542 pos_byte = CHAR_TO_BYTE (start);
5544 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5545 prop, string);
5547 if (it->cmp_it.id >= 0)
5549 it->cmp_it.ch = -1;
5550 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5551 it->cmp_it.nglyphs = -1;
5555 return HANDLED_NORMALLY;
5560 /***********************************************************************
5561 Overlay strings
5562 ***********************************************************************/
5564 /* The following structure is used to record overlay strings for
5565 later sorting in load_overlay_strings. */
5567 struct overlay_entry
5569 Lisp_Object overlay;
5570 Lisp_Object string;
5571 EMACS_INT priority;
5572 int after_string_p;
5576 /* Set up iterator IT from overlay strings at its current position.
5577 Called from handle_stop. */
5579 static enum prop_handled
5580 handle_overlay_change (struct it *it)
5582 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5583 return HANDLED_RECOMPUTE_PROPS;
5584 else
5585 return HANDLED_NORMALLY;
5589 /* Set up the next overlay string for delivery by IT, if there is an
5590 overlay string to deliver. Called by set_iterator_to_next when the
5591 end of the current overlay string is reached. If there are more
5592 overlay strings to display, IT->string and
5593 IT->current.overlay_string_index are set appropriately here.
5594 Otherwise IT->string is set to nil. */
5596 static void
5597 next_overlay_string (struct it *it)
5599 ++it->current.overlay_string_index;
5600 if (it->current.overlay_string_index == it->n_overlay_strings)
5602 /* No more overlay strings. Restore IT's settings to what
5603 they were before overlay strings were processed, and
5604 continue to deliver from current_buffer. */
5606 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5607 pop_it (it);
5608 eassert (it->sp > 0
5609 || (NILP (it->string)
5610 && it->method == GET_FROM_BUFFER
5611 && it->stop_charpos >= BEGV
5612 && it->stop_charpos <= it->end_charpos));
5613 it->current.overlay_string_index = -1;
5614 it->n_overlay_strings = 0;
5615 it->overlay_strings_charpos = -1;
5616 /* If there's an empty display string on the stack, pop the
5617 stack, to resync the bidi iterator with IT's position. Such
5618 empty strings are pushed onto the stack in
5619 get_overlay_strings_1. */
5620 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5621 pop_it (it);
5623 /* If we're at the end of the buffer, record that we have
5624 processed the overlay strings there already, so that
5625 next_element_from_buffer doesn't try it again. */
5626 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5627 it->overlay_strings_at_end_processed_p = true;
5629 else
5631 /* There are more overlay strings to process. If
5632 IT->current.overlay_string_index has advanced to a position
5633 where we must load IT->overlay_strings with more strings, do
5634 it. We must load at the IT->overlay_strings_charpos where
5635 IT->n_overlay_strings was originally computed; when invisible
5636 text is present, this might not be IT_CHARPOS (Bug#7016). */
5637 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5639 if (it->current.overlay_string_index && i == 0)
5640 load_overlay_strings (it, it->overlay_strings_charpos);
5642 /* Initialize IT to deliver display elements from the overlay
5643 string. */
5644 it->string = it->overlay_strings[i];
5645 it->multibyte_p = STRING_MULTIBYTE (it->string);
5646 SET_TEXT_POS (it->current.string_pos, 0, 0);
5647 it->method = GET_FROM_STRING;
5648 it->stop_charpos = 0;
5649 it->end_charpos = SCHARS (it->string);
5650 if (it->cmp_it.stop_pos >= 0)
5651 it->cmp_it.stop_pos = 0;
5652 it->prev_stop = 0;
5653 it->base_level_stop = 0;
5655 /* Set up the bidi iterator for this overlay string. */
5656 if (it->bidi_p)
5658 it->bidi_it.string.lstring = it->string;
5659 it->bidi_it.string.s = NULL;
5660 it->bidi_it.string.schars = SCHARS (it->string);
5661 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5662 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5663 it->bidi_it.string.unibyte = !it->multibyte_p;
5664 it->bidi_it.w = it->w;
5665 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5669 CHECK_IT (it);
5673 /* Compare two overlay_entry structures E1 and E2. Used as a
5674 comparison function for qsort in load_overlay_strings. Overlay
5675 strings for the same position are sorted so that
5677 1. All after-strings come in front of before-strings, except
5678 when they come from the same overlay.
5680 2. Within after-strings, strings are sorted so that overlay strings
5681 from overlays with higher priorities come first.
5683 2. Within before-strings, strings are sorted so that overlay
5684 strings from overlays with higher priorities come last.
5686 Value is analogous to strcmp. */
5689 static int
5690 compare_overlay_entries (const void *e1, const void *e2)
5692 struct overlay_entry const *entry1 = e1;
5693 struct overlay_entry const *entry2 = e2;
5694 int result;
5696 if (entry1->after_string_p != entry2->after_string_p)
5698 /* Let after-strings appear in front of before-strings if
5699 they come from different overlays. */
5700 if (EQ (entry1->overlay, entry2->overlay))
5701 result = entry1->after_string_p ? 1 : -1;
5702 else
5703 result = entry1->after_string_p ? -1 : 1;
5705 else if (entry1->priority != entry2->priority)
5707 if (entry1->after_string_p)
5708 /* After-strings sorted in order of decreasing priority. */
5709 result = entry2->priority < entry1->priority ? -1 : 1;
5710 else
5711 /* Before-strings sorted in order of increasing priority. */
5712 result = entry1->priority < entry2->priority ? -1 : 1;
5714 else
5715 result = 0;
5717 return result;
5721 /* Load the vector IT->overlay_strings with overlay strings from IT's
5722 current buffer position, or from CHARPOS if that is > 0. Set
5723 IT->n_overlays to the total number of overlay strings found.
5725 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5726 a time. On entry into load_overlay_strings,
5727 IT->current.overlay_string_index gives the number of overlay
5728 strings that have already been loaded by previous calls to this
5729 function.
5731 IT->add_overlay_start contains an additional overlay start
5732 position to consider for taking overlay strings from, if non-zero.
5733 This position comes into play when the overlay has an `invisible'
5734 property, and both before and after-strings. When we've skipped to
5735 the end of the overlay, because of its `invisible' property, we
5736 nevertheless want its before-string to appear.
5737 IT->add_overlay_start will contain the overlay start position
5738 in this case.
5740 Overlay strings are sorted so that after-string strings come in
5741 front of before-string strings. Within before and after-strings,
5742 strings are sorted by overlay priority. See also function
5743 compare_overlay_entries. */
5745 static void
5746 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5748 Lisp_Object overlay, window, str, invisible;
5749 struct Lisp_Overlay *ov;
5750 ptrdiff_t start, end;
5751 ptrdiff_t n = 0, i, j;
5752 int invis_p;
5753 struct overlay_entry entriesbuf[20];
5754 ptrdiff_t size = ARRAYELTS (entriesbuf);
5755 struct overlay_entry *entries = entriesbuf;
5756 USE_SAFE_ALLOCA;
5758 if (charpos <= 0)
5759 charpos = IT_CHARPOS (*it);
5761 /* Append the overlay string STRING of overlay OVERLAY to vector
5762 `entries' which has size `size' and currently contains `n'
5763 elements. AFTER_P non-zero means STRING is an after-string of
5764 OVERLAY. */
5765 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5766 do \
5768 Lisp_Object priority; \
5770 if (n == size) \
5772 struct overlay_entry *old = entries; \
5773 SAFE_NALLOCA (entries, 2, size); \
5774 memcpy (entries, old, size * sizeof *entries); \
5775 size *= 2; \
5778 entries[n].string = (STRING); \
5779 entries[n].overlay = (OVERLAY); \
5780 priority = Foverlay_get ((OVERLAY), Qpriority); \
5781 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5782 entries[n].after_string_p = (AFTER_P); \
5783 ++n; \
5785 while (0)
5787 /* Process overlay before the overlay center. */
5788 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5790 XSETMISC (overlay, ov);
5791 eassert (OVERLAYP (overlay));
5792 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5793 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5795 if (end < charpos)
5796 break;
5798 /* Skip this overlay if it doesn't start or end at IT's current
5799 position. */
5800 if (end != charpos && start != charpos)
5801 continue;
5803 /* Skip this overlay if it doesn't apply to IT->w. */
5804 window = Foverlay_get (overlay, Qwindow);
5805 if (WINDOWP (window) && XWINDOW (window) != it->w)
5806 continue;
5808 /* If the text ``under'' the overlay is invisible, both before-
5809 and after-strings from this overlay are visible; start and
5810 end position are indistinguishable. */
5811 invisible = Foverlay_get (overlay, Qinvisible);
5812 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5814 /* If overlay has a non-empty before-string, record it. */
5815 if ((start == charpos || (end == charpos && invis_p))
5816 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5817 && SCHARS (str))
5818 RECORD_OVERLAY_STRING (overlay, str, 0);
5820 /* If overlay has a non-empty after-string, record it. */
5821 if ((end == charpos || (start == charpos && invis_p))
5822 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5823 && SCHARS (str))
5824 RECORD_OVERLAY_STRING (overlay, str, 1);
5827 /* Process overlays after the overlay center. */
5828 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5830 XSETMISC (overlay, ov);
5831 eassert (OVERLAYP (overlay));
5832 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5833 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5835 if (start > charpos)
5836 break;
5838 /* Skip this overlay if it doesn't start or end at IT's current
5839 position. */
5840 if (end != charpos && start != charpos)
5841 continue;
5843 /* Skip this overlay if it doesn't apply to IT->w. */
5844 window = Foverlay_get (overlay, Qwindow);
5845 if (WINDOWP (window) && XWINDOW (window) != it->w)
5846 continue;
5848 /* If the text ``under'' the overlay is invisible, it has a zero
5849 dimension, and both before- and after-strings apply. */
5850 invisible = Foverlay_get (overlay, Qinvisible);
5851 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5853 /* If overlay has a non-empty before-string, record it. */
5854 if ((start == charpos || (end == charpos && invis_p))
5855 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5856 && SCHARS (str))
5857 RECORD_OVERLAY_STRING (overlay, str, 0);
5859 /* If overlay has a non-empty after-string, record it. */
5860 if ((end == charpos || (start == charpos && invis_p))
5861 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5862 && SCHARS (str))
5863 RECORD_OVERLAY_STRING (overlay, str, 1);
5866 #undef RECORD_OVERLAY_STRING
5868 /* Sort entries. */
5869 if (n > 1)
5870 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5872 /* Record number of overlay strings, and where we computed it. */
5873 it->n_overlay_strings = n;
5874 it->overlay_strings_charpos = charpos;
5876 /* IT->current.overlay_string_index is the number of overlay strings
5877 that have already been consumed by IT. Copy some of the
5878 remaining overlay strings to IT->overlay_strings. */
5879 i = 0;
5880 j = it->current.overlay_string_index;
5881 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5883 it->overlay_strings[i] = entries[j].string;
5884 it->string_overlays[i++] = entries[j++].overlay;
5887 CHECK_IT (it);
5888 SAFE_FREE ();
5892 /* Get the first chunk of overlay strings at IT's current buffer
5893 position, or at CHARPOS if that is > 0. Value is non-zero if at
5894 least one overlay string was found. */
5896 static int
5897 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5899 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5900 process. This fills IT->overlay_strings with strings, and sets
5901 IT->n_overlay_strings to the total number of strings to process.
5902 IT->pos.overlay_string_index has to be set temporarily to zero
5903 because load_overlay_strings needs this; it must be set to -1
5904 when no overlay strings are found because a zero value would
5905 indicate a position in the first overlay string. */
5906 it->current.overlay_string_index = 0;
5907 load_overlay_strings (it, charpos);
5909 /* If we found overlay strings, set up IT to deliver display
5910 elements from the first one. Otherwise set up IT to deliver
5911 from current_buffer. */
5912 if (it->n_overlay_strings)
5914 /* Make sure we know settings in current_buffer, so that we can
5915 restore meaningful values when we're done with the overlay
5916 strings. */
5917 if (compute_stop_p)
5918 compute_stop_pos (it);
5919 eassert (it->face_id >= 0);
5921 /* Save IT's settings. They are restored after all overlay
5922 strings have been processed. */
5923 eassert (!compute_stop_p || it->sp == 0);
5925 /* When called from handle_stop, there might be an empty display
5926 string loaded. In that case, don't bother saving it. But
5927 don't use this optimization with the bidi iterator, since we
5928 need the corresponding pop_it call to resync the bidi
5929 iterator's position with IT's position, after we are done
5930 with the overlay strings. (The corresponding call to pop_it
5931 in case of an empty display string is in
5932 next_overlay_string.) */
5933 if (!(!it->bidi_p
5934 && STRINGP (it->string) && !SCHARS (it->string)))
5935 push_it (it, NULL);
5937 /* Set up IT to deliver display elements from the first overlay
5938 string. */
5939 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5940 it->string = it->overlay_strings[0];
5941 it->from_overlay = Qnil;
5942 it->stop_charpos = 0;
5943 eassert (STRINGP (it->string));
5944 it->end_charpos = SCHARS (it->string);
5945 it->prev_stop = 0;
5946 it->base_level_stop = 0;
5947 it->multibyte_p = STRING_MULTIBYTE (it->string);
5948 it->method = GET_FROM_STRING;
5949 it->from_disp_prop_p = 0;
5951 /* Force paragraph direction to be that of the parent
5952 buffer. */
5953 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5954 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5955 else
5956 it->paragraph_embedding = L2R;
5958 /* Set up the bidi iterator for this overlay string. */
5959 if (it->bidi_p)
5961 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5963 it->bidi_it.string.lstring = it->string;
5964 it->bidi_it.string.s = NULL;
5965 it->bidi_it.string.schars = SCHARS (it->string);
5966 it->bidi_it.string.bufpos = pos;
5967 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5968 it->bidi_it.string.unibyte = !it->multibyte_p;
5969 it->bidi_it.w = it->w;
5970 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5972 return 1;
5975 it->current.overlay_string_index = -1;
5976 return 0;
5979 static int
5980 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5982 it->string = Qnil;
5983 it->method = GET_FROM_BUFFER;
5985 (void) get_overlay_strings_1 (it, charpos, 1);
5987 CHECK_IT (it);
5989 /* Value is non-zero if we found at least one overlay string. */
5990 return STRINGP (it->string);
5995 /***********************************************************************
5996 Saving and restoring state
5997 ***********************************************************************/
5999 /* Save current settings of IT on IT->stack. Called, for example,
6000 before setting up IT for an overlay string, to be able to restore
6001 IT's settings to what they were after the overlay string has been
6002 processed. If POSITION is non-NULL, it is the position to save on
6003 the stack instead of IT->position. */
6005 static void
6006 push_it (struct it *it, struct text_pos *position)
6008 struct iterator_stack_entry *p;
6010 eassert (it->sp < IT_STACK_SIZE);
6011 p = it->stack + it->sp;
6013 p->stop_charpos = it->stop_charpos;
6014 p->prev_stop = it->prev_stop;
6015 p->base_level_stop = it->base_level_stop;
6016 p->cmp_it = it->cmp_it;
6017 eassert (it->face_id >= 0);
6018 p->face_id = it->face_id;
6019 p->string = it->string;
6020 p->method = it->method;
6021 p->from_overlay = it->from_overlay;
6022 switch (p->method)
6024 case GET_FROM_IMAGE:
6025 p->u.image.object = it->object;
6026 p->u.image.image_id = it->image_id;
6027 p->u.image.slice = it->slice;
6028 break;
6029 case GET_FROM_STRETCH:
6030 p->u.stretch.object = it->object;
6031 break;
6033 p->position = position ? *position : it->position;
6034 p->current = it->current;
6035 p->end_charpos = it->end_charpos;
6036 p->string_nchars = it->string_nchars;
6037 p->area = it->area;
6038 p->multibyte_p = it->multibyte_p;
6039 p->avoid_cursor_p = it->avoid_cursor_p;
6040 p->space_width = it->space_width;
6041 p->font_height = it->font_height;
6042 p->voffset = it->voffset;
6043 p->string_from_display_prop_p = it->string_from_display_prop_p;
6044 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6045 p->display_ellipsis_p = 0;
6046 p->line_wrap = it->line_wrap;
6047 p->bidi_p = it->bidi_p;
6048 p->paragraph_embedding = it->paragraph_embedding;
6049 p->from_disp_prop_p = it->from_disp_prop_p;
6050 ++it->sp;
6052 /* Save the state of the bidi iterator as well. */
6053 if (it->bidi_p)
6054 bidi_push_it (&it->bidi_it);
6057 static void
6058 iterate_out_of_display_property (struct it *it)
6060 int buffer_p = !STRINGP (it->string);
6061 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6062 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6064 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6066 /* Maybe initialize paragraph direction. If we are at the beginning
6067 of a new paragraph, next_element_from_buffer may not have a
6068 chance to do that. */
6069 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6070 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6071 /* prev_stop can be zero, so check against BEGV as well. */
6072 while (it->bidi_it.charpos >= bob
6073 && it->prev_stop <= it->bidi_it.charpos
6074 && it->bidi_it.charpos < CHARPOS (it->position)
6075 && it->bidi_it.charpos < eob)
6076 bidi_move_to_visually_next (&it->bidi_it);
6077 /* Record the stop_pos we just crossed, for when we cross it
6078 back, maybe. */
6079 if (it->bidi_it.charpos > CHARPOS (it->position))
6080 it->prev_stop = CHARPOS (it->position);
6081 /* If we ended up not where pop_it put us, resync IT's
6082 positional members with the bidi iterator. */
6083 if (it->bidi_it.charpos != CHARPOS (it->position))
6084 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6085 if (buffer_p)
6086 it->current.pos = it->position;
6087 else
6088 it->current.string_pos = it->position;
6091 /* Restore IT's settings from IT->stack. Called, for example, when no
6092 more overlay strings must be processed, and we return to delivering
6093 display elements from a buffer, or when the end of a string from a
6094 `display' property is reached and we return to delivering display
6095 elements from an overlay string, or from a buffer. */
6097 static void
6098 pop_it (struct it *it)
6100 struct iterator_stack_entry *p;
6101 int from_display_prop = it->from_disp_prop_p;
6103 eassert (it->sp > 0);
6104 --it->sp;
6105 p = it->stack + it->sp;
6106 it->stop_charpos = p->stop_charpos;
6107 it->prev_stop = p->prev_stop;
6108 it->base_level_stop = p->base_level_stop;
6109 it->cmp_it = p->cmp_it;
6110 it->face_id = p->face_id;
6111 it->current = p->current;
6112 it->position = p->position;
6113 it->string = p->string;
6114 it->from_overlay = p->from_overlay;
6115 if (NILP (it->string))
6116 SET_TEXT_POS (it->current.string_pos, -1, -1);
6117 it->method = p->method;
6118 switch (it->method)
6120 case GET_FROM_IMAGE:
6121 it->image_id = p->u.image.image_id;
6122 it->object = p->u.image.object;
6123 it->slice = p->u.image.slice;
6124 break;
6125 case GET_FROM_STRETCH:
6126 it->object = p->u.stretch.object;
6127 break;
6128 case GET_FROM_BUFFER:
6129 it->object = it->w->contents;
6130 break;
6131 case GET_FROM_STRING:
6133 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6135 /* Restore the face_box_p flag, since it could have been
6136 overwritten by the face of the object that we just finished
6137 displaying. */
6138 if (face)
6139 it->face_box_p = face->box != FACE_NO_BOX;
6140 it->object = it->string;
6142 break;
6143 case GET_FROM_DISPLAY_VECTOR:
6144 if (it->s)
6145 it->method = GET_FROM_C_STRING;
6146 else if (STRINGP (it->string))
6147 it->method = GET_FROM_STRING;
6148 else
6150 it->method = GET_FROM_BUFFER;
6151 it->object = it->w->contents;
6154 it->end_charpos = p->end_charpos;
6155 it->string_nchars = p->string_nchars;
6156 it->area = p->area;
6157 it->multibyte_p = p->multibyte_p;
6158 it->avoid_cursor_p = p->avoid_cursor_p;
6159 it->space_width = p->space_width;
6160 it->font_height = p->font_height;
6161 it->voffset = p->voffset;
6162 it->string_from_display_prop_p = p->string_from_display_prop_p;
6163 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6164 it->line_wrap = p->line_wrap;
6165 it->bidi_p = p->bidi_p;
6166 it->paragraph_embedding = p->paragraph_embedding;
6167 it->from_disp_prop_p = p->from_disp_prop_p;
6168 if (it->bidi_p)
6170 bidi_pop_it (&it->bidi_it);
6171 /* Bidi-iterate until we get out of the portion of text, if any,
6172 covered by a `display' text property or by an overlay with
6173 `display' property. (We cannot just jump there, because the
6174 internal coherency of the bidi iterator state can not be
6175 preserved across such jumps.) We also must determine the
6176 paragraph base direction if the overlay we just processed is
6177 at the beginning of a new paragraph. */
6178 if (from_display_prop
6179 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6180 iterate_out_of_display_property (it);
6182 eassert ((BUFFERP (it->object)
6183 && IT_CHARPOS (*it) == it->bidi_it.charpos
6184 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6185 || (STRINGP (it->object)
6186 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6187 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6188 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6194 /***********************************************************************
6195 Moving over lines
6196 ***********************************************************************/
6198 /* Set IT's current position to the previous line start. */
6200 static void
6201 back_to_previous_line_start (struct it *it)
6203 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6205 DEC_BOTH (cp, bp);
6206 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6210 /* Move IT to the next line start.
6212 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6213 we skipped over part of the text (as opposed to moving the iterator
6214 continuously over the text). Otherwise, don't change the value
6215 of *SKIPPED_P.
6217 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6218 iterator on the newline, if it was found.
6220 Newlines may come from buffer text, overlay strings, or strings
6221 displayed via the `display' property. That's the reason we can't
6222 simply use find_newline_no_quit.
6224 Note that this function may not skip over invisible text that is so
6225 because of text properties and immediately follows a newline. If
6226 it would, function reseat_at_next_visible_line_start, when called
6227 from set_iterator_to_next, would effectively make invisible
6228 characters following a newline part of the wrong glyph row, which
6229 leads to wrong cursor motion. */
6231 static int
6232 forward_to_next_line_start (struct it *it, int *skipped_p,
6233 struct bidi_it *bidi_it_prev)
6235 ptrdiff_t old_selective;
6236 int newline_found_p, n;
6237 const int MAX_NEWLINE_DISTANCE = 500;
6239 /* If already on a newline, just consume it to avoid unintended
6240 skipping over invisible text below. */
6241 if (it->what == IT_CHARACTER
6242 && it->c == '\n'
6243 && CHARPOS (it->position) == IT_CHARPOS (*it))
6245 if (it->bidi_p && bidi_it_prev)
6246 *bidi_it_prev = it->bidi_it;
6247 set_iterator_to_next (it, 0);
6248 it->c = 0;
6249 return 1;
6252 /* Don't handle selective display in the following. It's (a)
6253 unnecessary because it's done by the caller, and (b) leads to an
6254 infinite recursion because next_element_from_ellipsis indirectly
6255 calls this function. */
6256 old_selective = it->selective;
6257 it->selective = 0;
6259 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6260 from buffer text. */
6261 for (n = newline_found_p = 0;
6262 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6263 n += STRINGP (it->string) ? 0 : 1)
6265 if (!get_next_display_element (it))
6266 return 0;
6267 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6268 if (newline_found_p && it->bidi_p && bidi_it_prev)
6269 *bidi_it_prev = it->bidi_it;
6270 set_iterator_to_next (it, 0);
6273 /* If we didn't find a newline near enough, see if we can use a
6274 short-cut. */
6275 if (!newline_found_p)
6277 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6278 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6279 1, &bytepos);
6280 Lisp_Object pos;
6282 eassert (!STRINGP (it->string));
6284 /* If there isn't any `display' property in sight, and no
6285 overlays, we can just use the position of the newline in
6286 buffer text. */
6287 if (it->stop_charpos >= limit
6288 || ((pos = Fnext_single_property_change (make_number (start),
6289 Qdisplay, Qnil,
6290 make_number (limit)),
6291 NILP (pos))
6292 && next_overlay_change (start) == ZV))
6294 if (!it->bidi_p)
6296 IT_CHARPOS (*it) = limit;
6297 IT_BYTEPOS (*it) = bytepos;
6299 else
6301 struct bidi_it bprev;
6303 /* Help bidi.c avoid expensive searches for display
6304 properties and overlays, by telling it that there are
6305 none up to `limit'. */
6306 if (it->bidi_it.disp_pos < limit)
6308 it->bidi_it.disp_pos = limit;
6309 it->bidi_it.disp_prop = 0;
6311 do {
6312 bprev = it->bidi_it;
6313 bidi_move_to_visually_next (&it->bidi_it);
6314 } while (it->bidi_it.charpos != limit);
6315 IT_CHARPOS (*it) = limit;
6316 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6317 if (bidi_it_prev)
6318 *bidi_it_prev = bprev;
6320 *skipped_p = newline_found_p = true;
6322 else
6324 while (get_next_display_element (it)
6325 && !newline_found_p)
6327 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6328 if (newline_found_p && it->bidi_p && bidi_it_prev)
6329 *bidi_it_prev = it->bidi_it;
6330 set_iterator_to_next (it, 0);
6335 it->selective = old_selective;
6336 return newline_found_p;
6340 /* Set IT's current position to the previous visible line start. Skip
6341 invisible text that is so either due to text properties or due to
6342 selective display. Caution: this does not change IT->current_x and
6343 IT->hpos. */
6345 static void
6346 back_to_previous_visible_line_start (struct it *it)
6348 while (IT_CHARPOS (*it) > BEGV)
6350 back_to_previous_line_start (it);
6352 if (IT_CHARPOS (*it) <= BEGV)
6353 break;
6355 /* If selective > 0, then lines indented more than its value are
6356 invisible. */
6357 if (it->selective > 0
6358 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6359 it->selective))
6360 continue;
6362 /* Check the newline before point for invisibility. */
6364 Lisp_Object prop;
6365 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6366 Qinvisible, it->window);
6367 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6368 continue;
6371 if (IT_CHARPOS (*it) <= BEGV)
6372 break;
6375 struct it it2;
6376 void *it2data = NULL;
6377 ptrdiff_t pos;
6378 ptrdiff_t beg, end;
6379 Lisp_Object val, overlay;
6381 SAVE_IT (it2, *it, it2data);
6383 /* If newline is part of a composition, continue from start of composition */
6384 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6385 && beg < IT_CHARPOS (*it))
6386 goto replaced;
6388 /* If newline is replaced by a display property, find start of overlay
6389 or interval and continue search from that point. */
6390 pos = --IT_CHARPOS (it2);
6391 --IT_BYTEPOS (it2);
6392 it2.sp = 0;
6393 bidi_unshelve_cache (NULL, 0);
6394 it2.string_from_display_prop_p = 0;
6395 it2.from_disp_prop_p = 0;
6396 if (handle_display_prop (&it2) == HANDLED_RETURN
6397 && !NILP (val = get_char_property_and_overlay
6398 (make_number (pos), Qdisplay, Qnil, &overlay))
6399 && (OVERLAYP (overlay)
6400 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6401 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6403 RESTORE_IT (it, it, it2data);
6404 goto replaced;
6407 /* Newline is not replaced by anything -- so we are done. */
6408 RESTORE_IT (it, it, it2data);
6409 break;
6411 replaced:
6412 if (beg < BEGV)
6413 beg = BEGV;
6414 IT_CHARPOS (*it) = beg;
6415 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6419 it->continuation_lines_width = 0;
6421 eassert (IT_CHARPOS (*it) >= BEGV);
6422 eassert (IT_CHARPOS (*it) == BEGV
6423 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6424 CHECK_IT (it);
6428 /* Reseat iterator IT at the previous visible line start. Skip
6429 invisible text that is so either due to text properties or due to
6430 selective display. At the end, update IT's overlay information,
6431 face information etc. */
6433 void
6434 reseat_at_previous_visible_line_start (struct it *it)
6436 back_to_previous_visible_line_start (it);
6437 reseat (it, it->current.pos, 1);
6438 CHECK_IT (it);
6442 /* Reseat iterator IT on the next visible line start in the current
6443 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6444 preceding the line start. Skip over invisible text that is so
6445 because of selective display. Compute faces, overlays etc at the
6446 new position. Note that this function does not skip over text that
6447 is invisible because of text properties. */
6449 static void
6450 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6452 int newline_found_p, skipped_p = 0;
6453 struct bidi_it bidi_it_prev;
6455 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6457 /* Skip over lines that are invisible because they are indented
6458 more than the value of IT->selective. */
6459 if (it->selective > 0)
6460 while (IT_CHARPOS (*it) < ZV
6461 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6462 it->selective))
6464 eassert (IT_BYTEPOS (*it) == BEGV
6465 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6466 newline_found_p =
6467 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6470 /* Position on the newline if that's what's requested. */
6471 if (on_newline_p && newline_found_p)
6473 if (STRINGP (it->string))
6475 if (IT_STRING_CHARPOS (*it) > 0)
6477 if (!it->bidi_p)
6479 --IT_STRING_CHARPOS (*it);
6480 --IT_STRING_BYTEPOS (*it);
6482 else
6484 /* We need to restore the bidi iterator to the state
6485 it had on the newline, and resync the IT's
6486 position with that. */
6487 it->bidi_it = bidi_it_prev;
6488 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6489 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6493 else if (IT_CHARPOS (*it) > BEGV)
6495 if (!it->bidi_p)
6497 --IT_CHARPOS (*it);
6498 --IT_BYTEPOS (*it);
6500 else
6502 /* We need to restore the bidi iterator to the state it
6503 had on the newline and resync IT with that. */
6504 it->bidi_it = bidi_it_prev;
6505 IT_CHARPOS (*it) = it->bidi_it.charpos;
6506 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6508 reseat (it, it->current.pos, 0);
6511 else if (skipped_p)
6512 reseat (it, it->current.pos, 0);
6514 CHECK_IT (it);
6519 /***********************************************************************
6520 Changing an iterator's position
6521 ***********************************************************************/
6523 /* Change IT's current position to POS in current_buffer. If FORCE_P
6524 is non-zero, always check for text properties at the new position.
6525 Otherwise, text properties are only looked up if POS >=
6526 IT->check_charpos of a property. */
6528 static void
6529 reseat (struct it *it, struct text_pos pos, int force_p)
6531 ptrdiff_t original_pos = IT_CHARPOS (*it);
6533 reseat_1 (it, pos, 0);
6535 /* Determine where to check text properties. Avoid doing it
6536 where possible because text property lookup is very expensive. */
6537 if (force_p
6538 || CHARPOS (pos) > it->stop_charpos
6539 || CHARPOS (pos) < original_pos)
6541 if (it->bidi_p)
6543 /* For bidi iteration, we need to prime prev_stop and
6544 base_level_stop with our best estimations. */
6545 /* Implementation note: Of course, POS is not necessarily a
6546 stop position, so assigning prev_pos to it is a lie; we
6547 should have called compute_stop_backwards. However, if
6548 the current buffer does not include any R2L characters,
6549 that call would be a waste of cycles, because the
6550 iterator will never move back, and thus never cross this
6551 "fake" stop position. So we delay that backward search
6552 until the time we really need it, in next_element_from_buffer. */
6553 if (CHARPOS (pos) != it->prev_stop)
6554 it->prev_stop = CHARPOS (pos);
6555 if (CHARPOS (pos) < it->base_level_stop)
6556 it->base_level_stop = 0; /* meaning it's unknown */
6557 handle_stop (it);
6559 else
6561 handle_stop (it);
6562 it->prev_stop = it->base_level_stop = 0;
6567 CHECK_IT (it);
6571 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6572 IT->stop_pos to POS, also. */
6574 static void
6575 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6577 /* Don't call this function when scanning a C string. */
6578 eassert (it->s == NULL);
6580 /* POS must be a reasonable value. */
6581 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6583 it->current.pos = it->position = pos;
6584 it->end_charpos = ZV;
6585 it->dpvec = NULL;
6586 it->current.dpvec_index = -1;
6587 it->current.overlay_string_index = -1;
6588 IT_STRING_CHARPOS (*it) = -1;
6589 IT_STRING_BYTEPOS (*it) = -1;
6590 it->string = Qnil;
6591 it->method = GET_FROM_BUFFER;
6592 it->object = it->w->contents;
6593 it->area = TEXT_AREA;
6594 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6595 it->sp = 0;
6596 it->string_from_display_prop_p = 0;
6597 it->string_from_prefix_prop_p = 0;
6599 it->from_disp_prop_p = 0;
6600 it->face_before_selective_p = 0;
6601 if (it->bidi_p)
6603 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6604 &it->bidi_it);
6605 bidi_unshelve_cache (NULL, 0);
6606 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6607 it->bidi_it.string.s = NULL;
6608 it->bidi_it.string.lstring = Qnil;
6609 it->bidi_it.string.bufpos = 0;
6610 it->bidi_it.string.from_disp_str = 0;
6611 it->bidi_it.string.unibyte = 0;
6612 it->bidi_it.w = it->w;
6615 if (set_stop_p)
6617 it->stop_charpos = CHARPOS (pos);
6618 it->base_level_stop = CHARPOS (pos);
6620 /* This make the information stored in it->cmp_it invalidate. */
6621 it->cmp_it.id = -1;
6625 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6626 If S is non-null, it is a C string to iterate over. Otherwise,
6627 STRING gives a Lisp string to iterate over.
6629 If PRECISION > 0, don't return more then PRECISION number of
6630 characters from the string.
6632 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6633 characters have been returned. FIELD_WIDTH < 0 means an infinite
6634 field width.
6636 MULTIBYTE = 0 means disable processing of multibyte characters,
6637 MULTIBYTE > 0 means enable it,
6638 MULTIBYTE < 0 means use IT->multibyte_p.
6640 IT must be initialized via a prior call to init_iterator before
6641 calling this function. */
6643 static void
6644 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6645 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6646 int multibyte)
6648 /* No text property checks performed by default, but see below. */
6649 it->stop_charpos = -1;
6651 /* Set iterator position and end position. */
6652 memset (&it->current, 0, sizeof it->current);
6653 it->current.overlay_string_index = -1;
6654 it->current.dpvec_index = -1;
6655 eassert (charpos >= 0);
6657 /* If STRING is specified, use its multibyteness, otherwise use the
6658 setting of MULTIBYTE, if specified. */
6659 if (multibyte >= 0)
6660 it->multibyte_p = multibyte > 0;
6662 /* Bidirectional reordering of strings is controlled by the default
6663 value of bidi-display-reordering. Don't try to reorder while
6664 loading loadup.el, as the necessary character property tables are
6665 not yet available. */
6666 it->bidi_p =
6667 NILP (Vpurify_flag)
6668 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6670 if (s == NULL)
6672 eassert (STRINGP (string));
6673 it->string = string;
6674 it->s = NULL;
6675 it->end_charpos = it->string_nchars = SCHARS (string);
6676 it->method = GET_FROM_STRING;
6677 it->current.string_pos = string_pos (charpos, string);
6679 if (it->bidi_p)
6681 it->bidi_it.string.lstring = string;
6682 it->bidi_it.string.s = NULL;
6683 it->bidi_it.string.schars = it->end_charpos;
6684 it->bidi_it.string.bufpos = 0;
6685 it->bidi_it.string.from_disp_str = 0;
6686 it->bidi_it.string.unibyte = !it->multibyte_p;
6687 it->bidi_it.w = it->w;
6688 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6689 FRAME_WINDOW_P (it->f), &it->bidi_it);
6692 else
6694 it->s = (const unsigned char *) s;
6695 it->string = Qnil;
6697 /* Note that we use IT->current.pos, not it->current.string_pos,
6698 for displaying C strings. */
6699 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6700 if (it->multibyte_p)
6702 it->current.pos = c_string_pos (charpos, s, 1);
6703 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6705 else
6707 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6708 it->end_charpos = it->string_nchars = strlen (s);
6711 if (it->bidi_p)
6713 it->bidi_it.string.lstring = Qnil;
6714 it->bidi_it.string.s = (const unsigned char *) s;
6715 it->bidi_it.string.schars = it->end_charpos;
6716 it->bidi_it.string.bufpos = 0;
6717 it->bidi_it.string.from_disp_str = 0;
6718 it->bidi_it.string.unibyte = !it->multibyte_p;
6719 it->bidi_it.w = it->w;
6720 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6721 &it->bidi_it);
6723 it->method = GET_FROM_C_STRING;
6726 /* PRECISION > 0 means don't return more than PRECISION characters
6727 from the string. */
6728 if (precision > 0 && it->end_charpos - charpos > precision)
6730 it->end_charpos = it->string_nchars = charpos + precision;
6731 if (it->bidi_p)
6732 it->bidi_it.string.schars = it->end_charpos;
6735 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6736 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6737 FIELD_WIDTH < 0 means infinite field width. This is useful for
6738 padding with `-' at the end of a mode line. */
6739 if (field_width < 0)
6740 field_width = INFINITY;
6741 /* Implementation note: We deliberately don't enlarge
6742 it->bidi_it.string.schars here to fit it->end_charpos, because
6743 the bidi iterator cannot produce characters out of thin air. */
6744 if (field_width > it->end_charpos - charpos)
6745 it->end_charpos = charpos + field_width;
6747 /* Use the standard display table for displaying strings. */
6748 if (DISP_TABLE_P (Vstandard_display_table))
6749 it->dp = XCHAR_TABLE (Vstandard_display_table);
6751 it->stop_charpos = charpos;
6752 it->prev_stop = charpos;
6753 it->base_level_stop = 0;
6754 if (it->bidi_p)
6756 it->bidi_it.first_elt = 1;
6757 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6758 it->bidi_it.disp_pos = -1;
6760 if (s == NULL && it->multibyte_p)
6762 ptrdiff_t endpos = SCHARS (it->string);
6763 if (endpos > it->end_charpos)
6764 endpos = it->end_charpos;
6765 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6766 it->string);
6768 CHECK_IT (it);
6773 /***********************************************************************
6774 Iteration
6775 ***********************************************************************/
6777 /* Map enum it_method value to corresponding next_element_from_* function. */
6779 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6781 next_element_from_buffer,
6782 next_element_from_display_vector,
6783 next_element_from_string,
6784 next_element_from_c_string,
6785 next_element_from_image,
6786 next_element_from_stretch
6789 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6792 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6793 (possibly with the following characters). */
6795 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6796 ((IT)->cmp_it.id >= 0 \
6797 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6798 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6799 END_CHARPOS, (IT)->w, \
6800 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6801 (IT)->string)))
6804 /* Lookup the char-table Vglyphless_char_display for character C (-1
6805 if we want information for no-font case), and return the display
6806 method symbol. By side-effect, update it->what and
6807 it->glyphless_method. This function is called from
6808 get_next_display_element for each character element, and from
6809 x_produce_glyphs when no suitable font was found. */
6811 Lisp_Object
6812 lookup_glyphless_char_display (int c, struct it *it)
6814 Lisp_Object glyphless_method = Qnil;
6816 if (CHAR_TABLE_P (Vglyphless_char_display)
6817 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6819 if (c >= 0)
6821 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6822 if (CONSP (glyphless_method))
6823 glyphless_method = FRAME_WINDOW_P (it->f)
6824 ? XCAR (glyphless_method)
6825 : XCDR (glyphless_method);
6827 else
6828 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6831 retry:
6832 if (NILP (glyphless_method))
6834 if (c >= 0)
6835 /* The default is to display the character by a proper font. */
6836 return Qnil;
6837 /* The default for the no-font case is to display an empty box. */
6838 glyphless_method = Qempty_box;
6840 if (EQ (glyphless_method, Qzero_width))
6842 if (c >= 0)
6843 return glyphless_method;
6844 /* This method can't be used for the no-font case. */
6845 glyphless_method = Qempty_box;
6847 if (EQ (glyphless_method, Qthin_space))
6848 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6849 else if (EQ (glyphless_method, Qempty_box))
6850 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6851 else if (EQ (glyphless_method, Qhex_code))
6852 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6853 else if (STRINGP (glyphless_method))
6854 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6855 else
6857 /* Invalid value. We use the default method. */
6858 glyphless_method = Qnil;
6859 goto retry;
6861 it->what = IT_GLYPHLESS;
6862 return glyphless_method;
6865 /* Merge escape glyph face and cache the result. */
6867 static struct frame *last_escape_glyph_frame = NULL;
6868 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6869 static int last_escape_glyph_merged_face_id = 0;
6871 static int
6872 merge_escape_glyph_face (struct it *it)
6874 int face_id;
6876 if (it->f == last_escape_glyph_frame
6877 && it->face_id == last_escape_glyph_face_id)
6878 face_id = last_escape_glyph_merged_face_id;
6879 else
6881 /* Merge the `escape-glyph' face into the current face. */
6882 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6883 last_escape_glyph_frame = it->f;
6884 last_escape_glyph_face_id = it->face_id;
6885 last_escape_glyph_merged_face_id = face_id;
6887 return face_id;
6890 /* Likewise for glyphless glyph face. */
6892 static struct frame *last_glyphless_glyph_frame = NULL;
6893 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6894 static int last_glyphless_glyph_merged_face_id = 0;
6897 merge_glyphless_glyph_face (struct it *it)
6899 int face_id;
6901 if (it->f == last_glyphless_glyph_frame
6902 && it->face_id == last_glyphless_glyph_face_id)
6903 face_id = last_glyphless_glyph_merged_face_id;
6904 else
6906 /* Merge the `glyphless-char' face into the current face. */
6907 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6908 last_glyphless_glyph_frame = it->f;
6909 last_glyphless_glyph_face_id = it->face_id;
6910 last_glyphless_glyph_merged_face_id = face_id;
6912 return face_id;
6915 /* Load IT's display element fields with information about the next
6916 display element from the current position of IT. Value is zero if
6917 end of buffer (or C string) is reached. */
6919 static int
6920 get_next_display_element (struct it *it)
6922 /* Non-zero means that we found a display element. Zero means that
6923 we hit the end of what we iterate over. Performance note: the
6924 function pointer `method' used here turns out to be faster than
6925 using a sequence of if-statements. */
6926 int success_p;
6928 get_next:
6929 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6931 if (it->what == IT_CHARACTER)
6933 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6934 and only if (a) the resolved directionality of that character
6935 is R..." */
6936 /* FIXME: Do we need an exception for characters from display
6937 tables? */
6938 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6939 it->c = bidi_mirror_char (it->c);
6940 /* Map via display table or translate control characters.
6941 IT->c, IT->len etc. have been set to the next character by
6942 the function call above. If we have a display table, and it
6943 contains an entry for IT->c, translate it. Don't do this if
6944 IT->c itself comes from a display table, otherwise we could
6945 end up in an infinite recursion. (An alternative could be to
6946 count the recursion depth of this function and signal an
6947 error when a certain maximum depth is reached.) Is it worth
6948 it? */
6949 if (success_p && it->dpvec == NULL)
6951 Lisp_Object dv;
6952 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6953 int nonascii_space_p = 0;
6954 int nonascii_hyphen_p = 0;
6955 int c = it->c; /* This is the character to display. */
6957 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6959 eassert (SINGLE_BYTE_CHAR_P (c));
6960 if (unibyte_display_via_language_environment)
6962 c = DECODE_CHAR (unibyte, c);
6963 if (c < 0)
6964 c = BYTE8_TO_CHAR (it->c);
6966 else
6967 c = BYTE8_TO_CHAR (it->c);
6970 if (it->dp
6971 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6972 VECTORP (dv)))
6974 struct Lisp_Vector *v = XVECTOR (dv);
6976 /* Return the first character from the display table
6977 entry, if not empty. If empty, don't display the
6978 current character. */
6979 if (v->header.size)
6981 it->dpvec_char_len = it->len;
6982 it->dpvec = v->contents;
6983 it->dpend = v->contents + v->header.size;
6984 it->current.dpvec_index = 0;
6985 it->dpvec_face_id = -1;
6986 it->saved_face_id = it->face_id;
6987 it->method = GET_FROM_DISPLAY_VECTOR;
6988 it->ellipsis_p = 0;
6990 else
6992 set_iterator_to_next (it, 0);
6994 goto get_next;
6997 if (! NILP (lookup_glyphless_char_display (c, it)))
6999 if (it->what == IT_GLYPHLESS)
7000 goto done;
7001 /* Don't display this character. */
7002 set_iterator_to_next (it, 0);
7003 goto get_next;
7006 /* If `nobreak-char-display' is non-nil, we display
7007 non-ASCII spaces and hyphens specially. */
7008 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7010 if (c == 0xA0)
7011 nonascii_space_p = true;
7012 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
7013 nonascii_hyphen_p = true;
7016 /* Translate control characters into `\003' or `^C' form.
7017 Control characters coming from a display table entry are
7018 currently not translated because we use IT->dpvec to hold
7019 the translation. This could easily be changed but I
7020 don't believe that it is worth doing.
7022 The characters handled by `nobreak-char-display' must be
7023 translated too.
7025 Non-printable characters and raw-byte characters are also
7026 translated to octal form. */
7027 if (((c < ' ' || c == 127) /* ASCII control chars. */
7028 ? (it->area != TEXT_AREA
7029 /* In mode line, treat \n, \t like other crl chars. */
7030 || (c != '\t'
7031 && it->glyph_row
7032 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7033 || (c != '\n' && c != '\t'))
7034 : (nonascii_space_p
7035 || nonascii_hyphen_p
7036 || CHAR_BYTE8_P (c)
7037 || ! CHAR_PRINTABLE_P (c))))
7039 /* C is a control character, non-ASCII space/hyphen,
7040 raw-byte, or a non-printable character which must be
7041 displayed either as '\003' or as `^C' where the '\\'
7042 and '^' can be defined in the display table. Fill
7043 IT->ctl_chars with glyphs for what we have to
7044 display. Then, set IT->dpvec to these glyphs. */
7045 Lisp_Object gc;
7046 int ctl_len;
7047 int face_id;
7048 int lface_id = 0;
7049 int escape_glyph;
7051 /* Handle control characters with ^. */
7053 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7055 int g;
7057 g = '^'; /* default glyph for Control */
7058 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7059 if (it->dp
7060 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7062 g = GLYPH_CODE_CHAR (gc);
7063 lface_id = GLYPH_CODE_FACE (gc);
7066 face_id = (lface_id
7067 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7068 : merge_escape_glyph_face (it));
7070 XSETINT (it->ctl_chars[0], g);
7071 XSETINT (it->ctl_chars[1], c ^ 0100);
7072 ctl_len = 2;
7073 goto display_control;
7076 /* Handle non-ascii space in the mode where it only gets
7077 highlighting. */
7079 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7081 /* Merge `nobreak-space' into the current face. */
7082 face_id = merge_faces (it->f, Qnobreak_space, 0,
7083 it->face_id);
7084 XSETINT (it->ctl_chars[0], ' ');
7085 ctl_len = 1;
7086 goto display_control;
7089 /* Handle sequences that start with the "escape glyph". */
7091 /* the default escape glyph is \. */
7092 escape_glyph = '\\';
7094 if (it->dp
7095 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7097 escape_glyph = GLYPH_CODE_CHAR (gc);
7098 lface_id = GLYPH_CODE_FACE (gc);
7101 face_id = (lface_id
7102 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7103 : merge_escape_glyph_face (it));
7105 /* Draw non-ASCII hyphen with just highlighting: */
7107 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7109 XSETINT (it->ctl_chars[0], '-');
7110 ctl_len = 1;
7111 goto display_control;
7114 /* Draw non-ASCII space/hyphen with escape glyph: */
7116 if (nonascii_space_p || nonascii_hyphen_p)
7118 XSETINT (it->ctl_chars[0], escape_glyph);
7119 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7120 ctl_len = 2;
7121 goto display_control;
7125 char str[10];
7126 int len, i;
7128 if (CHAR_BYTE8_P (c))
7129 /* Display \200 instead of \17777600. */
7130 c = CHAR_TO_BYTE8 (c);
7131 len = sprintf (str, "%03o", c);
7133 XSETINT (it->ctl_chars[0], escape_glyph);
7134 for (i = 0; i < len; i++)
7135 XSETINT (it->ctl_chars[i + 1], str[i]);
7136 ctl_len = len + 1;
7139 display_control:
7140 /* Set up IT->dpvec and return first character from it. */
7141 it->dpvec_char_len = it->len;
7142 it->dpvec = it->ctl_chars;
7143 it->dpend = it->dpvec + ctl_len;
7144 it->current.dpvec_index = 0;
7145 it->dpvec_face_id = face_id;
7146 it->saved_face_id = it->face_id;
7147 it->method = GET_FROM_DISPLAY_VECTOR;
7148 it->ellipsis_p = 0;
7149 goto get_next;
7151 it->char_to_display = c;
7153 else if (success_p)
7155 it->char_to_display = it->c;
7159 #ifdef HAVE_WINDOW_SYSTEM
7160 /* Adjust face id for a multibyte character. There are no multibyte
7161 character in unibyte text. */
7162 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7163 && it->multibyte_p
7164 && success_p
7165 && FRAME_WINDOW_P (it->f))
7167 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7169 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7171 /* Automatic composition with glyph-string. */
7172 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7174 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7176 else
7178 ptrdiff_t pos = (it->s ? -1
7179 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7180 : IT_CHARPOS (*it));
7181 int c;
7183 if (it->what == IT_CHARACTER)
7184 c = it->char_to_display;
7185 else
7187 struct composition *cmp = composition_table[it->cmp_it.id];
7188 int i;
7190 c = ' ';
7191 for (i = 0; i < cmp->glyph_len; i++)
7192 /* TAB in a composition means display glyphs with
7193 padding space on the left or right. */
7194 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7195 break;
7197 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7200 #endif /* HAVE_WINDOW_SYSTEM */
7202 done:
7203 /* Is this character the last one of a run of characters with
7204 box? If yes, set IT->end_of_box_run_p to 1. */
7205 if (it->face_box_p
7206 && it->s == NULL)
7208 if (it->method == GET_FROM_STRING && it->sp)
7210 int face_id = underlying_face_id (it);
7211 struct face *face = FACE_FROM_ID (it->f, face_id);
7213 if (face)
7215 if (face->box == FACE_NO_BOX)
7217 /* If the box comes from face properties in a
7218 display string, check faces in that string. */
7219 int string_face_id = face_after_it_pos (it);
7220 it->end_of_box_run_p
7221 = (FACE_FROM_ID (it->f, string_face_id)->box
7222 == FACE_NO_BOX);
7224 /* Otherwise, the box comes from the underlying face.
7225 If this is the last string character displayed, check
7226 the next buffer location. */
7227 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7228 /* n_overlay_strings is unreliable unless
7229 overlay_string_index is non-negative. */
7230 && ((it->current.overlay_string_index >= 0
7231 && (it->current.overlay_string_index
7232 == it->n_overlay_strings - 1))
7233 /* A string from display property. */
7234 || it->from_disp_prop_p))
7236 ptrdiff_t ignore;
7237 int next_face_id;
7238 struct text_pos pos = it->current.pos;
7240 /* For a string from a display property, the next
7241 buffer position is stored in the 'position'
7242 member of the iteration stack slot below the
7243 current one, see handle_single_display_spec. By
7244 contrast, it->current.pos was is not yet updated
7245 to point to that buffer position; that will
7246 happen in pop_it, after we finish displaying the
7247 current string. Note that we already checked
7248 above that it->sp is positive, so subtracting one
7249 from it is safe. */
7250 if (it->from_disp_prop_p)
7251 pos = (it->stack + it->sp - 1)->position;
7252 else
7253 INC_TEXT_POS (pos, it->multibyte_p);
7255 if (CHARPOS (pos) >= ZV)
7256 it->end_of_box_run_p = true;
7257 else
7259 next_face_id = face_at_buffer_position
7260 (it->w, CHARPOS (pos), &ignore,
7261 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7262 it->end_of_box_run_p
7263 = (FACE_FROM_ID (it->f, next_face_id)->box
7264 == FACE_NO_BOX);
7269 /* next_element_from_display_vector sets this flag according to
7270 faces of the display vector glyphs, see there. */
7271 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7273 int face_id = face_after_it_pos (it);
7274 it->end_of_box_run_p
7275 = (face_id != it->face_id
7276 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7279 /* If we reached the end of the object we've been iterating (e.g., a
7280 display string or an overlay string), and there's something on
7281 IT->stack, proceed with what's on the stack. It doesn't make
7282 sense to return zero if there's unprocessed stuff on the stack,
7283 because otherwise that stuff will never be displayed. */
7284 if (!success_p && it->sp > 0)
7286 set_iterator_to_next (it, 0);
7287 success_p = get_next_display_element (it);
7290 /* Value is 0 if end of buffer or string reached. */
7291 return success_p;
7295 /* Move IT to the next display element.
7297 RESEAT_P non-zero means if called on a newline in buffer text,
7298 skip to the next visible line start.
7300 Functions get_next_display_element and set_iterator_to_next are
7301 separate because I find this arrangement easier to handle than a
7302 get_next_display_element function that also increments IT's
7303 position. The way it is we can first look at an iterator's current
7304 display element, decide whether it fits on a line, and if it does,
7305 increment the iterator position. The other way around we probably
7306 would either need a flag indicating whether the iterator has to be
7307 incremented the next time, or we would have to implement a
7308 decrement position function which would not be easy to write. */
7310 void
7311 set_iterator_to_next (struct it *it, int reseat_p)
7313 /* Reset flags indicating start and end of a sequence of characters
7314 with box. Reset them at the start of this function because
7315 moving the iterator to a new position might set them. */
7316 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7318 switch (it->method)
7320 case GET_FROM_BUFFER:
7321 /* The current display element of IT is a character from
7322 current_buffer. Advance in the buffer, and maybe skip over
7323 invisible lines that are so because of selective display. */
7324 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7325 reseat_at_next_visible_line_start (it, 0);
7326 else if (it->cmp_it.id >= 0)
7328 /* We are currently getting glyphs from a composition. */
7329 int i;
7331 if (! it->bidi_p)
7333 IT_CHARPOS (*it) += it->cmp_it.nchars;
7334 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7335 if (it->cmp_it.to < it->cmp_it.nglyphs)
7337 it->cmp_it.from = it->cmp_it.to;
7339 else
7341 it->cmp_it.id = -1;
7342 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7343 IT_BYTEPOS (*it),
7344 it->end_charpos, Qnil);
7347 else if (! it->cmp_it.reversed_p)
7349 /* Composition created while scanning forward. */
7350 /* Update IT's char/byte positions to point to the first
7351 character of the next grapheme cluster, or to the
7352 character visually after the current composition. */
7353 for (i = 0; i < it->cmp_it.nchars; i++)
7354 bidi_move_to_visually_next (&it->bidi_it);
7355 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7356 IT_CHARPOS (*it) = it->bidi_it.charpos;
7358 if (it->cmp_it.to < it->cmp_it.nglyphs)
7360 /* Proceed to the next grapheme cluster. */
7361 it->cmp_it.from = it->cmp_it.to;
7363 else
7365 /* No more grapheme clusters in this composition.
7366 Find the next stop position. */
7367 ptrdiff_t stop = it->end_charpos;
7368 if (it->bidi_it.scan_dir < 0)
7369 /* Now we are scanning backward and don't know
7370 where to stop. */
7371 stop = -1;
7372 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7373 IT_BYTEPOS (*it), stop, Qnil);
7376 else
7378 /* Composition created while scanning backward. */
7379 /* Update IT's char/byte positions to point to the last
7380 character of the previous grapheme cluster, or the
7381 character visually after the current composition. */
7382 for (i = 0; i < it->cmp_it.nchars; i++)
7383 bidi_move_to_visually_next (&it->bidi_it);
7384 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7385 IT_CHARPOS (*it) = it->bidi_it.charpos;
7386 if (it->cmp_it.from > 0)
7388 /* Proceed to the previous grapheme cluster. */
7389 it->cmp_it.to = it->cmp_it.from;
7391 else
7393 /* No more grapheme clusters in this composition.
7394 Find the next stop position. */
7395 ptrdiff_t stop = it->end_charpos;
7396 if (it->bidi_it.scan_dir < 0)
7397 /* Now we are scanning backward and don't know
7398 where to stop. */
7399 stop = -1;
7400 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7401 IT_BYTEPOS (*it), stop, Qnil);
7405 else
7407 eassert (it->len != 0);
7409 if (!it->bidi_p)
7411 IT_BYTEPOS (*it) += it->len;
7412 IT_CHARPOS (*it) += 1;
7414 else
7416 int prev_scan_dir = it->bidi_it.scan_dir;
7417 /* If this is a new paragraph, determine its base
7418 direction (a.k.a. its base embedding level). */
7419 if (it->bidi_it.new_paragraph)
7420 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7421 bidi_move_to_visually_next (&it->bidi_it);
7422 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7423 IT_CHARPOS (*it) = it->bidi_it.charpos;
7424 if (prev_scan_dir != it->bidi_it.scan_dir)
7426 /* As the scan direction was changed, we must
7427 re-compute the stop position for composition. */
7428 ptrdiff_t stop = it->end_charpos;
7429 if (it->bidi_it.scan_dir < 0)
7430 stop = -1;
7431 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7432 IT_BYTEPOS (*it), stop, Qnil);
7435 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7437 break;
7439 case GET_FROM_C_STRING:
7440 /* Current display element of IT is from a C string. */
7441 if (!it->bidi_p
7442 /* If the string position is beyond string's end, it means
7443 next_element_from_c_string is padding the string with
7444 blanks, in which case we bypass the bidi iterator,
7445 because it cannot deal with such virtual characters. */
7446 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7448 IT_BYTEPOS (*it) += it->len;
7449 IT_CHARPOS (*it) += 1;
7451 else
7453 bidi_move_to_visually_next (&it->bidi_it);
7454 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7455 IT_CHARPOS (*it) = it->bidi_it.charpos;
7457 break;
7459 case GET_FROM_DISPLAY_VECTOR:
7460 /* Current display element of IT is from a display table entry.
7461 Advance in the display table definition. Reset it to null if
7462 end reached, and continue with characters from buffers/
7463 strings. */
7464 ++it->current.dpvec_index;
7466 /* Restore face of the iterator to what they were before the
7467 display vector entry (these entries may contain faces). */
7468 it->face_id = it->saved_face_id;
7470 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7472 int recheck_faces = it->ellipsis_p;
7474 if (it->s)
7475 it->method = GET_FROM_C_STRING;
7476 else if (STRINGP (it->string))
7477 it->method = GET_FROM_STRING;
7478 else
7480 it->method = GET_FROM_BUFFER;
7481 it->object = it->w->contents;
7484 it->dpvec = NULL;
7485 it->current.dpvec_index = -1;
7487 /* Skip over characters which were displayed via IT->dpvec. */
7488 if (it->dpvec_char_len < 0)
7489 reseat_at_next_visible_line_start (it, 1);
7490 else if (it->dpvec_char_len > 0)
7492 if (it->method == GET_FROM_STRING
7493 && it->current.overlay_string_index >= 0
7494 && it->n_overlay_strings > 0)
7495 it->ignore_overlay_strings_at_pos_p = true;
7496 it->len = it->dpvec_char_len;
7497 set_iterator_to_next (it, reseat_p);
7500 /* Maybe recheck faces after display vector. */
7501 if (recheck_faces)
7502 it->stop_charpos = IT_CHARPOS (*it);
7504 break;
7506 case GET_FROM_STRING:
7507 /* Current display element is a character from a Lisp string. */
7508 eassert (it->s == NULL && STRINGP (it->string));
7509 /* Don't advance past string end. These conditions are true
7510 when set_iterator_to_next is called at the end of
7511 get_next_display_element, in which case the Lisp string is
7512 already exhausted, and all we want is pop the iterator
7513 stack. */
7514 if (it->current.overlay_string_index >= 0)
7516 /* This is an overlay string, so there's no padding with
7517 spaces, and the number of characters in the string is
7518 where the string ends. */
7519 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7520 goto consider_string_end;
7522 else
7524 /* Not an overlay string. There could be padding, so test
7525 against it->end_charpos. */
7526 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7527 goto consider_string_end;
7529 if (it->cmp_it.id >= 0)
7531 int i;
7533 if (! it->bidi_p)
7535 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7536 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7537 if (it->cmp_it.to < it->cmp_it.nglyphs)
7538 it->cmp_it.from = it->cmp_it.to;
7539 else
7541 it->cmp_it.id = -1;
7542 composition_compute_stop_pos (&it->cmp_it,
7543 IT_STRING_CHARPOS (*it),
7544 IT_STRING_BYTEPOS (*it),
7545 it->end_charpos, it->string);
7548 else if (! it->cmp_it.reversed_p)
7550 for (i = 0; i < it->cmp_it.nchars; i++)
7551 bidi_move_to_visually_next (&it->bidi_it);
7552 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7553 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7555 if (it->cmp_it.to < it->cmp_it.nglyphs)
7556 it->cmp_it.from = it->cmp_it.to;
7557 else
7559 ptrdiff_t stop = it->end_charpos;
7560 if (it->bidi_it.scan_dir < 0)
7561 stop = -1;
7562 composition_compute_stop_pos (&it->cmp_it,
7563 IT_STRING_CHARPOS (*it),
7564 IT_STRING_BYTEPOS (*it), stop,
7565 it->string);
7568 else
7570 for (i = 0; i < it->cmp_it.nchars; i++)
7571 bidi_move_to_visually_next (&it->bidi_it);
7572 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7573 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7574 if (it->cmp_it.from > 0)
7575 it->cmp_it.to = it->cmp_it.from;
7576 else
7578 ptrdiff_t stop = it->end_charpos;
7579 if (it->bidi_it.scan_dir < 0)
7580 stop = -1;
7581 composition_compute_stop_pos (&it->cmp_it,
7582 IT_STRING_CHARPOS (*it),
7583 IT_STRING_BYTEPOS (*it), stop,
7584 it->string);
7588 else
7590 if (!it->bidi_p
7591 /* If the string position is beyond string's end, it
7592 means next_element_from_string is padding the string
7593 with blanks, in which case we bypass the bidi
7594 iterator, because it cannot deal with such virtual
7595 characters. */
7596 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7598 IT_STRING_BYTEPOS (*it) += it->len;
7599 IT_STRING_CHARPOS (*it) += 1;
7601 else
7603 int prev_scan_dir = it->bidi_it.scan_dir;
7605 bidi_move_to_visually_next (&it->bidi_it);
7606 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7607 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7608 if (prev_scan_dir != it->bidi_it.scan_dir)
7610 ptrdiff_t stop = it->end_charpos;
7612 if (it->bidi_it.scan_dir < 0)
7613 stop = -1;
7614 composition_compute_stop_pos (&it->cmp_it,
7615 IT_STRING_CHARPOS (*it),
7616 IT_STRING_BYTEPOS (*it), stop,
7617 it->string);
7622 consider_string_end:
7624 if (it->current.overlay_string_index >= 0)
7626 /* IT->string is an overlay string. Advance to the
7627 next, if there is one. */
7628 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7630 it->ellipsis_p = 0;
7631 next_overlay_string (it);
7632 if (it->ellipsis_p)
7633 setup_for_ellipsis (it, 0);
7636 else
7638 /* IT->string is not an overlay string. If we reached
7639 its end, and there is something on IT->stack, proceed
7640 with what is on the stack. This can be either another
7641 string, this time an overlay string, or a buffer. */
7642 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7643 && it->sp > 0)
7645 pop_it (it);
7646 if (it->method == GET_FROM_STRING)
7647 goto consider_string_end;
7650 break;
7652 case GET_FROM_IMAGE:
7653 case GET_FROM_STRETCH:
7654 /* The position etc with which we have to proceed are on
7655 the stack. The position may be at the end of a string,
7656 if the `display' property takes up the whole string. */
7657 eassert (it->sp > 0);
7658 pop_it (it);
7659 if (it->method == GET_FROM_STRING)
7660 goto consider_string_end;
7661 break;
7663 default:
7664 /* There are no other methods defined, so this should be a bug. */
7665 emacs_abort ();
7668 eassert (it->method != GET_FROM_STRING
7669 || (STRINGP (it->string)
7670 && IT_STRING_CHARPOS (*it) >= 0));
7673 /* Load IT's display element fields with information about the next
7674 display element which comes from a display table entry or from the
7675 result of translating a control character to one of the forms `^C'
7676 or `\003'.
7678 IT->dpvec holds the glyphs to return as characters.
7679 IT->saved_face_id holds the face id before the display vector--it
7680 is restored into IT->face_id in set_iterator_to_next. */
7682 static int
7683 next_element_from_display_vector (struct it *it)
7685 Lisp_Object gc;
7686 int prev_face_id = it->face_id;
7687 int next_face_id;
7689 /* Precondition. */
7690 eassert (it->dpvec && it->current.dpvec_index >= 0);
7692 it->face_id = it->saved_face_id;
7694 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7695 That seemed totally bogus - so I changed it... */
7696 gc = it->dpvec[it->current.dpvec_index];
7698 if (GLYPH_CODE_P (gc))
7700 struct face *this_face, *prev_face, *next_face;
7702 it->c = GLYPH_CODE_CHAR (gc);
7703 it->len = CHAR_BYTES (it->c);
7705 /* The entry may contain a face id to use. Such a face id is
7706 the id of a Lisp face, not a realized face. A face id of
7707 zero means no face is specified. */
7708 if (it->dpvec_face_id >= 0)
7709 it->face_id = it->dpvec_face_id;
7710 else
7712 int lface_id = GLYPH_CODE_FACE (gc);
7713 if (lface_id > 0)
7714 it->face_id = merge_faces (it->f, Qt, lface_id,
7715 it->saved_face_id);
7718 /* Glyphs in the display vector could have the box face, so we
7719 need to set the related flags in the iterator, as
7720 appropriate. */
7721 this_face = FACE_FROM_ID (it->f, it->face_id);
7722 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7724 /* Is this character the first character of a box-face run? */
7725 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7726 && (!prev_face
7727 || prev_face->box == FACE_NO_BOX));
7729 /* For the last character of the box-face run, we need to look
7730 either at the next glyph from the display vector, or at the
7731 face we saw before the display vector. */
7732 next_face_id = it->saved_face_id;
7733 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7735 if (it->dpvec_face_id >= 0)
7736 next_face_id = it->dpvec_face_id;
7737 else
7739 int lface_id =
7740 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7742 if (lface_id > 0)
7743 next_face_id = merge_faces (it->f, Qt, lface_id,
7744 it->saved_face_id);
7747 next_face = FACE_FROM_ID (it->f, next_face_id);
7748 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7749 && (!next_face
7750 || next_face->box == FACE_NO_BOX));
7751 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7753 else
7754 /* Display table entry is invalid. Return a space. */
7755 it->c = ' ', it->len = 1;
7757 /* Don't change position and object of the iterator here. They are
7758 still the values of the character that had this display table
7759 entry or was translated, and that's what we want. */
7760 it->what = IT_CHARACTER;
7761 return 1;
7764 /* Get the first element of string/buffer in the visual order, after
7765 being reseated to a new position in a string or a buffer. */
7766 static void
7767 get_visually_first_element (struct it *it)
7769 int string_p = STRINGP (it->string) || it->s;
7770 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7771 ptrdiff_t bob = (string_p ? 0 : BEGV);
7773 if (STRINGP (it->string))
7775 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7776 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7778 else
7780 it->bidi_it.charpos = IT_CHARPOS (*it);
7781 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7784 if (it->bidi_it.charpos == eob)
7786 /* Nothing to do, but reset the FIRST_ELT flag, like
7787 bidi_paragraph_init does, because we are not going to
7788 call it. */
7789 it->bidi_it.first_elt = 0;
7791 else if (it->bidi_it.charpos == bob
7792 || (!string_p
7793 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7794 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7796 /* If we are at the beginning of a line/string, we can produce
7797 the next element right away. */
7798 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7799 bidi_move_to_visually_next (&it->bidi_it);
7801 else
7803 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7805 /* We need to prime the bidi iterator starting at the line's or
7806 string's beginning, before we will be able to produce the
7807 next element. */
7808 if (string_p)
7809 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7810 else
7811 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7812 IT_BYTEPOS (*it), -1,
7813 &it->bidi_it.bytepos);
7814 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7817 /* Now return to buffer/string position where we were asked
7818 to get the next display element, and produce that. */
7819 bidi_move_to_visually_next (&it->bidi_it);
7821 while (it->bidi_it.bytepos != orig_bytepos
7822 && it->bidi_it.charpos < eob);
7825 /* Adjust IT's position information to where we ended up. */
7826 if (STRINGP (it->string))
7828 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7829 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7831 else
7833 IT_CHARPOS (*it) = it->bidi_it.charpos;
7834 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7837 if (STRINGP (it->string) || !it->s)
7839 ptrdiff_t stop, charpos, bytepos;
7841 if (STRINGP (it->string))
7843 eassert (!it->s);
7844 stop = SCHARS (it->string);
7845 if (stop > it->end_charpos)
7846 stop = it->end_charpos;
7847 charpos = IT_STRING_CHARPOS (*it);
7848 bytepos = IT_STRING_BYTEPOS (*it);
7850 else
7852 stop = it->end_charpos;
7853 charpos = IT_CHARPOS (*it);
7854 bytepos = IT_BYTEPOS (*it);
7856 if (it->bidi_it.scan_dir < 0)
7857 stop = -1;
7858 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7859 it->string);
7863 /* Load IT with the next display element from Lisp string IT->string.
7864 IT->current.string_pos is the current position within the string.
7865 If IT->current.overlay_string_index >= 0, the Lisp string is an
7866 overlay string. */
7868 static int
7869 next_element_from_string (struct it *it)
7871 struct text_pos position;
7873 eassert (STRINGP (it->string));
7874 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7875 eassert (IT_STRING_CHARPOS (*it) >= 0);
7876 position = it->current.string_pos;
7878 /* With bidi reordering, the character to display might not be the
7879 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7880 that we were reseat()ed to a new string, whose paragraph
7881 direction is not known. */
7882 if (it->bidi_p && it->bidi_it.first_elt)
7884 get_visually_first_element (it);
7885 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7888 /* Time to check for invisible text? */
7889 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7891 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7893 if (!(!it->bidi_p
7894 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7895 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7897 /* With bidi non-linear iteration, we could find
7898 ourselves far beyond the last computed stop_charpos,
7899 with several other stop positions in between that we
7900 missed. Scan them all now, in buffer's logical
7901 order, until we find and handle the last stop_charpos
7902 that precedes our current position. */
7903 handle_stop_backwards (it, it->stop_charpos);
7904 return GET_NEXT_DISPLAY_ELEMENT (it);
7906 else
7908 if (it->bidi_p)
7910 /* Take note of the stop position we just moved
7911 across, for when we will move back across it. */
7912 it->prev_stop = it->stop_charpos;
7913 /* If we are at base paragraph embedding level, take
7914 note of the last stop position seen at this
7915 level. */
7916 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7917 it->base_level_stop = it->stop_charpos;
7919 handle_stop (it);
7921 /* Since a handler may have changed IT->method, we must
7922 recurse here. */
7923 return GET_NEXT_DISPLAY_ELEMENT (it);
7926 else if (it->bidi_p
7927 /* If we are before prev_stop, we may have overstepped
7928 on our way backwards a stop_pos, and if so, we need
7929 to handle that stop_pos. */
7930 && IT_STRING_CHARPOS (*it) < it->prev_stop
7931 /* We can sometimes back up for reasons that have nothing
7932 to do with bidi reordering. E.g., compositions. The
7933 code below is only needed when we are above the base
7934 embedding level, so test for that explicitly. */
7935 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7937 /* If we lost track of base_level_stop, we have no better
7938 place for handle_stop_backwards to start from than string
7939 beginning. This happens, e.g., when we were reseated to
7940 the previous screenful of text by vertical-motion. */
7941 if (it->base_level_stop <= 0
7942 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7943 it->base_level_stop = 0;
7944 handle_stop_backwards (it, it->base_level_stop);
7945 return GET_NEXT_DISPLAY_ELEMENT (it);
7949 if (it->current.overlay_string_index >= 0)
7951 /* Get the next character from an overlay string. In overlay
7952 strings, there is no field width or padding with spaces to
7953 do. */
7954 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7956 it->what = IT_EOB;
7957 return 0;
7959 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7960 IT_STRING_BYTEPOS (*it),
7961 it->bidi_it.scan_dir < 0
7962 ? -1
7963 : SCHARS (it->string))
7964 && next_element_from_composition (it))
7966 return 1;
7968 else if (STRING_MULTIBYTE (it->string))
7970 const unsigned char *s = (SDATA (it->string)
7971 + IT_STRING_BYTEPOS (*it));
7972 it->c = string_char_and_length (s, &it->len);
7974 else
7976 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7977 it->len = 1;
7980 else
7982 /* Get the next character from a Lisp string that is not an
7983 overlay string. Such strings come from the mode line, for
7984 example. We may have to pad with spaces, or truncate the
7985 string. See also next_element_from_c_string. */
7986 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7988 it->what = IT_EOB;
7989 return 0;
7991 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7993 /* Pad with spaces. */
7994 it->c = ' ', it->len = 1;
7995 CHARPOS (position) = BYTEPOS (position) = -1;
7997 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7998 IT_STRING_BYTEPOS (*it),
7999 it->bidi_it.scan_dir < 0
8000 ? -1
8001 : it->string_nchars)
8002 && next_element_from_composition (it))
8004 return 1;
8006 else if (STRING_MULTIBYTE (it->string))
8008 const unsigned char *s = (SDATA (it->string)
8009 + IT_STRING_BYTEPOS (*it));
8010 it->c = string_char_and_length (s, &it->len);
8012 else
8014 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8015 it->len = 1;
8019 /* Record what we have and where it came from. */
8020 it->what = IT_CHARACTER;
8021 it->object = it->string;
8022 it->position = position;
8023 return 1;
8027 /* Load IT with next display element from C string IT->s.
8028 IT->string_nchars is the maximum number of characters to return
8029 from the string. IT->end_charpos may be greater than
8030 IT->string_nchars when this function is called, in which case we
8031 may have to return padding spaces. Value is zero if end of string
8032 reached, including padding spaces. */
8034 static int
8035 next_element_from_c_string (struct it *it)
8037 bool success_p = true;
8039 eassert (it->s);
8040 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8041 it->what = IT_CHARACTER;
8042 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8043 it->object = Qnil;
8045 /* With bidi reordering, the character to display might not be the
8046 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8047 we were reseated to a new string, whose paragraph direction is
8048 not known. */
8049 if (it->bidi_p && it->bidi_it.first_elt)
8050 get_visually_first_element (it);
8052 /* IT's position can be greater than IT->string_nchars in case a
8053 field width or precision has been specified when the iterator was
8054 initialized. */
8055 if (IT_CHARPOS (*it) >= it->end_charpos)
8057 /* End of the game. */
8058 it->what = IT_EOB;
8059 success_p = 0;
8061 else if (IT_CHARPOS (*it) >= it->string_nchars)
8063 /* Pad with spaces. */
8064 it->c = ' ', it->len = 1;
8065 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8067 else if (it->multibyte_p)
8068 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8069 else
8070 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8072 return success_p;
8076 /* Set up IT to return characters from an ellipsis, if appropriate.
8077 The definition of the ellipsis glyphs may come from a display table
8078 entry. This function fills IT with the first glyph from the
8079 ellipsis if an ellipsis is to be displayed. */
8081 static int
8082 next_element_from_ellipsis (struct it *it)
8084 if (it->selective_display_ellipsis_p)
8085 setup_for_ellipsis (it, it->len);
8086 else
8088 /* The face at the current position may be different from the
8089 face we find after the invisible text. Remember what it
8090 was in IT->saved_face_id, and signal that it's there by
8091 setting face_before_selective_p. */
8092 it->saved_face_id = it->face_id;
8093 it->method = GET_FROM_BUFFER;
8094 it->object = it->w->contents;
8095 reseat_at_next_visible_line_start (it, 1);
8096 it->face_before_selective_p = true;
8099 return GET_NEXT_DISPLAY_ELEMENT (it);
8103 /* Deliver an image display element. The iterator IT is already
8104 filled with image information (done in handle_display_prop). Value
8105 is always 1. */
8108 static int
8109 next_element_from_image (struct it *it)
8111 it->what = IT_IMAGE;
8112 it->ignore_overlay_strings_at_pos_p = 0;
8113 return 1;
8117 /* Fill iterator IT with next display element from a stretch glyph
8118 property. IT->object is the value of the text property. Value is
8119 always 1. */
8121 static int
8122 next_element_from_stretch (struct it *it)
8124 it->what = IT_STRETCH;
8125 return 1;
8128 /* Scan backwards from IT's current position until we find a stop
8129 position, or until BEGV. This is called when we find ourself
8130 before both the last known prev_stop and base_level_stop while
8131 reordering bidirectional text. */
8133 static void
8134 compute_stop_pos_backwards (struct it *it)
8136 const int SCAN_BACK_LIMIT = 1000;
8137 struct text_pos pos;
8138 struct display_pos save_current = it->current;
8139 struct text_pos save_position = it->position;
8140 ptrdiff_t charpos = IT_CHARPOS (*it);
8141 ptrdiff_t where_we_are = charpos;
8142 ptrdiff_t save_stop_pos = it->stop_charpos;
8143 ptrdiff_t save_end_pos = it->end_charpos;
8145 eassert (NILP (it->string) && !it->s);
8146 eassert (it->bidi_p);
8147 it->bidi_p = 0;
8150 it->end_charpos = min (charpos + 1, ZV);
8151 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8152 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8153 reseat_1 (it, pos, 0);
8154 compute_stop_pos (it);
8155 /* We must advance forward, right? */
8156 if (it->stop_charpos <= charpos)
8157 emacs_abort ();
8159 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8161 if (it->stop_charpos <= where_we_are)
8162 it->prev_stop = it->stop_charpos;
8163 else
8164 it->prev_stop = BEGV;
8165 it->bidi_p = true;
8166 it->current = save_current;
8167 it->position = save_position;
8168 it->stop_charpos = save_stop_pos;
8169 it->end_charpos = save_end_pos;
8172 /* Scan forward from CHARPOS in the current buffer/string, until we
8173 find a stop position > current IT's position. Then handle the stop
8174 position before that. This is called when we bump into a stop
8175 position while reordering bidirectional text. CHARPOS should be
8176 the last previously processed stop_pos (or BEGV/0, if none were
8177 processed yet) whose position is less that IT's current
8178 position. */
8180 static void
8181 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8183 int bufp = !STRINGP (it->string);
8184 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8185 struct display_pos save_current = it->current;
8186 struct text_pos save_position = it->position;
8187 struct text_pos pos1;
8188 ptrdiff_t next_stop;
8190 /* Scan in strict logical order. */
8191 eassert (it->bidi_p);
8192 it->bidi_p = 0;
8195 it->prev_stop = charpos;
8196 if (bufp)
8198 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8199 reseat_1 (it, pos1, 0);
8201 else
8202 it->current.string_pos = string_pos (charpos, it->string);
8203 compute_stop_pos (it);
8204 /* We must advance forward, right? */
8205 if (it->stop_charpos <= it->prev_stop)
8206 emacs_abort ();
8207 charpos = it->stop_charpos;
8209 while (charpos <= where_we_are);
8211 it->bidi_p = true;
8212 it->current = save_current;
8213 it->position = save_position;
8214 next_stop = it->stop_charpos;
8215 it->stop_charpos = it->prev_stop;
8216 handle_stop (it);
8217 it->stop_charpos = next_stop;
8220 /* Load IT with the next display element from current_buffer. Value
8221 is zero if end of buffer reached. IT->stop_charpos is the next
8222 position at which to stop and check for text properties or buffer
8223 end. */
8225 static int
8226 next_element_from_buffer (struct it *it)
8228 bool success_p = true;
8230 eassert (IT_CHARPOS (*it) >= BEGV);
8231 eassert (NILP (it->string) && !it->s);
8232 eassert (!it->bidi_p
8233 || (EQ (it->bidi_it.string.lstring, Qnil)
8234 && it->bidi_it.string.s == NULL));
8236 /* With bidi reordering, the character to display might not be the
8237 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8238 we were reseat()ed to a new buffer position, which is potentially
8239 a different paragraph. */
8240 if (it->bidi_p && it->bidi_it.first_elt)
8242 get_visually_first_element (it);
8243 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8246 if (IT_CHARPOS (*it) >= it->stop_charpos)
8248 if (IT_CHARPOS (*it) >= it->end_charpos)
8250 int overlay_strings_follow_p;
8252 /* End of the game, except when overlay strings follow that
8253 haven't been returned yet. */
8254 if (it->overlay_strings_at_end_processed_p)
8255 overlay_strings_follow_p = 0;
8256 else
8258 it->overlay_strings_at_end_processed_p = true;
8259 overlay_strings_follow_p = get_overlay_strings (it, 0);
8262 if (overlay_strings_follow_p)
8263 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8264 else
8266 it->what = IT_EOB;
8267 it->position = it->current.pos;
8268 success_p = 0;
8271 else if (!(!it->bidi_p
8272 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8273 || IT_CHARPOS (*it) == it->stop_charpos))
8275 /* With bidi non-linear iteration, we could find ourselves
8276 far beyond the last computed stop_charpos, with several
8277 other stop positions in between that we missed. Scan
8278 them all now, in buffer's logical order, until we find
8279 and handle the last stop_charpos that precedes our
8280 current position. */
8281 handle_stop_backwards (it, it->stop_charpos);
8282 return GET_NEXT_DISPLAY_ELEMENT (it);
8284 else
8286 if (it->bidi_p)
8288 /* Take note of the stop position we just moved across,
8289 for when we will move back across it. */
8290 it->prev_stop = it->stop_charpos;
8291 /* If we are at base paragraph embedding level, take
8292 note of the last stop position seen at this
8293 level. */
8294 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8295 it->base_level_stop = it->stop_charpos;
8297 handle_stop (it);
8298 return GET_NEXT_DISPLAY_ELEMENT (it);
8301 else if (it->bidi_p
8302 /* If we are before prev_stop, we may have overstepped on
8303 our way backwards a stop_pos, and if so, we need to
8304 handle that stop_pos. */
8305 && IT_CHARPOS (*it) < it->prev_stop
8306 /* We can sometimes back up for reasons that have nothing
8307 to do with bidi reordering. E.g., compositions. The
8308 code below is only needed when we are above the base
8309 embedding level, so test for that explicitly. */
8310 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8312 if (it->base_level_stop <= 0
8313 || IT_CHARPOS (*it) < it->base_level_stop)
8315 /* If we lost track of base_level_stop, we need to find
8316 prev_stop by looking backwards. This happens, e.g., when
8317 we were reseated to the previous screenful of text by
8318 vertical-motion. */
8319 it->base_level_stop = BEGV;
8320 compute_stop_pos_backwards (it);
8321 handle_stop_backwards (it, it->prev_stop);
8323 else
8324 handle_stop_backwards (it, it->base_level_stop);
8325 return GET_NEXT_DISPLAY_ELEMENT (it);
8327 else
8329 /* No face changes, overlays etc. in sight, so just return a
8330 character from current_buffer. */
8331 unsigned char *p;
8332 ptrdiff_t stop;
8334 /* Maybe run the redisplay end trigger hook. Performance note:
8335 This doesn't seem to cost measurable time. */
8336 if (it->redisplay_end_trigger_charpos
8337 && it->glyph_row
8338 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8339 run_redisplay_end_trigger_hook (it);
8341 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8342 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8343 stop)
8344 && next_element_from_composition (it))
8346 return 1;
8349 /* Get the next character, maybe multibyte. */
8350 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8351 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8352 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8353 else
8354 it->c = *p, it->len = 1;
8356 /* Record what we have and where it came from. */
8357 it->what = IT_CHARACTER;
8358 it->object = it->w->contents;
8359 it->position = it->current.pos;
8361 /* Normally we return the character found above, except when we
8362 really want to return an ellipsis for selective display. */
8363 if (it->selective)
8365 if (it->c == '\n')
8367 /* A value of selective > 0 means hide lines indented more
8368 than that number of columns. */
8369 if (it->selective > 0
8370 && IT_CHARPOS (*it) + 1 < ZV
8371 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8372 IT_BYTEPOS (*it) + 1,
8373 it->selective))
8375 success_p = next_element_from_ellipsis (it);
8376 it->dpvec_char_len = -1;
8379 else if (it->c == '\r' && it->selective == -1)
8381 /* A value of selective == -1 means that everything from the
8382 CR to the end of the line is invisible, with maybe an
8383 ellipsis displayed for it. */
8384 success_p = next_element_from_ellipsis (it);
8385 it->dpvec_char_len = -1;
8390 /* Value is zero if end of buffer reached. */
8391 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8392 return success_p;
8396 /* Run the redisplay end trigger hook for IT. */
8398 static void
8399 run_redisplay_end_trigger_hook (struct it *it)
8401 Lisp_Object args[3];
8403 /* IT->glyph_row should be non-null, i.e. we should be actually
8404 displaying something, or otherwise we should not run the hook. */
8405 eassert (it->glyph_row);
8407 /* Set up hook arguments. */
8408 args[0] = Qredisplay_end_trigger_functions;
8409 args[1] = it->window;
8410 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8411 it->redisplay_end_trigger_charpos = 0;
8413 /* Since we are *trying* to run these functions, don't try to run
8414 them again, even if they get an error. */
8415 wset_redisplay_end_trigger (it->w, Qnil);
8416 Frun_hook_with_args (3, args);
8418 /* Notice if it changed the face of the character we are on. */
8419 handle_face_prop (it);
8423 /* Deliver a composition display element. Unlike the other
8424 next_element_from_XXX, this function is not registered in the array
8425 get_next_element[]. It is called from next_element_from_buffer and
8426 next_element_from_string when necessary. */
8428 static int
8429 next_element_from_composition (struct it *it)
8431 it->what = IT_COMPOSITION;
8432 it->len = it->cmp_it.nbytes;
8433 if (STRINGP (it->string))
8435 if (it->c < 0)
8437 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8438 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8439 return 0;
8441 it->position = it->current.string_pos;
8442 it->object = it->string;
8443 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8444 IT_STRING_BYTEPOS (*it), it->string);
8446 else
8448 if (it->c < 0)
8450 IT_CHARPOS (*it) += it->cmp_it.nchars;
8451 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8452 if (it->bidi_p)
8454 if (it->bidi_it.new_paragraph)
8455 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8456 /* Resync the bidi iterator with IT's new position.
8457 FIXME: this doesn't support bidirectional text. */
8458 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8459 bidi_move_to_visually_next (&it->bidi_it);
8461 return 0;
8463 it->position = it->current.pos;
8464 it->object = it->w->contents;
8465 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8466 IT_BYTEPOS (*it), Qnil);
8468 return 1;
8473 /***********************************************************************
8474 Moving an iterator without producing glyphs
8475 ***********************************************************************/
8477 /* Check if iterator is at a position corresponding to a valid buffer
8478 position after some move_it_ call. */
8480 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8481 ((it)->method == GET_FROM_STRING \
8482 ? IT_STRING_CHARPOS (*it) == 0 \
8483 : 1)
8486 /* Move iterator IT to a specified buffer or X position within one
8487 line on the display without producing glyphs.
8489 OP should be a bit mask including some or all of these bits:
8490 MOVE_TO_X: Stop upon reaching x-position TO_X.
8491 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8492 Regardless of OP's value, stop upon reaching the end of the display line.
8494 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8495 This means, in particular, that TO_X includes window's horizontal
8496 scroll amount.
8498 The return value has several possible values that
8499 say what condition caused the scan to stop:
8501 MOVE_POS_MATCH_OR_ZV
8502 - when TO_POS or ZV was reached.
8504 MOVE_X_REACHED
8505 -when TO_X was reached before TO_POS or ZV were reached.
8507 MOVE_LINE_CONTINUED
8508 - when we reached the end of the display area and the line must
8509 be continued.
8511 MOVE_LINE_TRUNCATED
8512 - when we reached the end of the display area and the line is
8513 truncated.
8515 MOVE_NEWLINE_OR_CR
8516 - when we stopped at a line end, i.e. a newline or a CR and selective
8517 display is on. */
8519 static enum move_it_result
8520 move_it_in_display_line_to (struct it *it,
8521 ptrdiff_t to_charpos, int to_x,
8522 enum move_operation_enum op)
8524 enum move_it_result result = MOVE_UNDEFINED;
8525 struct glyph_row *saved_glyph_row;
8526 struct it wrap_it, atpos_it, atx_it, ppos_it;
8527 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8528 void *ppos_data = NULL;
8529 int may_wrap = 0;
8530 enum it_method prev_method = it->method;
8531 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8532 int saw_smaller_pos = prev_pos < to_charpos;
8534 /* Don't produce glyphs in produce_glyphs. */
8535 saved_glyph_row = it->glyph_row;
8536 it->glyph_row = NULL;
8538 /* Use wrap_it to save a copy of IT wherever a word wrap could
8539 occur. Use atpos_it to save a copy of IT at the desired buffer
8540 position, if found, so that we can scan ahead and check if the
8541 word later overshoots the window edge. Use atx_it similarly, for
8542 pixel positions. */
8543 wrap_it.sp = -1;
8544 atpos_it.sp = -1;
8545 atx_it.sp = -1;
8547 /* Use ppos_it under bidi reordering to save a copy of IT for the
8548 initial position. We restore that position in IT when we have
8549 scanned the entire display line without finding a match for
8550 TO_CHARPOS and all the character positions are greater than
8551 TO_CHARPOS. We then restart the scan from the initial position,
8552 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8553 the closest to TO_CHARPOS. */
8554 if (it->bidi_p)
8556 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8558 SAVE_IT (ppos_it, *it, ppos_data);
8559 closest_pos = IT_CHARPOS (*it);
8561 else
8562 closest_pos = ZV;
8565 #define BUFFER_POS_REACHED_P() \
8566 ((op & MOVE_TO_POS) != 0 \
8567 && BUFFERP (it->object) \
8568 && (IT_CHARPOS (*it) == to_charpos \
8569 || ((!it->bidi_p \
8570 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8571 && IT_CHARPOS (*it) > to_charpos) \
8572 || (it->what == IT_COMPOSITION \
8573 && ((IT_CHARPOS (*it) > to_charpos \
8574 && to_charpos >= it->cmp_it.charpos) \
8575 || (IT_CHARPOS (*it) < to_charpos \
8576 && to_charpos <= it->cmp_it.charpos)))) \
8577 && (it->method == GET_FROM_BUFFER \
8578 || (it->method == GET_FROM_DISPLAY_VECTOR \
8579 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8581 /* If there's a line-/wrap-prefix, handle it. */
8582 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8583 && it->current_y < it->last_visible_y)
8584 handle_line_prefix (it);
8586 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8587 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8589 while (1)
8591 int x, i, ascent = 0, descent = 0;
8593 /* Utility macro to reset an iterator with x, ascent, and descent. */
8594 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8595 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8596 (IT)->max_descent = descent)
8598 /* Stop if we move beyond TO_CHARPOS (after an image or a
8599 display string or stretch glyph). */
8600 if ((op & MOVE_TO_POS) != 0
8601 && BUFFERP (it->object)
8602 && it->method == GET_FROM_BUFFER
8603 && (((!it->bidi_p
8604 /* When the iterator is at base embedding level, we
8605 are guaranteed that characters are delivered for
8606 display in strictly increasing order of their
8607 buffer positions. */
8608 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8609 && IT_CHARPOS (*it) > to_charpos)
8610 || (it->bidi_p
8611 && (prev_method == GET_FROM_IMAGE
8612 || prev_method == GET_FROM_STRETCH
8613 || prev_method == GET_FROM_STRING)
8614 /* Passed TO_CHARPOS from left to right. */
8615 && ((prev_pos < to_charpos
8616 && IT_CHARPOS (*it) > to_charpos)
8617 /* Passed TO_CHARPOS from right to left. */
8618 || (prev_pos > to_charpos
8619 && IT_CHARPOS (*it) < to_charpos)))))
8621 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8623 result = MOVE_POS_MATCH_OR_ZV;
8624 break;
8626 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8627 /* If wrap_it is valid, the current position might be in a
8628 word that is wrapped. So, save the iterator in
8629 atpos_it and continue to see if wrapping happens. */
8630 SAVE_IT (atpos_it, *it, atpos_data);
8633 /* Stop when ZV reached.
8634 We used to stop here when TO_CHARPOS reached as well, but that is
8635 too soon if this glyph does not fit on this line. So we handle it
8636 explicitly below. */
8637 if (!get_next_display_element (it))
8639 result = MOVE_POS_MATCH_OR_ZV;
8640 break;
8643 if (it->line_wrap == TRUNCATE)
8645 if (BUFFER_POS_REACHED_P ())
8647 result = MOVE_POS_MATCH_OR_ZV;
8648 break;
8651 else
8653 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8655 if (IT_DISPLAYING_WHITESPACE (it))
8656 may_wrap = 1;
8657 else if (may_wrap)
8659 /* We have reached a glyph that follows one or more
8660 whitespace characters. If the position is
8661 already found, we are done. */
8662 if (atpos_it.sp >= 0)
8664 RESTORE_IT (it, &atpos_it, atpos_data);
8665 result = MOVE_POS_MATCH_OR_ZV;
8666 goto done;
8668 if (atx_it.sp >= 0)
8670 RESTORE_IT (it, &atx_it, atx_data);
8671 result = MOVE_X_REACHED;
8672 goto done;
8674 /* Otherwise, we can wrap here. */
8675 SAVE_IT (wrap_it, *it, wrap_data);
8676 may_wrap = 0;
8681 /* Remember the line height for the current line, in case
8682 the next element doesn't fit on the line. */
8683 ascent = it->max_ascent;
8684 descent = it->max_descent;
8686 /* The call to produce_glyphs will get the metrics of the
8687 display element IT is loaded with. Record the x-position
8688 before this display element, in case it doesn't fit on the
8689 line. */
8690 x = it->current_x;
8692 PRODUCE_GLYPHS (it);
8694 if (it->area != TEXT_AREA)
8696 prev_method = it->method;
8697 if (it->method == GET_FROM_BUFFER)
8698 prev_pos = IT_CHARPOS (*it);
8699 set_iterator_to_next (it, 1);
8700 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8701 SET_TEXT_POS (this_line_min_pos,
8702 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8703 if (it->bidi_p
8704 && (op & MOVE_TO_POS)
8705 && IT_CHARPOS (*it) > to_charpos
8706 && IT_CHARPOS (*it) < closest_pos)
8707 closest_pos = IT_CHARPOS (*it);
8708 continue;
8711 /* The number of glyphs we get back in IT->nglyphs will normally
8712 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8713 character on a terminal frame, or (iii) a line end. For the
8714 second case, IT->nglyphs - 1 padding glyphs will be present.
8715 (On X frames, there is only one glyph produced for a
8716 composite character.)
8718 The behavior implemented below means, for continuation lines,
8719 that as many spaces of a TAB as fit on the current line are
8720 displayed there. For terminal frames, as many glyphs of a
8721 multi-glyph character are displayed in the current line, too.
8722 This is what the old redisplay code did, and we keep it that
8723 way. Under X, the whole shape of a complex character must
8724 fit on the line or it will be completely displayed in the
8725 next line.
8727 Note that both for tabs and padding glyphs, all glyphs have
8728 the same width. */
8729 if (it->nglyphs)
8731 /* More than one glyph or glyph doesn't fit on line. All
8732 glyphs have the same width. */
8733 int single_glyph_width = it->pixel_width / it->nglyphs;
8734 int new_x;
8735 int x_before_this_char = x;
8736 int hpos_before_this_char = it->hpos;
8738 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8740 new_x = x + single_glyph_width;
8742 /* We want to leave anything reaching TO_X to the caller. */
8743 if ((op & MOVE_TO_X) && new_x > to_x)
8745 if (BUFFER_POS_REACHED_P ())
8747 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8748 goto buffer_pos_reached;
8749 if (atpos_it.sp < 0)
8751 SAVE_IT (atpos_it, *it, atpos_data);
8752 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8755 else
8757 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8759 it->current_x = x;
8760 result = MOVE_X_REACHED;
8761 break;
8763 if (atx_it.sp < 0)
8765 SAVE_IT (atx_it, *it, atx_data);
8766 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8771 if (/* Lines are continued. */
8772 it->line_wrap != TRUNCATE
8773 && (/* And glyph doesn't fit on the line. */
8774 new_x > it->last_visible_x
8775 /* Or it fits exactly and we're on a window
8776 system frame. */
8777 || (new_x == it->last_visible_x
8778 && FRAME_WINDOW_P (it->f)
8779 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8780 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8781 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8783 if (/* IT->hpos == 0 means the very first glyph
8784 doesn't fit on the line, e.g. a wide image. */
8785 it->hpos == 0
8786 || (new_x == it->last_visible_x
8787 && FRAME_WINDOW_P (it->f)
8788 /* When word-wrap is ON and we have a valid
8789 wrap point, we don't allow the last glyph
8790 to "just barely fit" on the line. */
8791 && (it->line_wrap != WORD_WRAP
8792 || wrap_it.sp < 0)))
8794 ++it->hpos;
8795 it->current_x = new_x;
8797 /* The character's last glyph just barely fits
8798 in this row. */
8799 if (i == it->nglyphs - 1)
8801 /* If this is the destination position,
8802 return a position *before* it in this row,
8803 now that we know it fits in this row. */
8804 if (BUFFER_POS_REACHED_P ())
8806 if (it->line_wrap != WORD_WRAP
8807 || wrap_it.sp < 0)
8809 it->hpos = hpos_before_this_char;
8810 it->current_x = x_before_this_char;
8811 result = MOVE_POS_MATCH_OR_ZV;
8812 break;
8814 if (it->line_wrap == WORD_WRAP
8815 && atpos_it.sp < 0)
8817 SAVE_IT (atpos_it, *it, atpos_data);
8818 atpos_it.current_x = x_before_this_char;
8819 atpos_it.hpos = hpos_before_this_char;
8823 prev_method = it->method;
8824 if (it->method == GET_FROM_BUFFER)
8825 prev_pos = IT_CHARPOS (*it);
8826 set_iterator_to_next (it, 1);
8827 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8828 SET_TEXT_POS (this_line_min_pos,
8829 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8830 /* On graphical terminals, newlines may
8831 "overflow" into the fringe if
8832 overflow-newline-into-fringe is non-nil.
8833 On text terminals, and on graphical
8834 terminals with no right margin, newlines
8835 may overflow into the last glyph on the
8836 display line.*/
8837 if (!FRAME_WINDOW_P (it->f)
8838 || ((it->bidi_p
8839 && it->bidi_it.paragraph_dir == R2L)
8840 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8841 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8842 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8844 if (!get_next_display_element (it))
8846 result = MOVE_POS_MATCH_OR_ZV;
8847 break;
8849 if (BUFFER_POS_REACHED_P ())
8851 if (ITERATOR_AT_END_OF_LINE_P (it))
8852 result = MOVE_POS_MATCH_OR_ZV;
8853 else
8854 result = MOVE_LINE_CONTINUED;
8855 break;
8857 if (ITERATOR_AT_END_OF_LINE_P (it)
8858 && (it->line_wrap != WORD_WRAP
8859 || wrap_it.sp < 0))
8861 result = MOVE_NEWLINE_OR_CR;
8862 break;
8867 else
8868 IT_RESET_X_ASCENT_DESCENT (it);
8870 if (wrap_it.sp >= 0)
8872 RESTORE_IT (it, &wrap_it, wrap_data);
8873 atpos_it.sp = -1;
8874 atx_it.sp = -1;
8877 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8878 IT_CHARPOS (*it)));
8879 result = MOVE_LINE_CONTINUED;
8880 break;
8883 if (BUFFER_POS_REACHED_P ())
8885 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8886 goto buffer_pos_reached;
8887 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8889 SAVE_IT (atpos_it, *it, atpos_data);
8890 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8894 if (new_x > it->first_visible_x)
8896 /* Glyph is visible. Increment number of glyphs that
8897 would be displayed. */
8898 ++it->hpos;
8902 if (result != MOVE_UNDEFINED)
8903 break;
8905 else if (BUFFER_POS_REACHED_P ())
8907 buffer_pos_reached:
8908 IT_RESET_X_ASCENT_DESCENT (it);
8909 result = MOVE_POS_MATCH_OR_ZV;
8910 break;
8912 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8914 /* Stop when TO_X specified and reached. This check is
8915 necessary here because of lines consisting of a line end,
8916 only. The line end will not produce any glyphs and we
8917 would never get MOVE_X_REACHED. */
8918 eassert (it->nglyphs == 0);
8919 result = MOVE_X_REACHED;
8920 break;
8923 /* Is this a line end? If yes, we're done. */
8924 if (ITERATOR_AT_END_OF_LINE_P (it))
8926 /* If we are past TO_CHARPOS, but never saw any character
8927 positions smaller than TO_CHARPOS, return
8928 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8929 did. */
8930 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8932 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8934 if (closest_pos < ZV)
8936 RESTORE_IT (it, &ppos_it, ppos_data);
8937 /* Don't recurse if closest_pos is equal to
8938 to_charpos, since we have just tried that. */
8939 if (closest_pos != to_charpos)
8940 move_it_in_display_line_to (it, closest_pos, -1,
8941 MOVE_TO_POS);
8942 result = MOVE_POS_MATCH_OR_ZV;
8944 else
8945 goto buffer_pos_reached;
8947 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8948 && IT_CHARPOS (*it) > to_charpos)
8949 goto buffer_pos_reached;
8950 else
8951 result = MOVE_NEWLINE_OR_CR;
8953 else
8954 result = MOVE_NEWLINE_OR_CR;
8955 break;
8958 prev_method = it->method;
8959 if (it->method == GET_FROM_BUFFER)
8960 prev_pos = IT_CHARPOS (*it);
8961 /* The current display element has been consumed. Advance
8962 to the next. */
8963 set_iterator_to_next (it, 1);
8964 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8965 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8966 if (IT_CHARPOS (*it) < to_charpos)
8967 saw_smaller_pos = 1;
8968 if (it->bidi_p
8969 && (op & MOVE_TO_POS)
8970 && IT_CHARPOS (*it) >= to_charpos
8971 && IT_CHARPOS (*it) < closest_pos)
8972 closest_pos = IT_CHARPOS (*it);
8974 /* Stop if lines are truncated and IT's current x-position is
8975 past the right edge of the window now. */
8976 if (it->line_wrap == TRUNCATE
8977 && it->current_x >= it->last_visible_x)
8979 if (!FRAME_WINDOW_P (it->f)
8980 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8981 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8982 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8983 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8985 int at_eob_p = 0;
8987 if ((at_eob_p = !get_next_display_element (it))
8988 || BUFFER_POS_REACHED_P ()
8989 /* If we are past TO_CHARPOS, but never saw any
8990 character positions smaller than TO_CHARPOS,
8991 return MOVE_POS_MATCH_OR_ZV, like the
8992 unidirectional display did. */
8993 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8994 && !saw_smaller_pos
8995 && IT_CHARPOS (*it) > to_charpos))
8997 if (it->bidi_p
8998 && !BUFFER_POS_REACHED_P ()
8999 && !at_eob_p && closest_pos < ZV)
9001 RESTORE_IT (it, &ppos_it, ppos_data);
9002 if (closest_pos != to_charpos)
9003 move_it_in_display_line_to (it, closest_pos, -1,
9004 MOVE_TO_POS);
9006 result = MOVE_POS_MATCH_OR_ZV;
9007 break;
9009 if (ITERATOR_AT_END_OF_LINE_P (it))
9011 result = MOVE_NEWLINE_OR_CR;
9012 break;
9015 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9016 && !saw_smaller_pos
9017 && IT_CHARPOS (*it) > to_charpos)
9019 if (closest_pos < ZV)
9021 RESTORE_IT (it, &ppos_it, ppos_data);
9022 if (closest_pos != to_charpos)
9023 move_it_in_display_line_to (it, closest_pos, -1,
9024 MOVE_TO_POS);
9026 result = MOVE_POS_MATCH_OR_ZV;
9027 break;
9029 result = MOVE_LINE_TRUNCATED;
9030 break;
9032 #undef IT_RESET_X_ASCENT_DESCENT
9035 #undef BUFFER_POS_REACHED_P
9037 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9038 restore the saved iterator. */
9039 if (atpos_it.sp >= 0)
9040 RESTORE_IT (it, &atpos_it, atpos_data);
9041 else if (atx_it.sp >= 0)
9042 RESTORE_IT (it, &atx_it, atx_data);
9044 done:
9046 if (atpos_data)
9047 bidi_unshelve_cache (atpos_data, 1);
9048 if (atx_data)
9049 bidi_unshelve_cache (atx_data, 1);
9050 if (wrap_data)
9051 bidi_unshelve_cache (wrap_data, 1);
9052 if (ppos_data)
9053 bidi_unshelve_cache (ppos_data, 1);
9055 /* Restore the iterator settings altered at the beginning of this
9056 function. */
9057 it->glyph_row = saved_glyph_row;
9058 return result;
9061 /* For external use. */
9062 void
9063 move_it_in_display_line (struct it *it,
9064 ptrdiff_t to_charpos, int to_x,
9065 enum move_operation_enum op)
9067 if (it->line_wrap == WORD_WRAP
9068 && (op & MOVE_TO_X))
9070 struct it save_it;
9071 void *save_data = NULL;
9072 int skip;
9074 SAVE_IT (save_it, *it, save_data);
9075 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9076 /* When word-wrap is on, TO_X may lie past the end
9077 of a wrapped line. Then it->current is the
9078 character on the next line, so backtrack to the
9079 space before the wrap point. */
9080 if (skip == MOVE_LINE_CONTINUED)
9082 int prev_x = max (it->current_x - 1, 0);
9083 RESTORE_IT (it, &save_it, save_data);
9084 move_it_in_display_line_to
9085 (it, -1, prev_x, MOVE_TO_X);
9087 else
9088 bidi_unshelve_cache (save_data, 1);
9090 else
9091 move_it_in_display_line_to (it, to_charpos, to_x, op);
9095 /* Move IT forward until it satisfies one or more of the criteria in
9096 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9098 OP is a bit-mask that specifies where to stop, and in particular,
9099 which of those four position arguments makes a difference. See the
9100 description of enum move_operation_enum.
9102 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9103 screen line, this function will set IT to the next position that is
9104 displayed to the right of TO_CHARPOS on the screen.
9106 Return the maximum pixel length of any line scanned but never more
9107 than it.last_visible_x. */
9110 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9112 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9113 int line_height, line_start_x = 0, reached = 0;
9114 int max_current_x = 0;
9115 void *backup_data = NULL;
9117 for (;;)
9119 if (op & MOVE_TO_VPOS)
9121 /* If no TO_CHARPOS and no TO_X specified, stop at the
9122 start of the line TO_VPOS. */
9123 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9125 if (it->vpos == to_vpos)
9127 reached = 1;
9128 break;
9130 else
9131 skip = move_it_in_display_line_to (it, -1, -1, 0);
9133 else
9135 /* TO_VPOS >= 0 means stop at TO_X in the line at
9136 TO_VPOS, or at TO_POS, whichever comes first. */
9137 if (it->vpos == to_vpos)
9139 reached = 2;
9140 break;
9143 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9145 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9147 reached = 3;
9148 break;
9150 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9152 /* We have reached TO_X but not in the line we want. */
9153 skip = move_it_in_display_line_to (it, to_charpos,
9154 -1, MOVE_TO_POS);
9155 if (skip == MOVE_POS_MATCH_OR_ZV)
9157 reached = 4;
9158 break;
9163 else if (op & MOVE_TO_Y)
9165 struct it it_backup;
9167 if (it->line_wrap == WORD_WRAP)
9168 SAVE_IT (it_backup, *it, backup_data);
9170 /* TO_Y specified means stop at TO_X in the line containing
9171 TO_Y---or at TO_CHARPOS if this is reached first. The
9172 problem is that we can't really tell whether the line
9173 contains TO_Y before we have completely scanned it, and
9174 this may skip past TO_X. What we do is to first scan to
9175 TO_X.
9177 If TO_X is not specified, use a TO_X of zero. The reason
9178 is to make the outcome of this function more predictable.
9179 If we didn't use TO_X == 0, we would stop at the end of
9180 the line which is probably not what a caller would expect
9181 to happen. */
9182 skip = move_it_in_display_line_to
9183 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9184 (MOVE_TO_X | (op & MOVE_TO_POS)));
9186 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9187 if (skip == MOVE_POS_MATCH_OR_ZV)
9188 reached = 5;
9189 else if (skip == MOVE_X_REACHED)
9191 /* If TO_X was reached, we want to know whether TO_Y is
9192 in the line. We know this is the case if the already
9193 scanned glyphs make the line tall enough. Otherwise,
9194 we must check by scanning the rest of the line. */
9195 line_height = it->max_ascent + it->max_descent;
9196 if (to_y >= it->current_y
9197 && to_y < it->current_y + line_height)
9199 reached = 6;
9200 break;
9202 SAVE_IT (it_backup, *it, backup_data);
9203 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9204 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9205 op & MOVE_TO_POS);
9206 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9207 line_height = it->max_ascent + it->max_descent;
9208 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9210 if (to_y >= it->current_y
9211 && to_y < it->current_y + line_height)
9213 /* If TO_Y is in this line and TO_X was reached
9214 above, we scanned too far. We have to restore
9215 IT's settings to the ones before skipping. But
9216 keep the more accurate values of max_ascent and
9217 max_descent we've found while skipping the rest
9218 of the line, for the sake of callers, such as
9219 pos_visible_p, that need to know the line
9220 height. */
9221 int max_ascent = it->max_ascent;
9222 int max_descent = it->max_descent;
9224 RESTORE_IT (it, &it_backup, backup_data);
9225 it->max_ascent = max_ascent;
9226 it->max_descent = max_descent;
9227 reached = 6;
9229 else
9231 skip = skip2;
9232 if (skip == MOVE_POS_MATCH_OR_ZV)
9233 reached = 7;
9236 else
9238 /* Check whether TO_Y is in this line. */
9239 line_height = it->max_ascent + it->max_descent;
9240 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9242 if (to_y >= it->current_y
9243 && to_y < it->current_y + line_height)
9245 if (to_y > it->current_y)
9246 max_current_x = max (it->current_x, max_current_x);
9248 /* When word-wrap is on, TO_X may lie past the end
9249 of a wrapped line. Then it->current is the
9250 character on the next line, so backtrack to the
9251 space before the wrap point. */
9252 if (skip == MOVE_LINE_CONTINUED
9253 && it->line_wrap == WORD_WRAP)
9255 int prev_x = max (it->current_x - 1, 0);
9256 RESTORE_IT (it, &it_backup, backup_data);
9257 skip = move_it_in_display_line_to
9258 (it, -1, prev_x, MOVE_TO_X);
9261 reached = 6;
9265 if (reached)
9267 max_current_x = max (it->current_x, max_current_x);
9268 break;
9271 else if (BUFFERP (it->object)
9272 && (it->method == GET_FROM_BUFFER
9273 || it->method == GET_FROM_STRETCH)
9274 && IT_CHARPOS (*it) >= to_charpos
9275 /* Under bidi iteration, a call to set_iterator_to_next
9276 can scan far beyond to_charpos if the initial
9277 portion of the next line needs to be reordered. In
9278 that case, give move_it_in_display_line_to another
9279 chance below. */
9280 && !(it->bidi_p
9281 && it->bidi_it.scan_dir == -1))
9282 skip = MOVE_POS_MATCH_OR_ZV;
9283 else
9284 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9286 switch (skip)
9288 case MOVE_POS_MATCH_OR_ZV:
9289 max_current_x = max (it->current_x, max_current_x);
9290 reached = 8;
9291 goto out;
9293 case MOVE_NEWLINE_OR_CR:
9294 max_current_x = max (it->current_x, max_current_x);
9295 set_iterator_to_next (it, 1);
9296 it->continuation_lines_width = 0;
9297 break;
9299 case MOVE_LINE_TRUNCATED:
9300 max_current_x = it->last_visible_x;
9301 it->continuation_lines_width = 0;
9302 reseat_at_next_visible_line_start (it, 0);
9303 if ((op & MOVE_TO_POS) != 0
9304 && IT_CHARPOS (*it) > to_charpos)
9306 reached = 9;
9307 goto out;
9309 break;
9311 case MOVE_LINE_CONTINUED:
9312 max_current_x = it->last_visible_x;
9313 /* For continued lines ending in a tab, some of the glyphs
9314 associated with the tab are displayed on the current
9315 line. Since it->current_x does not include these glyphs,
9316 we use it->last_visible_x instead. */
9317 if (it->c == '\t')
9319 it->continuation_lines_width += it->last_visible_x;
9320 /* When moving by vpos, ensure that the iterator really
9321 advances to the next line (bug#847, bug#969). Fixme:
9322 do we need to do this in other circumstances? */
9323 if (it->current_x != it->last_visible_x
9324 && (op & MOVE_TO_VPOS)
9325 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9327 line_start_x = it->current_x + it->pixel_width
9328 - it->last_visible_x;
9329 if (FRAME_WINDOW_P (it->f))
9331 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9332 struct font *face_font = face->font;
9334 /* When display_line produces a continued line
9335 that ends in a TAB, it skips a tab stop that
9336 is closer than the font's space character
9337 width (see x_produce_glyphs where it produces
9338 the stretch glyph which represents a TAB).
9339 We need to reproduce the same logic here. */
9340 eassert (face_font);
9341 if (face_font)
9343 if (line_start_x < face_font->space_width)
9344 line_start_x
9345 += it->tab_width * face_font->space_width;
9348 set_iterator_to_next (it, 0);
9351 else
9352 it->continuation_lines_width += it->current_x;
9353 break;
9355 default:
9356 emacs_abort ();
9359 /* Reset/increment for the next run. */
9360 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9361 it->current_x = line_start_x;
9362 line_start_x = 0;
9363 it->hpos = 0;
9364 it->current_y += it->max_ascent + it->max_descent;
9365 ++it->vpos;
9366 last_height = it->max_ascent + it->max_descent;
9367 it->max_ascent = it->max_descent = 0;
9370 out:
9372 /* On text terminals, we may stop at the end of a line in the middle
9373 of a multi-character glyph. If the glyph itself is continued,
9374 i.e. it is actually displayed on the next line, don't treat this
9375 stopping point as valid; move to the next line instead (unless
9376 that brings us offscreen). */
9377 if (!FRAME_WINDOW_P (it->f)
9378 && op & MOVE_TO_POS
9379 && IT_CHARPOS (*it) == to_charpos
9380 && it->what == IT_CHARACTER
9381 && it->nglyphs > 1
9382 && it->line_wrap == WINDOW_WRAP
9383 && it->current_x == it->last_visible_x - 1
9384 && it->c != '\n'
9385 && it->c != '\t'
9386 && it->vpos < it->w->window_end_vpos)
9388 it->continuation_lines_width += it->current_x;
9389 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9390 it->current_y += it->max_ascent + it->max_descent;
9391 ++it->vpos;
9392 last_height = it->max_ascent + it->max_descent;
9395 if (backup_data)
9396 bidi_unshelve_cache (backup_data, 1);
9398 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9400 return max_current_x;
9404 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9406 If DY > 0, move IT backward at least that many pixels. DY = 0
9407 means move IT backward to the preceding line start or BEGV. This
9408 function may move over more than DY pixels if IT->current_y - DY
9409 ends up in the middle of a line; in this case IT->current_y will be
9410 set to the top of the line moved to. */
9412 void
9413 move_it_vertically_backward (struct it *it, int dy)
9415 int nlines, h;
9416 struct it it2, it3;
9417 void *it2data = NULL, *it3data = NULL;
9418 ptrdiff_t start_pos;
9419 int nchars_per_row
9420 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9421 ptrdiff_t pos_limit;
9423 move_further_back:
9424 eassert (dy >= 0);
9426 start_pos = IT_CHARPOS (*it);
9428 /* Estimate how many newlines we must move back. */
9429 nlines = max (1, dy / default_line_pixel_height (it->w));
9430 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9431 pos_limit = BEGV;
9432 else
9433 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9435 /* Set the iterator's position that many lines back. But don't go
9436 back more than NLINES full screen lines -- this wins a day with
9437 buffers which have very long lines. */
9438 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9439 back_to_previous_visible_line_start (it);
9441 /* Reseat the iterator here. When moving backward, we don't want
9442 reseat to skip forward over invisible text, set up the iterator
9443 to deliver from overlay strings at the new position etc. So,
9444 use reseat_1 here. */
9445 reseat_1 (it, it->current.pos, 1);
9447 /* We are now surely at a line start. */
9448 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9449 reordering is in effect. */
9450 it->continuation_lines_width = 0;
9452 /* Move forward and see what y-distance we moved. First move to the
9453 start of the next line so that we get its height. We need this
9454 height to be able to tell whether we reached the specified
9455 y-distance. */
9456 SAVE_IT (it2, *it, it2data);
9457 it2.max_ascent = it2.max_descent = 0;
9460 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9461 MOVE_TO_POS | MOVE_TO_VPOS);
9463 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9464 /* If we are in a display string which starts at START_POS,
9465 and that display string includes a newline, and we are
9466 right after that newline (i.e. at the beginning of a
9467 display line), exit the loop, because otherwise we will
9468 infloop, since move_it_to will see that it is already at
9469 START_POS and will not move. */
9470 || (it2.method == GET_FROM_STRING
9471 && IT_CHARPOS (it2) == start_pos
9472 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9473 eassert (IT_CHARPOS (*it) >= BEGV);
9474 SAVE_IT (it3, it2, it3data);
9476 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9477 eassert (IT_CHARPOS (*it) >= BEGV);
9478 /* H is the actual vertical distance from the position in *IT
9479 and the starting position. */
9480 h = it2.current_y - it->current_y;
9481 /* NLINES is the distance in number of lines. */
9482 nlines = it2.vpos - it->vpos;
9484 /* Correct IT's y and vpos position
9485 so that they are relative to the starting point. */
9486 it->vpos -= nlines;
9487 it->current_y -= h;
9489 if (dy == 0)
9491 /* DY == 0 means move to the start of the screen line. The
9492 value of nlines is > 0 if continuation lines were involved,
9493 or if the original IT position was at start of a line. */
9494 RESTORE_IT (it, it, it2data);
9495 if (nlines > 0)
9496 move_it_by_lines (it, nlines);
9497 /* The above code moves us to some position NLINES down,
9498 usually to its first glyph (leftmost in an L2R line), but
9499 that's not necessarily the start of the line, under bidi
9500 reordering. We want to get to the character position
9501 that is immediately after the newline of the previous
9502 line. */
9503 if (it->bidi_p
9504 && !it->continuation_lines_width
9505 && !STRINGP (it->string)
9506 && IT_CHARPOS (*it) > BEGV
9507 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9509 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9511 DEC_BOTH (cp, bp);
9512 cp = find_newline_no_quit (cp, bp, -1, NULL);
9513 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9515 bidi_unshelve_cache (it3data, 1);
9517 else
9519 /* The y-position we try to reach, relative to *IT.
9520 Note that H has been subtracted in front of the if-statement. */
9521 int target_y = it->current_y + h - dy;
9522 int y0 = it3.current_y;
9523 int y1;
9524 int line_height;
9526 RESTORE_IT (&it3, &it3, it3data);
9527 y1 = line_bottom_y (&it3);
9528 line_height = y1 - y0;
9529 RESTORE_IT (it, it, it2data);
9530 /* If we did not reach target_y, try to move further backward if
9531 we can. If we moved too far backward, try to move forward. */
9532 if (target_y < it->current_y
9533 /* This is heuristic. In a window that's 3 lines high, with
9534 a line height of 13 pixels each, recentering with point
9535 on the bottom line will try to move -39/2 = 19 pixels
9536 backward. Try to avoid moving into the first line. */
9537 && (it->current_y - target_y
9538 > min (window_box_height (it->w), line_height * 2 / 3))
9539 && IT_CHARPOS (*it) > BEGV)
9541 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9542 target_y - it->current_y));
9543 dy = it->current_y - target_y;
9544 goto move_further_back;
9546 else if (target_y >= it->current_y + line_height
9547 && IT_CHARPOS (*it) < ZV)
9549 /* Should move forward by at least one line, maybe more.
9551 Note: Calling move_it_by_lines can be expensive on
9552 terminal frames, where compute_motion is used (via
9553 vmotion) to do the job, when there are very long lines
9554 and truncate-lines is nil. That's the reason for
9555 treating terminal frames specially here. */
9557 if (!FRAME_WINDOW_P (it->f))
9558 move_it_vertically (it, target_y - (it->current_y + line_height));
9559 else
9563 move_it_by_lines (it, 1);
9565 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9572 /* Move IT by a specified amount of pixel lines DY. DY negative means
9573 move backwards. DY = 0 means move to start of screen line. At the
9574 end, IT will be on the start of a screen line. */
9576 void
9577 move_it_vertically (struct it *it, int dy)
9579 if (dy <= 0)
9580 move_it_vertically_backward (it, -dy);
9581 else
9583 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9584 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9585 MOVE_TO_POS | MOVE_TO_Y);
9586 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9588 /* If buffer ends in ZV without a newline, move to the start of
9589 the line to satisfy the post-condition. */
9590 if (IT_CHARPOS (*it) == ZV
9591 && ZV > BEGV
9592 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9593 move_it_by_lines (it, 0);
9598 /* Move iterator IT past the end of the text line it is in. */
9600 void
9601 move_it_past_eol (struct it *it)
9603 enum move_it_result rc;
9605 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9606 if (rc == MOVE_NEWLINE_OR_CR)
9607 set_iterator_to_next (it, 0);
9611 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9612 negative means move up. DVPOS == 0 means move to the start of the
9613 screen line.
9615 Optimization idea: If we would know that IT->f doesn't use
9616 a face with proportional font, we could be faster for
9617 truncate-lines nil. */
9619 void
9620 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9623 /* The commented-out optimization uses vmotion on terminals. This
9624 gives bad results, because elements like it->what, on which
9625 callers such as pos_visible_p rely, aren't updated. */
9626 /* struct position pos;
9627 if (!FRAME_WINDOW_P (it->f))
9629 struct text_pos textpos;
9631 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9632 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9633 reseat (it, textpos, 1);
9634 it->vpos += pos.vpos;
9635 it->current_y += pos.vpos;
9637 else */
9639 if (dvpos == 0)
9641 /* DVPOS == 0 means move to the start of the screen line. */
9642 move_it_vertically_backward (it, 0);
9643 /* Let next call to line_bottom_y calculate real line height. */
9644 last_height = 0;
9646 else if (dvpos > 0)
9648 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9649 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9651 /* Only move to the next buffer position if we ended up in a
9652 string from display property, not in an overlay string
9653 (before-string or after-string). That is because the
9654 latter don't conceal the underlying buffer position, so
9655 we can ask to move the iterator to the exact position we
9656 are interested in. Note that, even if we are already at
9657 IT_CHARPOS (*it), the call below is not a no-op, as it
9658 will detect that we are at the end of the string, pop the
9659 iterator, and compute it->current_x and it->hpos
9660 correctly. */
9661 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9662 -1, -1, -1, MOVE_TO_POS);
9665 else
9667 struct it it2;
9668 void *it2data = NULL;
9669 ptrdiff_t start_charpos, i;
9670 int nchars_per_row
9671 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9672 bool hit_pos_limit = false;
9673 ptrdiff_t pos_limit;
9675 /* Start at the beginning of the screen line containing IT's
9676 position. This may actually move vertically backwards,
9677 in case of overlays, so adjust dvpos accordingly. */
9678 dvpos += it->vpos;
9679 move_it_vertically_backward (it, 0);
9680 dvpos -= it->vpos;
9682 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9683 screen lines, and reseat the iterator there. */
9684 start_charpos = IT_CHARPOS (*it);
9685 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9686 pos_limit = BEGV;
9687 else
9688 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9690 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9691 back_to_previous_visible_line_start (it);
9692 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9693 hit_pos_limit = true;
9694 reseat (it, it->current.pos, 1);
9696 /* Move further back if we end up in a string or an image. */
9697 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9699 /* First try to move to start of display line. */
9700 dvpos += it->vpos;
9701 move_it_vertically_backward (it, 0);
9702 dvpos -= it->vpos;
9703 if (IT_POS_VALID_AFTER_MOVE_P (it))
9704 break;
9705 /* If start of line is still in string or image,
9706 move further back. */
9707 back_to_previous_visible_line_start (it);
9708 reseat (it, it->current.pos, 1);
9709 dvpos--;
9712 it->current_x = it->hpos = 0;
9714 /* Above call may have moved too far if continuation lines
9715 are involved. Scan forward and see if it did. */
9716 SAVE_IT (it2, *it, it2data);
9717 it2.vpos = it2.current_y = 0;
9718 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9719 it->vpos -= it2.vpos;
9720 it->current_y -= it2.current_y;
9721 it->current_x = it->hpos = 0;
9723 /* If we moved too far back, move IT some lines forward. */
9724 if (it2.vpos > -dvpos)
9726 int delta = it2.vpos + dvpos;
9728 RESTORE_IT (&it2, &it2, it2data);
9729 SAVE_IT (it2, *it, it2data);
9730 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9731 /* Move back again if we got too far ahead. */
9732 if (IT_CHARPOS (*it) >= start_charpos)
9733 RESTORE_IT (it, &it2, it2data);
9734 else
9735 bidi_unshelve_cache (it2data, 1);
9737 else if (hit_pos_limit && pos_limit > BEGV
9738 && dvpos < 0 && it2.vpos < -dvpos)
9740 /* If we hit the limit, but still didn't make it far enough
9741 back, that means there's a display string with a newline
9742 covering a large chunk of text, and that caused
9743 back_to_previous_visible_line_start try to go too far.
9744 Punish those who commit such atrocities by going back
9745 until we've reached DVPOS, after lifting the limit, which
9746 could make it slow for very long lines. "If it hurts,
9747 don't do that!" */
9748 dvpos += it2.vpos;
9749 RESTORE_IT (it, it, it2data);
9750 for (i = -dvpos; i > 0; --i)
9752 back_to_previous_visible_line_start (it);
9753 it->vpos--;
9755 reseat_1 (it, it->current.pos, 1);
9757 else
9758 RESTORE_IT (it, it, it2data);
9762 /* Return true if IT points into the middle of a display vector. */
9764 bool
9765 in_display_vector_p (struct it *it)
9767 return (it->method == GET_FROM_DISPLAY_VECTOR
9768 && it->current.dpvec_index > 0
9769 && it->dpvec + it->current.dpvec_index != it->dpend);
9772 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9773 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9774 WINDOW must be a live window and defaults to the selected one. The
9775 return value is a cons of the maximum pixel-width of any text line and
9776 the maximum pixel-height of all text lines.
9778 The optional argument FROM, if non-nil, specifies the first text
9779 position and defaults to the minimum accessible position of the buffer.
9780 If FROM is t, use the minimum accessible position that is not a newline
9781 character. TO, if non-nil, specifies the last text position and
9782 defaults to the maximum accessible position of the buffer. If TO is t,
9783 use the maximum accessible position that is not a newline character.
9785 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9786 width that can be returned. X-LIMIT nil or omitted, means to use the
9787 pixel-width of WINDOW's body; use this if you do not intend to change
9788 the width of WINDOW. Use the maximum width WINDOW may assume if you
9789 intend to change WINDOW's width. In any case, text whose x-coordinate
9790 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9791 can take some time, it's always a good idea to make this argument as
9792 small as possible; in particular, if the buffer contains long lines that
9793 shall be truncated anyway.
9795 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9796 height that can be returned. Text lines whose y-coordinate is beyond
9797 Y-LIMIT are ignored. Since calculating the text height of a large
9798 buffer can take some time, it makes sense to specify this argument if
9799 the size of the buffer is unknown.
9801 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9802 include the height of the mode- or header-line of WINDOW in the return
9803 value. If it is either the symbol `mode-line' or `header-line', include
9804 only the height of that line, if present, in the return value. If t,
9805 include the height of both, if present, in the return value. */)
9806 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9807 Lisp_Object mode_and_header_line)
9809 struct window *w = decode_live_window (window);
9810 Lisp_Object buf;
9811 struct buffer *b;
9812 struct it it;
9813 struct buffer *old_buffer = NULL;
9814 ptrdiff_t start, end, pos;
9815 struct text_pos startp;
9816 void *itdata = NULL;
9817 int c, max_y = -1, x = 0, y = 0;
9819 buf = w->contents;
9820 CHECK_BUFFER (buf);
9821 b = XBUFFER (buf);
9823 if (b != current_buffer)
9825 old_buffer = current_buffer;
9826 set_buffer_internal (b);
9829 if (NILP (from))
9830 start = BEGV;
9831 else if (EQ (from, Qt))
9833 start = pos = BEGV;
9834 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9835 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9836 start = pos;
9837 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9838 start = pos;
9840 else
9842 CHECK_NUMBER_COERCE_MARKER (from);
9843 start = min (max (XINT (from), BEGV), ZV);
9846 if (NILP (to))
9847 end = ZV;
9848 else if (EQ (to, Qt))
9850 end = pos = ZV;
9851 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9852 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9853 end = pos;
9854 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9855 end = pos;
9857 else
9859 CHECK_NUMBER_COERCE_MARKER (to);
9860 end = max (start, min (XINT (to), ZV));
9863 if (!NILP (y_limit))
9865 CHECK_NUMBER (y_limit);
9866 max_y = min (XINT (y_limit), INT_MAX);
9869 itdata = bidi_shelve_cache ();
9870 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9871 start_display (&it, w, startp);
9873 if (NILP (x_limit))
9874 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9875 else
9877 CHECK_NUMBER (x_limit);
9878 it.last_visible_x = min (XINT (x_limit), INFINITY);
9879 /* Actually, we never want move_it_to stop at to_x. But to make
9880 sure that move_it_in_display_line_to always moves far enough,
9881 we set it to INT_MAX and specify MOVE_TO_X. */
9882 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9883 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9886 y = it.current_y + it.max_ascent + it.max_descent;
9888 if (!EQ (mode_and_header_line, Qheader_line)
9889 && !EQ (mode_and_header_line, Qt))
9890 /* Do not count the header-line which was counted automatically by
9891 start_display. */
9892 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9894 if (EQ (mode_and_header_line, Qmode_line)
9895 || EQ (mode_and_header_line, Qt))
9896 /* Do count the mode-line which is not included automatically by
9897 start_display. */
9898 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9900 bidi_unshelve_cache (itdata, 0);
9902 if (old_buffer)
9903 set_buffer_internal (old_buffer);
9905 return Fcons (make_number (x), make_number (y));
9908 /***********************************************************************
9909 Messages
9910 ***********************************************************************/
9913 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9914 to *Messages*. */
9916 void
9917 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9919 Lisp_Object args[3];
9920 Lisp_Object msg, fmt;
9921 char *buffer;
9922 ptrdiff_t len;
9923 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9924 USE_SAFE_ALLOCA;
9926 fmt = msg = Qnil;
9927 GCPRO4 (fmt, msg, arg1, arg2);
9929 args[0] = fmt = build_string (format);
9930 args[1] = arg1;
9931 args[2] = arg2;
9932 msg = Fformat (3, args);
9934 len = SBYTES (msg) + 1;
9935 buffer = SAFE_ALLOCA (len);
9936 memcpy (buffer, SDATA (msg), len);
9938 message_dolog (buffer, len - 1, 1, 0);
9939 SAFE_FREE ();
9941 UNGCPRO;
9945 /* Output a newline in the *Messages* buffer if "needs" one. */
9947 void
9948 message_log_maybe_newline (void)
9950 if (message_log_need_newline)
9951 message_dolog ("", 0, 1, 0);
9955 /* Add a string M of length NBYTES to the message log, optionally
9956 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9957 true, means interpret the contents of M as multibyte. This
9958 function calls low-level routines in order to bypass text property
9959 hooks, etc. which might not be safe to run.
9961 This may GC (insert may run before/after change hooks),
9962 so the buffer M must NOT point to a Lisp string. */
9964 void
9965 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9967 const unsigned char *msg = (const unsigned char *) m;
9969 if (!NILP (Vmemory_full))
9970 return;
9972 if (!NILP (Vmessage_log_max))
9974 struct buffer *oldbuf;
9975 Lisp_Object oldpoint, oldbegv, oldzv;
9976 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9977 ptrdiff_t point_at_end = 0;
9978 ptrdiff_t zv_at_end = 0;
9979 Lisp_Object old_deactivate_mark;
9980 struct gcpro gcpro1;
9982 old_deactivate_mark = Vdeactivate_mark;
9983 oldbuf = current_buffer;
9985 /* Ensure the Messages buffer exists, and switch to it.
9986 If we created it, set the major-mode. */
9988 int newbuffer = 0;
9989 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
9991 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9993 if (newbuffer
9994 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
9995 call0 (intern ("messages-buffer-mode"));
9998 bset_undo_list (current_buffer, Qt);
9999 bset_cache_long_scans (current_buffer, Qnil);
10001 oldpoint = message_dolog_marker1;
10002 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10003 oldbegv = message_dolog_marker2;
10004 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10005 oldzv = message_dolog_marker3;
10006 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10007 GCPRO1 (old_deactivate_mark);
10009 if (PT == Z)
10010 point_at_end = 1;
10011 if (ZV == Z)
10012 zv_at_end = 1;
10014 BEGV = BEG;
10015 BEGV_BYTE = BEG_BYTE;
10016 ZV = Z;
10017 ZV_BYTE = Z_BYTE;
10018 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10020 /* Insert the string--maybe converting multibyte to single byte
10021 or vice versa, so that all the text fits the buffer. */
10022 if (multibyte
10023 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10025 ptrdiff_t i;
10026 int c, char_bytes;
10027 char work[1];
10029 /* Convert a multibyte string to single-byte
10030 for the *Message* buffer. */
10031 for (i = 0; i < nbytes; i += char_bytes)
10033 c = string_char_and_length (msg + i, &char_bytes);
10034 work[0] = CHAR_TO_BYTE8 (c);
10035 insert_1_both (work, 1, 1, 1, 0, 0);
10038 else if (! multibyte
10039 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10041 ptrdiff_t i;
10042 int c, char_bytes;
10043 unsigned char str[MAX_MULTIBYTE_LENGTH];
10044 /* Convert a single-byte string to multibyte
10045 for the *Message* buffer. */
10046 for (i = 0; i < nbytes; i++)
10048 c = msg[i];
10049 MAKE_CHAR_MULTIBYTE (c);
10050 char_bytes = CHAR_STRING (c, str);
10051 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
10054 else if (nbytes)
10055 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
10057 if (nlflag)
10059 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10060 printmax_t dups;
10062 insert_1_both ("\n", 1, 1, 1, 0, 0);
10064 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
10065 this_bol = PT;
10066 this_bol_byte = PT_BYTE;
10068 /* See if this line duplicates the previous one.
10069 If so, combine duplicates. */
10070 if (this_bol > BEG)
10072 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
10073 prev_bol = PT;
10074 prev_bol_byte = PT_BYTE;
10076 dups = message_log_check_duplicate (prev_bol_byte,
10077 this_bol_byte);
10078 if (dups)
10080 del_range_both (prev_bol, prev_bol_byte,
10081 this_bol, this_bol_byte, 0);
10082 if (dups > 1)
10084 char dupstr[sizeof " [ times]"
10085 + INT_STRLEN_BOUND (printmax_t)];
10087 /* If you change this format, don't forget to also
10088 change message_log_check_duplicate. */
10089 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10090 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10091 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
10096 /* If we have more than the desired maximum number of lines
10097 in the *Messages* buffer now, delete the oldest ones.
10098 This is safe because we don't have undo in this buffer. */
10100 if (NATNUMP (Vmessage_log_max))
10102 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10103 -XFASTINT (Vmessage_log_max) - 1, 0);
10104 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
10107 BEGV = marker_position (oldbegv);
10108 BEGV_BYTE = marker_byte_position (oldbegv);
10110 if (zv_at_end)
10112 ZV = Z;
10113 ZV_BYTE = Z_BYTE;
10115 else
10117 ZV = marker_position (oldzv);
10118 ZV_BYTE = marker_byte_position (oldzv);
10121 if (point_at_end)
10122 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10123 else
10124 /* We can't do Fgoto_char (oldpoint) because it will run some
10125 Lisp code. */
10126 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10127 marker_byte_position (oldpoint));
10129 UNGCPRO;
10130 unchain_marker (XMARKER (oldpoint));
10131 unchain_marker (XMARKER (oldbegv));
10132 unchain_marker (XMARKER (oldzv));
10134 /* We called insert_1_both above with its 5th argument (PREPARE)
10135 zero, which prevents insert_1_both from calling
10136 prepare_to_modify_buffer, which in turns prevents us from
10137 incrementing windows_or_buffers_changed even if *Messages* is
10138 shown in some window. So we must manually set
10139 windows_or_buffers_changed here to make up for that. */
10140 windows_or_buffers_changed = old_windows_or_buffers_changed;
10141 bset_redisplay (current_buffer);
10143 set_buffer_internal (oldbuf);
10145 message_log_need_newline = !nlflag;
10146 Vdeactivate_mark = old_deactivate_mark;
10151 /* We are at the end of the buffer after just having inserted a newline.
10152 (Note: We depend on the fact we won't be crossing the gap.)
10153 Check to see if the most recent message looks a lot like the previous one.
10154 Return 0 if different, 1 if the new one should just replace it, or a
10155 value N > 1 if we should also append " [N times]". */
10157 static intmax_t
10158 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10160 ptrdiff_t i;
10161 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10162 int seen_dots = 0;
10163 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10164 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10166 for (i = 0; i < len; i++)
10168 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10169 seen_dots = 1;
10170 if (p1[i] != p2[i])
10171 return seen_dots;
10173 p1 += len;
10174 if (*p1 == '\n')
10175 return 2;
10176 if (*p1++ == ' ' && *p1++ == '[')
10178 char *pend;
10179 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10180 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10181 return n + 1;
10183 return 0;
10187 /* Display an echo area message M with a specified length of NBYTES
10188 bytes. The string may include null characters. If M is not a
10189 string, clear out any existing message, and let the mini-buffer
10190 text show through.
10192 This function cancels echoing. */
10194 void
10195 message3 (Lisp_Object m)
10197 struct gcpro gcpro1;
10199 GCPRO1 (m);
10200 clear_message (true, true);
10201 cancel_echoing ();
10203 /* First flush out any partial line written with print. */
10204 message_log_maybe_newline ();
10205 if (STRINGP (m))
10207 ptrdiff_t nbytes = SBYTES (m);
10208 bool multibyte = STRING_MULTIBYTE (m);
10209 char *buffer;
10210 USE_SAFE_ALLOCA;
10211 SAFE_ALLOCA_STRING (buffer, m);
10212 message_dolog (buffer, nbytes, 1, multibyte);
10213 SAFE_FREE ();
10215 message3_nolog (m);
10217 UNGCPRO;
10221 /* The non-logging version of message3.
10222 This does not cancel echoing, because it is used for echoing.
10223 Perhaps we need to make a separate function for echoing
10224 and make this cancel echoing. */
10226 void
10227 message3_nolog (Lisp_Object m)
10229 struct frame *sf = SELECTED_FRAME ();
10231 if (FRAME_INITIAL_P (sf))
10233 if (noninteractive_need_newline)
10234 putc ('\n', stderr);
10235 noninteractive_need_newline = 0;
10236 if (STRINGP (m))
10238 Lisp_Object s = ENCODE_SYSTEM (m);
10240 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10242 if (cursor_in_echo_area == 0)
10243 fprintf (stderr, "\n");
10244 fflush (stderr);
10246 /* Error messages get reported properly by cmd_error, so this must be just an
10247 informative message; if the frame hasn't really been initialized yet, just
10248 toss it. */
10249 else if (INTERACTIVE && sf->glyphs_initialized_p)
10251 /* Get the frame containing the mini-buffer
10252 that the selected frame is using. */
10253 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10254 Lisp_Object frame = XWINDOW (mini_window)->frame;
10255 struct frame *f = XFRAME (frame);
10257 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10258 Fmake_frame_visible (frame);
10260 if (STRINGP (m) && SCHARS (m) > 0)
10262 set_message (m);
10263 if (minibuffer_auto_raise)
10264 Fraise_frame (frame);
10265 /* Assume we are not echoing.
10266 (If we are, echo_now will override this.) */
10267 echo_message_buffer = Qnil;
10269 else
10270 clear_message (true, true);
10272 do_pending_window_change (0);
10273 echo_area_display (1);
10274 do_pending_window_change (0);
10275 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10276 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10281 /* Display a null-terminated echo area message M. If M is 0, clear
10282 out any existing message, and let the mini-buffer text show through.
10284 The buffer M must continue to exist until after the echo area gets
10285 cleared or some other message gets displayed there. Do not pass
10286 text that is stored in a Lisp string. Do not pass text in a buffer
10287 that was alloca'd. */
10289 void
10290 message1 (const char *m)
10292 message3 (m ? build_unibyte_string (m) : Qnil);
10296 /* The non-logging counterpart of message1. */
10298 void
10299 message1_nolog (const char *m)
10301 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10304 /* Display a message M which contains a single %s
10305 which gets replaced with STRING. */
10307 void
10308 message_with_string (const char *m, Lisp_Object string, int log)
10310 CHECK_STRING (string);
10312 if (noninteractive)
10314 if (m)
10316 /* ENCODE_SYSTEM below can GC and/or relocate the
10317 Lisp data, so make sure we don't use it here. */
10318 eassert (relocatable_string_data_p (m) != 1);
10320 if (noninteractive_need_newline)
10321 putc ('\n', stderr);
10322 noninteractive_need_newline = 0;
10323 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10324 if (!cursor_in_echo_area)
10325 fprintf (stderr, "\n");
10326 fflush (stderr);
10329 else if (INTERACTIVE)
10331 /* The frame whose minibuffer we're going to display the message on.
10332 It may be larger than the selected frame, so we need
10333 to use its buffer, not the selected frame's buffer. */
10334 Lisp_Object mini_window;
10335 struct frame *f, *sf = SELECTED_FRAME ();
10337 /* Get the frame containing the minibuffer
10338 that the selected frame is using. */
10339 mini_window = FRAME_MINIBUF_WINDOW (sf);
10340 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10342 /* Error messages get reported properly by cmd_error, so this must be
10343 just an informative message; if the frame hasn't really been
10344 initialized yet, just toss it. */
10345 if (f->glyphs_initialized_p)
10347 Lisp_Object args[2], msg;
10348 struct gcpro gcpro1, gcpro2;
10350 args[0] = build_string (m);
10351 args[1] = msg = string;
10352 GCPRO2 (args[0], msg);
10353 gcpro1.nvars = 2;
10355 msg = Fformat (2, args);
10357 if (log)
10358 message3 (msg);
10359 else
10360 message3_nolog (msg);
10362 UNGCPRO;
10364 /* Print should start at the beginning of the message
10365 buffer next time. */
10366 message_buf_print = 0;
10372 /* Dump an informative message to the minibuf. If M is 0, clear out
10373 any existing message, and let the mini-buffer text show through. */
10375 static void
10376 vmessage (const char *m, va_list ap)
10378 if (noninteractive)
10380 if (m)
10382 if (noninteractive_need_newline)
10383 putc ('\n', stderr);
10384 noninteractive_need_newline = 0;
10385 vfprintf (stderr, m, ap);
10386 if (cursor_in_echo_area == 0)
10387 fprintf (stderr, "\n");
10388 fflush (stderr);
10391 else if (INTERACTIVE)
10393 /* The frame whose mini-buffer we're going to display the message
10394 on. It may be larger than the selected frame, so we need to
10395 use its buffer, not the selected frame's buffer. */
10396 Lisp_Object mini_window;
10397 struct frame *f, *sf = SELECTED_FRAME ();
10399 /* Get the frame containing the mini-buffer
10400 that the selected frame is using. */
10401 mini_window = FRAME_MINIBUF_WINDOW (sf);
10402 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10404 /* Error messages get reported properly by cmd_error, so this must be
10405 just an informative message; if the frame hasn't really been
10406 initialized yet, just toss it. */
10407 if (f->glyphs_initialized_p)
10409 if (m)
10411 ptrdiff_t len;
10412 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10413 USE_SAFE_ALLOCA;
10414 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10416 len = doprnt (message_buf, maxsize, m, 0, ap);
10418 message3 (make_string (message_buf, len));
10419 SAFE_FREE ();
10421 else
10422 message1 (0);
10424 /* Print should start at the beginning of the message
10425 buffer next time. */
10426 message_buf_print = 0;
10431 void
10432 message (const char *m, ...)
10434 va_list ap;
10435 va_start (ap, m);
10436 vmessage (m, ap);
10437 va_end (ap);
10441 #if 0
10442 /* The non-logging version of message. */
10444 void
10445 message_nolog (const char *m, ...)
10447 Lisp_Object old_log_max;
10448 va_list ap;
10449 va_start (ap, m);
10450 old_log_max = Vmessage_log_max;
10451 Vmessage_log_max = Qnil;
10452 vmessage (m, ap);
10453 Vmessage_log_max = old_log_max;
10454 va_end (ap);
10456 #endif
10459 /* Display the current message in the current mini-buffer. This is
10460 only called from error handlers in process.c, and is not time
10461 critical. */
10463 void
10464 update_echo_area (void)
10466 if (!NILP (echo_area_buffer[0]))
10468 Lisp_Object string;
10469 string = Fcurrent_message ();
10470 message3 (string);
10475 /* Make sure echo area buffers in `echo_buffers' are live.
10476 If they aren't, make new ones. */
10478 static void
10479 ensure_echo_area_buffers (void)
10481 int i;
10483 for (i = 0; i < 2; ++i)
10484 if (!BUFFERP (echo_buffer[i])
10485 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10487 char name[30];
10488 Lisp_Object old_buffer;
10489 int j;
10491 old_buffer = echo_buffer[i];
10492 echo_buffer[i] = Fget_buffer_create
10493 (make_formatted_string (name, " *Echo Area %d*", i));
10494 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10495 /* to force word wrap in echo area -
10496 it was decided to postpone this*/
10497 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10499 for (j = 0; j < 2; ++j)
10500 if (EQ (old_buffer, echo_area_buffer[j]))
10501 echo_area_buffer[j] = echo_buffer[i];
10506 /* Call FN with args A1..A2 with either the current or last displayed
10507 echo_area_buffer as current buffer.
10509 WHICH zero means use the current message buffer
10510 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10511 from echo_buffer[] and clear it.
10513 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10514 suitable buffer from echo_buffer[] and clear it.
10516 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10517 that the current message becomes the last displayed one, make
10518 choose a suitable buffer for echo_area_buffer[0], and clear it.
10520 Value is what FN returns. */
10522 static int
10523 with_echo_area_buffer (struct window *w, int which,
10524 int (*fn) (ptrdiff_t, Lisp_Object),
10525 ptrdiff_t a1, Lisp_Object a2)
10527 Lisp_Object buffer;
10528 int this_one, the_other, clear_buffer_p, rc;
10529 ptrdiff_t count = SPECPDL_INDEX ();
10531 /* If buffers aren't live, make new ones. */
10532 ensure_echo_area_buffers ();
10534 clear_buffer_p = 0;
10536 if (which == 0)
10537 this_one = 0, the_other = 1;
10538 else if (which > 0)
10539 this_one = 1, the_other = 0;
10540 else
10542 this_one = 0, the_other = 1;
10543 clear_buffer_p = true;
10545 /* We need a fresh one in case the current echo buffer equals
10546 the one containing the last displayed echo area message. */
10547 if (!NILP (echo_area_buffer[this_one])
10548 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10549 echo_area_buffer[this_one] = Qnil;
10552 /* Choose a suitable buffer from echo_buffer[] is we don't
10553 have one. */
10554 if (NILP (echo_area_buffer[this_one]))
10556 echo_area_buffer[this_one]
10557 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10558 ? echo_buffer[the_other]
10559 : echo_buffer[this_one]);
10560 clear_buffer_p = true;
10563 buffer = echo_area_buffer[this_one];
10565 /* Don't get confused by reusing the buffer used for echoing
10566 for a different purpose. */
10567 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10568 cancel_echoing ();
10570 record_unwind_protect (unwind_with_echo_area_buffer,
10571 with_echo_area_buffer_unwind_data (w));
10573 /* Make the echo area buffer current. Note that for display
10574 purposes, it is not necessary that the displayed window's buffer
10575 == current_buffer, except for text property lookup. So, let's
10576 only set that buffer temporarily here without doing a full
10577 Fset_window_buffer. We must also change w->pointm, though,
10578 because otherwise an assertions in unshow_buffer fails, and Emacs
10579 aborts. */
10580 set_buffer_internal_1 (XBUFFER (buffer));
10581 if (w)
10583 wset_buffer (w, buffer);
10584 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10585 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10588 bset_undo_list (current_buffer, Qt);
10589 bset_read_only (current_buffer, Qnil);
10590 specbind (Qinhibit_read_only, Qt);
10591 specbind (Qinhibit_modification_hooks, Qt);
10593 if (clear_buffer_p && Z > BEG)
10594 del_range (BEG, Z);
10596 eassert (BEGV >= BEG);
10597 eassert (ZV <= Z && ZV >= BEGV);
10599 rc = fn (a1, a2);
10601 eassert (BEGV >= BEG);
10602 eassert (ZV <= Z && ZV >= BEGV);
10604 unbind_to (count, Qnil);
10605 return rc;
10609 /* Save state that should be preserved around the call to the function
10610 FN called in with_echo_area_buffer. */
10612 static Lisp_Object
10613 with_echo_area_buffer_unwind_data (struct window *w)
10615 int i = 0;
10616 Lisp_Object vector, tmp;
10618 /* Reduce consing by keeping one vector in
10619 Vwith_echo_area_save_vector. */
10620 vector = Vwith_echo_area_save_vector;
10621 Vwith_echo_area_save_vector = Qnil;
10623 if (NILP (vector))
10624 vector = Fmake_vector (make_number (11), Qnil);
10626 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10627 ASET (vector, i, Vdeactivate_mark); ++i;
10628 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10630 if (w)
10632 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10633 ASET (vector, i, w->contents); ++i;
10634 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10635 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10636 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10637 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10638 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10639 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10641 else
10643 int end = i + 8;
10644 for (; i < end; ++i)
10645 ASET (vector, i, Qnil);
10648 eassert (i == ASIZE (vector));
10649 return vector;
10653 /* Restore global state from VECTOR which was created by
10654 with_echo_area_buffer_unwind_data. */
10656 static void
10657 unwind_with_echo_area_buffer (Lisp_Object vector)
10659 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10660 Vdeactivate_mark = AREF (vector, 1);
10661 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10663 if (WINDOWP (AREF (vector, 3)))
10665 struct window *w;
10666 Lisp_Object buffer;
10668 w = XWINDOW (AREF (vector, 3));
10669 buffer = AREF (vector, 4);
10671 wset_buffer (w, buffer);
10672 set_marker_both (w->pointm, buffer,
10673 XFASTINT (AREF (vector, 5)),
10674 XFASTINT (AREF (vector, 6)));
10675 set_marker_both (w->old_pointm, buffer,
10676 XFASTINT (AREF (vector, 7)),
10677 XFASTINT (AREF (vector, 8)));
10678 set_marker_both (w->start, buffer,
10679 XFASTINT (AREF (vector, 9)),
10680 XFASTINT (AREF (vector, 10)));
10683 Vwith_echo_area_save_vector = vector;
10687 /* Set up the echo area for use by print functions. MULTIBYTE_P
10688 non-zero means we will print multibyte. */
10690 void
10691 setup_echo_area_for_printing (int multibyte_p)
10693 /* If we can't find an echo area any more, exit. */
10694 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10695 Fkill_emacs (Qnil);
10697 ensure_echo_area_buffers ();
10699 if (!message_buf_print)
10701 /* A message has been output since the last time we printed.
10702 Choose a fresh echo area buffer. */
10703 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10704 echo_area_buffer[0] = echo_buffer[1];
10705 else
10706 echo_area_buffer[0] = echo_buffer[0];
10708 /* Switch to that buffer and clear it. */
10709 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10710 bset_truncate_lines (current_buffer, Qnil);
10712 if (Z > BEG)
10714 ptrdiff_t count = SPECPDL_INDEX ();
10715 specbind (Qinhibit_read_only, Qt);
10716 /* Note that undo recording is always disabled. */
10717 del_range (BEG, Z);
10718 unbind_to (count, Qnil);
10720 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10722 /* Set up the buffer for the multibyteness we need. */
10723 if (multibyte_p
10724 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10725 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10727 /* Raise the frame containing the echo area. */
10728 if (minibuffer_auto_raise)
10730 struct frame *sf = SELECTED_FRAME ();
10731 Lisp_Object mini_window;
10732 mini_window = FRAME_MINIBUF_WINDOW (sf);
10733 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10736 message_log_maybe_newline ();
10737 message_buf_print = 1;
10739 else
10741 if (NILP (echo_area_buffer[0]))
10743 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10744 echo_area_buffer[0] = echo_buffer[1];
10745 else
10746 echo_area_buffer[0] = echo_buffer[0];
10749 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10751 /* Someone switched buffers between print requests. */
10752 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10753 bset_truncate_lines (current_buffer, Qnil);
10759 /* Display an echo area message in window W. Value is non-zero if W's
10760 height is changed. If display_last_displayed_message_p is
10761 non-zero, display the message that was last displayed, otherwise
10762 display the current message. */
10764 static int
10765 display_echo_area (struct window *w)
10767 int i, no_message_p, window_height_changed_p;
10769 /* Temporarily disable garbage collections while displaying the echo
10770 area. This is done because a GC can print a message itself.
10771 That message would modify the echo area buffer's contents while a
10772 redisplay of the buffer is going on, and seriously confuse
10773 redisplay. */
10774 ptrdiff_t count = inhibit_garbage_collection ();
10776 /* If there is no message, we must call display_echo_area_1
10777 nevertheless because it resizes the window. But we will have to
10778 reset the echo_area_buffer in question to nil at the end because
10779 with_echo_area_buffer will sets it to an empty buffer. */
10780 i = display_last_displayed_message_p ? 1 : 0;
10781 no_message_p = NILP (echo_area_buffer[i]);
10783 window_height_changed_p
10784 = with_echo_area_buffer (w, display_last_displayed_message_p,
10785 display_echo_area_1,
10786 (intptr_t) w, Qnil);
10788 if (no_message_p)
10789 echo_area_buffer[i] = Qnil;
10791 unbind_to (count, Qnil);
10792 return window_height_changed_p;
10796 /* Helper for display_echo_area. Display the current buffer which
10797 contains the current echo area message in window W, a mini-window,
10798 a pointer to which is passed in A1. A2..A4 are currently not used.
10799 Change the height of W so that all of the message is displayed.
10800 Value is non-zero if height of W was changed. */
10802 static int
10803 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10805 intptr_t i1 = a1;
10806 struct window *w = (struct window *) i1;
10807 Lisp_Object window;
10808 struct text_pos start;
10809 int window_height_changed_p = 0;
10811 /* Do this before displaying, so that we have a large enough glyph
10812 matrix for the display. If we can't get enough space for the
10813 whole text, display the last N lines. That works by setting w->start. */
10814 window_height_changed_p = resize_mini_window (w, 0);
10816 /* Use the starting position chosen by resize_mini_window. */
10817 SET_TEXT_POS_FROM_MARKER (start, w->start);
10819 /* Display. */
10820 clear_glyph_matrix (w->desired_matrix);
10821 XSETWINDOW (window, w);
10822 try_window (window, start, 0);
10824 return window_height_changed_p;
10828 /* Resize the echo area window to exactly the size needed for the
10829 currently displayed message, if there is one. If a mini-buffer
10830 is active, don't shrink it. */
10832 void
10833 resize_echo_area_exactly (void)
10835 if (BUFFERP (echo_area_buffer[0])
10836 && WINDOWP (echo_area_window))
10838 struct window *w = XWINDOW (echo_area_window);
10839 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10840 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10841 (intptr_t) w, resize_exactly);
10842 if (resized_p)
10844 windows_or_buffers_changed = 42;
10845 update_mode_lines = 30;
10846 redisplay_internal ();
10852 /* Callback function for with_echo_area_buffer, when used from
10853 resize_echo_area_exactly. A1 contains a pointer to the window to
10854 resize, EXACTLY non-nil means resize the mini-window exactly to the
10855 size of the text displayed. A3 and A4 are not used. Value is what
10856 resize_mini_window returns. */
10858 static int
10859 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10861 intptr_t i1 = a1;
10862 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10866 /* Resize mini-window W to fit the size of its contents. EXACT_P
10867 means size the window exactly to the size needed. Otherwise, it's
10868 only enlarged until W's buffer is empty.
10870 Set W->start to the right place to begin display. If the whole
10871 contents fit, start at the beginning. Otherwise, start so as
10872 to make the end of the contents appear. This is particularly
10873 important for y-or-n-p, but seems desirable generally.
10875 Value is non-zero if the window height has been changed. */
10878 resize_mini_window (struct window *w, int exact_p)
10880 struct frame *f = XFRAME (w->frame);
10881 int window_height_changed_p = 0;
10883 eassert (MINI_WINDOW_P (w));
10885 /* By default, start display at the beginning. */
10886 set_marker_both (w->start, w->contents,
10887 BUF_BEGV (XBUFFER (w->contents)),
10888 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10890 /* Don't resize windows while redisplaying a window; it would
10891 confuse redisplay functions when the size of the window they are
10892 displaying changes from under them. Such a resizing can happen,
10893 for instance, when which-func prints a long message while
10894 we are running fontification-functions. We're running these
10895 functions with safe_call which binds inhibit-redisplay to t. */
10896 if (!NILP (Vinhibit_redisplay))
10897 return 0;
10899 /* Nil means don't try to resize. */
10900 if (NILP (Vresize_mini_windows)
10901 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10902 return 0;
10904 if (!FRAME_MINIBUF_ONLY_P (f))
10906 struct it it;
10907 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10908 + WINDOW_PIXEL_HEIGHT (w));
10909 int unit = FRAME_LINE_HEIGHT (f);
10910 int height, max_height;
10911 struct text_pos start;
10912 struct buffer *old_current_buffer = NULL;
10914 if (current_buffer != XBUFFER (w->contents))
10916 old_current_buffer = current_buffer;
10917 set_buffer_internal (XBUFFER (w->contents));
10920 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10922 /* Compute the max. number of lines specified by the user. */
10923 if (FLOATP (Vmax_mini_window_height))
10924 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10925 else if (INTEGERP (Vmax_mini_window_height))
10926 max_height = XINT (Vmax_mini_window_height) * unit;
10927 else
10928 max_height = total_height / 4;
10930 /* Correct that max. height if it's bogus. */
10931 max_height = clip_to_bounds (unit, max_height, total_height);
10933 /* Find out the height of the text in the window. */
10934 if (it.line_wrap == TRUNCATE)
10935 height = unit;
10936 else
10938 last_height = 0;
10939 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10940 if (it.max_ascent == 0 && it.max_descent == 0)
10941 height = it.current_y + last_height;
10942 else
10943 height = it.current_y + it.max_ascent + it.max_descent;
10944 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10947 /* Compute a suitable window start. */
10948 if (height > max_height)
10950 height = (max_height / unit) * unit;
10951 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10952 move_it_vertically_backward (&it, height - unit);
10953 start = it.current.pos;
10955 else
10956 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10957 SET_MARKER_FROM_TEXT_POS (w->start, start);
10959 if (EQ (Vresize_mini_windows, Qgrow_only))
10961 /* Let it grow only, until we display an empty message, in which
10962 case the window shrinks again. */
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)
10972 && (exact_p || BEGV == ZV))
10974 int old_height = WINDOW_PIXEL_HEIGHT (w);
10976 FRAME_WINDOWS_FROZEN (f) = 0;
10977 shrink_mini_window (w, 1);
10978 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10981 else
10983 /* Always resize to exact size needed. */
10984 if (height > WINDOW_PIXEL_HEIGHT (w))
10986 int old_height = WINDOW_PIXEL_HEIGHT (w);
10988 FRAME_WINDOWS_FROZEN (f) = 1;
10989 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
10990 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
10992 else if (height < WINDOW_PIXEL_HEIGHT (w))
10994 int old_height = WINDOW_PIXEL_HEIGHT (w);
10996 FRAME_WINDOWS_FROZEN (f) = 0;
10997 shrink_mini_window (w, 1);
10999 if (height)
11001 FRAME_WINDOWS_FROZEN (f) = 1;
11002 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
11005 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11009 if (old_current_buffer)
11010 set_buffer_internal (old_current_buffer);
11013 return window_height_changed_p;
11017 /* Value is the current message, a string, or nil if there is no
11018 current message. */
11020 Lisp_Object
11021 current_message (void)
11023 Lisp_Object msg;
11025 if (!BUFFERP (echo_area_buffer[0]))
11026 msg = Qnil;
11027 else
11029 with_echo_area_buffer (0, 0, current_message_1,
11030 (intptr_t) &msg, Qnil);
11031 if (NILP (msg))
11032 echo_area_buffer[0] = Qnil;
11035 return msg;
11039 static int
11040 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11042 intptr_t i1 = a1;
11043 Lisp_Object *msg = (Lisp_Object *) i1;
11045 if (Z > BEG)
11046 *msg = make_buffer_string (BEG, Z, 1);
11047 else
11048 *msg = Qnil;
11049 return 0;
11053 /* Push the current message on Vmessage_stack for later restoration
11054 by restore_message. Value is non-zero if the current message isn't
11055 empty. This is a relatively infrequent operation, so it's not
11056 worth optimizing. */
11058 bool
11059 push_message (void)
11061 Lisp_Object msg = current_message ();
11062 Vmessage_stack = Fcons (msg, Vmessage_stack);
11063 return STRINGP (msg);
11067 /* Restore message display from the top of Vmessage_stack. */
11069 void
11070 restore_message (void)
11072 eassert (CONSP (Vmessage_stack));
11073 message3_nolog (XCAR (Vmessage_stack));
11077 /* Handler for unwind-protect calling pop_message. */
11079 void
11080 pop_message_unwind (void)
11082 /* Pop the top-most entry off Vmessage_stack. */
11083 eassert (CONSP (Vmessage_stack));
11084 Vmessage_stack = XCDR (Vmessage_stack);
11088 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11089 exits. If the stack is not empty, we have a missing pop_message
11090 somewhere. */
11092 void
11093 check_message_stack (void)
11095 if (!NILP (Vmessage_stack))
11096 emacs_abort ();
11100 /* Truncate to NCHARS what will be displayed in the echo area the next
11101 time we display it---but don't redisplay it now. */
11103 void
11104 truncate_echo_area (ptrdiff_t nchars)
11106 if (nchars == 0)
11107 echo_area_buffer[0] = Qnil;
11108 else if (!noninteractive
11109 && INTERACTIVE
11110 && !NILP (echo_area_buffer[0]))
11112 struct frame *sf = SELECTED_FRAME ();
11113 /* Error messages get reported properly by cmd_error, so this must be
11114 just an informative message; if the frame hasn't really been
11115 initialized yet, just toss it. */
11116 if (sf->glyphs_initialized_p)
11117 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11122 /* Helper function for truncate_echo_area. Truncate the current
11123 message to at most NCHARS characters. */
11125 static int
11126 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11128 if (BEG + nchars < Z)
11129 del_range (BEG + nchars, Z);
11130 if (Z == BEG)
11131 echo_area_buffer[0] = Qnil;
11132 return 0;
11135 /* Set the current message to STRING. */
11137 static void
11138 set_message (Lisp_Object string)
11140 eassert (STRINGP (string));
11142 message_enable_multibyte = STRING_MULTIBYTE (string);
11144 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11145 message_buf_print = 0;
11146 help_echo_showing_p = 0;
11148 if (STRINGP (Vdebug_on_message)
11149 && STRINGP (string)
11150 && fast_string_match (Vdebug_on_message, string) >= 0)
11151 call_debugger (list2 (Qerror, string));
11155 /* Helper function for set_message. First argument is ignored and second
11156 argument has the same meaning as for set_message.
11157 This function is called with the echo area buffer being current. */
11159 static int
11160 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11162 eassert (STRINGP (string));
11164 /* Change multibyteness of the echo buffer appropriately. */
11165 if (message_enable_multibyte
11166 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11167 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11169 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11170 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11171 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11173 /* Insert new message at BEG. */
11174 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11176 /* This function takes care of single/multibyte conversion.
11177 We just have to ensure that the echo area buffer has the right
11178 setting of enable_multibyte_characters. */
11179 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11181 return 0;
11185 /* Clear messages. CURRENT_P non-zero means clear the current
11186 message. LAST_DISPLAYED_P non-zero means clear the message
11187 last displayed. */
11189 void
11190 clear_message (bool current_p, bool last_displayed_p)
11192 if (current_p)
11194 echo_area_buffer[0] = Qnil;
11195 message_cleared_p = true;
11198 if (last_displayed_p)
11199 echo_area_buffer[1] = Qnil;
11201 message_buf_print = 0;
11204 /* Clear garbaged frames.
11206 This function is used where the old redisplay called
11207 redraw_garbaged_frames which in turn called redraw_frame which in
11208 turn called clear_frame. The call to clear_frame was a source of
11209 flickering. I believe a clear_frame is not necessary. It should
11210 suffice in the new redisplay to invalidate all current matrices,
11211 and ensure a complete redisplay of all windows. */
11213 static void
11214 clear_garbaged_frames (void)
11216 if (frame_garbaged)
11218 Lisp_Object tail, frame;
11220 FOR_EACH_FRAME (tail, frame)
11222 struct frame *f = XFRAME (frame);
11224 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11226 if (f->resized_p)
11227 redraw_frame (f);
11228 else
11229 clear_current_matrices (f);
11230 fset_redisplay (f);
11231 f->garbaged = false;
11232 f->resized_p = false;
11236 frame_garbaged = false;
11241 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11242 is non-zero update selected_frame. Value is non-zero if the
11243 mini-windows height has been changed. */
11245 static int
11246 echo_area_display (int update_frame_p)
11248 Lisp_Object mini_window;
11249 struct window *w;
11250 struct frame *f;
11251 int window_height_changed_p = 0;
11252 struct frame *sf = SELECTED_FRAME ();
11254 mini_window = FRAME_MINIBUF_WINDOW (sf);
11255 w = XWINDOW (mini_window);
11256 f = XFRAME (WINDOW_FRAME (w));
11258 /* Don't display if frame is invisible or not yet initialized. */
11259 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11260 return 0;
11262 #ifdef HAVE_WINDOW_SYSTEM
11263 /* When Emacs starts, selected_frame may be the initial terminal
11264 frame. If we let this through, a message would be displayed on
11265 the terminal. */
11266 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11267 return 0;
11268 #endif /* HAVE_WINDOW_SYSTEM */
11270 /* Redraw garbaged frames. */
11271 clear_garbaged_frames ();
11273 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11275 echo_area_window = mini_window;
11276 window_height_changed_p = display_echo_area (w);
11277 w->must_be_updated_p = true;
11279 /* Update the display, unless called from redisplay_internal.
11280 Also don't update the screen during redisplay itself. The
11281 update will happen at the end of redisplay, and an update
11282 here could cause confusion. */
11283 if (update_frame_p && !redisplaying_p)
11285 int n = 0;
11287 /* If the display update has been interrupted by pending
11288 input, update mode lines in the frame. Due to the
11289 pending input, it might have been that redisplay hasn't
11290 been called, so that mode lines above the echo area are
11291 garbaged. This looks odd, so we prevent it here. */
11292 if (!display_completed)
11293 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11295 if (window_height_changed_p
11296 /* Don't do this if Emacs is shutting down. Redisplay
11297 needs to run hooks. */
11298 && !NILP (Vrun_hooks))
11300 /* Must update other windows. Likewise as in other
11301 cases, don't let this update be interrupted by
11302 pending input. */
11303 ptrdiff_t count = SPECPDL_INDEX ();
11304 specbind (Qredisplay_dont_pause, Qt);
11305 windows_or_buffers_changed = 44;
11306 redisplay_internal ();
11307 unbind_to (count, Qnil);
11309 else if (FRAME_WINDOW_P (f) && n == 0)
11311 /* Window configuration is the same as before.
11312 Can do with a display update of the echo area,
11313 unless we displayed some mode lines. */
11314 update_single_window (w, 1);
11315 flush_frame (f);
11317 else
11318 update_frame (f, 1, 1);
11320 /* If cursor is in the echo area, make sure that the next
11321 redisplay displays the minibuffer, so that the cursor will
11322 be replaced with what the minibuffer wants. */
11323 if (cursor_in_echo_area)
11324 wset_redisplay (XWINDOW (mini_window));
11327 else if (!EQ (mini_window, selected_window))
11328 wset_redisplay (XWINDOW (mini_window));
11330 /* Last displayed message is now the current message. */
11331 echo_area_buffer[1] = echo_area_buffer[0];
11332 /* Inform read_char that we're not echoing. */
11333 echo_message_buffer = Qnil;
11335 /* Prevent redisplay optimization in redisplay_internal by resetting
11336 this_line_start_pos. This is done because the mini-buffer now
11337 displays the message instead of its buffer text. */
11338 if (EQ (mini_window, selected_window))
11339 CHARPOS (this_line_start_pos) = 0;
11341 return window_height_changed_p;
11344 /* Nonzero if W's buffer was changed but not saved. */
11346 static int
11347 window_buffer_changed (struct window *w)
11349 struct buffer *b = XBUFFER (w->contents);
11351 eassert (BUFFER_LIVE_P (b));
11353 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11356 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11358 static int
11359 mode_line_update_needed (struct window *w)
11361 return (w->column_number_displayed != -1
11362 && !(PT == w->last_point && !window_outdated (w))
11363 && (w->column_number_displayed != current_column ()));
11366 /* Nonzero if window start of W is frozen and may not be changed during
11367 redisplay. */
11369 static bool
11370 window_frozen_p (struct window *w)
11372 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11374 Lisp_Object window;
11376 XSETWINDOW (window, w);
11377 if (MINI_WINDOW_P (w))
11378 return 0;
11379 else if (EQ (window, selected_window))
11380 return 0;
11381 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11382 && EQ (window, Vminibuf_scroll_window))
11383 /* This special window can't be frozen too. */
11384 return 0;
11385 else
11386 return 1;
11388 return 0;
11391 /***********************************************************************
11392 Mode Lines and Frame Titles
11393 ***********************************************************************/
11395 /* A buffer for constructing non-propertized mode-line strings and
11396 frame titles in it; allocated from the heap in init_xdisp and
11397 resized as needed in store_mode_line_noprop_char. */
11399 static char *mode_line_noprop_buf;
11401 /* The buffer's end, and a current output position in it. */
11403 static char *mode_line_noprop_buf_end;
11404 static char *mode_line_noprop_ptr;
11406 #define MODE_LINE_NOPROP_LEN(start) \
11407 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11409 static enum {
11410 MODE_LINE_DISPLAY = 0,
11411 MODE_LINE_TITLE,
11412 MODE_LINE_NOPROP,
11413 MODE_LINE_STRING
11414 } mode_line_target;
11416 /* Alist that caches the results of :propertize.
11417 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11418 static Lisp_Object mode_line_proptrans_alist;
11420 /* List of strings making up the mode-line. */
11421 static Lisp_Object mode_line_string_list;
11423 /* Base face property when building propertized mode line string. */
11424 static Lisp_Object mode_line_string_face;
11425 static Lisp_Object mode_line_string_face_prop;
11428 /* Unwind data for mode line strings */
11430 static Lisp_Object Vmode_line_unwind_vector;
11432 static Lisp_Object
11433 format_mode_line_unwind_data (struct frame *target_frame,
11434 struct buffer *obuf,
11435 Lisp_Object owin,
11436 int save_proptrans)
11438 Lisp_Object vector, tmp;
11440 /* Reduce consing by keeping one vector in
11441 Vwith_echo_area_save_vector. */
11442 vector = Vmode_line_unwind_vector;
11443 Vmode_line_unwind_vector = Qnil;
11445 if (NILP (vector))
11446 vector = Fmake_vector (make_number (10), Qnil);
11448 ASET (vector, 0, make_number (mode_line_target));
11449 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11450 ASET (vector, 2, mode_line_string_list);
11451 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11452 ASET (vector, 4, mode_line_string_face);
11453 ASET (vector, 5, mode_line_string_face_prop);
11455 if (obuf)
11456 XSETBUFFER (tmp, obuf);
11457 else
11458 tmp = Qnil;
11459 ASET (vector, 6, tmp);
11460 ASET (vector, 7, owin);
11461 if (target_frame)
11463 /* Similarly to `with-selected-window', if the operation selects
11464 a window on another frame, we must restore that frame's
11465 selected window, and (for a tty) the top-frame. */
11466 ASET (vector, 8, target_frame->selected_window);
11467 if (FRAME_TERMCAP_P (target_frame))
11468 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11471 return vector;
11474 static void
11475 unwind_format_mode_line (Lisp_Object vector)
11477 Lisp_Object old_window = AREF (vector, 7);
11478 Lisp_Object target_frame_window = AREF (vector, 8);
11479 Lisp_Object old_top_frame = AREF (vector, 9);
11481 mode_line_target = XINT (AREF (vector, 0));
11482 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11483 mode_line_string_list = AREF (vector, 2);
11484 if (! EQ (AREF (vector, 3), Qt))
11485 mode_line_proptrans_alist = AREF (vector, 3);
11486 mode_line_string_face = AREF (vector, 4);
11487 mode_line_string_face_prop = AREF (vector, 5);
11489 /* Select window before buffer, since it may change the buffer. */
11490 if (!NILP (old_window))
11492 /* If the operation that we are unwinding had selected a window
11493 on a different frame, reset its frame-selected-window. For a
11494 text terminal, reset its top-frame if necessary. */
11495 if (!NILP (target_frame_window))
11497 Lisp_Object frame
11498 = WINDOW_FRAME (XWINDOW (target_frame_window));
11500 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11501 Fselect_window (target_frame_window, Qt);
11503 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11504 Fselect_frame (old_top_frame, Qt);
11507 Fselect_window (old_window, Qt);
11510 if (!NILP (AREF (vector, 6)))
11512 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11513 ASET (vector, 6, Qnil);
11516 Vmode_line_unwind_vector = vector;
11520 /* Store a single character C for the frame title in mode_line_noprop_buf.
11521 Re-allocate mode_line_noprop_buf if necessary. */
11523 static void
11524 store_mode_line_noprop_char (char c)
11526 /* If output position has reached the end of the allocated buffer,
11527 increase the buffer's size. */
11528 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11530 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11531 ptrdiff_t size = len;
11532 mode_line_noprop_buf =
11533 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11534 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11535 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11538 *mode_line_noprop_ptr++ = c;
11542 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11543 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11544 characters that yield more columns than PRECISION; PRECISION <= 0
11545 means copy the whole string. Pad with spaces until FIELD_WIDTH
11546 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11547 pad. Called from display_mode_element when it is used to build a
11548 frame title. */
11550 static int
11551 store_mode_line_noprop (const char *string, int field_width, int precision)
11553 const unsigned char *str = (const unsigned char *) string;
11554 int n = 0;
11555 ptrdiff_t dummy, nbytes;
11557 /* Copy at most PRECISION chars from STR. */
11558 nbytes = strlen (string);
11559 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11560 while (nbytes--)
11561 store_mode_line_noprop_char (*str++);
11563 /* Fill up with spaces until FIELD_WIDTH reached. */
11564 while (field_width > 0
11565 && n < field_width)
11567 store_mode_line_noprop_char (' ');
11568 ++n;
11571 return n;
11574 /***********************************************************************
11575 Frame Titles
11576 ***********************************************************************/
11578 #ifdef HAVE_WINDOW_SYSTEM
11580 /* Set the title of FRAME, if it has changed. The title format is
11581 Vicon_title_format if FRAME is iconified, otherwise it is
11582 frame_title_format. */
11584 static void
11585 x_consider_frame_title (Lisp_Object frame)
11587 struct frame *f = XFRAME (frame);
11589 if (FRAME_WINDOW_P (f)
11590 || FRAME_MINIBUF_ONLY_P (f)
11591 || f->explicit_name)
11593 /* Do we have more than one visible frame on this X display? */
11594 Lisp_Object tail, other_frame, fmt;
11595 ptrdiff_t title_start;
11596 char *title;
11597 ptrdiff_t len;
11598 struct it it;
11599 ptrdiff_t count = SPECPDL_INDEX ();
11601 FOR_EACH_FRAME (tail, other_frame)
11603 struct frame *tf = XFRAME (other_frame);
11605 if (tf != f
11606 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11607 && !FRAME_MINIBUF_ONLY_P (tf)
11608 && !EQ (other_frame, tip_frame)
11609 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11610 break;
11613 /* Set global variable indicating that multiple frames exist. */
11614 multiple_frames = CONSP (tail);
11616 /* Switch to the buffer of selected window of the frame. Set up
11617 mode_line_target so that display_mode_element will output into
11618 mode_line_noprop_buf; then display the title. */
11619 record_unwind_protect (unwind_format_mode_line,
11620 format_mode_line_unwind_data
11621 (f, current_buffer, selected_window, 0));
11623 Fselect_window (f->selected_window, Qt);
11624 set_buffer_internal_1
11625 (XBUFFER (XWINDOW (f->selected_window)->contents));
11626 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11628 mode_line_target = MODE_LINE_TITLE;
11629 title_start = MODE_LINE_NOPROP_LEN (0);
11630 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11631 NULL, DEFAULT_FACE_ID);
11632 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11633 len = MODE_LINE_NOPROP_LEN (title_start);
11634 title = mode_line_noprop_buf + title_start;
11635 unbind_to (count, Qnil);
11637 /* Set the title only if it's changed. This avoids consing in
11638 the common case where it hasn't. (If it turns out that we've
11639 already wasted too much time by walking through the list with
11640 display_mode_element, then we might need to optimize at a
11641 higher level than this.) */
11642 if (! STRINGP (f->name)
11643 || SBYTES (f->name) != len
11644 || memcmp (title, SDATA (f->name), len) != 0)
11645 x_implicitly_set_name (f, make_string (title, len), Qnil);
11649 #endif /* not HAVE_WINDOW_SYSTEM */
11652 /***********************************************************************
11653 Menu Bars
11654 ***********************************************************************/
11656 /* Non-zero if we will not redisplay all visible windows. */
11657 #define REDISPLAY_SOME_P() \
11658 ((windows_or_buffers_changed == 0 \
11659 || windows_or_buffers_changed == REDISPLAY_SOME) \
11660 && (update_mode_lines == 0 \
11661 || update_mode_lines == REDISPLAY_SOME))
11663 /* Prepare for redisplay by updating menu-bar item lists when
11664 appropriate. This can call eval. */
11666 static void
11667 prepare_menu_bars (void)
11669 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11670 bool some_windows = REDISPLAY_SOME_P ();
11671 struct gcpro gcpro1, gcpro2;
11672 Lisp_Object tooltip_frame;
11674 #ifdef HAVE_WINDOW_SYSTEM
11675 tooltip_frame = tip_frame;
11676 #else
11677 tooltip_frame = Qnil;
11678 #endif
11680 if (FUNCTIONP (Vpre_redisplay_function))
11682 Lisp_Object windows = all_windows ? Qt : Qnil;
11683 if (all_windows && some_windows)
11685 Lisp_Object ws = window_list ();
11686 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11688 Lisp_Object this = XCAR (ws);
11689 struct window *w = XWINDOW (this);
11690 if (w->redisplay
11691 || XFRAME (w->frame)->redisplay
11692 || XBUFFER (w->contents)->text->redisplay)
11694 windows = Fcons (this, windows);
11698 safe__call1 (true, Vpre_redisplay_function, windows);
11701 /* Update all frame titles based on their buffer names, etc. We do
11702 this before the menu bars so that the buffer-menu will show the
11703 up-to-date frame titles. */
11704 #ifdef HAVE_WINDOW_SYSTEM
11705 if (all_windows)
11707 Lisp_Object tail, frame;
11709 FOR_EACH_FRAME (tail, frame)
11711 struct frame *f = XFRAME (frame);
11712 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11713 if (some_windows
11714 && !f->redisplay
11715 && !w->redisplay
11716 && !XBUFFER (w->contents)->text->redisplay)
11717 continue;
11719 if (!EQ (frame, tooltip_frame)
11720 && (FRAME_ICONIFIED_P (f)
11721 || FRAME_VISIBLE_P (f) == 1
11722 /* Exclude TTY frames that are obscured because they
11723 are not the top frame on their console. This is
11724 because x_consider_frame_title actually switches
11725 to the frame, which for TTY frames means it is
11726 marked as garbaged, and will be completely
11727 redrawn on the next redisplay cycle. This causes
11728 TTY frames to be completely redrawn, when there
11729 are more than one of them, even though nothing
11730 should be changed on display. */
11731 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11732 x_consider_frame_title (frame);
11735 #endif /* HAVE_WINDOW_SYSTEM */
11737 /* Update the menu bar item lists, if appropriate. This has to be
11738 done before any actual redisplay or generation of display lines. */
11740 if (all_windows)
11742 Lisp_Object tail, frame;
11743 ptrdiff_t count = SPECPDL_INDEX ();
11744 /* 1 means that update_menu_bar has run its hooks
11745 so any further calls to update_menu_bar shouldn't do so again. */
11746 int menu_bar_hooks_run = 0;
11748 record_unwind_save_match_data ();
11750 FOR_EACH_FRAME (tail, frame)
11752 struct frame *f = XFRAME (frame);
11753 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11755 /* Ignore tooltip frame. */
11756 if (EQ (frame, tooltip_frame))
11757 continue;
11759 if (some_windows
11760 && !f->redisplay
11761 && !w->redisplay
11762 && !XBUFFER (w->contents)->text->redisplay)
11763 continue;
11765 /* If a window on this frame changed size, report that to
11766 the user and clear the size-change flag. */
11767 if (FRAME_WINDOW_SIZES_CHANGED (f))
11769 Lisp_Object functions;
11771 /* Clear flag first in case we get an error below. */
11772 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11773 functions = Vwindow_size_change_functions;
11774 GCPRO2 (tail, functions);
11776 while (CONSP (functions))
11778 if (!EQ (XCAR (functions), Qt))
11779 call1 (XCAR (functions), frame);
11780 functions = XCDR (functions);
11782 UNGCPRO;
11785 GCPRO1 (tail);
11786 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11787 #ifdef HAVE_WINDOW_SYSTEM
11788 update_tool_bar (f, 0);
11789 #endif
11790 #ifdef HAVE_NS
11791 if (windows_or_buffers_changed
11792 && FRAME_NS_P (f))
11793 ns_set_doc_edited
11794 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11795 #endif
11796 UNGCPRO;
11799 unbind_to (count, Qnil);
11801 else
11803 struct frame *sf = SELECTED_FRAME ();
11804 update_menu_bar (sf, 1, 0);
11805 #ifdef HAVE_WINDOW_SYSTEM
11806 update_tool_bar (sf, 1);
11807 #endif
11812 /* Update the menu bar item list for frame F. This has to be done
11813 before we start to fill in any display lines, because it can call
11814 eval.
11816 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11818 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11819 already ran the menu bar hooks for this redisplay, so there
11820 is no need to run them again. The return value is the
11821 updated value of this flag, to pass to the next call. */
11823 static int
11824 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11826 Lisp_Object window;
11827 register struct window *w;
11829 /* If called recursively during a menu update, do nothing. This can
11830 happen when, for instance, an activate-menubar-hook causes a
11831 redisplay. */
11832 if (inhibit_menubar_update)
11833 return hooks_run;
11835 window = FRAME_SELECTED_WINDOW (f);
11836 w = XWINDOW (window);
11838 if (FRAME_WINDOW_P (f)
11840 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11841 || defined (HAVE_NS) || defined (USE_GTK)
11842 FRAME_EXTERNAL_MENU_BAR (f)
11843 #else
11844 FRAME_MENU_BAR_LINES (f) > 0
11845 #endif
11846 : FRAME_MENU_BAR_LINES (f) > 0)
11848 /* If the user has switched buffers or windows, we need to
11849 recompute to reflect the new bindings. But we'll
11850 recompute when update_mode_lines is set too; that means
11851 that people can use force-mode-line-update to request
11852 that the menu bar be recomputed. The adverse effect on
11853 the rest of the redisplay algorithm is about the same as
11854 windows_or_buffers_changed anyway. */
11855 if (windows_or_buffers_changed
11856 /* This used to test w->update_mode_line, but we believe
11857 there is no need to recompute the menu in that case. */
11858 || update_mode_lines
11859 || window_buffer_changed (w))
11861 struct buffer *prev = current_buffer;
11862 ptrdiff_t count = SPECPDL_INDEX ();
11864 specbind (Qinhibit_menubar_update, Qt);
11866 set_buffer_internal_1 (XBUFFER (w->contents));
11867 if (save_match_data)
11868 record_unwind_save_match_data ();
11869 if (NILP (Voverriding_local_map_menu_flag))
11871 specbind (Qoverriding_terminal_local_map, Qnil);
11872 specbind (Qoverriding_local_map, Qnil);
11875 if (!hooks_run)
11877 /* Run the Lucid hook. */
11878 safe_run_hooks (Qactivate_menubar_hook);
11880 /* If it has changed current-menubar from previous value,
11881 really recompute the menu-bar from the value. */
11882 if (! NILP (Vlucid_menu_bar_dirty_flag))
11883 call0 (Qrecompute_lucid_menubar);
11885 safe_run_hooks (Qmenu_bar_update_hook);
11887 hooks_run = 1;
11890 XSETFRAME (Vmenu_updating_frame, f);
11891 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11893 /* Redisplay the menu bar in case we changed it. */
11894 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11895 || defined (HAVE_NS) || defined (USE_GTK)
11896 if (FRAME_WINDOW_P (f))
11898 #if defined (HAVE_NS)
11899 /* All frames on Mac OS share the same menubar. So only
11900 the selected frame should be allowed to set it. */
11901 if (f == SELECTED_FRAME ())
11902 #endif
11903 set_frame_menubar (f, 0, 0);
11905 else
11906 /* On a terminal screen, the menu bar is an ordinary screen
11907 line, and this makes it get updated. */
11908 w->update_mode_line = 1;
11909 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11910 /* In the non-toolkit version, the menu bar is an ordinary screen
11911 line, and this makes it get updated. */
11912 w->update_mode_line = 1;
11913 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11915 unbind_to (count, Qnil);
11916 set_buffer_internal_1 (prev);
11920 return hooks_run;
11923 /***********************************************************************
11924 Tool-bars
11925 ***********************************************************************/
11927 #ifdef HAVE_WINDOW_SYSTEM
11929 /* Select `frame' temporarily without running all the code in
11930 do_switch_frame.
11931 FIXME: Maybe do_switch_frame should be trimmed down similarly
11932 when `norecord' is set. */
11933 static void
11934 fast_set_selected_frame (Lisp_Object frame)
11936 if (!EQ (selected_frame, frame))
11938 selected_frame = frame;
11939 selected_window = XFRAME (frame)->selected_window;
11943 /* Update the tool-bar item list for frame F. This has to be done
11944 before we start to fill in any display lines. Called from
11945 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11946 and restore it here. */
11948 static void
11949 update_tool_bar (struct frame *f, int save_match_data)
11951 #if defined (USE_GTK) || defined (HAVE_NS)
11952 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11953 #else
11954 int do_update = (WINDOWP (f->tool_bar_window)
11955 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11956 #endif
11958 if (do_update)
11960 Lisp_Object window;
11961 struct window *w;
11963 window = FRAME_SELECTED_WINDOW (f);
11964 w = XWINDOW (window);
11966 /* If the user has switched buffers or windows, we need to
11967 recompute to reflect the new bindings. But we'll
11968 recompute when update_mode_lines is set too; that means
11969 that people can use force-mode-line-update to request
11970 that the menu bar be recomputed. The adverse effect on
11971 the rest of the redisplay algorithm is about the same as
11972 windows_or_buffers_changed anyway. */
11973 if (windows_or_buffers_changed
11974 || w->update_mode_line
11975 || update_mode_lines
11976 || window_buffer_changed (w))
11978 struct buffer *prev = current_buffer;
11979 ptrdiff_t count = SPECPDL_INDEX ();
11980 Lisp_Object frame, new_tool_bar;
11981 int new_n_tool_bar;
11982 struct gcpro gcpro1;
11984 /* Set current_buffer to the buffer of the selected
11985 window of the frame, so that we get the right local
11986 keymaps. */
11987 set_buffer_internal_1 (XBUFFER (w->contents));
11989 /* Save match data, if we must. */
11990 if (save_match_data)
11991 record_unwind_save_match_data ();
11993 /* Make sure that we don't accidentally use bogus keymaps. */
11994 if (NILP (Voverriding_local_map_menu_flag))
11996 specbind (Qoverriding_terminal_local_map, Qnil);
11997 specbind (Qoverriding_local_map, Qnil);
12000 GCPRO1 (new_tool_bar);
12002 /* We must temporarily set the selected frame to this frame
12003 before calling tool_bar_items, because the calculation of
12004 the tool-bar keymap uses the selected frame (see
12005 `tool-bar-make-keymap' in tool-bar.el). */
12006 eassert (EQ (selected_window,
12007 /* Since we only explicitly preserve selected_frame,
12008 check that selected_window would be redundant. */
12009 XFRAME (selected_frame)->selected_window));
12010 record_unwind_protect (fast_set_selected_frame, selected_frame);
12011 XSETFRAME (frame, f);
12012 fast_set_selected_frame (frame);
12014 /* Build desired tool-bar items from keymaps. */
12015 new_tool_bar
12016 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12017 &new_n_tool_bar);
12019 /* Redisplay the tool-bar if we changed it. */
12020 if (new_n_tool_bar != f->n_tool_bar_items
12021 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12023 /* Redisplay that happens asynchronously due to an expose event
12024 may access f->tool_bar_items. Make sure we update both
12025 variables within BLOCK_INPUT so no such event interrupts. */
12026 block_input ();
12027 fset_tool_bar_items (f, new_tool_bar);
12028 f->n_tool_bar_items = new_n_tool_bar;
12029 w->update_mode_line = 1;
12030 unblock_input ();
12033 UNGCPRO;
12035 unbind_to (count, Qnil);
12036 set_buffer_internal_1 (prev);
12041 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12043 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12044 F's desired tool-bar contents. F->tool_bar_items must have
12045 been set up previously by calling prepare_menu_bars. */
12047 static void
12048 build_desired_tool_bar_string (struct frame *f)
12050 int i, size, size_needed;
12051 struct gcpro gcpro1, gcpro2;
12052 Lisp_Object image, plist;
12054 image = plist = Qnil;
12055 GCPRO2 (image, plist);
12057 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12058 Otherwise, make a new string. */
12060 /* The size of the string we might be able to reuse. */
12061 size = (STRINGP (f->desired_tool_bar_string)
12062 ? SCHARS (f->desired_tool_bar_string)
12063 : 0);
12065 /* We need one space in the string for each image. */
12066 size_needed = f->n_tool_bar_items;
12068 /* Reuse f->desired_tool_bar_string, if possible. */
12069 if (size < size_needed || NILP (f->desired_tool_bar_string))
12070 fset_desired_tool_bar_string
12071 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12072 else
12074 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12075 struct gcpro gcpro1;
12076 GCPRO1 (props);
12077 Fremove_text_properties (make_number (0), make_number (size),
12078 props, f->desired_tool_bar_string);
12079 UNGCPRO;
12082 /* Put a `display' property on the string for the images to display,
12083 put a `menu_item' property on tool-bar items with a value that
12084 is the index of the item in F's tool-bar item vector. */
12085 for (i = 0; i < f->n_tool_bar_items; ++i)
12087 #define PROP(IDX) \
12088 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12090 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12091 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12092 int hmargin, vmargin, relief, idx, end;
12094 /* If image is a vector, choose the image according to the
12095 button state. */
12096 image = PROP (TOOL_BAR_ITEM_IMAGES);
12097 if (VECTORP (image))
12099 if (enabled_p)
12100 idx = (selected_p
12101 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12102 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12103 else
12104 idx = (selected_p
12105 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12106 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12108 eassert (ASIZE (image) >= idx);
12109 image = AREF (image, idx);
12111 else
12112 idx = -1;
12114 /* Ignore invalid image specifications. */
12115 if (!valid_image_p (image))
12116 continue;
12118 /* Display the tool-bar button pressed, or depressed. */
12119 plist = Fcopy_sequence (XCDR (image));
12121 /* Compute margin and relief to draw. */
12122 relief = (tool_bar_button_relief >= 0
12123 ? tool_bar_button_relief
12124 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12125 hmargin = vmargin = relief;
12127 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12128 INT_MAX - max (hmargin, vmargin)))
12130 hmargin += XFASTINT (Vtool_bar_button_margin);
12131 vmargin += XFASTINT (Vtool_bar_button_margin);
12133 else if (CONSP (Vtool_bar_button_margin))
12135 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12136 INT_MAX - hmargin))
12137 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12139 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12140 INT_MAX - vmargin))
12141 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12144 if (auto_raise_tool_bar_buttons_p)
12146 /* Add a `:relief' property to the image spec if the item is
12147 selected. */
12148 if (selected_p)
12150 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12151 hmargin -= relief;
12152 vmargin -= relief;
12155 else
12157 /* If image is selected, display it pressed, i.e. with a
12158 negative relief. If it's not selected, display it with a
12159 raised relief. */
12160 plist = Fplist_put (plist, QCrelief,
12161 (selected_p
12162 ? make_number (-relief)
12163 : make_number (relief)));
12164 hmargin -= relief;
12165 vmargin -= relief;
12168 /* Put a margin around the image. */
12169 if (hmargin || vmargin)
12171 if (hmargin == vmargin)
12172 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12173 else
12174 plist = Fplist_put (plist, QCmargin,
12175 Fcons (make_number (hmargin),
12176 make_number (vmargin)));
12179 /* If button is not enabled, and we don't have special images
12180 for the disabled state, make the image appear disabled by
12181 applying an appropriate algorithm to it. */
12182 if (!enabled_p && idx < 0)
12183 plist = Fplist_put (plist, QCconversion, Qdisabled);
12185 /* Put a `display' text property on the string for the image to
12186 display. Put a `menu-item' property on the string that gives
12187 the start of this item's properties in the tool-bar items
12188 vector. */
12189 image = Fcons (Qimage, plist);
12190 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12191 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12192 struct gcpro gcpro1;
12193 GCPRO1 (props);
12195 /* Let the last image hide all remaining spaces in the tool bar
12196 string. The string can be longer than needed when we reuse a
12197 previous string. */
12198 if (i + 1 == f->n_tool_bar_items)
12199 end = SCHARS (f->desired_tool_bar_string);
12200 else
12201 end = i + 1;
12202 Fadd_text_properties (make_number (i), make_number (end),
12203 props, f->desired_tool_bar_string);
12204 UNGCPRO;
12205 #undef PROP
12208 UNGCPRO;
12212 /* Display one line of the tool-bar of frame IT->f.
12214 HEIGHT specifies the desired height of the tool-bar line.
12215 If the actual height of the glyph row is less than HEIGHT, the
12216 row's height is increased to HEIGHT, and the icons are centered
12217 vertically in the new height.
12219 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12220 count a final empty row in case the tool-bar width exactly matches
12221 the window width.
12224 static void
12225 display_tool_bar_line (struct it *it, int height)
12227 struct glyph_row *row = it->glyph_row;
12228 int max_x = it->last_visible_x;
12229 struct glyph *last;
12231 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12232 clear_glyph_row (row);
12233 row->enabled_p = true;
12234 row->y = it->current_y;
12236 /* Note that this isn't made use of if the face hasn't a box,
12237 so there's no need to check the face here. */
12238 it->start_of_box_run_p = 1;
12240 while (it->current_x < max_x)
12242 int x, n_glyphs_before, i, nglyphs;
12243 struct it it_before;
12245 /* Get the next display element. */
12246 if (!get_next_display_element (it))
12248 /* Don't count empty row if we are counting needed tool-bar lines. */
12249 if (height < 0 && !it->hpos)
12250 return;
12251 break;
12254 /* Produce glyphs. */
12255 n_glyphs_before = row->used[TEXT_AREA];
12256 it_before = *it;
12258 PRODUCE_GLYPHS (it);
12260 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12261 i = 0;
12262 x = it_before.current_x;
12263 while (i < nglyphs)
12265 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12267 if (x + glyph->pixel_width > max_x)
12269 /* Glyph doesn't fit on line. Backtrack. */
12270 row->used[TEXT_AREA] = n_glyphs_before;
12271 *it = it_before;
12272 /* If this is the only glyph on this line, it will never fit on the
12273 tool-bar, so skip it. But ensure there is at least one glyph,
12274 so we don't accidentally disable the tool-bar. */
12275 if (n_glyphs_before == 0
12276 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12277 break;
12278 goto out;
12281 ++it->hpos;
12282 x += glyph->pixel_width;
12283 ++i;
12286 /* Stop at line end. */
12287 if (ITERATOR_AT_END_OF_LINE_P (it))
12288 break;
12290 set_iterator_to_next (it, 1);
12293 out:;
12295 row->displays_text_p = row->used[TEXT_AREA] != 0;
12297 /* Use default face for the border below the tool bar.
12299 FIXME: When auto-resize-tool-bars is grow-only, there is
12300 no additional border below the possibly empty tool-bar lines.
12301 So to make the extra empty lines look "normal", we have to
12302 use the tool-bar face for the border too. */
12303 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12304 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12305 it->face_id = DEFAULT_FACE_ID;
12307 extend_face_to_end_of_line (it);
12308 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12309 last->right_box_line_p = 1;
12310 if (last == row->glyphs[TEXT_AREA])
12311 last->left_box_line_p = 1;
12313 /* Make line the desired height and center it vertically. */
12314 if ((height -= it->max_ascent + it->max_descent) > 0)
12316 /* Don't add more than one line height. */
12317 height %= FRAME_LINE_HEIGHT (it->f);
12318 it->max_ascent += height / 2;
12319 it->max_descent += (height + 1) / 2;
12322 compute_line_metrics (it);
12324 /* If line is empty, make it occupy the rest of the tool-bar. */
12325 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12327 row->height = row->phys_height = it->last_visible_y - row->y;
12328 row->visible_height = row->height;
12329 row->ascent = row->phys_ascent = 0;
12330 row->extra_line_spacing = 0;
12333 row->full_width_p = 1;
12334 row->continued_p = 0;
12335 row->truncated_on_left_p = 0;
12336 row->truncated_on_right_p = 0;
12338 it->current_x = it->hpos = 0;
12339 it->current_y += row->height;
12340 ++it->vpos;
12341 ++it->glyph_row;
12345 /* Value is the number of pixels needed to make all tool-bar items of
12346 frame F visible. The actual number of glyph rows needed is
12347 returned in *N_ROWS if non-NULL. */
12348 static int
12349 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12351 struct window *w = XWINDOW (f->tool_bar_window);
12352 struct it it;
12353 /* tool_bar_height is called from redisplay_tool_bar after building
12354 the desired matrix, so use (unused) mode-line row as temporary row to
12355 avoid destroying the first tool-bar row. */
12356 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12358 /* Initialize an iterator for iteration over
12359 F->desired_tool_bar_string in the tool-bar window of frame F. */
12360 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12361 temp_row->reversed_p = false;
12362 it.first_visible_x = 0;
12363 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12364 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12365 it.paragraph_embedding = L2R;
12367 while (!ITERATOR_AT_END_P (&it))
12369 clear_glyph_row (temp_row);
12370 it.glyph_row = temp_row;
12371 display_tool_bar_line (&it, -1);
12373 clear_glyph_row (temp_row);
12375 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12376 if (n_rows)
12377 *n_rows = it.vpos > 0 ? it.vpos : -1;
12379 if (pixelwise)
12380 return it.current_y;
12381 else
12382 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12385 #endif /* !USE_GTK && !HAVE_NS */
12387 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12388 0, 2, 0,
12389 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12390 If FRAME is nil or omitted, use the selected frame. Optional argument
12391 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12392 (Lisp_Object frame, Lisp_Object pixelwise)
12394 int height = 0;
12396 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12397 struct frame *f = decode_any_frame (frame);
12399 if (WINDOWP (f->tool_bar_window)
12400 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12402 update_tool_bar (f, 1);
12403 if (f->n_tool_bar_items)
12405 build_desired_tool_bar_string (f);
12406 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12409 #endif
12411 return make_number (height);
12415 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12416 height should be changed. */
12417 static int
12418 redisplay_tool_bar (struct frame *f)
12420 #if defined (USE_GTK) || defined (HAVE_NS)
12422 if (FRAME_EXTERNAL_TOOL_BAR (f))
12423 update_frame_tool_bar (f);
12424 return 0;
12426 #else /* !USE_GTK && !HAVE_NS */
12428 struct window *w;
12429 struct it it;
12430 struct glyph_row *row;
12432 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12433 do anything. This means you must start with tool-bar-lines
12434 non-zero to get the auto-sizing effect. Or in other words, you
12435 can turn off tool-bars by specifying tool-bar-lines zero. */
12436 if (!WINDOWP (f->tool_bar_window)
12437 || (w = XWINDOW (f->tool_bar_window),
12438 WINDOW_TOTAL_LINES (w) == 0))
12439 return 0;
12441 /* Set up an iterator for the tool-bar window. */
12442 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12443 it.first_visible_x = 0;
12444 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12445 row = it.glyph_row;
12446 row->reversed_p = false;
12448 /* Build a string that represents the contents of the tool-bar. */
12449 build_desired_tool_bar_string (f);
12450 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12451 /* FIXME: This should be controlled by a user option. But it
12452 doesn't make sense to have an R2L tool bar if the menu bar cannot
12453 be drawn also R2L, and making the menu bar R2L is tricky due
12454 toolkit-specific code that implements it. If an R2L tool bar is
12455 ever supported, display_tool_bar_line should also be augmented to
12456 call unproduce_glyphs like display_line and display_string
12457 do. */
12458 it.paragraph_embedding = L2R;
12460 if (f->n_tool_bar_rows == 0)
12462 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12464 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12466 x_change_tool_bar_height (f, new_height);
12467 /* Always do that now. */
12468 clear_glyph_matrix (w->desired_matrix);
12469 f->fonts_changed = 1;
12470 return 1;
12474 /* Display as many lines as needed to display all tool-bar items. */
12476 if (f->n_tool_bar_rows > 0)
12478 int border, rows, height, extra;
12480 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12481 border = XINT (Vtool_bar_border);
12482 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12483 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12484 else if (EQ (Vtool_bar_border, Qborder_width))
12485 border = f->border_width;
12486 else
12487 border = 0;
12488 if (border < 0)
12489 border = 0;
12491 rows = f->n_tool_bar_rows;
12492 height = max (1, (it.last_visible_y - border) / rows);
12493 extra = it.last_visible_y - border - height * rows;
12495 while (it.current_y < it.last_visible_y)
12497 int h = 0;
12498 if (extra > 0 && rows-- > 0)
12500 h = (extra + rows - 1) / rows;
12501 extra -= h;
12503 display_tool_bar_line (&it, height + h);
12506 else
12508 while (it.current_y < it.last_visible_y)
12509 display_tool_bar_line (&it, 0);
12512 /* It doesn't make much sense to try scrolling in the tool-bar
12513 window, so don't do it. */
12514 w->desired_matrix->no_scrolling_p = 1;
12515 w->must_be_updated_p = 1;
12517 if (!NILP (Vauto_resize_tool_bars))
12519 int change_height_p = 0;
12521 /* If we couldn't display everything, change the tool-bar's
12522 height if there is room for more. */
12523 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12524 change_height_p = 1;
12526 /* We subtract 1 because display_tool_bar_line advances the
12527 glyph_row pointer before returning to its caller. We want to
12528 examine the last glyph row produced by
12529 display_tool_bar_line. */
12530 row = it.glyph_row - 1;
12532 /* If there are blank lines at the end, except for a partially
12533 visible blank line at the end that is smaller than
12534 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12535 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12536 && row->height >= FRAME_LINE_HEIGHT (f))
12537 change_height_p = 1;
12539 /* If row displays tool-bar items, but is partially visible,
12540 change the tool-bar's height. */
12541 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12542 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
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 int nrows;
12550 int new_height = tool_bar_height (f, &nrows, 1);
12552 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12553 && !f->minimize_tool_bar_window_p)
12554 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12555 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12556 f->minimize_tool_bar_window_p = 0;
12558 if (change_height_p)
12560 x_change_tool_bar_height (f, new_height);
12561 clear_glyph_matrix (w->desired_matrix);
12562 f->n_tool_bar_rows = nrows;
12563 f->fonts_changed = 1;
12565 return 1;
12570 f->minimize_tool_bar_window_p = 0;
12571 return 0;
12573 #endif /* USE_GTK || HAVE_NS */
12576 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12578 /* Get information about the tool-bar item which is displayed in GLYPH
12579 on frame F. Return in *PROP_IDX the index where tool-bar item
12580 properties start in F->tool_bar_items. Value is zero if
12581 GLYPH doesn't display a tool-bar item. */
12583 static int
12584 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12586 Lisp_Object prop;
12587 int success_p;
12588 int charpos;
12590 /* This function can be called asynchronously, which means we must
12591 exclude any possibility that Fget_text_property signals an
12592 error. */
12593 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12594 charpos = max (0, charpos);
12596 /* Get the text property `menu-item' at pos. The value of that
12597 property is the start index of this item's properties in
12598 F->tool_bar_items. */
12599 prop = Fget_text_property (make_number (charpos),
12600 Qmenu_item, f->current_tool_bar_string);
12601 if (INTEGERP (prop))
12603 *prop_idx = XINT (prop);
12604 success_p = 1;
12606 else
12607 success_p = 0;
12609 return success_p;
12613 /* Get information about the tool-bar item at position X/Y on frame F.
12614 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12615 the current matrix of the tool-bar window of F, or NULL if not
12616 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12617 item in F->tool_bar_items. Value is
12619 -1 if X/Y is not on a tool-bar item
12620 0 if X/Y is on the same item that was highlighted before.
12621 1 otherwise. */
12623 static int
12624 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12625 int *hpos, int *vpos, int *prop_idx)
12627 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12628 struct window *w = XWINDOW (f->tool_bar_window);
12629 int area;
12631 /* Find the glyph under X/Y. */
12632 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12633 if (*glyph == NULL)
12634 return -1;
12636 /* Get the start of this tool-bar item's properties in
12637 f->tool_bar_items. */
12638 if (!tool_bar_item_info (f, *glyph, prop_idx))
12639 return -1;
12641 /* Is mouse on the highlighted item? */
12642 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12643 && *vpos >= hlinfo->mouse_face_beg_row
12644 && *vpos <= hlinfo->mouse_face_end_row
12645 && (*vpos > hlinfo->mouse_face_beg_row
12646 || *hpos >= hlinfo->mouse_face_beg_col)
12647 && (*vpos < hlinfo->mouse_face_end_row
12648 || *hpos < hlinfo->mouse_face_end_col
12649 || hlinfo->mouse_face_past_end))
12650 return 0;
12652 return 1;
12656 /* EXPORT:
12657 Handle mouse button event on the tool-bar of frame F, at
12658 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12659 0 for button release. MODIFIERS is event modifiers for button
12660 release. */
12662 void
12663 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12664 int modifiers)
12666 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12667 struct window *w = XWINDOW (f->tool_bar_window);
12668 int hpos, vpos, prop_idx;
12669 struct glyph *glyph;
12670 Lisp_Object enabled_p;
12671 int ts;
12673 /* If not on the highlighted tool-bar item, and mouse-highlight is
12674 non-nil, return. This is so we generate the tool-bar button
12675 click only when the mouse button is released on the same item as
12676 where it was pressed. However, when mouse-highlight is disabled,
12677 generate the click when the button is released regardless of the
12678 highlight, since tool-bar items are not highlighted in that
12679 case. */
12680 frame_to_window_pixel_xy (w, &x, &y);
12681 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12682 if (ts == -1
12683 || (ts != 0 && !NILP (Vmouse_highlight)))
12684 return;
12686 /* When mouse-highlight is off, generate the click for the item
12687 where the button was pressed, disregarding where it was
12688 released. */
12689 if (NILP (Vmouse_highlight) && !down_p)
12690 prop_idx = f->last_tool_bar_item;
12692 /* If item is disabled, do nothing. */
12693 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12694 if (NILP (enabled_p))
12695 return;
12697 if (down_p)
12699 /* Show item in pressed state. */
12700 if (!NILP (Vmouse_highlight))
12701 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12702 f->last_tool_bar_item = prop_idx;
12704 else
12706 Lisp_Object key, frame;
12707 struct input_event event;
12708 EVENT_INIT (event);
12710 /* Show item in released state. */
12711 if (!NILP (Vmouse_highlight))
12712 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12714 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12716 XSETFRAME (frame, f);
12717 event.kind = TOOL_BAR_EVENT;
12718 event.frame_or_window = frame;
12719 event.arg = frame;
12720 kbd_buffer_store_event (&event);
12722 event.kind = TOOL_BAR_EVENT;
12723 event.frame_or_window = frame;
12724 event.arg = key;
12725 event.modifiers = modifiers;
12726 kbd_buffer_store_event (&event);
12727 f->last_tool_bar_item = -1;
12732 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12733 tool-bar window-relative coordinates X/Y. Called from
12734 note_mouse_highlight. */
12736 static void
12737 note_tool_bar_highlight (struct frame *f, int x, int y)
12739 Lisp_Object window = f->tool_bar_window;
12740 struct window *w = XWINDOW (window);
12741 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12742 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12743 int hpos, vpos;
12744 struct glyph *glyph;
12745 struct glyph_row *row;
12746 int i;
12747 Lisp_Object enabled_p;
12748 int prop_idx;
12749 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12750 int mouse_down_p, rc;
12752 /* Function note_mouse_highlight is called with negative X/Y
12753 values when mouse moves outside of the frame. */
12754 if (x <= 0 || y <= 0)
12756 clear_mouse_face (hlinfo);
12757 return;
12760 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12761 if (rc < 0)
12763 /* Not on tool-bar item. */
12764 clear_mouse_face (hlinfo);
12765 return;
12767 else if (rc == 0)
12768 /* On same tool-bar item as before. */
12769 goto set_help_echo;
12771 clear_mouse_face (hlinfo);
12773 /* Mouse is down, but on different tool-bar item? */
12774 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12775 && f == dpyinfo->last_mouse_frame);
12777 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12778 return;
12780 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12782 /* If tool-bar item is not enabled, don't highlight it. */
12783 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12784 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12786 /* Compute the x-position of the glyph. In front and past the
12787 image is a space. We include this in the highlighted area. */
12788 row = MATRIX_ROW (w->current_matrix, vpos);
12789 for (i = x = 0; i < hpos; ++i)
12790 x += row->glyphs[TEXT_AREA][i].pixel_width;
12792 /* Record this as the current active region. */
12793 hlinfo->mouse_face_beg_col = hpos;
12794 hlinfo->mouse_face_beg_row = vpos;
12795 hlinfo->mouse_face_beg_x = x;
12796 hlinfo->mouse_face_past_end = 0;
12798 hlinfo->mouse_face_end_col = hpos + 1;
12799 hlinfo->mouse_face_end_row = vpos;
12800 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12801 hlinfo->mouse_face_window = window;
12802 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12804 /* Display it as active. */
12805 show_mouse_face (hlinfo, draw);
12808 set_help_echo:
12810 /* Set help_echo_string to a help string to display for this tool-bar item.
12811 XTread_socket does the rest. */
12812 help_echo_object = help_echo_window = Qnil;
12813 help_echo_pos = -1;
12814 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12815 if (NILP (help_echo_string))
12816 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12819 #endif /* !USE_GTK && !HAVE_NS */
12821 #endif /* HAVE_WINDOW_SYSTEM */
12825 /************************************************************************
12826 Horizontal scrolling
12827 ************************************************************************/
12829 static int hscroll_window_tree (Lisp_Object);
12830 static int hscroll_windows (Lisp_Object);
12832 /* For all leaf windows in the window tree rooted at WINDOW, set their
12833 hscroll value so that PT is (i) visible in the window, and (ii) so
12834 that it is not within a certain margin at the window's left and
12835 right border. Value is non-zero if any window's hscroll has been
12836 changed. */
12838 static int
12839 hscroll_window_tree (Lisp_Object window)
12841 int hscrolled_p = 0;
12842 int hscroll_relative_p = FLOATP (Vhscroll_step);
12843 int hscroll_step_abs = 0;
12844 double hscroll_step_rel = 0;
12846 if (hscroll_relative_p)
12848 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12849 if (hscroll_step_rel < 0)
12851 hscroll_relative_p = 0;
12852 hscroll_step_abs = 0;
12855 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12857 hscroll_step_abs = XINT (Vhscroll_step);
12858 if (hscroll_step_abs < 0)
12859 hscroll_step_abs = 0;
12861 else
12862 hscroll_step_abs = 0;
12864 while (WINDOWP (window))
12866 struct window *w = XWINDOW (window);
12868 if (WINDOWP (w->contents))
12869 hscrolled_p |= hscroll_window_tree (w->contents);
12870 else if (w->cursor.vpos >= 0)
12872 int h_margin;
12873 int text_area_width;
12874 struct glyph_row *cursor_row;
12875 struct glyph_row *bottom_row;
12876 int row_r2l_p;
12878 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12879 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12880 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12881 else
12882 cursor_row = bottom_row - 1;
12884 if (!cursor_row->enabled_p)
12886 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12887 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12888 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12889 else
12890 cursor_row = bottom_row - 1;
12892 row_r2l_p = cursor_row->reversed_p;
12894 text_area_width = window_box_width (w, TEXT_AREA);
12896 /* Scroll when cursor is inside this scroll margin. */
12897 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12899 /* If the position of this window's point has explicitly
12900 changed, no more suspend auto hscrolling. */
12901 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12902 w->suspend_auto_hscroll = 0;
12904 /* Remember window point. */
12905 Fset_marker (w->old_pointm,
12906 ((w == XWINDOW (selected_window))
12907 ? make_number (BUF_PT (XBUFFER (w->contents)))
12908 : Fmarker_position (w->pointm)),
12909 w->contents);
12911 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12912 && w->suspend_auto_hscroll == 0
12913 /* In some pathological cases, like restoring a window
12914 configuration into a frame that is much smaller than
12915 the one from which the configuration was saved, we
12916 get glyph rows whose start and end have zero buffer
12917 positions, which we cannot handle below. Just skip
12918 such windows. */
12919 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12920 /* For left-to-right rows, hscroll when cursor is either
12921 (i) inside the right hscroll margin, or (ii) if it is
12922 inside the left margin and the window is already
12923 hscrolled. */
12924 && ((!row_r2l_p
12925 && ((w->hscroll && w->cursor.x <= h_margin)
12926 || (cursor_row->enabled_p
12927 && cursor_row->truncated_on_right_p
12928 && (w->cursor.x >= text_area_width - h_margin))))
12929 /* For right-to-left rows, the logic is similar,
12930 except that rules for scrolling to left and right
12931 are reversed. E.g., if cursor.x <= h_margin, we
12932 need to hscroll "to the right" unconditionally,
12933 and that will scroll the screen to the left so as
12934 to reveal the next portion of the row. */
12935 || (row_r2l_p
12936 && ((cursor_row->enabled_p
12937 /* FIXME: It is confusing to set the
12938 truncated_on_right_p flag when R2L rows
12939 are actually truncated on the left. */
12940 && cursor_row->truncated_on_right_p
12941 && w->cursor.x <= h_margin)
12942 || (w->hscroll
12943 && (w->cursor.x >= text_area_width - h_margin))))))
12945 struct it it;
12946 ptrdiff_t hscroll;
12947 struct buffer *saved_current_buffer;
12948 ptrdiff_t pt;
12949 int wanted_x;
12951 /* Find point in a display of infinite width. */
12952 saved_current_buffer = current_buffer;
12953 current_buffer = XBUFFER (w->contents);
12955 if (w == XWINDOW (selected_window))
12956 pt = PT;
12957 else
12958 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12960 /* Move iterator to pt starting at cursor_row->start in
12961 a line with infinite width. */
12962 init_to_row_start (&it, w, cursor_row);
12963 it.last_visible_x = INFINITY;
12964 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12965 current_buffer = saved_current_buffer;
12967 /* Position cursor in window. */
12968 if (!hscroll_relative_p && hscroll_step_abs == 0)
12969 hscroll = max (0, (it.current_x
12970 - (ITERATOR_AT_END_OF_LINE_P (&it)
12971 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12972 : (text_area_width / 2))))
12973 / FRAME_COLUMN_WIDTH (it.f);
12974 else if ((!row_r2l_p
12975 && w->cursor.x >= text_area_width - h_margin)
12976 || (row_r2l_p && w->cursor.x <= h_margin))
12978 if (hscroll_relative_p)
12979 wanted_x = text_area_width * (1 - hscroll_step_rel)
12980 - h_margin;
12981 else
12982 wanted_x = text_area_width
12983 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12984 - h_margin;
12985 hscroll
12986 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12988 else
12990 if (hscroll_relative_p)
12991 wanted_x = text_area_width * hscroll_step_rel
12992 + h_margin;
12993 else
12994 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12995 + h_margin;
12996 hscroll
12997 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12999 hscroll = max (hscroll, w->min_hscroll);
13001 /* Don't prevent redisplay optimizations if hscroll
13002 hasn't changed, as it will unnecessarily slow down
13003 redisplay. */
13004 if (w->hscroll != hscroll)
13006 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
13007 w->hscroll = hscroll;
13008 hscrolled_p = 1;
13013 window = w->next;
13016 /* Value is non-zero if hscroll of any leaf window has been changed. */
13017 return hscrolled_p;
13021 /* Set hscroll so that cursor is visible and not inside horizontal
13022 scroll margins for all windows in the tree rooted at WINDOW. See
13023 also hscroll_window_tree above. Value is non-zero if any window's
13024 hscroll has been changed. If it has, desired matrices on the frame
13025 of WINDOW are cleared. */
13027 static int
13028 hscroll_windows (Lisp_Object window)
13030 int hscrolled_p = hscroll_window_tree (window);
13031 if (hscrolled_p)
13032 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13033 return hscrolled_p;
13038 /************************************************************************
13039 Redisplay
13040 ************************************************************************/
13042 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
13043 to a non-zero value. This is sometimes handy to have in a debugger
13044 session. */
13046 #ifdef GLYPH_DEBUG
13048 /* First and last unchanged row for try_window_id. */
13050 static int debug_first_unchanged_at_end_vpos;
13051 static int debug_last_unchanged_at_beg_vpos;
13053 /* Delta vpos and y. */
13055 static int debug_dvpos, debug_dy;
13057 /* Delta in characters and bytes for try_window_id. */
13059 static ptrdiff_t debug_delta, debug_delta_bytes;
13061 /* Values of window_end_pos and window_end_vpos at the end of
13062 try_window_id. */
13064 static ptrdiff_t debug_end_vpos;
13066 /* Append a string to W->desired_matrix->method. FMT is a printf
13067 format string. If trace_redisplay_p is true also printf the
13068 resulting string to stderr. */
13070 static void debug_method_add (struct window *, char const *, ...)
13071 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13073 static void
13074 debug_method_add (struct window *w, char const *fmt, ...)
13076 void *ptr = w;
13077 char *method = w->desired_matrix->method;
13078 int len = strlen (method);
13079 int size = sizeof w->desired_matrix->method;
13080 int remaining = size - len - 1;
13081 va_list ap;
13083 if (len && remaining)
13085 method[len] = '|';
13086 --remaining, ++len;
13089 va_start (ap, fmt);
13090 vsnprintf (method + len, remaining + 1, fmt, ap);
13091 va_end (ap);
13093 if (trace_redisplay_p)
13094 fprintf (stderr, "%p (%s): %s\n",
13095 ptr,
13096 ((BUFFERP (w->contents)
13097 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13098 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13099 : "no buffer"),
13100 method + len);
13103 #endif /* GLYPH_DEBUG */
13106 /* Value is non-zero if all changes in window W, which displays
13107 current_buffer, are in the text between START and END. START is a
13108 buffer position, END is given as a distance from Z. Used in
13109 redisplay_internal for display optimization. */
13111 static int
13112 text_outside_line_unchanged_p (struct window *w,
13113 ptrdiff_t start, ptrdiff_t end)
13115 int unchanged_p = 1;
13117 /* If text or overlays have changed, see where. */
13118 if (window_outdated (w))
13120 /* Gap in the line? */
13121 if (GPT < start || Z - GPT < end)
13122 unchanged_p = 0;
13124 /* Changes start in front of the line, or end after it? */
13125 if (unchanged_p
13126 && (BEG_UNCHANGED < start - 1
13127 || END_UNCHANGED < end))
13128 unchanged_p = 0;
13130 /* If selective display, can't optimize if changes start at the
13131 beginning of the line. */
13132 if (unchanged_p
13133 && INTEGERP (BVAR (current_buffer, selective_display))
13134 && XINT (BVAR (current_buffer, selective_display)) > 0
13135 && (BEG_UNCHANGED < start || GPT <= start))
13136 unchanged_p = 0;
13138 /* If there are overlays at the start or end of the line, these
13139 may have overlay strings with newlines in them. A change at
13140 START, for instance, may actually concern the display of such
13141 overlay strings as well, and they are displayed on different
13142 lines. So, quickly rule out this case. (For the future, it
13143 might be desirable to implement something more telling than
13144 just BEG/END_UNCHANGED.) */
13145 if (unchanged_p)
13147 if (BEG + BEG_UNCHANGED == start
13148 && overlay_touches_p (start))
13149 unchanged_p = 0;
13150 if (END_UNCHANGED == end
13151 && overlay_touches_p (Z - end))
13152 unchanged_p = 0;
13155 /* Under bidi reordering, adding or deleting a character in the
13156 beginning of a paragraph, before the first strong directional
13157 character, can change the base direction of the paragraph (unless
13158 the buffer specifies a fixed paragraph direction), which will
13159 require to redisplay the whole paragraph. It might be worthwhile
13160 to find the paragraph limits and widen the range of redisplayed
13161 lines to that, but for now just give up this optimization. */
13162 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13163 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13164 unchanged_p = 0;
13167 return unchanged_p;
13171 /* Do a frame update, taking possible shortcuts into account. This is
13172 the main external entry point for redisplay.
13174 If the last redisplay displayed an echo area message and that message
13175 is no longer requested, we clear the echo area or bring back the
13176 mini-buffer if that is in use. */
13178 void
13179 redisplay (void)
13181 redisplay_internal ();
13185 static Lisp_Object
13186 overlay_arrow_string_or_property (Lisp_Object var)
13188 Lisp_Object val;
13190 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13191 return val;
13193 return Voverlay_arrow_string;
13196 /* Return 1 if there are any overlay-arrows in current_buffer. */
13197 static int
13198 overlay_arrow_in_current_buffer_p (void)
13200 Lisp_Object vlist;
13202 for (vlist = Voverlay_arrow_variable_list;
13203 CONSP (vlist);
13204 vlist = XCDR (vlist))
13206 Lisp_Object var = XCAR (vlist);
13207 Lisp_Object val;
13209 if (!SYMBOLP (var))
13210 continue;
13211 val = find_symbol_value (var);
13212 if (MARKERP (val)
13213 && current_buffer == XMARKER (val)->buffer)
13214 return 1;
13216 return 0;
13220 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13221 has changed. */
13223 static int
13224 overlay_arrows_changed_p (void)
13226 Lisp_Object vlist;
13228 for (vlist = Voverlay_arrow_variable_list;
13229 CONSP (vlist);
13230 vlist = XCDR (vlist))
13232 Lisp_Object var = XCAR (vlist);
13233 Lisp_Object val, pstr;
13235 if (!SYMBOLP (var))
13236 continue;
13237 val = find_symbol_value (var);
13238 if (!MARKERP (val))
13239 continue;
13240 if (! EQ (COERCE_MARKER (val),
13241 Fget (var, Qlast_arrow_position))
13242 || ! (pstr = overlay_arrow_string_or_property (var),
13243 EQ (pstr, Fget (var, Qlast_arrow_string))))
13244 return 1;
13246 return 0;
13249 /* Mark overlay arrows to be updated on next redisplay. */
13251 static void
13252 update_overlay_arrows (int up_to_date)
13254 Lisp_Object vlist;
13256 for (vlist = Voverlay_arrow_variable_list;
13257 CONSP (vlist);
13258 vlist = XCDR (vlist))
13260 Lisp_Object var = XCAR (vlist);
13262 if (!SYMBOLP (var))
13263 continue;
13265 if (up_to_date > 0)
13267 Lisp_Object val = find_symbol_value (var);
13268 Fput (var, Qlast_arrow_position,
13269 COERCE_MARKER (val));
13270 Fput (var, Qlast_arrow_string,
13271 overlay_arrow_string_or_property (var));
13273 else if (up_to_date < 0
13274 || !NILP (Fget (var, Qlast_arrow_position)))
13276 Fput (var, Qlast_arrow_position, Qt);
13277 Fput (var, Qlast_arrow_string, Qt);
13283 /* Return overlay arrow string to display at row.
13284 Return integer (bitmap number) for arrow bitmap in left fringe.
13285 Return nil if no overlay arrow. */
13287 static Lisp_Object
13288 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13290 Lisp_Object vlist;
13292 for (vlist = Voverlay_arrow_variable_list;
13293 CONSP (vlist);
13294 vlist = XCDR (vlist))
13296 Lisp_Object var = XCAR (vlist);
13297 Lisp_Object val;
13299 if (!SYMBOLP (var))
13300 continue;
13302 val = find_symbol_value (var);
13304 if (MARKERP (val)
13305 && current_buffer == XMARKER (val)->buffer
13306 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13308 if (FRAME_WINDOW_P (it->f)
13309 /* FIXME: if ROW->reversed_p is set, this should test
13310 the right fringe, not the left one. */
13311 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13313 #ifdef HAVE_WINDOW_SYSTEM
13314 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13316 int fringe_bitmap;
13317 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13318 return make_number (fringe_bitmap);
13320 #endif
13321 return make_number (-1); /* Use default arrow bitmap. */
13323 return overlay_arrow_string_or_property (var);
13327 return Qnil;
13330 /* Return 1 if point moved out of or into a composition. Otherwise
13331 return 0. PREV_BUF and PREV_PT are the last point buffer and
13332 position. BUF and PT are the current point buffer and position. */
13334 static int
13335 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13336 struct buffer *buf, ptrdiff_t pt)
13338 ptrdiff_t start, end;
13339 Lisp_Object prop;
13340 Lisp_Object buffer;
13342 XSETBUFFER (buffer, buf);
13343 /* Check a composition at the last point if point moved within the
13344 same buffer. */
13345 if (prev_buf == buf)
13347 if (prev_pt == pt)
13348 /* Point didn't move. */
13349 return 0;
13351 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13352 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13353 && composition_valid_p (start, end, prop)
13354 && start < prev_pt && end > prev_pt)
13355 /* The last point was within the composition. Return 1 iff
13356 point moved out of the composition. */
13357 return (pt <= start || pt >= end);
13360 /* Check a composition at the current point. */
13361 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13362 && find_composition (pt, -1, &start, &end, &prop, buffer)
13363 && composition_valid_p (start, end, prop)
13364 && start < pt && end > pt);
13367 /* Reconsider the clip changes of buffer which is displayed in W. */
13369 static void
13370 reconsider_clip_changes (struct window *w)
13372 struct buffer *b = XBUFFER (w->contents);
13374 if (b->clip_changed
13375 && w->window_end_valid
13376 && w->current_matrix->buffer == b
13377 && w->current_matrix->zv == BUF_ZV (b)
13378 && w->current_matrix->begv == BUF_BEGV (b))
13379 b->clip_changed = 0;
13381 /* If display wasn't paused, and W is not a tool bar window, see if
13382 point has been moved into or out of a composition. In that case,
13383 we set b->clip_changed to 1 to force updating the screen. If
13384 b->clip_changed has already been set to 1, we can skip this
13385 check. */
13386 if (!b->clip_changed && w->window_end_valid)
13388 ptrdiff_t pt = (w == XWINDOW (selected_window)
13389 ? PT : marker_position (w->pointm));
13391 if ((w->current_matrix->buffer != b || pt != w->last_point)
13392 && check_point_in_composition (w->current_matrix->buffer,
13393 w->last_point, b, pt))
13394 b->clip_changed = 1;
13398 static void
13399 propagate_buffer_redisplay (void)
13400 { /* Resetting b->text->redisplay is problematic!
13401 We can't just reset it in the case that some window that displays
13402 it has not been redisplayed; and such a window can stay
13403 unredisplayed for a long time if it's currently invisible.
13404 But we do want to reset it at the end of redisplay otherwise
13405 its displayed windows will keep being redisplayed over and over
13406 again.
13407 So we copy all b->text->redisplay flags up to their windows here,
13408 such that mark_window_display_accurate can safely reset
13409 b->text->redisplay. */
13410 Lisp_Object ws = window_list ();
13411 for (; CONSP (ws); ws = XCDR (ws))
13413 struct window *thisw = XWINDOW (XCAR (ws));
13414 struct buffer *thisb = XBUFFER (thisw->contents);
13415 if (thisb->text->redisplay)
13416 thisw->redisplay = true;
13420 #define STOP_POLLING \
13421 do { if (! polling_stopped_here) stop_polling (); \
13422 polling_stopped_here = 1; } while (0)
13424 #define RESUME_POLLING \
13425 do { if (polling_stopped_here) start_polling (); \
13426 polling_stopped_here = 0; } while (0)
13429 /* Perhaps in the future avoid recentering windows if it
13430 is not necessary; currently that causes some problems. */
13432 static void
13433 redisplay_internal (void)
13435 struct window *w = XWINDOW (selected_window);
13436 struct window *sw;
13437 struct frame *fr;
13438 int pending;
13439 bool must_finish = 0, match_p;
13440 struct text_pos tlbufpos, tlendpos;
13441 int number_of_visible_frames;
13442 ptrdiff_t count;
13443 struct frame *sf;
13444 int polling_stopped_here = 0;
13445 Lisp_Object tail, frame;
13447 /* True means redisplay has to consider all windows on all
13448 frames. False, only selected_window is considered. */
13449 bool consider_all_windows_p;
13451 /* True means redisplay has to redisplay the miniwindow. */
13452 bool update_miniwindow_p = false;
13454 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13456 /* No redisplay if running in batch mode or frame is not yet fully
13457 initialized, or redisplay is explicitly turned off by setting
13458 Vinhibit_redisplay. */
13459 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13460 || !NILP (Vinhibit_redisplay))
13461 return;
13463 /* Don't examine these until after testing Vinhibit_redisplay.
13464 When Emacs is shutting down, perhaps because its connection to
13465 X has dropped, we should not look at them at all. */
13466 fr = XFRAME (w->frame);
13467 sf = SELECTED_FRAME ();
13469 if (!fr->glyphs_initialized_p)
13470 return;
13472 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13473 if (popup_activated ())
13474 return;
13475 #endif
13477 /* I don't think this happens but let's be paranoid. */
13478 if (redisplaying_p)
13479 return;
13481 /* Record a function that clears redisplaying_p
13482 when we leave this function. */
13483 count = SPECPDL_INDEX ();
13484 record_unwind_protect_void (unwind_redisplay);
13485 redisplaying_p = 1;
13486 specbind (Qinhibit_free_realized_faces, Qnil);
13488 /* Record this function, so it appears on the profiler's backtraces. */
13489 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13491 FOR_EACH_FRAME (tail, frame)
13492 XFRAME (frame)->already_hscrolled_p = 0;
13494 retry:
13495 /* Remember the currently selected window. */
13496 sw = w;
13498 pending = 0;
13499 last_escape_glyph_frame = NULL;
13500 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13501 last_glyphless_glyph_frame = NULL;
13502 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13504 /* If face_change_count is non-zero, init_iterator will free all
13505 realized faces, which includes the faces referenced from current
13506 matrices. So, we can't reuse current matrices in this case. */
13507 if (face_change_count)
13508 windows_or_buffers_changed = 47;
13510 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13511 && FRAME_TTY (sf)->previous_frame != sf)
13513 /* Since frames on a single ASCII terminal share the same
13514 display area, displaying a different frame means redisplay
13515 the whole thing. */
13516 SET_FRAME_GARBAGED (sf);
13517 #ifndef DOS_NT
13518 set_tty_color_mode (FRAME_TTY (sf), sf);
13519 #endif
13520 FRAME_TTY (sf)->previous_frame = sf;
13523 /* Set the visible flags for all frames. Do this before checking for
13524 resized or garbaged frames; they want to know if their frames are
13525 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13526 number_of_visible_frames = 0;
13528 FOR_EACH_FRAME (tail, frame)
13530 struct frame *f = XFRAME (frame);
13532 if (FRAME_VISIBLE_P (f))
13534 ++number_of_visible_frames;
13535 /* Adjust matrices for visible frames only. */
13536 if (f->fonts_changed)
13538 adjust_frame_glyphs (f);
13539 f->fonts_changed = 0;
13541 /* If cursor type has been changed on the frame
13542 other than selected, consider all frames. */
13543 if (f != sf && f->cursor_type_changed)
13544 update_mode_lines = 31;
13546 clear_desired_matrices (f);
13549 /* Notice any pending interrupt request to change frame size. */
13550 do_pending_window_change (1);
13552 /* do_pending_window_change could change the selected_window due to
13553 frame resizing which makes the selected window too small. */
13554 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13555 sw = w;
13557 /* Clear frames marked as garbaged. */
13558 clear_garbaged_frames ();
13560 /* Build menubar and tool-bar items. */
13561 if (NILP (Vmemory_full))
13562 prepare_menu_bars ();
13564 reconsider_clip_changes (w);
13566 /* In most cases selected window displays current buffer. */
13567 match_p = XBUFFER (w->contents) == current_buffer;
13568 if (match_p)
13570 /* Detect case that we need to write or remove a star in the mode line. */
13571 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13572 w->update_mode_line = 1;
13574 if (mode_line_update_needed (w))
13575 w->update_mode_line = 1;
13577 /* If reconsider_clip_changes above decided that the narrowing
13578 in the current buffer changed, make sure all other windows
13579 showing that buffer will be redisplayed. */
13580 if (current_buffer->clip_changed)
13581 bset_update_mode_line (current_buffer);
13584 /* Normally the message* functions will have already displayed and
13585 updated the echo area, but the frame may have been trashed, or
13586 the update may have been preempted, so display the echo area
13587 again here. Checking message_cleared_p captures the case that
13588 the echo area should be cleared. */
13589 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13590 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13591 || (message_cleared_p
13592 && minibuf_level == 0
13593 /* If the mini-window is currently selected, this means the
13594 echo-area doesn't show through. */
13595 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13597 int window_height_changed_p = echo_area_display (0);
13599 if (message_cleared_p)
13600 update_miniwindow_p = true;
13602 must_finish = 1;
13604 /* If we don't display the current message, don't clear the
13605 message_cleared_p flag, because, if we did, we wouldn't clear
13606 the echo area in the next redisplay which doesn't preserve
13607 the echo area. */
13608 if (!display_last_displayed_message_p)
13609 message_cleared_p = 0;
13611 if (window_height_changed_p)
13613 windows_or_buffers_changed = 50;
13615 /* If window configuration was changed, frames may have been
13616 marked garbaged. Clear them or we will experience
13617 surprises wrt scrolling. */
13618 clear_garbaged_frames ();
13621 else if (EQ (selected_window, minibuf_window)
13622 && (current_buffer->clip_changed || window_outdated (w))
13623 && resize_mini_window (w, 0))
13625 /* Resized active mini-window to fit the size of what it is
13626 showing if its contents might have changed. */
13627 must_finish = 1;
13629 /* If window configuration was changed, frames may have been
13630 marked garbaged. Clear them or we will experience
13631 surprises wrt scrolling. */
13632 clear_garbaged_frames ();
13635 if (windows_or_buffers_changed && !update_mode_lines)
13636 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13637 only the windows's contents needs to be refreshed, or whether the
13638 mode-lines also need a refresh. */
13639 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13640 ? REDISPLAY_SOME : 32);
13642 /* If specs for an arrow have changed, do thorough redisplay
13643 to ensure we remove any arrow that should no longer exist. */
13644 if (overlay_arrows_changed_p ())
13645 /* Apparently, this is the only case where we update other windows,
13646 without updating other mode-lines. */
13647 windows_or_buffers_changed = 49;
13649 consider_all_windows_p = (update_mode_lines
13650 || windows_or_buffers_changed);
13652 #define AINC(a,i) \
13653 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13654 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13656 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13657 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13659 /* Optimize the case that only the line containing the cursor in the
13660 selected window has changed. Variables starting with this_ are
13661 set in display_line and record information about the line
13662 containing the cursor. */
13663 tlbufpos = this_line_start_pos;
13664 tlendpos = this_line_end_pos;
13665 if (!consider_all_windows_p
13666 && CHARPOS (tlbufpos) > 0
13667 && !w->update_mode_line
13668 && !current_buffer->clip_changed
13669 && !current_buffer->prevent_redisplay_optimizations_p
13670 && FRAME_VISIBLE_P (XFRAME (w->frame))
13671 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13672 && !XFRAME (w->frame)->cursor_type_changed
13673 /* Make sure recorded data applies to current buffer, etc. */
13674 && this_line_buffer == current_buffer
13675 && match_p
13676 && !w->force_start
13677 && !w->optional_new_start
13678 /* Point must be on the line that we have info recorded about. */
13679 && PT >= CHARPOS (tlbufpos)
13680 && PT <= Z - CHARPOS (tlendpos)
13681 /* All text outside that line, including its final newline,
13682 must be unchanged. */
13683 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13684 CHARPOS (tlendpos)))
13686 if (CHARPOS (tlbufpos) > BEGV
13687 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13688 && (CHARPOS (tlbufpos) == ZV
13689 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13690 /* Former continuation line has disappeared by becoming empty. */
13691 goto cancel;
13692 else if (window_outdated (w) || MINI_WINDOW_P (w))
13694 /* We have to handle the case of continuation around a
13695 wide-column character (see the comment in indent.c around
13696 line 1340).
13698 For instance, in the following case:
13700 -------- Insert --------
13701 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13702 J_I_ ==> J_I_ `^^' are cursors.
13703 ^^ ^^
13704 -------- --------
13706 As we have to redraw the line above, we cannot use this
13707 optimization. */
13709 struct it it;
13710 int line_height_before = this_line_pixel_height;
13712 /* Note that start_display will handle the case that the
13713 line starting at tlbufpos is a continuation line. */
13714 start_display (&it, w, tlbufpos);
13716 /* Implementation note: It this still necessary? */
13717 if (it.current_x != this_line_start_x)
13718 goto cancel;
13720 TRACE ((stderr, "trying display optimization 1\n"));
13721 w->cursor.vpos = -1;
13722 overlay_arrow_seen = 0;
13723 it.vpos = this_line_vpos;
13724 it.current_y = this_line_y;
13725 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13726 display_line (&it);
13728 /* If line contains point, is not continued,
13729 and ends at same distance from eob as before, we win. */
13730 if (w->cursor.vpos >= 0
13731 /* Line is not continued, otherwise this_line_start_pos
13732 would have been set to 0 in display_line. */
13733 && CHARPOS (this_line_start_pos)
13734 /* Line ends as before. */
13735 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13736 /* Line has same height as before. Otherwise other lines
13737 would have to be shifted up or down. */
13738 && this_line_pixel_height == line_height_before)
13740 /* If this is not the window's last line, we must adjust
13741 the charstarts of the lines below. */
13742 if (it.current_y < it.last_visible_y)
13744 struct glyph_row *row
13745 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13746 ptrdiff_t delta, delta_bytes;
13748 /* We used to distinguish between two cases here,
13749 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13750 when the line ends in a newline or the end of the
13751 buffer's accessible portion. But both cases did
13752 the same, so they were collapsed. */
13753 delta = (Z
13754 - CHARPOS (tlendpos)
13755 - MATRIX_ROW_START_CHARPOS (row));
13756 delta_bytes = (Z_BYTE
13757 - BYTEPOS (tlendpos)
13758 - MATRIX_ROW_START_BYTEPOS (row));
13760 increment_matrix_positions (w->current_matrix,
13761 this_line_vpos + 1,
13762 w->current_matrix->nrows,
13763 delta, delta_bytes);
13766 /* If this row displays text now but previously didn't,
13767 or vice versa, w->window_end_vpos may have to be
13768 adjusted. */
13769 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13771 if (w->window_end_vpos < this_line_vpos)
13772 w->window_end_vpos = this_line_vpos;
13774 else if (w->window_end_vpos == this_line_vpos
13775 && this_line_vpos > 0)
13776 w->window_end_vpos = this_line_vpos - 1;
13777 w->window_end_valid = 0;
13779 /* Update hint: No need to try to scroll in update_window. */
13780 w->desired_matrix->no_scrolling_p = 1;
13782 #ifdef GLYPH_DEBUG
13783 *w->desired_matrix->method = 0;
13784 debug_method_add (w, "optimization 1");
13785 #endif
13786 #ifdef HAVE_WINDOW_SYSTEM
13787 update_window_fringes (w, 0);
13788 #endif
13789 goto update;
13791 else
13792 goto cancel;
13794 else if (/* Cursor position hasn't changed. */
13795 PT == w->last_point
13796 /* Make sure the cursor was last displayed
13797 in this window. Otherwise we have to reposition it. */
13799 /* PXW: Must be converted to pixels, probably. */
13800 && 0 <= w->cursor.vpos
13801 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13803 if (!must_finish)
13805 do_pending_window_change (1);
13806 /* If selected_window changed, redisplay again. */
13807 if (WINDOWP (selected_window)
13808 && (w = XWINDOW (selected_window)) != sw)
13809 goto retry;
13811 /* We used to always goto end_of_redisplay here, but this
13812 isn't enough if we have a blinking cursor. */
13813 if (w->cursor_off_p == w->last_cursor_off_p)
13814 goto end_of_redisplay;
13816 goto update;
13818 /* If highlighting the region, or if the cursor is in the echo area,
13819 then we can't just move the cursor. */
13820 else if (NILP (Vshow_trailing_whitespace)
13821 && !cursor_in_echo_area)
13823 struct it it;
13824 struct glyph_row *row;
13826 /* Skip from tlbufpos to PT and see where it is. Note that
13827 PT may be in invisible text. If so, we will end at the
13828 next visible position. */
13829 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13830 NULL, DEFAULT_FACE_ID);
13831 it.current_x = this_line_start_x;
13832 it.current_y = this_line_y;
13833 it.vpos = this_line_vpos;
13835 /* The call to move_it_to stops in front of PT, but
13836 moves over before-strings. */
13837 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13839 if (it.vpos == this_line_vpos
13840 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13841 row->enabled_p))
13843 eassert (this_line_vpos == it.vpos);
13844 eassert (this_line_y == it.current_y);
13845 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13846 #ifdef GLYPH_DEBUG
13847 *w->desired_matrix->method = 0;
13848 debug_method_add (w, "optimization 3");
13849 #endif
13850 goto update;
13852 else
13853 goto cancel;
13856 cancel:
13857 /* Text changed drastically or point moved off of line. */
13858 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13861 CHARPOS (this_line_start_pos) = 0;
13862 ++clear_face_cache_count;
13863 #ifdef HAVE_WINDOW_SYSTEM
13864 ++clear_image_cache_count;
13865 #endif
13867 /* Build desired matrices, and update the display. If
13868 consider_all_windows_p is non-zero, do it for all windows on all
13869 frames. Otherwise do it for selected_window, only. */
13871 if (consider_all_windows_p)
13873 FOR_EACH_FRAME (tail, frame)
13874 XFRAME (frame)->updated_p = 0;
13876 propagate_buffer_redisplay ();
13878 FOR_EACH_FRAME (tail, frame)
13880 struct frame *f = XFRAME (frame);
13882 /* We don't have to do anything for unselected terminal
13883 frames. */
13884 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13885 && !EQ (FRAME_TTY (f)->top_frame, frame))
13886 continue;
13888 retry_frame:
13890 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13892 bool gcscrollbars
13893 /* Only GC scrollbars when we redisplay the whole frame. */
13894 = f->redisplay || !REDISPLAY_SOME_P ();
13895 /* Mark all the scroll bars to be removed; we'll redeem
13896 the ones we want when we redisplay their windows. */
13897 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13898 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13900 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13901 redisplay_windows (FRAME_ROOT_WINDOW (f));
13902 /* Remember that the invisible frames need to be redisplayed next
13903 time they're visible. */
13904 else if (!REDISPLAY_SOME_P ())
13905 f->redisplay = true;
13907 /* The X error handler may have deleted that frame. */
13908 if (!FRAME_LIVE_P (f))
13909 continue;
13911 /* Any scroll bars which redisplay_windows should have
13912 nuked should now go away. */
13913 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13914 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13916 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13918 /* If fonts changed on visible frame, display again. */
13919 if (f->fonts_changed)
13921 adjust_frame_glyphs (f);
13922 f->fonts_changed = 0;
13923 goto retry_frame;
13926 /* See if we have to hscroll. */
13927 if (!f->already_hscrolled_p)
13929 f->already_hscrolled_p = 1;
13930 if (hscroll_windows (f->root_window))
13931 goto retry_frame;
13934 /* Prevent various kinds of signals during display
13935 update. stdio is not robust about handling
13936 signals, which can cause an apparent I/O error. */
13937 if (interrupt_input)
13938 unrequest_sigio ();
13939 STOP_POLLING;
13941 pending |= update_frame (f, 0, 0);
13942 f->cursor_type_changed = 0;
13943 f->updated_p = 1;
13948 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13950 if (!pending)
13952 /* Do the mark_window_display_accurate after all windows have
13953 been redisplayed because this call resets flags in buffers
13954 which are needed for proper redisplay. */
13955 FOR_EACH_FRAME (tail, frame)
13957 struct frame *f = XFRAME (frame);
13958 if (f->updated_p)
13960 f->redisplay = false;
13961 mark_window_display_accurate (f->root_window, 1);
13962 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13963 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13968 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13970 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13971 struct frame *mini_frame;
13973 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13974 /* Use list_of_error, not Qerror, so that
13975 we catch only errors and don't run the debugger. */
13976 internal_condition_case_1 (redisplay_window_1, selected_window,
13977 list_of_error,
13978 redisplay_window_error);
13979 if (update_miniwindow_p)
13980 internal_condition_case_1 (redisplay_window_1, mini_window,
13981 list_of_error,
13982 redisplay_window_error);
13984 /* Compare desired and current matrices, perform output. */
13986 update:
13987 /* If fonts changed, display again. */
13988 if (sf->fonts_changed)
13989 goto retry;
13991 /* Prevent various kinds of signals during display update.
13992 stdio is not robust about handling signals,
13993 which can cause an apparent I/O error. */
13994 if (interrupt_input)
13995 unrequest_sigio ();
13996 STOP_POLLING;
13998 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14000 if (hscroll_windows (selected_window))
14001 goto retry;
14003 XWINDOW (selected_window)->must_be_updated_p = true;
14004 pending = update_frame (sf, 0, 0);
14005 sf->cursor_type_changed = 0;
14008 /* We may have called echo_area_display at the top of this
14009 function. If the echo area is on another frame, that may
14010 have put text on a frame other than the selected one, so the
14011 above call to update_frame would not have caught it. Catch
14012 it here. */
14013 mini_window = FRAME_MINIBUF_WINDOW (sf);
14014 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14016 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14018 XWINDOW (mini_window)->must_be_updated_p = true;
14019 pending |= update_frame (mini_frame, 0, 0);
14020 mini_frame->cursor_type_changed = 0;
14021 if (!pending && hscroll_windows (mini_window))
14022 goto retry;
14026 /* If display was paused because of pending input, make sure we do a
14027 thorough update the next time. */
14028 if (pending)
14030 /* Prevent the optimization at the beginning of
14031 redisplay_internal that tries a single-line update of the
14032 line containing the cursor in the selected window. */
14033 CHARPOS (this_line_start_pos) = 0;
14035 /* Let the overlay arrow be updated the next time. */
14036 update_overlay_arrows (0);
14038 /* If we pause after scrolling, some rows in the current
14039 matrices of some windows are not valid. */
14040 if (!WINDOW_FULL_WIDTH_P (w)
14041 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14042 update_mode_lines = 36;
14044 else
14046 if (!consider_all_windows_p)
14048 /* This has already been done above if
14049 consider_all_windows_p is set. */
14050 if (XBUFFER (w->contents)->text->redisplay
14051 && buffer_window_count (XBUFFER (w->contents)) > 1)
14052 /* This can happen if b->text->redisplay was set during
14053 jit-lock. */
14054 propagate_buffer_redisplay ();
14055 mark_window_display_accurate_1 (w, 1);
14057 /* Say overlay arrows are up to date. */
14058 update_overlay_arrows (1);
14060 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14061 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14064 update_mode_lines = 0;
14065 windows_or_buffers_changed = 0;
14068 /* Start SIGIO interrupts coming again. Having them off during the
14069 code above makes it less likely one will discard output, but not
14070 impossible, since there might be stuff in the system buffer here.
14071 But it is much hairier to try to do anything about that. */
14072 if (interrupt_input)
14073 request_sigio ();
14074 RESUME_POLLING;
14076 /* If a frame has become visible which was not before, redisplay
14077 again, so that we display it. Expose events for such a frame
14078 (which it gets when becoming visible) don't call the parts of
14079 redisplay constructing glyphs, so simply exposing a frame won't
14080 display anything in this case. So, we have to display these
14081 frames here explicitly. */
14082 if (!pending)
14084 int new_count = 0;
14086 FOR_EACH_FRAME (tail, frame)
14088 if (XFRAME (frame)->visible)
14089 new_count++;
14092 if (new_count != number_of_visible_frames)
14093 windows_or_buffers_changed = 52;
14096 /* Change frame size now if a change is pending. */
14097 do_pending_window_change (1);
14099 /* If we just did a pending size change, or have additional
14100 visible frames, or selected_window changed, redisplay again. */
14101 if ((windows_or_buffers_changed && !pending)
14102 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14103 goto retry;
14105 /* Clear the face and image caches.
14107 We used to do this only if consider_all_windows_p. But the cache
14108 needs to be cleared if a timer creates images in the current
14109 buffer (e.g. the test case in Bug#6230). */
14111 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14113 clear_face_cache (0);
14114 clear_face_cache_count = 0;
14117 #ifdef HAVE_WINDOW_SYSTEM
14118 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14120 clear_image_caches (Qnil);
14121 clear_image_cache_count = 0;
14123 #endif /* HAVE_WINDOW_SYSTEM */
14125 end_of_redisplay:
14126 if (interrupt_input && interrupts_deferred)
14127 request_sigio ();
14129 unbind_to (count, Qnil);
14130 RESUME_POLLING;
14134 /* Redisplay, but leave alone any recent echo area message unless
14135 another message has been requested in its place.
14137 This is useful in situations where you need to redisplay but no
14138 user action has occurred, making it inappropriate for the message
14139 area to be cleared. See tracking_off and
14140 wait_reading_process_output for examples of these situations.
14142 FROM_WHERE is an integer saying from where this function was
14143 called. This is useful for debugging. */
14145 void
14146 redisplay_preserve_echo_area (int from_where)
14148 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14150 if (!NILP (echo_area_buffer[1]))
14152 /* We have a previously displayed message, but no current
14153 message. Redisplay the previous message. */
14154 display_last_displayed_message_p = 1;
14155 redisplay_internal ();
14156 display_last_displayed_message_p = 0;
14158 else
14159 redisplay_internal ();
14161 flush_frame (SELECTED_FRAME ());
14165 /* Function registered with record_unwind_protect in redisplay_internal. */
14167 static void
14168 unwind_redisplay (void)
14170 redisplaying_p = 0;
14174 /* Mark the display of leaf window W as accurate or inaccurate.
14175 If ACCURATE_P is non-zero mark display of W as accurate. If
14176 ACCURATE_P is zero, arrange for W to be redisplayed the next
14177 time redisplay_internal is called. */
14179 static void
14180 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14182 struct buffer *b = XBUFFER (w->contents);
14184 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14185 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14186 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14188 if (accurate_p)
14190 b->clip_changed = false;
14191 b->prevent_redisplay_optimizations_p = false;
14192 eassert (buffer_window_count (b) > 0);
14193 /* Resetting b->text->redisplay is problematic!
14194 In order to make it safer to do it here, redisplay_internal must
14195 have copied all b->text->redisplay to their respective windows. */
14196 b->text->redisplay = false;
14198 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14199 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14200 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14201 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14203 w->current_matrix->buffer = b;
14204 w->current_matrix->begv = BUF_BEGV (b);
14205 w->current_matrix->zv = BUF_ZV (b);
14207 w->last_cursor_vpos = w->cursor.vpos;
14208 w->last_cursor_off_p = w->cursor_off_p;
14210 if (w == XWINDOW (selected_window))
14211 w->last_point = BUF_PT (b);
14212 else
14213 w->last_point = marker_position (w->pointm);
14215 w->window_end_valid = true;
14216 w->update_mode_line = false;
14219 w->redisplay = !accurate_p;
14223 /* Mark the display of windows in the window tree rooted at WINDOW as
14224 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14225 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14226 be redisplayed the next time redisplay_internal is called. */
14228 void
14229 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14231 struct window *w;
14233 for (; !NILP (window); window = w->next)
14235 w = XWINDOW (window);
14236 if (WINDOWP (w->contents))
14237 mark_window_display_accurate (w->contents, accurate_p);
14238 else
14239 mark_window_display_accurate_1 (w, accurate_p);
14242 if (accurate_p)
14243 update_overlay_arrows (1);
14244 else
14245 /* Force a thorough redisplay the next time by setting
14246 last_arrow_position and last_arrow_string to t, which is
14247 unequal to any useful value of Voverlay_arrow_... */
14248 update_overlay_arrows (-1);
14252 /* Return value in display table DP (Lisp_Char_Table *) for character
14253 C. Since a display table doesn't have any parent, we don't have to
14254 follow parent. Do not call this function directly but use the
14255 macro DISP_CHAR_VECTOR. */
14257 Lisp_Object
14258 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14260 Lisp_Object val;
14262 if (ASCII_CHAR_P (c))
14264 val = dp->ascii;
14265 if (SUB_CHAR_TABLE_P (val))
14266 val = XSUB_CHAR_TABLE (val)->contents[c];
14268 else
14270 Lisp_Object table;
14272 XSETCHAR_TABLE (table, dp);
14273 val = char_table_ref (table, c);
14275 if (NILP (val))
14276 val = dp->defalt;
14277 return val;
14282 /***********************************************************************
14283 Window Redisplay
14284 ***********************************************************************/
14286 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14288 static void
14289 redisplay_windows (Lisp_Object window)
14291 while (!NILP (window))
14293 struct window *w = XWINDOW (window);
14295 if (WINDOWP (w->contents))
14296 redisplay_windows (w->contents);
14297 else if (BUFFERP (w->contents))
14299 displayed_buffer = XBUFFER (w->contents);
14300 /* Use list_of_error, not Qerror, so that
14301 we catch only errors and don't run the debugger. */
14302 internal_condition_case_1 (redisplay_window_0, window,
14303 list_of_error,
14304 redisplay_window_error);
14307 window = w->next;
14311 static Lisp_Object
14312 redisplay_window_error (Lisp_Object ignore)
14314 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14315 return Qnil;
14318 static Lisp_Object
14319 redisplay_window_0 (Lisp_Object window)
14321 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14322 redisplay_window (window, false);
14323 return Qnil;
14326 static Lisp_Object
14327 redisplay_window_1 (Lisp_Object window)
14329 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14330 redisplay_window (window, true);
14331 return Qnil;
14335 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14336 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14337 which positions recorded in ROW differ from current buffer
14338 positions.
14340 Return 0 if cursor is not on this row, 1 otherwise. */
14342 static int
14343 set_cursor_from_row (struct window *w, struct glyph_row *row,
14344 struct glyph_matrix *matrix,
14345 ptrdiff_t delta, ptrdiff_t delta_bytes,
14346 int dy, int dvpos)
14348 struct glyph *glyph = row->glyphs[TEXT_AREA];
14349 struct glyph *end = glyph + row->used[TEXT_AREA];
14350 struct glyph *cursor = NULL;
14351 /* The last known character position in row. */
14352 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14353 int x = row->x;
14354 ptrdiff_t pt_old = PT - delta;
14355 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14356 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14357 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14358 /* A glyph beyond the edge of TEXT_AREA which we should never
14359 touch. */
14360 struct glyph *glyphs_end = end;
14361 /* Non-zero means we've found a match for cursor position, but that
14362 glyph has the avoid_cursor_p flag set. */
14363 int match_with_avoid_cursor = 0;
14364 /* Non-zero means we've seen at least one glyph that came from a
14365 display string. */
14366 int string_seen = 0;
14367 /* Largest and smallest buffer positions seen so far during scan of
14368 glyph row. */
14369 ptrdiff_t bpos_max = pos_before;
14370 ptrdiff_t bpos_min = pos_after;
14371 /* Last buffer position covered by an overlay string with an integer
14372 `cursor' property. */
14373 ptrdiff_t bpos_covered = 0;
14374 /* Non-zero means the display string on which to display the cursor
14375 comes from a text property, not from an overlay. */
14376 int string_from_text_prop = 0;
14378 /* Don't even try doing anything if called for a mode-line or
14379 header-line row, since the rest of the code isn't prepared to
14380 deal with such calamities. */
14381 eassert (!row->mode_line_p);
14382 if (row->mode_line_p)
14383 return 0;
14385 /* Skip over glyphs not having an object at the start and the end of
14386 the row. These are special glyphs like truncation marks on
14387 terminal frames. */
14388 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14390 if (!row->reversed_p)
14392 while (glyph < end
14393 && INTEGERP (glyph->object)
14394 && glyph->charpos < 0)
14396 x += glyph->pixel_width;
14397 ++glyph;
14399 while (end > glyph
14400 && INTEGERP ((end - 1)->object)
14401 /* CHARPOS is zero for blanks and stretch glyphs
14402 inserted by extend_face_to_end_of_line. */
14403 && (end - 1)->charpos <= 0)
14404 --end;
14405 glyph_before = glyph - 1;
14406 glyph_after = end;
14408 else
14410 struct glyph *g;
14412 /* If the glyph row is reversed, we need to process it from back
14413 to front, so swap the edge pointers. */
14414 glyphs_end = end = glyph - 1;
14415 glyph += row->used[TEXT_AREA] - 1;
14417 while (glyph > end + 1
14418 && INTEGERP (glyph->object)
14419 && glyph->charpos < 0)
14421 --glyph;
14422 x -= glyph->pixel_width;
14424 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14425 --glyph;
14426 /* By default, in reversed rows we put the cursor on the
14427 rightmost (first in the reading order) glyph. */
14428 for (g = end + 1; g < glyph; g++)
14429 x += g->pixel_width;
14430 while (end < glyph
14431 && INTEGERP ((end + 1)->object)
14432 && (end + 1)->charpos <= 0)
14433 ++end;
14434 glyph_before = glyph + 1;
14435 glyph_after = end;
14438 else if (row->reversed_p)
14440 /* In R2L rows that don't display text, put the cursor on the
14441 rightmost glyph. Case in point: an empty last line that is
14442 part of an R2L paragraph. */
14443 cursor = end - 1;
14444 /* Avoid placing the cursor on the last glyph of the row, where
14445 on terminal frames we hold the vertical border between
14446 adjacent windows. */
14447 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14448 && !WINDOW_RIGHTMOST_P (w)
14449 && cursor == row->glyphs[LAST_AREA] - 1)
14450 cursor--;
14451 x = -1; /* will be computed below, at label compute_x */
14454 /* Step 1: Try to find the glyph whose character position
14455 corresponds to point. If that's not possible, find 2 glyphs
14456 whose character positions are the closest to point, one before
14457 point, the other after it. */
14458 if (!row->reversed_p)
14459 while (/* not marched to end of glyph row */
14460 glyph < end
14461 /* glyph was not inserted by redisplay for internal purposes */
14462 && !INTEGERP (glyph->object))
14464 if (BUFFERP (glyph->object))
14466 ptrdiff_t dpos = glyph->charpos - pt_old;
14468 if (glyph->charpos > bpos_max)
14469 bpos_max = glyph->charpos;
14470 if (glyph->charpos < bpos_min)
14471 bpos_min = glyph->charpos;
14472 if (!glyph->avoid_cursor_p)
14474 /* If we hit point, we've found the glyph on which to
14475 display the cursor. */
14476 if (dpos == 0)
14478 match_with_avoid_cursor = 0;
14479 break;
14481 /* See if we've found a better approximation to
14482 POS_BEFORE or to POS_AFTER. */
14483 if (0 > dpos && dpos > pos_before - pt_old)
14485 pos_before = glyph->charpos;
14486 glyph_before = glyph;
14488 else if (0 < dpos && dpos < pos_after - pt_old)
14490 pos_after = glyph->charpos;
14491 glyph_after = glyph;
14494 else if (dpos == 0)
14495 match_with_avoid_cursor = 1;
14497 else if (STRINGP (glyph->object))
14499 Lisp_Object chprop;
14500 ptrdiff_t glyph_pos = glyph->charpos;
14502 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14503 glyph->object);
14504 if (!NILP (chprop))
14506 /* If the string came from a `display' text property,
14507 look up the buffer position of that property and
14508 use that position to update bpos_max, as if we
14509 actually saw such a position in one of the row's
14510 glyphs. This helps with supporting integer values
14511 of `cursor' property on the display string in
14512 situations where most or all of the row's buffer
14513 text is completely covered by display properties,
14514 so that no glyph with valid buffer positions is
14515 ever seen in the row. */
14516 ptrdiff_t prop_pos =
14517 string_buffer_position_lim (glyph->object, pos_before,
14518 pos_after, 0);
14520 if (prop_pos >= pos_before)
14521 bpos_max = prop_pos;
14523 if (INTEGERP (chprop))
14525 bpos_covered = bpos_max + XINT (chprop);
14526 /* If the `cursor' property covers buffer positions up
14527 to and including point, we should display cursor on
14528 this glyph. Note that, if a `cursor' property on one
14529 of the string's characters has an integer value, we
14530 will break out of the loop below _before_ we get to
14531 the position match above. IOW, integer values of
14532 the `cursor' property override the "exact match for
14533 point" strategy of positioning the cursor. */
14534 /* Implementation note: bpos_max == pt_old when, e.g.,
14535 we are in an empty line, where bpos_max is set to
14536 MATRIX_ROW_START_CHARPOS, see above. */
14537 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14539 cursor = glyph;
14540 break;
14544 string_seen = 1;
14546 x += glyph->pixel_width;
14547 ++glyph;
14549 else if (glyph > end) /* row is reversed */
14550 while (!INTEGERP (glyph->object))
14552 if (BUFFERP (glyph->object))
14554 ptrdiff_t dpos = glyph->charpos - pt_old;
14556 if (glyph->charpos > bpos_max)
14557 bpos_max = glyph->charpos;
14558 if (glyph->charpos < bpos_min)
14559 bpos_min = glyph->charpos;
14560 if (!glyph->avoid_cursor_p)
14562 if (dpos == 0)
14564 match_with_avoid_cursor = 0;
14565 break;
14567 if (0 > dpos && dpos > pos_before - pt_old)
14569 pos_before = glyph->charpos;
14570 glyph_before = glyph;
14572 else if (0 < dpos && dpos < pos_after - pt_old)
14574 pos_after = glyph->charpos;
14575 glyph_after = glyph;
14578 else if (dpos == 0)
14579 match_with_avoid_cursor = 1;
14581 else if (STRINGP (glyph->object))
14583 Lisp_Object chprop;
14584 ptrdiff_t glyph_pos = glyph->charpos;
14586 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14587 glyph->object);
14588 if (!NILP (chprop))
14590 ptrdiff_t prop_pos =
14591 string_buffer_position_lim (glyph->object, pos_before,
14592 pos_after, 0);
14594 if (prop_pos >= pos_before)
14595 bpos_max = prop_pos;
14597 if (INTEGERP (chprop))
14599 bpos_covered = bpos_max + XINT (chprop);
14600 /* If the `cursor' property covers buffer positions up
14601 to and including point, we should display cursor on
14602 this glyph. */
14603 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14605 cursor = glyph;
14606 break;
14609 string_seen = 1;
14611 --glyph;
14612 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14614 x--; /* can't use any pixel_width */
14615 break;
14617 x -= glyph->pixel_width;
14620 /* Step 2: If we didn't find an exact match for point, we need to
14621 look for a proper place to put the cursor among glyphs between
14622 GLYPH_BEFORE and GLYPH_AFTER. */
14623 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14624 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14625 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14627 /* An empty line has a single glyph whose OBJECT is zero and
14628 whose CHARPOS is the position of a newline on that line.
14629 Note that on a TTY, there are more glyphs after that, which
14630 were produced by extend_face_to_end_of_line, but their
14631 CHARPOS is zero or negative. */
14632 int empty_line_p =
14633 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14634 && INTEGERP (glyph->object) && glyph->charpos > 0
14635 /* On a TTY, continued and truncated rows also have a glyph at
14636 their end whose OBJECT is zero and whose CHARPOS is
14637 positive (the continuation and truncation glyphs), but such
14638 rows are obviously not "empty". */
14639 && !(row->continued_p || row->truncated_on_right_p);
14641 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14643 ptrdiff_t ellipsis_pos;
14645 /* Scan back over the ellipsis glyphs. */
14646 if (!row->reversed_p)
14648 ellipsis_pos = (glyph - 1)->charpos;
14649 while (glyph > row->glyphs[TEXT_AREA]
14650 && (glyph - 1)->charpos == ellipsis_pos)
14651 glyph--, x -= glyph->pixel_width;
14652 /* That loop always goes one position too far, including
14653 the glyph before the ellipsis. So scan forward over
14654 that one. */
14655 x += glyph->pixel_width;
14656 glyph++;
14658 else /* row is reversed */
14660 ellipsis_pos = (glyph + 1)->charpos;
14661 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14662 && (glyph + 1)->charpos == ellipsis_pos)
14663 glyph++, x += glyph->pixel_width;
14664 x -= glyph->pixel_width;
14665 glyph--;
14668 else if (match_with_avoid_cursor)
14670 cursor = glyph_after;
14671 x = -1;
14673 else if (string_seen)
14675 int incr = row->reversed_p ? -1 : +1;
14677 /* Need to find the glyph that came out of a string which is
14678 present at point. That glyph is somewhere between
14679 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14680 positioned between POS_BEFORE and POS_AFTER in the
14681 buffer. */
14682 struct glyph *start, *stop;
14683 ptrdiff_t pos = pos_before;
14685 x = -1;
14687 /* If the row ends in a newline from a display string,
14688 reordering could have moved the glyphs belonging to the
14689 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14690 in this case we extend the search to the last glyph in
14691 the row that was not inserted by redisplay. */
14692 if (row->ends_in_newline_from_string_p)
14694 glyph_after = end;
14695 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14698 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14699 correspond to POS_BEFORE and POS_AFTER, respectively. We
14700 need START and STOP in the order that corresponds to the
14701 row's direction as given by its reversed_p flag. If the
14702 directionality of characters between POS_BEFORE and
14703 POS_AFTER is the opposite of the row's base direction,
14704 these characters will have been reordered for display,
14705 and we need to reverse START and STOP. */
14706 if (!row->reversed_p)
14708 start = min (glyph_before, glyph_after);
14709 stop = max (glyph_before, glyph_after);
14711 else
14713 start = max (glyph_before, glyph_after);
14714 stop = min (glyph_before, glyph_after);
14716 for (glyph = start + incr;
14717 row->reversed_p ? glyph > stop : glyph < stop; )
14720 /* Any glyphs that come from the buffer are here because
14721 of bidi reordering. Skip them, and only pay
14722 attention to glyphs that came from some string. */
14723 if (STRINGP (glyph->object))
14725 Lisp_Object str;
14726 ptrdiff_t tem;
14727 /* If the display property covers the newline, we
14728 need to search for it one position farther. */
14729 ptrdiff_t lim = pos_after
14730 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14732 string_from_text_prop = 0;
14733 str = glyph->object;
14734 tem = string_buffer_position_lim (str, pos, lim, 0);
14735 if (tem == 0 /* from overlay */
14736 || pos <= tem)
14738 /* If the string from which this glyph came is
14739 found in the buffer at point, or at position
14740 that is closer to point than pos_after, then
14741 we've found the glyph we've been looking for.
14742 If it comes from an overlay (tem == 0), and
14743 it has the `cursor' property on one of its
14744 glyphs, record that glyph as a candidate for
14745 displaying the cursor. (As in the
14746 unidirectional version, we will display the
14747 cursor on the last candidate we find.) */
14748 if (tem == 0
14749 || tem == pt_old
14750 || (tem - pt_old > 0 && tem < pos_after))
14752 /* The glyphs from this string could have
14753 been reordered. Find the one with the
14754 smallest string position. Or there could
14755 be a character in the string with the
14756 `cursor' property, which means display
14757 cursor on that character's glyph. */
14758 ptrdiff_t strpos = glyph->charpos;
14760 if (tem)
14762 cursor = glyph;
14763 string_from_text_prop = 1;
14765 for ( ;
14766 (row->reversed_p ? glyph > stop : glyph < stop)
14767 && EQ (glyph->object, str);
14768 glyph += incr)
14770 Lisp_Object cprop;
14771 ptrdiff_t gpos = glyph->charpos;
14773 cprop = Fget_char_property (make_number (gpos),
14774 Qcursor,
14775 glyph->object);
14776 if (!NILP (cprop))
14778 cursor = glyph;
14779 break;
14781 if (tem && glyph->charpos < strpos)
14783 strpos = glyph->charpos;
14784 cursor = glyph;
14788 if (tem == pt_old
14789 || (tem - pt_old > 0 && tem < pos_after))
14790 goto compute_x;
14792 if (tem)
14793 pos = tem + 1; /* don't find previous instances */
14795 /* This string is not what we want; skip all of the
14796 glyphs that came from it. */
14797 while ((row->reversed_p ? glyph > stop : glyph < stop)
14798 && EQ (glyph->object, str))
14799 glyph += incr;
14801 else
14802 glyph += incr;
14805 /* If we reached the end of the line, and END was from a string,
14806 the cursor is not on this line. */
14807 if (cursor == NULL
14808 && (row->reversed_p ? glyph <= end : glyph >= end)
14809 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14810 && STRINGP (end->object)
14811 && row->continued_p)
14812 return 0;
14814 /* A truncated row may not include PT among its character positions.
14815 Setting the cursor inside the scroll margin will trigger
14816 recalculation of hscroll in hscroll_window_tree. But if a
14817 display string covers point, defer to the string-handling
14818 code below to figure this out. */
14819 else if (row->truncated_on_left_p && pt_old < bpos_min)
14821 cursor = glyph_before;
14822 x = -1;
14824 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14825 /* Zero-width characters produce no glyphs. */
14826 || (!empty_line_p
14827 && (row->reversed_p
14828 ? glyph_after > glyphs_end
14829 : glyph_after < glyphs_end)))
14831 cursor = glyph_after;
14832 x = -1;
14836 compute_x:
14837 if (cursor != NULL)
14838 glyph = cursor;
14839 else if (glyph == glyphs_end
14840 && pos_before == pos_after
14841 && STRINGP ((row->reversed_p
14842 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14843 : row->glyphs[TEXT_AREA])->object))
14845 /* If all the glyphs of this row came from strings, put the
14846 cursor on the first glyph of the row. This avoids having the
14847 cursor outside of the text area in this very rare and hard
14848 use case. */
14849 glyph =
14850 row->reversed_p
14851 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14852 : row->glyphs[TEXT_AREA];
14854 if (x < 0)
14856 struct glyph *g;
14858 /* Need to compute x that corresponds to GLYPH. */
14859 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14861 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14862 emacs_abort ();
14863 x += g->pixel_width;
14867 /* ROW could be part of a continued line, which, under bidi
14868 reordering, might have other rows whose start and end charpos
14869 occlude point. Only set w->cursor if we found a better
14870 approximation to the cursor position than we have from previously
14871 examined candidate rows belonging to the same continued line. */
14872 if (/* We already have a candidate row. */
14873 w->cursor.vpos >= 0
14874 /* That candidate is not the row we are processing. */
14875 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14876 /* Make sure cursor.vpos specifies a row whose start and end
14877 charpos occlude point, and it is valid candidate for being a
14878 cursor-row. This is because some callers of this function
14879 leave cursor.vpos at the row where the cursor was displayed
14880 during the last redisplay cycle. */
14881 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14882 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14883 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14885 struct glyph *g1
14886 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14888 /* Don't consider glyphs that are outside TEXT_AREA. */
14889 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14890 return 0;
14891 /* Keep the candidate whose buffer position is the closest to
14892 point or has the `cursor' property. */
14893 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14894 w->cursor.hpos >= 0
14895 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14896 && ((BUFFERP (g1->object)
14897 && (g1->charpos == pt_old /* An exact match always wins. */
14898 || (BUFFERP (glyph->object)
14899 && eabs (g1->charpos - pt_old)
14900 < eabs (glyph->charpos - pt_old))))
14901 /* Previous candidate is a glyph from a string that has
14902 a non-nil `cursor' property. */
14903 || (STRINGP (g1->object)
14904 && (!NILP (Fget_char_property (make_number (g1->charpos),
14905 Qcursor, g1->object))
14906 /* Previous candidate is from the same display
14907 string as this one, and the display string
14908 came from a text property. */
14909 || (EQ (g1->object, glyph->object)
14910 && string_from_text_prop)
14911 /* this candidate is from newline and its
14912 position is not an exact match */
14913 || (INTEGERP (glyph->object)
14914 && glyph->charpos != pt_old)))))
14915 return 0;
14916 /* If this candidate gives an exact match, use that. */
14917 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14918 /* If this candidate is a glyph created for the
14919 terminating newline of a line, and point is on that
14920 newline, it wins because it's an exact match. */
14921 || (!row->continued_p
14922 && INTEGERP (glyph->object)
14923 && glyph->charpos == 0
14924 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14925 /* Otherwise, keep the candidate that comes from a row
14926 spanning less buffer positions. This may win when one or
14927 both candidate positions are on glyphs that came from
14928 display strings, for which we cannot compare buffer
14929 positions. */
14930 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14931 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14932 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14933 return 0;
14935 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14936 w->cursor.x = x;
14937 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14938 w->cursor.y = row->y + dy;
14940 if (w == XWINDOW (selected_window))
14942 if (!row->continued_p
14943 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14944 && row->x == 0)
14946 this_line_buffer = XBUFFER (w->contents);
14948 CHARPOS (this_line_start_pos)
14949 = MATRIX_ROW_START_CHARPOS (row) + delta;
14950 BYTEPOS (this_line_start_pos)
14951 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14953 CHARPOS (this_line_end_pos)
14954 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14955 BYTEPOS (this_line_end_pos)
14956 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14958 this_line_y = w->cursor.y;
14959 this_line_pixel_height = row->height;
14960 this_line_vpos = w->cursor.vpos;
14961 this_line_start_x = row->x;
14963 else
14964 CHARPOS (this_line_start_pos) = 0;
14967 return 1;
14971 /* Run window scroll functions, if any, for WINDOW with new window
14972 start STARTP. Sets the window start of WINDOW to that position.
14974 We assume that the window's buffer is really current. */
14976 static struct text_pos
14977 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14979 struct window *w = XWINDOW (window);
14980 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14982 eassert (current_buffer == XBUFFER (w->contents));
14984 if (!NILP (Vwindow_scroll_functions))
14986 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14987 make_number (CHARPOS (startp)));
14988 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14989 /* In case the hook functions switch buffers. */
14990 set_buffer_internal (XBUFFER (w->contents));
14993 return startp;
14997 /* Make sure the line containing the cursor is fully visible.
14998 A value of 1 means there is nothing to be done.
14999 (Either the line is fully visible, or it cannot be made so,
15000 or we cannot tell.)
15002 If FORCE_P is non-zero, return 0 even if partial visible cursor row
15003 is higher than window.
15005 If CURRENT_MATRIX_P is non-zero, use the information from the
15006 window's current glyph matrix; otherwise use the desired glyph
15007 matrix.
15009 A value of 0 means the caller should do scrolling
15010 as if point had gone off the screen. */
15012 static int
15013 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
15015 struct glyph_matrix *matrix;
15016 struct glyph_row *row;
15017 int window_height;
15019 if (!make_cursor_line_fully_visible_p)
15020 return 1;
15022 /* It's not always possible to find the cursor, e.g, when a window
15023 is full of overlay strings. Don't do anything in that case. */
15024 if (w->cursor.vpos < 0)
15025 return 1;
15027 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15028 row = MATRIX_ROW (matrix, w->cursor.vpos);
15030 /* If the cursor row is not partially visible, there's nothing to do. */
15031 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15032 return 1;
15034 /* If the row the cursor is in is taller than the window's height,
15035 it's not clear what to do, so do nothing. */
15036 window_height = window_box_height (w);
15037 if (row->height >= window_height)
15039 if (!force_p || MINI_WINDOW_P (w)
15040 || w->vscroll || w->cursor.vpos == 0)
15041 return 1;
15043 return 0;
15047 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15048 non-zero means only WINDOW is redisplayed in redisplay_internal.
15049 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15050 in redisplay_window to bring a partially visible line into view in
15051 the case that only the cursor has moved.
15053 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
15054 last screen line's vertical height extends past the end of the screen.
15056 Value is
15058 1 if scrolling succeeded
15060 0 if scrolling didn't find point.
15062 -1 if new fonts have been loaded so that we must interrupt
15063 redisplay, adjust glyph matrices, and try again. */
15065 enum
15067 SCROLLING_SUCCESS,
15068 SCROLLING_FAILED,
15069 SCROLLING_NEED_LARGER_MATRICES
15072 /* If scroll-conservatively is more than this, never recenter.
15074 If you change this, don't forget to update the doc string of
15075 `scroll-conservatively' and the Emacs manual. */
15076 #define SCROLL_LIMIT 100
15078 static int
15079 try_scrolling (Lisp_Object window, int just_this_one_p,
15080 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15081 int temp_scroll_step, int last_line_misfit)
15083 struct window *w = XWINDOW (window);
15084 struct frame *f = XFRAME (w->frame);
15085 struct text_pos pos, startp;
15086 struct it it;
15087 int this_scroll_margin, scroll_max, rc, height;
15088 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
15089 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
15090 Lisp_Object aggressive;
15091 /* We will never try scrolling more than this number of lines. */
15092 int scroll_limit = SCROLL_LIMIT;
15093 int frame_line_height = default_line_pixel_height (w);
15094 int window_total_lines
15095 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15097 #ifdef GLYPH_DEBUG
15098 debug_method_add (w, "try_scrolling");
15099 #endif
15101 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15103 /* Compute scroll margin height in pixels. We scroll when point is
15104 within this distance from the top or bottom of the window. */
15105 if (scroll_margin > 0)
15106 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15107 * frame_line_height;
15108 else
15109 this_scroll_margin = 0;
15111 /* Force arg_scroll_conservatively to have a reasonable value, to
15112 avoid scrolling too far away with slow move_it_* functions. Note
15113 that the user can supply scroll-conservatively equal to
15114 `most-positive-fixnum', which can be larger than INT_MAX. */
15115 if (arg_scroll_conservatively > scroll_limit)
15117 arg_scroll_conservatively = scroll_limit + 1;
15118 scroll_max = scroll_limit * frame_line_height;
15120 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15121 /* Compute how much we should try to scroll maximally to bring
15122 point into view. */
15123 scroll_max = (max (scroll_step,
15124 max (arg_scroll_conservatively, temp_scroll_step))
15125 * frame_line_height);
15126 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15127 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15128 /* We're trying to scroll because of aggressive scrolling but no
15129 scroll_step is set. Choose an arbitrary one. */
15130 scroll_max = 10 * frame_line_height;
15131 else
15132 scroll_max = 0;
15134 too_near_end:
15136 /* Decide whether to scroll down. */
15137 if (PT > CHARPOS (startp))
15139 int scroll_margin_y;
15141 /* Compute the pixel ypos of the scroll margin, then move IT to
15142 either that ypos or PT, whichever comes first. */
15143 start_display (&it, w, startp);
15144 scroll_margin_y = it.last_visible_y - this_scroll_margin
15145 - frame_line_height * extra_scroll_margin_lines;
15146 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15147 (MOVE_TO_POS | MOVE_TO_Y));
15149 if (PT > CHARPOS (it.current.pos))
15151 int y0 = line_bottom_y (&it);
15152 /* Compute how many pixels below window bottom to stop searching
15153 for PT. This avoids costly search for PT that is far away if
15154 the user limited scrolling by a small number of lines, but
15155 always finds PT if scroll_conservatively is set to a large
15156 number, such as most-positive-fixnum. */
15157 int slack = max (scroll_max, 10 * frame_line_height);
15158 int y_to_move = it.last_visible_y + slack;
15160 /* Compute the distance from the scroll margin to PT or to
15161 the scroll limit, whichever comes first. This should
15162 include the height of the cursor line, to make that line
15163 fully visible. */
15164 move_it_to (&it, PT, -1, y_to_move,
15165 -1, MOVE_TO_POS | MOVE_TO_Y);
15166 dy = line_bottom_y (&it) - y0;
15168 if (dy > scroll_max)
15169 return SCROLLING_FAILED;
15171 if (dy > 0)
15172 scroll_down_p = 1;
15176 if (scroll_down_p)
15178 /* Point is in or below the bottom scroll margin, so move the
15179 window start down. If scrolling conservatively, move it just
15180 enough down to make point visible. If scroll_step is set,
15181 move it down by scroll_step. */
15182 if (arg_scroll_conservatively)
15183 amount_to_scroll
15184 = min (max (dy, frame_line_height),
15185 frame_line_height * arg_scroll_conservatively);
15186 else if (scroll_step || temp_scroll_step)
15187 amount_to_scroll = scroll_max;
15188 else
15190 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15191 height = WINDOW_BOX_TEXT_HEIGHT (w);
15192 if (NUMBERP (aggressive))
15194 double float_amount = XFLOATINT (aggressive) * height;
15195 int aggressive_scroll = float_amount;
15196 if (aggressive_scroll == 0 && float_amount > 0)
15197 aggressive_scroll = 1;
15198 /* Don't let point enter the scroll margin near top of
15199 the window. This could happen if the value of
15200 scroll_up_aggressively is too large and there are
15201 non-zero margins, because scroll_up_aggressively
15202 means put point that fraction of window height
15203 _from_the_bottom_margin_. */
15204 if (aggressive_scroll + 2*this_scroll_margin > height)
15205 aggressive_scroll = height - 2*this_scroll_margin;
15206 amount_to_scroll = dy + aggressive_scroll;
15210 if (amount_to_scroll <= 0)
15211 return SCROLLING_FAILED;
15213 start_display (&it, w, startp);
15214 if (arg_scroll_conservatively <= scroll_limit)
15215 move_it_vertically (&it, amount_to_scroll);
15216 else
15218 /* Extra precision for users who set scroll-conservatively
15219 to a large number: make sure the amount we scroll
15220 the window start is never less than amount_to_scroll,
15221 which was computed as distance from window bottom to
15222 point. This matters when lines at window top and lines
15223 below window bottom have different height. */
15224 struct it it1;
15225 void *it1data = NULL;
15226 /* We use a temporary it1 because line_bottom_y can modify
15227 its argument, if it moves one line down; see there. */
15228 int start_y;
15230 SAVE_IT (it1, it, it1data);
15231 start_y = line_bottom_y (&it1);
15232 do {
15233 RESTORE_IT (&it, &it, it1data);
15234 move_it_by_lines (&it, 1);
15235 SAVE_IT (it1, it, it1data);
15236 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15239 /* If STARTP is unchanged, move it down another screen line. */
15240 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15241 move_it_by_lines (&it, 1);
15242 startp = it.current.pos;
15244 else
15246 struct text_pos scroll_margin_pos = startp;
15247 int y_offset = 0;
15249 /* See if point is inside the scroll margin at the top of the
15250 window. */
15251 if (this_scroll_margin)
15253 int y_start;
15255 start_display (&it, w, startp);
15256 y_start = it.current_y;
15257 move_it_vertically (&it, this_scroll_margin);
15258 scroll_margin_pos = it.current.pos;
15259 /* If we didn't move enough before hitting ZV, request
15260 additional amount of scroll, to move point out of the
15261 scroll margin. */
15262 if (IT_CHARPOS (it) == ZV
15263 && it.current_y - y_start < this_scroll_margin)
15264 y_offset = this_scroll_margin - (it.current_y - y_start);
15267 if (PT < CHARPOS (scroll_margin_pos))
15269 /* Point is in the scroll margin at the top of the window or
15270 above what is displayed in the window. */
15271 int y0, y_to_move;
15273 /* Compute the vertical distance from PT to the scroll
15274 margin position. Move as far as scroll_max allows, or
15275 one screenful, or 10 screen lines, whichever is largest.
15276 Give up if distance is greater than scroll_max or if we
15277 didn't reach the scroll margin position. */
15278 SET_TEXT_POS (pos, PT, PT_BYTE);
15279 start_display (&it, w, pos);
15280 y0 = it.current_y;
15281 y_to_move = max (it.last_visible_y,
15282 max (scroll_max, 10 * frame_line_height));
15283 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15284 y_to_move, -1,
15285 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15286 dy = it.current_y - y0;
15287 if (dy > scroll_max
15288 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15289 return SCROLLING_FAILED;
15291 /* Additional scroll for when ZV was too close to point. */
15292 dy += y_offset;
15294 /* Compute new window start. */
15295 start_display (&it, w, startp);
15297 if (arg_scroll_conservatively)
15298 amount_to_scroll = max (dy, frame_line_height *
15299 max (scroll_step, temp_scroll_step));
15300 else if (scroll_step || temp_scroll_step)
15301 amount_to_scroll = scroll_max;
15302 else
15304 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15305 height = WINDOW_BOX_TEXT_HEIGHT (w);
15306 if (NUMBERP (aggressive))
15308 double float_amount = XFLOATINT (aggressive) * height;
15309 int aggressive_scroll = float_amount;
15310 if (aggressive_scroll == 0 && float_amount > 0)
15311 aggressive_scroll = 1;
15312 /* Don't let point enter the scroll margin near
15313 bottom of the window, if the value of
15314 scroll_down_aggressively happens to be too
15315 large. */
15316 if (aggressive_scroll + 2*this_scroll_margin > height)
15317 aggressive_scroll = height - 2*this_scroll_margin;
15318 amount_to_scroll = dy + aggressive_scroll;
15322 if (amount_to_scroll <= 0)
15323 return SCROLLING_FAILED;
15325 move_it_vertically_backward (&it, amount_to_scroll);
15326 startp = it.current.pos;
15330 /* Run window scroll functions. */
15331 startp = run_window_scroll_functions (window, startp);
15333 /* Display the window. Give up if new fonts are loaded, or if point
15334 doesn't appear. */
15335 if (!try_window (window, startp, 0))
15336 rc = SCROLLING_NEED_LARGER_MATRICES;
15337 else if (w->cursor.vpos < 0)
15339 clear_glyph_matrix (w->desired_matrix);
15340 rc = SCROLLING_FAILED;
15342 else
15344 /* Maybe forget recorded base line for line number display. */
15345 if (!just_this_one_p
15346 || current_buffer->clip_changed
15347 || BEG_UNCHANGED < CHARPOS (startp))
15348 w->base_line_number = 0;
15350 /* If cursor ends up on a partially visible line,
15351 treat that as being off the bottom of the screen. */
15352 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15353 /* It's possible that the cursor is on the first line of the
15354 buffer, which is partially obscured due to a vscroll
15355 (Bug#7537). In that case, avoid looping forever. */
15356 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15358 clear_glyph_matrix (w->desired_matrix);
15359 ++extra_scroll_margin_lines;
15360 goto too_near_end;
15362 rc = SCROLLING_SUCCESS;
15365 return rc;
15369 /* Compute a suitable window start for window W if display of W starts
15370 on a continuation line. Value is non-zero if a new window start
15371 was computed.
15373 The new window start will be computed, based on W's width, starting
15374 from the start of the continued line. It is the start of the
15375 screen line with the minimum distance from the old start W->start. */
15377 static int
15378 compute_window_start_on_continuation_line (struct window *w)
15380 struct text_pos pos, start_pos;
15381 int window_start_changed_p = 0;
15383 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15385 /* If window start is on a continuation line... Window start may be
15386 < BEGV in case there's invisible text at the start of the
15387 buffer (M-x rmail, for example). */
15388 if (CHARPOS (start_pos) > BEGV
15389 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15391 struct it it;
15392 struct glyph_row *row;
15394 /* Handle the case that the window start is out of range. */
15395 if (CHARPOS (start_pos) < BEGV)
15396 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15397 else if (CHARPOS (start_pos) > ZV)
15398 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15400 /* Find the start of the continued line. This should be fast
15401 because find_newline is fast (newline cache). */
15402 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15403 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15404 row, DEFAULT_FACE_ID);
15405 reseat_at_previous_visible_line_start (&it);
15407 /* If the line start is "too far" away from the window start,
15408 say it takes too much time to compute a new window start. */
15409 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15410 /* PXW: Do we need upper bounds here? */
15411 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15413 int min_distance, distance;
15415 /* Move forward by display lines to find the new window
15416 start. If window width was enlarged, the new start can
15417 be expected to be > the old start. If window width was
15418 decreased, the new window start will be < the old start.
15419 So, we're looking for the display line start with the
15420 minimum distance from the old window start. */
15421 pos = it.current.pos;
15422 min_distance = INFINITY;
15423 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15424 distance < min_distance)
15426 min_distance = distance;
15427 pos = it.current.pos;
15428 if (it.line_wrap == WORD_WRAP)
15430 /* Under WORD_WRAP, move_it_by_lines is likely to
15431 overshoot and stop not at the first, but the
15432 second character from the left margin. So in
15433 that case, we need a more tight control on the X
15434 coordinate of the iterator than move_it_by_lines
15435 promises in its contract. The method is to first
15436 go to the last (rightmost) visible character of a
15437 line, then move to the leftmost character on the
15438 next line in a separate call. */
15439 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15440 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15441 move_it_to (&it, ZV, 0,
15442 it.current_y + it.max_ascent + it.max_descent, -1,
15443 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15445 else
15446 move_it_by_lines (&it, 1);
15449 /* Set the window start there. */
15450 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15451 window_start_changed_p = 1;
15455 return window_start_changed_p;
15459 /* Try cursor movement in case text has not changed in window WINDOW,
15460 with window start STARTP. Value is
15462 CURSOR_MOVEMENT_SUCCESS if successful
15464 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15466 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15467 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15468 we want to scroll as if scroll-step were set to 1. See the code.
15470 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15471 which case we have to abort this redisplay, and adjust matrices
15472 first. */
15474 enum
15476 CURSOR_MOVEMENT_SUCCESS,
15477 CURSOR_MOVEMENT_CANNOT_BE_USED,
15478 CURSOR_MOVEMENT_MUST_SCROLL,
15479 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15482 static int
15483 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15485 struct window *w = XWINDOW (window);
15486 struct frame *f = XFRAME (w->frame);
15487 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15489 #ifdef GLYPH_DEBUG
15490 if (inhibit_try_cursor_movement)
15491 return rc;
15492 #endif
15494 /* Previously, there was a check for Lisp integer in the
15495 if-statement below. Now, this field is converted to
15496 ptrdiff_t, thus zero means invalid position in a buffer. */
15497 eassert (w->last_point > 0);
15498 /* Likewise there was a check whether window_end_vpos is nil or larger
15499 than the window. Now window_end_vpos is int and so never nil, but
15500 let's leave eassert to check whether it fits in the window. */
15501 eassert (w->window_end_vpos < w->current_matrix->nrows);
15503 /* Handle case where text has not changed, only point, and it has
15504 not moved off the frame. */
15505 if (/* Point may be in this window. */
15506 PT >= CHARPOS (startp)
15507 /* Selective display hasn't changed. */
15508 && !current_buffer->clip_changed
15509 /* Function force-mode-line-update is used to force a thorough
15510 redisplay. It sets either windows_or_buffers_changed or
15511 update_mode_lines. So don't take a shortcut here for these
15512 cases. */
15513 && !update_mode_lines
15514 && !windows_or_buffers_changed
15515 && !f->cursor_type_changed
15516 && NILP (Vshow_trailing_whitespace)
15517 /* This code is not used for mini-buffer for the sake of the case
15518 of redisplaying to replace an echo area message; since in
15519 that case the mini-buffer contents per se are usually
15520 unchanged. This code is of no real use in the mini-buffer
15521 since the handling of this_line_start_pos, etc., in redisplay
15522 handles the same cases. */
15523 && !EQ (window, minibuf_window)
15524 && (FRAME_WINDOW_P (f)
15525 || !overlay_arrow_in_current_buffer_p ()))
15527 int this_scroll_margin, top_scroll_margin;
15528 struct glyph_row *row = NULL;
15529 int frame_line_height = default_line_pixel_height (w);
15530 int window_total_lines
15531 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15533 #ifdef GLYPH_DEBUG
15534 debug_method_add (w, "cursor movement");
15535 #endif
15537 /* Scroll if point within this distance from the top or bottom
15538 of the window. This is a pixel value. */
15539 if (scroll_margin > 0)
15541 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15542 this_scroll_margin *= frame_line_height;
15544 else
15545 this_scroll_margin = 0;
15547 top_scroll_margin = this_scroll_margin;
15548 if (WINDOW_WANTS_HEADER_LINE_P (w))
15549 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15551 /* Start with the row the cursor was displayed during the last
15552 not paused redisplay. Give up if that row is not valid. */
15553 if (w->last_cursor_vpos < 0
15554 || w->last_cursor_vpos >= w->current_matrix->nrows)
15555 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15556 else
15558 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15559 if (row->mode_line_p)
15560 ++row;
15561 if (!row->enabled_p)
15562 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15565 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15567 int scroll_p = 0, must_scroll = 0;
15568 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15570 if (PT > w->last_point)
15572 /* Point has moved forward. */
15573 while (MATRIX_ROW_END_CHARPOS (row) < PT
15574 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15576 eassert (row->enabled_p);
15577 ++row;
15580 /* If the end position of a row equals the start
15581 position of the next row, and PT is at that position,
15582 we would rather display cursor in the next line. */
15583 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15584 && MATRIX_ROW_END_CHARPOS (row) == PT
15585 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15586 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15587 && !cursor_row_p (row))
15588 ++row;
15590 /* If within the scroll margin, scroll. Note that
15591 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15592 the next line would be drawn, and that
15593 this_scroll_margin can be zero. */
15594 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15595 || PT > MATRIX_ROW_END_CHARPOS (row)
15596 /* Line is completely visible last line in window
15597 and PT is to be set in the next line. */
15598 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15599 && PT == MATRIX_ROW_END_CHARPOS (row)
15600 && !row->ends_at_zv_p
15601 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15602 scroll_p = 1;
15604 else if (PT < w->last_point)
15606 /* Cursor has to be moved backward. Note that PT >=
15607 CHARPOS (startp) because of the outer if-statement. */
15608 while (!row->mode_line_p
15609 && (MATRIX_ROW_START_CHARPOS (row) > PT
15610 || (MATRIX_ROW_START_CHARPOS (row) == PT
15611 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15612 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15613 row > w->current_matrix->rows
15614 && (row-1)->ends_in_newline_from_string_p))))
15615 && (row->y > top_scroll_margin
15616 || CHARPOS (startp) == BEGV))
15618 eassert (row->enabled_p);
15619 --row;
15622 /* Consider the following case: Window starts at BEGV,
15623 there is invisible, intangible text at BEGV, so that
15624 display starts at some point START > BEGV. It can
15625 happen that we are called with PT somewhere between
15626 BEGV and START. Try to handle that case. */
15627 if (row < w->current_matrix->rows
15628 || row->mode_line_p)
15630 row = w->current_matrix->rows;
15631 if (row->mode_line_p)
15632 ++row;
15635 /* Due to newlines in overlay strings, we may have to
15636 skip forward over overlay strings. */
15637 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15638 && MATRIX_ROW_END_CHARPOS (row) == PT
15639 && !cursor_row_p (row))
15640 ++row;
15642 /* If within the scroll margin, scroll. */
15643 if (row->y < top_scroll_margin
15644 && CHARPOS (startp) != BEGV)
15645 scroll_p = 1;
15647 else
15649 /* Cursor did not move. So don't scroll even if cursor line
15650 is partially visible, as it was so before. */
15651 rc = CURSOR_MOVEMENT_SUCCESS;
15654 if (PT < MATRIX_ROW_START_CHARPOS (row)
15655 || PT > MATRIX_ROW_END_CHARPOS (row))
15657 /* if PT is not in the glyph row, give up. */
15658 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15659 must_scroll = 1;
15661 else if (rc != CURSOR_MOVEMENT_SUCCESS
15662 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15664 struct glyph_row *row1;
15666 /* If rows are bidi-reordered and point moved, back up
15667 until we find a row that does not belong to a
15668 continuation line. This is because we must consider
15669 all rows of a continued line as candidates for the
15670 new cursor positioning, since row start and end
15671 positions change non-linearly with vertical position
15672 in such rows. */
15673 /* FIXME: Revisit this when glyph ``spilling'' in
15674 continuation lines' rows is implemented for
15675 bidi-reordered rows. */
15676 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15677 MATRIX_ROW_CONTINUATION_LINE_P (row);
15678 --row)
15680 /* If we hit the beginning of the displayed portion
15681 without finding the first row of a continued
15682 line, give up. */
15683 if (row <= row1)
15685 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15686 break;
15688 eassert (row->enabled_p);
15691 if (must_scroll)
15693 else if (rc != CURSOR_MOVEMENT_SUCCESS
15694 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15695 /* Make sure this isn't a header line by any chance, since
15696 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15697 && !row->mode_line_p
15698 && make_cursor_line_fully_visible_p)
15700 if (PT == MATRIX_ROW_END_CHARPOS (row)
15701 && !row->ends_at_zv_p
15702 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15703 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15704 else if (row->height > window_box_height (w))
15706 /* If we end up in a partially visible line, let's
15707 make it fully visible, except when it's taller
15708 than the window, in which case we can't do much
15709 about it. */
15710 *scroll_step = 1;
15711 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15713 else
15715 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15716 if (!cursor_row_fully_visible_p (w, 0, 1))
15717 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15718 else
15719 rc = CURSOR_MOVEMENT_SUCCESS;
15722 else if (scroll_p)
15723 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15724 else if (rc != CURSOR_MOVEMENT_SUCCESS
15725 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15727 /* With bidi-reordered rows, there could be more than
15728 one candidate row whose start and end positions
15729 occlude point. We need to let set_cursor_from_row
15730 find the best candidate. */
15731 /* FIXME: Revisit this when glyph ``spilling'' in
15732 continuation lines' rows is implemented for
15733 bidi-reordered rows. */
15734 int rv = 0;
15738 int at_zv_p = 0, exact_match_p = 0;
15740 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15741 && PT <= MATRIX_ROW_END_CHARPOS (row)
15742 && cursor_row_p (row))
15743 rv |= set_cursor_from_row (w, row, w->current_matrix,
15744 0, 0, 0, 0);
15745 /* As soon as we've found the exact match for point,
15746 or the first suitable row whose ends_at_zv_p flag
15747 is set, we are done. */
15748 if (rv)
15750 at_zv_p = MATRIX_ROW (w->current_matrix,
15751 w->cursor.vpos)->ends_at_zv_p;
15752 if (!at_zv_p
15753 && w->cursor.hpos >= 0
15754 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15755 w->cursor.vpos))
15757 struct glyph_row *candidate =
15758 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15759 struct glyph *g =
15760 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15761 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15763 exact_match_p =
15764 (BUFFERP (g->object) && g->charpos == PT)
15765 || (INTEGERP (g->object)
15766 && (g->charpos == PT
15767 || (g->charpos == 0 && endpos - 1 == PT)));
15769 if (at_zv_p || exact_match_p)
15771 rc = CURSOR_MOVEMENT_SUCCESS;
15772 break;
15775 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15776 break;
15777 ++row;
15779 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15780 || row->continued_p)
15781 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15782 || (MATRIX_ROW_START_CHARPOS (row) == PT
15783 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15784 /* If we didn't find any candidate rows, or exited the
15785 loop before all the candidates were examined, signal
15786 to the caller that this method failed. */
15787 if (rc != CURSOR_MOVEMENT_SUCCESS
15788 && !(rv
15789 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15790 && !row->continued_p))
15791 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15792 else if (rv)
15793 rc = CURSOR_MOVEMENT_SUCCESS;
15795 else
15799 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15801 rc = CURSOR_MOVEMENT_SUCCESS;
15802 break;
15804 ++row;
15806 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15807 && MATRIX_ROW_START_CHARPOS (row) == PT
15808 && cursor_row_p (row));
15813 return rc;
15817 void
15818 set_vertical_scroll_bar (struct window *w)
15820 ptrdiff_t start, end, whole;
15822 /* Calculate the start and end positions for the current window.
15823 At some point, it would be nice to choose between scrollbars
15824 which reflect the whole buffer size, with special markers
15825 indicating narrowing, and scrollbars which reflect only the
15826 visible region.
15828 Note that mini-buffers sometimes aren't displaying any text. */
15829 if (!MINI_WINDOW_P (w)
15830 || (w == XWINDOW (minibuf_window)
15831 && NILP (echo_area_buffer[0])))
15833 struct buffer *buf = XBUFFER (w->contents);
15834 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15835 start = marker_position (w->start) - BUF_BEGV (buf);
15836 /* I don't think this is guaranteed to be right. For the
15837 moment, we'll pretend it is. */
15838 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15840 if (end < start)
15841 end = start;
15842 if (whole < (end - start))
15843 whole = end - start;
15845 else
15846 start = end = whole = 0;
15848 /* Indicate what this scroll bar ought to be displaying now. */
15849 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15850 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15851 (w, end - start, whole, start);
15855 void
15856 set_horizontal_scroll_bar (struct window *w)
15858 int start, end, whole, portion;
15860 if (!MINI_WINDOW_P (w)
15861 || (w == XWINDOW (minibuf_window)
15862 && NILP (echo_area_buffer[0])))
15864 struct buffer *b = XBUFFER (w->contents);
15865 struct buffer *old_buffer = NULL;
15866 struct it it;
15867 struct text_pos startp;
15869 if (b != current_buffer)
15871 old_buffer = current_buffer;
15872 set_buffer_internal (b);
15875 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15876 start_display (&it, w, startp);
15877 it.last_visible_x = INT_MAX;
15878 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15879 MOVE_TO_X | MOVE_TO_Y);
15880 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15881 window_box_height (w), -1,
15882 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15884 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15885 end = start + window_box_width (w, TEXT_AREA);
15886 portion = end - start;
15887 /* After enlarging a horizontally scrolled window such that it
15888 gets at least as wide as the text it contains, make sure that
15889 the thumb doesn't fill the entire scroll bar so we can still
15890 drag it back to see the entire text. */
15891 whole = max (whole, end);
15893 if (it.bidi_p)
15895 Lisp_Object pdir;
15897 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15898 if (EQ (pdir, Qright_to_left))
15900 start = whole - end;
15901 end = start + portion;
15905 if (old_buffer)
15906 set_buffer_internal (old_buffer);
15908 else
15909 start = end = whole = portion = 0;
15911 w->hscroll_whole = whole;
15913 /* Indicate what this scroll bar ought to be displaying now. */
15914 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15915 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15916 (w, portion, whole, start);
15920 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15921 selected_window is redisplayed.
15923 We can return without actually redisplaying the window if fonts has been
15924 changed on window's frame. In that case, redisplay_internal will retry.
15926 As one of the important parts of redisplaying a window, we need to
15927 decide whether the previous window-start position (stored in the
15928 window's w->start marker position) is still valid, and if it isn't,
15929 recompute it. Some details about that:
15931 . The previous window-start could be in a continuation line, in
15932 which case we need to recompute it when the window width
15933 changes. See compute_window_start_on_continuation_line and its
15934 call below.
15936 . The text that changed since last redisplay could include the
15937 previous window-start position. In that case, we try to salvage
15938 what we can from the current glyph matrix by calling
15939 try_scrolling, which see.
15941 . Some Emacs command could force us to use a specific window-start
15942 position by setting the window's force_start flag, or gently
15943 propose doing that by setting the window's optional_new_start
15944 flag. In these cases, we try using the specified start point if
15945 that succeeds (i.e. the window desired matrix is successfully
15946 recomputed, and point location is within the window). In case
15947 of optional_new_start, we first check if the specified start
15948 position is feasible, i.e. if it will allow point to be
15949 displayed in the window. If using the specified start point
15950 fails, e.g., if new fonts are needed to be loaded, we abort the
15951 redisplay cycle and leave it up to the next cycle to figure out
15952 things.
15954 . Note that the window's force_start flag is sometimes set by
15955 redisplay itself, when it decides that the previous window start
15956 point is fine and should be kept. Search for "goto force_start"
15957 below to see the details. Like the values of window-start
15958 specified outside of redisplay, these internally-deduced values
15959 are tested for feasibility, and ignored if found to be
15960 unfeasible.
15962 . Note that the function try_window, used to completely redisplay
15963 a window, accepts the window's start point as its argument.
15964 This is used several times in the redisplay code to control
15965 where the window start will be, according to user options such
15966 as scroll-conservatively, and also to ensure the screen line
15967 showing point will be fully (as opposed to partially) visible on
15968 display. */
15970 static void
15971 redisplay_window (Lisp_Object window, bool just_this_one_p)
15973 struct window *w = XWINDOW (window);
15974 struct frame *f = XFRAME (w->frame);
15975 struct buffer *buffer = XBUFFER (w->contents);
15976 struct buffer *old = current_buffer;
15977 struct text_pos lpoint, opoint, startp;
15978 int update_mode_line;
15979 int tem;
15980 struct it it;
15981 /* Record it now because it's overwritten. */
15982 bool current_matrix_up_to_date_p = false;
15983 bool used_current_matrix_p = false;
15984 /* This is less strict than current_matrix_up_to_date_p.
15985 It indicates that the buffer contents and narrowing are unchanged. */
15986 bool buffer_unchanged_p = false;
15987 int temp_scroll_step = 0;
15988 ptrdiff_t count = SPECPDL_INDEX ();
15989 int rc;
15990 int centering_position = -1;
15991 int last_line_misfit = 0;
15992 ptrdiff_t beg_unchanged, end_unchanged;
15993 int frame_line_height;
15995 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15996 opoint = lpoint;
15998 #ifdef GLYPH_DEBUG
15999 *w->desired_matrix->method = 0;
16000 #endif
16002 if (!just_this_one_p
16003 && REDISPLAY_SOME_P ()
16004 && !w->redisplay
16005 && !f->redisplay
16006 && !buffer->text->redisplay
16007 && BUF_PT (buffer) == w->last_point)
16008 return;
16010 /* Make sure that both W's markers are valid. */
16011 eassert (XMARKER (w->start)->buffer == buffer);
16012 eassert (XMARKER (w->pointm)->buffer == buffer);
16014 /* We come here again if we need to run window-text-change-functions
16015 below. */
16016 restart:
16017 reconsider_clip_changes (w);
16018 frame_line_height = default_line_pixel_height (w);
16020 /* Has the mode line to be updated? */
16021 update_mode_line = (w->update_mode_line
16022 || update_mode_lines
16023 || buffer->clip_changed
16024 || buffer->prevent_redisplay_optimizations_p);
16026 if (!just_this_one_p)
16027 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16028 cleverly elsewhere. */
16029 w->must_be_updated_p = true;
16031 if (MINI_WINDOW_P (w))
16033 if (w == XWINDOW (echo_area_window)
16034 && !NILP (echo_area_buffer[0]))
16036 if (update_mode_line)
16037 /* We may have to update a tty frame's menu bar or a
16038 tool-bar. Example `M-x C-h C-h C-g'. */
16039 goto finish_menu_bars;
16040 else
16041 /* We've already displayed the echo area glyphs in this window. */
16042 goto finish_scroll_bars;
16044 else if ((w != XWINDOW (minibuf_window)
16045 || minibuf_level == 0)
16046 /* When buffer is nonempty, redisplay window normally. */
16047 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16048 /* Quail displays non-mini buffers in minibuffer window.
16049 In that case, redisplay the window normally. */
16050 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16052 /* W is a mini-buffer window, but it's not active, so clear
16053 it. */
16054 int yb = window_text_bottom_y (w);
16055 struct glyph_row *row;
16056 int y;
16058 for (y = 0, row = w->desired_matrix->rows;
16059 y < yb;
16060 y += row->height, ++row)
16061 blank_row (w, row, y);
16062 goto finish_scroll_bars;
16065 clear_glyph_matrix (w->desired_matrix);
16068 /* Otherwise set up data on this window; select its buffer and point
16069 value. */
16070 /* Really select the buffer, for the sake of buffer-local
16071 variables. */
16072 set_buffer_internal_1 (XBUFFER (w->contents));
16074 current_matrix_up_to_date_p
16075 = (w->window_end_valid
16076 && !current_buffer->clip_changed
16077 && !current_buffer->prevent_redisplay_optimizations_p
16078 && !window_outdated (w));
16080 /* Run the window-text-change-functions
16081 if it is possible that the text on the screen has changed
16082 (either due to modification of the text, or any other reason). */
16083 if (!current_matrix_up_to_date_p
16084 && !NILP (Vwindow_text_change_functions))
16086 safe_run_hooks (Qwindow_text_change_functions);
16087 goto restart;
16090 beg_unchanged = BEG_UNCHANGED;
16091 end_unchanged = END_UNCHANGED;
16093 SET_TEXT_POS (opoint, PT, PT_BYTE);
16095 specbind (Qinhibit_point_motion_hooks, Qt);
16097 buffer_unchanged_p
16098 = (w->window_end_valid
16099 && !current_buffer->clip_changed
16100 && !window_outdated (w));
16102 /* When windows_or_buffers_changed is non-zero, we can't rely
16103 on the window end being valid, so set it to zero there. */
16104 if (windows_or_buffers_changed)
16106 /* If window starts on a continuation line, maybe adjust the
16107 window start in case the window's width changed. */
16108 if (XMARKER (w->start)->buffer == current_buffer)
16109 compute_window_start_on_continuation_line (w);
16111 w->window_end_valid = false;
16112 /* If so, we also can't rely on current matrix
16113 and should not fool try_cursor_movement below. */
16114 current_matrix_up_to_date_p = false;
16117 /* Some sanity checks. */
16118 CHECK_WINDOW_END (w);
16119 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16120 emacs_abort ();
16121 if (BYTEPOS (opoint) < CHARPOS (opoint))
16122 emacs_abort ();
16124 if (mode_line_update_needed (w))
16125 update_mode_line = 1;
16127 /* Point refers normally to the selected window. For any other
16128 window, set up appropriate value. */
16129 if (!EQ (window, selected_window))
16131 ptrdiff_t new_pt = marker_position (w->pointm);
16132 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16134 if (new_pt < BEGV)
16136 new_pt = BEGV;
16137 new_pt_byte = BEGV_BYTE;
16138 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16140 else if (new_pt > (ZV - 1))
16142 new_pt = ZV;
16143 new_pt_byte = ZV_BYTE;
16144 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16147 /* We don't use SET_PT so that the point-motion hooks don't run. */
16148 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16151 /* If any of the character widths specified in the display table
16152 have changed, invalidate the width run cache. It's true that
16153 this may be a bit late to catch such changes, but the rest of
16154 redisplay goes (non-fatally) haywire when the display table is
16155 changed, so why should we worry about doing any better? */
16156 if (current_buffer->width_run_cache
16157 || (current_buffer->base_buffer
16158 && current_buffer->base_buffer->width_run_cache))
16160 struct Lisp_Char_Table *disptab = buffer_display_table ();
16162 if (! disptab_matches_widthtab
16163 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16165 struct buffer *buf = current_buffer;
16167 if (buf->base_buffer)
16168 buf = buf->base_buffer;
16169 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16170 recompute_width_table (current_buffer, disptab);
16174 /* If window-start is screwed up, choose a new one. */
16175 if (XMARKER (w->start)->buffer != current_buffer)
16176 goto recenter;
16178 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16180 /* If someone specified a new starting point but did not insist,
16181 check whether it can be used. */
16182 if ((w->optional_new_start || window_frozen_p (w))
16183 && CHARPOS (startp) >= BEGV
16184 && CHARPOS (startp) <= ZV)
16186 ptrdiff_t it_charpos;
16188 w->optional_new_start = 0;
16189 start_display (&it, w, startp);
16190 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16191 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16192 /* Record IT's position now, since line_bottom_y might change
16193 that. */
16194 it_charpos = IT_CHARPOS (it);
16195 /* Make sure we set the force_start flag only if the cursor row
16196 will be fully visible. Otherwise, the code under force_start
16197 label below will try to move point back into view, which is
16198 not what the code which sets optional_new_start wants. */
16199 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16200 && !w->force_start)
16202 if (it_charpos == PT)
16203 w->force_start = 1;
16204 /* IT may overshoot PT if text at PT is invisible. */
16205 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16206 w->force_start = 1;
16207 #ifdef GLYPH_DEBUG
16208 if (w->force_start)
16210 if (window_frozen_p (w))
16211 debug_method_add (w, "set force_start from frozen window start");
16212 else
16213 debug_method_add (w, "set force_start from optional_new_start");
16215 #endif
16219 force_start:
16221 /* Handle case where place to start displaying has been specified,
16222 unless the specified location is outside the accessible range. */
16223 if (w->force_start)
16225 /* We set this later on if we have to adjust point. */
16226 int new_vpos = -1;
16228 w->force_start = 0;
16229 w->vscroll = 0;
16230 w->window_end_valid = 0;
16232 /* Forget any recorded base line for line number display. */
16233 if (!buffer_unchanged_p)
16234 w->base_line_number = 0;
16236 /* Redisplay the mode line. Select the buffer properly for that.
16237 Also, run the hook window-scroll-functions
16238 because we have scrolled. */
16239 /* Note, we do this after clearing force_start because
16240 if there's an error, it is better to forget about force_start
16241 than to get into an infinite loop calling the hook functions
16242 and having them get more errors. */
16243 if (!update_mode_line
16244 || ! NILP (Vwindow_scroll_functions))
16246 update_mode_line = 1;
16247 w->update_mode_line = 1;
16248 startp = run_window_scroll_functions (window, startp);
16251 if (CHARPOS (startp) < BEGV)
16252 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16253 else if (CHARPOS (startp) > ZV)
16254 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16256 /* Redisplay, then check if cursor has been set during the
16257 redisplay. Give up if new fonts were loaded. */
16258 /* We used to issue a CHECK_MARGINS argument to try_window here,
16259 but this causes scrolling to fail when point begins inside
16260 the scroll margin (bug#148) -- cyd */
16261 if (!try_window (window, startp, 0))
16263 w->force_start = 1;
16264 clear_glyph_matrix (w->desired_matrix);
16265 goto need_larger_matrices;
16268 if (w->cursor.vpos < 0)
16270 /* If point does not appear, try to move point so it does
16271 appear. The desired matrix has been built above, so we
16272 can use it here. */
16273 new_vpos = window_box_height (w) / 2;
16276 if (!cursor_row_fully_visible_p (w, 0, 0))
16278 /* Point does appear, but on a line partly visible at end of window.
16279 Move it back to a fully-visible line. */
16280 new_vpos = window_box_height (w);
16281 /* But if window_box_height suggests a Y coordinate that is
16282 not less than we already have, that line will clearly not
16283 be fully visible, so give up and scroll the display.
16284 This can happen when the default face uses a font whose
16285 dimensions are different from the frame's default
16286 font. */
16287 if (new_vpos >= w->cursor.y)
16289 w->cursor.vpos = -1;
16290 clear_glyph_matrix (w->desired_matrix);
16291 goto try_to_scroll;
16294 else if (w->cursor.vpos >= 0)
16296 /* Some people insist on not letting point enter the scroll
16297 margin, even though this part handles windows that didn't
16298 scroll at all. */
16299 int window_total_lines
16300 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16301 int margin = min (scroll_margin, window_total_lines / 4);
16302 int pixel_margin = margin * frame_line_height;
16303 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16305 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16306 below, which finds the row to move point to, advances by
16307 the Y coordinate of the _next_ row, see the definition of
16308 MATRIX_ROW_BOTTOM_Y. */
16309 if (w->cursor.vpos < margin + header_line)
16311 w->cursor.vpos = -1;
16312 clear_glyph_matrix (w->desired_matrix);
16313 goto try_to_scroll;
16315 else
16317 int window_height = window_box_height (w);
16319 if (header_line)
16320 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16321 if (w->cursor.y >= window_height - pixel_margin)
16323 w->cursor.vpos = -1;
16324 clear_glyph_matrix (w->desired_matrix);
16325 goto try_to_scroll;
16330 /* If we need to move point for either of the above reasons,
16331 now actually do it. */
16332 if (new_vpos >= 0)
16334 struct glyph_row *row;
16336 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16337 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16338 ++row;
16340 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16341 MATRIX_ROW_START_BYTEPOS (row));
16343 if (w != XWINDOW (selected_window))
16344 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16345 else if (current_buffer == old)
16346 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16348 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16350 /* If we are highlighting the region, then we just changed
16351 the region, so redisplay to show it. */
16352 /* FIXME: We need to (re)run pre-redisplay-function! */
16353 /* if (markpos_of_region () >= 0)
16355 clear_glyph_matrix (w->desired_matrix);
16356 if (!try_window (window, startp, 0))
16357 goto need_larger_matrices;
16361 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16363 clear_glyph_matrix (w->desired_matrix);
16364 goto try_to_scroll;
16367 #ifdef GLYPH_DEBUG
16368 debug_method_add (w, "forced window start");
16369 #endif
16370 goto done;
16373 /* Handle case where text has not changed, only point, and it has
16374 not moved off the frame, and we are not retrying after hscroll.
16375 (current_matrix_up_to_date_p is nonzero when retrying.) */
16376 if (current_matrix_up_to_date_p
16377 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16378 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16380 switch (rc)
16382 case CURSOR_MOVEMENT_SUCCESS:
16383 used_current_matrix_p = 1;
16384 goto done;
16386 case CURSOR_MOVEMENT_MUST_SCROLL:
16387 goto try_to_scroll;
16389 default:
16390 emacs_abort ();
16393 /* If current starting point was originally the beginning of a line
16394 but no longer is, find a new starting point. */
16395 else if (w->start_at_line_beg
16396 && !(CHARPOS (startp) <= BEGV
16397 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16399 #ifdef GLYPH_DEBUG
16400 debug_method_add (w, "recenter 1");
16401 #endif
16402 goto recenter;
16405 /* Try scrolling with try_window_id. Value is > 0 if update has
16406 been done, it is -1 if we know that the same window start will
16407 not work. It is 0 if unsuccessful for some other reason. */
16408 else if ((tem = try_window_id (w)) != 0)
16410 #ifdef GLYPH_DEBUG
16411 debug_method_add (w, "try_window_id %d", tem);
16412 #endif
16414 if (f->fonts_changed)
16415 goto need_larger_matrices;
16416 if (tem > 0)
16417 goto done;
16419 /* Otherwise try_window_id has returned -1 which means that we
16420 don't want the alternative below this comment to execute. */
16422 else if (CHARPOS (startp) >= BEGV
16423 && CHARPOS (startp) <= ZV
16424 && PT >= CHARPOS (startp)
16425 && (CHARPOS (startp) < ZV
16426 /* Avoid starting at end of buffer. */
16427 || CHARPOS (startp) == BEGV
16428 || !window_outdated (w)))
16430 int d1, d2, d5, d6;
16431 int rtop, rbot;
16433 /* If first window line is a continuation line, and window start
16434 is inside the modified region, but the first change is before
16435 current window start, we must select a new window start.
16437 However, if this is the result of a down-mouse event (e.g. by
16438 extending the mouse-drag-overlay), we don't want to select a
16439 new window start, since that would change the position under
16440 the mouse, resulting in an unwanted mouse-movement rather
16441 than a simple mouse-click. */
16442 if (!w->start_at_line_beg
16443 && NILP (do_mouse_tracking)
16444 && CHARPOS (startp) > BEGV
16445 && CHARPOS (startp) > BEG + beg_unchanged
16446 && CHARPOS (startp) <= Z - end_unchanged
16447 /* Even if w->start_at_line_beg is nil, a new window may
16448 start at a line_beg, since that's how set_buffer_window
16449 sets it. So, we need to check the return value of
16450 compute_window_start_on_continuation_line. (See also
16451 bug#197). */
16452 && XMARKER (w->start)->buffer == current_buffer
16453 && compute_window_start_on_continuation_line (w)
16454 /* It doesn't make sense to force the window start like we
16455 do at label force_start if it is already known that point
16456 will not be fully visible in the resulting window, because
16457 doing so will move point from its correct position
16458 instead of scrolling the window to bring point into view.
16459 See bug#9324. */
16460 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16461 /* A very tall row could need more than the window height,
16462 in which case we accept that it is partially visible. */
16463 && (rtop != 0) == (rbot != 0))
16465 w->force_start = 1;
16466 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16467 #ifdef GLYPH_DEBUG
16468 debug_method_add (w, "recomputed window start in continuation line");
16469 #endif
16470 goto force_start;
16473 #ifdef GLYPH_DEBUG
16474 debug_method_add (w, "same window start");
16475 #endif
16477 /* Try to redisplay starting at same place as before.
16478 If point has not moved off frame, accept the results. */
16479 if (!current_matrix_up_to_date_p
16480 /* Don't use try_window_reusing_current_matrix in this case
16481 because a window scroll function can have changed the
16482 buffer. */
16483 || !NILP (Vwindow_scroll_functions)
16484 || MINI_WINDOW_P (w)
16485 || !(used_current_matrix_p
16486 = try_window_reusing_current_matrix (w)))
16488 IF_DEBUG (debug_method_add (w, "1"));
16489 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16490 /* -1 means we need to scroll.
16491 0 means we need new matrices, but fonts_changed
16492 is set in that case, so we will detect it below. */
16493 goto try_to_scroll;
16496 if (f->fonts_changed)
16497 goto need_larger_matrices;
16499 if (w->cursor.vpos >= 0)
16501 if (!just_this_one_p
16502 || current_buffer->clip_changed
16503 || BEG_UNCHANGED < CHARPOS (startp))
16504 /* Forget any recorded base line for line number display. */
16505 w->base_line_number = 0;
16507 if (!cursor_row_fully_visible_p (w, 1, 0))
16509 clear_glyph_matrix (w->desired_matrix);
16510 last_line_misfit = 1;
16512 /* Drop through and scroll. */
16513 else
16514 goto done;
16516 else
16517 clear_glyph_matrix (w->desired_matrix);
16520 try_to_scroll:
16522 /* Redisplay the mode line. Select the buffer properly for that. */
16523 if (!update_mode_line)
16525 update_mode_line = 1;
16526 w->update_mode_line = 1;
16529 /* Try to scroll by specified few lines. */
16530 if ((scroll_conservatively
16531 || emacs_scroll_step
16532 || temp_scroll_step
16533 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16534 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16535 && CHARPOS (startp) >= BEGV
16536 && CHARPOS (startp) <= ZV)
16538 /* The function returns -1 if new fonts were loaded, 1 if
16539 successful, 0 if not successful. */
16540 int ss = try_scrolling (window, just_this_one_p,
16541 scroll_conservatively,
16542 emacs_scroll_step,
16543 temp_scroll_step, last_line_misfit);
16544 switch (ss)
16546 case SCROLLING_SUCCESS:
16547 goto done;
16549 case SCROLLING_NEED_LARGER_MATRICES:
16550 goto need_larger_matrices;
16552 case SCROLLING_FAILED:
16553 break;
16555 default:
16556 emacs_abort ();
16560 /* Finally, just choose a place to start which positions point
16561 according to user preferences. */
16563 recenter:
16565 #ifdef GLYPH_DEBUG
16566 debug_method_add (w, "recenter");
16567 #endif
16569 /* Forget any previously recorded base line for line number display. */
16570 if (!buffer_unchanged_p)
16571 w->base_line_number = 0;
16573 /* Determine the window start relative to point. */
16574 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16575 it.current_y = it.last_visible_y;
16576 if (centering_position < 0)
16578 int window_total_lines
16579 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16580 int margin =
16581 scroll_margin > 0
16582 ? min (scroll_margin, window_total_lines / 4)
16583 : 0;
16584 ptrdiff_t margin_pos = CHARPOS (startp);
16585 Lisp_Object aggressive;
16586 int scrolling_up;
16588 /* If there is a scroll margin at the top of the window, find
16589 its character position. */
16590 if (margin
16591 /* Cannot call start_display if startp is not in the
16592 accessible region of the buffer. This can happen when we
16593 have just switched to a different buffer and/or changed
16594 its restriction. In that case, startp is initialized to
16595 the character position 1 (BEGV) because we did not yet
16596 have chance to display the buffer even once. */
16597 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16599 struct it it1;
16600 void *it1data = NULL;
16602 SAVE_IT (it1, it, it1data);
16603 start_display (&it1, w, startp);
16604 move_it_vertically (&it1, margin * frame_line_height);
16605 margin_pos = IT_CHARPOS (it1);
16606 RESTORE_IT (&it, &it, it1data);
16608 scrolling_up = PT > margin_pos;
16609 aggressive =
16610 scrolling_up
16611 ? BVAR (current_buffer, scroll_up_aggressively)
16612 : BVAR (current_buffer, scroll_down_aggressively);
16614 if (!MINI_WINDOW_P (w)
16615 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16617 int pt_offset = 0;
16619 /* Setting scroll-conservatively overrides
16620 scroll-*-aggressively. */
16621 if (!scroll_conservatively && NUMBERP (aggressive))
16623 double float_amount = XFLOATINT (aggressive);
16625 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16626 if (pt_offset == 0 && float_amount > 0)
16627 pt_offset = 1;
16628 if (pt_offset && margin > 0)
16629 margin -= 1;
16631 /* Compute how much to move the window start backward from
16632 point so that point will be displayed where the user
16633 wants it. */
16634 if (scrolling_up)
16636 centering_position = it.last_visible_y;
16637 if (pt_offset)
16638 centering_position -= pt_offset;
16639 centering_position -=
16640 frame_line_height * (1 + margin + (last_line_misfit != 0))
16641 + WINDOW_HEADER_LINE_HEIGHT (w);
16642 /* Don't let point enter the scroll margin near top of
16643 the window. */
16644 if (centering_position < margin * frame_line_height)
16645 centering_position = margin * frame_line_height;
16647 else
16648 centering_position = margin * frame_line_height + pt_offset;
16650 else
16651 /* Set the window start half the height of the window backward
16652 from point. */
16653 centering_position = window_box_height (w) / 2;
16655 move_it_vertically_backward (&it, centering_position);
16657 eassert (IT_CHARPOS (it) >= BEGV);
16659 /* The function move_it_vertically_backward may move over more
16660 than the specified y-distance. If it->w is small, e.g. a
16661 mini-buffer window, we may end up in front of the window's
16662 display area. Start displaying at the start of the line
16663 containing PT in this case. */
16664 if (it.current_y <= 0)
16666 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16667 move_it_vertically_backward (&it, 0);
16668 it.current_y = 0;
16671 it.current_x = it.hpos = 0;
16673 /* Set the window start position here explicitly, to avoid an
16674 infinite loop in case the functions in window-scroll-functions
16675 get errors. */
16676 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16678 /* Run scroll hooks. */
16679 startp = run_window_scroll_functions (window, it.current.pos);
16681 /* Redisplay the window. */
16682 if (!current_matrix_up_to_date_p
16683 || windows_or_buffers_changed
16684 || f->cursor_type_changed
16685 /* Don't use try_window_reusing_current_matrix in this case
16686 because it can have changed the buffer. */
16687 || !NILP (Vwindow_scroll_functions)
16688 || !just_this_one_p
16689 || MINI_WINDOW_P (w)
16690 || !(used_current_matrix_p
16691 = try_window_reusing_current_matrix (w)))
16692 try_window (window, startp, 0);
16694 /* If new fonts have been loaded (due to fontsets), give up. We
16695 have to start a new redisplay since we need to re-adjust glyph
16696 matrices. */
16697 if (f->fonts_changed)
16698 goto need_larger_matrices;
16700 /* If cursor did not appear assume that the middle of the window is
16701 in the first line of the window. Do it again with the next line.
16702 (Imagine a window of height 100, displaying two lines of height
16703 60. Moving back 50 from it->last_visible_y will end in the first
16704 line.) */
16705 if (w->cursor.vpos < 0)
16707 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16709 clear_glyph_matrix (w->desired_matrix);
16710 move_it_by_lines (&it, 1);
16711 try_window (window, it.current.pos, 0);
16713 else if (PT < IT_CHARPOS (it))
16715 clear_glyph_matrix (w->desired_matrix);
16716 move_it_by_lines (&it, -1);
16717 try_window (window, it.current.pos, 0);
16719 else
16721 /* Not much we can do about it. */
16725 /* Consider the following case: Window starts at BEGV, there is
16726 invisible, intangible text at BEGV, so that display starts at
16727 some point START > BEGV. It can happen that we are called with
16728 PT somewhere between BEGV and START. Try to handle that case,
16729 and similar ones. */
16730 if (w->cursor.vpos < 0)
16732 /* First, try locating the proper glyph row for PT. */
16733 struct glyph_row *row =
16734 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16736 /* Sometimes point is at the beginning of invisible text that is
16737 before the 1st character displayed in the row. In that case,
16738 row_containing_pos fails to find the row, because no glyphs
16739 with appropriate buffer positions are present in the row.
16740 Therefore, we next try to find the row which shows the 1st
16741 position after the invisible text. */
16742 if (!row)
16744 Lisp_Object val =
16745 get_char_property_and_overlay (make_number (PT), Qinvisible,
16746 Qnil, NULL);
16748 if (TEXT_PROP_MEANS_INVISIBLE (val))
16750 ptrdiff_t alt_pos;
16751 Lisp_Object invis_end =
16752 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16753 Qnil, Qnil);
16755 if (NATNUMP (invis_end))
16756 alt_pos = XFASTINT (invis_end);
16757 else
16758 alt_pos = ZV;
16759 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16760 NULL, 0);
16763 /* Finally, fall back on the first row of the window after the
16764 header line (if any). This is slightly better than not
16765 displaying the cursor at all. */
16766 if (!row)
16768 row = w->current_matrix->rows;
16769 if (row->mode_line_p)
16770 ++row;
16772 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16775 if (!cursor_row_fully_visible_p (w, 0, 0))
16777 /* If vscroll is enabled, disable it and try again. */
16778 if (w->vscroll)
16780 w->vscroll = 0;
16781 clear_glyph_matrix (w->desired_matrix);
16782 goto recenter;
16785 /* Users who set scroll-conservatively to a large number want
16786 point just above/below the scroll margin. If we ended up
16787 with point's row partially visible, move the window start to
16788 make that row fully visible and out of the margin. */
16789 if (scroll_conservatively > SCROLL_LIMIT)
16791 int window_total_lines
16792 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16793 int margin =
16794 scroll_margin > 0
16795 ? min (scroll_margin, window_total_lines / 4)
16796 : 0;
16797 int move_down = w->cursor.vpos >= window_total_lines / 2;
16799 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16800 clear_glyph_matrix (w->desired_matrix);
16801 if (1 == try_window (window, it.current.pos,
16802 TRY_WINDOW_CHECK_MARGINS))
16803 goto done;
16806 /* If centering point failed to make the whole line visible,
16807 put point at the top instead. That has to make the whole line
16808 visible, if it can be done. */
16809 if (centering_position == 0)
16810 goto done;
16812 clear_glyph_matrix (w->desired_matrix);
16813 centering_position = 0;
16814 goto recenter;
16817 done:
16819 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16820 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16821 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16823 /* Display the mode line, if we must. */
16824 if ((update_mode_line
16825 /* If window not full width, must redo its mode line
16826 if (a) the window to its side is being redone and
16827 (b) we do a frame-based redisplay. This is a consequence
16828 of how inverted lines are drawn in frame-based redisplay. */
16829 || (!just_this_one_p
16830 && !FRAME_WINDOW_P (f)
16831 && !WINDOW_FULL_WIDTH_P (w))
16832 /* Line number to display. */
16833 || w->base_line_pos > 0
16834 /* Column number is displayed and different from the one displayed. */
16835 || (w->column_number_displayed != -1
16836 && (w->column_number_displayed != current_column ())))
16837 /* This means that the window has a mode line. */
16838 && (WINDOW_WANTS_MODELINE_P (w)
16839 || WINDOW_WANTS_HEADER_LINE_P (w)))
16842 display_mode_lines (w);
16844 /* If mode line height has changed, arrange for a thorough
16845 immediate redisplay using the correct mode line height. */
16846 if (WINDOW_WANTS_MODELINE_P (w)
16847 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16849 f->fonts_changed = 1;
16850 w->mode_line_height = -1;
16851 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16852 = DESIRED_MODE_LINE_HEIGHT (w);
16855 /* If header line height has changed, arrange for a thorough
16856 immediate redisplay using the correct header line height. */
16857 if (WINDOW_WANTS_HEADER_LINE_P (w)
16858 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16860 f->fonts_changed = 1;
16861 w->header_line_height = -1;
16862 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16863 = DESIRED_HEADER_LINE_HEIGHT (w);
16866 if (f->fonts_changed)
16867 goto need_larger_matrices;
16870 if (!line_number_displayed && w->base_line_pos != -1)
16872 w->base_line_pos = 0;
16873 w->base_line_number = 0;
16876 finish_menu_bars:
16878 /* When we reach a frame's selected window, redo the frame's menu bar. */
16879 if (update_mode_line
16880 && EQ (FRAME_SELECTED_WINDOW (f), window))
16882 int redisplay_menu_p = 0;
16884 if (FRAME_WINDOW_P (f))
16886 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16887 || defined (HAVE_NS) || defined (USE_GTK)
16888 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16889 #else
16890 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16891 #endif
16893 else
16894 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16896 if (redisplay_menu_p)
16897 display_menu_bar (w);
16899 #ifdef HAVE_WINDOW_SYSTEM
16900 if (FRAME_WINDOW_P (f))
16902 #if defined (USE_GTK) || defined (HAVE_NS)
16903 if (FRAME_EXTERNAL_TOOL_BAR (f))
16904 redisplay_tool_bar (f);
16905 #else
16906 if (WINDOWP (f->tool_bar_window)
16907 && (FRAME_TOOL_BAR_LINES (f) > 0
16908 || !NILP (Vauto_resize_tool_bars))
16909 && redisplay_tool_bar (f))
16910 ignore_mouse_drag_p = 1;
16911 #endif
16913 #endif
16916 #ifdef HAVE_WINDOW_SYSTEM
16917 if (FRAME_WINDOW_P (f)
16918 && update_window_fringes (w, (just_this_one_p
16919 || (!used_current_matrix_p && !overlay_arrow_seen)
16920 || w->pseudo_window_p)))
16922 update_begin (f);
16923 block_input ();
16924 if (draw_window_fringes (w, 1))
16926 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16927 x_draw_right_divider (w);
16928 else
16929 x_draw_vertical_border (w);
16931 unblock_input ();
16932 update_end (f);
16935 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16936 x_draw_bottom_divider (w);
16937 #endif /* HAVE_WINDOW_SYSTEM */
16939 /* We go to this label, with fonts_changed set, if it is
16940 necessary to try again using larger glyph matrices.
16941 We have to redeem the scroll bar even in this case,
16942 because the loop in redisplay_internal expects that. */
16943 need_larger_matrices:
16945 finish_scroll_bars:
16947 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16949 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16950 /* Set the thumb's position and size. */
16951 set_vertical_scroll_bar (w);
16953 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
16954 /* Set the thumb's position and size. */
16955 set_horizontal_scroll_bar (w);
16957 /* Note that we actually used the scroll bar attached to this
16958 window, so it shouldn't be deleted at the end of redisplay. */
16959 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16960 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16963 /* Restore current_buffer and value of point in it. The window
16964 update may have changed the buffer, so first make sure `opoint'
16965 is still valid (Bug#6177). */
16966 if (CHARPOS (opoint) < BEGV)
16967 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16968 else if (CHARPOS (opoint) > ZV)
16969 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16970 else
16971 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16973 set_buffer_internal_1 (old);
16974 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16975 shorter. This can be caused by log truncation in *Messages*. */
16976 if (CHARPOS (lpoint) <= ZV)
16977 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16979 unbind_to (count, Qnil);
16983 /* Build the complete desired matrix of WINDOW with a window start
16984 buffer position POS.
16986 Value is 1 if successful. It is zero if fonts were loaded during
16987 redisplay which makes re-adjusting glyph matrices necessary, and -1
16988 if point would appear in the scroll margins.
16989 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16990 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16991 set in FLAGS.) */
16994 try_window (Lisp_Object window, struct text_pos pos, int flags)
16996 struct window *w = XWINDOW (window);
16997 struct it it;
16998 struct glyph_row *last_text_row = NULL;
16999 struct frame *f = XFRAME (w->frame);
17000 int frame_line_height = default_line_pixel_height (w);
17002 /* Make POS the new window start. */
17003 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17005 /* Mark cursor position as unknown. No overlay arrow seen. */
17006 w->cursor.vpos = -1;
17007 overlay_arrow_seen = 0;
17009 /* Initialize iterator and info to start at POS. */
17010 start_display (&it, w, pos);
17011 it.glyph_row->reversed_p = false;
17013 /* Display all lines of W. */
17014 while (it.current_y < it.last_visible_y)
17016 if (display_line (&it))
17017 last_text_row = it.glyph_row - 1;
17018 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17019 return 0;
17022 /* Don't let the cursor end in the scroll margins. */
17023 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17024 && !MINI_WINDOW_P (w))
17026 int this_scroll_margin;
17027 int window_total_lines
17028 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17030 if (scroll_margin > 0)
17032 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17033 this_scroll_margin *= frame_line_height;
17035 else
17036 this_scroll_margin = 0;
17038 if ((w->cursor.y >= 0 /* not vscrolled */
17039 && w->cursor.y < this_scroll_margin
17040 && CHARPOS (pos) > BEGV
17041 && IT_CHARPOS (it) < ZV)
17042 /* rms: considering make_cursor_line_fully_visible_p here
17043 seems to give wrong results. We don't want to recenter
17044 when the last line is partly visible, we want to allow
17045 that case to be handled in the usual way. */
17046 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17048 w->cursor.vpos = -1;
17049 clear_glyph_matrix (w->desired_matrix);
17050 return -1;
17054 /* If bottom moved off end of frame, change mode line percentage. */
17055 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17056 w->update_mode_line = 1;
17058 /* Set window_end_pos to the offset of the last character displayed
17059 on the window from the end of current_buffer. Set
17060 window_end_vpos to its row number. */
17061 if (last_text_row)
17063 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17064 adjust_window_ends (w, last_text_row, 0);
17065 eassert
17066 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17067 w->window_end_vpos)));
17069 else
17071 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17072 w->window_end_pos = Z - ZV;
17073 w->window_end_vpos = 0;
17076 /* But that is not valid info until redisplay finishes. */
17077 w->window_end_valid = 0;
17078 return 1;
17083 /************************************************************************
17084 Window redisplay reusing current matrix when buffer has not changed
17085 ************************************************************************/
17087 /* Try redisplay of window W showing an unchanged buffer with a
17088 different window start than the last time it was displayed by
17089 reusing its current matrix. Value is non-zero if successful.
17090 W->start is the new window start. */
17092 static int
17093 try_window_reusing_current_matrix (struct window *w)
17095 struct frame *f = XFRAME (w->frame);
17096 struct glyph_row *bottom_row;
17097 struct it it;
17098 struct run run;
17099 struct text_pos start, new_start;
17100 int nrows_scrolled, i;
17101 struct glyph_row *last_text_row;
17102 struct glyph_row *last_reused_text_row;
17103 struct glyph_row *start_row;
17104 int start_vpos, min_y, max_y;
17106 #ifdef GLYPH_DEBUG
17107 if (inhibit_try_window_reusing)
17108 return 0;
17109 #endif
17111 if (/* This function doesn't handle terminal frames. */
17112 !FRAME_WINDOW_P (f)
17113 /* Don't try to reuse the display if windows have been split
17114 or such. */
17115 || windows_or_buffers_changed
17116 || f->cursor_type_changed)
17117 return 0;
17119 /* Can't do this if showing trailing whitespace. */
17120 if (!NILP (Vshow_trailing_whitespace))
17121 return 0;
17123 /* If top-line visibility has changed, give up. */
17124 if (WINDOW_WANTS_HEADER_LINE_P (w)
17125 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17126 return 0;
17128 /* Give up if old or new display is scrolled vertically. We could
17129 make this function handle this, but right now it doesn't. */
17130 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17131 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17132 return 0;
17134 /* The variable new_start now holds the new window start. The old
17135 start `start' can be determined from the current matrix. */
17136 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17137 start = start_row->minpos;
17138 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17140 /* Clear the desired matrix for the display below. */
17141 clear_glyph_matrix (w->desired_matrix);
17143 if (CHARPOS (new_start) <= CHARPOS (start))
17145 /* Don't use this method if the display starts with an ellipsis
17146 displayed for invisible text. It's not easy to handle that case
17147 below, and it's certainly not worth the effort since this is
17148 not a frequent case. */
17149 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17150 return 0;
17152 IF_DEBUG (debug_method_add (w, "twu1"));
17154 /* Display up to a row that can be reused. The variable
17155 last_text_row is set to the last row displayed that displays
17156 text. Note that it.vpos == 0 if or if not there is a
17157 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17158 start_display (&it, w, new_start);
17159 w->cursor.vpos = -1;
17160 last_text_row = last_reused_text_row = NULL;
17162 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17164 /* If we have reached into the characters in the START row,
17165 that means the line boundaries have changed. So we
17166 can't start copying with the row START. Maybe it will
17167 work to start copying with the following row. */
17168 while (IT_CHARPOS (it) > CHARPOS (start))
17170 /* Advance to the next row as the "start". */
17171 start_row++;
17172 start = start_row->minpos;
17173 /* If there are no more rows to try, or just one, give up. */
17174 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17175 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17176 || CHARPOS (start) == ZV)
17178 clear_glyph_matrix (w->desired_matrix);
17179 return 0;
17182 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17184 /* If we have reached alignment, we can copy the rest of the
17185 rows. */
17186 if (IT_CHARPOS (it) == CHARPOS (start)
17187 /* Don't accept "alignment" inside a display vector,
17188 since start_row could have started in the middle of
17189 that same display vector (thus their character
17190 positions match), and we have no way of telling if
17191 that is the case. */
17192 && it.current.dpvec_index < 0)
17193 break;
17195 it.glyph_row->reversed_p = false;
17196 if (display_line (&it))
17197 last_text_row = it.glyph_row - 1;
17201 /* A value of current_y < last_visible_y means that we stopped
17202 at the previous window start, which in turn means that we
17203 have at least one reusable row. */
17204 if (it.current_y < it.last_visible_y)
17206 struct glyph_row *row;
17208 /* IT.vpos always starts from 0; it counts text lines. */
17209 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17211 /* Find PT if not already found in the lines displayed. */
17212 if (w->cursor.vpos < 0)
17214 int dy = it.current_y - start_row->y;
17216 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17217 row = row_containing_pos (w, PT, row, NULL, dy);
17218 if (row)
17219 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17220 dy, nrows_scrolled);
17221 else
17223 clear_glyph_matrix (w->desired_matrix);
17224 return 0;
17228 /* Scroll the display. Do it before the current matrix is
17229 changed. The problem here is that update has not yet
17230 run, i.e. part of the current matrix is not up to date.
17231 scroll_run_hook will clear the cursor, and use the
17232 current matrix to get the height of the row the cursor is
17233 in. */
17234 run.current_y = start_row->y;
17235 run.desired_y = it.current_y;
17236 run.height = it.last_visible_y - it.current_y;
17238 if (run.height > 0 && run.current_y != run.desired_y)
17240 update_begin (f);
17241 FRAME_RIF (f)->update_window_begin_hook (w);
17242 FRAME_RIF (f)->clear_window_mouse_face (w);
17243 FRAME_RIF (f)->scroll_run_hook (w, &run);
17244 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17245 update_end (f);
17248 /* Shift current matrix down by nrows_scrolled lines. */
17249 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17250 rotate_matrix (w->current_matrix,
17251 start_vpos,
17252 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17253 nrows_scrolled);
17255 /* Disable lines that must be updated. */
17256 for (i = 0; i < nrows_scrolled; ++i)
17257 (start_row + i)->enabled_p = false;
17259 /* Re-compute Y positions. */
17260 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17261 max_y = it.last_visible_y;
17262 for (row = start_row + nrows_scrolled;
17263 row < bottom_row;
17264 ++row)
17266 row->y = it.current_y;
17267 row->visible_height = row->height;
17269 if (row->y < min_y)
17270 row->visible_height -= min_y - row->y;
17271 if (row->y + row->height > max_y)
17272 row->visible_height -= row->y + row->height - max_y;
17273 if (row->fringe_bitmap_periodic_p)
17274 row->redraw_fringe_bitmaps_p = 1;
17276 it.current_y += row->height;
17278 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17279 last_reused_text_row = row;
17280 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17281 break;
17284 /* Disable lines in the current matrix which are now
17285 below the window. */
17286 for (++row; row < bottom_row; ++row)
17287 row->enabled_p = row->mode_line_p = 0;
17290 /* Update window_end_pos etc.; last_reused_text_row is the last
17291 reused row from the current matrix containing text, if any.
17292 The value of last_text_row is the last displayed line
17293 containing text. */
17294 if (last_reused_text_row)
17295 adjust_window_ends (w, last_reused_text_row, 1);
17296 else if (last_text_row)
17297 adjust_window_ends (w, last_text_row, 0);
17298 else
17300 /* This window must be completely empty. */
17301 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17302 w->window_end_pos = Z - ZV;
17303 w->window_end_vpos = 0;
17305 w->window_end_valid = 0;
17307 /* Update hint: don't try scrolling again in update_window. */
17308 w->desired_matrix->no_scrolling_p = 1;
17310 #ifdef GLYPH_DEBUG
17311 debug_method_add (w, "try_window_reusing_current_matrix 1");
17312 #endif
17313 return 1;
17315 else if (CHARPOS (new_start) > CHARPOS (start))
17317 struct glyph_row *pt_row, *row;
17318 struct glyph_row *first_reusable_row;
17319 struct glyph_row *first_row_to_display;
17320 int dy;
17321 int yb = window_text_bottom_y (w);
17323 /* Find the row starting at new_start, if there is one. Don't
17324 reuse a partially visible line at the end. */
17325 first_reusable_row = start_row;
17326 while (first_reusable_row->enabled_p
17327 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17328 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17329 < CHARPOS (new_start)))
17330 ++first_reusable_row;
17332 /* Give up if there is no row to reuse. */
17333 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17334 || !first_reusable_row->enabled_p
17335 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17336 != CHARPOS (new_start)))
17337 return 0;
17339 /* We can reuse fully visible rows beginning with
17340 first_reusable_row to the end of the window. Set
17341 first_row_to_display to the first row that cannot be reused.
17342 Set pt_row to the row containing point, if there is any. */
17343 pt_row = NULL;
17344 for (first_row_to_display = first_reusable_row;
17345 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17346 ++first_row_to_display)
17348 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17349 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17350 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17351 && first_row_to_display->ends_at_zv_p
17352 && pt_row == NULL)))
17353 pt_row = first_row_to_display;
17356 /* Start displaying at the start of first_row_to_display. */
17357 eassert (first_row_to_display->y < yb);
17358 init_to_row_start (&it, w, first_row_to_display);
17360 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17361 - start_vpos);
17362 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17363 - nrows_scrolled);
17364 it.current_y = (first_row_to_display->y - first_reusable_row->y
17365 + WINDOW_HEADER_LINE_HEIGHT (w));
17367 /* Display lines beginning with first_row_to_display in the
17368 desired matrix. Set last_text_row to the last row displayed
17369 that displays text. */
17370 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17371 if (pt_row == NULL)
17372 w->cursor.vpos = -1;
17373 last_text_row = NULL;
17374 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17375 if (display_line (&it))
17376 last_text_row = it.glyph_row - 1;
17378 /* If point is in a reused row, adjust y and vpos of the cursor
17379 position. */
17380 if (pt_row)
17382 w->cursor.vpos -= nrows_scrolled;
17383 w->cursor.y -= first_reusable_row->y - start_row->y;
17386 /* Give up if point isn't in a row displayed or reused. (This
17387 also handles the case where w->cursor.vpos < nrows_scrolled
17388 after the calls to display_line, which can happen with scroll
17389 margins. See bug#1295.) */
17390 if (w->cursor.vpos < 0)
17392 clear_glyph_matrix (w->desired_matrix);
17393 return 0;
17396 /* Scroll the display. */
17397 run.current_y = first_reusable_row->y;
17398 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17399 run.height = it.last_visible_y - run.current_y;
17400 dy = run.current_y - run.desired_y;
17402 if (run.height)
17404 update_begin (f);
17405 FRAME_RIF (f)->update_window_begin_hook (w);
17406 FRAME_RIF (f)->clear_window_mouse_face (w);
17407 FRAME_RIF (f)->scroll_run_hook (w, &run);
17408 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17409 update_end (f);
17412 /* Adjust Y positions of reused rows. */
17413 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17414 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17415 max_y = it.last_visible_y;
17416 for (row = first_reusable_row; row < first_row_to_display; ++row)
17418 row->y -= dy;
17419 row->visible_height = row->height;
17420 if (row->y < min_y)
17421 row->visible_height -= min_y - row->y;
17422 if (row->y + row->height > max_y)
17423 row->visible_height -= row->y + row->height - max_y;
17424 if (row->fringe_bitmap_periodic_p)
17425 row->redraw_fringe_bitmaps_p = 1;
17428 /* Scroll the current matrix. */
17429 eassert (nrows_scrolled > 0);
17430 rotate_matrix (w->current_matrix,
17431 start_vpos,
17432 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17433 -nrows_scrolled);
17435 /* Disable rows not reused. */
17436 for (row -= nrows_scrolled; row < bottom_row; ++row)
17437 row->enabled_p = false;
17439 /* Point may have moved to a different line, so we cannot assume that
17440 the previous cursor position is valid; locate the correct row. */
17441 if (pt_row)
17443 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17444 row < bottom_row
17445 && PT >= MATRIX_ROW_END_CHARPOS (row)
17446 && !row->ends_at_zv_p;
17447 row++)
17449 w->cursor.vpos++;
17450 w->cursor.y = row->y;
17452 if (row < bottom_row)
17454 /* Can't simply scan the row for point with
17455 bidi-reordered glyph rows. Let set_cursor_from_row
17456 figure out where to put the cursor, and if it fails,
17457 give up. */
17458 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17460 if (!set_cursor_from_row (w, row, w->current_matrix,
17461 0, 0, 0, 0))
17463 clear_glyph_matrix (w->desired_matrix);
17464 return 0;
17467 else
17469 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17470 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17472 for (; glyph < end
17473 && (!BUFFERP (glyph->object)
17474 || glyph->charpos < PT);
17475 glyph++)
17477 w->cursor.hpos++;
17478 w->cursor.x += glyph->pixel_width;
17484 /* Adjust window end. A null value of last_text_row means that
17485 the window end is in reused rows which in turn means that
17486 only its vpos can have changed. */
17487 if (last_text_row)
17488 adjust_window_ends (w, last_text_row, 0);
17489 else
17490 w->window_end_vpos -= nrows_scrolled;
17492 w->window_end_valid = 0;
17493 w->desired_matrix->no_scrolling_p = 1;
17495 #ifdef GLYPH_DEBUG
17496 debug_method_add (w, "try_window_reusing_current_matrix 2");
17497 #endif
17498 return 1;
17501 return 0;
17506 /************************************************************************
17507 Window redisplay reusing current matrix when buffer has changed
17508 ************************************************************************/
17510 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17511 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17512 ptrdiff_t *, ptrdiff_t *);
17513 static struct glyph_row *
17514 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17515 struct glyph_row *);
17518 /* Return the last row in MATRIX displaying text. If row START is
17519 non-null, start searching with that row. IT gives the dimensions
17520 of the display. Value is null if matrix is empty; otherwise it is
17521 a pointer to the row found. */
17523 static struct glyph_row *
17524 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17525 struct glyph_row *start)
17527 struct glyph_row *row, *row_found;
17529 /* Set row_found to the last row in IT->w's current matrix
17530 displaying text. The loop looks funny but think of partially
17531 visible lines. */
17532 row_found = NULL;
17533 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17534 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17536 eassert (row->enabled_p);
17537 row_found = row;
17538 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17539 break;
17540 ++row;
17543 return row_found;
17547 /* Return the last row in the current matrix of W that is not affected
17548 by changes at the start of current_buffer that occurred since W's
17549 current matrix was built. Value is null if no such row exists.
17551 BEG_UNCHANGED us the number of characters unchanged at the start of
17552 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17553 first changed character in current_buffer. Characters at positions <
17554 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17555 when the current matrix was built. */
17557 static struct glyph_row *
17558 find_last_unchanged_at_beg_row (struct window *w)
17560 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17561 struct glyph_row *row;
17562 struct glyph_row *row_found = NULL;
17563 int yb = window_text_bottom_y (w);
17565 /* Find the last row displaying unchanged text. */
17566 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17567 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17568 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17569 ++row)
17571 if (/* If row ends before first_changed_pos, it is unchanged,
17572 except in some case. */
17573 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17574 /* When row ends in ZV and we write at ZV it is not
17575 unchanged. */
17576 && !row->ends_at_zv_p
17577 /* When first_changed_pos is the end of a continued line,
17578 row is not unchanged because it may be no longer
17579 continued. */
17580 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17581 && (row->continued_p
17582 || row->exact_window_width_line_p))
17583 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17584 needs to be recomputed, so don't consider this row as
17585 unchanged. This happens when the last line was
17586 bidi-reordered and was killed immediately before this
17587 redisplay cycle. In that case, ROW->end stores the
17588 buffer position of the first visual-order character of
17589 the killed text, which is now beyond ZV. */
17590 && CHARPOS (row->end.pos) <= ZV)
17591 row_found = row;
17593 /* Stop if last visible row. */
17594 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17595 break;
17598 return row_found;
17602 /* Find the first glyph row in the current matrix of W that is not
17603 affected by changes at the end of current_buffer since the
17604 time W's current matrix was built.
17606 Return in *DELTA the number of chars by which buffer positions in
17607 unchanged text at the end of current_buffer must be adjusted.
17609 Return in *DELTA_BYTES the corresponding number of bytes.
17611 Value is null if no such row exists, i.e. all rows are affected by
17612 changes. */
17614 static struct glyph_row *
17615 find_first_unchanged_at_end_row (struct window *w,
17616 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17618 struct glyph_row *row;
17619 struct glyph_row *row_found = NULL;
17621 *delta = *delta_bytes = 0;
17623 /* Display must not have been paused, otherwise the current matrix
17624 is not up to date. */
17625 eassert (w->window_end_valid);
17627 /* A value of window_end_pos >= END_UNCHANGED means that the window
17628 end is in the range of changed text. If so, there is no
17629 unchanged row at the end of W's current matrix. */
17630 if (w->window_end_pos >= END_UNCHANGED)
17631 return NULL;
17633 /* Set row to the last row in W's current matrix displaying text. */
17634 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17636 /* If matrix is entirely empty, no unchanged row exists. */
17637 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17639 /* The value of row is the last glyph row in the matrix having a
17640 meaningful buffer position in it. The end position of row
17641 corresponds to window_end_pos. This allows us to translate
17642 buffer positions in the current matrix to current buffer
17643 positions for characters not in changed text. */
17644 ptrdiff_t Z_old =
17645 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17646 ptrdiff_t Z_BYTE_old =
17647 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17648 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17649 struct glyph_row *first_text_row
17650 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17652 *delta = Z - Z_old;
17653 *delta_bytes = Z_BYTE - Z_BYTE_old;
17655 /* Set last_unchanged_pos to the buffer position of the last
17656 character in the buffer that has not been changed. Z is the
17657 index + 1 of the last character in current_buffer, i.e. by
17658 subtracting END_UNCHANGED we get the index of the last
17659 unchanged character, and we have to add BEG to get its buffer
17660 position. */
17661 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17662 last_unchanged_pos_old = last_unchanged_pos - *delta;
17664 /* Search backward from ROW for a row displaying a line that
17665 starts at a minimum position >= last_unchanged_pos_old. */
17666 for (; row > first_text_row; --row)
17668 /* This used to abort, but it can happen.
17669 It is ok to just stop the search instead here. KFS. */
17670 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17671 break;
17673 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17674 row_found = row;
17678 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17680 return row_found;
17684 /* Make sure that glyph rows in the current matrix of window W
17685 reference the same glyph memory as corresponding rows in the
17686 frame's frame matrix. This function is called after scrolling W's
17687 current matrix on a terminal frame in try_window_id and
17688 try_window_reusing_current_matrix. */
17690 static void
17691 sync_frame_with_window_matrix_rows (struct window *w)
17693 struct frame *f = XFRAME (w->frame);
17694 struct glyph_row *window_row, *window_row_end, *frame_row;
17696 /* Preconditions: W must be a leaf window and full-width. Its frame
17697 must have a frame matrix. */
17698 eassert (BUFFERP (w->contents));
17699 eassert (WINDOW_FULL_WIDTH_P (w));
17700 eassert (!FRAME_WINDOW_P (f));
17702 /* If W is a full-width window, glyph pointers in W's current matrix
17703 have, by definition, to be the same as glyph pointers in the
17704 corresponding frame matrix. Note that frame matrices have no
17705 marginal areas (see build_frame_matrix). */
17706 window_row = w->current_matrix->rows;
17707 window_row_end = window_row + w->current_matrix->nrows;
17708 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17709 while (window_row < window_row_end)
17711 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17712 struct glyph *end = window_row->glyphs[LAST_AREA];
17714 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17715 frame_row->glyphs[TEXT_AREA] = start;
17716 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17717 frame_row->glyphs[LAST_AREA] = end;
17719 /* Disable frame rows whose corresponding window rows have
17720 been disabled in try_window_id. */
17721 if (!window_row->enabled_p)
17722 frame_row->enabled_p = false;
17724 ++window_row, ++frame_row;
17729 /* Find the glyph row in window W containing CHARPOS. Consider all
17730 rows between START and END (not inclusive). END null means search
17731 all rows to the end of the display area of W. Value is the row
17732 containing CHARPOS or null. */
17734 struct glyph_row *
17735 row_containing_pos (struct window *w, ptrdiff_t charpos,
17736 struct glyph_row *start, struct glyph_row *end, int dy)
17738 struct glyph_row *row = start;
17739 struct glyph_row *best_row = NULL;
17740 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17741 int last_y;
17743 /* If we happen to start on a header-line, skip that. */
17744 if (row->mode_line_p)
17745 ++row;
17747 if ((end && row >= end) || !row->enabled_p)
17748 return NULL;
17750 last_y = window_text_bottom_y (w) - dy;
17752 while (1)
17754 /* Give up if we have gone too far. */
17755 if (end && row >= end)
17756 return NULL;
17757 /* This formerly returned if they were equal.
17758 I think that both quantities are of a "last plus one" type;
17759 if so, when they are equal, the row is within the screen. -- rms. */
17760 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17761 return NULL;
17763 /* If it is in this row, return this row. */
17764 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17765 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17766 /* The end position of a row equals the start
17767 position of the next row. If CHARPOS is there, we
17768 would rather consider it displayed in the next
17769 line, except when this line ends in ZV. */
17770 && !row_for_charpos_p (row, charpos)))
17771 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17773 struct glyph *g;
17775 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17776 || (!best_row && !row->continued_p))
17777 return row;
17778 /* In bidi-reordered rows, there could be several rows whose
17779 edges surround CHARPOS, all of these rows belonging to
17780 the same continued line. We need to find the row which
17781 fits CHARPOS the best. */
17782 for (g = row->glyphs[TEXT_AREA];
17783 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17784 g++)
17786 if (!STRINGP (g->object))
17788 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17790 mindif = eabs (g->charpos - charpos);
17791 best_row = row;
17792 /* Exact match always wins. */
17793 if (mindif == 0)
17794 return best_row;
17799 else if (best_row && !row->continued_p)
17800 return best_row;
17801 ++row;
17806 /* Try to redisplay window W by reusing its existing display. W's
17807 current matrix must be up to date when this function is called,
17808 i.e. window_end_valid must be nonzero.
17810 Value is
17812 >= 1 if successful, i.e. display has been updated
17813 specifically:
17814 1 means the changes were in front of a newline that precedes
17815 the window start, and the whole current matrix was reused
17816 2 means the changes were after the last position displayed
17817 in the window, and the whole current matrix was reused
17818 3 means portions of the current matrix were reused, while
17819 some of the screen lines were redrawn
17820 -1 if redisplay with same window start is known not to succeed
17821 0 if otherwise unsuccessful
17823 The following steps are performed:
17825 1. Find the last row in the current matrix of W that is not
17826 affected by changes at the start of current_buffer. If no such row
17827 is found, give up.
17829 2. Find the first row in W's current matrix that is not affected by
17830 changes at the end of current_buffer. Maybe there is no such row.
17832 3. Display lines beginning with the row + 1 found in step 1 to the
17833 row found in step 2 or, if step 2 didn't find a row, to the end of
17834 the window.
17836 4. If cursor is not known to appear on the window, give up.
17838 5. If display stopped at the row found in step 2, scroll the
17839 display and current matrix as needed.
17841 6. Maybe display some lines at the end of W, if we must. This can
17842 happen under various circumstances, like a partially visible line
17843 becoming fully visible, or because newly displayed lines are displayed
17844 in smaller font sizes.
17846 7. Update W's window end information. */
17848 static int
17849 try_window_id (struct window *w)
17851 struct frame *f = XFRAME (w->frame);
17852 struct glyph_matrix *current_matrix = w->current_matrix;
17853 struct glyph_matrix *desired_matrix = w->desired_matrix;
17854 struct glyph_row *last_unchanged_at_beg_row;
17855 struct glyph_row *first_unchanged_at_end_row;
17856 struct glyph_row *row;
17857 struct glyph_row *bottom_row;
17858 int bottom_vpos;
17859 struct it it;
17860 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17861 int dvpos, dy;
17862 struct text_pos start_pos;
17863 struct run run;
17864 int first_unchanged_at_end_vpos = 0;
17865 struct glyph_row *last_text_row, *last_text_row_at_end;
17866 struct text_pos start;
17867 ptrdiff_t first_changed_charpos, last_changed_charpos;
17869 #ifdef GLYPH_DEBUG
17870 if (inhibit_try_window_id)
17871 return 0;
17872 #endif
17874 /* This is handy for debugging. */
17875 #if 0
17876 #define GIVE_UP(X) \
17877 do { \
17878 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17879 return 0; \
17880 } while (0)
17881 #else
17882 #define GIVE_UP(X) return 0
17883 #endif
17885 SET_TEXT_POS_FROM_MARKER (start, w->start);
17887 /* Don't use this for mini-windows because these can show
17888 messages and mini-buffers, and we don't handle that here. */
17889 if (MINI_WINDOW_P (w))
17890 GIVE_UP (1);
17892 /* This flag is used to prevent redisplay optimizations. */
17893 if (windows_or_buffers_changed || f->cursor_type_changed)
17894 GIVE_UP (2);
17896 /* This function's optimizations cannot be used if overlays have
17897 changed in the buffer displayed by the window, so give up if they
17898 have. */
17899 if (w->last_overlay_modified != OVERLAY_MODIFF)
17900 GIVE_UP (21);
17902 /* Verify that narrowing has not changed.
17903 Also verify that we were not told to prevent redisplay optimizations.
17904 It would be nice to further
17905 reduce the number of cases where this prevents try_window_id. */
17906 if (current_buffer->clip_changed
17907 || current_buffer->prevent_redisplay_optimizations_p)
17908 GIVE_UP (3);
17910 /* Window must either use window-based redisplay or be full width. */
17911 if (!FRAME_WINDOW_P (f)
17912 && (!FRAME_LINE_INS_DEL_OK (f)
17913 || !WINDOW_FULL_WIDTH_P (w)))
17914 GIVE_UP (4);
17916 /* Give up if point is known NOT to appear in W. */
17917 if (PT < CHARPOS (start))
17918 GIVE_UP (5);
17920 /* Another way to prevent redisplay optimizations. */
17921 if (w->last_modified == 0)
17922 GIVE_UP (6);
17924 /* Verify that window is not hscrolled. */
17925 if (w->hscroll != 0)
17926 GIVE_UP (7);
17928 /* Verify that display wasn't paused. */
17929 if (!w->window_end_valid)
17930 GIVE_UP (8);
17932 /* Likewise if highlighting trailing whitespace. */
17933 if (!NILP (Vshow_trailing_whitespace))
17934 GIVE_UP (11);
17936 /* Can't use this if overlay arrow position and/or string have
17937 changed. */
17938 if (overlay_arrows_changed_p ())
17939 GIVE_UP (12);
17941 /* When word-wrap is on, adding a space to the first word of a
17942 wrapped line can change the wrap position, altering the line
17943 above it. It might be worthwhile to handle this more
17944 intelligently, but for now just redisplay from scratch. */
17945 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17946 GIVE_UP (21);
17948 /* Under bidi reordering, adding or deleting a character in the
17949 beginning of a paragraph, before the first strong directional
17950 character, can change the base direction of the paragraph (unless
17951 the buffer specifies a fixed paragraph direction), which will
17952 require to redisplay the whole paragraph. It might be worthwhile
17953 to find the paragraph limits and widen the range of redisplayed
17954 lines to that, but for now just give up this optimization and
17955 redisplay from scratch. */
17956 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17957 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17958 GIVE_UP (22);
17960 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17961 only if buffer has really changed. The reason is that the gap is
17962 initially at Z for freshly visited files. The code below would
17963 set end_unchanged to 0 in that case. */
17964 if (MODIFF > SAVE_MODIFF
17965 /* This seems to happen sometimes after saving a buffer. */
17966 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17968 if (GPT - BEG < BEG_UNCHANGED)
17969 BEG_UNCHANGED = GPT - BEG;
17970 if (Z - GPT < END_UNCHANGED)
17971 END_UNCHANGED = Z - GPT;
17974 /* The position of the first and last character that has been changed. */
17975 first_changed_charpos = BEG + BEG_UNCHANGED;
17976 last_changed_charpos = Z - END_UNCHANGED;
17978 /* If window starts after a line end, and the last change is in
17979 front of that newline, then changes don't affect the display.
17980 This case happens with stealth-fontification. Note that although
17981 the display is unchanged, glyph positions in the matrix have to
17982 be adjusted, of course. */
17983 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17984 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17985 && ((last_changed_charpos < CHARPOS (start)
17986 && CHARPOS (start) == BEGV)
17987 || (last_changed_charpos < CHARPOS (start) - 1
17988 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17990 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17991 struct glyph_row *r0;
17993 /* Compute how many chars/bytes have been added to or removed
17994 from the buffer. */
17995 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17996 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17997 Z_delta = Z - Z_old;
17998 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18000 /* Give up if PT is not in the window. Note that it already has
18001 been checked at the start of try_window_id that PT is not in
18002 front of the window start. */
18003 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18004 GIVE_UP (13);
18006 /* If window start is unchanged, we can reuse the whole matrix
18007 as is, after adjusting glyph positions. No need to compute
18008 the window end again, since its offset from Z hasn't changed. */
18009 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18010 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18011 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18012 /* PT must not be in a partially visible line. */
18013 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18014 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18016 /* Adjust positions in the glyph matrix. */
18017 if (Z_delta || Z_delta_bytes)
18019 struct glyph_row *r1
18020 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18021 increment_matrix_positions (w->current_matrix,
18022 MATRIX_ROW_VPOS (r0, current_matrix),
18023 MATRIX_ROW_VPOS (r1, current_matrix),
18024 Z_delta, Z_delta_bytes);
18027 /* Set the cursor. */
18028 row = row_containing_pos (w, PT, r0, NULL, 0);
18029 if (row)
18030 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18031 return 1;
18035 /* Handle the case that changes are all below what is displayed in
18036 the window, and that PT is in the window. This shortcut cannot
18037 be taken if ZV is visible in the window, and text has been added
18038 there that is visible in the window. */
18039 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18040 /* ZV is not visible in the window, or there are no
18041 changes at ZV, actually. */
18042 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18043 || first_changed_charpos == last_changed_charpos))
18045 struct glyph_row *r0;
18047 /* Give up if PT is not in the window. Note that it already has
18048 been checked at the start of try_window_id that PT is not in
18049 front of the window start. */
18050 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18051 GIVE_UP (14);
18053 /* If window start is unchanged, we can reuse the whole matrix
18054 as is, without changing glyph positions since no text has
18055 been added/removed in front of the window end. */
18056 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18057 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18058 /* PT must not be in a partially visible line. */
18059 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18060 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18062 /* We have to compute the window end anew since text
18063 could have been added/removed after it. */
18064 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18065 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18067 /* Set the cursor. */
18068 row = row_containing_pos (w, PT, r0, NULL, 0);
18069 if (row)
18070 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18071 return 2;
18075 /* Give up if window start is in the changed area.
18077 The condition used to read
18079 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18081 but why that was tested escapes me at the moment. */
18082 if (CHARPOS (start) >= first_changed_charpos
18083 && CHARPOS (start) <= last_changed_charpos)
18084 GIVE_UP (15);
18086 /* Check that window start agrees with the start of the first glyph
18087 row in its current matrix. Check this after we know the window
18088 start is not in changed text, otherwise positions would not be
18089 comparable. */
18090 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18091 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18092 GIVE_UP (16);
18094 /* Give up if the window ends in strings. Overlay strings
18095 at the end are difficult to handle, so don't try. */
18096 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18097 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18098 GIVE_UP (20);
18100 /* Compute the position at which we have to start displaying new
18101 lines. Some of the lines at the top of the window might be
18102 reusable because they are not displaying changed text. Find the
18103 last row in W's current matrix not affected by changes at the
18104 start of current_buffer. Value is null if changes start in the
18105 first line of window. */
18106 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18107 if (last_unchanged_at_beg_row)
18109 /* Avoid starting to display in the middle of a character, a TAB
18110 for instance. This is easier than to set up the iterator
18111 exactly, and it's not a frequent case, so the additional
18112 effort wouldn't really pay off. */
18113 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18114 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18115 && last_unchanged_at_beg_row > w->current_matrix->rows)
18116 --last_unchanged_at_beg_row;
18118 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18119 GIVE_UP (17);
18121 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18122 GIVE_UP (18);
18123 start_pos = it.current.pos;
18125 /* Start displaying new lines in the desired matrix at the same
18126 vpos we would use in the current matrix, i.e. below
18127 last_unchanged_at_beg_row. */
18128 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18129 current_matrix);
18130 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18131 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18133 eassert (it.hpos == 0 && it.current_x == 0);
18135 else
18137 /* There are no reusable lines at the start of the window.
18138 Start displaying in the first text line. */
18139 start_display (&it, w, start);
18140 it.vpos = it.first_vpos;
18141 start_pos = it.current.pos;
18144 /* Find the first row that is not affected by changes at the end of
18145 the buffer. Value will be null if there is no unchanged row, in
18146 which case we must redisplay to the end of the window. delta
18147 will be set to the value by which buffer positions beginning with
18148 first_unchanged_at_end_row have to be adjusted due to text
18149 changes. */
18150 first_unchanged_at_end_row
18151 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18152 IF_DEBUG (debug_delta = delta);
18153 IF_DEBUG (debug_delta_bytes = delta_bytes);
18155 /* Set stop_pos to the buffer position up to which we will have to
18156 display new lines. If first_unchanged_at_end_row != NULL, this
18157 is the buffer position of the start of the line displayed in that
18158 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18159 that we don't stop at a buffer position. */
18160 stop_pos = 0;
18161 if (first_unchanged_at_end_row)
18163 eassert (last_unchanged_at_beg_row == NULL
18164 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18166 /* If this is a continuation line, move forward to the next one
18167 that isn't. Changes in lines above affect this line.
18168 Caution: this may move first_unchanged_at_end_row to a row
18169 not displaying text. */
18170 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18171 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18172 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18173 < it.last_visible_y))
18174 ++first_unchanged_at_end_row;
18176 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18177 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18178 >= it.last_visible_y))
18179 first_unchanged_at_end_row = NULL;
18180 else
18182 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18183 + delta);
18184 first_unchanged_at_end_vpos
18185 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18186 eassert (stop_pos >= Z - END_UNCHANGED);
18189 else if (last_unchanged_at_beg_row == NULL)
18190 GIVE_UP (19);
18193 #ifdef GLYPH_DEBUG
18195 /* Either there is no unchanged row at the end, or the one we have
18196 now displays text. This is a necessary condition for the window
18197 end pos calculation at the end of this function. */
18198 eassert (first_unchanged_at_end_row == NULL
18199 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18201 debug_last_unchanged_at_beg_vpos
18202 = (last_unchanged_at_beg_row
18203 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18204 : -1);
18205 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18207 #endif /* GLYPH_DEBUG */
18210 /* Display new lines. Set last_text_row to the last new line
18211 displayed which has text on it, i.e. might end up as being the
18212 line where the window_end_vpos is. */
18213 w->cursor.vpos = -1;
18214 last_text_row = NULL;
18215 overlay_arrow_seen = 0;
18216 if (it.current_y < it.last_visible_y
18217 && !f->fonts_changed
18218 && (first_unchanged_at_end_row == NULL
18219 || IT_CHARPOS (it) < stop_pos))
18220 it.glyph_row->reversed_p = false;
18221 while (it.current_y < it.last_visible_y
18222 && !f->fonts_changed
18223 && (first_unchanged_at_end_row == NULL
18224 || IT_CHARPOS (it) < stop_pos))
18226 if (display_line (&it))
18227 last_text_row = it.glyph_row - 1;
18230 if (f->fonts_changed)
18231 return -1;
18234 /* Compute differences in buffer positions, y-positions etc. for
18235 lines reused at the bottom of the window. Compute what we can
18236 scroll. */
18237 if (first_unchanged_at_end_row
18238 /* No lines reused because we displayed everything up to the
18239 bottom of the window. */
18240 && it.current_y < it.last_visible_y)
18242 dvpos = (it.vpos
18243 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18244 current_matrix));
18245 dy = it.current_y - first_unchanged_at_end_row->y;
18246 run.current_y = first_unchanged_at_end_row->y;
18247 run.desired_y = run.current_y + dy;
18248 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18250 else
18252 delta = delta_bytes = dvpos = dy
18253 = run.current_y = run.desired_y = run.height = 0;
18254 first_unchanged_at_end_row = NULL;
18256 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18259 /* Find the cursor if not already found. We have to decide whether
18260 PT will appear on this window (it sometimes doesn't, but this is
18261 not a very frequent case.) This decision has to be made before
18262 the current matrix is altered. A value of cursor.vpos < 0 means
18263 that PT is either in one of the lines beginning at
18264 first_unchanged_at_end_row or below the window. Don't care for
18265 lines that might be displayed later at the window end; as
18266 mentioned, this is not a frequent case. */
18267 if (w->cursor.vpos < 0)
18269 /* Cursor in unchanged rows at the top? */
18270 if (PT < CHARPOS (start_pos)
18271 && last_unchanged_at_beg_row)
18273 row = row_containing_pos (w, PT,
18274 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18275 last_unchanged_at_beg_row + 1, 0);
18276 if (row)
18277 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18280 /* Start from first_unchanged_at_end_row looking for PT. */
18281 else if (first_unchanged_at_end_row)
18283 row = row_containing_pos (w, PT - delta,
18284 first_unchanged_at_end_row, NULL, 0);
18285 if (row)
18286 set_cursor_from_row (w, row, w->current_matrix, delta,
18287 delta_bytes, dy, dvpos);
18290 /* Give up if cursor was not found. */
18291 if (w->cursor.vpos < 0)
18293 clear_glyph_matrix (w->desired_matrix);
18294 return -1;
18298 /* Don't let the cursor end in the scroll margins. */
18300 int this_scroll_margin, cursor_height;
18301 int frame_line_height = default_line_pixel_height (w);
18302 int window_total_lines
18303 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18305 this_scroll_margin =
18306 max (0, min (scroll_margin, window_total_lines / 4));
18307 this_scroll_margin *= frame_line_height;
18308 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18310 if ((w->cursor.y < this_scroll_margin
18311 && CHARPOS (start) > BEGV)
18312 /* Old redisplay didn't take scroll margin into account at the bottom,
18313 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18314 || (w->cursor.y + (make_cursor_line_fully_visible_p
18315 ? cursor_height + this_scroll_margin
18316 : 1)) > it.last_visible_y)
18318 w->cursor.vpos = -1;
18319 clear_glyph_matrix (w->desired_matrix);
18320 return -1;
18324 /* Scroll the display. Do it before changing the current matrix so
18325 that xterm.c doesn't get confused about where the cursor glyph is
18326 found. */
18327 if (dy && run.height)
18329 update_begin (f);
18331 if (FRAME_WINDOW_P (f))
18333 FRAME_RIF (f)->update_window_begin_hook (w);
18334 FRAME_RIF (f)->clear_window_mouse_face (w);
18335 FRAME_RIF (f)->scroll_run_hook (w, &run);
18336 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18338 else
18340 /* Terminal frame. In this case, dvpos gives the number of
18341 lines to scroll by; dvpos < 0 means scroll up. */
18342 int from_vpos
18343 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18344 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18345 int end = (WINDOW_TOP_EDGE_LINE (w)
18346 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18347 + window_internal_height (w));
18349 #if defined (HAVE_GPM) || defined (MSDOS)
18350 x_clear_window_mouse_face (w);
18351 #endif
18352 /* Perform the operation on the screen. */
18353 if (dvpos > 0)
18355 /* Scroll last_unchanged_at_beg_row to the end of the
18356 window down dvpos lines. */
18357 set_terminal_window (f, end);
18359 /* On dumb terminals delete dvpos lines at the end
18360 before inserting dvpos empty lines. */
18361 if (!FRAME_SCROLL_REGION_OK (f))
18362 ins_del_lines (f, end - dvpos, -dvpos);
18364 /* Insert dvpos empty lines in front of
18365 last_unchanged_at_beg_row. */
18366 ins_del_lines (f, from, dvpos);
18368 else if (dvpos < 0)
18370 /* Scroll up last_unchanged_at_beg_vpos to the end of
18371 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18372 set_terminal_window (f, end);
18374 /* Delete dvpos lines in front of
18375 last_unchanged_at_beg_vpos. ins_del_lines will set
18376 the cursor to the given vpos and emit |dvpos| delete
18377 line sequences. */
18378 ins_del_lines (f, from + dvpos, dvpos);
18380 /* On a dumb terminal insert dvpos empty lines at the
18381 end. */
18382 if (!FRAME_SCROLL_REGION_OK (f))
18383 ins_del_lines (f, end + dvpos, -dvpos);
18386 set_terminal_window (f, 0);
18389 update_end (f);
18392 /* Shift reused rows of the current matrix to the right position.
18393 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18394 text. */
18395 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18396 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18397 if (dvpos < 0)
18399 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18400 bottom_vpos, dvpos);
18401 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18402 bottom_vpos);
18404 else if (dvpos > 0)
18406 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18407 bottom_vpos, dvpos);
18408 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18409 first_unchanged_at_end_vpos + dvpos);
18412 /* For frame-based redisplay, make sure that current frame and window
18413 matrix are in sync with respect to glyph memory. */
18414 if (!FRAME_WINDOW_P (f))
18415 sync_frame_with_window_matrix_rows (w);
18417 /* Adjust buffer positions in reused rows. */
18418 if (delta || delta_bytes)
18419 increment_matrix_positions (current_matrix,
18420 first_unchanged_at_end_vpos + dvpos,
18421 bottom_vpos, delta, delta_bytes);
18423 /* Adjust Y positions. */
18424 if (dy)
18425 shift_glyph_matrix (w, current_matrix,
18426 first_unchanged_at_end_vpos + dvpos,
18427 bottom_vpos, dy);
18429 if (first_unchanged_at_end_row)
18431 first_unchanged_at_end_row += dvpos;
18432 if (first_unchanged_at_end_row->y >= it.last_visible_y
18433 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18434 first_unchanged_at_end_row = NULL;
18437 /* If scrolling up, there may be some lines to display at the end of
18438 the window. */
18439 last_text_row_at_end = NULL;
18440 if (dy < 0)
18442 /* Scrolling up can leave for example a partially visible line
18443 at the end of the window to be redisplayed. */
18444 /* Set last_row to the glyph row in the current matrix where the
18445 window end line is found. It has been moved up or down in
18446 the matrix by dvpos. */
18447 int last_vpos = w->window_end_vpos + dvpos;
18448 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18450 /* If last_row is the window end line, it should display text. */
18451 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18453 /* If window end line was partially visible before, begin
18454 displaying at that line. Otherwise begin displaying with the
18455 line following it. */
18456 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18458 init_to_row_start (&it, w, last_row);
18459 it.vpos = last_vpos;
18460 it.current_y = last_row->y;
18462 else
18464 init_to_row_end (&it, w, last_row);
18465 it.vpos = 1 + last_vpos;
18466 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18467 ++last_row;
18470 /* We may start in a continuation line. If so, we have to
18471 get the right continuation_lines_width and current_x. */
18472 it.continuation_lines_width = last_row->continuation_lines_width;
18473 it.hpos = it.current_x = 0;
18475 /* Display the rest of the lines at the window end. */
18476 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18477 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18479 /* Is it always sure that the display agrees with lines in
18480 the current matrix? I don't think so, so we mark rows
18481 displayed invalid in the current matrix by setting their
18482 enabled_p flag to zero. */
18483 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18484 if (display_line (&it))
18485 last_text_row_at_end = it.glyph_row - 1;
18489 /* Update window_end_pos and window_end_vpos. */
18490 if (first_unchanged_at_end_row && !last_text_row_at_end)
18492 /* Window end line if one of the preserved rows from the current
18493 matrix. Set row to the last row displaying text in current
18494 matrix starting at first_unchanged_at_end_row, after
18495 scrolling. */
18496 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18497 row = find_last_row_displaying_text (w->current_matrix, &it,
18498 first_unchanged_at_end_row);
18499 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18500 adjust_window_ends (w, row, 1);
18501 eassert (w->window_end_bytepos >= 0);
18502 IF_DEBUG (debug_method_add (w, "A"));
18504 else if (last_text_row_at_end)
18506 adjust_window_ends (w, last_text_row_at_end, 0);
18507 eassert (w->window_end_bytepos >= 0);
18508 IF_DEBUG (debug_method_add (w, "B"));
18510 else if (last_text_row)
18512 /* We have displayed either to the end of the window or at the
18513 end of the window, i.e. the last row with text is to be found
18514 in the desired matrix. */
18515 adjust_window_ends (w, last_text_row, 0);
18516 eassert (w->window_end_bytepos >= 0);
18518 else if (first_unchanged_at_end_row == NULL
18519 && last_text_row == NULL
18520 && last_text_row_at_end == NULL)
18522 /* Displayed to end of window, but no line containing text was
18523 displayed. Lines were deleted at the end of the window. */
18524 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18525 int vpos = w->window_end_vpos;
18526 struct glyph_row *current_row = current_matrix->rows + vpos;
18527 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18529 for (row = NULL;
18530 row == NULL && vpos >= first_vpos;
18531 --vpos, --current_row, --desired_row)
18533 if (desired_row->enabled_p)
18535 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18536 row = desired_row;
18538 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18539 row = current_row;
18542 eassert (row != NULL);
18543 w->window_end_vpos = vpos + 1;
18544 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18545 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18546 eassert (w->window_end_bytepos >= 0);
18547 IF_DEBUG (debug_method_add (w, "C"));
18549 else
18550 emacs_abort ();
18552 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18553 debug_end_vpos = w->window_end_vpos));
18555 /* Record that display has not been completed. */
18556 w->window_end_valid = 0;
18557 w->desired_matrix->no_scrolling_p = 1;
18558 return 3;
18560 #undef GIVE_UP
18565 /***********************************************************************
18566 More debugging support
18567 ***********************************************************************/
18569 #ifdef GLYPH_DEBUG
18571 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18572 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18573 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18576 /* Dump the contents of glyph matrix MATRIX on stderr.
18578 GLYPHS 0 means don't show glyph contents.
18579 GLYPHS 1 means show glyphs in short form
18580 GLYPHS > 1 means show glyphs in long form. */
18582 void
18583 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18585 int i;
18586 for (i = 0; i < matrix->nrows; ++i)
18587 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18591 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18592 the glyph row and area where the glyph comes from. */
18594 void
18595 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18597 if (glyph->type == CHAR_GLYPH
18598 || glyph->type == GLYPHLESS_GLYPH)
18600 fprintf (stderr,
18601 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18602 glyph - row->glyphs[TEXT_AREA],
18603 (glyph->type == CHAR_GLYPH
18604 ? 'C'
18605 : 'G'),
18606 glyph->charpos,
18607 (BUFFERP (glyph->object)
18608 ? 'B'
18609 : (STRINGP (glyph->object)
18610 ? 'S'
18611 : (INTEGERP (glyph->object)
18612 ? '0'
18613 : '-'))),
18614 glyph->pixel_width,
18615 glyph->u.ch,
18616 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18617 ? glyph->u.ch
18618 : '.'),
18619 glyph->face_id,
18620 glyph->left_box_line_p,
18621 glyph->right_box_line_p);
18623 else if (glyph->type == STRETCH_GLYPH)
18625 fprintf (stderr,
18626 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18627 glyph - row->glyphs[TEXT_AREA],
18628 'S',
18629 glyph->charpos,
18630 (BUFFERP (glyph->object)
18631 ? 'B'
18632 : (STRINGP (glyph->object)
18633 ? 'S'
18634 : (INTEGERP (glyph->object)
18635 ? '0'
18636 : '-'))),
18637 glyph->pixel_width,
18639 ' ',
18640 glyph->face_id,
18641 glyph->left_box_line_p,
18642 glyph->right_box_line_p);
18644 else if (glyph->type == IMAGE_GLYPH)
18646 fprintf (stderr,
18647 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18648 glyph - row->glyphs[TEXT_AREA],
18649 'I',
18650 glyph->charpos,
18651 (BUFFERP (glyph->object)
18652 ? 'B'
18653 : (STRINGP (glyph->object)
18654 ? 'S'
18655 : (INTEGERP (glyph->object)
18656 ? '0'
18657 : '-'))),
18658 glyph->pixel_width,
18659 glyph->u.img_id,
18660 '.',
18661 glyph->face_id,
18662 glyph->left_box_line_p,
18663 glyph->right_box_line_p);
18665 else if (glyph->type == COMPOSITE_GLYPH)
18667 fprintf (stderr,
18668 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18669 glyph - row->glyphs[TEXT_AREA],
18670 '+',
18671 glyph->charpos,
18672 (BUFFERP (glyph->object)
18673 ? 'B'
18674 : (STRINGP (glyph->object)
18675 ? 'S'
18676 : (INTEGERP (glyph->object)
18677 ? '0'
18678 : '-'))),
18679 glyph->pixel_width,
18680 glyph->u.cmp.id);
18681 if (glyph->u.cmp.automatic)
18682 fprintf (stderr,
18683 "[%d-%d]",
18684 glyph->slice.cmp.from, glyph->slice.cmp.to);
18685 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18686 glyph->face_id,
18687 glyph->left_box_line_p,
18688 glyph->right_box_line_p);
18693 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18694 GLYPHS 0 means don't show glyph contents.
18695 GLYPHS 1 means show glyphs in short form
18696 GLYPHS > 1 means show glyphs in long form. */
18698 void
18699 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18701 if (glyphs != 1)
18703 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18704 fprintf (stderr, "==============================================================================\n");
18706 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18707 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18708 vpos,
18709 MATRIX_ROW_START_CHARPOS (row),
18710 MATRIX_ROW_END_CHARPOS (row),
18711 row->used[TEXT_AREA],
18712 row->contains_overlapping_glyphs_p,
18713 row->enabled_p,
18714 row->truncated_on_left_p,
18715 row->truncated_on_right_p,
18716 row->continued_p,
18717 MATRIX_ROW_CONTINUATION_LINE_P (row),
18718 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18719 row->ends_at_zv_p,
18720 row->fill_line_p,
18721 row->ends_in_middle_of_char_p,
18722 row->starts_in_middle_of_char_p,
18723 row->mouse_face_p,
18724 row->x,
18725 row->y,
18726 row->pixel_width,
18727 row->height,
18728 row->visible_height,
18729 row->ascent,
18730 row->phys_ascent);
18731 /* The next 3 lines should align to "Start" in the header. */
18732 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18733 row->end.overlay_string_index,
18734 row->continuation_lines_width);
18735 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18736 CHARPOS (row->start.string_pos),
18737 CHARPOS (row->end.string_pos));
18738 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18739 row->end.dpvec_index);
18742 if (glyphs > 1)
18744 int area;
18746 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18748 struct glyph *glyph = row->glyphs[area];
18749 struct glyph *glyph_end = glyph + row->used[area];
18751 /* Glyph for a line end in text. */
18752 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18753 ++glyph_end;
18755 if (glyph < glyph_end)
18756 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18758 for (; glyph < glyph_end; ++glyph)
18759 dump_glyph (row, glyph, area);
18762 else if (glyphs == 1)
18764 int area;
18765 char s[SHRT_MAX + 4];
18767 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18769 int i;
18771 for (i = 0; i < row->used[area]; ++i)
18773 struct glyph *glyph = row->glyphs[area] + i;
18774 if (i == row->used[area] - 1
18775 && area == TEXT_AREA
18776 && INTEGERP (glyph->object)
18777 && glyph->type == CHAR_GLYPH
18778 && glyph->u.ch == ' ')
18780 strcpy (&s[i], "[\\n]");
18781 i += 4;
18783 else if (glyph->type == CHAR_GLYPH
18784 && glyph->u.ch < 0x80
18785 && glyph->u.ch >= ' ')
18786 s[i] = glyph->u.ch;
18787 else
18788 s[i] = '.';
18791 s[i] = '\0';
18792 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18798 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18799 Sdump_glyph_matrix, 0, 1, "p",
18800 doc: /* Dump the current matrix of the selected window to stderr.
18801 Shows contents of glyph row structures. With non-nil
18802 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18803 glyphs in short form, otherwise show glyphs in long form. */)
18804 (Lisp_Object glyphs)
18806 struct window *w = XWINDOW (selected_window);
18807 struct buffer *buffer = XBUFFER (w->contents);
18809 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18810 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18811 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18812 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18813 fprintf (stderr, "=============================================\n");
18814 dump_glyph_matrix (w->current_matrix,
18815 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18816 return Qnil;
18820 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18821 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18822 (void)
18824 struct frame *f = XFRAME (selected_frame);
18825 dump_glyph_matrix (f->current_matrix, 1);
18826 return Qnil;
18830 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18831 doc: /* Dump glyph row ROW to stderr.
18832 GLYPH 0 means don't dump glyphs.
18833 GLYPH 1 means dump glyphs in short form.
18834 GLYPH > 1 or omitted means dump glyphs in long form. */)
18835 (Lisp_Object row, Lisp_Object glyphs)
18837 struct glyph_matrix *matrix;
18838 EMACS_INT vpos;
18840 CHECK_NUMBER (row);
18841 matrix = XWINDOW (selected_window)->current_matrix;
18842 vpos = XINT (row);
18843 if (vpos >= 0 && vpos < matrix->nrows)
18844 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18845 vpos,
18846 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18847 return Qnil;
18851 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18852 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18853 GLYPH 0 means don't dump glyphs.
18854 GLYPH 1 means dump glyphs in short form.
18855 GLYPH > 1 or omitted means dump glyphs in long form.
18857 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18858 do nothing. */)
18859 (Lisp_Object row, Lisp_Object glyphs)
18861 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18862 struct frame *sf = SELECTED_FRAME ();
18863 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18864 EMACS_INT vpos;
18866 CHECK_NUMBER (row);
18867 vpos = XINT (row);
18868 if (vpos >= 0 && vpos < m->nrows)
18869 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18870 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18871 #endif
18872 return Qnil;
18876 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18877 doc: /* Toggle tracing of redisplay.
18878 With ARG, turn tracing on if and only if ARG is positive. */)
18879 (Lisp_Object arg)
18881 if (NILP (arg))
18882 trace_redisplay_p = !trace_redisplay_p;
18883 else
18885 arg = Fprefix_numeric_value (arg);
18886 trace_redisplay_p = XINT (arg) > 0;
18889 return Qnil;
18893 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18894 doc: /* Like `format', but print result to stderr.
18895 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18896 (ptrdiff_t nargs, Lisp_Object *args)
18898 Lisp_Object s = Fformat (nargs, args);
18899 fprintf (stderr, "%s", SDATA (s));
18900 return Qnil;
18903 #endif /* GLYPH_DEBUG */
18907 /***********************************************************************
18908 Building Desired Matrix Rows
18909 ***********************************************************************/
18911 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18912 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18914 static struct glyph_row *
18915 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18917 struct frame *f = XFRAME (WINDOW_FRAME (w));
18918 struct buffer *buffer = XBUFFER (w->contents);
18919 struct buffer *old = current_buffer;
18920 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18921 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
18922 const unsigned char *arrow_end = arrow_string + arrow_len;
18923 const unsigned char *p;
18924 struct it it;
18925 bool multibyte_p;
18926 int n_glyphs_before;
18928 set_buffer_temp (buffer);
18929 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18930 scratch_glyph_row.reversed_p = false;
18931 it.glyph_row->used[TEXT_AREA] = 0;
18932 SET_TEXT_POS (it.position, 0, 0);
18934 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18935 p = arrow_string;
18936 while (p < arrow_end)
18938 Lisp_Object face, ilisp;
18940 /* Get the next character. */
18941 if (multibyte_p)
18942 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18943 else
18945 it.c = it.char_to_display = *p, it.len = 1;
18946 if (! ASCII_CHAR_P (it.c))
18947 it.char_to_display = BYTE8_TO_CHAR (it.c);
18949 p += it.len;
18951 /* Get its face. */
18952 ilisp = make_number (p - arrow_string);
18953 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18954 it.face_id = compute_char_face (f, it.char_to_display, face);
18956 /* Compute its width, get its glyphs. */
18957 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18958 SET_TEXT_POS (it.position, -1, -1);
18959 PRODUCE_GLYPHS (&it);
18961 /* If this character doesn't fit any more in the line, we have
18962 to remove some glyphs. */
18963 if (it.current_x > it.last_visible_x)
18965 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18966 break;
18970 set_buffer_temp (old);
18971 return it.glyph_row;
18975 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18976 glyphs to insert is determined by produce_special_glyphs. */
18978 static void
18979 insert_left_trunc_glyphs (struct it *it)
18981 struct it truncate_it;
18982 struct glyph *from, *end, *to, *toend;
18984 eassert (!FRAME_WINDOW_P (it->f)
18985 || (!it->glyph_row->reversed_p
18986 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18987 || (it->glyph_row->reversed_p
18988 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18990 /* Get the truncation glyphs. */
18991 truncate_it = *it;
18992 truncate_it.current_x = 0;
18993 truncate_it.face_id = DEFAULT_FACE_ID;
18994 truncate_it.glyph_row = &scratch_glyph_row;
18995 truncate_it.area = TEXT_AREA;
18996 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18997 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18998 truncate_it.object = make_number (0);
18999 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19001 /* Overwrite glyphs from IT with truncation glyphs. */
19002 if (!it->glyph_row->reversed_p)
19004 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19006 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19007 end = from + tused;
19008 to = it->glyph_row->glyphs[TEXT_AREA];
19009 toend = to + it->glyph_row->used[TEXT_AREA];
19010 if (FRAME_WINDOW_P (it->f))
19012 /* On GUI frames, when variable-size fonts are displayed,
19013 the truncation glyphs may need more pixels than the row's
19014 glyphs they overwrite. We overwrite more glyphs to free
19015 enough screen real estate, and enlarge the stretch glyph
19016 on the right (see display_line), if there is one, to
19017 preserve the screen position of the truncation glyphs on
19018 the right. */
19019 int w = 0;
19020 struct glyph *g = to;
19021 short used;
19023 /* The first glyph could be partially visible, in which case
19024 it->glyph_row->x will be negative. But we want the left
19025 truncation glyphs to be aligned at the left margin of the
19026 window, so we override the x coordinate at which the row
19027 will begin. */
19028 it->glyph_row->x = 0;
19029 while (g < toend && w < it->truncation_pixel_width)
19031 w += g->pixel_width;
19032 ++g;
19034 if (g - to - tused > 0)
19036 memmove (to + tused, g, (toend - g) * sizeof(*g));
19037 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19039 used = it->glyph_row->used[TEXT_AREA];
19040 if (it->glyph_row->truncated_on_right_p
19041 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19042 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19043 == STRETCH_GLYPH)
19045 int extra = w - it->truncation_pixel_width;
19047 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19051 while (from < end)
19052 *to++ = *from++;
19054 /* There may be padding glyphs left over. Overwrite them too. */
19055 if (!FRAME_WINDOW_P (it->f))
19057 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19059 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19060 while (from < end)
19061 *to++ = *from++;
19065 if (to > toend)
19066 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19068 else
19070 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19072 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19073 that back to front. */
19074 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19075 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19076 toend = it->glyph_row->glyphs[TEXT_AREA];
19077 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19078 if (FRAME_WINDOW_P (it->f))
19080 int w = 0;
19081 struct glyph *g = to;
19083 while (g >= toend && w < it->truncation_pixel_width)
19085 w += g->pixel_width;
19086 --g;
19088 if (to - g - tused > 0)
19089 to = g + tused;
19090 if (it->glyph_row->truncated_on_right_p
19091 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19092 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19094 int extra = w - it->truncation_pixel_width;
19096 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19100 while (from >= end && to >= toend)
19101 *to-- = *from--;
19102 if (!FRAME_WINDOW_P (it->f))
19104 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19106 from =
19107 truncate_it.glyph_row->glyphs[TEXT_AREA]
19108 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19109 while (from >= end && to >= toend)
19110 *to-- = *from--;
19113 if (from >= end)
19115 /* Need to free some room before prepending additional
19116 glyphs. */
19117 int move_by = from - end + 1;
19118 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19119 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19121 for ( ; g >= g0; g--)
19122 g[move_by] = *g;
19123 while (from >= end)
19124 *to-- = *from--;
19125 it->glyph_row->used[TEXT_AREA] += move_by;
19130 /* Compute the hash code for ROW. */
19131 unsigned
19132 row_hash (struct glyph_row *row)
19134 int area, k;
19135 unsigned hashval = 0;
19137 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19138 for (k = 0; k < row->used[area]; ++k)
19139 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19140 + row->glyphs[area][k].u.val
19141 + row->glyphs[area][k].face_id
19142 + row->glyphs[area][k].padding_p
19143 + (row->glyphs[area][k].type << 2));
19145 return hashval;
19148 /* Compute the pixel height and width of IT->glyph_row.
19150 Most of the time, ascent and height of a display line will be equal
19151 to the max_ascent and max_height values of the display iterator
19152 structure. This is not the case if
19154 1. We hit ZV without displaying anything. In this case, max_ascent
19155 and max_height will be zero.
19157 2. We have some glyphs that don't contribute to the line height.
19158 (The glyph row flag contributes_to_line_height_p is for future
19159 pixmap extensions).
19161 The first case is easily covered by using default values because in
19162 these cases, the line height does not really matter, except that it
19163 must not be zero. */
19165 static void
19166 compute_line_metrics (struct it *it)
19168 struct glyph_row *row = it->glyph_row;
19170 if (FRAME_WINDOW_P (it->f))
19172 int i, min_y, max_y;
19174 /* The line may consist of one space only, that was added to
19175 place the cursor on it. If so, the row's height hasn't been
19176 computed yet. */
19177 if (row->height == 0)
19179 if (it->max_ascent + it->max_descent == 0)
19180 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19181 row->ascent = it->max_ascent;
19182 row->height = it->max_ascent + it->max_descent;
19183 row->phys_ascent = it->max_phys_ascent;
19184 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19185 row->extra_line_spacing = it->max_extra_line_spacing;
19188 /* Compute the width of this line. */
19189 row->pixel_width = row->x;
19190 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19191 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19193 eassert (row->pixel_width >= 0);
19194 eassert (row->ascent >= 0 && row->height > 0);
19196 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19197 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19199 /* If first line's physical ascent is larger than its logical
19200 ascent, use the physical ascent, and make the row taller.
19201 This makes accented characters fully visible. */
19202 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19203 && row->phys_ascent > row->ascent)
19205 row->height += row->phys_ascent - row->ascent;
19206 row->ascent = row->phys_ascent;
19209 /* Compute how much of the line is visible. */
19210 row->visible_height = row->height;
19212 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19213 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19215 if (row->y < min_y)
19216 row->visible_height -= min_y - row->y;
19217 if (row->y + row->height > max_y)
19218 row->visible_height -= row->y + row->height - max_y;
19220 else
19222 row->pixel_width = row->used[TEXT_AREA];
19223 if (row->continued_p)
19224 row->pixel_width -= it->continuation_pixel_width;
19225 else if (row->truncated_on_right_p)
19226 row->pixel_width -= it->truncation_pixel_width;
19227 row->ascent = row->phys_ascent = 0;
19228 row->height = row->phys_height = row->visible_height = 1;
19229 row->extra_line_spacing = 0;
19232 /* Compute a hash code for this row. */
19233 row->hash = row_hash (row);
19235 it->max_ascent = it->max_descent = 0;
19236 it->max_phys_ascent = it->max_phys_descent = 0;
19240 /* Append one space to the glyph row of iterator IT if doing a
19241 window-based redisplay. The space has the same face as
19242 IT->face_id. Value is non-zero if a space was added.
19244 This function is called to make sure that there is always one glyph
19245 at the end of a glyph row that the cursor can be set on under
19246 window-systems. (If there weren't such a glyph we would not know
19247 how wide and tall a box cursor should be displayed).
19249 At the same time this space let's a nicely handle clearing to the
19250 end of the line if the row ends in italic text. */
19252 static int
19253 append_space_for_newline (struct it *it, int default_face_p)
19255 if (FRAME_WINDOW_P (it->f))
19257 int n = it->glyph_row->used[TEXT_AREA];
19259 if (it->glyph_row->glyphs[TEXT_AREA] + n
19260 < it->glyph_row->glyphs[1 + TEXT_AREA])
19262 /* Save some values that must not be changed.
19263 Must save IT->c and IT->len because otherwise
19264 ITERATOR_AT_END_P wouldn't work anymore after
19265 append_space_for_newline has been called. */
19266 enum display_element_type saved_what = it->what;
19267 int saved_c = it->c, saved_len = it->len;
19268 int saved_char_to_display = it->char_to_display;
19269 int saved_x = it->current_x;
19270 int saved_face_id = it->face_id;
19271 int saved_box_end = it->end_of_box_run_p;
19272 struct text_pos saved_pos;
19273 Lisp_Object saved_object;
19274 struct face *face;
19276 saved_object = it->object;
19277 saved_pos = it->position;
19279 it->what = IT_CHARACTER;
19280 memset (&it->position, 0, sizeof it->position);
19281 it->object = make_number (0);
19282 it->c = it->char_to_display = ' ';
19283 it->len = 1;
19285 /* If the default face was remapped, be sure to use the
19286 remapped face for the appended newline. */
19287 if (default_face_p)
19288 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19289 else if (it->face_before_selective_p)
19290 it->face_id = it->saved_face_id;
19291 face = FACE_FROM_ID (it->f, it->face_id);
19292 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19293 /* In R2L rows, we will prepend a stretch glyph that will
19294 have the end_of_box_run_p flag set for it, so there's no
19295 need for the appended newline glyph to have that flag
19296 set. */
19297 if (it->glyph_row->reversed_p
19298 /* But if the appended newline glyph goes all the way to
19299 the end of the row, there will be no stretch glyph,
19300 so leave the box flag set. */
19301 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19302 it->end_of_box_run_p = 0;
19304 PRODUCE_GLYPHS (it);
19306 it->override_ascent = -1;
19307 it->constrain_row_ascent_descent_p = 0;
19308 it->current_x = saved_x;
19309 it->object = saved_object;
19310 it->position = saved_pos;
19311 it->what = saved_what;
19312 it->face_id = saved_face_id;
19313 it->len = saved_len;
19314 it->c = saved_c;
19315 it->char_to_display = saved_char_to_display;
19316 it->end_of_box_run_p = saved_box_end;
19317 return 1;
19321 return 0;
19325 /* Extend the face of the last glyph in the text area of IT->glyph_row
19326 to the end of the display line. Called from display_line. If the
19327 glyph row is empty, add a space glyph to it so that we know the
19328 face to draw. Set the glyph row flag fill_line_p. If the glyph
19329 row is R2L, prepend a stretch glyph to cover the empty space to the
19330 left of the leftmost glyph. */
19332 static void
19333 extend_face_to_end_of_line (struct it *it)
19335 struct face *face, *default_face;
19336 struct frame *f = it->f;
19338 /* If line is already filled, do nothing. Non window-system frames
19339 get a grace of one more ``pixel'' because their characters are
19340 1-``pixel'' wide, so they hit the equality too early. This grace
19341 is needed only for R2L rows that are not continued, to produce
19342 one extra blank where we could display the cursor. */
19343 if ((it->current_x >= it->last_visible_x
19344 + (!FRAME_WINDOW_P (f)
19345 && it->glyph_row->reversed_p
19346 && !it->glyph_row->continued_p))
19347 /* If the window has display margins, we will need to extend
19348 their face even if the text area is filled. */
19349 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19350 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19351 return;
19353 /* The default face, possibly remapped. */
19354 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19356 /* Face extension extends the background and box of IT->face_id
19357 to the end of the line. If the background equals the background
19358 of the frame, we don't have to do anything. */
19359 if (it->face_before_selective_p)
19360 face = FACE_FROM_ID (f, it->saved_face_id);
19361 else
19362 face = FACE_FROM_ID (f, it->face_id);
19364 if (FRAME_WINDOW_P (f)
19365 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19366 && face->box == FACE_NO_BOX
19367 && face->background == FRAME_BACKGROUND_PIXEL (f)
19368 #ifdef HAVE_WINDOW_SYSTEM
19369 && !face->stipple
19370 #endif
19371 && !it->glyph_row->reversed_p)
19372 return;
19374 /* Set the glyph row flag indicating that the face of the last glyph
19375 in the text area has to be drawn to the end of the text area. */
19376 it->glyph_row->fill_line_p = 1;
19378 /* If current character of IT is not ASCII, make sure we have the
19379 ASCII face. This will be automatically undone the next time
19380 get_next_display_element returns a multibyte character. Note
19381 that the character will always be single byte in unibyte
19382 text. */
19383 if (!ASCII_CHAR_P (it->c))
19385 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19388 if (FRAME_WINDOW_P (f))
19390 /* If the row is empty, add a space with the current face of IT,
19391 so that we know which face to draw. */
19392 if (it->glyph_row->used[TEXT_AREA] == 0)
19394 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19395 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19396 it->glyph_row->used[TEXT_AREA] = 1;
19398 /* Mode line and the header line don't have margins, and
19399 likewise the frame's tool-bar window, if there is any. */
19400 if (!(it->glyph_row->mode_line_p
19401 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19402 || (WINDOWP (f->tool_bar_window)
19403 && it->w == XWINDOW (f->tool_bar_window))
19404 #endif
19407 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19408 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19410 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19411 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19412 default_face->id;
19413 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19415 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19416 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19418 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19419 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19420 default_face->id;
19421 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19424 #ifdef HAVE_WINDOW_SYSTEM
19425 if (it->glyph_row->reversed_p)
19427 /* Prepend a stretch glyph to the row, such that the
19428 rightmost glyph will be drawn flushed all the way to the
19429 right margin of the window. The stretch glyph that will
19430 occupy the empty space, if any, to the left of the
19431 glyphs. */
19432 struct font *font = face->font ? face->font : FRAME_FONT (f);
19433 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19434 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19435 struct glyph *g;
19436 int row_width, stretch_ascent, stretch_width;
19437 struct text_pos saved_pos;
19438 int saved_face_id, saved_avoid_cursor, saved_box_start;
19440 for (row_width = 0, g = row_start; g < row_end; g++)
19441 row_width += g->pixel_width;
19443 /* FIXME: There are various minor display glitches in R2L
19444 rows when only one of the fringes is missing. The
19445 strange condition below produces the least bad effect. */
19446 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19447 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19448 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19449 stretch_width = window_box_width (it->w, TEXT_AREA);
19450 else
19451 stretch_width = it->last_visible_x - it->first_visible_x;
19452 stretch_width -= row_width;
19454 if (stretch_width > 0)
19456 stretch_ascent =
19457 (((it->ascent + it->descent)
19458 * FONT_BASE (font)) / FONT_HEIGHT (font));
19459 saved_pos = it->position;
19460 memset (&it->position, 0, sizeof it->position);
19461 saved_avoid_cursor = it->avoid_cursor_p;
19462 it->avoid_cursor_p = 1;
19463 saved_face_id = it->face_id;
19464 saved_box_start = it->start_of_box_run_p;
19465 /* The last row's stretch glyph should get the default
19466 face, to avoid painting the rest of the window with
19467 the region face, if the region ends at ZV. */
19468 if (it->glyph_row->ends_at_zv_p)
19469 it->face_id = default_face->id;
19470 else
19471 it->face_id = face->id;
19472 it->start_of_box_run_p = 0;
19473 append_stretch_glyph (it, make_number (0), stretch_width,
19474 it->ascent + it->descent, stretch_ascent);
19475 it->position = saved_pos;
19476 it->avoid_cursor_p = saved_avoid_cursor;
19477 it->face_id = saved_face_id;
19478 it->start_of_box_run_p = saved_box_start;
19480 /* If stretch_width comes out negative, it means that the
19481 last glyph is only partially visible. In R2L rows, we
19482 want the leftmost glyph to be partially visible, so we
19483 need to give the row the corresponding left offset. */
19484 if (stretch_width < 0)
19485 it->glyph_row->x = stretch_width;
19487 #endif /* HAVE_WINDOW_SYSTEM */
19489 else
19491 /* Save some values that must not be changed. */
19492 int saved_x = it->current_x;
19493 struct text_pos saved_pos;
19494 Lisp_Object saved_object;
19495 enum display_element_type saved_what = it->what;
19496 int saved_face_id = it->face_id;
19498 saved_object = it->object;
19499 saved_pos = it->position;
19501 it->what = IT_CHARACTER;
19502 memset (&it->position, 0, sizeof it->position);
19503 it->object = make_number (0);
19504 it->c = it->char_to_display = ' ';
19505 it->len = 1;
19507 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19508 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19509 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19510 && !it->glyph_row->mode_line_p
19511 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19513 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19514 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19516 for (it->current_x = 0; g < e; g++)
19517 it->current_x += g->pixel_width;
19519 it->area = LEFT_MARGIN_AREA;
19520 it->face_id = default_face->id;
19521 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19522 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19524 PRODUCE_GLYPHS (it);
19525 /* term.c:produce_glyphs advances it->current_x only for
19526 TEXT_AREA. */
19527 it->current_x += it->pixel_width;
19530 it->current_x = saved_x;
19531 it->area = TEXT_AREA;
19534 /* The last row's blank glyphs should get the default face, to
19535 avoid painting the rest of the window with the region face,
19536 if the region ends at ZV. */
19537 if (it->glyph_row->ends_at_zv_p)
19538 it->face_id = default_face->id;
19539 else
19540 it->face_id = face->id;
19541 PRODUCE_GLYPHS (it);
19543 while (it->current_x <= it->last_visible_x)
19544 PRODUCE_GLYPHS (it);
19546 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19547 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19548 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19549 && !it->glyph_row->mode_line_p
19550 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19552 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19553 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19555 for ( ; g < e; g++)
19556 it->current_x += g->pixel_width;
19558 it->area = RIGHT_MARGIN_AREA;
19559 it->face_id = default_face->id;
19560 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19561 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19563 PRODUCE_GLYPHS (it);
19564 it->current_x += it->pixel_width;
19567 it->area = TEXT_AREA;
19570 /* Don't count these blanks really. It would let us insert a left
19571 truncation glyph below and make us set the cursor on them, maybe. */
19572 it->current_x = saved_x;
19573 it->object = saved_object;
19574 it->position = saved_pos;
19575 it->what = saved_what;
19576 it->face_id = saved_face_id;
19581 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19582 trailing whitespace. */
19584 static int
19585 trailing_whitespace_p (ptrdiff_t charpos)
19587 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19588 int c = 0;
19590 while (bytepos < ZV_BYTE
19591 && (c = FETCH_CHAR (bytepos),
19592 c == ' ' || c == '\t'))
19593 ++bytepos;
19595 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19597 if (bytepos != PT_BYTE)
19598 return 1;
19600 return 0;
19604 /* Highlight trailing whitespace, if any, in ROW. */
19606 static void
19607 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19609 int used = row->used[TEXT_AREA];
19611 if (used)
19613 struct glyph *start = row->glyphs[TEXT_AREA];
19614 struct glyph *glyph = start + used - 1;
19616 if (row->reversed_p)
19618 /* Right-to-left rows need to be processed in the opposite
19619 direction, so swap the edge pointers. */
19620 glyph = start;
19621 start = row->glyphs[TEXT_AREA] + used - 1;
19624 /* Skip over glyphs inserted to display the cursor at the
19625 end of a line, for extending the face of the last glyph
19626 to the end of the line on terminals, and for truncation
19627 and continuation glyphs. */
19628 if (!row->reversed_p)
19630 while (glyph >= start
19631 && glyph->type == CHAR_GLYPH
19632 && INTEGERP (glyph->object))
19633 --glyph;
19635 else
19637 while (glyph <= start
19638 && glyph->type == CHAR_GLYPH
19639 && INTEGERP (glyph->object))
19640 ++glyph;
19643 /* If last glyph is a space or stretch, and it's trailing
19644 whitespace, set the face of all trailing whitespace glyphs in
19645 IT->glyph_row to `trailing-whitespace'. */
19646 if ((row->reversed_p ? glyph <= start : glyph >= start)
19647 && BUFFERP (glyph->object)
19648 && (glyph->type == STRETCH_GLYPH
19649 || (glyph->type == CHAR_GLYPH
19650 && glyph->u.ch == ' '))
19651 && trailing_whitespace_p (glyph->charpos))
19653 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19654 if (face_id < 0)
19655 return;
19657 if (!row->reversed_p)
19659 while (glyph >= start
19660 && BUFFERP (glyph->object)
19661 && (glyph->type == STRETCH_GLYPH
19662 || (glyph->type == CHAR_GLYPH
19663 && glyph->u.ch == ' ')))
19664 (glyph--)->face_id = face_id;
19666 else
19668 while (glyph <= start
19669 && BUFFERP (glyph->object)
19670 && (glyph->type == STRETCH_GLYPH
19671 || (glyph->type == CHAR_GLYPH
19672 && glyph->u.ch == ' ')))
19673 (glyph++)->face_id = face_id;
19680 /* Value is non-zero if glyph row ROW should be
19681 considered to hold the buffer position CHARPOS. */
19683 static int
19684 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19686 int result = 1;
19688 if (charpos == CHARPOS (row->end.pos)
19689 || charpos == MATRIX_ROW_END_CHARPOS (row))
19691 /* Suppose the row ends on a string.
19692 Unless the row is continued, that means it ends on a newline
19693 in the string. If it's anything other than a display string
19694 (e.g., a before-string from an overlay), we don't want the
19695 cursor there. (This heuristic seems to give the optimal
19696 behavior for the various types of multi-line strings.)
19697 One exception: if the string has `cursor' property on one of
19698 its characters, we _do_ want the cursor there. */
19699 if (CHARPOS (row->end.string_pos) >= 0)
19701 if (row->continued_p)
19702 result = 1;
19703 else
19705 /* Check for `display' property. */
19706 struct glyph *beg = row->glyphs[TEXT_AREA];
19707 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19708 struct glyph *glyph;
19710 result = 0;
19711 for (glyph = end; glyph >= beg; --glyph)
19712 if (STRINGP (glyph->object))
19714 Lisp_Object prop
19715 = Fget_char_property (make_number (charpos),
19716 Qdisplay, Qnil);
19717 result =
19718 (!NILP (prop)
19719 && display_prop_string_p (prop, glyph->object));
19720 /* If there's a `cursor' property on one of the
19721 string's characters, this row is a cursor row,
19722 even though this is not a display string. */
19723 if (!result)
19725 Lisp_Object s = glyph->object;
19727 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19729 ptrdiff_t gpos = glyph->charpos;
19731 if (!NILP (Fget_char_property (make_number (gpos),
19732 Qcursor, s)))
19734 result = 1;
19735 break;
19739 break;
19743 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19745 /* If the row ends in middle of a real character,
19746 and the line is continued, we want the cursor here.
19747 That's because CHARPOS (ROW->end.pos) would equal
19748 PT if PT is before the character. */
19749 if (!row->ends_in_ellipsis_p)
19750 result = row->continued_p;
19751 else
19752 /* If the row ends in an ellipsis, then
19753 CHARPOS (ROW->end.pos) will equal point after the
19754 invisible text. We want that position to be displayed
19755 after the ellipsis. */
19756 result = 0;
19758 /* If the row ends at ZV, display the cursor at the end of that
19759 row instead of at the start of the row below. */
19760 else if (row->ends_at_zv_p)
19761 result = 1;
19762 else
19763 result = 0;
19766 return result;
19769 /* Value is non-zero if glyph row ROW should be
19770 used to hold the cursor. */
19772 static int
19773 cursor_row_p (struct glyph_row *row)
19775 return row_for_charpos_p (row, PT);
19780 /* Push the property PROP so that it will be rendered at the current
19781 position in IT. Return 1 if PROP was successfully pushed, 0
19782 otherwise. Called from handle_line_prefix to handle the
19783 `line-prefix' and `wrap-prefix' properties. */
19785 static int
19786 push_prefix_prop (struct it *it, Lisp_Object prop)
19788 struct text_pos pos =
19789 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19791 eassert (it->method == GET_FROM_BUFFER
19792 || it->method == GET_FROM_DISPLAY_VECTOR
19793 || it->method == GET_FROM_STRING);
19795 /* We need to save the current buffer/string position, so it will be
19796 restored by pop_it, because iterate_out_of_display_property
19797 depends on that being set correctly, but some situations leave
19798 it->position not yet set when this function is called. */
19799 push_it (it, &pos);
19801 if (STRINGP (prop))
19803 if (SCHARS (prop) == 0)
19805 pop_it (it);
19806 return 0;
19809 it->string = prop;
19810 it->string_from_prefix_prop_p = 1;
19811 it->multibyte_p = STRING_MULTIBYTE (it->string);
19812 it->current.overlay_string_index = -1;
19813 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19814 it->end_charpos = it->string_nchars = SCHARS (it->string);
19815 it->method = GET_FROM_STRING;
19816 it->stop_charpos = 0;
19817 it->prev_stop = 0;
19818 it->base_level_stop = 0;
19820 /* Force paragraph direction to be that of the parent
19821 buffer/string. */
19822 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19823 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19824 else
19825 it->paragraph_embedding = L2R;
19827 /* Set up the bidi iterator for this display string. */
19828 if (it->bidi_p)
19830 it->bidi_it.string.lstring = it->string;
19831 it->bidi_it.string.s = NULL;
19832 it->bidi_it.string.schars = it->end_charpos;
19833 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19834 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19835 it->bidi_it.string.unibyte = !it->multibyte_p;
19836 it->bidi_it.w = it->w;
19837 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19840 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19842 it->method = GET_FROM_STRETCH;
19843 it->object = prop;
19845 #ifdef HAVE_WINDOW_SYSTEM
19846 else if (IMAGEP (prop))
19848 it->what = IT_IMAGE;
19849 it->image_id = lookup_image (it->f, prop);
19850 it->method = GET_FROM_IMAGE;
19852 #endif /* HAVE_WINDOW_SYSTEM */
19853 else
19855 pop_it (it); /* bogus display property, give up */
19856 return 0;
19859 return 1;
19862 /* Return the character-property PROP at the current position in IT. */
19864 static Lisp_Object
19865 get_it_property (struct it *it, Lisp_Object prop)
19867 Lisp_Object position, object = it->object;
19869 if (STRINGP (object))
19870 position = make_number (IT_STRING_CHARPOS (*it));
19871 else if (BUFFERP (object))
19873 position = make_number (IT_CHARPOS (*it));
19874 object = it->window;
19876 else
19877 return Qnil;
19879 return Fget_char_property (position, prop, object);
19882 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19884 static void
19885 handle_line_prefix (struct it *it)
19887 Lisp_Object prefix;
19889 if (it->continuation_lines_width > 0)
19891 prefix = get_it_property (it, Qwrap_prefix);
19892 if (NILP (prefix))
19893 prefix = Vwrap_prefix;
19895 else
19897 prefix = get_it_property (it, Qline_prefix);
19898 if (NILP (prefix))
19899 prefix = Vline_prefix;
19901 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19903 /* If the prefix is wider than the window, and we try to wrap
19904 it, it would acquire its own wrap prefix, and so on till the
19905 iterator stack overflows. So, don't wrap the prefix. */
19906 it->line_wrap = TRUNCATE;
19907 it->avoid_cursor_p = 1;
19913 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19914 only for R2L lines from display_line and display_string, when they
19915 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19916 the line/string needs to be continued on the next glyph row. */
19917 static void
19918 unproduce_glyphs (struct it *it, int n)
19920 struct glyph *glyph, *end;
19922 eassert (it->glyph_row);
19923 eassert (it->glyph_row->reversed_p);
19924 eassert (it->area == TEXT_AREA);
19925 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19927 if (n > it->glyph_row->used[TEXT_AREA])
19928 n = it->glyph_row->used[TEXT_AREA];
19929 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19930 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19931 for ( ; glyph < end; glyph++)
19932 glyph[-n] = *glyph;
19935 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19936 and ROW->maxpos. */
19937 static void
19938 find_row_edges (struct it *it, struct glyph_row *row,
19939 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19940 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19942 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19943 lines' rows is implemented for bidi-reordered rows. */
19945 /* ROW->minpos is the value of min_pos, the minimal buffer position
19946 we have in ROW, or ROW->start.pos if that is smaller. */
19947 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19948 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19949 else
19950 /* We didn't find buffer positions smaller than ROW->start, or
19951 didn't find _any_ valid buffer positions in any of the glyphs,
19952 so we must trust the iterator's computed positions. */
19953 row->minpos = row->start.pos;
19954 if (max_pos <= 0)
19956 max_pos = CHARPOS (it->current.pos);
19957 max_bpos = BYTEPOS (it->current.pos);
19960 /* Here are the various use-cases for ending the row, and the
19961 corresponding values for ROW->maxpos:
19963 Line ends in a newline from buffer eol_pos + 1
19964 Line is continued from buffer max_pos + 1
19965 Line is truncated on right it->current.pos
19966 Line ends in a newline from string max_pos + 1(*)
19967 (*) + 1 only when line ends in a forward scan
19968 Line is continued from string max_pos
19969 Line is continued from display vector max_pos
19970 Line is entirely from a string min_pos == max_pos
19971 Line is entirely from a display vector min_pos == max_pos
19972 Line that ends at ZV ZV
19974 If you discover other use-cases, please add them here as
19975 appropriate. */
19976 if (row->ends_at_zv_p)
19977 row->maxpos = it->current.pos;
19978 else if (row->used[TEXT_AREA])
19980 int seen_this_string = 0;
19981 struct glyph_row *r1 = row - 1;
19983 /* Did we see the same display string on the previous row? */
19984 if (STRINGP (it->object)
19985 /* this is not the first row */
19986 && row > it->w->desired_matrix->rows
19987 /* previous row is not the header line */
19988 && !r1->mode_line_p
19989 /* previous row also ends in a newline from a string */
19990 && r1->ends_in_newline_from_string_p)
19992 struct glyph *start, *end;
19994 /* Search for the last glyph of the previous row that came
19995 from buffer or string. Depending on whether the row is
19996 L2R or R2L, we need to process it front to back or the
19997 other way round. */
19998 if (!r1->reversed_p)
20000 start = r1->glyphs[TEXT_AREA];
20001 end = start + r1->used[TEXT_AREA];
20002 /* Glyphs inserted by redisplay have an integer (zero)
20003 as their object. */
20004 while (end > start
20005 && INTEGERP ((end - 1)->object)
20006 && (end - 1)->charpos <= 0)
20007 --end;
20008 if (end > start)
20010 if (EQ ((end - 1)->object, it->object))
20011 seen_this_string = 1;
20013 else
20014 /* If all the glyphs of the previous row were inserted
20015 by redisplay, it means the previous row was
20016 produced from a single newline, which is only
20017 possible if that newline came from the same string
20018 as the one which produced this ROW. */
20019 seen_this_string = 1;
20021 else
20023 end = r1->glyphs[TEXT_AREA] - 1;
20024 start = end + r1->used[TEXT_AREA];
20025 while (end < start
20026 && INTEGERP ((end + 1)->object)
20027 && (end + 1)->charpos <= 0)
20028 ++end;
20029 if (end < start)
20031 if (EQ ((end + 1)->object, it->object))
20032 seen_this_string = 1;
20034 else
20035 seen_this_string = 1;
20038 /* Take note of each display string that covers a newline only
20039 once, the first time we see it. This is for when a display
20040 string includes more than one newline in it. */
20041 if (row->ends_in_newline_from_string_p && !seen_this_string)
20043 /* If we were scanning the buffer forward when we displayed
20044 the string, we want to account for at least one buffer
20045 position that belongs to this row (position covered by
20046 the display string), so that cursor positioning will
20047 consider this row as a candidate when point is at the end
20048 of the visual line represented by this row. This is not
20049 required when scanning back, because max_pos will already
20050 have a much larger value. */
20051 if (CHARPOS (row->end.pos) > max_pos)
20052 INC_BOTH (max_pos, max_bpos);
20053 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20055 else if (CHARPOS (it->eol_pos) > 0)
20056 SET_TEXT_POS (row->maxpos,
20057 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20058 else if (row->continued_p)
20060 /* If max_pos is different from IT's current position, it
20061 means IT->method does not belong to the display element
20062 at max_pos. However, it also means that the display
20063 element at max_pos was displayed in its entirety on this
20064 line, which is equivalent to saying that the next line
20065 starts at the next buffer position. */
20066 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20067 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20068 else
20070 INC_BOTH (max_pos, max_bpos);
20071 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20074 else if (row->truncated_on_right_p)
20075 /* display_line already called reseat_at_next_visible_line_start,
20076 which puts the iterator at the beginning of the next line, in
20077 the logical order. */
20078 row->maxpos = it->current.pos;
20079 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20080 /* A line that is entirely from a string/image/stretch... */
20081 row->maxpos = row->minpos;
20082 else
20083 emacs_abort ();
20085 else
20086 row->maxpos = it->current.pos;
20089 /* Construct the glyph row IT->glyph_row in the desired matrix of
20090 IT->w from text at the current position of IT. See dispextern.h
20091 for an overview of struct it. Value is non-zero if
20092 IT->glyph_row displays text, as opposed to a line displaying ZV
20093 only. */
20095 static int
20096 display_line (struct it *it)
20098 struct glyph_row *row = it->glyph_row;
20099 Lisp_Object overlay_arrow_string;
20100 struct it wrap_it;
20101 void *wrap_data = NULL;
20102 int may_wrap = 0, wrap_x IF_LINT (= 0);
20103 int wrap_row_used = -1;
20104 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20105 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20106 int wrap_row_extra_line_spacing IF_LINT (= 0);
20107 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20108 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20109 int cvpos;
20110 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20111 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20112 bool pending_handle_line_prefix = false;
20114 /* We always start displaying at hpos zero even if hscrolled. */
20115 eassert (it->hpos == 0 && it->current_x == 0);
20117 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20118 >= it->w->desired_matrix->nrows)
20120 it->w->nrows_scale_factor++;
20121 it->f->fonts_changed = 1;
20122 return 0;
20125 /* Clear the result glyph row and enable it. */
20126 prepare_desired_row (it->w, row, false);
20128 row->y = it->current_y;
20129 row->start = it->start;
20130 row->continuation_lines_width = it->continuation_lines_width;
20131 row->displays_text_p = 1;
20132 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20133 it->starts_in_middle_of_char_p = 0;
20135 /* Arrange the overlays nicely for our purposes. Usually, we call
20136 display_line on only one line at a time, in which case this
20137 can't really hurt too much, or we call it on lines which appear
20138 one after another in the buffer, in which case all calls to
20139 recenter_overlay_lists but the first will be pretty cheap. */
20140 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20142 /* Move over display elements that are not visible because we are
20143 hscrolled. This may stop at an x-position < IT->first_visible_x
20144 if the first glyph is partially visible or if we hit a line end. */
20145 if (it->current_x < it->first_visible_x)
20147 enum move_it_result move_result;
20149 this_line_min_pos = row->start.pos;
20150 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20151 MOVE_TO_POS | MOVE_TO_X);
20152 /* If we are under a large hscroll, move_it_in_display_line_to
20153 could hit the end of the line without reaching
20154 it->first_visible_x. Pretend that we did reach it. This is
20155 especially important on a TTY, where we will call
20156 extend_face_to_end_of_line, which needs to know how many
20157 blank glyphs to produce. */
20158 if (it->current_x < it->first_visible_x
20159 && (move_result == MOVE_NEWLINE_OR_CR
20160 || move_result == MOVE_POS_MATCH_OR_ZV))
20161 it->current_x = it->first_visible_x;
20163 /* Record the smallest positions seen while we moved over
20164 display elements that are not visible. This is needed by
20165 redisplay_internal for optimizing the case where the cursor
20166 stays inside the same line. The rest of this function only
20167 considers positions that are actually displayed, so
20168 RECORD_MAX_MIN_POS will not otherwise record positions that
20169 are hscrolled to the left of the left edge of the window. */
20170 min_pos = CHARPOS (this_line_min_pos);
20171 min_bpos = BYTEPOS (this_line_min_pos);
20173 else if (it->area == TEXT_AREA)
20175 /* We only do this when not calling move_it_in_display_line_to
20176 above, because that function calls itself handle_line_prefix. */
20177 handle_line_prefix (it);
20179 else
20181 /* Line-prefix and wrap-prefix are always displayed in the text
20182 area. But if this is the first call to display_line after
20183 init_iterator, the iterator might have been set up to write
20184 into a marginal area, e.g. if the line begins with some
20185 display property that writes to the margins. So we need to
20186 wait with the call to handle_line_prefix until whatever
20187 writes to the margin has done its job. */
20188 pending_handle_line_prefix = true;
20191 /* Get the initial row height. This is either the height of the
20192 text hscrolled, if there is any, or zero. */
20193 row->ascent = it->max_ascent;
20194 row->height = it->max_ascent + it->max_descent;
20195 row->phys_ascent = it->max_phys_ascent;
20196 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20197 row->extra_line_spacing = it->max_extra_line_spacing;
20199 /* Utility macro to record max and min buffer positions seen until now. */
20200 #define RECORD_MAX_MIN_POS(IT) \
20201 do \
20203 int composition_p = !STRINGP ((IT)->string) \
20204 && ((IT)->what == IT_COMPOSITION); \
20205 ptrdiff_t current_pos = \
20206 composition_p ? (IT)->cmp_it.charpos \
20207 : IT_CHARPOS (*(IT)); \
20208 ptrdiff_t current_bpos = \
20209 composition_p ? CHAR_TO_BYTE (current_pos) \
20210 : IT_BYTEPOS (*(IT)); \
20211 if (current_pos < min_pos) \
20213 min_pos = current_pos; \
20214 min_bpos = current_bpos; \
20216 if (IT_CHARPOS (*it) > max_pos) \
20218 max_pos = IT_CHARPOS (*it); \
20219 max_bpos = IT_BYTEPOS (*it); \
20222 while (0)
20224 /* Loop generating characters. The loop is left with IT on the next
20225 character to display. */
20226 while (1)
20228 int n_glyphs_before, hpos_before, x_before;
20229 int x, nglyphs;
20230 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20232 /* Retrieve the next thing to display. Value is zero if end of
20233 buffer reached. */
20234 if (!get_next_display_element (it))
20236 /* Maybe add a space at the end of this line that is used to
20237 display the cursor there under X. Set the charpos of the
20238 first glyph of blank lines not corresponding to any text
20239 to -1. */
20240 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20241 row->exact_window_width_line_p = 1;
20242 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20243 || row->used[TEXT_AREA] == 0)
20245 row->glyphs[TEXT_AREA]->charpos = -1;
20246 row->displays_text_p = 0;
20248 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20249 && (!MINI_WINDOW_P (it->w)
20250 || (minibuf_level && EQ (it->window, minibuf_window))))
20251 row->indicate_empty_line_p = 1;
20254 it->continuation_lines_width = 0;
20255 row->ends_at_zv_p = 1;
20256 /* A row that displays right-to-left text must always have
20257 its last face extended all the way to the end of line,
20258 even if this row ends in ZV, because we still write to
20259 the screen left to right. We also need to extend the
20260 last face if the default face is remapped to some
20261 different face, otherwise the functions that clear
20262 portions of the screen will clear with the default face's
20263 background color. */
20264 if (row->reversed_p
20265 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20266 extend_face_to_end_of_line (it);
20267 break;
20270 /* Now, get the metrics of what we want to display. This also
20271 generates glyphs in `row' (which is IT->glyph_row). */
20272 n_glyphs_before = row->used[TEXT_AREA];
20273 x = it->current_x;
20275 /* Remember the line height so far in case the next element doesn't
20276 fit on the line. */
20277 if (it->line_wrap != TRUNCATE)
20279 ascent = it->max_ascent;
20280 descent = it->max_descent;
20281 phys_ascent = it->max_phys_ascent;
20282 phys_descent = it->max_phys_descent;
20284 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20286 if (IT_DISPLAYING_WHITESPACE (it))
20287 may_wrap = 1;
20288 else if (may_wrap)
20290 SAVE_IT (wrap_it, *it, wrap_data);
20291 wrap_x = x;
20292 wrap_row_used = row->used[TEXT_AREA];
20293 wrap_row_ascent = row->ascent;
20294 wrap_row_height = row->height;
20295 wrap_row_phys_ascent = row->phys_ascent;
20296 wrap_row_phys_height = row->phys_height;
20297 wrap_row_extra_line_spacing = row->extra_line_spacing;
20298 wrap_row_min_pos = min_pos;
20299 wrap_row_min_bpos = min_bpos;
20300 wrap_row_max_pos = max_pos;
20301 wrap_row_max_bpos = max_bpos;
20302 may_wrap = 0;
20307 PRODUCE_GLYPHS (it);
20309 /* If this display element was in marginal areas, continue with
20310 the next one. */
20311 if (it->area != TEXT_AREA)
20313 row->ascent = max (row->ascent, it->max_ascent);
20314 row->height = max (row->height, it->max_ascent + it->max_descent);
20315 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20316 row->phys_height = max (row->phys_height,
20317 it->max_phys_ascent + it->max_phys_descent);
20318 row->extra_line_spacing = max (row->extra_line_spacing,
20319 it->max_extra_line_spacing);
20320 set_iterator_to_next (it, 1);
20321 /* If we didn't handle the line/wrap prefix above, and the
20322 call to set_iterator_to_next just switched to TEXT_AREA,
20323 process the prefix now. */
20324 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20326 pending_handle_line_prefix = false;
20327 handle_line_prefix (it);
20329 continue;
20332 /* Does the display element fit on the line? If we truncate
20333 lines, we should draw past the right edge of the window. If
20334 we don't truncate, we want to stop so that we can display the
20335 continuation glyph before the right margin. If lines are
20336 continued, there are two possible strategies for characters
20337 resulting in more than 1 glyph (e.g. tabs): Display as many
20338 glyphs as possible in this line and leave the rest for the
20339 continuation line, or display the whole element in the next
20340 line. Original redisplay did the former, so we do it also. */
20341 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20342 hpos_before = it->hpos;
20343 x_before = x;
20345 if (/* Not a newline. */
20346 nglyphs > 0
20347 /* Glyphs produced fit entirely in the line. */
20348 && it->current_x < it->last_visible_x)
20350 it->hpos += nglyphs;
20351 row->ascent = max (row->ascent, it->max_ascent);
20352 row->height = max (row->height, it->max_ascent + it->max_descent);
20353 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20354 row->phys_height = max (row->phys_height,
20355 it->max_phys_ascent + it->max_phys_descent);
20356 row->extra_line_spacing = max (row->extra_line_spacing,
20357 it->max_extra_line_spacing);
20358 if (it->current_x - it->pixel_width < it->first_visible_x
20359 /* In R2L rows, we arrange in extend_face_to_end_of_line
20360 to add a right offset to the line, by a suitable
20361 change to the stretch glyph that is the leftmost
20362 glyph of the line. */
20363 && !row->reversed_p)
20364 row->x = x - it->first_visible_x;
20365 /* Record the maximum and minimum buffer positions seen so
20366 far in glyphs that will be displayed by this row. */
20367 if (it->bidi_p)
20368 RECORD_MAX_MIN_POS (it);
20370 else
20372 int i, new_x;
20373 struct glyph *glyph;
20375 for (i = 0; i < nglyphs; ++i, x = new_x)
20377 /* Identify the glyphs added by the last call to
20378 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20379 the previous glyphs. */
20380 if (!row->reversed_p)
20381 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20382 else
20383 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20384 new_x = x + glyph->pixel_width;
20386 if (/* Lines are continued. */
20387 it->line_wrap != TRUNCATE
20388 && (/* Glyph doesn't fit on the line. */
20389 new_x > it->last_visible_x
20390 /* Or it fits exactly on a window system frame. */
20391 || (new_x == it->last_visible_x
20392 && FRAME_WINDOW_P (it->f)
20393 && (row->reversed_p
20394 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20395 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20397 /* End of a continued line. */
20399 if (it->hpos == 0
20400 || (new_x == it->last_visible_x
20401 && FRAME_WINDOW_P (it->f)
20402 && (row->reversed_p
20403 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20404 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20406 /* Current glyph is the only one on the line or
20407 fits exactly on the line. We must continue
20408 the line because we can't draw the cursor
20409 after the glyph. */
20410 row->continued_p = 1;
20411 it->current_x = new_x;
20412 it->continuation_lines_width += new_x;
20413 ++it->hpos;
20414 if (i == nglyphs - 1)
20416 /* If line-wrap is on, check if a previous
20417 wrap point was found. */
20418 if (wrap_row_used > 0
20419 /* Even if there is a previous wrap
20420 point, continue the line here as
20421 usual, if (i) the previous character
20422 was a space or tab AND (ii) the
20423 current character is not. */
20424 && (!may_wrap
20425 || IT_DISPLAYING_WHITESPACE (it)))
20426 goto back_to_wrap;
20428 /* Record the maximum and minimum buffer
20429 positions seen so far in glyphs that will be
20430 displayed by this row. */
20431 if (it->bidi_p)
20432 RECORD_MAX_MIN_POS (it);
20433 set_iterator_to_next (it, 1);
20434 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20436 if (!get_next_display_element (it))
20438 row->exact_window_width_line_p = 1;
20439 it->continuation_lines_width = 0;
20440 row->continued_p = 0;
20441 row->ends_at_zv_p = 1;
20443 else if (ITERATOR_AT_END_OF_LINE_P (it))
20445 row->continued_p = 0;
20446 row->exact_window_width_line_p = 1;
20450 else if (it->bidi_p)
20451 RECORD_MAX_MIN_POS (it);
20452 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20453 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20454 extend_face_to_end_of_line (it);
20456 else if (CHAR_GLYPH_PADDING_P (*glyph)
20457 && !FRAME_WINDOW_P (it->f))
20459 /* A padding glyph that doesn't fit on this line.
20460 This means the whole character doesn't fit
20461 on the line. */
20462 if (row->reversed_p)
20463 unproduce_glyphs (it, row->used[TEXT_AREA]
20464 - n_glyphs_before);
20465 row->used[TEXT_AREA] = n_glyphs_before;
20467 /* Fill the rest of the row with continuation
20468 glyphs like in 20.x. */
20469 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20470 < row->glyphs[1 + TEXT_AREA])
20471 produce_special_glyphs (it, IT_CONTINUATION);
20473 row->continued_p = 1;
20474 it->current_x = x_before;
20475 it->continuation_lines_width += x_before;
20477 /* Restore the height to what it was before the
20478 element not fitting on the line. */
20479 it->max_ascent = ascent;
20480 it->max_descent = descent;
20481 it->max_phys_ascent = phys_ascent;
20482 it->max_phys_descent = phys_descent;
20483 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20484 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20485 extend_face_to_end_of_line (it);
20487 else if (wrap_row_used > 0)
20489 back_to_wrap:
20490 if (row->reversed_p)
20491 unproduce_glyphs (it,
20492 row->used[TEXT_AREA] - wrap_row_used);
20493 RESTORE_IT (it, &wrap_it, wrap_data);
20494 it->continuation_lines_width += wrap_x;
20495 row->used[TEXT_AREA] = wrap_row_used;
20496 row->ascent = wrap_row_ascent;
20497 row->height = wrap_row_height;
20498 row->phys_ascent = wrap_row_phys_ascent;
20499 row->phys_height = wrap_row_phys_height;
20500 row->extra_line_spacing = wrap_row_extra_line_spacing;
20501 min_pos = wrap_row_min_pos;
20502 min_bpos = wrap_row_min_bpos;
20503 max_pos = wrap_row_max_pos;
20504 max_bpos = wrap_row_max_bpos;
20505 row->continued_p = 1;
20506 row->ends_at_zv_p = 0;
20507 row->exact_window_width_line_p = 0;
20508 it->continuation_lines_width += x;
20510 /* Make sure that a non-default face is extended
20511 up to the right margin of the window. */
20512 extend_face_to_end_of_line (it);
20514 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20516 /* A TAB that extends past the right edge of the
20517 window. This produces a single glyph on
20518 window system frames. We leave the glyph in
20519 this row and let it fill the row, but don't
20520 consume the TAB. */
20521 if ((row->reversed_p
20522 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20523 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20524 produce_special_glyphs (it, IT_CONTINUATION);
20525 it->continuation_lines_width += it->last_visible_x;
20526 row->ends_in_middle_of_char_p = 1;
20527 row->continued_p = 1;
20528 glyph->pixel_width = it->last_visible_x - x;
20529 it->starts_in_middle_of_char_p = 1;
20530 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20531 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20532 extend_face_to_end_of_line (it);
20534 else
20536 /* Something other than a TAB that draws past
20537 the right edge of the window. Restore
20538 positions to values before the element. */
20539 if (row->reversed_p)
20540 unproduce_glyphs (it, row->used[TEXT_AREA]
20541 - (n_glyphs_before + i));
20542 row->used[TEXT_AREA] = n_glyphs_before + i;
20544 /* Display continuation glyphs. */
20545 it->current_x = x_before;
20546 it->continuation_lines_width += x;
20547 if (!FRAME_WINDOW_P (it->f)
20548 || (row->reversed_p
20549 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20550 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20551 produce_special_glyphs (it, IT_CONTINUATION);
20552 row->continued_p = 1;
20554 extend_face_to_end_of_line (it);
20556 if (nglyphs > 1 && i > 0)
20558 row->ends_in_middle_of_char_p = 1;
20559 it->starts_in_middle_of_char_p = 1;
20562 /* Restore the height to what it was before the
20563 element not fitting on the line. */
20564 it->max_ascent = ascent;
20565 it->max_descent = descent;
20566 it->max_phys_ascent = phys_ascent;
20567 it->max_phys_descent = phys_descent;
20570 break;
20572 else if (new_x > it->first_visible_x)
20574 /* Increment number of glyphs actually displayed. */
20575 ++it->hpos;
20577 /* Record the maximum and minimum buffer positions
20578 seen so far in glyphs that will be displayed by
20579 this row. */
20580 if (it->bidi_p)
20581 RECORD_MAX_MIN_POS (it);
20583 if (x < it->first_visible_x && !row->reversed_p)
20584 /* Glyph is partially visible, i.e. row starts at
20585 negative X position. Don't do that in R2L
20586 rows, where we arrange to add a right offset to
20587 the line in extend_face_to_end_of_line, by a
20588 suitable change to the stretch glyph that is
20589 the leftmost glyph of the line. */
20590 row->x = x - it->first_visible_x;
20591 /* When the last glyph of an R2L row only fits
20592 partially on the line, we need to set row->x to a
20593 negative offset, so that the leftmost glyph is
20594 the one that is partially visible. But if we are
20595 going to produce the truncation glyph, this will
20596 be taken care of in produce_special_glyphs. */
20597 if (row->reversed_p
20598 && new_x > it->last_visible_x
20599 && !(it->line_wrap == TRUNCATE
20600 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20602 eassert (FRAME_WINDOW_P (it->f));
20603 row->x = it->last_visible_x - new_x;
20606 else
20608 /* Glyph is completely off the left margin of the
20609 window. This should not happen because of the
20610 move_it_in_display_line at the start of this
20611 function, unless the text display area of the
20612 window is empty. */
20613 eassert (it->first_visible_x <= it->last_visible_x);
20616 /* Even if this display element produced no glyphs at all,
20617 we want to record its position. */
20618 if (it->bidi_p && nglyphs == 0)
20619 RECORD_MAX_MIN_POS (it);
20621 row->ascent = max (row->ascent, it->max_ascent);
20622 row->height = max (row->height, it->max_ascent + it->max_descent);
20623 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20624 row->phys_height = max (row->phys_height,
20625 it->max_phys_ascent + it->max_phys_descent);
20626 row->extra_line_spacing = max (row->extra_line_spacing,
20627 it->max_extra_line_spacing);
20629 /* End of this display line if row is continued. */
20630 if (row->continued_p || row->ends_at_zv_p)
20631 break;
20634 at_end_of_line:
20635 /* Is this a line end? If yes, we're also done, after making
20636 sure that a non-default face is extended up to the right
20637 margin of the window. */
20638 if (ITERATOR_AT_END_OF_LINE_P (it))
20640 int used_before = row->used[TEXT_AREA];
20642 row->ends_in_newline_from_string_p = STRINGP (it->object);
20644 /* Add a space at the end of the line that is used to
20645 display the cursor there. */
20646 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20647 append_space_for_newline (it, 0);
20649 /* Extend the face to the end of the line. */
20650 extend_face_to_end_of_line (it);
20652 /* Make sure we have the position. */
20653 if (used_before == 0)
20654 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20656 /* Record the position of the newline, for use in
20657 find_row_edges. */
20658 it->eol_pos = it->current.pos;
20660 /* Consume the line end. This skips over invisible lines. */
20661 set_iterator_to_next (it, 1);
20662 it->continuation_lines_width = 0;
20663 break;
20666 /* Proceed with next display element. Note that this skips
20667 over lines invisible because of selective display. */
20668 set_iterator_to_next (it, 1);
20670 /* If we truncate lines, we are done when the last displayed
20671 glyphs reach past the right margin of the window. */
20672 if (it->line_wrap == TRUNCATE
20673 && ((FRAME_WINDOW_P (it->f)
20674 /* Images are preprocessed in produce_image_glyph such
20675 that they are cropped at the right edge of the
20676 window, so an image glyph will always end exactly at
20677 last_visible_x, even if there's no right fringe. */
20678 && ((row->reversed_p
20679 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20680 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20681 || it->what == IT_IMAGE))
20682 ? (it->current_x >= it->last_visible_x)
20683 : (it->current_x > it->last_visible_x)))
20685 /* Maybe add truncation glyphs. */
20686 if (!FRAME_WINDOW_P (it->f)
20687 || (row->reversed_p
20688 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20689 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20691 int i, n;
20693 if (!row->reversed_p)
20695 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20696 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20697 break;
20699 else
20701 for (i = 0; i < row->used[TEXT_AREA]; i++)
20702 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20703 break;
20704 /* Remove any padding glyphs at the front of ROW, to
20705 make room for the truncation glyphs we will be
20706 adding below. The loop below always inserts at
20707 least one truncation glyph, so also remove the
20708 last glyph added to ROW. */
20709 unproduce_glyphs (it, i + 1);
20710 /* Adjust i for the loop below. */
20711 i = row->used[TEXT_AREA] - (i + 1);
20714 /* produce_special_glyphs overwrites the last glyph, so
20715 we don't want that if we want to keep that last
20716 glyph, which means it's an image. */
20717 if (it->current_x > it->last_visible_x)
20719 it->current_x = x_before;
20720 if (!FRAME_WINDOW_P (it->f))
20722 for (n = row->used[TEXT_AREA]; i < n; ++i)
20724 row->used[TEXT_AREA] = i;
20725 produce_special_glyphs (it, IT_TRUNCATION);
20728 else
20730 row->used[TEXT_AREA] = i;
20731 produce_special_glyphs (it, IT_TRUNCATION);
20733 it->hpos = hpos_before;
20736 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20738 /* Don't truncate if we can overflow newline into fringe. */
20739 if (!get_next_display_element (it))
20741 it->continuation_lines_width = 0;
20742 row->ends_at_zv_p = 1;
20743 row->exact_window_width_line_p = 1;
20744 break;
20746 if (ITERATOR_AT_END_OF_LINE_P (it))
20748 row->exact_window_width_line_p = 1;
20749 goto at_end_of_line;
20751 it->current_x = x_before;
20752 it->hpos = hpos_before;
20755 row->truncated_on_right_p = 1;
20756 it->continuation_lines_width = 0;
20757 reseat_at_next_visible_line_start (it, 0);
20758 /* We insist below that IT's position be at ZV because in
20759 bidi-reordered lines the character at visible line start
20760 might not be the character that follows the newline in
20761 the logical order. */
20762 if (IT_BYTEPOS (*it) > BEG_BYTE)
20763 row->ends_at_zv_p =
20764 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20765 else
20766 row->ends_at_zv_p = false;
20767 break;
20771 if (wrap_data)
20772 bidi_unshelve_cache (wrap_data, 1);
20774 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20775 at the left window margin. */
20776 if (it->first_visible_x
20777 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20779 if (!FRAME_WINDOW_P (it->f)
20780 || (((row->reversed_p
20781 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20782 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20783 /* Don't let insert_left_trunc_glyphs overwrite the
20784 first glyph of the row if it is an image. */
20785 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20786 insert_left_trunc_glyphs (it);
20787 row->truncated_on_left_p = 1;
20790 /* Remember the position at which this line ends.
20792 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20793 cannot be before the call to find_row_edges below, since that is
20794 where these positions are determined. */
20795 row->end = it->current;
20796 if (!it->bidi_p)
20798 row->minpos = row->start.pos;
20799 row->maxpos = row->end.pos;
20801 else
20803 /* ROW->minpos and ROW->maxpos must be the smallest and
20804 `1 + the largest' buffer positions in ROW. But if ROW was
20805 bidi-reordered, these two positions can be anywhere in the
20806 row, so we must determine them now. */
20807 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20810 /* If the start of this line is the overlay arrow-position, then
20811 mark this glyph row as the one containing the overlay arrow.
20812 This is clearly a mess with variable size fonts. It would be
20813 better to let it be displayed like cursors under X. */
20814 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20815 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20816 !NILP (overlay_arrow_string)))
20818 /* Overlay arrow in window redisplay is a fringe bitmap. */
20819 if (STRINGP (overlay_arrow_string))
20821 struct glyph_row *arrow_row
20822 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20823 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20824 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20825 struct glyph *p = row->glyphs[TEXT_AREA];
20826 struct glyph *p2, *end;
20828 /* Copy the arrow glyphs. */
20829 while (glyph < arrow_end)
20830 *p++ = *glyph++;
20832 /* Throw away padding glyphs. */
20833 p2 = p;
20834 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20835 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20836 ++p2;
20837 if (p2 > p)
20839 while (p2 < end)
20840 *p++ = *p2++;
20841 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20844 else
20846 eassert (INTEGERP (overlay_arrow_string));
20847 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20849 overlay_arrow_seen = 1;
20852 /* Highlight trailing whitespace. */
20853 if (!NILP (Vshow_trailing_whitespace))
20854 highlight_trailing_whitespace (it->f, it->glyph_row);
20856 /* Compute pixel dimensions of this line. */
20857 compute_line_metrics (it);
20859 /* Implementation note: No changes in the glyphs of ROW or in their
20860 faces can be done past this point, because compute_line_metrics
20861 computes ROW's hash value and stores it within the glyph_row
20862 structure. */
20864 /* Record whether this row ends inside an ellipsis. */
20865 row->ends_in_ellipsis_p
20866 = (it->method == GET_FROM_DISPLAY_VECTOR
20867 && it->ellipsis_p);
20869 /* Save fringe bitmaps in this row. */
20870 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20871 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20872 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20873 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20875 it->left_user_fringe_bitmap = 0;
20876 it->left_user_fringe_face_id = 0;
20877 it->right_user_fringe_bitmap = 0;
20878 it->right_user_fringe_face_id = 0;
20880 /* Maybe set the cursor. */
20881 cvpos = it->w->cursor.vpos;
20882 if ((cvpos < 0
20883 /* In bidi-reordered rows, keep checking for proper cursor
20884 position even if one has been found already, because buffer
20885 positions in such rows change non-linearly with ROW->VPOS,
20886 when a line is continued. One exception: when we are at ZV,
20887 display cursor on the first suitable glyph row, since all
20888 the empty rows after that also have their position set to ZV. */
20889 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20890 lines' rows is implemented for bidi-reordered rows. */
20891 || (it->bidi_p
20892 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20893 && PT >= MATRIX_ROW_START_CHARPOS (row)
20894 && PT <= MATRIX_ROW_END_CHARPOS (row)
20895 && cursor_row_p (row))
20896 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20898 /* Prepare for the next line. This line starts horizontally at (X
20899 HPOS) = (0 0). Vertical positions are incremented. As a
20900 convenience for the caller, IT->glyph_row is set to the next
20901 row to be used. */
20902 it->current_x = it->hpos = 0;
20903 it->current_y += row->height;
20904 SET_TEXT_POS (it->eol_pos, 0, 0);
20905 ++it->vpos;
20906 ++it->glyph_row;
20907 /* The next row should by default use the same value of the
20908 reversed_p flag as this one. set_iterator_to_next decides when
20909 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20910 the flag accordingly. */
20911 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20912 it->glyph_row->reversed_p = row->reversed_p;
20913 it->start = row->end;
20914 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20916 #undef RECORD_MAX_MIN_POS
20919 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20920 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20921 doc: /* Return paragraph direction at point in BUFFER.
20922 Value is either `left-to-right' or `right-to-left'.
20923 If BUFFER is omitted or nil, it defaults to the current buffer.
20925 Paragraph direction determines how the text in the paragraph is displayed.
20926 In left-to-right paragraphs, text begins at the left margin of the window
20927 and the reading direction is generally left to right. In right-to-left
20928 paragraphs, text begins at the right margin and is read from right to left.
20930 See also `bidi-paragraph-direction'. */)
20931 (Lisp_Object buffer)
20933 struct buffer *buf = current_buffer;
20934 struct buffer *old = buf;
20936 if (! NILP (buffer))
20938 CHECK_BUFFER (buffer);
20939 buf = XBUFFER (buffer);
20942 if (NILP (BVAR (buf, bidi_display_reordering))
20943 || NILP (BVAR (buf, enable_multibyte_characters))
20944 /* When we are loading loadup.el, the character property tables
20945 needed for bidi iteration are not yet available. */
20946 || !NILP (Vpurify_flag))
20947 return Qleft_to_right;
20948 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20949 return BVAR (buf, bidi_paragraph_direction);
20950 else
20952 /* Determine the direction from buffer text. We could try to
20953 use current_matrix if it is up to date, but this seems fast
20954 enough as it is. */
20955 struct bidi_it itb;
20956 ptrdiff_t pos = BUF_PT (buf);
20957 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20958 int c;
20959 void *itb_data = bidi_shelve_cache ();
20961 set_buffer_temp (buf);
20962 /* bidi_paragraph_init finds the base direction of the paragraph
20963 by searching forward from paragraph start. We need the base
20964 direction of the current or _previous_ paragraph, so we need
20965 to make sure we are within that paragraph. To that end, find
20966 the previous non-empty line. */
20967 if (pos >= ZV && pos > BEGV)
20968 DEC_BOTH (pos, bytepos);
20969 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
20970 if (fast_looking_at (trailing_white_space,
20971 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20973 while ((c = FETCH_BYTE (bytepos)) == '\n'
20974 || c == ' ' || c == '\t' || c == '\f')
20976 if (bytepos <= BEGV_BYTE)
20977 break;
20978 bytepos--;
20979 pos--;
20981 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20982 bytepos--;
20984 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20985 itb.paragraph_dir = NEUTRAL_DIR;
20986 itb.string.s = NULL;
20987 itb.string.lstring = Qnil;
20988 itb.string.bufpos = 0;
20989 itb.string.from_disp_str = 0;
20990 itb.string.unibyte = 0;
20991 /* We have no window to use here for ignoring window-specific
20992 overlays. Using NULL for window pointer will cause
20993 compute_display_string_pos to use the current buffer. */
20994 itb.w = NULL;
20995 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20996 bidi_unshelve_cache (itb_data, 0);
20997 set_buffer_temp (old);
20998 switch (itb.paragraph_dir)
21000 case L2R:
21001 return Qleft_to_right;
21002 break;
21003 case R2L:
21004 return Qright_to_left;
21005 break;
21006 default:
21007 emacs_abort ();
21012 DEFUN ("move-point-visually", Fmove_point_visually,
21013 Smove_point_visually, 1, 1, 0,
21014 doc: /* Move point in the visual order in the specified DIRECTION.
21015 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21016 left.
21018 Value is the new character position of point. */)
21019 (Lisp_Object direction)
21021 struct window *w = XWINDOW (selected_window);
21022 struct buffer *b = XBUFFER (w->contents);
21023 struct glyph_row *row;
21024 int dir;
21025 Lisp_Object paragraph_dir;
21027 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21028 (!(ROW)->continued_p \
21029 && INTEGERP ((GLYPH)->object) \
21030 && (GLYPH)->type == CHAR_GLYPH \
21031 && (GLYPH)->u.ch == ' ' \
21032 && (GLYPH)->charpos >= 0 \
21033 && !(GLYPH)->avoid_cursor_p)
21035 CHECK_NUMBER (direction);
21036 dir = XINT (direction);
21037 if (dir > 0)
21038 dir = 1;
21039 else
21040 dir = -1;
21042 /* If current matrix is up-to-date, we can use the information
21043 recorded in the glyphs, at least as long as the goal is on the
21044 screen. */
21045 if (w->window_end_valid
21046 && !windows_or_buffers_changed
21047 && b
21048 && !b->clip_changed
21049 && !b->prevent_redisplay_optimizations_p
21050 && !window_outdated (w)
21051 /* We rely below on the cursor coordinates to be up to date, but
21052 we cannot trust them if some command moved point since the
21053 last complete redisplay. */
21054 && w->last_point == BUF_PT (b)
21055 && w->cursor.vpos >= 0
21056 && w->cursor.vpos < w->current_matrix->nrows
21057 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21059 struct glyph *g = row->glyphs[TEXT_AREA];
21060 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21061 struct glyph *gpt = g + w->cursor.hpos;
21063 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21065 if (BUFFERP (g->object) && g->charpos != PT)
21067 SET_PT (g->charpos);
21068 w->cursor.vpos = -1;
21069 return make_number (PT);
21071 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
21073 ptrdiff_t new_pos;
21075 if (BUFFERP (gpt->object))
21077 new_pos = PT;
21078 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21079 new_pos += (row->reversed_p ? -dir : dir);
21080 else
21081 new_pos -= (row->reversed_p ? -dir : dir);
21083 else if (BUFFERP (g->object))
21084 new_pos = g->charpos;
21085 else
21086 break;
21087 SET_PT (new_pos);
21088 w->cursor.vpos = -1;
21089 return make_number (PT);
21091 else if (ROW_GLYPH_NEWLINE_P (row, g))
21093 /* Glyphs inserted at the end of a non-empty line for
21094 positioning the cursor have zero charpos, so we must
21095 deduce the value of point by other means. */
21096 if (g->charpos > 0)
21097 SET_PT (g->charpos);
21098 else if (row->ends_at_zv_p && PT != ZV)
21099 SET_PT (ZV);
21100 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21101 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21102 else
21103 break;
21104 w->cursor.vpos = -1;
21105 return make_number (PT);
21108 if (g == e || INTEGERP (g->object))
21110 if (row->truncated_on_left_p || row->truncated_on_right_p)
21111 goto simulate_display;
21112 if (!row->reversed_p)
21113 row += dir;
21114 else
21115 row -= dir;
21116 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21117 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21118 goto simulate_display;
21120 if (dir > 0)
21122 if (row->reversed_p && !row->continued_p)
21124 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21125 w->cursor.vpos = -1;
21126 return make_number (PT);
21128 g = row->glyphs[TEXT_AREA];
21129 e = g + row->used[TEXT_AREA];
21130 for ( ; g < e; g++)
21132 if (BUFFERP (g->object)
21133 /* Empty lines have only one glyph, which stands
21134 for the newline, and whose charpos is the
21135 buffer position of the newline. */
21136 || ROW_GLYPH_NEWLINE_P (row, g)
21137 /* When the buffer ends in a newline, the line at
21138 EOB also has one glyph, but its charpos is -1. */
21139 || (row->ends_at_zv_p
21140 && !row->reversed_p
21141 && INTEGERP (g->object)
21142 && g->type == CHAR_GLYPH
21143 && g->u.ch == ' '))
21145 if (g->charpos > 0)
21146 SET_PT (g->charpos);
21147 else if (!row->reversed_p
21148 && row->ends_at_zv_p
21149 && PT != ZV)
21150 SET_PT (ZV);
21151 else
21152 continue;
21153 w->cursor.vpos = -1;
21154 return make_number (PT);
21158 else
21160 if (!row->reversed_p && !row->continued_p)
21162 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21163 w->cursor.vpos = -1;
21164 return make_number (PT);
21166 e = row->glyphs[TEXT_AREA];
21167 g = e + row->used[TEXT_AREA] - 1;
21168 for ( ; g >= e; g--)
21170 if (BUFFERP (g->object)
21171 || (ROW_GLYPH_NEWLINE_P (row, g)
21172 && g->charpos > 0)
21173 /* Empty R2L lines on GUI frames have the buffer
21174 position of the newline stored in the stretch
21175 glyph. */
21176 || g->type == STRETCH_GLYPH
21177 || (row->ends_at_zv_p
21178 && row->reversed_p
21179 && INTEGERP (g->object)
21180 && g->type == CHAR_GLYPH
21181 && g->u.ch == ' '))
21183 if (g->charpos > 0)
21184 SET_PT (g->charpos);
21185 else if (row->reversed_p
21186 && row->ends_at_zv_p
21187 && PT != ZV)
21188 SET_PT (ZV);
21189 else
21190 continue;
21191 w->cursor.vpos = -1;
21192 return make_number (PT);
21199 simulate_display:
21201 /* If we wind up here, we failed to move by using the glyphs, so we
21202 need to simulate display instead. */
21204 if (b)
21205 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21206 else
21207 paragraph_dir = Qleft_to_right;
21208 if (EQ (paragraph_dir, Qright_to_left))
21209 dir = -dir;
21210 if (PT <= BEGV && dir < 0)
21211 xsignal0 (Qbeginning_of_buffer);
21212 else if (PT >= ZV && dir > 0)
21213 xsignal0 (Qend_of_buffer);
21214 else
21216 struct text_pos pt;
21217 struct it it;
21218 int pt_x, target_x, pixel_width, pt_vpos;
21219 bool at_eol_p;
21220 bool overshoot_expected = false;
21221 bool target_is_eol_p = false;
21223 /* Setup the arena. */
21224 SET_TEXT_POS (pt, PT, PT_BYTE);
21225 start_display (&it, w, pt);
21227 if (it.cmp_it.id < 0
21228 && it.method == GET_FROM_STRING
21229 && it.area == TEXT_AREA
21230 && it.string_from_display_prop_p
21231 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21232 overshoot_expected = true;
21234 /* Find the X coordinate of point. We start from the beginning
21235 of this or previous line to make sure we are before point in
21236 the logical order (since the move_it_* functions can only
21237 move forward). */
21238 reseat:
21239 reseat_at_previous_visible_line_start (&it);
21240 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21241 if (IT_CHARPOS (it) != PT)
21243 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21244 -1, -1, -1, MOVE_TO_POS);
21245 /* If we missed point because the character there is
21246 displayed out of a display vector that has more than one
21247 glyph, retry expecting overshoot. */
21248 if (it.method == GET_FROM_DISPLAY_VECTOR
21249 && it.current.dpvec_index > 0
21250 && !overshoot_expected)
21252 overshoot_expected = true;
21253 goto reseat;
21255 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21256 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21258 pt_x = it.current_x;
21259 pt_vpos = it.vpos;
21260 if (dir > 0 || overshoot_expected)
21262 struct glyph_row *row = it.glyph_row;
21264 /* When point is at beginning of line, we don't have
21265 information about the glyph there loaded into struct
21266 it. Calling get_next_display_element fixes that. */
21267 if (pt_x == 0)
21268 get_next_display_element (&it);
21269 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21270 it.glyph_row = NULL;
21271 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21272 it.glyph_row = row;
21273 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21274 it, lest it will become out of sync with it's buffer
21275 position. */
21276 it.current_x = pt_x;
21278 else
21279 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21280 pixel_width = it.pixel_width;
21281 if (overshoot_expected && at_eol_p)
21282 pixel_width = 0;
21283 else if (pixel_width <= 0)
21284 pixel_width = 1;
21286 /* If there's a display string (or something similar) at point,
21287 we are actually at the glyph to the left of point, so we need
21288 to correct the X coordinate. */
21289 if (overshoot_expected)
21291 if (it.bidi_p)
21292 pt_x += pixel_width * it.bidi_it.scan_dir;
21293 else
21294 pt_x += pixel_width;
21297 /* Compute target X coordinate, either to the left or to the
21298 right of point. On TTY frames, all characters have the same
21299 pixel width of 1, so we can use that. On GUI frames we don't
21300 have an easy way of getting at the pixel width of the
21301 character to the left of point, so we use a different method
21302 of getting to that place. */
21303 if (dir > 0)
21304 target_x = pt_x + pixel_width;
21305 else
21306 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21308 /* Target X coordinate could be one line above or below the line
21309 of point, in which case we need to adjust the target X
21310 coordinate. Also, if moving to the left, we need to begin at
21311 the left edge of the point's screen line. */
21312 if (dir < 0)
21314 if (pt_x > 0)
21316 start_display (&it, w, pt);
21317 reseat_at_previous_visible_line_start (&it);
21318 it.current_x = it.current_y = it.hpos = 0;
21319 if (pt_vpos != 0)
21320 move_it_by_lines (&it, pt_vpos);
21322 else
21324 move_it_by_lines (&it, -1);
21325 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21326 target_is_eol_p = true;
21327 /* Under word-wrap, we don't know the x coordinate of
21328 the last character displayed on the previous line,
21329 which immediately precedes the wrap point. To find
21330 out its x coordinate, we try moving to the right
21331 margin of the window, which will stop at the wrap
21332 point, and then reset target_x to point at the
21333 character that precedes the wrap point. This is not
21334 needed on GUI frames, because (see below) there we
21335 move from the left margin one grapheme cluster at a
21336 time, and stop when we hit the wrap point. */
21337 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21339 void *it_data = NULL;
21340 struct it it2;
21342 SAVE_IT (it2, it, it_data);
21343 move_it_in_display_line_to (&it, ZV, target_x,
21344 MOVE_TO_POS | MOVE_TO_X);
21345 /* If we arrived at target_x, that _is_ the last
21346 character on the previous line. */
21347 if (it.current_x != target_x)
21348 target_x = it.current_x - 1;
21349 RESTORE_IT (&it, &it2, it_data);
21353 else
21355 if (at_eol_p
21356 || (target_x >= it.last_visible_x
21357 && it.line_wrap != TRUNCATE))
21359 if (pt_x > 0)
21360 move_it_by_lines (&it, 0);
21361 move_it_by_lines (&it, 1);
21362 target_x = 0;
21366 /* Move to the target X coordinate. */
21367 #ifdef HAVE_WINDOW_SYSTEM
21368 /* On GUI frames, as we don't know the X coordinate of the
21369 character to the left of point, moving point to the left
21370 requires walking, one grapheme cluster at a time, until we
21371 find ourself at a place immediately to the left of the
21372 character at point. */
21373 if (FRAME_WINDOW_P (it.f) && dir < 0)
21375 struct text_pos new_pos;
21376 enum move_it_result rc = MOVE_X_REACHED;
21378 if (it.current_x == 0)
21379 get_next_display_element (&it);
21380 if (it.what == IT_COMPOSITION)
21382 new_pos.charpos = it.cmp_it.charpos;
21383 new_pos.bytepos = -1;
21385 else
21386 new_pos = it.current.pos;
21388 while (it.current_x + it.pixel_width <= target_x
21389 && (rc == MOVE_X_REACHED
21390 /* Under word-wrap, move_it_in_display_line_to
21391 stops at correct coordinates, but sometimes
21392 returns MOVE_POS_MATCH_OR_ZV. */
21393 || (it.line_wrap == WORD_WRAP
21394 && rc == MOVE_POS_MATCH_OR_ZV)))
21396 int new_x = it.current_x + it.pixel_width;
21398 /* For composed characters, we want the position of the
21399 first character in the grapheme cluster (usually, the
21400 composition's base character), whereas it.current
21401 might give us the position of the _last_ one, e.g. if
21402 the composition is rendered in reverse due to bidi
21403 reordering. */
21404 if (it.what == IT_COMPOSITION)
21406 new_pos.charpos = it.cmp_it.charpos;
21407 new_pos.bytepos = -1;
21409 else
21410 new_pos = it.current.pos;
21411 if (new_x == it.current_x)
21412 new_x++;
21413 rc = move_it_in_display_line_to (&it, ZV, new_x,
21414 MOVE_TO_POS | MOVE_TO_X);
21415 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21416 break;
21418 /* The previous position we saw in the loop is the one we
21419 want. */
21420 if (new_pos.bytepos == -1)
21421 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21422 it.current.pos = new_pos;
21424 else
21425 #endif
21426 if (it.current_x != target_x)
21427 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21429 /* When lines are truncated, the above loop will stop at the
21430 window edge. But we want to get to the end of line, even if
21431 it is beyond the window edge; automatic hscroll will then
21432 scroll the window to show point as appropriate. */
21433 if (target_is_eol_p && it.line_wrap == TRUNCATE
21434 && get_next_display_element (&it))
21436 struct text_pos new_pos = it.current.pos;
21438 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21440 set_iterator_to_next (&it, 0);
21441 if (it.method == GET_FROM_BUFFER)
21442 new_pos = it.current.pos;
21443 if (!get_next_display_element (&it))
21444 break;
21447 it.current.pos = new_pos;
21450 /* If we ended up in a display string that covers point, move to
21451 buffer position to the right in the visual order. */
21452 if (dir > 0)
21454 while (IT_CHARPOS (it) == PT)
21456 set_iterator_to_next (&it, 0);
21457 if (!get_next_display_element (&it))
21458 break;
21462 /* Move point to that position. */
21463 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21466 return make_number (PT);
21468 #undef ROW_GLYPH_NEWLINE_P
21472 /***********************************************************************
21473 Menu Bar
21474 ***********************************************************************/
21476 /* Redisplay the menu bar in the frame for window W.
21478 The menu bar of X frames that don't have X toolkit support is
21479 displayed in a special window W->frame->menu_bar_window.
21481 The menu bar of terminal frames is treated specially as far as
21482 glyph matrices are concerned. Menu bar lines are not part of
21483 windows, so the update is done directly on the frame matrix rows
21484 for the menu bar. */
21486 static void
21487 display_menu_bar (struct window *w)
21489 struct frame *f = XFRAME (WINDOW_FRAME (w));
21490 struct it it;
21491 Lisp_Object items;
21492 int i;
21494 /* Don't do all this for graphical frames. */
21495 #ifdef HAVE_NTGUI
21496 if (FRAME_W32_P (f))
21497 return;
21498 #endif
21499 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21500 if (FRAME_X_P (f))
21501 return;
21502 #endif
21504 #ifdef HAVE_NS
21505 if (FRAME_NS_P (f))
21506 return;
21507 #endif /* HAVE_NS */
21509 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21510 eassert (!FRAME_WINDOW_P (f));
21511 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21512 it.first_visible_x = 0;
21513 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21514 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21515 if (FRAME_WINDOW_P (f))
21517 /* Menu bar lines are displayed in the desired matrix of the
21518 dummy window menu_bar_window. */
21519 struct window *menu_w;
21520 menu_w = XWINDOW (f->menu_bar_window);
21521 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21522 MENU_FACE_ID);
21523 it.first_visible_x = 0;
21524 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21526 else
21527 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21529 /* This is a TTY frame, i.e. character hpos/vpos are used as
21530 pixel x/y. */
21531 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21532 MENU_FACE_ID);
21533 it.first_visible_x = 0;
21534 it.last_visible_x = FRAME_COLS (f);
21537 /* FIXME: This should be controlled by a user option. See the
21538 comments in redisplay_tool_bar and display_mode_line about
21539 this. */
21540 it.paragraph_embedding = L2R;
21542 /* Clear all rows of the menu bar. */
21543 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21545 struct glyph_row *row = it.glyph_row + i;
21546 clear_glyph_row (row);
21547 row->enabled_p = true;
21548 row->full_width_p = 1;
21549 row->reversed_p = false;
21552 /* Display all items of the menu bar. */
21553 items = FRAME_MENU_BAR_ITEMS (it.f);
21554 for (i = 0; i < ASIZE (items); i += 4)
21556 Lisp_Object string;
21558 /* Stop at nil string. */
21559 string = AREF (items, i + 1);
21560 if (NILP (string))
21561 break;
21563 /* Remember where item was displayed. */
21564 ASET (items, i + 3, make_number (it.hpos));
21566 /* Display the item, pad with one space. */
21567 if (it.current_x < it.last_visible_x)
21568 display_string (NULL, string, Qnil, 0, 0, &it,
21569 SCHARS (string) + 1, 0, 0, -1);
21572 /* Fill out the line with spaces. */
21573 if (it.current_x < it.last_visible_x)
21574 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21576 /* Compute the total height of the lines. */
21577 compute_line_metrics (&it);
21580 /* Deep copy of a glyph row, including the glyphs. */
21581 static void
21582 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21584 struct glyph *pointers[1 + LAST_AREA];
21585 int to_used = to->used[TEXT_AREA];
21587 /* Save glyph pointers of TO. */
21588 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21590 /* Do a structure assignment. */
21591 *to = *from;
21593 /* Restore original glyph pointers of TO. */
21594 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21596 /* Copy the glyphs. */
21597 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21598 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21600 /* If we filled only part of the TO row, fill the rest with
21601 space_glyph (which will display as empty space). */
21602 if (to_used > from->used[TEXT_AREA])
21603 fill_up_frame_row_with_spaces (to, to_used);
21606 /* Display one menu item on a TTY, by overwriting the glyphs in the
21607 frame F's desired glyph matrix with glyphs produced from the menu
21608 item text. Called from term.c to display TTY drop-down menus one
21609 item at a time.
21611 ITEM_TEXT is the menu item text as a C string.
21613 FACE_ID is the face ID to be used for this menu item. FACE_ID
21614 could specify one of 3 faces: a face for an enabled item, a face
21615 for a disabled item, or a face for a selected item.
21617 X and Y are coordinates of the first glyph in the frame's desired
21618 matrix to be overwritten by the menu item. Since this is a TTY, Y
21619 is the zero-based number of the glyph row and X is the zero-based
21620 glyph number in the row, starting from left, where to start
21621 displaying the item.
21623 SUBMENU non-zero means this menu item drops down a submenu, which
21624 should be indicated by displaying a proper visual cue after the
21625 item text. */
21627 void
21628 display_tty_menu_item (const char *item_text, int width, int face_id,
21629 int x, int y, int submenu)
21631 struct it it;
21632 struct frame *f = SELECTED_FRAME ();
21633 struct window *w = XWINDOW (f->selected_window);
21634 int saved_used, saved_truncated, saved_width, saved_reversed;
21635 struct glyph_row *row;
21636 size_t item_len = strlen (item_text);
21638 eassert (FRAME_TERMCAP_P (f));
21640 /* Don't write beyond the matrix's last row. This can happen for
21641 TTY screens that are not high enough to show the entire menu.
21642 (This is actually a bit of defensive programming, as
21643 tty_menu_display already limits the number of menu items to one
21644 less than the number of screen lines.) */
21645 if (y >= f->desired_matrix->nrows)
21646 return;
21648 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
21649 it.first_visible_x = 0;
21650 it.last_visible_x = FRAME_COLS (f) - 1;
21651 row = it.glyph_row;
21652 /* Start with the row contents from the current matrix. */
21653 deep_copy_glyph_row (row, f->current_matrix->rows + y);
21654 saved_width = row->full_width_p;
21655 row->full_width_p = 1;
21656 saved_reversed = row->reversed_p;
21657 row->reversed_p = 0;
21658 row->enabled_p = true;
21660 /* Arrange for the menu item glyphs to start at (X,Y) and have the
21661 desired face. */
21662 eassert (x < f->desired_matrix->matrix_w);
21663 it.current_x = it.hpos = x;
21664 it.current_y = it.vpos = y;
21665 saved_used = row->used[TEXT_AREA];
21666 saved_truncated = row->truncated_on_right_p;
21667 row->used[TEXT_AREA] = x;
21668 it.face_id = face_id;
21669 it.line_wrap = TRUNCATE;
21671 /* FIXME: This should be controlled by a user option. See the
21672 comments in redisplay_tool_bar and display_mode_line about this.
21673 Also, if paragraph_embedding could ever be R2L, changes will be
21674 needed to avoid shifting to the right the row characters in
21675 term.c:append_glyph. */
21676 it.paragraph_embedding = L2R;
21678 /* Pad with a space on the left. */
21679 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
21680 width--;
21681 /* Display the menu item, pad with spaces to WIDTH. */
21682 if (submenu)
21684 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21685 item_len, 0, FRAME_COLS (f) - 1, -1);
21686 width -= item_len;
21687 /* Indicate with " >" that there's a submenu. */
21688 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
21689 FRAME_COLS (f) - 1, -1);
21691 else
21692 display_string (item_text, Qnil, Qnil, 0, 0, &it,
21693 width, 0, FRAME_COLS (f) - 1, -1);
21695 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
21696 row->truncated_on_right_p = saved_truncated;
21697 row->hash = row_hash (row);
21698 row->full_width_p = saved_width;
21699 row->reversed_p = saved_reversed;
21702 /***********************************************************************
21703 Mode Line
21704 ***********************************************************************/
21706 /* Redisplay mode lines in the window tree whose root is WINDOW. If
21707 FORCE is non-zero, redisplay mode lines unconditionally.
21708 Otherwise, redisplay only mode lines that are garbaged. Value is
21709 the number of windows whose mode lines were redisplayed. */
21711 static int
21712 redisplay_mode_lines (Lisp_Object window, bool force)
21714 int nwindows = 0;
21716 while (!NILP (window))
21718 struct window *w = XWINDOW (window);
21720 if (WINDOWP (w->contents))
21721 nwindows += redisplay_mode_lines (w->contents, force);
21722 else if (force
21723 || FRAME_GARBAGED_P (XFRAME (w->frame))
21724 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
21726 struct text_pos lpoint;
21727 struct buffer *old = current_buffer;
21729 /* Set the window's buffer for the mode line display. */
21730 SET_TEXT_POS (lpoint, PT, PT_BYTE);
21731 set_buffer_internal_1 (XBUFFER (w->contents));
21733 /* Point refers normally to the selected window. For any
21734 other window, set up appropriate value. */
21735 if (!EQ (window, selected_window))
21737 struct text_pos pt;
21739 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
21740 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
21743 /* Display mode lines. */
21744 clear_glyph_matrix (w->desired_matrix);
21745 if (display_mode_lines (w))
21746 ++nwindows;
21748 /* Restore old settings. */
21749 set_buffer_internal_1 (old);
21750 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
21753 window = w->next;
21756 return nwindows;
21760 /* Display the mode and/or header line of window W. Value is the
21761 sum number of mode lines and header lines displayed. */
21763 static int
21764 display_mode_lines (struct window *w)
21766 Lisp_Object old_selected_window = selected_window;
21767 Lisp_Object old_selected_frame = selected_frame;
21768 Lisp_Object new_frame = w->frame;
21769 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
21770 int n = 0;
21772 selected_frame = new_frame;
21773 /* FIXME: If we were to allow the mode-line's computation changing the buffer
21774 or window's point, then we'd need select_window_1 here as well. */
21775 XSETWINDOW (selected_window, w);
21776 XFRAME (new_frame)->selected_window = selected_window;
21778 /* These will be set while the mode line specs are processed. */
21779 line_number_displayed = 0;
21780 w->column_number_displayed = -1;
21782 if (WINDOW_WANTS_MODELINE_P (w))
21784 struct window *sel_w = XWINDOW (old_selected_window);
21786 /* Select mode line face based on the real selected window. */
21787 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
21788 BVAR (current_buffer, mode_line_format));
21789 ++n;
21792 if (WINDOW_WANTS_HEADER_LINE_P (w))
21794 display_mode_line (w, HEADER_LINE_FACE_ID,
21795 BVAR (current_buffer, header_line_format));
21796 ++n;
21799 XFRAME (new_frame)->selected_window = old_frame_selected_window;
21800 selected_frame = old_selected_frame;
21801 selected_window = old_selected_window;
21802 if (n > 0)
21803 w->must_be_updated_p = true;
21804 return n;
21808 /* Display mode or header line of window W. FACE_ID specifies which
21809 line to display; it is either MODE_LINE_FACE_ID or
21810 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
21811 display. Value is the pixel height of the mode/header line
21812 displayed. */
21814 static int
21815 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
21817 struct it it;
21818 struct face *face;
21819 ptrdiff_t count = SPECPDL_INDEX ();
21821 init_iterator (&it, w, -1, -1, NULL, face_id);
21822 /* Don't extend on a previously drawn mode-line.
21823 This may happen if called from pos_visible_p. */
21824 it.glyph_row->enabled_p = false;
21825 prepare_desired_row (w, it.glyph_row, true);
21827 it.glyph_row->mode_line_p = 1;
21829 /* FIXME: This should be controlled by a user option. But
21830 supporting such an option is not trivial, since the mode line is
21831 made up of many separate strings. */
21832 it.paragraph_embedding = L2R;
21834 record_unwind_protect (unwind_format_mode_line,
21835 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
21837 mode_line_target = MODE_LINE_DISPLAY;
21839 /* Temporarily make frame's keyboard the current kboard so that
21840 kboard-local variables in the mode_line_format will get the right
21841 values. */
21842 push_kboard (FRAME_KBOARD (it.f));
21843 record_unwind_save_match_data ();
21844 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21845 pop_kboard ();
21847 unbind_to (count, Qnil);
21849 /* Fill up with spaces. */
21850 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
21852 compute_line_metrics (&it);
21853 it.glyph_row->full_width_p = 1;
21854 it.glyph_row->continued_p = 0;
21855 it.glyph_row->truncated_on_left_p = 0;
21856 it.glyph_row->truncated_on_right_p = 0;
21858 /* Make a 3D mode-line have a shadow at its right end. */
21859 face = FACE_FROM_ID (it.f, face_id);
21860 extend_face_to_end_of_line (&it);
21861 if (face->box != FACE_NO_BOX)
21863 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
21864 + it.glyph_row->used[TEXT_AREA] - 1);
21865 last->right_box_line_p = 1;
21868 return it.glyph_row->height;
21871 /* Move element ELT in LIST to the front of LIST.
21872 Return the updated list. */
21874 static Lisp_Object
21875 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
21877 register Lisp_Object tail, prev;
21878 register Lisp_Object tem;
21880 tail = list;
21881 prev = Qnil;
21882 while (CONSP (tail))
21884 tem = XCAR (tail);
21886 if (EQ (elt, tem))
21888 /* Splice out the link TAIL. */
21889 if (NILP (prev))
21890 list = XCDR (tail);
21891 else
21892 Fsetcdr (prev, XCDR (tail));
21894 /* Now make it the first. */
21895 Fsetcdr (tail, list);
21896 return tail;
21898 else
21899 prev = tail;
21900 tail = XCDR (tail);
21901 QUIT;
21904 /* Not found--return unchanged LIST. */
21905 return list;
21908 /* Contribute ELT to the mode line for window IT->w. How it
21909 translates into text depends on its data type.
21911 IT describes the display environment in which we display, as usual.
21913 DEPTH is the depth in recursion. It is used to prevent
21914 infinite recursion here.
21916 FIELD_WIDTH is the number of characters the display of ELT should
21917 occupy in the mode line, and PRECISION is the maximum number of
21918 characters to display from ELT's representation. See
21919 display_string for details.
21921 Returns the hpos of the end of the text generated by ELT.
21923 PROPS is a property list to add to any string we encounter.
21925 If RISKY is nonzero, remove (disregard) any properties in any string
21926 we encounter, and ignore :eval and :propertize.
21928 The global variable `mode_line_target' determines whether the
21929 output is passed to `store_mode_line_noprop',
21930 `store_mode_line_string', or `display_string'. */
21932 static int
21933 display_mode_element (struct it *it, int depth, int field_width, int precision,
21934 Lisp_Object elt, Lisp_Object props, int risky)
21936 int n = 0, field, prec;
21937 int literal = 0;
21939 tail_recurse:
21940 if (depth > 100)
21941 elt = build_string ("*too-deep*");
21943 depth++;
21945 switch (XTYPE (elt))
21947 case Lisp_String:
21949 /* A string: output it and check for %-constructs within it. */
21950 unsigned char c;
21951 ptrdiff_t offset = 0;
21953 if (SCHARS (elt) > 0
21954 && (!NILP (props) || risky))
21956 Lisp_Object oprops, aelt;
21957 oprops = Ftext_properties_at (make_number (0), elt);
21959 /* If the starting string's properties are not what
21960 we want, translate the string. Also, if the string
21961 is risky, do that anyway. */
21963 if (NILP (Fequal (props, oprops)) || risky)
21965 /* If the starting string has properties,
21966 merge the specified ones onto the existing ones. */
21967 if (! NILP (oprops) && !risky)
21969 Lisp_Object tem;
21971 oprops = Fcopy_sequence (oprops);
21972 tem = props;
21973 while (CONSP (tem))
21975 oprops = Fplist_put (oprops, XCAR (tem),
21976 XCAR (XCDR (tem)));
21977 tem = XCDR (XCDR (tem));
21979 props = oprops;
21982 aelt = Fassoc (elt, mode_line_proptrans_alist);
21983 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
21985 /* AELT is what we want. Move it to the front
21986 without consing. */
21987 elt = XCAR (aelt);
21988 mode_line_proptrans_alist
21989 = move_elt_to_front (aelt, mode_line_proptrans_alist);
21991 else
21993 Lisp_Object tem;
21995 /* If AELT has the wrong props, it is useless.
21996 so get rid of it. */
21997 if (! NILP (aelt))
21998 mode_line_proptrans_alist
21999 = Fdelq (aelt, mode_line_proptrans_alist);
22001 elt = Fcopy_sequence (elt);
22002 Fset_text_properties (make_number (0), Flength (elt),
22003 props, elt);
22004 /* Add this item to mode_line_proptrans_alist. */
22005 mode_line_proptrans_alist
22006 = Fcons (Fcons (elt, props),
22007 mode_line_proptrans_alist);
22008 /* Truncate mode_line_proptrans_alist
22009 to at most 50 elements. */
22010 tem = Fnthcdr (make_number (50),
22011 mode_line_proptrans_alist);
22012 if (! NILP (tem))
22013 XSETCDR (tem, Qnil);
22018 offset = 0;
22020 if (literal)
22022 prec = precision - n;
22023 switch (mode_line_target)
22025 case MODE_LINE_NOPROP:
22026 case MODE_LINE_TITLE:
22027 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22028 break;
22029 case MODE_LINE_STRING:
22030 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22031 break;
22032 case MODE_LINE_DISPLAY:
22033 n += display_string (NULL, elt, Qnil, 0, 0, it,
22034 0, prec, 0, STRING_MULTIBYTE (elt));
22035 break;
22038 break;
22041 /* Handle the non-literal case. */
22043 while ((precision <= 0 || n < precision)
22044 && SREF (elt, offset) != 0
22045 && (mode_line_target != MODE_LINE_DISPLAY
22046 || it->current_x < it->last_visible_x))
22048 ptrdiff_t last_offset = offset;
22050 /* Advance to end of string or next format specifier. */
22051 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22054 if (offset - 1 != last_offset)
22056 ptrdiff_t nchars, nbytes;
22058 /* Output to end of string or up to '%'. Field width
22059 is length of string. Don't output more than
22060 PRECISION allows us. */
22061 offset--;
22063 prec = c_string_width (SDATA (elt) + last_offset,
22064 offset - last_offset, precision - n,
22065 &nchars, &nbytes);
22067 switch (mode_line_target)
22069 case MODE_LINE_NOPROP:
22070 case MODE_LINE_TITLE:
22071 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22072 break;
22073 case MODE_LINE_STRING:
22075 ptrdiff_t bytepos = last_offset;
22076 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22077 ptrdiff_t endpos = (precision <= 0
22078 ? string_byte_to_char (elt, offset)
22079 : charpos + nchars);
22081 n += store_mode_line_string (NULL,
22082 Fsubstring (elt, make_number (charpos),
22083 make_number (endpos)),
22084 0, 0, 0, Qnil);
22086 break;
22087 case MODE_LINE_DISPLAY:
22089 ptrdiff_t bytepos = last_offset;
22090 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22092 if (precision <= 0)
22093 nchars = string_byte_to_char (elt, offset) - charpos;
22094 n += display_string (NULL, elt, Qnil, 0, charpos,
22095 it, 0, nchars, 0,
22096 STRING_MULTIBYTE (elt));
22098 break;
22101 else /* c == '%' */
22103 ptrdiff_t percent_position = offset;
22105 /* Get the specified minimum width. Zero means
22106 don't pad. */
22107 field = 0;
22108 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22109 field = field * 10 + c - '0';
22111 /* Don't pad beyond the total padding allowed. */
22112 if (field_width - n > 0 && field > field_width - n)
22113 field = field_width - n;
22115 /* Note that either PRECISION <= 0 or N < PRECISION. */
22116 prec = precision - n;
22118 if (c == 'M')
22119 n += display_mode_element (it, depth, field, prec,
22120 Vglobal_mode_string, props,
22121 risky);
22122 else if (c != 0)
22124 bool multibyte;
22125 ptrdiff_t bytepos, charpos;
22126 const char *spec;
22127 Lisp_Object string;
22129 bytepos = percent_position;
22130 charpos = (STRING_MULTIBYTE (elt)
22131 ? string_byte_to_char (elt, bytepos)
22132 : bytepos);
22133 spec = decode_mode_spec (it->w, c, field, &string);
22134 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22136 switch (mode_line_target)
22138 case MODE_LINE_NOPROP:
22139 case MODE_LINE_TITLE:
22140 n += store_mode_line_noprop (spec, field, prec);
22141 break;
22142 case MODE_LINE_STRING:
22144 Lisp_Object tem = build_string (spec);
22145 props = Ftext_properties_at (make_number (charpos), elt);
22146 /* Should only keep face property in props */
22147 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22149 break;
22150 case MODE_LINE_DISPLAY:
22152 int nglyphs_before, nwritten;
22154 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22155 nwritten = display_string (spec, string, elt,
22156 charpos, 0, it,
22157 field, prec, 0,
22158 multibyte);
22160 /* Assign to the glyphs written above the
22161 string where the `%x' came from, position
22162 of the `%'. */
22163 if (nwritten > 0)
22165 struct glyph *glyph
22166 = (it->glyph_row->glyphs[TEXT_AREA]
22167 + nglyphs_before);
22168 int i;
22170 for (i = 0; i < nwritten; ++i)
22172 glyph[i].object = elt;
22173 glyph[i].charpos = charpos;
22176 n += nwritten;
22179 break;
22182 else /* c == 0 */
22183 break;
22187 break;
22189 case Lisp_Symbol:
22190 /* A symbol: process the value of the symbol recursively
22191 as if it appeared here directly. Avoid error if symbol void.
22192 Special case: if value of symbol is a string, output the string
22193 literally. */
22195 register Lisp_Object tem;
22197 /* If the variable is not marked as risky to set
22198 then its contents are risky to use. */
22199 if (NILP (Fget (elt, Qrisky_local_variable)))
22200 risky = 1;
22202 tem = Fboundp (elt);
22203 if (!NILP (tem))
22205 tem = Fsymbol_value (elt);
22206 /* If value is a string, output that string literally:
22207 don't check for % within it. */
22208 if (STRINGP (tem))
22209 literal = 1;
22211 if (!EQ (tem, elt))
22213 /* Give up right away for nil or t. */
22214 elt = tem;
22215 goto tail_recurse;
22219 break;
22221 case Lisp_Cons:
22223 register Lisp_Object car, tem;
22225 /* A cons cell: five distinct cases.
22226 If first element is :eval or :propertize, do something special.
22227 If first element is a string or a cons, process all the elements
22228 and effectively concatenate them.
22229 If first element is a negative number, truncate displaying cdr to
22230 at most that many characters. If positive, pad (with spaces)
22231 to at least that many characters.
22232 If first element is a symbol, process the cadr or caddr recursively
22233 according to whether the symbol's value is non-nil or nil. */
22234 car = XCAR (elt);
22235 if (EQ (car, QCeval))
22237 /* An element of the form (:eval FORM) means evaluate FORM
22238 and use the result as mode line elements. */
22240 if (risky)
22241 break;
22243 if (CONSP (XCDR (elt)))
22245 Lisp_Object spec;
22246 spec = safe__eval (true, XCAR (XCDR (elt)));
22247 n += display_mode_element (it, depth, field_width - n,
22248 precision - n, spec, props,
22249 risky);
22252 else if (EQ (car, QCpropertize))
22254 /* An element of the form (:propertize ELT PROPS...)
22255 means display ELT but applying properties PROPS. */
22257 if (risky)
22258 break;
22260 if (CONSP (XCDR (elt)))
22261 n += display_mode_element (it, depth, field_width - n,
22262 precision - n, XCAR (XCDR (elt)),
22263 XCDR (XCDR (elt)), risky);
22265 else if (SYMBOLP (car))
22267 tem = Fboundp (car);
22268 elt = XCDR (elt);
22269 if (!CONSP (elt))
22270 goto invalid;
22271 /* elt is now the cdr, and we know it is a cons cell.
22272 Use its car if CAR has a non-nil value. */
22273 if (!NILP (tem))
22275 tem = Fsymbol_value (car);
22276 if (!NILP (tem))
22278 elt = XCAR (elt);
22279 goto tail_recurse;
22282 /* Symbol's value is nil (or symbol is unbound)
22283 Get the cddr of the original list
22284 and if possible find the caddr and use that. */
22285 elt = XCDR (elt);
22286 if (NILP (elt))
22287 break;
22288 else if (!CONSP (elt))
22289 goto invalid;
22290 elt = XCAR (elt);
22291 goto tail_recurse;
22293 else if (INTEGERP (car))
22295 register int lim = XINT (car);
22296 elt = XCDR (elt);
22297 if (lim < 0)
22299 /* Negative int means reduce maximum width. */
22300 if (precision <= 0)
22301 precision = -lim;
22302 else
22303 precision = min (precision, -lim);
22305 else if (lim > 0)
22307 /* Padding specified. Don't let it be more than
22308 current maximum. */
22309 if (precision > 0)
22310 lim = min (precision, lim);
22312 /* If that's more padding than already wanted, queue it.
22313 But don't reduce padding already specified even if
22314 that is beyond the current truncation point. */
22315 field_width = max (lim, field_width);
22317 goto tail_recurse;
22319 else if (STRINGP (car) || CONSP (car))
22321 Lisp_Object halftail = elt;
22322 int len = 0;
22324 while (CONSP (elt)
22325 && (precision <= 0 || n < precision))
22327 n += display_mode_element (it, depth,
22328 /* Do padding only after the last
22329 element in the list. */
22330 (! CONSP (XCDR (elt))
22331 ? field_width - n
22332 : 0),
22333 precision - n, XCAR (elt),
22334 props, risky);
22335 elt = XCDR (elt);
22336 len++;
22337 if ((len & 1) == 0)
22338 halftail = XCDR (halftail);
22339 /* Check for cycle. */
22340 if (EQ (halftail, elt))
22341 break;
22345 break;
22347 default:
22348 invalid:
22349 elt = build_string ("*invalid*");
22350 goto tail_recurse;
22353 /* Pad to FIELD_WIDTH. */
22354 if (field_width > 0 && n < field_width)
22356 switch (mode_line_target)
22358 case MODE_LINE_NOPROP:
22359 case MODE_LINE_TITLE:
22360 n += store_mode_line_noprop ("", field_width - n, 0);
22361 break;
22362 case MODE_LINE_STRING:
22363 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22364 break;
22365 case MODE_LINE_DISPLAY:
22366 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22367 0, 0, 0);
22368 break;
22372 return n;
22375 /* Store a mode-line string element in mode_line_string_list.
22377 If STRING is non-null, display that C string. Otherwise, the Lisp
22378 string LISP_STRING is displayed.
22380 FIELD_WIDTH is the minimum number of output glyphs to produce.
22381 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22382 with spaces. FIELD_WIDTH <= 0 means don't pad.
22384 PRECISION is the maximum number of characters to output from
22385 STRING. PRECISION <= 0 means don't truncate the string.
22387 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22388 properties to the string.
22390 PROPS are the properties to add to the string.
22391 The mode_line_string_face face property is always added to the string.
22394 static int
22395 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22396 int field_width, int precision, Lisp_Object props)
22398 ptrdiff_t len;
22399 int n = 0;
22401 if (string != NULL)
22403 len = strlen (string);
22404 if (precision > 0 && len > precision)
22405 len = precision;
22406 lisp_string = make_string (string, len);
22407 if (NILP (props))
22408 props = mode_line_string_face_prop;
22409 else if (!NILP (mode_line_string_face))
22411 Lisp_Object face = Fplist_get (props, Qface);
22412 props = Fcopy_sequence (props);
22413 if (NILP (face))
22414 face = mode_line_string_face;
22415 else
22416 face = list2 (face, mode_line_string_face);
22417 props = Fplist_put (props, Qface, face);
22419 Fadd_text_properties (make_number (0), make_number (len),
22420 props, lisp_string);
22422 else
22424 len = XFASTINT (Flength (lisp_string));
22425 if (precision > 0 && len > precision)
22427 len = precision;
22428 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22429 precision = -1;
22431 if (!NILP (mode_line_string_face))
22433 Lisp_Object face;
22434 if (NILP (props))
22435 props = Ftext_properties_at (make_number (0), lisp_string);
22436 face = Fplist_get (props, Qface);
22437 if (NILP (face))
22438 face = mode_line_string_face;
22439 else
22440 face = list2 (face, mode_line_string_face);
22441 props = list2 (Qface, face);
22442 if (copy_string)
22443 lisp_string = Fcopy_sequence (lisp_string);
22445 if (!NILP (props))
22446 Fadd_text_properties (make_number (0), make_number (len),
22447 props, lisp_string);
22450 if (len > 0)
22452 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22453 n += len;
22456 if (field_width > len)
22458 field_width -= len;
22459 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22460 if (!NILP (props))
22461 Fadd_text_properties (make_number (0), make_number (field_width),
22462 props, lisp_string);
22463 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22464 n += field_width;
22467 return n;
22471 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22472 1, 4, 0,
22473 doc: /* Format a string out of a mode line format specification.
22474 First arg FORMAT specifies the mode line format (see `mode-line-format'
22475 for details) to use.
22477 By default, the format is evaluated for the currently selected window.
22479 Optional second arg FACE specifies the face property to put on all
22480 characters for which no face is specified. The value nil means the
22481 default face. The value t means whatever face the window's mode line
22482 currently uses (either `mode-line' or `mode-line-inactive',
22483 depending on whether the window is the selected window or not).
22484 An integer value means the value string has no text
22485 properties.
22487 Optional third and fourth args WINDOW and BUFFER specify the window
22488 and buffer to use as the context for the formatting (defaults
22489 are the selected window and the WINDOW's buffer). */)
22490 (Lisp_Object format, Lisp_Object face,
22491 Lisp_Object window, Lisp_Object buffer)
22493 struct it it;
22494 int len;
22495 struct window *w;
22496 struct buffer *old_buffer = NULL;
22497 int face_id;
22498 int no_props = INTEGERP (face);
22499 ptrdiff_t count = SPECPDL_INDEX ();
22500 Lisp_Object str;
22501 int string_start = 0;
22503 w = decode_any_window (window);
22504 XSETWINDOW (window, w);
22506 if (NILP (buffer))
22507 buffer = w->contents;
22508 CHECK_BUFFER (buffer);
22510 /* Make formatting the modeline a non-op when noninteractive, otherwise
22511 there will be problems later caused by a partially initialized frame. */
22512 if (NILP (format) || noninteractive)
22513 return empty_unibyte_string;
22515 if (no_props)
22516 face = Qnil;
22518 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22519 : EQ (face, Qt) ? (EQ (window, selected_window)
22520 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22521 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22522 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22523 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22524 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22525 : DEFAULT_FACE_ID;
22527 old_buffer = current_buffer;
22529 /* Save things including mode_line_proptrans_alist,
22530 and set that to nil so that we don't alter the outer value. */
22531 record_unwind_protect (unwind_format_mode_line,
22532 format_mode_line_unwind_data
22533 (XFRAME (WINDOW_FRAME (w)),
22534 old_buffer, selected_window, 1));
22535 mode_line_proptrans_alist = Qnil;
22537 Fselect_window (window, Qt);
22538 set_buffer_internal_1 (XBUFFER (buffer));
22540 init_iterator (&it, w, -1, -1, NULL, face_id);
22542 if (no_props)
22544 mode_line_target = MODE_LINE_NOPROP;
22545 mode_line_string_face_prop = Qnil;
22546 mode_line_string_list = Qnil;
22547 string_start = MODE_LINE_NOPROP_LEN (0);
22549 else
22551 mode_line_target = MODE_LINE_STRING;
22552 mode_line_string_list = Qnil;
22553 mode_line_string_face = face;
22554 mode_line_string_face_prop
22555 = NILP (face) ? Qnil : list2 (Qface, face);
22558 push_kboard (FRAME_KBOARD (it.f));
22559 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22560 pop_kboard ();
22562 if (no_props)
22564 len = MODE_LINE_NOPROP_LEN (string_start);
22565 str = make_string (mode_line_noprop_buf + string_start, len);
22567 else
22569 mode_line_string_list = Fnreverse (mode_line_string_list);
22570 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22571 empty_unibyte_string);
22574 unbind_to (count, Qnil);
22575 return str;
22578 /* Write a null-terminated, right justified decimal representation of
22579 the positive integer D to BUF using a minimal field width WIDTH. */
22581 static void
22582 pint2str (register char *buf, register int width, register ptrdiff_t d)
22584 register char *p = buf;
22586 if (d <= 0)
22587 *p++ = '0';
22588 else
22590 while (d > 0)
22592 *p++ = d % 10 + '0';
22593 d /= 10;
22597 for (width -= (int) (p - buf); width > 0; --width)
22598 *p++ = ' ';
22599 *p-- = '\0';
22600 while (p > buf)
22602 d = *buf;
22603 *buf++ = *p;
22604 *p-- = d;
22608 /* Write a null-terminated, right justified decimal and "human
22609 readable" representation of the nonnegative integer D to BUF using
22610 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22612 static const char power_letter[] =
22614 0, /* no letter */
22615 'k', /* kilo */
22616 'M', /* mega */
22617 'G', /* giga */
22618 'T', /* tera */
22619 'P', /* peta */
22620 'E', /* exa */
22621 'Z', /* zetta */
22622 'Y' /* yotta */
22625 static void
22626 pint2hrstr (char *buf, int width, ptrdiff_t d)
22628 /* We aim to represent the nonnegative integer D as
22629 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22630 ptrdiff_t quotient = d;
22631 int remainder = 0;
22632 /* -1 means: do not use TENTHS. */
22633 int tenths = -1;
22634 int exponent = 0;
22636 /* Length of QUOTIENT.TENTHS as a string. */
22637 int length;
22639 char * psuffix;
22640 char * p;
22642 if (quotient >= 1000)
22644 /* Scale to the appropriate EXPONENT. */
22647 remainder = quotient % 1000;
22648 quotient /= 1000;
22649 exponent++;
22651 while (quotient >= 1000);
22653 /* Round to nearest and decide whether to use TENTHS or not. */
22654 if (quotient <= 9)
22656 tenths = remainder / 100;
22657 if (remainder % 100 >= 50)
22659 if (tenths < 9)
22660 tenths++;
22661 else
22663 quotient++;
22664 if (quotient == 10)
22665 tenths = -1;
22666 else
22667 tenths = 0;
22671 else
22672 if (remainder >= 500)
22674 if (quotient < 999)
22675 quotient++;
22676 else
22678 quotient = 1;
22679 exponent++;
22680 tenths = 0;
22685 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
22686 if (tenths == -1 && quotient <= 99)
22687 if (quotient <= 9)
22688 length = 1;
22689 else
22690 length = 2;
22691 else
22692 length = 3;
22693 p = psuffix = buf + max (width, length);
22695 /* Print EXPONENT. */
22696 *psuffix++ = power_letter[exponent];
22697 *psuffix = '\0';
22699 /* Print TENTHS. */
22700 if (tenths >= 0)
22702 *--p = '0' + tenths;
22703 *--p = '.';
22706 /* Print QUOTIENT. */
22709 int digit = quotient % 10;
22710 *--p = '0' + digit;
22712 while ((quotient /= 10) != 0);
22714 /* Print leading spaces. */
22715 while (buf < p)
22716 *--p = ' ';
22719 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
22720 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
22721 type of CODING_SYSTEM. Return updated pointer into BUF. */
22723 static unsigned char invalid_eol_type[] = "(*invalid*)";
22725 static char *
22726 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
22728 Lisp_Object val;
22729 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
22730 const unsigned char *eol_str;
22731 int eol_str_len;
22732 /* The EOL conversion we are using. */
22733 Lisp_Object eoltype;
22735 val = CODING_SYSTEM_SPEC (coding_system);
22736 eoltype = Qnil;
22738 if (!VECTORP (val)) /* Not yet decided. */
22740 *buf++ = multibyte ? '-' : ' ';
22741 if (eol_flag)
22742 eoltype = eol_mnemonic_undecided;
22743 /* Don't mention EOL conversion if it isn't decided. */
22745 else
22747 Lisp_Object attrs;
22748 Lisp_Object eolvalue;
22750 attrs = AREF (val, 0);
22751 eolvalue = AREF (val, 2);
22753 *buf++ = multibyte
22754 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
22755 : ' ';
22757 if (eol_flag)
22759 /* The EOL conversion that is normal on this system. */
22761 if (NILP (eolvalue)) /* Not yet decided. */
22762 eoltype = eol_mnemonic_undecided;
22763 else if (VECTORP (eolvalue)) /* Not yet decided. */
22764 eoltype = eol_mnemonic_undecided;
22765 else /* eolvalue is Qunix, Qdos, or Qmac. */
22766 eoltype = (EQ (eolvalue, Qunix)
22767 ? eol_mnemonic_unix
22768 : (EQ (eolvalue, Qdos) == 1
22769 ? eol_mnemonic_dos : eol_mnemonic_mac));
22773 if (eol_flag)
22775 /* Mention the EOL conversion if it is not the usual one. */
22776 if (STRINGP (eoltype))
22778 eol_str = SDATA (eoltype);
22779 eol_str_len = SBYTES (eoltype);
22781 else if (CHARACTERP (eoltype))
22783 int c = XFASTINT (eoltype);
22784 return buf + CHAR_STRING (c, (unsigned char *) buf);
22786 else
22788 eol_str = invalid_eol_type;
22789 eol_str_len = sizeof (invalid_eol_type) - 1;
22791 memcpy (buf, eol_str, eol_str_len);
22792 buf += eol_str_len;
22795 return buf;
22798 /* Return a string for the output of a mode line %-spec for window W,
22799 generated by character C. FIELD_WIDTH > 0 means pad the string
22800 returned with spaces to that value. Return a Lisp string in
22801 *STRING if the resulting string is taken from that Lisp string.
22803 Note we operate on the current buffer for most purposes. */
22805 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
22807 static const char *
22808 decode_mode_spec (struct window *w, register int c, int field_width,
22809 Lisp_Object *string)
22811 Lisp_Object obj;
22812 struct frame *f = XFRAME (WINDOW_FRAME (w));
22813 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
22814 /* We are going to use f->decode_mode_spec_buffer as the buffer to
22815 produce strings from numerical values, so limit preposterously
22816 large values of FIELD_WIDTH to avoid overrunning the buffer's
22817 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
22818 bytes plus the terminating null. */
22819 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
22820 struct buffer *b = current_buffer;
22822 obj = Qnil;
22823 *string = Qnil;
22825 switch (c)
22827 case '*':
22828 if (!NILP (BVAR (b, read_only)))
22829 return "%";
22830 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22831 return "*";
22832 return "-";
22834 case '+':
22835 /* This differs from %* only for a modified read-only buffer. */
22836 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22837 return "*";
22838 if (!NILP (BVAR (b, read_only)))
22839 return "%";
22840 return "-";
22842 case '&':
22843 /* This differs from %* in ignoring read-only-ness. */
22844 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
22845 return "*";
22846 return "-";
22848 case '%':
22849 return "%";
22851 case '[':
22853 int i;
22854 char *p;
22856 if (command_loop_level > 5)
22857 return "[[[... ";
22858 p = decode_mode_spec_buf;
22859 for (i = 0; i < command_loop_level; i++)
22860 *p++ = '[';
22861 *p = 0;
22862 return decode_mode_spec_buf;
22865 case ']':
22867 int i;
22868 char *p;
22870 if (command_loop_level > 5)
22871 return " ...]]]";
22872 p = decode_mode_spec_buf;
22873 for (i = 0; i < command_loop_level; i++)
22874 *p++ = ']';
22875 *p = 0;
22876 return decode_mode_spec_buf;
22879 case '-':
22881 register int i;
22883 /* Let lots_of_dashes be a string of infinite length. */
22884 if (mode_line_target == MODE_LINE_NOPROP
22885 || mode_line_target == MODE_LINE_STRING)
22886 return "--";
22887 if (field_width <= 0
22888 || field_width > sizeof (lots_of_dashes))
22890 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
22891 decode_mode_spec_buf[i] = '-';
22892 decode_mode_spec_buf[i] = '\0';
22893 return decode_mode_spec_buf;
22895 else
22896 return lots_of_dashes;
22899 case 'b':
22900 obj = BVAR (b, name);
22901 break;
22903 case 'c':
22904 /* %c and %l are ignored in `frame-title-format'.
22905 (In redisplay_internal, the frame title is drawn _before_ the
22906 windows are updated, so the stuff which depends on actual
22907 window contents (such as %l) may fail to render properly, or
22908 even crash emacs.) */
22909 if (mode_line_target == MODE_LINE_TITLE)
22910 return "";
22911 else
22913 ptrdiff_t col = current_column ();
22914 w->column_number_displayed = col;
22915 pint2str (decode_mode_spec_buf, width, col);
22916 return decode_mode_spec_buf;
22919 case 'e':
22920 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
22922 if (NILP (Vmemory_full))
22923 return "";
22924 else
22925 return "!MEM FULL! ";
22927 #else
22928 return "";
22929 #endif
22931 case 'F':
22932 /* %F displays the frame name. */
22933 if (!NILP (f->title))
22934 return SSDATA (f->title);
22935 if (f->explicit_name || ! FRAME_WINDOW_P (f))
22936 return SSDATA (f->name);
22937 return "Emacs";
22939 case 'f':
22940 obj = BVAR (b, filename);
22941 break;
22943 case 'i':
22945 ptrdiff_t size = ZV - BEGV;
22946 pint2str (decode_mode_spec_buf, width, size);
22947 return decode_mode_spec_buf;
22950 case 'I':
22952 ptrdiff_t size = ZV - BEGV;
22953 pint2hrstr (decode_mode_spec_buf, width, size);
22954 return decode_mode_spec_buf;
22957 case 'l':
22959 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
22960 ptrdiff_t topline, nlines, height;
22961 ptrdiff_t junk;
22963 /* %c and %l are ignored in `frame-title-format'. */
22964 if (mode_line_target == MODE_LINE_TITLE)
22965 return "";
22967 startpos = marker_position (w->start);
22968 startpos_byte = marker_byte_position (w->start);
22969 height = WINDOW_TOTAL_LINES (w);
22971 /* If we decided that this buffer isn't suitable for line numbers,
22972 don't forget that too fast. */
22973 if (w->base_line_pos == -1)
22974 goto no_value;
22976 /* If the buffer is very big, don't waste time. */
22977 if (INTEGERP (Vline_number_display_limit)
22978 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
22980 w->base_line_pos = 0;
22981 w->base_line_number = 0;
22982 goto no_value;
22985 if (w->base_line_number > 0
22986 && w->base_line_pos > 0
22987 && w->base_line_pos <= startpos)
22989 line = w->base_line_number;
22990 linepos = w->base_line_pos;
22991 linepos_byte = buf_charpos_to_bytepos (b, linepos);
22993 else
22995 line = 1;
22996 linepos = BUF_BEGV (b);
22997 linepos_byte = BUF_BEGV_BYTE (b);
23000 /* Count lines from base line to window start position. */
23001 nlines = display_count_lines (linepos_byte,
23002 startpos_byte,
23003 startpos, &junk);
23005 topline = nlines + line;
23007 /* Determine a new base line, if the old one is too close
23008 or too far away, or if we did not have one.
23009 "Too close" means it's plausible a scroll-down would
23010 go back past it. */
23011 if (startpos == BUF_BEGV (b))
23013 w->base_line_number = topline;
23014 w->base_line_pos = BUF_BEGV (b);
23016 else if (nlines < height + 25 || nlines > height * 3 + 50
23017 || linepos == BUF_BEGV (b))
23019 ptrdiff_t limit = BUF_BEGV (b);
23020 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23021 ptrdiff_t position;
23022 ptrdiff_t distance =
23023 (height * 2 + 30) * line_number_display_limit_width;
23025 if (startpos - distance > limit)
23027 limit = startpos - distance;
23028 limit_byte = CHAR_TO_BYTE (limit);
23031 nlines = display_count_lines (startpos_byte,
23032 limit_byte,
23033 - (height * 2 + 30),
23034 &position);
23035 /* If we couldn't find the lines we wanted within
23036 line_number_display_limit_width chars per line,
23037 give up on line numbers for this window. */
23038 if (position == limit_byte && limit == startpos - distance)
23040 w->base_line_pos = -1;
23041 w->base_line_number = 0;
23042 goto no_value;
23045 w->base_line_number = topline - nlines;
23046 w->base_line_pos = BYTE_TO_CHAR (position);
23049 /* Now count lines from the start pos to point. */
23050 nlines = display_count_lines (startpos_byte,
23051 PT_BYTE, PT, &junk);
23053 /* Record that we did display the line number. */
23054 line_number_displayed = 1;
23056 /* Make the string to show. */
23057 pint2str (decode_mode_spec_buf, width, topline + nlines);
23058 return decode_mode_spec_buf;
23059 no_value:
23061 char *p = decode_mode_spec_buf;
23062 int pad = width - 2;
23063 while (pad-- > 0)
23064 *p++ = ' ';
23065 *p++ = '?';
23066 *p++ = '?';
23067 *p = '\0';
23068 return decode_mode_spec_buf;
23071 break;
23073 case 'm':
23074 obj = BVAR (b, mode_name);
23075 break;
23077 case 'n':
23078 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23079 return " Narrow";
23080 break;
23082 case 'p':
23084 ptrdiff_t pos = marker_position (w->start);
23085 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23087 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23089 if (pos <= BUF_BEGV (b))
23090 return "All";
23091 else
23092 return "Bottom";
23094 else if (pos <= BUF_BEGV (b))
23095 return "Top";
23096 else
23098 if (total > 1000000)
23099 /* Do it differently for a large value, to avoid overflow. */
23100 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23101 else
23102 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23103 /* We can't normally display a 3-digit number,
23104 so get us a 2-digit number that is close. */
23105 if (total == 100)
23106 total = 99;
23107 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23108 return decode_mode_spec_buf;
23112 /* Display percentage of size above the bottom of the screen. */
23113 case 'P':
23115 ptrdiff_t toppos = marker_position (w->start);
23116 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23117 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23119 if (botpos >= BUF_ZV (b))
23121 if (toppos <= BUF_BEGV (b))
23122 return "All";
23123 else
23124 return "Bottom";
23126 else
23128 if (total > 1000000)
23129 /* Do it differently for a large value, to avoid overflow. */
23130 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23131 else
23132 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23133 /* We can't normally display a 3-digit number,
23134 so get us a 2-digit number that is close. */
23135 if (total == 100)
23136 total = 99;
23137 if (toppos <= BUF_BEGV (b))
23138 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23139 else
23140 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23141 return decode_mode_spec_buf;
23145 case 's':
23146 /* status of process */
23147 obj = Fget_buffer_process (Fcurrent_buffer ());
23148 if (NILP (obj))
23149 return "no process";
23150 #ifndef MSDOS
23151 obj = Fsymbol_name (Fprocess_status (obj));
23152 #endif
23153 break;
23155 case '@':
23157 ptrdiff_t count = inhibit_garbage_collection ();
23158 Lisp_Object curdir = BVAR (current_buffer, directory);
23159 Lisp_Object val = Qnil;
23161 if (STRINGP (curdir))
23162 val = call1 (intern ("file-remote-p"), curdir);
23164 unbind_to (count, Qnil);
23166 if (NILP (val))
23167 return "-";
23168 else
23169 return "@";
23172 case 'z':
23173 /* coding-system (not including end-of-line format) */
23174 case 'Z':
23175 /* coding-system (including end-of-line type) */
23177 int eol_flag = (c == 'Z');
23178 char *p = decode_mode_spec_buf;
23180 if (! FRAME_WINDOW_P (f))
23182 /* No need to mention EOL here--the terminal never needs
23183 to do EOL conversion. */
23184 p = decode_mode_spec_coding (CODING_ID_NAME
23185 (FRAME_KEYBOARD_CODING (f)->id),
23186 p, 0);
23187 p = decode_mode_spec_coding (CODING_ID_NAME
23188 (FRAME_TERMINAL_CODING (f)->id),
23189 p, 0);
23191 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23192 p, eol_flag);
23194 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23195 #ifdef subprocesses
23196 obj = Fget_buffer_process (Fcurrent_buffer ());
23197 if (PROCESSP (obj))
23199 p = decode_mode_spec_coding
23200 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23201 p = decode_mode_spec_coding
23202 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23204 #endif /* subprocesses */
23205 #endif /* 0 */
23206 *p = 0;
23207 return decode_mode_spec_buf;
23211 if (STRINGP (obj))
23213 *string = obj;
23214 return SSDATA (obj);
23216 else
23217 return "";
23221 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23222 means count lines back from START_BYTE. But don't go beyond
23223 LIMIT_BYTE. Return the number of lines thus found (always
23224 nonnegative).
23226 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23227 either the position COUNT lines after/before START_BYTE, if we
23228 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23229 COUNT lines. */
23231 static ptrdiff_t
23232 display_count_lines (ptrdiff_t start_byte,
23233 ptrdiff_t limit_byte, ptrdiff_t count,
23234 ptrdiff_t *byte_pos_ptr)
23236 register unsigned char *cursor;
23237 unsigned char *base;
23239 register ptrdiff_t ceiling;
23240 register unsigned char *ceiling_addr;
23241 ptrdiff_t orig_count = count;
23243 /* If we are not in selective display mode,
23244 check only for newlines. */
23245 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23246 && !INTEGERP (BVAR (current_buffer, selective_display)));
23248 if (count > 0)
23250 while (start_byte < limit_byte)
23252 ceiling = BUFFER_CEILING_OF (start_byte);
23253 ceiling = min (limit_byte - 1, ceiling);
23254 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23255 base = (cursor = BYTE_POS_ADDR (start_byte));
23259 if (selective_display)
23261 while (*cursor != '\n' && *cursor != 015
23262 && ++cursor != ceiling_addr)
23263 continue;
23264 if (cursor == ceiling_addr)
23265 break;
23267 else
23269 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23270 if (! cursor)
23271 break;
23274 cursor++;
23276 if (--count == 0)
23278 start_byte += cursor - base;
23279 *byte_pos_ptr = start_byte;
23280 return orig_count;
23283 while (cursor < ceiling_addr);
23285 start_byte += ceiling_addr - base;
23288 else
23290 while (start_byte > limit_byte)
23292 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23293 ceiling = max (limit_byte, ceiling);
23294 ceiling_addr = BYTE_POS_ADDR (ceiling);
23295 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23296 while (1)
23298 if (selective_display)
23300 while (--cursor >= ceiling_addr
23301 && *cursor != '\n' && *cursor != 015)
23302 continue;
23303 if (cursor < ceiling_addr)
23304 break;
23306 else
23308 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23309 if (! cursor)
23310 break;
23313 if (++count == 0)
23315 start_byte += cursor - base + 1;
23316 *byte_pos_ptr = start_byte;
23317 /* When scanning backwards, we should
23318 not count the newline posterior to which we stop. */
23319 return - orig_count - 1;
23322 start_byte += ceiling_addr - base;
23326 *byte_pos_ptr = limit_byte;
23328 if (count < 0)
23329 return - orig_count + count;
23330 return orig_count - count;
23336 /***********************************************************************
23337 Displaying strings
23338 ***********************************************************************/
23340 /* Display a NUL-terminated string, starting with index START.
23342 If STRING is non-null, display that C string. Otherwise, the Lisp
23343 string LISP_STRING is displayed. There's a case that STRING is
23344 non-null and LISP_STRING is not nil. It means STRING is a string
23345 data of LISP_STRING. In that case, we display LISP_STRING while
23346 ignoring its text properties.
23348 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23349 FACE_STRING. Display STRING or LISP_STRING with the face at
23350 FACE_STRING_POS in FACE_STRING:
23352 Display the string in the environment given by IT, but use the
23353 standard display table, temporarily.
23355 FIELD_WIDTH is the minimum number of output glyphs to produce.
23356 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23357 with spaces. If STRING has more characters, more than FIELD_WIDTH
23358 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23360 PRECISION is the maximum number of characters to output from
23361 STRING. PRECISION < 0 means don't truncate the string.
23363 This is roughly equivalent to printf format specifiers:
23365 FIELD_WIDTH PRECISION PRINTF
23366 ----------------------------------------
23367 -1 -1 %s
23368 -1 10 %.10s
23369 10 -1 %10s
23370 20 10 %20.10s
23372 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23373 display them, and < 0 means obey the current buffer's value of
23374 enable_multibyte_characters.
23376 Value is the number of columns displayed. */
23378 static int
23379 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23380 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23381 int field_width, int precision, int max_x, int multibyte)
23383 int hpos_at_start = it->hpos;
23384 int saved_face_id = it->face_id;
23385 struct glyph_row *row = it->glyph_row;
23386 ptrdiff_t it_charpos;
23388 /* Initialize the iterator IT for iteration over STRING beginning
23389 with index START. */
23390 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23391 precision, field_width, multibyte);
23392 if (string && STRINGP (lisp_string))
23393 /* LISP_STRING is the one returned by decode_mode_spec. We should
23394 ignore its text properties. */
23395 it->stop_charpos = it->end_charpos;
23397 /* If displaying STRING, set up the face of the iterator from
23398 FACE_STRING, if that's given. */
23399 if (STRINGP (face_string))
23401 ptrdiff_t endptr;
23402 struct face *face;
23404 it->face_id
23405 = face_at_string_position (it->w, face_string, face_string_pos,
23406 0, &endptr, it->base_face_id, 0);
23407 face = FACE_FROM_ID (it->f, it->face_id);
23408 it->face_box_p = face->box != FACE_NO_BOX;
23411 /* Set max_x to the maximum allowed X position. Don't let it go
23412 beyond the right edge of the window. */
23413 if (max_x <= 0)
23414 max_x = it->last_visible_x;
23415 else
23416 max_x = min (max_x, it->last_visible_x);
23418 /* Skip over display elements that are not visible. because IT->w is
23419 hscrolled. */
23420 if (it->current_x < it->first_visible_x)
23421 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23422 MOVE_TO_POS | MOVE_TO_X);
23424 row->ascent = it->max_ascent;
23425 row->height = it->max_ascent + it->max_descent;
23426 row->phys_ascent = it->max_phys_ascent;
23427 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23428 row->extra_line_spacing = it->max_extra_line_spacing;
23430 if (STRINGP (it->string))
23431 it_charpos = IT_STRING_CHARPOS (*it);
23432 else
23433 it_charpos = IT_CHARPOS (*it);
23435 /* This condition is for the case that we are called with current_x
23436 past last_visible_x. */
23437 while (it->current_x < max_x)
23439 int x_before, x, n_glyphs_before, i, nglyphs;
23441 /* Get the next display element. */
23442 if (!get_next_display_element (it))
23443 break;
23445 /* Produce glyphs. */
23446 x_before = it->current_x;
23447 n_glyphs_before = row->used[TEXT_AREA];
23448 PRODUCE_GLYPHS (it);
23450 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23451 i = 0;
23452 x = x_before;
23453 while (i < nglyphs)
23455 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23457 if (it->line_wrap != TRUNCATE
23458 && x + glyph->pixel_width > max_x)
23460 /* End of continued line or max_x reached. */
23461 if (CHAR_GLYPH_PADDING_P (*glyph))
23463 /* A wide character is unbreakable. */
23464 if (row->reversed_p)
23465 unproduce_glyphs (it, row->used[TEXT_AREA]
23466 - n_glyphs_before);
23467 row->used[TEXT_AREA] = n_glyphs_before;
23468 it->current_x = x_before;
23470 else
23472 if (row->reversed_p)
23473 unproduce_glyphs (it, row->used[TEXT_AREA]
23474 - (n_glyphs_before + i));
23475 row->used[TEXT_AREA] = n_glyphs_before + i;
23476 it->current_x = x;
23478 break;
23480 else if (x + glyph->pixel_width >= it->first_visible_x)
23482 /* Glyph is at least partially visible. */
23483 ++it->hpos;
23484 if (x < it->first_visible_x)
23485 row->x = x - it->first_visible_x;
23487 else
23489 /* Glyph is off the left margin of the display area.
23490 Should not happen. */
23491 emacs_abort ();
23494 row->ascent = max (row->ascent, it->max_ascent);
23495 row->height = max (row->height, it->max_ascent + it->max_descent);
23496 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23497 row->phys_height = max (row->phys_height,
23498 it->max_phys_ascent + it->max_phys_descent);
23499 row->extra_line_spacing = max (row->extra_line_spacing,
23500 it->max_extra_line_spacing);
23501 x += glyph->pixel_width;
23502 ++i;
23505 /* Stop if max_x reached. */
23506 if (i < nglyphs)
23507 break;
23509 /* Stop at line ends. */
23510 if (ITERATOR_AT_END_OF_LINE_P (it))
23512 it->continuation_lines_width = 0;
23513 break;
23516 set_iterator_to_next (it, 1);
23517 if (STRINGP (it->string))
23518 it_charpos = IT_STRING_CHARPOS (*it);
23519 else
23520 it_charpos = IT_CHARPOS (*it);
23522 /* Stop if truncating at the right edge. */
23523 if (it->line_wrap == TRUNCATE
23524 && it->current_x >= it->last_visible_x)
23526 /* Add truncation mark, but don't do it if the line is
23527 truncated at a padding space. */
23528 if (it_charpos < it->string_nchars)
23530 if (!FRAME_WINDOW_P (it->f))
23532 int ii, n;
23534 if (it->current_x > it->last_visible_x)
23536 if (!row->reversed_p)
23538 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23539 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23540 break;
23542 else
23544 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23545 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23546 break;
23547 unproduce_glyphs (it, ii + 1);
23548 ii = row->used[TEXT_AREA] - (ii + 1);
23550 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23552 row->used[TEXT_AREA] = ii;
23553 produce_special_glyphs (it, IT_TRUNCATION);
23556 produce_special_glyphs (it, IT_TRUNCATION);
23558 row->truncated_on_right_p = 1;
23560 break;
23564 /* Maybe insert a truncation at the left. */
23565 if (it->first_visible_x
23566 && it_charpos > 0)
23568 if (!FRAME_WINDOW_P (it->f)
23569 || (row->reversed_p
23570 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23571 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23572 insert_left_trunc_glyphs (it);
23573 row->truncated_on_left_p = 1;
23576 it->face_id = saved_face_id;
23578 /* Value is number of columns displayed. */
23579 return it->hpos - hpos_at_start;
23584 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23585 appears as an element of LIST or as the car of an element of LIST.
23586 If PROPVAL is a list, compare each element against LIST in that
23587 way, and return 1/2 if any element of PROPVAL is found in LIST.
23588 Otherwise return 0. This function cannot quit.
23589 The return value is 2 if the text is invisible but with an ellipsis
23590 and 1 if it's invisible and without an ellipsis. */
23593 invisible_p (register Lisp_Object propval, Lisp_Object list)
23595 register Lisp_Object tail, proptail;
23597 for (tail = list; CONSP (tail); tail = XCDR (tail))
23599 register Lisp_Object tem;
23600 tem = XCAR (tail);
23601 if (EQ (propval, tem))
23602 return 1;
23603 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23604 return NILP (XCDR (tem)) ? 1 : 2;
23607 if (CONSP (propval))
23609 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23611 Lisp_Object propelt;
23612 propelt = XCAR (proptail);
23613 for (tail = list; CONSP (tail); tail = XCDR (tail))
23615 register Lisp_Object tem;
23616 tem = XCAR (tail);
23617 if (EQ (propelt, tem))
23618 return 1;
23619 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23620 return NILP (XCDR (tem)) ? 1 : 2;
23625 return 0;
23628 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23629 doc: /* Non-nil if the property makes the text invisible.
23630 POS-OR-PROP can be a marker or number, in which case it is taken to be
23631 a position in the current buffer and the value of the `invisible' property
23632 is checked; or it can be some other value, which is then presumed to be the
23633 value of the `invisible' property of the text of interest.
23634 The non-nil value returned can be t for truly invisible text or something
23635 else if the text is replaced by an ellipsis. */)
23636 (Lisp_Object pos_or_prop)
23638 Lisp_Object prop
23639 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
23640 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
23641 : pos_or_prop);
23642 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
23643 return (invis == 0 ? Qnil
23644 : invis == 1 ? Qt
23645 : make_number (invis));
23648 /* Calculate a width or height in pixels from a specification using
23649 the following elements:
23651 SPEC ::=
23652 NUM - a (fractional) multiple of the default font width/height
23653 (NUM) - specifies exactly NUM pixels
23654 UNIT - a fixed number of pixels, see below.
23655 ELEMENT - size of a display element in pixels, see below.
23656 (NUM . SPEC) - equals NUM * SPEC
23657 (+ SPEC SPEC ...) - add pixel values
23658 (- SPEC SPEC ...) - subtract pixel values
23659 (- SPEC) - negate pixel value
23661 NUM ::=
23662 INT or FLOAT - a number constant
23663 SYMBOL - use symbol's (buffer local) variable binding.
23665 UNIT ::=
23666 in - pixels per inch *)
23667 mm - pixels per 1/1000 meter *)
23668 cm - pixels per 1/100 meter *)
23669 width - width of current font in pixels.
23670 height - height of current font in pixels.
23672 *) using the ratio(s) defined in display-pixels-per-inch.
23674 ELEMENT ::=
23676 left-fringe - left fringe width in pixels
23677 right-fringe - right fringe width in pixels
23679 left-margin - left margin width in pixels
23680 right-margin - right margin width in pixels
23682 scroll-bar - scroll-bar area width in pixels
23684 Examples:
23686 Pixels corresponding to 5 inches:
23687 (5 . in)
23689 Total width of non-text areas on left side of window (if scroll-bar is on left):
23690 '(space :width (+ left-fringe left-margin scroll-bar))
23692 Align to first text column (in header line):
23693 '(space :align-to 0)
23695 Align to middle of text area minus half the width of variable `my-image'
23696 containing a loaded image:
23697 '(space :align-to (0.5 . (- text my-image)))
23699 Width of left margin minus width of 1 character in the default font:
23700 '(space :width (- left-margin 1))
23702 Width of left margin minus width of 2 characters in the current font:
23703 '(space :width (- left-margin (2 . width)))
23705 Center 1 character over left-margin (in header line):
23706 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
23708 Different ways to express width of left fringe plus left margin minus one pixel:
23709 '(space :width (- (+ left-fringe left-margin) (1)))
23710 '(space :width (+ left-fringe left-margin (- (1))))
23711 '(space :width (+ left-fringe left-margin (-1)))
23715 static int
23716 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
23717 struct font *font, int width_p, int *align_to)
23719 double pixels;
23721 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
23722 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
23724 if (NILP (prop))
23725 return OK_PIXELS (0);
23727 eassert (FRAME_LIVE_P (it->f));
23729 if (SYMBOLP (prop))
23731 if (SCHARS (SYMBOL_NAME (prop)) == 2)
23733 char *unit = SSDATA (SYMBOL_NAME (prop));
23735 if (unit[0] == 'i' && unit[1] == 'n')
23736 pixels = 1.0;
23737 else if (unit[0] == 'm' && unit[1] == 'm')
23738 pixels = 25.4;
23739 else if (unit[0] == 'c' && unit[1] == 'm')
23740 pixels = 2.54;
23741 else
23742 pixels = 0;
23743 if (pixels > 0)
23745 double ppi = (width_p ? FRAME_RES_X (it->f)
23746 : FRAME_RES_Y (it->f));
23748 if (ppi > 0)
23749 return OK_PIXELS (ppi / pixels);
23750 return 0;
23754 #ifdef HAVE_WINDOW_SYSTEM
23755 if (EQ (prop, Qheight))
23756 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
23757 if (EQ (prop, Qwidth))
23758 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
23759 #else
23760 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
23761 return OK_PIXELS (1);
23762 #endif
23764 if (EQ (prop, Qtext))
23765 return OK_PIXELS (width_p
23766 ? window_box_width (it->w, TEXT_AREA)
23767 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
23769 if (align_to && *align_to < 0)
23771 *res = 0;
23772 if (EQ (prop, Qleft))
23773 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
23774 if (EQ (prop, Qright))
23775 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
23776 if (EQ (prop, Qcenter))
23777 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
23778 + window_box_width (it->w, TEXT_AREA) / 2);
23779 if (EQ (prop, Qleft_fringe))
23780 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23781 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
23782 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
23783 if (EQ (prop, Qright_fringe))
23784 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23785 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23786 : window_box_right_offset (it->w, TEXT_AREA));
23787 if (EQ (prop, Qleft_margin))
23788 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
23789 if (EQ (prop, Qright_margin))
23790 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
23791 if (EQ (prop, Qscroll_bar))
23792 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
23794 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
23795 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
23796 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23797 : 0)));
23799 else
23801 if (EQ (prop, Qleft_fringe))
23802 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
23803 if (EQ (prop, Qright_fringe))
23804 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
23805 if (EQ (prop, Qleft_margin))
23806 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
23807 if (EQ (prop, Qright_margin))
23808 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
23809 if (EQ (prop, Qscroll_bar))
23810 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
23813 prop = buffer_local_value (prop, it->w->contents);
23814 if (EQ (prop, Qunbound))
23815 prop = Qnil;
23818 if (INTEGERP (prop) || FLOATP (prop))
23820 int base_unit = (width_p
23821 ? FRAME_COLUMN_WIDTH (it->f)
23822 : FRAME_LINE_HEIGHT (it->f));
23823 return OK_PIXELS (XFLOATINT (prop) * base_unit);
23826 if (CONSP (prop))
23828 Lisp_Object car = XCAR (prop);
23829 Lisp_Object cdr = XCDR (prop);
23831 if (SYMBOLP (car))
23833 #ifdef HAVE_WINDOW_SYSTEM
23834 if (FRAME_WINDOW_P (it->f)
23835 && valid_image_p (prop))
23837 ptrdiff_t id = lookup_image (it->f, prop);
23838 struct image *img = IMAGE_FROM_ID (it->f, id);
23840 return OK_PIXELS (width_p ? img->width : img->height);
23842 #endif
23843 if (EQ (car, Qplus) || EQ (car, Qminus))
23845 int first = 1;
23846 double px;
23848 pixels = 0;
23849 while (CONSP (cdr))
23851 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
23852 font, width_p, align_to))
23853 return 0;
23854 if (first)
23855 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
23856 else
23857 pixels += px;
23858 cdr = XCDR (cdr);
23860 if (EQ (car, Qminus))
23861 pixels = -pixels;
23862 return OK_PIXELS (pixels);
23865 car = buffer_local_value (car, it->w->contents);
23866 if (EQ (car, Qunbound))
23867 car = Qnil;
23870 if (INTEGERP (car) || FLOATP (car))
23872 double fact;
23873 pixels = XFLOATINT (car);
23874 if (NILP (cdr))
23875 return OK_PIXELS (pixels);
23876 if (calc_pixel_width_or_height (&fact, it, cdr,
23877 font, width_p, align_to))
23878 return OK_PIXELS (pixels * fact);
23879 return 0;
23882 return 0;
23885 return 0;
23889 /***********************************************************************
23890 Glyph Display
23891 ***********************************************************************/
23893 #ifdef HAVE_WINDOW_SYSTEM
23895 #ifdef GLYPH_DEBUG
23897 void
23898 dump_glyph_string (struct glyph_string *s)
23900 fprintf (stderr, "glyph string\n");
23901 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
23902 s->x, s->y, s->width, s->height);
23903 fprintf (stderr, " ybase = %d\n", s->ybase);
23904 fprintf (stderr, " hl = %d\n", s->hl);
23905 fprintf (stderr, " left overhang = %d, right = %d\n",
23906 s->left_overhang, s->right_overhang);
23907 fprintf (stderr, " nchars = %d\n", s->nchars);
23908 fprintf (stderr, " extends to end of line = %d\n",
23909 s->extends_to_end_of_line_p);
23910 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
23911 fprintf (stderr, " bg width = %d\n", s->background_width);
23914 #endif /* GLYPH_DEBUG */
23916 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
23917 of XChar2b structures for S; it can't be allocated in
23918 init_glyph_string because it must be allocated via `alloca'. W
23919 is the window on which S is drawn. ROW and AREA are the glyph row
23920 and area within the row from which S is constructed. START is the
23921 index of the first glyph structure covered by S. HL is a
23922 face-override for drawing S. */
23924 #ifdef HAVE_NTGUI
23925 #define OPTIONAL_HDC(hdc) HDC hdc,
23926 #define DECLARE_HDC(hdc) HDC hdc;
23927 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
23928 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
23929 #endif
23931 #ifndef OPTIONAL_HDC
23932 #define OPTIONAL_HDC(hdc)
23933 #define DECLARE_HDC(hdc)
23934 #define ALLOCATE_HDC(hdc, f)
23935 #define RELEASE_HDC(hdc, f)
23936 #endif
23938 static void
23939 init_glyph_string (struct glyph_string *s,
23940 OPTIONAL_HDC (hdc)
23941 XChar2b *char2b, struct window *w, struct glyph_row *row,
23942 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
23944 memset (s, 0, sizeof *s);
23945 s->w = w;
23946 s->f = XFRAME (w->frame);
23947 #ifdef HAVE_NTGUI
23948 s->hdc = hdc;
23949 #endif
23950 s->display = FRAME_X_DISPLAY (s->f);
23951 s->window = FRAME_X_WINDOW (s->f);
23952 s->char2b = char2b;
23953 s->hl = hl;
23954 s->row = row;
23955 s->area = area;
23956 s->first_glyph = row->glyphs[area] + start;
23957 s->height = row->height;
23958 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
23959 s->ybase = s->y + row->ascent;
23963 /* Append the list of glyph strings with head H and tail T to the list
23964 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
23966 static void
23967 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23968 struct glyph_string *h, struct glyph_string *t)
23970 if (h)
23972 if (*head)
23973 (*tail)->next = h;
23974 else
23975 *head = h;
23976 h->prev = *tail;
23977 *tail = t;
23982 /* Prepend the list of glyph strings with head H and tail T to the
23983 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
23984 result. */
23986 static void
23987 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
23988 struct glyph_string *h, struct glyph_string *t)
23990 if (h)
23992 if (*head)
23993 (*head)->prev = t;
23994 else
23995 *tail = t;
23996 t->next = *head;
23997 *head = h;
24002 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24003 Set *HEAD and *TAIL to the resulting list. */
24005 static void
24006 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24007 struct glyph_string *s)
24009 s->next = s->prev = NULL;
24010 append_glyph_string_lists (head, tail, s, s);
24014 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24015 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24016 make sure that X resources for the face returned are allocated.
24017 Value is a pointer to a realized face that is ready for display if
24018 DISPLAY_P is non-zero. */
24020 static struct face *
24021 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24022 XChar2b *char2b, int display_p)
24024 struct face *face = FACE_FROM_ID (f, face_id);
24025 unsigned code = 0;
24027 if (face->font)
24029 code = face->font->driver->encode_char (face->font, c);
24031 if (code == FONT_INVALID_CODE)
24032 code = 0;
24034 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24036 /* Make sure X resources of the face are allocated. */
24037 #ifdef HAVE_X_WINDOWS
24038 if (display_p)
24039 #endif
24041 eassert (face != NULL);
24042 prepare_face_for_display (f, face);
24045 return face;
24049 /* Get face and two-byte form of character glyph GLYPH on frame F.
24050 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24051 a pointer to a realized face that is ready for display. */
24053 static struct face *
24054 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24055 XChar2b *char2b, int *two_byte_p)
24057 struct face *face;
24058 unsigned code = 0;
24060 eassert (glyph->type == CHAR_GLYPH);
24061 face = FACE_FROM_ID (f, glyph->face_id);
24063 /* Make sure X resources of the face are allocated. */
24064 eassert (face != NULL);
24065 prepare_face_for_display (f, face);
24067 if (two_byte_p)
24068 *two_byte_p = 0;
24070 if (face->font)
24072 if (CHAR_BYTE8_P (glyph->u.ch))
24073 code = CHAR_TO_BYTE8 (glyph->u.ch);
24074 else
24075 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24077 if (code == FONT_INVALID_CODE)
24078 code = 0;
24081 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24082 return face;
24086 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24087 Return 1 if FONT has a glyph for C, otherwise return 0. */
24089 static int
24090 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24092 unsigned code;
24094 if (CHAR_BYTE8_P (c))
24095 code = CHAR_TO_BYTE8 (c);
24096 else
24097 code = font->driver->encode_char (font, c);
24099 if (code == FONT_INVALID_CODE)
24100 return 0;
24101 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24102 return 1;
24106 /* Fill glyph string S with composition components specified by S->cmp.
24108 BASE_FACE is the base face of the composition.
24109 S->cmp_from is the index of the first component for S.
24111 OVERLAPS non-zero means S should draw the foreground only, and use
24112 its physical height for clipping. See also draw_glyphs.
24114 Value is the index of a component not in S. */
24116 static int
24117 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24118 int overlaps)
24120 int i;
24121 /* For all glyphs of this composition, starting at the offset
24122 S->cmp_from, until we reach the end of the definition or encounter a
24123 glyph that requires the different face, add it to S. */
24124 struct face *face;
24126 eassert (s);
24128 s->for_overlaps = overlaps;
24129 s->face = NULL;
24130 s->font = NULL;
24131 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24133 int c = COMPOSITION_GLYPH (s->cmp, i);
24135 /* TAB in a composition means display glyphs with padding space
24136 on the left or right. */
24137 if (c != '\t')
24139 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24140 -1, Qnil);
24142 face = get_char_face_and_encoding (s->f, c, face_id,
24143 s->char2b + i, 1);
24144 if (face)
24146 if (! s->face)
24148 s->face = face;
24149 s->font = s->face->font;
24151 else if (s->face != face)
24152 break;
24155 ++s->nchars;
24157 s->cmp_to = i;
24159 if (s->face == NULL)
24161 s->face = base_face->ascii_face;
24162 s->font = s->face->font;
24165 /* All glyph strings for the same composition has the same width,
24166 i.e. the width set for the first component of the composition. */
24167 s->width = s->first_glyph->pixel_width;
24169 /* If the specified font could not be loaded, use the frame's
24170 default font, but record the fact that we couldn't load it in
24171 the glyph string so that we can draw rectangles for the
24172 characters of the glyph string. */
24173 if (s->font == NULL)
24175 s->font_not_found_p = 1;
24176 s->font = FRAME_FONT (s->f);
24179 /* Adjust base line for subscript/superscript text. */
24180 s->ybase += s->first_glyph->voffset;
24182 /* This glyph string must always be drawn with 16-bit functions. */
24183 s->two_byte_p = 1;
24185 return s->cmp_to;
24188 static int
24189 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24190 int start, int end, int overlaps)
24192 struct glyph *glyph, *last;
24193 Lisp_Object lgstring;
24194 int i;
24196 s->for_overlaps = overlaps;
24197 glyph = s->row->glyphs[s->area] + start;
24198 last = s->row->glyphs[s->area] + end;
24199 s->cmp_id = glyph->u.cmp.id;
24200 s->cmp_from = glyph->slice.cmp.from;
24201 s->cmp_to = glyph->slice.cmp.to + 1;
24202 s->face = FACE_FROM_ID (s->f, face_id);
24203 lgstring = composition_gstring_from_id (s->cmp_id);
24204 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24205 glyph++;
24206 while (glyph < last
24207 && glyph->u.cmp.automatic
24208 && glyph->u.cmp.id == s->cmp_id
24209 && s->cmp_to == glyph->slice.cmp.from)
24210 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24212 for (i = s->cmp_from; i < s->cmp_to; i++)
24214 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24215 unsigned code = LGLYPH_CODE (lglyph);
24217 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24219 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24220 return glyph - s->row->glyphs[s->area];
24224 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24225 See the comment of fill_glyph_string for arguments.
24226 Value is the index of the first glyph not in S. */
24229 static int
24230 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24231 int start, int end, int overlaps)
24233 struct glyph *glyph, *last;
24234 int voffset;
24236 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24237 s->for_overlaps = overlaps;
24238 glyph = s->row->glyphs[s->area] + start;
24239 last = s->row->glyphs[s->area] + end;
24240 voffset = glyph->voffset;
24241 s->face = FACE_FROM_ID (s->f, face_id);
24242 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24243 s->nchars = 1;
24244 s->width = glyph->pixel_width;
24245 glyph++;
24246 while (glyph < last
24247 && glyph->type == GLYPHLESS_GLYPH
24248 && glyph->voffset == voffset
24249 && glyph->face_id == face_id)
24251 s->nchars++;
24252 s->width += glyph->pixel_width;
24253 glyph++;
24255 s->ybase += voffset;
24256 return glyph - s->row->glyphs[s->area];
24260 /* Fill glyph string S from a sequence of character glyphs.
24262 FACE_ID is the face id of the string. START is the index of the
24263 first glyph to consider, END is the index of the last + 1.
24264 OVERLAPS non-zero means S should draw the foreground only, and use
24265 its physical height for clipping. See also draw_glyphs.
24267 Value is the index of the first glyph not in S. */
24269 static int
24270 fill_glyph_string (struct glyph_string *s, int face_id,
24271 int start, int end, int overlaps)
24273 struct glyph *glyph, *last;
24274 int voffset;
24275 int glyph_not_available_p;
24277 eassert (s->f == XFRAME (s->w->frame));
24278 eassert (s->nchars == 0);
24279 eassert (start >= 0 && end > start);
24281 s->for_overlaps = overlaps;
24282 glyph = s->row->glyphs[s->area] + start;
24283 last = s->row->glyphs[s->area] + end;
24284 voffset = glyph->voffset;
24285 s->padding_p = glyph->padding_p;
24286 glyph_not_available_p = glyph->glyph_not_available_p;
24288 while (glyph < last
24289 && glyph->type == CHAR_GLYPH
24290 && glyph->voffset == voffset
24291 /* Same face id implies same font, nowadays. */
24292 && glyph->face_id == face_id
24293 && glyph->glyph_not_available_p == glyph_not_available_p)
24295 int two_byte_p;
24297 s->face = get_glyph_face_and_encoding (s->f, glyph,
24298 s->char2b + s->nchars,
24299 &two_byte_p);
24300 s->two_byte_p = two_byte_p;
24301 ++s->nchars;
24302 eassert (s->nchars <= end - start);
24303 s->width += glyph->pixel_width;
24304 if (glyph++->padding_p != s->padding_p)
24305 break;
24308 s->font = s->face->font;
24310 /* If the specified font could not be loaded, use the frame's font,
24311 but record the fact that we couldn't load it in
24312 S->font_not_found_p so that we can draw rectangles for the
24313 characters of the glyph string. */
24314 if (s->font == NULL || glyph_not_available_p)
24316 s->font_not_found_p = 1;
24317 s->font = FRAME_FONT (s->f);
24320 /* Adjust base line for subscript/superscript text. */
24321 s->ybase += voffset;
24323 eassert (s->face && s->face->gc);
24324 return glyph - s->row->glyphs[s->area];
24328 /* Fill glyph string S from image glyph S->first_glyph. */
24330 static void
24331 fill_image_glyph_string (struct glyph_string *s)
24333 eassert (s->first_glyph->type == IMAGE_GLYPH);
24334 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24335 eassert (s->img);
24336 s->slice = s->first_glyph->slice.img;
24337 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24338 s->font = s->face->font;
24339 s->width = s->first_glyph->pixel_width;
24341 /* Adjust base line for subscript/superscript text. */
24342 s->ybase += s->first_glyph->voffset;
24346 /* Fill glyph string S from a sequence of stretch glyphs.
24348 START is the index of the first glyph to consider,
24349 END is the index of the last + 1.
24351 Value is the index of the first glyph not in S. */
24353 static int
24354 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24356 struct glyph *glyph, *last;
24357 int voffset, face_id;
24359 eassert (s->first_glyph->type == STRETCH_GLYPH);
24361 glyph = s->row->glyphs[s->area] + start;
24362 last = s->row->glyphs[s->area] + end;
24363 face_id = glyph->face_id;
24364 s->face = FACE_FROM_ID (s->f, face_id);
24365 s->font = s->face->font;
24366 s->width = glyph->pixel_width;
24367 s->nchars = 1;
24368 voffset = glyph->voffset;
24370 for (++glyph;
24371 (glyph < last
24372 && glyph->type == STRETCH_GLYPH
24373 && glyph->voffset == voffset
24374 && glyph->face_id == face_id);
24375 ++glyph)
24376 s->width += glyph->pixel_width;
24378 /* Adjust base line for subscript/superscript text. */
24379 s->ybase += voffset;
24381 /* The case that face->gc == 0 is handled when drawing the glyph
24382 string by calling prepare_face_for_display. */
24383 eassert (s->face);
24384 return glyph - s->row->glyphs[s->area];
24387 static struct font_metrics *
24388 get_per_char_metric (struct font *font, XChar2b *char2b)
24390 static struct font_metrics metrics;
24391 unsigned code;
24393 if (! font)
24394 return NULL;
24395 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24396 if (code == FONT_INVALID_CODE)
24397 return NULL;
24398 font->driver->text_extents (font, &code, 1, &metrics);
24399 return &metrics;
24402 /* EXPORT for RIF:
24403 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24404 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24405 assumed to be zero. */
24407 void
24408 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24410 *left = *right = 0;
24412 if (glyph->type == CHAR_GLYPH)
24414 struct face *face;
24415 XChar2b char2b;
24416 struct font_metrics *pcm;
24418 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24419 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24421 if (pcm->rbearing > pcm->width)
24422 *right = pcm->rbearing - pcm->width;
24423 if (pcm->lbearing < 0)
24424 *left = -pcm->lbearing;
24427 else if (glyph->type == COMPOSITE_GLYPH)
24429 if (! glyph->u.cmp.automatic)
24431 struct composition *cmp = composition_table[glyph->u.cmp.id];
24433 if (cmp->rbearing > cmp->pixel_width)
24434 *right = cmp->rbearing - cmp->pixel_width;
24435 if (cmp->lbearing < 0)
24436 *left = - cmp->lbearing;
24438 else
24440 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24441 struct font_metrics metrics;
24443 composition_gstring_width (gstring, glyph->slice.cmp.from,
24444 glyph->slice.cmp.to + 1, &metrics);
24445 if (metrics.rbearing > metrics.width)
24446 *right = metrics.rbearing - metrics.width;
24447 if (metrics.lbearing < 0)
24448 *left = - metrics.lbearing;
24454 /* Return the index of the first glyph preceding glyph string S that
24455 is overwritten by S because of S's left overhang. Value is -1
24456 if no glyphs are overwritten. */
24458 static int
24459 left_overwritten (struct glyph_string *s)
24461 int k;
24463 if (s->left_overhang)
24465 int x = 0, i;
24466 struct glyph *glyphs = s->row->glyphs[s->area];
24467 int first = s->first_glyph - glyphs;
24469 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24470 x -= glyphs[i].pixel_width;
24472 k = i + 1;
24474 else
24475 k = -1;
24477 return k;
24481 /* Return the index of the first glyph preceding glyph string S that
24482 is overwriting S because of its right overhang. Value is -1 if no
24483 glyph in front of S overwrites S. */
24485 static int
24486 left_overwriting (struct glyph_string *s)
24488 int i, k, x;
24489 struct glyph *glyphs = s->row->glyphs[s->area];
24490 int first = s->first_glyph - glyphs;
24492 k = -1;
24493 x = 0;
24494 for (i = first - 1; i >= 0; --i)
24496 int left, right;
24497 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24498 if (x + right > 0)
24499 k = i;
24500 x -= glyphs[i].pixel_width;
24503 return k;
24507 /* Return the index of the last glyph following glyph string S that is
24508 overwritten by S because of S's right overhang. Value is -1 if
24509 no such glyph is found. */
24511 static int
24512 right_overwritten (struct glyph_string *s)
24514 int k = -1;
24516 if (s->right_overhang)
24518 int x = 0, i;
24519 struct glyph *glyphs = s->row->glyphs[s->area];
24520 int first = (s->first_glyph - glyphs
24521 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24522 int end = s->row->used[s->area];
24524 for (i = first; i < end && s->right_overhang > x; ++i)
24525 x += glyphs[i].pixel_width;
24527 k = i;
24530 return k;
24534 /* Return the index of the last glyph following glyph string S that
24535 overwrites S because of its left overhang. Value is negative
24536 if no such glyph is found. */
24538 static int
24539 right_overwriting (struct glyph_string *s)
24541 int i, k, x;
24542 int end = s->row->used[s->area];
24543 struct glyph *glyphs = s->row->glyphs[s->area];
24544 int first = (s->first_glyph - glyphs
24545 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24547 k = -1;
24548 x = 0;
24549 for (i = first; i < end; ++i)
24551 int left, right;
24552 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24553 if (x - left < 0)
24554 k = i;
24555 x += glyphs[i].pixel_width;
24558 return k;
24562 /* Set background width of glyph string S. START is the index of the
24563 first glyph following S. LAST_X is the right-most x-position + 1
24564 in the drawing area. */
24566 static void
24567 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24569 /* If the face of this glyph string has to be drawn to the end of
24570 the drawing area, set S->extends_to_end_of_line_p. */
24572 if (start == s->row->used[s->area]
24573 && ((s->row->fill_line_p
24574 && (s->hl == DRAW_NORMAL_TEXT
24575 || s->hl == DRAW_IMAGE_RAISED
24576 || s->hl == DRAW_IMAGE_SUNKEN))
24577 || s->hl == DRAW_MOUSE_FACE))
24578 s->extends_to_end_of_line_p = 1;
24580 /* If S extends its face to the end of the line, set its
24581 background_width to the distance to the right edge of the drawing
24582 area. */
24583 if (s->extends_to_end_of_line_p)
24584 s->background_width = last_x - s->x + 1;
24585 else
24586 s->background_width = s->width;
24590 /* Compute overhangs and x-positions for glyph string S and its
24591 predecessors, or successors. X is the starting x-position for S.
24592 BACKWARD_P non-zero means process predecessors. */
24594 static void
24595 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24597 if (backward_p)
24599 while (s)
24601 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24602 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24603 x -= s->width;
24604 s->x = x;
24605 s = s->prev;
24608 else
24610 while (s)
24612 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24613 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24614 s->x = x;
24615 x += s->width;
24616 s = s->next;
24623 /* The following macros are only called from draw_glyphs below.
24624 They reference the following parameters of that function directly:
24625 `w', `row', `area', and `overlap_p'
24626 as well as the following local variables:
24627 `s', `f', and `hdc' (in W32) */
24629 #ifdef HAVE_NTGUI
24630 /* On W32, silently add local `hdc' variable to argument list of
24631 init_glyph_string. */
24632 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24633 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
24634 #else
24635 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
24636 init_glyph_string (s, char2b, w, row, area, start, hl)
24637 #endif
24639 /* Add a glyph string for a stretch glyph to the list of strings
24640 between HEAD and TAIL. START is the index of the stretch glyph in
24641 row area AREA of glyph row ROW. END is the index of the last glyph
24642 in that glyph row area. X is the current output position assigned
24643 to the new glyph string constructed. HL overrides that face of the
24644 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24645 is the right-most x-position of the drawing area. */
24647 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
24648 and below -- keep them on one line. */
24649 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24650 do \
24652 s = alloca (sizeof *s); \
24653 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24654 START = fill_stretch_glyph_string (s, START, END); \
24655 append_glyph_string (&HEAD, &TAIL, s); \
24656 s->x = (X); \
24658 while (0)
24661 /* Add a glyph string for an image glyph to the list of strings
24662 between HEAD and TAIL. START is the index of the image glyph in
24663 row area AREA of glyph row ROW. END is the index of the last glyph
24664 in that glyph row area. X is the current output position assigned
24665 to the new glyph string constructed. HL overrides that face of the
24666 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
24667 is the right-most x-position of the drawing area. */
24669 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24670 do \
24672 s = alloca (sizeof *s); \
24673 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24674 fill_image_glyph_string (s); \
24675 append_glyph_string (&HEAD, &TAIL, s); \
24676 ++START; \
24677 s->x = (X); \
24679 while (0)
24682 /* Add a glyph string for a sequence of character glyphs to the list
24683 of strings between HEAD and TAIL. START is the index of the first
24684 glyph in row area AREA of glyph row ROW that is part of the new
24685 glyph string. END is the index of the last glyph in that glyph row
24686 area. X is the current output position assigned to the new glyph
24687 string constructed. HL overrides that face of the glyph; e.g. it
24688 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
24689 right-most x-position of the drawing area. */
24691 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24692 do \
24694 int face_id; \
24695 XChar2b *char2b; \
24697 face_id = (row)->glyphs[area][START].face_id; \
24699 s = alloca (sizeof *s); \
24700 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
24701 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24702 append_glyph_string (&HEAD, &TAIL, s); \
24703 s->x = (X); \
24704 START = fill_glyph_string (s, face_id, START, END, overlaps); \
24706 while (0)
24709 /* Add a glyph string for a composite sequence to the list of strings
24710 between HEAD and TAIL. START is the index of the first glyph in
24711 row area AREA of glyph row ROW that is part of the new glyph
24712 string. END is the index of the last glyph in that glyph row area.
24713 X is the current output position assigned to the new glyph string
24714 constructed. HL overrides that face of the glyph; e.g. it is
24715 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
24716 x-position of the drawing area. */
24718 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24719 do { \
24720 int face_id = (row)->glyphs[area][START].face_id; \
24721 struct face *base_face = FACE_FROM_ID (f, face_id); \
24722 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
24723 struct composition *cmp = composition_table[cmp_id]; \
24724 XChar2b *char2b; \
24725 struct glyph_string *first_s = NULL; \
24726 int n; \
24728 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
24730 /* Make glyph_strings for each glyph sequence that is drawable by \
24731 the same face, and append them to HEAD/TAIL. */ \
24732 for (n = 0; n < cmp->glyph_len;) \
24734 s = alloca (sizeof *s); \
24735 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24736 append_glyph_string (&(HEAD), &(TAIL), s); \
24737 s->cmp = cmp; \
24738 s->cmp_from = n; \
24739 s->x = (X); \
24740 if (n == 0) \
24741 first_s = s; \
24742 n = fill_composite_glyph_string (s, base_face, overlaps); \
24745 ++START; \
24746 s = first_s; \
24747 } while (0)
24750 /* Add a glyph string for a glyph-string sequence to the list of strings
24751 between HEAD and TAIL. */
24753 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24754 do { \
24755 int face_id; \
24756 XChar2b *char2b; \
24757 Lisp_Object gstring; \
24759 face_id = (row)->glyphs[area][START].face_id; \
24760 gstring = (composition_gstring_from_id \
24761 ((row)->glyphs[area][START].u.cmp.id)); \
24762 s = alloca (sizeof *s); \
24763 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
24764 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
24765 append_glyph_string (&(HEAD), &(TAIL), s); \
24766 s->x = (X); \
24767 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
24768 } while (0)
24771 /* Add a glyph string for a sequence of glyphless character's glyphs
24772 to the list of strings between HEAD and TAIL. The meanings of
24773 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
24775 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
24776 do \
24778 int face_id; \
24780 face_id = (row)->glyphs[area][START].face_id; \
24782 s = alloca (sizeof *s); \
24783 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
24784 append_glyph_string (&HEAD, &TAIL, s); \
24785 s->x = (X); \
24786 START = fill_glyphless_glyph_string (s, face_id, START, END, \
24787 overlaps); \
24789 while (0)
24792 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
24793 of AREA of glyph row ROW on window W between indices START and END.
24794 HL overrides the face for drawing glyph strings, e.g. it is
24795 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
24796 x-positions of the drawing area.
24798 This is an ugly monster macro construct because we must use alloca
24799 to allocate glyph strings (because draw_glyphs can be called
24800 asynchronously). */
24802 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
24803 do \
24805 HEAD = TAIL = NULL; \
24806 while (START < END) \
24808 struct glyph *first_glyph = (row)->glyphs[area] + START; \
24809 switch (first_glyph->type) \
24811 case CHAR_GLYPH: \
24812 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
24813 HL, X, LAST_X); \
24814 break; \
24816 case COMPOSITE_GLYPH: \
24817 if (first_glyph->u.cmp.automatic) \
24818 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
24819 HL, X, LAST_X); \
24820 else \
24821 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
24822 HL, X, LAST_X); \
24823 break; \
24825 case STRETCH_GLYPH: \
24826 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
24827 HL, X, LAST_X); \
24828 break; \
24830 case IMAGE_GLYPH: \
24831 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
24832 HL, X, LAST_X); \
24833 break; \
24835 case GLYPHLESS_GLYPH: \
24836 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
24837 HL, X, LAST_X); \
24838 break; \
24840 default: \
24841 emacs_abort (); \
24844 if (s) \
24846 set_glyph_string_background_width (s, START, LAST_X); \
24847 (X) += s->width; \
24850 } while (0)
24853 /* Draw glyphs between START and END in AREA of ROW on window W,
24854 starting at x-position X. X is relative to AREA in W. HL is a
24855 face-override with the following meaning:
24857 DRAW_NORMAL_TEXT draw normally
24858 DRAW_CURSOR draw in cursor face
24859 DRAW_MOUSE_FACE draw in mouse face.
24860 DRAW_INVERSE_VIDEO draw in mode line face
24861 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
24862 DRAW_IMAGE_RAISED draw an image with a raised relief around it
24864 If OVERLAPS is non-zero, draw only the foreground of characters and
24865 clip to the physical height of ROW. Non-zero value also defines
24866 the overlapping part to be drawn:
24868 OVERLAPS_PRED overlap with preceding rows
24869 OVERLAPS_SUCC overlap with succeeding rows
24870 OVERLAPS_BOTH overlap with both preceding/succeeding rows
24871 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
24873 Value is the x-position reached, relative to AREA of W. */
24875 static int
24876 draw_glyphs (struct window *w, int x, struct glyph_row *row,
24877 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
24878 enum draw_glyphs_face hl, int overlaps)
24880 struct glyph_string *head, *tail;
24881 struct glyph_string *s;
24882 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
24883 int i, j, x_reached, last_x, area_left = 0;
24884 struct frame *f = XFRAME (WINDOW_FRAME (w));
24885 DECLARE_HDC (hdc);
24887 ALLOCATE_HDC (hdc, f);
24889 /* Let's rather be paranoid than getting a SEGV. */
24890 end = min (end, row->used[area]);
24891 start = clip_to_bounds (0, start, end);
24893 /* Translate X to frame coordinates. Set last_x to the right
24894 end of the drawing area. */
24895 if (row->full_width_p)
24897 /* X is relative to the left edge of W, without scroll bars
24898 or fringes. */
24899 area_left = WINDOW_LEFT_EDGE_X (w);
24900 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
24901 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
24903 else
24905 area_left = window_box_left (w, area);
24906 last_x = area_left + window_box_width (w, area);
24908 x += area_left;
24910 /* Build a doubly-linked list of glyph_string structures between
24911 head and tail from what we have to draw. Note that the macro
24912 BUILD_GLYPH_STRINGS will modify its start parameter. That's
24913 the reason we use a separate variable `i'. */
24914 i = start;
24915 USE_SAFE_ALLOCA;
24916 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
24917 if (tail)
24918 x_reached = tail->x + tail->background_width;
24919 else
24920 x_reached = x;
24922 /* If there are any glyphs with lbearing < 0 or rbearing > width in
24923 the row, redraw some glyphs in front or following the glyph
24924 strings built above. */
24925 if (head && !overlaps && row->contains_overlapping_glyphs_p)
24927 struct glyph_string *h, *t;
24928 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24929 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
24930 int check_mouse_face = 0;
24931 int dummy_x = 0;
24933 /* If mouse highlighting is on, we may need to draw adjacent
24934 glyphs using mouse-face highlighting. */
24935 if (area == TEXT_AREA && row->mouse_face_p
24936 && hlinfo->mouse_face_beg_row >= 0
24937 && hlinfo->mouse_face_end_row >= 0)
24939 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
24941 if (row_vpos >= hlinfo->mouse_face_beg_row
24942 && row_vpos <= hlinfo->mouse_face_end_row)
24944 check_mouse_face = 1;
24945 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
24946 ? hlinfo->mouse_face_beg_col : 0;
24947 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
24948 ? hlinfo->mouse_face_end_col
24949 : row->used[TEXT_AREA];
24953 /* Compute overhangs for all glyph strings. */
24954 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
24955 for (s = head; s; s = s->next)
24956 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
24958 /* Prepend glyph strings for glyphs in front of the first glyph
24959 string that are overwritten because of the first glyph
24960 string's left overhang. The background of all strings
24961 prepended must be drawn because the first glyph string
24962 draws over it. */
24963 i = left_overwritten (head);
24964 if (i >= 0)
24966 enum draw_glyphs_face overlap_hl;
24968 /* If this row contains mouse highlighting, attempt to draw
24969 the overlapped glyphs with the correct highlight. This
24970 code fails if the overlap encompasses more than one glyph
24971 and mouse-highlight spans only some of these glyphs.
24972 However, making it work perfectly involves a lot more
24973 code, and I don't know if the pathological case occurs in
24974 practice, so we'll stick to this for now. --- cyd */
24975 if (check_mouse_face
24976 && mouse_beg_col < start && mouse_end_col > i)
24977 overlap_hl = DRAW_MOUSE_FACE;
24978 else
24979 overlap_hl = DRAW_NORMAL_TEXT;
24981 if (hl != overlap_hl)
24982 clip_head = head;
24983 j = i;
24984 BUILD_GLYPH_STRINGS (j, start, h, t,
24985 overlap_hl, dummy_x, last_x);
24986 start = i;
24987 compute_overhangs_and_x (t, head->x, 1);
24988 prepend_glyph_string_lists (&head, &tail, h, t);
24989 if (clip_head == NULL)
24990 clip_head = head;
24993 /* Prepend glyph strings for glyphs in front of the first glyph
24994 string that overwrite that glyph string because of their
24995 right overhang. For these strings, only the foreground must
24996 be drawn, because it draws over the glyph string at `head'.
24997 The background must not be drawn because this would overwrite
24998 right overhangs of preceding glyphs for which no glyph
24999 strings exist. */
25000 i = left_overwriting (head);
25001 if (i >= 0)
25003 enum draw_glyphs_face overlap_hl;
25005 if (check_mouse_face
25006 && mouse_beg_col < start && mouse_end_col > i)
25007 overlap_hl = DRAW_MOUSE_FACE;
25008 else
25009 overlap_hl = DRAW_NORMAL_TEXT;
25011 if (hl == overlap_hl || clip_head == NULL)
25012 clip_head = head;
25013 BUILD_GLYPH_STRINGS (i, start, h, t,
25014 overlap_hl, dummy_x, last_x);
25015 for (s = h; s; s = s->next)
25016 s->background_filled_p = 1;
25017 compute_overhangs_and_x (t, head->x, 1);
25018 prepend_glyph_string_lists (&head, &tail, h, t);
25021 /* Append glyphs strings for glyphs following the last glyph
25022 string tail that are overwritten by tail. The background of
25023 these strings has to be drawn because tail's foreground draws
25024 over it. */
25025 i = right_overwritten (tail);
25026 if (i >= 0)
25028 enum draw_glyphs_face overlap_hl;
25030 if (check_mouse_face
25031 && mouse_beg_col < i && mouse_end_col > end)
25032 overlap_hl = DRAW_MOUSE_FACE;
25033 else
25034 overlap_hl = DRAW_NORMAL_TEXT;
25036 if (hl != overlap_hl)
25037 clip_tail = tail;
25038 BUILD_GLYPH_STRINGS (end, i, h, t,
25039 overlap_hl, x, last_x);
25040 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25041 we don't have `end = i;' here. */
25042 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25043 append_glyph_string_lists (&head, &tail, h, t);
25044 if (clip_tail == NULL)
25045 clip_tail = tail;
25048 /* Append glyph strings for glyphs following the last glyph
25049 string tail that overwrite tail. The foreground of such
25050 glyphs has to be drawn because it writes into the background
25051 of tail. The background must not be drawn because it could
25052 paint over the foreground of following glyphs. */
25053 i = right_overwriting (tail);
25054 if (i >= 0)
25056 enum draw_glyphs_face overlap_hl;
25057 if (check_mouse_face
25058 && mouse_beg_col < i && mouse_end_col > end)
25059 overlap_hl = DRAW_MOUSE_FACE;
25060 else
25061 overlap_hl = DRAW_NORMAL_TEXT;
25063 if (hl == overlap_hl || clip_tail == NULL)
25064 clip_tail = tail;
25065 i++; /* We must include the Ith glyph. */
25066 BUILD_GLYPH_STRINGS (end, i, h, t,
25067 overlap_hl, x, last_x);
25068 for (s = h; s; s = s->next)
25069 s->background_filled_p = 1;
25070 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25071 append_glyph_string_lists (&head, &tail, h, t);
25073 if (clip_head || clip_tail)
25074 for (s = head; s; s = s->next)
25076 s->clip_head = clip_head;
25077 s->clip_tail = clip_tail;
25081 /* Draw all strings. */
25082 for (s = head; s; s = s->next)
25083 FRAME_RIF (f)->draw_glyph_string (s);
25085 #ifndef HAVE_NS
25086 /* When focus a sole frame and move horizontally, this sets on_p to 0
25087 causing a failure to erase prev cursor position. */
25088 if (area == TEXT_AREA
25089 && !row->full_width_p
25090 /* When drawing overlapping rows, only the glyph strings'
25091 foreground is drawn, which doesn't erase a cursor
25092 completely. */
25093 && !overlaps)
25095 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25096 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25097 : (tail ? tail->x + tail->background_width : x));
25098 x0 -= area_left;
25099 x1 -= area_left;
25101 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25102 row->y, MATRIX_ROW_BOTTOM_Y (row));
25104 #endif
25106 /* Value is the x-position up to which drawn, relative to AREA of W.
25107 This doesn't include parts drawn because of overhangs. */
25108 if (row->full_width_p)
25109 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25110 else
25111 x_reached -= area_left;
25113 RELEASE_HDC (hdc, f);
25115 SAFE_FREE ();
25116 return x_reached;
25119 /* Expand row matrix if too narrow. Don't expand if area
25120 is not present. */
25122 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25124 if (!it->f->fonts_changed \
25125 && (it->glyph_row->glyphs[area] \
25126 < it->glyph_row->glyphs[area + 1])) \
25128 it->w->ncols_scale_factor++; \
25129 it->f->fonts_changed = 1; \
25133 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25134 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25136 static void
25137 append_glyph (struct it *it)
25139 struct glyph *glyph;
25140 enum glyph_row_area area = it->area;
25142 eassert (it->glyph_row);
25143 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25145 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25146 if (glyph < it->glyph_row->glyphs[area + 1])
25148 /* If the glyph row is reversed, we need to prepend the glyph
25149 rather than append it. */
25150 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25152 struct glyph *g;
25154 /* Make room for the additional glyph. */
25155 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25156 g[1] = *g;
25157 glyph = it->glyph_row->glyphs[area];
25159 glyph->charpos = CHARPOS (it->position);
25160 glyph->object = it->object;
25161 if (it->pixel_width > 0)
25163 glyph->pixel_width = it->pixel_width;
25164 glyph->padding_p = 0;
25166 else
25168 /* Assure at least 1-pixel width. Otherwise, cursor can't
25169 be displayed correctly. */
25170 glyph->pixel_width = 1;
25171 glyph->padding_p = 1;
25173 glyph->ascent = it->ascent;
25174 glyph->descent = it->descent;
25175 glyph->voffset = it->voffset;
25176 glyph->type = CHAR_GLYPH;
25177 glyph->avoid_cursor_p = it->avoid_cursor_p;
25178 glyph->multibyte_p = it->multibyte_p;
25179 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25181 /* In R2L rows, the left and the right box edges need to be
25182 drawn in reverse direction. */
25183 glyph->right_box_line_p = it->start_of_box_run_p;
25184 glyph->left_box_line_p = it->end_of_box_run_p;
25186 else
25188 glyph->left_box_line_p = it->start_of_box_run_p;
25189 glyph->right_box_line_p = it->end_of_box_run_p;
25191 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25192 || it->phys_descent > it->descent);
25193 glyph->glyph_not_available_p = it->glyph_not_available_p;
25194 glyph->face_id = it->face_id;
25195 glyph->u.ch = it->char_to_display;
25196 glyph->slice.img = null_glyph_slice;
25197 glyph->font_type = FONT_TYPE_UNKNOWN;
25198 if (it->bidi_p)
25200 glyph->resolved_level = it->bidi_it.resolved_level;
25201 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25202 emacs_abort ();
25203 glyph->bidi_type = it->bidi_it.type;
25205 else
25207 glyph->resolved_level = 0;
25208 glyph->bidi_type = UNKNOWN_BT;
25210 ++it->glyph_row->used[area];
25212 else
25213 IT_EXPAND_MATRIX_WIDTH (it, area);
25216 /* Store one glyph for the composition IT->cmp_it.id in
25217 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25218 non-null. */
25220 static void
25221 append_composite_glyph (struct it *it)
25223 struct glyph *glyph;
25224 enum glyph_row_area area = it->area;
25226 eassert (it->glyph_row);
25228 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25229 if (glyph < it->glyph_row->glyphs[area + 1])
25231 /* If the glyph row is reversed, we need to prepend the glyph
25232 rather than append it. */
25233 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25235 struct glyph *g;
25237 /* Make room for the new glyph. */
25238 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25239 g[1] = *g;
25240 glyph = it->glyph_row->glyphs[it->area];
25242 glyph->charpos = it->cmp_it.charpos;
25243 glyph->object = it->object;
25244 glyph->pixel_width = it->pixel_width;
25245 glyph->ascent = it->ascent;
25246 glyph->descent = it->descent;
25247 glyph->voffset = it->voffset;
25248 glyph->type = COMPOSITE_GLYPH;
25249 if (it->cmp_it.ch < 0)
25251 glyph->u.cmp.automatic = 0;
25252 glyph->u.cmp.id = it->cmp_it.id;
25253 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25255 else
25257 glyph->u.cmp.automatic = 1;
25258 glyph->u.cmp.id = it->cmp_it.id;
25259 glyph->slice.cmp.from = it->cmp_it.from;
25260 glyph->slice.cmp.to = it->cmp_it.to - 1;
25262 glyph->avoid_cursor_p = it->avoid_cursor_p;
25263 glyph->multibyte_p = it->multibyte_p;
25264 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25266 /* In R2L rows, the left and the right box edges need to be
25267 drawn in reverse direction. */
25268 glyph->right_box_line_p = it->start_of_box_run_p;
25269 glyph->left_box_line_p = it->end_of_box_run_p;
25271 else
25273 glyph->left_box_line_p = it->start_of_box_run_p;
25274 glyph->right_box_line_p = it->end_of_box_run_p;
25276 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25277 || it->phys_descent > it->descent);
25278 glyph->padding_p = 0;
25279 glyph->glyph_not_available_p = 0;
25280 glyph->face_id = it->face_id;
25281 glyph->font_type = FONT_TYPE_UNKNOWN;
25282 if (it->bidi_p)
25284 glyph->resolved_level = it->bidi_it.resolved_level;
25285 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25286 emacs_abort ();
25287 glyph->bidi_type = it->bidi_it.type;
25289 ++it->glyph_row->used[area];
25291 else
25292 IT_EXPAND_MATRIX_WIDTH (it, area);
25296 /* Change IT->ascent and IT->height according to the setting of
25297 IT->voffset. */
25299 static void
25300 take_vertical_position_into_account (struct it *it)
25302 if (it->voffset)
25304 if (it->voffset < 0)
25305 /* Increase the ascent so that we can display the text higher
25306 in the line. */
25307 it->ascent -= it->voffset;
25308 else
25309 /* Increase the descent so that we can display the text lower
25310 in the line. */
25311 it->descent += it->voffset;
25316 /* Produce glyphs/get display metrics for the image IT is loaded with.
25317 See the description of struct display_iterator in dispextern.h for
25318 an overview of struct display_iterator. */
25320 static void
25321 produce_image_glyph (struct it *it)
25323 struct image *img;
25324 struct face *face;
25325 int glyph_ascent, crop;
25326 struct glyph_slice slice;
25328 eassert (it->what == IT_IMAGE);
25330 face = FACE_FROM_ID (it->f, it->face_id);
25331 eassert (face);
25332 /* Make sure X resources of the face is loaded. */
25333 prepare_face_for_display (it->f, face);
25335 if (it->image_id < 0)
25337 /* Fringe bitmap. */
25338 it->ascent = it->phys_ascent = 0;
25339 it->descent = it->phys_descent = 0;
25340 it->pixel_width = 0;
25341 it->nglyphs = 0;
25342 return;
25345 img = IMAGE_FROM_ID (it->f, it->image_id);
25346 eassert (img);
25347 /* Make sure X resources of the image is loaded. */
25348 prepare_image_for_display (it->f, img);
25350 slice.x = slice.y = 0;
25351 slice.width = img->width;
25352 slice.height = img->height;
25354 if (INTEGERP (it->slice.x))
25355 slice.x = XINT (it->slice.x);
25356 else if (FLOATP (it->slice.x))
25357 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25359 if (INTEGERP (it->slice.y))
25360 slice.y = XINT (it->slice.y);
25361 else if (FLOATP (it->slice.y))
25362 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25364 if (INTEGERP (it->slice.width))
25365 slice.width = XINT (it->slice.width);
25366 else if (FLOATP (it->slice.width))
25367 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25369 if (INTEGERP (it->slice.height))
25370 slice.height = XINT (it->slice.height);
25371 else if (FLOATP (it->slice.height))
25372 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25374 if (slice.x >= img->width)
25375 slice.x = img->width;
25376 if (slice.y >= img->height)
25377 slice.y = img->height;
25378 if (slice.x + slice.width >= img->width)
25379 slice.width = img->width - slice.x;
25380 if (slice.y + slice.height > img->height)
25381 slice.height = img->height - slice.y;
25383 if (slice.width == 0 || slice.height == 0)
25384 return;
25386 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25388 it->descent = slice.height - glyph_ascent;
25389 if (slice.y == 0)
25390 it->descent += img->vmargin;
25391 if (slice.y + slice.height == img->height)
25392 it->descent += img->vmargin;
25393 it->phys_descent = it->descent;
25395 it->pixel_width = slice.width;
25396 if (slice.x == 0)
25397 it->pixel_width += img->hmargin;
25398 if (slice.x + slice.width == img->width)
25399 it->pixel_width += img->hmargin;
25401 /* It's quite possible for images to have an ascent greater than
25402 their height, so don't get confused in that case. */
25403 if (it->descent < 0)
25404 it->descent = 0;
25406 it->nglyphs = 1;
25408 if (face->box != FACE_NO_BOX)
25410 if (face->box_line_width > 0)
25412 if (slice.y == 0)
25413 it->ascent += face->box_line_width;
25414 if (slice.y + slice.height == img->height)
25415 it->descent += face->box_line_width;
25418 if (it->start_of_box_run_p && slice.x == 0)
25419 it->pixel_width += eabs (face->box_line_width);
25420 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25421 it->pixel_width += eabs (face->box_line_width);
25424 take_vertical_position_into_account (it);
25426 /* Automatically crop wide image glyphs at right edge so we can
25427 draw the cursor on same display row. */
25428 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25429 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25431 it->pixel_width -= crop;
25432 slice.width -= crop;
25435 if (it->glyph_row)
25437 struct glyph *glyph;
25438 enum glyph_row_area area = it->area;
25440 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25441 if (glyph < it->glyph_row->glyphs[area + 1])
25443 glyph->charpos = CHARPOS (it->position);
25444 glyph->object = it->object;
25445 glyph->pixel_width = it->pixel_width;
25446 glyph->ascent = glyph_ascent;
25447 glyph->descent = it->descent;
25448 glyph->voffset = it->voffset;
25449 glyph->type = IMAGE_GLYPH;
25450 glyph->avoid_cursor_p = it->avoid_cursor_p;
25451 glyph->multibyte_p = it->multibyte_p;
25452 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25454 /* In R2L rows, the left and the right box edges need to be
25455 drawn in reverse direction. */
25456 glyph->right_box_line_p = it->start_of_box_run_p;
25457 glyph->left_box_line_p = it->end_of_box_run_p;
25459 else
25461 glyph->left_box_line_p = it->start_of_box_run_p;
25462 glyph->right_box_line_p = it->end_of_box_run_p;
25464 glyph->overlaps_vertically_p = 0;
25465 glyph->padding_p = 0;
25466 glyph->glyph_not_available_p = 0;
25467 glyph->face_id = it->face_id;
25468 glyph->u.img_id = img->id;
25469 glyph->slice.img = slice;
25470 glyph->font_type = FONT_TYPE_UNKNOWN;
25471 if (it->bidi_p)
25473 glyph->resolved_level = it->bidi_it.resolved_level;
25474 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25475 emacs_abort ();
25476 glyph->bidi_type = it->bidi_it.type;
25478 ++it->glyph_row->used[area];
25480 else
25481 IT_EXPAND_MATRIX_WIDTH (it, area);
25486 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25487 of the glyph, WIDTH and HEIGHT are the width and height of the
25488 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25490 static void
25491 append_stretch_glyph (struct it *it, Lisp_Object object,
25492 int width, int height, int ascent)
25494 struct glyph *glyph;
25495 enum glyph_row_area area = it->area;
25497 eassert (ascent >= 0 && ascent <= height);
25499 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25500 if (glyph < it->glyph_row->glyphs[area + 1])
25502 /* If the glyph row is reversed, we need to prepend the glyph
25503 rather than append it. */
25504 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25506 struct glyph *g;
25508 /* Make room for the additional glyph. */
25509 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25510 g[1] = *g;
25511 glyph = it->glyph_row->glyphs[area];
25513 /* Decrease the width of the first glyph of the row that
25514 begins before first_visible_x (e.g., due to hscroll).
25515 This is so the overall width of the row becomes smaller
25516 by the scroll amount, and the stretch glyph appended by
25517 extend_face_to_end_of_line will be wider, to shift the
25518 row glyphs to the right. (In L2R rows, the corresponding
25519 left-shift effect is accomplished by setting row->x to a
25520 negative value, which won't work with R2L rows.)
25522 This must leave us with a positive value of WIDTH, since
25523 otherwise the call to move_it_in_display_line_to at the
25524 beginning of display_line would have got past the entire
25525 first glyph, and then it->current_x would have been
25526 greater or equal to it->first_visible_x. */
25527 if (it->current_x < it->first_visible_x)
25528 width -= it->first_visible_x - it->current_x;
25529 eassert (width > 0);
25531 glyph->charpos = CHARPOS (it->position);
25532 glyph->object = object;
25533 glyph->pixel_width = width;
25534 glyph->ascent = ascent;
25535 glyph->descent = height - ascent;
25536 glyph->voffset = it->voffset;
25537 glyph->type = STRETCH_GLYPH;
25538 glyph->avoid_cursor_p = it->avoid_cursor_p;
25539 glyph->multibyte_p = it->multibyte_p;
25540 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25542 /* In R2L rows, the left and the right box edges need to be
25543 drawn in reverse direction. */
25544 glyph->right_box_line_p = it->start_of_box_run_p;
25545 glyph->left_box_line_p = it->end_of_box_run_p;
25547 else
25549 glyph->left_box_line_p = it->start_of_box_run_p;
25550 glyph->right_box_line_p = it->end_of_box_run_p;
25552 glyph->overlaps_vertically_p = 0;
25553 glyph->padding_p = 0;
25554 glyph->glyph_not_available_p = 0;
25555 glyph->face_id = it->face_id;
25556 glyph->u.stretch.ascent = ascent;
25557 glyph->u.stretch.height = height;
25558 glyph->slice.img = null_glyph_slice;
25559 glyph->font_type = FONT_TYPE_UNKNOWN;
25560 if (it->bidi_p)
25562 glyph->resolved_level = it->bidi_it.resolved_level;
25563 if ((it->bidi_it.type & 7) != it->bidi_it.type)
25564 emacs_abort ();
25565 glyph->bidi_type = it->bidi_it.type;
25567 else
25569 glyph->resolved_level = 0;
25570 glyph->bidi_type = UNKNOWN_BT;
25572 ++it->glyph_row->used[area];
25574 else
25575 IT_EXPAND_MATRIX_WIDTH (it, area);
25578 #endif /* HAVE_WINDOW_SYSTEM */
25580 /* Produce a stretch glyph for iterator IT. IT->object is the value
25581 of the glyph property displayed. The value must be a list
25582 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
25583 being recognized:
25585 1. `:width WIDTH' specifies that the space should be WIDTH *
25586 canonical char width wide. WIDTH may be an integer or floating
25587 point number.
25589 2. `:relative-width FACTOR' specifies that the width of the stretch
25590 should be computed from the width of the first character having the
25591 `glyph' property, and should be FACTOR times that width.
25593 3. `:align-to HPOS' specifies that the space should be wide enough
25594 to reach HPOS, a value in canonical character units.
25596 Exactly one of the above pairs must be present.
25598 4. `:height HEIGHT' specifies that the height of the stretch produced
25599 should be HEIGHT, measured in canonical character units.
25601 5. `:relative-height FACTOR' specifies that the height of the
25602 stretch should be FACTOR times the height of the characters having
25603 the glyph property.
25605 Either none or exactly one of 4 or 5 must be present.
25607 6. `:ascent ASCENT' specifies that ASCENT percent of the height
25608 of the stretch should be used for the ascent of the stretch.
25609 ASCENT must be in the range 0 <= ASCENT <= 100. */
25611 void
25612 produce_stretch_glyph (struct it *it)
25614 /* (space :width WIDTH :height HEIGHT ...) */
25615 Lisp_Object prop, plist;
25616 int width = 0, height = 0, align_to = -1;
25617 int zero_width_ok_p = 0;
25618 double tem;
25619 struct font *font = NULL;
25621 #ifdef HAVE_WINDOW_SYSTEM
25622 int ascent = 0;
25623 int zero_height_ok_p = 0;
25625 if (FRAME_WINDOW_P (it->f))
25627 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25628 font = face->font ? face->font : FRAME_FONT (it->f);
25629 prepare_face_for_display (it->f, face);
25631 #endif
25633 /* List should start with `space'. */
25634 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
25635 plist = XCDR (it->object);
25637 /* Compute the width of the stretch. */
25638 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
25639 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
25641 /* Absolute width `:width WIDTH' specified and valid. */
25642 zero_width_ok_p = 1;
25643 width = (int)tem;
25645 #ifdef HAVE_WINDOW_SYSTEM
25646 else if (FRAME_WINDOW_P (it->f)
25647 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
25649 /* Relative width `:relative-width FACTOR' specified and valid.
25650 Compute the width of the characters having the `glyph'
25651 property. */
25652 struct it it2;
25653 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
25655 it2 = *it;
25656 if (it->multibyte_p)
25657 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
25658 else
25660 it2.c = it2.char_to_display = *p, it2.len = 1;
25661 if (! ASCII_CHAR_P (it2.c))
25662 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
25665 it2.glyph_row = NULL;
25666 it2.what = IT_CHARACTER;
25667 x_produce_glyphs (&it2);
25668 width = NUMVAL (prop) * it2.pixel_width;
25670 #endif /* HAVE_WINDOW_SYSTEM */
25671 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
25672 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
25674 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
25675 align_to = (align_to < 0
25677 : align_to - window_box_left_offset (it->w, TEXT_AREA));
25678 else if (align_to < 0)
25679 align_to = window_box_left_offset (it->w, TEXT_AREA);
25680 width = max (0, (int)tem + align_to - it->current_x);
25681 zero_width_ok_p = 1;
25683 else
25684 /* Nothing specified -> width defaults to canonical char width. */
25685 width = FRAME_COLUMN_WIDTH (it->f);
25687 if (width <= 0 && (width < 0 || !zero_width_ok_p))
25688 width = 1;
25690 #ifdef HAVE_WINDOW_SYSTEM
25691 /* Compute height. */
25692 if (FRAME_WINDOW_P (it->f))
25694 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
25695 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25697 height = (int)tem;
25698 zero_height_ok_p = 1;
25700 else if (prop = Fplist_get (plist, QCrelative_height),
25701 NUMVAL (prop) > 0)
25702 height = FONT_HEIGHT (font) * NUMVAL (prop);
25703 else
25704 height = FONT_HEIGHT (font);
25706 if (height <= 0 && (height < 0 || !zero_height_ok_p))
25707 height = 1;
25709 /* Compute percentage of height used for ascent. If
25710 `:ascent ASCENT' is present and valid, use that. Otherwise,
25711 derive the ascent from the font in use. */
25712 if (prop = Fplist_get (plist, QCascent),
25713 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
25714 ascent = height * NUMVAL (prop) / 100.0;
25715 else if (!NILP (prop)
25716 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
25717 ascent = min (max (0, (int)tem), height);
25718 else
25719 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
25721 else
25722 #endif /* HAVE_WINDOW_SYSTEM */
25723 height = 1;
25725 if (width > 0 && it->line_wrap != TRUNCATE
25726 && it->current_x + width > it->last_visible_x)
25728 width = it->last_visible_x - it->current_x;
25729 #ifdef HAVE_WINDOW_SYSTEM
25730 /* Subtract one more pixel from the stretch width, but only on
25731 GUI frames, since on a TTY each glyph is one "pixel" wide. */
25732 width -= FRAME_WINDOW_P (it->f);
25733 #endif
25736 if (width > 0 && height > 0 && it->glyph_row)
25738 Lisp_Object o_object = it->object;
25739 Lisp_Object object = it->stack[it->sp - 1].string;
25740 int n = width;
25742 if (!STRINGP (object))
25743 object = it->w->contents;
25744 #ifdef HAVE_WINDOW_SYSTEM
25745 if (FRAME_WINDOW_P (it->f))
25746 append_stretch_glyph (it, object, width, height, ascent);
25747 else
25748 #endif
25750 it->object = object;
25751 it->char_to_display = ' ';
25752 it->pixel_width = it->len = 1;
25753 while (n--)
25754 tty_append_glyph (it);
25755 it->object = o_object;
25759 it->pixel_width = width;
25760 #ifdef HAVE_WINDOW_SYSTEM
25761 if (FRAME_WINDOW_P (it->f))
25763 it->ascent = it->phys_ascent = ascent;
25764 it->descent = it->phys_descent = height - it->ascent;
25765 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
25766 take_vertical_position_into_account (it);
25768 else
25769 #endif
25770 it->nglyphs = width;
25773 /* Get information about special display element WHAT in an
25774 environment described by IT. WHAT is one of IT_TRUNCATION or
25775 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
25776 non-null glyph_row member. This function ensures that fields like
25777 face_id, c, len of IT are left untouched. */
25779 static void
25780 produce_special_glyphs (struct it *it, enum display_element_type what)
25782 struct it temp_it;
25783 Lisp_Object gc;
25784 GLYPH glyph;
25786 temp_it = *it;
25787 temp_it.object = make_number (0);
25788 memset (&temp_it.current, 0, sizeof temp_it.current);
25790 if (what == IT_CONTINUATION)
25792 /* Continuation glyph. For R2L lines, we mirror it by hand. */
25793 if (it->bidi_it.paragraph_dir == R2L)
25794 SET_GLYPH_FROM_CHAR (glyph, '/');
25795 else
25796 SET_GLYPH_FROM_CHAR (glyph, '\\');
25797 if (it->dp
25798 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25800 /* FIXME: Should we mirror GC for R2L lines? */
25801 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25802 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25805 else if (what == IT_TRUNCATION)
25807 /* Truncation glyph. */
25808 SET_GLYPH_FROM_CHAR (glyph, '$');
25809 if (it->dp
25810 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
25812 /* FIXME: Should we mirror GC for R2L lines? */
25813 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
25814 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
25817 else
25818 emacs_abort ();
25820 #ifdef HAVE_WINDOW_SYSTEM
25821 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
25822 is turned off, we precede the truncation/continuation glyphs by a
25823 stretch glyph whose width is computed such that these special
25824 glyphs are aligned at the window margin, even when very different
25825 fonts are used in different glyph rows. */
25826 if (FRAME_WINDOW_P (temp_it.f)
25827 /* init_iterator calls this with it->glyph_row == NULL, and it
25828 wants only the pixel width of the truncation/continuation
25829 glyphs. */
25830 && temp_it.glyph_row
25831 /* insert_left_trunc_glyphs calls us at the beginning of the
25832 row, and it has its own calculation of the stretch glyph
25833 width. */
25834 && temp_it.glyph_row->used[TEXT_AREA] > 0
25835 && (temp_it.glyph_row->reversed_p
25836 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
25837 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
25839 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
25841 if (stretch_width > 0)
25843 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
25844 struct font *font =
25845 face->font ? face->font : FRAME_FONT (temp_it.f);
25846 int stretch_ascent =
25847 (((temp_it.ascent + temp_it.descent)
25848 * FONT_BASE (font)) / FONT_HEIGHT (font));
25850 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
25851 temp_it.ascent + temp_it.descent,
25852 stretch_ascent);
25855 #endif
25857 temp_it.dp = NULL;
25858 temp_it.what = IT_CHARACTER;
25859 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
25860 temp_it.face_id = GLYPH_FACE (glyph);
25861 temp_it.len = CHAR_BYTES (temp_it.c);
25863 PRODUCE_GLYPHS (&temp_it);
25864 it->pixel_width = temp_it.pixel_width;
25865 it->nglyphs = temp_it.nglyphs;
25868 #ifdef HAVE_WINDOW_SYSTEM
25870 /* Calculate line-height and line-spacing properties.
25871 An integer value specifies explicit pixel value.
25872 A float value specifies relative value to current face height.
25873 A cons (float . face-name) specifies relative value to
25874 height of specified face font.
25876 Returns height in pixels, or nil. */
25879 static Lisp_Object
25880 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
25881 int boff, int override)
25883 Lisp_Object face_name = Qnil;
25884 int ascent, descent, height;
25886 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
25887 return val;
25889 if (CONSP (val))
25891 face_name = XCAR (val);
25892 val = XCDR (val);
25893 if (!NUMBERP (val))
25894 val = make_number (1);
25895 if (NILP (face_name))
25897 height = it->ascent + it->descent;
25898 goto scale;
25902 if (NILP (face_name))
25904 font = FRAME_FONT (it->f);
25905 boff = FRAME_BASELINE_OFFSET (it->f);
25907 else if (EQ (face_name, Qt))
25909 override = 0;
25911 else
25913 int face_id;
25914 struct face *face;
25916 face_id = lookup_named_face (it->f, face_name, 0);
25917 if (face_id < 0)
25918 return make_number (-1);
25920 face = FACE_FROM_ID (it->f, face_id);
25921 font = face->font;
25922 if (font == NULL)
25923 return make_number (-1);
25924 boff = font->baseline_offset;
25925 if (font->vertical_centering)
25926 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25929 ascent = FONT_BASE (font) + boff;
25930 descent = FONT_DESCENT (font) - boff;
25932 if (override)
25934 it->override_ascent = ascent;
25935 it->override_descent = descent;
25936 it->override_boff = boff;
25939 height = ascent + descent;
25941 scale:
25942 if (FLOATP (val))
25943 height = (int)(XFLOAT_DATA (val) * height);
25944 else if (INTEGERP (val))
25945 height *= XINT (val);
25947 return make_number (height);
25951 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
25952 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
25953 and only if this is for a character for which no font was found.
25955 If the display method (it->glyphless_method) is
25956 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
25957 length of the acronym or the hexadecimal string, UPPER_XOFF and
25958 UPPER_YOFF are pixel offsets for the upper part of the string,
25959 LOWER_XOFF and LOWER_YOFF are for the lower part.
25961 For the other display methods, LEN through LOWER_YOFF are zero. */
25963 static void
25964 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
25965 short upper_xoff, short upper_yoff,
25966 short lower_xoff, short lower_yoff)
25968 struct glyph *glyph;
25969 enum glyph_row_area area = it->area;
25971 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25972 if (glyph < it->glyph_row->glyphs[area + 1])
25974 /* If the glyph row is reversed, we need to prepend the glyph
25975 rather than append it. */
25976 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25978 struct glyph *g;
25980 /* Make room for the additional glyph. */
25981 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25982 g[1] = *g;
25983 glyph = it->glyph_row->glyphs[area];
25985 glyph->charpos = CHARPOS (it->position);
25986 glyph->object = it->object;
25987 glyph->pixel_width = it->pixel_width;
25988 glyph->ascent = it->ascent;
25989 glyph->descent = it->descent;
25990 glyph->voffset = it->voffset;
25991 glyph->type = GLYPHLESS_GLYPH;
25992 glyph->u.glyphless.method = it->glyphless_method;
25993 glyph->u.glyphless.for_no_font = for_no_font;
25994 glyph->u.glyphless.len = len;
25995 glyph->u.glyphless.ch = it->c;
25996 glyph->slice.glyphless.upper_xoff = upper_xoff;
25997 glyph->slice.glyphless.upper_yoff = upper_yoff;
25998 glyph->slice.glyphless.lower_xoff = lower_xoff;
25999 glyph->slice.glyphless.lower_yoff = lower_yoff;
26000 glyph->avoid_cursor_p = it->avoid_cursor_p;
26001 glyph->multibyte_p = it->multibyte_p;
26002 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26004 /* In R2L rows, the left and the right box edges need to be
26005 drawn in reverse direction. */
26006 glyph->right_box_line_p = it->start_of_box_run_p;
26007 glyph->left_box_line_p = it->end_of_box_run_p;
26009 else
26011 glyph->left_box_line_p = it->start_of_box_run_p;
26012 glyph->right_box_line_p = it->end_of_box_run_p;
26014 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26015 || it->phys_descent > it->descent);
26016 glyph->padding_p = 0;
26017 glyph->glyph_not_available_p = 0;
26018 glyph->face_id = face_id;
26019 glyph->font_type = FONT_TYPE_UNKNOWN;
26020 if (it->bidi_p)
26022 glyph->resolved_level = it->bidi_it.resolved_level;
26023 if ((it->bidi_it.type & 7) != it->bidi_it.type)
26024 emacs_abort ();
26025 glyph->bidi_type = it->bidi_it.type;
26027 ++it->glyph_row->used[area];
26029 else
26030 IT_EXPAND_MATRIX_WIDTH (it, area);
26034 /* Produce a glyph for a glyphless character for iterator IT.
26035 IT->glyphless_method specifies which method to use for displaying
26036 the character. See the description of enum
26037 glyphless_display_method in dispextern.h for the detail.
26039 FOR_NO_FONT is nonzero if and only if this is for a character for
26040 which no font was found. ACRONYM, if non-nil, is an acronym string
26041 for the character. */
26043 static void
26044 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26046 int face_id;
26047 struct face *face;
26048 struct font *font;
26049 int base_width, base_height, width, height;
26050 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26051 int len;
26053 /* Get the metrics of the base font. We always refer to the current
26054 ASCII face. */
26055 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26056 font = face->font ? face->font : FRAME_FONT (it->f);
26057 it->ascent = FONT_BASE (font) + font->baseline_offset;
26058 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26059 base_height = it->ascent + it->descent;
26060 base_width = font->average_width;
26062 face_id = merge_glyphless_glyph_face (it);
26064 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26066 it->pixel_width = THIN_SPACE_WIDTH;
26067 len = 0;
26068 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26070 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26072 width = CHAR_WIDTH (it->c);
26073 if (width == 0)
26074 width = 1;
26075 else if (width > 4)
26076 width = 4;
26077 it->pixel_width = base_width * width;
26078 len = 0;
26079 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26081 else
26083 char buf[7];
26084 const char *str;
26085 unsigned int code[6];
26086 int upper_len;
26087 int ascent, descent;
26088 struct font_metrics metrics_upper, metrics_lower;
26090 face = FACE_FROM_ID (it->f, face_id);
26091 font = face->font ? face->font : FRAME_FONT (it->f);
26092 prepare_face_for_display (it->f, face);
26094 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26096 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26097 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26098 if (CONSP (acronym))
26099 acronym = XCAR (acronym);
26100 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26102 else
26104 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26105 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26106 str = buf;
26108 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26109 code[len] = font->driver->encode_char (font, str[len]);
26110 upper_len = (len + 1) / 2;
26111 font->driver->text_extents (font, code, upper_len,
26112 &metrics_upper);
26113 font->driver->text_extents (font, code + upper_len, len - upper_len,
26114 &metrics_lower);
26118 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26119 width = max (metrics_upper.width, metrics_lower.width) + 4;
26120 upper_xoff = upper_yoff = 2; /* the typical case */
26121 if (base_width >= width)
26123 /* Align the upper to the left, the lower to the right. */
26124 it->pixel_width = base_width;
26125 lower_xoff = base_width - 2 - metrics_lower.width;
26127 else
26129 /* Center the shorter one. */
26130 it->pixel_width = width;
26131 if (metrics_upper.width >= metrics_lower.width)
26132 lower_xoff = (width - metrics_lower.width) / 2;
26133 else
26135 /* FIXME: This code doesn't look right. It formerly was
26136 missing the "lower_xoff = 0;", which couldn't have
26137 been right since it left lower_xoff uninitialized. */
26138 lower_xoff = 0;
26139 upper_xoff = (width - metrics_upper.width) / 2;
26143 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26144 top, bottom, and between upper and lower strings. */
26145 height = (metrics_upper.ascent + metrics_upper.descent
26146 + metrics_lower.ascent + metrics_lower.descent) + 5;
26147 /* Center vertically.
26148 H:base_height, D:base_descent
26149 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26151 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26152 descent = D - H/2 + h/2;
26153 lower_yoff = descent - 2 - ld;
26154 upper_yoff = lower_yoff - la - 1 - ud; */
26155 ascent = - (it->descent - (base_height + height + 1) / 2);
26156 descent = it->descent - (base_height - height) / 2;
26157 lower_yoff = descent - 2 - metrics_lower.descent;
26158 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26159 - metrics_upper.descent);
26160 /* Don't make the height shorter than the base height. */
26161 if (height > base_height)
26163 it->ascent = ascent;
26164 it->descent = descent;
26168 it->phys_ascent = it->ascent;
26169 it->phys_descent = it->descent;
26170 if (it->glyph_row)
26171 append_glyphless_glyph (it, face_id, for_no_font, len,
26172 upper_xoff, upper_yoff,
26173 lower_xoff, lower_yoff);
26174 it->nglyphs = 1;
26175 take_vertical_position_into_account (it);
26179 /* RIF:
26180 Produce glyphs/get display metrics for the display element IT is
26181 loaded with. See the description of struct it in dispextern.h
26182 for an overview of struct it. */
26184 void
26185 x_produce_glyphs (struct it *it)
26187 int extra_line_spacing = it->extra_line_spacing;
26189 it->glyph_not_available_p = 0;
26191 if (it->what == IT_CHARACTER)
26193 XChar2b char2b;
26194 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26195 struct font *font = face->font;
26196 struct font_metrics *pcm = NULL;
26197 int boff; /* Baseline offset. */
26199 if (font == NULL)
26201 /* When no suitable font is found, display this character by
26202 the method specified in the first extra slot of
26203 Vglyphless_char_display. */
26204 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26206 eassert (it->what == IT_GLYPHLESS);
26207 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26208 goto done;
26211 boff = font->baseline_offset;
26212 if (font->vertical_centering)
26213 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26215 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26217 int stretched_p;
26219 it->nglyphs = 1;
26221 if (it->override_ascent >= 0)
26223 it->ascent = it->override_ascent;
26224 it->descent = it->override_descent;
26225 boff = it->override_boff;
26227 else
26229 it->ascent = FONT_BASE (font) + boff;
26230 it->descent = FONT_DESCENT (font) - boff;
26233 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26235 pcm = get_per_char_metric (font, &char2b);
26236 if (pcm->width == 0
26237 && pcm->rbearing == 0 && pcm->lbearing == 0)
26238 pcm = NULL;
26241 if (pcm)
26243 it->phys_ascent = pcm->ascent + boff;
26244 it->phys_descent = pcm->descent - boff;
26245 it->pixel_width = pcm->width;
26247 else
26249 it->glyph_not_available_p = 1;
26250 it->phys_ascent = it->ascent;
26251 it->phys_descent = it->descent;
26252 it->pixel_width = font->space_width;
26255 if (it->constrain_row_ascent_descent_p)
26257 if (it->descent > it->max_descent)
26259 it->ascent += it->descent - it->max_descent;
26260 it->descent = it->max_descent;
26262 if (it->ascent > it->max_ascent)
26264 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26265 it->ascent = it->max_ascent;
26267 it->phys_ascent = min (it->phys_ascent, it->ascent);
26268 it->phys_descent = min (it->phys_descent, it->descent);
26269 extra_line_spacing = 0;
26272 /* If this is a space inside a region of text with
26273 `space-width' property, change its width. */
26274 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26275 if (stretched_p)
26276 it->pixel_width *= XFLOATINT (it->space_width);
26278 /* If face has a box, add the box thickness to the character
26279 height. If character has a box line to the left and/or
26280 right, add the box line width to the character's width. */
26281 if (face->box != FACE_NO_BOX)
26283 int thick = face->box_line_width;
26285 if (thick > 0)
26287 it->ascent += thick;
26288 it->descent += thick;
26290 else
26291 thick = -thick;
26293 if (it->start_of_box_run_p)
26294 it->pixel_width += thick;
26295 if (it->end_of_box_run_p)
26296 it->pixel_width += thick;
26299 /* If face has an overline, add the height of the overline
26300 (1 pixel) and a 1 pixel margin to the character height. */
26301 if (face->overline_p)
26302 it->ascent += overline_margin;
26304 if (it->constrain_row_ascent_descent_p)
26306 if (it->ascent > it->max_ascent)
26307 it->ascent = it->max_ascent;
26308 if (it->descent > it->max_descent)
26309 it->descent = it->max_descent;
26312 take_vertical_position_into_account (it);
26314 /* If we have to actually produce glyphs, do it. */
26315 if (it->glyph_row)
26317 if (stretched_p)
26319 /* Translate a space with a `space-width' property
26320 into a stretch glyph. */
26321 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26322 / FONT_HEIGHT (font));
26323 append_stretch_glyph (it, it->object, it->pixel_width,
26324 it->ascent + it->descent, ascent);
26326 else
26327 append_glyph (it);
26329 /* If characters with lbearing or rbearing are displayed
26330 in this line, record that fact in a flag of the
26331 glyph row. This is used to optimize X output code. */
26332 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26333 it->glyph_row->contains_overlapping_glyphs_p = 1;
26335 if (! stretched_p && it->pixel_width == 0)
26336 /* We assure that all visible glyphs have at least 1-pixel
26337 width. */
26338 it->pixel_width = 1;
26340 else if (it->char_to_display == '\n')
26342 /* A newline has no width, but we need the height of the
26343 line. But if previous part of the line sets a height,
26344 don't increase that height. */
26346 Lisp_Object height;
26347 Lisp_Object total_height = Qnil;
26349 it->override_ascent = -1;
26350 it->pixel_width = 0;
26351 it->nglyphs = 0;
26353 height = get_it_property (it, Qline_height);
26354 /* Split (line-height total-height) list. */
26355 if (CONSP (height)
26356 && CONSP (XCDR (height))
26357 && NILP (XCDR (XCDR (height))))
26359 total_height = XCAR (XCDR (height));
26360 height = XCAR (height);
26362 height = calc_line_height_property (it, height, font, boff, 1);
26364 if (it->override_ascent >= 0)
26366 it->ascent = it->override_ascent;
26367 it->descent = it->override_descent;
26368 boff = it->override_boff;
26370 else
26372 it->ascent = FONT_BASE (font) + boff;
26373 it->descent = FONT_DESCENT (font) - boff;
26376 if (EQ (height, Qt))
26378 if (it->descent > it->max_descent)
26380 it->ascent += it->descent - it->max_descent;
26381 it->descent = it->max_descent;
26383 if (it->ascent > it->max_ascent)
26385 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26386 it->ascent = it->max_ascent;
26388 it->phys_ascent = min (it->phys_ascent, it->ascent);
26389 it->phys_descent = min (it->phys_descent, it->descent);
26390 it->constrain_row_ascent_descent_p = 1;
26391 extra_line_spacing = 0;
26393 else
26395 Lisp_Object spacing;
26397 it->phys_ascent = it->ascent;
26398 it->phys_descent = it->descent;
26400 if ((it->max_ascent > 0 || it->max_descent > 0)
26401 && face->box != FACE_NO_BOX
26402 && face->box_line_width > 0)
26404 it->ascent += face->box_line_width;
26405 it->descent += face->box_line_width;
26407 if (!NILP (height)
26408 && XINT (height) > it->ascent + it->descent)
26409 it->ascent = XINT (height) - it->descent;
26411 if (!NILP (total_height))
26412 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26413 else
26415 spacing = get_it_property (it, Qline_spacing);
26416 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26418 if (INTEGERP (spacing))
26420 extra_line_spacing = XINT (spacing);
26421 if (!NILP (total_height))
26422 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26426 else /* i.e. (it->char_to_display == '\t') */
26428 if (font->space_width > 0)
26430 int tab_width = it->tab_width * font->space_width;
26431 int x = it->current_x + it->continuation_lines_width;
26432 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26434 /* If the distance from the current position to the next tab
26435 stop is less than a space character width, use the
26436 tab stop after that. */
26437 if (next_tab_x - x < font->space_width)
26438 next_tab_x += tab_width;
26440 it->pixel_width = next_tab_x - x;
26441 it->nglyphs = 1;
26442 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26443 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26445 if (it->glyph_row)
26447 append_stretch_glyph (it, it->object, it->pixel_width,
26448 it->ascent + it->descent, it->ascent);
26451 else
26453 it->pixel_width = 0;
26454 it->nglyphs = 1;
26458 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26460 /* A static composition.
26462 Note: A composition is represented as one glyph in the
26463 glyph matrix. There are no padding glyphs.
26465 Important note: pixel_width, ascent, and descent are the
26466 values of what is drawn by draw_glyphs (i.e. the values of
26467 the overall glyphs composed). */
26468 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26469 int boff; /* baseline offset */
26470 struct composition *cmp = composition_table[it->cmp_it.id];
26471 int glyph_len = cmp->glyph_len;
26472 struct font *font = face->font;
26474 it->nglyphs = 1;
26476 /* If we have not yet calculated pixel size data of glyphs of
26477 the composition for the current face font, calculate them
26478 now. Theoretically, we have to check all fonts for the
26479 glyphs, but that requires much time and memory space. So,
26480 here we check only the font of the first glyph. This may
26481 lead to incorrect display, but it's very rare, and C-l
26482 (recenter-top-bottom) can correct the display anyway. */
26483 if (! cmp->font || cmp->font != font)
26485 /* Ascent and descent of the font of the first character
26486 of this composition (adjusted by baseline offset).
26487 Ascent and descent of overall glyphs should not be less
26488 than these, respectively. */
26489 int font_ascent, font_descent, font_height;
26490 /* Bounding box of the overall glyphs. */
26491 int leftmost, rightmost, lowest, highest;
26492 int lbearing, rbearing;
26493 int i, width, ascent, descent;
26494 int left_padded = 0, right_padded = 0;
26495 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26496 XChar2b char2b;
26497 struct font_metrics *pcm;
26498 int font_not_found_p;
26499 ptrdiff_t pos;
26501 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26502 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26503 break;
26504 if (glyph_len < cmp->glyph_len)
26505 right_padded = 1;
26506 for (i = 0; i < glyph_len; i++)
26508 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
26509 break;
26510 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26512 if (i > 0)
26513 left_padded = 1;
26515 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
26516 : IT_CHARPOS (*it));
26517 /* If no suitable font is found, use the default font. */
26518 font_not_found_p = font == NULL;
26519 if (font_not_found_p)
26521 face = face->ascii_face;
26522 font = face->font;
26524 boff = font->baseline_offset;
26525 if (font->vertical_centering)
26526 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26527 font_ascent = FONT_BASE (font) + boff;
26528 font_descent = FONT_DESCENT (font) - boff;
26529 font_height = FONT_HEIGHT (font);
26531 cmp->font = font;
26533 pcm = NULL;
26534 if (! font_not_found_p)
26536 get_char_face_and_encoding (it->f, c, it->face_id,
26537 &char2b, 0);
26538 pcm = get_per_char_metric (font, &char2b);
26541 /* Initialize the bounding box. */
26542 if (pcm)
26544 width = cmp->glyph_len > 0 ? pcm->width : 0;
26545 ascent = pcm->ascent;
26546 descent = pcm->descent;
26547 lbearing = pcm->lbearing;
26548 rbearing = pcm->rbearing;
26550 else
26552 width = cmp->glyph_len > 0 ? font->space_width : 0;
26553 ascent = FONT_BASE (font);
26554 descent = FONT_DESCENT (font);
26555 lbearing = 0;
26556 rbearing = width;
26559 rightmost = width;
26560 leftmost = 0;
26561 lowest = - descent + boff;
26562 highest = ascent + boff;
26564 if (! font_not_found_p
26565 && font->default_ascent
26566 && CHAR_TABLE_P (Vuse_default_ascent)
26567 && !NILP (Faref (Vuse_default_ascent,
26568 make_number (it->char_to_display))))
26569 highest = font->default_ascent + boff;
26571 /* Draw the first glyph at the normal position. It may be
26572 shifted to right later if some other glyphs are drawn
26573 at the left. */
26574 cmp->offsets[i * 2] = 0;
26575 cmp->offsets[i * 2 + 1] = boff;
26576 cmp->lbearing = lbearing;
26577 cmp->rbearing = rbearing;
26579 /* Set cmp->offsets for the remaining glyphs. */
26580 for (i++; i < glyph_len; i++)
26582 int left, right, btm, top;
26583 int ch = COMPOSITION_GLYPH (cmp, i);
26584 int face_id;
26585 struct face *this_face;
26587 if (ch == '\t')
26588 ch = ' ';
26589 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
26590 this_face = FACE_FROM_ID (it->f, face_id);
26591 font = this_face->font;
26593 if (font == NULL)
26594 pcm = NULL;
26595 else
26597 get_char_face_and_encoding (it->f, ch, face_id,
26598 &char2b, 0);
26599 pcm = get_per_char_metric (font, &char2b);
26601 if (! pcm)
26602 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
26603 else
26605 width = pcm->width;
26606 ascent = pcm->ascent;
26607 descent = pcm->descent;
26608 lbearing = pcm->lbearing;
26609 rbearing = pcm->rbearing;
26610 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
26612 /* Relative composition with or without
26613 alternate chars. */
26614 left = (leftmost + rightmost - width) / 2;
26615 btm = - descent + boff;
26616 if (font->relative_compose
26617 && (! CHAR_TABLE_P (Vignore_relative_composition)
26618 || NILP (Faref (Vignore_relative_composition,
26619 make_number (ch)))))
26622 if (- descent >= font->relative_compose)
26623 /* One extra pixel between two glyphs. */
26624 btm = highest + 1;
26625 else if (ascent <= 0)
26626 /* One extra pixel between two glyphs. */
26627 btm = lowest - 1 - ascent - descent;
26630 else
26632 /* A composition rule is specified by an integer
26633 value that encodes global and new reference
26634 points (GREF and NREF). GREF and NREF are
26635 specified by numbers as below:
26637 0---1---2 -- ascent
26641 9--10--11 -- center
26643 ---3---4---5--- baseline
26645 6---7---8 -- descent
26647 int rule = COMPOSITION_RULE (cmp, i);
26648 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
26650 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
26651 grefx = gref % 3, nrefx = nref % 3;
26652 grefy = gref / 3, nrefy = nref / 3;
26653 if (xoff)
26654 xoff = font_height * (xoff - 128) / 256;
26655 if (yoff)
26656 yoff = font_height * (yoff - 128) / 256;
26658 left = (leftmost
26659 + grefx * (rightmost - leftmost) / 2
26660 - nrefx * width / 2
26661 + xoff);
26663 btm = ((grefy == 0 ? highest
26664 : grefy == 1 ? 0
26665 : grefy == 2 ? lowest
26666 : (highest + lowest) / 2)
26667 - (nrefy == 0 ? ascent + descent
26668 : nrefy == 1 ? descent - boff
26669 : nrefy == 2 ? 0
26670 : (ascent + descent) / 2)
26671 + yoff);
26674 cmp->offsets[i * 2] = left;
26675 cmp->offsets[i * 2 + 1] = btm + descent;
26677 /* Update the bounding box of the overall glyphs. */
26678 if (width > 0)
26680 right = left + width;
26681 if (left < leftmost)
26682 leftmost = left;
26683 if (right > rightmost)
26684 rightmost = right;
26686 top = btm + descent + ascent;
26687 if (top > highest)
26688 highest = top;
26689 if (btm < lowest)
26690 lowest = btm;
26692 if (cmp->lbearing > left + lbearing)
26693 cmp->lbearing = left + lbearing;
26694 if (cmp->rbearing < left + rbearing)
26695 cmp->rbearing = left + rbearing;
26699 /* If there are glyphs whose x-offsets are negative,
26700 shift all glyphs to the right and make all x-offsets
26701 non-negative. */
26702 if (leftmost < 0)
26704 for (i = 0; i < cmp->glyph_len; i++)
26705 cmp->offsets[i * 2] -= leftmost;
26706 rightmost -= leftmost;
26707 cmp->lbearing -= leftmost;
26708 cmp->rbearing -= leftmost;
26711 if (left_padded && cmp->lbearing < 0)
26713 for (i = 0; i < cmp->glyph_len; i++)
26714 cmp->offsets[i * 2] -= cmp->lbearing;
26715 rightmost -= cmp->lbearing;
26716 cmp->rbearing -= cmp->lbearing;
26717 cmp->lbearing = 0;
26719 if (right_padded && rightmost < cmp->rbearing)
26721 rightmost = cmp->rbearing;
26724 cmp->pixel_width = rightmost;
26725 cmp->ascent = highest;
26726 cmp->descent = - lowest;
26727 if (cmp->ascent < font_ascent)
26728 cmp->ascent = font_ascent;
26729 if (cmp->descent < font_descent)
26730 cmp->descent = font_descent;
26733 if (it->glyph_row
26734 && (cmp->lbearing < 0
26735 || cmp->rbearing > cmp->pixel_width))
26736 it->glyph_row->contains_overlapping_glyphs_p = 1;
26738 it->pixel_width = cmp->pixel_width;
26739 it->ascent = it->phys_ascent = cmp->ascent;
26740 it->descent = it->phys_descent = cmp->descent;
26741 if (face->box != FACE_NO_BOX)
26743 int thick = face->box_line_width;
26745 if (thick > 0)
26747 it->ascent += thick;
26748 it->descent += thick;
26750 else
26751 thick = - thick;
26753 if (it->start_of_box_run_p)
26754 it->pixel_width += thick;
26755 if (it->end_of_box_run_p)
26756 it->pixel_width += thick;
26759 /* If face has an overline, add the height of the overline
26760 (1 pixel) and a 1 pixel margin to the character height. */
26761 if (face->overline_p)
26762 it->ascent += overline_margin;
26764 take_vertical_position_into_account (it);
26765 if (it->ascent < 0)
26766 it->ascent = 0;
26767 if (it->descent < 0)
26768 it->descent = 0;
26770 if (it->glyph_row && cmp->glyph_len > 0)
26771 append_composite_glyph (it);
26773 else if (it->what == IT_COMPOSITION)
26775 /* A dynamic (automatic) composition. */
26776 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26777 Lisp_Object gstring;
26778 struct font_metrics metrics;
26780 it->nglyphs = 1;
26782 gstring = composition_gstring_from_id (it->cmp_it.id);
26783 it->pixel_width
26784 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
26785 &metrics);
26786 if (it->glyph_row
26787 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
26788 it->glyph_row->contains_overlapping_glyphs_p = 1;
26789 it->ascent = it->phys_ascent = metrics.ascent;
26790 it->descent = it->phys_descent = metrics.descent;
26791 if (face->box != FACE_NO_BOX)
26793 int thick = face->box_line_width;
26795 if (thick > 0)
26797 it->ascent += thick;
26798 it->descent += thick;
26800 else
26801 thick = - thick;
26803 if (it->start_of_box_run_p)
26804 it->pixel_width += thick;
26805 if (it->end_of_box_run_p)
26806 it->pixel_width += thick;
26808 /* If face has an overline, add the height of the overline
26809 (1 pixel) and a 1 pixel margin to the character height. */
26810 if (face->overline_p)
26811 it->ascent += overline_margin;
26812 take_vertical_position_into_account (it);
26813 if (it->ascent < 0)
26814 it->ascent = 0;
26815 if (it->descent < 0)
26816 it->descent = 0;
26818 if (it->glyph_row)
26819 append_composite_glyph (it);
26821 else if (it->what == IT_GLYPHLESS)
26822 produce_glyphless_glyph (it, 0, Qnil);
26823 else if (it->what == IT_IMAGE)
26824 produce_image_glyph (it);
26825 else if (it->what == IT_STRETCH)
26826 produce_stretch_glyph (it);
26828 done:
26829 /* Accumulate dimensions. Note: can't assume that it->descent > 0
26830 because this isn't true for images with `:ascent 100'. */
26831 eassert (it->ascent >= 0 && it->descent >= 0);
26832 if (it->area == TEXT_AREA)
26833 it->current_x += it->pixel_width;
26835 if (extra_line_spacing > 0)
26837 it->descent += extra_line_spacing;
26838 if (extra_line_spacing > it->max_extra_line_spacing)
26839 it->max_extra_line_spacing = extra_line_spacing;
26842 it->max_ascent = max (it->max_ascent, it->ascent);
26843 it->max_descent = max (it->max_descent, it->descent);
26844 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
26845 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
26848 /* EXPORT for RIF:
26849 Output LEN glyphs starting at START at the nominal cursor position.
26850 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
26851 being updated, and UPDATED_AREA is the area of that row being updated. */
26853 void
26854 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
26855 struct glyph *start, enum glyph_row_area updated_area, int len)
26857 int x, hpos, chpos = w->phys_cursor.hpos;
26859 eassert (updated_row);
26860 /* When the window is hscrolled, cursor hpos can legitimately be out
26861 of bounds, but we draw the cursor at the corresponding window
26862 margin in that case. */
26863 if (!updated_row->reversed_p && chpos < 0)
26864 chpos = 0;
26865 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
26866 chpos = updated_row->used[TEXT_AREA] - 1;
26868 block_input ();
26870 /* Write glyphs. */
26872 hpos = start - updated_row->glyphs[updated_area];
26873 x = draw_glyphs (w, w->output_cursor.x,
26874 updated_row, updated_area,
26875 hpos, hpos + len,
26876 DRAW_NORMAL_TEXT, 0);
26878 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
26879 if (updated_area == TEXT_AREA
26880 && w->phys_cursor_on_p
26881 && w->phys_cursor.vpos == w->output_cursor.vpos
26882 && chpos >= hpos
26883 && chpos < hpos + len)
26884 w->phys_cursor_on_p = 0;
26886 unblock_input ();
26888 /* Advance the output cursor. */
26889 w->output_cursor.hpos += len;
26890 w->output_cursor.x = x;
26894 /* EXPORT for RIF:
26895 Insert LEN glyphs from START at the nominal cursor position. */
26897 void
26898 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
26899 struct glyph *start, enum glyph_row_area updated_area, int len)
26901 struct frame *f;
26902 int line_height, shift_by_width, shifted_region_width;
26903 struct glyph_row *row;
26904 struct glyph *glyph;
26905 int frame_x, frame_y;
26906 ptrdiff_t hpos;
26908 eassert (updated_row);
26909 block_input ();
26910 f = XFRAME (WINDOW_FRAME (w));
26912 /* Get the height of the line we are in. */
26913 row = updated_row;
26914 line_height = row->height;
26916 /* Get the width of the glyphs to insert. */
26917 shift_by_width = 0;
26918 for (glyph = start; glyph < start + len; ++glyph)
26919 shift_by_width += glyph->pixel_width;
26921 /* Get the width of the region to shift right. */
26922 shifted_region_width = (window_box_width (w, updated_area)
26923 - w->output_cursor.x
26924 - shift_by_width);
26926 /* Shift right. */
26927 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
26928 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
26930 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
26931 line_height, shift_by_width);
26933 /* Write the glyphs. */
26934 hpos = start - row->glyphs[updated_area];
26935 draw_glyphs (w, w->output_cursor.x, row, updated_area,
26936 hpos, hpos + len,
26937 DRAW_NORMAL_TEXT, 0);
26939 /* Advance the output cursor. */
26940 w->output_cursor.hpos += len;
26941 w->output_cursor.x += shift_by_width;
26942 unblock_input ();
26946 /* EXPORT for RIF:
26947 Erase the current text line from the nominal cursor position
26948 (inclusive) to pixel column TO_X (exclusive). The idea is that
26949 everything from TO_X onward is already erased.
26951 TO_X is a pixel position relative to UPDATED_AREA of currently
26952 updated window W. TO_X == -1 means clear to the end of this area. */
26954 void
26955 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
26956 enum glyph_row_area updated_area, int to_x)
26958 struct frame *f;
26959 int max_x, min_y, max_y;
26960 int from_x, from_y, to_y;
26962 eassert (updated_row);
26963 f = XFRAME (w->frame);
26965 if (updated_row->full_width_p)
26966 max_x = (WINDOW_PIXEL_WIDTH (w)
26967 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
26968 else
26969 max_x = window_box_width (w, updated_area);
26970 max_y = window_text_bottom_y (w);
26972 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
26973 of window. For TO_X > 0, truncate to end of drawing area. */
26974 if (to_x == 0)
26975 return;
26976 else if (to_x < 0)
26977 to_x = max_x;
26978 else
26979 to_x = min (to_x, max_x);
26981 to_y = min (max_y, w->output_cursor.y + updated_row->height);
26983 /* Notice if the cursor will be cleared by this operation. */
26984 if (!updated_row->full_width_p)
26985 notice_overwritten_cursor (w, updated_area,
26986 w->output_cursor.x, -1,
26987 updated_row->y,
26988 MATRIX_ROW_BOTTOM_Y (updated_row));
26990 from_x = w->output_cursor.x;
26992 /* Translate to frame coordinates. */
26993 if (updated_row->full_width_p)
26995 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
26996 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
26998 else
27000 int area_left = window_box_left (w, updated_area);
27001 from_x += area_left;
27002 to_x += area_left;
27005 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27006 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27007 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27009 /* Prevent inadvertently clearing to end of the X window. */
27010 if (to_x > from_x && to_y > from_y)
27012 block_input ();
27013 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27014 to_x - from_x, to_y - from_y);
27015 unblock_input ();
27019 #endif /* HAVE_WINDOW_SYSTEM */
27023 /***********************************************************************
27024 Cursor types
27025 ***********************************************************************/
27027 /* Value is the internal representation of the specified cursor type
27028 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27029 of the bar cursor. */
27031 static enum text_cursor_kinds
27032 get_specified_cursor_type (Lisp_Object arg, int *width)
27034 enum text_cursor_kinds type;
27036 if (NILP (arg))
27037 return NO_CURSOR;
27039 if (EQ (arg, Qbox))
27040 return FILLED_BOX_CURSOR;
27042 if (EQ (arg, Qhollow))
27043 return HOLLOW_BOX_CURSOR;
27045 if (EQ (arg, Qbar))
27047 *width = 2;
27048 return BAR_CURSOR;
27051 if (CONSP (arg)
27052 && EQ (XCAR (arg), Qbar)
27053 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27055 *width = XINT (XCDR (arg));
27056 return BAR_CURSOR;
27059 if (EQ (arg, Qhbar))
27061 *width = 2;
27062 return HBAR_CURSOR;
27065 if (CONSP (arg)
27066 && EQ (XCAR (arg), Qhbar)
27067 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27069 *width = XINT (XCDR (arg));
27070 return HBAR_CURSOR;
27073 /* Treat anything unknown as "hollow box cursor".
27074 It was bad to signal an error; people have trouble fixing
27075 .Xdefaults with Emacs, when it has something bad in it. */
27076 type = HOLLOW_BOX_CURSOR;
27078 return type;
27081 /* Set the default cursor types for specified frame. */
27082 void
27083 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27085 int width = 1;
27086 Lisp_Object tem;
27088 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27089 FRAME_CURSOR_WIDTH (f) = width;
27091 /* By default, set up the blink-off state depending on the on-state. */
27093 tem = Fassoc (arg, Vblink_cursor_alist);
27094 if (!NILP (tem))
27096 FRAME_BLINK_OFF_CURSOR (f)
27097 = get_specified_cursor_type (XCDR (tem), &width);
27098 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27100 else
27101 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27103 /* Make sure the cursor gets redrawn. */
27104 f->cursor_type_changed = 1;
27108 #ifdef HAVE_WINDOW_SYSTEM
27110 /* Return the cursor we want to be displayed in window W. Return
27111 width of bar/hbar cursor through WIDTH arg. Return with
27112 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27113 (i.e. if the `system caret' should track this cursor).
27115 In a mini-buffer window, we want the cursor only to appear if we
27116 are reading input from this window. For the selected window, we
27117 want the cursor type given by the frame parameter or buffer local
27118 setting of cursor-type. If explicitly marked off, draw no cursor.
27119 In all other cases, we want a hollow box cursor. */
27121 static enum text_cursor_kinds
27122 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27123 int *active_cursor)
27125 struct frame *f = XFRAME (w->frame);
27126 struct buffer *b = XBUFFER (w->contents);
27127 int cursor_type = DEFAULT_CURSOR;
27128 Lisp_Object alt_cursor;
27129 int non_selected = 0;
27131 *active_cursor = 1;
27133 /* Echo area */
27134 if (cursor_in_echo_area
27135 && FRAME_HAS_MINIBUF_P (f)
27136 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27138 if (w == XWINDOW (echo_area_window))
27140 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27142 *width = FRAME_CURSOR_WIDTH (f);
27143 return FRAME_DESIRED_CURSOR (f);
27145 else
27146 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27149 *active_cursor = 0;
27150 non_selected = 1;
27153 /* Detect a nonselected window or nonselected frame. */
27154 else if (w != XWINDOW (f->selected_window)
27155 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27157 *active_cursor = 0;
27159 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27160 return NO_CURSOR;
27162 non_selected = 1;
27165 /* Never display a cursor in a window in which cursor-type is nil. */
27166 if (NILP (BVAR (b, cursor_type)))
27167 return NO_CURSOR;
27169 /* Get the normal cursor type for this window. */
27170 if (EQ (BVAR (b, cursor_type), Qt))
27172 cursor_type = FRAME_DESIRED_CURSOR (f);
27173 *width = FRAME_CURSOR_WIDTH (f);
27175 else
27176 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27178 /* Use cursor-in-non-selected-windows instead
27179 for non-selected window or frame. */
27180 if (non_selected)
27182 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27183 if (!EQ (Qt, alt_cursor))
27184 return get_specified_cursor_type (alt_cursor, width);
27185 /* t means modify the normal cursor type. */
27186 if (cursor_type == FILLED_BOX_CURSOR)
27187 cursor_type = HOLLOW_BOX_CURSOR;
27188 else if (cursor_type == BAR_CURSOR && *width > 1)
27189 --*width;
27190 return cursor_type;
27193 /* Use normal cursor if not blinked off. */
27194 if (!w->cursor_off_p)
27196 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27198 if (cursor_type == FILLED_BOX_CURSOR)
27200 /* Using a block cursor on large images can be very annoying.
27201 So use a hollow cursor for "large" images.
27202 If image is not transparent (no mask), also use hollow cursor. */
27203 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27204 if (img != NULL && IMAGEP (img->spec))
27206 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27207 where N = size of default frame font size.
27208 This should cover most of the "tiny" icons people may use. */
27209 if (!img->mask
27210 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27211 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27212 cursor_type = HOLLOW_BOX_CURSOR;
27215 else if (cursor_type != NO_CURSOR)
27217 /* Display current only supports BOX and HOLLOW cursors for images.
27218 So for now, unconditionally use a HOLLOW cursor when cursor is
27219 not a solid box cursor. */
27220 cursor_type = HOLLOW_BOX_CURSOR;
27223 return cursor_type;
27226 /* Cursor is blinked off, so determine how to "toggle" it. */
27228 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27229 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27230 return get_specified_cursor_type (XCDR (alt_cursor), width);
27232 /* Then see if frame has specified a specific blink off cursor type. */
27233 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27235 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27236 return FRAME_BLINK_OFF_CURSOR (f);
27239 #if 0
27240 /* Some people liked having a permanently visible blinking cursor,
27241 while others had very strong opinions against it. So it was
27242 decided to remove it. KFS 2003-09-03 */
27244 /* Finally perform built-in cursor blinking:
27245 filled box <-> hollow box
27246 wide [h]bar <-> narrow [h]bar
27247 narrow [h]bar <-> no cursor
27248 other type <-> no cursor */
27250 if (cursor_type == FILLED_BOX_CURSOR)
27251 return HOLLOW_BOX_CURSOR;
27253 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27255 *width = 1;
27256 return cursor_type;
27258 #endif
27260 return NO_CURSOR;
27264 /* Notice when the text cursor of window W has been completely
27265 overwritten by a drawing operation that outputs glyphs in AREA
27266 starting at X0 and ending at X1 in the line starting at Y0 and
27267 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27268 the rest of the line after X0 has been written. Y coordinates
27269 are window-relative. */
27271 static void
27272 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27273 int x0, int x1, int y0, int y1)
27275 int cx0, cx1, cy0, cy1;
27276 struct glyph_row *row;
27278 if (!w->phys_cursor_on_p)
27279 return;
27280 if (area != TEXT_AREA)
27281 return;
27283 if (w->phys_cursor.vpos < 0
27284 || w->phys_cursor.vpos >= w->current_matrix->nrows
27285 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27286 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27287 return;
27289 if (row->cursor_in_fringe_p)
27291 row->cursor_in_fringe_p = 0;
27292 draw_fringe_bitmap (w, row, row->reversed_p);
27293 w->phys_cursor_on_p = 0;
27294 return;
27297 cx0 = w->phys_cursor.x;
27298 cx1 = cx0 + w->phys_cursor_width;
27299 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27300 return;
27302 /* The cursor image will be completely removed from the
27303 screen if the output area intersects the cursor area in
27304 y-direction. When we draw in [y0 y1[, and some part of
27305 the cursor is at y < y0, that part must have been drawn
27306 before. When scrolling, the cursor is erased before
27307 actually scrolling, so we don't come here. When not
27308 scrolling, the rows above the old cursor row must have
27309 changed, and in this case these rows must have written
27310 over the cursor image.
27312 Likewise if part of the cursor is below y1, with the
27313 exception of the cursor being in the first blank row at
27314 the buffer and window end because update_text_area
27315 doesn't draw that row. (Except when it does, but
27316 that's handled in update_text_area.) */
27318 cy0 = w->phys_cursor.y;
27319 cy1 = cy0 + w->phys_cursor_height;
27320 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27321 return;
27323 w->phys_cursor_on_p = 0;
27326 #endif /* HAVE_WINDOW_SYSTEM */
27329 /************************************************************************
27330 Mouse Face
27331 ************************************************************************/
27333 #ifdef HAVE_WINDOW_SYSTEM
27335 /* EXPORT for RIF:
27336 Fix the display of area AREA of overlapping row ROW in window W
27337 with respect to the overlapping part OVERLAPS. */
27339 void
27340 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27341 enum glyph_row_area area, int overlaps)
27343 int i, x;
27345 block_input ();
27347 x = 0;
27348 for (i = 0; i < row->used[area];)
27350 if (row->glyphs[area][i].overlaps_vertically_p)
27352 int start = i, start_x = x;
27356 x += row->glyphs[area][i].pixel_width;
27357 ++i;
27359 while (i < row->used[area]
27360 && row->glyphs[area][i].overlaps_vertically_p);
27362 draw_glyphs (w, start_x, row, area,
27363 start, i,
27364 DRAW_NORMAL_TEXT, overlaps);
27366 else
27368 x += row->glyphs[area][i].pixel_width;
27369 ++i;
27373 unblock_input ();
27377 /* EXPORT:
27378 Draw the cursor glyph of window W in glyph row ROW. See the
27379 comment of draw_glyphs for the meaning of HL. */
27381 void
27382 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27383 enum draw_glyphs_face hl)
27385 /* If cursor hpos is out of bounds, don't draw garbage. This can
27386 happen in mini-buffer windows when switching between echo area
27387 glyphs and mini-buffer. */
27388 if ((row->reversed_p
27389 ? (w->phys_cursor.hpos >= 0)
27390 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27392 int on_p = w->phys_cursor_on_p;
27393 int x1;
27394 int hpos = w->phys_cursor.hpos;
27396 /* When the window is hscrolled, cursor hpos can legitimately be
27397 out of bounds, but we draw the cursor at the corresponding
27398 window margin in that case. */
27399 if (!row->reversed_p && hpos < 0)
27400 hpos = 0;
27401 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27402 hpos = row->used[TEXT_AREA] - 1;
27404 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27405 hl, 0);
27406 w->phys_cursor_on_p = on_p;
27408 if (hl == DRAW_CURSOR)
27409 w->phys_cursor_width = x1 - w->phys_cursor.x;
27410 /* When we erase the cursor, and ROW is overlapped by other
27411 rows, make sure that these overlapping parts of other rows
27412 are redrawn. */
27413 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27415 w->phys_cursor_width = x1 - w->phys_cursor.x;
27417 if (row > w->current_matrix->rows
27418 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27419 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27420 OVERLAPS_ERASED_CURSOR);
27422 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27423 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27424 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27425 OVERLAPS_ERASED_CURSOR);
27431 /* Erase the image of a cursor of window W from the screen. */
27433 void
27434 erase_phys_cursor (struct window *w)
27436 struct frame *f = XFRAME (w->frame);
27437 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27438 int hpos = w->phys_cursor.hpos;
27439 int vpos = w->phys_cursor.vpos;
27440 int mouse_face_here_p = 0;
27441 struct glyph_matrix *active_glyphs = w->current_matrix;
27442 struct glyph_row *cursor_row;
27443 struct glyph *cursor_glyph;
27444 enum draw_glyphs_face hl;
27446 /* No cursor displayed or row invalidated => nothing to do on the
27447 screen. */
27448 if (w->phys_cursor_type == NO_CURSOR)
27449 goto mark_cursor_off;
27451 /* VPOS >= active_glyphs->nrows means that window has been resized.
27452 Don't bother to erase the cursor. */
27453 if (vpos >= active_glyphs->nrows)
27454 goto mark_cursor_off;
27456 /* If row containing cursor is marked invalid, there is nothing we
27457 can do. */
27458 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27459 if (!cursor_row->enabled_p)
27460 goto mark_cursor_off;
27462 /* If line spacing is > 0, old cursor may only be partially visible in
27463 window after split-window. So adjust visible height. */
27464 cursor_row->visible_height = min (cursor_row->visible_height,
27465 window_text_bottom_y (w) - cursor_row->y);
27467 /* If row is completely invisible, don't attempt to delete a cursor which
27468 isn't there. This can happen if cursor is at top of a window, and
27469 we switch to a buffer with a header line in that window. */
27470 if (cursor_row->visible_height <= 0)
27471 goto mark_cursor_off;
27473 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27474 if (cursor_row->cursor_in_fringe_p)
27476 cursor_row->cursor_in_fringe_p = 0;
27477 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27478 goto mark_cursor_off;
27481 /* This can happen when the new row is shorter than the old one.
27482 In this case, either draw_glyphs or clear_end_of_line
27483 should have cleared the cursor. Note that we wouldn't be
27484 able to erase the cursor in this case because we don't have a
27485 cursor glyph at hand. */
27486 if ((cursor_row->reversed_p
27487 ? (w->phys_cursor.hpos < 0)
27488 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27489 goto mark_cursor_off;
27491 /* When the window is hscrolled, cursor hpos can legitimately be out
27492 of bounds, but we draw the cursor at the corresponding window
27493 margin in that case. */
27494 if (!cursor_row->reversed_p && hpos < 0)
27495 hpos = 0;
27496 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
27497 hpos = cursor_row->used[TEXT_AREA] - 1;
27499 /* If the cursor is in the mouse face area, redisplay that when
27500 we clear the cursor. */
27501 if (! NILP (hlinfo->mouse_face_window)
27502 && coords_in_mouse_face_p (w, hpos, vpos)
27503 /* Don't redraw the cursor's spot in mouse face if it is at the
27504 end of a line (on a newline). The cursor appears there, but
27505 mouse highlighting does not. */
27506 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
27507 mouse_face_here_p = 1;
27509 /* Maybe clear the display under the cursor. */
27510 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
27512 int x, y;
27513 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
27514 int width;
27516 cursor_glyph = get_phys_cursor_glyph (w);
27517 if (cursor_glyph == NULL)
27518 goto mark_cursor_off;
27520 width = cursor_glyph->pixel_width;
27521 x = w->phys_cursor.x;
27522 if (x < 0)
27524 width += x;
27525 x = 0;
27527 width = min (width, window_box_width (w, TEXT_AREA) - x);
27528 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
27529 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
27531 if (width > 0)
27532 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
27535 /* Erase the cursor by redrawing the character underneath it. */
27536 if (mouse_face_here_p)
27537 hl = DRAW_MOUSE_FACE;
27538 else
27539 hl = DRAW_NORMAL_TEXT;
27540 draw_phys_cursor_glyph (w, cursor_row, hl);
27542 mark_cursor_off:
27543 w->phys_cursor_on_p = 0;
27544 w->phys_cursor_type = NO_CURSOR;
27548 /* EXPORT:
27549 Display or clear cursor of window W. If ON is zero, clear the
27550 cursor. If it is non-zero, display the cursor. If ON is nonzero,
27551 where to put the cursor is specified by HPOS, VPOS, X and Y. */
27553 void
27554 display_and_set_cursor (struct window *w, bool on,
27555 int hpos, int vpos, int x, int y)
27557 struct frame *f = XFRAME (w->frame);
27558 int new_cursor_type;
27559 int new_cursor_width;
27560 int active_cursor;
27561 struct glyph_row *glyph_row;
27562 struct glyph *glyph;
27564 /* This is pointless on invisible frames, and dangerous on garbaged
27565 windows and frames; in the latter case, the frame or window may
27566 be in the midst of changing its size, and x and y may be off the
27567 window. */
27568 if (! FRAME_VISIBLE_P (f)
27569 || FRAME_GARBAGED_P (f)
27570 || vpos >= w->current_matrix->nrows
27571 || hpos >= w->current_matrix->matrix_w)
27572 return;
27574 /* If cursor is off and we want it off, return quickly. */
27575 if (!on && !w->phys_cursor_on_p)
27576 return;
27578 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
27579 /* If cursor row is not enabled, we don't really know where to
27580 display the cursor. */
27581 if (!glyph_row->enabled_p)
27583 w->phys_cursor_on_p = 0;
27584 return;
27587 glyph = NULL;
27588 if (!glyph_row->exact_window_width_line_p
27589 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
27590 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
27592 eassert (input_blocked_p ());
27594 /* Set new_cursor_type to the cursor we want to be displayed. */
27595 new_cursor_type = get_window_cursor_type (w, glyph,
27596 &new_cursor_width, &active_cursor);
27598 /* If cursor is currently being shown and we don't want it to be or
27599 it is in the wrong place, or the cursor type is not what we want,
27600 erase it. */
27601 if (w->phys_cursor_on_p
27602 && (!on
27603 || w->phys_cursor.x != x
27604 || w->phys_cursor.y != y
27605 /* HPOS can be negative in R2L rows whose
27606 exact_window_width_line_p flag is set (i.e. their newline
27607 would "overflow into the fringe"). */
27608 || hpos < 0
27609 || new_cursor_type != w->phys_cursor_type
27610 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
27611 && new_cursor_width != w->phys_cursor_width)))
27612 erase_phys_cursor (w);
27614 /* Don't check phys_cursor_on_p here because that flag is only set
27615 to zero in some cases where we know that the cursor has been
27616 completely erased, to avoid the extra work of erasing the cursor
27617 twice. In other words, phys_cursor_on_p can be 1 and the cursor
27618 still not be visible, or it has only been partly erased. */
27619 if (on)
27621 w->phys_cursor_ascent = glyph_row->ascent;
27622 w->phys_cursor_height = glyph_row->height;
27624 /* Set phys_cursor_.* before x_draw_.* is called because some
27625 of them may need the information. */
27626 w->phys_cursor.x = x;
27627 w->phys_cursor.y = glyph_row->y;
27628 w->phys_cursor.hpos = hpos;
27629 w->phys_cursor.vpos = vpos;
27632 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
27633 new_cursor_type, new_cursor_width,
27634 on, active_cursor);
27638 /* Switch the display of W's cursor on or off, according to the value
27639 of ON. */
27641 static void
27642 update_window_cursor (struct window *w, bool on)
27644 /* Don't update cursor in windows whose frame is in the process
27645 of being deleted. */
27646 if (w->current_matrix)
27648 int hpos = w->phys_cursor.hpos;
27649 int vpos = w->phys_cursor.vpos;
27650 struct glyph_row *row;
27652 if (vpos >= w->current_matrix->nrows
27653 || hpos >= w->current_matrix->matrix_w)
27654 return;
27656 row = MATRIX_ROW (w->current_matrix, vpos);
27658 /* When the window is hscrolled, cursor hpos can legitimately be
27659 out of bounds, but we draw the cursor at the corresponding
27660 window margin in that case. */
27661 if (!row->reversed_p && hpos < 0)
27662 hpos = 0;
27663 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27664 hpos = row->used[TEXT_AREA] - 1;
27666 block_input ();
27667 display_and_set_cursor (w, on, hpos, vpos,
27668 w->phys_cursor.x, w->phys_cursor.y);
27669 unblock_input ();
27674 /* Call update_window_cursor with parameter ON_P on all leaf windows
27675 in the window tree rooted at W. */
27677 static void
27678 update_cursor_in_window_tree (struct window *w, bool on_p)
27680 while (w)
27682 if (WINDOWP (w->contents))
27683 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
27684 else
27685 update_window_cursor (w, on_p);
27687 w = NILP (w->next) ? 0 : XWINDOW (w->next);
27692 /* EXPORT:
27693 Display the cursor on window W, or clear it, according to ON_P.
27694 Don't change the cursor's position. */
27696 void
27697 x_update_cursor (struct frame *f, bool on_p)
27699 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
27703 /* EXPORT:
27704 Clear the cursor of window W to background color, and mark the
27705 cursor as not shown. This is used when the text where the cursor
27706 is about to be rewritten. */
27708 void
27709 x_clear_cursor (struct window *w)
27711 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
27712 update_window_cursor (w, 0);
27715 #endif /* HAVE_WINDOW_SYSTEM */
27717 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
27718 and MSDOS. */
27719 static void
27720 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
27721 int start_hpos, int end_hpos,
27722 enum draw_glyphs_face draw)
27724 #ifdef HAVE_WINDOW_SYSTEM
27725 if (FRAME_WINDOW_P (XFRAME (w->frame)))
27727 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
27728 return;
27730 #endif
27731 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
27732 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
27733 #endif
27736 /* Display the active region described by mouse_face_* according to DRAW. */
27738 static void
27739 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
27741 struct window *w = XWINDOW (hlinfo->mouse_face_window);
27742 struct frame *f = XFRAME (WINDOW_FRAME (w));
27744 if (/* If window is in the process of being destroyed, don't bother
27745 to do anything. */
27746 w->current_matrix != NULL
27747 /* Don't update mouse highlight if hidden. */
27748 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
27749 /* Recognize when we are called to operate on rows that don't exist
27750 anymore. This can happen when a window is split. */
27751 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
27753 int phys_cursor_on_p = w->phys_cursor_on_p;
27754 struct glyph_row *row, *first, *last;
27756 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
27757 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
27759 for (row = first; row <= last && row->enabled_p; ++row)
27761 int start_hpos, end_hpos, start_x;
27763 /* For all but the first row, the highlight starts at column 0. */
27764 if (row == first)
27766 /* R2L rows have BEG and END in reversed order, but the
27767 screen drawing geometry is always left to right. So
27768 we need to mirror the beginning and end of the
27769 highlighted area in R2L rows. */
27770 if (!row->reversed_p)
27772 start_hpos = hlinfo->mouse_face_beg_col;
27773 start_x = hlinfo->mouse_face_beg_x;
27775 else if (row == last)
27777 start_hpos = hlinfo->mouse_face_end_col;
27778 start_x = hlinfo->mouse_face_end_x;
27780 else
27782 start_hpos = 0;
27783 start_x = 0;
27786 else if (row->reversed_p && row == last)
27788 start_hpos = hlinfo->mouse_face_end_col;
27789 start_x = hlinfo->mouse_face_end_x;
27791 else
27793 start_hpos = 0;
27794 start_x = 0;
27797 if (row == last)
27799 if (!row->reversed_p)
27800 end_hpos = hlinfo->mouse_face_end_col;
27801 else if (row == first)
27802 end_hpos = hlinfo->mouse_face_beg_col;
27803 else
27805 end_hpos = row->used[TEXT_AREA];
27806 if (draw == DRAW_NORMAL_TEXT)
27807 row->fill_line_p = 1; /* Clear to end of line */
27810 else if (row->reversed_p && row == first)
27811 end_hpos = hlinfo->mouse_face_beg_col;
27812 else
27814 end_hpos = row->used[TEXT_AREA];
27815 if (draw == DRAW_NORMAL_TEXT)
27816 row->fill_line_p = 1; /* Clear to end of line */
27819 if (end_hpos > start_hpos)
27821 draw_row_with_mouse_face (w, start_x, row,
27822 start_hpos, end_hpos, draw);
27824 row->mouse_face_p
27825 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
27829 #ifdef HAVE_WINDOW_SYSTEM
27830 /* When we've written over the cursor, arrange for it to
27831 be displayed again. */
27832 if (FRAME_WINDOW_P (f)
27833 && phys_cursor_on_p && !w->phys_cursor_on_p)
27835 int hpos = w->phys_cursor.hpos;
27837 /* When the window is hscrolled, cursor hpos can legitimately be
27838 out of bounds, but we draw the cursor at the corresponding
27839 window margin in that case. */
27840 if (!row->reversed_p && hpos < 0)
27841 hpos = 0;
27842 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27843 hpos = row->used[TEXT_AREA] - 1;
27845 block_input ();
27846 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
27847 w->phys_cursor.x, w->phys_cursor.y);
27848 unblock_input ();
27850 #endif /* HAVE_WINDOW_SYSTEM */
27853 #ifdef HAVE_WINDOW_SYSTEM
27854 /* Change the mouse cursor. */
27855 if (FRAME_WINDOW_P (f))
27857 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
27858 if (draw == DRAW_NORMAL_TEXT
27859 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
27860 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
27861 else
27862 #endif
27863 if (draw == DRAW_MOUSE_FACE)
27864 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
27865 else
27866 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
27868 #endif /* HAVE_WINDOW_SYSTEM */
27871 /* EXPORT:
27872 Clear out the mouse-highlighted active region.
27873 Redraw it un-highlighted first. Value is non-zero if mouse
27874 face was actually drawn unhighlighted. */
27877 clear_mouse_face (Mouse_HLInfo *hlinfo)
27879 int cleared = 0;
27881 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
27883 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
27884 cleared = 1;
27887 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27888 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27889 hlinfo->mouse_face_window = Qnil;
27890 hlinfo->mouse_face_overlay = Qnil;
27891 return cleared;
27894 /* Return true if the coordinates HPOS and VPOS on windows W are
27895 within the mouse face on that window. */
27896 static bool
27897 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
27899 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27901 /* Quickly resolve the easy cases. */
27902 if (!(WINDOWP (hlinfo->mouse_face_window)
27903 && XWINDOW (hlinfo->mouse_face_window) == w))
27904 return false;
27905 if (vpos < hlinfo->mouse_face_beg_row
27906 || vpos > hlinfo->mouse_face_end_row)
27907 return false;
27908 if (vpos > hlinfo->mouse_face_beg_row
27909 && vpos < hlinfo->mouse_face_end_row)
27910 return true;
27912 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
27914 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27916 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
27917 return true;
27919 else if ((vpos == hlinfo->mouse_face_beg_row
27920 && hpos >= hlinfo->mouse_face_beg_col)
27921 || (vpos == hlinfo->mouse_face_end_row
27922 && hpos < hlinfo->mouse_face_end_col))
27923 return true;
27925 else
27927 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
27929 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
27930 return true;
27932 else if ((vpos == hlinfo->mouse_face_beg_row
27933 && hpos <= hlinfo->mouse_face_beg_col)
27934 || (vpos == hlinfo->mouse_face_end_row
27935 && hpos > hlinfo->mouse_face_end_col))
27936 return true;
27938 return false;
27942 /* EXPORT:
27943 True if physical cursor of window W is within mouse face. */
27945 bool
27946 cursor_in_mouse_face_p (struct window *w)
27948 int hpos = w->phys_cursor.hpos;
27949 int vpos = w->phys_cursor.vpos;
27950 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
27952 /* When the window is hscrolled, cursor hpos can legitimately be out
27953 of bounds, but we draw the cursor at the corresponding window
27954 margin in that case. */
27955 if (!row->reversed_p && hpos < 0)
27956 hpos = 0;
27957 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27958 hpos = row->used[TEXT_AREA] - 1;
27960 return coords_in_mouse_face_p (w, hpos, vpos);
27965 /* Find the glyph rows START_ROW and END_ROW of window W that display
27966 characters between buffer positions START_CHARPOS and END_CHARPOS
27967 (excluding END_CHARPOS). DISP_STRING is a display string that
27968 covers these buffer positions. This is similar to
27969 row_containing_pos, but is more accurate when bidi reordering makes
27970 buffer positions change non-linearly with glyph rows. */
27971 static void
27972 rows_from_pos_range (struct window *w,
27973 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
27974 Lisp_Object disp_string,
27975 struct glyph_row **start, struct glyph_row **end)
27977 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27978 int last_y = window_text_bottom_y (w);
27979 struct glyph_row *row;
27981 *start = NULL;
27982 *end = NULL;
27984 while (!first->enabled_p
27985 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
27986 first++;
27988 /* Find the START row. */
27989 for (row = first;
27990 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
27991 row++)
27993 /* A row can potentially be the START row if the range of the
27994 characters it displays intersects the range
27995 [START_CHARPOS..END_CHARPOS). */
27996 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
27997 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
27998 /* See the commentary in row_containing_pos, for the
27999 explanation of the complicated way to check whether
28000 some position is beyond the end of the characters
28001 displayed by a row. */
28002 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28003 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28004 && !row->ends_at_zv_p
28005 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28006 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28007 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28008 && !row->ends_at_zv_p
28009 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28011 /* Found a candidate row. Now make sure at least one of the
28012 glyphs it displays has a charpos from the range
28013 [START_CHARPOS..END_CHARPOS).
28015 This is not obvious because bidi reordering could make
28016 buffer positions of a row be 1,2,3,102,101,100, and if we
28017 want to highlight characters in [50..60), we don't want
28018 this row, even though [50..60) does intersect [1..103),
28019 the range of character positions given by the row's start
28020 and end positions. */
28021 struct glyph *g = row->glyphs[TEXT_AREA];
28022 struct glyph *e = g + row->used[TEXT_AREA];
28024 while (g < e)
28026 if (((BUFFERP (g->object) || INTEGERP (g->object))
28027 && start_charpos <= g->charpos && g->charpos < end_charpos)
28028 /* A glyph that comes from DISP_STRING is by
28029 definition to be highlighted. */
28030 || EQ (g->object, disp_string))
28031 *start = row;
28032 g++;
28034 if (*start)
28035 break;
28039 /* Find the END row. */
28040 if (!*start
28041 /* If the last row is partially visible, start looking for END
28042 from that row, instead of starting from FIRST. */
28043 && !(row->enabled_p
28044 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28045 row = first;
28046 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28048 struct glyph_row *next = row + 1;
28049 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28051 if (!next->enabled_p
28052 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28053 /* The first row >= START whose range of displayed characters
28054 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28055 is the row END + 1. */
28056 || (start_charpos < next_start
28057 && end_charpos < next_start)
28058 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28059 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28060 && !next->ends_at_zv_p
28061 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28062 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28063 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28064 && !next->ends_at_zv_p
28065 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28067 *end = row;
28068 break;
28070 else
28072 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28073 but none of the characters it displays are in the range, it is
28074 also END + 1. */
28075 struct glyph *g = next->glyphs[TEXT_AREA];
28076 struct glyph *s = g;
28077 struct glyph *e = g + next->used[TEXT_AREA];
28079 while (g < e)
28081 if (((BUFFERP (g->object) || INTEGERP (g->object))
28082 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28083 /* If the buffer position of the first glyph in
28084 the row is equal to END_CHARPOS, it means
28085 the last character to be highlighted is the
28086 newline of ROW, and we must consider NEXT as
28087 END, not END+1. */
28088 || (((!next->reversed_p && g == s)
28089 || (next->reversed_p && g == e - 1))
28090 && (g->charpos == end_charpos
28091 /* Special case for when NEXT is an
28092 empty line at ZV. */
28093 || (g->charpos == -1
28094 && !row->ends_at_zv_p
28095 && next_start == end_charpos)))))
28096 /* A glyph that comes from DISP_STRING is by
28097 definition to be highlighted. */
28098 || EQ (g->object, disp_string))
28099 break;
28100 g++;
28102 if (g == e)
28104 *end = row;
28105 break;
28107 /* The first row that ends at ZV must be the last to be
28108 highlighted. */
28109 else if (next->ends_at_zv_p)
28111 *end = next;
28112 break;
28118 /* This function sets the mouse_face_* elements of HLINFO, assuming
28119 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28120 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28121 for the overlay or run of text properties specifying the mouse
28122 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28123 before-string and after-string that must also be highlighted.
28124 DISP_STRING, if non-nil, is a display string that may cover some
28125 or all of the highlighted text. */
28127 static void
28128 mouse_face_from_buffer_pos (Lisp_Object window,
28129 Mouse_HLInfo *hlinfo,
28130 ptrdiff_t mouse_charpos,
28131 ptrdiff_t start_charpos,
28132 ptrdiff_t end_charpos,
28133 Lisp_Object before_string,
28134 Lisp_Object after_string,
28135 Lisp_Object disp_string)
28137 struct window *w = XWINDOW (window);
28138 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28139 struct glyph_row *r1, *r2;
28140 struct glyph *glyph, *end;
28141 ptrdiff_t ignore, pos;
28142 int x;
28144 eassert (NILP (disp_string) || STRINGP (disp_string));
28145 eassert (NILP (before_string) || STRINGP (before_string));
28146 eassert (NILP (after_string) || STRINGP (after_string));
28148 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28149 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28150 if (r1 == NULL)
28151 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28152 /* If the before-string or display-string contains newlines,
28153 rows_from_pos_range skips to its last row. Move back. */
28154 if (!NILP (before_string) || !NILP (disp_string))
28156 struct glyph_row *prev;
28157 while ((prev = r1 - 1, prev >= first)
28158 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28159 && prev->used[TEXT_AREA] > 0)
28161 struct glyph *beg = prev->glyphs[TEXT_AREA];
28162 glyph = beg + prev->used[TEXT_AREA];
28163 while (--glyph >= beg && INTEGERP (glyph->object));
28164 if (glyph < beg
28165 || !(EQ (glyph->object, before_string)
28166 || EQ (glyph->object, disp_string)))
28167 break;
28168 r1 = prev;
28171 if (r2 == NULL)
28173 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28174 hlinfo->mouse_face_past_end = 1;
28176 else if (!NILP (after_string))
28178 /* If the after-string has newlines, advance to its last row. */
28179 struct glyph_row *next;
28180 struct glyph_row *last
28181 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28183 for (next = r2 + 1;
28184 next <= last
28185 && next->used[TEXT_AREA] > 0
28186 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28187 ++next)
28188 r2 = next;
28190 /* The rest of the display engine assumes that mouse_face_beg_row is
28191 either above mouse_face_end_row or identical to it. But with
28192 bidi-reordered continued lines, the row for START_CHARPOS could
28193 be below the row for END_CHARPOS. If so, swap the rows and store
28194 them in correct order. */
28195 if (r1->y > r2->y)
28197 struct glyph_row *tem = r2;
28199 r2 = r1;
28200 r1 = tem;
28203 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28204 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28206 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28207 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28208 could be anywhere in the row and in any order. The strategy
28209 below is to find the leftmost and the rightmost glyph that
28210 belongs to either of these 3 strings, or whose position is
28211 between START_CHARPOS and END_CHARPOS, and highlight all the
28212 glyphs between those two. This may cover more than just the text
28213 between START_CHARPOS and END_CHARPOS if the range of characters
28214 strides the bidi level boundary, e.g. if the beginning is in R2L
28215 text while the end is in L2R text or vice versa. */
28216 if (!r1->reversed_p)
28218 /* This row is in a left to right paragraph. Scan it left to
28219 right. */
28220 glyph = r1->glyphs[TEXT_AREA];
28221 end = glyph + r1->used[TEXT_AREA];
28222 x = r1->x;
28224 /* Skip truncation glyphs at the start of the glyph row. */
28225 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28226 for (; glyph < end
28227 && INTEGERP (glyph->object)
28228 && glyph->charpos < 0;
28229 ++glyph)
28230 x += glyph->pixel_width;
28232 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28233 or DISP_STRING, and the first glyph from buffer whose
28234 position is between START_CHARPOS and END_CHARPOS. */
28235 for (; glyph < end
28236 && !INTEGERP (glyph->object)
28237 && !EQ (glyph->object, disp_string)
28238 && !(BUFFERP (glyph->object)
28239 && (glyph->charpos >= start_charpos
28240 && glyph->charpos < end_charpos));
28241 ++glyph)
28243 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28244 are present at buffer positions between START_CHARPOS and
28245 END_CHARPOS, or if they come from an overlay. */
28246 if (EQ (glyph->object, before_string))
28248 pos = string_buffer_position (before_string,
28249 start_charpos);
28250 /* If pos == 0, it means before_string came from an
28251 overlay, not from a buffer position. */
28252 if (!pos || (pos >= start_charpos && pos < end_charpos))
28253 break;
28255 else if (EQ (glyph->object, after_string))
28257 pos = string_buffer_position (after_string, end_charpos);
28258 if (!pos || (pos >= start_charpos && pos < end_charpos))
28259 break;
28261 x += glyph->pixel_width;
28263 hlinfo->mouse_face_beg_x = x;
28264 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28266 else
28268 /* This row is in a right to left paragraph. Scan it right to
28269 left. */
28270 struct glyph *g;
28272 end = r1->glyphs[TEXT_AREA] - 1;
28273 glyph = end + r1->used[TEXT_AREA];
28275 /* Skip truncation glyphs at the start of the glyph row. */
28276 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28277 for (; glyph > end
28278 && INTEGERP (glyph->object)
28279 && glyph->charpos < 0;
28280 --glyph)
28283 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28284 or DISP_STRING, and the first glyph from buffer whose
28285 position is between START_CHARPOS and END_CHARPOS. */
28286 for (; glyph > end
28287 && !INTEGERP (glyph->object)
28288 && !EQ (glyph->object, disp_string)
28289 && !(BUFFERP (glyph->object)
28290 && (glyph->charpos >= start_charpos
28291 && glyph->charpos < end_charpos));
28292 --glyph)
28294 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28295 are present at buffer positions between START_CHARPOS and
28296 END_CHARPOS, or if they come from an overlay. */
28297 if (EQ (glyph->object, before_string))
28299 pos = string_buffer_position (before_string, start_charpos);
28300 /* If pos == 0, it means before_string came from an
28301 overlay, not from a buffer position. */
28302 if (!pos || (pos >= start_charpos && pos < end_charpos))
28303 break;
28305 else if (EQ (glyph->object, after_string))
28307 pos = string_buffer_position (after_string, end_charpos);
28308 if (!pos || (pos >= start_charpos && pos < end_charpos))
28309 break;
28313 glyph++; /* first glyph to the right of the highlighted area */
28314 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28315 x += g->pixel_width;
28316 hlinfo->mouse_face_beg_x = x;
28317 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28320 /* If the highlight ends in a different row, compute GLYPH and END
28321 for the end row. Otherwise, reuse the values computed above for
28322 the row where the highlight begins. */
28323 if (r2 != r1)
28325 if (!r2->reversed_p)
28327 glyph = r2->glyphs[TEXT_AREA];
28328 end = glyph + r2->used[TEXT_AREA];
28329 x = r2->x;
28331 else
28333 end = r2->glyphs[TEXT_AREA] - 1;
28334 glyph = end + r2->used[TEXT_AREA];
28338 if (!r2->reversed_p)
28340 /* Skip truncation and continuation glyphs near the end of the
28341 row, and also blanks and stretch glyphs inserted by
28342 extend_face_to_end_of_line. */
28343 while (end > glyph
28344 && INTEGERP ((end - 1)->object))
28345 --end;
28346 /* Scan the rest of the glyph row from the end, looking for the
28347 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28348 DISP_STRING, or whose position is between START_CHARPOS
28349 and END_CHARPOS */
28350 for (--end;
28351 end > glyph
28352 && !INTEGERP (end->object)
28353 && !EQ (end->object, disp_string)
28354 && !(BUFFERP (end->object)
28355 && (end->charpos >= start_charpos
28356 && end->charpos < end_charpos));
28357 --end)
28359 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28360 are present at buffer positions between START_CHARPOS and
28361 END_CHARPOS, or if they come from an overlay. */
28362 if (EQ (end->object, before_string))
28364 pos = string_buffer_position (before_string, start_charpos);
28365 if (!pos || (pos >= start_charpos && pos < end_charpos))
28366 break;
28368 else if (EQ (end->object, after_string))
28370 pos = string_buffer_position (after_string, end_charpos);
28371 if (!pos || (pos >= start_charpos && pos < end_charpos))
28372 break;
28375 /* Find the X coordinate of the last glyph to be highlighted. */
28376 for (; glyph <= end; ++glyph)
28377 x += glyph->pixel_width;
28379 hlinfo->mouse_face_end_x = x;
28380 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28382 else
28384 /* Skip truncation and continuation glyphs near the end of the
28385 row, and also blanks and stretch glyphs inserted by
28386 extend_face_to_end_of_line. */
28387 x = r2->x;
28388 end++;
28389 while (end < glyph
28390 && INTEGERP (end->object))
28392 x += end->pixel_width;
28393 ++end;
28395 /* Scan the rest of the glyph row from the end, looking for the
28396 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28397 DISP_STRING, or whose position is between START_CHARPOS
28398 and END_CHARPOS */
28399 for ( ;
28400 end < glyph
28401 && !INTEGERP (end->object)
28402 && !EQ (end->object, disp_string)
28403 && !(BUFFERP (end->object)
28404 && (end->charpos >= start_charpos
28405 && end->charpos < end_charpos));
28406 ++end)
28408 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28409 are present at buffer positions between START_CHARPOS and
28410 END_CHARPOS, or if they come from an overlay. */
28411 if (EQ (end->object, before_string))
28413 pos = string_buffer_position (before_string, start_charpos);
28414 if (!pos || (pos >= start_charpos && pos < end_charpos))
28415 break;
28417 else if (EQ (end->object, after_string))
28419 pos = string_buffer_position (after_string, end_charpos);
28420 if (!pos || (pos >= start_charpos && pos < end_charpos))
28421 break;
28423 x += end->pixel_width;
28425 /* If we exited the above loop because we arrived at the last
28426 glyph of the row, and its buffer position is still not in
28427 range, it means the last character in range is the preceding
28428 newline. Bump the end column and x values to get past the
28429 last glyph. */
28430 if (end == glyph
28431 && BUFFERP (end->object)
28432 && (end->charpos < start_charpos
28433 || end->charpos >= end_charpos))
28435 x += end->pixel_width;
28436 ++end;
28438 hlinfo->mouse_face_end_x = x;
28439 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28442 hlinfo->mouse_face_window = window;
28443 hlinfo->mouse_face_face_id
28444 = face_at_buffer_position (w, mouse_charpos, &ignore,
28445 mouse_charpos + 1,
28446 !hlinfo->mouse_face_hidden, -1);
28447 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28450 /* The following function is not used anymore (replaced with
28451 mouse_face_from_string_pos), but I leave it here for the time
28452 being, in case someone would. */
28454 #if 0 /* not used */
28456 /* Find the position of the glyph for position POS in OBJECT in
28457 window W's current matrix, and return in *X, *Y the pixel
28458 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28460 RIGHT_P non-zero means return the position of the right edge of the
28461 glyph, RIGHT_P zero means return the left edge position.
28463 If no glyph for POS exists in the matrix, return the position of
28464 the glyph with the next smaller position that is in the matrix, if
28465 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28466 exists in the matrix, return the position of the glyph with the
28467 next larger position in OBJECT.
28469 Value is non-zero if a glyph was found. */
28471 static int
28472 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28473 int *hpos, int *vpos, int *x, int *y, int right_p)
28475 int yb = window_text_bottom_y (w);
28476 struct glyph_row *r;
28477 struct glyph *best_glyph = NULL;
28478 struct glyph_row *best_row = NULL;
28479 int best_x = 0;
28481 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28482 r->enabled_p && r->y < yb;
28483 ++r)
28485 struct glyph *g = r->glyphs[TEXT_AREA];
28486 struct glyph *e = g + r->used[TEXT_AREA];
28487 int gx;
28489 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28490 if (EQ (g->object, object))
28492 if (g->charpos == pos)
28494 best_glyph = g;
28495 best_x = gx;
28496 best_row = r;
28497 goto found;
28499 else if (best_glyph == NULL
28500 || ((eabs (g->charpos - pos)
28501 < eabs (best_glyph->charpos - pos))
28502 && (right_p
28503 ? g->charpos < pos
28504 : g->charpos > pos)))
28506 best_glyph = g;
28507 best_x = gx;
28508 best_row = r;
28513 found:
28515 if (best_glyph)
28517 *x = best_x;
28518 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
28520 if (right_p)
28522 *x += best_glyph->pixel_width;
28523 ++*hpos;
28526 *y = best_row->y;
28527 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
28530 return best_glyph != NULL;
28532 #endif /* not used */
28534 /* Find the positions of the first and the last glyphs in window W's
28535 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
28536 (assumed to be a string), and return in HLINFO's mouse_face_*
28537 members the pixel and column/row coordinates of those glyphs. */
28539 static void
28540 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
28541 Lisp_Object object,
28542 ptrdiff_t startpos, ptrdiff_t endpos)
28544 int yb = window_text_bottom_y (w);
28545 struct glyph_row *r;
28546 struct glyph *g, *e;
28547 int gx;
28548 int found = 0;
28550 /* Find the glyph row with at least one position in the range
28551 [STARTPOS..ENDPOS), and the first glyph in that row whose
28552 position belongs to that range. */
28553 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28554 r->enabled_p && r->y < yb;
28555 ++r)
28557 if (!r->reversed_p)
28559 g = r->glyphs[TEXT_AREA];
28560 e = g + r->used[TEXT_AREA];
28561 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28562 if (EQ (g->object, object)
28563 && startpos <= g->charpos && g->charpos < endpos)
28565 hlinfo->mouse_face_beg_row
28566 = MATRIX_ROW_VPOS (r, w->current_matrix);
28567 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28568 hlinfo->mouse_face_beg_x = gx;
28569 found = 1;
28570 break;
28573 else
28575 struct glyph *g1;
28577 e = r->glyphs[TEXT_AREA];
28578 g = e + r->used[TEXT_AREA];
28579 for ( ; g > e; --g)
28580 if (EQ ((g-1)->object, object)
28581 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
28583 hlinfo->mouse_face_beg_row
28584 = MATRIX_ROW_VPOS (r, w->current_matrix);
28585 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
28586 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
28587 gx += g1->pixel_width;
28588 hlinfo->mouse_face_beg_x = gx;
28589 found = 1;
28590 break;
28593 if (found)
28594 break;
28597 if (!found)
28598 return;
28600 /* Starting with the next row, look for the first row which does NOT
28601 include any glyphs whose positions are in the range. */
28602 for (++r; r->enabled_p && r->y < yb; ++r)
28604 g = r->glyphs[TEXT_AREA];
28605 e = g + r->used[TEXT_AREA];
28606 found = 0;
28607 for ( ; g < e; ++g)
28608 if (EQ (g->object, object)
28609 && startpos <= g->charpos && g->charpos < endpos)
28611 found = 1;
28612 break;
28614 if (!found)
28615 break;
28618 /* The highlighted region ends on the previous row. */
28619 r--;
28621 /* Set the end row. */
28622 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
28624 /* Compute and set the end column and the end column's horizontal
28625 pixel coordinate. */
28626 if (!r->reversed_p)
28628 g = r->glyphs[TEXT_AREA];
28629 e = g + r->used[TEXT_AREA];
28630 for ( ; e > g; --e)
28631 if (EQ ((e-1)->object, object)
28632 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
28633 break;
28634 hlinfo->mouse_face_end_col = e - g;
28636 for (gx = r->x; g < e; ++g)
28637 gx += g->pixel_width;
28638 hlinfo->mouse_face_end_x = gx;
28640 else
28642 e = r->glyphs[TEXT_AREA];
28643 g = e + r->used[TEXT_AREA];
28644 for (gx = r->x ; e < g; ++e)
28646 if (EQ (e->object, object)
28647 && startpos <= e->charpos && e->charpos < endpos)
28648 break;
28649 gx += e->pixel_width;
28651 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
28652 hlinfo->mouse_face_end_x = gx;
28656 #ifdef HAVE_WINDOW_SYSTEM
28658 /* See if position X, Y is within a hot-spot of an image. */
28660 static int
28661 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
28663 if (!CONSP (hot_spot))
28664 return 0;
28666 if (EQ (XCAR (hot_spot), Qrect))
28668 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
28669 Lisp_Object rect = XCDR (hot_spot);
28670 Lisp_Object tem;
28671 if (!CONSP (rect))
28672 return 0;
28673 if (!CONSP (XCAR (rect)))
28674 return 0;
28675 if (!CONSP (XCDR (rect)))
28676 return 0;
28677 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
28678 return 0;
28679 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
28680 return 0;
28681 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
28682 return 0;
28683 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
28684 return 0;
28685 return 1;
28687 else if (EQ (XCAR (hot_spot), Qcircle))
28689 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
28690 Lisp_Object circ = XCDR (hot_spot);
28691 Lisp_Object lr, lx0, ly0;
28692 if (CONSP (circ)
28693 && CONSP (XCAR (circ))
28694 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
28695 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
28696 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
28698 double r = XFLOATINT (lr);
28699 double dx = XINT (lx0) - x;
28700 double dy = XINT (ly0) - y;
28701 return (dx * dx + dy * dy <= r * r);
28704 else if (EQ (XCAR (hot_spot), Qpoly))
28706 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
28707 if (VECTORP (XCDR (hot_spot)))
28709 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
28710 Lisp_Object *poly = v->contents;
28711 ptrdiff_t n = v->header.size;
28712 ptrdiff_t i;
28713 int inside = 0;
28714 Lisp_Object lx, ly;
28715 int x0, y0;
28717 /* Need an even number of coordinates, and at least 3 edges. */
28718 if (n < 6 || n & 1)
28719 return 0;
28721 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
28722 If count is odd, we are inside polygon. Pixels on edges
28723 may or may not be included depending on actual geometry of the
28724 polygon. */
28725 if ((lx = poly[n-2], !INTEGERP (lx))
28726 || (ly = poly[n-1], !INTEGERP (lx)))
28727 return 0;
28728 x0 = XINT (lx), y0 = XINT (ly);
28729 for (i = 0; i < n; i += 2)
28731 int x1 = x0, y1 = y0;
28732 if ((lx = poly[i], !INTEGERP (lx))
28733 || (ly = poly[i+1], !INTEGERP (ly)))
28734 return 0;
28735 x0 = XINT (lx), y0 = XINT (ly);
28737 /* Does this segment cross the X line? */
28738 if (x0 >= x)
28740 if (x1 >= x)
28741 continue;
28743 else if (x1 < x)
28744 continue;
28745 if (y > y0 && y > y1)
28746 continue;
28747 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
28748 inside = !inside;
28750 return inside;
28753 return 0;
28756 Lisp_Object
28757 find_hot_spot (Lisp_Object map, int x, int y)
28759 while (CONSP (map))
28761 if (CONSP (XCAR (map))
28762 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
28763 return XCAR (map);
28764 map = XCDR (map);
28767 return Qnil;
28770 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
28771 3, 3, 0,
28772 doc: /* Lookup in image map MAP coordinates X and Y.
28773 An image map is an alist where each element has the format (AREA ID PLIST).
28774 An AREA is specified as either a rectangle, a circle, or a polygon:
28775 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
28776 pixel coordinates of the upper left and bottom right corners.
28777 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
28778 and the radius of the circle; r may be a float or integer.
28779 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
28780 vector describes one corner in the polygon.
28781 Returns the alist element for the first matching AREA in MAP. */)
28782 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
28784 if (NILP (map))
28785 return Qnil;
28787 CHECK_NUMBER (x);
28788 CHECK_NUMBER (y);
28790 return find_hot_spot (map,
28791 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
28792 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
28796 /* Display frame CURSOR, optionally using shape defined by POINTER. */
28797 static void
28798 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
28800 /* Do not change cursor shape while dragging mouse. */
28801 if (!NILP (do_mouse_tracking))
28802 return;
28804 if (!NILP (pointer))
28806 if (EQ (pointer, Qarrow))
28807 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28808 else if (EQ (pointer, Qhand))
28809 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
28810 else if (EQ (pointer, Qtext))
28811 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28812 else if (EQ (pointer, intern ("hdrag")))
28813 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28814 else if (EQ (pointer, intern ("nhdrag")))
28815 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
28816 #ifdef HAVE_X_WINDOWS
28817 else if (EQ (pointer, intern ("vdrag")))
28818 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
28819 #endif
28820 else if (EQ (pointer, intern ("hourglass")))
28821 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
28822 else if (EQ (pointer, Qmodeline))
28823 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
28824 else
28825 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28828 if (cursor != No_Cursor)
28829 FRAME_RIF (f)->define_frame_cursor (f, cursor);
28832 #endif /* HAVE_WINDOW_SYSTEM */
28834 /* Take proper action when mouse has moved to the mode or header line
28835 or marginal area AREA of window W, x-position X and y-position Y.
28836 X is relative to the start of the text display area of W, so the
28837 width of bitmap areas and scroll bars must be subtracted to get a
28838 position relative to the start of the mode line. */
28840 static void
28841 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
28842 enum window_part area)
28844 struct window *w = XWINDOW (window);
28845 struct frame *f = XFRAME (w->frame);
28846 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28847 #ifdef HAVE_WINDOW_SYSTEM
28848 Display_Info *dpyinfo;
28849 #endif
28850 Cursor cursor = No_Cursor;
28851 Lisp_Object pointer = Qnil;
28852 int dx, dy, width, height;
28853 ptrdiff_t charpos;
28854 Lisp_Object string, object = Qnil;
28855 Lisp_Object pos IF_LINT (= Qnil), help;
28857 Lisp_Object mouse_face;
28858 int original_x_pixel = x;
28859 struct glyph * glyph = NULL, * row_start_glyph = NULL;
28860 struct glyph_row *row IF_LINT (= 0);
28862 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
28864 int x0;
28865 struct glyph *end;
28867 /* Kludge alert: mode_line_string takes X/Y in pixels, but
28868 returns them in row/column units! */
28869 string = mode_line_string (w, area, &x, &y, &charpos,
28870 &object, &dx, &dy, &width, &height);
28872 row = (area == ON_MODE_LINE
28873 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
28874 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
28876 /* Find the glyph under the mouse pointer. */
28877 if (row->mode_line_p && row->enabled_p)
28879 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
28880 end = glyph + row->used[TEXT_AREA];
28882 for (x0 = original_x_pixel;
28883 glyph < end && x0 >= glyph->pixel_width;
28884 ++glyph)
28885 x0 -= glyph->pixel_width;
28887 if (glyph >= end)
28888 glyph = NULL;
28891 else
28893 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
28894 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
28895 returns them in row/column units! */
28896 string = marginal_area_string (w, area, &x, &y, &charpos,
28897 &object, &dx, &dy, &width, &height);
28900 help = Qnil;
28902 #ifdef HAVE_WINDOW_SYSTEM
28903 if (IMAGEP (object))
28905 Lisp_Object image_map, hotspot;
28906 if ((image_map = Fplist_get (XCDR (object), QCmap),
28907 !NILP (image_map))
28908 && (hotspot = find_hot_spot (image_map, dx, dy),
28909 CONSP (hotspot))
28910 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28912 Lisp_Object plist;
28914 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
28915 If so, we could look for mouse-enter, mouse-leave
28916 properties in PLIST (and do something...). */
28917 hotspot = XCDR (hotspot);
28918 if (CONSP (hotspot)
28919 && (plist = XCAR (hotspot), CONSP (plist)))
28921 pointer = Fplist_get (plist, Qpointer);
28922 if (NILP (pointer))
28923 pointer = Qhand;
28924 help = Fplist_get (plist, Qhelp_echo);
28925 if (!NILP (help))
28927 help_echo_string = help;
28928 XSETWINDOW (help_echo_window, w);
28929 help_echo_object = w->contents;
28930 help_echo_pos = charpos;
28934 if (NILP (pointer))
28935 pointer = Fplist_get (XCDR (object), QCpointer);
28937 #endif /* HAVE_WINDOW_SYSTEM */
28939 if (STRINGP (string))
28940 pos = make_number (charpos);
28942 /* Set the help text and mouse pointer. If the mouse is on a part
28943 of the mode line without any text (e.g. past the right edge of
28944 the mode line text), use the default help text and pointer. */
28945 if (STRINGP (string) || area == ON_MODE_LINE)
28947 /* Arrange to display the help by setting the global variables
28948 help_echo_string, help_echo_object, and help_echo_pos. */
28949 if (NILP (help))
28951 if (STRINGP (string))
28952 help = Fget_text_property (pos, Qhelp_echo, string);
28954 if (!NILP (help))
28956 help_echo_string = help;
28957 XSETWINDOW (help_echo_window, w);
28958 help_echo_object = string;
28959 help_echo_pos = charpos;
28961 else if (area == ON_MODE_LINE)
28963 Lisp_Object default_help
28964 = buffer_local_value (Qmode_line_default_help_echo,
28965 w->contents);
28967 if (STRINGP (default_help))
28969 help_echo_string = default_help;
28970 XSETWINDOW (help_echo_window, w);
28971 help_echo_object = Qnil;
28972 help_echo_pos = -1;
28977 #ifdef HAVE_WINDOW_SYSTEM
28978 /* Change the mouse pointer according to what is under it. */
28979 if (FRAME_WINDOW_P (f))
28981 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
28982 || minibuf_level
28983 || NILP (Vresize_mini_windows));
28985 dpyinfo = FRAME_DISPLAY_INFO (f);
28986 if (STRINGP (string))
28988 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28990 if (NILP (pointer))
28991 pointer = Fget_text_property (pos, Qpointer, string);
28993 /* Change the mouse pointer according to what is under X/Y. */
28994 if (NILP (pointer)
28995 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
28997 Lisp_Object map;
28998 map = Fget_text_property (pos, Qlocal_map, string);
28999 if (!KEYMAPP (map))
29000 map = Fget_text_property (pos, Qkeymap, string);
29001 if (!KEYMAPP (map) && draggable)
29002 cursor = dpyinfo->vertical_scroll_bar_cursor;
29005 else if (draggable)
29006 /* Default mode-line pointer. */
29007 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29009 #endif
29012 /* Change the mouse face according to what is under X/Y. */
29013 if (STRINGP (string))
29015 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29016 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29017 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29018 && glyph)
29020 Lisp_Object b, e;
29022 struct glyph * tmp_glyph;
29024 int gpos;
29025 int gseq_length;
29026 int total_pixel_width;
29027 ptrdiff_t begpos, endpos, ignore;
29029 int vpos, hpos;
29031 b = Fprevious_single_property_change (make_number (charpos + 1),
29032 Qmouse_face, string, Qnil);
29033 if (NILP (b))
29034 begpos = 0;
29035 else
29036 begpos = XINT (b);
29038 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29039 if (NILP (e))
29040 endpos = SCHARS (string);
29041 else
29042 endpos = XINT (e);
29044 /* Calculate the glyph position GPOS of GLYPH in the
29045 displayed string, relative to the beginning of the
29046 highlighted part of the string.
29048 Note: GPOS is different from CHARPOS. CHARPOS is the
29049 position of GLYPH in the internal string object. A mode
29050 line string format has structures which are converted to
29051 a flattened string by the Emacs Lisp interpreter. The
29052 internal string is an element of those structures. The
29053 displayed string is the flattened string. */
29054 tmp_glyph = row_start_glyph;
29055 while (tmp_glyph < glyph
29056 && (!(EQ (tmp_glyph->object, glyph->object)
29057 && begpos <= tmp_glyph->charpos
29058 && tmp_glyph->charpos < endpos)))
29059 tmp_glyph++;
29060 gpos = glyph - tmp_glyph;
29062 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29063 the highlighted part of the displayed string to which
29064 GLYPH belongs. Note: GSEQ_LENGTH is different from
29065 SCHARS (STRING), because the latter returns the length of
29066 the internal string. */
29067 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29068 tmp_glyph > glyph
29069 && (!(EQ (tmp_glyph->object, glyph->object)
29070 && begpos <= tmp_glyph->charpos
29071 && tmp_glyph->charpos < endpos));
29072 tmp_glyph--)
29074 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29076 /* Calculate the total pixel width of all the glyphs between
29077 the beginning of the highlighted area and GLYPH. */
29078 total_pixel_width = 0;
29079 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29080 total_pixel_width += tmp_glyph->pixel_width;
29082 /* Pre calculation of re-rendering position. Note: X is in
29083 column units here, after the call to mode_line_string or
29084 marginal_area_string. */
29085 hpos = x - gpos;
29086 vpos = (area == ON_MODE_LINE
29087 ? (w->current_matrix)->nrows - 1
29088 : 0);
29090 /* If GLYPH's position is included in the region that is
29091 already drawn in mouse face, we have nothing to do. */
29092 if ( EQ (window, hlinfo->mouse_face_window)
29093 && (!row->reversed_p
29094 ? (hlinfo->mouse_face_beg_col <= hpos
29095 && hpos < hlinfo->mouse_face_end_col)
29096 /* In R2L rows we swap BEG and END, see below. */
29097 : (hlinfo->mouse_face_end_col <= hpos
29098 && hpos < hlinfo->mouse_face_beg_col))
29099 && hlinfo->mouse_face_beg_row == vpos )
29100 return;
29102 if (clear_mouse_face (hlinfo))
29103 cursor = No_Cursor;
29105 if (!row->reversed_p)
29107 hlinfo->mouse_face_beg_col = hpos;
29108 hlinfo->mouse_face_beg_x = original_x_pixel
29109 - (total_pixel_width + dx);
29110 hlinfo->mouse_face_end_col = hpos + gseq_length;
29111 hlinfo->mouse_face_end_x = 0;
29113 else
29115 /* In R2L rows, show_mouse_face expects BEG and END
29116 coordinates to be swapped. */
29117 hlinfo->mouse_face_end_col = hpos;
29118 hlinfo->mouse_face_end_x = original_x_pixel
29119 - (total_pixel_width + dx);
29120 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29121 hlinfo->mouse_face_beg_x = 0;
29124 hlinfo->mouse_face_beg_row = vpos;
29125 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29126 hlinfo->mouse_face_past_end = 0;
29127 hlinfo->mouse_face_window = window;
29129 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29130 charpos,
29131 0, &ignore,
29132 glyph->face_id,
29134 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29136 if (NILP (pointer))
29137 pointer = Qhand;
29139 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29140 clear_mouse_face (hlinfo);
29142 #ifdef HAVE_WINDOW_SYSTEM
29143 if (FRAME_WINDOW_P (f))
29144 define_frame_cursor1 (f, cursor, pointer);
29145 #endif
29149 /* EXPORT:
29150 Take proper action when the mouse has moved to position X, Y on
29151 frame F with regards to highlighting portions of display that have
29152 mouse-face properties. Also de-highlight portions of display where
29153 the mouse was before, set the mouse pointer shape as appropriate
29154 for the mouse coordinates, and activate help echo (tooltips).
29155 X and Y can be negative or out of range. */
29157 void
29158 note_mouse_highlight (struct frame *f, int x, int y)
29160 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29161 enum window_part part = ON_NOTHING;
29162 Lisp_Object window;
29163 struct window *w;
29164 Cursor cursor = No_Cursor;
29165 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29166 struct buffer *b;
29168 /* When a menu is active, don't highlight because this looks odd. */
29169 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29170 if (popup_activated ())
29171 return;
29172 #endif
29174 if (!f->glyphs_initialized_p
29175 || f->pointer_invisible)
29176 return;
29178 hlinfo->mouse_face_mouse_x = x;
29179 hlinfo->mouse_face_mouse_y = y;
29180 hlinfo->mouse_face_mouse_frame = f;
29182 if (hlinfo->mouse_face_defer)
29183 return;
29185 /* Which window is that in? */
29186 window = window_from_coordinates (f, x, y, &part, 1);
29188 /* If displaying active text in another window, clear that. */
29189 if (! EQ (window, hlinfo->mouse_face_window)
29190 /* Also clear if we move out of text area in same window. */
29191 || (!NILP (hlinfo->mouse_face_window)
29192 && !NILP (window)
29193 && part != ON_TEXT
29194 && part != ON_MODE_LINE
29195 && part != ON_HEADER_LINE))
29196 clear_mouse_face (hlinfo);
29198 /* Not on a window -> return. */
29199 if (!WINDOWP (window))
29200 return;
29202 /* Reset help_echo_string. It will get recomputed below. */
29203 help_echo_string = Qnil;
29205 /* Convert to window-relative pixel coordinates. */
29206 w = XWINDOW (window);
29207 frame_to_window_pixel_xy (w, &x, &y);
29209 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29210 /* Handle tool-bar window differently since it doesn't display a
29211 buffer. */
29212 if (EQ (window, f->tool_bar_window))
29214 note_tool_bar_highlight (f, x, y);
29215 return;
29217 #endif
29219 /* Mouse is on the mode, header line or margin? */
29220 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29221 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29223 note_mode_line_or_margin_highlight (window, x, y, part);
29225 #ifdef HAVE_WINDOW_SYSTEM
29226 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29228 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29229 /* Show non-text cursor (Bug#16647). */
29230 goto set_cursor;
29232 else
29233 #endif
29234 return;
29237 #ifdef HAVE_WINDOW_SYSTEM
29238 if (part == ON_VERTICAL_BORDER)
29240 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29241 help_echo_string = build_string ("drag-mouse-1: resize");
29243 else if (part == ON_RIGHT_DIVIDER)
29245 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29246 help_echo_string = build_string ("drag-mouse-1: resize");
29248 else if (part == ON_BOTTOM_DIVIDER)
29249 if (! WINDOW_BOTTOMMOST_P (w)
29250 || minibuf_level
29251 || NILP (Vresize_mini_windows))
29253 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29254 help_echo_string = build_string ("drag-mouse-1: resize");
29256 else
29257 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29258 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29259 || part == ON_VERTICAL_SCROLL_BAR
29260 || part == ON_HORIZONTAL_SCROLL_BAR)
29261 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29262 else
29263 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29264 #endif
29266 /* Are we in a window whose display is up to date?
29267 And verify the buffer's text has not changed. */
29268 b = XBUFFER (w->contents);
29269 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29271 int hpos, vpos, dx, dy, area = LAST_AREA;
29272 ptrdiff_t pos;
29273 struct glyph *glyph;
29274 Lisp_Object object;
29275 Lisp_Object mouse_face = Qnil, position;
29276 Lisp_Object *overlay_vec = NULL;
29277 ptrdiff_t i, noverlays;
29278 struct buffer *obuf;
29279 ptrdiff_t obegv, ozv;
29280 int same_region;
29282 /* Find the glyph under X/Y. */
29283 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29285 #ifdef HAVE_WINDOW_SYSTEM
29286 /* Look for :pointer property on image. */
29287 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29289 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29290 if (img != NULL && IMAGEP (img->spec))
29292 Lisp_Object image_map, hotspot;
29293 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29294 !NILP (image_map))
29295 && (hotspot = find_hot_spot (image_map,
29296 glyph->slice.img.x + dx,
29297 glyph->slice.img.y + dy),
29298 CONSP (hotspot))
29299 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29301 Lisp_Object plist;
29303 /* Could check XCAR (hotspot) to see if we enter/leave
29304 this hot-spot.
29305 If so, we could look for mouse-enter, mouse-leave
29306 properties in PLIST (and do something...). */
29307 hotspot = XCDR (hotspot);
29308 if (CONSP (hotspot)
29309 && (plist = XCAR (hotspot), CONSP (plist)))
29311 pointer = Fplist_get (plist, Qpointer);
29312 if (NILP (pointer))
29313 pointer = Qhand;
29314 help_echo_string = Fplist_get (plist, Qhelp_echo);
29315 if (!NILP (help_echo_string))
29317 help_echo_window = window;
29318 help_echo_object = glyph->object;
29319 help_echo_pos = glyph->charpos;
29323 if (NILP (pointer))
29324 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29327 #endif /* HAVE_WINDOW_SYSTEM */
29329 /* Clear mouse face if X/Y not over text. */
29330 if (glyph == NULL
29331 || area != TEXT_AREA
29332 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29333 /* Glyph's OBJECT is an integer for glyphs inserted by the
29334 display engine for its internal purposes, like truncation
29335 and continuation glyphs and blanks beyond the end of
29336 line's text on text terminals. If we are over such a
29337 glyph, we are not over any text. */
29338 || INTEGERP (glyph->object)
29339 /* R2L rows have a stretch glyph at their front, which
29340 stands for no text, whereas L2R rows have no glyphs at
29341 all beyond the end of text. Treat such stretch glyphs
29342 like we do with NULL glyphs in L2R rows. */
29343 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29344 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29345 && glyph->type == STRETCH_GLYPH
29346 && glyph->avoid_cursor_p))
29348 if (clear_mouse_face (hlinfo))
29349 cursor = No_Cursor;
29350 #ifdef HAVE_WINDOW_SYSTEM
29351 if (FRAME_WINDOW_P (f) && NILP (pointer))
29353 if (area != TEXT_AREA)
29354 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29355 else
29356 pointer = Vvoid_text_area_pointer;
29358 #endif
29359 goto set_cursor;
29362 pos = glyph->charpos;
29363 object = glyph->object;
29364 if (!STRINGP (object) && !BUFFERP (object))
29365 goto set_cursor;
29367 /* If we get an out-of-range value, return now; avoid an error. */
29368 if (BUFFERP (object) && pos > BUF_Z (b))
29369 goto set_cursor;
29371 /* Make the window's buffer temporarily current for
29372 overlays_at and compute_char_face. */
29373 obuf = current_buffer;
29374 current_buffer = b;
29375 obegv = BEGV;
29376 ozv = ZV;
29377 BEGV = BEG;
29378 ZV = Z;
29380 /* Is this char mouse-active or does it have help-echo? */
29381 position = make_number (pos);
29383 USE_SAFE_ALLOCA;
29385 if (BUFFERP (object))
29387 /* Put all the overlays we want in a vector in overlay_vec. */
29388 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29389 /* Sort overlays into increasing priority order. */
29390 noverlays = sort_overlays (overlay_vec, noverlays, w);
29392 else
29393 noverlays = 0;
29395 if (NILP (Vmouse_highlight))
29397 clear_mouse_face (hlinfo);
29398 goto check_help_echo;
29401 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29403 if (same_region)
29404 cursor = No_Cursor;
29406 /* Check mouse-face highlighting. */
29407 if (! same_region
29408 /* If there exists an overlay with mouse-face overlapping
29409 the one we are currently highlighting, we have to
29410 check if we enter the overlapping overlay, and then
29411 highlight only that. */
29412 || (OVERLAYP (hlinfo->mouse_face_overlay)
29413 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29415 /* Find the highest priority overlay with a mouse-face. */
29416 Lisp_Object overlay = Qnil;
29417 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29419 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29420 if (!NILP (mouse_face))
29421 overlay = overlay_vec[i];
29424 /* If we're highlighting the same overlay as before, there's
29425 no need to do that again. */
29426 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29427 goto check_help_echo;
29428 hlinfo->mouse_face_overlay = overlay;
29430 /* Clear the display of the old active region, if any. */
29431 if (clear_mouse_face (hlinfo))
29432 cursor = No_Cursor;
29434 /* If no overlay applies, get a text property. */
29435 if (NILP (overlay))
29436 mouse_face = Fget_text_property (position, Qmouse_face, object);
29438 /* Next, compute the bounds of the mouse highlighting and
29439 display it. */
29440 if (!NILP (mouse_face) && STRINGP (object))
29442 /* The mouse-highlighting comes from a display string
29443 with a mouse-face. */
29444 Lisp_Object s, e;
29445 ptrdiff_t ignore;
29447 s = Fprevious_single_property_change
29448 (make_number (pos + 1), Qmouse_face, object, Qnil);
29449 e = Fnext_single_property_change
29450 (position, Qmouse_face, object, Qnil);
29451 if (NILP (s))
29452 s = make_number (0);
29453 if (NILP (e))
29454 e = make_number (SCHARS (object));
29455 mouse_face_from_string_pos (w, hlinfo, object,
29456 XINT (s), XINT (e));
29457 hlinfo->mouse_face_past_end = 0;
29458 hlinfo->mouse_face_window = window;
29459 hlinfo->mouse_face_face_id
29460 = face_at_string_position (w, object, pos, 0, &ignore,
29461 glyph->face_id, 1);
29462 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29463 cursor = No_Cursor;
29465 else
29467 /* The mouse-highlighting, if any, comes from an overlay
29468 or text property in the buffer. */
29469 Lisp_Object buffer IF_LINT (= Qnil);
29470 Lisp_Object disp_string IF_LINT (= Qnil);
29472 if (STRINGP (object))
29474 /* If we are on a display string with no mouse-face,
29475 check if the text under it has one. */
29476 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29477 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29478 pos = string_buffer_position (object, start);
29479 if (pos > 0)
29481 mouse_face = get_char_property_and_overlay
29482 (make_number (pos), Qmouse_face, w->contents, &overlay);
29483 buffer = w->contents;
29484 disp_string = object;
29487 else
29489 buffer = object;
29490 disp_string = Qnil;
29493 if (!NILP (mouse_face))
29495 Lisp_Object before, after;
29496 Lisp_Object before_string, after_string;
29497 /* To correctly find the limits of mouse highlight
29498 in a bidi-reordered buffer, we must not use the
29499 optimization of limiting the search in
29500 previous-single-property-change and
29501 next-single-property-change, because
29502 rows_from_pos_range needs the real start and end
29503 positions to DTRT in this case. That's because
29504 the first row visible in a window does not
29505 necessarily display the character whose position
29506 is the smallest. */
29507 Lisp_Object lim1
29508 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29509 ? Fmarker_position (w->start)
29510 : Qnil;
29511 Lisp_Object lim2
29512 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
29513 ? make_number (BUF_Z (XBUFFER (buffer))
29514 - w->window_end_pos)
29515 : Qnil;
29517 if (NILP (overlay))
29519 /* Handle the text property case. */
29520 before = Fprevious_single_property_change
29521 (make_number (pos + 1), Qmouse_face, buffer, lim1);
29522 after = Fnext_single_property_change
29523 (make_number (pos), Qmouse_face, buffer, lim2);
29524 before_string = after_string = Qnil;
29526 else
29528 /* Handle the overlay case. */
29529 before = Foverlay_start (overlay);
29530 after = Foverlay_end (overlay);
29531 before_string = Foverlay_get (overlay, Qbefore_string);
29532 after_string = Foverlay_get (overlay, Qafter_string);
29534 if (!STRINGP (before_string)) before_string = Qnil;
29535 if (!STRINGP (after_string)) after_string = Qnil;
29538 mouse_face_from_buffer_pos (window, hlinfo, pos,
29539 NILP (before)
29541 : XFASTINT (before),
29542 NILP (after)
29543 ? BUF_Z (XBUFFER (buffer))
29544 : XFASTINT (after),
29545 before_string, after_string,
29546 disp_string);
29547 cursor = No_Cursor;
29552 check_help_echo:
29554 /* Look for a `help-echo' property. */
29555 if (NILP (help_echo_string)) {
29556 Lisp_Object help, overlay;
29558 /* Check overlays first. */
29559 help = overlay = Qnil;
29560 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
29562 overlay = overlay_vec[i];
29563 help = Foverlay_get (overlay, Qhelp_echo);
29566 if (!NILP (help))
29568 help_echo_string = help;
29569 help_echo_window = window;
29570 help_echo_object = overlay;
29571 help_echo_pos = pos;
29573 else
29575 Lisp_Object obj = glyph->object;
29576 ptrdiff_t charpos = glyph->charpos;
29578 /* Try text properties. */
29579 if (STRINGP (obj)
29580 && charpos >= 0
29581 && charpos < SCHARS (obj))
29583 help = Fget_text_property (make_number (charpos),
29584 Qhelp_echo, obj);
29585 if (NILP (help))
29587 /* If the string itself doesn't specify a help-echo,
29588 see if the buffer text ``under'' it does. */
29589 struct glyph_row *r
29590 = MATRIX_ROW (w->current_matrix, vpos);
29591 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29592 ptrdiff_t p = string_buffer_position (obj, start);
29593 if (p > 0)
29595 help = Fget_char_property (make_number (p),
29596 Qhelp_echo, w->contents);
29597 if (!NILP (help))
29599 charpos = p;
29600 obj = w->contents;
29605 else if (BUFFERP (obj)
29606 && charpos >= BEGV
29607 && charpos < ZV)
29608 help = Fget_text_property (make_number (charpos), Qhelp_echo,
29609 obj);
29611 if (!NILP (help))
29613 help_echo_string = help;
29614 help_echo_window = window;
29615 help_echo_object = obj;
29616 help_echo_pos = charpos;
29621 #ifdef HAVE_WINDOW_SYSTEM
29622 /* Look for a `pointer' property. */
29623 if (FRAME_WINDOW_P (f) && NILP (pointer))
29625 /* Check overlays first. */
29626 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
29627 pointer = Foverlay_get (overlay_vec[i], Qpointer);
29629 if (NILP (pointer))
29631 Lisp_Object obj = glyph->object;
29632 ptrdiff_t charpos = glyph->charpos;
29634 /* Try text properties. */
29635 if (STRINGP (obj)
29636 && charpos >= 0
29637 && charpos < SCHARS (obj))
29639 pointer = Fget_text_property (make_number (charpos),
29640 Qpointer, obj);
29641 if (NILP (pointer))
29643 /* If the string itself doesn't specify a pointer,
29644 see if the buffer text ``under'' it does. */
29645 struct glyph_row *r
29646 = MATRIX_ROW (w->current_matrix, vpos);
29647 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29648 ptrdiff_t p = string_buffer_position (obj, start);
29649 if (p > 0)
29650 pointer = Fget_char_property (make_number (p),
29651 Qpointer, w->contents);
29654 else if (BUFFERP (obj)
29655 && charpos >= BEGV
29656 && charpos < ZV)
29657 pointer = Fget_text_property (make_number (charpos),
29658 Qpointer, obj);
29661 #endif /* HAVE_WINDOW_SYSTEM */
29663 BEGV = obegv;
29664 ZV = ozv;
29665 current_buffer = obuf;
29666 SAFE_FREE ();
29669 set_cursor:
29671 #ifdef HAVE_WINDOW_SYSTEM
29672 if (FRAME_WINDOW_P (f))
29673 define_frame_cursor1 (f, cursor, pointer);
29674 #else
29675 /* This is here to prevent a compiler error, about "label at end of
29676 compound statement". */
29677 return;
29678 #endif
29682 /* EXPORT for RIF:
29683 Clear any mouse-face on window W. This function is part of the
29684 redisplay interface, and is called from try_window_id and similar
29685 functions to ensure the mouse-highlight is off. */
29687 void
29688 x_clear_window_mouse_face (struct window *w)
29690 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
29691 Lisp_Object window;
29693 block_input ();
29694 XSETWINDOW (window, w);
29695 if (EQ (window, hlinfo->mouse_face_window))
29696 clear_mouse_face (hlinfo);
29697 unblock_input ();
29701 /* EXPORT:
29702 Just discard the mouse face information for frame F, if any.
29703 This is used when the size of F is changed. */
29705 void
29706 cancel_mouse_face (struct frame *f)
29708 Lisp_Object window;
29709 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29711 window = hlinfo->mouse_face_window;
29712 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
29713 reset_mouse_highlight (hlinfo);
29718 /***********************************************************************
29719 Exposure Events
29720 ***********************************************************************/
29722 #ifdef HAVE_WINDOW_SYSTEM
29724 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
29725 which intersects rectangle R. R is in window-relative coordinates. */
29727 static void
29728 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
29729 enum glyph_row_area area)
29731 struct glyph *first = row->glyphs[area];
29732 struct glyph *end = row->glyphs[area] + row->used[area];
29733 struct glyph *last;
29734 int first_x, start_x, x;
29736 if (area == TEXT_AREA && row->fill_line_p)
29737 /* If row extends face to end of line write the whole line. */
29738 draw_glyphs (w, 0, row, area,
29739 0, row->used[area],
29740 DRAW_NORMAL_TEXT, 0);
29741 else
29743 /* Set START_X to the window-relative start position for drawing glyphs of
29744 AREA. The first glyph of the text area can be partially visible.
29745 The first glyphs of other areas cannot. */
29746 start_x = window_box_left_offset (w, area);
29747 x = start_x;
29748 if (area == TEXT_AREA)
29749 x += row->x;
29751 /* Find the first glyph that must be redrawn. */
29752 while (first < end
29753 && x + first->pixel_width < r->x)
29755 x += first->pixel_width;
29756 ++first;
29759 /* Find the last one. */
29760 last = first;
29761 first_x = x;
29762 while (last < end
29763 && x < r->x + r->width)
29765 x += last->pixel_width;
29766 ++last;
29769 /* Repaint. */
29770 if (last > first)
29771 draw_glyphs (w, first_x - start_x, row, area,
29772 first - row->glyphs[area], last - row->glyphs[area],
29773 DRAW_NORMAL_TEXT, 0);
29778 /* Redraw the parts of the glyph row ROW on window W intersecting
29779 rectangle R. R is in window-relative coordinates. Value is
29780 non-zero if mouse-face was overwritten. */
29782 static int
29783 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
29785 eassert (row->enabled_p);
29787 if (row->mode_line_p || w->pseudo_window_p)
29788 draw_glyphs (w, 0, row, TEXT_AREA,
29789 0, row->used[TEXT_AREA],
29790 DRAW_NORMAL_TEXT, 0);
29791 else
29793 if (row->used[LEFT_MARGIN_AREA])
29794 expose_area (w, row, r, LEFT_MARGIN_AREA);
29795 if (row->used[TEXT_AREA])
29796 expose_area (w, row, r, TEXT_AREA);
29797 if (row->used[RIGHT_MARGIN_AREA])
29798 expose_area (w, row, r, RIGHT_MARGIN_AREA);
29799 draw_row_fringe_bitmaps (w, row);
29802 return row->mouse_face_p;
29806 /* Redraw those parts of glyphs rows during expose event handling that
29807 overlap other rows. Redrawing of an exposed line writes over parts
29808 of lines overlapping that exposed line; this function fixes that.
29810 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
29811 row in W's current matrix that is exposed and overlaps other rows.
29812 LAST_OVERLAPPING_ROW is the last such row. */
29814 static void
29815 expose_overlaps (struct window *w,
29816 struct glyph_row *first_overlapping_row,
29817 struct glyph_row *last_overlapping_row,
29818 XRectangle *r)
29820 struct glyph_row *row;
29822 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
29823 if (row->overlapping_p)
29825 eassert (row->enabled_p && !row->mode_line_p);
29827 row->clip = r;
29828 if (row->used[LEFT_MARGIN_AREA])
29829 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
29831 if (row->used[TEXT_AREA])
29832 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
29834 if (row->used[RIGHT_MARGIN_AREA])
29835 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
29836 row->clip = NULL;
29841 /* Return non-zero if W's cursor intersects rectangle R. */
29843 static int
29844 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
29846 XRectangle cr, result;
29847 struct glyph *cursor_glyph;
29848 struct glyph_row *row;
29850 if (w->phys_cursor.vpos >= 0
29851 && w->phys_cursor.vpos < w->current_matrix->nrows
29852 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
29853 row->enabled_p)
29854 && row->cursor_in_fringe_p)
29856 /* Cursor is in the fringe. */
29857 cr.x = window_box_right_offset (w,
29858 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
29859 ? RIGHT_MARGIN_AREA
29860 : TEXT_AREA));
29861 cr.y = row->y;
29862 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
29863 cr.height = row->height;
29864 return x_intersect_rectangles (&cr, r, &result);
29867 cursor_glyph = get_phys_cursor_glyph (w);
29868 if (cursor_glyph)
29870 /* r is relative to W's box, but w->phys_cursor.x is relative
29871 to left edge of W's TEXT area. Adjust it. */
29872 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
29873 cr.y = w->phys_cursor.y;
29874 cr.width = cursor_glyph->pixel_width;
29875 cr.height = w->phys_cursor_height;
29876 /* ++KFS: W32 version used W32-specific IntersectRect here, but
29877 I assume the effect is the same -- and this is portable. */
29878 return x_intersect_rectangles (&cr, r, &result);
29880 /* If we don't understand the format, pretend we're not in the hot-spot. */
29881 return 0;
29885 /* EXPORT:
29886 Draw a vertical window border to the right of window W if W doesn't
29887 have vertical scroll bars. */
29889 void
29890 x_draw_vertical_border (struct window *w)
29892 struct frame *f = XFRAME (WINDOW_FRAME (w));
29894 /* We could do better, if we knew what type of scroll-bar the adjacent
29895 windows (on either side) have... But we don't :-(
29896 However, I think this works ok. ++KFS 2003-04-25 */
29898 /* Redraw borders between horizontally adjacent windows. Don't
29899 do it for frames with vertical scroll bars because either the
29900 right scroll bar of a window, or the left scroll bar of its
29901 neighbor will suffice as a border. */
29902 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
29903 return;
29905 /* Note: It is necessary to redraw both the left and the right
29906 borders, for when only this single window W is being
29907 redisplayed. */
29908 if (!WINDOW_RIGHTMOST_P (w)
29909 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
29911 int x0, x1, y0, y1;
29913 window_box_edges (w, &x0, &y0, &x1, &y1);
29914 y1 -= 1;
29916 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29917 x1 -= 1;
29919 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
29922 if (!WINDOW_LEFTMOST_P (w)
29923 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
29925 int x0, x1, y0, y1;
29927 window_box_edges (w, &x0, &y0, &x1, &y1);
29928 y1 -= 1;
29930 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
29931 x0 -= 1;
29933 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
29938 /* Draw window dividers for window W. */
29940 void
29941 x_draw_right_divider (struct window *w)
29943 struct frame *f = WINDOW_XFRAME (w);
29945 if (w->mini || w->pseudo_window_p)
29946 return;
29947 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
29949 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
29950 int x1 = WINDOW_RIGHT_EDGE_X (w);
29951 int y0 = WINDOW_TOP_EDGE_Y (w);
29952 /* The bottom divider prevails. */
29953 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29955 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29959 static void
29960 x_draw_bottom_divider (struct window *w)
29962 struct frame *f = XFRAME (WINDOW_FRAME (w));
29964 if (w->mini || w->pseudo_window_p)
29965 return;
29966 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
29968 int x0 = WINDOW_LEFT_EDGE_X (w);
29969 int x1 = WINDOW_RIGHT_EDGE_X (w);
29970 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
29971 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
29973 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
29977 /* Redraw the part of window W intersection rectangle FR. Pixel
29978 coordinates in FR are frame-relative. Call this function with
29979 input blocked. Value is non-zero if the exposure overwrites
29980 mouse-face. */
29982 static int
29983 expose_window (struct window *w, XRectangle *fr)
29985 struct frame *f = XFRAME (w->frame);
29986 XRectangle wr, r;
29987 int mouse_face_overwritten_p = 0;
29989 /* If window is not yet fully initialized, do nothing. This can
29990 happen when toolkit scroll bars are used and a window is split.
29991 Reconfiguring the scroll bar will generate an expose for a newly
29992 created window. */
29993 if (w->current_matrix == NULL)
29994 return 0;
29996 /* When we're currently updating the window, display and current
29997 matrix usually don't agree. Arrange for a thorough display
29998 later. */
29999 if (w->must_be_updated_p)
30001 SET_FRAME_GARBAGED (f);
30002 return 0;
30005 /* Frame-relative pixel rectangle of W. */
30006 wr.x = WINDOW_LEFT_EDGE_X (w);
30007 wr.y = WINDOW_TOP_EDGE_Y (w);
30008 wr.width = WINDOW_PIXEL_WIDTH (w);
30009 wr.height = WINDOW_PIXEL_HEIGHT (w);
30011 if (x_intersect_rectangles (fr, &wr, &r))
30013 int yb = window_text_bottom_y (w);
30014 struct glyph_row *row;
30015 int cursor_cleared_p, phys_cursor_on_p;
30016 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30018 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30019 r.x, r.y, r.width, r.height));
30021 /* Convert to window coordinates. */
30022 r.x -= WINDOW_LEFT_EDGE_X (w);
30023 r.y -= WINDOW_TOP_EDGE_Y (w);
30025 /* Turn off the cursor. */
30026 if (!w->pseudo_window_p
30027 && phys_cursor_in_rect_p (w, &r))
30029 x_clear_cursor (w);
30030 cursor_cleared_p = 1;
30032 else
30033 cursor_cleared_p = 0;
30035 /* If the row containing the cursor extends face to end of line,
30036 then expose_area might overwrite the cursor outside the
30037 rectangle and thus notice_overwritten_cursor might clear
30038 w->phys_cursor_on_p. We remember the original value and
30039 check later if it is changed. */
30040 phys_cursor_on_p = w->phys_cursor_on_p;
30042 /* Update lines intersecting rectangle R. */
30043 first_overlapping_row = last_overlapping_row = NULL;
30044 for (row = w->current_matrix->rows;
30045 row->enabled_p;
30046 ++row)
30048 int y0 = row->y;
30049 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30051 if ((y0 >= r.y && y0 < r.y + r.height)
30052 || (y1 > r.y && y1 < r.y + r.height)
30053 || (r.y >= y0 && r.y < y1)
30054 || (r.y + r.height > y0 && r.y + r.height < y1))
30056 /* A header line may be overlapping, but there is no need
30057 to fix overlapping areas for them. KFS 2005-02-12 */
30058 if (row->overlapping_p && !row->mode_line_p)
30060 if (first_overlapping_row == NULL)
30061 first_overlapping_row = row;
30062 last_overlapping_row = row;
30065 row->clip = fr;
30066 if (expose_line (w, row, &r))
30067 mouse_face_overwritten_p = 1;
30068 row->clip = NULL;
30070 else if (row->overlapping_p)
30072 /* We must redraw a row overlapping the exposed area. */
30073 if (y0 < r.y
30074 ? y0 + row->phys_height > r.y
30075 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30077 if (first_overlapping_row == NULL)
30078 first_overlapping_row = row;
30079 last_overlapping_row = row;
30083 if (y1 >= yb)
30084 break;
30087 /* Display the mode line if there is one. */
30088 if (WINDOW_WANTS_MODELINE_P (w)
30089 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30090 row->enabled_p)
30091 && row->y < r.y + r.height)
30093 if (expose_line (w, row, &r))
30094 mouse_face_overwritten_p = 1;
30097 if (!w->pseudo_window_p)
30099 /* Fix the display of overlapping rows. */
30100 if (first_overlapping_row)
30101 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30102 fr);
30104 /* Draw border between windows. */
30105 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30106 x_draw_right_divider (w);
30107 else
30108 x_draw_vertical_border (w);
30110 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30111 x_draw_bottom_divider (w);
30113 /* Turn the cursor on again. */
30114 if (cursor_cleared_p
30115 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30116 update_window_cursor (w, 1);
30120 return mouse_face_overwritten_p;
30125 /* Redraw (parts) of all windows in the window tree rooted at W that
30126 intersect R. R contains frame pixel coordinates. Value is
30127 non-zero if the exposure overwrites mouse-face. */
30129 static int
30130 expose_window_tree (struct window *w, XRectangle *r)
30132 struct frame *f = XFRAME (w->frame);
30133 int mouse_face_overwritten_p = 0;
30135 while (w && !FRAME_GARBAGED_P (f))
30137 if (WINDOWP (w->contents))
30138 mouse_face_overwritten_p
30139 |= expose_window_tree (XWINDOW (w->contents), r);
30140 else
30141 mouse_face_overwritten_p |= expose_window (w, r);
30143 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30146 return mouse_face_overwritten_p;
30150 /* EXPORT:
30151 Redisplay an exposed area of frame F. X and Y are the upper-left
30152 corner of the exposed rectangle. W and H are width and height of
30153 the exposed area. All are pixel values. W or H zero means redraw
30154 the entire frame. */
30156 void
30157 expose_frame (struct frame *f, int x, int y, int w, int h)
30159 XRectangle r;
30160 int mouse_face_overwritten_p = 0;
30162 TRACE ((stderr, "expose_frame "));
30164 /* No need to redraw if frame will be redrawn soon. */
30165 if (FRAME_GARBAGED_P (f))
30167 TRACE ((stderr, " garbaged\n"));
30168 return;
30171 /* If basic faces haven't been realized yet, there is no point in
30172 trying to redraw anything. This can happen when we get an expose
30173 event while Emacs is starting, e.g. by moving another window. */
30174 if (FRAME_FACE_CACHE (f) == NULL
30175 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30177 TRACE ((stderr, " no faces\n"));
30178 return;
30181 if (w == 0 || h == 0)
30183 r.x = r.y = 0;
30184 r.width = FRAME_TEXT_WIDTH (f);
30185 r.height = FRAME_TEXT_HEIGHT (f);
30187 else
30189 r.x = x;
30190 r.y = y;
30191 r.width = w;
30192 r.height = h;
30195 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30196 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30198 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30199 if (WINDOWP (f->tool_bar_window))
30200 mouse_face_overwritten_p
30201 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30202 #endif
30204 #ifdef HAVE_X_WINDOWS
30205 #ifndef MSDOS
30206 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30207 if (WINDOWP (f->menu_bar_window))
30208 mouse_face_overwritten_p
30209 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30210 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30211 #endif
30212 #endif
30214 /* Some window managers support a focus-follows-mouse style with
30215 delayed raising of frames. Imagine a partially obscured frame,
30216 and moving the mouse into partially obscured mouse-face on that
30217 frame. The visible part of the mouse-face will be highlighted,
30218 then the WM raises the obscured frame. With at least one WM, KDE
30219 2.1, Emacs is not getting any event for the raising of the frame
30220 (even tried with SubstructureRedirectMask), only Expose events.
30221 These expose events will draw text normally, i.e. not
30222 highlighted. Which means we must redo the highlight here.
30223 Subsume it under ``we love X''. --gerd 2001-08-15 */
30224 /* Included in Windows version because Windows most likely does not
30225 do the right thing if any third party tool offers
30226 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30227 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30229 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30230 if (f == hlinfo->mouse_face_mouse_frame)
30232 int mouse_x = hlinfo->mouse_face_mouse_x;
30233 int mouse_y = hlinfo->mouse_face_mouse_y;
30234 clear_mouse_face (hlinfo);
30235 note_mouse_highlight (f, mouse_x, mouse_y);
30241 /* EXPORT:
30242 Determine the intersection of two rectangles R1 and R2. Return
30243 the intersection in *RESULT. Value is non-zero if RESULT is not
30244 empty. */
30247 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30249 XRectangle *left, *right;
30250 XRectangle *upper, *lower;
30251 int intersection_p = 0;
30253 /* Rearrange so that R1 is the left-most rectangle. */
30254 if (r1->x < r2->x)
30255 left = r1, right = r2;
30256 else
30257 left = r2, right = r1;
30259 /* X0 of the intersection is right.x0, if this is inside R1,
30260 otherwise there is no intersection. */
30261 if (right->x <= left->x + left->width)
30263 result->x = right->x;
30265 /* The right end of the intersection is the minimum of
30266 the right ends of left and right. */
30267 result->width = (min (left->x + left->width, right->x + right->width)
30268 - result->x);
30270 /* Same game for Y. */
30271 if (r1->y < r2->y)
30272 upper = r1, lower = r2;
30273 else
30274 upper = r2, lower = r1;
30276 /* The upper end of the intersection is lower.y0, if this is inside
30277 of upper. Otherwise, there is no intersection. */
30278 if (lower->y <= upper->y + upper->height)
30280 result->y = lower->y;
30282 /* The lower end of the intersection is the minimum of the lower
30283 ends of upper and lower. */
30284 result->height = (min (lower->y + lower->height,
30285 upper->y + upper->height)
30286 - result->y);
30287 intersection_p = 1;
30291 return intersection_p;
30294 #endif /* HAVE_WINDOW_SYSTEM */
30297 /***********************************************************************
30298 Initialization
30299 ***********************************************************************/
30301 void
30302 syms_of_xdisp (void)
30304 Vwith_echo_area_save_vector = Qnil;
30305 staticpro (&Vwith_echo_area_save_vector);
30307 Vmessage_stack = Qnil;
30308 staticpro (&Vmessage_stack);
30310 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30311 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30313 message_dolog_marker1 = Fmake_marker ();
30314 staticpro (&message_dolog_marker1);
30315 message_dolog_marker2 = Fmake_marker ();
30316 staticpro (&message_dolog_marker2);
30317 message_dolog_marker3 = Fmake_marker ();
30318 staticpro (&message_dolog_marker3);
30320 #ifdef GLYPH_DEBUG
30321 defsubr (&Sdump_frame_glyph_matrix);
30322 defsubr (&Sdump_glyph_matrix);
30323 defsubr (&Sdump_glyph_row);
30324 defsubr (&Sdump_tool_bar_row);
30325 defsubr (&Strace_redisplay);
30326 defsubr (&Strace_to_stderr);
30327 #endif
30328 #ifdef HAVE_WINDOW_SYSTEM
30329 defsubr (&Stool_bar_height);
30330 defsubr (&Slookup_image_map);
30331 #endif
30332 defsubr (&Sline_pixel_height);
30333 defsubr (&Sformat_mode_line);
30334 defsubr (&Sinvisible_p);
30335 defsubr (&Scurrent_bidi_paragraph_direction);
30336 defsubr (&Swindow_text_pixel_size);
30337 defsubr (&Smove_point_visually);
30339 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30340 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30341 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30342 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30343 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30344 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30345 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30346 DEFSYM (Qeval, "eval");
30347 DEFSYM (QCdata, ":data");
30348 DEFSYM (Qdisplay, "display");
30349 DEFSYM (Qspace_width, "space-width");
30350 DEFSYM (Qraise, "raise");
30351 DEFSYM (Qslice, "slice");
30352 DEFSYM (Qspace, "space");
30353 DEFSYM (Qmargin, "margin");
30354 DEFSYM (Qpointer, "pointer");
30355 DEFSYM (Qleft_margin, "left-margin");
30356 DEFSYM (Qright_margin, "right-margin");
30357 DEFSYM (Qcenter, "center");
30358 DEFSYM (Qline_height, "line-height");
30359 DEFSYM (QCalign_to, ":align-to");
30360 DEFSYM (QCrelative_width, ":relative-width");
30361 DEFSYM (QCrelative_height, ":relative-height");
30362 DEFSYM (QCeval, ":eval");
30363 DEFSYM (QCpropertize, ":propertize");
30364 DEFSYM (QCfile, ":file");
30365 DEFSYM (Qfontified, "fontified");
30366 DEFSYM (Qfontification_functions, "fontification-functions");
30367 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30368 DEFSYM (Qescape_glyph, "escape-glyph");
30369 DEFSYM (Qnobreak_space, "nobreak-space");
30370 DEFSYM (Qimage, "image");
30371 DEFSYM (Qtext, "text");
30372 DEFSYM (Qboth, "both");
30373 DEFSYM (Qboth_horiz, "both-horiz");
30374 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30375 DEFSYM (QCmap, ":map");
30376 DEFSYM (QCpointer, ":pointer");
30377 DEFSYM (Qrect, "rect");
30378 DEFSYM (Qcircle, "circle");
30379 DEFSYM (Qpoly, "poly");
30380 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30381 DEFSYM (Qgrow_only, "grow-only");
30382 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30383 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30384 DEFSYM (Qposition, "position");
30385 DEFSYM (Qbuffer_position, "buffer-position");
30386 DEFSYM (Qobject, "object");
30387 DEFSYM (Qbar, "bar");
30388 DEFSYM (Qhbar, "hbar");
30389 DEFSYM (Qbox, "box");
30390 DEFSYM (Qhollow, "hollow");
30391 DEFSYM (Qhand, "hand");
30392 DEFSYM (Qarrow, "arrow");
30393 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30395 list_of_error = list1 (list2 (intern_c_string ("error"),
30396 intern_c_string ("void-variable")));
30397 staticpro (&list_of_error);
30399 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30400 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30401 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30402 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30404 echo_buffer[0] = echo_buffer[1] = Qnil;
30405 staticpro (&echo_buffer[0]);
30406 staticpro (&echo_buffer[1]);
30408 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30409 staticpro (&echo_area_buffer[0]);
30410 staticpro (&echo_area_buffer[1]);
30412 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30413 staticpro (&Vmessages_buffer_name);
30415 mode_line_proptrans_alist = Qnil;
30416 staticpro (&mode_line_proptrans_alist);
30417 mode_line_string_list = Qnil;
30418 staticpro (&mode_line_string_list);
30419 mode_line_string_face = Qnil;
30420 staticpro (&mode_line_string_face);
30421 mode_line_string_face_prop = Qnil;
30422 staticpro (&mode_line_string_face_prop);
30423 Vmode_line_unwind_vector = Qnil;
30424 staticpro (&Vmode_line_unwind_vector);
30426 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30428 help_echo_string = Qnil;
30429 staticpro (&help_echo_string);
30430 help_echo_object = Qnil;
30431 staticpro (&help_echo_object);
30432 help_echo_window = Qnil;
30433 staticpro (&help_echo_window);
30434 previous_help_echo_string = Qnil;
30435 staticpro (&previous_help_echo_string);
30436 help_echo_pos = -1;
30438 DEFSYM (Qright_to_left, "right-to-left");
30439 DEFSYM (Qleft_to_right, "left-to-right");
30441 #ifdef HAVE_WINDOW_SYSTEM
30442 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30443 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30444 For example, if a block cursor is over a tab, it will be drawn as
30445 wide as that tab on the display. */);
30446 x_stretch_cursor_p = 0;
30447 #endif
30449 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30450 doc: /* Non-nil means highlight trailing whitespace.
30451 The face used for trailing whitespace is `trailing-whitespace'. */);
30452 Vshow_trailing_whitespace = Qnil;
30454 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30455 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30456 If the value is t, Emacs highlights non-ASCII chars which have the
30457 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30458 or `escape-glyph' face respectively.
30460 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30461 U+2011 (non-breaking hyphen) are affected.
30463 Any other non-nil value means to display these characters as a escape
30464 glyph followed by an ordinary space or hyphen.
30466 A value of nil means no special handling of these characters. */);
30467 Vnobreak_char_display = Qt;
30469 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30470 doc: /* The pointer shape to show in void text areas.
30471 A value of nil means to show the text pointer. Other options are
30472 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30473 `hourglass'. */);
30474 Vvoid_text_area_pointer = Qarrow;
30476 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30477 doc: /* Non-nil means don't actually do any redisplay.
30478 This is used for internal purposes. */);
30479 Vinhibit_redisplay = Qnil;
30481 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30482 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30483 Vglobal_mode_string = Qnil;
30485 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30486 doc: /* Marker for where to display an arrow on top of the buffer text.
30487 This must be the beginning of a line in order to work.
30488 See also `overlay-arrow-string'. */);
30489 Voverlay_arrow_position = Qnil;
30491 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30492 doc: /* String to display as an arrow in non-window frames.
30493 See also `overlay-arrow-position'. */);
30494 Voverlay_arrow_string = build_pure_c_string ("=>");
30496 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
30497 doc: /* List of variables (symbols) which hold markers for overlay arrows.
30498 The symbols on this list are examined during redisplay to determine
30499 where to display overlay arrows. */);
30500 Voverlay_arrow_variable_list
30501 = list1 (intern_c_string ("overlay-arrow-position"));
30503 DEFVAR_INT ("scroll-step", emacs_scroll_step,
30504 doc: /* The number of lines to try scrolling a window by when point moves out.
30505 If that fails to bring point back on frame, point is centered instead.
30506 If this is zero, point is always centered after it moves off frame.
30507 If you want scrolling to always be a line at a time, you should set
30508 `scroll-conservatively' to a large value rather than set this to 1. */);
30510 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
30511 doc: /* Scroll up to this many lines, to bring point back on screen.
30512 If point moves off-screen, redisplay will scroll by up to
30513 `scroll-conservatively' lines in order to bring point just barely
30514 onto the screen again. If that cannot be done, then redisplay
30515 recenters point as usual.
30517 If the value is greater than 100, redisplay will never recenter point,
30518 but will always scroll just enough text to bring point into view, even
30519 if you move far away.
30521 A value of zero means always recenter point if it moves off screen. */);
30522 scroll_conservatively = 0;
30524 DEFVAR_INT ("scroll-margin", scroll_margin,
30525 doc: /* Number of lines of margin at the top and bottom of a window.
30526 Recenter the window whenever point gets within this many lines
30527 of the top or bottom of the window. */);
30528 scroll_margin = 0;
30530 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
30531 doc: /* Pixels per inch value for non-window system displays.
30532 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
30533 Vdisplay_pixels_per_inch = make_float (72.0);
30535 #ifdef GLYPH_DEBUG
30536 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
30537 #endif
30539 DEFVAR_LISP ("truncate-partial-width-windows",
30540 Vtruncate_partial_width_windows,
30541 doc: /* Non-nil means truncate lines in windows narrower than the frame.
30542 For an integer value, truncate lines in each window narrower than the
30543 full frame width, provided the window width is less than that integer;
30544 otherwise, respect the value of `truncate-lines'.
30546 For any other non-nil value, truncate lines in all windows that do
30547 not span the full frame width.
30549 A value of nil means to respect the value of `truncate-lines'.
30551 If `word-wrap' is enabled, you might want to reduce this. */);
30552 Vtruncate_partial_width_windows = make_number (50);
30554 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
30555 doc: /* Maximum buffer size for which line number should be displayed.
30556 If the buffer is bigger than this, the line number does not appear
30557 in the mode line. A value of nil means no limit. */);
30558 Vline_number_display_limit = Qnil;
30560 DEFVAR_INT ("line-number-display-limit-width",
30561 line_number_display_limit_width,
30562 doc: /* Maximum line width (in characters) for line number display.
30563 If the average length of the lines near point is bigger than this, then the
30564 line number may be omitted from the mode line. */);
30565 line_number_display_limit_width = 200;
30567 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
30568 doc: /* Non-nil means highlight region even in nonselected windows. */);
30569 highlight_nonselected_windows = 0;
30571 DEFVAR_BOOL ("multiple-frames", multiple_frames,
30572 doc: /* Non-nil if more than one frame is visible on this display.
30573 Minibuffer-only frames don't count, but iconified frames do.
30574 This variable is not guaranteed to be accurate except while processing
30575 `frame-title-format' and `icon-title-format'. */);
30577 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
30578 doc: /* Template for displaying the title bar of visible frames.
30579 \(Assuming the window manager supports this feature.)
30581 This variable has the same structure as `mode-line-format', except that
30582 the %c and %l constructs are ignored. It is used only on frames for
30583 which no explicit name has been set \(see `modify-frame-parameters'). */);
30585 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
30586 doc: /* Template for displaying the title bar of an iconified frame.
30587 \(Assuming the window manager supports this feature.)
30588 This variable has the same structure as `mode-line-format' (which see),
30589 and is used only on frames for which no explicit name has been set
30590 \(see `modify-frame-parameters'). */);
30591 Vicon_title_format
30592 = Vframe_title_format
30593 = listn (CONSTYPE_PURE, 3,
30594 intern_c_string ("multiple-frames"),
30595 build_pure_c_string ("%b"),
30596 listn (CONSTYPE_PURE, 4,
30597 empty_unibyte_string,
30598 intern_c_string ("invocation-name"),
30599 build_pure_c_string ("@"),
30600 intern_c_string ("system-name")));
30602 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
30603 doc: /* Maximum number of lines to keep in the message log buffer.
30604 If nil, disable message logging. If t, log messages but don't truncate
30605 the buffer when it becomes large. */);
30606 Vmessage_log_max = make_number (1000);
30608 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
30609 doc: /* Functions called before redisplay, if window sizes have changed.
30610 The value should be a list of functions that take one argument.
30611 Just before redisplay, for each frame, if any of its windows have changed
30612 size since the last redisplay, or have been split or deleted,
30613 all the functions in the list are called, with the frame as argument. */);
30614 Vwindow_size_change_functions = Qnil;
30616 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
30617 doc: /* List of functions to call before redisplaying a window with scrolling.
30618 Each function is called with two arguments, the window and its new
30619 display-start position. Note that these functions are also called by
30620 `set-window-buffer'. Also note that the value of `window-end' is not
30621 valid when these functions are called.
30623 Warning: Do not use this feature to alter the way the window
30624 is scrolled. It is not designed for that, and such use probably won't
30625 work. */);
30626 Vwindow_scroll_functions = Qnil;
30628 DEFVAR_LISP ("window-text-change-functions",
30629 Vwindow_text_change_functions,
30630 doc: /* Functions to call in redisplay when text in the window might change. */);
30631 Vwindow_text_change_functions = Qnil;
30633 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
30634 doc: /* Functions called when redisplay of a window reaches the end trigger.
30635 Each function is called with two arguments, the window and the end trigger value.
30636 See `set-window-redisplay-end-trigger'. */);
30637 Vredisplay_end_trigger_functions = Qnil;
30639 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
30640 doc: /* Non-nil means autoselect window with mouse pointer.
30641 If nil, do not autoselect windows.
30642 A positive number means delay autoselection by that many seconds: a
30643 window is autoselected only after the mouse has remained in that
30644 window for the duration of the delay.
30645 A negative number has a similar effect, but causes windows to be
30646 autoselected only after the mouse has stopped moving. \(Because of
30647 the way Emacs compares mouse events, you will occasionally wait twice
30648 that time before the window gets selected.\)
30649 Any other value means to autoselect window instantaneously when the
30650 mouse pointer enters it.
30652 Autoselection selects the minibuffer only if it is active, and never
30653 unselects the minibuffer if it is active.
30655 When customizing this variable make sure that the actual value of
30656 `focus-follows-mouse' matches the behavior of your window manager. */);
30657 Vmouse_autoselect_window = Qnil;
30659 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
30660 doc: /* Non-nil means automatically resize tool-bars.
30661 This dynamically changes the tool-bar's height to the minimum height
30662 that is needed to make all tool-bar items visible.
30663 If value is `grow-only', the tool-bar's height is only increased
30664 automatically; to decrease the tool-bar height, use \\[recenter]. */);
30665 Vauto_resize_tool_bars = Qt;
30667 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
30668 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
30669 auto_raise_tool_bar_buttons_p = 1;
30671 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
30672 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
30673 make_cursor_line_fully_visible_p = 1;
30675 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
30676 doc: /* Border below tool-bar in pixels.
30677 If an integer, use it as the height of the border.
30678 If it is one of `internal-border-width' or `border-width', use the
30679 value of the corresponding frame parameter.
30680 Otherwise, no border is added below the tool-bar. */);
30681 Vtool_bar_border = Qinternal_border_width;
30683 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
30684 doc: /* Margin around tool-bar buttons in pixels.
30685 If an integer, use that for both horizontal and vertical margins.
30686 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
30687 HORZ specifying the horizontal margin, and VERT specifying the
30688 vertical margin. */);
30689 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
30691 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
30692 doc: /* Relief thickness of tool-bar buttons. */);
30693 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
30695 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
30696 doc: /* Tool bar style to use.
30697 It can be one of
30698 image - show images only
30699 text - show text only
30700 both - show both, text below image
30701 both-horiz - show text to the right of the image
30702 text-image-horiz - show text to the left of the image
30703 any other - use system default or image if no system default.
30705 This variable only affects the GTK+ toolkit version of Emacs. */);
30706 Vtool_bar_style = Qnil;
30708 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
30709 doc: /* Maximum number of characters a label can have to be shown.
30710 The tool bar style must also show labels for this to have any effect, see
30711 `tool-bar-style'. */);
30712 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
30714 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
30715 doc: /* List of functions to call to fontify regions of text.
30716 Each function is called with one argument POS. Functions must
30717 fontify a region starting at POS in the current buffer, and give
30718 fontified regions the property `fontified'. */);
30719 Vfontification_functions = Qnil;
30720 Fmake_variable_buffer_local (Qfontification_functions);
30722 DEFVAR_BOOL ("unibyte-display-via-language-environment",
30723 unibyte_display_via_language_environment,
30724 doc: /* Non-nil means display unibyte text according to language environment.
30725 Specifically, this means that raw bytes in the range 160-255 decimal
30726 are displayed by converting them to the equivalent multibyte characters
30727 according to the current language environment. As a result, they are
30728 displayed according to the current fontset.
30730 Note that this variable affects only how these bytes are displayed,
30731 but does not change the fact they are interpreted as raw bytes. */);
30732 unibyte_display_via_language_environment = 0;
30734 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
30735 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
30736 If a float, it specifies a fraction of the mini-window frame's height.
30737 If an integer, it specifies a number of lines. */);
30738 Vmax_mini_window_height = make_float (0.25);
30740 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
30741 doc: /* How to resize mini-windows (the minibuffer and the echo area).
30742 A value of nil means don't automatically resize mini-windows.
30743 A value of t means resize them to fit the text displayed in them.
30744 A value of `grow-only', the default, means let mini-windows grow only;
30745 they return to their normal size when the minibuffer is closed, or the
30746 echo area becomes empty. */);
30747 Vresize_mini_windows = Qgrow_only;
30749 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
30750 doc: /* Alist specifying how to blink the cursor off.
30751 Each element has the form (ON-STATE . OFF-STATE). Whenever the
30752 `cursor-type' frame-parameter or variable equals ON-STATE,
30753 comparing using `equal', Emacs uses OFF-STATE to specify
30754 how to blink it off. ON-STATE and OFF-STATE are values for
30755 the `cursor-type' frame parameter.
30757 If a frame's ON-STATE has no entry in this list,
30758 the frame's other specifications determine how to blink the cursor off. */);
30759 Vblink_cursor_alist = Qnil;
30761 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
30762 doc: /* Allow or disallow automatic horizontal scrolling of windows.
30763 If non-nil, windows are automatically scrolled horizontally to make
30764 point visible. */);
30765 automatic_hscrolling_p = 1;
30766 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
30768 DEFVAR_INT ("hscroll-margin", hscroll_margin,
30769 doc: /* How many columns away from the window edge point is allowed to get
30770 before automatic hscrolling will horizontally scroll the window. */);
30771 hscroll_margin = 5;
30773 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
30774 doc: /* How many columns to scroll the window when point gets too close to the edge.
30775 When point is less than `hscroll-margin' columns from the window
30776 edge, automatic hscrolling will scroll the window by the amount of columns
30777 determined by this variable. If its value is a positive integer, scroll that
30778 many columns. If it's a positive floating-point number, it specifies the
30779 fraction of the window's width to scroll. If it's nil or zero, point will be
30780 centered horizontally after the scroll. Any other value, including negative
30781 numbers, are treated as if the value were zero.
30783 Automatic hscrolling always moves point outside the scroll margin, so if
30784 point was more than scroll step columns inside the margin, the window will
30785 scroll more than the value given by the scroll step.
30787 Note that the lower bound for automatic hscrolling specified by `scroll-left'
30788 and `scroll-right' overrides this variable's effect. */);
30789 Vhscroll_step = make_number (0);
30791 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
30792 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
30793 Bind this around calls to `message' to let it take effect. */);
30794 message_truncate_lines = 0;
30796 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
30797 doc: /* Normal hook run to update the menu bar definitions.
30798 Redisplay runs this hook before it redisplays the menu bar.
30799 This is used to update menus such as Buffers, whose contents depend on
30800 various data. */);
30801 Vmenu_bar_update_hook = Qnil;
30803 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
30804 doc: /* Frame for which we are updating a menu.
30805 The enable predicate for a menu binding should check this variable. */);
30806 Vmenu_updating_frame = Qnil;
30808 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
30809 doc: /* Non-nil means don't update menu bars. Internal use only. */);
30810 inhibit_menubar_update = 0;
30812 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
30813 doc: /* Prefix prepended to all continuation lines at display time.
30814 The value may be a string, an image, or a stretch-glyph; it is
30815 interpreted in the same way as the value of a `display' text property.
30817 This variable is overridden by any `wrap-prefix' text or overlay
30818 property.
30820 To add a prefix to non-continuation lines, use `line-prefix'. */);
30821 Vwrap_prefix = Qnil;
30822 DEFSYM (Qwrap_prefix, "wrap-prefix");
30823 Fmake_variable_buffer_local (Qwrap_prefix);
30825 DEFVAR_LISP ("line-prefix", Vline_prefix,
30826 doc: /* Prefix prepended to all non-continuation lines at display time.
30827 The value may be a string, an image, or a stretch-glyph; it is
30828 interpreted in the same way as the value of a `display' text property.
30830 This variable is overridden by any `line-prefix' text or overlay
30831 property.
30833 To add a prefix to continuation lines, use `wrap-prefix'. */);
30834 Vline_prefix = Qnil;
30835 DEFSYM (Qline_prefix, "line-prefix");
30836 Fmake_variable_buffer_local (Qline_prefix);
30838 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
30839 doc: /* Non-nil means don't eval Lisp during redisplay. */);
30840 inhibit_eval_during_redisplay = 0;
30842 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
30843 doc: /* Non-nil means don't free realized faces. Internal use only. */);
30844 inhibit_free_realized_faces = 0;
30846 #ifdef GLYPH_DEBUG
30847 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
30848 doc: /* Inhibit try_window_id display optimization. */);
30849 inhibit_try_window_id = 0;
30851 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
30852 doc: /* Inhibit try_window_reusing display optimization. */);
30853 inhibit_try_window_reusing = 0;
30855 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
30856 doc: /* Inhibit try_cursor_movement display optimization. */);
30857 inhibit_try_cursor_movement = 0;
30858 #endif /* GLYPH_DEBUG */
30860 DEFVAR_INT ("overline-margin", overline_margin,
30861 doc: /* Space between overline and text, in pixels.
30862 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
30863 margin to the character height. */);
30864 overline_margin = 2;
30866 DEFVAR_INT ("underline-minimum-offset",
30867 underline_minimum_offset,
30868 doc: /* Minimum distance between baseline and underline.
30869 This can improve legibility of underlined text at small font sizes,
30870 particularly when using variable `x-use-underline-position-properties'
30871 with fonts that specify an UNDERLINE_POSITION relatively close to the
30872 baseline. The default value is 1. */);
30873 underline_minimum_offset = 1;
30875 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
30876 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
30877 This feature only works when on a window system that can change
30878 cursor shapes. */);
30879 display_hourglass_p = 1;
30881 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
30882 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
30883 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
30885 #ifdef HAVE_WINDOW_SYSTEM
30886 hourglass_atimer = NULL;
30887 hourglass_shown_p = 0;
30888 #endif /* HAVE_WINDOW_SYSTEM */
30890 DEFSYM (Qglyphless_char, "glyphless-char");
30891 DEFSYM (Qhex_code, "hex-code");
30892 DEFSYM (Qempty_box, "empty-box");
30893 DEFSYM (Qthin_space, "thin-space");
30894 DEFSYM (Qzero_width, "zero-width");
30896 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
30897 doc: /* Function run just before redisplay.
30898 It is called with one argument, which is the set of windows that are to
30899 be redisplayed. This set can be nil (meaning, only the selected window),
30900 or t (meaning all windows). */);
30901 Vpre_redisplay_function = intern ("ignore");
30903 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
30904 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
30906 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
30907 doc: /* Char-table defining glyphless characters.
30908 Each element, if non-nil, should be one of the following:
30909 an ASCII acronym string: display this string in a box
30910 `hex-code': display the hexadecimal code of a character in a box
30911 `empty-box': display as an empty box
30912 `thin-space': display as 1-pixel width space
30913 `zero-width': don't display
30914 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
30915 display method for graphical terminals and text terminals respectively.
30916 GRAPHICAL and TEXT should each have one of the values listed above.
30918 The char-table has one extra slot to control the display of a character for
30919 which no font is found. This slot only takes effect on graphical terminals.
30920 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
30921 `thin-space'. The default is `empty-box'.
30923 If a character has a non-nil entry in an active display table, the
30924 display table takes effect; in this case, Emacs does not consult
30925 `glyphless-char-display' at all. */);
30926 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
30927 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
30928 Qempty_box);
30930 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
30931 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
30932 Vdebug_on_message = Qnil;
30934 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
30935 doc: /* */);
30936 Vredisplay__all_windows_cause
30937 = Fmake_vector (make_number (100), make_number (0));
30939 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
30940 doc: /* */);
30941 Vredisplay__mode_lines_cause
30942 = Fmake_vector (make_number (100), make_number (0));
30946 /* Initialize this module when Emacs starts. */
30948 void
30949 init_xdisp (void)
30951 CHARPOS (this_line_start_pos) = 0;
30953 if (!noninteractive)
30955 struct window *m = XWINDOW (minibuf_window);
30956 Lisp_Object frame = m->frame;
30957 struct frame *f = XFRAME (frame);
30958 Lisp_Object root = FRAME_ROOT_WINDOW (f);
30959 struct window *r = XWINDOW (root);
30960 int i;
30962 echo_area_window = minibuf_window;
30964 r->top_line = FRAME_TOP_MARGIN (f);
30965 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
30966 r->total_cols = FRAME_COLS (f);
30967 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
30968 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
30969 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
30971 m->top_line = FRAME_TOTAL_LINES (f) - 1;
30972 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
30973 m->total_cols = FRAME_COLS (f);
30974 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
30975 m->total_lines = 1;
30976 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
30978 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
30979 scratch_glyph_row.glyphs[TEXT_AREA + 1]
30980 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
30982 /* The default ellipsis glyphs `...'. */
30983 for (i = 0; i < 3; ++i)
30984 default_invis_vector[i] = make_number ('.');
30988 /* Allocate the buffer for frame titles.
30989 Also used for `format-mode-line'. */
30990 int size = 100;
30991 mode_line_noprop_buf = xmalloc (size);
30992 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
30993 mode_line_noprop_ptr = mode_line_noprop_buf;
30994 mode_line_target = MODE_LINE_DISPLAY;
30997 help_echo_showing_p = 0;
31000 #ifdef HAVE_WINDOW_SYSTEM
31002 /* Platform-independent portion of hourglass implementation. */
31004 /* Timer function of hourglass_atimer. */
31006 static void
31007 show_hourglass (struct atimer *timer)
31009 /* The timer implementation will cancel this timer automatically
31010 after this function has run. Set hourglass_atimer to null
31011 so that we know the timer doesn't have to be canceled. */
31012 hourglass_atimer = NULL;
31014 if (!hourglass_shown_p)
31016 Lisp_Object tail, frame;
31018 block_input ();
31020 FOR_EACH_FRAME (tail, frame)
31022 struct frame *f = XFRAME (frame);
31024 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31025 && FRAME_RIF (f)->show_hourglass)
31026 FRAME_RIF (f)->show_hourglass (f);
31029 hourglass_shown_p = 1;
31030 unblock_input ();
31034 /* Cancel a currently active hourglass timer, and start a new one. */
31036 void
31037 start_hourglass (void)
31039 struct timespec delay;
31041 cancel_hourglass ();
31043 if (INTEGERP (Vhourglass_delay)
31044 && XINT (Vhourglass_delay) > 0)
31045 delay = make_timespec (min (XINT (Vhourglass_delay),
31046 TYPE_MAXIMUM (time_t)),
31048 else if (FLOATP (Vhourglass_delay)
31049 && XFLOAT_DATA (Vhourglass_delay) > 0)
31050 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31051 else
31052 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31054 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31055 show_hourglass, NULL);
31058 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31059 shown. */
31061 void
31062 cancel_hourglass (void)
31064 if (hourglass_atimer)
31066 cancel_atimer (hourglass_atimer);
31067 hourglass_atimer = NULL;
31070 if (hourglass_shown_p)
31072 Lisp_Object tail, frame;
31074 block_input ();
31076 FOR_EACH_FRAME (tail, frame)
31078 struct frame *f = XFRAME (frame);
31080 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31081 && FRAME_RIF (f)->hide_hourglass)
31082 FRAME_RIF (f)->hide_hourglass (f);
31083 #ifdef HAVE_NTGUI
31084 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31085 else if (!FRAME_W32_P (f))
31086 w32_arrow_cursor ();
31087 #endif
31090 hourglass_shown_p = 0;
31091 unblock_input ();
31095 #endif /* HAVE_WINDOW_SYSTEM */