merge upstream
[emacs.git] / src / xdisp.c
blobc87862d1b6d092cf17db80514cae3451d512c014
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 #include "font.h"
322 #ifdef HAVE_XWIDGETS
323 #include "xwidget.h"
324 #endif
325 #ifndef FRAME_X_OUTPUT
326 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
327 #endif
329 #define INFINITY 10000000
331 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
332 Lisp_Object Qwindow_scroll_functions;
333 static Lisp_Object Qwindow_text_change_functions;
334 static Lisp_Object Qredisplay_end_trigger_functions;
335 Lisp_Object Qinhibit_point_motion_hooks;
336 static Lisp_Object QCeval, QCpropertize;
337 Lisp_Object QCfile, QCdata;
338 static Lisp_Object Qfontified;
339 static Lisp_Object Qgrow_only;
340 static Lisp_Object Qinhibit_eval_during_redisplay;
341 static Lisp_Object Qbuffer_position, Qposition, Qobject;
342 static Lisp_Object Qright_to_left, Qleft_to_right;
344 /* Cursor shapes. */
345 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
347 /* Pointer shapes. */
348 static Lisp_Object Qarrow, Qhand;
349 Lisp_Object Qtext;
351 /* Holds the list (error). */
352 static Lisp_Object list_of_error;
354 Lisp_Object Qfontification_functions;
356 static Lisp_Object Qwrap_prefix;
357 static Lisp_Object Qline_prefix;
358 static Lisp_Object Qredisplay_internal;
360 /* Non-nil means don't actually do any redisplay. */
362 Lisp_Object Qinhibit_redisplay;
364 /* Names of text properties relevant for redisplay. */
366 Lisp_Object Qdisplay;
368 Lisp_Object Qspace, QCalign_to;
369 static Lisp_Object QCrelative_width, QCrelative_height;
370 Lisp_Object Qleft_margin, Qright_margin;
371 static Lisp_Object Qspace_width, Qraise;
372 static Lisp_Object Qslice;
373 Lisp_Object Qcenter;
374 static Lisp_Object Qmargin, Qpointer;
375 static Lisp_Object Qline_height;
377 #ifdef HAVE_WINDOW_SYSTEM
379 /* Test if overflow newline into fringe. Called with iterator IT
380 at or past right window margin, and with IT->current_x set. */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
383 (!NILP (Voverflow_newline_into_fringe) \
384 && FRAME_WINDOW_P ((IT)->f) \
385 && ((IT)->bidi_it.paragraph_dir == R2L \
386 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
387 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
388 && (IT)->current_x == (IT)->last_visible_x)
390 #else /* !HAVE_WINDOW_SYSTEM */
391 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
392 #endif /* HAVE_WINDOW_SYSTEM */
394 /* Test if the display element loaded in IT, or the underlying buffer
395 or string character, is a space or a TAB character. This is used
396 to determine where word wrapping can occur. */
398 #define IT_DISPLAYING_WHITESPACE(it) \
399 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
400 || ((STRINGP (it->string) \
401 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
402 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
403 || (it->s \
404 && (it->s[IT_BYTEPOS (*it)] == ' ' \
405 || it->s[IT_BYTEPOS (*it)] == '\t')) \
406 || (IT_BYTEPOS (*it) < ZV_BYTE \
407 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
408 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
410 /* Name of the face used to highlight trailing whitespace. */
412 static Lisp_Object Qtrailing_whitespace;
414 /* Name and number of the face used to highlight escape glyphs. */
416 static Lisp_Object Qescape_glyph;
418 /* Name and number of the face used to highlight non-breaking spaces. */
420 static Lisp_Object Qnobreak_space;
422 /* The symbol `image' which is the car of the lists used to represent
423 images in Lisp. Also a tool bar style. */
425 Lisp_Object Qimage;
427 /* The image map types. */
428 Lisp_Object QCmap;
429 static Lisp_Object QCpointer;
430 static Lisp_Object Qrect, Qcircle, Qpoly;
432 /* Tool bar styles */
433 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
435 /* Non-zero means print newline to stdout before next mini-buffer
436 message. */
438 bool noninteractive_need_newline;
440 /* Non-zero means print newline to message log before next message. */
442 static bool message_log_need_newline;
444 /* Three markers that message_dolog uses.
445 It could allocate them itself, but that causes trouble
446 in handling memory-full errors. */
447 static Lisp_Object message_dolog_marker1;
448 static Lisp_Object message_dolog_marker2;
449 static Lisp_Object message_dolog_marker3;
451 /* The buffer position of the first character appearing entirely or
452 partially on the line of the selected window which contains the
453 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
454 redisplay optimization in redisplay_internal. */
456 static struct text_pos this_line_start_pos;
458 /* Number of characters past the end of the line above, including the
459 terminating newline. */
461 static struct text_pos this_line_end_pos;
463 /* The vertical positions and the height of this line. */
465 static int this_line_vpos;
466 static int this_line_y;
467 static int this_line_pixel_height;
469 /* X position at which this display line starts. Usually zero;
470 negative if first character is partially visible. */
472 static int this_line_start_x;
474 /* The smallest character position seen by move_it_* functions as they
475 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
476 hscrolled lines, see display_line. */
478 static struct text_pos this_line_min_pos;
480 /* Buffer that this_line_.* variables are referring to. */
482 static struct buffer *this_line_buffer;
485 /* Values of those variables at last redisplay are stored as
486 properties on `overlay-arrow-position' symbol. However, if
487 Voverlay_arrow_position is a marker, last-arrow-position is its
488 numerical position. */
490 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
492 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
493 properties on a symbol in overlay-arrow-variable-list. */
495 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
497 Lisp_Object Qmenu_bar_update_hook;
499 /* Nonzero if an overlay arrow has been displayed in this window. */
501 static bool overlay_arrow_seen;
503 /* Vector containing glyphs for an ellipsis `...'. */
505 static Lisp_Object default_invis_vector[3];
507 /* This is the window where the echo area message was displayed. It
508 is always a mini-buffer window, but it may not be the same window
509 currently active as a mini-buffer. */
511 Lisp_Object echo_area_window;
513 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
514 pushes the current message and the value of
515 message_enable_multibyte on the stack, the function restore_message
516 pops the stack and displays MESSAGE again. */
518 static Lisp_Object Vmessage_stack;
520 /* Nonzero means multibyte characters were enabled when the echo area
521 message was specified. */
523 static bool message_enable_multibyte;
525 /* Nonzero if we should redraw the mode lines on the next redisplay.
526 If it has value REDISPLAY_SOME, then only redisplay the mode lines where
527 the `redisplay' bit has been set. Otherwise, redisplay all mode lines
528 (the number used is then only used to track down the cause for this
529 full-redisplay). */
531 int update_mode_lines;
533 /* Nonzero if window sizes or contents other than selected-window have changed
534 since last redisplay that finished.
535 If it has value REDISPLAY_SOME, then only redisplay the windows where
536 the `redisplay' bit has been set. Otherwise, redisplay all windows
537 (the number used is then only used to track down the cause for this
538 full-redisplay). */
540 int windows_or_buffers_changed;
542 /* Nonzero after display_mode_line if %l was used and it displayed a
543 line number. */
545 static bool line_number_displayed;
547 /* The name of the *Messages* buffer, a string. */
549 static Lisp_Object Vmessages_buffer_name;
551 /* Current, index 0, and last displayed echo area message. Either
552 buffers from echo_buffers, or nil to indicate no message. */
554 Lisp_Object echo_area_buffer[2];
556 /* The buffers referenced from echo_area_buffer. */
558 static Lisp_Object echo_buffer[2];
560 /* A vector saved used in with_area_buffer to reduce consing. */
562 static Lisp_Object Vwith_echo_area_save_vector;
564 /* Non-zero means display_echo_area should display the last echo area
565 message again. Set by redisplay_preserve_echo_area. */
567 static bool display_last_displayed_message_p;
569 /* Nonzero if echo area is being used by print; zero if being used by
570 message. */
572 static bool message_buf_print;
574 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
576 static Lisp_Object Qinhibit_menubar_update;
577 static Lisp_Object Qmessage_truncate_lines;
579 /* Set to 1 in clear_message to make redisplay_internal aware
580 of an emptied echo area. */
582 static bool message_cleared_p;
584 /* A scratch glyph row with contents used for generating truncation
585 glyphs. Also used in direct_output_for_insert. */
587 #define MAX_SCRATCH_GLYPHS 100
588 static struct glyph_row scratch_glyph_row;
589 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
591 /* Ascent and height of the last line processed by move_it_to. */
593 static int last_height;
595 /* Non-zero if there's a help-echo in the echo area. */
597 bool help_echo_showing_p;
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
605 #define TEXT_PROP_DISTANCE_LIMIT 100
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
630 /* Functions to mark elements as needing redisplay. */
631 enum { REDISPLAY_SOME = 2}; /* Arbitrary choice. */
633 void
634 redisplay_other_windows (void)
636 if (!windows_or_buffers_changed)
637 windows_or_buffers_changed = REDISPLAY_SOME;
640 void
641 wset_redisplay (struct window *w)
643 /* Beware: selected_window can be nil during early stages. */
644 if (!EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
645 redisplay_other_windows ();
646 w->redisplay = true;
649 void
650 fset_redisplay (struct frame *f)
652 redisplay_other_windows ();
653 f->redisplay = true;
656 void
657 bset_redisplay (struct buffer *b)
659 int count = buffer_window_count (b);
660 if (count > 0)
662 /* ... it's visible in other window than selected, */
663 if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
664 redisplay_other_windows ();
665 /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
666 so that if we later set windows_or_buffers_changed, this buffer will
667 not be omitted. */
668 b->text->redisplay = true;
672 void
673 bset_update_mode_line (struct buffer *b)
675 if (!update_mode_lines)
676 update_mode_lines = REDISPLAY_SOME;
677 b->text->redisplay = true;
680 #ifdef GLYPH_DEBUG
682 /* Non-zero means print traces of redisplay if compiled with
683 GLYPH_DEBUG defined. */
685 bool trace_redisplay_p;
687 #endif /* GLYPH_DEBUG */
689 #ifdef DEBUG_TRACE_MOVE
690 /* Non-zero means trace with TRACE_MOVE to stderr. */
691 int trace_move;
693 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
694 #else
695 #define TRACE_MOVE(x) (void) 0
696 #endif
698 static Lisp_Object Qauto_hscroll_mode;
700 /* Buffer being redisplayed -- for redisplay_window_error. */
702 static struct buffer *displayed_buffer;
704 /* Value returned from text property handlers (see below). */
706 enum prop_handled
708 HANDLED_NORMALLY,
709 HANDLED_RECOMPUTE_PROPS,
710 HANDLED_OVERLAY_STRING_CONSUMED,
711 HANDLED_RETURN
714 /* A description of text properties that redisplay is interested
715 in. */
717 struct props
719 /* The name of the property. */
720 Lisp_Object *name;
722 /* A unique index for the property. */
723 enum prop_idx idx;
725 /* A handler function called to set up iterator IT from the property
726 at IT's current position. Value is used to steer handle_stop. */
727 enum prop_handled (*handler) (struct it *it);
730 static enum prop_handled handle_face_prop (struct it *);
731 static enum prop_handled handle_invisible_prop (struct it *);
732 static enum prop_handled handle_display_prop (struct it *);
733 static enum prop_handled handle_composition_prop (struct it *);
734 static enum prop_handled handle_overlay_change (struct it *);
735 static enum prop_handled handle_fontified_prop (struct it *);
737 /* Properties handled by iterators. */
739 static struct props it_props[] =
741 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
742 /* Handle `face' before `display' because some sub-properties of
743 `display' need to know the face. */
744 {&Qface, FACE_PROP_IDX, handle_face_prop},
745 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
746 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
747 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
748 {NULL, 0, NULL}
751 /* Value is the position described by X. If X is a marker, value is
752 the marker_position of X. Otherwise, value is X. */
754 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
756 /* Enumeration returned by some move_it_.* functions internally. */
758 enum move_it_result
760 /* Not used. Undefined value. */
761 MOVE_UNDEFINED,
763 /* Move ended at the requested buffer position or ZV. */
764 MOVE_POS_MATCH_OR_ZV,
766 /* Move ended at the requested X pixel position. */
767 MOVE_X_REACHED,
769 /* Move within a line ended at the end of a line that must be
770 continued. */
771 MOVE_LINE_CONTINUED,
773 /* Move within a line ended at the end of a line that would
774 be displayed truncated. */
775 MOVE_LINE_TRUNCATED,
777 /* Move within a line ended at a line end. */
778 MOVE_NEWLINE_OR_CR
781 /* This counter is used to clear the face cache every once in a while
782 in redisplay_internal. It is incremented for each redisplay.
783 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
784 cleared. */
786 #define CLEAR_FACE_CACHE_COUNT 500
787 static int clear_face_cache_count;
789 /* Similarly for the image cache. */
791 #ifdef HAVE_WINDOW_SYSTEM
792 #define CLEAR_IMAGE_CACHE_COUNT 101
793 static int clear_image_cache_count;
795 /* Null glyph slice */
796 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
797 #endif
799 /* True while redisplay_internal is in progress. */
801 bool redisplaying_p;
803 static Lisp_Object Qinhibit_free_realized_faces;
804 static Lisp_Object Qmode_line_default_help_echo;
806 /* If a string, XTread_socket generates an event to display that string.
807 (The display is done in read_char.) */
809 Lisp_Object help_echo_string;
810 Lisp_Object help_echo_window;
811 Lisp_Object help_echo_object;
812 ptrdiff_t help_echo_pos;
814 /* Temporary variable for XTread_socket. */
816 Lisp_Object previous_help_echo_string;
818 /* Platform-independent portion of hourglass implementation. */
820 #ifdef HAVE_WINDOW_SYSTEM
822 /* Non-zero means an hourglass cursor is currently shown. */
823 static bool hourglass_shown_p;
825 /* If non-null, an asynchronous timer that, when it expires, displays
826 an hourglass cursor on all frames. */
827 static struct atimer *hourglass_atimer;
829 #endif /* HAVE_WINDOW_SYSTEM */
831 /* Name of the face used to display glyphless characters. */
832 static Lisp_Object Qglyphless_char;
834 /* Symbol for the purpose of Vglyphless_char_display. */
835 static Lisp_Object Qglyphless_char_display;
837 /* Method symbols for Vglyphless_char_display. */
838 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
840 /* Default number of seconds to wait before displaying an hourglass
841 cursor. */
842 #define DEFAULT_HOURGLASS_DELAY 1
844 #ifdef HAVE_WINDOW_SYSTEM
846 /* Default pixel width of `thin-space' display method. */
847 #define THIN_SPACE_WIDTH 1
849 #endif /* HAVE_WINDOW_SYSTEM */
851 /* Function prototypes. */
853 static void setup_for_ellipsis (struct it *, int);
854 static void set_iterator_to_next (struct it *, int);
855 static void mark_window_display_accurate_1 (struct window *, int);
856 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
857 static int display_prop_string_p (Lisp_Object, Lisp_Object);
858 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
859 static int cursor_row_p (struct glyph_row *);
860 static int redisplay_mode_lines (Lisp_Object, bool);
861 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
863 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
865 static void handle_line_prefix (struct it *);
867 static void pint2str (char *, int, ptrdiff_t);
868 static void pint2hrstr (char *, int, ptrdiff_t);
869 static struct text_pos run_window_scroll_functions (Lisp_Object,
870 struct text_pos);
871 static int text_outside_line_unchanged_p (struct window *,
872 ptrdiff_t, ptrdiff_t);
873 static void store_mode_line_noprop_char (char);
874 static int store_mode_line_noprop (const char *, int, int);
875 static void handle_stop (struct it *);
876 static void handle_stop_backwards (struct it *, ptrdiff_t);
877 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
878 static void ensure_echo_area_buffers (void);
879 static void unwind_with_echo_area_buffer (Lisp_Object);
880 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
881 static int with_echo_area_buffer (struct window *, int,
882 int (*) (ptrdiff_t, Lisp_Object),
883 ptrdiff_t, Lisp_Object);
884 static void clear_garbaged_frames (void);
885 static int current_message_1 (ptrdiff_t, Lisp_Object);
886 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
887 static void set_message (Lisp_Object);
888 static int set_message_1 (ptrdiff_t, Lisp_Object);
889 static int display_echo_area (struct window *);
890 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
891 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
892 static void unwind_redisplay (void);
893 static int string_char_and_length (const unsigned char *, int *);
894 static struct text_pos display_prop_end (struct it *, Lisp_Object,
895 struct text_pos);
896 static int compute_window_start_on_continuation_line (struct window *);
897 static void insert_left_trunc_glyphs (struct it *);
898 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
899 Lisp_Object);
900 static void extend_face_to_end_of_line (struct it *);
901 static int append_space_for_newline (struct it *, int);
902 static int cursor_row_fully_visible_p (struct window *, int, int);
903 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
904 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
905 static int trailing_whitespace_p (ptrdiff_t);
906 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
907 static void push_it (struct it *, struct text_pos *);
908 static void iterate_out_of_display_property (struct it *);
909 static void pop_it (struct it *);
910 static void sync_frame_with_window_matrix_rows (struct window *);
911 static void redisplay_internal (void);
912 static bool echo_area_display (bool);
913 static void redisplay_windows (Lisp_Object);
914 static void redisplay_window (Lisp_Object, bool);
915 static Lisp_Object redisplay_window_error (Lisp_Object);
916 static Lisp_Object redisplay_window_0 (Lisp_Object);
917 static Lisp_Object redisplay_window_1 (Lisp_Object);
918 static int set_cursor_from_row (struct window *, struct glyph_row *,
919 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
920 int, int);
921 static int update_menu_bar (struct frame *, int, int);
922 static int try_window_reusing_current_matrix (struct window *);
923 static int try_window_id (struct window *);
924 static int display_line (struct it *);
925 static int display_mode_lines (struct window *);
926 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
927 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
928 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
929 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
930 static void display_menu_bar (struct window *);
931 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
932 ptrdiff_t *);
933 static int display_string (const char *, Lisp_Object, Lisp_Object,
934 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
935 static void compute_line_metrics (struct it *);
936 static void run_redisplay_end_trigger_hook (struct it *);
937 static int get_overlay_strings (struct it *, ptrdiff_t);
938 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
939 static void next_overlay_string (struct it *);
940 static void reseat (struct it *, struct text_pos, int);
941 static void reseat_1 (struct it *, struct text_pos, int);
942 static void back_to_previous_visible_line_start (struct it *);
943 static void reseat_at_next_visible_line_start (struct it *, int);
944 static int next_element_from_ellipsis (struct it *);
945 static int next_element_from_display_vector (struct it *);
946 static int next_element_from_string (struct it *);
947 static int next_element_from_c_string (struct it *);
948 static int next_element_from_buffer (struct it *);
949 static int next_element_from_composition (struct it *);
950 static int next_element_from_image (struct it *);
951 #ifdef HAVE_XWIDGETS
952 static int next_element_from_xwidget(struct it *);
953 #endif
954 static int next_element_from_stretch (struct it *);
955 static void load_overlay_strings (struct it *, ptrdiff_t);
956 static int init_from_display_pos (struct it *, struct window *,
957 struct display_pos *);
958 static void reseat_to_string (struct it *, const char *,
959 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
960 static int get_next_display_element (struct it *);
961 static enum move_it_result
962 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
963 enum move_operation_enum);
964 static void get_visually_first_element (struct it *);
965 static void init_to_row_start (struct it *, struct window *,
966 struct glyph_row *);
967 static int init_to_row_end (struct it *, struct window *,
968 struct glyph_row *);
969 static void back_to_previous_line_start (struct it *);
970 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
971 static struct text_pos string_pos_nchars_ahead (struct text_pos,
972 Lisp_Object, ptrdiff_t);
973 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
974 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
975 static ptrdiff_t number_of_chars (const char *, bool);
976 static void compute_stop_pos (struct it *);
977 static void compute_string_pos (struct text_pos *, struct text_pos,
978 Lisp_Object);
979 static int face_before_or_after_it_pos (struct it *, int);
980 static ptrdiff_t next_overlay_change (ptrdiff_t);
981 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
982 Lisp_Object, struct text_pos *, ptrdiff_t, int);
983 static int handle_single_display_spec (struct it *, Lisp_Object,
984 Lisp_Object, Lisp_Object,
985 struct text_pos *, ptrdiff_t, int, int);
986 static int underlying_face_id (struct it *);
987 static int in_ellipses_for_invisible_text_p (struct display_pos *,
988 struct window *);
990 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
991 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
993 #ifdef HAVE_WINDOW_SYSTEM
995 static void x_consider_frame_title (Lisp_Object);
996 static void update_tool_bar (struct frame *, int);
997 static int redisplay_tool_bar (struct frame *);
998 static void x_draw_bottom_divider (struct window *w);
999 static void notice_overwritten_cursor (struct window *,
1000 enum glyph_row_area,
1001 int, int, int, int);
1002 static void append_stretch_glyph (struct it *, Lisp_Object,
1003 int, int, int);
1006 #endif /* HAVE_WINDOW_SYSTEM */
1008 static void produce_special_glyphs (struct it *, enum display_element_type);
1009 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
1010 static bool coords_in_mouse_face_p (struct window *, int, int);
1014 /***********************************************************************
1015 Window display dimensions
1016 ***********************************************************************/
1018 /* Return the bottom boundary y-position for text lines in window W.
1019 This is the first y position at which a line cannot start.
1020 It is relative to the top of the window.
1022 This is the height of W minus the height of a mode line, if any. */
1025 window_text_bottom_y (struct window *w)
1027 int height = WINDOW_PIXEL_HEIGHT (w);
1029 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1031 if (WINDOW_WANTS_MODELINE_P (w))
1032 height -= CURRENT_MODE_LINE_HEIGHT (w);
1034 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
1036 return height;
1039 /* Return the pixel width of display area AREA of window W.
1040 ANY_AREA means return the total width of W, not including
1041 fringes to the left and right of the window. */
1044 window_box_width (struct window *w, enum glyph_row_area area)
1046 int width = w->pixel_width;
1048 if (!w->pseudo_window_p)
1050 width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
1051 width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
1053 if (area == TEXT_AREA)
1054 width -= (WINDOW_MARGINS_WIDTH (w)
1055 + WINDOW_FRINGES_WIDTH (w));
1056 else if (area == LEFT_MARGIN_AREA)
1057 width = WINDOW_LEFT_MARGIN_WIDTH (w);
1058 else if (area == RIGHT_MARGIN_AREA)
1059 width = WINDOW_RIGHT_MARGIN_WIDTH (w);
1062 /* With wide margins, fringes, etc. we might end up with a negative
1063 width, correct that here. */
1064 return max (0, width);
1068 /* Return the pixel height of the display area of window W, not
1069 including mode lines of W, if any. */
1072 window_box_height (struct window *w)
1074 struct frame *f = XFRAME (w->frame);
1075 int height = WINDOW_PIXEL_HEIGHT (w);
1077 eassert (height >= 0);
1079 height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
1080 height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
1082 /* Note: the code below that determines the mode-line/header-line
1083 height is essentially the same as that contained in the macro
1084 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1085 the appropriate glyph row has its `mode_line_p' flag set,
1086 and if it doesn't, uses estimate_mode_line_height instead. */
1088 if (WINDOW_WANTS_MODELINE_P (w))
1090 struct glyph_row *ml_row
1091 = (w->current_matrix && w->current_matrix->rows
1092 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1093 : 0);
1094 if (ml_row && ml_row->mode_line_p)
1095 height -= ml_row->height;
1096 else
1097 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1100 if (WINDOW_WANTS_HEADER_LINE_P (w))
1102 struct glyph_row *hl_row
1103 = (w->current_matrix && w->current_matrix->rows
1104 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1105 : 0);
1106 if (hl_row && hl_row->mode_line_p)
1107 height -= hl_row->height;
1108 else
1109 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1112 /* With a very small font and a mode-line that's taller than
1113 default, we might end up with a negative height. */
1114 return max (0, height);
1117 /* Return the window-relative coordinate of the left edge of display
1118 area AREA of window W. ANY_AREA means return the left edge of the
1119 whole window, to the right of the left fringe of W. */
1122 window_box_left_offset (struct window *w, enum glyph_row_area area)
1124 int x;
1126 if (w->pseudo_window_p)
1127 return 0;
1129 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1131 if (area == TEXT_AREA)
1132 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1133 + window_box_width (w, LEFT_MARGIN_AREA));
1134 else if (area == RIGHT_MARGIN_AREA)
1135 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1136 + window_box_width (w, LEFT_MARGIN_AREA)
1137 + window_box_width (w, TEXT_AREA)
1138 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1140 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1141 else if (area == LEFT_MARGIN_AREA
1142 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1143 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1145 /* Don't return more than the window's pixel width. */
1146 return min (x, w->pixel_width);
1150 /* Return the window-relative coordinate of the right edge of display
1151 area AREA of window W. ANY_AREA means return the right edge of the
1152 whole window, to the left of the right fringe of W. */
1154 static int
1155 window_box_right_offset (struct window *w, enum glyph_row_area area)
1157 /* Don't return more than the window's pixel width. */
1158 return min (window_box_left_offset (w, area) + window_box_width (w, area),
1159 w->pixel_width);
1162 /* Return the frame-relative coordinate of the left edge of display
1163 area AREA of window W. ANY_AREA means return the left edge of the
1164 whole window, to the right of the left fringe of W. */
1167 window_box_left (struct window *w, enum glyph_row_area area)
1169 struct frame *f = XFRAME (w->frame);
1170 int x;
1172 if (w->pseudo_window_p)
1173 return FRAME_INTERNAL_BORDER_WIDTH (f);
1175 x = (WINDOW_LEFT_EDGE_X (w)
1176 + window_box_left_offset (w, area));
1178 return x;
1182 /* Return the frame-relative coordinate of the right edge of display
1183 area AREA of window W. ANY_AREA means return the right edge of the
1184 whole window, to the left of the right fringe of W. */
1187 window_box_right (struct window *w, enum glyph_row_area area)
1189 return window_box_left (w, area) + window_box_width (w, area);
1192 /* Get the bounding box of the display area AREA of window W, without
1193 mode lines, in frame-relative coordinates. ANY_AREA means the
1194 whole window, not including the left and right fringes of
1195 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1196 coordinates of the upper-left corner of the box. Return in
1197 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1199 void
1200 window_box (struct window *w, enum glyph_row_area area, int *box_x,
1201 int *box_y, int *box_width, int *box_height)
1203 if (box_width)
1204 *box_width = window_box_width (w, area);
1205 if (box_height)
1206 *box_height = window_box_height (w);
1207 if (box_x)
1208 *box_x = window_box_left (w, area);
1209 if (box_y)
1211 *box_y = WINDOW_TOP_EDGE_Y (w);
1212 if (WINDOW_WANTS_HEADER_LINE_P (w))
1213 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1217 #ifdef HAVE_WINDOW_SYSTEM
1219 /* Get the bounding box of the display area AREA of window W, without
1220 mode lines and both fringes of the window. Return in *TOP_LEFT_X
1221 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1222 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1223 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1224 box. */
1226 static void
1227 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
1228 int *bottom_right_x, int *bottom_right_y)
1230 window_box (w, ANY_AREA, top_left_x, top_left_y,
1231 bottom_right_x, bottom_right_y);
1232 *bottom_right_x += *top_left_x;
1233 *bottom_right_y += *top_left_y;
1236 #endif /* HAVE_WINDOW_SYSTEM */
1238 /***********************************************************************
1239 Utilities
1240 ***********************************************************************/
1242 /* Return the bottom y-position of the line the iterator IT is in.
1243 This can modify IT's settings. */
1246 line_bottom_y (struct it *it)
1248 int line_height = it->max_ascent + it->max_descent;
1249 int line_top_y = it->current_y;
1251 if (line_height == 0)
1253 if (last_height)
1254 line_height = last_height;
1255 else if (IT_CHARPOS (*it) < ZV)
1257 move_it_by_lines (it, 1);
1258 line_height = (it->max_ascent || it->max_descent
1259 ? it->max_ascent + it->max_descent
1260 : last_height);
1262 else
1264 struct glyph_row *row = it->glyph_row;
1266 /* Use the default character height. */
1267 it->glyph_row = NULL;
1268 it->what = IT_CHARACTER;
1269 it->c = ' ';
1270 it->len = 1;
1271 PRODUCE_GLYPHS (it);
1272 line_height = it->ascent + it->descent;
1273 it->glyph_row = row;
1277 return line_top_y + line_height;
1280 DEFUN ("line-pixel-height", Fline_pixel_height,
1281 Sline_pixel_height, 0, 0, 0,
1282 doc: /* Return height in pixels of text line in the selected window.
1284 Value is the height in pixels of the line at point. */)
1285 (void)
1287 struct it it;
1288 struct text_pos pt;
1289 struct window *w = XWINDOW (selected_window);
1290 struct buffer *old_buffer = NULL;
1291 Lisp_Object result;
1293 if (XBUFFER (w->contents) != current_buffer)
1295 old_buffer = current_buffer;
1296 set_buffer_internal_1 (XBUFFER (w->contents));
1298 SET_TEXT_POS (pt, PT, PT_BYTE);
1299 start_display (&it, w, pt);
1300 it.vpos = it.current_y = 0;
1301 last_height = 0;
1302 result = make_number (line_bottom_y (&it));
1303 if (old_buffer)
1304 set_buffer_internal_1 (old_buffer);
1306 return result;
1309 /* Return the default pixel height of text lines in window W. The
1310 value is the canonical height of the W frame's default font, plus
1311 any extra space required by the line-spacing variable or frame
1312 parameter.
1314 Implementation note: this ignores any line-spacing text properties
1315 put on the newline characters. This is because those properties
1316 only affect the _screen_ line ending in the newline (i.e., in a
1317 continued line, only the last screen line will be affected), which
1318 means only a small number of lines in a buffer can ever use this
1319 feature. Since this function is used to compute the default pixel
1320 equivalent of text lines in a window, we can safely ignore those
1321 few lines. For the same reasons, we ignore the line-height
1322 properties. */
1324 default_line_pixel_height (struct window *w)
1326 struct frame *f = WINDOW_XFRAME (w);
1327 int height = FRAME_LINE_HEIGHT (f);
1329 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1331 struct buffer *b = XBUFFER (w->contents);
1332 Lisp_Object val = BVAR (b, extra_line_spacing);
1334 if (NILP (val))
1335 val = BVAR (&buffer_defaults, extra_line_spacing);
1336 if (!NILP (val))
1338 if (RANGED_INTEGERP (0, val, INT_MAX))
1339 height += XFASTINT (val);
1340 else if (FLOATP (val))
1342 int addon = XFLOAT_DATA (val) * height + 0.5;
1344 if (addon >= 0)
1345 height += addon;
1348 else
1349 height += f->extra_line_spacing;
1352 return height;
1355 /* Subroutine of pos_visible_p below. Extracts a display string, if
1356 any, from the display spec given as its argument. */
1357 static Lisp_Object
1358 string_from_display_spec (Lisp_Object spec)
1360 if (CONSP (spec))
1362 while (CONSP (spec))
1364 if (STRINGP (XCAR (spec)))
1365 return XCAR (spec);
1366 spec = XCDR (spec);
1369 else if (VECTORP (spec))
1371 ptrdiff_t i;
1373 for (i = 0; i < ASIZE (spec); i++)
1375 if (STRINGP (AREF (spec, i)))
1376 return AREF (spec, i);
1378 return Qnil;
1381 return spec;
1385 /* Limit insanely large values of W->hscroll on frame F to the largest
1386 value that will still prevent first_visible_x and last_visible_x of
1387 'struct it' from overflowing an int. */
1388 static int
1389 window_hscroll_limited (struct window *w, struct frame *f)
1391 ptrdiff_t window_hscroll = w->hscroll;
1392 int window_text_width = window_box_width (w, TEXT_AREA);
1393 int colwidth = FRAME_COLUMN_WIDTH (f);
1395 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1396 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1398 return window_hscroll;
1401 /* Return 1 if position CHARPOS is visible in window W.
1402 CHARPOS < 0 means return info about WINDOW_END position.
1403 If visible, set *X and *Y to pixel coordinates of top left corner.
1404 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1405 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1408 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1409 int *rtop, int *rbot, int *rowh, int *vpos)
1411 struct it it;
1412 void *itdata = bidi_shelve_cache ();
1413 struct text_pos top;
1414 int visible_p = 0;
1415 struct buffer *old_buffer = NULL;
1417 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1418 return visible_p;
1420 if (XBUFFER (w->contents) != current_buffer)
1422 old_buffer = current_buffer;
1423 set_buffer_internal_1 (XBUFFER (w->contents));
1426 SET_TEXT_POS_FROM_MARKER (top, w->start);
1427 /* Scrolling a minibuffer window via scroll bar when the echo area
1428 shows long text sometimes resets the minibuffer contents behind
1429 our backs. */
1430 if (CHARPOS (top) > ZV)
1431 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1433 /* Compute exact mode line heights. */
1434 if (WINDOW_WANTS_MODELINE_P (w))
1435 w->mode_line_height
1436 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1437 BVAR (current_buffer, mode_line_format));
1439 if (WINDOW_WANTS_HEADER_LINE_P (w))
1440 w->header_line_height
1441 = display_mode_line (w, HEADER_LINE_FACE_ID,
1442 BVAR (current_buffer, header_line_format));
1444 start_display (&it, w, top);
1445 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1446 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1448 if (charpos >= 0
1449 && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
1450 && IT_CHARPOS (it) >= charpos)
1451 /* When scanning backwards under bidi iteration, move_it_to
1452 stops at or _before_ CHARPOS, because it stops at or to
1453 the _right_ of the character at CHARPOS. */
1454 || (it.bidi_p && it.bidi_it.scan_dir == -1
1455 && IT_CHARPOS (it) <= charpos)))
1457 /* We have reached CHARPOS, or passed it. How the call to
1458 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1459 or covered by a display property, move_it_to stops at the end
1460 of the invisible text, to the right of CHARPOS. (ii) If
1461 CHARPOS is in a display vector, move_it_to stops on its last
1462 glyph. */
1463 int top_x = it.current_x;
1464 int top_y = it.current_y;
1465 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1466 int bottom_y;
1467 struct it save_it;
1468 void *save_it_data = NULL;
1470 /* Calling line_bottom_y may change it.method, it.position, etc. */
1471 SAVE_IT (save_it, it, save_it_data);
1472 last_height = 0;
1473 bottom_y = line_bottom_y (&it);
1474 if (top_y < window_top_y)
1475 visible_p = bottom_y > window_top_y;
1476 else if (top_y < it.last_visible_y)
1477 visible_p = 1;
1478 if (bottom_y >= it.last_visible_y
1479 && it.bidi_p && it.bidi_it.scan_dir == -1
1480 && IT_CHARPOS (it) < charpos)
1482 /* When the last line of the window is scanned backwards
1483 under bidi iteration, we could be duped into thinking
1484 that we have passed CHARPOS, when in fact move_it_to
1485 simply stopped short of CHARPOS because it reached
1486 last_visible_y. To see if that's what happened, we call
1487 move_it_to again with a slightly larger vertical limit,
1488 and see if it actually moved vertically; if it did, we
1489 didn't really reach CHARPOS, which is beyond window end. */
1490 /* Why 10? because we don't know how many canonical lines
1491 will the height of the next line(s) be. So we guess. */
1492 int ten_more_lines = 10 * default_line_pixel_height (w);
1494 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1495 MOVE_TO_POS | MOVE_TO_Y);
1496 if (it.current_y > top_y)
1497 visible_p = 0;
1500 RESTORE_IT (&it, &save_it, save_it_data);
1501 if (visible_p)
1503 if (it.method == GET_FROM_DISPLAY_VECTOR)
1505 /* We stopped on the last glyph of a display vector.
1506 Try and recompute. Hack alert! */
1507 if (charpos < 2 || top.charpos >= charpos)
1508 top_x = it.glyph_row->x;
1509 else
1511 struct it it2, it2_prev;
1512 /* The idea is to get to the previous buffer
1513 position, consume the character there, and use
1514 the pixel coordinates we get after that. But if
1515 the previous buffer position is also displayed
1516 from a display vector, we need to consume all of
1517 the glyphs from that display vector. */
1518 start_display (&it2, w, top);
1519 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1520 /* If we didn't get to CHARPOS - 1, there's some
1521 replacing display property at that position, and
1522 we stopped after it. That is exactly the place
1523 whose coordinates we want. */
1524 if (IT_CHARPOS (it2) != charpos - 1)
1525 it2_prev = it2;
1526 else
1528 /* Iterate until we get out of the display
1529 vector that displays the character at
1530 CHARPOS - 1. */
1531 do {
1532 get_next_display_element (&it2);
1533 PRODUCE_GLYPHS (&it2);
1534 it2_prev = it2;
1535 set_iterator_to_next (&it2, 1);
1536 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1537 && IT_CHARPOS (it2) < charpos);
1539 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1540 || it2_prev.current_x > it2_prev.last_visible_x)
1541 top_x = it.glyph_row->x;
1542 else
1544 top_x = it2_prev.current_x;
1545 top_y = it2_prev.current_y;
1549 else if (IT_CHARPOS (it) != charpos)
1551 Lisp_Object cpos = make_number (charpos);
1552 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1553 Lisp_Object string = string_from_display_spec (spec);
1554 struct text_pos tpos;
1555 int replacing_spec_p;
1556 bool newline_in_string
1557 = (STRINGP (string)
1558 && memchr (SDATA (string), '\n', SBYTES (string)));
1560 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1561 replacing_spec_p
1562 = (!NILP (spec)
1563 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1564 charpos, FRAME_WINDOW_P (it.f)));
1565 /* The tricky code below is needed because there's a
1566 discrepancy between move_it_to and how we set cursor
1567 when PT is at the beginning of a portion of text
1568 covered by a display property or an overlay with a
1569 display property, or the display line ends in a
1570 newline from a display string. move_it_to will stop
1571 _after_ such display strings, whereas
1572 set_cursor_from_row conspires with cursor_row_p to
1573 place the cursor on the first glyph produced from the
1574 display string. */
1576 /* We have overshoot PT because it is covered by a
1577 display property that replaces the text it covers.
1578 If the string includes embedded newlines, we are also
1579 in the wrong display line. Backtrack to the correct
1580 line, where the display property begins. */
1581 if (replacing_spec_p)
1583 Lisp_Object startpos, endpos;
1584 EMACS_INT start, end;
1585 struct it it3;
1586 int it3_moved;
1588 /* Find the first and the last buffer positions
1589 covered by the display string. */
1590 endpos =
1591 Fnext_single_char_property_change (cpos, Qdisplay,
1592 Qnil, Qnil);
1593 startpos =
1594 Fprevious_single_char_property_change (endpos, Qdisplay,
1595 Qnil, Qnil);
1596 start = XFASTINT (startpos);
1597 end = XFASTINT (endpos);
1598 /* Move to the last buffer position before the
1599 display property. */
1600 start_display (&it3, w, top);
1601 if (start > CHARPOS (top))
1602 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1603 /* Move forward one more line if the position before
1604 the display string is a newline or if it is the
1605 rightmost character on a line that is
1606 continued or word-wrapped. */
1607 if (it3.method == GET_FROM_BUFFER
1608 && (it3.c == '\n'
1609 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1610 move_it_by_lines (&it3, 1);
1611 else if (move_it_in_display_line_to (&it3, -1,
1612 it3.current_x
1613 + it3.pixel_width,
1614 MOVE_TO_X)
1615 == MOVE_LINE_CONTINUED)
1617 move_it_by_lines (&it3, 1);
1618 /* When we are under word-wrap, the #$@%!
1619 move_it_by_lines moves 2 lines, so we need to
1620 fix that up. */
1621 if (it3.line_wrap == WORD_WRAP)
1622 move_it_by_lines (&it3, -1);
1625 /* Record the vertical coordinate of the display
1626 line where we wound up. */
1627 top_y = it3.current_y;
1628 if (it3.bidi_p)
1630 /* When characters are reordered for display,
1631 the character displayed to the left of the
1632 display string could be _after_ the display
1633 property in the logical order. Use the
1634 smallest vertical position of these two. */
1635 start_display (&it3, w, top);
1636 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1637 if (it3.current_y < top_y)
1638 top_y = it3.current_y;
1640 /* Move from the top of the window to the beginning
1641 of the display line where the display string
1642 begins. */
1643 start_display (&it3, w, top);
1644 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1645 /* If it3_moved stays zero after the 'while' loop
1646 below, that means we already were at a newline
1647 before the loop (e.g., the display string begins
1648 with a newline), so we don't need to (and cannot)
1649 inspect the glyphs of it3.glyph_row, because
1650 PRODUCE_GLYPHS will not produce anything for a
1651 newline, and thus it3.glyph_row stays at its
1652 stale content it got at top of the window. */
1653 it3_moved = 0;
1654 /* Finally, advance the iterator until we hit the
1655 first display element whose character position is
1656 CHARPOS, or until the first newline from the
1657 display string, which signals the end of the
1658 display line. */
1659 while (get_next_display_element (&it3))
1661 PRODUCE_GLYPHS (&it3);
1662 if (IT_CHARPOS (it3) == charpos
1663 || ITERATOR_AT_END_OF_LINE_P (&it3))
1664 break;
1665 it3_moved = 1;
1666 set_iterator_to_next (&it3, 0);
1668 top_x = it3.current_x - it3.pixel_width;
1669 /* Normally, we would exit the above loop because we
1670 found the display element whose character
1671 position is CHARPOS. For the contingency that we
1672 didn't, and stopped at the first newline from the
1673 display string, move back over the glyphs
1674 produced from the string, until we find the
1675 rightmost glyph not from the string. */
1676 if (it3_moved
1677 && newline_in_string
1678 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1680 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1681 + it3.glyph_row->used[TEXT_AREA];
1683 while (EQ ((g - 1)->object, string))
1685 --g;
1686 top_x -= g->pixel_width;
1688 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1689 + it3.glyph_row->used[TEXT_AREA]);
1694 *x = top_x;
1695 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1696 *rtop = max (0, window_top_y - top_y);
1697 *rbot = max (0, bottom_y - it.last_visible_y);
1698 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1699 - max (top_y, window_top_y)));
1700 *vpos = it.vpos;
1703 else
1705 /* Either we were asked to provide info about WINDOW_END, or
1706 CHARPOS is in the partially visible glyph row at end of
1707 window. */
1708 struct it it2;
1709 void *it2data = NULL;
1711 SAVE_IT (it2, it, it2data);
1712 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1713 move_it_by_lines (&it, 1);
1714 if (charpos < IT_CHARPOS (it)
1715 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1717 visible_p = true;
1718 RESTORE_IT (&it2, &it2, it2data);
1719 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1720 *x = it2.current_x;
1721 *y = it2.current_y + it2.max_ascent - it2.ascent;
1722 *rtop = max (0, -it2.current_y);
1723 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1724 - it.last_visible_y));
1725 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1726 it.last_visible_y)
1727 - max (it2.current_y,
1728 WINDOW_HEADER_LINE_HEIGHT (w))));
1729 *vpos = it2.vpos;
1731 else
1732 bidi_unshelve_cache (it2data, 1);
1734 bidi_unshelve_cache (itdata, 0);
1736 if (old_buffer)
1737 set_buffer_internal_1 (old_buffer);
1739 if (visible_p && w->hscroll > 0)
1740 *x -=
1741 window_hscroll_limited (w, WINDOW_XFRAME (w))
1742 * WINDOW_FRAME_COLUMN_WIDTH (w);
1744 #if 0
1745 /* Debugging code. */
1746 if (visible_p)
1747 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1748 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1749 else
1750 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1751 #endif
1753 return visible_p;
1757 /* Return the next character from STR. Return in *LEN the length of
1758 the character. This is like STRING_CHAR_AND_LENGTH but never
1759 returns an invalid character. If we find one, we return a `?', but
1760 with the length of the invalid character. */
1762 static int
1763 string_char_and_length (const unsigned char *str, int *len)
1765 int c;
1767 c = STRING_CHAR_AND_LENGTH (str, *len);
1768 if (!CHAR_VALID_P (c))
1769 /* We may not change the length here because other places in Emacs
1770 don't use this function, i.e. they silently accept invalid
1771 characters. */
1772 c = '?';
1774 return c;
1779 /* Given a position POS containing a valid character and byte position
1780 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1782 static struct text_pos
1783 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1785 eassert (STRINGP (string) && nchars >= 0);
1787 if (STRING_MULTIBYTE (string))
1789 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1790 int len;
1792 while (nchars--)
1794 string_char_and_length (p, &len);
1795 p += len;
1796 CHARPOS (pos) += 1;
1797 BYTEPOS (pos) += len;
1800 else
1801 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1803 return pos;
1807 /* Value is the text position, i.e. character and byte position,
1808 for character position CHARPOS in STRING. */
1810 static struct text_pos
1811 string_pos (ptrdiff_t charpos, Lisp_Object string)
1813 struct text_pos pos;
1814 eassert (STRINGP (string));
1815 eassert (charpos >= 0);
1816 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1817 return pos;
1821 /* Value is a text position, i.e. character and byte position, for
1822 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1823 means recognize multibyte characters. */
1825 static struct text_pos
1826 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1828 struct text_pos pos;
1830 eassert (s != NULL);
1831 eassert (charpos >= 0);
1833 if (multibyte_p)
1835 int len;
1837 SET_TEXT_POS (pos, 0, 0);
1838 while (charpos--)
1840 string_char_and_length ((const unsigned char *) s, &len);
1841 s += len;
1842 CHARPOS (pos) += 1;
1843 BYTEPOS (pos) += len;
1846 else
1847 SET_TEXT_POS (pos, charpos, charpos);
1849 return pos;
1853 /* Value is the number of characters in C string S. MULTIBYTE_P
1854 non-zero means recognize multibyte characters. */
1856 static ptrdiff_t
1857 number_of_chars (const char *s, bool multibyte_p)
1859 ptrdiff_t nchars;
1861 if (multibyte_p)
1863 ptrdiff_t rest = strlen (s);
1864 int len;
1865 const unsigned char *p = (const unsigned char *) s;
1867 for (nchars = 0; rest > 0; ++nchars)
1869 string_char_and_length (p, &len);
1870 rest -= len, p += len;
1873 else
1874 nchars = strlen (s);
1876 return nchars;
1880 /* Compute byte position NEWPOS->bytepos corresponding to
1881 NEWPOS->charpos. POS is a known position in string STRING.
1882 NEWPOS->charpos must be >= POS.charpos. */
1884 static void
1885 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1887 eassert (STRINGP (string));
1888 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1890 if (STRING_MULTIBYTE (string))
1891 *newpos = string_pos_nchars_ahead (pos, string,
1892 CHARPOS (*newpos) - CHARPOS (pos));
1893 else
1894 BYTEPOS (*newpos) = CHARPOS (*newpos);
1897 /* EXPORT:
1898 Return an estimation of the pixel height of mode or header lines on
1899 frame F. FACE_ID specifies what line's height to estimate. */
1902 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1904 #ifdef HAVE_WINDOW_SYSTEM
1905 if (FRAME_WINDOW_P (f))
1907 int height = FONT_HEIGHT (FRAME_FONT (f));
1909 /* This function is called so early when Emacs starts that the face
1910 cache and mode line face are not yet initialized. */
1911 if (FRAME_FACE_CACHE (f))
1913 struct face *face = FACE_FROM_ID (f, face_id);
1914 if (face)
1916 if (face->font)
1917 height = FONT_HEIGHT (face->font);
1918 if (face->box_line_width > 0)
1919 height += 2 * face->box_line_width;
1923 return height;
1925 #endif
1927 return 1;
1930 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1931 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1932 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1933 not force the value into range. */
1935 void
1936 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1937 int *x, int *y, NativeRectangle *bounds, int noclip)
1940 #ifdef HAVE_WINDOW_SYSTEM
1941 if (FRAME_WINDOW_P (f))
1943 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1944 even for negative values. */
1945 if (pix_x < 0)
1946 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1947 if (pix_y < 0)
1948 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1950 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1951 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1953 if (bounds)
1954 STORE_NATIVE_RECT (*bounds,
1955 FRAME_COL_TO_PIXEL_X (f, pix_x),
1956 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1957 FRAME_COLUMN_WIDTH (f) - 1,
1958 FRAME_LINE_HEIGHT (f) - 1);
1960 /* PXW: Should we clip pixels before converting to columns/lines? */
1961 if (!noclip)
1963 if (pix_x < 0)
1964 pix_x = 0;
1965 else if (pix_x > FRAME_TOTAL_COLS (f))
1966 pix_x = FRAME_TOTAL_COLS (f);
1968 if (pix_y < 0)
1969 pix_y = 0;
1970 else if (pix_y > FRAME_TOTAL_LINES (f))
1971 pix_y = FRAME_TOTAL_LINES (f);
1974 #endif
1976 *x = pix_x;
1977 *y = pix_y;
1981 /* Find the glyph under window-relative coordinates X/Y in window W.
1982 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1983 strings. Return in *HPOS and *VPOS the row and column number of
1984 the glyph found. Return in *AREA the glyph area containing X.
1985 Value is a pointer to the glyph found or null if X/Y is not on
1986 text, or we can't tell because W's current matrix is not up to
1987 date. */
1989 static struct glyph *
1990 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1991 int *dx, int *dy, int *area)
1993 struct glyph *glyph, *end;
1994 struct glyph_row *row = NULL;
1995 int x0, i;
1997 /* Find row containing Y. Give up if some row is not enabled. */
1998 for (i = 0; i < w->current_matrix->nrows; ++i)
2000 row = MATRIX_ROW (w->current_matrix, i);
2001 if (!row->enabled_p)
2002 return NULL;
2003 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
2004 break;
2007 *vpos = i;
2008 *hpos = 0;
2010 /* Give up if Y is not in the window. */
2011 if (i == w->current_matrix->nrows)
2012 return NULL;
2014 /* Get the glyph area containing X. */
2015 if (w->pseudo_window_p)
2017 *area = TEXT_AREA;
2018 x0 = 0;
2020 else
2022 if (x < window_box_left_offset (w, TEXT_AREA))
2024 *area = LEFT_MARGIN_AREA;
2025 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
2027 else if (x < window_box_right_offset (w, TEXT_AREA))
2029 *area = TEXT_AREA;
2030 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
2032 else
2034 *area = RIGHT_MARGIN_AREA;
2035 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
2039 /* Find glyph containing X. */
2040 glyph = row->glyphs[*area];
2041 end = glyph + row->used[*area];
2042 x -= x0;
2043 while (glyph < end && x >= glyph->pixel_width)
2045 x -= glyph->pixel_width;
2046 ++glyph;
2049 if (glyph == end)
2050 return NULL;
2052 if (dx)
2054 *dx = x;
2055 *dy = y - (row->y + row->ascent - glyph->ascent);
2058 *hpos = glyph - row->glyphs[*area];
2059 return glyph;
2062 /* Convert frame-relative x/y to coordinates relative to window W.
2063 Takes pseudo-windows into account. */
2065 static void
2066 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
2068 if (w->pseudo_window_p)
2070 /* A pseudo-window is always full-width, and starts at the
2071 left edge of the frame, plus a frame border. */
2072 struct frame *f = XFRAME (w->frame);
2073 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
2074 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2076 else
2078 *x -= WINDOW_LEFT_EDGE_X (w);
2079 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2083 #ifdef HAVE_WINDOW_SYSTEM
2085 /* EXPORT:
2086 Return in RECTS[] at most N clipping rectangles for glyph string S.
2087 Return the number of stored rectangles. */
2090 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2092 XRectangle r;
2094 if (n <= 0)
2095 return 0;
2097 if (s->row->full_width_p)
2099 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2100 r.x = WINDOW_LEFT_EDGE_X (s->w);
2101 if (s->row->mode_line_p)
2102 r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
2103 else
2104 r.width = WINDOW_PIXEL_WIDTH (s->w);
2106 /* Unless displaying a mode or menu bar line, which are always
2107 fully visible, clip to the visible part of the row. */
2108 if (s->w->pseudo_window_p)
2109 r.height = s->row->visible_height;
2110 else
2111 r.height = s->height;
2113 else
2115 /* This is a text line that may be partially visible. */
2116 r.x = window_box_left (s->w, s->area);
2117 r.width = window_box_width (s->w, s->area);
2118 r.height = s->row->visible_height;
2121 if (s->clip_head)
2122 if (r.x < s->clip_head->x)
2124 if (r.width >= s->clip_head->x - r.x)
2125 r.width -= s->clip_head->x - r.x;
2126 else
2127 r.width = 0;
2128 r.x = s->clip_head->x;
2130 if (s->clip_tail)
2131 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2133 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2134 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2135 else
2136 r.width = 0;
2139 /* If S draws overlapping rows, it's sufficient to use the top and
2140 bottom of the window for clipping because this glyph string
2141 intentionally draws over other lines. */
2142 if (s->for_overlaps)
2144 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2145 r.height = window_text_bottom_y (s->w) - r.y;
2147 /* Alas, the above simple strategy does not work for the
2148 environments with anti-aliased text: if the same text is
2149 drawn onto the same place multiple times, it gets thicker.
2150 If the overlap we are processing is for the erased cursor, we
2151 take the intersection with the rectangle of the cursor. */
2152 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2154 XRectangle rc, r_save = r;
2156 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2157 rc.y = s->w->phys_cursor.y;
2158 rc.width = s->w->phys_cursor_width;
2159 rc.height = s->w->phys_cursor_height;
2161 x_intersect_rectangles (&r_save, &rc, &r);
2164 else
2166 /* Don't use S->y for clipping because it doesn't take partially
2167 visible lines into account. For example, it can be negative for
2168 partially visible lines at the top of a window. */
2169 if (!s->row->full_width_p
2170 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2171 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2172 else
2173 r.y = max (0, s->row->y);
2176 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2178 /* If drawing the cursor, don't let glyph draw outside its
2179 advertised boundaries. Cleartype does this under some circumstances. */
2180 if (s->hl == DRAW_CURSOR)
2182 struct glyph *glyph = s->first_glyph;
2183 int height, max_y;
2185 if (s->x > r.x)
2187 if (r.width >= s->x - r.x)
2188 r.width -= s->x - r.x;
2189 else /* R2L hscrolled row with cursor outside text area */
2190 r.width = 0;
2191 r.x = s->x;
2193 r.width = min (r.width, glyph->pixel_width);
2195 /* If r.y is below window bottom, ensure that we still see a cursor. */
2196 height = min (glyph->ascent + glyph->descent,
2197 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2198 max_y = window_text_bottom_y (s->w) - height;
2199 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2200 if (s->ybase - glyph->ascent > max_y)
2202 r.y = max_y;
2203 r.height = height;
2205 else
2207 /* Don't draw cursor glyph taller than our actual glyph. */
2208 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2209 if (height < r.height)
2211 max_y = r.y + r.height;
2212 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2213 r.height = min (max_y - r.y, height);
2218 if (s->row->clip)
2220 XRectangle r_save = r;
2222 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2223 r.width = 0;
2226 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2227 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2229 #ifdef CONVERT_FROM_XRECT
2230 CONVERT_FROM_XRECT (r, *rects);
2231 #else
2232 *rects = r;
2233 #endif
2234 return 1;
2236 else
2238 /* If we are processing overlapping and allowed to return
2239 multiple clipping rectangles, we exclude the row of the glyph
2240 string from the clipping rectangle. This is to avoid drawing
2241 the same text on the environment with anti-aliasing. */
2242 #ifdef CONVERT_FROM_XRECT
2243 XRectangle rs[2];
2244 #else
2245 XRectangle *rs = rects;
2246 #endif
2247 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2249 if (s->for_overlaps & OVERLAPS_PRED)
2251 rs[i] = r;
2252 if (r.y + r.height > row_y)
2254 if (r.y < row_y)
2255 rs[i].height = row_y - r.y;
2256 else
2257 rs[i].height = 0;
2259 i++;
2261 if (s->for_overlaps & OVERLAPS_SUCC)
2263 rs[i] = r;
2264 if (r.y < row_y + s->row->visible_height)
2266 if (r.y + r.height > row_y + s->row->visible_height)
2268 rs[i].y = row_y + s->row->visible_height;
2269 rs[i].height = r.y + r.height - rs[i].y;
2271 else
2272 rs[i].height = 0;
2274 i++;
2277 n = i;
2278 #ifdef CONVERT_FROM_XRECT
2279 for (i = 0; i < n; i++)
2280 CONVERT_FROM_XRECT (rs[i], rects[i]);
2281 #endif
2282 return n;
2286 /* EXPORT:
2287 Return in *NR the clipping rectangle for glyph string S. */
2289 void
2290 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2292 get_glyph_string_clip_rects (s, nr, 1);
2296 /* EXPORT:
2297 Return the position and height of the phys cursor in window W.
2298 Set w->phys_cursor_width to width of phys cursor.
2301 void
2302 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2303 struct glyph *glyph, int *xp, int *yp, int *heightp)
2305 struct frame *f = XFRAME (WINDOW_FRAME (w));
2306 int x, y, wd, h, h0, y0;
2308 /* Compute the width of the rectangle to draw. If on a stretch
2309 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2310 rectangle as wide as the glyph, but use a canonical character
2311 width instead. */
2312 wd = glyph->pixel_width;
2314 x = w->phys_cursor.x;
2315 if (x < 0)
2317 wd += x;
2318 x = 0;
2321 if (glyph->type == STRETCH_GLYPH
2322 && !x_stretch_cursor_p)
2323 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2324 w->phys_cursor_width = wd;
2326 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2328 /* If y is below window bottom, ensure that we still see a cursor. */
2329 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2331 h = max (h0, glyph->ascent + glyph->descent);
2332 h0 = min (h0, glyph->ascent + glyph->descent);
2334 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2335 if (y < y0)
2337 h = max (h - (y0 - y) + 1, h0);
2338 y = y0 - 1;
2340 else
2342 y0 = window_text_bottom_y (w) - h0;
2343 if (y > y0)
2345 h += y - y0;
2346 y = y0;
2350 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2351 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2352 *heightp = h;
2356 * Remember which glyph the mouse is over.
2359 void
2360 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2362 Lisp_Object window;
2363 struct window *w;
2364 struct glyph_row *r, *gr, *end_row;
2365 enum window_part part;
2366 enum glyph_row_area area;
2367 int x, y, width, height;
2369 /* Try to determine frame pixel position and size of the glyph under
2370 frame pixel coordinates X/Y on frame F. */
2372 if (window_resize_pixelwise)
2374 width = height = 1;
2375 goto virtual_glyph;
2377 else if (!f->glyphs_initialized_p
2378 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2379 NILP (window)))
2381 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2382 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2383 goto virtual_glyph;
2386 w = XWINDOW (window);
2387 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2388 height = WINDOW_FRAME_LINE_HEIGHT (w);
2390 x = window_relative_x_coord (w, part, gx);
2391 y = gy - WINDOW_TOP_EDGE_Y (w);
2393 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2394 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2396 if (w->pseudo_window_p)
2398 area = TEXT_AREA;
2399 part = ON_MODE_LINE; /* Don't adjust margin. */
2400 goto text_glyph;
2403 switch (part)
2405 case ON_LEFT_MARGIN:
2406 area = LEFT_MARGIN_AREA;
2407 goto text_glyph;
2409 case ON_RIGHT_MARGIN:
2410 area = RIGHT_MARGIN_AREA;
2411 goto text_glyph;
2413 case ON_HEADER_LINE:
2414 case ON_MODE_LINE:
2415 gr = (part == ON_HEADER_LINE
2416 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2417 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2418 gy = gr->y;
2419 area = TEXT_AREA;
2420 goto text_glyph_row_found;
2422 case ON_TEXT:
2423 area = TEXT_AREA;
2425 text_glyph:
2426 gr = 0; gy = 0;
2427 for (; r <= end_row && r->enabled_p; ++r)
2428 if (r->y + r->height > y)
2430 gr = r; gy = r->y;
2431 break;
2434 text_glyph_row_found:
2435 if (gr && gy <= y)
2437 struct glyph *g = gr->glyphs[area];
2438 struct glyph *end = g + gr->used[area];
2440 height = gr->height;
2441 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2442 if (gx + g->pixel_width > x)
2443 break;
2445 if (g < end)
2447 if (g->type == IMAGE_GLYPH)
2449 /* Don't remember when mouse is over image, as
2450 image may have hot-spots. */
2451 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2452 return;
2454 width = g->pixel_width;
2456 else
2458 /* Use nominal char spacing at end of line. */
2459 x -= gx;
2460 gx += (x / width) * width;
2463 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2465 gx += window_box_left_offset (w, area);
2466 /* Don't expand over the modeline to make sure the vertical
2467 drag cursor is shown early enough. */
2468 height = min (height,
2469 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2472 else
2474 /* Use nominal line height at end of window. */
2475 gx = (x / width) * width;
2476 y -= gy;
2477 gy += (y / height) * height;
2478 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2479 /* See comment above. */
2480 height = min (height,
2481 max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
2483 break;
2485 case ON_LEFT_FRINGE:
2486 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2487 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2488 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2489 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2490 goto row_glyph;
2492 case ON_RIGHT_FRINGE:
2493 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2494 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2495 : window_box_right_offset (w, TEXT_AREA));
2496 if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
2497 && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
2498 && !WINDOW_RIGHTMOST_P (w))
2499 if (gx < WINDOW_PIXEL_WIDTH (w) - width)
2500 /* Make sure the vertical border can get her own glyph to the
2501 right of the one we build here. */
2502 width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
2503 else
2504 width = WINDOW_PIXEL_WIDTH (w) - gx;
2505 else
2506 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2508 goto row_glyph;
2510 case ON_VERTICAL_BORDER:
2511 gx = WINDOW_PIXEL_WIDTH (w) - width;
2512 goto row_glyph;
2514 case ON_VERTICAL_SCROLL_BAR:
2515 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2517 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2518 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2519 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2520 : 0)));
2521 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2523 row_glyph:
2524 gr = 0, gy = 0;
2525 for (; r <= end_row && r->enabled_p; ++r)
2526 if (r->y + r->height > y)
2528 gr = r; gy = r->y;
2529 break;
2532 if (gr && gy <= y)
2533 height = gr->height;
2534 else
2536 /* Use nominal line height at end of window. */
2537 y -= gy;
2538 gy += (y / height) * height;
2540 break;
2542 case ON_RIGHT_DIVIDER:
2543 gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
2544 width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
2545 gy = 0;
2546 /* The bottom divider prevails. */
2547 height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2548 goto add_edge;
2550 case ON_BOTTOM_DIVIDER:
2551 gx = 0;
2552 width = WINDOW_PIXEL_WIDTH (w);
2553 gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2554 height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
2555 goto add_edge;
2557 default:
2559 virtual_glyph:
2560 /* If there is no glyph under the mouse, then we divide the screen
2561 into a grid of the smallest glyph in the frame, and use that
2562 as our "glyph". */
2564 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2565 round down even for negative values. */
2566 if (gx < 0)
2567 gx -= width - 1;
2568 if (gy < 0)
2569 gy -= height - 1;
2571 gx = (gx / width) * width;
2572 gy = (gy / height) * height;
2574 goto store_rect;
2577 add_edge:
2578 gx += WINDOW_LEFT_EDGE_X (w);
2579 gy += WINDOW_TOP_EDGE_Y (w);
2581 store_rect:
2582 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2584 /* Visible feedback for debugging. */
2585 #if 0
2586 #if HAVE_X_WINDOWS
2587 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2588 f->output_data.x->normal_gc,
2589 gx, gy, width, height);
2590 #endif
2591 #endif
2595 #endif /* HAVE_WINDOW_SYSTEM */
2597 static void
2598 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
2600 eassert (w);
2601 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
2602 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
2603 w->window_end_vpos
2604 = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
2607 /***********************************************************************
2608 Lisp form evaluation
2609 ***********************************************************************/
2611 /* Error handler for safe_eval and safe_call. */
2613 static Lisp_Object
2614 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2616 add_to_log ("Error during redisplay: %S signaled %S",
2617 Flist (nargs, args), arg);
2618 return Qnil;
2621 /* Call function FUNC with the rest of NARGS - 1 arguments
2622 following. Return the result, or nil if something went
2623 wrong. Prevent redisplay during the evaluation. */
2625 static Lisp_Object
2626 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
2628 Lisp_Object val;
2630 if (inhibit_eval_during_redisplay)
2631 val = Qnil;
2632 else
2634 ptrdiff_t i;
2635 ptrdiff_t count = SPECPDL_INDEX ();
2636 Lisp_Object *args;
2637 USE_SAFE_ALLOCA;
2638 SAFE_ALLOCA_LISP (args, nargs);
2640 args[0] = func;
2641 for (i = 1; i < nargs; i++)
2642 args[i] = va_arg (ap, Lisp_Object);
2644 specbind (Qinhibit_redisplay, Qt);
2645 if (inhibit_quit)
2646 specbind (Qinhibit_quit, Qt);
2647 /* Use Qt to ensure debugger does not run,
2648 so there is no possibility of wanting to redisplay. */
2649 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2650 safe_eval_handler);
2651 SAFE_FREE ();
2652 val = unbind_to (count, val);
2655 return val;
2658 Lisp_Object
2659 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2661 Lisp_Object retval;
2662 va_list ap;
2664 va_start (ap, func);
2665 retval = safe__call (false, nargs, func, ap);
2666 va_end (ap);
2667 return retval;
2670 /* Call function FN with one argument ARG.
2671 Return the result, or nil if something went wrong. */
2673 Lisp_Object
2674 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2676 return safe_call (2, fn, arg);
2679 static Lisp_Object
2680 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
2682 Lisp_Object retval;
2683 va_list ap;
2685 va_start (ap, fn);
2686 retval = safe__call (inhibit_quit, 2, fn, ap);
2687 va_end (ap);
2688 return retval;
2691 static Lisp_Object Qeval;
2693 Lisp_Object
2694 safe_eval (Lisp_Object sexpr)
2696 return safe__call1 (false, Qeval, sexpr);
2699 static Lisp_Object
2700 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
2702 return safe__call1 (inhibit_quit, Qeval, sexpr);
2705 /* Call function FN with two arguments ARG1 and ARG2.
2706 Return the result, or nil if something went wrong. */
2708 Lisp_Object
2709 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2711 return safe_call (3, fn, arg1, arg2);
2716 /***********************************************************************
2717 Debugging
2718 ***********************************************************************/
2720 #if 0
2722 /* Define CHECK_IT to perform sanity checks on iterators.
2723 This is for debugging. It is too slow to do unconditionally. */
2725 static void
2726 check_it (struct it *it)
2728 if (it->method == GET_FROM_STRING)
2730 eassert (STRINGP (it->string));
2731 eassert (IT_STRING_CHARPOS (*it) >= 0);
2733 else
2735 eassert (IT_STRING_CHARPOS (*it) < 0);
2736 if (it->method == GET_FROM_BUFFER)
2738 /* Check that character and byte positions agree. */
2739 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2743 if (it->dpvec)
2744 eassert (it->current.dpvec_index >= 0);
2745 else
2746 eassert (it->current.dpvec_index < 0);
2749 #define CHECK_IT(IT) check_it ((IT))
2751 #else /* not 0 */
2753 #define CHECK_IT(IT) (void) 0
2755 #endif /* not 0 */
2758 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2760 /* Check that the window end of window W is what we expect it
2761 to be---the last row in the current matrix displaying text. */
2763 static void
2764 check_window_end (struct window *w)
2766 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2768 struct glyph_row *row;
2769 eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
2770 !row->enabled_p
2771 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2772 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2776 #define CHECK_WINDOW_END(W) check_window_end ((W))
2778 #else
2780 #define CHECK_WINDOW_END(W) (void) 0
2782 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2784 /***********************************************************************
2785 Iterator initialization
2786 ***********************************************************************/
2788 /* Initialize IT for displaying current_buffer in window W, starting
2789 at character position CHARPOS. CHARPOS < 0 means that no buffer
2790 position is specified which is useful when the iterator is assigned
2791 a position later. BYTEPOS is the byte position corresponding to
2792 CHARPOS.
2794 If ROW is not null, calls to produce_glyphs with IT as parameter
2795 will produce glyphs in that row.
2797 BASE_FACE_ID is the id of a base face to use. It must be one of
2798 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2799 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2800 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2802 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2803 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2804 will be initialized to use the corresponding mode line glyph row of
2805 the desired matrix of W. */
2807 void
2808 init_iterator (struct it *it, struct window *w,
2809 ptrdiff_t charpos, ptrdiff_t bytepos,
2810 struct glyph_row *row, enum face_id base_face_id)
2812 enum face_id remapped_base_face_id = base_face_id;
2814 /* Some precondition checks. */
2815 eassert (w != NULL && it != NULL);
2816 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2817 && charpos <= ZV));
2819 /* If face attributes have been changed since the last redisplay,
2820 free realized faces now because they depend on face definitions
2821 that might have changed. Don't free faces while there might be
2822 desired matrices pending which reference these faces. */
2823 if (face_change_count && !inhibit_free_realized_faces)
2825 face_change_count = 0;
2826 free_all_realized_faces (Qnil);
2829 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2830 if (! NILP (Vface_remapping_alist))
2831 remapped_base_face_id
2832 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2834 /* Use one of the mode line rows of W's desired matrix if
2835 appropriate. */
2836 if (row == NULL)
2838 if (base_face_id == MODE_LINE_FACE_ID
2839 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2840 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2841 else if (base_face_id == HEADER_LINE_FACE_ID)
2842 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2845 /* Clear IT. */
2846 memset (it, 0, sizeof *it);
2847 it->current.overlay_string_index = -1;
2848 it->current.dpvec_index = -1;
2849 it->base_face_id = remapped_base_face_id;
2850 it->string = Qnil;
2851 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2852 it->paragraph_embedding = L2R;
2853 it->bidi_it.string.lstring = Qnil;
2854 it->bidi_it.string.s = NULL;
2855 it->bidi_it.string.bufpos = 0;
2856 it->bidi_it.w = w;
2858 /* The window in which we iterate over current_buffer: */
2859 XSETWINDOW (it->window, w);
2860 it->w = w;
2861 it->f = XFRAME (w->frame);
2863 it->cmp_it.id = -1;
2865 /* Extra space between lines (on window systems only). */
2866 if (base_face_id == DEFAULT_FACE_ID
2867 && FRAME_WINDOW_P (it->f))
2869 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2870 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2871 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2872 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2873 * FRAME_LINE_HEIGHT (it->f));
2874 else if (it->f->extra_line_spacing > 0)
2875 it->extra_line_spacing = it->f->extra_line_spacing;
2876 it->max_extra_line_spacing = 0;
2879 /* If realized faces have been removed, e.g. because of face
2880 attribute changes of named faces, recompute them. When running
2881 in batch mode, the face cache of the initial frame is null. If
2882 we happen to get called, make a dummy face cache. */
2883 if (FRAME_FACE_CACHE (it->f) == NULL)
2884 init_frame_faces (it->f);
2885 if (FRAME_FACE_CACHE (it->f)->used == 0)
2886 recompute_basic_faces (it->f);
2888 /* Current value of the `slice', `space-width', and 'height' properties. */
2889 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2890 it->space_width = Qnil;
2891 it->font_height = Qnil;
2892 it->override_ascent = -1;
2894 /* Are control characters displayed as `^C'? */
2895 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2897 /* -1 means everything between a CR and the following line end
2898 is invisible. >0 means lines indented more than this value are
2899 invisible. */
2900 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2901 ? (clip_to_bounds
2902 (-1, XINT (BVAR (current_buffer, selective_display)),
2903 PTRDIFF_MAX))
2904 : (!NILP (BVAR (current_buffer, selective_display))
2905 ? -1 : 0));
2906 it->selective_display_ellipsis_p
2907 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2909 /* Display table to use. */
2910 it->dp = window_display_table (w);
2912 /* Are multibyte characters enabled in current_buffer? */
2913 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2915 /* Get the position at which the redisplay_end_trigger hook should
2916 be run, if it is to be run at all. */
2917 if (MARKERP (w->redisplay_end_trigger)
2918 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2919 it->redisplay_end_trigger_charpos
2920 = marker_position (w->redisplay_end_trigger);
2921 else if (INTEGERP (w->redisplay_end_trigger))
2922 it->redisplay_end_trigger_charpos
2923 = clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger),
2924 PTRDIFF_MAX);
2926 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2928 /* Are lines in the display truncated? */
2929 if (base_face_id != DEFAULT_FACE_ID
2930 || it->w->hscroll
2931 || (! WINDOW_FULL_WIDTH_P (it->w)
2932 && ((!NILP (Vtruncate_partial_width_windows)
2933 && !INTEGERP (Vtruncate_partial_width_windows))
2934 || (INTEGERP (Vtruncate_partial_width_windows)
2935 /* PXW: Shall we do something about this? */
2936 && (WINDOW_TOTAL_COLS (it->w)
2937 < XINT (Vtruncate_partial_width_windows))))))
2938 it->line_wrap = TRUNCATE;
2939 else if (NILP (BVAR (current_buffer, truncate_lines)))
2940 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2941 ? WINDOW_WRAP : WORD_WRAP;
2942 else
2943 it->line_wrap = TRUNCATE;
2945 /* Get dimensions of truncation and continuation glyphs. These are
2946 displayed as fringe bitmaps under X, but we need them for such
2947 frames when the fringes are turned off. But leave the dimensions
2948 zero for tooltip frames, as these glyphs look ugly there and also
2949 sabotage calculations of tooltip dimensions in x-show-tip. */
2950 #ifdef HAVE_WINDOW_SYSTEM
2951 if (!(FRAME_WINDOW_P (it->f)
2952 && FRAMEP (tip_frame)
2953 && it->f == XFRAME (tip_frame)))
2954 #endif
2956 if (it->line_wrap == TRUNCATE)
2958 /* We will need the truncation glyph. */
2959 eassert (it->glyph_row == NULL);
2960 produce_special_glyphs (it, IT_TRUNCATION);
2961 it->truncation_pixel_width = it->pixel_width;
2963 else
2965 /* We will need the continuation glyph. */
2966 eassert (it->glyph_row == NULL);
2967 produce_special_glyphs (it, IT_CONTINUATION);
2968 it->continuation_pixel_width = it->pixel_width;
2972 /* Reset these values to zero because the produce_special_glyphs
2973 above has changed them. */
2974 it->pixel_width = it->ascent = it->descent = 0;
2975 it->phys_ascent = it->phys_descent = 0;
2977 /* Set this after getting the dimensions of truncation and
2978 continuation glyphs, so that we don't produce glyphs when calling
2979 produce_special_glyphs, above. */
2980 it->glyph_row = row;
2981 it->area = TEXT_AREA;
2983 /* Get the dimensions of the display area. The display area
2984 consists of the visible window area plus a horizontally scrolled
2985 part to the left of the window. All x-values are relative to the
2986 start of this total display area. */
2987 if (base_face_id != DEFAULT_FACE_ID)
2989 /* Mode lines, menu bar in terminal frames. */
2990 it->first_visible_x = 0;
2991 it->last_visible_x = WINDOW_PIXEL_WIDTH (w);
2993 else
2995 it->first_visible_x
2996 = window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2997 it->last_visible_x = (it->first_visible_x
2998 + window_box_width (w, TEXT_AREA));
3000 /* If we truncate lines, leave room for the truncation glyph(s) at
3001 the right margin. Otherwise, leave room for the continuation
3002 glyph(s). Done only if the window has no right fringe. */
3003 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
3005 if (it->line_wrap == TRUNCATE)
3006 it->last_visible_x -= it->truncation_pixel_width;
3007 else
3008 it->last_visible_x -= it->continuation_pixel_width;
3011 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
3012 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
3015 /* Leave room for a border glyph. */
3016 if (!FRAME_WINDOW_P (it->f)
3017 && !WINDOW_RIGHTMOST_P (it->w))
3018 it->last_visible_x -= 1;
3020 it->last_visible_y = window_text_bottom_y (w);
3022 /* For mode lines and alike, arrange for the first glyph having a
3023 left box line if the face specifies a box. */
3024 if (base_face_id != DEFAULT_FACE_ID)
3026 struct face *face;
3028 it->face_id = remapped_base_face_id;
3030 /* If we have a boxed mode line, make the first character appear
3031 with a left box line. */
3032 face = FACE_FROM_ID (it->f, remapped_base_face_id);
3033 if (face && face->box != FACE_NO_BOX)
3034 it->start_of_box_run_p = true;
3037 /* If a buffer position was specified, set the iterator there,
3038 getting overlays and face properties from that position. */
3039 if (charpos >= BUF_BEG (current_buffer))
3041 it->stop_charpos = charpos;
3042 it->end_charpos = ZV;
3043 eassert (charpos == BYTE_TO_CHAR (bytepos));
3044 IT_CHARPOS (*it) = charpos;
3045 IT_BYTEPOS (*it) = bytepos;
3047 /* We will rely on `reseat' to set this up properly, via
3048 handle_face_prop. */
3049 it->face_id = it->base_face_id;
3051 it->start = it->current;
3052 /* Do we need to reorder bidirectional text? Not if this is a
3053 unibyte buffer: by definition, none of the single-byte
3054 characters are strong R2L, so no reordering is needed. And
3055 bidi.c doesn't support unibyte buffers anyway. Also, don't
3056 reorder while we are loading loadup.el, since the tables of
3057 character properties needed for reordering are not yet
3058 available. */
3059 it->bidi_p =
3060 NILP (Vpurify_flag)
3061 && !NILP (BVAR (current_buffer, bidi_display_reordering))
3062 && it->multibyte_p;
3064 /* If we are to reorder bidirectional text, init the bidi
3065 iterator. */
3066 if (it->bidi_p)
3068 /* Since we don't know at this point whether there will be
3069 any R2L lines in the window, we reserve space for
3070 truncation/continuation glyphs even if only the left
3071 fringe is absent. */
3072 if (base_face_id == DEFAULT_FACE_ID
3073 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
3074 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
3076 if (it->line_wrap == TRUNCATE)
3077 it->last_visible_x -= it->truncation_pixel_width;
3078 else
3079 it->last_visible_x -= it->continuation_pixel_width;
3081 /* Note the paragraph direction that this buffer wants to
3082 use. */
3083 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3084 Qleft_to_right))
3085 it->paragraph_embedding = L2R;
3086 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
3087 Qright_to_left))
3088 it->paragraph_embedding = R2L;
3089 else
3090 it->paragraph_embedding = NEUTRAL_DIR;
3091 bidi_unshelve_cache (NULL, 0);
3092 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
3093 &it->bidi_it);
3096 /* Compute faces etc. */
3097 reseat (it, it->current.pos, 1);
3100 CHECK_IT (it);
3104 /* Initialize IT for the display of window W with window start POS. */
3106 void
3107 start_display (struct it *it, struct window *w, struct text_pos pos)
3109 struct glyph_row *row;
3110 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
3112 row = w->desired_matrix->rows + first_vpos;
3113 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
3114 it->first_vpos = first_vpos;
3116 /* Don't reseat to previous visible line start if current start
3117 position is in a string or image. */
3118 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
3120 int start_at_line_beg_p;
3121 int first_y = it->current_y;
3123 /* If window start is not at a line start, skip forward to POS to
3124 get the correct continuation lines width. */
3125 start_at_line_beg_p = (CHARPOS (pos) == BEGV
3126 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
3127 if (!start_at_line_beg_p)
3129 int new_x;
3131 reseat_at_previous_visible_line_start (it);
3132 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3134 new_x = it->current_x + it->pixel_width;
3136 /* If lines are continued, this line may end in the middle
3137 of a multi-glyph character (e.g. a control character
3138 displayed as \003, or in the middle of an overlay
3139 string). In this case move_it_to above will not have
3140 taken us to the start of the continuation line but to the
3141 end of the continued line. */
3142 if (it->current_x > 0
3143 && it->line_wrap != TRUNCATE /* Lines are continued. */
3144 && (/* And glyph doesn't fit on the line. */
3145 new_x > it->last_visible_x
3146 /* Or it fits exactly and we're on a window
3147 system frame. */
3148 || (new_x == it->last_visible_x
3149 && FRAME_WINDOW_P (it->f)
3150 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3151 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3152 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3154 if ((it->current.dpvec_index >= 0
3155 || it->current.overlay_string_index >= 0)
3156 /* If we are on a newline from a display vector or
3157 overlay string, then we are already at the end of
3158 a screen line; no need to go to the next line in
3159 that case, as this line is not really continued.
3160 (If we do go to the next line, C-e will not DTRT.) */
3161 && it->c != '\n')
3163 set_iterator_to_next (it, 1);
3164 move_it_in_display_line_to (it, -1, -1, 0);
3167 it->continuation_lines_width += it->current_x;
3169 /* If the character at POS is displayed via a display
3170 vector, move_it_to above stops at the final glyph of
3171 IT->dpvec. To make the caller redisplay that character
3172 again (a.k.a. start at POS), we need to reset the
3173 dpvec_index to the beginning of IT->dpvec. */
3174 else if (it->current.dpvec_index >= 0)
3175 it->current.dpvec_index = 0;
3177 /* We're starting a new display line, not affected by the
3178 height of the continued line, so clear the appropriate
3179 fields in the iterator structure. */
3180 it->max_ascent = it->max_descent = 0;
3181 it->max_phys_ascent = it->max_phys_descent = 0;
3183 it->current_y = first_y;
3184 it->vpos = 0;
3185 it->current_x = it->hpos = 0;
3191 /* Return 1 if POS is a position in ellipses displayed for invisible
3192 text. W is the window we display, for text property lookup. */
3194 static int
3195 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3197 Lisp_Object prop, window;
3198 int ellipses_p = 0;
3199 ptrdiff_t charpos = CHARPOS (pos->pos);
3201 /* If POS specifies a position in a display vector, this might
3202 be for an ellipsis displayed for invisible text. We won't
3203 get the iterator set up for delivering that ellipsis unless
3204 we make sure that it gets aware of the invisible text. */
3205 if (pos->dpvec_index >= 0
3206 && pos->overlay_string_index < 0
3207 && CHARPOS (pos->string_pos) < 0
3208 && charpos > BEGV
3209 && (XSETWINDOW (window, w),
3210 prop = Fget_char_property (make_number (charpos),
3211 Qinvisible, window),
3212 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3214 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3215 window);
3216 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3219 return ellipses_p;
3223 /* Initialize IT for stepping through current_buffer in window W,
3224 starting at position POS that includes overlay string and display
3225 vector/ control character translation position information. Value
3226 is zero if there are overlay strings with newlines at POS. */
3228 static int
3229 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3231 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3232 int i, overlay_strings_with_newlines = 0;
3234 /* If POS specifies a position in a display vector, this might
3235 be for an ellipsis displayed for invisible text. We won't
3236 get the iterator set up for delivering that ellipsis unless
3237 we make sure that it gets aware of the invisible text. */
3238 if (in_ellipses_for_invisible_text_p (pos, w))
3240 --charpos;
3241 bytepos = 0;
3244 /* Keep in mind: the call to reseat in init_iterator skips invisible
3245 text, so we might end up at a position different from POS. This
3246 is only a problem when POS is a row start after a newline and an
3247 overlay starts there with an after-string, and the overlay has an
3248 invisible property. Since we don't skip invisible text in
3249 display_line and elsewhere immediately after consuming the
3250 newline before the row start, such a POS will not be in a string,
3251 but the call to init_iterator below will move us to the
3252 after-string. */
3253 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3255 /* This only scans the current chunk -- it should scan all chunks.
3256 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3257 to 16 in 22.1 to make this a lesser problem. */
3258 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3260 const char *s = SSDATA (it->overlay_strings[i]);
3261 const char *e = s + SBYTES (it->overlay_strings[i]);
3263 while (s < e && *s != '\n')
3264 ++s;
3266 if (s < e)
3268 overlay_strings_with_newlines = 1;
3269 break;
3273 /* If position is within an overlay string, set up IT to the right
3274 overlay string. */
3275 if (pos->overlay_string_index >= 0)
3277 int relative_index;
3279 /* If the first overlay string happens to have a `display'
3280 property for an image, the iterator will be set up for that
3281 image, and we have to undo that setup first before we can
3282 correct the overlay string index. */
3283 if (it->method == GET_FROM_IMAGE)
3284 pop_it (it);
3286 /* We already have the first chunk of overlay strings in
3287 IT->overlay_strings. Load more until the one for
3288 pos->overlay_string_index is in IT->overlay_strings. */
3289 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3291 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3292 it->current.overlay_string_index = 0;
3293 while (n--)
3295 load_overlay_strings (it, 0);
3296 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3300 it->current.overlay_string_index = pos->overlay_string_index;
3301 relative_index = (it->current.overlay_string_index
3302 % OVERLAY_STRING_CHUNK_SIZE);
3303 it->string = it->overlay_strings[relative_index];
3304 eassert (STRINGP (it->string));
3305 it->current.string_pos = pos->string_pos;
3306 it->method = GET_FROM_STRING;
3307 it->end_charpos = SCHARS (it->string);
3308 /* Set up the bidi iterator for this overlay string. */
3309 if (it->bidi_p)
3311 it->bidi_it.string.lstring = it->string;
3312 it->bidi_it.string.s = NULL;
3313 it->bidi_it.string.schars = SCHARS (it->string);
3314 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3315 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3316 it->bidi_it.string.unibyte = !it->multibyte_p;
3317 it->bidi_it.w = it->w;
3318 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3319 FRAME_WINDOW_P (it->f), &it->bidi_it);
3321 /* Synchronize the state of the bidi iterator with
3322 pos->string_pos. For any string position other than
3323 zero, this will be done automagically when we resume
3324 iteration over the string and get_visually_first_element
3325 is called. But if string_pos is zero, and the string is
3326 to be reordered for display, we need to resync manually,
3327 since it could be that the iteration state recorded in
3328 pos ended at string_pos of 0 moving backwards in string. */
3329 if (CHARPOS (pos->string_pos) == 0)
3331 get_visually_first_element (it);
3332 if (IT_STRING_CHARPOS (*it) != 0)
3333 do {
3334 /* Paranoia. */
3335 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3336 bidi_move_to_visually_next (&it->bidi_it);
3337 } while (it->bidi_it.charpos != 0);
3339 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3340 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3344 if (CHARPOS (pos->string_pos) >= 0)
3346 /* Recorded position is not in an overlay string, but in another
3347 string. This can only be a string from a `display' property.
3348 IT should already be filled with that string. */
3349 it->current.string_pos = pos->string_pos;
3350 eassert (STRINGP (it->string));
3351 if (it->bidi_p)
3352 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3353 FRAME_WINDOW_P (it->f), &it->bidi_it);
3356 /* Restore position in display vector translations, control
3357 character translations or ellipses. */
3358 if (pos->dpvec_index >= 0)
3360 if (it->dpvec == NULL)
3361 get_next_display_element (it);
3362 eassert (it->dpvec && it->current.dpvec_index == 0);
3363 it->current.dpvec_index = pos->dpvec_index;
3366 CHECK_IT (it);
3367 return !overlay_strings_with_newlines;
3371 /* Initialize IT for stepping through current_buffer in window W
3372 starting at ROW->start. */
3374 static void
3375 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3377 init_from_display_pos (it, w, &row->start);
3378 it->start = row->start;
3379 it->continuation_lines_width = row->continuation_lines_width;
3380 CHECK_IT (it);
3384 /* Initialize IT for stepping through current_buffer in window W
3385 starting in the line following ROW, i.e. starting at ROW->end.
3386 Value is zero if there are overlay strings with newlines at ROW's
3387 end position. */
3389 static int
3390 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3392 int success = 0;
3394 if (init_from_display_pos (it, w, &row->end))
3396 if (row->continued_p)
3397 it->continuation_lines_width
3398 = row->continuation_lines_width + row->pixel_width;
3399 CHECK_IT (it);
3400 success = 1;
3403 return success;
3409 /***********************************************************************
3410 Text properties
3411 ***********************************************************************/
3413 /* Called when IT reaches IT->stop_charpos. Handle text property and
3414 overlay changes. Set IT->stop_charpos to the next position where
3415 to stop. */
3417 static void
3418 handle_stop (struct it *it)
3420 enum prop_handled handled;
3421 int handle_overlay_change_p;
3422 struct props *p;
3424 it->dpvec = NULL;
3425 it->current.dpvec_index = -1;
3426 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3427 it->ignore_overlay_strings_at_pos_p = 0;
3428 it->ellipsis_p = 0;
3430 /* Use face of preceding text for ellipsis (if invisible) */
3431 if (it->selective_display_ellipsis_p)
3432 it->saved_face_id = it->face_id;
3434 /* Here's the description of the semantics of, and the logic behind,
3435 the various HANDLED_* statuses:
3437 HANDLED_NORMALLY means the handler did its job, and the loop
3438 should proceed to calling the next handler in order.
3440 HANDLED_RECOMPUTE_PROPS means the handler caused a significant
3441 change in the properties and overlays at current position, so the
3442 loop should be restarted, to re-invoke the handlers that were
3443 already called. This happens when fontification-functions were
3444 called by handle_fontified_prop, and actually fontified
3445 something. Another case where HANDLED_RECOMPUTE_PROPS is
3446 returned is when we discover overlay strings that need to be
3447 displayed right away. The loop below will continue for as long
3448 as the status is HANDLED_RECOMPUTE_PROPS.
3450 HANDLED_RETURN means return immediately to the caller, to
3451 continue iteration without calling any further handlers. This is
3452 used when we need to act on some property right away, for example
3453 when we need to display the ellipsis or a replacing display
3454 property, such as display string or image.
3456 HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
3457 consumed, and the handler switched to the next overlay string.
3458 This signals the loop below to refrain from looking for more
3459 overlays before all the overlay strings of the current overlay
3460 are processed.
3462 Some of the handlers called by the loop push the iterator state
3463 onto the stack (see 'push_it'), and arrange for the iteration to
3464 continue with another object, such as an image, a display string,
3465 or an overlay string. In most such cases, it->stop_charpos is
3466 set to the first character of the string, so that when the
3467 iteration resumes, this function will immediately be called
3468 again, to examine the properties at the beginning of the string.
3470 When a display or overlay string is exhausted, the iterator state
3471 is popped (see 'pop_it'), and iteration continues with the
3472 previous object. Again, in many such cases this function is
3473 called again to find the next position where properties might
3474 change. */
3478 handled = HANDLED_NORMALLY;
3480 /* Call text property handlers. */
3481 for (p = it_props; p->handler; ++p)
3483 handled = p->handler (it);
3485 if (handled == HANDLED_RECOMPUTE_PROPS)
3486 break;
3487 else if (handled == HANDLED_RETURN)
3489 /* We still want to show before and after strings from
3490 overlays even if the actual buffer text is replaced. */
3491 if (!handle_overlay_change_p
3492 || it->sp > 1
3493 /* Don't call get_overlay_strings_1 if we already
3494 have overlay strings loaded, because doing so
3495 will load them again and push the iterator state
3496 onto the stack one more time, which is not
3497 expected by the rest of the code that processes
3498 overlay strings. */
3499 || (it->current.overlay_string_index < 0
3500 ? !get_overlay_strings_1 (it, 0, 0)
3501 : 0))
3503 if (it->ellipsis_p)
3504 setup_for_ellipsis (it, 0);
3505 /* When handling a display spec, we might load an
3506 empty string. In that case, discard it here. We
3507 used to discard it in handle_single_display_spec,
3508 but that causes get_overlay_strings_1, above, to
3509 ignore overlay strings that we must check. */
3510 if (STRINGP (it->string) && !SCHARS (it->string))
3511 pop_it (it);
3512 return;
3514 else if (STRINGP (it->string) && !SCHARS (it->string))
3515 pop_it (it);
3516 else
3518 it->ignore_overlay_strings_at_pos_p = true;
3519 it->string_from_display_prop_p = 0;
3520 it->from_disp_prop_p = 0;
3521 handle_overlay_change_p = 0;
3523 handled = HANDLED_RECOMPUTE_PROPS;
3524 break;
3526 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3527 handle_overlay_change_p = 0;
3530 if (handled != HANDLED_RECOMPUTE_PROPS)
3532 /* Don't check for overlay strings below when set to deliver
3533 characters from a display vector. */
3534 if (it->method == GET_FROM_DISPLAY_VECTOR)
3535 handle_overlay_change_p = 0;
3537 /* Handle overlay changes.
3538 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3539 if it finds overlays. */
3540 if (handle_overlay_change_p)
3541 handled = handle_overlay_change (it);
3544 if (it->ellipsis_p)
3546 setup_for_ellipsis (it, 0);
3547 break;
3550 while (handled == HANDLED_RECOMPUTE_PROPS);
3552 /* Determine where to stop next. */
3553 if (handled == HANDLED_NORMALLY)
3554 compute_stop_pos (it);
3558 /* Compute IT->stop_charpos from text property and overlay change
3559 information for IT's current position. */
3561 static void
3562 compute_stop_pos (struct it *it)
3564 register INTERVAL iv, next_iv;
3565 Lisp_Object object, limit, position;
3566 ptrdiff_t charpos, bytepos;
3568 if (STRINGP (it->string))
3570 /* Strings are usually short, so don't limit the search for
3571 properties. */
3572 it->stop_charpos = it->end_charpos;
3573 object = it->string;
3574 limit = Qnil;
3575 charpos = IT_STRING_CHARPOS (*it);
3576 bytepos = IT_STRING_BYTEPOS (*it);
3578 else
3580 ptrdiff_t pos;
3582 /* If end_charpos is out of range for some reason, such as a
3583 misbehaving display function, rationalize it (Bug#5984). */
3584 if (it->end_charpos > ZV)
3585 it->end_charpos = ZV;
3586 it->stop_charpos = it->end_charpos;
3588 /* If next overlay change is in front of the current stop pos
3589 (which is IT->end_charpos), stop there. Note: value of
3590 next_overlay_change is point-max if no overlay change
3591 follows. */
3592 charpos = IT_CHARPOS (*it);
3593 bytepos = IT_BYTEPOS (*it);
3594 pos = next_overlay_change (charpos);
3595 if (pos < it->stop_charpos)
3596 it->stop_charpos = pos;
3598 /* Set up variables for computing the stop position from text
3599 property changes. */
3600 XSETBUFFER (object, current_buffer);
3601 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3604 /* Get the interval containing IT's position. Value is a null
3605 interval if there isn't such an interval. */
3606 position = make_number (charpos);
3607 iv = validate_interval_range (object, &position, &position, 0);
3608 if (iv)
3610 Lisp_Object values_here[LAST_PROP_IDX];
3611 struct props *p;
3613 /* Get properties here. */
3614 for (p = it_props; p->handler; ++p)
3615 values_here[p->idx] = textget (iv->plist, *p->name);
3617 /* Look for an interval following iv that has different
3618 properties. */
3619 for (next_iv = next_interval (iv);
3620 (next_iv
3621 && (NILP (limit)
3622 || XFASTINT (limit) > next_iv->position));
3623 next_iv = next_interval (next_iv))
3625 for (p = it_props; p->handler; ++p)
3627 Lisp_Object new_value;
3629 new_value = textget (next_iv->plist, *p->name);
3630 if (!EQ (values_here[p->idx], new_value))
3631 break;
3634 if (p->handler)
3635 break;
3638 if (next_iv)
3640 if (INTEGERP (limit)
3641 && next_iv->position >= XFASTINT (limit))
3642 /* No text property change up to limit. */
3643 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3644 else
3645 /* Text properties change in next_iv. */
3646 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3650 if (it->cmp_it.id < 0)
3652 ptrdiff_t stoppos = it->end_charpos;
3654 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3655 stoppos = -1;
3656 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3657 stoppos, it->string);
3660 eassert (STRINGP (it->string)
3661 || (it->stop_charpos >= BEGV
3662 && it->stop_charpos >= IT_CHARPOS (*it)));
3666 /* Return the position of the next overlay change after POS in
3667 current_buffer. Value is point-max if no overlay change
3668 follows. This is like `next-overlay-change' but doesn't use
3669 xmalloc. */
3671 static ptrdiff_t
3672 next_overlay_change (ptrdiff_t pos)
3674 ptrdiff_t i, noverlays;
3675 ptrdiff_t endpos;
3676 Lisp_Object *overlays;
3677 USE_SAFE_ALLOCA;
3679 /* Get all overlays at the given position. */
3680 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3682 /* If any of these overlays ends before endpos,
3683 use its ending point instead. */
3684 for (i = 0; i < noverlays; ++i)
3686 Lisp_Object oend;
3687 ptrdiff_t oendpos;
3689 oend = OVERLAY_END (overlays[i]);
3690 oendpos = OVERLAY_POSITION (oend);
3691 endpos = min (endpos, oendpos);
3694 SAFE_FREE ();
3695 return endpos;
3698 /* How many characters forward to search for a display property or
3699 display string. Searching too far forward makes the bidi display
3700 sluggish, especially in small windows. */
3701 #define MAX_DISP_SCAN 250
3703 /* Return the character position of a display string at or after
3704 position specified by POSITION. If no display string exists at or
3705 after POSITION, return ZV. A display string is either an overlay
3706 with `display' property whose value is a string, or a `display'
3707 text property whose value is a string. STRING is data about the
3708 string to iterate; if STRING->lstring is nil, we are iterating a
3709 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3710 on a GUI frame. DISP_PROP is set to zero if we searched
3711 MAX_DISP_SCAN characters forward without finding any display
3712 strings, non-zero otherwise. It is set to 2 if the display string
3713 uses any kind of `(space ...)' spec that will produce a stretch of
3714 white space in the text area. */
3715 ptrdiff_t
3716 compute_display_string_pos (struct text_pos *position,
3717 struct bidi_string_data *string,
3718 struct window *w,
3719 int frame_window_p, int *disp_prop)
3721 /* OBJECT = nil means current buffer. */
3722 Lisp_Object object, object1;
3723 Lisp_Object pos, spec, limpos;
3724 int string_p = (string && (STRINGP (string->lstring) || string->s));
3725 ptrdiff_t eob = string_p ? string->schars : ZV;
3726 ptrdiff_t begb = string_p ? 0 : BEGV;
3727 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3728 ptrdiff_t lim =
3729 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3730 struct text_pos tpos;
3731 int rv = 0;
3733 if (string && STRINGP (string->lstring))
3734 object1 = object = string->lstring;
3735 else if (w && !string_p)
3737 XSETWINDOW (object, w);
3738 object1 = Qnil;
3740 else
3741 object1 = object = Qnil;
3743 *disp_prop = 1;
3745 if (charpos >= eob
3746 /* We don't support display properties whose values are strings
3747 that have display string properties. */
3748 || string->from_disp_str
3749 /* C strings cannot have display properties. */
3750 || (string->s && !STRINGP (object)))
3752 *disp_prop = 0;
3753 return eob;
3756 /* If the character at CHARPOS is where the display string begins,
3757 return CHARPOS. */
3758 pos = make_number (charpos);
3759 if (STRINGP (object))
3760 bufpos = string->bufpos;
3761 else
3762 bufpos = charpos;
3763 tpos = *position;
3764 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3765 && (charpos <= begb
3766 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3767 object),
3768 spec))
3769 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3770 frame_window_p)))
3772 if (rv == 2)
3773 *disp_prop = 2;
3774 return charpos;
3777 /* Look forward for the first character with a `display' property
3778 that will replace the underlying text when displayed. */
3779 limpos = make_number (lim);
3780 do {
3781 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3782 CHARPOS (tpos) = XFASTINT (pos);
3783 if (CHARPOS (tpos) >= lim)
3785 *disp_prop = 0;
3786 break;
3788 if (STRINGP (object))
3789 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3790 else
3791 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3792 spec = Fget_char_property (pos, Qdisplay, object);
3793 if (!STRINGP (object))
3794 bufpos = CHARPOS (tpos);
3795 } while (NILP (spec)
3796 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3797 bufpos, frame_window_p)));
3798 if (rv == 2)
3799 *disp_prop = 2;
3801 return CHARPOS (tpos);
3804 /* Return the character position of the end of the display string that
3805 started at CHARPOS. If there's no display string at CHARPOS,
3806 return -1. A display string is either an overlay with `display'
3807 property whose value is a string or a `display' text property whose
3808 value is a string. */
3809 ptrdiff_t
3810 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3812 /* OBJECT = nil means current buffer. */
3813 Lisp_Object object =
3814 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3815 Lisp_Object pos = make_number (charpos);
3816 ptrdiff_t eob =
3817 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3819 if (charpos >= eob || (string->s && !STRINGP (object)))
3820 return eob;
3822 /* It could happen that the display property or overlay was removed
3823 since we found it in compute_display_string_pos above. One way
3824 this can happen is if JIT font-lock was called (through
3825 handle_fontified_prop), and jit-lock-functions remove text
3826 properties or overlays from the portion of buffer that includes
3827 CHARPOS. Muse mode is known to do that, for example. In this
3828 case, we return -1 to the caller, to signal that no display
3829 string is actually present at CHARPOS. See bidi_fetch_char for
3830 how this is handled.
3832 An alternative would be to never look for display properties past
3833 it->stop_charpos. But neither compute_display_string_pos nor
3834 bidi_fetch_char that calls it know or care where the next
3835 stop_charpos is. */
3836 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3837 return -1;
3839 /* Look forward for the first character where the `display' property
3840 changes. */
3841 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3843 return XFASTINT (pos);
3848 /***********************************************************************
3849 Fontification
3850 ***********************************************************************/
3852 /* Handle changes in the `fontified' property of the current buffer by
3853 calling hook functions from Qfontification_functions to fontify
3854 regions of text. */
3856 static enum prop_handled
3857 handle_fontified_prop (struct it *it)
3859 Lisp_Object prop, pos;
3860 enum prop_handled handled = HANDLED_NORMALLY;
3862 if (!NILP (Vmemory_full))
3863 return handled;
3865 /* Get the value of the `fontified' property at IT's current buffer
3866 position. (The `fontified' property doesn't have a special
3867 meaning in strings.) If the value is nil, call functions from
3868 Qfontification_functions. */
3869 if (!STRINGP (it->string)
3870 && it->s == NULL
3871 && !NILP (Vfontification_functions)
3872 && !NILP (Vrun_hooks)
3873 && (pos = make_number (IT_CHARPOS (*it)),
3874 prop = Fget_char_property (pos, Qfontified, Qnil),
3875 /* Ignore the special cased nil value always present at EOB since
3876 no amount of fontifying will be able to change it. */
3877 NILP (prop) && IT_CHARPOS (*it) < Z))
3879 ptrdiff_t count = SPECPDL_INDEX ();
3880 Lisp_Object val;
3881 struct buffer *obuf = current_buffer;
3882 ptrdiff_t begv = BEGV, zv = ZV;
3883 bool old_clip_changed = current_buffer->clip_changed;
3885 val = Vfontification_functions;
3886 specbind (Qfontification_functions, Qnil);
3888 eassert (it->end_charpos == ZV);
3890 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3891 safe_call1 (val, pos);
3892 else
3894 Lisp_Object fns, fn;
3895 struct gcpro gcpro1, gcpro2;
3897 fns = Qnil;
3898 GCPRO2 (val, fns);
3900 for (; CONSP (val); val = XCDR (val))
3902 fn = XCAR (val);
3904 if (EQ (fn, Qt))
3906 /* A value of t indicates this hook has a local
3907 binding; it means to run the global binding too.
3908 In a global value, t should not occur. If it
3909 does, we must ignore it to avoid an endless
3910 loop. */
3911 for (fns = Fdefault_value (Qfontification_functions);
3912 CONSP (fns);
3913 fns = XCDR (fns))
3915 fn = XCAR (fns);
3916 if (!EQ (fn, Qt))
3917 safe_call1 (fn, pos);
3920 else
3921 safe_call1 (fn, pos);
3924 UNGCPRO;
3927 unbind_to (count, Qnil);
3929 /* Fontification functions routinely call `save-restriction'.
3930 Normally, this tags clip_changed, which can confuse redisplay
3931 (see discussion in Bug#6671). Since we don't perform any
3932 special handling of fontification changes in the case where
3933 `save-restriction' isn't called, there's no point doing so in
3934 this case either. So, if the buffer's restrictions are
3935 actually left unchanged, reset clip_changed. */
3936 if (obuf == current_buffer)
3938 if (begv == BEGV && zv == ZV)
3939 current_buffer->clip_changed = old_clip_changed;
3941 /* There isn't much we can reasonably do to protect against
3942 misbehaving fontification, but here's a fig leaf. */
3943 else if (BUFFER_LIVE_P (obuf))
3944 set_buffer_internal_1 (obuf);
3946 /* The fontification code may have added/removed text.
3947 It could do even a lot worse, but let's at least protect against
3948 the most obvious case where only the text past `pos' gets changed',
3949 as is/was done in grep.el where some escapes sequences are turned
3950 into face properties (bug#7876). */
3951 it->end_charpos = ZV;
3953 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3954 something. This avoids an endless loop if they failed to
3955 fontify the text for which reason ever. */
3956 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3957 handled = HANDLED_RECOMPUTE_PROPS;
3960 return handled;
3965 /***********************************************************************
3966 Faces
3967 ***********************************************************************/
3969 /* Set up iterator IT from face properties at its current position.
3970 Called from handle_stop. */
3972 static enum prop_handled
3973 handle_face_prop (struct it *it)
3975 int new_face_id;
3976 ptrdiff_t next_stop;
3978 if (!STRINGP (it->string))
3980 new_face_id
3981 = face_at_buffer_position (it->w,
3982 IT_CHARPOS (*it),
3983 &next_stop,
3984 (IT_CHARPOS (*it)
3985 + TEXT_PROP_DISTANCE_LIMIT),
3986 0, it->base_face_id);
3988 /* Is this a start of a run of characters with box face?
3989 Caveat: this can be called for a freshly initialized
3990 iterator; face_id is -1 in this case. We know that the new
3991 face will not change until limit, i.e. if the new face has a
3992 box, all characters up to limit will have one. But, as
3993 usual, we don't know whether limit is really the end. */
3994 if (new_face_id != it->face_id)
3996 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3997 /* If it->face_id is -1, old_face below will be NULL, see
3998 the definition of FACE_FROM_ID. This will happen if this
3999 is the initial call that gets the face. */
4000 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4002 /* If the value of face_id of the iterator is -1, we have to
4003 look in front of IT's position and see whether there is a
4004 face there that's different from new_face_id. */
4005 if (!old_face && IT_CHARPOS (*it) > BEG)
4007 int prev_face_id = face_before_it_pos (it);
4009 old_face = FACE_FROM_ID (it->f, prev_face_id);
4012 /* If the new face has a box, but the old face does not,
4013 this is the start of a run of characters with box face,
4014 i.e. this character has a shadow on the left side. */
4015 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
4016 && (old_face == NULL || !old_face->box));
4017 it->face_box_p = new_face->box != FACE_NO_BOX;
4020 else
4022 int base_face_id;
4023 ptrdiff_t bufpos;
4024 int i;
4025 Lisp_Object from_overlay
4026 = (it->current.overlay_string_index >= 0
4027 ? it->string_overlays[it->current.overlay_string_index
4028 % OVERLAY_STRING_CHUNK_SIZE]
4029 : Qnil);
4031 /* See if we got to this string directly or indirectly from
4032 an overlay property. That includes the before-string or
4033 after-string of an overlay, strings in display properties
4034 provided by an overlay, their text properties, etc.
4036 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
4037 if (! NILP (from_overlay))
4038 for (i = it->sp - 1; i >= 0; i--)
4040 if (it->stack[i].current.overlay_string_index >= 0)
4041 from_overlay
4042 = it->string_overlays[it->stack[i].current.overlay_string_index
4043 % OVERLAY_STRING_CHUNK_SIZE];
4044 else if (! NILP (it->stack[i].from_overlay))
4045 from_overlay = it->stack[i].from_overlay;
4047 if (!NILP (from_overlay))
4048 break;
4051 if (! NILP (from_overlay))
4053 bufpos = IT_CHARPOS (*it);
4054 /* For a string from an overlay, the base face depends
4055 only on text properties and ignores overlays. */
4056 base_face_id
4057 = face_for_overlay_string (it->w,
4058 IT_CHARPOS (*it),
4059 &next_stop,
4060 (IT_CHARPOS (*it)
4061 + TEXT_PROP_DISTANCE_LIMIT),
4063 from_overlay);
4065 else
4067 bufpos = 0;
4069 /* For strings from a `display' property, use the face at
4070 IT's current buffer position as the base face to merge
4071 with, so that overlay strings appear in the same face as
4072 surrounding text, unless they specify their own faces.
4073 For strings from wrap-prefix and line-prefix properties,
4074 use the default face, possibly remapped via
4075 Vface_remapping_alist. */
4076 /* Note that the fact that we use the face at _buffer_
4077 position means that a 'display' property on an overlay
4078 string will not inherit the face of that overlay string,
4079 but will instead revert to the face of buffer text
4080 covered by the overlay. This is visible, e.g., when the
4081 overlay specifies a box face, but neither the buffer nor
4082 the display string do. This sounds like a design bug,
4083 but Emacs always did that since v21.1, so changing that
4084 might be a big deal. */
4085 base_face_id = it->string_from_prefix_prop_p
4086 ? (!NILP (Vface_remapping_alist)
4087 ? lookup_basic_face (it->f, DEFAULT_FACE_ID)
4088 : DEFAULT_FACE_ID)
4089 : underlying_face_id (it);
4092 new_face_id = face_at_string_position (it->w,
4093 it->string,
4094 IT_STRING_CHARPOS (*it),
4095 bufpos,
4096 &next_stop,
4097 base_face_id, 0);
4099 /* Is this a start of a run of characters with box? Caveat:
4100 this can be called for a freshly allocated iterator; face_id
4101 is -1 is this case. We know that the new face will not
4102 change until the next check pos, i.e. if the new face has a
4103 box, all characters up to that position will have a
4104 box. But, as usual, we don't know whether that position
4105 is really the end. */
4106 if (new_face_id != it->face_id)
4108 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
4109 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
4111 /* If new face has a box but old face hasn't, this is the
4112 start of a run of characters with box, i.e. it has a
4113 shadow on the left side. */
4114 it->start_of_box_run_p
4115 = new_face->box && (old_face == NULL || !old_face->box);
4116 it->face_box_p = new_face->box != FACE_NO_BOX;
4120 it->face_id = new_face_id;
4121 return HANDLED_NORMALLY;
4125 /* Return the ID of the face ``underlying'' IT's current position,
4126 which is in a string. If the iterator is associated with a
4127 buffer, return the face at IT's current buffer position.
4128 Otherwise, use the iterator's base_face_id. */
4130 static int
4131 underlying_face_id (struct it *it)
4133 int face_id = it->base_face_id, i;
4135 eassert (STRINGP (it->string));
4137 for (i = it->sp - 1; i >= 0; --i)
4138 if (NILP (it->stack[i].string))
4139 face_id = it->stack[i].face_id;
4141 return face_id;
4145 /* Compute the face one character before or after the current position
4146 of IT, in the visual order. BEFORE_P non-zero means get the face
4147 in front (to the left in L2R paragraphs, to the right in R2L
4148 paragraphs) of IT's screen position. Value is the ID of the face. */
4150 static int
4151 face_before_or_after_it_pos (struct it *it, int before_p)
4153 int face_id, limit;
4154 ptrdiff_t next_check_charpos;
4155 struct it it_copy;
4156 void *it_copy_data = NULL;
4158 eassert (it->s == NULL);
4160 if (STRINGP (it->string))
4162 ptrdiff_t bufpos, charpos;
4163 int base_face_id;
4165 /* No face change past the end of the string (for the case
4166 we are padding with spaces). No face change before the
4167 string start. */
4168 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
4169 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
4170 return it->face_id;
4172 if (!it->bidi_p)
4174 /* Set charpos to the position before or after IT's current
4175 position, in the logical order, which in the non-bidi
4176 case is the same as the visual order. */
4177 if (before_p)
4178 charpos = IT_STRING_CHARPOS (*it) - 1;
4179 else if (it->what == IT_COMPOSITION)
4180 /* For composition, we must check the character after the
4181 composition. */
4182 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4183 else
4184 charpos = IT_STRING_CHARPOS (*it) + 1;
4186 else
4188 if (before_p)
4190 /* With bidi iteration, the character before the current
4191 in the visual order cannot be found by simple
4192 iteration, because "reverse" reordering is not
4193 supported. Instead, we need to use the move_it_*
4194 family of functions. */
4195 /* Ignore face changes before the first visible
4196 character on this display line. */
4197 if (it->current_x <= it->first_visible_x)
4198 return it->face_id;
4199 SAVE_IT (it_copy, *it, it_copy_data);
4200 /* Implementation note: Since move_it_in_display_line
4201 works in the iterator geometry, and thinks the first
4202 character is always the leftmost, even in R2L lines,
4203 we don't need to distinguish between the R2L and L2R
4204 cases here. */
4205 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4206 it_copy.current_x - 1, MOVE_TO_X);
4207 charpos = IT_STRING_CHARPOS (it_copy);
4208 RESTORE_IT (it, it, it_copy_data);
4210 else
4212 /* Set charpos to the string position of the character
4213 that comes after IT's current position in the visual
4214 order. */
4215 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4217 it_copy = *it;
4218 while (n--)
4219 bidi_move_to_visually_next (&it_copy.bidi_it);
4221 charpos = it_copy.bidi_it.charpos;
4224 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4226 if (it->current.overlay_string_index >= 0)
4227 bufpos = IT_CHARPOS (*it);
4228 else
4229 bufpos = 0;
4231 base_face_id = underlying_face_id (it);
4233 /* Get the face for ASCII, or unibyte. */
4234 face_id = face_at_string_position (it->w,
4235 it->string,
4236 charpos,
4237 bufpos,
4238 &next_check_charpos,
4239 base_face_id, 0);
4241 /* Correct the face for charsets different from ASCII. Do it
4242 for the multibyte case only. The face returned above is
4243 suitable for unibyte text if IT->string is unibyte. */
4244 if (STRING_MULTIBYTE (it->string))
4246 struct text_pos pos1 = string_pos (charpos, it->string);
4247 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4248 int c, len;
4249 struct face *face = FACE_FROM_ID (it->f, face_id);
4251 c = string_char_and_length (p, &len);
4252 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4255 else
4257 struct text_pos pos;
4259 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4260 || (IT_CHARPOS (*it) <= BEGV && before_p))
4261 return it->face_id;
4263 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4264 pos = it->current.pos;
4266 if (!it->bidi_p)
4268 if (before_p)
4269 DEC_TEXT_POS (pos, it->multibyte_p);
4270 else
4272 if (it->what == IT_COMPOSITION)
4274 /* For composition, we must check the position after
4275 the composition. */
4276 pos.charpos += it->cmp_it.nchars;
4277 pos.bytepos += it->len;
4279 else
4280 INC_TEXT_POS (pos, it->multibyte_p);
4283 else
4285 if (before_p)
4287 /* With bidi iteration, the character before the current
4288 in the visual order cannot be found by simple
4289 iteration, because "reverse" reordering is not
4290 supported. Instead, we need to use the move_it_*
4291 family of functions. */
4292 /* Ignore face changes before the first visible
4293 character on this display line. */
4294 if (it->current_x <= it->first_visible_x)
4295 return it->face_id;
4296 SAVE_IT (it_copy, *it, it_copy_data);
4297 /* Implementation note: Since move_it_in_display_line
4298 works in the iterator geometry, and thinks the first
4299 character is always the leftmost, even in R2L lines,
4300 we don't need to distinguish between the R2L and L2R
4301 cases here. */
4302 move_it_in_display_line (&it_copy, ZV,
4303 it_copy.current_x - 1, MOVE_TO_X);
4304 pos = it_copy.current.pos;
4305 RESTORE_IT (it, it, it_copy_data);
4307 else
4309 /* Set charpos to the buffer position of the character
4310 that comes after IT's current position in the visual
4311 order. */
4312 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4314 it_copy = *it;
4315 while (n--)
4316 bidi_move_to_visually_next (&it_copy.bidi_it);
4318 SET_TEXT_POS (pos,
4319 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4322 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4324 /* Determine face for CHARSET_ASCII, or unibyte. */
4325 face_id = face_at_buffer_position (it->w,
4326 CHARPOS (pos),
4327 &next_check_charpos,
4328 limit, 0, -1);
4330 /* Correct the face for charsets different from ASCII. Do it
4331 for the multibyte case only. The face returned above is
4332 suitable for unibyte text if current_buffer is unibyte. */
4333 if (it->multibyte_p)
4335 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4336 struct face *face = FACE_FROM_ID (it->f, face_id);
4337 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4341 return face_id;
4346 /***********************************************************************
4347 Invisible text
4348 ***********************************************************************/
4350 /* Set up iterator IT from invisible properties at its current
4351 position. Called from handle_stop. */
4353 static enum prop_handled
4354 handle_invisible_prop (struct it *it)
4356 enum prop_handled handled = HANDLED_NORMALLY;
4357 int invis_p;
4358 Lisp_Object prop;
4360 if (STRINGP (it->string))
4362 Lisp_Object end_charpos, limit, charpos;
4364 /* Get the value of the invisible text property at the
4365 current position. Value will be nil if there is no such
4366 property. */
4367 charpos = make_number (IT_STRING_CHARPOS (*it));
4368 prop = Fget_text_property (charpos, Qinvisible, it->string);
4369 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4371 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4373 /* Record whether we have to display an ellipsis for the
4374 invisible text. */
4375 int display_ellipsis_p = (invis_p == 2);
4376 ptrdiff_t len, endpos;
4378 handled = HANDLED_RECOMPUTE_PROPS;
4380 /* Get the position at which the next visible text can be
4381 found in IT->string, if any. */
4382 endpos = len = SCHARS (it->string);
4383 XSETINT (limit, len);
4386 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4387 it->string, limit);
4388 if (INTEGERP (end_charpos))
4390 endpos = XFASTINT (end_charpos);
4391 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4392 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4393 if (invis_p == 2)
4394 display_ellipsis_p = true;
4397 while (invis_p && endpos < len);
4399 if (display_ellipsis_p)
4400 it->ellipsis_p = true;
4402 if (endpos < len)
4404 /* Text at END_CHARPOS is visible. Move IT there. */
4405 struct text_pos old;
4406 ptrdiff_t oldpos;
4408 old = it->current.string_pos;
4409 oldpos = CHARPOS (old);
4410 if (it->bidi_p)
4412 if (it->bidi_it.first_elt
4413 && it->bidi_it.charpos < SCHARS (it->string))
4414 bidi_paragraph_init (it->paragraph_embedding,
4415 &it->bidi_it, 1);
4416 /* Bidi-iterate out of the invisible text. */
4419 bidi_move_to_visually_next (&it->bidi_it);
4421 while (oldpos <= it->bidi_it.charpos
4422 && it->bidi_it.charpos < endpos);
4424 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4425 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4426 if (IT_CHARPOS (*it) >= endpos)
4427 it->prev_stop = endpos;
4429 else
4431 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4432 compute_string_pos (&it->current.string_pos, old, it->string);
4435 else
4437 /* The rest of the string is invisible. If this is an
4438 overlay string, proceed with the next overlay string
4439 or whatever comes and return a character from there. */
4440 if (it->current.overlay_string_index >= 0
4441 && !display_ellipsis_p)
4443 next_overlay_string (it);
4444 /* Don't check for overlay strings when we just
4445 finished processing them. */
4446 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4448 else
4450 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4451 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4456 else
4458 ptrdiff_t newpos, next_stop, start_charpos, tem;
4459 Lisp_Object pos, overlay;
4461 /* First of all, is there invisible text at this position? */
4462 tem = start_charpos = IT_CHARPOS (*it);
4463 pos = make_number (tem);
4464 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4465 &overlay);
4466 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4468 /* If we are on invisible text, skip over it. */
4469 if (invis_p && start_charpos < it->end_charpos)
4471 /* Record whether we have to display an ellipsis for the
4472 invisible text. */
4473 int display_ellipsis_p = invis_p == 2;
4475 handled = HANDLED_RECOMPUTE_PROPS;
4477 /* Loop skipping over invisible text. The loop is left at
4478 ZV or with IT on the first char being visible again. */
4481 /* Try to skip some invisible text. Return value is the
4482 position reached which can be equal to where we start
4483 if there is nothing invisible there. This skips both
4484 over invisible text properties and overlays with
4485 invisible property. */
4486 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4488 /* If we skipped nothing at all we weren't at invisible
4489 text in the first place. If everything to the end of
4490 the buffer was skipped, end the loop. */
4491 if (newpos == tem || newpos >= ZV)
4492 invis_p = 0;
4493 else
4495 /* We skipped some characters but not necessarily
4496 all there are. Check if we ended up on visible
4497 text. Fget_char_property returns the property of
4498 the char before the given position, i.e. if we
4499 get invis_p = 0, this means that the char at
4500 newpos is visible. */
4501 pos = make_number (newpos);
4502 prop = Fget_char_property (pos, Qinvisible, it->window);
4503 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4506 /* If we ended up on invisible text, proceed to
4507 skip starting with next_stop. */
4508 if (invis_p)
4509 tem = next_stop;
4511 /* If there are adjacent invisible texts, don't lose the
4512 second one's ellipsis. */
4513 if (invis_p == 2)
4514 display_ellipsis_p = true;
4516 while (invis_p);
4518 /* The position newpos is now either ZV or on visible text. */
4519 if (it->bidi_p)
4521 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4522 int on_newline
4523 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4524 int after_newline
4525 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4527 /* If the invisible text ends on a newline or on a
4528 character after a newline, we can avoid the costly,
4529 character by character, bidi iteration to NEWPOS, and
4530 instead simply reseat the iterator there. That's
4531 because all bidi reordering information is tossed at
4532 the newline. This is a big win for modes that hide
4533 complete lines, like Outline, Org, etc. */
4534 if (on_newline || after_newline)
4536 struct text_pos tpos;
4537 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4539 SET_TEXT_POS (tpos, newpos, bpos);
4540 reseat_1 (it, tpos, 0);
4541 /* If we reseat on a newline/ZV, we need to prep the
4542 bidi iterator for advancing to the next character
4543 after the newline/EOB, keeping the current paragraph
4544 direction (so that PRODUCE_GLYPHS does TRT wrt
4545 prepending/appending glyphs to a glyph row). */
4546 if (on_newline)
4548 it->bidi_it.first_elt = 0;
4549 it->bidi_it.paragraph_dir = pdir;
4550 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4551 it->bidi_it.nchars = 1;
4552 it->bidi_it.ch_len = 1;
4555 else /* Must use the slow method. */
4557 /* With bidi iteration, the region of invisible text
4558 could start and/or end in the middle of a
4559 non-base embedding level. Therefore, we need to
4560 skip invisible text using the bidi iterator,
4561 starting at IT's current position, until we find
4562 ourselves outside of the invisible text.
4563 Skipping invisible text _after_ bidi iteration
4564 avoids affecting the visual order of the
4565 displayed text when invisible properties are
4566 added or removed. */
4567 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4569 /* If we were `reseat'ed to a new paragraph,
4570 determine the paragraph base direction. We
4571 need to do it now because
4572 next_element_from_buffer may not have a
4573 chance to do it, if we are going to skip any
4574 text at the beginning, which resets the
4575 FIRST_ELT flag. */
4576 bidi_paragraph_init (it->paragraph_embedding,
4577 &it->bidi_it, 1);
4581 bidi_move_to_visually_next (&it->bidi_it);
4583 while (it->stop_charpos <= it->bidi_it.charpos
4584 && it->bidi_it.charpos < newpos);
4585 IT_CHARPOS (*it) = it->bidi_it.charpos;
4586 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4587 /* If we overstepped NEWPOS, record its position in
4588 the iterator, so that we skip invisible text if
4589 later the bidi iteration lands us in the
4590 invisible region again. */
4591 if (IT_CHARPOS (*it) >= newpos)
4592 it->prev_stop = newpos;
4595 else
4597 IT_CHARPOS (*it) = newpos;
4598 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4601 /* If there are before-strings at the start of invisible
4602 text, and the text is invisible because of a text
4603 property, arrange to show before-strings because 20.x did
4604 it that way. (If the text is invisible because of an
4605 overlay property instead of a text property, this is
4606 already handled in the overlay code.) */
4607 if (NILP (overlay)
4608 && get_overlay_strings (it, it->stop_charpos))
4610 handled = HANDLED_RECOMPUTE_PROPS;
4611 if (it->sp > 0)
4613 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4614 /* The call to get_overlay_strings above recomputes
4615 it->stop_charpos, but it only considers changes
4616 in properties and overlays beyond iterator's
4617 current position. This causes us to miss changes
4618 that happen exactly where the invisible property
4619 ended. So we play it safe here and force the
4620 iterator to check for potential stop positions
4621 immediately after the invisible text. Note that
4622 if get_overlay_strings returns non-zero, it
4623 normally also pushed the iterator stack, so we
4624 need to update the stop position in the slot
4625 below the current one. */
4626 it->stack[it->sp - 1].stop_charpos
4627 = CHARPOS (it->stack[it->sp - 1].current.pos);
4630 else if (display_ellipsis_p)
4632 /* Make sure that the glyphs of the ellipsis will get
4633 correct `charpos' values. If we would not update
4634 it->position here, the glyphs would belong to the
4635 last visible character _before_ the invisible
4636 text, which confuses `set_cursor_from_row'.
4638 We use the last invisible position instead of the
4639 first because this way the cursor is always drawn on
4640 the first "." of the ellipsis, whenever PT is inside
4641 the invisible text. Otherwise the cursor would be
4642 placed _after_ the ellipsis when the point is after the
4643 first invisible character. */
4644 if (!STRINGP (it->object))
4646 it->position.charpos = newpos - 1;
4647 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4649 it->ellipsis_p = true;
4650 /* Let the ellipsis display before
4651 considering any properties of the following char.
4652 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4653 handled = HANDLED_RETURN;
4658 return handled;
4662 /* Make iterator IT return `...' next.
4663 Replaces LEN characters from buffer. */
4665 static void
4666 setup_for_ellipsis (struct it *it, int len)
4668 /* Use the display table definition for `...'. Invalid glyphs
4669 will be handled by the method returning elements from dpvec. */
4670 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4672 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4673 it->dpvec = v->contents;
4674 it->dpend = v->contents + v->header.size;
4676 else
4678 /* Default `...'. */
4679 it->dpvec = default_invis_vector;
4680 it->dpend = default_invis_vector + 3;
4683 it->dpvec_char_len = len;
4684 it->current.dpvec_index = 0;
4685 it->dpvec_face_id = -1;
4687 /* Remember the current face id in case glyphs specify faces.
4688 IT's face is restored in set_iterator_to_next.
4689 saved_face_id was set to preceding char's face in handle_stop. */
4690 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4691 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4693 it->method = GET_FROM_DISPLAY_VECTOR;
4694 it->ellipsis_p = true;
4699 /***********************************************************************
4700 'display' property
4701 ***********************************************************************/
4703 /* Set up iterator IT from `display' property at its current position.
4704 Called from handle_stop.
4705 We return HANDLED_RETURN if some part of the display property
4706 overrides the display of the buffer text itself.
4707 Otherwise we return HANDLED_NORMALLY. */
4709 static enum prop_handled
4710 handle_display_prop (struct it *it)
4712 Lisp_Object propval, object, overlay;
4713 struct text_pos *position;
4714 ptrdiff_t bufpos;
4715 /* Nonzero if some property replaces the display of the text itself. */
4716 int display_replaced_p = 0;
4718 if (STRINGP (it->string))
4720 object = it->string;
4721 position = &it->current.string_pos;
4722 bufpos = CHARPOS (it->current.pos);
4724 else
4726 XSETWINDOW (object, it->w);
4727 position = &it->current.pos;
4728 bufpos = CHARPOS (*position);
4731 /* Reset those iterator values set from display property values. */
4732 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4733 it->space_width = Qnil;
4734 it->font_height = Qnil;
4735 it->voffset = 0;
4737 /* We don't support recursive `display' properties, i.e. string
4738 values that have a string `display' property, that have a string
4739 `display' property etc. */
4740 if (!it->string_from_display_prop_p)
4741 it->area = TEXT_AREA;
4743 propval = get_char_property_and_overlay (make_number (position->charpos),
4744 Qdisplay, object, &overlay);
4745 if (NILP (propval))
4746 return HANDLED_NORMALLY;
4747 /* Now OVERLAY is the overlay that gave us this property, or nil
4748 if it was a text property. */
4750 if (!STRINGP (it->string))
4751 object = it->w->contents;
4753 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4754 position, bufpos,
4755 FRAME_WINDOW_P (it->f));
4757 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4760 /* Subroutine of handle_display_prop. Returns non-zero if the display
4761 specification in SPEC is a replacing specification, i.e. it would
4762 replace the text covered by `display' property with something else,
4763 such as an image or a display string. If SPEC includes any kind or
4764 `(space ...) specification, the value is 2; this is used by
4765 compute_display_string_pos, which see.
4767 See handle_single_display_spec for documentation of arguments.
4768 frame_window_p is non-zero if the window being redisplayed is on a
4769 GUI frame; this argument is used only if IT is NULL, see below.
4771 IT can be NULL, if this is called by the bidi reordering code
4772 through compute_display_string_pos, which see. In that case, this
4773 function only examines SPEC, but does not otherwise "handle" it, in
4774 the sense that it doesn't set up members of IT from the display
4775 spec. */
4776 static int
4777 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4778 Lisp_Object overlay, struct text_pos *position,
4779 ptrdiff_t bufpos, int frame_window_p)
4781 int replacing_p = 0;
4782 int rv;
4784 if (CONSP (spec)
4785 /* Simple specifications. */
4786 && !EQ (XCAR (spec), Qimage)
4787 #ifdef HAVE_XWIDGETS
4788 && !EQ (XCAR (spec), Qxwidget)
4789 #endif
4790 && !EQ (XCAR (spec), Qspace)
4791 && !EQ (XCAR (spec), Qwhen)
4792 && !EQ (XCAR (spec), Qslice)
4793 && !EQ (XCAR (spec), Qspace_width)
4794 && !EQ (XCAR (spec), Qheight)
4795 && !EQ (XCAR (spec), Qraise)
4796 /* Marginal area specifications. */
4797 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4798 && !EQ (XCAR (spec), Qleft_fringe)
4799 && !EQ (XCAR (spec), Qright_fringe)
4800 && !NILP (XCAR (spec)))
4802 for (; CONSP (spec); spec = XCDR (spec))
4804 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4805 overlay, position, bufpos,
4806 replacing_p, frame_window_p)))
4808 replacing_p = rv;
4809 /* If some text in a string is replaced, `position' no
4810 longer points to the position of `object'. */
4811 if (!it || STRINGP (object))
4812 break;
4816 else if (VECTORP (spec))
4818 ptrdiff_t i;
4819 for (i = 0; i < ASIZE (spec); ++i)
4820 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4821 overlay, position, bufpos,
4822 replacing_p, frame_window_p)))
4824 replacing_p = rv;
4825 /* If some text in a string is replaced, `position' no
4826 longer points to the position of `object'. */
4827 if (!it || STRINGP (object))
4828 break;
4831 else
4833 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4834 position, bufpos, 0,
4835 frame_window_p)))
4836 replacing_p = rv;
4839 return replacing_p;
4842 /* Value is the position of the end of the `display' property starting
4843 at START_POS in OBJECT. */
4845 static struct text_pos
4846 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4848 Lisp_Object end;
4849 struct text_pos end_pos;
4851 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4852 Qdisplay, object, Qnil);
4853 CHARPOS (end_pos) = XFASTINT (end);
4854 if (STRINGP (object))
4855 compute_string_pos (&end_pos, start_pos, it->string);
4856 else
4857 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4859 return end_pos;
4863 /* Set up IT from a single `display' property specification SPEC. OBJECT
4864 is the object in which the `display' property was found. *POSITION
4865 is the position in OBJECT at which the `display' property was found.
4866 BUFPOS is the buffer position of OBJECT (different from POSITION if
4867 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4868 previously saw a display specification which already replaced text
4869 display with something else, for example an image; we ignore such
4870 properties after the first one has been processed.
4872 OVERLAY is the overlay this `display' property came from,
4873 or nil if it was a text property.
4875 If SPEC is a `space' or `image' specification, and in some other
4876 cases too, set *POSITION to the position where the `display'
4877 property ends.
4879 If IT is NULL, only examine the property specification in SPEC, but
4880 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4881 is intended to be displayed in a window on a GUI frame.
4883 Value is non-zero if something was found which replaces the display
4884 of buffer or string text. */
4886 static int
4887 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4888 Lisp_Object overlay, struct text_pos *position,
4889 ptrdiff_t bufpos, int display_replaced_p,
4890 int frame_window_p)
4892 Lisp_Object form;
4893 Lisp_Object location, value;
4894 struct text_pos start_pos = *position;
4895 int valid_p;
4897 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4898 If the result is non-nil, use VALUE instead of SPEC. */
4899 form = Qt;
4900 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4902 spec = XCDR (spec);
4903 if (!CONSP (spec))
4904 return 0;
4905 form = XCAR (spec);
4906 spec = XCDR (spec);
4909 if (!NILP (form) && !EQ (form, Qt))
4911 ptrdiff_t count = SPECPDL_INDEX ();
4912 struct gcpro gcpro1;
4914 /* Bind `object' to the object having the `display' property, a
4915 buffer or string. Bind `position' to the position in the
4916 object where the property was found, and `buffer-position'
4917 to the current position in the buffer. */
4919 if (NILP (object))
4920 XSETBUFFER (object, current_buffer);
4921 specbind (Qobject, object);
4922 specbind (Qposition, make_number (CHARPOS (*position)));
4923 specbind (Qbuffer_position, make_number (bufpos));
4924 GCPRO1 (form);
4925 form = safe_eval (form);
4926 UNGCPRO;
4927 unbind_to (count, Qnil);
4930 if (NILP (form))
4931 return 0;
4933 /* Handle `(height HEIGHT)' specifications. */
4934 if (CONSP (spec)
4935 && EQ (XCAR (spec), Qheight)
4936 && CONSP (XCDR (spec)))
4938 if (it)
4940 if (!FRAME_WINDOW_P (it->f))
4941 return 0;
4943 it->font_height = XCAR (XCDR (spec));
4944 if (!NILP (it->font_height))
4946 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4947 int new_height = -1;
4949 if (CONSP (it->font_height)
4950 && (EQ (XCAR (it->font_height), Qplus)
4951 || EQ (XCAR (it->font_height), Qminus))
4952 && CONSP (XCDR (it->font_height))
4953 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4955 /* `(+ N)' or `(- N)' where N is an integer. */
4956 int steps = XINT (XCAR (XCDR (it->font_height)));
4957 if (EQ (XCAR (it->font_height), Qplus))
4958 steps = - steps;
4959 it->face_id = smaller_face (it->f, it->face_id, steps);
4961 else if (FUNCTIONP (it->font_height))
4963 /* Call function with current height as argument.
4964 Value is the new height. */
4965 Lisp_Object height;
4966 height = safe_call1 (it->font_height,
4967 face->lface[LFACE_HEIGHT_INDEX]);
4968 if (NUMBERP (height))
4969 new_height = XFLOATINT (height);
4971 else if (NUMBERP (it->font_height))
4973 /* Value is a multiple of the canonical char height. */
4974 struct face *f;
4976 f = FACE_FROM_ID (it->f,
4977 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4978 new_height = (XFLOATINT (it->font_height)
4979 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4981 else
4983 /* Evaluate IT->font_height with `height' bound to the
4984 current specified height to get the new height. */
4985 ptrdiff_t count = SPECPDL_INDEX ();
4987 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4988 value = safe_eval (it->font_height);
4989 unbind_to (count, Qnil);
4991 if (NUMBERP (value))
4992 new_height = XFLOATINT (value);
4995 if (new_height > 0)
4996 it->face_id = face_with_height (it->f, it->face_id, new_height);
5000 return 0;
5003 /* Handle `(space-width WIDTH)'. */
5004 if (CONSP (spec)
5005 && EQ (XCAR (spec), Qspace_width)
5006 && CONSP (XCDR (spec)))
5008 if (it)
5010 if (!FRAME_WINDOW_P (it->f))
5011 return 0;
5013 value = XCAR (XCDR (spec));
5014 if (NUMBERP (value) && XFLOATINT (value) > 0)
5015 it->space_width = value;
5018 return 0;
5021 /* Handle `(slice X Y WIDTH HEIGHT)'. */
5022 if (CONSP (spec)
5023 && EQ (XCAR (spec), Qslice))
5025 Lisp_Object tem;
5027 if (it)
5029 if (!FRAME_WINDOW_P (it->f))
5030 return 0;
5032 if (tem = XCDR (spec), CONSP (tem))
5034 it->slice.x = XCAR (tem);
5035 if (tem = XCDR (tem), CONSP (tem))
5037 it->slice.y = XCAR (tem);
5038 if (tem = XCDR (tem), CONSP (tem))
5040 it->slice.width = XCAR (tem);
5041 if (tem = XCDR (tem), CONSP (tem))
5042 it->slice.height = XCAR (tem);
5048 return 0;
5051 /* Handle `(raise FACTOR)'. */
5052 if (CONSP (spec)
5053 && EQ (XCAR (spec), Qraise)
5054 && CONSP (XCDR (spec)))
5056 if (it)
5058 if (!FRAME_WINDOW_P (it->f))
5059 return 0;
5061 #ifdef HAVE_WINDOW_SYSTEM
5062 value = XCAR (XCDR (spec));
5063 if (NUMBERP (value))
5065 struct face *face = FACE_FROM_ID (it->f, it->face_id);
5066 it->voffset = - (XFLOATINT (value)
5067 * (FONT_HEIGHT (face->font)));
5069 #endif /* HAVE_WINDOW_SYSTEM */
5072 return 0;
5075 /* Don't handle the other kinds of display specifications
5076 inside a string that we got from a `display' property. */
5077 if (it && it->string_from_display_prop_p)
5078 return 0;
5080 /* Characters having this form of property are not displayed, so
5081 we have to find the end of the property. */
5082 if (it)
5084 start_pos = *position;
5085 *position = display_prop_end (it, object, start_pos);
5087 value = Qnil;
5089 /* Stop the scan at that end position--we assume that all
5090 text properties change there. */
5091 if (it)
5092 it->stop_charpos = position->charpos;
5094 /* Handle `(left-fringe BITMAP [FACE])'
5095 and `(right-fringe BITMAP [FACE])'. */
5096 if (CONSP (spec)
5097 && (EQ (XCAR (spec), Qleft_fringe)
5098 || EQ (XCAR (spec), Qright_fringe))
5099 && CONSP (XCDR (spec)))
5101 int fringe_bitmap;
5103 if (it)
5105 if (!FRAME_WINDOW_P (it->f))
5106 /* If we return here, POSITION has been advanced
5107 across the text with this property. */
5109 /* Synchronize the bidi iterator with POSITION. This is
5110 needed because we are not going to push the iterator
5111 on behalf of this display property, so there will be
5112 no pop_it call to do this synchronization for us. */
5113 if (it->bidi_p)
5115 it->position = *position;
5116 iterate_out_of_display_property (it);
5117 *position = it->position;
5119 /* If we were to display this fringe bitmap,
5120 next_element_from_image would have reset this flag.
5121 Do the same, to avoid affecting overlays that
5122 follow. */
5123 it->ignore_overlay_strings_at_pos_p = 0;
5124 return 1;
5127 else if (!frame_window_p)
5128 return 1;
5130 #ifdef HAVE_WINDOW_SYSTEM
5131 value = XCAR (XCDR (spec));
5132 if (!SYMBOLP (value)
5133 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
5134 /* If we return here, POSITION has been advanced
5135 across the text with this property. */
5137 if (it && it->bidi_p)
5139 it->position = *position;
5140 iterate_out_of_display_property (it);
5141 *position = it->position;
5143 if (it)
5144 /* Reset this flag like next_element_from_image would. */
5145 it->ignore_overlay_strings_at_pos_p = 0;
5146 return 1;
5149 if (it)
5151 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
5153 if (CONSP (XCDR (XCDR (spec))))
5155 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
5156 int face_id2 = lookup_derived_face (it->f, face_name,
5157 FRINGE_FACE_ID, 0);
5158 if (face_id2 >= 0)
5159 face_id = face_id2;
5162 /* Save current settings of IT so that we can restore them
5163 when we are finished with the glyph property value. */
5164 push_it (it, position);
5166 it->area = TEXT_AREA;
5167 it->what = IT_IMAGE;
5168 it->image_id = -1; /* no image */
5169 it->position = start_pos;
5170 it->object = NILP (object) ? it->w->contents : object;
5171 it->method = GET_FROM_IMAGE;
5172 it->from_overlay = Qnil;
5173 it->face_id = face_id;
5174 it->from_disp_prop_p = true;
5176 /* Say that we haven't consumed the characters with
5177 `display' property yet. The call to pop_it in
5178 set_iterator_to_next will clean this up. */
5179 *position = start_pos;
5181 if (EQ (XCAR (spec), Qleft_fringe))
5183 it->left_user_fringe_bitmap = fringe_bitmap;
5184 it->left_user_fringe_face_id = face_id;
5186 else
5188 it->right_user_fringe_bitmap = fringe_bitmap;
5189 it->right_user_fringe_face_id = face_id;
5192 #endif /* HAVE_WINDOW_SYSTEM */
5193 return 1;
5196 /* Prepare to handle `((margin left-margin) ...)',
5197 `((margin right-margin) ...)' and `((margin nil) ...)'
5198 prefixes for display specifications. */
5199 location = Qunbound;
5200 if (CONSP (spec) && CONSP (XCAR (spec)))
5202 Lisp_Object tem;
5204 value = XCDR (spec);
5205 if (CONSP (value))
5206 value = XCAR (value);
5208 tem = XCAR (spec);
5209 if (EQ (XCAR (tem), Qmargin)
5210 && (tem = XCDR (tem),
5211 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5212 (NILP (tem)
5213 || EQ (tem, Qleft_margin)
5214 || EQ (tem, Qright_margin))))
5215 location = tem;
5218 if (EQ (location, Qunbound))
5220 location = Qnil;
5221 value = spec;
5224 /* After this point, VALUE is the property after any
5225 margin prefix has been stripped. It must be a string,
5226 an image specification, or `(space ...)'.
5228 LOCATION specifies where to display: `left-margin',
5229 `right-margin' or nil. */
5231 valid_p = (STRINGP (value)
5232 #ifdef HAVE_WINDOW_SYSTEM
5233 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5234 && valid_image_p (value))
5235 #endif /* not HAVE_WINDOW_SYSTEM */
5236 || (CONSP (value) && EQ (XCAR (value), Qspace))
5237 #ifdef HAVE_XWIDGETS
5238 || valid_xwidget_spec_p(value)
5239 #endif
5242 if (valid_p && !display_replaced_p)
5244 int retval = 1;
5246 if (!it)
5248 /* Callers need to know whether the display spec is any kind
5249 of `(space ...)' spec that is about to affect text-area
5250 display. */
5251 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5252 retval = 2;
5253 return retval;
5256 /* Save current settings of IT so that we can restore them
5257 when we are finished with the glyph property value. */
5258 push_it (it, position);
5259 it->from_overlay = overlay;
5260 it->from_disp_prop_p = true;
5262 if (NILP (location))
5263 it->area = TEXT_AREA;
5264 else if (EQ (location, Qleft_margin))
5265 it->area = LEFT_MARGIN_AREA;
5266 else
5267 it->area = RIGHT_MARGIN_AREA;
5269 if (STRINGP (value))
5271 it->string = value;
5272 it->multibyte_p = STRING_MULTIBYTE (it->string);
5273 it->current.overlay_string_index = -1;
5274 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5275 it->end_charpos = it->string_nchars = SCHARS (it->string);
5276 it->method = GET_FROM_STRING;
5277 it->stop_charpos = 0;
5278 it->prev_stop = 0;
5279 it->base_level_stop = 0;
5280 it->string_from_display_prop_p = true;
5281 /* Say that we haven't consumed the characters with
5282 `display' property yet. The call to pop_it in
5283 set_iterator_to_next will clean this up. */
5284 if (BUFFERP (object))
5285 *position = start_pos;
5287 /* Force paragraph direction to be that of the parent
5288 object. If the parent object's paragraph direction is
5289 not yet determined, default to L2R. */
5290 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5291 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5292 else
5293 it->paragraph_embedding = L2R;
5295 /* Set up the bidi iterator for this display string. */
5296 if (it->bidi_p)
5298 it->bidi_it.string.lstring = it->string;
5299 it->bidi_it.string.s = NULL;
5300 it->bidi_it.string.schars = it->end_charpos;
5301 it->bidi_it.string.bufpos = bufpos;
5302 it->bidi_it.string.from_disp_str = 1;
5303 it->bidi_it.string.unibyte = !it->multibyte_p;
5304 it->bidi_it.w = it->w;
5305 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5308 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5310 it->method = GET_FROM_STRETCH;
5311 it->object = value;
5312 *position = it->position = start_pos;
5313 retval = 1 + (it->area == TEXT_AREA);
5315 #ifdef HAVE_XWIDGETS
5316 else if (valid_xwidget_spec_p(value))
5318 //printf("handle_single_display_spec: im an xwidget!!\n");
5319 it->what = IT_XWIDGET;
5320 it->method = GET_FROM_XWIDGET;
5321 it->position = start_pos;
5322 it->object = NILP (object) ? it->w->contents : object;
5323 *position = start_pos;
5325 it->xwidget = lookup_xwidget(value);
5327 #endif
5328 #ifdef HAVE_WINDOW_SYSTEM
5329 else
5331 it->what = IT_IMAGE;
5332 it->image_id = lookup_image (it->f, value);
5333 it->position = start_pos;
5334 it->object = NILP (object) ? it->w->contents : object;
5335 it->method = GET_FROM_IMAGE;
5337 /* Say that we haven't consumed the characters with
5338 `display' property yet. The call to pop_it in
5339 set_iterator_to_next will clean this up. */
5340 *position = start_pos;
5342 #endif /* HAVE_WINDOW_SYSTEM */
5344 return retval;
5347 /* Invalid property or property not supported. Restore
5348 POSITION to what it was before. */
5349 *position = start_pos;
5350 return 0;
5353 /* Check if PROP is a display property value whose text should be
5354 treated as intangible. OVERLAY is the overlay from which PROP
5355 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5356 specify the buffer position covered by PROP. */
5359 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5360 ptrdiff_t charpos, ptrdiff_t bytepos)
5362 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5363 struct text_pos position;
5365 SET_TEXT_POS (position, charpos, bytepos);
5366 return handle_display_spec (NULL, prop, Qnil, overlay,
5367 &position, charpos, frame_window_p);
5371 /* Return 1 if PROP is a display sub-property value containing STRING.
5373 Implementation note: this and the following function are really
5374 special cases of handle_display_spec and
5375 handle_single_display_spec, and should ideally use the same code.
5376 Until they do, these two pairs must be consistent and must be
5377 modified in sync. */
5379 static int
5380 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5382 if (EQ (string, prop))
5383 return 1;
5385 /* Skip over `when FORM'. */
5386 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5388 prop = XCDR (prop);
5389 if (!CONSP (prop))
5390 return 0;
5391 /* Actually, the condition following `when' should be eval'ed,
5392 like handle_single_display_spec does, and we should return
5393 zero if it evaluates to nil. However, this function is
5394 called only when the buffer was already displayed and some
5395 glyph in the glyph matrix was found to come from a display
5396 string. Therefore, the condition was already evaluated, and
5397 the result was non-nil, otherwise the display string wouldn't
5398 have been displayed and we would have never been called for
5399 this property. Thus, we can skip the evaluation and assume
5400 its result is non-nil. */
5401 prop = XCDR (prop);
5404 if (CONSP (prop))
5405 /* Skip over `margin LOCATION'. */
5406 if (EQ (XCAR (prop), Qmargin))
5408 prop = XCDR (prop);
5409 if (!CONSP (prop))
5410 return 0;
5412 prop = XCDR (prop);
5413 if (!CONSP (prop))
5414 return 0;
5417 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5421 /* Return 1 if STRING appears in the `display' property PROP. */
5423 static int
5424 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5426 if (CONSP (prop)
5427 && !EQ (XCAR (prop), Qwhen)
5428 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5430 /* A list of sub-properties. */
5431 while (CONSP (prop))
5433 if (single_display_spec_string_p (XCAR (prop), string))
5434 return 1;
5435 prop = XCDR (prop);
5438 else if (VECTORP (prop))
5440 /* A vector of sub-properties. */
5441 ptrdiff_t i;
5442 for (i = 0; i < ASIZE (prop); ++i)
5443 if (single_display_spec_string_p (AREF (prop, i), string))
5444 return 1;
5446 else
5447 return single_display_spec_string_p (prop, string);
5449 return 0;
5452 /* Look for STRING in overlays and text properties in the current
5453 buffer, between character positions FROM and TO (excluding TO).
5454 BACK_P non-zero means look back (in this case, TO is supposed to be
5455 less than FROM).
5456 Value is the first character position where STRING was found, or
5457 zero if it wasn't found before hitting TO.
5459 This function may only use code that doesn't eval because it is
5460 called asynchronously from note_mouse_highlight. */
5462 static ptrdiff_t
5463 string_buffer_position_lim (Lisp_Object string,
5464 ptrdiff_t from, ptrdiff_t to, int back_p)
5466 Lisp_Object limit, prop, pos;
5467 int found = 0;
5469 pos = make_number (max (from, BEGV));
5471 if (!back_p) /* looking forward */
5473 limit = make_number (min (to, ZV));
5474 while (!found && !EQ (pos, limit))
5476 prop = Fget_char_property (pos, Qdisplay, Qnil);
5477 if (!NILP (prop) && display_prop_string_p (prop, string))
5478 found = 1;
5479 else
5480 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5481 limit);
5484 else /* looking back */
5486 limit = make_number (max (to, BEGV));
5487 while (!found && !EQ (pos, limit))
5489 prop = Fget_char_property (pos, Qdisplay, Qnil);
5490 if (!NILP (prop) && display_prop_string_p (prop, string))
5491 found = 1;
5492 else
5493 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5494 limit);
5498 return found ? XINT (pos) : 0;
5501 /* Determine which buffer position in current buffer STRING comes from.
5502 AROUND_CHARPOS is an approximate position where it could come from.
5503 Value is the buffer position or 0 if it couldn't be determined.
5505 This function is necessary because we don't record buffer positions
5506 in glyphs generated from strings (to keep struct glyph small).
5507 This function may only use code that doesn't eval because it is
5508 called asynchronously from note_mouse_highlight. */
5510 static ptrdiff_t
5511 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5513 const int MAX_DISTANCE = 1000;
5514 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5515 around_charpos + MAX_DISTANCE,
5518 if (!found)
5519 found = string_buffer_position_lim (string, around_charpos,
5520 around_charpos - MAX_DISTANCE, 1);
5521 return found;
5526 /***********************************************************************
5527 `composition' property
5528 ***********************************************************************/
5530 /* Set up iterator IT from `composition' property at its current
5531 position. Called from handle_stop. */
5533 static enum prop_handled
5534 handle_composition_prop (struct it *it)
5536 Lisp_Object prop, string;
5537 ptrdiff_t pos, pos_byte, start, end;
5539 if (STRINGP (it->string))
5541 unsigned char *s;
5543 pos = IT_STRING_CHARPOS (*it);
5544 pos_byte = IT_STRING_BYTEPOS (*it);
5545 string = it->string;
5546 s = SDATA (string) + pos_byte;
5547 it->c = STRING_CHAR (s);
5549 else
5551 pos = IT_CHARPOS (*it);
5552 pos_byte = IT_BYTEPOS (*it);
5553 string = Qnil;
5554 it->c = FETCH_CHAR (pos_byte);
5557 /* If there's a valid composition and point is not inside of the
5558 composition (in the case that the composition is from the current
5559 buffer), draw a glyph composed from the composition components. */
5560 if (find_composition (pos, -1, &start, &end, &prop, string)
5561 && composition_valid_p (start, end, prop)
5562 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5564 if (start < pos)
5565 /* As we can't handle this situation (perhaps font-lock added
5566 a new composition), we just return here hoping that next
5567 redisplay will detect this composition much earlier. */
5568 return HANDLED_NORMALLY;
5569 if (start != pos)
5571 if (STRINGP (it->string))
5572 pos_byte = string_char_to_byte (it->string, start);
5573 else
5574 pos_byte = CHAR_TO_BYTE (start);
5576 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5577 prop, string);
5579 if (it->cmp_it.id >= 0)
5581 it->cmp_it.ch = -1;
5582 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5583 it->cmp_it.nglyphs = -1;
5587 return HANDLED_NORMALLY;
5592 /***********************************************************************
5593 Overlay strings
5594 ***********************************************************************/
5596 /* The following structure is used to record overlay strings for
5597 later sorting in load_overlay_strings. */
5599 struct overlay_entry
5601 Lisp_Object overlay;
5602 Lisp_Object string;
5603 EMACS_INT priority;
5604 int after_string_p;
5608 /* Set up iterator IT from overlay strings at its current position.
5609 Called from handle_stop. */
5611 static enum prop_handled
5612 handle_overlay_change (struct it *it)
5614 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5615 return HANDLED_RECOMPUTE_PROPS;
5616 else
5617 return HANDLED_NORMALLY;
5621 /* Set up the next overlay string for delivery by IT, if there is an
5622 overlay string to deliver. Called by set_iterator_to_next when the
5623 end of the current overlay string is reached. If there are more
5624 overlay strings to display, IT->string and
5625 IT->current.overlay_string_index are set appropriately here.
5626 Otherwise IT->string is set to nil. */
5628 static void
5629 next_overlay_string (struct it *it)
5631 ++it->current.overlay_string_index;
5632 if (it->current.overlay_string_index == it->n_overlay_strings)
5634 /* No more overlay strings. Restore IT's settings to what
5635 they were before overlay strings were processed, and
5636 continue to deliver from current_buffer. */
5638 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5639 pop_it (it);
5640 eassert (it->sp > 0
5641 || (NILP (it->string)
5642 && it->method == GET_FROM_BUFFER
5643 && it->stop_charpos >= BEGV
5644 && it->stop_charpos <= it->end_charpos));
5645 it->current.overlay_string_index = -1;
5646 it->n_overlay_strings = 0;
5647 it->overlay_strings_charpos = -1;
5648 /* If there's an empty display string on the stack, pop the
5649 stack, to resync the bidi iterator with IT's position. Such
5650 empty strings are pushed onto the stack in
5651 get_overlay_strings_1. */
5652 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5653 pop_it (it);
5655 /* If we're at the end of the buffer, record that we have
5656 processed the overlay strings there already, so that
5657 next_element_from_buffer doesn't try it again. */
5658 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5659 it->overlay_strings_at_end_processed_p = true;
5661 else
5663 /* There are more overlay strings to process. If
5664 IT->current.overlay_string_index has advanced to a position
5665 where we must load IT->overlay_strings with more strings, do
5666 it. We must load at the IT->overlay_strings_charpos where
5667 IT->n_overlay_strings was originally computed; when invisible
5668 text is present, this might not be IT_CHARPOS (Bug#7016). */
5669 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5671 if (it->current.overlay_string_index && i == 0)
5672 load_overlay_strings (it, it->overlay_strings_charpos);
5674 /* Initialize IT to deliver display elements from the overlay
5675 string. */
5676 it->string = it->overlay_strings[i];
5677 it->multibyte_p = STRING_MULTIBYTE (it->string);
5678 SET_TEXT_POS (it->current.string_pos, 0, 0);
5679 it->method = GET_FROM_STRING;
5680 it->stop_charpos = 0;
5681 it->end_charpos = SCHARS (it->string);
5682 if (it->cmp_it.stop_pos >= 0)
5683 it->cmp_it.stop_pos = 0;
5684 it->prev_stop = 0;
5685 it->base_level_stop = 0;
5687 /* Set up the bidi iterator for this overlay string. */
5688 if (it->bidi_p)
5690 it->bidi_it.string.lstring = it->string;
5691 it->bidi_it.string.s = NULL;
5692 it->bidi_it.string.schars = SCHARS (it->string);
5693 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5694 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5695 it->bidi_it.string.unibyte = !it->multibyte_p;
5696 it->bidi_it.w = it->w;
5697 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5701 CHECK_IT (it);
5705 /* Compare two overlay_entry structures E1 and E2. Used as a
5706 comparison function for qsort in load_overlay_strings. Overlay
5707 strings for the same position are sorted so that
5709 1. All after-strings come in front of before-strings, except
5710 when they come from the same overlay.
5712 2. Within after-strings, strings are sorted so that overlay strings
5713 from overlays with higher priorities come first.
5715 2. Within before-strings, strings are sorted so that overlay
5716 strings from overlays with higher priorities come last.
5718 Value is analogous to strcmp. */
5721 static int
5722 compare_overlay_entries (const void *e1, const void *e2)
5724 struct overlay_entry const *entry1 = e1;
5725 struct overlay_entry const *entry2 = e2;
5726 int result;
5728 if (entry1->after_string_p != entry2->after_string_p)
5730 /* Let after-strings appear in front of before-strings if
5731 they come from different overlays. */
5732 if (EQ (entry1->overlay, entry2->overlay))
5733 result = entry1->after_string_p ? 1 : -1;
5734 else
5735 result = entry1->after_string_p ? -1 : 1;
5737 else if (entry1->priority != entry2->priority)
5739 if (entry1->after_string_p)
5740 /* After-strings sorted in order of decreasing priority. */
5741 result = entry2->priority < entry1->priority ? -1 : 1;
5742 else
5743 /* Before-strings sorted in order of increasing priority. */
5744 result = entry1->priority < entry2->priority ? -1 : 1;
5746 else
5747 result = 0;
5749 return result;
5753 /* Load the vector IT->overlay_strings with overlay strings from IT's
5754 current buffer position, or from CHARPOS if that is > 0. Set
5755 IT->n_overlays to the total number of overlay strings found.
5757 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5758 a time. On entry into load_overlay_strings,
5759 IT->current.overlay_string_index gives the number of overlay
5760 strings that have already been loaded by previous calls to this
5761 function.
5763 IT->add_overlay_start contains an additional overlay start
5764 position to consider for taking overlay strings from, if non-zero.
5765 This position comes into play when the overlay has an `invisible'
5766 property, and both before and after-strings. When we've skipped to
5767 the end of the overlay, because of its `invisible' property, we
5768 nevertheless want its before-string to appear.
5769 IT->add_overlay_start will contain the overlay start position
5770 in this case.
5772 Overlay strings are sorted so that after-string strings come in
5773 front of before-string strings. Within before and after-strings,
5774 strings are sorted by overlay priority. See also function
5775 compare_overlay_entries. */
5777 static void
5778 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5780 Lisp_Object overlay, window, str, invisible;
5781 struct Lisp_Overlay *ov;
5782 ptrdiff_t start, end;
5783 ptrdiff_t n = 0, i, j;
5784 int invis_p;
5785 struct overlay_entry entriesbuf[20];
5786 ptrdiff_t size = ARRAYELTS (entriesbuf);
5787 struct overlay_entry *entries = entriesbuf;
5788 USE_SAFE_ALLOCA;
5790 if (charpos <= 0)
5791 charpos = IT_CHARPOS (*it);
5793 /* Append the overlay string STRING of overlay OVERLAY to vector
5794 `entries' which has size `size' and currently contains `n'
5795 elements. AFTER_P non-zero means STRING is an after-string of
5796 OVERLAY. */
5797 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5798 do \
5800 Lisp_Object priority; \
5802 if (n == size) \
5804 struct overlay_entry *old = entries; \
5805 SAFE_NALLOCA (entries, 2, size); \
5806 memcpy (entries, old, size * sizeof *entries); \
5807 size *= 2; \
5810 entries[n].string = (STRING); \
5811 entries[n].overlay = (OVERLAY); \
5812 priority = Foverlay_get ((OVERLAY), Qpriority); \
5813 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5814 entries[n].after_string_p = (AFTER_P); \
5815 ++n; \
5817 while (0)
5819 /* Process overlay before the overlay center. */
5820 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5822 XSETMISC (overlay, ov);
5823 eassert (OVERLAYP (overlay));
5824 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5825 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5827 if (end < charpos)
5828 break;
5830 /* Skip this overlay if it doesn't start or end at IT's current
5831 position. */
5832 if (end != charpos && start != charpos)
5833 continue;
5835 /* Skip this overlay if it doesn't apply to IT->w. */
5836 window = Foverlay_get (overlay, Qwindow);
5837 if (WINDOWP (window) && XWINDOW (window) != it->w)
5838 continue;
5840 /* If the text ``under'' the overlay is invisible, both before-
5841 and after-strings from this overlay are visible; start and
5842 end position are indistinguishable. */
5843 invisible = Foverlay_get (overlay, Qinvisible);
5844 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5846 /* If overlay has a non-empty before-string, record it. */
5847 if ((start == charpos || (end == charpos && invis_p))
5848 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5849 && SCHARS (str))
5850 RECORD_OVERLAY_STRING (overlay, str, 0);
5852 /* If overlay has a non-empty after-string, record it. */
5853 if ((end == charpos || (start == charpos && invis_p))
5854 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5855 && SCHARS (str))
5856 RECORD_OVERLAY_STRING (overlay, str, 1);
5859 /* Process overlays after the overlay center. */
5860 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5862 XSETMISC (overlay, ov);
5863 eassert (OVERLAYP (overlay));
5864 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5865 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5867 if (start > charpos)
5868 break;
5870 /* Skip this overlay if it doesn't start or end at IT's current
5871 position. */
5872 if (end != charpos && start != charpos)
5873 continue;
5875 /* Skip this overlay if it doesn't apply to IT->w. */
5876 window = Foverlay_get (overlay, Qwindow);
5877 if (WINDOWP (window) && XWINDOW (window) != it->w)
5878 continue;
5880 /* If the text ``under'' the overlay is invisible, it has a zero
5881 dimension, and both before- and after-strings apply. */
5882 invisible = Foverlay_get (overlay, Qinvisible);
5883 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5885 /* If overlay has a non-empty before-string, record it. */
5886 if ((start == charpos || (end == charpos && invis_p))
5887 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5888 && SCHARS (str))
5889 RECORD_OVERLAY_STRING (overlay, str, 0);
5891 /* If overlay has a non-empty after-string, record it. */
5892 if ((end == charpos || (start == charpos && invis_p))
5893 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5894 && SCHARS (str))
5895 RECORD_OVERLAY_STRING (overlay, str, 1);
5898 #undef RECORD_OVERLAY_STRING
5900 /* Sort entries. */
5901 if (n > 1)
5902 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5904 /* Record number of overlay strings, and where we computed it. */
5905 it->n_overlay_strings = n;
5906 it->overlay_strings_charpos = charpos;
5908 /* IT->current.overlay_string_index is the number of overlay strings
5909 that have already been consumed by IT. Copy some of the
5910 remaining overlay strings to IT->overlay_strings. */
5911 i = 0;
5912 j = it->current.overlay_string_index;
5913 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5915 it->overlay_strings[i] = entries[j].string;
5916 it->string_overlays[i++] = entries[j++].overlay;
5919 CHECK_IT (it);
5920 SAFE_FREE ();
5924 /* Get the first chunk of overlay strings at IT's current buffer
5925 position, or at CHARPOS if that is > 0. Value is non-zero if at
5926 least one overlay string was found. */
5928 static int
5929 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5931 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5932 process. This fills IT->overlay_strings with strings, and sets
5933 IT->n_overlay_strings to the total number of strings to process.
5934 IT->pos.overlay_string_index has to be set temporarily to zero
5935 because load_overlay_strings needs this; it must be set to -1
5936 when no overlay strings are found because a zero value would
5937 indicate a position in the first overlay string. */
5938 it->current.overlay_string_index = 0;
5939 load_overlay_strings (it, charpos);
5941 /* If we found overlay strings, set up IT to deliver display
5942 elements from the first one. Otherwise set up IT to deliver
5943 from current_buffer. */
5944 if (it->n_overlay_strings)
5946 /* Make sure we know settings in current_buffer, so that we can
5947 restore meaningful values when we're done with the overlay
5948 strings. */
5949 if (compute_stop_p)
5950 compute_stop_pos (it);
5951 eassert (it->face_id >= 0);
5953 /* Save IT's settings. They are restored after all overlay
5954 strings have been processed. */
5955 eassert (!compute_stop_p || it->sp == 0);
5957 /* When called from handle_stop, there might be an empty display
5958 string loaded. In that case, don't bother saving it. But
5959 don't use this optimization with the bidi iterator, since we
5960 need the corresponding pop_it call to resync the bidi
5961 iterator's position with IT's position, after we are done
5962 with the overlay strings. (The corresponding call to pop_it
5963 in case of an empty display string is in
5964 next_overlay_string.) */
5965 if (!(!it->bidi_p
5966 && STRINGP (it->string) && !SCHARS (it->string)))
5967 push_it (it, NULL);
5969 /* Set up IT to deliver display elements from the first overlay
5970 string. */
5971 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5972 it->string = it->overlay_strings[0];
5973 it->from_overlay = Qnil;
5974 it->stop_charpos = 0;
5975 eassert (STRINGP (it->string));
5976 it->end_charpos = SCHARS (it->string);
5977 it->prev_stop = 0;
5978 it->base_level_stop = 0;
5979 it->multibyte_p = STRING_MULTIBYTE (it->string);
5980 it->method = GET_FROM_STRING;
5981 it->from_disp_prop_p = 0;
5983 /* Force paragraph direction to be that of the parent
5984 buffer. */
5985 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5986 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5987 else
5988 it->paragraph_embedding = L2R;
5990 /* Set up the bidi iterator for this overlay string. */
5991 if (it->bidi_p)
5993 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5995 it->bidi_it.string.lstring = it->string;
5996 it->bidi_it.string.s = NULL;
5997 it->bidi_it.string.schars = SCHARS (it->string);
5998 it->bidi_it.string.bufpos = pos;
5999 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
6000 it->bidi_it.string.unibyte = !it->multibyte_p;
6001 it->bidi_it.w = it->w;
6002 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
6004 return 1;
6007 it->current.overlay_string_index = -1;
6008 return 0;
6011 static int
6012 get_overlay_strings (struct it *it, ptrdiff_t charpos)
6014 it->string = Qnil;
6015 it->method = GET_FROM_BUFFER;
6017 (void) get_overlay_strings_1 (it, charpos, 1);
6019 CHECK_IT (it);
6021 /* Value is non-zero if we found at least one overlay string. */
6022 return STRINGP (it->string);
6027 /***********************************************************************
6028 Saving and restoring state
6029 ***********************************************************************/
6031 /* Save current settings of IT on IT->stack. Called, for example,
6032 before setting up IT for an overlay string, to be able to restore
6033 IT's settings to what they were after the overlay string has been
6034 processed. If POSITION is non-NULL, it is the position to save on
6035 the stack instead of IT->position. */
6037 static void
6038 push_it (struct it *it, struct text_pos *position)
6040 struct iterator_stack_entry *p;
6042 eassert (it->sp < IT_STACK_SIZE);
6043 p = it->stack + it->sp;
6045 p->stop_charpos = it->stop_charpos;
6046 p->prev_stop = it->prev_stop;
6047 p->base_level_stop = it->base_level_stop;
6048 p->cmp_it = it->cmp_it;
6049 eassert (it->face_id >= 0);
6050 p->face_id = it->face_id;
6051 p->string = it->string;
6052 p->method = it->method;
6053 p->from_overlay = it->from_overlay;
6054 switch (p->method)
6056 case GET_FROM_IMAGE:
6057 p->u.image.object = it->object;
6058 p->u.image.image_id = it->image_id;
6059 p->u.image.slice = it->slice;
6060 break;
6061 case GET_FROM_STRETCH:
6062 p->u.stretch.object = it->object;
6063 break;
6064 #ifdef HAVE_XWIDGETS
6065 case GET_FROM_XWIDGET:
6066 p->u.xwidget.object = it->object;
6067 break;
6068 #endif
6070 p->position = position ? *position : it->position;
6071 p->current = it->current;
6072 p->end_charpos = it->end_charpos;
6073 p->string_nchars = it->string_nchars;
6074 p->area = it->area;
6075 p->multibyte_p = it->multibyte_p;
6076 p->avoid_cursor_p = it->avoid_cursor_p;
6077 p->space_width = it->space_width;
6078 p->font_height = it->font_height;
6079 p->voffset = it->voffset;
6080 p->string_from_display_prop_p = it->string_from_display_prop_p;
6081 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
6082 p->display_ellipsis_p = 0;
6083 p->line_wrap = it->line_wrap;
6084 p->bidi_p = it->bidi_p;
6085 p->paragraph_embedding = it->paragraph_embedding;
6086 p->from_disp_prop_p = it->from_disp_prop_p;
6087 ++it->sp;
6089 /* Save the state of the bidi iterator as well. */
6090 if (it->bidi_p)
6091 bidi_push_it (&it->bidi_it);
6094 static void
6095 iterate_out_of_display_property (struct it *it)
6097 int buffer_p = !STRINGP (it->string);
6098 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
6099 ptrdiff_t bob = (buffer_p ? BEGV : 0);
6101 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
6103 /* Maybe initialize paragraph direction. If we are at the beginning
6104 of a new paragraph, next_element_from_buffer may not have a
6105 chance to do that. */
6106 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
6107 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6108 /* prev_stop can be zero, so check against BEGV as well. */
6109 while (it->bidi_it.charpos >= bob
6110 && it->prev_stop <= it->bidi_it.charpos
6111 && it->bidi_it.charpos < CHARPOS (it->position)
6112 && it->bidi_it.charpos < eob)
6113 bidi_move_to_visually_next (&it->bidi_it);
6114 /* Record the stop_pos we just crossed, for when we cross it
6115 back, maybe. */
6116 if (it->bidi_it.charpos > CHARPOS (it->position))
6117 it->prev_stop = CHARPOS (it->position);
6118 /* If we ended up not where pop_it put us, resync IT's
6119 positional members with the bidi iterator. */
6120 if (it->bidi_it.charpos != CHARPOS (it->position))
6121 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
6122 if (buffer_p)
6123 it->current.pos = it->position;
6124 else
6125 it->current.string_pos = it->position;
6128 /* Restore IT's settings from IT->stack. Called, for example, when no
6129 more overlay strings must be processed, and we return to delivering
6130 display elements from a buffer, or when the end of a string from a
6131 `display' property is reached and we return to delivering display
6132 elements from an overlay string, or from a buffer. */
6134 static void
6135 pop_it (struct it *it)
6137 struct iterator_stack_entry *p;
6138 int from_display_prop = it->from_disp_prop_p;
6140 eassert (it->sp > 0);
6141 --it->sp;
6142 p = it->stack + it->sp;
6143 it->stop_charpos = p->stop_charpos;
6144 it->prev_stop = p->prev_stop;
6145 it->base_level_stop = p->base_level_stop;
6146 it->cmp_it = p->cmp_it;
6147 it->face_id = p->face_id;
6148 it->current = p->current;
6149 it->position = p->position;
6150 it->string = p->string;
6151 it->from_overlay = p->from_overlay;
6152 if (NILP (it->string))
6153 SET_TEXT_POS (it->current.string_pos, -1, -1);
6154 it->method = p->method;
6155 switch (it->method)
6157 case GET_FROM_IMAGE:
6158 it->image_id = p->u.image.image_id;
6159 it->object = p->u.image.object;
6160 it->slice = p->u.image.slice;
6161 break;
6162 #ifdef HAVE_XWIDGETS
6163 case GET_FROM_XWIDGET:
6164 it->object = p->u.xwidget.object;
6165 break;
6166 #endif
6167 case GET_FROM_STRETCH:
6168 it->object = p->u.stretch.object;
6169 break;
6170 case GET_FROM_BUFFER:
6171 it->object = it->w->contents;
6172 break;
6173 case GET_FROM_STRING:
6175 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6177 /* Restore the face_box_p flag, since it could have been
6178 overwritten by the face of the object that we just finished
6179 displaying. */
6180 if (face)
6181 it->face_box_p = face->box != FACE_NO_BOX;
6182 it->object = it->string;
6184 break;
6185 case GET_FROM_DISPLAY_VECTOR:
6186 if (it->s)
6187 it->method = GET_FROM_C_STRING;
6188 else if (STRINGP (it->string))
6189 it->method = GET_FROM_STRING;
6190 else
6192 it->method = GET_FROM_BUFFER;
6193 it->object = it->w->contents;
6196 it->end_charpos = p->end_charpos;
6197 it->string_nchars = p->string_nchars;
6198 it->area = p->area;
6199 it->multibyte_p = p->multibyte_p;
6200 it->avoid_cursor_p = p->avoid_cursor_p;
6201 it->space_width = p->space_width;
6202 it->font_height = p->font_height;
6203 it->voffset = p->voffset;
6204 it->string_from_display_prop_p = p->string_from_display_prop_p;
6205 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
6206 it->line_wrap = p->line_wrap;
6207 it->bidi_p = p->bidi_p;
6208 it->paragraph_embedding = p->paragraph_embedding;
6209 it->from_disp_prop_p = p->from_disp_prop_p;
6210 if (it->bidi_p)
6212 bidi_pop_it (&it->bidi_it);
6213 /* Bidi-iterate until we get out of the portion of text, if any,
6214 covered by a `display' text property or by an overlay with
6215 `display' property. (We cannot just jump there, because the
6216 internal coherency of the bidi iterator state can not be
6217 preserved across such jumps.) We also must determine the
6218 paragraph base direction if the overlay we just processed is
6219 at the beginning of a new paragraph. */
6220 if (from_display_prop
6221 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
6222 iterate_out_of_display_property (it);
6224 eassert ((BUFFERP (it->object)
6225 && IT_CHARPOS (*it) == it->bidi_it.charpos
6226 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
6227 || (STRINGP (it->object)
6228 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
6229 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
6230 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
6236 /***********************************************************************
6237 Moving over lines
6238 ***********************************************************************/
6240 /* Set IT's current position to the previous line start. */
6242 static void
6243 back_to_previous_line_start (struct it *it)
6245 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6247 DEC_BOTH (cp, bp);
6248 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6252 /* Move IT to the next line start.
6254 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6255 we skipped over part of the text (as opposed to moving the iterator
6256 continuously over the text). Otherwise, don't change the value
6257 of *SKIPPED_P.
6259 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6260 iterator on the newline, if it was found.
6262 Newlines may come from buffer text, overlay strings, or strings
6263 displayed via the `display' property. That's the reason we can't
6264 simply use find_newline_no_quit.
6266 Note that this function may not skip over invisible text that is so
6267 because of text properties and immediately follows a newline. If
6268 it would, function reseat_at_next_visible_line_start, when called
6269 from set_iterator_to_next, would effectively make invisible
6270 characters following a newline part of the wrong glyph row, which
6271 leads to wrong cursor motion. */
6273 static int
6274 forward_to_next_line_start (struct it *it, int *skipped_p,
6275 struct bidi_it *bidi_it_prev)
6277 ptrdiff_t old_selective;
6278 int newline_found_p, n;
6279 const int MAX_NEWLINE_DISTANCE = 500;
6281 /* If already on a newline, just consume it to avoid unintended
6282 skipping over invisible text below. */
6283 if (it->what == IT_CHARACTER
6284 && it->c == '\n'
6285 && CHARPOS (it->position) == IT_CHARPOS (*it))
6287 if (it->bidi_p && bidi_it_prev)
6288 *bidi_it_prev = it->bidi_it;
6289 set_iterator_to_next (it, 0);
6290 it->c = 0;
6291 return 1;
6294 /* Don't handle selective display in the following. It's (a)
6295 unnecessary because it's done by the caller, and (b) leads to an
6296 infinite recursion because next_element_from_ellipsis indirectly
6297 calls this function. */
6298 old_selective = it->selective;
6299 it->selective = 0;
6301 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6302 from buffer text. */
6303 for (n = newline_found_p = 0;
6304 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6305 n += STRINGP (it->string) ? 0 : 1)
6307 if (!get_next_display_element (it))
6308 return 0;
6309 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6310 if (newline_found_p && it->bidi_p && bidi_it_prev)
6311 *bidi_it_prev = it->bidi_it;
6312 set_iterator_to_next (it, 0);
6315 /* If we didn't find a newline near enough, see if we can use a
6316 short-cut. */
6317 if (!newline_found_p)
6319 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6320 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6321 1, &bytepos);
6322 Lisp_Object pos;
6324 eassert (!STRINGP (it->string));
6326 /* If there isn't any `display' property in sight, and no
6327 overlays, we can just use the position of the newline in
6328 buffer text. */
6329 if (it->stop_charpos >= limit
6330 || ((pos = Fnext_single_property_change (make_number (start),
6331 Qdisplay, Qnil,
6332 make_number (limit)),
6333 NILP (pos))
6334 && next_overlay_change (start) == ZV))
6336 if (!it->bidi_p)
6338 IT_CHARPOS (*it) = limit;
6339 IT_BYTEPOS (*it) = bytepos;
6341 else
6343 struct bidi_it bprev;
6345 /* Help bidi.c avoid expensive searches for display
6346 properties and overlays, by telling it that there are
6347 none up to `limit'. */
6348 if (it->bidi_it.disp_pos < limit)
6350 it->bidi_it.disp_pos = limit;
6351 it->bidi_it.disp_prop = 0;
6353 do {
6354 bprev = it->bidi_it;
6355 bidi_move_to_visually_next (&it->bidi_it);
6356 } while (it->bidi_it.charpos != limit);
6357 IT_CHARPOS (*it) = limit;
6358 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6359 if (bidi_it_prev)
6360 *bidi_it_prev = bprev;
6362 *skipped_p = newline_found_p = true;
6364 else
6366 while (get_next_display_element (it)
6367 && !newline_found_p)
6369 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6370 if (newline_found_p && it->bidi_p && bidi_it_prev)
6371 *bidi_it_prev = it->bidi_it;
6372 set_iterator_to_next (it, 0);
6377 it->selective = old_selective;
6378 return newline_found_p;
6382 /* Set IT's current position to the previous visible line start. Skip
6383 invisible text that is so either due to text properties or due to
6384 selective display. Caution: this does not change IT->current_x and
6385 IT->hpos. */
6387 static void
6388 back_to_previous_visible_line_start (struct it *it)
6390 while (IT_CHARPOS (*it) > BEGV)
6392 back_to_previous_line_start (it);
6394 if (IT_CHARPOS (*it) <= BEGV)
6395 break;
6397 /* If selective > 0, then lines indented more than its value are
6398 invisible. */
6399 if (it->selective > 0
6400 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6401 it->selective))
6402 continue;
6404 /* Check the newline before point for invisibility. */
6406 Lisp_Object prop;
6407 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6408 Qinvisible, it->window);
6409 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6410 continue;
6413 if (IT_CHARPOS (*it) <= BEGV)
6414 break;
6417 struct it it2;
6418 void *it2data = NULL;
6419 ptrdiff_t pos;
6420 ptrdiff_t beg, end;
6421 Lisp_Object val, overlay;
6423 SAVE_IT (it2, *it, it2data);
6425 /* If newline is part of a composition, continue from start of composition */
6426 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6427 && beg < IT_CHARPOS (*it))
6428 goto replaced;
6430 /* If newline is replaced by a display property, find start of overlay
6431 or interval and continue search from that point. */
6432 pos = --IT_CHARPOS (it2);
6433 --IT_BYTEPOS (it2);
6434 it2.sp = 0;
6435 bidi_unshelve_cache (NULL, 0);
6436 it2.string_from_display_prop_p = 0;
6437 it2.from_disp_prop_p = 0;
6438 if (handle_display_prop (&it2) == HANDLED_RETURN
6439 && !NILP (val = get_char_property_and_overlay
6440 (make_number (pos), Qdisplay, Qnil, &overlay))
6441 && (OVERLAYP (overlay)
6442 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6443 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6445 RESTORE_IT (it, it, it2data);
6446 goto replaced;
6449 /* Newline is not replaced by anything -- so we are done. */
6450 RESTORE_IT (it, it, it2data);
6451 break;
6453 replaced:
6454 if (beg < BEGV)
6455 beg = BEGV;
6456 IT_CHARPOS (*it) = beg;
6457 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6461 it->continuation_lines_width = 0;
6463 eassert (IT_CHARPOS (*it) >= BEGV);
6464 eassert (IT_CHARPOS (*it) == BEGV
6465 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6466 CHECK_IT (it);
6470 /* Reseat iterator IT at the previous visible line start. Skip
6471 invisible text that is so either due to text properties or due to
6472 selective display. At the end, update IT's overlay information,
6473 face information etc. */
6475 void
6476 reseat_at_previous_visible_line_start (struct it *it)
6478 back_to_previous_visible_line_start (it);
6479 reseat (it, it->current.pos, 1);
6480 CHECK_IT (it);
6484 /* Reseat iterator IT on the next visible line start in the current
6485 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6486 preceding the line start. Skip over invisible text that is so
6487 because of selective display. Compute faces, overlays etc at the
6488 new position. Note that this function does not skip over text that
6489 is invisible because of text properties. */
6491 static void
6492 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6494 int newline_found_p, skipped_p = 0;
6495 struct bidi_it bidi_it_prev;
6497 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6499 /* Skip over lines that are invisible because they are indented
6500 more than the value of IT->selective. */
6501 if (it->selective > 0)
6502 while (IT_CHARPOS (*it) < ZV
6503 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6504 it->selective))
6506 eassert (IT_BYTEPOS (*it) == BEGV
6507 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6508 newline_found_p =
6509 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6512 /* Position on the newline if that's what's requested. */
6513 if (on_newline_p && newline_found_p)
6515 if (STRINGP (it->string))
6517 if (IT_STRING_CHARPOS (*it) > 0)
6519 if (!it->bidi_p)
6521 --IT_STRING_CHARPOS (*it);
6522 --IT_STRING_BYTEPOS (*it);
6524 else
6526 /* We need to restore the bidi iterator to the state
6527 it had on the newline, and resync the IT's
6528 position with that. */
6529 it->bidi_it = bidi_it_prev;
6530 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6531 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6535 else if (IT_CHARPOS (*it) > BEGV)
6537 if (!it->bidi_p)
6539 --IT_CHARPOS (*it);
6540 --IT_BYTEPOS (*it);
6542 else
6544 /* We need to restore the bidi iterator to the state it
6545 had on the newline and resync IT with that. */
6546 it->bidi_it = bidi_it_prev;
6547 IT_CHARPOS (*it) = it->bidi_it.charpos;
6548 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6550 reseat (it, it->current.pos, 0);
6553 else if (skipped_p)
6554 reseat (it, it->current.pos, 0);
6556 CHECK_IT (it);
6561 /***********************************************************************
6562 Changing an iterator's position
6563 ***********************************************************************/
6565 /* Change IT's current position to POS in current_buffer. If FORCE_P
6566 is non-zero, always check for text properties at the new position.
6567 Otherwise, text properties are only looked up if POS >=
6568 IT->check_charpos of a property. */
6570 static void
6571 reseat (struct it *it, struct text_pos pos, int force_p)
6573 ptrdiff_t original_pos = IT_CHARPOS (*it);
6575 reseat_1 (it, pos, 0);
6577 /* Determine where to check text properties. Avoid doing it
6578 where possible because text property lookup is very expensive. */
6579 if (force_p
6580 || CHARPOS (pos) > it->stop_charpos
6581 || CHARPOS (pos) < original_pos)
6583 if (it->bidi_p)
6585 /* For bidi iteration, we need to prime prev_stop and
6586 base_level_stop with our best estimations. */
6587 /* Implementation note: Of course, POS is not necessarily a
6588 stop position, so assigning prev_pos to it is a lie; we
6589 should have called compute_stop_backwards. However, if
6590 the current buffer does not include any R2L characters,
6591 that call would be a waste of cycles, because the
6592 iterator will never move back, and thus never cross this
6593 "fake" stop position. So we delay that backward search
6594 until the time we really need it, in next_element_from_buffer. */
6595 if (CHARPOS (pos) != it->prev_stop)
6596 it->prev_stop = CHARPOS (pos);
6597 if (CHARPOS (pos) < it->base_level_stop)
6598 it->base_level_stop = 0; /* meaning it's unknown */
6599 handle_stop (it);
6601 else
6603 handle_stop (it);
6604 it->prev_stop = it->base_level_stop = 0;
6609 CHECK_IT (it);
6613 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6614 IT->stop_pos to POS, also. */
6616 static void
6617 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6619 /* Don't call this function when scanning a C string. */
6620 eassert (it->s == NULL);
6622 /* POS must be a reasonable value. */
6623 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6625 it->current.pos = it->position = pos;
6626 it->end_charpos = ZV;
6627 it->dpvec = NULL;
6628 it->current.dpvec_index = -1;
6629 it->current.overlay_string_index = -1;
6630 IT_STRING_CHARPOS (*it) = -1;
6631 IT_STRING_BYTEPOS (*it) = -1;
6632 it->string = Qnil;
6633 it->method = GET_FROM_BUFFER;
6634 it->object = it->w->contents;
6635 it->area = TEXT_AREA;
6636 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6637 it->sp = 0;
6638 it->string_from_display_prop_p = 0;
6639 it->string_from_prefix_prop_p = 0;
6641 it->from_disp_prop_p = 0;
6642 it->face_before_selective_p = 0;
6643 if (it->bidi_p)
6645 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6646 &it->bidi_it);
6647 bidi_unshelve_cache (NULL, 0);
6648 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6649 it->bidi_it.string.s = NULL;
6650 it->bidi_it.string.lstring = Qnil;
6651 it->bidi_it.string.bufpos = 0;
6652 it->bidi_it.string.from_disp_str = 0;
6653 it->bidi_it.string.unibyte = 0;
6654 it->bidi_it.w = it->w;
6657 if (set_stop_p)
6659 it->stop_charpos = CHARPOS (pos);
6660 it->base_level_stop = CHARPOS (pos);
6662 /* This make the information stored in it->cmp_it invalidate. */
6663 it->cmp_it.id = -1;
6667 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6668 If S is non-null, it is a C string to iterate over. Otherwise,
6669 STRING gives a Lisp string to iterate over.
6671 If PRECISION > 0, don't return more then PRECISION number of
6672 characters from the string.
6674 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6675 characters have been returned. FIELD_WIDTH < 0 means an infinite
6676 field width.
6678 MULTIBYTE = 0 means disable processing of multibyte characters,
6679 MULTIBYTE > 0 means enable it,
6680 MULTIBYTE < 0 means use IT->multibyte_p.
6682 IT must be initialized via a prior call to init_iterator before
6683 calling this function. */
6685 static void
6686 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6687 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6688 int multibyte)
6690 /* No text property checks performed by default, but see below. */
6691 it->stop_charpos = -1;
6693 /* Set iterator position and end position. */
6694 memset (&it->current, 0, sizeof it->current);
6695 it->current.overlay_string_index = -1;
6696 it->current.dpvec_index = -1;
6697 eassert (charpos >= 0);
6699 /* If STRING is specified, use its multibyteness, otherwise use the
6700 setting of MULTIBYTE, if specified. */
6701 if (multibyte >= 0)
6702 it->multibyte_p = multibyte > 0;
6704 /* Bidirectional reordering of strings is controlled by the default
6705 value of bidi-display-reordering. Don't try to reorder while
6706 loading loadup.el, as the necessary character property tables are
6707 not yet available. */
6708 it->bidi_p =
6709 NILP (Vpurify_flag)
6710 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6712 if (s == NULL)
6714 eassert (STRINGP (string));
6715 it->string = string;
6716 it->s = NULL;
6717 it->end_charpos = it->string_nchars = SCHARS (string);
6718 it->method = GET_FROM_STRING;
6719 it->current.string_pos = string_pos (charpos, string);
6721 if (it->bidi_p)
6723 it->bidi_it.string.lstring = string;
6724 it->bidi_it.string.s = NULL;
6725 it->bidi_it.string.schars = it->end_charpos;
6726 it->bidi_it.string.bufpos = 0;
6727 it->bidi_it.string.from_disp_str = 0;
6728 it->bidi_it.string.unibyte = !it->multibyte_p;
6729 it->bidi_it.w = it->w;
6730 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6731 FRAME_WINDOW_P (it->f), &it->bidi_it);
6734 else
6736 it->s = (const unsigned char *) s;
6737 it->string = Qnil;
6739 /* Note that we use IT->current.pos, not it->current.string_pos,
6740 for displaying C strings. */
6741 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6742 if (it->multibyte_p)
6744 it->current.pos = c_string_pos (charpos, s, 1);
6745 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6747 else
6749 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6750 it->end_charpos = it->string_nchars = strlen (s);
6753 if (it->bidi_p)
6755 it->bidi_it.string.lstring = Qnil;
6756 it->bidi_it.string.s = (const unsigned char *) s;
6757 it->bidi_it.string.schars = it->end_charpos;
6758 it->bidi_it.string.bufpos = 0;
6759 it->bidi_it.string.from_disp_str = 0;
6760 it->bidi_it.string.unibyte = !it->multibyte_p;
6761 it->bidi_it.w = it->w;
6762 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6763 &it->bidi_it);
6765 it->method = GET_FROM_C_STRING;
6768 /* PRECISION > 0 means don't return more than PRECISION characters
6769 from the string. */
6770 if (precision > 0 && it->end_charpos - charpos > precision)
6772 it->end_charpos = it->string_nchars = charpos + precision;
6773 if (it->bidi_p)
6774 it->bidi_it.string.schars = it->end_charpos;
6777 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6778 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6779 FIELD_WIDTH < 0 means infinite field width. This is useful for
6780 padding with `-' at the end of a mode line. */
6781 if (field_width < 0)
6782 field_width = INFINITY;
6783 /* Implementation note: We deliberately don't enlarge
6784 it->bidi_it.string.schars here to fit it->end_charpos, because
6785 the bidi iterator cannot produce characters out of thin air. */
6786 if (field_width > it->end_charpos - charpos)
6787 it->end_charpos = charpos + field_width;
6789 /* Use the standard display table for displaying strings. */
6790 if (DISP_TABLE_P (Vstandard_display_table))
6791 it->dp = XCHAR_TABLE (Vstandard_display_table);
6793 it->stop_charpos = charpos;
6794 it->prev_stop = charpos;
6795 it->base_level_stop = 0;
6796 if (it->bidi_p)
6798 it->bidi_it.first_elt = 1;
6799 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6800 it->bidi_it.disp_pos = -1;
6802 if (s == NULL && it->multibyte_p)
6804 ptrdiff_t endpos = SCHARS (it->string);
6805 if (endpos > it->end_charpos)
6806 endpos = it->end_charpos;
6807 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6808 it->string);
6810 CHECK_IT (it);
6815 /***********************************************************************
6816 Iteration
6817 ***********************************************************************/
6819 /* Map enum it_method value to corresponding next_element_from_* function. */
6821 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6823 next_element_from_buffer,
6824 next_element_from_display_vector,
6825 next_element_from_string,
6826 next_element_from_c_string,
6827 next_element_from_image,
6828 next_element_from_stretch
6829 #ifdef HAVE_XWIDGETS
6830 ,next_element_from_xwidget
6831 #endif
6834 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6837 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6838 (possibly with the following characters). */
6840 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6841 ((IT)->cmp_it.id >= 0 \
6842 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6843 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6844 END_CHARPOS, (IT)->w, \
6845 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6846 (IT)->string)))
6849 /* Lookup the char-table Vglyphless_char_display for character C (-1
6850 if we want information for no-font case), and return the display
6851 method symbol. By side-effect, update it->what and
6852 it->glyphless_method. This function is called from
6853 get_next_display_element for each character element, and from
6854 x_produce_glyphs when no suitable font was found. */
6856 Lisp_Object
6857 lookup_glyphless_char_display (int c, struct it *it)
6859 Lisp_Object glyphless_method = Qnil;
6861 if (CHAR_TABLE_P (Vglyphless_char_display)
6862 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6864 if (c >= 0)
6866 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6867 if (CONSP (glyphless_method))
6868 glyphless_method = FRAME_WINDOW_P (it->f)
6869 ? XCAR (glyphless_method)
6870 : XCDR (glyphless_method);
6872 else
6873 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6876 retry:
6877 if (NILP (glyphless_method))
6879 if (c >= 0)
6880 /* The default is to display the character by a proper font. */
6881 return Qnil;
6882 /* The default for the no-font case is to display an empty box. */
6883 glyphless_method = Qempty_box;
6885 if (EQ (glyphless_method, Qzero_width))
6887 if (c >= 0)
6888 return glyphless_method;
6889 /* This method can't be used for the no-font case. */
6890 glyphless_method = Qempty_box;
6892 if (EQ (glyphless_method, Qthin_space))
6893 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6894 else if (EQ (glyphless_method, Qempty_box))
6895 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6896 else if (EQ (glyphless_method, Qhex_code))
6897 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6898 else if (STRINGP (glyphless_method))
6899 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6900 else
6902 /* Invalid value. We use the default method. */
6903 glyphless_method = Qnil;
6904 goto retry;
6906 it->what = IT_GLYPHLESS;
6907 return glyphless_method;
6910 /* Merge escape glyph face and cache the result. */
6912 static struct frame *last_escape_glyph_frame = NULL;
6913 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6914 static int last_escape_glyph_merged_face_id = 0;
6916 static int
6917 merge_escape_glyph_face (struct it *it)
6919 int face_id;
6921 if (it->f == last_escape_glyph_frame
6922 && it->face_id == last_escape_glyph_face_id)
6923 face_id = last_escape_glyph_merged_face_id;
6924 else
6926 /* Merge the `escape-glyph' face into the current face. */
6927 face_id = merge_faces (it->f, Qescape_glyph, 0, it->face_id);
6928 last_escape_glyph_frame = it->f;
6929 last_escape_glyph_face_id = it->face_id;
6930 last_escape_glyph_merged_face_id = face_id;
6932 return face_id;
6935 /* Likewise for glyphless glyph face. */
6937 static struct frame *last_glyphless_glyph_frame = NULL;
6938 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6939 static int last_glyphless_glyph_merged_face_id = 0;
6942 merge_glyphless_glyph_face (struct it *it)
6944 int face_id;
6946 if (it->f == last_glyphless_glyph_frame
6947 && it->face_id == last_glyphless_glyph_face_id)
6948 face_id = last_glyphless_glyph_merged_face_id;
6949 else
6951 /* Merge the `glyphless-char' face into the current face. */
6952 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
6953 last_glyphless_glyph_frame = it->f;
6954 last_glyphless_glyph_face_id = it->face_id;
6955 last_glyphless_glyph_merged_face_id = face_id;
6957 return face_id;
6960 /* Load IT's display element fields with information about the next
6961 display element from the current position of IT. Value is zero if
6962 end of buffer (or C string) is reached. */
6964 static int
6965 get_next_display_element (struct it *it)
6967 /* Non-zero means that we found a display element. Zero means that
6968 we hit the end of what we iterate over. Performance note: the
6969 function pointer `method' used here turns out to be faster than
6970 using a sequence of if-statements. */
6971 int success_p;
6973 get_next:
6974 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6976 if (it->what == IT_CHARACTER)
6978 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6979 and only if (a) the resolved directionality of that character
6980 is R..." */
6981 /* FIXME: Do we need an exception for characters from display
6982 tables? */
6983 if (it->bidi_p && it->bidi_it.type == STRONG_R
6984 && !inhibit_bidi_mirroring)
6985 it->c = bidi_mirror_char (it->c);
6986 /* Map via display table or translate control characters.
6987 IT->c, IT->len etc. have been set to the next character by
6988 the function call above. If we have a display table, and it
6989 contains an entry for IT->c, translate it. Don't do this if
6990 IT->c itself comes from a display table, otherwise we could
6991 end up in an infinite recursion. (An alternative could be to
6992 count the recursion depth of this function and signal an
6993 error when a certain maximum depth is reached.) Is it worth
6994 it? */
6995 if (success_p && it->dpvec == NULL)
6997 Lisp_Object dv;
6998 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6999 int nonascii_space_p = 0;
7000 int nonascii_hyphen_p = 0;
7001 int c = it->c; /* This is the character to display. */
7003 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
7005 eassert (SINGLE_BYTE_CHAR_P (c));
7006 if (unibyte_display_via_language_environment)
7008 c = DECODE_CHAR (unibyte, c);
7009 if (c < 0)
7010 c = BYTE8_TO_CHAR (it->c);
7012 else
7013 c = BYTE8_TO_CHAR (it->c);
7016 if (it->dp
7017 && (dv = DISP_CHAR_VECTOR (it->dp, c),
7018 VECTORP (dv)))
7020 struct Lisp_Vector *v = XVECTOR (dv);
7022 /* Return the first character from the display table
7023 entry, if not empty. If empty, don't display the
7024 current character. */
7025 if (v->header.size)
7027 it->dpvec_char_len = it->len;
7028 it->dpvec = v->contents;
7029 it->dpend = v->contents + v->header.size;
7030 it->current.dpvec_index = 0;
7031 it->dpvec_face_id = -1;
7032 it->saved_face_id = it->face_id;
7033 it->method = GET_FROM_DISPLAY_VECTOR;
7034 it->ellipsis_p = 0;
7036 else
7038 set_iterator_to_next (it, 0);
7040 goto get_next;
7043 if (! NILP (lookup_glyphless_char_display (c, it)))
7045 if (it->what == IT_GLYPHLESS)
7046 goto done;
7047 /* Don't display this character. */
7048 set_iterator_to_next (it, 0);
7049 goto get_next;
7052 /* If `nobreak-char-display' is non-nil, we display
7053 non-ASCII spaces and hyphens specially. */
7054 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
7056 if (c == 0xA0)
7057 nonascii_space_p = true;
7058 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
7059 nonascii_hyphen_p = true;
7062 /* Translate control characters into `\003' or `^C' form.
7063 Control characters coming from a display table entry are
7064 currently not translated because we use IT->dpvec to hold
7065 the translation. This could easily be changed but I
7066 don't believe that it is worth doing.
7068 The characters handled by `nobreak-char-display' must be
7069 translated too.
7071 Non-printable characters and raw-byte characters are also
7072 translated to octal form. */
7073 if (((c < ' ' || c == 127) /* ASCII control chars. */
7074 ? (it->area != TEXT_AREA
7075 /* In mode line, treat \n, \t like other crl chars. */
7076 || (c != '\t'
7077 && it->glyph_row
7078 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
7079 || (c != '\n' && c != '\t'))
7080 : (nonascii_space_p
7081 || nonascii_hyphen_p
7082 || CHAR_BYTE8_P (c)
7083 || ! CHAR_PRINTABLE_P (c))))
7085 /* C is a control character, non-ASCII space/hyphen,
7086 raw-byte, or a non-printable character which must be
7087 displayed either as '\003' or as `^C' where the '\\'
7088 and '^' can be defined in the display table. Fill
7089 IT->ctl_chars with glyphs for what we have to
7090 display. Then, set IT->dpvec to these glyphs. */
7091 Lisp_Object gc;
7092 int ctl_len;
7093 int face_id;
7094 int lface_id = 0;
7095 int escape_glyph;
7097 /* Handle control characters with ^. */
7099 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
7101 int g;
7103 g = '^'; /* default glyph for Control */
7104 /* Set IT->ctl_chars[0] to the glyph for `^'. */
7105 if (it->dp
7106 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7108 g = GLYPH_CODE_CHAR (gc);
7109 lface_id = GLYPH_CODE_FACE (gc);
7112 face_id = (lface_id
7113 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7114 : merge_escape_glyph_face (it));
7116 XSETINT (it->ctl_chars[0], g);
7117 XSETINT (it->ctl_chars[1], c ^ 0100);
7118 ctl_len = 2;
7119 goto display_control;
7122 /* Handle non-ascii space in the mode where it only gets
7123 highlighting. */
7125 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
7127 /* Merge `nobreak-space' into the current face. */
7128 face_id = merge_faces (it->f, Qnobreak_space, 0,
7129 it->face_id);
7130 XSETINT (it->ctl_chars[0], ' ');
7131 ctl_len = 1;
7132 goto display_control;
7135 /* Handle sequences that start with the "escape glyph". */
7137 /* the default escape glyph is \. */
7138 escape_glyph = '\\';
7140 if (it->dp
7141 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
7143 escape_glyph = GLYPH_CODE_CHAR (gc);
7144 lface_id = GLYPH_CODE_FACE (gc);
7147 face_id = (lface_id
7148 ? merge_faces (it->f, Qt, lface_id, it->face_id)
7149 : merge_escape_glyph_face (it));
7151 /* Draw non-ASCII hyphen with just highlighting: */
7153 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
7155 XSETINT (it->ctl_chars[0], '-');
7156 ctl_len = 1;
7157 goto display_control;
7160 /* Draw non-ASCII space/hyphen with escape glyph: */
7162 if (nonascii_space_p || nonascii_hyphen_p)
7164 XSETINT (it->ctl_chars[0], escape_glyph);
7165 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
7166 ctl_len = 2;
7167 goto display_control;
7171 char str[10];
7172 int len, i;
7174 if (CHAR_BYTE8_P (c))
7175 /* Display \200 instead of \17777600. */
7176 c = CHAR_TO_BYTE8 (c);
7177 len = sprintf (str, "%03o", c);
7179 XSETINT (it->ctl_chars[0], escape_glyph);
7180 for (i = 0; i < len; i++)
7181 XSETINT (it->ctl_chars[i + 1], str[i]);
7182 ctl_len = len + 1;
7185 display_control:
7186 /* Set up IT->dpvec and return first character from it. */
7187 it->dpvec_char_len = it->len;
7188 it->dpvec = it->ctl_chars;
7189 it->dpend = it->dpvec + ctl_len;
7190 it->current.dpvec_index = 0;
7191 it->dpvec_face_id = face_id;
7192 it->saved_face_id = it->face_id;
7193 it->method = GET_FROM_DISPLAY_VECTOR;
7194 it->ellipsis_p = 0;
7195 goto get_next;
7197 it->char_to_display = c;
7199 else if (success_p)
7201 it->char_to_display = it->c;
7205 #ifdef HAVE_WINDOW_SYSTEM
7206 /* Adjust face id for a multibyte character. There are no multibyte
7207 character in unibyte text. */
7208 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
7209 && it->multibyte_p
7210 && success_p
7211 && FRAME_WINDOW_P (it->f))
7213 struct face *face = FACE_FROM_ID (it->f, it->face_id);
7215 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
7217 /* Automatic composition with glyph-string. */
7218 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
7220 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
7222 else
7224 ptrdiff_t pos = (it->s ? -1
7225 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
7226 : IT_CHARPOS (*it));
7227 int c;
7229 if (it->what == IT_CHARACTER)
7230 c = it->char_to_display;
7231 else
7233 struct composition *cmp = composition_table[it->cmp_it.id];
7234 int i;
7236 c = ' ';
7237 for (i = 0; i < cmp->glyph_len; i++)
7238 /* TAB in a composition means display glyphs with
7239 padding space on the left or right. */
7240 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
7241 break;
7243 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
7246 #endif /* HAVE_WINDOW_SYSTEM */
7248 done:
7249 /* Is this character the last one of a run of characters with
7250 box? If yes, set IT->end_of_box_run_p to 1. */
7251 if (it->face_box_p
7252 && it->s == NULL)
7254 if (it->method == GET_FROM_STRING && it->sp)
7256 int face_id = underlying_face_id (it);
7257 struct face *face = FACE_FROM_ID (it->f, face_id);
7259 if (face)
7261 if (face->box == FACE_NO_BOX)
7263 /* If the box comes from face properties in a
7264 display string, check faces in that string. */
7265 int string_face_id = face_after_it_pos (it);
7266 it->end_of_box_run_p
7267 = (FACE_FROM_ID (it->f, string_face_id)->box
7268 == FACE_NO_BOX);
7270 /* Otherwise, the box comes from the underlying face.
7271 If this is the last string character displayed, check
7272 the next buffer location. */
7273 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7274 /* n_overlay_strings is unreliable unless
7275 overlay_string_index is non-negative. */
7276 && ((it->current.overlay_string_index >= 0
7277 && (it->current.overlay_string_index
7278 == it->n_overlay_strings - 1))
7279 /* A string from display property. */
7280 || it->from_disp_prop_p))
7282 ptrdiff_t ignore;
7283 int next_face_id;
7284 struct text_pos pos = it->current.pos;
7286 /* For a string from a display property, the next
7287 buffer position is stored in the 'position'
7288 member of the iteration stack slot below the
7289 current one, see handle_single_display_spec. By
7290 contrast, it->current.pos was is not yet updated
7291 to point to that buffer position; that will
7292 happen in pop_it, after we finish displaying the
7293 current string. Note that we already checked
7294 above that it->sp is positive, so subtracting one
7295 from it is safe. */
7296 if (it->from_disp_prop_p)
7297 pos = (it->stack + it->sp - 1)->position;
7298 else
7299 INC_TEXT_POS (pos, it->multibyte_p);
7301 if (CHARPOS (pos) >= ZV)
7302 it->end_of_box_run_p = true;
7303 else
7305 next_face_id = face_at_buffer_position
7306 (it->w, CHARPOS (pos), &ignore,
7307 CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
7308 it->end_of_box_run_p
7309 = (FACE_FROM_ID (it->f, next_face_id)->box
7310 == FACE_NO_BOX);
7315 /* next_element_from_display_vector sets this flag according to
7316 faces of the display vector glyphs, see there. */
7317 else if (it->method != GET_FROM_DISPLAY_VECTOR)
7319 int face_id = face_after_it_pos (it);
7320 it->end_of_box_run_p
7321 = (face_id != it->face_id
7322 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7325 /* If we reached the end of the object we've been iterating (e.g., a
7326 display string or an overlay string), and there's something on
7327 IT->stack, proceed with what's on the stack. It doesn't make
7328 sense to return zero if there's unprocessed stuff on the stack,
7329 because otherwise that stuff will never be displayed. */
7330 if (!success_p && it->sp > 0)
7332 set_iterator_to_next (it, 0);
7333 success_p = get_next_display_element (it);
7336 /* Value is 0 if end of buffer or string reached. */
7337 return success_p;
7341 /* Move IT to the next display element.
7343 RESEAT_P non-zero means if called on a newline in buffer text,
7344 skip to the next visible line start.
7346 Functions get_next_display_element and set_iterator_to_next are
7347 separate because I find this arrangement easier to handle than a
7348 get_next_display_element function that also increments IT's
7349 position. The way it is we can first look at an iterator's current
7350 display element, decide whether it fits on a line, and if it does,
7351 increment the iterator position. The other way around we probably
7352 would either need a flag indicating whether the iterator has to be
7353 incremented the next time, or we would have to implement a
7354 decrement position function which would not be easy to write. */
7356 void
7357 set_iterator_to_next (struct it *it, int reseat_p)
7359 /* Reset flags indicating start and end of a sequence of characters
7360 with box. Reset them at the start of this function because
7361 moving the iterator to a new position might set them. */
7362 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7364 switch (it->method)
7366 case GET_FROM_BUFFER:
7367 /* The current display element of IT is a character from
7368 current_buffer. Advance in the buffer, and maybe skip over
7369 invisible lines that are so because of selective display. */
7370 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7371 reseat_at_next_visible_line_start (it, 0);
7372 else if (it->cmp_it.id >= 0)
7374 /* We are currently getting glyphs from a composition. */
7375 if (! it->bidi_p)
7377 IT_CHARPOS (*it) += it->cmp_it.nchars;
7378 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7380 else
7382 int i;
7384 /* Update IT's char/byte positions to point to the first
7385 character of the next grapheme cluster, or to the
7386 character visually after the current composition. */
7387 for (i = 0; i < it->cmp_it.nchars; i++)
7388 bidi_move_to_visually_next (&it->bidi_it);
7389 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7390 IT_CHARPOS (*it) = it->bidi_it.charpos;
7393 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7394 && it->cmp_it.to < it->cmp_it.nglyphs)
7396 /* Composition created while scanning forward. Proceed
7397 to the next grapheme cluster. */
7398 it->cmp_it.from = it->cmp_it.to;
7400 else if ((it->bidi_p && it->cmp_it.reversed_p)
7401 && it->cmp_it.from > 0)
7403 /* Composition created while scanning backward. Proceed
7404 to the previous grapheme cluster. */
7405 it->cmp_it.to = it->cmp_it.from;
7407 else
7409 /* No more grapheme clusters in this composition.
7410 Find the next stop position. */
7411 ptrdiff_t stop = it->end_charpos;
7413 if (it->bidi_it.scan_dir < 0)
7414 /* Now we are scanning backward and don't know
7415 where to stop. */
7416 stop = -1;
7417 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7418 IT_BYTEPOS (*it), stop, Qnil);
7421 else
7423 eassert (it->len != 0);
7425 if (!it->bidi_p)
7427 IT_BYTEPOS (*it) += it->len;
7428 IT_CHARPOS (*it) += 1;
7430 else
7432 int prev_scan_dir = it->bidi_it.scan_dir;
7433 /* If this is a new paragraph, determine its base
7434 direction (a.k.a. its base embedding level). */
7435 if (it->bidi_it.new_paragraph)
7436 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7437 bidi_move_to_visually_next (&it->bidi_it);
7438 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7439 IT_CHARPOS (*it) = it->bidi_it.charpos;
7440 if (prev_scan_dir != it->bidi_it.scan_dir)
7442 /* As the scan direction was changed, we must
7443 re-compute the stop position for composition. */
7444 ptrdiff_t stop = it->end_charpos;
7445 if (it->bidi_it.scan_dir < 0)
7446 stop = -1;
7447 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7448 IT_BYTEPOS (*it), stop, Qnil);
7451 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7453 break;
7455 case GET_FROM_C_STRING:
7456 /* Current display element of IT is from a C string. */
7457 if (!it->bidi_p
7458 /* If the string position is beyond string's end, it means
7459 next_element_from_c_string is padding the string with
7460 blanks, in which case we bypass the bidi iterator,
7461 because it cannot deal with such virtual characters. */
7462 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7464 IT_BYTEPOS (*it) += it->len;
7465 IT_CHARPOS (*it) += 1;
7467 else
7469 bidi_move_to_visually_next (&it->bidi_it);
7470 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7471 IT_CHARPOS (*it) = it->bidi_it.charpos;
7473 break;
7475 case GET_FROM_DISPLAY_VECTOR:
7476 /* Current display element of IT is from a display table entry.
7477 Advance in the display table definition. Reset it to null if
7478 end reached, and continue with characters from buffers/
7479 strings. */
7480 ++it->current.dpvec_index;
7482 /* Restore face of the iterator to what they were before the
7483 display vector entry (these entries may contain faces). */
7484 it->face_id = it->saved_face_id;
7486 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7488 int recheck_faces = it->ellipsis_p;
7490 if (it->s)
7491 it->method = GET_FROM_C_STRING;
7492 else if (STRINGP (it->string))
7493 it->method = GET_FROM_STRING;
7494 else
7496 it->method = GET_FROM_BUFFER;
7497 it->object = it->w->contents;
7500 it->dpvec = NULL;
7501 it->current.dpvec_index = -1;
7503 /* Skip over characters which were displayed via IT->dpvec. */
7504 if (it->dpvec_char_len < 0)
7505 reseat_at_next_visible_line_start (it, 1);
7506 else if (it->dpvec_char_len > 0)
7508 if (it->method == GET_FROM_STRING
7509 && it->current.overlay_string_index >= 0
7510 && it->n_overlay_strings > 0)
7511 it->ignore_overlay_strings_at_pos_p = true;
7512 it->len = it->dpvec_char_len;
7513 set_iterator_to_next (it, reseat_p);
7516 /* Maybe recheck faces after display vector. */
7517 if (recheck_faces)
7518 it->stop_charpos = IT_CHARPOS (*it);
7520 break;
7522 case GET_FROM_STRING:
7523 /* Current display element is a character from a Lisp string. */
7524 eassert (it->s == NULL && STRINGP (it->string));
7525 /* Don't advance past string end. These conditions are true
7526 when set_iterator_to_next is called at the end of
7527 get_next_display_element, in which case the Lisp string is
7528 already exhausted, and all we want is pop the iterator
7529 stack. */
7530 if (it->current.overlay_string_index >= 0)
7532 /* This is an overlay string, so there's no padding with
7533 spaces, and the number of characters in the string is
7534 where the string ends. */
7535 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7536 goto consider_string_end;
7538 else
7540 /* Not an overlay string. There could be padding, so test
7541 against it->end_charpos. */
7542 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7543 goto consider_string_end;
7545 if (it->cmp_it.id >= 0)
7547 /* We are delivering display elements from a composition.
7548 Update the string position past the grapheme cluster
7549 we've just processed. */
7550 if (! it->bidi_p)
7552 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7553 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7555 else
7557 int i;
7559 for (i = 0; i < it->cmp_it.nchars; i++)
7560 bidi_move_to_visually_next (&it->bidi_it);
7561 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7562 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7565 /* Did we exhaust all the grapheme clusters of this
7566 composition? */
7567 if ((! it->bidi_p || ! it->cmp_it.reversed_p)
7568 && (it->cmp_it.to < it->cmp_it.nglyphs))
7570 /* Not all the grapheme clusters were processed yet;
7571 advance to the next cluster. */
7572 it->cmp_it.from = it->cmp_it.to;
7574 else if ((it->bidi_p && it->cmp_it.reversed_p)
7575 && it->cmp_it.from > 0)
7577 /* Likewise: advance to the next cluster, but going in
7578 the reverse direction. */
7579 it->cmp_it.to = it->cmp_it.from;
7581 else
7583 /* This composition was fully processed; find the next
7584 candidate place for checking for composed
7585 characters. */
7586 /* Always limit string searches to the string length;
7587 any padding spaces are not part of the string, and
7588 there cannot be any compositions in that padding. */
7589 ptrdiff_t stop = SCHARS (it->string);
7591 if (it->bidi_p && it->bidi_it.scan_dir < 0)
7592 stop = -1;
7593 else if (it->end_charpos < stop)
7595 /* Cf. PRECISION in reseat_to_string: we might be
7596 limited in how many of the string characters we
7597 need to deliver. */
7598 stop = it->end_charpos;
7600 composition_compute_stop_pos (&it->cmp_it,
7601 IT_STRING_CHARPOS (*it),
7602 IT_STRING_BYTEPOS (*it), stop,
7603 it->string);
7606 else
7608 if (!it->bidi_p
7609 /* If the string position is beyond string's end, it
7610 means next_element_from_string is padding the string
7611 with blanks, in which case we bypass the bidi
7612 iterator, because it cannot deal with such virtual
7613 characters. */
7614 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7616 IT_STRING_BYTEPOS (*it) += it->len;
7617 IT_STRING_CHARPOS (*it) += 1;
7619 else
7621 int prev_scan_dir = it->bidi_it.scan_dir;
7623 bidi_move_to_visually_next (&it->bidi_it);
7624 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7625 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7626 /* If the scan direction changes, we may need to update
7627 the place where to check for composed characters. */
7628 if (prev_scan_dir != it->bidi_it.scan_dir)
7630 ptrdiff_t stop = SCHARS (it->string);
7632 if (it->bidi_it.scan_dir < 0)
7633 stop = -1;
7634 else if (it->end_charpos < stop)
7635 stop = it->end_charpos;
7637 composition_compute_stop_pos (&it->cmp_it,
7638 IT_STRING_CHARPOS (*it),
7639 IT_STRING_BYTEPOS (*it), stop,
7640 it->string);
7645 consider_string_end:
7647 if (it->current.overlay_string_index >= 0)
7649 /* IT->string is an overlay string. Advance to the
7650 next, if there is one. */
7651 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7653 it->ellipsis_p = 0;
7654 next_overlay_string (it);
7655 if (it->ellipsis_p)
7656 setup_for_ellipsis (it, 0);
7659 else
7661 /* IT->string is not an overlay string. If we reached
7662 its end, and there is something on IT->stack, proceed
7663 with what is on the stack. This can be either another
7664 string, this time an overlay string, or a buffer. */
7665 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7666 && it->sp > 0)
7668 pop_it (it);
7669 if (it->method == GET_FROM_STRING)
7670 goto consider_string_end;
7673 break;
7675 case GET_FROM_IMAGE:
7676 case GET_FROM_STRETCH:
7677 #ifdef HAVE_XWIDGETS
7678 case GET_FROM_XWIDGET:
7680 /* The position etc with which we have to proceed are on
7681 the stack. The position may be at the end of a string,
7682 if the `display' property takes up the whole string. */
7683 eassert (it->sp > 0);
7684 pop_it (it);
7685 if (it->method == GET_FROM_STRING)
7686 goto consider_string_end;
7687 break;
7688 #endif
7689 default:
7690 /* There are no other methods defined, so this should be a bug. */
7691 emacs_abort ();
7694 eassert (it->method != GET_FROM_STRING
7695 || (STRINGP (it->string)
7696 && IT_STRING_CHARPOS (*it) >= 0));
7699 /* Load IT's display element fields with information about the next
7700 display element which comes from a display table entry or from the
7701 result of translating a control character to one of the forms `^C'
7702 or `\003'.
7704 IT->dpvec holds the glyphs to return as characters.
7705 IT->saved_face_id holds the face id before the display vector--it
7706 is restored into IT->face_id in set_iterator_to_next. */
7708 static int
7709 next_element_from_display_vector (struct it *it)
7711 Lisp_Object gc;
7712 int prev_face_id = it->face_id;
7713 int next_face_id;
7715 /* Precondition. */
7716 eassert (it->dpvec && it->current.dpvec_index >= 0);
7718 it->face_id = it->saved_face_id;
7720 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7721 That seemed totally bogus - so I changed it... */
7722 gc = it->dpvec[it->current.dpvec_index];
7724 if (GLYPH_CODE_P (gc))
7726 struct face *this_face, *prev_face, *next_face;
7728 it->c = GLYPH_CODE_CHAR (gc);
7729 it->len = CHAR_BYTES (it->c);
7731 /* The entry may contain a face id to use. Such a face id is
7732 the id of a Lisp face, not a realized face. A face id of
7733 zero means no face is specified. */
7734 if (it->dpvec_face_id >= 0)
7735 it->face_id = it->dpvec_face_id;
7736 else
7738 int lface_id = GLYPH_CODE_FACE (gc);
7739 if (lface_id > 0)
7740 it->face_id = merge_faces (it->f, Qt, lface_id,
7741 it->saved_face_id);
7744 /* Glyphs in the display vector could have the box face, so we
7745 need to set the related flags in the iterator, as
7746 appropriate. */
7747 this_face = FACE_FROM_ID (it->f, it->face_id);
7748 prev_face = FACE_FROM_ID (it->f, prev_face_id);
7750 /* Is this character the first character of a box-face run? */
7751 it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7752 && (!prev_face
7753 || prev_face->box == FACE_NO_BOX));
7755 /* For the last character of the box-face run, we need to look
7756 either at the next glyph from the display vector, or at the
7757 face we saw before the display vector. */
7758 next_face_id = it->saved_face_id;
7759 if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
7761 if (it->dpvec_face_id >= 0)
7762 next_face_id = it->dpvec_face_id;
7763 else
7765 int lface_id =
7766 GLYPH_CODE_FACE (it->dpvec[it->current.dpvec_index + 1]);
7768 if (lface_id > 0)
7769 next_face_id = merge_faces (it->f, Qt, lface_id,
7770 it->saved_face_id);
7773 next_face = FACE_FROM_ID (it->f, next_face_id);
7774 it->end_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
7775 && (!next_face
7776 || next_face->box == FACE_NO_BOX));
7777 it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
7779 else
7780 /* Display table entry is invalid. Return a space. */
7781 it->c = ' ', it->len = 1;
7783 /* Don't change position and object of the iterator here. They are
7784 still the values of the character that had this display table
7785 entry or was translated, and that's what we want. */
7786 it->what = IT_CHARACTER;
7787 return 1;
7790 /* Get the first element of string/buffer in the visual order, after
7791 being reseated to a new position in a string or a buffer. */
7792 static void
7793 get_visually_first_element (struct it *it)
7795 int string_p = STRINGP (it->string) || it->s;
7796 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7797 ptrdiff_t bob = (string_p ? 0 : BEGV);
7799 if (STRINGP (it->string))
7801 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7802 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7804 else
7806 it->bidi_it.charpos = IT_CHARPOS (*it);
7807 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7810 if (it->bidi_it.charpos == eob)
7812 /* Nothing to do, but reset the FIRST_ELT flag, like
7813 bidi_paragraph_init does, because we are not going to
7814 call it. */
7815 it->bidi_it.first_elt = 0;
7817 else if (it->bidi_it.charpos == bob
7818 || (!string_p
7819 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7820 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7822 /* If we are at the beginning of a line/string, we can produce
7823 the next element right away. */
7824 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7825 bidi_move_to_visually_next (&it->bidi_it);
7827 else
7829 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7831 /* We need to prime the bidi iterator starting at the line's or
7832 string's beginning, before we will be able to produce the
7833 next element. */
7834 if (string_p)
7835 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7836 else
7837 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7838 IT_BYTEPOS (*it), -1,
7839 &it->bidi_it.bytepos);
7840 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7843 /* Now return to buffer/string position where we were asked
7844 to get the next display element, and produce that. */
7845 bidi_move_to_visually_next (&it->bidi_it);
7847 while (it->bidi_it.bytepos != orig_bytepos
7848 && it->bidi_it.charpos < eob);
7851 /* Adjust IT's position information to where we ended up. */
7852 if (STRINGP (it->string))
7854 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7855 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7857 else
7859 IT_CHARPOS (*it) = it->bidi_it.charpos;
7860 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7863 if (STRINGP (it->string) || !it->s)
7865 ptrdiff_t stop, charpos, bytepos;
7867 if (STRINGP (it->string))
7869 eassert (!it->s);
7870 stop = SCHARS (it->string);
7871 if (stop > it->end_charpos)
7872 stop = it->end_charpos;
7873 charpos = IT_STRING_CHARPOS (*it);
7874 bytepos = IT_STRING_BYTEPOS (*it);
7876 else
7878 stop = it->end_charpos;
7879 charpos = IT_CHARPOS (*it);
7880 bytepos = IT_BYTEPOS (*it);
7882 if (it->bidi_it.scan_dir < 0)
7883 stop = -1;
7884 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7885 it->string);
7889 /* Load IT with the next display element from Lisp string IT->string.
7890 IT->current.string_pos is the current position within the string.
7891 If IT->current.overlay_string_index >= 0, the Lisp string is an
7892 overlay string. */
7894 static int
7895 next_element_from_string (struct it *it)
7897 struct text_pos position;
7899 eassert (STRINGP (it->string));
7900 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7901 eassert (IT_STRING_CHARPOS (*it) >= 0);
7902 position = it->current.string_pos;
7904 /* With bidi reordering, the character to display might not be the
7905 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7906 that we were reseat()ed to a new string, whose paragraph
7907 direction is not known. */
7908 if (it->bidi_p && it->bidi_it.first_elt)
7910 get_visually_first_element (it);
7911 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7914 /* Time to check for invisible text? */
7915 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7917 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7919 if (!(!it->bidi_p
7920 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7921 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7923 /* With bidi non-linear iteration, we could find
7924 ourselves far beyond the last computed stop_charpos,
7925 with several other stop positions in between that we
7926 missed. Scan them all now, in buffer's logical
7927 order, until we find and handle the last stop_charpos
7928 that precedes our current position. */
7929 handle_stop_backwards (it, it->stop_charpos);
7930 return GET_NEXT_DISPLAY_ELEMENT (it);
7932 else
7934 if (it->bidi_p)
7936 /* Take note of the stop position we just moved
7937 across, for when we will move back across it. */
7938 it->prev_stop = it->stop_charpos;
7939 /* If we are at base paragraph embedding level, take
7940 note of the last stop position seen at this
7941 level. */
7942 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7943 it->base_level_stop = it->stop_charpos;
7945 handle_stop (it);
7947 /* Since a handler may have changed IT->method, we must
7948 recurse here. */
7949 return GET_NEXT_DISPLAY_ELEMENT (it);
7952 else if (it->bidi_p
7953 /* If we are before prev_stop, we may have overstepped
7954 on our way backwards a stop_pos, and if so, we need
7955 to handle that stop_pos. */
7956 && IT_STRING_CHARPOS (*it) < it->prev_stop
7957 /* We can sometimes back up for reasons that have nothing
7958 to do with bidi reordering. E.g., compositions. The
7959 code below is only needed when we are above the base
7960 embedding level, so test for that explicitly. */
7961 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7963 /* If we lost track of base_level_stop, we have no better
7964 place for handle_stop_backwards to start from than string
7965 beginning. This happens, e.g., when we were reseated to
7966 the previous screenful of text by vertical-motion. */
7967 if (it->base_level_stop <= 0
7968 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7969 it->base_level_stop = 0;
7970 handle_stop_backwards (it, it->base_level_stop);
7971 return GET_NEXT_DISPLAY_ELEMENT (it);
7975 if (it->current.overlay_string_index >= 0)
7977 /* Get the next character from an overlay string. In overlay
7978 strings, there is no field width or padding with spaces to
7979 do. */
7980 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7982 it->what = IT_EOB;
7983 return 0;
7985 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7986 IT_STRING_BYTEPOS (*it),
7987 it->bidi_it.scan_dir < 0
7988 ? -1
7989 : SCHARS (it->string))
7990 && next_element_from_composition (it))
7992 return 1;
7994 else if (STRING_MULTIBYTE (it->string))
7996 const unsigned char *s = (SDATA (it->string)
7997 + IT_STRING_BYTEPOS (*it));
7998 it->c = string_char_and_length (s, &it->len);
8000 else
8002 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8003 it->len = 1;
8006 else
8008 /* Get the next character from a Lisp string that is not an
8009 overlay string. Such strings come from the mode line, for
8010 example. We may have to pad with spaces, or truncate the
8011 string. See also next_element_from_c_string. */
8012 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
8014 it->what = IT_EOB;
8015 return 0;
8017 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
8019 /* Pad with spaces. */
8020 it->c = ' ', it->len = 1;
8021 CHARPOS (position) = BYTEPOS (position) = -1;
8023 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
8024 IT_STRING_BYTEPOS (*it),
8025 it->bidi_it.scan_dir < 0
8026 ? -1
8027 : it->string_nchars)
8028 && next_element_from_composition (it))
8030 return 1;
8032 else if (STRING_MULTIBYTE (it->string))
8034 const unsigned char *s = (SDATA (it->string)
8035 + IT_STRING_BYTEPOS (*it));
8036 it->c = string_char_and_length (s, &it->len);
8038 else
8040 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
8041 it->len = 1;
8045 /* Record what we have and where it came from. */
8046 it->what = IT_CHARACTER;
8047 it->object = it->string;
8048 it->position = position;
8049 return 1;
8053 /* Load IT with next display element from C string IT->s.
8054 IT->string_nchars is the maximum number of characters to return
8055 from the string. IT->end_charpos may be greater than
8056 IT->string_nchars when this function is called, in which case we
8057 may have to return padding spaces. Value is zero if end of string
8058 reached, including padding spaces. */
8060 static int
8061 next_element_from_c_string (struct it *it)
8063 bool success_p = true;
8065 eassert (it->s);
8066 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
8067 it->what = IT_CHARACTER;
8068 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
8069 it->object = Qnil;
8071 /* With bidi reordering, the character to display might not be the
8072 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8073 we were reseated to a new string, whose paragraph direction is
8074 not known. */
8075 if (it->bidi_p && it->bidi_it.first_elt)
8076 get_visually_first_element (it);
8078 /* IT's position can be greater than IT->string_nchars in case a
8079 field width or precision has been specified when the iterator was
8080 initialized. */
8081 if (IT_CHARPOS (*it) >= it->end_charpos)
8083 /* End of the game. */
8084 it->what = IT_EOB;
8085 success_p = 0;
8087 else if (IT_CHARPOS (*it) >= it->string_nchars)
8089 /* Pad with spaces. */
8090 it->c = ' ', it->len = 1;
8091 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
8093 else if (it->multibyte_p)
8094 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
8095 else
8096 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
8098 return success_p;
8102 /* Set up IT to return characters from an ellipsis, if appropriate.
8103 The definition of the ellipsis glyphs may come from a display table
8104 entry. This function fills IT with the first glyph from the
8105 ellipsis if an ellipsis is to be displayed. */
8107 static int
8108 next_element_from_ellipsis (struct it *it)
8110 if (it->selective_display_ellipsis_p)
8111 setup_for_ellipsis (it, it->len);
8112 else
8114 /* The face at the current position may be different from the
8115 face we find after the invisible text. Remember what it
8116 was in IT->saved_face_id, and signal that it's there by
8117 setting face_before_selective_p. */
8118 it->saved_face_id = it->face_id;
8119 it->method = GET_FROM_BUFFER;
8120 it->object = it->w->contents;
8121 reseat_at_next_visible_line_start (it, 1);
8122 it->face_before_selective_p = true;
8125 return GET_NEXT_DISPLAY_ELEMENT (it);
8129 /* Deliver an image display element. The iterator IT is already
8130 filled with image information (done in handle_display_prop). Value
8131 is always 1. */
8134 static int
8135 next_element_from_image (struct it *it)
8137 it->what = IT_IMAGE;
8138 it->ignore_overlay_strings_at_pos_p = 0;
8139 return 1;
8142 #ifdef HAVE_XWIDGETS
8143 /* im not sure about this FIXME JAVE*/
8144 static int
8145 next_element_from_xwidget (struct it *it)
8147 it->what = IT_XWIDGET;
8148 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
8149 //this is shaky because why do we set "what" if we dont set the other parts??
8150 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
8151 return 1;
8153 #endif
8156 /* Fill iterator IT with next display element from a stretch glyph
8157 property. IT->object is the value of the text property. Value is
8158 always 1. */
8160 static int
8161 next_element_from_stretch (struct it *it)
8163 it->what = IT_STRETCH;
8164 return 1;
8167 /* Scan backwards from IT's current position until we find a stop
8168 position, or until BEGV. This is called when we find ourself
8169 before both the last known prev_stop and base_level_stop while
8170 reordering bidirectional text. */
8172 static void
8173 compute_stop_pos_backwards (struct it *it)
8175 const int SCAN_BACK_LIMIT = 1000;
8176 struct text_pos pos;
8177 struct display_pos save_current = it->current;
8178 struct text_pos save_position = it->position;
8179 ptrdiff_t charpos = IT_CHARPOS (*it);
8180 ptrdiff_t where_we_are = charpos;
8181 ptrdiff_t save_stop_pos = it->stop_charpos;
8182 ptrdiff_t save_end_pos = it->end_charpos;
8184 eassert (NILP (it->string) && !it->s);
8185 eassert (it->bidi_p);
8186 it->bidi_p = 0;
8189 it->end_charpos = min (charpos + 1, ZV);
8190 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
8191 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
8192 reseat_1 (it, pos, 0);
8193 compute_stop_pos (it);
8194 /* We must advance forward, right? */
8195 if (it->stop_charpos <= charpos)
8196 emacs_abort ();
8198 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
8200 if (it->stop_charpos <= where_we_are)
8201 it->prev_stop = it->stop_charpos;
8202 else
8203 it->prev_stop = BEGV;
8204 it->bidi_p = true;
8205 it->current = save_current;
8206 it->position = save_position;
8207 it->stop_charpos = save_stop_pos;
8208 it->end_charpos = save_end_pos;
8211 /* Scan forward from CHARPOS in the current buffer/string, until we
8212 find a stop position > current IT's position. Then handle the stop
8213 position before that. This is called when we bump into a stop
8214 position while reordering bidirectional text. CHARPOS should be
8215 the last previously processed stop_pos (or BEGV/0, if none were
8216 processed yet) whose position is less that IT's current
8217 position. */
8219 static void
8220 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
8222 int bufp = !STRINGP (it->string);
8223 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
8224 struct display_pos save_current = it->current;
8225 struct text_pos save_position = it->position;
8226 struct text_pos pos1;
8227 ptrdiff_t next_stop;
8229 /* Scan in strict logical order. */
8230 eassert (it->bidi_p);
8231 it->bidi_p = 0;
8234 it->prev_stop = charpos;
8235 if (bufp)
8237 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
8238 reseat_1 (it, pos1, 0);
8240 else
8241 it->current.string_pos = string_pos (charpos, it->string);
8242 compute_stop_pos (it);
8243 /* We must advance forward, right? */
8244 if (it->stop_charpos <= it->prev_stop)
8245 emacs_abort ();
8246 charpos = it->stop_charpos;
8248 while (charpos <= where_we_are);
8250 it->bidi_p = true;
8251 it->current = save_current;
8252 it->position = save_position;
8253 next_stop = it->stop_charpos;
8254 it->stop_charpos = it->prev_stop;
8255 handle_stop (it);
8256 it->stop_charpos = next_stop;
8259 /* Load IT with the next display element from current_buffer. Value
8260 is zero if end of buffer reached. IT->stop_charpos is the next
8261 position at which to stop and check for text properties or buffer
8262 end. */
8264 static int
8265 next_element_from_buffer (struct it *it)
8267 bool success_p = true;
8269 eassert (IT_CHARPOS (*it) >= BEGV);
8270 eassert (NILP (it->string) && !it->s);
8271 eassert (!it->bidi_p
8272 || (EQ (it->bidi_it.string.lstring, Qnil)
8273 && it->bidi_it.string.s == NULL));
8275 /* With bidi reordering, the character to display might not be the
8276 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
8277 we were reseat()ed to a new buffer position, which is potentially
8278 a different paragraph. */
8279 if (it->bidi_p && it->bidi_it.first_elt)
8281 get_visually_first_element (it);
8282 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8285 if (IT_CHARPOS (*it) >= it->stop_charpos)
8287 if (IT_CHARPOS (*it) >= it->end_charpos)
8289 int overlay_strings_follow_p;
8291 /* End of the game, except when overlay strings follow that
8292 haven't been returned yet. */
8293 if (it->overlay_strings_at_end_processed_p)
8294 overlay_strings_follow_p = 0;
8295 else
8297 it->overlay_strings_at_end_processed_p = true;
8298 overlay_strings_follow_p = get_overlay_strings (it, 0);
8301 if (overlay_strings_follow_p)
8302 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8303 else
8305 it->what = IT_EOB;
8306 it->position = it->current.pos;
8307 success_p = 0;
8310 else if (!(!it->bidi_p
8311 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8312 || IT_CHARPOS (*it) == it->stop_charpos))
8314 /* With bidi non-linear iteration, we could find ourselves
8315 far beyond the last computed stop_charpos, with several
8316 other stop positions in between that we missed. Scan
8317 them all now, in buffer's logical order, until we find
8318 and handle the last stop_charpos that precedes our
8319 current position. */
8320 handle_stop_backwards (it, it->stop_charpos);
8321 return GET_NEXT_DISPLAY_ELEMENT (it);
8323 else
8325 if (it->bidi_p)
8327 /* Take note of the stop position we just moved across,
8328 for when we will move back across it. */
8329 it->prev_stop = it->stop_charpos;
8330 /* If we are at base paragraph embedding level, take
8331 note of the last stop position seen at this
8332 level. */
8333 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8334 it->base_level_stop = it->stop_charpos;
8336 handle_stop (it);
8337 return GET_NEXT_DISPLAY_ELEMENT (it);
8340 else if (it->bidi_p
8341 /* If we are before prev_stop, we may have overstepped on
8342 our way backwards a stop_pos, and if so, we need to
8343 handle that stop_pos. */
8344 && IT_CHARPOS (*it) < it->prev_stop
8345 /* We can sometimes back up for reasons that have nothing
8346 to do with bidi reordering. E.g., compositions. The
8347 code below is only needed when we are above the base
8348 embedding level, so test for that explicitly. */
8349 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8351 if (it->base_level_stop <= 0
8352 || IT_CHARPOS (*it) < it->base_level_stop)
8354 /* If we lost track of base_level_stop, we need to find
8355 prev_stop by looking backwards. This happens, e.g., when
8356 we were reseated to the previous screenful of text by
8357 vertical-motion. */
8358 it->base_level_stop = BEGV;
8359 compute_stop_pos_backwards (it);
8360 handle_stop_backwards (it, it->prev_stop);
8362 else
8363 handle_stop_backwards (it, it->base_level_stop);
8364 return GET_NEXT_DISPLAY_ELEMENT (it);
8366 else
8368 /* No face changes, overlays etc. in sight, so just return a
8369 character from current_buffer. */
8370 unsigned char *p;
8371 ptrdiff_t stop;
8373 /* We moved to the next buffer position, so any info about
8374 previously seen overlays is no longer valid. */
8375 it->ignore_overlay_strings_at_pos_p = 0;
8377 /* Maybe run the redisplay end trigger hook. Performance note:
8378 This doesn't seem to cost measurable time. */
8379 if (it->redisplay_end_trigger_charpos
8380 && it->glyph_row
8381 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8382 run_redisplay_end_trigger_hook (it);
8384 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8385 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8386 stop)
8387 && next_element_from_composition (it))
8389 return 1;
8392 /* Get the next character, maybe multibyte. */
8393 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8394 if (it->multibyte_p && !ASCII_CHAR_P (*p))
8395 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8396 else
8397 it->c = *p, it->len = 1;
8399 /* Record what we have and where it came from. */
8400 it->what = IT_CHARACTER;
8401 it->object = it->w->contents;
8402 it->position = it->current.pos;
8404 /* Normally we return the character found above, except when we
8405 really want to return an ellipsis for selective display. */
8406 if (it->selective)
8408 if (it->c == '\n')
8410 /* A value of selective > 0 means hide lines indented more
8411 than that number of columns. */
8412 if (it->selective > 0
8413 && IT_CHARPOS (*it) + 1 < ZV
8414 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8415 IT_BYTEPOS (*it) + 1,
8416 it->selective))
8418 success_p = next_element_from_ellipsis (it);
8419 it->dpvec_char_len = -1;
8422 else if (it->c == '\r' && it->selective == -1)
8424 /* A value of selective == -1 means that everything from the
8425 CR to the end of the line is invisible, with maybe an
8426 ellipsis displayed for it. */
8427 success_p = next_element_from_ellipsis (it);
8428 it->dpvec_char_len = -1;
8433 /* Value is zero if end of buffer reached. */
8434 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8435 return success_p;
8439 /* Run the redisplay end trigger hook for IT. */
8441 static void
8442 run_redisplay_end_trigger_hook (struct it *it)
8444 Lisp_Object args[3];
8446 /* IT->glyph_row should be non-null, i.e. we should be actually
8447 displaying something, or otherwise we should not run the hook. */
8448 eassert (it->glyph_row);
8450 /* Set up hook arguments. */
8451 args[0] = Qredisplay_end_trigger_functions;
8452 args[1] = it->window;
8453 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8454 it->redisplay_end_trigger_charpos = 0;
8456 /* Since we are *trying* to run these functions, don't try to run
8457 them again, even if they get an error. */
8458 wset_redisplay_end_trigger (it->w, Qnil);
8459 Frun_hook_with_args (3, args);
8461 /* Notice if it changed the face of the character we are on. */
8462 handle_face_prop (it);
8466 /* Deliver a composition display element. Unlike the other
8467 next_element_from_XXX, this function is not registered in the array
8468 get_next_element[]. It is called from next_element_from_buffer and
8469 next_element_from_string when necessary. */
8471 static int
8472 next_element_from_composition (struct it *it)
8474 it->what = IT_COMPOSITION;
8475 it->len = it->cmp_it.nbytes;
8476 if (STRINGP (it->string))
8478 if (it->c < 0)
8480 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8481 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8482 return 0;
8484 it->position = it->current.string_pos;
8485 it->object = it->string;
8486 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8487 IT_STRING_BYTEPOS (*it), it->string);
8489 else
8491 if (it->c < 0)
8493 IT_CHARPOS (*it) += it->cmp_it.nchars;
8494 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8495 if (it->bidi_p)
8497 if (it->bidi_it.new_paragraph)
8498 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8499 /* Resync the bidi iterator with IT's new position.
8500 FIXME: this doesn't support bidirectional text. */
8501 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8502 bidi_move_to_visually_next (&it->bidi_it);
8504 return 0;
8506 it->position = it->current.pos;
8507 it->object = it->w->contents;
8508 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8509 IT_BYTEPOS (*it), Qnil);
8511 return 1;
8516 /***********************************************************************
8517 Moving an iterator without producing glyphs
8518 ***********************************************************************/
8520 /* Check if iterator is at a position corresponding to a valid buffer
8521 position after some move_it_ call. */
8523 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8524 ((it)->method == GET_FROM_STRING \
8525 ? IT_STRING_CHARPOS (*it) == 0 \
8526 : 1)
8529 /* Move iterator IT to a specified buffer or X position within one
8530 line on the display without producing glyphs.
8532 OP should be a bit mask including some or all of these bits:
8533 MOVE_TO_X: Stop upon reaching x-position TO_X.
8534 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8535 Regardless of OP's value, stop upon reaching the end of the display line.
8537 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8538 This means, in particular, that TO_X includes window's horizontal
8539 scroll amount.
8541 The return value has several possible values that
8542 say what condition caused the scan to stop:
8544 MOVE_POS_MATCH_OR_ZV
8545 - when TO_POS or ZV was reached.
8547 MOVE_X_REACHED
8548 -when TO_X was reached before TO_POS or ZV were reached.
8550 MOVE_LINE_CONTINUED
8551 - when we reached the end of the display area and the line must
8552 be continued.
8554 MOVE_LINE_TRUNCATED
8555 - when we reached the end of the display area and the line is
8556 truncated.
8558 MOVE_NEWLINE_OR_CR
8559 - when we stopped at a line end, i.e. a newline or a CR and selective
8560 display is on. */
8562 static enum move_it_result
8563 move_it_in_display_line_to (struct it *it,
8564 ptrdiff_t to_charpos, int to_x,
8565 enum move_operation_enum op)
8567 enum move_it_result result = MOVE_UNDEFINED;
8568 struct glyph_row *saved_glyph_row;
8569 struct it wrap_it, atpos_it, atx_it, ppos_it;
8570 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8571 void *ppos_data = NULL;
8572 int may_wrap = 0;
8573 enum it_method prev_method = it->method;
8574 ptrdiff_t closest_pos IF_LINT (= 0), prev_pos = IT_CHARPOS (*it);
8575 int saw_smaller_pos = prev_pos < to_charpos;
8577 /* Don't produce glyphs in produce_glyphs. */
8578 saved_glyph_row = it->glyph_row;
8579 it->glyph_row = NULL;
8581 /* Use wrap_it to save a copy of IT wherever a word wrap could
8582 occur. Use atpos_it to save a copy of IT at the desired buffer
8583 position, if found, so that we can scan ahead and check if the
8584 word later overshoots the window edge. Use atx_it similarly, for
8585 pixel positions. */
8586 wrap_it.sp = -1;
8587 atpos_it.sp = -1;
8588 atx_it.sp = -1;
8590 /* Use ppos_it under bidi reordering to save a copy of IT for the
8591 initial position. We restore that position in IT when we have
8592 scanned the entire display line without finding a match for
8593 TO_CHARPOS and all the character positions are greater than
8594 TO_CHARPOS. We then restart the scan from the initial position,
8595 and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
8596 the closest to TO_CHARPOS. */
8597 if (it->bidi_p)
8599 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8601 SAVE_IT (ppos_it, *it, ppos_data);
8602 closest_pos = IT_CHARPOS (*it);
8604 else
8605 closest_pos = ZV;
8608 #define BUFFER_POS_REACHED_P() \
8609 ((op & MOVE_TO_POS) != 0 \
8610 && BUFFERP (it->object) \
8611 && (IT_CHARPOS (*it) == to_charpos \
8612 || ((!it->bidi_p \
8613 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8614 && IT_CHARPOS (*it) > to_charpos) \
8615 || (it->what == IT_COMPOSITION \
8616 && ((IT_CHARPOS (*it) > to_charpos \
8617 && to_charpos >= it->cmp_it.charpos) \
8618 || (IT_CHARPOS (*it) < to_charpos \
8619 && to_charpos <= it->cmp_it.charpos)))) \
8620 && (it->method == GET_FROM_BUFFER \
8621 || (it->method == GET_FROM_DISPLAY_VECTOR \
8622 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8624 /* If there's a line-/wrap-prefix, handle it. */
8625 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8626 && it->current_y < it->last_visible_y)
8627 handle_line_prefix (it);
8629 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8630 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8632 while (1)
8634 int x, i, ascent = 0, descent = 0;
8636 /* Utility macro to reset an iterator with x, ascent, and descent. */
8637 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8638 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8639 (IT)->max_descent = descent)
8641 /* Stop if we move beyond TO_CHARPOS (after an image or a
8642 display string or stretch glyph). */
8643 if ((op & MOVE_TO_POS) != 0
8644 && BUFFERP (it->object)
8645 && it->method == GET_FROM_BUFFER
8646 && (((!it->bidi_p
8647 /* When the iterator is at base embedding level, we
8648 are guaranteed that characters are delivered for
8649 display in strictly increasing order of their
8650 buffer positions. */
8651 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8652 && IT_CHARPOS (*it) > to_charpos)
8653 || (it->bidi_p
8654 && (prev_method == GET_FROM_IMAGE
8655 || prev_method == GET_FROM_STRETCH
8656 || prev_method == GET_FROM_STRING)
8657 /* Passed TO_CHARPOS from left to right. */
8658 && ((prev_pos < to_charpos
8659 && IT_CHARPOS (*it) > to_charpos)
8660 /* Passed TO_CHARPOS from right to left. */
8661 || (prev_pos > to_charpos
8662 && IT_CHARPOS (*it) < to_charpos)))))
8664 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8666 result = MOVE_POS_MATCH_OR_ZV;
8667 break;
8669 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8670 /* If wrap_it is valid, the current position might be in a
8671 word that is wrapped. So, save the iterator in
8672 atpos_it and continue to see if wrapping happens. */
8673 SAVE_IT (atpos_it, *it, atpos_data);
8676 /* Stop when ZV reached.
8677 We used to stop here when TO_CHARPOS reached as well, but that is
8678 too soon if this glyph does not fit on this line. So we handle it
8679 explicitly below. */
8680 if (!get_next_display_element (it))
8682 result = MOVE_POS_MATCH_OR_ZV;
8683 break;
8686 if (it->line_wrap == TRUNCATE)
8688 if (BUFFER_POS_REACHED_P ())
8690 result = MOVE_POS_MATCH_OR_ZV;
8691 break;
8694 else
8696 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
8698 if (IT_DISPLAYING_WHITESPACE (it))
8699 may_wrap = 1;
8700 else if (may_wrap)
8702 /* We have reached a glyph that follows one or more
8703 whitespace characters. If the position is
8704 already found, we are done. */
8705 if (atpos_it.sp >= 0)
8707 RESTORE_IT (it, &atpos_it, atpos_data);
8708 result = MOVE_POS_MATCH_OR_ZV;
8709 goto done;
8711 if (atx_it.sp >= 0)
8713 RESTORE_IT (it, &atx_it, atx_data);
8714 result = MOVE_X_REACHED;
8715 goto done;
8717 /* Otherwise, we can wrap here. */
8718 SAVE_IT (wrap_it, *it, wrap_data);
8719 may_wrap = 0;
8724 /* Remember the line height for the current line, in case
8725 the next element doesn't fit on the line. */
8726 ascent = it->max_ascent;
8727 descent = it->max_descent;
8729 /* The call to produce_glyphs will get the metrics of the
8730 display element IT is loaded with. Record the x-position
8731 before this display element, in case it doesn't fit on the
8732 line. */
8733 x = it->current_x;
8735 PRODUCE_GLYPHS (it);
8737 if (it->area != TEXT_AREA)
8739 prev_method = it->method;
8740 if (it->method == GET_FROM_BUFFER)
8741 prev_pos = IT_CHARPOS (*it);
8742 set_iterator_to_next (it, 1);
8743 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8744 SET_TEXT_POS (this_line_min_pos,
8745 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8746 if (it->bidi_p
8747 && (op & MOVE_TO_POS)
8748 && IT_CHARPOS (*it) > to_charpos
8749 && IT_CHARPOS (*it) < closest_pos)
8750 closest_pos = IT_CHARPOS (*it);
8751 continue;
8754 /* The number of glyphs we get back in IT->nglyphs will normally
8755 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8756 character on a terminal frame, or (iii) a line end. For the
8757 second case, IT->nglyphs - 1 padding glyphs will be present.
8758 (On X frames, there is only one glyph produced for a
8759 composite character.)
8761 The behavior implemented below means, for continuation lines,
8762 that as many spaces of a TAB as fit on the current line are
8763 displayed there. For terminal frames, as many glyphs of a
8764 multi-glyph character are displayed in the current line, too.
8765 This is what the old redisplay code did, and we keep it that
8766 way. Under X, the whole shape of a complex character must
8767 fit on the line or it will be completely displayed in the
8768 next line.
8770 Note that both for tabs and padding glyphs, all glyphs have
8771 the same width. */
8772 if (it->nglyphs)
8774 /* More than one glyph or glyph doesn't fit on line. All
8775 glyphs have the same width. */
8776 int single_glyph_width = it->pixel_width / it->nglyphs;
8777 int new_x;
8778 int x_before_this_char = x;
8779 int hpos_before_this_char = it->hpos;
8781 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8783 new_x = x + single_glyph_width;
8785 /* We want to leave anything reaching TO_X to the caller. */
8786 if ((op & MOVE_TO_X) && new_x > to_x)
8788 if (BUFFER_POS_REACHED_P ())
8790 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8791 goto buffer_pos_reached;
8792 if (atpos_it.sp < 0)
8794 SAVE_IT (atpos_it, *it, atpos_data);
8795 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8798 else
8800 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8802 it->current_x = x;
8803 result = MOVE_X_REACHED;
8804 break;
8806 if (atx_it.sp < 0)
8808 SAVE_IT (atx_it, *it, atx_data);
8809 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8814 if (/* Lines are continued. */
8815 it->line_wrap != TRUNCATE
8816 && (/* And glyph doesn't fit on the line. */
8817 new_x > it->last_visible_x
8818 /* Or it fits exactly and we're on a window
8819 system frame. */
8820 || (new_x == it->last_visible_x
8821 && FRAME_WINDOW_P (it->f)
8822 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8823 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8824 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8826 if (/* IT->hpos == 0 means the very first glyph
8827 doesn't fit on the line, e.g. a wide image. */
8828 it->hpos == 0
8829 || (new_x == it->last_visible_x
8830 && FRAME_WINDOW_P (it->f)))
8832 ++it->hpos;
8833 it->current_x = new_x;
8835 /* The character's last glyph just barely fits
8836 in this row. */
8837 if (i == it->nglyphs - 1)
8839 /* If this is the destination position,
8840 return a position *before* it in this row,
8841 now that we know it fits in this row. */
8842 if (BUFFER_POS_REACHED_P ())
8844 if (it->line_wrap != WORD_WRAP
8845 || wrap_it.sp < 0)
8847 it->hpos = hpos_before_this_char;
8848 it->current_x = x_before_this_char;
8849 result = MOVE_POS_MATCH_OR_ZV;
8850 break;
8852 if (it->line_wrap == WORD_WRAP
8853 && atpos_it.sp < 0)
8855 SAVE_IT (atpos_it, *it, atpos_data);
8856 atpos_it.current_x = x_before_this_char;
8857 atpos_it.hpos = hpos_before_this_char;
8861 prev_method = it->method;
8862 if (it->method == GET_FROM_BUFFER)
8863 prev_pos = IT_CHARPOS (*it);
8864 set_iterator_to_next (it, 1);
8865 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8866 SET_TEXT_POS (this_line_min_pos,
8867 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8868 /* On graphical terminals, newlines may
8869 "overflow" into the fringe if
8870 overflow-newline-into-fringe is non-nil.
8871 On text terminals, and on graphical
8872 terminals with no right margin, newlines
8873 may overflow into the last glyph on the
8874 display line.*/
8875 if (!FRAME_WINDOW_P (it->f)
8876 || ((it->bidi_p
8877 && it->bidi_it.paragraph_dir == R2L)
8878 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8879 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8880 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8882 if (!get_next_display_element (it))
8884 result = MOVE_POS_MATCH_OR_ZV;
8885 break;
8887 if (BUFFER_POS_REACHED_P ())
8889 if (ITERATOR_AT_END_OF_LINE_P (it))
8890 result = MOVE_POS_MATCH_OR_ZV;
8891 else
8892 result = MOVE_LINE_CONTINUED;
8893 break;
8895 if (ITERATOR_AT_END_OF_LINE_P (it)
8896 && (it->line_wrap != WORD_WRAP
8897 || wrap_it.sp < 0
8898 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
8900 result = MOVE_NEWLINE_OR_CR;
8901 break;
8906 else
8907 IT_RESET_X_ASCENT_DESCENT (it);
8909 if (wrap_it.sp >= 0)
8911 RESTORE_IT (it, &wrap_it, wrap_data);
8912 atpos_it.sp = -1;
8913 atx_it.sp = -1;
8916 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8917 IT_CHARPOS (*it)));
8918 result = MOVE_LINE_CONTINUED;
8919 break;
8922 if (BUFFER_POS_REACHED_P ())
8924 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8925 goto buffer_pos_reached;
8926 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8928 SAVE_IT (atpos_it, *it, atpos_data);
8929 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8933 if (new_x > it->first_visible_x)
8935 /* Glyph is visible. Increment number of glyphs that
8936 would be displayed. */
8937 ++it->hpos;
8941 if (result != MOVE_UNDEFINED)
8942 break;
8944 else if (BUFFER_POS_REACHED_P ())
8946 buffer_pos_reached:
8947 IT_RESET_X_ASCENT_DESCENT (it);
8948 result = MOVE_POS_MATCH_OR_ZV;
8949 break;
8951 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8953 /* Stop when TO_X specified and reached. This check is
8954 necessary here because of lines consisting of a line end,
8955 only. The line end will not produce any glyphs and we
8956 would never get MOVE_X_REACHED. */
8957 eassert (it->nglyphs == 0);
8958 result = MOVE_X_REACHED;
8959 break;
8962 /* Is this a line end? If yes, we're done. */
8963 if (ITERATOR_AT_END_OF_LINE_P (it))
8965 /* If we are past TO_CHARPOS, but never saw any character
8966 positions smaller than TO_CHARPOS, return
8967 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8968 did. */
8969 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8971 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8973 if (closest_pos < ZV)
8975 RESTORE_IT (it, &ppos_it, ppos_data);
8976 /* Don't recurse if closest_pos is equal to
8977 to_charpos, since we have just tried that. */
8978 if (closest_pos != to_charpos)
8979 move_it_in_display_line_to (it, closest_pos, -1,
8980 MOVE_TO_POS);
8981 result = MOVE_POS_MATCH_OR_ZV;
8983 else
8984 goto buffer_pos_reached;
8986 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8987 && IT_CHARPOS (*it) > to_charpos)
8988 goto buffer_pos_reached;
8989 else
8990 result = MOVE_NEWLINE_OR_CR;
8992 else
8993 result = MOVE_NEWLINE_OR_CR;
8994 break;
8997 prev_method = it->method;
8998 if (it->method == GET_FROM_BUFFER)
8999 prev_pos = IT_CHARPOS (*it);
9000 /* The current display element has been consumed. Advance
9001 to the next. */
9002 set_iterator_to_next (it, 1);
9003 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
9004 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
9005 if (IT_CHARPOS (*it) < to_charpos)
9006 saw_smaller_pos = 1;
9007 if (it->bidi_p
9008 && (op & MOVE_TO_POS)
9009 && IT_CHARPOS (*it) >= to_charpos
9010 && IT_CHARPOS (*it) < closest_pos)
9011 closest_pos = IT_CHARPOS (*it);
9013 /* Stop if lines are truncated and IT's current x-position is
9014 past the right edge of the window now. */
9015 if (it->line_wrap == TRUNCATE
9016 && it->current_x >= it->last_visible_x)
9018 if (!FRAME_WINDOW_P (it->f)
9019 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
9020 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
9021 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
9022 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
9024 int at_eob_p = 0;
9026 if ((at_eob_p = !get_next_display_element (it))
9027 || BUFFER_POS_REACHED_P ()
9028 /* If we are past TO_CHARPOS, but never saw any
9029 character positions smaller than TO_CHARPOS,
9030 return MOVE_POS_MATCH_OR_ZV, like the
9031 unidirectional display did. */
9032 || (it->bidi_p && (op & MOVE_TO_POS) != 0
9033 && !saw_smaller_pos
9034 && IT_CHARPOS (*it) > to_charpos))
9036 if (it->bidi_p
9037 && !BUFFER_POS_REACHED_P ()
9038 && !at_eob_p && closest_pos < ZV)
9040 RESTORE_IT (it, &ppos_it, ppos_data);
9041 if (closest_pos != to_charpos)
9042 move_it_in_display_line_to (it, closest_pos, -1,
9043 MOVE_TO_POS);
9045 result = MOVE_POS_MATCH_OR_ZV;
9046 break;
9048 if (ITERATOR_AT_END_OF_LINE_P (it))
9050 result = MOVE_NEWLINE_OR_CR;
9051 break;
9054 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
9055 && !saw_smaller_pos
9056 && IT_CHARPOS (*it) > to_charpos)
9058 if (closest_pos < ZV)
9060 RESTORE_IT (it, &ppos_it, ppos_data);
9061 if (closest_pos != to_charpos)
9062 move_it_in_display_line_to (it, closest_pos, -1,
9063 MOVE_TO_POS);
9065 result = MOVE_POS_MATCH_OR_ZV;
9066 break;
9068 result = MOVE_LINE_TRUNCATED;
9069 break;
9071 #undef IT_RESET_X_ASCENT_DESCENT
9074 #undef BUFFER_POS_REACHED_P
9076 /* If we scanned beyond to_pos and didn't find a point to wrap at,
9077 restore the saved iterator. */
9078 if (atpos_it.sp >= 0)
9079 RESTORE_IT (it, &atpos_it, atpos_data);
9080 else if (atx_it.sp >= 0)
9081 RESTORE_IT (it, &atx_it, atx_data);
9083 done:
9085 if (atpos_data)
9086 bidi_unshelve_cache (atpos_data, 1);
9087 if (atx_data)
9088 bidi_unshelve_cache (atx_data, 1);
9089 if (wrap_data)
9090 bidi_unshelve_cache (wrap_data, 1);
9091 if (ppos_data)
9092 bidi_unshelve_cache (ppos_data, 1);
9094 /* Restore the iterator settings altered at the beginning of this
9095 function. */
9096 it->glyph_row = saved_glyph_row;
9097 return result;
9100 /* For external use. */
9101 void
9102 move_it_in_display_line (struct it *it,
9103 ptrdiff_t to_charpos, int to_x,
9104 enum move_operation_enum op)
9106 if (it->line_wrap == WORD_WRAP
9107 && (op & MOVE_TO_X))
9109 struct it save_it;
9110 void *save_data = NULL;
9111 int skip;
9113 SAVE_IT (save_it, *it, save_data);
9114 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9115 /* When word-wrap is on, TO_X may lie past the end
9116 of a wrapped line. Then it->current is the
9117 character on the next line, so backtrack to the
9118 space before the wrap point. */
9119 if (skip == MOVE_LINE_CONTINUED)
9121 int prev_x = max (it->current_x - 1, 0);
9122 RESTORE_IT (it, &save_it, save_data);
9123 move_it_in_display_line_to
9124 (it, -1, prev_x, MOVE_TO_X);
9126 else
9127 bidi_unshelve_cache (save_data, 1);
9129 else
9130 move_it_in_display_line_to (it, to_charpos, to_x, op);
9134 /* Move IT forward until it satisfies one or more of the criteria in
9135 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
9137 OP is a bit-mask that specifies where to stop, and in particular,
9138 which of those four position arguments makes a difference. See the
9139 description of enum move_operation_enum.
9141 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
9142 screen line, this function will set IT to the next position that is
9143 displayed to the right of TO_CHARPOS on the screen.
9145 Return the maximum pixel length of any line scanned but never more
9146 than it.last_visible_x. */
9149 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
9151 enum move_it_result skip, skip2 = MOVE_X_REACHED;
9152 int line_height, line_start_x = 0, reached = 0;
9153 int max_current_x = 0;
9154 void *backup_data = NULL;
9156 for (;;)
9158 if (op & MOVE_TO_VPOS)
9160 /* If no TO_CHARPOS and no TO_X specified, stop at the
9161 start of the line TO_VPOS. */
9162 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
9164 if (it->vpos == to_vpos)
9166 reached = 1;
9167 break;
9169 else
9170 skip = move_it_in_display_line_to (it, -1, -1, 0);
9172 else
9174 /* TO_VPOS >= 0 means stop at TO_X in the line at
9175 TO_VPOS, or at TO_POS, whichever comes first. */
9176 if (it->vpos == to_vpos)
9178 reached = 2;
9179 break;
9182 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
9184 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
9186 reached = 3;
9187 break;
9189 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
9191 /* We have reached TO_X but not in the line we want. */
9192 skip = move_it_in_display_line_to (it, to_charpos,
9193 -1, MOVE_TO_POS);
9194 if (skip == MOVE_POS_MATCH_OR_ZV)
9196 reached = 4;
9197 break;
9202 else if (op & MOVE_TO_Y)
9204 struct it it_backup;
9206 if (it->line_wrap == WORD_WRAP)
9207 SAVE_IT (it_backup, *it, backup_data);
9209 /* TO_Y specified means stop at TO_X in the line containing
9210 TO_Y---or at TO_CHARPOS if this is reached first. The
9211 problem is that we can't really tell whether the line
9212 contains TO_Y before we have completely scanned it, and
9213 this may skip past TO_X. What we do is to first scan to
9214 TO_X.
9216 If TO_X is not specified, use a TO_X of zero. The reason
9217 is to make the outcome of this function more predictable.
9218 If we didn't use TO_X == 0, we would stop at the end of
9219 the line which is probably not what a caller would expect
9220 to happen. */
9221 skip = move_it_in_display_line_to
9222 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
9223 (MOVE_TO_X | (op & MOVE_TO_POS)));
9225 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
9226 if (skip == MOVE_POS_MATCH_OR_ZV)
9227 reached = 5;
9228 else if (skip == MOVE_X_REACHED)
9230 /* If TO_X was reached, we want to know whether TO_Y is
9231 in the line. We know this is the case if the already
9232 scanned glyphs make the line tall enough. Otherwise,
9233 we must check by scanning the rest of the line. */
9234 line_height = it->max_ascent + it->max_descent;
9235 if (to_y >= it->current_y
9236 && to_y < it->current_y + line_height)
9238 reached = 6;
9239 break;
9241 SAVE_IT (it_backup, *it, backup_data);
9242 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
9243 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
9244 op & MOVE_TO_POS);
9245 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
9246 line_height = it->max_ascent + it->max_descent;
9247 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9249 if (to_y >= it->current_y
9250 && to_y < it->current_y + line_height)
9252 /* If TO_Y is in this line and TO_X was reached
9253 above, we scanned too far. We have to restore
9254 IT's settings to the ones before skipping. But
9255 keep the more accurate values of max_ascent and
9256 max_descent we've found while skipping the rest
9257 of the line, for the sake of callers, such as
9258 pos_visible_p, that need to know the line
9259 height. */
9260 int max_ascent = it->max_ascent;
9261 int max_descent = it->max_descent;
9263 RESTORE_IT (it, &it_backup, backup_data);
9264 it->max_ascent = max_ascent;
9265 it->max_descent = max_descent;
9266 reached = 6;
9268 else
9270 skip = skip2;
9271 if (skip == MOVE_POS_MATCH_OR_ZV)
9272 reached = 7;
9275 else
9277 /* Check whether TO_Y is in this line. */
9278 line_height = it->max_ascent + it->max_descent;
9279 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
9281 if (to_y >= it->current_y
9282 && to_y < it->current_y + line_height)
9284 if (to_y > it->current_y)
9285 max_current_x = max (it->current_x, max_current_x);
9287 /* When word-wrap is on, TO_X may lie past the end
9288 of a wrapped line. Then it->current is the
9289 character on the next line, so backtrack to the
9290 space before the wrap point. */
9291 if (skip == MOVE_LINE_CONTINUED
9292 && it->line_wrap == WORD_WRAP)
9294 int prev_x = max (it->current_x - 1, 0);
9295 RESTORE_IT (it, &it_backup, backup_data);
9296 skip = move_it_in_display_line_to
9297 (it, -1, prev_x, MOVE_TO_X);
9300 reached = 6;
9304 if (reached)
9306 max_current_x = max (it->current_x, max_current_x);
9307 break;
9310 else if (BUFFERP (it->object)
9311 && (it->method == GET_FROM_BUFFER
9312 || it->method == GET_FROM_STRETCH)
9313 && IT_CHARPOS (*it) >= to_charpos
9314 /* Under bidi iteration, a call to set_iterator_to_next
9315 can scan far beyond to_charpos if the initial
9316 portion of the next line needs to be reordered. In
9317 that case, give move_it_in_display_line_to another
9318 chance below. */
9319 && !(it->bidi_p
9320 && it->bidi_it.scan_dir == -1))
9321 skip = MOVE_POS_MATCH_OR_ZV;
9322 else
9323 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
9325 switch (skip)
9327 case MOVE_POS_MATCH_OR_ZV:
9328 max_current_x = max (it->current_x, max_current_x);
9329 reached = 8;
9330 goto out;
9332 case MOVE_NEWLINE_OR_CR:
9333 max_current_x = max (it->current_x, max_current_x);
9334 set_iterator_to_next (it, 1);
9335 it->continuation_lines_width = 0;
9336 break;
9338 case MOVE_LINE_TRUNCATED:
9339 max_current_x = it->last_visible_x;
9340 it->continuation_lines_width = 0;
9341 reseat_at_next_visible_line_start (it, 0);
9342 if ((op & MOVE_TO_POS) != 0
9343 && IT_CHARPOS (*it) > to_charpos)
9345 reached = 9;
9346 goto out;
9348 break;
9350 case MOVE_LINE_CONTINUED:
9351 max_current_x = it->last_visible_x;
9352 /* For continued lines ending in a tab, some of the glyphs
9353 associated with the tab are displayed on the current
9354 line. Since it->current_x does not include these glyphs,
9355 we use it->last_visible_x instead. */
9356 if (it->c == '\t')
9358 it->continuation_lines_width += it->last_visible_x;
9359 /* When moving by vpos, ensure that the iterator really
9360 advances to the next line (bug#847, bug#969). Fixme:
9361 do we need to do this in other circumstances? */
9362 if (it->current_x != it->last_visible_x
9363 && (op & MOVE_TO_VPOS)
9364 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9366 line_start_x = it->current_x + it->pixel_width
9367 - it->last_visible_x;
9368 if (FRAME_WINDOW_P (it->f))
9370 struct face *face = FACE_FROM_ID (it->f, it->face_id);
9371 struct font *face_font = face->font;
9373 /* When display_line produces a continued line
9374 that ends in a TAB, it skips a tab stop that
9375 is closer than the font's space character
9376 width (see x_produce_glyphs where it produces
9377 the stretch glyph which represents a TAB).
9378 We need to reproduce the same logic here. */
9379 eassert (face_font);
9380 if (face_font)
9382 if (line_start_x < face_font->space_width)
9383 line_start_x
9384 += it->tab_width * face_font->space_width;
9387 set_iterator_to_next (it, 0);
9390 else
9391 it->continuation_lines_width += it->current_x;
9392 break;
9394 default:
9395 emacs_abort ();
9398 /* Reset/increment for the next run. */
9399 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9400 it->current_x = line_start_x;
9401 line_start_x = 0;
9402 it->hpos = 0;
9403 it->current_y += it->max_ascent + it->max_descent;
9404 ++it->vpos;
9405 last_height = it->max_ascent + it->max_descent;
9406 it->max_ascent = it->max_descent = 0;
9409 out:
9411 /* On text terminals, we may stop at the end of a line in the middle
9412 of a multi-character glyph. If the glyph itself is continued,
9413 i.e. it is actually displayed on the next line, don't treat this
9414 stopping point as valid; move to the next line instead (unless
9415 that brings us offscreen). */
9416 if (!FRAME_WINDOW_P (it->f)
9417 && op & MOVE_TO_POS
9418 && IT_CHARPOS (*it) == to_charpos
9419 && it->what == IT_CHARACTER
9420 && it->nglyphs > 1
9421 && it->line_wrap == WINDOW_WRAP
9422 && it->current_x == it->last_visible_x - 1
9423 && it->c != '\n'
9424 && it->c != '\t'
9425 && it->vpos < it->w->window_end_vpos)
9427 it->continuation_lines_width += it->current_x;
9428 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9429 it->current_y += it->max_ascent + it->max_descent;
9430 ++it->vpos;
9431 last_height = it->max_ascent + it->max_descent;
9434 if (backup_data)
9435 bidi_unshelve_cache (backup_data, 1);
9437 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9439 return max_current_x;
9443 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9445 If DY > 0, move IT backward at least that many pixels. DY = 0
9446 means move IT backward to the preceding line start or BEGV. This
9447 function may move over more than DY pixels if IT->current_y - DY
9448 ends up in the middle of a line; in this case IT->current_y will be
9449 set to the top of the line moved to. */
9451 void
9452 move_it_vertically_backward (struct it *it, int dy)
9454 int nlines, h;
9455 struct it it2, it3;
9456 void *it2data = NULL, *it3data = NULL;
9457 ptrdiff_t start_pos;
9458 int nchars_per_row
9459 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9460 ptrdiff_t pos_limit;
9462 move_further_back:
9463 eassert (dy >= 0);
9465 start_pos = IT_CHARPOS (*it);
9467 /* Estimate how many newlines we must move back. */
9468 nlines = max (1, dy / default_line_pixel_height (it->w));
9469 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9470 pos_limit = BEGV;
9471 else
9472 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9474 /* Set the iterator's position that many lines back. But don't go
9475 back more than NLINES full screen lines -- this wins a day with
9476 buffers which have very long lines. */
9477 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9478 back_to_previous_visible_line_start (it);
9480 /* Reseat the iterator here. When moving backward, we don't want
9481 reseat to skip forward over invisible text, set up the iterator
9482 to deliver from overlay strings at the new position etc. So,
9483 use reseat_1 here. */
9484 reseat_1 (it, it->current.pos, 1);
9486 /* We are now surely at a line start. */
9487 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9488 reordering is in effect. */
9489 it->continuation_lines_width = 0;
9491 /* Move forward and see what y-distance we moved. First move to the
9492 start of the next line so that we get its height. We need this
9493 height to be able to tell whether we reached the specified
9494 y-distance. */
9495 SAVE_IT (it2, *it, it2data);
9496 it2.max_ascent = it2.max_descent = 0;
9499 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9500 MOVE_TO_POS | MOVE_TO_VPOS);
9502 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9503 /* If we are in a display string which starts at START_POS,
9504 and that display string includes a newline, and we are
9505 right after that newline (i.e. at the beginning of a
9506 display line), exit the loop, because otherwise we will
9507 infloop, since move_it_to will see that it is already at
9508 START_POS and will not move. */
9509 || (it2.method == GET_FROM_STRING
9510 && IT_CHARPOS (it2) == start_pos
9511 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9512 eassert (IT_CHARPOS (*it) >= BEGV);
9513 SAVE_IT (it3, it2, it3data);
9515 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9516 eassert (IT_CHARPOS (*it) >= BEGV);
9517 /* H is the actual vertical distance from the position in *IT
9518 and the starting position. */
9519 h = it2.current_y - it->current_y;
9520 /* NLINES is the distance in number of lines. */
9521 nlines = it2.vpos - it->vpos;
9523 /* Correct IT's y and vpos position
9524 so that they are relative to the starting point. */
9525 it->vpos -= nlines;
9526 it->current_y -= h;
9528 if (dy == 0)
9530 /* DY == 0 means move to the start of the screen line. The
9531 value of nlines is > 0 if continuation lines were involved,
9532 or if the original IT position was at start of a line. */
9533 RESTORE_IT (it, it, it2data);
9534 if (nlines > 0)
9535 move_it_by_lines (it, nlines);
9536 /* The above code moves us to some position NLINES down,
9537 usually to its first glyph (leftmost in an L2R line), but
9538 that's not necessarily the start of the line, under bidi
9539 reordering. We want to get to the character position
9540 that is immediately after the newline of the previous
9541 line. */
9542 if (it->bidi_p
9543 && !it->continuation_lines_width
9544 && !STRINGP (it->string)
9545 && IT_CHARPOS (*it) > BEGV
9546 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9548 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9550 DEC_BOTH (cp, bp);
9551 cp = find_newline_no_quit (cp, bp, -1, NULL);
9552 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9554 bidi_unshelve_cache (it3data, 1);
9556 else
9558 /* The y-position we try to reach, relative to *IT.
9559 Note that H has been subtracted in front of the if-statement. */
9560 int target_y = it->current_y + h - dy;
9561 int y0 = it3.current_y;
9562 int y1;
9563 int line_height;
9565 RESTORE_IT (&it3, &it3, it3data);
9566 y1 = line_bottom_y (&it3);
9567 line_height = y1 - y0;
9568 RESTORE_IT (it, it, it2data);
9569 /* If we did not reach target_y, try to move further backward if
9570 we can. If we moved too far backward, try to move forward. */
9571 if (target_y < it->current_y
9572 /* This is heuristic. In a window that's 3 lines high, with
9573 a line height of 13 pixels each, recentering with point
9574 on the bottom line will try to move -39/2 = 19 pixels
9575 backward. Try to avoid moving into the first line. */
9576 && (it->current_y - target_y
9577 > min (window_box_height (it->w), line_height * 2 / 3))
9578 && IT_CHARPOS (*it) > BEGV)
9580 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9581 target_y - it->current_y));
9582 dy = it->current_y - target_y;
9583 goto move_further_back;
9585 else if (target_y >= it->current_y + line_height
9586 && IT_CHARPOS (*it) < ZV)
9588 /* Should move forward by at least one line, maybe more.
9590 Note: Calling move_it_by_lines can be expensive on
9591 terminal frames, where compute_motion is used (via
9592 vmotion) to do the job, when there are very long lines
9593 and truncate-lines is nil. That's the reason for
9594 treating terminal frames specially here. */
9596 if (!FRAME_WINDOW_P (it->f))
9597 move_it_vertically (it, target_y - (it->current_y + line_height));
9598 else
9602 move_it_by_lines (it, 1);
9604 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9611 /* Move IT by a specified amount of pixel lines DY. DY negative means
9612 move backwards. DY = 0 means move to start of screen line. At the
9613 end, IT will be on the start of a screen line. */
9615 void
9616 move_it_vertically (struct it *it, int dy)
9618 if (dy <= 0)
9619 move_it_vertically_backward (it, -dy);
9620 else
9622 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9623 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9624 MOVE_TO_POS | MOVE_TO_Y);
9625 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9627 /* If buffer ends in ZV without a newline, move to the start of
9628 the line to satisfy the post-condition. */
9629 if (IT_CHARPOS (*it) == ZV
9630 && ZV > BEGV
9631 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9632 move_it_by_lines (it, 0);
9637 /* Move iterator IT past the end of the text line it is in. */
9639 void
9640 move_it_past_eol (struct it *it)
9642 enum move_it_result rc;
9644 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9645 if (rc == MOVE_NEWLINE_OR_CR)
9646 set_iterator_to_next (it, 0);
9650 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9651 negative means move up. DVPOS == 0 means move to the start of the
9652 screen line.
9654 Optimization idea: If we would know that IT->f doesn't use
9655 a face with proportional font, we could be faster for
9656 truncate-lines nil. */
9658 void
9659 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9662 /* The commented-out optimization uses vmotion on terminals. This
9663 gives bad results, because elements like it->what, on which
9664 callers such as pos_visible_p rely, aren't updated. */
9665 /* struct position pos;
9666 if (!FRAME_WINDOW_P (it->f))
9668 struct text_pos textpos;
9670 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9671 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9672 reseat (it, textpos, 1);
9673 it->vpos += pos.vpos;
9674 it->current_y += pos.vpos;
9676 else */
9678 if (dvpos == 0)
9680 /* DVPOS == 0 means move to the start of the screen line. */
9681 move_it_vertically_backward (it, 0);
9682 /* Let next call to line_bottom_y calculate real line height. */
9683 last_height = 0;
9685 else if (dvpos > 0)
9687 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9688 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9690 /* Only move to the next buffer position if we ended up in a
9691 string from display property, not in an overlay string
9692 (before-string or after-string). That is because the
9693 latter don't conceal the underlying buffer position, so
9694 we can ask to move the iterator to the exact position we
9695 are interested in. Note that, even if we are already at
9696 IT_CHARPOS (*it), the call below is not a no-op, as it
9697 will detect that we are at the end of the string, pop the
9698 iterator, and compute it->current_x and it->hpos
9699 correctly. */
9700 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9701 -1, -1, -1, MOVE_TO_POS);
9704 else
9706 struct it it2;
9707 void *it2data = NULL;
9708 ptrdiff_t start_charpos, i;
9709 int nchars_per_row
9710 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9711 bool hit_pos_limit = false;
9712 ptrdiff_t pos_limit;
9714 /* Start at the beginning of the screen line containing IT's
9715 position. This may actually move vertically backwards,
9716 in case of overlays, so adjust dvpos accordingly. */
9717 dvpos += it->vpos;
9718 move_it_vertically_backward (it, 0);
9719 dvpos -= it->vpos;
9721 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9722 screen lines, and reseat the iterator there. */
9723 start_charpos = IT_CHARPOS (*it);
9724 if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
9725 pos_limit = BEGV;
9726 else
9727 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9729 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9730 back_to_previous_visible_line_start (it);
9731 if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
9732 hit_pos_limit = true;
9733 reseat (it, it->current.pos, 1);
9735 /* Move further back if we end up in a string or an image. */
9736 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9738 /* First try to move to start of display line. */
9739 dvpos += it->vpos;
9740 move_it_vertically_backward (it, 0);
9741 dvpos -= it->vpos;
9742 if (IT_POS_VALID_AFTER_MOVE_P (it))
9743 break;
9744 /* If start of line is still in string or image,
9745 move further back. */
9746 back_to_previous_visible_line_start (it);
9747 reseat (it, it->current.pos, 1);
9748 dvpos--;
9751 it->current_x = it->hpos = 0;
9753 /* Above call may have moved too far if continuation lines
9754 are involved. Scan forward and see if it did. */
9755 SAVE_IT (it2, *it, it2data);
9756 it2.vpos = it2.current_y = 0;
9757 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9758 it->vpos -= it2.vpos;
9759 it->current_y -= it2.current_y;
9760 it->current_x = it->hpos = 0;
9762 /* If we moved too far back, move IT some lines forward. */
9763 if (it2.vpos > -dvpos)
9765 int delta = it2.vpos + dvpos;
9767 RESTORE_IT (&it2, &it2, it2data);
9768 SAVE_IT (it2, *it, it2data);
9769 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9770 /* Move back again if we got too far ahead. */
9771 if (IT_CHARPOS (*it) >= start_charpos)
9772 RESTORE_IT (it, &it2, it2data);
9773 else
9774 bidi_unshelve_cache (it2data, 1);
9776 else if (hit_pos_limit && pos_limit > BEGV
9777 && dvpos < 0 && it2.vpos < -dvpos)
9779 /* If we hit the limit, but still didn't make it far enough
9780 back, that means there's a display string with a newline
9781 covering a large chunk of text, and that caused
9782 back_to_previous_visible_line_start try to go too far.
9783 Punish those who commit such atrocities by going back
9784 until we've reached DVPOS, after lifting the limit, which
9785 could make it slow for very long lines. "If it hurts,
9786 don't do that!" */
9787 dvpos += it2.vpos;
9788 RESTORE_IT (it, it, it2data);
9789 for (i = -dvpos; i > 0; --i)
9791 back_to_previous_visible_line_start (it);
9792 it->vpos--;
9794 reseat_1 (it, it->current.pos, 1);
9796 else
9797 RESTORE_IT (it, it, it2data);
9801 /* Return true if IT points into the middle of a display vector. */
9803 bool
9804 in_display_vector_p (struct it *it)
9806 return (it->method == GET_FROM_DISPLAY_VECTOR
9807 && it->current.dpvec_index > 0
9808 && it->dpvec + it->current.dpvec_index != it->dpend);
9811 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 6, 0,
9812 doc: /* Return the size of the text of WINDOW's buffer in pixels.
9813 WINDOW must be a live window and defaults to the selected one. The
9814 return value is a cons of the maximum pixel-width of any text line and
9815 the maximum pixel-height of all text lines.
9817 The optional argument FROM, if non-nil, specifies the first text
9818 position and defaults to the minimum accessible position of the buffer.
9819 If FROM is t, use the minimum accessible position that is not a newline
9820 character. TO, if non-nil, specifies the last text position and
9821 defaults to the maximum accessible position of the buffer. If TO is t,
9822 use the maximum accessible position that is not a newline character.
9824 The optional argument X-LIMIT, if non-nil, specifies the maximum text
9825 width that can be returned. X-LIMIT nil or omitted, means to use the
9826 pixel-width of WINDOW's body; use this if you do not intend to change
9827 the width of WINDOW. Use the maximum width WINDOW may assume if you
9828 intend to change WINDOW's width. In any case, text whose x-coordinate
9829 is beyond X-LIMIT is ignored. Since calculating the width of long lines
9830 can take some time, it's always a good idea to make this argument as
9831 small as possible; in particular, if the buffer contains long lines that
9832 shall be truncated anyway.
9834 The optional argument Y-LIMIT, if non-nil, specifies the maximum text
9835 height that can be returned. Text lines whose y-coordinate is beyond
9836 Y-LIMIT are ignored. Since calculating the text height of a large
9837 buffer can take some time, it makes sense to specify this argument if
9838 the size of the buffer is unknown.
9840 Optional argument MODE-AND-HEADER-LINE nil or omitted means do not
9841 include the height of the mode- or header-line of WINDOW in the return
9842 value. If it is either the symbol `mode-line' or `header-line', include
9843 only the height of that line, if present, in the return value. If t,
9844 include the height of both, if present, in the return value. */)
9845 (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit, Lisp_Object y_limit,
9846 Lisp_Object mode_and_header_line)
9848 struct window *w = decode_live_window (window);
9849 Lisp_Object buf;
9850 struct buffer *b;
9851 struct it it;
9852 struct buffer *old_buffer = NULL;
9853 ptrdiff_t start, end, pos;
9854 struct text_pos startp;
9855 void *itdata = NULL;
9856 int c, max_y = -1, x = 0, y = 0;
9858 buf = w->contents;
9859 CHECK_BUFFER (buf);
9860 b = XBUFFER (buf);
9862 if (b != current_buffer)
9864 old_buffer = current_buffer;
9865 set_buffer_internal (b);
9868 if (NILP (from))
9869 start = BEGV;
9870 else if (EQ (from, Qt))
9872 start = pos = BEGV;
9873 while ((pos++ < ZV) && (c = FETCH_CHAR (pos))
9874 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9875 start = pos;
9876 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9877 start = pos;
9879 else
9881 CHECK_NUMBER_COERCE_MARKER (from);
9882 start = min (max (XINT (from), BEGV), ZV);
9885 if (NILP (to))
9886 end = ZV;
9887 else if (EQ (to, Qt))
9889 end = pos = ZV;
9890 while ((pos-- > BEGV) && (c = FETCH_CHAR (pos))
9891 && (c == ' ' || c == '\t' || c == '\n' || c == '\r'))
9892 end = pos;
9893 while ((pos++ < ZV) && (c = FETCH_CHAR (pos)) && (c == ' ' || c == '\t'))
9894 end = pos;
9896 else
9898 CHECK_NUMBER_COERCE_MARKER (to);
9899 end = max (start, min (XINT (to), ZV));
9902 if (!NILP (y_limit))
9904 CHECK_NUMBER (y_limit);
9905 max_y = min (XINT (y_limit), INT_MAX);
9908 itdata = bidi_shelve_cache ();
9909 SET_TEXT_POS (startp, start, CHAR_TO_BYTE (start));
9910 start_display (&it, w, startp);
9912 if (NILP (x_limit))
9913 x = move_it_to (&it, end, -1, max_y, -1, MOVE_TO_POS | MOVE_TO_Y);
9914 else
9916 CHECK_NUMBER (x_limit);
9917 it.last_visible_x = min (XINT (x_limit), INFINITY);
9918 /* Actually, we never want move_it_to stop at to_x. But to make
9919 sure that move_it_in_display_line_to always moves far enough,
9920 we set it to INT_MAX and specify MOVE_TO_X. */
9921 x = move_it_to (&it, end, INT_MAX, max_y, -1,
9922 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
9925 y = it.current_y + it.max_ascent + it.max_descent;
9927 if (!EQ (mode_and_header_line, Qheader_line)
9928 && !EQ (mode_and_header_line, Qt))
9929 /* Do not count the header-line which was counted automatically by
9930 start_display. */
9931 y = y - WINDOW_HEADER_LINE_HEIGHT (w);
9933 if (EQ (mode_and_header_line, Qmode_line)
9934 || EQ (mode_and_header_line, Qt))
9935 /* Do count the mode-line which is not included automatically by
9936 start_display. */
9937 y = y + WINDOW_MODE_LINE_HEIGHT (w);
9939 bidi_unshelve_cache (itdata, 0);
9941 if (old_buffer)
9942 set_buffer_internal (old_buffer);
9944 return Fcons (make_number (x), make_number (y));
9947 /***********************************************************************
9948 Messages
9949 ***********************************************************************/
9952 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9953 to *Messages*. */
9955 void
9956 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9958 Lisp_Object args[3];
9959 Lisp_Object msg, fmt;
9960 char *buffer;
9961 ptrdiff_t len;
9962 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9963 USE_SAFE_ALLOCA;
9965 fmt = msg = Qnil;
9966 GCPRO4 (fmt, msg, arg1, arg2);
9968 args[0] = fmt = build_string (format);
9969 args[1] = arg1;
9970 args[2] = arg2;
9971 msg = Fformat (3, args);
9973 len = SBYTES (msg) + 1;
9974 buffer = SAFE_ALLOCA (len);
9975 memcpy (buffer, SDATA (msg), len);
9977 message_dolog (buffer, len - 1, 1, 0);
9978 SAFE_FREE ();
9980 UNGCPRO;
9984 /* Output a newline in the *Messages* buffer if "needs" one. */
9986 void
9987 message_log_maybe_newline (void)
9989 if (message_log_need_newline)
9990 message_dolog ("", 0, 1, 0);
9994 /* Add a string M of length NBYTES to the message log, optionally
9995 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9996 true, means interpret the contents of M as multibyte. This
9997 function calls low-level routines in order to bypass text property
9998 hooks, etc. which might not be safe to run.
10000 This may GC (insert may run before/after change hooks),
10001 so the buffer M must NOT point to a Lisp string. */
10003 void
10004 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
10006 const unsigned char *msg = (const unsigned char *) m;
10008 if (!NILP (Vmemory_full))
10009 return;
10011 if (!NILP (Vmessage_log_max))
10013 struct buffer *oldbuf;
10014 Lisp_Object oldpoint, oldbegv, oldzv;
10015 int old_windows_or_buffers_changed = windows_or_buffers_changed;
10016 ptrdiff_t point_at_end = 0;
10017 ptrdiff_t zv_at_end = 0;
10018 Lisp_Object old_deactivate_mark;
10019 struct gcpro gcpro1;
10021 old_deactivate_mark = Vdeactivate_mark;
10022 oldbuf = current_buffer;
10024 /* Ensure the Messages buffer exists, and switch to it.
10025 If we created it, set the major-mode. */
10027 int newbuffer = 0;
10028 if (NILP (Fget_buffer (Vmessages_buffer_name))) newbuffer = 1;
10030 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
10032 if (newbuffer
10033 && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
10034 call0 (intern ("messages-buffer-mode"));
10037 bset_undo_list (current_buffer, Qt);
10038 bset_cache_long_scans (current_buffer, Qnil);
10040 oldpoint = message_dolog_marker1;
10041 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
10042 oldbegv = message_dolog_marker2;
10043 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
10044 oldzv = message_dolog_marker3;
10045 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
10046 GCPRO1 (old_deactivate_mark);
10048 if (PT == Z)
10049 point_at_end = 1;
10050 if (ZV == Z)
10051 zv_at_end = 1;
10053 BEGV = BEG;
10054 BEGV_BYTE = BEG_BYTE;
10055 ZV = Z;
10056 ZV_BYTE = Z_BYTE;
10057 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10059 /* Insert the string--maybe converting multibyte to single byte
10060 or vice versa, so that all the text fits the buffer. */
10061 if (multibyte
10062 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10064 ptrdiff_t i;
10065 int c, char_bytes;
10066 char work[1];
10068 /* Convert a multibyte string to single-byte
10069 for the *Message* buffer. */
10070 for (i = 0; i < nbytes; i += char_bytes)
10072 c = string_char_and_length (msg + i, &char_bytes);
10073 work[0] = CHAR_TO_BYTE8 (c);
10074 insert_1_both (work, 1, 1, 1, 0, 0);
10077 else if (! multibyte
10078 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
10080 ptrdiff_t i;
10081 int c, char_bytes;
10082 unsigned char str[MAX_MULTIBYTE_LENGTH];
10083 /* Convert a single-byte string to multibyte
10084 for the *Message* buffer. */
10085 for (i = 0; i < nbytes; i++)
10087 c = msg[i];
10088 MAKE_CHAR_MULTIBYTE (c);
10089 char_bytes = CHAR_STRING (c, str);
10090 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
10093 else if (nbytes)
10094 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
10096 if (nlflag)
10098 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
10099 printmax_t dups;
10101 insert_1_both ("\n", 1, 1, 1, 0, 0);
10103 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
10104 this_bol = PT;
10105 this_bol_byte = PT_BYTE;
10107 /* See if this line duplicates the previous one.
10108 If so, combine duplicates. */
10109 if (this_bol > BEG)
10111 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
10112 prev_bol = PT;
10113 prev_bol_byte = PT_BYTE;
10115 dups = message_log_check_duplicate (prev_bol_byte,
10116 this_bol_byte);
10117 if (dups)
10119 del_range_both (prev_bol, prev_bol_byte,
10120 this_bol, this_bol_byte, 0);
10121 if (dups > 1)
10123 char dupstr[sizeof " [ times]"
10124 + INT_STRLEN_BOUND (printmax_t)];
10126 /* If you change this format, don't forget to also
10127 change message_log_check_duplicate. */
10128 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
10129 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
10130 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
10135 /* If we have more than the desired maximum number of lines
10136 in the *Messages* buffer now, delete the oldest ones.
10137 This is safe because we don't have undo in this buffer. */
10139 if (NATNUMP (Vmessage_log_max))
10141 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
10142 -XFASTINT (Vmessage_log_max) - 1, 0);
10143 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
10146 BEGV = marker_position (oldbegv);
10147 BEGV_BYTE = marker_byte_position (oldbegv);
10149 if (zv_at_end)
10151 ZV = Z;
10152 ZV_BYTE = Z_BYTE;
10154 else
10156 ZV = marker_position (oldzv);
10157 ZV_BYTE = marker_byte_position (oldzv);
10160 if (point_at_end)
10161 TEMP_SET_PT_BOTH (Z, Z_BYTE);
10162 else
10163 /* We can't do Fgoto_char (oldpoint) because it will run some
10164 Lisp code. */
10165 TEMP_SET_PT_BOTH (marker_position (oldpoint),
10166 marker_byte_position (oldpoint));
10168 UNGCPRO;
10169 unchain_marker (XMARKER (oldpoint));
10170 unchain_marker (XMARKER (oldbegv));
10171 unchain_marker (XMARKER (oldzv));
10173 /* We called insert_1_both above with its 5th argument (PREPARE)
10174 zero, which prevents insert_1_both from calling
10175 prepare_to_modify_buffer, which in turns prevents us from
10176 incrementing windows_or_buffers_changed even if *Messages* is
10177 shown in some window. So we must manually set
10178 windows_or_buffers_changed here to make up for that. */
10179 windows_or_buffers_changed = old_windows_or_buffers_changed;
10180 bset_redisplay (current_buffer);
10182 set_buffer_internal (oldbuf);
10184 message_log_need_newline = !nlflag;
10185 Vdeactivate_mark = old_deactivate_mark;
10190 /* We are at the end of the buffer after just having inserted a newline.
10191 (Note: We depend on the fact we won't be crossing the gap.)
10192 Check to see if the most recent message looks a lot like the previous one.
10193 Return 0 if different, 1 if the new one should just replace it, or a
10194 value N > 1 if we should also append " [N times]". */
10196 static intmax_t
10197 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
10199 ptrdiff_t i;
10200 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
10201 int seen_dots = 0;
10202 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
10203 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
10205 for (i = 0; i < len; i++)
10207 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
10208 seen_dots = 1;
10209 if (p1[i] != p2[i])
10210 return seen_dots;
10212 p1 += len;
10213 if (*p1 == '\n')
10214 return 2;
10215 if (*p1++ == ' ' && *p1++ == '[')
10217 char *pend;
10218 intmax_t n = strtoimax ((char *) p1, &pend, 10);
10219 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
10220 return n + 1;
10222 return 0;
10226 /* Display an echo area message M with a specified length of NBYTES
10227 bytes. The string may include null characters. If M is not a
10228 string, clear out any existing message, and let the mini-buffer
10229 text show through.
10231 This function cancels echoing. */
10233 void
10234 message3 (Lisp_Object m)
10236 struct gcpro gcpro1;
10238 GCPRO1 (m);
10239 clear_message (true, true);
10240 cancel_echoing ();
10242 /* First flush out any partial line written with print. */
10243 message_log_maybe_newline ();
10244 if (STRINGP (m))
10246 ptrdiff_t nbytes = SBYTES (m);
10247 bool multibyte = STRING_MULTIBYTE (m);
10248 char *buffer;
10249 USE_SAFE_ALLOCA;
10250 SAFE_ALLOCA_STRING (buffer, m);
10251 message_dolog (buffer, nbytes, 1, multibyte);
10252 SAFE_FREE ();
10254 message3_nolog (m);
10256 UNGCPRO;
10260 /* The non-logging version of message3.
10261 This does not cancel echoing, because it is used for echoing.
10262 Perhaps we need to make a separate function for echoing
10263 and make this cancel echoing. */
10265 void
10266 message3_nolog (Lisp_Object m)
10268 struct frame *sf = SELECTED_FRAME ();
10270 if (FRAME_INITIAL_P (sf))
10272 if (noninteractive_need_newline)
10273 putc ('\n', stderr);
10274 noninteractive_need_newline = 0;
10275 if (STRINGP (m))
10277 Lisp_Object s = ENCODE_SYSTEM (m);
10279 fwrite (SDATA (s), SBYTES (s), 1, stderr);
10281 if (cursor_in_echo_area == 0)
10282 fprintf (stderr, "\n");
10283 fflush (stderr);
10285 /* Error messages get reported properly by cmd_error, so this must be just an
10286 informative message; if the frame hasn't really been initialized yet, just
10287 toss it. */
10288 else if (INTERACTIVE && sf->glyphs_initialized_p)
10290 /* Get the frame containing the mini-buffer
10291 that the selected frame is using. */
10292 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
10293 Lisp_Object frame = XWINDOW (mini_window)->frame;
10294 struct frame *f = XFRAME (frame);
10296 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
10297 Fmake_frame_visible (frame);
10299 if (STRINGP (m) && SCHARS (m) > 0)
10301 set_message (m);
10302 if (minibuffer_auto_raise)
10303 Fraise_frame (frame);
10304 /* Assume we are not echoing.
10305 (If we are, echo_now will override this.) */
10306 echo_message_buffer = Qnil;
10308 else
10309 clear_message (true, true);
10311 do_pending_window_change (false);
10312 echo_area_display (true);
10313 do_pending_window_change (false);
10314 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
10315 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
10320 /* Display a null-terminated echo area message M. If M is 0, clear
10321 out any existing message, and let the mini-buffer text show through.
10323 The buffer M must continue to exist until after the echo area gets
10324 cleared or some other message gets displayed there. Do not pass
10325 text that is stored in a Lisp string. Do not pass text in a buffer
10326 that was alloca'd. */
10328 void
10329 message1 (const char *m)
10331 message3 (m ? build_unibyte_string (m) : Qnil);
10335 /* The non-logging counterpart of message1. */
10337 void
10338 message1_nolog (const char *m)
10340 message3_nolog (m ? build_unibyte_string (m) : Qnil);
10343 /* Display a message M which contains a single %s
10344 which gets replaced with STRING. */
10346 void
10347 message_with_string (const char *m, Lisp_Object string, int log)
10349 CHECK_STRING (string);
10351 if (noninteractive)
10353 if (m)
10355 /* ENCODE_SYSTEM below can GC and/or relocate the
10356 Lisp data, so make sure we don't use it here. */
10357 eassert (relocatable_string_data_p (m) != 1);
10359 if (noninteractive_need_newline)
10360 putc ('\n', stderr);
10361 noninteractive_need_newline = 0;
10362 fprintf (stderr, m, SDATA (ENCODE_SYSTEM (string)));
10363 if (!cursor_in_echo_area)
10364 fprintf (stderr, "\n");
10365 fflush (stderr);
10368 else if (INTERACTIVE)
10370 /* The frame whose minibuffer we're going to display the message on.
10371 It may be larger than the selected frame, so we need
10372 to use its buffer, not the selected frame's buffer. */
10373 Lisp_Object mini_window;
10374 struct frame *f, *sf = SELECTED_FRAME ();
10376 /* Get the frame containing the minibuffer
10377 that the selected frame is using. */
10378 mini_window = FRAME_MINIBUF_WINDOW (sf);
10379 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10381 /* Error messages get reported properly by cmd_error, so this must be
10382 just an informative message; if the frame hasn't really been
10383 initialized yet, just toss it. */
10384 if (f->glyphs_initialized_p)
10386 Lisp_Object args[2], msg;
10387 struct gcpro gcpro1, gcpro2;
10389 args[0] = build_string (m);
10390 args[1] = msg = string;
10391 GCPRO2 (args[0], msg);
10392 gcpro1.nvars = 2;
10394 msg = Fformat (2, args);
10396 if (log)
10397 message3 (msg);
10398 else
10399 message3_nolog (msg);
10401 UNGCPRO;
10403 /* Print should start at the beginning of the message
10404 buffer next time. */
10405 message_buf_print = 0;
10411 /* Dump an informative message to the minibuf. If M is 0, clear out
10412 any existing message, and let the mini-buffer text show through. */
10414 static void
10415 vmessage (const char *m, va_list ap)
10417 if (noninteractive)
10419 if (m)
10421 if (noninteractive_need_newline)
10422 putc ('\n', stderr);
10423 noninteractive_need_newline = 0;
10424 vfprintf (stderr, m, ap);
10425 if (cursor_in_echo_area == 0)
10426 fprintf (stderr, "\n");
10427 fflush (stderr);
10430 else if (INTERACTIVE)
10432 /* The frame whose mini-buffer we're going to display the message
10433 on. It may be larger than the selected frame, so we need to
10434 use its buffer, not the selected frame's buffer. */
10435 Lisp_Object mini_window;
10436 struct frame *f, *sf = SELECTED_FRAME ();
10438 /* Get the frame containing the mini-buffer
10439 that the selected frame is using. */
10440 mini_window = FRAME_MINIBUF_WINDOW (sf);
10441 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
10443 /* Error messages get reported properly by cmd_error, so this must be
10444 just an informative message; if the frame hasn't really been
10445 initialized yet, just toss it. */
10446 if (f->glyphs_initialized_p)
10448 if (m)
10450 ptrdiff_t len;
10451 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
10452 USE_SAFE_ALLOCA;
10453 char *message_buf = SAFE_ALLOCA (maxsize + 1);
10455 len = doprnt (message_buf, maxsize, m, 0, ap);
10457 message3 (make_string (message_buf, len));
10458 SAFE_FREE ();
10460 else
10461 message1 (0);
10463 /* Print should start at the beginning of the message
10464 buffer next time. */
10465 message_buf_print = 0;
10470 void
10471 message (const char *m, ...)
10473 va_list ap;
10474 va_start (ap, m);
10475 vmessage (m, ap);
10476 va_end (ap);
10480 #if 0
10481 /* The non-logging version of message. */
10483 void
10484 message_nolog (const char *m, ...)
10486 Lisp_Object old_log_max;
10487 va_list ap;
10488 va_start (ap, m);
10489 old_log_max = Vmessage_log_max;
10490 Vmessage_log_max = Qnil;
10491 vmessage (m, ap);
10492 Vmessage_log_max = old_log_max;
10493 va_end (ap);
10495 #endif
10498 /* Display the current message in the current mini-buffer. This is
10499 only called from error handlers in process.c, and is not time
10500 critical. */
10502 void
10503 update_echo_area (void)
10505 if (!NILP (echo_area_buffer[0]))
10507 Lisp_Object string;
10508 string = Fcurrent_message ();
10509 message3 (string);
10514 /* Make sure echo area buffers in `echo_buffers' are live.
10515 If they aren't, make new ones. */
10517 static void
10518 ensure_echo_area_buffers (void)
10520 int i;
10522 for (i = 0; i < 2; ++i)
10523 if (!BUFFERP (echo_buffer[i])
10524 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10526 char name[30];
10527 Lisp_Object old_buffer;
10528 int j;
10530 old_buffer = echo_buffer[i];
10531 echo_buffer[i] = Fget_buffer_create
10532 (make_formatted_string (name, " *Echo Area %d*", i));
10533 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10534 /* to force word wrap in echo area -
10535 it was decided to postpone this*/
10536 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10538 for (j = 0; j < 2; ++j)
10539 if (EQ (old_buffer, echo_area_buffer[j]))
10540 echo_area_buffer[j] = echo_buffer[i];
10545 /* Call FN with args A1..A2 with either the current or last displayed
10546 echo_area_buffer as current buffer.
10548 WHICH zero means use the current message buffer
10549 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10550 from echo_buffer[] and clear it.
10552 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10553 suitable buffer from echo_buffer[] and clear it.
10555 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10556 that the current message becomes the last displayed one, make
10557 choose a suitable buffer for echo_area_buffer[0], and clear it.
10559 Value is what FN returns. */
10561 static int
10562 with_echo_area_buffer (struct window *w, int which,
10563 int (*fn) (ptrdiff_t, Lisp_Object),
10564 ptrdiff_t a1, Lisp_Object a2)
10566 Lisp_Object buffer;
10567 int this_one, the_other, clear_buffer_p, rc;
10568 ptrdiff_t count = SPECPDL_INDEX ();
10570 /* If buffers aren't live, make new ones. */
10571 ensure_echo_area_buffers ();
10573 clear_buffer_p = 0;
10575 if (which == 0)
10576 this_one = 0, the_other = 1;
10577 else if (which > 0)
10578 this_one = 1, the_other = 0;
10579 else
10581 this_one = 0, the_other = 1;
10582 clear_buffer_p = true;
10584 /* We need a fresh one in case the current echo buffer equals
10585 the one containing the last displayed echo area message. */
10586 if (!NILP (echo_area_buffer[this_one])
10587 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10588 echo_area_buffer[this_one] = Qnil;
10591 /* Choose a suitable buffer from echo_buffer[] is we don't
10592 have one. */
10593 if (NILP (echo_area_buffer[this_one]))
10595 echo_area_buffer[this_one]
10596 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10597 ? echo_buffer[the_other]
10598 : echo_buffer[this_one]);
10599 clear_buffer_p = true;
10602 buffer = echo_area_buffer[this_one];
10604 /* Don't get confused by reusing the buffer used for echoing
10605 for a different purpose. */
10606 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10607 cancel_echoing ();
10609 record_unwind_protect (unwind_with_echo_area_buffer,
10610 with_echo_area_buffer_unwind_data (w));
10612 /* Make the echo area buffer current. Note that for display
10613 purposes, it is not necessary that the displayed window's buffer
10614 == current_buffer, except for text property lookup. So, let's
10615 only set that buffer temporarily here without doing a full
10616 Fset_window_buffer. We must also change w->pointm, though,
10617 because otherwise an assertions in unshow_buffer fails, and Emacs
10618 aborts. */
10619 set_buffer_internal_1 (XBUFFER (buffer));
10620 if (w)
10622 wset_buffer (w, buffer);
10623 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10624 set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
10627 bset_undo_list (current_buffer, Qt);
10628 bset_read_only (current_buffer, Qnil);
10629 specbind (Qinhibit_read_only, Qt);
10630 specbind (Qinhibit_modification_hooks, Qt);
10632 if (clear_buffer_p && Z > BEG)
10633 del_range (BEG, Z);
10635 eassert (BEGV >= BEG);
10636 eassert (ZV <= Z && ZV >= BEGV);
10638 rc = fn (a1, a2);
10640 eassert (BEGV >= BEG);
10641 eassert (ZV <= Z && ZV >= BEGV);
10643 unbind_to (count, Qnil);
10644 return rc;
10648 /* Save state that should be preserved around the call to the function
10649 FN called in with_echo_area_buffer. */
10651 static Lisp_Object
10652 with_echo_area_buffer_unwind_data (struct window *w)
10654 int i = 0;
10655 Lisp_Object vector, tmp;
10657 /* Reduce consing by keeping one vector in
10658 Vwith_echo_area_save_vector. */
10659 vector = Vwith_echo_area_save_vector;
10660 Vwith_echo_area_save_vector = Qnil;
10662 if (NILP (vector))
10663 vector = Fmake_vector (make_number (11), Qnil);
10665 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10666 ASET (vector, i, Vdeactivate_mark); ++i;
10667 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10669 if (w)
10671 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10672 ASET (vector, i, w->contents); ++i;
10673 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10674 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10675 ASET (vector, i, make_number (marker_position (w->old_pointm))); ++i;
10676 ASET (vector, i, make_number (marker_byte_position (w->old_pointm))); ++i;
10677 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10678 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10680 else
10682 int end = i + 8;
10683 for (; i < end; ++i)
10684 ASET (vector, i, Qnil);
10687 eassert (i == ASIZE (vector));
10688 return vector;
10692 /* Restore global state from VECTOR which was created by
10693 with_echo_area_buffer_unwind_data. */
10695 static void
10696 unwind_with_echo_area_buffer (Lisp_Object vector)
10698 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10699 Vdeactivate_mark = AREF (vector, 1);
10700 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10702 if (WINDOWP (AREF (vector, 3)))
10704 struct window *w;
10705 Lisp_Object buffer;
10707 w = XWINDOW (AREF (vector, 3));
10708 buffer = AREF (vector, 4);
10710 wset_buffer (w, buffer);
10711 set_marker_both (w->pointm, buffer,
10712 XFASTINT (AREF (vector, 5)),
10713 XFASTINT (AREF (vector, 6)));
10714 set_marker_both (w->old_pointm, buffer,
10715 XFASTINT (AREF (vector, 7)),
10716 XFASTINT (AREF (vector, 8)));
10717 set_marker_both (w->start, buffer,
10718 XFASTINT (AREF (vector, 9)),
10719 XFASTINT (AREF (vector, 10)));
10722 Vwith_echo_area_save_vector = vector;
10726 /* Set up the echo area for use by print functions. MULTIBYTE_P
10727 non-zero means we will print multibyte. */
10729 void
10730 setup_echo_area_for_printing (int multibyte_p)
10732 /* If we can't find an echo area any more, exit. */
10733 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10734 Fkill_emacs (Qnil);
10736 ensure_echo_area_buffers ();
10738 if (!message_buf_print)
10740 /* A message has been output since the last time we printed.
10741 Choose a fresh echo area buffer. */
10742 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10743 echo_area_buffer[0] = echo_buffer[1];
10744 else
10745 echo_area_buffer[0] = echo_buffer[0];
10747 /* Switch to that buffer and clear it. */
10748 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10749 bset_truncate_lines (current_buffer, Qnil);
10751 if (Z > BEG)
10753 ptrdiff_t count = SPECPDL_INDEX ();
10754 specbind (Qinhibit_read_only, Qt);
10755 /* Note that undo recording is always disabled. */
10756 del_range (BEG, Z);
10757 unbind_to (count, Qnil);
10759 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10761 /* Set up the buffer for the multibyteness we need. */
10762 if (multibyte_p
10763 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10764 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10766 /* Raise the frame containing the echo area. */
10767 if (minibuffer_auto_raise)
10769 struct frame *sf = SELECTED_FRAME ();
10770 Lisp_Object mini_window;
10771 mini_window = FRAME_MINIBUF_WINDOW (sf);
10772 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10775 message_log_maybe_newline ();
10776 message_buf_print = 1;
10778 else
10780 if (NILP (echo_area_buffer[0]))
10782 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10783 echo_area_buffer[0] = echo_buffer[1];
10784 else
10785 echo_area_buffer[0] = echo_buffer[0];
10788 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10790 /* Someone switched buffers between print requests. */
10791 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10792 bset_truncate_lines (current_buffer, Qnil);
10798 /* Display an echo area message in window W. Value is non-zero if W's
10799 height is changed. If display_last_displayed_message_p is
10800 non-zero, display the message that was last displayed, otherwise
10801 display the current message. */
10803 static int
10804 display_echo_area (struct window *w)
10806 int i, no_message_p, window_height_changed_p;
10808 /* Temporarily disable garbage collections while displaying the echo
10809 area. This is done because a GC can print a message itself.
10810 That message would modify the echo area buffer's contents while a
10811 redisplay of the buffer is going on, and seriously confuse
10812 redisplay. */
10813 ptrdiff_t count = inhibit_garbage_collection ();
10815 /* If there is no message, we must call display_echo_area_1
10816 nevertheless because it resizes the window. But we will have to
10817 reset the echo_area_buffer in question to nil at the end because
10818 with_echo_area_buffer will sets it to an empty buffer. */
10819 i = display_last_displayed_message_p ? 1 : 0;
10820 no_message_p = NILP (echo_area_buffer[i]);
10822 window_height_changed_p
10823 = with_echo_area_buffer (w, display_last_displayed_message_p,
10824 display_echo_area_1,
10825 (intptr_t) w, Qnil);
10827 if (no_message_p)
10828 echo_area_buffer[i] = Qnil;
10830 unbind_to (count, Qnil);
10831 return window_height_changed_p;
10835 /* Helper for display_echo_area. Display the current buffer which
10836 contains the current echo area message in window W, a mini-window,
10837 a pointer to which is passed in A1. A2..A4 are currently not used.
10838 Change the height of W so that all of the message is displayed.
10839 Value is non-zero if height of W was changed. */
10841 static int
10842 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10844 intptr_t i1 = a1;
10845 struct window *w = (struct window *) i1;
10846 Lisp_Object window;
10847 struct text_pos start;
10848 int window_height_changed_p = 0;
10850 /* Do this before displaying, so that we have a large enough glyph
10851 matrix for the display. If we can't get enough space for the
10852 whole text, display the last N lines. That works by setting w->start. */
10853 window_height_changed_p = resize_mini_window (w, 0);
10855 /* Use the starting position chosen by resize_mini_window. */
10856 SET_TEXT_POS_FROM_MARKER (start, w->start);
10858 /* Display. */
10859 clear_glyph_matrix (w->desired_matrix);
10860 XSETWINDOW (window, w);
10861 try_window (window, start, 0);
10863 return window_height_changed_p;
10867 /* Resize the echo area window to exactly the size needed for the
10868 currently displayed message, if there is one. If a mini-buffer
10869 is active, don't shrink it. */
10871 void
10872 resize_echo_area_exactly (void)
10874 if (BUFFERP (echo_area_buffer[0])
10875 && WINDOWP (echo_area_window))
10877 struct window *w = XWINDOW (echo_area_window);
10878 Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
10879 int resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10880 (intptr_t) w, resize_exactly);
10881 if (resized_p)
10883 windows_or_buffers_changed = 42;
10884 update_mode_lines = 30;
10885 redisplay_internal ();
10891 /* Callback function for with_echo_area_buffer, when used from
10892 resize_echo_area_exactly. A1 contains a pointer to the window to
10893 resize, EXACTLY non-nil means resize the mini-window exactly to the
10894 size of the text displayed. A3 and A4 are not used. Value is what
10895 resize_mini_window returns. */
10897 static int
10898 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10900 intptr_t i1 = a1;
10901 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10905 /* Resize mini-window W to fit the size of its contents. EXACT_P
10906 means size the window exactly to the size needed. Otherwise, it's
10907 only enlarged until W's buffer is empty.
10909 Set W->start to the right place to begin display. If the whole
10910 contents fit, start at the beginning. Otherwise, start so as
10911 to make the end of the contents appear. This is particularly
10912 important for y-or-n-p, but seems desirable generally.
10914 Value is non-zero if the window height has been changed. */
10917 resize_mini_window (struct window *w, int exact_p)
10919 struct frame *f = XFRAME (w->frame);
10920 int window_height_changed_p = 0;
10922 eassert (MINI_WINDOW_P (w));
10924 /* By default, start display at the beginning. */
10925 set_marker_both (w->start, w->contents,
10926 BUF_BEGV (XBUFFER (w->contents)),
10927 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10929 /* Don't resize windows while redisplaying a window; it would
10930 confuse redisplay functions when the size of the window they are
10931 displaying changes from under them. Such a resizing can happen,
10932 for instance, when which-func prints a long message while
10933 we are running fontification-functions. We're running these
10934 functions with safe_call which binds inhibit-redisplay to t. */
10935 if (!NILP (Vinhibit_redisplay))
10936 return 0;
10938 /* Nil means don't try to resize. */
10939 if (NILP (Vresize_mini_windows)
10940 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10941 return 0;
10943 if (!FRAME_MINIBUF_ONLY_P (f))
10945 struct it it;
10946 int total_height = (WINDOW_PIXEL_HEIGHT (XWINDOW (FRAME_ROOT_WINDOW (f)))
10947 + WINDOW_PIXEL_HEIGHT (w));
10948 int unit = FRAME_LINE_HEIGHT (f);
10949 int height, max_height;
10950 struct text_pos start;
10951 struct buffer *old_current_buffer = NULL;
10953 if (current_buffer != XBUFFER (w->contents))
10955 old_current_buffer = current_buffer;
10956 set_buffer_internal (XBUFFER (w->contents));
10959 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10961 /* Compute the max. number of lines specified by the user. */
10962 if (FLOATP (Vmax_mini_window_height))
10963 max_height = XFLOATINT (Vmax_mini_window_height) * total_height;
10964 else if (INTEGERP (Vmax_mini_window_height))
10965 max_height = XINT (Vmax_mini_window_height) * unit;
10966 else
10967 max_height = total_height / 4;
10969 /* Correct that max. height if it's bogus. */
10970 max_height = clip_to_bounds (unit, max_height, total_height);
10972 /* Find out the height of the text in the window. */
10973 if (it.line_wrap == TRUNCATE)
10974 height = unit;
10975 else
10977 last_height = 0;
10978 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10979 if (it.max_ascent == 0 && it.max_descent == 0)
10980 height = it.current_y + last_height;
10981 else
10982 height = it.current_y + it.max_ascent + it.max_descent;
10983 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10986 /* Compute a suitable window start. */
10987 if (height > max_height)
10989 height = (max_height / unit) * unit;
10990 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10991 move_it_vertically_backward (&it, height - unit);
10992 start = it.current.pos;
10994 else
10995 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10996 SET_MARKER_FROM_TEXT_POS (w->start, start);
10998 if (EQ (Vresize_mini_windows, Qgrow_only))
11000 /* Let it grow only, until we display an empty message, in which
11001 case the window shrinks again. */
11002 if (height > WINDOW_PIXEL_HEIGHT (w))
11004 int old_height = WINDOW_PIXEL_HEIGHT (w);
11006 FRAME_WINDOWS_FROZEN (f) = 1;
11007 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
11008 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11010 else if (height < WINDOW_PIXEL_HEIGHT (w)
11011 && (exact_p || BEGV == ZV))
11013 int old_height = WINDOW_PIXEL_HEIGHT (w);
11015 FRAME_WINDOWS_FROZEN (f) = 0;
11016 shrink_mini_window (w, 1);
11017 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11020 else
11022 /* Always resize to exact size needed. */
11023 if (height > WINDOW_PIXEL_HEIGHT (w))
11025 int old_height = WINDOW_PIXEL_HEIGHT (w);
11027 FRAME_WINDOWS_FROZEN (f) = 1;
11028 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
11029 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11031 else if (height < WINDOW_PIXEL_HEIGHT (w))
11033 int old_height = WINDOW_PIXEL_HEIGHT (w);
11035 FRAME_WINDOWS_FROZEN (f) = 0;
11036 shrink_mini_window (w, 1);
11038 if (height)
11040 FRAME_WINDOWS_FROZEN (f) = 1;
11041 grow_mini_window (w, height - WINDOW_PIXEL_HEIGHT (w), 1);
11044 window_height_changed_p = WINDOW_PIXEL_HEIGHT (w) != old_height;
11048 if (old_current_buffer)
11049 set_buffer_internal (old_current_buffer);
11052 return window_height_changed_p;
11056 /* Value is the current message, a string, or nil if there is no
11057 current message. */
11059 Lisp_Object
11060 current_message (void)
11062 Lisp_Object msg;
11064 if (!BUFFERP (echo_area_buffer[0]))
11065 msg = Qnil;
11066 else
11068 with_echo_area_buffer (0, 0, current_message_1,
11069 (intptr_t) &msg, Qnil);
11070 if (NILP (msg))
11071 echo_area_buffer[0] = Qnil;
11074 return msg;
11078 static int
11079 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
11081 intptr_t i1 = a1;
11082 Lisp_Object *msg = (Lisp_Object *) i1;
11084 if (Z > BEG)
11085 *msg = make_buffer_string (BEG, Z, 1);
11086 else
11087 *msg = Qnil;
11088 return 0;
11092 /* Push the current message on Vmessage_stack for later restoration
11093 by restore_message. Value is non-zero if the current message isn't
11094 empty. This is a relatively infrequent operation, so it's not
11095 worth optimizing. */
11097 bool
11098 push_message (void)
11100 Lisp_Object msg = current_message ();
11101 Vmessage_stack = Fcons (msg, Vmessage_stack);
11102 return STRINGP (msg);
11106 /* Restore message display from the top of Vmessage_stack. */
11108 void
11109 restore_message (void)
11111 eassert (CONSP (Vmessage_stack));
11112 message3_nolog (XCAR (Vmessage_stack));
11116 /* Handler for unwind-protect calling pop_message. */
11118 void
11119 pop_message_unwind (void)
11121 /* Pop the top-most entry off Vmessage_stack. */
11122 eassert (CONSP (Vmessage_stack));
11123 Vmessage_stack = XCDR (Vmessage_stack);
11127 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
11128 exits. If the stack is not empty, we have a missing pop_message
11129 somewhere. */
11131 void
11132 check_message_stack (void)
11134 if (!NILP (Vmessage_stack))
11135 emacs_abort ();
11139 /* Truncate to NCHARS what will be displayed in the echo area the next
11140 time we display it---but don't redisplay it now. */
11142 void
11143 truncate_echo_area (ptrdiff_t nchars)
11145 if (nchars == 0)
11146 echo_area_buffer[0] = Qnil;
11147 else if (!noninteractive
11148 && INTERACTIVE
11149 && !NILP (echo_area_buffer[0]))
11151 struct frame *sf = SELECTED_FRAME ();
11152 /* Error messages get reported properly by cmd_error, so this must be
11153 just an informative message; if the frame hasn't really been
11154 initialized yet, just toss it. */
11155 if (sf->glyphs_initialized_p)
11156 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
11161 /* Helper function for truncate_echo_area. Truncate the current
11162 message to at most NCHARS characters. */
11164 static int
11165 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
11167 if (BEG + nchars < Z)
11168 del_range (BEG + nchars, Z);
11169 if (Z == BEG)
11170 echo_area_buffer[0] = Qnil;
11171 return 0;
11174 /* Set the current message to STRING. */
11176 static void
11177 set_message (Lisp_Object string)
11179 eassert (STRINGP (string));
11181 message_enable_multibyte = STRING_MULTIBYTE (string);
11183 with_echo_area_buffer (0, -1, set_message_1, 0, string);
11184 message_buf_print = 0;
11185 help_echo_showing_p = 0;
11187 if (STRINGP (Vdebug_on_message)
11188 && STRINGP (string)
11189 && fast_string_match (Vdebug_on_message, string) >= 0)
11190 call_debugger (list2 (Qerror, string));
11194 /* Helper function for set_message. First argument is ignored and second
11195 argument has the same meaning as for set_message.
11196 This function is called with the echo area buffer being current. */
11198 static int
11199 set_message_1 (ptrdiff_t a1, Lisp_Object string)
11201 eassert (STRINGP (string));
11203 /* Change multibyteness of the echo buffer appropriately. */
11204 if (message_enable_multibyte
11205 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
11206 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
11208 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
11209 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
11210 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
11212 /* Insert new message at BEG. */
11213 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
11215 /* This function takes care of single/multibyte conversion.
11216 We just have to ensure that the echo area buffer has the right
11217 setting of enable_multibyte_characters. */
11218 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
11220 return 0;
11224 /* Clear messages. CURRENT_P non-zero means clear the current
11225 message. LAST_DISPLAYED_P non-zero means clear the message
11226 last displayed. */
11228 void
11229 clear_message (bool current_p, bool last_displayed_p)
11231 if (current_p)
11233 echo_area_buffer[0] = Qnil;
11234 message_cleared_p = true;
11237 if (last_displayed_p)
11238 echo_area_buffer[1] = Qnil;
11240 message_buf_print = 0;
11243 /* Clear garbaged frames.
11245 This function is used where the old redisplay called
11246 redraw_garbaged_frames which in turn called redraw_frame which in
11247 turn called clear_frame. The call to clear_frame was a source of
11248 flickering. I believe a clear_frame is not necessary. It should
11249 suffice in the new redisplay to invalidate all current matrices,
11250 and ensure a complete redisplay of all windows. */
11252 static void
11253 clear_garbaged_frames (void)
11255 if (frame_garbaged)
11257 Lisp_Object tail, frame;
11259 FOR_EACH_FRAME (tail, frame)
11261 struct frame *f = XFRAME (frame);
11263 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
11265 if (f->resized_p)
11266 redraw_frame (f);
11267 else
11268 clear_current_matrices (f);
11269 fset_redisplay (f);
11270 f->garbaged = false;
11271 f->resized_p = false;
11275 frame_garbaged = false;
11280 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
11281 is non-zero update selected_frame. Value is non-zero if the
11282 mini-windows height has been changed. */
11284 static bool
11285 echo_area_display (bool update_frame_p)
11287 Lisp_Object mini_window;
11288 struct window *w;
11289 struct frame *f;
11290 bool window_height_changed_p = false;
11291 struct frame *sf = SELECTED_FRAME ();
11293 mini_window = FRAME_MINIBUF_WINDOW (sf);
11294 w = XWINDOW (mini_window);
11295 f = XFRAME (WINDOW_FRAME (w));
11297 /* Don't display if frame is invisible or not yet initialized. */
11298 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
11299 return 0;
11301 #ifdef HAVE_WINDOW_SYSTEM
11302 /* When Emacs starts, selected_frame may be the initial terminal
11303 frame. If we let this through, a message would be displayed on
11304 the terminal. */
11305 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
11306 return 0;
11307 #endif /* HAVE_WINDOW_SYSTEM */
11309 /* Redraw garbaged frames. */
11310 clear_garbaged_frames ();
11312 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
11314 echo_area_window = mini_window;
11315 window_height_changed_p = display_echo_area (w);
11316 w->must_be_updated_p = true;
11318 /* Update the display, unless called from redisplay_internal.
11319 Also don't update the screen during redisplay itself. The
11320 update will happen at the end of redisplay, and an update
11321 here could cause confusion. */
11322 if (update_frame_p && !redisplaying_p)
11324 int n = 0;
11326 /* If the display update has been interrupted by pending
11327 input, update mode lines in the frame. Due to the
11328 pending input, it might have been that redisplay hasn't
11329 been called, so that mode lines above the echo area are
11330 garbaged. This looks odd, so we prevent it here. */
11331 if (!display_completed)
11332 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
11334 if (window_height_changed_p
11335 /* Don't do this if Emacs is shutting down. Redisplay
11336 needs to run hooks. */
11337 && !NILP (Vrun_hooks))
11339 /* Must update other windows. Likewise as in other
11340 cases, don't let this update be interrupted by
11341 pending input. */
11342 ptrdiff_t count = SPECPDL_INDEX ();
11343 specbind (Qredisplay_dont_pause, Qt);
11344 windows_or_buffers_changed = 44;
11345 redisplay_internal ();
11346 unbind_to (count, Qnil);
11348 else if (FRAME_WINDOW_P (f) && n == 0)
11350 /* Window configuration is the same as before.
11351 Can do with a display update of the echo area,
11352 unless we displayed some mode lines. */
11353 update_single_window (w);
11354 flush_frame (f);
11356 else
11357 update_frame (f, true, true);
11359 /* If cursor is in the echo area, make sure that the next
11360 redisplay displays the minibuffer, so that the cursor will
11361 be replaced with what the minibuffer wants. */
11362 if (cursor_in_echo_area)
11363 wset_redisplay (XWINDOW (mini_window));
11366 else if (!EQ (mini_window, selected_window))
11367 wset_redisplay (XWINDOW (mini_window));
11369 /* Last displayed message is now the current message. */
11370 echo_area_buffer[1] = echo_area_buffer[0];
11371 /* Inform read_char that we're not echoing. */
11372 echo_message_buffer = Qnil;
11374 /* Prevent redisplay optimization in redisplay_internal by resetting
11375 this_line_start_pos. This is done because the mini-buffer now
11376 displays the message instead of its buffer text. */
11377 if (EQ (mini_window, selected_window))
11378 CHARPOS (this_line_start_pos) = 0;
11380 return window_height_changed_p;
11383 /* Nonzero if W's buffer was changed but not saved. */
11385 static int
11386 window_buffer_changed (struct window *w)
11388 struct buffer *b = XBUFFER (w->contents);
11390 eassert (BUFFER_LIVE_P (b));
11392 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star));
11395 /* Nonzero if W has %c in its mode line and mode line should be updated. */
11397 static int
11398 mode_line_update_needed (struct window *w)
11400 return (w->column_number_displayed != -1
11401 && !(PT == w->last_point && !window_outdated (w))
11402 && (w->column_number_displayed != current_column ()));
11405 /* Nonzero if window start of W is frozen and may not be changed during
11406 redisplay. */
11408 static bool
11409 window_frozen_p (struct window *w)
11411 if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
11413 Lisp_Object window;
11415 XSETWINDOW (window, w);
11416 if (MINI_WINDOW_P (w))
11417 return 0;
11418 else if (EQ (window, selected_window))
11419 return 0;
11420 else if (MINI_WINDOW_P (XWINDOW (selected_window))
11421 && EQ (window, Vminibuf_scroll_window))
11422 /* This special window can't be frozen too. */
11423 return 0;
11424 else
11425 return 1;
11427 return 0;
11430 /***********************************************************************
11431 Mode Lines and Frame Titles
11432 ***********************************************************************/
11434 /* A buffer for constructing non-propertized mode-line strings and
11435 frame titles in it; allocated from the heap in init_xdisp and
11436 resized as needed in store_mode_line_noprop_char. */
11438 static char *mode_line_noprop_buf;
11440 /* The buffer's end, and a current output position in it. */
11442 static char *mode_line_noprop_buf_end;
11443 static char *mode_line_noprop_ptr;
11445 #define MODE_LINE_NOPROP_LEN(start) \
11446 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
11448 static enum {
11449 MODE_LINE_DISPLAY = 0,
11450 MODE_LINE_TITLE,
11451 MODE_LINE_NOPROP,
11452 MODE_LINE_STRING
11453 } mode_line_target;
11455 /* Alist that caches the results of :propertize.
11456 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11457 static Lisp_Object mode_line_proptrans_alist;
11459 /* List of strings making up the mode-line. */
11460 static Lisp_Object mode_line_string_list;
11462 /* Base face property when building propertized mode line string. */
11463 static Lisp_Object mode_line_string_face;
11464 static Lisp_Object mode_line_string_face_prop;
11467 /* Unwind data for mode line strings */
11469 static Lisp_Object Vmode_line_unwind_vector;
11471 static Lisp_Object
11472 format_mode_line_unwind_data (struct frame *target_frame,
11473 struct buffer *obuf,
11474 Lisp_Object owin,
11475 int save_proptrans)
11477 Lisp_Object vector, tmp;
11479 /* Reduce consing by keeping one vector in
11480 Vwith_echo_area_save_vector. */
11481 vector = Vmode_line_unwind_vector;
11482 Vmode_line_unwind_vector = Qnil;
11484 if (NILP (vector))
11485 vector = Fmake_vector (make_number (10), Qnil);
11487 ASET (vector, 0, make_number (mode_line_target));
11488 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11489 ASET (vector, 2, mode_line_string_list);
11490 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11491 ASET (vector, 4, mode_line_string_face);
11492 ASET (vector, 5, mode_line_string_face_prop);
11494 if (obuf)
11495 XSETBUFFER (tmp, obuf);
11496 else
11497 tmp = Qnil;
11498 ASET (vector, 6, tmp);
11499 ASET (vector, 7, owin);
11500 if (target_frame)
11502 /* Similarly to `with-selected-window', if the operation selects
11503 a window on another frame, we must restore that frame's
11504 selected window, and (for a tty) the top-frame. */
11505 ASET (vector, 8, target_frame->selected_window);
11506 if (FRAME_TERMCAP_P (target_frame))
11507 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11510 return vector;
11513 static void
11514 unwind_format_mode_line (Lisp_Object vector)
11516 Lisp_Object old_window = AREF (vector, 7);
11517 Lisp_Object target_frame_window = AREF (vector, 8);
11518 Lisp_Object old_top_frame = AREF (vector, 9);
11520 mode_line_target = XINT (AREF (vector, 0));
11521 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11522 mode_line_string_list = AREF (vector, 2);
11523 if (! EQ (AREF (vector, 3), Qt))
11524 mode_line_proptrans_alist = AREF (vector, 3);
11525 mode_line_string_face = AREF (vector, 4);
11526 mode_line_string_face_prop = AREF (vector, 5);
11528 /* Select window before buffer, since it may change the buffer. */
11529 if (!NILP (old_window))
11531 /* If the operation that we are unwinding had selected a window
11532 on a different frame, reset its frame-selected-window. For a
11533 text terminal, reset its top-frame if necessary. */
11534 if (!NILP (target_frame_window))
11536 Lisp_Object frame
11537 = WINDOW_FRAME (XWINDOW (target_frame_window));
11539 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11540 Fselect_window (target_frame_window, Qt);
11542 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11543 Fselect_frame (old_top_frame, Qt);
11546 Fselect_window (old_window, Qt);
11549 if (!NILP (AREF (vector, 6)))
11551 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11552 ASET (vector, 6, Qnil);
11555 Vmode_line_unwind_vector = vector;
11559 /* Store a single character C for the frame title in mode_line_noprop_buf.
11560 Re-allocate mode_line_noprop_buf if necessary. */
11562 static void
11563 store_mode_line_noprop_char (char c)
11565 /* If output position has reached the end of the allocated buffer,
11566 increase the buffer's size. */
11567 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11569 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11570 ptrdiff_t size = len;
11571 mode_line_noprop_buf =
11572 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11573 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11574 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11577 *mode_line_noprop_ptr++ = c;
11581 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11582 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11583 characters that yield more columns than PRECISION; PRECISION <= 0
11584 means copy the whole string. Pad with spaces until FIELD_WIDTH
11585 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11586 pad. Called from display_mode_element when it is used to build a
11587 frame title. */
11589 static int
11590 store_mode_line_noprop (const char *string, int field_width, int precision)
11592 const unsigned char *str = (const unsigned char *) string;
11593 int n = 0;
11594 ptrdiff_t dummy, nbytes;
11596 /* Copy at most PRECISION chars from STR. */
11597 nbytes = strlen (string);
11598 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11599 while (nbytes--)
11600 store_mode_line_noprop_char (*str++);
11602 /* Fill up with spaces until FIELD_WIDTH reached. */
11603 while (field_width > 0
11604 && n < field_width)
11606 store_mode_line_noprop_char (' ');
11607 ++n;
11610 return n;
11613 /***********************************************************************
11614 Frame Titles
11615 ***********************************************************************/
11617 #ifdef HAVE_WINDOW_SYSTEM
11619 /* Set the title of FRAME, if it has changed. The title format is
11620 Vicon_title_format if FRAME is iconified, otherwise it is
11621 frame_title_format. */
11623 static void
11624 x_consider_frame_title (Lisp_Object frame)
11626 struct frame *f = XFRAME (frame);
11628 if (FRAME_WINDOW_P (f)
11629 || FRAME_MINIBUF_ONLY_P (f)
11630 || f->explicit_name)
11632 /* Do we have more than one visible frame on this X display? */
11633 Lisp_Object tail, other_frame, fmt;
11634 ptrdiff_t title_start;
11635 char *title;
11636 ptrdiff_t len;
11637 struct it it;
11638 ptrdiff_t count = SPECPDL_INDEX ();
11640 FOR_EACH_FRAME (tail, other_frame)
11642 struct frame *tf = XFRAME (other_frame);
11644 if (tf != f
11645 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11646 && !FRAME_MINIBUF_ONLY_P (tf)
11647 && !EQ (other_frame, tip_frame)
11648 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11649 break;
11652 /* Set global variable indicating that multiple frames exist. */
11653 multiple_frames = CONSP (tail);
11655 /* Switch to the buffer of selected window of the frame. Set up
11656 mode_line_target so that display_mode_element will output into
11657 mode_line_noprop_buf; then display the title. */
11658 record_unwind_protect (unwind_format_mode_line,
11659 format_mode_line_unwind_data
11660 (f, current_buffer, selected_window, 0));
11662 Fselect_window (f->selected_window, Qt);
11663 set_buffer_internal_1
11664 (XBUFFER (XWINDOW (f->selected_window)->contents));
11665 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11667 mode_line_target = MODE_LINE_TITLE;
11668 title_start = MODE_LINE_NOPROP_LEN (0);
11669 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11670 NULL, DEFAULT_FACE_ID);
11671 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11672 len = MODE_LINE_NOPROP_LEN (title_start);
11673 title = mode_line_noprop_buf + title_start;
11674 unbind_to (count, Qnil);
11676 /* Set the title only if it's changed. This avoids consing in
11677 the common case where it hasn't. (If it turns out that we've
11678 already wasted too much time by walking through the list with
11679 display_mode_element, then we might need to optimize at a
11680 higher level than this.) */
11681 if (! STRINGP (f->name)
11682 || SBYTES (f->name) != len
11683 || memcmp (title, SDATA (f->name), len) != 0)
11684 x_implicitly_set_name (f, make_string (title, len), Qnil);
11688 #endif /* not HAVE_WINDOW_SYSTEM */
11691 /***********************************************************************
11692 Menu Bars
11693 ***********************************************************************/
11695 /* Non-zero if we will not redisplay all visible windows. */
11696 #define REDISPLAY_SOME_P() \
11697 ((windows_or_buffers_changed == 0 \
11698 || windows_or_buffers_changed == REDISPLAY_SOME) \
11699 && (update_mode_lines == 0 \
11700 || update_mode_lines == REDISPLAY_SOME))
11702 /* Prepare for redisplay by updating menu-bar item lists when
11703 appropriate. This can call eval. */
11705 static void
11706 prepare_menu_bars (void)
11708 bool all_windows = windows_or_buffers_changed || update_mode_lines;
11709 bool some_windows = REDISPLAY_SOME_P ();
11710 struct gcpro gcpro1, gcpro2;
11711 Lisp_Object tooltip_frame;
11713 #ifdef HAVE_WINDOW_SYSTEM
11714 tooltip_frame = tip_frame;
11715 #else
11716 tooltip_frame = Qnil;
11717 #endif
11719 if (FUNCTIONP (Vpre_redisplay_function))
11721 Lisp_Object windows = all_windows ? Qt : Qnil;
11722 if (all_windows && some_windows)
11724 Lisp_Object ws = window_list ();
11725 for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
11727 Lisp_Object this = XCAR (ws);
11728 struct window *w = XWINDOW (this);
11729 if (w->redisplay
11730 || XFRAME (w->frame)->redisplay
11731 || XBUFFER (w->contents)->text->redisplay)
11733 windows = Fcons (this, windows);
11737 safe__call1 (true, Vpre_redisplay_function, windows);
11740 /* Update all frame titles based on their buffer names, etc. We do
11741 this before the menu bars so that the buffer-menu will show the
11742 up-to-date frame titles. */
11743 #ifdef HAVE_WINDOW_SYSTEM
11744 if (all_windows)
11746 Lisp_Object tail, frame;
11748 FOR_EACH_FRAME (tail, frame)
11750 struct frame *f = XFRAME (frame);
11751 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11752 if (some_windows
11753 && !f->redisplay
11754 && !w->redisplay
11755 && !XBUFFER (w->contents)->text->redisplay)
11756 continue;
11758 if (!EQ (frame, tooltip_frame)
11759 && (FRAME_ICONIFIED_P (f)
11760 || FRAME_VISIBLE_P (f) == 1
11761 /* Exclude TTY frames that are obscured because they
11762 are not the top frame on their console. This is
11763 because x_consider_frame_title actually switches
11764 to the frame, which for TTY frames means it is
11765 marked as garbaged, and will be completely
11766 redrawn on the next redisplay cycle. This causes
11767 TTY frames to be completely redrawn, when there
11768 are more than one of them, even though nothing
11769 should be changed on display. */
11770 || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
11771 x_consider_frame_title (frame);
11774 #endif /* HAVE_WINDOW_SYSTEM */
11776 /* Update the menu bar item lists, if appropriate. This has to be
11777 done before any actual redisplay or generation of display lines. */
11779 if (all_windows)
11781 Lisp_Object tail, frame;
11782 ptrdiff_t count = SPECPDL_INDEX ();
11783 /* 1 means that update_menu_bar has run its hooks
11784 so any further calls to update_menu_bar shouldn't do so again. */
11785 int menu_bar_hooks_run = 0;
11787 record_unwind_save_match_data ();
11789 FOR_EACH_FRAME (tail, frame)
11791 struct frame *f = XFRAME (frame);
11792 struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
11794 /* Ignore tooltip frame. */
11795 if (EQ (frame, tooltip_frame))
11796 continue;
11798 if (some_windows
11799 && !f->redisplay
11800 && !w->redisplay
11801 && !XBUFFER (w->contents)->text->redisplay)
11802 continue;
11804 /* If a window on this frame changed size, report that to
11805 the user and clear the size-change flag. */
11806 if (FRAME_WINDOW_SIZES_CHANGED (f))
11808 Lisp_Object functions;
11810 /* Clear flag first in case we get an error below. */
11811 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11812 functions = Vwindow_size_change_functions;
11813 GCPRO2 (tail, functions);
11815 while (CONSP (functions))
11817 if (!EQ (XCAR (functions), Qt))
11818 call1 (XCAR (functions), frame);
11819 functions = XCDR (functions);
11821 UNGCPRO;
11824 GCPRO1 (tail);
11825 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11826 #ifdef HAVE_WINDOW_SYSTEM
11827 update_tool_bar (f, 0);
11828 #endif
11829 UNGCPRO;
11832 unbind_to (count, Qnil);
11834 else
11836 struct frame *sf = SELECTED_FRAME ();
11837 update_menu_bar (sf, 1, 0);
11838 #ifdef HAVE_WINDOW_SYSTEM
11839 update_tool_bar (sf, 1);
11840 #endif
11845 /* Update the menu bar item list for frame F. This has to be done
11846 before we start to fill in any display lines, because it can call
11847 eval.
11849 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11851 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11852 already ran the menu bar hooks for this redisplay, so there
11853 is no need to run them again. The return value is the
11854 updated value of this flag, to pass to the next call. */
11856 static int
11857 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11859 Lisp_Object window;
11860 register struct window *w;
11862 /* If called recursively during a menu update, do nothing. This can
11863 happen when, for instance, an activate-menubar-hook causes a
11864 redisplay. */
11865 if (inhibit_menubar_update)
11866 return hooks_run;
11868 window = FRAME_SELECTED_WINDOW (f);
11869 w = XWINDOW (window);
11871 if (FRAME_WINDOW_P (f)
11873 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11874 || defined (HAVE_NS) || defined (USE_GTK)
11875 FRAME_EXTERNAL_MENU_BAR (f)
11876 #else
11877 FRAME_MENU_BAR_LINES (f) > 0
11878 #endif
11879 : FRAME_MENU_BAR_LINES (f) > 0)
11881 /* If the user has switched buffers or windows, we need to
11882 recompute to reflect the new bindings. But we'll
11883 recompute when update_mode_lines is set too; that means
11884 that people can use force-mode-line-update to request
11885 that the menu bar be recomputed. The adverse effect on
11886 the rest of the redisplay algorithm is about the same as
11887 windows_or_buffers_changed anyway. */
11888 if (windows_or_buffers_changed
11889 /* This used to test w->update_mode_line, but we believe
11890 there is no need to recompute the menu in that case. */
11891 || update_mode_lines
11892 || window_buffer_changed (w))
11894 struct buffer *prev = current_buffer;
11895 ptrdiff_t count = SPECPDL_INDEX ();
11897 specbind (Qinhibit_menubar_update, Qt);
11899 set_buffer_internal_1 (XBUFFER (w->contents));
11900 if (save_match_data)
11901 record_unwind_save_match_data ();
11902 if (NILP (Voverriding_local_map_menu_flag))
11904 specbind (Qoverriding_terminal_local_map, Qnil);
11905 specbind (Qoverriding_local_map, Qnil);
11908 if (!hooks_run)
11910 /* Run the Lucid hook. */
11911 safe_run_hooks (Qactivate_menubar_hook);
11913 /* If it has changed current-menubar from previous value,
11914 really recompute the menu-bar from the value. */
11915 if (! NILP (Vlucid_menu_bar_dirty_flag))
11916 call0 (Qrecompute_lucid_menubar);
11918 safe_run_hooks (Qmenu_bar_update_hook);
11920 hooks_run = 1;
11923 XSETFRAME (Vmenu_updating_frame, f);
11924 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11926 /* Redisplay the menu bar in case we changed it. */
11927 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11928 || defined (HAVE_NS) || defined (USE_GTK)
11929 if (FRAME_WINDOW_P (f))
11931 #if defined (HAVE_NS)
11932 /* All frames on Mac OS share the same menubar. So only
11933 the selected frame should be allowed to set it. */
11934 if (f == SELECTED_FRAME ())
11935 #endif
11936 set_frame_menubar (f, 0, 0);
11938 else
11939 /* On a terminal screen, the menu bar is an ordinary screen
11940 line, and this makes it get updated. */
11941 w->update_mode_line = 1;
11942 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11943 /* In the non-toolkit version, the menu bar is an ordinary screen
11944 line, and this makes it get updated. */
11945 w->update_mode_line = 1;
11946 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11948 unbind_to (count, Qnil);
11949 set_buffer_internal_1 (prev);
11953 return hooks_run;
11956 /***********************************************************************
11957 Tool-bars
11958 ***********************************************************************/
11960 #ifdef HAVE_WINDOW_SYSTEM
11962 /* Select `frame' temporarily without running all the code in
11963 do_switch_frame.
11964 FIXME: Maybe do_switch_frame should be trimmed down similarly
11965 when `norecord' is set. */
11966 static void
11967 fast_set_selected_frame (Lisp_Object frame)
11969 if (!EQ (selected_frame, frame))
11971 selected_frame = frame;
11972 selected_window = XFRAME (frame)->selected_window;
11976 /* Update the tool-bar item list for frame F. This has to be done
11977 before we start to fill in any display lines. Called from
11978 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11979 and restore it here. */
11981 static void
11982 update_tool_bar (struct frame *f, int save_match_data)
11984 #if defined (USE_GTK) || defined (HAVE_NS)
11985 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11986 #else
11987 int do_update = (WINDOWP (f->tool_bar_window)
11988 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
11989 #endif
11991 if (do_update)
11993 Lisp_Object window;
11994 struct window *w;
11996 window = FRAME_SELECTED_WINDOW (f);
11997 w = XWINDOW (window);
11999 /* If the user has switched buffers or windows, we need to
12000 recompute to reflect the new bindings. But we'll
12001 recompute when update_mode_lines is set too; that means
12002 that people can use force-mode-line-update to request
12003 that the menu bar be recomputed. The adverse effect on
12004 the rest of the redisplay algorithm is about the same as
12005 windows_or_buffers_changed anyway. */
12006 if (windows_or_buffers_changed
12007 || w->update_mode_line
12008 || update_mode_lines
12009 || window_buffer_changed (w))
12011 struct buffer *prev = current_buffer;
12012 ptrdiff_t count = SPECPDL_INDEX ();
12013 Lisp_Object frame, new_tool_bar;
12014 int new_n_tool_bar;
12015 struct gcpro gcpro1;
12017 /* Set current_buffer to the buffer of the selected
12018 window of the frame, so that we get the right local
12019 keymaps. */
12020 set_buffer_internal_1 (XBUFFER (w->contents));
12022 /* Save match data, if we must. */
12023 if (save_match_data)
12024 record_unwind_save_match_data ();
12026 /* Make sure that we don't accidentally use bogus keymaps. */
12027 if (NILP (Voverriding_local_map_menu_flag))
12029 specbind (Qoverriding_terminal_local_map, Qnil);
12030 specbind (Qoverriding_local_map, Qnil);
12033 GCPRO1 (new_tool_bar);
12035 /* We must temporarily set the selected frame to this frame
12036 before calling tool_bar_items, because the calculation of
12037 the tool-bar keymap uses the selected frame (see
12038 `tool-bar-make-keymap' in tool-bar.el). */
12039 eassert (EQ (selected_window,
12040 /* Since we only explicitly preserve selected_frame,
12041 check that selected_window would be redundant. */
12042 XFRAME (selected_frame)->selected_window));
12043 record_unwind_protect (fast_set_selected_frame, selected_frame);
12044 XSETFRAME (frame, f);
12045 fast_set_selected_frame (frame);
12047 /* Build desired tool-bar items from keymaps. */
12048 new_tool_bar
12049 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
12050 &new_n_tool_bar);
12052 /* Redisplay the tool-bar if we changed it. */
12053 if (new_n_tool_bar != f->n_tool_bar_items
12054 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
12056 /* Redisplay that happens asynchronously due to an expose event
12057 may access f->tool_bar_items. Make sure we update both
12058 variables within BLOCK_INPUT so no such event interrupts. */
12059 block_input ();
12060 fset_tool_bar_items (f, new_tool_bar);
12061 f->n_tool_bar_items = new_n_tool_bar;
12062 w->update_mode_line = 1;
12063 unblock_input ();
12066 UNGCPRO;
12068 unbind_to (count, Qnil);
12069 set_buffer_internal_1 (prev);
12074 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12076 /* Set F->desired_tool_bar_string to a Lisp string representing frame
12077 F's desired tool-bar contents. F->tool_bar_items must have
12078 been set up previously by calling prepare_menu_bars. */
12080 static void
12081 build_desired_tool_bar_string (struct frame *f)
12083 int i, size, size_needed;
12084 struct gcpro gcpro1, gcpro2;
12085 Lisp_Object image, plist;
12087 image = plist = Qnil;
12088 GCPRO2 (image, plist);
12090 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
12091 Otherwise, make a new string. */
12093 /* The size of the string we might be able to reuse. */
12094 size = (STRINGP (f->desired_tool_bar_string)
12095 ? SCHARS (f->desired_tool_bar_string)
12096 : 0);
12098 /* We need one space in the string for each image. */
12099 size_needed = f->n_tool_bar_items;
12101 /* Reuse f->desired_tool_bar_string, if possible. */
12102 if (size < size_needed || NILP (f->desired_tool_bar_string))
12103 fset_desired_tool_bar_string
12104 (f, Fmake_string (make_number (size_needed), make_number (' ')));
12105 else
12107 AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
12108 struct gcpro gcpro1;
12109 GCPRO1 (props);
12110 Fremove_text_properties (make_number (0), make_number (size),
12111 props, f->desired_tool_bar_string);
12112 UNGCPRO;
12115 /* Put a `display' property on the string for the images to display,
12116 put a `menu_item' property on tool-bar items with a value that
12117 is the index of the item in F's tool-bar item vector. */
12118 for (i = 0; i < f->n_tool_bar_items; ++i)
12120 #define PROP(IDX) \
12121 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
12123 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
12124 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
12125 int hmargin, vmargin, relief, idx, end;
12127 /* If image is a vector, choose the image according to the
12128 button state. */
12129 image = PROP (TOOL_BAR_ITEM_IMAGES);
12130 if (VECTORP (image))
12132 if (enabled_p)
12133 idx = (selected_p
12134 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
12135 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
12136 else
12137 idx = (selected_p
12138 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
12139 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
12141 eassert (ASIZE (image) >= idx);
12142 image = AREF (image, idx);
12144 else
12145 idx = -1;
12147 /* Ignore invalid image specifications. */
12148 if (!valid_image_p (image))
12149 continue;
12151 /* Display the tool-bar button pressed, or depressed. */
12152 plist = Fcopy_sequence (XCDR (image));
12154 /* Compute margin and relief to draw. */
12155 relief = (tool_bar_button_relief >= 0
12156 ? tool_bar_button_relief
12157 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
12158 hmargin = vmargin = relief;
12160 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
12161 INT_MAX - max (hmargin, vmargin)))
12163 hmargin += XFASTINT (Vtool_bar_button_margin);
12164 vmargin += XFASTINT (Vtool_bar_button_margin);
12166 else if (CONSP (Vtool_bar_button_margin))
12168 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
12169 INT_MAX - hmargin))
12170 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
12172 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
12173 INT_MAX - vmargin))
12174 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
12177 if (auto_raise_tool_bar_buttons_p)
12179 /* Add a `:relief' property to the image spec if the item is
12180 selected. */
12181 if (selected_p)
12183 plist = Fplist_put (plist, QCrelief, make_number (-relief));
12184 hmargin -= relief;
12185 vmargin -= relief;
12188 else
12190 /* If image is selected, display it pressed, i.e. with a
12191 negative relief. If it's not selected, display it with a
12192 raised relief. */
12193 plist = Fplist_put (plist, QCrelief,
12194 (selected_p
12195 ? make_number (-relief)
12196 : make_number (relief)));
12197 hmargin -= relief;
12198 vmargin -= relief;
12201 /* Put a margin around the image. */
12202 if (hmargin || vmargin)
12204 if (hmargin == vmargin)
12205 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
12206 else
12207 plist = Fplist_put (plist, QCmargin,
12208 Fcons (make_number (hmargin),
12209 make_number (vmargin)));
12212 /* If button is not enabled, and we don't have special images
12213 for the disabled state, make the image appear disabled by
12214 applying an appropriate algorithm to it. */
12215 if (!enabled_p && idx < 0)
12216 plist = Fplist_put (plist, QCconversion, Qdisabled);
12218 /* Put a `display' text property on the string for the image to
12219 display. Put a `menu-item' property on the string that gives
12220 the start of this item's properties in the tool-bar items
12221 vector. */
12222 image = Fcons (Qimage, plist);
12223 AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
12224 make_number (i * TOOL_BAR_ITEM_NSLOTS));
12225 struct gcpro gcpro1;
12226 GCPRO1 (props);
12228 /* Let the last image hide all remaining spaces in the tool bar
12229 string. The string can be longer than needed when we reuse a
12230 previous string. */
12231 if (i + 1 == f->n_tool_bar_items)
12232 end = SCHARS (f->desired_tool_bar_string);
12233 else
12234 end = i + 1;
12235 Fadd_text_properties (make_number (i), make_number (end),
12236 props, f->desired_tool_bar_string);
12237 UNGCPRO;
12238 #undef PROP
12241 UNGCPRO;
12245 /* Display one line of the tool-bar of frame IT->f.
12247 HEIGHT specifies the desired height of the tool-bar line.
12248 If the actual height of the glyph row is less than HEIGHT, the
12249 row's height is increased to HEIGHT, and the icons are centered
12250 vertically in the new height.
12252 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
12253 count a final empty row in case the tool-bar width exactly matches
12254 the window width.
12257 static void
12258 display_tool_bar_line (struct it *it, int height)
12260 struct glyph_row *row = it->glyph_row;
12261 int max_x = it->last_visible_x;
12262 struct glyph *last;
12264 /* Don't extend on a previously drawn tool bar items (Bug#16058). */
12265 clear_glyph_row (row);
12266 row->enabled_p = true;
12267 row->y = it->current_y;
12269 /* Note that this isn't made use of if the face hasn't a box,
12270 so there's no need to check the face here. */
12271 it->start_of_box_run_p = 1;
12273 while (it->current_x < max_x)
12275 int x, n_glyphs_before, i, nglyphs;
12276 struct it it_before;
12278 /* Get the next display element. */
12279 if (!get_next_display_element (it))
12281 /* Don't count empty row if we are counting needed tool-bar lines. */
12282 if (height < 0 && !it->hpos)
12283 return;
12284 break;
12287 /* Produce glyphs. */
12288 n_glyphs_before = row->used[TEXT_AREA];
12289 it_before = *it;
12291 PRODUCE_GLYPHS (it);
12293 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
12294 i = 0;
12295 x = it_before.current_x;
12296 while (i < nglyphs)
12298 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
12300 if (x + glyph->pixel_width > max_x)
12302 /* Glyph doesn't fit on line. Backtrack. */
12303 row->used[TEXT_AREA] = n_glyphs_before;
12304 *it = it_before;
12305 /* If this is the only glyph on this line, it will never fit on the
12306 tool-bar, so skip it. But ensure there is at least one glyph,
12307 so we don't accidentally disable the tool-bar. */
12308 if (n_glyphs_before == 0
12309 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
12310 break;
12311 goto out;
12314 ++it->hpos;
12315 x += glyph->pixel_width;
12316 ++i;
12319 /* Stop at line end. */
12320 if (ITERATOR_AT_END_OF_LINE_P (it))
12321 break;
12323 set_iterator_to_next (it, 1);
12326 out:;
12328 row->displays_text_p = row->used[TEXT_AREA] != 0;
12330 /* Use default face for the border below the tool bar.
12332 FIXME: When auto-resize-tool-bars is grow-only, there is
12333 no additional border below the possibly empty tool-bar lines.
12334 So to make the extra empty lines look "normal", we have to
12335 use the tool-bar face for the border too. */
12336 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12337 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
12338 it->face_id = DEFAULT_FACE_ID;
12340 extend_face_to_end_of_line (it);
12341 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
12342 last->right_box_line_p = 1;
12343 if (last == row->glyphs[TEXT_AREA])
12344 last->left_box_line_p = 1;
12346 /* Make line the desired height and center it vertically. */
12347 if ((height -= it->max_ascent + it->max_descent) > 0)
12349 /* Don't add more than one line height. */
12350 height %= FRAME_LINE_HEIGHT (it->f);
12351 it->max_ascent += height / 2;
12352 it->max_descent += (height + 1) / 2;
12355 compute_line_metrics (it);
12357 /* If line is empty, make it occupy the rest of the tool-bar. */
12358 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
12360 row->height = row->phys_height = it->last_visible_y - row->y;
12361 row->visible_height = row->height;
12362 row->ascent = row->phys_ascent = 0;
12363 row->extra_line_spacing = 0;
12366 row->full_width_p = 1;
12367 row->continued_p = 0;
12368 row->truncated_on_left_p = 0;
12369 row->truncated_on_right_p = 0;
12371 it->current_x = it->hpos = 0;
12372 it->current_y += row->height;
12373 ++it->vpos;
12374 ++it->glyph_row;
12378 /* Value is the number of pixels needed to make all tool-bar items of
12379 frame F visible. The actual number of glyph rows needed is
12380 returned in *N_ROWS if non-NULL. */
12381 static int
12382 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
12384 struct window *w = XWINDOW (f->tool_bar_window);
12385 struct it it;
12386 /* tool_bar_height is called from redisplay_tool_bar after building
12387 the desired matrix, so use (unused) mode-line row as temporary row to
12388 avoid destroying the first tool-bar row. */
12389 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
12391 /* Initialize an iterator for iteration over
12392 F->desired_tool_bar_string in the tool-bar window of frame F. */
12393 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
12394 temp_row->reversed_p = false;
12395 it.first_visible_x = 0;
12396 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12397 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12398 it.paragraph_embedding = L2R;
12400 while (!ITERATOR_AT_END_P (&it))
12402 clear_glyph_row (temp_row);
12403 it.glyph_row = temp_row;
12404 display_tool_bar_line (&it, -1);
12406 clear_glyph_row (temp_row);
12408 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
12409 if (n_rows)
12410 *n_rows = it.vpos > 0 ? it.vpos : -1;
12412 if (pixelwise)
12413 return it.current_y;
12414 else
12415 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
12418 #endif /* !USE_GTK && !HAVE_NS */
12420 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
12421 0, 2, 0,
12422 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12423 If FRAME is nil or omitted, use the selected frame. Optional argument
12424 PIXELWISE non-nil means return the height of the tool bar in pixels. */)
12425 (Lisp_Object frame, Lisp_Object pixelwise)
12427 int height = 0;
12429 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12430 struct frame *f = decode_any_frame (frame);
12432 if (WINDOWP (f->tool_bar_window)
12433 && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
12435 update_tool_bar (f, 1);
12436 if (f->n_tool_bar_items)
12438 build_desired_tool_bar_string (f);
12439 height = tool_bar_height (f, NULL, NILP (pixelwise) ? 0 : 1);
12442 #endif
12444 return make_number (height);
12448 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12449 height should be changed. */
12450 static int
12451 redisplay_tool_bar (struct frame *f)
12453 #if defined (USE_GTK) || defined (HAVE_NS)
12455 if (FRAME_EXTERNAL_TOOL_BAR (f))
12456 update_frame_tool_bar (f);
12457 return 0;
12459 #else /* !USE_GTK && !HAVE_NS */
12461 struct window *w;
12462 struct it it;
12463 struct glyph_row *row;
12465 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12466 do anything. This means you must start with tool-bar-lines
12467 non-zero to get the auto-sizing effect. Or in other words, you
12468 can turn off tool-bars by specifying tool-bar-lines zero. */
12469 if (!WINDOWP (f->tool_bar_window)
12470 || (w = XWINDOW (f->tool_bar_window),
12471 WINDOW_TOTAL_LINES (w) == 0))
12472 return 0;
12474 /* Set up an iterator for the tool-bar window. */
12475 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12476 it.first_visible_x = 0;
12477 it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
12478 row = it.glyph_row;
12479 row->reversed_p = false;
12481 /* Build a string that represents the contents of the tool-bar. */
12482 build_desired_tool_bar_string (f);
12483 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12484 /* FIXME: This should be controlled by a user option. But it
12485 doesn't make sense to have an R2L tool bar if the menu bar cannot
12486 be drawn also R2L, and making the menu bar R2L is tricky due
12487 toolkit-specific code that implements it. If an R2L tool bar is
12488 ever supported, display_tool_bar_line should also be augmented to
12489 call unproduce_glyphs like display_line and display_string
12490 do. */
12491 it.paragraph_embedding = L2R;
12493 if (f->n_tool_bar_rows == 0)
12495 int new_height = tool_bar_height (f, &f->n_tool_bar_rows, 1);
12497 if (new_height != WINDOW_PIXEL_HEIGHT (w))
12499 x_change_tool_bar_height (f, new_height);
12500 /* Always do that now. */
12501 clear_glyph_matrix (w->desired_matrix);
12502 f->fonts_changed = 1;
12503 return 1;
12507 /* Display as many lines as needed to display all tool-bar items. */
12509 if (f->n_tool_bar_rows > 0)
12511 int border, rows, height, extra;
12513 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12514 border = XINT (Vtool_bar_border);
12515 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12516 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12517 else if (EQ (Vtool_bar_border, Qborder_width))
12518 border = f->border_width;
12519 else
12520 border = 0;
12521 if (border < 0)
12522 border = 0;
12524 rows = f->n_tool_bar_rows;
12525 height = max (1, (it.last_visible_y - border) / rows);
12526 extra = it.last_visible_y - border - height * rows;
12528 while (it.current_y < it.last_visible_y)
12530 int h = 0;
12531 if (extra > 0 && rows-- > 0)
12533 h = (extra + rows - 1) / rows;
12534 extra -= h;
12536 display_tool_bar_line (&it, height + h);
12539 else
12541 while (it.current_y < it.last_visible_y)
12542 display_tool_bar_line (&it, 0);
12545 /* It doesn't make much sense to try scrolling in the tool-bar
12546 window, so don't do it. */
12547 w->desired_matrix->no_scrolling_p = 1;
12548 w->must_be_updated_p = 1;
12550 if (!NILP (Vauto_resize_tool_bars))
12552 int change_height_p = 0;
12554 /* If we couldn't display everything, change the tool-bar's
12555 height if there is room for more. */
12556 if (IT_STRING_CHARPOS (it) < it.end_charpos)
12557 change_height_p = 1;
12559 /* We subtract 1 because display_tool_bar_line advances the
12560 glyph_row pointer before returning to its caller. We want to
12561 examine the last glyph row produced by
12562 display_tool_bar_line. */
12563 row = it.glyph_row - 1;
12565 /* If there are blank lines at the end, except for a partially
12566 visible blank line at the end that is smaller than
12567 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12568 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12569 && row->height >= FRAME_LINE_HEIGHT (f))
12570 change_height_p = 1;
12572 /* If row displays tool-bar items, but is partially visible,
12573 change the tool-bar's height. */
12574 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12575 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
12576 change_height_p = 1;
12578 /* Resize windows as needed by changing the `tool-bar-lines'
12579 frame parameter. */
12580 if (change_height_p)
12582 int nrows;
12583 int new_height = tool_bar_height (f, &nrows, 1);
12585 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12586 && !f->minimize_tool_bar_window_p)
12587 ? (new_height > WINDOW_PIXEL_HEIGHT (w))
12588 : (new_height != WINDOW_PIXEL_HEIGHT (w)));
12589 f->minimize_tool_bar_window_p = 0;
12591 if (change_height_p)
12593 x_change_tool_bar_height (f, new_height);
12594 clear_glyph_matrix (w->desired_matrix);
12595 f->n_tool_bar_rows = nrows;
12596 f->fonts_changed = 1;
12598 return 1;
12603 f->minimize_tool_bar_window_p = 0;
12604 return 0;
12606 #endif /* USE_GTK || HAVE_NS */
12609 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
12611 /* Get information about the tool-bar item which is displayed in GLYPH
12612 on frame F. Return in *PROP_IDX the index where tool-bar item
12613 properties start in F->tool_bar_items. Value is zero if
12614 GLYPH doesn't display a tool-bar item. */
12616 static int
12617 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12619 Lisp_Object prop;
12620 int success_p;
12621 int charpos;
12623 /* This function can be called asynchronously, which means we must
12624 exclude any possibility that Fget_text_property signals an
12625 error. */
12626 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12627 charpos = max (0, charpos);
12629 /* Get the text property `menu-item' at pos. The value of that
12630 property is the start index of this item's properties in
12631 F->tool_bar_items. */
12632 prop = Fget_text_property (make_number (charpos),
12633 Qmenu_item, f->current_tool_bar_string);
12634 if (INTEGERP (prop))
12636 *prop_idx = XINT (prop);
12637 success_p = 1;
12639 else
12640 success_p = 0;
12642 return success_p;
12646 /* Get information about the tool-bar item at position X/Y on frame F.
12647 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12648 the current matrix of the tool-bar window of F, or NULL if not
12649 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12650 item in F->tool_bar_items. Value is
12652 -1 if X/Y is not on a tool-bar item
12653 0 if X/Y is on the same item that was highlighted before.
12654 1 otherwise. */
12656 static int
12657 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12658 int *hpos, int *vpos, int *prop_idx)
12660 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12661 struct window *w = XWINDOW (f->tool_bar_window);
12662 int area;
12664 /* Find the glyph under X/Y. */
12665 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12666 if (*glyph == NULL)
12667 return -1;
12669 /* Get the start of this tool-bar item's properties in
12670 f->tool_bar_items. */
12671 if (!tool_bar_item_info (f, *glyph, prop_idx))
12672 return -1;
12674 /* Is mouse on the highlighted item? */
12675 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12676 && *vpos >= hlinfo->mouse_face_beg_row
12677 && *vpos <= hlinfo->mouse_face_end_row
12678 && (*vpos > hlinfo->mouse_face_beg_row
12679 || *hpos >= hlinfo->mouse_face_beg_col)
12680 && (*vpos < hlinfo->mouse_face_end_row
12681 || *hpos < hlinfo->mouse_face_end_col
12682 || hlinfo->mouse_face_past_end))
12683 return 0;
12685 return 1;
12689 /* EXPORT:
12690 Handle mouse button event on the tool-bar of frame F, at
12691 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12692 0 for button release. MODIFIERS is event modifiers for button
12693 release. */
12695 void
12696 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12697 int modifiers)
12699 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12700 struct window *w = XWINDOW (f->tool_bar_window);
12701 int hpos, vpos, prop_idx;
12702 struct glyph *glyph;
12703 Lisp_Object enabled_p;
12704 int ts;
12706 /* If not on the highlighted tool-bar item, and mouse-highlight is
12707 non-nil, return. This is so we generate the tool-bar button
12708 click only when the mouse button is released on the same item as
12709 where it was pressed. However, when mouse-highlight is disabled,
12710 generate the click when the button is released regardless of the
12711 highlight, since tool-bar items are not highlighted in that
12712 case. */
12713 frame_to_window_pixel_xy (w, &x, &y);
12714 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12715 if (ts == -1
12716 || (ts != 0 && !NILP (Vmouse_highlight)))
12717 return;
12719 /* When mouse-highlight is off, generate the click for the item
12720 where the button was pressed, disregarding where it was
12721 released. */
12722 if (NILP (Vmouse_highlight) && !down_p)
12723 prop_idx = f->last_tool_bar_item;
12725 /* If item is disabled, do nothing. */
12726 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12727 if (NILP (enabled_p))
12728 return;
12730 if (down_p)
12732 /* Show item in pressed state. */
12733 if (!NILP (Vmouse_highlight))
12734 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12735 f->last_tool_bar_item = prop_idx;
12737 else
12739 Lisp_Object key, frame;
12740 struct input_event event;
12741 EVENT_INIT (event);
12743 /* Show item in released state. */
12744 if (!NILP (Vmouse_highlight))
12745 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12747 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12749 XSETFRAME (frame, f);
12750 event.kind = TOOL_BAR_EVENT;
12751 event.frame_or_window = frame;
12752 event.arg = frame;
12753 kbd_buffer_store_event (&event);
12755 event.kind = TOOL_BAR_EVENT;
12756 event.frame_or_window = frame;
12757 event.arg = key;
12758 event.modifiers = modifiers;
12759 kbd_buffer_store_event (&event);
12760 f->last_tool_bar_item = -1;
12765 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12766 tool-bar window-relative coordinates X/Y. Called from
12767 note_mouse_highlight. */
12769 static void
12770 note_tool_bar_highlight (struct frame *f, int x, int y)
12772 Lisp_Object window = f->tool_bar_window;
12773 struct window *w = XWINDOW (window);
12774 Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
12775 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12776 int hpos, vpos;
12777 struct glyph *glyph;
12778 struct glyph_row *row;
12779 int i;
12780 Lisp_Object enabled_p;
12781 int prop_idx;
12782 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12783 int mouse_down_p, rc;
12785 /* Function note_mouse_highlight is called with negative X/Y
12786 values when mouse moves outside of the frame. */
12787 if (x <= 0 || y <= 0)
12789 clear_mouse_face (hlinfo);
12790 return;
12793 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12794 if (rc < 0)
12796 /* Not on tool-bar item. */
12797 clear_mouse_face (hlinfo);
12798 return;
12800 else if (rc == 0)
12801 /* On same tool-bar item as before. */
12802 goto set_help_echo;
12804 clear_mouse_face (hlinfo);
12806 /* Mouse is down, but on different tool-bar item? */
12807 mouse_down_p = (x_mouse_grabbed (dpyinfo)
12808 && f == dpyinfo->last_mouse_frame);
12810 if (mouse_down_p && f->last_tool_bar_item != prop_idx)
12811 return;
12813 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12815 /* If tool-bar item is not enabled, don't highlight it. */
12816 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12817 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12819 /* Compute the x-position of the glyph. In front and past the
12820 image is a space. We include this in the highlighted area. */
12821 row = MATRIX_ROW (w->current_matrix, vpos);
12822 for (i = x = 0; i < hpos; ++i)
12823 x += row->glyphs[TEXT_AREA][i].pixel_width;
12825 /* Record this as the current active region. */
12826 hlinfo->mouse_face_beg_col = hpos;
12827 hlinfo->mouse_face_beg_row = vpos;
12828 hlinfo->mouse_face_beg_x = x;
12829 hlinfo->mouse_face_past_end = 0;
12831 hlinfo->mouse_face_end_col = hpos + 1;
12832 hlinfo->mouse_face_end_row = vpos;
12833 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12834 hlinfo->mouse_face_window = window;
12835 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12837 /* Display it as active. */
12838 show_mouse_face (hlinfo, draw);
12841 set_help_echo:
12843 /* Set help_echo_string to a help string to display for this tool-bar item.
12844 XTread_socket does the rest. */
12845 help_echo_object = help_echo_window = Qnil;
12846 help_echo_pos = -1;
12847 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12848 if (NILP (help_echo_string))
12849 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12852 #endif /* !USE_GTK && !HAVE_NS */
12854 #endif /* HAVE_WINDOW_SYSTEM */
12858 /************************************************************************
12859 Horizontal scrolling
12860 ************************************************************************/
12862 static int hscroll_window_tree (Lisp_Object);
12863 static int hscroll_windows (Lisp_Object);
12865 /* For all leaf windows in the window tree rooted at WINDOW, set their
12866 hscroll value so that PT is (i) visible in the window, and (ii) so
12867 that it is not within a certain margin at the window's left and
12868 right border. Value is non-zero if any window's hscroll has been
12869 changed. */
12871 static int
12872 hscroll_window_tree (Lisp_Object window)
12874 int hscrolled_p = 0;
12875 int hscroll_relative_p = FLOATP (Vhscroll_step);
12876 int hscroll_step_abs = 0;
12877 double hscroll_step_rel = 0;
12879 if (hscroll_relative_p)
12881 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12882 if (hscroll_step_rel < 0)
12884 hscroll_relative_p = 0;
12885 hscroll_step_abs = 0;
12888 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12890 hscroll_step_abs = XINT (Vhscroll_step);
12891 if (hscroll_step_abs < 0)
12892 hscroll_step_abs = 0;
12894 else
12895 hscroll_step_abs = 0;
12897 while (WINDOWP (window))
12899 struct window *w = XWINDOW (window);
12901 if (WINDOWP (w->contents))
12902 hscrolled_p |= hscroll_window_tree (w->contents);
12903 else if (w->cursor.vpos >= 0)
12905 int h_margin;
12906 int text_area_width;
12907 struct glyph_row *cursor_row;
12908 struct glyph_row *bottom_row;
12909 int row_r2l_p;
12911 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
12912 if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
12913 cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12914 else
12915 cursor_row = bottom_row - 1;
12917 if (!cursor_row->enabled_p)
12919 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
12920 if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
12921 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12922 else
12923 cursor_row = bottom_row - 1;
12925 row_r2l_p = cursor_row->reversed_p;
12927 text_area_width = window_box_width (w, TEXT_AREA);
12929 /* Scroll when cursor is inside this scroll margin. */
12930 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12932 /* If the position of this window's point has explicitly
12933 changed, no more suspend auto hscrolling. */
12934 if (NILP (Fequal (Fwindow_point (window), Fwindow_old_point (window))))
12935 w->suspend_auto_hscroll = 0;
12937 /* Remember window point. */
12938 Fset_marker (w->old_pointm,
12939 ((w == XWINDOW (selected_window))
12940 ? make_number (BUF_PT (XBUFFER (w->contents)))
12941 : Fmarker_position (w->pointm)),
12942 w->contents);
12944 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12945 && w->suspend_auto_hscroll == 0
12946 /* In some pathological cases, like restoring a window
12947 configuration into a frame that is much smaller than
12948 the one from which the configuration was saved, we
12949 get glyph rows whose start and end have zero buffer
12950 positions, which we cannot handle below. Just skip
12951 such windows. */
12952 && CHARPOS (cursor_row->start.pos) >= BUF_BEG (w->contents)
12953 /* For left-to-right rows, hscroll when cursor is either
12954 (i) inside the right hscroll margin, or (ii) if it is
12955 inside the left margin and the window is already
12956 hscrolled. */
12957 && ((!row_r2l_p
12958 && ((w->hscroll && w->cursor.x <= h_margin)
12959 || (cursor_row->enabled_p
12960 && cursor_row->truncated_on_right_p
12961 && (w->cursor.x >= text_area_width - h_margin))))
12962 /* For right-to-left rows, the logic is similar,
12963 except that rules for scrolling to left and right
12964 are reversed. E.g., if cursor.x <= h_margin, we
12965 need to hscroll "to the right" unconditionally,
12966 and that will scroll the screen to the left so as
12967 to reveal the next portion of the row. */
12968 || (row_r2l_p
12969 && ((cursor_row->enabled_p
12970 /* FIXME: It is confusing to set the
12971 truncated_on_right_p flag when R2L rows
12972 are actually truncated on the left. */
12973 && cursor_row->truncated_on_right_p
12974 && w->cursor.x <= h_margin)
12975 || (w->hscroll
12976 && (w->cursor.x >= text_area_width - h_margin))))))
12978 struct it it;
12979 ptrdiff_t hscroll;
12980 struct buffer *saved_current_buffer;
12981 ptrdiff_t pt;
12982 int wanted_x;
12984 /* Find point in a display of infinite width. */
12985 saved_current_buffer = current_buffer;
12986 current_buffer = XBUFFER (w->contents);
12988 if (w == XWINDOW (selected_window))
12989 pt = PT;
12990 else
12991 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12993 /* Move iterator to pt starting at cursor_row->start in
12994 a line with infinite width. */
12995 init_to_row_start (&it, w, cursor_row);
12996 it.last_visible_x = INFINITY;
12997 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12998 current_buffer = saved_current_buffer;
13000 /* Position cursor in window. */
13001 if (!hscroll_relative_p && hscroll_step_abs == 0)
13002 hscroll = max (0, (it.current_x
13003 - (ITERATOR_AT_END_OF_LINE_P (&it)
13004 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
13005 : (text_area_width / 2))))
13006 / FRAME_COLUMN_WIDTH (it.f);
13007 else if ((!row_r2l_p
13008 && w->cursor.x >= text_area_width - h_margin)
13009 || (row_r2l_p && w->cursor.x <= h_margin))
13011 if (hscroll_relative_p)
13012 wanted_x = text_area_width * (1 - hscroll_step_rel)
13013 - h_margin;
13014 else
13015 wanted_x = text_area_width
13016 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13017 - h_margin;
13018 hscroll
13019 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13021 else
13023 if (hscroll_relative_p)
13024 wanted_x = text_area_width * hscroll_step_rel
13025 + h_margin;
13026 else
13027 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
13028 + h_margin;
13029 hscroll
13030 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
13032 hscroll = max (hscroll, w->min_hscroll);
13034 /* Don't prevent redisplay optimizations if hscroll
13035 hasn't changed, as it will unnecessarily slow down
13036 redisplay. */
13037 if (w->hscroll != hscroll)
13039 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
13040 w->hscroll = hscroll;
13041 hscrolled_p = 1;
13046 window = w->next;
13049 /* Value is non-zero if hscroll of any leaf window has been changed. */
13050 return hscrolled_p;
13054 /* Set hscroll so that cursor is visible and not inside horizontal
13055 scroll margins for all windows in the tree rooted at WINDOW. See
13056 also hscroll_window_tree above. Value is non-zero if any window's
13057 hscroll has been changed. If it has, desired matrices on the frame
13058 of WINDOW are cleared. */
13060 static int
13061 hscroll_windows (Lisp_Object window)
13063 int hscrolled_p = hscroll_window_tree (window);
13064 if (hscrolled_p)
13065 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
13066 return hscrolled_p;
13071 /************************************************************************
13072 Redisplay
13073 ************************************************************************/
13075 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
13076 to a non-zero value. This is sometimes handy to have in a debugger
13077 session. */
13079 #ifdef GLYPH_DEBUG
13081 /* First and last unchanged row for try_window_id. */
13083 static int debug_first_unchanged_at_end_vpos;
13084 static int debug_last_unchanged_at_beg_vpos;
13086 /* Delta vpos and y. */
13088 static int debug_dvpos, debug_dy;
13090 /* Delta in characters and bytes for try_window_id. */
13092 static ptrdiff_t debug_delta, debug_delta_bytes;
13094 /* Values of window_end_pos and window_end_vpos at the end of
13095 try_window_id. */
13097 static ptrdiff_t debug_end_vpos;
13099 /* Append a string to W->desired_matrix->method. FMT is a printf
13100 format string. If trace_redisplay_p is true also printf the
13101 resulting string to stderr. */
13103 static void debug_method_add (struct window *, char const *, ...)
13104 ATTRIBUTE_FORMAT_PRINTF (2, 3);
13106 static void
13107 debug_method_add (struct window *w, char const *fmt, ...)
13109 void *ptr = w;
13110 char *method = w->desired_matrix->method;
13111 int len = strlen (method);
13112 int size = sizeof w->desired_matrix->method;
13113 int remaining = size - len - 1;
13114 va_list ap;
13116 if (len && remaining)
13118 method[len] = '|';
13119 --remaining, ++len;
13122 va_start (ap, fmt);
13123 vsnprintf (method + len, remaining + 1, fmt, ap);
13124 va_end (ap);
13126 if (trace_redisplay_p)
13127 fprintf (stderr, "%p (%s): %s\n",
13128 ptr,
13129 ((BUFFERP (w->contents)
13130 && STRINGP (BVAR (XBUFFER (w->contents), name)))
13131 ? SSDATA (BVAR (XBUFFER (w->contents), name))
13132 : "no buffer"),
13133 method + len);
13136 #endif /* GLYPH_DEBUG */
13139 /* Value is non-zero if all changes in window W, which displays
13140 current_buffer, are in the text between START and END. START is a
13141 buffer position, END is given as a distance from Z. Used in
13142 redisplay_internal for display optimization. */
13144 static int
13145 text_outside_line_unchanged_p (struct window *w,
13146 ptrdiff_t start, ptrdiff_t end)
13148 int unchanged_p = 1;
13150 /* If text or overlays have changed, see where. */
13151 if (window_outdated (w))
13153 /* Gap in the line? */
13154 if (GPT < start || Z - GPT < end)
13155 unchanged_p = 0;
13157 /* Changes start in front of the line, or end after it? */
13158 if (unchanged_p
13159 && (BEG_UNCHANGED < start - 1
13160 || END_UNCHANGED < end))
13161 unchanged_p = 0;
13163 /* If selective display, can't optimize if changes start at the
13164 beginning of the line. */
13165 if (unchanged_p
13166 && INTEGERP (BVAR (current_buffer, selective_display))
13167 && XINT (BVAR (current_buffer, selective_display)) > 0
13168 && (BEG_UNCHANGED < start || GPT <= start))
13169 unchanged_p = 0;
13171 /* If there are overlays at the start or end of the line, these
13172 may have overlay strings with newlines in them. A change at
13173 START, for instance, may actually concern the display of such
13174 overlay strings as well, and they are displayed on different
13175 lines. So, quickly rule out this case. (For the future, it
13176 might be desirable to implement something more telling than
13177 just BEG/END_UNCHANGED.) */
13178 if (unchanged_p)
13180 if (BEG + BEG_UNCHANGED == start
13181 && overlay_touches_p (start))
13182 unchanged_p = 0;
13183 if (END_UNCHANGED == end
13184 && overlay_touches_p (Z - end))
13185 unchanged_p = 0;
13188 /* Under bidi reordering, adding or deleting a character in the
13189 beginning of a paragraph, before the first strong directional
13190 character, can change the base direction of the paragraph (unless
13191 the buffer specifies a fixed paragraph direction), which will
13192 require to redisplay the whole paragraph. It might be worthwhile
13193 to find the paragraph limits and widen the range of redisplayed
13194 lines to that, but for now just give up this optimization. */
13195 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
13196 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
13197 unchanged_p = 0;
13200 return unchanged_p;
13204 /* Do a frame update, taking possible shortcuts into account. This is
13205 the main external entry point for redisplay.
13207 If the last redisplay displayed an echo area message and that message
13208 is no longer requested, we clear the echo area or bring back the
13209 mini-buffer if that is in use. */
13211 void
13212 redisplay (void)
13214 redisplay_internal ();
13218 static Lisp_Object
13219 overlay_arrow_string_or_property (Lisp_Object var)
13221 Lisp_Object val;
13223 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
13224 return val;
13226 return Voverlay_arrow_string;
13229 /* Return 1 if there are any overlay-arrows in current_buffer. */
13230 static int
13231 overlay_arrow_in_current_buffer_p (void)
13233 Lisp_Object vlist;
13235 for (vlist = Voverlay_arrow_variable_list;
13236 CONSP (vlist);
13237 vlist = XCDR (vlist))
13239 Lisp_Object var = XCAR (vlist);
13240 Lisp_Object val;
13242 if (!SYMBOLP (var))
13243 continue;
13244 val = find_symbol_value (var);
13245 if (MARKERP (val)
13246 && current_buffer == XMARKER (val)->buffer)
13247 return 1;
13249 return 0;
13253 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
13254 has changed. */
13256 static int
13257 overlay_arrows_changed_p (void)
13259 Lisp_Object vlist;
13261 for (vlist = Voverlay_arrow_variable_list;
13262 CONSP (vlist);
13263 vlist = XCDR (vlist))
13265 Lisp_Object var = XCAR (vlist);
13266 Lisp_Object val, pstr;
13268 if (!SYMBOLP (var))
13269 continue;
13270 val = find_symbol_value (var);
13271 if (!MARKERP (val))
13272 continue;
13273 if (! EQ (COERCE_MARKER (val),
13274 Fget (var, Qlast_arrow_position))
13275 || ! (pstr = overlay_arrow_string_or_property (var),
13276 EQ (pstr, Fget (var, Qlast_arrow_string))))
13277 return 1;
13279 return 0;
13282 /* Mark overlay arrows to be updated on next redisplay. */
13284 static void
13285 update_overlay_arrows (int up_to_date)
13287 Lisp_Object vlist;
13289 for (vlist = Voverlay_arrow_variable_list;
13290 CONSP (vlist);
13291 vlist = XCDR (vlist))
13293 Lisp_Object var = XCAR (vlist);
13295 if (!SYMBOLP (var))
13296 continue;
13298 if (up_to_date > 0)
13300 Lisp_Object val = find_symbol_value (var);
13301 Fput (var, Qlast_arrow_position,
13302 COERCE_MARKER (val));
13303 Fput (var, Qlast_arrow_string,
13304 overlay_arrow_string_or_property (var));
13306 else if (up_to_date < 0
13307 || !NILP (Fget (var, Qlast_arrow_position)))
13309 Fput (var, Qlast_arrow_position, Qt);
13310 Fput (var, Qlast_arrow_string, Qt);
13316 /* Return overlay arrow string to display at row.
13317 Return integer (bitmap number) for arrow bitmap in left fringe.
13318 Return nil if no overlay arrow. */
13320 static Lisp_Object
13321 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
13323 Lisp_Object vlist;
13325 for (vlist = Voverlay_arrow_variable_list;
13326 CONSP (vlist);
13327 vlist = XCDR (vlist))
13329 Lisp_Object var = XCAR (vlist);
13330 Lisp_Object val;
13332 if (!SYMBOLP (var))
13333 continue;
13335 val = find_symbol_value (var);
13337 if (MARKERP (val)
13338 && current_buffer == XMARKER (val)->buffer
13339 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
13341 if (FRAME_WINDOW_P (it->f)
13342 /* FIXME: if ROW->reversed_p is set, this should test
13343 the right fringe, not the left one. */
13344 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
13346 #ifdef HAVE_WINDOW_SYSTEM
13347 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
13349 int fringe_bitmap;
13350 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
13351 return make_number (fringe_bitmap);
13353 #endif
13354 return make_number (-1); /* Use default arrow bitmap. */
13356 return overlay_arrow_string_or_property (var);
13360 return Qnil;
13363 /* Return 1 if point moved out of or into a composition. Otherwise
13364 return 0. PREV_BUF and PREV_PT are the last point buffer and
13365 position. BUF and PT are the current point buffer and position. */
13367 static int
13368 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
13369 struct buffer *buf, ptrdiff_t pt)
13371 ptrdiff_t start, end;
13372 Lisp_Object prop;
13373 Lisp_Object buffer;
13375 XSETBUFFER (buffer, buf);
13376 /* Check a composition at the last point if point moved within the
13377 same buffer. */
13378 if (prev_buf == buf)
13380 if (prev_pt == pt)
13381 /* Point didn't move. */
13382 return 0;
13384 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
13385 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
13386 && composition_valid_p (start, end, prop)
13387 && start < prev_pt && end > prev_pt)
13388 /* The last point was within the composition. Return 1 iff
13389 point moved out of the composition. */
13390 return (pt <= start || pt >= end);
13393 /* Check a composition at the current point. */
13394 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
13395 && find_composition (pt, -1, &start, &end, &prop, buffer)
13396 && composition_valid_p (start, end, prop)
13397 && start < pt && end > pt);
13400 /* Reconsider the clip changes of buffer which is displayed in W. */
13402 static void
13403 reconsider_clip_changes (struct window *w)
13405 struct buffer *b = XBUFFER (w->contents);
13407 if (b->clip_changed
13408 && w->window_end_valid
13409 && w->current_matrix->buffer == b
13410 && w->current_matrix->zv == BUF_ZV (b)
13411 && w->current_matrix->begv == BUF_BEGV (b))
13412 b->clip_changed = 0;
13414 /* If display wasn't paused, and W is not a tool bar window, see if
13415 point has been moved into or out of a composition. In that case,
13416 we set b->clip_changed to 1 to force updating the screen. If
13417 b->clip_changed has already been set to 1, we can skip this
13418 check. */
13419 if (!b->clip_changed && w->window_end_valid)
13421 ptrdiff_t pt = (w == XWINDOW (selected_window)
13422 ? PT : marker_position (w->pointm));
13424 if ((w->current_matrix->buffer != b || pt != w->last_point)
13425 && check_point_in_composition (w->current_matrix->buffer,
13426 w->last_point, b, pt))
13427 b->clip_changed = 1;
13431 static void
13432 propagate_buffer_redisplay (void)
13433 { /* Resetting b->text->redisplay is problematic!
13434 We can't just reset it in the case that some window that displays
13435 it has not been redisplayed; and such a window can stay
13436 unredisplayed for a long time if it's currently invisible.
13437 But we do want to reset it at the end of redisplay otherwise
13438 its displayed windows will keep being redisplayed over and over
13439 again.
13440 So we copy all b->text->redisplay flags up to their windows here,
13441 such that mark_window_display_accurate can safely reset
13442 b->text->redisplay. */
13443 Lisp_Object ws = window_list ();
13444 for (; CONSP (ws); ws = XCDR (ws))
13446 struct window *thisw = XWINDOW (XCAR (ws));
13447 struct buffer *thisb = XBUFFER (thisw->contents);
13448 if (thisb->text->redisplay)
13449 thisw->redisplay = true;
13453 #define STOP_POLLING \
13454 do { if (! polling_stopped_here) stop_polling (); \
13455 polling_stopped_here = 1; } while (0)
13457 #define RESUME_POLLING \
13458 do { if (polling_stopped_here) start_polling (); \
13459 polling_stopped_here = 0; } while (0)
13462 /* Perhaps in the future avoid recentering windows if it
13463 is not necessary; currently that causes some problems. */
13465 static void
13466 redisplay_internal (void)
13468 struct window *w = XWINDOW (selected_window);
13469 struct window *sw;
13470 struct frame *fr;
13471 bool pending;
13472 bool must_finish = 0, match_p;
13473 struct text_pos tlbufpos, tlendpos;
13474 int number_of_visible_frames;
13475 ptrdiff_t count;
13476 struct frame *sf;
13477 int polling_stopped_here = 0;
13478 Lisp_Object tail, frame;
13480 /* True means redisplay has to consider all windows on all
13481 frames. False, only selected_window is considered. */
13482 bool consider_all_windows_p;
13484 /* True means redisplay has to redisplay the miniwindow. */
13485 bool update_miniwindow_p = false;
13487 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13489 /* No redisplay if running in batch mode or frame is not yet fully
13490 initialized, or redisplay is explicitly turned off by setting
13491 Vinhibit_redisplay. */
13492 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13493 || !NILP (Vinhibit_redisplay))
13494 return;
13496 /* Don't examine these until after testing Vinhibit_redisplay.
13497 When Emacs is shutting down, perhaps because its connection to
13498 X has dropped, we should not look at them at all. */
13499 fr = XFRAME (w->frame);
13500 sf = SELECTED_FRAME ();
13502 if (!fr->glyphs_initialized_p)
13503 return;
13505 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13506 if (popup_activated ())
13507 return;
13508 #endif
13510 /* I don't think this happens but let's be paranoid. */
13511 if (redisplaying_p)
13512 return;
13514 /* Record a function that clears redisplaying_p
13515 when we leave this function. */
13516 count = SPECPDL_INDEX ();
13517 record_unwind_protect_void (unwind_redisplay);
13518 redisplaying_p = 1;
13519 specbind (Qinhibit_free_realized_faces, Qnil);
13521 /* Record this function, so it appears on the profiler's backtraces. */
13522 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
13524 FOR_EACH_FRAME (tail, frame)
13525 XFRAME (frame)->already_hscrolled_p = 0;
13527 retry:
13528 /* Remember the currently selected window. */
13529 sw = w;
13531 pending = false;
13532 last_escape_glyph_frame = NULL;
13533 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13534 last_glyphless_glyph_frame = NULL;
13535 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13537 /* If face_change_count is non-zero, init_iterator will free all
13538 realized faces, which includes the faces referenced from current
13539 matrices. So, we can't reuse current matrices in this case. */
13540 if (face_change_count)
13541 windows_or_buffers_changed = 47;
13543 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13544 && FRAME_TTY (sf)->previous_frame != sf)
13546 /* Since frames on a single ASCII terminal share the same
13547 display area, displaying a different frame means redisplay
13548 the whole thing. */
13549 SET_FRAME_GARBAGED (sf);
13550 #ifndef DOS_NT
13551 set_tty_color_mode (FRAME_TTY (sf), sf);
13552 #endif
13553 FRAME_TTY (sf)->previous_frame = sf;
13556 /* Set the visible flags for all frames. Do this before checking for
13557 resized or garbaged frames; they want to know if their frames are
13558 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13559 number_of_visible_frames = 0;
13561 FOR_EACH_FRAME (tail, frame)
13563 struct frame *f = XFRAME (frame);
13565 if (FRAME_VISIBLE_P (f))
13567 ++number_of_visible_frames;
13568 /* Adjust matrices for visible frames only. */
13569 if (f->fonts_changed)
13571 adjust_frame_glyphs (f);
13572 f->fonts_changed = 0;
13574 /* If cursor type has been changed on the frame
13575 other than selected, consider all frames. */
13576 if (f != sf && f->cursor_type_changed)
13577 update_mode_lines = 31;
13579 clear_desired_matrices (f);
13582 /* Notice any pending interrupt request to change frame size. */
13583 do_pending_window_change (true);
13585 /* do_pending_window_change could change the selected_window due to
13586 frame resizing which makes the selected window too small. */
13587 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13588 sw = w;
13590 /* Clear frames marked as garbaged. */
13591 clear_garbaged_frames ();
13593 /* Build menubar and tool-bar items. */
13594 if (NILP (Vmemory_full))
13595 prepare_menu_bars ();
13597 reconsider_clip_changes (w);
13599 /* In most cases selected window displays current buffer. */
13600 match_p = XBUFFER (w->contents) == current_buffer;
13601 if (match_p)
13603 /* Detect case that we need to write or remove a star in the mode line. */
13604 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13605 w->update_mode_line = 1;
13607 if (mode_line_update_needed (w))
13608 w->update_mode_line = 1;
13610 /* If reconsider_clip_changes above decided that the narrowing
13611 in the current buffer changed, make sure all other windows
13612 showing that buffer will be redisplayed. */
13613 if (current_buffer->clip_changed)
13614 bset_update_mode_line (current_buffer);
13617 /* Normally the message* functions will have already displayed and
13618 updated the echo area, but the frame may have been trashed, or
13619 the update may have been preempted, so display the echo area
13620 again here. Checking message_cleared_p captures the case that
13621 the echo area should be cleared. */
13622 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13623 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13624 || (message_cleared_p
13625 && minibuf_level == 0
13626 /* If the mini-window is currently selected, this means the
13627 echo-area doesn't show through. */
13628 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13630 int window_height_changed_p = echo_area_display (false);
13632 if (message_cleared_p)
13633 update_miniwindow_p = true;
13635 must_finish = 1;
13637 /* If we don't display the current message, don't clear the
13638 message_cleared_p flag, because, if we did, we wouldn't clear
13639 the echo area in the next redisplay which doesn't preserve
13640 the echo area. */
13641 if (!display_last_displayed_message_p)
13642 message_cleared_p = 0;
13644 if (window_height_changed_p)
13646 windows_or_buffers_changed = 50;
13648 /* If window configuration was changed, frames may have been
13649 marked garbaged. Clear them or we will experience
13650 surprises wrt scrolling. */
13651 clear_garbaged_frames ();
13654 else if (EQ (selected_window, minibuf_window)
13655 && (current_buffer->clip_changed || window_outdated (w))
13656 && resize_mini_window (w, 0))
13658 /* Resized active mini-window to fit the size of what it is
13659 showing if its contents might have changed. */
13660 must_finish = 1;
13662 /* If window configuration was changed, frames may have been
13663 marked garbaged. Clear them or we will experience
13664 surprises wrt scrolling. */
13665 clear_garbaged_frames ();
13668 if (windows_or_buffers_changed && !update_mode_lines)
13669 /* Code that sets windows_or_buffers_changed doesn't distinguish whether
13670 only the windows's contents needs to be refreshed, or whether the
13671 mode-lines also need a refresh. */
13672 update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
13673 ? REDISPLAY_SOME : 32);
13675 /* If specs for an arrow have changed, do thorough redisplay
13676 to ensure we remove any arrow that should no longer exist. */
13677 if (overlay_arrows_changed_p ())
13678 /* Apparently, this is the only case where we update other windows,
13679 without updating other mode-lines. */
13680 windows_or_buffers_changed = 49;
13682 consider_all_windows_p = (update_mode_lines
13683 || windows_or_buffers_changed);
13685 #define AINC(a,i) \
13686 if (VECTORP (a) && i >= 0 && i < ASIZE (a) && INTEGERP (AREF (a, i))) \
13687 ASET (a, i, make_number (1 + XINT (AREF (a, i))))
13689 AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
13690 AINC (Vredisplay__mode_lines_cause, update_mode_lines);
13692 /* Optimize the case that only the line containing the cursor in the
13693 selected window has changed. Variables starting with this_ are
13694 set in display_line and record information about the line
13695 containing the cursor. */
13696 tlbufpos = this_line_start_pos;
13697 tlendpos = this_line_end_pos;
13698 if (!consider_all_windows_p
13699 && CHARPOS (tlbufpos) > 0
13700 && !w->update_mode_line
13701 && !current_buffer->clip_changed
13702 && !current_buffer->prevent_redisplay_optimizations_p
13703 && FRAME_VISIBLE_P (XFRAME (w->frame))
13704 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13705 && !XFRAME (w->frame)->cursor_type_changed
13706 /* Make sure recorded data applies to current buffer, etc. */
13707 && this_line_buffer == current_buffer
13708 && match_p
13709 && !w->force_start
13710 && !w->optional_new_start
13711 /* Point must be on the line that we have info recorded about. */
13712 && PT >= CHARPOS (tlbufpos)
13713 && PT <= Z - CHARPOS (tlendpos)
13714 /* All text outside that line, including its final newline,
13715 must be unchanged. */
13716 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13717 CHARPOS (tlendpos)))
13719 if (CHARPOS (tlbufpos) > BEGV
13720 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13721 && (CHARPOS (tlbufpos) == ZV
13722 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13723 /* Former continuation line has disappeared by becoming empty. */
13724 goto cancel;
13725 else if (window_outdated (w) || MINI_WINDOW_P (w))
13727 /* We have to handle the case of continuation around a
13728 wide-column character (see the comment in indent.c around
13729 line 1340).
13731 For instance, in the following case:
13733 -------- Insert --------
13734 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13735 J_I_ ==> J_I_ `^^' are cursors.
13736 ^^ ^^
13737 -------- --------
13739 As we have to redraw the line above, we cannot use this
13740 optimization. */
13742 struct it it;
13743 int line_height_before = this_line_pixel_height;
13745 /* Note that start_display will handle the case that the
13746 line starting at tlbufpos is a continuation line. */
13747 start_display (&it, w, tlbufpos);
13749 /* Implementation note: It this still necessary? */
13750 if (it.current_x != this_line_start_x)
13751 goto cancel;
13753 TRACE ((stderr, "trying display optimization 1\n"));
13754 w->cursor.vpos = -1;
13755 overlay_arrow_seen = 0;
13756 it.vpos = this_line_vpos;
13757 it.current_y = this_line_y;
13758 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13759 display_line (&it);
13761 /* If line contains point, is not continued,
13762 and ends at same distance from eob as before, we win. */
13763 if (w->cursor.vpos >= 0
13764 /* Line is not continued, otherwise this_line_start_pos
13765 would have been set to 0 in display_line. */
13766 && CHARPOS (this_line_start_pos)
13767 /* Line ends as before. */
13768 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13769 /* Line has same height as before. Otherwise other lines
13770 would have to be shifted up or down. */
13771 && this_line_pixel_height == line_height_before)
13773 /* If this is not the window's last line, we must adjust
13774 the charstarts of the lines below. */
13775 if (it.current_y < it.last_visible_y)
13777 struct glyph_row *row
13778 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13779 ptrdiff_t delta, delta_bytes;
13781 /* We used to distinguish between two cases here,
13782 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13783 when the line ends in a newline or the end of the
13784 buffer's accessible portion. But both cases did
13785 the same, so they were collapsed. */
13786 delta = (Z
13787 - CHARPOS (tlendpos)
13788 - MATRIX_ROW_START_CHARPOS (row));
13789 delta_bytes = (Z_BYTE
13790 - BYTEPOS (tlendpos)
13791 - MATRIX_ROW_START_BYTEPOS (row));
13793 increment_matrix_positions (w->current_matrix,
13794 this_line_vpos + 1,
13795 w->current_matrix->nrows,
13796 delta, delta_bytes);
13799 /* If this row displays text now but previously didn't,
13800 or vice versa, w->window_end_vpos may have to be
13801 adjusted. */
13802 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13804 if (w->window_end_vpos < this_line_vpos)
13805 w->window_end_vpos = this_line_vpos;
13807 else if (w->window_end_vpos == this_line_vpos
13808 && this_line_vpos > 0)
13809 w->window_end_vpos = this_line_vpos - 1;
13810 w->window_end_valid = 0;
13812 /* Update hint: No need to try to scroll in update_window. */
13813 w->desired_matrix->no_scrolling_p = 1;
13815 #ifdef GLYPH_DEBUG
13816 *w->desired_matrix->method = 0;
13817 debug_method_add (w, "optimization 1");
13818 #endif
13819 #if HAVE_XWIDGETS
13820 //debug optimization movement issue
13821 //w->desired_matrix->no_scrolling_p = 1;
13822 //*w->desired_matrix->method = 0;
13823 //debug_method_add (w, "optimization 1");
13824 #endif
13826 #ifdef HAVE_WINDOW_SYSTEM
13827 update_window_fringes (w, 0);
13828 #endif
13829 goto update;
13831 else
13832 goto cancel;
13834 else if (/* Cursor position hasn't changed. */
13835 PT == w->last_point
13836 /* Make sure the cursor was last displayed
13837 in this window. Otherwise we have to reposition it. */
13839 /* PXW: Must be converted to pixels, probably. */
13840 && 0 <= w->cursor.vpos
13841 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13843 if (!must_finish)
13845 do_pending_window_change (true);
13846 /* If selected_window changed, redisplay again. */
13847 if (WINDOWP (selected_window)
13848 && (w = XWINDOW (selected_window)) != sw)
13849 goto retry;
13851 /* We used to always goto end_of_redisplay here, but this
13852 isn't enough if we have a blinking cursor. */
13853 if (w->cursor_off_p == w->last_cursor_off_p)
13854 goto end_of_redisplay;
13856 goto update;
13858 /* If highlighting the region, or if the cursor is in the echo area,
13859 then we can't just move the cursor. */
13860 else if (NILP (Vshow_trailing_whitespace)
13861 && !cursor_in_echo_area)
13863 struct it it;
13864 struct glyph_row *row;
13866 /* Skip from tlbufpos to PT and see where it is. Note that
13867 PT may be in invisible text. If so, we will end at the
13868 next visible position. */
13869 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13870 NULL, DEFAULT_FACE_ID);
13871 it.current_x = this_line_start_x;
13872 it.current_y = this_line_y;
13873 it.vpos = this_line_vpos;
13875 /* The call to move_it_to stops in front of PT, but
13876 moves over before-strings. */
13877 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13879 if (it.vpos == this_line_vpos
13880 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13881 row->enabled_p))
13883 eassert (this_line_vpos == it.vpos);
13884 eassert (this_line_y == it.current_y);
13885 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13886 #ifdef GLYPH_DEBUG
13887 *w->desired_matrix->method = 0;
13888 debug_method_add (w, "optimization 3");
13889 #endif
13890 goto update;
13892 else
13893 goto cancel;
13896 cancel:
13897 /* Text changed drastically or point moved off of line. */
13898 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
13901 CHARPOS (this_line_start_pos) = 0;
13902 ++clear_face_cache_count;
13903 #ifdef HAVE_WINDOW_SYSTEM
13904 ++clear_image_cache_count;
13905 #endif
13907 /* Build desired matrices, and update the display. If
13908 consider_all_windows_p is non-zero, do it for all windows on all
13909 frames. Otherwise do it for selected_window, only. */
13911 if (consider_all_windows_p)
13913 FOR_EACH_FRAME (tail, frame)
13914 XFRAME (frame)->updated_p = 0;
13916 propagate_buffer_redisplay ();
13918 FOR_EACH_FRAME (tail, frame)
13920 struct frame *f = XFRAME (frame);
13922 /* We don't have to do anything for unselected terminal
13923 frames. */
13924 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13925 && !EQ (FRAME_TTY (f)->top_frame, frame))
13926 continue;
13928 retry_frame:
13930 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13932 bool gcscrollbars
13933 /* Only GC scrollbars when we redisplay the whole frame. */
13934 = f->redisplay || !REDISPLAY_SOME_P ();
13935 /* Mark all the scroll bars to be removed; we'll redeem
13936 the ones we want when we redisplay their windows. */
13937 if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13938 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13940 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13941 redisplay_windows (FRAME_ROOT_WINDOW (f));
13942 /* Remember that the invisible frames need to be redisplayed next
13943 time they're visible. */
13944 else if (!REDISPLAY_SOME_P ())
13945 f->redisplay = true;
13947 /* The X error handler may have deleted that frame. */
13948 if (!FRAME_LIVE_P (f))
13949 continue;
13951 /* Any scroll bars which redisplay_windows should have
13952 nuked should now go away. */
13953 if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13954 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13956 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13958 /* If fonts changed on visible frame, display again. */
13959 if (f->fonts_changed)
13961 adjust_frame_glyphs (f);
13962 f->fonts_changed = false;
13963 goto retry_frame;
13966 /* See if we have to hscroll. */
13967 if (!f->already_hscrolled_p)
13969 f->already_hscrolled_p = true;
13970 if (hscroll_windows (f->root_window))
13971 goto retry_frame;
13974 /* Prevent various kinds of signals during display
13975 update. stdio is not robust about handling
13976 signals, which can cause an apparent I/O error. */
13977 if (interrupt_input)
13978 unrequest_sigio ();
13979 STOP_POLLING;
13981 pending |= update_frame (f, false, false);
13982 f->cursor_type_changed = false;
13983 f->updated_p = true;
13988 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13990 if (!pending)
13992 /* Do the mark_window_display_accurate after all windows have
13993 been redisplayed because this call resets flags in buffers
13994 which are needed for proper redisplay. */
13995 FOR_EACH_FRAME (tail, frame)
13997 struct frame *f = XFRAME (frame);
13998 if (f->updated_p)
14000 f->redisplay = false;
14001 mark_window_display_accurate (f->root_window, 1);
14002 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
14003 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
14008 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14010 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
14011 struct frame *mini_frame;
14013 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
14014 /* Use list_of_error, not Qerror, so that
14015 we catch only errors and don't run the debugger. */
14016 internal_condition_case_1 (redisplay_window_1, selected_window,
14017 list_of_error,
14018 redisplay_window_error);
14019 if (update_miniwindow_p)
14020 internal_condition_case_1 (redisplay_window_1, mini_window,
14021 list_of_error,
14022 redisplay_window_error);
14024 /* Compare desired and current matrices, perform output. */
14026 update:
14027 /* If fonts changed, display again. */
14028 if (sf->fonts_changed)
14029 goto retry;
14031 /* Prevent various kinds of signals during display update.
14032 stdio is not robust about handling signals,
14033 which can cause an apparent I/O error. */
14034 if (interrupt_input)
14035 unrequest_sigio ();
14036 STOP_POLLING;
14038 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
14040 if (hscroll_windows (selected_window))
14041 goto retry;
14043 XWINDOW (selected_window)->must_be_updated_p = true;
14044 pending = update_frame (sf, false, false);
14045 sf->cursor_type_changed = false;
14048 /* We may have called echo_area_display at the top of this
14049 function. If the echo area is on another frame, that may
14050 have put text on a frame other than the selected one, so the
14051 above call to update_frame would not have caught it. Catch
14052 it here. */
14053 mini_window = FRAME_MINIBUF_WINDOW (sf);
14054 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
14056 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
14058 XWINDOW (mini_window)->must_be_updated_p = true;
14059 pending |= update_frame (mini_frame, false, false);
14060 mini_frame->cursor_type_changed = false;
14061 if (!pending && hscroll_windows (mini_window))
14062 goto retry;
14066 /* If display was paused because of pending input, make sure we do a
14067 thorough update the next time. */
14068 if (pending)
14070 /* Prevent the optimization at the beginning of
14071 redisplay_internal that tries a single-line update of the
14072 line containing the cursor in the selected window. */
14073 CHARPOS (this_line_start_pos) = 0;
14075 /* Let the overlay arrow be updated the next time. */
14076 update_overlay_arrows (0);
14078 /* If we pause after scrolling, some rows in the current
14079 matrices of some windows are not valid. */
14080 if (!WINDOW_FULL_WIDTH_P (w)
14081 && !FRAME_WINDOW_P (XFRAME (w->frame)))
14082 update_mode_lines = 36;
14084 else
14086 if (!consider_all_windows_p)
14088 /* This has already been done above if
14089 consider_all_windows_p is set. */
14090 if (XBUFFER (w->contents)->text->redisplay
14091 && buffer_window_count (XBUFFER (w->contents)) > 1)
14092 /* This can happen if b->text->redisplay was set during
14093 jit-lock. */
14094 propagate_buffer_redisplay ();
14095 mark_window_display_accurate_1 (w, 1);
14097 /* Say overlay arrows are up to date. */
14098 update_overlay_arrows (1);
14100 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
14101 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
14104 update_mode_lines = 0;
14105 windows_or_buffers_changed = 0;
14108 /* Start SIGIO interrupts coming again. Having them off during the
14109 code above makes it less likely one will discard output, but not
14110 impossible, since there might be stuff in the system buffer here.
14111 But it is much hairier to try to do anything about that. */
14112 if (interrupt_input)
14113 request_sigio ();
14114 RESUME_POLLING;
14116 /* If a frame has become visible which was not before, redisplay
14117 again, so that we display it. Expose events for such a frame
14118 (which it gets when becoming visible) don't call the parts of
14119 redisplay constructing glyphs, so simply exposing a frame won't
14120 display anything in this case. So, we have to display these
14121 frames here explicitly. */
14122 if (!pending)
14124 int new_count = 0;
14126 FOR_EACH_FRAME (tail, frame)
14128 if (XFRAME (frame)->visible)
14129 new_count++;
14132 if (new_count != number_of_visible_frames)
14133 windows_or_buffers_changed = 52;
14136 /* Change frame size now if a change is pending. */
14137 do_pending_window_change (true);
14139 /* If we just did a pending size change, or have additional
14140 visible frames, or selected_window changed, redisplay again. */
14141 if ((windows_or_buffers_changed && !pending)
14142 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
14143 goto retry;
14145 /* Clear the face and image caches.
14147 We used to do this only if consider_all_windows_p. But the cache
14148 needs to be cleared if a timer creates images in the current
14149 buffer (e.g. the test case in Bug#6230). */
14151 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
14153 clear_face_cache (false);
14154 clear_face_cache_count = 0;
14157 #ifdef HAVE_WINDOW_SYSTEM
14158 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
14160 clear_image_caches (Qnil);
14161 clear_image_cache_count = 0;
14163 #endif /* HAVE_WINDOW_SYSTEM */
14165 end_of_redisplay:
14166 #ifdef HAVE_NS
14167 ns_set_doc_edited ();
14168 #endif
14169 if (interrupt_input && interrupts_deferred)
14170 request_sigio ();
14172 unbind_to (count, Qnil);
14173 RESUME_POLLING;
14177 /* Redisplay, but leave alone any recent echo area message unless
14178 another message has been requested in its place.
14180 This is useful in situations where you need to redisplay but no
14181 user action has occurred, making it inappropriate for the message
14182 area to be cleared. See tracking_off and
14183 wait_reading_process_output for examples of these situations.
14185 FROM_WHERE is an integer saying from where this function was
14186 called. This is useful for debugging. */
14188 void
14189 redisplay_preserve_echo_area (int from_where)
14191 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
14193 if (!NILP (echo_area_buffer[1]))
14195 /* We have a previously displayed message, but no current
14196 message. Redisplay the previous message. */
14197 display_last_displayed_message_p = true;
14198 redisplay_internal ();
14199 display_last_displayed_message_p = false;
14201 else
14202 redisplay_internal ();
14204 flush_frame (SELECTED_FRAME ());
14208 /* Function registered with record_unwind_protect in redisplay_internal. */
14210 static void
14211 unwind_redisplay (void)
14213 redisplaying_p = 0;
14217 /* Mark the display of leaf window W as accurate or inaccurate.
14218 If ACCURATE_P is non-zero mark display of W as accurate. If
14219 ACCURATE_P is zero, arrange for W to be redisplayed the next
14220 time redisplay_internal is called. */
14222 static void
14223 mark_window_display_accurate_1 (struct window *w, int accurate_p)
14225 struct buffer *b = XBUFFER (w->contents);
14227 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
14228 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
14229 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
14231 if (accurate_p)
14233 b->clip_changed = false;
14234 b->prevent_redisplay_optimizations_p = false;
14235 eassert (buffer_window_count (b) > 0);
14236 /* Resetting b->text->redisplay is problematic!
14237 In order to make it safer to do it here, redisplay_internal must
14238 have copied all b->text->redisplay to their respective windows. */
14239 b->text->redisplay = false;
14241 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
14242 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
14243 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
14244 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
14246 w->current_matrix->buffer = b;
14247 w->current_matrix->begv = BUF_BEGV (b);
14248 w->current_matrix->zv = BUF_ZV (b);
14250 w->last_cursor_vpos = w->cursor.vpos;
14251 w->last_cursor_off_p = w->cursor_off_p;
14253 if (w == XWINDOW (selected_window))
14254 w->last_point = BUF_PT (b);
14255 else
14256 w->last_point = marker_position (w->pointm);
14258 w->window_end_valid = true;
14259 w->update_mode_line = false;
14262 w->redisplay = !accurate_p;
14266 /* Mark the display of windows in the window tree rooted at WINDOW as
14267 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
14268 windows as accurate. If ACCURATE_P is zero, arrange for windows to
14269 be redisplayed the next time redisplay_internal is called. */
14271 void
14272 mark_window_display_accurate (Lisp_Object window, int accurate_p)
14274 struct window *w;
14276 for (; !NILP (window); window = w->next)
14278 w = XWINDOW (window);
14279 if (WINDOWP (w->contents))
14280 mark_window_display_accurate (w->contents, accurate_p);
14281 else
14282 mark_window_display_accurate_1 (w, accurate_p);
14285 if (accurate_p)
14286 update_overlay_arrows (1);
14287 else
14288 /* Force a thorough redisplay the next time by setting
14289 last_arrow_position and last_arrow_string to t, which is
14290 unequal to any useful value of Voverlay_arrow_... */
14291 update_overlay_arrows (-1);
14295 /* Return value in display table DP (Lisp_Char_Table *) for character
14296 C. Since a display table doesn't have any parent, we don't have to
14297 follow parent. Do not call this function directly but use the
14298 macro DISP_CHAR_VECTOR. */
14300 Lisp_Object
14301 disp_char_vector (struct Lisp_Char_Table *dp, int c)
14303 Lisp_Object val;
14305 if (ASCII_CHAR_P (c))
14307 val = dp->ascii;
14308 if (SUB_CHAR_TABLE_P (val))
14309 val = XSUB_CHAR_TABLE (val)->contents[c];
14311 else
14313 Lisp_Object table;
14315 XSETCHAR_TABLE (table, dp);
14316 val = char_table_ref (table, c);
14318 if (NILP (val))
14319 val = dp->defalt;
14320 return val;
14325 /***********************************************************************
14326 Window Redisplay
14327 ***********************************************************************/
14329 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
14331 static void
14332 redisplay_windows (Lisp_Object window)
14334 while (!NILP (window))
14336 struct window *w = XWINDOW (window);
14338 if (WINDOWP (w->contents))
14339 redisplay_windows (w->contents);
14340 else if (BUFFERP (w->contents))
14342 displayed_buffer = XBUFFER (w->contents);
14343 /* Use list_of_error, not Qerror, so that
14344 we catch only errors and don't run the debugger. */
14345 internal_condition_case_1 (redisplay_window_0, window,
14346 list_of_error,
14347 redisplay_window_error);
14350 window = w->next;
14354 static Lisp_Object
14355 redisplay_window_error (Lisp_Object ignore)
14357 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
14358 return Qnil;
14361 static Lisp_Object
14362 redisplay_window_0 (Lisp_Object window)
14364 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14365 redisplay_window (window, false);
14366 return Qnil;
14369 static Lisp_Object
14370 redisplay_window_1 (Lisp_Object window)
14372 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14373 redisplay_window (window, true);
14374 return Qnil;
14378 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14379 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14380 which positions recorded in ROW differ from current buffer
14381 positions.
14383 Return 0 if cursor is not on this row, 1 otherwise. */
14385 static int
14386 set_cursor_from_row (struct window *w, struct glyph_row *row,
14387 struct glyph_matrix *matrix,
14388 ptrdiff_t delta, ptrdiff_t delta_bytes,
14389 int dy, int dvpos)
14391 struct glyph *glyph = row->glyphs[TEXT_AREA];
14392 struct glyph *end = glyph + row->used[TEXT_AREA];
14393 struct glyph *cursor = NULL;
14394 /* The last known character position in row. */
14395 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14396 int x = row->x;
14397 ptrdiff_t pt_old = PT - delta;
14398 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14399 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14400 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14401 /* A glyph beyond the edge of TEXT_AREA which we should never
14402 touch. */
14403 struct glyph *glyphs_end = end;
14404 /* Non-zero means we've found a match for cursor position, but that
14405 glyph has the avoid_cursor_p flag set. */
14406 int match_with_avoid_cursor = 0;
14407 /* Non-zero means we've seen at least one glyph that came from a
14408 display string. */
14409 int string_seen = 0;
14410 /* Largest and smallest buffer positions seen so far during scan of
14411 glyph row. */
14412 ptrdiff_t bpos_max = pos_before;
14413 ptrdiff_t bpos_min = pos_after;
14414 /* Last buffer position covered by an overlay string with an integer
14415 `cursor' property. */
14416 ptrdiff_t bpos_covered = 0;
14417 /* Non-zero means the display string on which to display the cursor
14418 comes from a text property, not from an overlay. */
14419 int string_from_text_prop = 0;
14421 /* Don't even try doing anything if called for a mode-line or
14422 header-line row, since the rest of the code isn't prepared to
14423 deal with such calamities. */
14424 eassert (!row->mode_line_p);
14425 if (row->mode_line_p)
14426 return 0;
14428 /* Skip over glyphs not having an object at the start and the end of
14429 the row. These are special glyphs like truncation marks on
14430 terminal frames. */
14431 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
14433 if (!row->reversed_p)
14435 while (glyph < end
14436 && INTEGERP (glyph->object)
14437 && glyph->charpos < 0)
14439 x += glyph->pixel_width;
14440 ++glyph;
14442 while (end > glyph
14443 && INTEGERP ((end - 1)->object)
14444 /* CHARPOS is zero for blanks and stretch glyphs
14445 inserted by extend_face_to_end_of_line. */
14446 && (end - 1)->charpos <= 0)
14447 --end;
14448 glyph_before = glyph - 1;
14449 glyph_after = end;
14451 else
14453 struct glyph *g;
14455 /* If the glyph row is reversed, we need to process it from back
14456 to front, so swap the edge pointers. */
14457 glyphs_end = end = glyph - 1;
14458 glyph += row->used[TEXT_AREA] - 1;
14460 while (glyph > end + 1
14461 && INTEGERP (glyph->object)
14462 && glyph->charpos < 0)
14464 --glyph;
14465 x -= glyph->pixel_width;
14467 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14468 --glyph;
14469 /* By default, in reversed rows we put the cursor on the
14470 rightmost (first in the reading order) glyph. */
14471 for (g = end + 1; g < glyph; g++)
14472 x += g->pixel_width;
14473 while (end < glyph
14474 && INTEGERP ((end + 1)->object)
14475 && (end + 1)->charpos <= 0)
14476 ++end;
14477 glyph_before = glyph + 1;
14478 glyph_after = end;
14481 else if (row->reversed_p)
14483 /* In R2L rows that don't display text, put the cursor on the
14484 rightmost glyph. Case in point: an empty last line that is
14485 part of an R2L paragraph. */
14486 cursor = end - 1;
14487 /* Avoid placing the cursor on the last glyph of the row, where
14488 on terminal frames we hold the vertical border between
14489 adjacent windows. */
14490 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14491 && !WINDOW_RIGHTMOST_P (w)
14492 && cursor == row->glyphs[LAST_AREA] - 1)
14493 cursor--;
14494 x = -1; /* will be computed below, at label compute_x */
14497 /* Step 1: Try to find the glyph whose character position
14498 corresponds to point. If that's not possible, find 2 glyphs
14499 whose character positions are the closest to point, one before
14500 point, the other after it. */
14501 if (!row->reversed_p)
14502 while (/* not marched to end of glyph row */
14503 glyph < end
14504 /* glyph was not inserted by redisplay for internal purposes */
14505 && !INTEGERP (glyph->object))
14507 if (BUFFERP (glyph->object))
14509 ptrdiff_t dpos = glyph->charpos - pt_old;
14511 if (glyph->charpos > bpos_max)
14512 bpos_max = glyph->charpos;
14513 if (glyph->charpos < bpos_min)
14514 bpos_min = glyph->charpos;
14515 if (!glyph->avoid_cursor_p)
14517 /* If we hit point, we've found the glyph on which to
14518 display the cursor. */
14519 if (dpos == 0)
14521 match_with_avoid_cursor = 0;
14522 break;
14524 /* See if we've found a better approximation to
14525 POS_BEFORE or to POS_AFTER. */
14526 if (0 > dpos && dpos > pos_before - pt_old)
14528 pos_before = glyph->charpos;
14529 glyph_before = glyph;
14531 else if (0 < dpos && dpos < pos_after - pt_old)
14533 pos_after = glyph->charpos;
14534 glyph_after = glyph;
14537 else if (dpos == 0)
14538 match_with_avoid_cursor = 1;
14540 else if (STRINGP (glyph->object))
14542 Lisp_Object chprop;
14543 ptrdiff_t glyph_pos = glyph->charpos;
14545 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14546 glyph->object);
14547 if (!NILP (chprop))
14549 /* If the string came from a `display' text property,
14550 look up the buffer position of that property and
14551 use that position to update bpos_max, as if we
14552 actually saw such a position in one of the row's
14553 glyphs. This helps with supporting integer values
14554 of `cursor' property on the display string in
14555 situations where most or all of the row's buffer
14556 text is completely covered by display properties,
14557 so that no glyph with valid buffer positions is
14558 ever seen in the row. */
14559 ptrdiff_t prop_pos =
14560 string_buffer_position_lim (glyph->object, pos_before,
14561 pos_after, 0);
14563 if (prop_pos >= pos_before)
14564 bpos_max = prop_pos;
14566 if (INTEGERP (chprop))
14568 bpos_covered = bpos_max + XINT (chprop);
14569 /* If the `cursor' property covers buffer positions up
14570 to and including point, we should display cursor on
14571 this glyph. Note that, if a `cursor' property on one
14572 of the string's characters has an integer value, we
14573 will break out of the loop below _before_ we get to
14574 the position match above. IOW, integer values of
14575 the `cursor' property override the "exact match for
14576 point" strategy of positioning the cursor. */
14577 /* Implementation note: bpos_max == pt_old when, e.g.,
14578 we are in an empty line, where bpos_max is set to
14579 MATRIX_ROW_START_CHARPOS, see above. */
14580 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14582 cursor = glyph;
14583 break;
14587 string_seen = 1;
14589 x += glyph->pixel_width;
14590 ++glyph;
14592 else if (glyph > end) /* row is reversed */
14593 while (!INTEGERP (glyph->object))
14595 if (BUFFERP (glyph->object))
14597 ptrdiff_t dpos = glyph->charpos - pt_old;
14599 if (glyph->charpos > bpos_max)
14600 bpos_max = glyph->charpos;
14601 if (glyph->charpos < bpos_min)
14602 bpos_min = glyph->charpos;
14603 if (!glyph->avoid_cursor_p)
14605 if (dpos == 0)
14607 match_with_avoid_cursor = 0;
14608 break;
14610 if (0 > dpos && dpos > pos_before - pt_old)
14612 pos_before = glyph->charpos;
14613 glyph_before = glyph;
14615 else if (0 < dpos && dpos < pos_after - pt_old)
14617 pos_after = glyph->charpos;
14618 glyph_after = glyph;
14621 else if (dpos == 0)
14622 match_with_avoid_cursor = 1;
14624 else if (STRINGP (glyph->object))
14626 Lisp_Object chprop;
14627 ptrdiff_t glyph_pos = glyph->charpos;
14629 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14630 glyph->object);
14631 if (!NILP (chprop))
14633 ptrdiff_t prop_pos =
14634 string_buffer_position_lim (glyph->object, pos_before,
14635 pos_after, 0);
14637 if (prop_pos >= pos_before)
14638 bpos_max = prop_pos;
14640 if (INTEGERP (chprop))
14642 bpos_covered = bpos_max + XINT (chprop);
14643 /* If the `cursor' property covers buffer positions up
14644 to and including point, we should display cursor on
14645 this glyph. */
14646 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14648 cursor = glyph;
14649 break;
14652 string_seen = 1;
14654 --glyph;
14655 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14657 x--; /* can't use any pixel_width */
14658 break;
14660 x -= glyph->pixel_width;
14663 /* Step 2: If we didn't find an exact match for point, we need to
14664 look for a proper place to put the cursor among glyphs between
14665 GLYPH_BEFORE and GLYPH_AFTER. */
14666 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14667 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14668 && !(bpos_max <= pt_old && pt_old <= bpos_covered))
14670 /* An empty line has a single glyph whose OBJECT is zero and
14671 whose CHARPOS is the position of a newline on that line.
14672 Note that on a TTY, there are more glyphs after that, which
14673 were produced by extend_face_to_end_of_line, but their
14674 CHARPOS is zero or negative. */
14675 int empty_line_p =
14676 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14677 && INTEGERP (glyph->object) && glyph->charpos > 0
14678 /* On a TTY, continued and truncated rows also have a glyph at
14679 their end whose OBJECT is zero and whose CHARPOS is
14680 positive (the continuation and truncation glyphs), but such
14681 rows are obviously not "empty". */
14682 && !(row->continued_p || row->truncated_on_right_p);
14684 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14686 ptrdiff_t ellipsis_pos;
14688 /* Scan back over the ellipsis glyphs. */
14689 if (!row->reversed_p)
14691 ellipsis_pos = (glyph - 1)->charpos;
14692 while (glyph > row->glyphs[TEXT_AREA]
14693 && (glyph - 1)->charpos == ellipsis_pos)
14694 glyph--, x -= glyph->pixel_width;
14695 /* That loop always goes one position too far, including
14696 the glyph before the ellipsis. So scan forward over
14697 that one. */
14698 x += glyph->pixel_width;
14699 glyph++;
14701 else /* row is reversed */
14703 ellipsis_pos = (glyph + 1)->charpos;
14704 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14705 && (glyph + 1)->charpos == ellipsis_pos)
14706 glyph++, x += glyph->pixel_width;
14707 x -= glyph->pixel_width;
14708 glyph--;
14711 else if (match_with_avoid_cursor)
14713 cursor = glyph_after;
14714 x = -1;
14716 else if (string_seen)
14718 int incr = row->reversed_p ? -1 : +1;
14720 /* Need to find the glyph that came out of a string which is
14721 present at point. That glyph is somewhere between
14722 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14723 positioned between POS_BEFORE and POS_AFTER in the
14724 buffer. */
14725 struct glyph *start, *stop;
14726 ptrdiff_t pos = pos_before;
14728 x = -1;
14730 /* If the row ends in a newline from a display string,
14731 reordering could have moved the glyphs belonging to the
14732 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14733 in this case we extend the search to the last glyph in
14734 the row that was not inserted by redisplay. */
14735 if (row->ends_in_newline_from_string_p)
14737 glyph_after = end;
14738 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14741 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14742 correspond to POS_BEFORE and POS_AFTER, respectively. We
14743 need START and STOP in the order that corresponds to the
14744 row's direction as given by its reversed_p flag. If the
14745 directionality of characters between POS_BEFORE and
14746 POS_AFTER is the opposite of the row's base direction,
14747 these characters will have been reordered for display,
14748 and we need to reverse START and STOP. */
14749 if (!row->reversed_p)
14751 start = min (glyph_before, glyph_after);
14752 stop = max (glyph_before, glyph_after);
14754 else
14756 start = max (glyph_before, glyph_after);
14757 stop = min (glyph_before, glyph_after);
14759 for (glyph = start + incr;
14760 row->reversed_p ? glyph > stop : glyph < stop; )
14763 /* Any glyphs that come from the buffer are here because
14764 of bidi reordering. Skip them, and only pay
14765 attention to glyphs that came from some string. */
14766 if (STRINGP (glyph->object))
14768 Lisp_Object str;
14769 ptrdiff_t tem;
14770 /* If the display property covers the newline, we
14771 need to search for it one position farther. */
14772 ptrdiff_t lim = pos_after
14773 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14775 string_from_text_prop = 0;
14776 str = glyph->object;
14777 tem = string_buffer_position_lim (str, pos, lim, 0);
14778 if (tem == 0 /* from overlay */
14779 || pos <= tem)
14781 /* If the string from which this glyph came is
14782 found in the buffer at point, or at position
14783 that is closer to point than pos_after, then
14784 we've found the glyph we've been looking for.
14785 If it comes from an overlay (tem == 0), and
14786 it has the `cursor' property on one of its
14787 glyphs, record that glyph as a candidate for
14788 displaying the cursor. (As in the
14789 unidirectional version, we will display the
14790 cursor on the last candidate we find.) */
14791 if (tem == 0
14792 || tem == pt_old
14793 || (tem - pt_old > 0 && tem < pos_after))
14795 /* The glyphs from this string could have
14796 been reordered. Find the one with the
14797 smallest string position. Or there could
14798 be a character in the string with the
14799 `cursor' property, which means display
14800 cursor on that character's glyph. */
14801 ptrdiff_t strpos = glyph->charpos;
14803 if (tem)
14805 cursor = glyph;
14806 string_from_text_prop = 1;
14808 for ( ;
14809 (row->reversed_p ? glyph > stop : glyph < stop)
14810 && EQ (glyph->object, str);
14811 glyph += incr)
14813 Lisp_Object cprop;
14814 ptrdiff_t gpos = glyph->charpos;
14816 cprop = Fget_char_property (make_number (gpos),
14817 Qcursor,
14818 glyph->object);
14819 if (!NILP (cprop))
14821 cursor = glyph;
14822 break;
14824 if (tem && glyph->charpos < strpos)
14826 strpos = glyph->charpos;
14827 cursor = glyph;
14831 if (tem == pt_old
14832 || (tem - pt_old > 0 && tem < pos_after))
14833 goto compute_x;
14835 if (tem)
14836 pos = tem + 1; /* don't find previous instances */
14838 /* This string is not what we want; skip all of the
14839 glyphs that came from it. */
14840 while ((row->reversed_p ? glyph > stop : glyph < stop)
14841 && EQ (glyph->object, str))
14842 glyph += incr;
14844 else
14845 glyph += incr;
14848 /* If we reached the end of the line, and END was from a string,
14849 the cursor is not on this line. */
14850 if (cursor == NULL
14851 && (row->reversed_p ? glyph <= end : glyph >= end)
14852 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14853 && STRINGP (end->object)
14854 && row->continued_p)
14855 return 0;
14857 /* A truncated row may not include PT among its character positions.
14858 Setting the cursor inside the scroll margin will trigger
14859 recalculation of hscroll in hscroll_window_tree. But if a
14860 display string covers point, defer to the string-handling
14861 code below to figure this out. */
14862 else if (row->truncated_on_left_p && pt_old < bpos_min)
14864 cursor = glyph_before;
14865 x = -1;
14867 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14868 /* Zero-width characters produce no glyphs. */
14869 || (!empty_line_p
14870 && (row->reversed_p
14871 ? glyph_after > glyphs_end
14872 : glyph_after < glyphs_end)))
14874 cursor = glyph_after;
14875 x = -1;
14879 compute_x:
14880 if (cursor != NULL)
14881 glyph = cursor;
14882 else if (glyph == glyphs_end
14883 && pos_before == pos_after
14884 && STRINGP ((row->reversed_p
14885 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14886 : row->glyphs[TEXT_AREA])->object))
14888 /* If all the glyphs of this row came from strings, put the
14889 cursor on the first glyph of the row. This avoids having the
14890 cursor outside of the text area in this very rare and hard
14891 use case. */
14892 glyph =
14893 row->reversed_p
14894 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14895 : row->glyphs[TEXT_AREA];
14897 if (x < 0)
14899 struct glyph *g;
14901 /* Need to compute x that corresponds to GLYPH. */
14902 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14904 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14905 emacs_abort ();
14906 x += g->pixel_width;
14910 /* ROW could be part of a continued line, which, under bidi
14911 reordering, might have other rows whose start and end charpos
14912 occlude point. Only set w->cursor if we found a better
14913 approximation to the cursor position than we have from previously
14914 examined candidate rows belonging to the same continued line. */
14915 if (/* We already have a candidate row. */
14916 w->cursor.vpos >= 0
14917 /* That candidate is not the row we are processing. */
14918 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14919 /* Make sure cursor.vpos specifies a row whose start and end
14920 charpos occlude point, and it is valid candidate for being a
14921 cursor-row. This is because some callers of this function
14922 leave cursor.vpos at the row where the cursor was displayed
14923 during the last redisplay cycle. */
14924 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14925 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14926 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14928 struct glyph *g1
14929 = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14931 /* Don't consider glyphs that are outside TEXT_AREA. */
14932 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14933 return 0;
14934 /* Keep the candidate whose buffer position is the closest to
14935 point or has the `cursor' property. */
14936 if (/* Previous candidate is a glyph in TEXT_AREA of that row. */
14937 w->cursor.hpos >= 0
14938 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14939 && ((BUFFERP (g1->object)
14940 && (g1->charpos == pt_old /* An exact match always wins. */
14941 || (BUFFERP (glyph->object)
14942 && eabs (g1->charpos - pt_old)
14943 < eabs (glyph->charpos - pt_old))))
14944 /* Previous candidate is a glyph from a string that has
14945 a non-nil `cursor' property. */
14946 || (STRINGP (g1->object)
14947 && (!NILP (Fget_char_property (make_number (g1->charpos),
14948 Qcursor, g1->object))
14949 /* Previous candidate is from the same display
14950 string as this one, and the display string
14951 came from a text property. */
14952 || (EQ (g1->object, glyph->object)
14953 && string_from_text_prop)
14954 /* this candidate is from newline and its
14955 position is not an exact match */
14956 || (INTEGERP (glyph->object)
14957 && glyph->charpos != pt_old)))))
14958 return 0;
14959 /* If this candidate gives an exact match, use that. */
14960 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14961 /* If this candidate is a glyph created for the
14962 terminating newline of a line, and point is on that
14963 newline, it wins because it's an exact match. */
14964 || (!row->continued_p
14965 && INTEGERP (glyph->object)
14966 && glyph->charpos == 0
14967 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14968 /* Otherwise, keep the candidate that comes from a row
14969 spanning less buffer positions. This may win when one or
14970 both candidate positions are on glyphs that came from
14971 display strings, for which we cannot compare buffer
14972 positions. */
14973 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14974 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14975 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14976 return 0;
14978 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14979 w->cursor.x = x;
14980 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14981 w->cursor.y = row->y + dy;
14983 if (w == XWINDOW (selected_window))
14985 if (!row->continued_p
14986 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14987 && row->x == 0)
14989 this_line_buffer = XBUFFER (w->contents);
14991 CHARPOS (this_line_start_pos)
14992 = MATRIX_ROW_START_CHARPOS (row) + delta;
14993 BYTEPOS (this_line_start_pos)
14994 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14996 CHARPOS (this_line_end_pos)
14997 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14998 BYTEPOS (this_line_end_pos)
14999 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
15001 this_line_y = w->cursor.y;
15002 this_line_pixel_height = row->height;
15003 this_line_vpos = w->cursor.vpos;
15004 this_line_start_x = row->x;
15006 else
15007 CHARPOS (this_line_start_pos) = 0;
15010 return 1;
15014 /* Run window scroll functions, if any, for WINDOW with new window
15015 start STARTP. Sets the window start of WINDOW to that position.
15017 We assume that the window's buffer is really current. */
15019 static struct text_pos
15020 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
15022 struct window *w = XWINDOW (window);
15023 SET_MARKER_FROM_TEXT_POS (w->start, startp);
15025 eassert (current_buffer == XBUFFER (w->contents));
15027 if (!NILP (Vwindow_scroll_functions))
15029 run_hook_with_args_2 (Qwindow_scroll_functions, window,
15030 make_number (CHARPOS (startp)));
15031 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15032 /* In case the hook functions switch buffers. */
15033 set_buffer_internal (XBUFFER (w->contents));
15036 return startp;
15040 /* Make sure the line containing the cursor is fully visible.
15041 A value of 1 means there is nothing to be done.
15042 (Either the line is fully visible, or it cannot be made so,
15043 or we cannot tell.)
15045 If FORCE_P is non-zero, return 0 even if partial visible cursor row
15046 is higher than window.
15048 If CURRENT_MATRIX_P is non-zero, use the information from the
15049 window's current glyph matrix; otherwise use the desired glyph
15050 matrix.
15052 A value of 0 means the caller should do scrolling
15053 as if point had gone off the screen. */
15055 static int
15056 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
15058 struct glyph_matrix *matrix;
15059 struct glyph_row *row;
15060 int window_height;
15062 if (!make_cursor_line_fully_visible_p)
15063 return 1;
15065 /* It's not always possible to find the cursor, e.g, when a window
15066 is full of overlay strings. Don't do anything in that case. */
15067 if (w->cursor.vpos < 0)
15068 return 1;
15070 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
15071 row = MATRIX_ROW (matrix, w->cursor.vpos);
15073 /* If the cursor row is not partially visible, there's nothing to do. */
15074 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
15075 return 1;
15077 /* If the row the cursor is in is taller than the window's height,
15078 it's not clear what to do, so do nothing. */
15079 window_height = window_box_height (w);
15080 if (row->height >= window_height)
15082 if (!force_p || MINI_WINDOW_P (w)
15083 || w->vscroll || w->cursor.vpos == 0)
15084 return 1;
15086 return 0;
15090 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
15091 non-zero means only WINDOW is redisplayed in redisplay_internal.
15092 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
15093 in redisplay_window to bring a partially visible line into view in
15094 the case that only the cursor has moved.
15096 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
15097 last screen line's vertical height extends past the end of the screen.
15099 Value is
15101 1 if scrolling succeeded
15103 0 if scrolling didn't find point.
15105 -1 if new fonts have been loaded so that we must interrupt
15106 redisplay, adjust glyph matrices, and try again. */
15108 enum
15110 SCROLLING_SUCCESS,
15111 SCROLLING_FAILED,
15112 SCROLLING_NEED_LARGER_MATRICES
15115 /* If scroll-conservatively is more than this, never recenter.
15117 If you change this, don't forget to update the doc string of
15118 `scroll-conservatively' and the Emacs manual. */
15119 #define SCROLL_LIMIT 100
15121 static int
15122 try_scrolling (Lisp_Object window, int just_this_one_p,
15123 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
15124 int temp_scroll_step, int last_line_misfit)
15126 struct window *w = XWINDOW (window);
15127 struct frame *f = XFRAME (w->frame);
15128 struct text_pos pos, startp;
15129 struct it it;
15130 int this_scroll_margin, scroll_max, rc, height;
15131 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
15132 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
15133 Lisp_Object aggressive;
15134 /* We will never try scrolling more than this number of lines. */
15135 int scroll_limit = SCROLL_LIMIT;
15136 int frame_line_height = default_line_pixel_height (w);
15137 int window_total_lines
15138 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15140 #ifdef GLYPH_DEBUG
15141 debug_method_add (w, "try_scrolling");
15142 #endif
15144 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15146 /* Compute scroll margin height in pixels. We scroll when point is
15147 within this distance from the top or bottom of the window. */
15148 if (scroll_margin > 0)
15149 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
15150 * frame_line_height;
15151 else
15152 this_scroll_margin = 0;
15154 /* Force arg_scroll_conservatively to have a reasonable value, to
15155 avoid scrolling too far away with slow move_it_* functions. Note
15156 that the user can supply scroll-conservatively equal to
15157 `most-positive-fixnum', which can be larger than INT_MAX. */
15158 if (arg_scroll_conservatively > scroll_limit)
15160 arg_scroll_conservatively = scroll_limit + 1;
15161 scroll_max = scroll_limit * frame_line_height;
15163 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
15164 /* Compute how much we should try to scroll maximally to bring
15165 point into view. */
15166 scroll_max = (max (scroll_step,
15167 max (arg_scroll_conservatively, temp_scroll_step))
15168 * frame_line_height);
15169 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
15170 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
15171 /* We're trying to scroll because of aggressive scrolling but no
15172 scroll_step is set. Choose an arbitrary one. */
15173 scroll_max = 10 * frame_line_height;
15174 else
15175 scroll_max = 0;
15177 too_near_end:
15179 /* Decide whether to scroll down. */
15180 if (PT > CHARPOS (startp))
15182 int scroll_margin_y;
15184 /* Compute the pixel ypos of the scroll margin, then move IT to
15185 either that ypos or PT, whichever comes first. */
15186 start_display (&it, w, startp);
15187 scroll_margin_y = it.last_visible_y - this_scroll_margin
15188 - frame_line_height * extra_scroll_margin_lines;
15189 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
15190 (MOVE_TO_POS | MOVE_TO_Y));
15192 if (PT > CHARPOS (it.current.pos))
15194 int y0 = line_bottom_y (&it);
15195 /* Compute how many pixels below window bottom to stop searching
15196 for PT. This avoids costly search for PT that is far away if
15197 the user limited scrolling by a small number of lines, but
15198 always finds PT if scroll_conservatively is set to a large
15199 number, such as most-positive-fixnum. */
15200 int slack = max (scroll_max, 10 * frame_line_height);
15201 int y_to_move = it.last_visible_y + slack;
15203 /* Compute the distance from the scroll margin to PT or to
15204 the scroll limit, whichever comes first. This should
15205 include the height of the cursor line, to make that line
15206 fully visible. */
15207 move_it_to (&it, PT, -1, y_to_move,
15208 -1, MOVE_TO_POS | MOVE_TO_Y);
15209 dy = line_bottom_y (&it) - y0;
15211 if (dy > scroll_max)
15212 return SCROLLING_FAILED;
15214 if (dy > 0)
15215 scroll_down_p = 1;
15219 if (scroll_down_p)
15221 /* Point is in or below the bottom scroll margin, so move the
15222 window start down. If scrolling conservatively, move it just
15223 enough down to make point visible. If scroll_step is set,
15224 move it down by scroll_step. */
15225 if (arg_scroll_conservatively)
15226 amount_to_scroll
15227 = min (max (dy, frame_line_height),
15228 frame_line_height * arg_scroll_conservatively);
15229 else if (scroll_step || temp_scroll_step)
15230 amount_to_scroll = scroll_max;
15231 else
15233 aggressive = BVAR (current_buffer, scroll_up_aggressively);
15234 height = WINDOW_BOX_TEXT_HEIGHT (w);
15235 if (NUMBERP (aggressive))
15237 double float_amount = XFLOATINT (aggressive) * height;
15238 int aggressive_scroll = float_amount;
15239 if (aggressive_scroll == 0 && float_amount > 0)
15240 aggressive_scroll = 1;
15241 /* Don't let point enter the scroll margin near top of
15242 the window. This could happen if the value of
15243 scroll_up_aggressively is too large and there are
15244 non-zero margins, because scroll_up_aggressively
15245 means put point that fraction of window height
15246 _from_the_bottom_margin_. */
15247 if (aggressive_scroll + 2 * this_scroll_margin > height)
15248 aggressive_scroll = height - 2 * this_scroll_margin;
15249 amount_to_scroll = dy + aggressive_scroll;
15253 if (amount_to_scroll <= 0)
15254 return SCROLLING_FAILED;
15256 start_display (&it, w, startp);
15257 if (arg_scroll_conservatively <= scroll_limit)
15258 move_it_vertically (&it, amount_to_scroll);
15259 else
15261 /* Extra precision for users who set scroll-conservatively
15262 to a large number: make sure the amount we scroll
15263 the window start is never less than amount_to_scroll,
15264 which was computed as distance from window bottom to
15265 point. This matters when lines at window top and lines
15266 below window bottom have different height. */
15267 struct it it1;
15268 void *it1data = NULL;
15269 /* We use a temporary it1 because line_bottom_y can modify
15270 its argument, if it moves one line down; see there. */
15271 int start_y;
15273 SAVE_IT (it1, it, it1data);
15274 start_y = line_bottom_y (&it1);
15275 do {
15276 RESTORE_IT (&it, &it, it1data);
15277 move_it_by_lines (&it, 1);
15278 SAVE_IT (it1, it, it1data);
15279 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
15282 /* If STARTP is unchanged, move it down another screen line. */
15283 if (CHARPOS (it.current.pos) == CHARPOS (startp))
15284 move_it_by_lines (&it, 1);
15285 startp = it.current.pos;
15287 else
15289 struct text_pos scroll_margin_pos = startp;
15290 int y_offset = 0;
15292 /* See if point is inside the scroll margin at the top of the
15293 window. */
15294 if (this_scroll_margin)
15296 int y_start;
15298 start_display (&it, w, startp);
15299 y_start = it.current_y;
15300 move_it_vertically (&it, this_scroll_margin);
15301 scroll_margin_pos = it.current.pos;
15302 /* If we didn't move enough before hitting ZV, request
15303 additional amount of scroll, to move point out of the
15304 scroll margin. */
15305 if (IT_CHARPOS (it) == ZV
15306 && it.current_y - y_start < this_scroll_margin)
15307 y_offset = this_scroll_margin - (it.current_y - y_start);
15310 if (PT < CHARPOS (scroll_margin_pos))
15312 /* Point is in the scroll margin at the top of the window or
15313 above what is displayed in the window. */
15314 int y0, y_to_move;
15316 /* Compute the vertical distance from PT to the scroll
15317 margin position. Move as far as scroll_max allows, or
15318 one screenful, or 10 screen lines, whichever is largest.
15319 Give up if distance is greater than scroll_max or if we
15320 didn't reach the scroll margin position. */
15321 SET_TEXT_POS (pos, PT, PT_BYTE);
15322 start_display (&it, w, pos);
15323 y0 = it.current_y;
15324 y_to_move = max (it.last_visible_y,
15325 max (scroll_max, 10 * frame_line_height));
15326 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
15327 y_to_move, -1,
15328 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15329 dy = it.current_y - y0;
15330 if (dy > scroll_max
15331 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
15332 return SCROLLING_FAILED;
15334 /* Additional scroll for when ZV was too close to point. */
15335 dy += y_offset;
15337 /* Compute new window start. */
15338 start_display (&it, w, startp);
15340 if (arg_scroll_conservatively)
15341 amount_to_scroll = max (dy, frame_line_height
15342 * max (scroll_step, temp_scroll_step));
15343 else if (scroll_step || temp_scroll_step)
15344 amount_to_scroll = scroll_max;
15345 else
15347 aggressive = BVAR (current_buffer, scroll_down_aggressively);
15348 height = WINDOW_BOX_TEXT_HEIGHT (w);
15349 if (NUMBERP (aggressive))
15351 double float_amount = XFLOATINT (aggressive) * height;
15352 int aggressive_scroll = float_amount;
15353 if (aggressive_scroll == 0 && float_amount > 0)
15354 aggressive_scroll = 1;
15355 /* Don't let point enter the scroll margin near
15356 bottom of the window, if the value of
15357 scroll_down_aggressively happens to be too
15358 large. */
15359 if (aggressive_scroll + 2 * this_scroll_margin > height)
15360 aggressive_scroll = height - 2 * this_scroll_margin;
15361 amount_to_scroll = dy + aggressive_scroll;
15365 if (amount_to_scroll <= 0)
15366 return SCROLLING_FAILED;
15368 move_it_vertically_backward (&it, amount_to_scroll);
15369 startp = it.current.pos;
15373 /* Run window scroll functions. */
15374 startp = run_window_scroll_functions (window, startp);
15376 /* Display the window. Give up if new fonts are loaded, or if point
15377 doesn't appear. */
15378 if (!try_window (window, startp, 0))
15379 rc = SCROLLING_NEED_LARGER_MATRICES;
15380 else if (w->cursor.vpos < 0)
15382 clear_glyph_matrix (w->desired_matrix);
15383 rc = SCROLLING_FAILED;
15385 else
15387 /* Maybe forget recorded base line for line number display. */
15388 if (!just_this_one_p
15389 || current_buffer->clip_changed
15390 || BEG_UNCHANGED < CHARPOS (startp))
15391 w->base_line_number = 0;
15393 /* If cursor ends up on a partially visible line,
15394 treat that as being off the bottom of the screen. */
15395 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15396 /* It's possible that the cursor is on the first line of the
15397 buffer, which is partially obscured due to a vscroll
15398 (Bug#7537). In that case, avoid looping forever. */
15399 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15401 clear_glyph_matrix (w->desired_matrix);
15402 ++extra_scroll_margin_lines;
15403 goto too_near_end;
15405 rc = SCROLLING_SUCCESS;
15408 return rc;
15412 /* Compute a suitable window start for window W if display of W starts
15413 on a continuation line. Value is non-zero if a new window start
15414 was computed.
15416 The new window start will be computed, based on W's width, starting
15417 from the start of the continued line. It is the start of the
15418 screen line with the minimum distance from the old start W->start. */
15420 static int
15421 compute_window_start_on_continuation_line (struct window *w)
15423 struct text_pos pos, start_pos;
15424 int window_start_changed_p = 0;
15426 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15428 /* If window start is on a continuation line... Window start may be
15429 < BEGV in case there's invisible text at the start of the
15430 buffer (M-x rmail, for example). */
15431 if (CHARPOS (start_pos) > BEGV
15432 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15434 struct it it;
15435 struct glyph_row *row;
15437 /* Handle the case that the window start is out of range. */
15438 if (CHARPOS (start_pos) < BEGV)
15439 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15440 else if (CHARPOS (start_pos) > ZV)
15441 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15443 /* Find the start of the continued line. This should be fast
15444 because find_newline is fast (newline cache). */
15445 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15446 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15447 row, DEFAULT_FACE_ID);
15448 reseat_at_previous_visible_line_start (&it);
15450 /* If the line start is "too far" away from the window start,
15451 say it takes too much time to compute a new window start. */
15452 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15453 /* PXW: Do we need upper bounds here? */
15454 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15456 int min_distance, distance;
15458 /* Move forward by display lines to find the new window
15459 start. If window width was enlarged, the new start can
15460 be expected to be > the old start. If window width was
15461 decreased, the new window start will be < the old start.
15462 So, we're looking for the display line start with the
15463 minimum distance from the old window start. */
15464 pos = it.current.pos;
15465 min_distance = INFINITY;
15466 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15467 distance < min_distance)
15469 min_distance = distance;
15470 pos = it.current.pos;
15471 if (it.line_wrap == WORD_WRAP)
15473 /* Under WORD_WRAP, move_it_by_lines is likely to
15474 overshoot and stop not at the first, but the
15475 second character from the left margin. So in
15476 that case, we need a more tight control on the X
15477 coordinate of the iterator than move_it_by_lines
15478 promises in its contract. The method is to first
15479 go to the last (rightmost) visible character of a
15480 line, then move to the leftmost character on the
15481 next line in a separate call. */
15482 move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
15483 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15484 move_it_to (&it, ZV, 0,
15485 it.current_y + it.max_ascent + it.max_descent, -1,
15486 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15488 else
15489 move_it_by_lines (&it, 1);
15492 /* Set the window start there. */
15493 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15494 window_start_changed_p = 1;
15498 return window_start_changed_p;
15502 /* Try cursor movement in case text has not changed in window WINDOW,
15503 with window start STARTP. Value is
15505 CURSOR_MOVEMENT_SUCCESS if successful
15507 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15509 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15510 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15511 we want to scroll as if scroll-step were set to 1. See the code.
15513 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15514 which case we have to abort this redisplay, and adjust matrices
15515 first. */
15517 enum
15519 CURSOR_MOVEMENT_SUCCESS,
15520 CURSOR_MOVEMENT_CANNOT_BE_USED,
15521 CURSOR_MOVEMENT_MUST_SCROLL,
15522 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15525 static int
15526 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15528 struct window *w = XWINDOW (window);
15529 struct frame *f = XFRAME (w->frame);
15530 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15532 #ifdef GLYPH_DEBUG
15533 if (inhibit_try_cursor_movement)
15534 return rc;
15535 #endif
15537 /* Previously, there was a check for Lisp integer in the
15538 if-statement below. Now, this field is converted to
15539 ptrdiff_t, thus zero means invalid position in a buffer. */
15540 eassert (w->last_point > 0);
15541 /* Likewise there was a check whether window_end_vpos is nil or larger
15542 than the window. Now window_end_vpos is int and so never nil, but
15543 let's leave eassert to check whether it fits in the window. */
15544 eassert (w->window_end_vpos < w->current_matrix->nrows);
15546 /* Handle case where text has not changed, only point, and it has
15547 not moved off the frame. */
15548 if (/* Point may be in this window. */
15549 PT >= CHARPOS (startp)
15550 /* Selective display hasn't changed. */
15551 && !current_buffer->clip_changed
15552 /* Function force-mode-line-update is used to force a thorough
15553 redisplay. It sets either windows_or_buffers_changed or
15554 update_mode_lines. So don't take a shortcut here for these
15555 cases. */
15556 && !update_mode_lines
15557 && !windows_or_buffers_changed
15558 && !f->cursor_type_changed
15559 && NILP (Vshow_trailing_whitespace)
15560 /* This code is not used for mini-buffer for the sake of the case
15561 of redisplaying to replace an echo area message; since in
15562 that case the mini-buffer contents per se are usually
15563 unchanged. This code is of no real use in the mini-buffer
15564 since the handling of this_line_start_pos, etc., in redisplay
15565 handles the same cases. */
15566 && !EQ (window, minibuf_window)
15567 && (FRAME_WINDOW_P (f)
15568 || !overlay_arrow_in_current_buffer_p ()))
15570 int this_scroll_margin, top_scroll_margin;
15571 struct glyph_row *row = NULL;
15572 int frame_line_height = default_line_pixel_height (w);
15573 int window_total_lines
15574 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15576 #ifdef GLYPH_DEBUG
15577 debug_method_add (w, "cursor movement");
15578 #endif
15580 /* Scroll if point within this distance from the top or bottom
15581 of the window. This is a pixel value. */
15582 if (scroll_margin > 0)
15584 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15585 this_scroll_margin *= frame_line_height;
15587 else
15588 this_scroll_margin = 0;
15590 top_scroll_margin = this_scroll_margin;
15591 if (WINDOW_WANTS_HEADER_LINE_P (w))
15592 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15594 /* Start with the row the cursor was displayed during the last
15595 not paused redisplay. Give up if that row is not valid. */
15596 if (w->last_cursor_vpos < 0
15597 || w->last_cursor_vpos >= w->current_matrix->nrows)
15598 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15599 else
15601 row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
15602 if (row->mode_line_p)
15603 ++row;
15604 if (!row->enabled_p)
15605 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15608 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15610 int scroll_p = 0, must_scroll = 0;
15611 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15613 if (PT > w->last_point)
15615 /* Point has moved forward. */
15616 while (MATRIX_ROW_END_CHARPOS (row) < PT
15617 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15619 eassert (row->enabled_p);
15620 ++row;
15623 /* If the end position of a row equals the start
15624 position of the next row, and PT is at that position,
15625 we would rather display cursor in the next line. */
15626 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15627 && MATRIX_ROW_END_CHARPOS (row) == PT
15628 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15629 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15630 && !cursor_row_p (row))
15631 ++row;
15633 /* If within the scroll margin, scroll. Note that
15634 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15635 the next line would be drawn, and that
15636 this_scroll_margin can be zero. */
15637 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15638 || PT > MATRIX_ROW_END_CHARPOS (row)
15639 /* Line is completely visible last line in window
15640 and PT is to be set in the next line. */
15641 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15642 && PT == MATRIX_ROW_END_CHARPOS (row)
15643 && !row->ends_at_zv_p
15644 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15645 scroll_p = 1;
15647 else if (PT < w->last_point)
15649 /* Cursor has to be moved backward. Note that PT >=
15650 CHARPOS (startp) because of the outer if-statement. */
15651 while (!row->mode_line_p
15652 && (MATRIX_ROW_START_CHARPOS (row) > PT
15653 || (MATRIX_ROW_START_CHARPOS (row) == PT
15654 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15655 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15656 row > w->current_matrix->rows
15657 && (row-1)->ends_in_newline_from_string_p))))
15658 && (row->y > top_scroll_margin
15659 || CHARPOS (startp) == BEGV))
15661 eassert (row->enabled_p);
15662 --row;
15665 /* Consider the following case: Window starts at BEGV,
15666 there is invisible, intangible text at BEGV, so that
15667 display starts at some point START > BEGV. It can
15668 happen that we are called with PT somewhere between
15669 BEGV and START. Try to handle that case. */
15670 if (row < w->current_matrix->rows
15671 || row->mode_line_p)
15673 row = w->current_matrix->rows;
15674 if (row->mode_line_p)
15675 ++row;
15678 /* Due to newlines in overlay strings, we may have to
15679 skip forward over overlay strings. */
15680 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15681 && MATRIX_ROW_END_CHARPOS (row) == PT
15682 && !cursor_row_p (row))
15683 ++row;
15685 /* If within the scroll margin, scroll. */
15686 if (row->y < top_scroll_margin
15687 && CHARPOS (startp) != BEGV)
15688 scroll_p = 1;
15690 else
15692 /* Cursor did not move. So don't scroll even if cursor line
15693 is partially visible, as it was so before. */
15694 rc = CURSOR_MOVEMENT_SUCCESS;
15697 if (PT < MATRIX_ROW_START_CHARPOS (row)
15698 || PT > MATRIX_ROW_END_CHARPOS (row))
15700 /* if PT is not in the glyph row, give up. */
15701 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15702 must_scroll = 1;
15704 else if (rc != CURSOR_MOVEMENT_SUCCESS
15705 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15707 struct glyph_row *row1;
15709 /* If rows are bidi-reordered and point moved, back up
15710 until we find a row that does not belong to a
15711 continuation line. This is because we must consider
15712 all rows of a continued line as candidates for the
15713 new cursor positioning, since row start and end
15714 positions change non-linearly with vertical position
15715 in such rows. */
15716 /* FIXME: Revisit this when glyph ``spilling'' in
15717 continuation lines' rows is implemented for
15718 bidi-reordered rows. */
15719 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15720 MATRIX_ROW_CONTINUATION_LINE_P (row);
15721 --row)
15723 /* If we hit the beginning of the displayed portion
15724 without finding the first row of a continued
15725 line, give up. */
15726 if (row <= row1)
15728 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15729 break;
15731 eassert (row->enabled_p);
15734 if (must_scroll)
15736 else if (rc != CURSOR_MOVEMENT_SUCCESS
15737 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15738 /* Make sure this isn't a header line by any chance, since
15739 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15740 && !row->mode_line_p
15741 && make_cursor_line_fully_visible_p)
15743 if (PT == MATRIX_ROW_END_CHARPOS (row)
15744 && !row->ends_at_zv_p
15745 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15746 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15747 else if (row->height > window_box_height (w))
15749 /* If we end up in a partially visible line, let's
15750 make it fully visible, except when it's taller
15751 than the window, in which case we can't do much
15752 about it. */
15753 *scroll_step = 1;
15754 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15756 else
15758 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15759 if (!cursor_row_fully_visible_p (w, 0, 1))
15760 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15761 else
15762 rc = CURSOR_MOVEMENT_SUCCESS;
15765 else if (scroll_p)
15766 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15767 else if (rc != CURSOR_MOVEMENT_SUCCESS
15768 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15770 /* With bidi-reordered rows, there could be more than
15771 one candidate row whose start and end positions
15772 occlude point. We need to let set_cursor_from_row
15773 find the best candidate. */
15774 /* FIXME: Revisit this when glyph ``spilling'' in
15775 continuation lines' rows is implemented for
15776 bidi-reordered rows. */
15777 int rv = 0;
15781 int at_zv_p = 0, exact_match_p = 0;
15783 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15784 && PT <= MATRIX_ROW_END_CHARPOS (row)
15785 && cursor_row_p (row))
15786 rv |= set_cursor_from_row (w, row, w->current_matrix,
15787 0, 0, 0, 0);
15788 /* As soon as we've found the exact match for point,
15789 or the first suitable row whose ends_at_zv_p flag
15790 is set, we are done. */
15791 if (rv)
15793 at_zv_p = MATRIX_ROW (w->current_matrix,
15794 w->cursor.vpos)->ends_at_zv_p;
15795 if (!at_zv_p
15796 && w->cursor.hpos >= 0
15797 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15798 w->cursor.vpos))
15800 struct glyph_row *candidate =
15801 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15802 struct glyph *g =
15803 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15804 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15806 exact_match_p =
15807 (BUFFERP (g->object) && g->charpos == PT)
15808 || (INTEGERP (g->object)
15809 && (g->charpos == PT
15810 || (g->charpos == 0 && endpos - 1 == PT)));
15812 if (at_zv_p || exact_match_p)
15814 rc = CURSOR_MOVEMENT_SUCCESS;
15815 break;
15818 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15819 break;
15820 ++row;
15822 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15823 || row->continued_p)
15824 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15825 || (MATRIX_ROW_START_CHARPOS (row) == PT
15826 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15827 /* If we didn't find any candidate rows, or exited the
15828 loop before all the candidates were examined, signal
15829 to the caller that this method failed. */
15830 if (rc != CURSOR_MOVEMENT_SUCCESS
15831 && !(rv
15832 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15833 && !row->continued_p))
15834 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15835 else if (rv)
15836 rc = CURSOR_MOVEMENT_SUCCESS;
15838 else
15842 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15844 rc = CURSOR_MOVEMENT_SUCCESS;
15845 break;
15847 ++row;
15849 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15850 && MATRIX_ROW_START_CHARPOS (row) == PT
15851 && cursor_row_p (row));
15856 return rc;
15860 void
15861 set_vertical_scroll_bar (struct window *w)
15863 ptrdiff_t start, end, whole;
15865 /* Calculate the start and end positions for the current window.
15866 At some point, it would be nice to choose between scrollbars
15867 which reflect the whole buffer size, with special markers
15868 indicating narrowing, and scrollbars which reflect only the
15869 visible region.
15871 Note that mini-buffers sometimes aren't displaying any text. */
15872 if (!MINI_WINDOW_P (w)
15873 || (w == XWINDOW (minibuf_window)
15874 && NILP (echo_area_buffer[0])))
15876 struct buffer *buf = XBUFFER (w->contents);
15877 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15878 start = marker_position (w->start) - BUF_BEGV (buf);
15879 /* I don't think this is guaranteed to be right. For the
15880 moment, we'll pretend it is. */
15881 end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
15883 if (end < start)
15884 end = start;
15885 if (whole < (end - start))
15886 whole = end - start;
15888 else
15889 start = end = whole = 0;
15891 /* Indicate what this scroll bar ought to be displaying now. */
15892 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15893 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15894 (w, end - start, whole, start);
15898 void
15899 set_horizontal_scroll_bar (struct window *w)
15901 int start, end, whole, portion;
15903 if (!MINI_WINDOW_P (w)
15904 || (w == XWINDOW (minibuf_window)
15905 && NILP (echo_area_buffer[0])))
15907 struct buffer *b = XBUFFER (w->contents);
15908 struct buffer *old_buffer = NULL;
15909 struct it it;
15910 struct text_pos startp;
15912 if (b != current_buffer)
15914 old_buffer = current_buffer;
15915 set_buffer_internal (b);
15918 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15919 start_display (&it, w, startp);
15920 it.last_visible_x = INT_MAX;
15921 whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
15922 MOVE_TO_X | MOVE_TO_Y);
15923 /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
15924 window_box_height (w), -1,
15925 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
15927 start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
15928 end = start + window_box_width (w, TEXT_AREA);
15929 portion = end - start;
15930 /* After enlarging a horizontally scrolled window such that it
15931 gets at least as wide as the text it contains, make sure that
15932 the thumb doesn't fill the entire scroll bar so we can still
15933 drag it back to see the entire text. */
15934 whole = max (whole, end);
15936 if (it.bidi_p)
15938 Lisp_Object pdir;
15940 pdir = Fcurrent_bidi_paragraph_direction (Qnil);
15941 if (EQ (pdir, Qright_to_left))
15943 start = whole - end;
15944 end = start + portion;
15948 if (old_buffer)
15949 set_buffer_internal (old_buffer);
15951 else
15952 start = end = whole = portion = 0;
15954 w->hscroll_whole = whole;
15956 /* Indicate what this scroll bar ought to be displaying now. */
15957 if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15958 (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
15959 (w, portion, whole, start);
15963 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15964 selected_window is redisplayed.
15966 We can return without actually redisplaying the window if fonts has been
15967 changed on window's frame. In that case, redisplay_internal will retry.
15969 As one of the important parts of redisplaying a window, we need to
15970 decide whether the previous window-start position (stored in the
15971 window's w->start marker position) is still valid, and if it isn't,
15972 recompute it. Some details about that:
15974 . The previous window-start could be in a continuation line, in
15975 which case we need to recompute it when the window width
15976 changes. See compute_window_start_on_continuation_line and its
15977 call below.
15979 . The text that changed since last redisplay could include the
15980 previous window-start position. In that case, we try to salvage
15981 what we can from the current glyph matrix by calling
15982 try_scrolling, which see.
15984 . Some Emacs command could force us to use a specific window-start
15985 position by setting the window's force_start flag, or gently
15986 propose doing that by setting the window's optional_new_start
15987 flag. In these cases, we try using the specified start point if
15988 that succeeds (i.e. the window desired matrix is successfully
15989 recomputed, and point location is within the window). In case
15990 of optional_new_start, we first check if the specified start
15991 position is feasible, i.e. if it will allow point to be
15992 displayed in the window. If using the specified start point
15993 fails, e.g., if new fonts are needed to be loaded, we abort the
15994 redisplay cycle and leave it up to the next cycle to figure out
15995 things.
15997 . Note that the window's force_start flag is sometimes set by
15998 redisplay itself, when it decides that the previous window start
15999 point is fine and should be kept. Search for "goto force_start"
16000 below to see the details. Like the values of window-start
16001 specified outside of redisplay, these internally-deduced values
16002 are tested for feasibility, and ignored if found to be
16003 unfeasible.
16005 . Note that the function try_window, used to completely redisplay
16006 a window, accepts the window's start point as its argument.
16007 This is used several times in the redisplay code to control
16008 where the window start will be, according to user options such
16009 as scroll-conservatively, and also to ensure the screen line
16010 showing point will be fully (as opposed to partially) visible on
16011 display. */
16013 static void
16014 redisplay_window (Lisp_Object window, bool just_this_one_p)
16016 struct window *w = XWINDOW (window);
16017 struct frame *f = XFRAME (w->frame);
16018 struct buffer *buffer = XBUFFER (w->contents);
16019 struct buffer *old = current_buffer;
16020 struct text_pos lpoint, opoint, startp;
16021 int update_mode_line;
16022 int tem;
16023 struct it it;
16024 /* Record it now because it's overwritten. */
16025 bool current_matrix_up_to_date_p = false;
16026 bool used_current_matrix_p = false;
16027 /* This is less strict than current_matrix_up_to_date_p.
16028 It indicates that the buffer contents and narrowing are unchanged. */
16029 bool buffer_unchanged_p = false;
16030 int temp_scroll_step = 0;
16031 ptrdiff_t count = SPECPDL_INDEX ();
16032 int rc;
16033 int centering_position = -1;
16034 int last_line_misfit = 0;
16035 ptrdiff_t beg_unchanged, end_unchanged;
16036 int frame_line_height;
16038 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16039 opoint = lpoint;
16041 #ifdef GLYPH_DEBUG
16042 *w->desired_matrix->method = 0;
16043 #endif
16045 if (!just_this_one_p
16046 && REDISPLAY_SOME_P ()
16047 && !w->redisplay
16048 && !f->redisplay
16049 && !buffer->text->redisplay
16050 && BUF_PT (buffer) == w->last_point)
16051 return;
16053 /* Make sure that both W's markers are valid. */
16054 eassert (XMARKER (w->start)->buffer == buffer);
16055 eassert (XMARKER (w->pointm)->buffer == buffer);
16057 /* We come here again if we need to run window-text-change-functions
16058 below. */
16059 restart:
16060 reconsider_clip_changes (w);
16061 frame_line_height = default_line_pixel_height (w);
16063 /* Has the mode line to be updated? */
16064 update_mode_line = (w->update_mode_line
16065 || update_mode_lines
16066 || buffer->clip_changed
16067 || buffer->prevent_redisplay_optimizations_p);
16069 if (!just_this_one_p)
16070 /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
16071 cleverly elsewhere. */
16072 w->must_be_updated_p = true;
16074 if (MINI_WINDOW_P (w))
16076 if (w == XWINDOW (echo_area_window)
16077 && !NILP (echo_area_buffer[0]))
16079 if (update_mode_line)
16080 /* We may have to update a tty frame's menu bar or a
16081 tool-bar. Example `M-x C-h C-h C-g'. */
16082 goto finish_menu_bars;
16083 else
16084 /* We've already displayed the echo area glyphs in this window. */
16085 goto finish_scroll_bars;
16087 else if ((w != XWINDOW (minibuf_window)
16088 || minibuf_level == 0)
16089 /* When buffer is nonempty, redisplay window normally. */
16090 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
16091 /* Quail displays non-mini buffers in minibuffer window.
16092 In that case, redisplay the window normally. */
16093 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
16095 /* W is a mini-buffer window, but it's not active, so clear
16096 it. */
16097 int yb = window_text_bottom_y (w);
16098 struct glyph_row *row;
16099 int y;
16101 for (y = 0, row = w->desired_matrix->rows;
16102 y < yb;
16103 y += row->height, ++row)
16104 blank_row (w, row, y);
16105 goto finish_scroll_bars;
16108 clear_glyph_matrix (w->desired_matrix);
16111 /* Otherwise set up data on this window; select its buffer and point
16112 value. */
16113 /* Really select the buffer, for the sake of buffer-local
16114 variables. */
16115 set_buffer_internal_1 (XBUFFER (w->contents));
16117 current_matrix_up_to_date_p
16118 = (w->window_end_valid
16119 && !current_buffer->clip_changed
16120 && !current_buffer->prevent_redisplay_optimizations_p
16121 && !window_outdated (w));
16123 /* Run the window-text-change-functions
16124 if it is possible that the text on the screen has changed
16125 (either due to modification of the text, or any other reason). */
16126 if (!current_matrix_up_to_date_p
16127 && !NILP (Vwindow_text_change_functions))
16129 safe_run_hooks (Qwindow_text_change_functions);
16130 goto restart;
16133 beg_unchanged = BEG_UNCHANGED;
16134 end_unchanged = END_UNCHANGED;
16136 SET_TEXT_POS (opoint, PT, PT_BYTE);
16138 specbind (Qinhibit_point_motion_hooks, Qt);
16140 buffer_unchanged_p
16141 = (w->window_end_valid
16142 && !current_buffer->clip_changed
16143 && !window_outdated (w));
16145 /* When windows_or_buffers_changed is non-zero, we can't rely
16146 on the window end being valid, so set it to zero there. */
16147 if (windows_or_buffers_changed)
16149 /* If window starts on a continuation line, maybe adjust the
16150 window start in case the window's width changed. */
16151 if (XMARKER (w->start)->buffer == current_buffer)
16152 compute_window_start_on_continuation_line (w);
16154 w->window_end_valid = false;
16155 /* If so, we also can't rely on current matrix
16156 and should not fool try_cursor_movement below. */
16157 current_matrix_up_to_date_p = false;
16160 /* Some sanity checks. */
16161 CHECK_WINDOW_END (w);
16162 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
16163 emacs_abort ();
16164 if (BYTEPOS (opoint) < CHARPOS (opoint))
16165 emacs_abort ();
16167 if (mode_line_update_needed (w))
16168 update_mode_line = 1;
16170 /* Point refers normally to the selected window. For any other
16171 window, set up appropriate value. */
16172 if (!EQ (window, selected_window))
16174 ptrdiff_t new_pt = marker_position (w->pointm);
16175 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
16177 if (new_pt < BEGV)
16179 new_pt = BEGV;
16180 new_pt_byte = BEGV_BYTE;
16181 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
16183 else if (new_pt > (ZV - 1))
16185 new_pt = ZV;
16186 new_pt_byte = ZV_BYTE;
16187 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
16190 /* We don't use SET_PT so that the point-motion hooks don't run. */
16191 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
16194 /* If any of the character widths specified in the display table
16195 have changed, invalidate the width run cache. It's true that
16196 this may be a bit late to catch such changes, but the rest of
16197 redisplay goes (non-fatally) haywire when the display table is
16198 changed, so why should we worry about doing any better? */
16199 if (current_buffer->width_run_cache
16200 || (current_buffer->base_buffer
16201 && current_buffer->base_buffer->width_run_cache))
16203 struct Lisp_Char_Table *disptab = buffer_display_table ();
16205 if (! disptab_matches_widthtab
16206 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
16208 struct buffer *buf = current_buffer;
16210 if (buf->base_buffer)
16211 buf = buf->base_buffer;
16212 invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
16213 recompute_width_table (current_buffer, disptab);
16217 /* If window-start is screwed up, choose a new one. */
16218 if (XMARKER (w->start)->buffer != current_buffer)
16219 goto recenter;
16221 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16223 /* If someone specified a new starting point but did not insist,
16224 check whether it can be used. */
16225 if ((w->optional_new_start || window_frozen_p (w))
16226 && CHARPOS (startp) >= BEGV
16227 && CHARPOS (startp) <= ZV)
16229 ptrdiff_t it_charpos;
16231 w->optional_new_start = 0;
16232 start_display (&it, w, startp);
16233 move_it_to (&it, PT, 0, it.last_visible_y, -1,
16234 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
16235 /* Record IT's position now, since line_bottom_y might change
16236 that. */
16237 it_charpos = IT_CHARPOS (it);
16238 /* Make sure we set the force_start flag only if the cursor row
16239 will be fully visible. Otherwise, the code under force_start
16240 label below will try to move point back into view, which is
16241 not what the code which sets optional_new_start wants. */
16242 if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
16243 && !w->force_start)
16245 if (it_charpos == PT)
16246 w->force_start = 1;
16247 /* IT may overshoot PT if text at PT is invisible. */
16248 else if (it_charpos > PT && CHARPOS (startp) <= PT)
16249 w->force_start = 1;
16250 #ifdef GLYPH_DEBUG
16251 if (w->force_start)
16253 if (window_frozen_p (w))
16254 debug_method_add (w, "set force_start from frozen window start");
16255 else
16256 debug_method_add (w, "set force_start from optional_new_start");
16258 #endif
16262 force_start:
16264 /* Handle case where place to start displaying has been specified,
16265 unless the specified location is outside the accessible range. */
16266 if (w->force_start)
16268 /* We set this later on if we have to adjust point. */
16269 int new_vpos = -1;
16271 w->force_start = 0;
16272 w->vscroll = 0;
16273 w->window_end_valid = 0;
16275 /* Forget any recorded base line for line number display. */
16276 if (!buffer_unchanged_p)
16277 w->base_line_number = 0;
16279 /* Redisplay the mode line. Select the buffer properly for that.
16280 Also, run the hook window-scroll-functions
16281 because we have scrolled. */
16282 /* Note, we do this after clearing force_start because
16283 if there's an error, it is better to forget about force_start
16284 than to get into an infinite loop calling the hook functions
16285 and having them get more errors. */
16286 if (!update_mode_line
16287 || ! NILP (Vwindow_scroll_functions))
16289 update_mode_line = 1;
16290 w->update_mode_line = 1;
16291 startp = run_window_scroll_functions (window, startp);
16294 if (CHARPOS (startp) < BEGV)
16295 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
16296 else if (CHARPOS (startp) > ZV)
16297 SET_TEXT_POS (startp, ZV, ZV_BYTE);
16299 /* Redisplay, then check if cursor has been set during the
16300 redisplay. Give up if new fonts were loaded. */
16301 /* We used to issue a CHECK_MARGINS argument to try_window here,
16302 but this causes scrolling to fail when point begins inside
16303 the scroll margin (bug#148) -- cyd */
16304 if (!try_window (window, startp, 0))
16306 w->force_start = 1;
16307 clear_glyph_matrix (w->desired_matrix);
16308 goto need_larger_matrices;
16311 if (w->cursor.vpos < 0)
16313 /* If point does not appear, try to move point so it does
16314 appear. The desired matrix has been built above, so we
16315 can use it here. */
16316 new_vpos = window_box_height (w) / 2;
16319 if (!cursor_row_fully_visible_p (w, 0, 0))
16321 /* Point does appear, but on a line partly visible at end of window.
16322 Move it back to a fully-visible line. */
16323 new_vpos = window_box_height (w);
16324 /* But if window_box_height suggests a Y coordinate that is
16325 not less than we already have, that line will clearly not
16326 be fully visible, so give up and scroll the display.
16327 This can happen when the default face uses a font whose
16328 dimensions are different from the frame's default
16329 font. */
16330 if (new_vpos >= w->cursor.y)
16332 w->cursor.vpos = -1;
16333 clear_glyph_matrix (w->desired_matrix);
16334 goto try_to_scroll;
16337 else if (w->cursor.vpos >= 0)
16339 /* Some people insist on not letting point enter the scroll
16340 margin, even though this part handles windows that didn't
16341 scroll at all. */
16342 int window_total_lines
16343 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16344 int margin = min (scroll_margin, window_total_lines / 4);
16345 int pixel_margin = margin * frame_line_height;
16346 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
16348 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
16349 below, which finds the row to move point to, advances by
16350 the Y coordinate of the _next_ row, see the definition of
16351 MATRIX_ROW_BOTTOM_Y. */
16352 if (w->cursor.vpos < margin + header_line)
16354 w->cursor.vpos = -1;
16355 clear_glyph_matrix (w->desired_matrix);
16356 goto try_to_scroll;
16358 else
16360 int window_height = window_box_height (w);
16362 if (header_line)
16363 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
16364 if (w->cursor.y >= window_height - pixel_margin)
16366 w->cursor.vpos = -1;
16367 clear_glyph_matrix (w->desired_matrix);
16368 goto try_to_scroll;
16373 /* If we need to move point for either of the above reasons,
16374 now actually do it. */
16375 if (new_vpos >= 0)
16377 struct glyph_row *row;
16379 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
16380 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
16381 ++row;
16383 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
16384 MATRIX_ROW_START_BYTEPOS (row));
16386 if (w != XWINDOW (selected_window))
16387 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
16388 else if (current_buffer == old)
16389 SET_TEXT_POS (lpoint, PT, PT_BYTE);
16391 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
16393 /* Re-run pre-redisplay-function so it can update the region
16394 according to the new position of point. */
16395 /* Other than the cursor, w's redisplay is done so we can set its
16396 redisplay to false. Also the buffer's redisplay can be set to
16397 false, since propagate_buffer_redisplay should have already
16398 propagated its info to `w' anyway. */
16399 w->redisplay = false;
16400 XBUFFER (w->contents)->text->redisplay = false;
16401 safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
16403 if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
16405 /* pre-redisplay-function made changes (e.g. move the region)
16406 that require another round of redisplay. */
16407 clear_glyph_matrix (w->desired_matrix);
16408 if (!try_window (window, startp, 0))
16409 goto need_larger_matrices;
16412 if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
16414 clear_glyph_matrix (w->desired_matrix);
16415 goto try_to_scroll;
16418 #ifdef GLYPH_DEBUG
16419 debug_method_add (w, "forced window start");
16420 #endif
16421 goto done;
16424 /* Handle case where text has not changed, only point, and it has
16425 not moved off the frame, and we are not retrying after hscroll.
16426 (current_matrix_up_to_date_p is nonzero when retrying.) */
16427 if (current_matrix_up_to_date_p
16428 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
16429 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
16431 switch (rc)
16433 case CURSOR_MOVEMENT_SUCCESS:
16434 used_current_matrix_p = 1;
16435 goto done;
16437 case CURSOR_MOVEMENT_MUST_SCROLL:
16438 goto try_to_scroll;
16440 default:
16441 emacs_abort ();
16444 /* If current starting point was originally the beginning of a line
16445 but no longer is, find a new starting point. */
16446 else if (w->start_at_line_beg
16447 && !(CHARPOS (startp) <= BEGV
16448 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
16450 #ifdef GLYPH_DEBUG
16451 debug_method_add (w, "recenter 1");
16452 #endif
16453 goto recenter;
16456 /* Try scrolling with try_window_id. Value is > 0 if update has
16457 been done, it is -1 if we know that the same window start will
16458 not work. It is 0 if unsuccessful for some other reason. */
16459 else if ((tem = try_window_id (w)) != 0)
16461 #ifdef GLYPH_DEBUG
16462 debug_method_add (w, "try_window_id %d", tem);
16463 #endif
16465 if (f->fonts_changed)
16466 goto need_larger_matrices;
16467 if (tem > 0)
16468 goto done;
16470 /* Otherwise try_window_id has returned -1 which means that we
16471 don't want the alternative below this comment to execute. */
16473 else if (CHARPOS (startp) >= BEGV
16474 && CHARPOS (startp) <= ZV
16475 && PT >= CHARPOS (startp)
16476 && (CHARPOS (startp) < ZV
16477 /* Avoid starting at end of buffer. */
16478 || CHARPOS (startp) == BEGV
16479 || !window_outdated (w)))
16481 int d1, d2, d5, d6;
16482 int rtop, rbot;
16484 /* If first window line is a continuation line, and window start
16485 is inside the modified region, but the first change is before
16486 current window start, we must select a new window start.
16488 However, if this is the result of a down-mouse event (e.g. by
16489 extending the mouse-drag-overlay), we don't want to select a
16490 new window start, since that would change the position under
16491 the mouse, resulting in an unwanted mouse-movement rather
16492 than a simple mouse-click. */
16493 if (!w->start_at_line_beg
16494 && NILP (do_mouse_tracking)
16495 && CHARPOS (startp) > BEGV
16496 && CHARPOS (startp) > BEG + beg_unchanged
16497 && CHARPOS (startp) <= Z - end_unchanged
16498 /* Even if w->start_at_line_beg is nil, a new window may
16499 start at a line_beg, since that's how set_buffer_window
16500 sets it. So, we need to check the return value of
16501 compute_window_start_on_continuation_line. (See also
16502 bug#197). */
16503 && XMARKER (w->start)->buffer == current_buffer
16504 && compute_window_start_on_continuation_line (w)
16505 /* It doesn't make sense to force the window start like we
16506 do at label force_start if it is already known that point
16507 will not be fully visible in the resulting window, because
16508 doing so will move point from its correct position
16509 instead of scrolling the window to bring point into view.
16510 See bug#9324. */
16511 && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
16512 /* A very tall row could need more than the window height,
16513 in which case we accept that it is partially visible. */
16514 && (rtop != 0) == (rbot != 0))
16516 w->force_start = 1;
16517 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16518 #ifdef GLYPH_DEBUG
16519 debug_method_add (w, "recomputed window start in continuation line");
16520 #endif
16521 goto force_start;
16524 #ifdef GLYPH_DEBUG
16525 debug_method_add (w, "same window start");
16526 #endif
16528 /* Try to redisplay starting at same place as before.
16529 If point has not moved off frame, accept the results. */
16530 if (!current_matrix_up_to_date_p
16531 /* Don't use try_window_reusing_current_matrix in this case
16532 because a window scroll function can have changed the
16533 buffer. */
16534 || !NILP (Vwindow_scroll_functions)
16535 || MINI_WINDOW_P (w)
16536 || !(used_current_matrix_p
16537 = try_window_reusing_current_matrix (w)))
16539 IF_DEBUG (debug_method_add (w, "1"));
16540 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
16541 /* -1 means we need to scroll.
16542 0 means we need new matrices, but fonts_changed
16543 is set in that case, so we will detect it below. */
16544 goto try_to_scroll;
16547 if (f->fonts_changed)
16548 goto need_larger_matrices;
16550 if (w->cursor.vpos >= 0)
16552 if (!just_this_one_p
16553 || current_buffer->clip_changed
16554 || BEG_UNCHANGED < CHARPOS (startp))
16555 /* Forget any recorded base line for line number display. */
16556 w->base_line_number = 0;
16558 if (!cursor_row_fully_visible_p (w, 1, 0))
16560 clear_glyph_matrix (w->desired_matrix);
16561 last_line_misfit = 1;
16563 /* Drop through and scroll. */
16564 else
16565 goto done;
16567 else
16568 clear_glyph_matrix (w->desired_matrix);
16571 try_to_scroll:
16573 /* Redisplay the mode line. Select the buffer properly for that. */
16574 if (!update_mode_line)
16576 update_mode_line = 1;
16577 w->update_mode_line = 1;
16580 /* Try to scroll by specified few lines. */
16581 if ((scroll_conservatively
16582 || emacs_scroll_step
16583 || temp_scroll_step
16584 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
16585 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
16586 && CHARPOS (startp) >= BEGV
16587 && CHARPOS (startp) <= ZV)
16589 /* The function returns -1 if new fonts were loaded, 1 if
16590 successful, 0 if not successful. */
16591 int ss = try_scrolling (window, just_this_one_p,
16592 scroll_conservatively,
16593 emacs_scroll_step,
16594 temp_scroll_step, last_line_misfit);
16595 switch (ss)
16597 case SCROLLING_SUCCESS:
16598 goto done;
16600 case SCROLLING_NEED_LARGER_MATRICES:
16601 goto need_larger_matrices;
16603 case SCROLLING_FAILED:
16604 break;
16606 default:
16607 emacs_abort ();
16611 /* Finally, just choose a place to start which positions point
16612 according to user preferences. */
16614 recenter:
16616 #ifdef GLYPH_DEBUG
16617 debug_method_add (w, "recenter");
16618 #endif
16620 /* Forget any previously recorded base line for line number display. */
16621 if (!buffer_unchanged_p)
16622 w->base_line_number = 0;
16624 /* Determine the window start relative to point. */
16625 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16626 it.current_y = it.last_visible_y;
16627 if (centering_position < 0)
16629 int window_total_lines
16630 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16631 int margin
16632 = scroll_margin > 0
16633 ? min (scroll_margin, window_total_lines / 4)
16634 : 0;
16635 ptrdiff_t margin_pos = CHARPOS (startp);
16636 Lisp_Object aggressive;
16637 int scrolling_up;
16639 /* If there is a scroll margin at the top of the window, find
16640 its character position. */
16641 if (margin
16642 /* Cannot call start_display if startp is not in the
16643 accessible region of the buffer. This can happen when we
16644 have just switched to a different buffer and/or changed
16645 its restriction. In that case, startp is initialized to
16646 the character position 1 (BEGV) because we did not yet
16647 have chance to display the buffer even once. */
16648 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16650 struct it it1;
16651 void *it1data = NULL;
16653 SAVE_IT (it1, it, it1data);
16654 start_display (&it1, w, startp);
16655 move_it_vertically (&it1, margin * frame_line_height);
16656 margin_pos = IT_CHARPOS (it1);
16657 RESTORE_IT (&it, &it, it1data);
16659 scrolling_up = PT > margin_pos;
16660 aggressive =
16661 scrolling_up
16662 ? BVAR (current_buffer, scroll_up_aggressively)
16663 : BVAR (current_buffer, scroll_down_aggressively);
16665 if (!MINI_WINDOW_P (w)
16666 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16668 int pt_offset = 0;
16670 /* Setting scroll-conservatively overrides
16671 scroll-*-aggressively. */
16672 if (!scroll_conservatively && NUMBERP (aggressive))
16674 double float_amount = XFLOATINT (aggressive);
16676 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16677 if (pt_offset == 0 && float_amount > 0)
16678 pt_offset = 1;
16679 if (pt_offset && margin > 0)
16680 margin -= 1;
16682 /* Compute how much to move the window start backward from
16683 point so that point will be displayed where the user
16684 wants it. */
16685 if (scrolling_up)
16687 centering_position = it.last_visible_y;
16688 if (pt_offset)
16689 centering_position -= pt_offset;
16690 centering_position -=
16691 frame_line_height * (1 + margin + (last_line_misfit != 0))
16692 + WINDOW_HEADER_LINE_HEIGHT (w);
16693 /* Don't let point enter the scroll margin near top of
16694 the window. */
16695 if (centering_position < margin * frame_line_height)
16696 centering_position = margin * frame_line_height;
16698 else
16699 centering_position = margin * frame_line_height + pt_offset;
16701 else
16702 /* Set the window start half the height of the window backward
16703 from point. */
16704 centering_position = window_box_height (w) / 2;
16706 move_it_vertically_backward (&it, centering_position);
16708 eassert (IT_CHARPOS (it) >= BEGV);
16710 /* The function move_it_vertically_backward may move over more
16711 than the specified y-distance. If it->w is small, e.g. a
16712 mini-buffer window, we may end up in front of the window's
16713 display area. Start displaying at the start of the line
16714 containing PT in this case. */
16715 if (it.current_y <= 0)
16717 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16718 move_it_vertically_backward (&it, 0);
16719 it.current_y = 0;
16722 it.current_x = it.hpos = 0;
16724 /* Set the window start position here explicitly, to avoid an
16725 infinite loop in case the functions in window-scroll-functions
16726 get errors. */
16727 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16729 /* Run scroll hooks. */
16730 startp = run_window_scroll_functions (window, it.current.pos);
16732 /* Redisplay the window. */
16733 if (!current_matrix_up_to_date_p
16734 || windows_or_buffers_changed
16735 || f->cursor_type_changed
16736 /* Don't use try_window_reusing_current_matrix in this case
16737 because it can have changed the buffer. */
16738 || !NILP (Vwindow_scroll_functions)
16739 || !just_this_one_p
16740 || MINI_WINDOW_P (w)
16741 || !(used_current_matrix_p
16742 = try_window_reusing_current_matrix (w)))
16743 try_window (window, startp, 0);
16745 /* If new fonts have been loaded (due to fontsets), give up. We
16746 have to start a new redisplay since we need to re-adjust glyph
16747 matrices. */
16748 if (f->fonts_changed)
16749 goto need_larger_matrices;
16751 /* If cursor did not appear assume that the middle of the window is
16752 in the first line of the window. Do it again with the next line.
16753 (Imagine a window of height 100, displaying two lines of height
16754 60. Moving back 50 from it->last_visible_y will end in the first
16755 line.) */
16756 if (w->cursor.vpos < 0)
16758 if (w->window_end_valid && PT >= Z - w->window_end_pos)
16760 clear_glyph_matrix (w->desired_matrix);
16761 move_it_by_lines (&it, 1);
16762 try_window (window, it.current.pos, 0);
16764 else if (PT < IT_CHARPOS (it))
16766 clear_glyph_matrix (w->desired_matrix);
16767 move_it_by_lines (&it, -1);
16768 try_window (window, it.current.pos, 0);
16770 else
16772 /* Not much we can do about it. */
16776 /* Consider the following case: Window starts at BEGV, there is
16777 invisible, intangible text at BEGV, so that display starts at
16778 some point START > BEGV. It can happen that we are called with
16779 PT somewhere between BEGV and START. Try to handle that case,
16780 and similar ones. */
16781 if (w->cursor.vpos < 0)
16783 /* First, try locating the proper glyph row for PT. */
16784 struct glyph_row *row =
16785 row_containing_pos (w, PT, w->current_matrix->rows, NULL, 0);
16787 /* Sometimes point is at the beginning of invisible text that is
16788 before the 1st character displayed in the row. In that case,
16789 row_containing_pos fails to find the row, because no glyphs
16790 with appropriate buffer positions are present in the row.
16791 Therefore, we next try to find the row which shows the 1st
16792 position after the invisible text. */
16793 if (!row)
16795 Lisp_Object val =
16796 get_char_property_and_overlay (make_number (PT), Qinvisible,
16797 Qnil, NULL);
16799 if (TEXT_PROP_MEANS_INVISIBLE (val))
16801 ptrdiff_t alt_pos;
16802 Lisp_Object invis_end =
16803 Fnext_single_char_property_change (make_number (PT), Qinvisible,
16804 Qnil, Qnil);
16806 if (NATNUMP (invis_end))
16807 alt_pos = XFASTINT (invis_end);
16808 else
16809 alt_pos = ZV;
16810 row = row_containing_pos (w, alt_pos, w->current_matrix->rows,
16811 NULL, 0);
16814 /* Finally, fall back on the first row of the window after the
16815 header line (if any). This is slightly better than not
16816 displaying the cursor at all. */
16817 if (!row)
16819 row = w->current_matrix->rows;
16820 if (row->mode_line_p)
16821 ++row;
16823 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16826 if (!cursor_row_fully_visible_p (w, 0, 0))
16828 /* If vscroll is enabled, disable it and try again. */
16829 if (w->vscroll)
16831 w->vscroll = 0;
16832 clear_glyph_matrix (w->desired_matrix);
16833 goto recenter;
16836 /* Users who set scroll-conservatively to a large number want
16837 point just above/below the scroll margin. If we ended up
16838 with point's row partially visible, move the window start to
16839 make that row fully visible and out of the margin. */
16840 if (scroll_conservatively > SCROLL_LIMIT)
16842 int window_total_lines
16843 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16844 int margin =
16845 scroll_margin > 0
16846 ? min (scroll_margin, window_total_lines / 4)
16847 : 0;
16848 int move_down = w->cursor.vpos >= window_total_lines / 2;
16850 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16851 clear_glyph_matrix (w->desired_matrix);
16852 if (1 == try_window (window, it.current.pos,
16853 TRY_WINDOW_CHECK_MARGINS))
16854 goto done;
16857 /* If centering point failed to make the whole line visible,
16858 put point at the top instead. That has to make the whole line
16859 visible, if it can be done. */
16860 if (centering_position == 0)
16861 goto done;
16863 clear_glyph_matrix (w->desired_matrix);
16864 centering_position = 0;
16865 goto recenter;
16868 done:
16870 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16871 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16872 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16874 /* Display the mode line, if we must. */
16875 if ((update_mode_line
16876 /* If window not full width, must redo its mode line
16877 if (a) the window to its side is being redone and
16878 (b) we do a frame-based redisplay. This is a consequence
16879 of how inverted lines are drawn in frame-based redisplay. */
16880 || (!just_this_one_p
16881 && !FRAME_WINDOW_P (f)
16882 && !WINDOW_FULL_WIDTH_P (w))
16883 /* Line number to display. */
16884 || w->base_line_pos > 0
16885 /* Column number is displayed and different from the one displayed. */
16886 || (w->column_number_displayed != -1
16887 && (w->column_number_displayed != current_column ())))
16888 /* This means that the window has a mode line. */
16889 && (WINDOW_WANTS_MODELINE_P (w)
16890 || WINDOW_WANTS_HEADER_LINE_P (w)))
16893 display_mode_lines (w);
16895 /* If mode line height has changed, arrange for a thorough
16896 immediate redisplay using the correct mode line height. */
16897 if (WINDOW_WANTS_MODELINE_P (w)
16898 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16900 f->fonts_changed = 1;
16901 w->mode_line_height = -1;
16902 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16903 = DESIRED_MODE_LINE_HEIGHT (w);
16906 /* If header line height has changed, arrange for a thorough
16907 immediate redisplay using the correct header line height. */
16908 if (WINDOW_WANTS_HEADER_LINE_P (w)
16909 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16911 f->fonts_changed = 1;
16912 w->header_line_height = -1;
16913 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16914 = DESIRED_HEADER_LINE_HEIGHT (w);
16917 if (f->fonts_changed)
16918 goto need_larger_matrices;
16921 if (!line_number_displayed && w->base_line_pos != -1)
16923 w->base_line_pos = 0;
16924 w->base_line_number = 0;
16927 finish_menu_bars:
16929 /* When we reach a frame's selected window, redo the frame's menu bar. */
16930 if (update_mode_line
16931 && EQ (FRAME_SELECTED_WINDOW (f), window))
16933 int redisplay_menu_p = 0;
16935 if (FRAME_WINDOW_P (f))
16937 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16938 || defined (HAVE_NS) || defined (USE_GTK)
16939 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16940 #else
16941 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16942 #endif
16944 else
16945 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16947 if (redisplay_menu_p)
16948 display_menu_bar (w);
16950 #ifdef HAVE_WINDOW_SYSTEM
16951 if (FRAME_WINDOW_P (f))
16953 #if defined (USE_GTK) || defined (HAVE_NS)
16954 if (FRAME_EXTERNAL_TOOL_BAR (f))
16955 redisplay_tool_bar (f);
16956 #else
16957 if (WINDOWP (f->tool_bar_window)
16958 && (FRAME_TOOL_BAR_LINES (f) > 0
16959 || !NILP (Vauto_resize_tool_bars))
16960 && redisplay_tool_bar (f))
16961 ignore_mouse_drag_p = 1;
16962 #endif
16964 #endif
16967 #ifdef HAVE_WINDOW_SYSTEM
16968 if (FRAME_WINDOW_P (f)
16969 && update_window_fringes (w, (just_this_one_p
16970 || (!used_current_matrix_p && !overlay_arrow_seen)
16971 || w->pseudo_window_p)))
16973 update_begin (f);
16974 block_input ();
16975 if (draw_window_fringes (w, 1))
16977 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
16978 x_draw_right_divider (w);
16979 else
16980 x_draw_vertical_border (w);
16982 unblock_input ();
16983 update_end (f);
16986 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
16987 x_draw_bottom_divider (w);
16988 #endif /* HAVE_WINDOW_SYSTEM */
16990 /* We go to this label, with fonts_changed set, if it is
16991 necessary to try again using larger glyph matrices.
16992 We have to redeem the scroll bar even in this case,
16993 because the loop in redisplay_internal expects that. */
16994 need_larger_matrices:
16996 finish_scroll_bars:
16998 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17000 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
17001 /* Set the thumb's position and size. */
17002 set_vertical_scroll_bar (w);
17004 if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
17005 /* Set the thumb's position and size. */
17006 set_horizontal_scroll_bar (w);
17008 /* Note that we actually used the scroll bar attached to this
17009 window, so it shouldn't be deleted at the end of redisplay. */
17010 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
17011 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
17014 /* Restore current_buffer and value of point in it. The window
17015 update may have changed the buffer, so first make sure `opoint'
17016 is still valid (Bug#6177). */
17017 if (CHARPOS (opoint) < BEGV)
17018 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
17019 else if (CHARPOS (opoint) > ZV)
17020 TEMP_SET_PT_BOTH (Z, Z_BYTE);
17021 else
17022 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
17024 set_buffer_internal_1 (old);
17025 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
17026 shorter. This can be caused by log truncation in *Messages*. */
17027 if (CHARPOS (lpoint) <= ZV)
17028 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
17030 unbind_to (count, Qnil);
17034 /* Build the complete desired matrix of WINDOW with a window start
17035 buffer position POS.
17037 Value is 1 if successful. It is zero if fonts were loaded during
17038 redisplay which makes re-adjusting glyph matrices necessary, and -1
17039 if point would appear in the scroll margins.
17040 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
17041 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
17042 set in FLAGS.) */
17045 try_window (Lisp_Object window, struct text_pos pos, int flags)
17047 struct window *w = XWINDOW (window);
17048 struct it it;
17049 struct glyph_row *last_text_row = NULL;
17050 struct frame *f = XFRAME (w->frame);
17051 int frame_line_height = default_line_pixel_height (w);
17053 /* Make POS the new window start. */
17054 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
17056 /* Mark cursor position as unknown. No overlay arrow seen. */
17057 w->cursor.vpos = -1;
17058 overlay_arrow_seen = 0;
17060 /* Initialize iterator and info to start at POS. */
17061 start_display (&it, w, pos);
17062 it.glyph_row->reversed_p = false;
17066 /* Display all lines of W. */
17067 while (it.current_y < it.last_visible_y)
17069 if (display_line (&it))
17070 last_text_row = it.glyph_row - 1;
17071 if (f->fonts_changed && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
17072 return 0;
17074 #ifdef HAVE_XWIDGETS_xxx
17075 //currently this is needed to detect xwidget movement reliably. or probably not.
17076 printf("try_window\n");
17077 return 0;
17078 #endif
17080 /* Don't let the cursor end in the scroll margins. */
17081 if ((flags & TRY_WINDOW_CHECK_MARGINS)
17082 && !MINI_WINDOW_P (w))
17084 int this_scroll_margin;
17085 int window_total_lines
17086 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
17088 if (scroll_margin > 0)
17090 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
17091 this_scroll_margin *= frame_line_height;
17093 else
17094 this_scroll_margin = 0;
17096 if ((w->cursor.y >= 0 /* not vscrolled */
17097 && w->cursor.y < this_scroll_margin
17098 && CHARPOS (pos) > BEGV
17099 && IT_CHARPOS (it) < ZV)
17100 /* rms: considering make_cursor_line_fully_visible_p here
17101 seems to give wrong results. We don't want to recenter
17102 when the last line is partly visible, we want to allow
17103 that case to be handled in the usual way. */
17104 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
17106 w->cursor.vpos = -1;
17107 clear_glyph_matrix (w->desired_matrix);
17108 return -1;
17112 /* If bottom moved off end of frame, change mode line percentage. */
17113 if (w->window_end_pos <= 0 && Z != IT_CHARPOS (it))
17114 w->update_mode_line = 1;
17116 /* Set window_end_pos to the offset of the last character displayed
17117 on the window from the end of current_buffer. Set
17118 window_end_vpos to its row number. */
17119 if (last_text_row)
17121 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
17122 adjust_window_ends (w, last_text_row, 0);
17123 eassert
17124 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
17125 w->window_end_vpos)));
17127 else
17129 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17130 w->window_end_pos = Z - ZV;
17131 w->window_end_vpos = 0;
17134 /* But that is not valid info until redisplay finishes. */
17135 w->window_end_valid = 0;
17136 return 1;
17141 /************************************************************************
17142 Window redisplay reusing current matrix when buffer has not changed
17143 ************************************************************************/
17145 /* Try redisplay of window W showing an unchanged buffer with a
17146 different window start than the last time it was displayed by
17147 reusing its current matrix. Value is non-zero if successful.
17148 W->start is the new window start. */
17150 static int
17151 try_window_reusing_current_matrix (struct window *w)
17153 struct frame *f = XFRAME (w->frame);
17154 struct glyph_row *bottom_row;
17155 struct it it;
17156 struct run run;
17157 struct text_pos start, new_start;
17158 int nrows_scrolled, i;
17159 struct glyph_row *last_text_row;
17160 struct glyph_row *last_reused_text_row;
17161 struct glyph_row *start_row;
17162 int start_vpos, min_y, max_y;
17164 #ifdef GLYPH_DEBUG
17165 if (inhibit_try_window_reusing)
17166 return 0;
17167 #endif
17169 #ifdef HAVE_XWIDGETS_xxx
17170 //currently this is needed to detect xwidget movement reliably. or probably not.
17171 printf("try_window_reusing_current_matrix\n");
17172 return 0;
17173 #endif
17176 if (/* This function doesn't handle terminal frames. */
17177 !FRAME_WINDOW_P (f)
17178 /* Don't try to reuse the display if windows have been split
17179 or such. */
17180 || windows_or_buffers_changed
17181 || f->cursor_type_changed)
17182 return 0;
17184 /* Can't do this if showing trailing whitespace. */
17185 if (!NILP (Vshow_trailing_whitespace))
17186 return 0;
17188 /* If top-line visibility has changed, give up. */
17189 if (WINDOW_WANTS_HEADER_LINE_P (w)
17190 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
17191 return 0;
17193 /* Give up if old or new display is scrolled vertically. We could
17194 make this function handle this, but right now it doesn't. */
17195 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17196 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
17197 return 0;
17199 /* The variable new_start now holds the new window start. The old
17200 start `start' can be determined from the current matrix. */
17201 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
17202 start = start_row->minpos;
17203 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17205 /* Clear the desired matrix for the display below. */
17206 clear_glyph_matrix (w->desired_matrix);
17208 if (CHARPOS (new_start) <= CHARPOS (start))
17210 /* Don't use this method if the display starts with an ellipsis
17211 displayed for invisible text. It's not easy to handle that case
17212 below, and it's certainly not worth the effort since this is
17213 not a frequent case. */
17214 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
17215 return 0;
17217 IF_DEBUG (debug_method_add (w, "twu1"));
17219 /* Display up to a row that can be reused. The variable
17220 last_text_row is set to the last row displayed that displays
17221 text. Note that it.vpos == 0 if or if not there is a
17222 header-line; it's not the same as the MATRIX_ROW_VPOS! */
17223 start_display (&it, w, new_start);
17224 w->cursor.vpos = -1;
17225 last_text_row = last_reused_text_row = NULL;
17227 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17229 /* If we have reached into the characters in the START row,
17230 that means the line boundaries have changed. So we
17231 can't start copying with the row START. Maybe it will
17232 work to start copying with the following row. */
17233 while (IT_CHARPOS (it) > CHARPOS (start))
17235 /* Advance to the next row as the "start". */
17236 start_row++;
17237 start = start_row->minpos;
17238 /* If there are no more rows to try, or just one, give up. */
17239 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
17240 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
17241 || CHARPOS (start) == ZV)
17243 clear_glyph_matrix (w->desired_matrix);
17244 return 0;
17247 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
17249 /* If we have reached alignment, we can copy the rest of the
17250 rows. */
17251 if (IT_CHARPOS (it) == CHARPOS (start)
17252 /* Don't accept "alignment" inside a display vector,
17253 since start_row could have started in the middle of
17254 that same display vector (thus their character
17255 positions match), and we have no way of telling if
17256 that is the case. */
17257 && it.current.dpvec_index < 0)
17258 break;
17260 it.glyph_row->reversed_p = false;
17261 if (display_line (&it))
17262 last_text_row = it.glyph_row - 1;
17266 /* A value of current_y < last_visible_y means that we stopped
17267 at the previous window start, which in turn means that we
17268 have at least one reusable row. */
17269 if (it.current_y < it.last_visible_y)
17271 struct glyph_row *row;
17273 /* IT.vpos always starts from 0; it counts text lines. */
17274 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
17276 /* Find PT if not already found in the lines displayed. */
17277 if (w->cursor.vpos < 0)
17279 int dy = it.current_y - start_row->y;
17281 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17282 row = row_containing_pos (w, PT, row, NULL, dy);
17283 if (row)
17284 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
17285 dy, nrows_scrolled);
17286 else
17288 clear_glyph_matrix (w->desired_matrix);
17289 return 0;
17293 /* Scroll the display. Do it before the current matrix is
17294 changed. The problem here is that update has not yet
17295 run, i.e. part of the current matrix is not up to date.
17296 scroll_run_hook will clear the cursor, and use the
17297 current matrix to get the height of the row the cursor is
17298 in. */
17299 run.current_y = start_row->y;
17300 run.desired_y = it.current_y;
17301 run.height = it.last_visible_y - it.current_y;
17303 if (run.height > 0 && run.current_y != run.desired_y)
17305 update_begin (f);
17306 FRAME_RIF (f)->update_window_begin_hook (w);
17307 FRAME_RIF (f)->clear_window_mouse_face (w);
17308 FRAME_RIF (f)->scroll_run_hook (w, &run);
17309 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17310 update_end (f);
17313 /* Shift current matrix down by nrows_scrolled lines. */
17314 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17315 rotate_matrix (w->current_matrix,
17316 start_vpos,
17317 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17318 nrows_scrolled);
17320 /* Disable lines that must be updated. */
17321 for (i = 0; i < nrows_scrolled; ++i)
17322 (start_row + i)->enabled_p = false;
17324 /* Re-compute Y positions. */
17325 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17326 max_y = it.last_visible_y;
17327 for (row = start_row + nrows_scrolled;
17328 row < bottom_row;
17329 ++row)
17331 row->y = it.current_y;
17332 row->visible_height = row->height;
17334 if (row->y < min_y)
17335 row->visible_height -= min_y - row->y;
17336 if (row->y + row->height > max_y)
17337 row->visible_height -= row->y + row->height - max_y;
17338 if (row->fringe_bitmap_periodic_p)
17339 row->redraw_fringe_bitmaps_p = 1;
17341 it.current_y += row->height;
17343 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17344 last_reused_text_row = row;
17345 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
17346 break;
17349 /* Disable lines in the current matrix which are now
17350 below the window. */
17351 for (++row; row < bottom_row; ++row)
17352 row->enabled_p = row->mode_line_p = 0;
17355 /* Update window_end_pos etc.; last_reused_text_row is the last
17356 reused row from the current matrix containing text, if any.
17357 The value of last_text_row is the last displayed line
17358 containing text. */
17359 if (last_reused_text_row)
17360 adjust_window_ends (w, last_reused_text_row, 1);
17361 else if (last_text_row)
17362 adjust_window_ends (w, last_text_row, 0);
17363 else
17365 /* This window must be completely empty. */
17366 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
17367 w->window_end_pos = Z - ZV;
17368 w->window_end_vpos = 0;
17370 w->window_end_valid = 0;
17372 /* Update hint: don't try scrolling again in update_window. */
17373 w->desired_matrix->no_scrolling_p = 1;
17375 #ifdef GLYPH_DEBUG
17376 debug_method_add (w, "try_window_reusing_current_matrix 1");
17377 #endif
17378 return 1;
17380 else if (CHARPOS (new_start) > CHARPOS (start))
17382 struct glyph_row *pt_row, *row;
17383 struct glyph_row *first_reusable_row;
17384 struct glyph_row *first_row_to_display;
17385 int dy;
17386 int yb = window_text_bottom_y (w);
17388 /* Find the row starting at new_start, if there is one. Don't
17389 reuse a partially visible line at the end. */
17390 first_reusable_row = start_row;
17391 while (first_reusable_row->enabled_p
17392 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
17393 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17394 < CHARPOS (new_start)))
17395 ++first_reusable_row;
17397 /* Give up if there is no row to reuse. */
17398 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
17399 || !first_reusable_row->enabled_p
17400 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
17401 != CHARPOS (new_start)))
17402 return 0;
17404 /* We can reuse fully visible rows beginning with
17405 first_reusable_row to the end of the window. Set
17406 first_row_to_display to the first row that cannot be reused.
17407 Set pt_row to the row containing point, if there is any. */
17408 pt_row = NULL;
17409 for (first_row_to_display = first_reusable_row;
17410 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
17411 ++first_row_to_display)
17413 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
17414 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
17415 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
17416 && first_row_to_display->ends_at_zv_p
17417 && pt_row == NULL)))
17418 pt_row = first_row_to_display;
17421 /* Start displaying at the start of first_row_to_display. */
17422 eassert (first_row_to_display->y < yb);
17423 init_to_row_start (&it, w, first_row_to_display);
17425 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
17426 - start_vpos);
17427 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
17428 - nrows_scrolled);
17429 it.current_y = (first_row_to_display->y - first_reusable_row->y
17430 + WINDOW_HEADER_LINE_HEIGHT (w));
17432 /* Display lines beginning with first_row_to_display in the
17433 desired matrix. Set last_text_row to the last row displayed
17434 that displays text. */
17435 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
17436 if (pt_row == NULL)
17437 w->cursor.vpos = -1;
17438 last_text_row = NULL;
17439 while (it.current_y < it.last_visible_y && !f->fonts_changed)
17440 if (display_line (&it))
17441 last_text_row = it.glyph_row - 1;
17443 /* If point is in a reused row, adjust y and vpos of the cursor
17444 position. */
17445 if (pt_row)
17447 w->cursor.vpos -= nrows_scrolled;
17448 w->cursor.y -= first_reusable_row->y - start_row->y;
17451 /* Give up if point isn't in a row displayed or reused. (This
17452 also handles the case where w->cursor.vpos < nrows_scrolled
17453 after the calls to display_line, which can happen with scroll
17454 margins. See bug#1295.) */
17455 if (w->cursor.vpos < 0)
17457 clear_glyph_matrix (w->desired_matrix);
17458 return 0;
17461 /* Scroll the display. */
17462 run.current_y = first_reusable_row->y;
17463 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
17464 run.height = it.last_visible_y - run.current_y;
17465 dy = run.current_y - run.desired_y;
17467 if (run.height)
17469 update_begin (f);
17470 FRAME_RIF (f)->update_window_begin_hook (w);
17471 FRAME_RIF (f)->clear_window_mouse_face (w);
17472 FRAME_RIF (f)->scroll_run_hook (w, &run);
17473 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17474 update_end (f);
17477 /* Adjust Y positions of reused rows. */
17478 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
17479 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
17480 max_y = it.last_visible_y;
17481 for (row = first_reusable_row; row < first_row_to_display; ++row)
17483 row->y -= dy;
17484 row->visible_height = row->height;
17485 if (row->y < min_y)
17486 row->visible_height -= min_y - row->y;
17487 if (row->y + row->height > max_y)
17488 row->visible_height -= row->y + row->height - max_y;
17489 if (row->fringe_bitmap_periodic_p)
17490 row->redraw_fringe_bitmaps_p = 1;
17493 /* Scroll the current matrix. */
17494 eassert (nrows_scrolled > 0);
17495 rotate_matrix (w->current_matrix,
17496 start_vpos,
17497 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
17498 -nrows_scrolled);
17500 /* Disable rows not reused. */
17501 for (row -= nrows_scrolled; row < bottom_row; ++row)
17502 row->enabled_p = false;
17504 /* Point may have moved to a different line, so we cannot assume that
17505 the previous cursor position is valid; locate the correct row. */
17506 if (pt_row)
17508 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
17509 row < bottom_row
17510 && PT >= MATRIX_ROW_END_CHARPOS (row)
17511 && !row->ends_at_zv_p;
17512 row++)
17514 w->cursor.vpos++;
17515 w->cursor.y = row->y;
17517 if (row < bottom_row)
17519 /* Can't simply scan the row for point with
17520 bidi-reordered glyph rows. Let set_cursor_from_row
17521 figure out where to put the cursor, and if it fails,
17522 give up. */
17523 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
17525 if (!set_cursor_from_row (w, row, w->current_matrix,
17526 0, 0, 0, 0))
17528 clear_glyph_matrix (w->desired_matrix);
17529 return 0;
17532 else
17534 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
17535 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17537 for (; glyph < end
17538 && (!BUFFERP (glyph->object)
17539 || glyph->charpos < PT);
17540 glyph++)
17542 w->cursor.hpos++;
17543 w->cursor.x += glyph->pixel_width;
17549 /* Adjust window end. A null value of last_text_row means that
17550 the window end is in reused rows which in turn means that
17551 only its vpos can have changed. */
17552 if (last_text_row)
17553 adjust_window_ends (w, last_text_row, 0);
17554 else
17555 w->window_end_vpos -= nrows_scrolled;
17557 w->window_end_valid = 0;
17558 w->desired_matrix->no_scrolling_p = 1;
17560 #ifdef GLYPH_DEBUG
17561 debug_method_add (w, "try_window_reusing_current_matrix 2");
17562 #endif
17563 return 1;
17566 return 0;
17571 /************************************************************************
17572 Window redisplay reusing current matrix when buffer has changed
17573 ************************************************************************/
17575 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
17576 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
17577 ptrdiff_t *, ptrdiff_t *);
17578 static struct glyph_row *
17579 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
17580 struct glyph_row *);
17583 /* Return the last row in MATRIX displaying text. If row START is
17584 non-null, start searching with that row. IT gives the dimensions
17585 of the display. Value is null if matrix is empty; otherwise it is
17586 a pointer to the row found. */
17588 static struct glyph_row *
17589 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
17590 struct glyph_row *start)
17592 struct glyph_row *row, *row_found;
17594 /* Set row_found to the last row in IT->w's current matrix
17595 displaying text. The loop looks funny but think of partially
17596 visible lines. */
17597 row_found = NULL;
17598 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
17599 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17601 eassert (row->enabled_p);
17602 row_found = row;
17603 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
17604 break;
17605 ++row;
17608 return row_found;
17612 /* Return the last row in the current matrix of W that is not affected
17613 by changes at the start of current_buffer that occurred since W's
17614 current matrix was built. Value is null if no such row exists.
17616 BEG_UNCHANGED us the number of characters unchanged at the start of
17617 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
17618 first changed character in current_buffer. Characters at positions <
17619 BEG + BEG_UNCHANGED are at the same buffer positions as they were
17620 when the current matrix was built. */
17622 static struct glyph_row *
17623 find_last_unchanged_at_beg_row (struct window *w)
17625 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
17626 struct glyph_row *row;
17627 struct glyph_row *row_found = NULL;
17628 int yb = window_text_bottom_y (w);
17630 /* Find the last row displaying unchanged text. */
17631 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17632 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17633 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17634 ++row)
17636 if (/* If row ends before first_changed_pos, it is unchanged,
17637 except in some case. */
17638 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17639 /* When row ends in ZV and we write at ZV it is not
17640 unchanged. */
17641 && !row->ends_at_zv_p
17642 /* When first_changed_pos is the end of a continued line,
17643 row is not unchanged because it may be no longer
17644 continued. */
17645 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17646 && (row->continued_p
17647 || row->exact_window_width_line_p))
17648 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17649 needs to be recomputed, so don't consider this row as
17650 unchanged. This happens when the last line was
17651 bidi-reordered and was killed immediately before this
17652 redisplay cycle. In that case, ROW->end stores the
17653 buffer position of the first visual-order character of
17654 the killed text, which is now beyond ZV. */
17655 && CHARPOS (row->end.pos) <= ZV)
17656 row_found = row;
17658 /* Stop if last visible row. */
17659 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17660 break;
17663 return row_found;
17667 /* Find the first glyph row in the current matrix of W that is not
17668 affected by changes at the end of current_buffer since the
17669 time W's current matrix was built.
17671 Return in *DELTA the number of chars by which buffer positions in
17672 unchanged text at the end of current_buffer must be adjusted.
17674 Return in *DELTA_BYTES the corresponding number of bytes.
17676 Value is null if no such row exists, i.e. all rows are affected by
17677 changes. */
17679 static struct glyph_row *
17680 find_first_unchanged_at_end_row (struct window *w,
17681 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17683 struct glyph_row *row;
17684 struct glyph_row *row_found = NULL;
17686 *delta = *delta_bytes = 0;
17688 /* Display must not have been paused, otherwise the current matrix
17689 is not up to date. */
17690 eassert (w->window_end_valid);
17692 /* A value of window_end_pos >= END_UNCHANGED means that the window
17693 end is in the range of changed text. If so, there is no
17694 unchanged row at the end of W's current matrix. */
17695 if (w->window_end_pos >= END_UNCHANGED)
17696 return NULL;
17698 /* Set row to the last row in W's current matrix displaying text. */
17699 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
17701 /* If matrix is entirely empty, no unchanged row exists. */
17702 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17704 /* The value of row is the last glyph row in the matrix having a
17705 meaningful buffer position in it. The end position of row
17706 corresponds to window_end_pos. This allows us to translate
17707 buffer positions in the current matrix to current buffer
17708 positions for characters not in changed text. */
17709 ptrdiff_t Z_old =
17710 MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
17711 ptrdiff_t Z_BYTE_old =
17712 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17713 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17714 struct glyph_row *first_text_row
17715 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17717 *delta = Z - Z_old;
17718 *delta_bytes = Z_BYTE - Z_BYTE_old;
17720 /* Set last_unchanged_pos to the buffer position of the last
17721 character in the buffer that has not been changed. Z is the
17722 index + 1 of the last character in current_buffer, i.e. by
17723 subtracting END_UNCHANGED we get the index of the last
17724 unchanged character, and we have to add BEG to get its buffer
17725 position. */
17726 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17727 last_unchanged_pos_old = last_unchanged_pos - *delta;
17729 /* Search backward from ROW for a row displaying a line that
17730 starts at a minimum position >= last_unchanged_pos_old. */
17731 for (; row > first_text_row; --row)
17733 /* This used to abort, but it can happen.
17734 It is ok to just stop the search instead here. KFS. */
17735 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17736 break;
17738 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17739 row_found = row;
17743 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17745 return row_found;
17749 /* Make sure that glyph rows in the current matrix of window W
17750 reference the same glyph memory as corresponding rows in the
17751 frame's frame matrix. This function is called after scrolling W's
17752 current matrix on a terminal frame in try_window_id and
17753 try_window_reusing_current_matrix. */
17755 static void
17756 sync_frame_with_window_matrix_rows (struct window *w)
17758 struct frame *f = XFRAME (w->frame);
17759 struct glyph_row *window_row, *window_row_end, *frame_row;
17761 /* Preconditions: W must be a leaf window and full-width. Its frame
17762 must have a frame matrix. */
17763 eassert (BUFFERP (w->contents));
17764 eassert (WINDOW_FULL_WIDTH_P (w));
17765 eassert (!FRAME_WINDOW_P (f));
17767 /* If W is a full-width window, glyph pointers in W's current matrix
17768 have, by definition, to be the same as glyph pointers in the
17769 corresponding frame matrix. Note that frame matrices have no
17770 marginal areas (see build_frame_matrix). */
17771 window_row = w->current_matrix->rows;
17772 window_row_end = window_row + w->current_matrix->nrows;
17773 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17774 while (window_row < window_row_end)
17776 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17777 struct glyph *end = window_row->glyphs[LAST_AREA];
17779 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17780 frame_row->glyphs[TEXT_AREA] = start;
17781 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17782 frame_row->glyphs[LAST_AREA] = end;
17784 /* Disable frame rows whose corresponding window rows have
17785 been disabled in try_window_id. */
17786 if (!window_row->enabled_p)
17787 frame_row->enabled_p = false;
17789 ++window_row, ++frame_row;
17794 /* Find the glyph row in window W containing CHARPOS. Consider all
17795 rows between START and END (not inclusive). END null means search
17796 all rows to the end of the display area of W. Value is the row
17797 containing CHARPOS or null. */
17799 struct glyph_row *
17800 row_containing_pos (struct window *w, ptrdiff_t charpos,
17801 struct glyph_row *start, struct glyph_row *end, int dy)
17803 struct glyph_row *row = start;
17804 struct glyph_row *best_row = NULL;
17805 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17806 int last_y;
17808 /* If we happen to start on a header-line, skip that. */
17809 if (row->mode_line_p)
17810 ++row;
17812 if ((end && row >= end) || !row->enabled_p)
17813 return NULL;
17815 last_y = window_text_bottom_y (w) - dy;
17817 while (1)
17819 /* Give up if we have gone too far. */
17820 if (end && row >= end)
17821 return NULL;
17822 /* This formerly returned if they were equal.
17823 I think that both quantities are of a "last plus one" type;
17824 if so, when they are equal, the row is within the screen. -- rms. */
17825 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17826 return NULL;
17828 /* If it is in this row, return this row. */
17829 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17830 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17831 /* The end position of a row equals the start
17832 position of the next row. If CHARPOS is there, we
17833 would rather consider it displayed in the next
17834 line, except when this line ends in ZV. */
17835 && !row_for_charpos_p (row, charpos)))
17836 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17838 struct glyph *g;
17840 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17841 || (!best_row && !row->continued_p))
17842 return row;
17843 /* In bidi-reordered rows, there could be several rows whose
17844 edges surround CHARPOS, all of these rows belonging to
17845 the same continued line. We need to find the row which
17846 fits CHARPOS the best. */
17847 for (g = row->glyphs[TEXT_AREA];
17848 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17849 g++)
17851 if (!STRINGP (g->object))
17853 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17855 mindif = eabs (g->charpos - charpos);
17856 best_row = row;
17857 /* Exact match always wins. */
17858 if (mindif == 0)
17859 return best_row;
17864 else if (best_row && !row->continued_p)
17865 return best_row;
17866 ++row;
17871 /* Try to redisplay window W by reusing its existing display. W's
17872 current matrix must be up to date when this function is called,
17873 i.e. window_end_valid must be nonzero.
17875 Value is
17877 >= 1 if successful, i.e. display has been updated
17878 specifically:
17879 1 means the changes were in front of a newline that precedes
17880 the window start, and the whole current matrix was reused
17881 2 means the changes were after the last position displayed
17882 in the window, and the whole current matrix was reused
17883 3 means portions of the current matrix were reused, while
17884 some of the screen lines were redrawn
17885 -1 if redisplay with same window start is known not to succeed
17886 0 if otherwise unsuccessful
17888 The following steps are performed:
17890 1. Find the last row in the current matrix of W that is not
17891 affected by changes at the start of current_buffer. If no such row
17892 is found, give up.
17894 2. Find the first row in W's current matrix that is not affected by
17895 changes at the end of current_buffer. Maybe there is no such row.
17897 3. Display lines beginning with the row + 1 found in step 1 to the
17898 row found in step 2 or, if step 2 didn't find a row, to the end of
17899 the window.
17901 4. If cursor is not known to appear on the window, give up.
17903 5. If display stopped at the row found in step 2, scroll the
17904 display and current matrix as needed.
17906 6. Maybe display some lines at the end of W, if we must. This can
17907 happen under various circumstances, like a partially visible line
17908 becoming fully visible, or because newly displayed lines are displayed
17909 in smaller font sizes.
17911 7. Update W's window end information. */
17913 static int
17914 try_window_id (struct window *w)
17916 struct frame *f = XFRAME (w->frame);
17917 struct glyph_matrix *current_matrix = w->current_matrix;
17918 struct glyph_matrix *desired_matrix = w->desired_matrix;
17919 struct glyph_row *last_unchanged_at_beg_row;
17920 struct glyph_row *first_unchanged_at_end_row;
17921 struct glyph_row *row;
17922 struct glyph_row *bottom_row;
17923 int bottom_vpos;
17924 struct it it;
17925 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17926 int dvpos, dy;
17927 struct text_pos start_pos;
17928 struct run run;
17929 int first_unchanged_at_end_vpos = 0;
17930 struct glyph_row *last_text_row, *last_text_row_at_end;
17931 struct text_pos start;
17932 ptrdiff_t first_changed_charpos, last_changed_charpos;
17934 #ifdef GLYPH_DEBUG
17935 if (inhibit_try_window_id)
17936 return 0;
17937 #endif
17939 #ifdef HAVE_XWIDGETS_xxx
17940 //maybe needed for proper xwidget movement
17941 printf("try_window_id\n");
17942 return -1;
17943 #endif
17946 /* This is handy for debugging. */
17947 #if 0
17948 #define GIVE_UP(X) \
17949 do { \
17950 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17951 return 0; \
17952 } while (0)
17953 #else
17954 #define GIVE_UP(X) return 0
17955 #endif
17957 SET_TEXT_POS_FROM_MARKER (start, w->start);
17959 /* Don't use this for mini-windows because these can show
17960 messages and mini-buffers, and we don't handle that here. */
17961 if (MINI_WINDOW_P (w))
17962 GIVE_UP (1);
17964 /* This flag is used to prevent redisplay optimizations. */
17965 if (windows_or_buffers_changed || f->cursor_type_changed)
17966 GIVE_UP (2);
17968 /* This function's optimizations cannot be used if overlays have
17969 changed in the buffer displayed by the window, so give up if they
17970 have. */
17971 if (w->last_overlay_modified != OVERLAY_MODIFF)
17972 GIVE_UP (21);
17974 /* Verify that narrowing has not changed.
17975 Also verify that we were not told to prevent redisplay optimizations.
17976 It would be nice to further
17977 reduce the number of cases where this prevents try_window_id. */
17978 if (current_buffer->clip_changed
17979 || current_buffer->prevent_redisplay_optimizations_p)
17980 GIVE_UP (3);
17982 /* Window must either use window-based redisplay or be full width. */
17983 if (!FRAME_WINDOW_P (f)
17984 && (!FRAME_LINE_INS_DEL_OK (f)
17985 || !WINDOW_FULL_WIDTH_P (w)))
17986 GIVE_UP (4);
17988 /* Give up if point is known NOT to appear in W. */
17989 if (PT < CHARPOS (start))
17990 GIVE_UP (5);
17992 /* Another way to prevent redisplay optimizations. */
17993 if (w->last_modified == 0)
17994 GIVE_UP (6);
17996 /* Verify that window is not hscrolled. */
17997 if (w->hscroll != 0)
17998 GIVE_UP (7);
18000 /* Verify that display wasn't paused. */
18001 if (!w->window_end_valid)
18002 GIVE_UP (8);
18004 /* Likewise if highlighting trailing whitespace. */
18005 if (!NILP (Vshow_trailing_whitespace))
18006 GIVE_UP (11);
18008 /* Can't use this if overlay arrow position and/or string have
18009 changed. */
18010 if (overlay_arrows_changed_p ())
18011 GIVE_UP (12);
18013 /* When word-wrap is on, adding a space to the first word of a
18014 wrapped line can change the wrap position, altering the line
18015 above it. It might be worthwhile to handle this more
18016 intelligently, but for now just redisplay from scratch. */
18017 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
18018 GIVE_UP (21);
18020 /* Under bidi reordering, adding or deleting a character in the
18021 beginning of a paragraph, before the first strong directional
18022 character, can change the base direction of the paragraph (unless
18023 the buffer specifies a fixed paragraph direction), which will
18024 require to redisplay the whole paragraph. It might be worthwhile
18025 to find the paragraph limits and widen the range of redisplayed
18026 lines to that, but for now just give up this optimization and
18027 redisplay from scratch. */
18028 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
18029 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
18030 GIVE_UP (22);
18032 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
18033 only if buffer has really changed. The reason is that the gap is
18034 initially at Z for freshly visited files. The code below would
18035 set end_unchanged to 0 in that case. */
18036 if (MODIFF > SAVE_MODIFF
18037 /* This seems to happen sometimes after saving a buffer. */
18038 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
18040 if (GPT - BEG < BEG_UNCHANGED)
18041 BEG_UNCHANGED = GPT - BEG;
18042 if (Z - GPT < END_UNCHANGED)
18043 END_UNCHANGED = Z - GPT;
18046 /* The position of the first and last character that has been changed. */
18047 first_changed_charpos = BEG + BEG_UNCHANGED;
18048 last_changed_charpos = Z - END_UNCHANGED;
18050 /* If window starts after a line end, and the last change is in
18051 front of that newline, then changes don't affect the display.
18052 This case happens with stealth-fontification. Note that although
18053 the display is unchanged, glyph positions in the matrix have to
18054 be adjusted, of course. */
18055 row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
18056 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
18057 && ((last_changed_charpos < CHARPOS (start)
18058 && CHARPOS (start) == BEGV)
18059 || (last_changed_charpos < CHARPOS (start) - 1
18060 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
18062 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
18063 struct glyph_row *r0;
18065 /* Compute how many chars/bytes have been added to or removed
18066 from the buffer. */
18067 Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
18068 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
18069 Z_delta = Z - Z_old;
18070 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
18072 /* Give up if PT is not in the window. Note that it already has
18073 been checked at the start of try_window_id that PT is not in
18074 front of the window start. */
18075 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
18076 GIVE_UP (13);
18078 /* If window start is unchanged, we can reuse the whole matrix
18079 as is, after adjusting glyph positions. No need to compute
18080 the window end again, since its offset from Z hasn't changed. */
18081 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18082 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
18083 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
18084 /* PT must not be in a partially visible line. */
18085 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
18086 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18088 /* Adjust positions in the glyph matrix. */
18089 if (Z_delta || Z_delta_bytes)
18091 struct glyph_row *r1
18092 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18093 increment_matrix_positions (w->current_matrix,
18094 MATRIX_ROW_VPOS (r0, current_matrix),
18095 MATRIX_ROW_VPOS (r1, current_matrix),
18096 Z_delta, Z_delta_bytes);
18099 /* Set the cursor. */
18100 row = row_containing_pos (w, PT, r0, NULL, 0);
18101 if (row)
18102 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18103 return 1;
18107 /* Handle the case that changes are all below what is displayed in
18108 the window, and that PT is in the window. This shortcut cannot
18109 be taken if ZV is visible in the window, and text has been added
18110 there that is visible in the window. */
18111 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
18112 /* ZV is not visible in the window, or there are no
18113 changes at ZV, actually. */
18114 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
18115 || first_changed_charpos == last_changed_charpos))
18117 struct glyph_row *r0;
18119 /* Give up if PT is not in the window. Note that it already has
18120 been checked at the start of try_window_id that PT is not in
18121 front of the window start. */
18122 if (PT >= MATRIX_ROW_END_CHARPOS (row))
18123 GIVE_UP (14);
18125 /* If window start is unchanged, we can reuse the whole matrix
18126 as is, without changing glyph positions since no text has
18127 been added/removed in front of the window end. */
18128 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
18129 if (TEXT_POS_EQUAL_P (start, r0->minpos)
18130 /* PT must not be in a partially visible line. */
18131 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
18132 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
18134 /* We have to compute the window end anew since text
18135 could have been added/removed after it. */
18136 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18137 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18139 /* Set the cursor. */
18140 row = row_containing_pos (w, PT, r0, NULL, 0);
18141 if (row)
18142 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
18143 return 2;
18147 /* Give up if window start is in the changed area.
18149 The condition used to read
18151 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
18153 but why that was tested escapes me at the moment. */
18154 if (CHARPOS (start) >= first_changed_charpos
18155 && CHARPOS (start) <= last_changed_charpos)
18156 GIVE_UP (15);
18158 /* Check that window start agrees with the start of the first glyph
18159 row in its current matrix. Check this after we know the window
18160 start is not in changed text, otherwise positions would not be
18161 comparable. */
18162 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
18163 if (!TEXT_POS_EQUAL_P (start, row->minpos))
18164 GIVE_UP (16);
18166 /* Give up if the window ends in strings. Overlay strings
18167 at the end are difficult to handle, so don't try. */
18168 row = MATRIX_ROW (current_matrix, w->window_end_vpos);
18169 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
18170 GIVE_UP (20);
18172 /* Compute the position at which we have to start displaying new
18173 lines. Some of the lines at the top of the window might be
18174 reusable because they are not displaying changed text. Find the
18175 last row in W's current matrix not affected by changes at the
18176 start of current_buffer. Value is null if changes start in the
18177 first line of window. */
18178 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
18179 if (last_unchanged_at_beg_row)
18181 /* Avoid starting to display in the middle of a character, a TAB
18182 for instance. This is easier than to set up the iterator
18183 exactly, and it's not a frequent case, so the additional
18184 effort wouldn't really pay off. */
18185 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
18186 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
18187 && last_unchanged_at_beg_row > w->current_matrix->rows)
18188 --last_unchanged_at_beg_row;
18190 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
18191 GIVE_UP (17);
18193 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
18194 GIVE_UP (18);
18195 start_pos = it.current.pos;
18197 /* Start displaying new lines in the desired matrix at the same
18198 vpos we would use in the current matrix, i.e. below
18199 last_unchanged_at_beg_row. */
18200 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
18201 current_matrix);
18202 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18203 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
18205 eassert (it.hpos == 0 && it.current_x == 0);
18207 else
18209 /* There are no reusable lines at the start of the window.
18210 Start displaying in the first text line. */
18211 start_display (&it, w, start);
18212 it.vpos = it.first_vpos;
18213 start_pos = it.current.pos;
18216 /* Find the first row that is not affected by changes at the end of
18217 the buffer. Value will be null if there is no unchanged row, in
18218 which case we must redisplay to the end of the window. delta
18219 will be set to the value by which buffer positions beginning with
18220 first_unchanged_at_end_row have to be adjusted due to text
18221 changes. */
18222 first_unchanged_at_end_row
18223 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
18224 IF_DEBUG (debug_delta = delta);
18225 IF_DEBUG (debug_delta_bytes = delta_bytes);
18227 /* Set stop_pos to the buffer position up to which we will have to
18228 display new lines. If first_unchanged_at_end_row != NULL, this
18229 is the buffer position of the start of the line displayed in that
18230 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
18231 that we don't stop at a buffer position. */
18232 stop_pos = 0;
18233 if (first_unchanged_at_end_row)
18235 eassert (last_unchanged_at_beg_row == NULL
18236 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
18238 /* If this is a continuation line, move forward to the next one
18239 that isn't. Changes in lines above affect this line.
18240 Caution: this may move first_unchanged_at_end_row to a row
18241 not displaying text. */
18242 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
18243 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18244 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18245 < it.last_visible_y))
18246 ++first_unchanged_at_end_row;
18248 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
18249 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
18250 >= it.last_visible_y))
18251 first_unchanged_at_end_row = NULL;
18252 else
18254 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
18255 + delta);
18256 first_unchanged_at_end_vpos
18257 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
18258 eassert (stop_pos >= Z - END_UNCHANGED);
18261 else if (last_unchanged_at_beg_row == NULL)
18262 GIVE_UP (19);
18265 #ifdef GLYPH_DEBUG
18267 /* Either there is no unchanged row at the end, or the one we have
18268 now displays text. This is a necessary condition for the window
18269 end pos calculation at the end of this function. */
18270 eassert (first_unchanged_at_end_row == NULL
18271 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18273 debug_last_unchanged_at_beg_vpos
18274 = (last_unchanged_at_beg_row
18275 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
18276 : -1);
18277 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
18279 #endif /* GLYPH_DEBUG */
18282 /* Display new lines. Set last_text_row to the last new line
18283 displayed which has text on it, i.e. might end up as being the
18284 line where the window_end_vpos is. */
18285 w->cursor.vpos = -1;
18286 last_text_row = NULL;
18287 overlay_arrow_seen = 0;
18288 if (it.current_y < it.last_visible_y
18289 && !f->fonts_changed
18290 && (first_unchanged_at_end_row == NULL
18291 || IT_CHARPOS (it) < stop_pos))
18292 it.glyph_row->reversed_p = false;
18293 while (it.current_y < it.last_visible_y
18294 && !f->fonts_changed
18295 && (first_unchanged_at_end_row == NULL
18296 || IT_CHARPOS (it) < stop_pos))
18298 if (display_line (&it))
18299 last_text_row = it.glyph_row - 1;
18302 if (f->fonts_changed)
18303 return -1;
18306 /* Compute differences in buffer positions, y-positions etc. for
18307 lines reused at the bottom of the window. Compute what we can
18308 scroll. */
18309 if (first_unchanged_at_end_row
18310 /* No lines reused because we displayed everything up to the
18311 bottom of the window. */
18312 && it.current_y < it.last_visible_y)
18314 dvpos = (it.vpos
18315 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
18316 current_matrix));
18317 dy = it.current_y - first_unchanged_at_end_row->y;
18318 run.current_y = first_unchanged_at_end_row->y;
18319 run.desired_y = run.current_y + dy;
18320 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
18322 else
18324 delta = delta_bytes = dvpos = dy
18325 = run.current_y = run.desired_y = run.height = 0;
18326 first_unchanged_at_end_row = NULL;
18328 IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
18331 /* Find the cursor if not already found. We have to decide whether
18332 PT will appear on this window (it sometimes doesn't, but this is
18333 not a very frequent case.) This decision has to be made before
18334 the current matrix is altered. A value of cursor.vpos < 0 means
18335 that PT is either in one of the lines beginning at
18336 first_unchanged_at_end_row or below the window. Don't care for
18337 lines that might be displayed later at the window end; as
18338 mentioned, this is not a frequent case. */
18339 if (w->cursor.vpos < 0)
18341 /* Cursor in unchanged rows at the top? */
18342 if (PT < CHARPOS (start_pos)
18343 && last_unchanged_at_beg_row)
18345 row = row_containing_pos (w, PT,
18346 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
18347 last_unchanged_at_beg_row + 1, 0);
18348 if (row)
18349 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
18352 /* Start from first_unchanged_at_end_row looking for PT. */
18353 else if (first_unchanged_at_end_row)
18355 row = row_containing_pos (w, PT - delta,
18356 first_unchanged_at_end_row, NULL, 0);
18357 if (row)
18358 set_cursor_from_row (w, row, w->current_matrix, delta,
18359 delta_bytes, dy, dvpos);
18362 /* Give up if cursor was not found. */
18363 if (w->cursor.vpos < 0)
18365 clear_glyph_matrix (w->desired_matrix);
18366 return -1;
18370 /* Don't let the cursor end in the scroll margins. */
18372 int this_scroll_margin, cursor_height;
18373 int frame_line_height = default_line_pixel_height (w);
18374 int window_total_lines
18375 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
18377 this_scroll_margin =
18378 max (0, min (scroll_margin, window_total_lines / 4));
18379 this_scroll_margin *= frame_line_height;
18380 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
18382 if ((w->cursor.y < this_scroll_margin
18383 && CHARPOS (start) > BEGV)
18384 /* Old redisplay didn't take scroll margin into account at the bottom,
18385 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
18386 || (w->cursor.y + (make_cursor_line_fully_visible_p
18387 ? cursor_height + this_scroll_margin
18388 : 1)) > it.last_visible_y)
18390 w->cursor.vpos = -1;
18391 clear_glyph_matrix (w->desired_matrix);
18392 return -1;
18396 /* Scroll the display. Do it before changing the current matrix so
18397 that xterm.c doesn't get confused about where the cursor glyph is
18398 found. */
18399 if (dy && run.height)
18401 update_begin (f);
18403 if (FRAME_WINDOW_P (f))
18405 FRAME_RIF (f)->update_window_begin_hook (w);
18406 FRAME_RIF (f)->clear_window_mouse_face (w);
18407 FRAME_RIF (f)->scroll_run_hook (w, &run);
18408 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
18410 else
18412 /* Terminal frame. In this case, dvpos gives the number of
18413 lines to scroll by; dvpos < 0 means scroll up. */
18414 int from_vpos
18415 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
18416 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
18417 int end = (WINDOW_TOP_EDGE_LINE (w)
18418 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
18419 + window_internal_height (w));
18421 #if defined (HAVE_GPM) || defined (MSDOS)
18422 x_clear_window_mouse_face (w);
18423 #endif
18424 /* Perform the operation on the screen. */
18425 if (dvpos > 0)
18427 /* Scroll last_unchanged_at_beg_row to the end of the
18428 window down dvpos lines. */
18429 set_terminal_window (f, end);
18431 /* On dumb terminals delete dvpos lines at the end
18432 before inserting dvpos empty lines. */
18433 if (!FRAME_SCROLL_REGION_OK (f))
18434 ins_del_lines (f, end - dvpos, -dvpos);
18436 /* Insert dvpos empty lines in front of
18437 last_unchanged_at_beg_row. */
18438 ins_del_lines (f, from, dvpos);
18440 else if (dvpos < 0)
18442 /* Scroll up last_unchanged_at_beg_vpos to the end of
18443 the window to last_unchanged_at_beg_vpos - |dvpos|. */
18444 set_terminal_window (f, end);
18446 /* Delete dvpos lines in front of
18447 last_unchanged_at_beg_vpos. ins_del_lines will set
18448 the cursor to the given vpos and emit |dvpos| delete
18449 line sequences. */
18450 ins_del_lines (f, from + dvpos, dvpos);
18452 /* On a dumb terminal insert dvpos empty lines at the
18453 end. */
18454 if (!FRAME_SCROLL_REGION_OK (f))
18455 ins_del_lines (f, end + dvpos, -dvpos);
18458 set_terminal_window (f, 0);
18461 update_end (f);
18464 /* Shift reused rows of the current matrix to the right position.
18465 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
18466 text. */
18467 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
18468 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
18469 if (dvpos < 0)
18471 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
18472 bottom_vpos, dvpos);
18473 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
18474 bottom_vpos);
18476 else if (dvpos > 0)
18478 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
18479 bottom_vpos, dvpos);
18480 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
18481 first_unchanged_at_end_vpos + dvpos);
18484 /* For frame-based redisplay, make sure that current frame and window
18485 matrix are in sync with respect to glyph memory. */
18486 if (!FRAME_WINDOW_P (f))
18487 sync_frame_with_window_matrix_rows (w);
18489 /* Adjust buffer positions in reused rows. */
18490 if (delta || delta_bytes)
18491 increment_matrix_positions (current_matrix,
18492 first_unchanged_at_end_vpos + dvpos,
18493 bottom_vpos, delta, delta_bytes);
18495 /* Adjust Y positions. */
18496 if (dy)
18497 shift_glyph_matrix (w, current_matrix,
18498 first_unchanged_at_end_vpos + dvpos,
18499 bottom_vpos, dy);
18501 if (first_unchanged_at_end_row)
18503 first_unchanged_at_end_row += dvpos;
18504 if (first_unchanged_at_end_row->y >= it.last_visible_y
18505 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
18506 first_unchanged_at_end_row = NULL;
18509 /* If scrolling up, there may be some lines to display at the end of
18510 the window. */
18511 last_text_row_at_end = NULL;
18512 if (dy < 0)
18514 /* Scrolling up can leave for example a partially visible line
18515 at the end of the window to be redisplayed. */
18516 /* Set last_row to the glyph row in the current matrix where the
18517 window end line is found. It has been moved up or down in
18518 the matrix by dvpos. */
18519 int last_vpos = w->window_end_vpos + dvpos;
18520 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
18522 /* If last_row is the window end line, it should display text. */
18523 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
18525 /* If window end line was partially visible before, begin
18526 displaying at that line. Otherwise begin displaying with the
18527 line following it. */
18528 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
18530 init_to_row_start (&it, w, last_row);
18531 it.vpos = last_vpos;
18532 it.current_y = last_row->y;
18534 else
18536 init_to_row_end (&it, w, last_row);
18537 it.vpos = 1 + last_vpos;
18538 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
18539 ++last_row;
18542 /* We may start in a continuation line. If so, we have to
18543 get the right continuation_lines_width and current_x. */
18544 it.continuation_lines_width = last_row->continuation_lines_width;
18545 it.hpos = it.current_x = 0;
18547 /* Display the rest of the lines at the window end. */
18548 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
18549 while (it.current_y < it.last_visible_y && !f->fonts_changed)
18551 /* Is it always sure that the display agrees with lines in
18552 the current matrix? I don't think so, so we mark rows
18553 displayed invalid in the current matrix by setting their
18554 enabled_p flag to zero. */
18555 SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
18556 if (display_line (&it))
18557 last_text_row_at_end = it.glyph_row - 1;
18561 /* Update window_end_pos and window_end_vpos. */
18562 if (first_unchanged_at_end_row && !last_text_row_at_end)
18564 /* Window end line if one of the preserved rows from the current
18565 matrix. Set row to the last row displaying text in current
18566 matrix starting at first_unchanged_at_end_row, after
18567 scrolling. */
18568 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
18569 row = find_last_row_displaying_text (w->current_matrix, &it,
18570 first_unchanged_at_end_row);
18571 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
18572 adjust_window_ends (w, row, 1);
18573 eassert (w->window_end_bytepos >= 0);
18574 IF_DEBUG (debug_method_add (w, "A"));
18576 else if (last_text_row_at_end)
18578 adjust_window_ends (w, last_text_row_at_end, 0);
18579 eassert (w->window_end_bytepos >= 0);
18580 IF_DEBUG (debug_method_add (w, "B"));
18582 else if (last_text_row)
18584 /* We have displayed either to the end of the window or at the
18585 end of the window, i.e. the last row with text is to be found
18586 in the desired matrix. */
18587 adjust_window_ends (w, last_text_row, 0);
18588 eassert (w->window_end_bytepos >= 0);
18590 else if (first_unchanged_at_end_row == NULL
18591 && last_text_row == NULL
18592 && last_text_row_at_end == NULL)
18594 /* Displayed to end of window, but no line containing text was
18595 displayed. Lines were deleted at the end of the window. */
18596 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
18597 int vpos = w->window_end_vpos;
18598 struct glyph_row *current_row = current_matrix->rows + vpos;
18599 struct glyph_row *desired_row = desired_matrix->rows + vpos;
18601 for (row = NULL;
18602 row == NULL && vpos >= first_vpos;
18603 --vpos, --current_row, --desired_row)
18605 if (desired_row->enabled_p)
18607 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
18608 row = desired_row;
18610 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
18611 row = current_row;
18614 eassert (row != NULL);
18615 w->window_end_vpos = vpos + 1;
18616 w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
18617 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18618 eassert (w->window_end_bytepos >= 0);
18619 IF_DEBUG (debug_method_add (w, "C"));
18621 else
18622 emacs_abort ();
18624 IF_DEBUG ((debug_end_pos = w->window_end_pos,
18625 debug_end_vpos = w->window_end_vpos));
18627 /* Record that display has not been completed. */
18628 w->window_end_valid = 0;
18629 w->desired_matrix->no_scrolling_p = 1;
18630 return 3;
18632 #undef GIVE_UP
18637 /***********************************************************************
18638 More debugging support
18639 ***********************************************************************/
18641 #ifdef GLYPH_DEBUG
18643 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18644 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18645 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18648 /* Dump the contents of glyph matrix MATRIX on stderr.
18650 GLYPHS 0 means don't show glyph contents.
18651 GLYPHS 1 means show glyphs in short form
18652 GLYPHS > 1 means show glyphs in long form. */
18654 void
18655 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18657 int i;
18658 for (i = 0; i < matrix->nrows; ++i)
18659 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18663 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18664 the glyph row and area where the glyph comes from. */
18666 void
18667 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18669 if (glyph->type == CHAR_GLYPH
18670 || glyph->type == GLYPHLESS_GLYPH)
18672 fprintf (stderr,
18673 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18674 glyph - row->glyphs[TEXT_AREA],
18675 (glyph->type == CHAR_GLYPH
18676 ? 'C'
18677 : 'G'),
18678 glyph->charpos,
18679 (BUFFERP (glyph->object)
18680 ? 'B'
18681 : (STRINGP (glyph->object)
18682 ? 'S'
18683 : (INTEGERP (glyph->object)
18684 ? '0'
18685 : '-'))),
18686 glyph->pixel_width,
18687 glyph->u.ch,
18688 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18689 ? glyph->u.ch
18690 : '.'),
18691 glyph->face_id,
18692 glyph->left_box_line_p,
18693 glyph->right_box_line_p);
18695 else if (glyph->type == STRETCH_GLYPH)
18697 fprintf (stderr,
18698 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18699 glyph - row->glyphs[TEXT_AREA],
18700 'S',
18701 glyph->charpos,
18702 (BUFFERP (glyph->object)
18703 ? 'B'
18704 : (STRINGP (glyph->object)
18705 ? 'S'
18706 : (INTEGERP (glyph->object)
18707 ? '0'
18708 : '-'))),
18709 glyph->pixel_width,
18711 ' ',
18712 glyph->face_id,
18713 glyph->left_box_line_p,
18714 glyph->right_box_line_p);
18716 else if (glyph->type == IMAGE_GLYPH)
18718 fprintf (stderr,
18719 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18720 glyph - row->glyphs[TEXT_AREA],
18721 'I',
18722 glyph->charpos,
18723 (BUFFERP (glyph->object)
18724 ? 'B'
18725 : (STRINGP (glyph->object)
18726 ? 'S'
18727 : (INTEGERP (glyph->object)
18728 ? '0'
18729 : '-'))),
18730 glyph->pixel_width,
18731 glyph->u.img_id,
18732 '.',
18733 glyph->face_id,
18734 glyph->left_box_line_p,
18735 glyph->right_box_line_p);
18737 else if (glyph->type == COMPOSITE_GLYPH)
18739 fprintf (stderr,
18740 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18741 glyph - row->glyphs[TEXT_AREA],
18742 '+',
18743 glyph->charpos,
18744 (BUFFERP (glyph->object)
18745 ? 'B'
18746 : (STRINGP (glyph->object)
18747 ? 'S'
18748 : (INTEGERP (glyph->object)
18749 ? '0'
18750 : '-'))),
18751 glyph->pixel_width,
18752 glyph->u.cmp.id);
18753 if (glyph->u.cmp.automatic)
18754 fprintf (stderr,
18755 "[%d-%d]",
18756 glyph->slice.cmp.from, glyph->slice.cmp.to);
18757 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18758 glyph->face_id,
18759 glyph->left_box_line_p,
18760 glyph->right_box_line_p);
18762 #ifdef HAVE_XWIDGETS
18763 else if (glyph->type == XWIDGET_GLYPH)
18765 fprintf (stderr,
18766 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18767 glyph - row->glyphs[TEXT_AREA],
18768 'X',
18769 glyph->charpos,
18770 (BUFFERP (glyph->object)
18771 ? 'B'
18772 : (STRINGP (glyph->object)
18773 ? 'S'
18774 : '-')),
18775 glyph->pixel_width,
18776 glyph->u.xwidget,
18777 '.',
18778 glyph->face_id,
18779 glyph->left_box_line_p,
18780 glyph->right_box_line_p);
18782 // printf("dump xwidget glyph\n");
18784 #endif
18788 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18789 GLYPHS 0 means don't show glyph contents.
18790 GLYPHS 1 means show glyphs in short form
18791 GLYPHS > 1 means show glyphs in long form. */
18793 void
18794 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18796 if (glyphs != 1)
18798 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18799 fprintf (stderr, "==============================================================================\n");
18801 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18802 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18803 vpos,
18804 MATRIX_ROW_START_CHARPOS (row),
18805 MATRIX_ROW_END_CHARPOS (row),
18806 row->used[TEXT_AREA],
18807 row->contains_overlapping_glyphs_p,
18808 row->enabled_p,
18809 row->truncated_on_left_p,
18810 row->truncated_on_right_p,
18811 row->continued_p,
18812 MATRIX_ROW_CONTINUATION_LINE_P (row),
18813 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18814 row->ends_at_zv_p,
18815 row->fill_line_p,
18816 row->ends_in_middle_of_char_p,
18817 row->starts_in_middle_of_char_p,
18818 row->mouse_face_p,
18819 row->x,
18820 row->y,
18821 row->pixel_width,
18822 row->height,
18823 row->visible_height,
18824 row->ascent,
18825 row->phys_ascent);
18826 /* The next 3 lines should align to "Start" in the header. */
18827 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18828 row->end.overlay_string_index,
18829 row->continuation_lines_width);
18830 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18831 CHARPOS (row->start.string_pos),
18832 CHARPOS (row->end.string_pos));
18833 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18834 row->end.dpvec_index);
18837 if (glyphs > 1)
18839 int area;
18841 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18843 struct glyph *glyph = row->glyphs[area];
18844 struct glyph *glyph_end = glyph + row->used[area];
18846 /* Glyph for a line end in text. */
18847 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18848 ++glyph_end;
18850 if (glyph < glyph_end)
18851 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18853 for (; glyph < glyph_end; ++glyph)
18854 dump_glyph (row, glyph, area);
18857 else if (glyphs == 1)
18859 int area;
18860 char s[SHRT_MAX + 4];
18862 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18864 int i;
18866 for (i = 0; i < row->used[area]; ++i)
18868 struct glyph *glyph = row->glyphs[area] + i;
18869 if (i == row->used[area] - 1
18870 && area == TEXT_AREA
18871 && INTEGERP (glyph->object)
18872 && glyph->type == CHAR_GLYPH
18873 && glyph->u.ch == ' ')
18875 strcpy (&s[i], "[\\n]");
18876 i += 4;
18878 else if (glyph->type == CHAR_GLYPH
18879 && glyph->u.ch < 0x80
18880 && glyph->u.ch >= ' ')
18881 s[i] = glyph->u.ch;
18882 else
18883 s[i] = '.';
18886 s[i] = '\0';
18887 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18893 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18894 Sdump_glyph_matrix, 0, 1, "p",
18895 doc: /* Dump the current matrix of the selected window to stderr.
18896 Shows contents of glyph row structures. With non-nil
18897 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18898 glyphs in short form, otherwise show glyphs in long form.
18900 Interactively, no argument means show glyphs in short form;
18901 with numeric argument, its value is passed as the GLYPHS flag. */)
18902 (Lisp_Object glyphs)
18904 struct window *w = XWINDOW (selected_window);
18905 struct buffer *buffer = XBUFFER (w->contents);
18907 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18908 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18909 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18910 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18911 fprintf (stderr, "=============================================\n");
18912 dump_glyph_matrix (w->current_matrix,
18913 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18914 return Qnil;
18918 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18919 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
18920 Only text-mode frames have frame glyph matrices. */)
18921 (void)
18923 struct frame *f = XFRAME (selected_frame);
18925 if (f->current_matrix)
18926 dump_glyph_matrix (f->current_matrix, 1);
18927 else
18928 fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
18929 return Qnil;
18933 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18934 doc: /* Dump glyph row ROW to stderr.
18935 GLYPH 0 means don't dump glyphs.
18936 GLYPH 1 means dump glyphs in short form.
18937 GLYPH > 1 or omitted means dump glyphs in long form. */)
18938 (Lisp_Object row, Lisp_Object glyphs)
18940 struct glyph_matrix *matrix;
18941 EMACS_INT vpos;
18943 CHECK_NUMBER (row);
18944 matrix = XWINDOW (selected_window)->current_matrix;
18945 vpos = XINT (row);
18946 if (vpos >= 0 && vpos < matrix->nrows)
18947 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18948 vpos,
18949 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18950 return Qnil;
18954 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18955 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18956 GLYPH 0 means don't dump glyphs.
18957 GLYPH 1 means dump glyphs in short form.
18958 GLYPH > 1 or omitted means dump glyphs in long form.
18960 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
18961 do nothing. */)
18962 (Lisp_Object row, Lisp_Object glyphs)
18964 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
18965 struct frame *sf = SELECTED_FRAME ();
18966 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18967 EMACS_INT vpos;
18969 CHECK_NUMBER (row);
18970 vpos = XINT (row);
18971 if (vpos >= 0 && vpos < m->nrows)
18972 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18973 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18974 #endif
18975 return Qnil;
18979 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18980 doc: /* Toggle tracing of redisplay.
18981 With ARG, turn tracing on if and only if ARG is positive. */)
18982 (Lisp_Object arg)
18984 if (NILP (arg))
18985 trace_redisplay_p = !trace_redisplay_p;
18986 else
18988 arg = Fprefix_numeric_value (arg);
18989 trace_redisplay_p = XINT (arg) > 0;
18992 return Qnil;
18996 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18997 doc: /* Like `format', but print result to stderr.
18998 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18999 (ptrdiff_t nargs, Lisp_Object *args)
19001 Lisp_Object s = Fformat (nargs, args);
19002 fprintf (stderr, "%s", SDATA (s));
19003 return Qnil;
19006 #endif /* GLYPH_DEBUG */
19010 /***********************************************************************
19011 Building Desired Matrix Rows
19012 ***********************************************************************/
19014 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
19015 Used for non-window-redisplay windows, and for windows w/o left fringe. */
19017 static struct glyph_row *
19018 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
19020 struct frame *f = XFRAME (WINDOW_FRAME (w));
19021 struct buffer *buffer = XBUFFER (w->contents);
19022 struct buffer *old = current_buffer;
19023 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
19024 ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
19025 const unsigned char *arrow_end = arrow_string + arrow_len;
19026 const unsigned char *p;
19027 struct it it;
19028 bool multibyte_p;
19029 int n_glyphs_before;
19031 set_buffer_temp (buffer);
19032 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
19033 scratch_glyph_row.reversed_p = false;
19034 it.glyph_row->used[TEXT_AREA] = 0;
19035 SET_TEXT_POS (it.position, 0, 0);
19037 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
19038 p = arrow_string;
19039 while (p < arrow_end)
19041 Lisp_Object face, ilisp;
19043 /* Get the next character. */
19044 if (multibyte_p)
19045 it.c = it.char_to_display = string_char_and_length (p, &it.len);
19046 else
19048 it.c = it.char_to_display = *p, it.len = 1;
19049 if (! ASCII_CHAR_P (it.c))
19050 it.char_to_display = BYTE8_TO_CHAR (it.c);
19052 p += it.len;
19054 /* Get its face. */
19055 ilisp = make_number (p - arrow_string);
19056 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
19057 it.face_id = compute_char_face (f, it.char_to_display, face);
19059 /* Compute its width, get its glyphs. */
19060 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
19061 SET_TEXT_POS (it.position, -1, -1);
19062 PRODUCE_GLYPHS (&it);
19064 /* If this character doesn't fit any more in the line, we have
19065 to remove some glyphs. */
19066 if (it.current_x > it.last_visible_x)
19068 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
19069 break;
19073 set_buffer_temp (old);
19074 return it.glyph_row;
19078 /* Insert truncation glyphs at the start of IT->glyph_row. Which
19079 glyphs to insert is determined by produce_special_glyphs. */
19081 static void
19082 insert_left_trunc_glyphs (struct it *it)
19084 struct it truncate_it;
19085 struct glyph *from, *end, *to, *toend;
19087 eassert (!FRAME_WINDOW_P (it->f)
19088 || (!it->glyph_row->reversed_p
19089 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19090 || (it->glyph_row->reversed_p
19091 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
19093 /* Get the truncation glyphs. */
19094 truncate_it = *it;
19095 truncate_it.current_x = 0;
19096 truncate_it.face_id = DEFAULT_FACE_ID;
19097 truncate_it.glyph_row = &scratch_glyph_row;
19098 truncate_it.area = TEXT_AREA;
19099 truncate_it.glyph_row->used[TEXT_AREA] = 0;
19100 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
19101 truncate_it.object = make_number (0);
19102 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
19104 /* Overwrite glyphs from IT with truncation glyphs. */
19105 if (!it->glyph_row->reversed_p)
19107 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19109 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19110 end = from + tused;
19111 to = it->glyph_row->glyphs[TEXT_AREA];
19112 toend = to + it->glyph_row->used[TEXT_AREA];
19113 if (FRAME_WINDOW_P (it->f))
19115 /* On GUI frames, when variable-size fonts are displayed,
19116 the truncation glyphs may need more pixels than the row's
19117 glyphs they overwrite. We overwrite more glyphs to free
19118 enough screen real estate, and enlarge the stretch glyph
19119 on the right (see display_line), if there is one, to
19120 preserve the screen position of the truncation glyphs on
19121 the right. */
19122 int w = 0;
19123 struct glyph *g = to;
19124 short used;
19126 /* The first glyph could be partially visible, in which case
19127 it->glyph_row->x will be negative. But we want the left
19128 truncation glyphs to be aligned at the left margin of the
19129 window, so we override the x coordinate at which the row
19130 will begin. */
19131 it->glyph_row->x = 0;
19132 while (g < toend && w < it->truncation_pixel_width)
19134 w += g->pixel_width;
19135 ++g;
19137 if (g - to - tused > 0)
19139 memmove (to + tused, g, (toend - g) * sizeof(*g));
19140 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
19142 used = it->glyph_row->used[TEXT_AREA];
19143 if (it->glyph_row->truncated_on_right_p
19144 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
19145 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
19146 == STRETCH_GLYPH)
19148 int extra = w - it->truncation_pixel_width;
19150 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
19154 while (from < end)
19155 *to++ = *from++;
19157 /* There may be padding glyphs left over. Overwrite them too. */
19158 if (!FRAME_WINDOW_P (it->f))
19160 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
19162 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
19163 while (from < end)
19164 *to++ = *from++;
19168 if (to > toend)
19169 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
19171 else
19173 short tused = truncate_it.glyph_row->used[TEXT_AREA];
19175 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
19176 that back to front. */
19177 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
19178 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19179 toend = it->glyph_row->glyphs[TEXT_AREA];
19180 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
19181 if (FRAME_WINDOW_P (it->f))
19183 int w = 0;
19184 struct glyph *g = to;
19186 while (g >= toend && w < it->truncation_pixel_width)
19188 w += g->pixel_width;
19189 --g;
19191 if (to - g - tused > 0)
19192 to = g + tused;
19193 if (it->glyph_row->truncated_on_right_p
19194 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
19195 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
19197 int extra = w - it->truncation_pixel_width;
19199 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
19203 while (from >= end && to >= toend)
19204 *to-- = *from--;
19205 if (!FRAME_WINDOW_P (it->f))
19207 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
19209 from =
19210 truncate_it.glyph_row->glyphs[TEXT_AREA]
19211 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
19212 while (from >= end && to >= toend)
19213 *to-- = *from--;
19216 if (from >= end)
19218 /* Need to free some room before prepending additional
19219 glyphs. */
19220 int move_by = from - end + 1;
19221 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
19222 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
19224 for ( ; g >= g0; g--)
19225 g[move_by] = *g;
19226 while (from >= end)
19227 *to-- = *from--;
19228 it->glyph_row->used[TEXT_AREA] += move_by;
19233 /* Compute the hash code for ROW. */
19234 unsigned
19235 row_hash (struct glyph_row *row)
19237 int area, k;
19238 unsigned hashval = 0;
19240 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
19241 for (k = 0; k < row->used[area]; ++k)
19242 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
19243 + row->glyphs[area][k].u.val
19244 + row->glyphs[area][k].face_id
19245 + row->glyphs[area][k].padding_p
19246 + (row->glyphs[area][k].type << 2));
19248 return hashval;
19251 /* Compute the pixel height and width of IT->glyph_row.
19253 Most of the time, ascent and height of a display line will be equal
19254 to the max_ascent and max_height values of the display iterator
19255 structure. This is not the case if
19257 1. We hit ZV without displaying anything. In this case, max_ascent
19258 and max_height will be zero.
19260 2. We have some glyphs that don't contribute to the line height.
19261 (The glyph row flag contributes_to_line_height_p is for future
19262 pixmap extensions).
19264 The first case is easily covered by using default values because in
19265 these cases, the line height does not really matter, except that it
19266 must not be zero. */
19268 static void
19269 compute_line_metrics (struct it *it)
19271 struct glyph_row *row = it->glyph_row;
19273 if (FRAME_WINDOW_P (it->f))
19275 int i, min_y, max_y;
19277 /* The line may consist of one space only, that was added to
19278 place the cursor on it. If so, the row's height hasn't been
19279 computed yet. */
19280 if (row->height == 0)
19282 if (it->max_ascent + it->max_descent == 0)
19283 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
19284 row->ascent = it->max_ascent;
19285 row->height = it->max_ascent + it->max_descent;
19286 row->phys_ascent = it->max_phys_ascent;
19287 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19288 row->extra_line_spacing = it->max_extra_line_spacing;
19291 /* Compute the width of this line. */
19292 row->pixel_width = row->x;
19293 for (i = 0; i < row->used[TEXT_AREA]; ++i)
19294 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
19296 eassert (row->pixel_width >= 0);
19297 eassert (row->ascent >= 0 && row->height > 0);
19299 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
19300 || MATRIX_ROW_OVERLAPS_PRED_P (row));
19302 /* If first line's physical ascent is larger than its logical
19303 ascent, use the physical ascent, and make the row taller.
19304 This makes accented characters fully visible. */
19305 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
19306 && row->phys_ascent > row->ascent)
19308 row->height += row->phys_ascent - row->ascent;
19309 row->ascent = row->phys_ascent;
19312 /* Compute how much of the line is visible. */
19313 row->visible_height = row->height;
19315 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
19316 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
19318 if (row->y < min_y)
19319 row->visible_height -= min_y - row->y;
19320 if (row->y + row->height > max_y)
19321 row->visible_height -= row->y + row->height - max_y;
19323 else
19325 row->pixel_width = row->used[TEXT_AREA];
19326 if (row->continued_p)
19327 row->pixel_width -= it->continuation_pixel_width;
19328 else if (row->truncated_on_right_p)
19329 row->pixel_width -= it->truncation_pixel_width;
19330 row->ascent = row->phys_ascent = 0;
19331 row->height = row->phys_height = row->visible_height = 1;
19332 row->extra_line_spacing = 0;
19335 /* Compute a hash code for this row. */
19336 row->hash = row_hash (row);
19338 it->max_ascent = it->max_descent = 0;
19339 it->max_phys_ascent = it->max_phys_descent = 0;
19343 /* Append one space to the glyph row of iterator IT if doing a
19344 window-based redisplay. The space has the same face as
19345 IT->face_id. Value is non-zero if a space was added.
19347 This function is called to make sure that there is always one glyph
19348 at the end of a glyph row that the cursor can be set on under
19349 window-systems. (If there weren't such a glyph we would not know
19350 how wide and tall a box cursor should be displayed).
19352 At the same time this space let's a nicely handle clearing to the
19353 end of the line if the row ends in italic text. */
19355 static int
19356 append_space_for_newline (struct it *it, int default_face_p)
19358 if (FRAME_WINDOW_P (it->f))
19360 int n = it->glyph_row->used[TEXT_AREA];
19362 if (it->glyph_row->glyphs[TEXT_AREA] + n
19363 < it->glyph_row->glyphs[1 + TEXT_AREA])
19365 /* Save some values that must not be changed.
19366 Must save IT->c and IT->len because otherwise
19367 ITERATOR_AT_END_P wouldn't work anymore after
19368 append_space_for_newline has been called. */
19369 enum display_element_type saved_what = it->what;
19370 int saved_c = it->c, saved_len = it->len;
19371 int saved_char_to_display = it->char_to_display;
19372 int saved_x = it->current_x;
19373 int saved_face_id = it->face_id;
19374 int saved_box_end = it->end_of_box_run_p;
19375 struct text_pos saved_pos;
19376 Lisp_Object saved_object;
19377 struct face *face;
19379 saved_object = it->object;
19380 saved_pos = it->position;
19382 it->what = IT_CHARACTER;
19383 memset (&it->position, 0, sizeof it->position);
19384 it->object = make_number (0);
19385 it->c = it->char_to_display = ' ';
19386 it->len = 1;
19388 /* If the default face was remapped, be sure to use the
19389 remapped face for the appended newline. */
19390 if (default_face_p)
19391 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
19392 else if (it->face_before_selective_p)
19393 it->face_id = it->saved_face_id;
19394 face = FACE_FROM_ID (it->f, it->face_id);
19395 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
19396 /* In R2L rows, we will prepend a stretch glyph that will
19397 have the end_of_box_run_p flag set for it, so there's no
19398 need for the appended newline glyph to have that flag
19399 set. */
19400 if (it->glyph_row->reversed_p
19401 /* But if the appended newline glyph goes all the way to
19402 the end of the row, there will be no stretch glyph,
19403 so leave the box flag set. */
19404 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
19405 it->end_of_box_run_p = 0;
19407 PRODUCE_GLYPHS (it);
19409 it->override_ascent = -1;
19410 it->constrain_row_ascent_descent_p = 0;
19411 it->current_x = saved_x;
19412 it->object = saved_object;
19413 it->position = saved_pos;
19414 it->what = saved_what;
19415 it->face_id = saved_face_id;
19416 it->len = saved_len;
19417 it->c = saved_c;
19418 it->char_to_display = saved_char_to_display;
19419 it->end_of_box_run_p = saved_box_end;
19420 return 1;
19424 return 0;
19428 /* Extend the face of the last glyph in the text area of IT->glyph_row
19429 to the end of the display line. Called from display_line. If the
19430 glyph row is empty, add a space glyph to it so that we know the
19431 face to draw. Set the glyph row flag fill_line_p. If the glyph
19432 row is R2L, prepend a stretch glyph to cover the empty space to the
19433 left of the leftmost glyph. */
19435 static void
19436 extend_face_to_end_of_line (struct it *it)
19438 struct face *face, *default_face;
19439 struct frame *f = it->f;
19441 /* If line is already filled, do nothing. Non window-system frames
19442 get a grace of one more ``pixel'' because their characters are
19443 1-``pixel'' wide, so they hit the equality too early. This grace
19444 is needed only for R2L rows that are not continued, to produce
19445 one extra blank where we could display the cursor. */
19446 if ((it->current_x >= it->last_visible_x
19447 + (!FRAME_WINDOW_P (f)
19448 && it->glyph_row->reversed_p
19449 && !it->glyph_row->continued_p))
19450 /* If the window has display margins, we will need to extend
19451 their face even if the text area is filled. */
19452 && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19453 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
19454 return;
19456 /* The default face, possibly remapped. */
19457 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
19459 /* Face extension extends the background and box of IT->face_id
19460 to the end of the line. If the background equals the background
19461 of the frame, we don't have to do anything. */
19462 if (it->face_before_selective_p)
19463 face = FACE_FROM_ID (f, it->saved_face_id);
19464 else
19465 face = FACE_FROM_ID (f, it->face_id);
19467 if (FRAME_WINDOW_P (f)
19468 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
19469 && face->box == FACE_NO_BOX
19470 && face->background == FRAME_BACKGROUND_PIXEL (f)
19471 #ifdef HAVE_WINDOW_SYSTEM
19472 && !face->stipple
19473 #endif
19474 && !it->glyph_row->reversed_p)
19475 return;
19477 /* Set the glyph row flag indicating that the face of the last glyph
19478 in the text area has to be drawn to the end of the text area. */
19479 it->glyph_row->fill_line_p = 1;
19481 /* If current character of IT is not ASCII, make sure we have the
19482 ASCII face. This will be automatically undone the next time
19483 get_next_display_element returns a multibyte character. Note
19484 that the character will always be single byte in unibyte
19485 text. */
19486 if (!ASCII_CHAR_P (it->c))
19488 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
19491 if (FRAME_WINDOW_P (f))
19493 /* If the row is empty, add a space with the current face of IT,
19494 so that we know which face to draw. */
19495 if (it->glyph_row->used[TEXT_AREA] == 0)
19497 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
19498 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
19499 it->glyph_row->used[TEXT_AREA] = 1;
19501 /* Mode line and the header line don't have margins, and
19502 likewise the frame's tool-bar window, if there is any. */
19503 if (!(it->glyph_row->mode_line_p
19504 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
19505 || (WINDOWP (f->tool_bar_window)
19506 && it->w == XWINDOW (f->tool_bar_window))
19507 #endif
19510 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19511 && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
19513 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
19514 it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
19515 default_face->id;
19516 it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
19518 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19519 && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
19521 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
19522 it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
19523 default_face->id;
19524 it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
19527 #ifdef HAVE_WINDOW_SYSTEM
19528 if (it->glyph_row->reversed_p)
19530 /* Prepend a stretch glyph to the row, such that the
19531 rightmost glyph will be drawn flushed all the way to the
19532 right margin of the window. The stretch glyph that will
19533 occupy the empty space, if any, to the left of the
19534 glyphs. */
19535 struct font *font = face->font ? face->font : FRAME_FONT (f);
19536 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
19537 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
19538 struct glyph *g;
19539 int row_width, stretch_ascent, stretch_width;
19540 struct text_pos saved_pos;
19541 int saved_face_id, saved_avoid_cursor, saved_box_start;
19543 for (row_width = 0, g = row_start; g < row_end; g++)
19544 row_width += g->pixel_width;
19546 /* FIXME: There are various minor display glitches in R2L
19547 rows when only one of the fringes is missing. The
19548 strange condition below produces the least bad effect. */
19549 if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
19550 == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
19551 || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
19552 stretch_width = window_box_width (it->w, TEXT_AREA);
19553 else
19554 stretch_width = it->last_visible_x - it->first_visible_x;
19555 stretch_width -= row_width;
19557 if (stretch_width > 0)
19559 stretch_ascent =
19560 (((it->ascent + it->descent)
19561 * FONT_BASE (font)) / FONT_HEIGHT (font));
19562 saved_pos = it->position;
19563 memset (&it->position, 0, sizeof it->position);
19564 saved_avoid_cursor = it->avoid_cursor_p;
19565 it->avoid_cursor_p = 1;
19566 saved_face_id = it->face_id;
19567 saved_box_start = it->start_of_box_run_p;
19568 /* The last row's stretch glyph should get the default
19569 face, to avoid painting the rest of the window with
19570 the region face, if the region ends at ZV. */
19571 if (it->glyph_row->ends_at_zv_p)
19572 it->face_id = default_face->id;
19573 else
19574 it->face_id = face->id;
19575 it->start_of_box_run_p = 0;
19576 append_stretch_glyph (it, make_number (0), stretch_width,
19577 it->ascent + it->descent, stretch_ascent);
19578 it->position = saved_pos;
19579 it->avoid_cursor_p = saved_avoid_cursor;
19580 it->face_id = saved_face_id;
19581 it->start_of_box_run_p = saved_box_start;
19583 /* If stretch_width comes out negative, it means that the
19584 last glyph is only partially visible. In R2L rows, we
19585 want the leftmost glyph to be partially visible, so we
19586 need to give the row the corresponding left offset. */
19587 if (stretch_width < 0)
19588 it->glyph_row->x = stretch_width;
19590 #endif /* HAVE_WINDOW_SYSTEM */
19592 else
19594 /* Save some values that must not be changed. */
19595 int saved_x = it->current_x;
19596 struct text_pos saved_pos;
19597 Lisp_Object saved_object;
19598 enum display_element_type saved_what = it->what;
19599 int saved_face_id = it->face_id;
19601 saved_object = it->object;
19602 saved_pos = it->position;
19604 it->what = IT_CHARACTER;
19605 memset (&it->position, 0, sizeof it->position);
19606 it->object = make_number (0);
19607 it->c = it->char_to_display = ' ';
19608 it->len = 1;
19610 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
19611 && (it->glyph_row->used[LEFT_MARGIN_AREA]
19612 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19613 && !it->glyph_row->mode_line_p
19614 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19616 struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
19617 struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
19619 for (it->current_x = 0; g < e; g++)
19620 it->current_x += g->pixel_width;
19622 it->area = LEFT_MARGIN_AREA;
19623 it->face_id = default_face->id;
19624 while (it->glyph_row->used[LEFT_MARGIN_AREA]
19625 < WINDOW_LEFT_MARGIN_WIDTH (it->w))
19627 PRODUCE_GLYPHS (it);
19628 /* term.c:produce_glyphs advances it->current_x only for
19629 TEXT_AREA. */
19630 it->current_x += it->pixel_width;
19633 it->current_x = saved_x;
19634 it->area = TEXT_AREA;
19637 /* The last row's blank glyphs should get the default face, to
19638 avoid painting the rest of the window with the region face,
19639 if the region ends at ZV. */
19640 if (it->glyph_row->ends_at_zv_p)
19641 it->face_id = default_face->id;
19642 else
19643 it->face_id = face->id;
19644 PRODUCE_GLYPHS (it);
19646 while (it->current_x <= it->last_visible_x)
19647 PRODUCE_GLYPHS (it);
19649 if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
19650 && (it->glyph_row->used[RIGHT_MARGIN_AREA]
19651 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19652 && !it->glyph_row->mode_line_p
19653 && default_face->background != FRAME_BACKGROUND_PIXEL (f))
19655 struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
19656 struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
19658 for ( ; g < e; g++)
19659 it->current_x += g->pixel_width;
19661 it->area = RIGHT_MARGIN_AREA;
19662 it->face_id = default_face->id;
19663 while (it->glyph_row->used[RIGHT_MARGIN_AREA]
19664 < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
19666 PRODUCE_GLYPHS (it);
19667 it->current_x += it->pixel_width;
19670 it->area = TEXT_AREA;
19673 /* Don't count these blanks really. It would let us insert a left
19674 truncation glyph below and make us set the cursor on them, maybe. */
19675 it->current_x = saved_x;
19676 it->object = saved_object;
19677 it->position = saved_pos;
19678 it->what = saved_what;
19679 it->face_id = saved_face_id;
19684 /* Value is non-zero if text starting at CHARPOS in current_buffer is
19685 trailing whitespace. */
19687 static int
19688 trailing_whitespace_p (ptrdiff_t charpos)
19690 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
19691 int c = 0;
19693 while (bytepos < ZV_BYTE
19694 && (c = FETCH_CHAR (bytepos),
19695 c == ' ' || c == '\t'))
19696 ++bytepos;
19698 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
19700 if (bytepos != PT_BYTE)
19701 return 1;
19703 return 0;
19707 /* Highlight trailing whitespace, if any, in ROW. */
19709 static void
19710 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
19712 int used = row->used[TEXT_AREA];
19714 if (used)
19716 struct glyph *start = row->glyphs[TEXT_AREA];
19717 struct glyph *glyph = start + used - 1;
19719 if (row->reversed_p)
19721 /* Right-to-left rows need to be processed in the opposite
19722 direction, so swap the edge pointers. */
19723 glyph = start;
19724 start = row->glyphs[TEXT_AREA] + used - 1;
19727 /* Skip over glyphs inserted to display the cursor at the
19728 end of a line, for extending the face of the last glyph
19729 to the end of the line on terminals, and for truncation
19730 and continuation glyphs. */
19731 if (!row->reversed_p)
19733 while (glyph >= start
19734 && glyph->type == CHAR_GLYPH
19735 && INTEGERP (glyph->object))
19736 --glyph;
19738 else
19740 while (glyph <= start
19741 && glyph->type == CHAR_GLYPH
19742 && INTEGERP (glyph->object))
19743 ++glyph;
19746 /* If last glyph is a space or stretch, and it's trailing
19747 whitespace, set the face of all trailing whitespace glyphs in
19748 IT->glyph_row to `trailing-whitespace'. */
19749 if ((row->reversed_p ? glyph <= start : glyph >= start)
19750 && BUFFERP (glyph->object)
19751 && (glyph->type == STRETCH_GLYPH
19752 || (glyph->type == CHAR_GLYPH
19753 && glyph->u.ch == ' '))
19754 && trailing_whitespace_p (glyph->charpos))
19756 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
19757 if (face_id < 0)
19758 return;
19760 if (!row->reversed_p)
19762 while (glyph >= start
19763 && BUFFERP (glyph->object)
19764 && (glyph->type == STRETCH_GLYPH
19765 || (glyph->type == CHAR_GLYPH
19766 && glyph->u.ch == ' ')))
19767 (glyph--)->face_id = face_id;
19769 else
19771 while (glyph <= start
19772 && BUFFERP (glyph->object)
19773 && (glyph->type == STRETCH_GLYPH
19774 || (glyph->type == CHAR_GLYPH
19775 && glyph->u.ch == ' ')))
19776 (glyph++)->face_id = face_id;
19783 /* Value is non-zero if glyph row ROW should be
19784 considered to hold the buffer position CHARPOS. */
19786 static int
19787 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
19789 int result = 1;
19791 if (charpos == CHARPOS (row->end.pos)
19792 || charpos == MATRIX_ROW_END_CHARPOS (row))
19794 /* Suppose the row ends on a string.
19795 Unless the row is continued, that means it ends on a newline
19796 in the string. If it's anything other than a display string
19797 (e.g., a before-string from an overlay), we don't want the
19798 cursor there. (This heuristic seems to give the optimal
19799 behavior for the various types of multi-line strings.)
19800 One exception: if the string has `cursor' property on one of
19801 its characters, we _do_ want the cursor there. */
19802 if (CHARPOS (row->end.string_pos) >= 0)
19804 if (row->continued_p)
19805 result = 1;
19806 else
19808 /* Check for `display' property. */
19809 struct glyph *beg = row->glyphs[TEXT_AREA];
19810 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19811 struct glyph *glyph;
19813 result = 0;
19814 for (glyph = end; glyph >= beg; --glyph)
19815 if (STRINGP (glyph->object))
19817 Lisp_Object prop
19818 = Fget_char_property (make_number (charpos),
19819 Qdisplay, Qnil);
19820 result =
19821 (!NILP (prop)
19822 && display_prop_string_p (prop, glyph->object));
19823 /* If there's a `cursor' property on one of the
19824 string's characters, this row is a cursor row,
19825 even though this is not a display string. */
19826 if (!result)
19828 Lisp_Object s = glyph->object;
19830 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19832 ptrdiff_t gpos = glyph->charpos;
19834 if (!NILP (Fget_char_property (make_number (gpos),
19835 Qcursor, s)))
19837 result = 1;
19838 break;
19842 break;
19846 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19848 /* If the row ends in middle of a real character,
19849 and the line is continued, we want the cursor here.
19850 That's because CHARPOS (ROW->end.pos) would equal
19851 PT if PT is before the character. */
19852 if (!row->ends_in_ellipsis_p)
19853 result = row->continued_p;
19854 else
19855 /* If the row ends in an ellipsis, then
19856 CHARPOS (ROW->end.pos) will equal point after the
19857 invisible text. We want that position to be displayed
19858 after the ellipsis. */
19859 result = 0;
19861 /* If the row ends at ZV, display the cursor at the end of that
19862 row instead of at the start of the row below. */
19863 else if (row->ends_at_zv_p)
19864 result = 1;
19865 else
19866 result = 0;
19869 return result;
19872 /* Value is non-zero if glyph row ROW should be
19873 used to hold the cursor. */
19875 static int
19876 cursor_row_p (struct glyph_row *row)
19878 return row_for_charpos_p (row, PT);
19883 /* Push the property PROP so that it will be rendered at the current
19884 position in IT. Return 1 if PROP was successfully pushed, 0
19885 otherwise. Called from handle_line_prefix to handle the
19886 `line-prefix' and `wrap-prefix' properties. */
19888 static int
19889 push_prefix_prop (struct it *it, Lisp_Object prop)
19891 struct text_pos pos =
19892 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19894 eassert (it->method == GET_FROM_BUFFER
19895 || it->method == GET_FROM_DISPLAY_VECTOR
19896 || it->method == GET_FROM_STRING);
19898 /* We need to save the current buffer/string position, so it will be
19899 restored by pop_it, because iterate_out_of_display_property
19900 depends on that being set correctly, but some situations leave
19901 it->position not yet set when this function is called. */
19902 push_it (it, &pos);
19904 if (STRINGP (prop))
19906 if (SCHARS (prop) == 0)
19908 pop_it (it);
19909 return 0;
19912 it->string = prop;
19913 it->string_from_prefix_prop_p = 1;
19914 it->multibyte_p = STRING_MULTIBYTE (it->string);
19915 it->current.overlay_string_index = -1;
19916 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19917 it->end_charpos = it->string_nchars = SCHARS (it->string);
19918 it->method = GET_FROM_STRING;
19919 it->stop_charpos = 0;
19920 it->prev_stop = 0;
19921 it->base_level_stop = 0;
19923 /* Force paragraph direction to be that of the parent
19924 buffer/string. */
19925 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19926 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19927 else
19928 it->paragraph_embedding = L2R;
19930 /* Set up the bidi iterator for this display string. */
19931 if (it->bidi_p)
19933 it->bidi_it.string.lstring = it->string;
19934 it->bidi_it.string.s = NULL;
19935 it->bidi_it.string.schars = it->end_charpos;
19936 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19937 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19938 it->bidi_it.string.unibyte = !it->multibyte_p;
19939 it->bidi_it.w = it->w;
19940 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19943 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19945 it->method = GET_FROM_STRETCH;
19946 it->object = prop;
19948 #ifdef HAVE_WINDOW_SYSTEM
19949 else if (IMAGEP (prop))
19951 it->what = IT_IMAGE;
19952 it->image_id = lookup_image (it->f, prop);
19953 it->method = GET_FROM_IMAGE;
19955 #endif /* HAVE_WINDOW_SYSTEM */
19956 else
19958 pop_it (it); /* bogus display property, give up */
19959 return 0;
19962 return 1;
19965 /* Return the character-property PROP at the current position in IT. */
19967 static Lisp_Object
19968 get_it_property (struct it *it, Lisp_Object prop)
19970 Lisp_Object position, object = it->object;
19972 if (STRINGP (object))
19973 position = make_number (IT_STRING_CHARPOS (*it));
19974 else if (BUFFERP (object))
19976 position = make_number (IT_CHARPOS (*it));
19977 object = it->window;
19979 else
19980 return Qnil;
19982 return Fget_char_property (position, prop, object);
19985 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19987 static void
19988 handle_line_prefix (struct it *it)
19990 Lisp_Object prefix;
19992 if (it->continuation_lines_width > 0)
19994 prefix = get_it_property (it, Qwrap_prefix);
19995 if (NILP (prefix))
19996 prefix = Vwrap_prefix;
19998 else
20000 prefix = get_it_property (it, Qline_prefix);
20001 if (NILP (prefix))
20002 prefix = Vline_prefix;
20004 if (! NILP (prefix) && push_prefix_prop (it, prefix))
20006 /* If the prefix is wider than the window, and we try to wrap
20007 it, it would acquire its own wrap prefix, and so on till the
20008 iterator stack overflows. So, don't wrap the prefix. */
20009 it->line_wrap = TRUNCATE;
20010 it->avoid_cursor_p = 1;
20016 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
20017 only for R2L lines from display_line and display_string, when they
20018 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
20019 the line/string needs to be continued on the next glyph row. */
20020 static void
20021 unproduce_glyphs (struct it *it, int n)
20023 struct glyph *glyph, *end;
20025 eassert (it->glyph_row);
20026 eassert (it->glyph_row->reversed_p);
20027 eassert (it->area == TEXT_AREA);
20028 eassert (n <= it->glyph_row->used[TEXT_AREA]);
20030 if (n > it->glyph_row->used[TEXT_AREA])
20031 n = it->glyph_row->used[TEXT_AREA];
20032 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
20033 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
20034 for ( ; glyph < end; glyph++)
20035 glyph[-n] = *glyph;
20038 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
20039 and ROW->maxpos. */
20040 static void
20041 find_row_edges (struct it *it, struct glyph_row *row,
20042 ptrdiff_t min_pos, ptrdiff_t min_bpos,
20043 ptrdiff_t max_pos, ptrdiff_t max_bpos)
20045 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20046 lines' rows is implemented for bidi-reordered rows. */
20048 /* ROW->minpos is the value of min_pos, the minimal buffer position
20049 we have in ROW, or ROW->start.pos if that is smaller. */
20050 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
20051 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
20052 else
20053 /* We didn't find buffer positions smaller than ROW->start, or
20054 didn't find _any_ valid buffer positions in any of the glyphs,
20055 so we must trust the iterator's computed positions. */
20056 row->minpos = row->start.pos;
20057 if (max_pos <= 0)
20059 max_pos = CHARPOS (it->current.pos);
20060 max_bpos = BYTEPOS (it->current.pos);
20063 /* Here are the various use-cases for ending the row, and the
20064 corresponding values for ROW->maxpos:
20066 Line ends in a newline from buffer eol_pos + 1
20067 Line is continued from buffer max_pos + 1
20068 Line is truncated on right it->current.pos
20069 Line ends in a newline from string max_pos + 1(*)
20070 (*) + 1 only when line ends in a forward scan
20071 Line is continued from string max_pos
20072 Line is continued from display vector max_pos
20073 Line is entirely from a string min_pos == max_pos
20074 Line is entirely from a display vector min_pos == max_pos
20075 Line that ends at ZV ZV
20077 If you discover other use-cases, please add them here as
20078 appropriate. */
20079 if (row->ends_at_zv_p)
20080 row->maxpos = it->current.pos;
20081 else if (row->used[TEXT_AREA])
20083 int seen_this_string = 0;
20084 struct glyph_row *r1 = row - 1;
20086 /* Did we see the same display string on the previous row? */
20087 if (STRINGP (it->object)
20088 /* this is not the first row */
20089 && row > it->w->desired_matrix->rows
20090 /* previous row is not the header line */
20091 && !r1->mode_line_p
20092 /* previous row also ends in a newline from a string */
20093 && r1->ends_in_newline_from_string_p)
20095 struct glyph *start, *end;
20097 /* Search for the last glyph of the previous row that came
20098 from buffer or string. Depending on whether the row is
20099 L2R or R2L, we need to process it front to back or the
20100 other way round. */
20101 if (!r1->reversed_p)
20103 start = r1->glyphs[TEXT_AREA];
20104 end = start + r1->used[TEXT_AREA];
20105 /* Glyphs inserted by redisplay have an integer (zero)
20106 as their object. */
20107 while (end > start
20108 && INTEGERP ((end - 1)->object)
20109 && (end - 1)->charpos <= 0)
20110 --end;
20111 if (end > start)
20113 if (EQ ((end - 1)->object, it->object))
20114 seen_this_string = 1;
20116 else
20117 /* If all the glyphs of the previous row were inserted
20118 by redisplay, it means the previous row was
20119 produced from a single newline, which is only
20120 possible if that newline came from the same string
20121 as the one which produced this ROW. */
20122 seen_this_string = 1;
20124 else
20126 end = r1->glyphs[TEXT_AREA] - 1;
20127 start = end + r1->used[TEXT_AREA];
20128 while (end < start
20129 && INTEGERP ((end + 1)->object)
20130 && (end + 1)->charpos <= 0)
20131 ++end;
20132 if (end < start)
20134 if (EQ ((end + 1)->object, it->object))
20135 seen_this_string = 1;
20137 else
20138 seen_this_string = 1;
20141 /* Take note of each display string that covers a newline only
20142 once, the first time we see it. This is for when a display
20143 string includes more than one newline in it. */
20144 if (row->ends_in_newline_from_string_p && !seen_this_string)
20146 /* If we were scanning the buffer forward when we displayed
20147 the string, we want to account for at least one buffer
20148 position that belongs to this row (position covered by
20149 the display string), so that cursor positioning will
20150 consider this row as a candidate when point is at the end
20151 of the visual line represented by this row. This is not
20152 required when scanning back, because max_pos will already
20153 have a much larger value. */
20154 if (CHARPOS (row->end.pos) > max_pos)
20155 INC_BOTH (max_pos, max_bpos);
20156 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20158 else if (CHARPOS (it->eol_pos) > 0)
20159 SET_TEXT_POS (row->maxpos,
20160 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
20161 else if (row->continued_p)
20163 /* If max_pos is different from IT's current position, it
20164 means IT->method does not belong to the display element
20165 at max_pos. However, it also means that the display
20166 element at max_pos was displayed in its entirety on this
20167 line, which is equivalent to saying that the next line
20168 starts at the next buffer position. */
20169 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
20170 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20171 else
20173 INC_BOTH (max_pos, max_bpos);
20174 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
20177 else if (row->truncated_on_right_p)
20178 /* display_line already called reseat_at_next_visible_line_start,
20179 which puts the iterator at the beginning of the next line, in
20180 the logical order. */
20181 row->maxpos = it->current.pos;
20182 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
20183 /* A line that is entirely from a string/image/stretch... */
20184 row->maxpos = row->minpos;
20185 else
20186 emacs_abort ();
20188 else
20189 row->maxpos = it->current.pos;
20192 /* Construct the glyph row IT->glyph_row in the desired matrix of
20193 IT->w from text at the current position of IT. See dispextern.h
20194 for an overview of struct it. Value is non-zero if
20195 IT->glyph_row displays text, as opposed to a line displaying ZV
20196 only. */
20198 static int
20199 display_line (struct it *it)
20201 struct glyph_row *row = it->glyph_row;
20202 Lisp_Object overlay_arrow_string;
20203 struct it wrap_it;
20204 void *wrap_data = NULL;
20205 int may_wrap = 0, wrap_x IF_LINT (= 0);
20206 int wrap_row_used = -1;
20207 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
20208 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
20209 int wrap_row_extra_line_spacing IF_LINT (= 0);
20210 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
20211 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
20212 int cvpos;
20213 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
20214 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
20215 bool pending_handle_line_prefix = false;
20217 /* We always start displaying at hpos zero even if hscrolled. */
20218 eassert (it->hpos == 0 && it->current_x == 0);
20220 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
20221 >= it->w->desired_matrix->nrows)
20223 it->w->nrows_scale_factor++;
20224 it->f->fonts_changed = 1;
20225 return 0;
20228 /* Clear the result glyph row and enable it. */
20229 prepare_desired_row (it->w, row, false);
20231 row->y = it->current_y;
20232 row->start = it->start;
20233 row->continuation_lines_width = it->continuation_lines_width;
20234 row->displays_text_p = 1;
20235 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
20236 it->starts_in_middle_of_char_p = 0;
20238 /* Arrange the overlays nicely for our purposes. Usually, we call
20239 display_line on only one line at a time, in which case this
20240 can't really hurt too much, or we call it on lines which appear
20241 one after another in the buffer, in which case all calls to
20242 recenter_overlay_lists but the first will be pretty cheap. */
20243 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
20245 /* Move over display elements that are not visible because we are
20246 hscrolled. This may stop at an x-position < IT->first_visible_x
20247 if the first glyph is partially visible or if we hit a line end. */
20248 if (it->current_x < it->first_visible_x)
20250 enum move_it_result move_result;
20252 this_line_min_pos = row->start.pos;
20253 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
20254 MOVE_TO_POS | MOVE_TO_X);
20255 /* If we are under a large hscroll, move_it_in_display_line_to
20256 could hit the end of the line without reaching
20257 it->first_visible_x. Pretend that we did reach it. This is
20258 especially important on a TTY, where we will call
20259 extend_face_to_end_of_line, which needs to know how many
20260 blank glyphs to produce. */
20261 if (it->current_x < it->first_visible_x
20262 && (move_result == MOVE_NEWLINE_OR_CR
20263 || move_result == MOVE_POS_MATCH_OR_ZV))
20264 it->current_x = it->first_visible_x;
20266 /* Record the smallest positions seen while we moved over
20267 display elements that are not visible. This is needed by
20268 redisplay_internal for optimizing the case where the cursor
20269 stays inside the same line. The rest of this function only
20270 considers positions that are actually displayed, so
20271 RECORD_MAX_MIN_POS will not otherwise record positions that
20272 are hscrolled to the left of the left edge of the window. */
20273 min_pos = CHARPOS (this_line_min_pos);
20274 min_bpos = BYTEPOS (this_line_min_pos);
20276 else if (it->area == TEXT_AREA)
20278 /* We only do this when not calling move_it_in_display_line_to
20279 above, because that function calls itself handle_line_prefix. */
20280 handle_line_prefix (it);
20282 else
20284 /* Line-prefix and wrap-prefix are always displayed in the text
20285 area. But if this is the first call to display_line after
20286 init_iterator, the iterator might have been set up to write
20287 into a marginal area, e.g. if the line begins with some
20288 display property that writes to the margins. So we need to
20289 wait with the call to handle_line_prefix until whatever
20290 writes to the margin has done its job. */
20291 pending_handle_line_prefix = true;
20294 /* Get the initial row height. This is either the height of the
20295 text hscrolled, if there is any, or zero. */
20296 row->ascent = it->max_ascent;
20297 row->height = it->max_ascent + it->max_descent;
20298 row->phys_ascent = it->max_phys_ascent;
20299 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20300 row->extra_line_spacing = it->max_extra_line_spacing;
20302 /* Utility macro to record max and min buffer positions seen until now. */
20303 #define RECORD_MAX_MIN_POS(IT) \
20304 do \
20306 int composition_p = !STRINGP ((IT)->string) \
20307 && ((IT)->what == IT_COMPOSITION); \
20308 ptrdiff_t current_pos = \
20309 composition_p ? (IT)->cmp_it.charpos \
20310 : IT_CHARPOS (*(IT)); \
20311 ptrdiff_t current_bpos = \
20312 composition_p ? CHAR_TO_BYTE (current_pos) \
20313 : IT_BYTEPOS (*(IT)); \
20314 if (current_pos < min_pos) \
20316 min_pos = current_pos; \
20317 min_bpos = current_bpos; \
20319 if (IT_CHARPOS (*it) > max_pos) \
20321 max_pos = IT_CHARPOS (*it); \
20322 max_bpos = IT_BYTEPOS (*it); \
20325 while (0)
20327 /* Loop generating characters. The loop is left with IT on the next
20328 character to display. */
20329 while (1)
20331 int n_glyphs_before, hpos_before, x_before;
20332 int x, nglyphs;
20333 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
20335 /* Retrieve the next thing to display. Value is zero if end of
20336 buffer reached. */
20337 if (!get_next_display_element (it))
20339 /* Maybe add a space at the end of this line that is used to
20340 display the cursor there under X. Set the charpos of the
20341 first glyph of blank lines not corresponding to any text
20342 to -1. */
20343 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20344 row->exact_window_width_line_p = 1;
20345 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
20346 || row->used[TEXT_AREA] == 0)
20348 row->glyphs[TEXT_AREA]->charpos = -1;
20349 row->displays_text_p = 0;
20351 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
20352 && (!MINI_WINDOW_P (it->w)
20353 || (minibuf_level && EQ (it->window, minibuf_window))))
20354 row->indicate_empty_line_p = 1;
20357 it->continuation_lines_width = 0;
20358 row->ends_at_zv_p = 1;
20359 /* A row that displays right-to-left text must always have
20360 its last face extended all the way to the end of line,
20361 even if this row ends in ZV, because we still write to
20362 the screen left to right. We also need to extend the
20363 last face if the default face is remapped to some
20364 different face, otherwise the functions that clear
20365 portions of the screen will clear with the default face's
20366 background color. */
20367 if (row->reversed_p
20368 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
20369 extend_face_to_end_of_line (it);
20370 break;
20373 /* Now, get the metrics of what we want to display. This also
20374 generates glyphs in `row' (which is IT->glyph_row). */
20375 n_glyphs_before = row->used[TEXT_AREA];
20376 x = it->current_x;
20378 /* Remember the line height so far in case the next element doesn't
20379 fit on the line. */
20380 if (it->line_wrap != TRUNCATE)
20382 ascent = it->max_ascent;
20383 descent = it->max_descent;
20384 phys_ascent = it->max_phys_ascent;
20385 phys_descent = it->max_phys_descent;
20387 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
20389 if (IT_DISPLAYING_WHITESPACE (it))
20390 may_wrap = 1;
20391 else if (may_wrap)
20393 SAVE_IT (wrap_it, *it, wrap_data);
20394 wrap_x = x;
20395 wrap_row_used = row->used[TEXT_AREA];
20396 wrap_row_ascent = row->ascent;
20397 wrap_row_height = row->height;
20398 wrap_row_phys_ascent = row->phys_ascent;
20399 wrap_row_phys_height = row->phys_height;
20400 wrap_row_extra_line_spacing = row->extra_line_spacing;
20401 wrap_row_min_pos = min_pos;
20402 wrap_row_min_bpos = min_bpos;
20403 wrap_row_max_pos = max_pos;
20404 wrap_row_max_bpos = max_bpos;
20405 may_wrap = 0;
20410 PRODUCE_GLYPHS (it);
20412 /* If this display element was in marginal areas, continue with
20413 the next one. */
20414 if (it->area != TEXT_AREA)
20416 row->ascent = max (row->ascent, it->max_ascent);
20417 row->height = max (row->height, it->max_ascent + it->max_descent);
20418 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20419 row->phys_height = max (row->phys_height,
20420 it->max_phys_ascent + it->max_phys_descent);
20421 row->extra_line_spacing = max (row->extra_line_spacing,
20422 it->max_extra_line_spacing);
20423 set_iterator_to_next (it, 1);
20424 /* If we didn't handle the line/wrap prefix above, and the
20425 call to set_iterator_to_next just switched to TEXT_AREA,
20426 process the prefix now. */
20427 if (it->area == TEXT_AREA && pending_handle_line_prefix)
20429 pending_handle_line_prefix = false;
20430 handle_line_prefix (it);
20432 continue;
20435 /* Does the display element fit on the line? If we truncate
20436 lines, we should draw past the right edge of the window. If
20437 we don't truncate, we want to stop so that we can display the
20438 continuation glyph before the right margin. If lines are
20439 continued, there are two possible strategies for characters
20440 resulting in more than 1 glyph (e.g. tabs): Display as many
20441 glyphs as possible in this line and leave the rest for the
20442 continuation line, or display the whole element in the next
20443 line. Original redisplay did the former, so we do it also. */
20444 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20445 hpos_before = it->hpos;
20446 x_before = x;
20448 if (/* Not a newline. */
20449 nglyphs > 0
20450 /* Glyphs produced fit entirely in the line. */
20451 && it->current_x < it->last_visible_x)
20453 it->hpos += nglyphs;
20454 row->ascent = max (row->ascent, it->max_ascent);
20455 row->height = max (row->height, it->max_ascent + it->max_descent);
20456 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20457 row->phys_height = max (row->phys_height,
20458 it->max_phys_ascent + it->max_phys_descent);
20459 row->extra_line_spacing = max (row->extra_line_spacing,
20460 it->max_extra_line_spacing);
20461 if (it->current_x - it->pixel_width < it->first_visible_x
20462 /* In R2L rows, we arrange in extend_face_to_end_of_line
20463 to add a right offset to the line, by a suitable
20464 change to the stretch glyph that is the leftmost
20465 glyph of the line. */
20466 && !row->reversed_p)
20467 row->x = x - it->first_visible_x;
20468 /* Record the maximum and minimum buffer positions seen so
20469 far in glyphs that will be displayed by this row. */
20470 if (it->bidi_p)
20471 RECORD_MAX_MIN_POS (it);
20473 else
20475 int i, new_x;
20476 struct glyph *glyph;
20478 for (i = 0; i < nglyphs; ++i, x = new_x)
20480 /* Identify the glyphs added by the last call to
20481 PRODUCE_GLYPHS. In R2L rows, they are prepended to
20482 the previous glyphs. */
20483 if (!row->reversed_p)
20484 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20485 else
20486 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
20487 new_x = x + glyph->pixel_width;
20489 if (/* Lines are continued. */
20490 it->line_wrap != TRUNCATE
20491 && (/* Glyph doesn't fit on the line. */
20492 new_x > it->last_visible_x
20493 /* Or it fits exactly on a window system frame. */
20494 || (new_x == it->last_visible_x
20495 && FRAME_WINDOW_P (it->f)
20496 && (row->reversed_p
20497 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20498 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
20500 /* End of a continued line. */
20502 if (it->hpos == 0
20503 || (new_x == it->last_visible_x
20504 && FRAME_WINDOW_P (it->f)
20505 && (row->reversed_p
20506 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20507 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
20509 /* Current glyph is the only one on the line or
20510 fits exactly on the line. We must continue
20511 the line because we can't draw the cursor
20512 after the glyph. */
20513 row->continued_p = 1;
20514 it->current_x = new_x;
20515 it->continuation_lines_width += new_x;
20516 ++it->hpos;
20517 if (i == nglyphs - 1)
20519 /* If line-wrap is on, check if a previous
20520 wrap point was found. */
20521 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
20522 && wrap_row_used > 0
20523 /* Even if there is a previous wrap
20524 point, continue the line here as
20525 usual, if (i) the previous character
20526 was a space or tab AND (ii) the
20527 current character is not. */
20528 && (!may_wrap
20529 || IT_DISPLAYING_WHITESPACE (it)))
20530 goto back_to_wrap;
20532 /* Record the maximum and minimum buffer
20533 positions seen so far in glyphs that will be
20534 displayed by this row. */
20535 if (it->bidi_p)
20536 RECORD_MAX_MIN_POS (it);
20537 set_iterator_to_next (it, 1);
20538 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20540 if (!get_next_display_element (it))
20542 row->exact_window_width_line_p = 1;
20543 it->continuation_lines_width = 0;
20544 row->continued_p = 0;
20545 row->ends_at_zv_p = 1;
20547 else if (ITERATOR_AT_END_OF_LINE_P (it))
20549 row->continued_p = 0;
20550 row->exact_window_width_line_p = 1;
20552 /* If line-wrap is on, check if a
20553 previous wrap point was found. */
20554 else if (wrap_row_used > 0
20555 /* Even if there is a previous wrap
20556 point, continue the line here as
20557 usual, if (i) the previous character
20558 was a space or tab AND (ii) the
20559 current character is not. */
20560 && (!may_wrap
20561 || IT_DISPLAYING_WHITESPACE (it)))
20562 goto back_to_wrap;
20566 else if (it->bidi_p)
20567 RECORD_MAX_MIN_POS (it);
20568 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20569 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20570 extend_face_to_end_of_line (it);
20572 else if (CHAR_GLYPH_PADDING_P (*glyph)
20573 && !FRAME_WINDOW_P (it->f))
20575 /* A padding glyph that doesn't fit on this line.
20576 This means the whole character doesn't fit
20577 on the line. */
20578 if (row->reversed_p)
20579 unproduce_glyphs (it, row->used[TEXT_AREA]
20580 - n_glyphs_before);
20581 row->used[TEXT_AREA] = n_glyphs_before;
20583 /* Fill the rest of the row with continuation
20584 glyphs like in 20.x. */
20585 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
20586 < row->glyphs[1 + TEXT_AREA])
20587 produce_special_glyphs (it, IT_CONTINUATION);
20589 row->continued_p = 1;
20590 it->current_x = x_before;
20591 it->continuation_lines_width += x_before;
20593 /* Restore the height to what it was before the
20594 element not fitting on the line. */
20595 it->max_ascent = ascent;
20596 it->max_descent = descent;
20597 it->max_phys_ascent = phys_ascent;
20598 it->max_phys_descent = phys_descent;
20599 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20600 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20601 extend_face_to_end_of_line (it);
20603 else if (wrap_row_used > 0)
20605 back_to_wrap:
20606 if (row->reversed_p)
20607 unproduce_glyphs (it,
20608 row->used[TEXT_AREA] - wrap_row_used);
20609 RESTORE_IT (it, &wrap_it, wrap_data);
20610 it->continuation_lines_width += wrap_x;
20611 row->used[TEXT_AREA] = wrap_row_used;
20612 row->ascent = wrap_row_ascent;
20613 row->height = wrap_row_height;
20614 row->phys_ascent = wrap_row_phys_ascent;
20615 row->phys_height = wrap_row_phys_height;
20616 row->extra_line_spacing = wrap_row_extra_line_spacing;
20617 min_pos = wrap_row_min_pos;
20618 min_bpos = wrap_row_min_bpos;
20619 max_pos = wrap_row_max_pos;
20620 max_bpos = wrap_row_max_bpos;
20621 row->continued_p = 1;
20622 row->ends_at_zv_p = 0;
20623 row->exact_window_width_line_p = 0;
20624 it->continuation_lines_width += x;
20626 /* Make sure that a non-default face is extended
20627 up to the right margin of the window. */
20628 extend_face_to_end_of_line (it);
20630 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
20632 /* A TAB that extends past the right edge of the
20633 window. This produces a single glyph on
20634 window system frames. We leave the glyph in
20635 this row and let it fill the row, but don't
20636 consume the TAB. */
20637 if ((row->reversed_p
20638 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20639 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20640 produce_special_glyphs (it, IT_CONTINUATION);
20641 it->continuation_lines_width += it->last_visible_x;
20642 row->ends_in_middle_of_char_p = 1;
20643 row->continued_p = 1;
20644 glyph->pixel_width = it->last_visible_x - x;
20645 it->starts_in_middle_of_char_p = 1;
20646 if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
20647 || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
20648 extend_face_to_end_of_line (it);
20650 else
20652 /* Something other than a TAB that draws past
20653 the right edge of the window. Restore
20654 positions to values before the element. */
20655 if (row->reversed_p)
20656 unproduce_glyphs (it, row->used[TEXT_AREA]
20657 - (n_glyphs_before + i));
20658 row->used[TEXT_AREA] = n_glyphs_before + i;
20660 /* Display continuation glyphs. */
20661 it->current_x = x_before;
20662 it->continuation_lines_width += x;
20663 if (!FRAME_WINDOW_P (it->f)
20664 || (row->reversed_p
20665 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20666 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20667 produce_special_glyphs (it, IT_CONTINUATION);
20668 row->continued_p = 1;
20670 extend_face_to_end_of_line (it);
20672 if (nglyphs > 1 && i > 0)
20674 row->ends_in_middle_of_char_p = 1;
20675 it->starts_in_middle_of_char_p = 1;
20678 /* Restore the height to what it was before the
20679 element not fitting on the line. */
20680 it->max_ascent = ascent;
20681 it->max_descent = descent;
20682 it->max_phys_ascent = phys_ascent;
20683 it->max_phys_descent = phys_descent;
20686 break;
20688 else if (new_x > it->first_visible_x)
20690 /* Increment number of glyphs actually displayed. */
20691 ++it->hpos;
20693 /* Record the maximum and minimum buffer positions
20694 seen so far in glyphs that will be displayed by
20695 this row. */
20696 if (it->bidi_p)
20697 RECORD_MAX_MIN_POS (it);
20699 if (x < it->first_visible_x && !row->reversed_p)
20700 /* Glyph is partially visible, i.e. row starts at
20701 negative X position. Don't do that in R2L
20702 rows, where we arrange to add a right offset to
20703 the line in extend_face_to_end_of_line, by a
20704 suitable change to the stretch glyph that is
20705 the leftmost glyph of the line. */
20706 row->x = x - it->first_visible_x;
20707 /* When the last glyph of an R2L row only fits
20708 partially on the line, we need to set row->x to a
20709 negative offset, so that the leftmost glyph is
20710 the one that is partially visible. But if we are
20711 going to produce the truncation glyph, this will
20712 be taken care of in produce_special_glyphs. */
20713 if (row->reversed_p
20714 && new_x > it->last_visible_x
20715 && !(it->line_wrap == TRUNCATE
20716 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
20718 eassert (FRAME_WINDOW_P (it->f));
20719 row->x = it->last_visible_x - new_x;
20722 else
20724 /* Glyph is completely off the left margin of the
20725 window. This should not happen because of the
20726 move_it_in_display_line at the start of this
20727 function, unless the text display area of the
20728 window is empty. */
20729 eassert (it->first_visible_x <= it->last_visible_x);
20732 /* Even if this display element produced no glyphs at all,
20733 we want to record its position. */
20734 if (it->bidi_p && nglyphs == 0)
20735 RECORD_MAX_MIN_POS (it);
20737 row->ascent = max (row->ascent, it->max_ascent);
20738 row->height = max (row->height, it->max_ascent + it->max_descent);
20739 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20740 row->phys_height = max (row->phys_height,
20741 it->max_phys_ascent + it->max_phys_descent);
20742 row->extra_line_spacing = max (row->extra_line_spacing,
20743 it->max_extra_line_spacing);
20745 /* End of this display line if row is continued. */
20746 if (row->continued_p || row->ends_at_zv_p)
20747 break;
20750 at_end_of_line:
20751 /* Is this a line end? If yes, we're also done, after making
20752 sure that a non-default face is extended up to the right
20753 margin of the window. */
20754 if (ITERATOR_AT_END_OF_LINE_P (it))
20756 int used_before = row->used[TEXT_AREA];
20758 row->ends_in_newline_from_string_p = STRINGP (it->object);
20760 /* Add a space at the end of the line that is used to
20761 display the cursor there. */
20762 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20763 append_space_for_newline (it, 0);
20765 /* Extend the face to the end of the line. */
20766 extend_face_to_end_of_line (it);
20768 /* Make sure we have the position. */
20769 if (used_before == 0)
20770 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
20772 /* Record the position of the newline, for use in
20773 find_row_edges. */
20774 it->eol_pos = it->current.pos;
20776 /* Consume the line end. This skips over invisible lines. */
20777 set_iterator_to_next (it, 1);
20778 it->continuation_lines_width = 0;
20779 break;
20782 /* Proceed with next display element. Note that this skips
20783 over lines invisible because of selective display. */
20784 set_iterator_to_next (it, 1);
20786 /* If we truncate lines, we are done when the last displayed
20787 glyphs reach past the right margin of the window. */
20788 if (it->line_wrap == TRUNCATE
20789 && ((FRAME_WINDOW_P (it->f)
20790 /* Images are preprocessed in produce_image_glyph such
20791 that they are cropped at the right edge of the
20792 window, so an image glyph will always end exactly at
20793 last_visible_x, even if there's no right fringe. */
20794 && ((row->reversed_p
20795 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20796 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
20797 || it->what == IT_IMAGE))
20798 ? (it->current_x >= it->last_visible_x)
20799 : (it->current_x > it->last_visible_x)))
20801 /* Maybe add truncation glyphs. */
20802 if (!FRAME_WINDOW_P (it->f)
20803 || (row->reversed_p
20804 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
20805 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
20807 int i, n;
20809 if (!row->reversed_p)
20811 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
20812 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20813 break;
20815 else
20817 for (i = 0; i < row->used[TEXT_AREA]; i++)
20818 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
20819 break;
20820 /* Remove any padding glyphs at the front of ROW, to
20821 make room for the truncation glyphs we will be
20822 adding below. The loop below always inserts at
20823 least one truncation glyph, so also remove the
20824 last glyph added to ROW. */
20825 unproduce_glyphs (it, i + 1);
20826 /* Adjust i for the loop below. */
20827 i = row->used[TEXT_AREA] - (i + 1);
20830 /* produce_special_glyphs overwrites the last glyph, so
20831 we don't want that if we want to keep that last
20832 glyph, which means it's an image. */
20833 if (it->current_x > it->last_visible_x)
20835 it->current_x = x_before;
20836 if (!FRAME_WINDOW_P (it->f))
20838 for (n = row->used[TEXT_AREA]; i < n; ++i)
20840 row->used[TEXT_AREA] = i;
20841 produce_special_glyphs (it, IT_TRUNCATION);
20844 else
20846 row->used[TEXT_AREA] = i;
20847 produce_special_glyphs (it, IT_TRUNCATION);
20849 it->hpos = hpos_before;
20852 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
20854 /* Don't truncate if we can overflow newline into fringe. */
20855 if (!get_next_display_element (it))
20857 it->continuation_lines_width = 0;
20858 row->ends_at_zv_p = 1;
20859 row->exact_window_width_line_p = 1;
20860 break;
20862 if (ITERATOR_AT_END_OF_LINE_P (it))
20864 row->exact_window_width_line_p = 1;
20865 goto at_end_of_line;
20867 it->current_x = x_before;
20868 it->hpos = hpos_before;
20871 row->truncated_on_right_p = 1;
20872 it->continuation_lines_width = 0;
20873 reseat_at_next_visible_line_start (it, 0);
20874 /* We insist below that IT's position be at ZV because in
20875 bidi-reordered lines the character at visible line start
20876 might not be the character that follows the newline in
20877 the logical order. */
20878 if (IT_BYTEPOS (*it) > BEG_BYTE)
20879 row->ends_at_zv_p =
20880 IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
20881 else
20882 row->ends_at_zv_p = false;
20883 break;
20887 if (wrap_data)
20888 bidi_unshelve_cache (wrap_data, 1);
20890 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20891 at the left window margin. */
20892 if (it->first_visible_x
20893 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20895 if (!FRAME_WINDOW_P (it->f)
20896 || (((row->reversed_p
20897 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20898 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20899 /* Don't let insert_left_trunc_glyphs overwrite the
20900 first glyph of the row if it is an image. */
20901 && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
20902 insert_left_trunc_glyphs (it);
20903 row->truncated_on_left_p = 1;
20906 /* Remember the position at which this line ends.
20908 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20909 cannot be before the call to find_row_edges below, since that is
20910 where these positions are determined. */
20911 row->end = it->current;
20912 if (!it->bidi_p)
20914 row->minpos = row->start.pos;
20915 row->maxpos = row->end.pos;
20917 else
20919 /* ROW->minpos and ROW->maxpos must be the smallest and
20920 `1 + the largest' buffer positions in ROW. But if ROW was
20921 bidi-reordered, these two positions can be anywhere in the
20922 row, so we must determine them now. */
20923 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20926 /* If the start of this line is the overlay arrow-position, then
20927 mark this glyph row as the one containing the overlay arrow.
20928 This is clearly a mess with variable size fonts. It would be
20929 better to let it be displayed like cursors under X. */
20930 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
20931 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20932 !NILP (overlay_arrow_string)))
20934 /* Overlay arrow in window redisplay is a fringe bitmap. */
20935 if (STRINGP (overlay_arrow_string))
20937 struct glyph_row *arrow_row
20938 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20939 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20940 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20941 struct glyph *p = row->glyphs[TEXT_AREA];
20942 struct glyph *p2, *end;
20944 /* Copy the arrow glyphs. */
20945 while (glyph < arrow_end)
20946 *p++ = *glyph++;
20948 /* Throw away padding glyphs. */
20949 p2 = p;
20950 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20951 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20952 ++p2;
20953 if (p2 > p)
20955 while (p2 < end)
20956 *p++ = *p2++;
20957 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20960 else
20962 eassert (INTEGERP (overlay_arrow_string));
20963 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20965 overlay_arrow_seen = 1;
20968 /* Highlight trailing whitespace. */
20969 if (!NILP (Vshow_trailing_whitespace))
20970 highlight_trailing_whitespace (it->f, it->glyph_row);
20972 /* Compute pixel dimensions of this line. */
20973 compute_line_metrics (it);
20975 /* Implementation note: No changes in the glyphs of ROW or in their
20976 faces can be done past this point, because compute_line_metrics
20977 computes ROW's hash value and stores it within the glyph_row
20978 structure. */
20980 /* Record whether this row ends inside an ellipsis. */
20981 row->ends_in_ellipsis_p
20982 = (it->method == GET_FROM_DISPLAY_VECTOR
20983 && it->ellipsis_p);
20985 /* Save fringe bitmaps in this row. */
20986 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20987 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20988 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20989 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20991 it->left_user_fringe_bitmap = 0;
20992 it->left_user_fringe_face_id = 0;
20993 it->right_user_fringe_bitmap = 0;
20994 it->right_user_fringe_face_id = 0;
20996 /* Maybe set the cursor. */
20997 cvpos = it->w->cursor.vpos;
20998 if ((cvpos < 0
20999 /* In bidi-reordered rows, keep checking for proper cursor
21000 position even if one has been found already, because buffer
21001 positions in such rows change non-linearly with ROW->VPOS,
21002 when a line is continued. One exception: when we are at ZV,
21003 display cursor on the first suitable glyph row, since all
21004 the empty rows after that also have their position set to ZV. */
21005 /* FIXME: Revisit this when glyph ``spilling'' in continuation
21006 lines' rows is implemented for bidi-reordered rows. */
21007 || (it->bidi_p
21008 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
21009 && PT >= MATRIX_ROW_START_CHARPOS (row)
21010 && PT <= MATRIX_ROW_END_CHARPOS (row)
21011 && cursor_row_p (row))
21012 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
21014 /* Prepare for the next line. This line starts horizontally at (X
21015 HPOS) = (0 0). Vertical positions are incremented. As a
21016 convenience for the caller, IT->glyph_row is set to the next
21017 row to be used. */
21018 it->current_x = it->hpos = 0;
21019 it->current_y += row->height;
21020 SET_TEXT_POS (it->eol_pos, 0, 0);
21021 ++it->vpos;
21022 ++it->glyph_row;
21023 /* The next row should by default use the same value of the
21024 reversed_p flag as this one. set_iterator_to_next decides when
21025 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
21026 the flag accordingly. */
21027 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
21028 it->glyph_row->reversed_p = row->reversed_p;
21029 it->start = row->end;
21030 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
21032 #undef RECORD_MAX_MIN_POS
21035 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
21036 Scurrent_bidi_paragraph_direction, 0, 1, 0,
21037 doc: /* Return paragraph direction at point in BUFFER.
21038 Value is either `left-to-right' or `right-to-left'.
21039 If BUFFER is omitted or nil, it defaults to the current buffer.
21041 Paragraph direction determines how the text in the paragraph is displayed.
21042 In left-to-right paragraphs, text begins at the left margin of the window
21043 and the reading direction is generally left to right. In right-to-left
21044 paragraphs, text begins at the right margin and is read from right to left.
21046 See also `bidi-paragraph-direction'. */)
21047 (Lisp_Object buffer)
21049 struct buffer *buf = current_buffer;
21050 struct buffer *old = buf;
21052 if (! NILP (buffer))
21054 CHECK_BUFFER (buffer);
21055 buf = XBUFFER (buffer);
21058 if (NILP (BVAR (buf, bidi_display_reordering))
21059 || NILP (BVAR (buf, enable_multibyte_characters))
21060 /* When we are loading loadup.el, the character property tables
21061 needed for bidi iteration are not yet available. */
21062 || !NILP (Vpurify_flag))
21063 return Qleft_to_right;
21064 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
21065 return BVAR (buf, bidi_paragraph_direction);
21066 else
21068 /* Determine the direction from buffer text. We could try to
21069 use current_matrix if it is up to date, but this seems fast
21070 enough as it is. */
21071 struct bidi_it itb;
21072 ptrdiff_t pos = BUF_PT (buf);
21073 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
21074 int c;
21075 void *itb_data = bidi_shelve_cache ();
21077 set_buffer_temp (buf);
21078 /* bidi_paragraph_init finds the base direction of the paragraph
21079 by searching forward from paragraph start. We need the base
21080 direction of the current or _previous_ paragraph, so we need
21081 to make sure we are within that paragraph. To that end, find
21082 the previous non-empty line. */
21083 if (pos >= ZV && pos > BEGV)
21084 DEC_BOTH (pos, bytepos);
21085 AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
21086 if (fast_looking_at (trailing_white_space,
21087 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
21089 while ((c = FETCH_BYTE (bytepos)) == '\n'
21090 || c == ' ' || c == '\t' || c == '\f')
21092 if (bytepos <= BEGV_BYTE)
21093 break;
21094 bytepos--;
21095 pos--;
21097 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
21098 bytepos--;
21100 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
21101 itb.paragraph_dir = NEUTRAL_DIR;
21102 itb.string.s = NULL;
21103 itb.string.lstring = Qnil;
21104 itb.string.bufpos = 0;
21105 itb.string.from_disp_str = 0;
21106 itb.string.unibyte = 0;
21107 /* We have no window to use here for ignoring window-specific
21108 overlays. Using NULL for window pointer will cause
21109 compute_display_string_pos to use the current buffer. */
21110 itb.w = NULL;
21111 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
21112 bidi_unshelve_cache (itb_data, 0);
21113 set_buffer_temp (old);
21114 switch (itb.paragraph_dir)
21116 case L2R:
21117 return Qleft_to_right;
21118 break;
21119 case R2L:
21120 return Qright_to_left;
21121 break;
21122 default:
21123 emacs_abort ();
21128 DEFUN ("bidi-find-overridden-directionality",
21129 Fbidi_find_overridden_directionality,
21130 Sbidi_find_overridden_directionality, 2, 3, 0,
21131 doc: /* Return position between FROM and TO where directionality was overridden.
21133 This function returns the first character position in the specified
21134 region of OBJECT where there is a character whose `bidi-class' property
21135 is `L', but which was forced to display as `R' by a directional
21136 override, and likewise with characters whose `bidi-class' is `R'
21137 or `AL' that were forced to display as `L'.
21139 If no such character is found, the function returns nil.
21141 OBJECT is a Lisp string or buffer to search for overridden
21142 directionality, and defaults to the current buffer if nil or omitted.
21143 OBJECT can also be a window, in which case the function will search
21144 the buffer displayed in that window. Passing the window instead of
21145 a buffer is preferable when the buffer is displayed in some window,
21146 because this function will then be able to correctly account for
21147 window-specific overlays, which can affect the results.
21149 Strong directional characters `L', `R', and `AL' can have their
21150 intrinsic directionality overridden by directional override
21151 control characters RLO \(u+202e) and LRO \(u+202d). See the
21152 function `get-char-code-property' for a way to inquire about
21153 the `bidi-class' property of a character. */)
21154 (Lisp_Object from, Lisp_Object to, Lisp_Object object)
21156 struct buffer *buf = current_buffer;
21157 struct buffer *old = buf;
21158 struct window *w = NULL;
21159 bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
21160 struct bidi_it itb;
21161 ptrdiff_t from_pos, to_pos, from_bpos;
21162 void *itb_data;
21164 if (!NILP (object))
21166 if (BUFFERP (object))
21167 buf = XBUFFER (object);
21168 else if (WINDOWP (object))
21170 w = decode_live_window (object);
21171 buf = XBUFFER (w->contents);
21172 frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
21174 else
21175 CHECK_STRING (object);
21178 if (STRINGP (object))
21180 /* Characters in unibyte strings are always treated by bidi.c as
21181 strong LTR. */
21182 if (!STRING_MULTIBYTE (object)
21183 /* When we are loading loadup.el, the character property
21184 tables needed for bidi iteration are not yet
21185 available. */
21186 || !NILP (Vpurify_flag))
21187 return Qnil;
21189 validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
21190 if (from_pos >= SCHARS (object))
21191 return Qnil;
21193 /* Set up the bidi iterator. */
21194 itb_data = bidi_shelve_cache ();
21195 itb.paragraph_dir = NEUTRAL_DIR;
21196 itb.string.lstring = object;
21197 itb.string.s = NULL;
21198 itb.string.schars = SCHARS (object);
21199 itb.string.bufpos = 0;
21200 itb.string.from_disp_str = 0;
21201 itb.string.unibyte = 0;
21202 itb.w = w;
21203 bidi_init_it (0, 0, frame_window_p, &itb);
21205 else
21207 /* Nothing this fancy can happen in unibyte buffers, or in a
21208 buffer that disabled reordering, or if FROM is at EOB. */
21209 if (NILP (BVAR (buf, bidi_display_reordering))
21210 || NILP (BVAR (buf, enable_multibyte_characters))
21211 /* When we are loading loadup.el, the character property
21212 tables needed for bidi iteration are not yet
21213 available. */
21214 || !NILP (Vpurify_flag))
21215 return Qnil;
21217 set_buffer_temp (buf);
21218 validate_region (&from, &to);
21219 from_pos = XINT (from);
21220 to_pos = XINT (to);
21221 if (from_pos >= ZV)
21222 return Qnil;
21224 /* Set up the bidi iterator. */
21225 itb_data = bidi_shelve_cache ();
21226 from_bpos = CHAR_TO_BYTE (from_pos);
21227 if (from_pos == BEGV)
21229 itb.charpos = BEGV;
21230 itb.bytepos = BEGV_BYTE;
21232 else if (FETCH_CHAR (from_bpos - 1) == '\n')
21234 itb.charpos = from_pos;
21235 itb.bytepos = from_bpos;
21237 else
21238 itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
21239 -1, &itb.bytepos);
21240 itb.paragraph_dir = NEUTRAL_DIR;
21241 itb.string.s = NULL;
21242 itb.string.lstring = Qnil;
21243 itb.string.bufpos = 0;
21244 itb.string.from_disp_str = 0;
21245 itb.string.unibyte = 0;
21246 itb.w = w;
21247 bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
21250 ptrdiff_t found;
21251 do {
21252 /* For the purposes of this function, the actual base direction of
21253 the paragraph doesn't matter, so just set it to L2R. */
21254 bidi_paragraph_init (L2R, &itb, 0);
21255 while ((found = bidi_find_first_overridden (&itb)) < from_pos)
21257 } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
21259 bidi_unshelve_cache (itb_data, 0);
21260 set_buffer_temp (old);
21262 return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
21265 DEFUN ("move-point-visually", Fmove_point_visually,
21266 Smove_point_visually, 1, 1, 0,
21267 doc: /* Move point in the visual order in the specified DIRECTION.
21268 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
21269 left.
21271 Value is the new character position of point. */)
21272 (Lisp_Object direction)
21274 struct window *w = XWINDOW (selected_window);
21275 struct buffer *b = XBUFFER (w->contents);
21276 struct glyph_row *row;
21277 int dir;
21278 Lisp_Object paragraph_dir;
21280 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
21281 (!(ROW)->continued_p \
21282 && INTEGERP ((GLYPH)->object) \
21283 && (GLYPH)->type == CHAR_GLYPH \
21284 && (GLYPH)->u.ch == ' ' \
21285 && (GLYPH)->charpos >= 0 \
21286 && !(GLYPH)->avoid_cursor_p)
21288 CHECK_NUMBER (direction);
21289 dir = XINT (direction);
21290 if (dir > 0)
21291 dir = 1;
21292 else
21293 dir = -1;
21295 /* If current matrix is up-to-date, we can use the information
21296 recorded in the glyphs, at least as long as the goal is on the
21297 screen. */
21298 if (w->window_end_valid
21299 && !windows_or_buffers_changed
21300 && b
21301 && !b->clip_changed
21302 && !b->prevent_redisplay_optimizations_p
21303 && !window_outdated (w)
21304 /* We rely below on the cursor coordinates to be up to date, but
21305 we cannot trust them if some command moved point since the
21306 last complete redisplay. */
21307 && w->last_point == BUF_PT (b)
21308 && w->cursor.vpos >= 0
21309 && w->cursor.vpos < w->current_matrix->nrows
21310 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
21312 struct glyph *g = row->glyphs[TEXT_AREA];
21313 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
21314 struct glyph *gpt = g + w->cursor.hpos;
21316 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
21318 if (BUFFERP (g->object) && g->charpos != PT)
21320 SET_PT (g->charpos);
21321 w->cursor.vpos = -1;
21322 return make_number (PT);
21324 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
21326 ptrdiff_t new_pos;
21328 if (BUFFERP (gpt->object))
21330 new_pos = PT;
21331 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
21332 new_pos += (row->reversed_p ? -dir : dir);
21333 else
21334 new_pos -= (row->reversed_p ? -dir : dir);
21336 else if (BUFFERP (g->object))
21337 new_pos = g->charpos;
21338 else
21339 break;
21340 SET_PT (new_pos);
21341 w->cursor.vpos = -1;
21342 return make_number (PT);
21344 else if (ROW_GLYPH_NEWLINE_P (row, g))
21346 /* Glyphs inserted at the end of a non-empty line for
21347 positioning the cursor have zero charpos, so we must
21348 deduce the value of point by other means. */
21349 if (g->charpos > 0)
21350 SET_PT (g->charpos);
21351 else if (row->ends_at_zv_p && PT != ZV)
21352 SET_PT (ZV);
21353 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
21354 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21355 else
21356 break;
21357 w->cursor.vpos = -1;
21358 return make_number (PT);
21361 if (g == e || INTEGERP (g->object))
21363 if (row->truncated_on_left_p || row->truncated_on_right_p)
21364 goto simulate_display;
21365 if (!row->reversed_p)
21366 row += dir;
21367 else
21368 row -= dir;
21369 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
21370 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
21371 goto simulate_display;
21373 if (dir > 0)
21375 if (row->reversed_p && !row->continued_p)
21377 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21378 w->cursor.vpos = -1;
21379 return make_number (PT);
21381 g = row->glyphs[TEXT_AREA];
21382 e = g + row->used[TEXT_AREA];
21383 for ( ; g < e; g++)
21385 if (BUFFERP (g->object)
21386 /* Empty lines have only one glyph, which stands
21387 for the newline, and whose charpos is the
21388 buffer position of the newline. */
21389 || ROW_GLYPH_NEWLINE_P (row, g)
21390 /* When the buffer ends in a newline, the line at
21391 EOB also has one glyph, but its charpos is -1. */
21392 || (row->ends_at_zv_p
21393 && !row->reversed_p
21394 && INTEGERP (g->object)
21395 && g->type == CHAR_GLYPH
21396 && g->u.ch == ' '))
21398 if (g->charpos > 0)
21399 SET_PT (g->charpos);
21400 else if (!row->reversed_p
21401 && row->ends_at_zv_p
21402 && PT != ZV)
21403 SET_PT (ZV);
21404 else
21405 continue;
21406 w->cursor.vpos = -1;
21407 return make_number (PT);
21411 else
21413 if (!row->reversed_p && !row->continued_p)
21415 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
21416 w->cursor.vpos = -1;
21417 return make_number (PT);
21419 e = row->glyphs[TEXT_AREA];
21420 g = e + row->used[TEXT_AREA] - 1;
21421 for ( ; g >= e; g--)
21423 if (BUFFERP (g->object)
21424 || (ROW_GLYPH_NEWLINE_P (row, g)
21425 && g->charpos > 0)
21426 /* Empty R2L lines on GUI frames have the buffer
21427 position of the newline stored in the stretch
21428 glyph. */
21429 || g->type == STRETCH_GLYPH
21430 || (row->ends_at_zv_p
21431 && row->reversed_p
21432 && INTEGERP (g->object)
21433 && g->type == CHAR_GLYPH
21434 && g->u.ch == ' '))
21436 if (g->charpos > 0)
21437 SET_PT (g->charpos);
21438 else if (row->reversed_p
21439 && row->ends_at_zv_p
21440 && PT != ZV)
21441 SET_PT (ZV);
21442 else
21443 continue;
21444 w->cursor.vpos = -1;
21445 return make_number (PT);
21452 simulate_display:
21454 /* If we wind up here, we failed to move by using the glyphs, so we
21455 need to simulate display instead. */
21457 if (b)
21458 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
21459 else
21460 paragraph_dir = Qleft_to_right;
21461 if (EQ (paragraph_dir, Qright_to_left))
21462 dir = -dir;
21463 if (PT <= BEGV && dir < 0)
21464 xsignal0 (Qbeginning_of_buffer);
21465 else if (PT >= ZV && dir > 0)
21466 xsignal0 (Qend_of_buffer);
21467 else
21469 struct text_pos pt;
21470 struct it it;
21471 int pt_x, target_x, pixel_width, pt_vpos;
21472 bool at_eol_p;
21473 bool overshoot_expected = false;
21474 bool target_is_eol_p = false;
21476 /* Setup the arena. */
21477 SET_TEXT_POS (pt, PT, PT_BYTE);
21478 start_display (&it, w, pt);
21480 if (it.cmp_it.id < 0
21481 && it.method == GET_FROM_STRING
21482 && it.area == TEXT_AREA
21483 && it.string_from_display_prop_p
21484 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
21485 overshoot_expected = true;
21487 /* Find the X coordinate of point. We start from the beginning
21488 of this or previous line to make sure we are before point in
21489 the logical order (since the move_it_* functions can only
21490 move forward). */
21491 reseat:
21492 reseat_at_previous_visible_line_start (&it);
21493 it.current_x = it.hpos = it.current_y = it.vpos = 0;
21494 if (IT_CHARPOS (it) != PT)
21496 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
21497 -1, -1, -1, MOVE_TO_POS);
21498 /* If we missed point because the character there is
21499 displayed out of a display vector that has more than one
21500 glyph, retry expecting overshoot. */
21501 if (it.method == GET_FROM_DISPLAY_VECTOR
21502 && it.current.dpvec_index > 0
21503 && !overshoot_expected)
21505 overshoot_expected = true;
21506 goto reseat;
21508 else if (IT_CHARPOS (it) != PT && !overshoot_expected)
21509 move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
21511 pt_x = it.current_x;
21512 pt_vpos = it.vpos;
21513 if (dir > 0 || overshoot_expected)
21515 struct glyph_row *row = it.glyph_row;
21517 /* When point is at beginning of line, we don't have
21518 information about the glyph there loaded into struct
21519 it. Calling get_next_display_element fixes that. */
21520 if (pt_x == 0)
21521 get_next_display_element (&it);
21522 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21523 it.glyph_row = NULL;
21524 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
21525 it.glyph_row = row;
21526 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
21527 it, lest it will become out of sync with it's buffer
21528 position. */
21529 it.current_x = pt_x;
21531 else
21532 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
21533 pixel_width = it.pixel_width;
21534 if (overshoot_expected && at_eol_p)
21535 pixel_width = 0;
21536 else if (pixel_width <= 0)
21537 pixel_width = 1;
21539 /* If there's a display string (or something similar) at point,
21540 we are actually at the glyph to the left of point, so we need
21541 to correct the X coordinate. */
21542 if (overshoot_expected)
21544 if (it.bidi_p)
21545 pt_x += pixel_width * it.bidi_it.scan_dir;
21546 else
21547 pt_x += pixel_width;
21550 /* Compute target X coordinate, either to the left or to the
21551 right of point. On TTY frames, all characters have the same
21552 pixel width of 1, so we can use that. On GUI frames we don't
21553 have an easy way of getting at the pixel width of the
21554 character to the left of point, so we use a different method
21555 of getting to that place. */
21556 if (dir > 0)
21557 target_x = pt_x + pixel_width;
21558 else
21559 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
21561 /* Target X coordinate could be one line above or below the line
21562 of point, in which case we need to adjust the target X
21563 coordinate. Also, if moving to the left, we need to begin at
21564 the left edge of the point's screen line. */
21565 if (dir < 0)
21567 if (pt_x > 0)
21569 start_display (&it, w, pt);
21570 reseat_at_previous_visible_line_start (&it);
21571 it.current_x = it.current_y = it.hpos = 0;
21572 if (pt_vpos != 0)
21573 move_it_by_lines (&it, pt_vpos);
21575 else
21577 move_it_by_lines (&it, -1);
21578 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
21579 target_is_eol_p = true;
21580 /* Under word-wrap, we don't know the x coordinate of
21581 the last character displayed on the previous line,
21582 which immediately precedes the wrap point. To find
21583 out its x coordinate, we try moving to the right
21584 margin of the window, which will stop at the wrap
21585 point, and then reset target_x to point at the
21586 character that precedes the wrap point. This is not
21587 needed on GUI frames, because (see below) there we
21588 move from the left margin one grapheme cluster at a
21589 time, and stop when we hit the wrap point. */
21590 if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
21592 void *it_data = NULL;
21593 struct it it2;
21595 SAVE_IT (it2, it, it_data);
21596 move_it_in_display_line_to (&it, ZV, target_x,
21597 MOVE_TO_POS | MOVE_TO_X);
21598 /* If we arrived at target_x, that _is_ the last
21599 character on the previous line. */
21600 if (it.current_x != target_x)
21601 target_x = it.current_x - 1;
21602 RESTORE_IT (&it, &it2, it_data);
21606 else
21608 if (at_eol_p
21609 || (target_x >= it.last_visible_x
21610 && it.line_wrap != TRUNCATE))
21612 if (pt_x > 0)
21613 move_it_by_lines (&it, 0);
21614 move_it_by_lines (&it, 1);
21615 target_x = 0;
21619 /* Move to the target X coordinate. */
21620 #ifdef HAVE_WINDOW_SYSTEM
21621 /* On GUI frames, as we don't know the X coordinate of the
21622 character to the left of point, moving point to the left
21623 requires walking, one grapheme cluster at a time, until we
21624 find ourself at a place immediately to the left of the
21625 character at point. */
21626 if (FRAME_WINDOW_P (it.f) && dir < 0)
21628 struct text_pos new_pos;
21629 enum move_it_result rc = MOVE_X_REACHED;
21631 if (it.current_x == 0)
21632 get_next_display_element (&it);
21633 if (it.what == IT_COMPOSITION)
21635 new_pos.charpos = it.cmp_it.charpos;
21636 new_pos.bytepos = -1;
21638 else
21639 new_pos = it.current.pos;
21641 while (it.current_x + it.pixel_width <= target_x
21642 && (rc == MOVE_X_REACHED
21643 /* Under word-wrap, move_it_in_display_line_to
21644 stops at correct coordinates, but sometimes
21645 returns MOVE_POS_MATCH_OR_ZV. */
21646 || (it.line_wrap == WORD_WRAP
21647 && rc == MOVE_POS_MATCH_OR_ZV)))
21649 int new_x = it.current_x + it.pixel_width;
21651 /* For composed characters, we want the position of the
21652 first character in the grapheme cluster (usually, the
21653 composition's base character), whereas it.current
21654 might give us the position of the _last_ one, e.g. if
21655 the composition is rendered in reverse due to bidi
21656 reordering. */
21657 if (it.what == IT_COMPOSITION)
21659 new_pos.charpos = it.cmp_it.charpos;
21660 new_pos.bytepos = -1;
21662 else
21663 new_pos = it.current.pos;
21664 if (new_x == it.current_x)
21665 new_x++;
21666 rc = move_it_in_display_line_to (&it, ZV, new_x,
21667 MOVE_TO_POS | MOVE_TO_X);
21668 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
21669 break;
21671 /* The previous position we saw in the loop is the one we
21672 want. */
21673 if (new_pos.bytepos == -1)
21674 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
21675 it.current.pos = new_pos;
21677 else
21678 #endif
21679 if (it.current_x != target_x)
21680 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
21682 /* When lines are truncated, the above loop will stop at the
21683 window edge. But we want to get to the end of line, even if
21684 it is beyond the window edge; automatic hscroll will then
21685 scroll the window to show point as appropriate. */
21686 if (target_is_eol_p && it.line_wrap == TRUNCATE
21687 && get_next_display_element (&it))
21689 struct text_pos new_pos = it.current.pos;
21691 while (!ITERATOR_AT_END_OF_LINE_P (&it))
21693 set_iterator_to_next (&it, 0);
21694 if (it.method == GET_FROM_BUFFER)
21695 new_pos = it.current.pos;
21696 if (!get_next_display_element (&it))
21697 break;
21700 it.current.pos = new_pos;
21703 /* If we ended up in a display string that covers point, move to
21704 buffer position to the right in the visual order. */
21705 if (dir > 0)
21707 while (IT_CHARPOS (it) == PT)
21709 set_iterator_to_next (&it, 0);
21710 if (!get_next_display_element (&it))
21711 break;
21715 /* Move point to that position. */
21716 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
21719 return make_number (PT);
21721 #undef ROW_GLYPH_NEWLINE_P
21724 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
21725 Sbidi_resolved_levels, 0, 1, 0,
21726 doc: /* Return the resolved bidirectional levels of characters at VPOS.
21728 The resolved levels are produced by the Emacs bidi reordering engine
21729 that implements the UBA, the Unicode Bidirectional Algorithm. Please
21730 read the Unicode Standard Annex 9 (UAX#9) for background information
21731 about these levels.
21733 VPOS is the zero-based number of the current window's screen line
21734 for which to produce the resolved levels. If VPOS is nil or omitted,
21735 it defaults to the screen line of point. If the window displays a
21736 header line, VPOS of zero will report on the header line, and first
21737 line of text in the window will have VPOS of 1.
21739 Value is an array of resolved levels, indexed by glyph number.
21740 Glyphs are numbered from zero starting from the beginning of the
21741 screen line, i.e. the left edge of the window for left-to-right lines
21742 and from the right edge for right-to-left lines. The resolved levels
21743 are produced only for the window's text area; text in display margins
21744 is not included.
21746 If the selected window's display is not up-to-date, or if the specified
21747 screen line does not display text, this function returns nil. It is
21748 highly recommended to bind this function to some simple key, like F8,
21749 in order to avoid these problems.
21751 This function exists mainly for testing the correctness of the
21752 Emacs UBA implementation, in particular with the test suite. */)
21753 (Lisp_Object vpos)
21755 struct window *w = XWINDOW (selected_window);
21756 struct buffer *b = XBUFFER (w->contents);
21757 int nrow;
21758 struct glyph_row *row;
21760 if (NILP (vpos))
21762 int d1, d2, d3, d4, d5;
21764 pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
21766 else
21768 CHECK_NUMBER_COERCE_MARKER (vpos);
21769 nrow = XINT (vpos);
21772 /* We require up-to-date glyph matrix for this window. */
21773 if (w->window_end_valid
21774 && !windows_or_buffers_changed
21775 && b
21776 && !b->clip_changed
21777 && !b->prevent_redisplay_optimizations_p
21778 && !window_outdated (w)
21779 && nrow >= 0
21780 && nrow < w->current_matrix->nrows
21781 && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
21782 && MATRIX_ROW_DISPLAYS_TEXT_P (row))
21784 struct glyph *g, *e, *g1;
21785 int nglyphs, i;
21786 Lisp_Object levels;
21788 if (!row->reversed_p) /* Left-to-right glyph row. */
21790 g = g1 = row->glyphs[TEXT_AREA];
21791 e = g + row->used[TEXT_AREA];
21793 /* Skip over glyphs at the start of the row that was
21794 generated by redisplay for its own needs. */
21795 while (g < e
21796 && INTEGERP (g->object)
21797 && g->charpos < 0)
21798 g++;
21799 g1 = g;
21801 /* Count the "interesting" glyphs in this row. */
21802 for (nglyphs = 0; g < e && !INTEGERP (g->object); g++)
21803 nglyphs++;
21805 /* Create and fill the array. */
21806 levels = make_uninit_vector (nglyphs);
21807 for (i = 0; g1 < g; i++, g1++)
21808 ASET (levels, i, make_number (g1->resolved_level));
21810 else /* Right-to-left glyph row. */
21812 g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
21813 e = row->glyphs[TEXT_AREA] - 1;
21814 while (g > e
21815 && INTEGERP (g->object)
21816 && g->charpos < 0)
21817 g--;
21818 g1 = g;
21819 for (nglyphs = 0; g > e && !INTEGERP (g->object); g--)
21820 nglyphs++;
21821 levels = make_uninit_vector (nglyphs);
21822 for (i = 0; g1 > g; i++, g1--)
21823 ASET (levels, i, make_number (g1->resolved_level));
21825 return levels;
21827 else
21828 return Qnil;
21833 /***********************************************************************
21834 Menu Bar
21835 ***********************************************************************/
21837 /* Redisplay the menu bar in the frame for window W.
21839 The menu bar of X frames that don't have X toolkit support is
21840 displayed in a special window W->frame->menu_bar_window.
21842 The menu bar of terminal frames is treated specially as far as
21843 glyph matrices are concerned. Menu bar lines are not part of
21844 windows, so the update is done directly on the frame matrix rows
21845 for the menu bar. */
21847 static void
21848 display_menu_bar (struct window *w)
21850 struct frame *f = XFRAME (WINDOW_FRAME (w));
21851 struct it it;
21852 Lisp_Object items;
21853 int i;
21855 /* Don't do all this for graphical frames. */
21856 #ifdef HAVE_NTGUI
21857 if (FRAME_W32_P (f))
21858 return;
21859 #endif
21860 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21861 if (FRAME_X_P (f))
21862 return;
21863 #endif
21865 #ifdef HAVE_NS
21866 if (FRAME_NS_P (f))
21867 return;
21868 #endif /* HAVE_NS */
21870 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
21871 eassert (!FRAME_WINDOW_P (f));
21872 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
21873 it.first_visible_x = 0;
21874 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21875 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
21876 if (FRAME_WINDOW_P (f))
21878 /* Menu bar lines are displayed in the desired matrix of the
21879 dummy window menu_bar_window. */
21880 struct window *menu_w;
21881 menu_w = XWINDOW (f->menu_bar_window);
21882 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
21883 MENU_FACE_ID);
21884 it.first_visible_x = 0;
21885 it.last_visible_x = FRAME_PIXEL_WIDTH (f);
21887 else
21888 #endif /* not USE_X_TOOLKIT and not USE_GTK */
21890 /* This is a TTY frame, i.e. character hpos/vpos are used as
21891 pixel x/y. */
21892 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
21893 MENU_FACE_ID);
21894 it.first_visible_x = 0;
21895 it.last_visible_x = FRAME_COLS (f);
21898 /* FIXME: This should be controlled by a user option. See the
21899 comments in redisplay_tool_bar and display_mode_line about
21900 this. */
21901 it.paragraph_embedding = L2R;
21903 /* Clear all rows of the menu bar. */
21904 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
21906 struct glyph_row *row = it.glyph_row + i;
21907 clear_glyph_row (row);
21908 row->enabled_p = true;
21909 row->full_width_p = 1;
21910 row->reversed_p = false;
21913 /* Display all items of the menu bar. */
21914 items = FRAME_MENU_BAR_ITEMS (it.f);
21915 for (i = 0; i < ASIZE (items); i += 4)
21917 Lisp_Object string;
21919 /* Stop at nil string. */
21920 string = AREF (items, i + 1);
21921 if (NILP (string))
21922 break;
21924 /* Remember where item was displayed. */
21925 ASET (items, i + 3, make_number (it.hpos));
21927 /* Display the item, pad with one space. */
21928 if (it.current_x < it.last_visible_x)
21929 display_string (NULL, string, Qnil, 0, 0, &it,
21930 SCHARS (string) + 1, 0, 0, -1);
21933 /* Fill out the line with spaces. */
21934 if (it.current_x < it.last_visible_x)
21935 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
21937 /* Compute the total height of the lines. */
21938 compute_line_metrics (&it);
21941 /* Deep copy of a glyph row, including the glyphs. */
21942 static void
21943 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
21945 struct glyph *pointers[1 + LAST_AREA];
21946 int to_used = to->used[TEXT_AREA];
21948 /* Save glyph pointers of TO. */
21949 memcpy (pointers, to->glyphs, sizeof to->glyphs);
21951 /* Do a structure assignment. */
21952 *to = *from;
21954 /* Restore original glyph pointers of TO. */
21955 memcpy (to->glyphs, pointers, sizeof to->glyphs);
21957 /* Copy the glyphs. */
21958 memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
21959 min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
21961 /* If we filled only part of the TO row, fill the rest with
21962 space_glyph (which will display as empty space). */
21963 if (to_used > from->used[TEXT_AREA])
21964 fill_up_frame_row_with_spaces (to, to_used);
21967 /* Display one menu item on a TTY, by overwriting the glyphs in the
21968 frame F's desired glyph matrix with glyphs produced from the menu
21969 item text. Called from term.c to display TTY drop-down menus one
21970 item at a time.
21972 ITEM_TEXT is the menu item text as a C string.
21974 FACE_ID is the face ID to be used for this menu item. FACE_ID
21975 could specify one of 3 faces: a face for an enabled item, a face
21976 for a disabled item, or a face for a selected item.
21978 X and Y are coordinates of the first glyph in the frame's desired
21979 matrix to be overwritten by the menu item. Since this is a TTY, Y
21980 is the zero-based number of the glyph row and X is the zero-based
21981 glyph number in the row, starting from left, where to start
21982 displaying the item.
21984 SUBMENU non-zero means this menu item drops down a submenu, which
21985 should be indicated by displaying a proper visual cue after the
21986 item text. */
21988 void
21989 display_tty_menu_item (const char *item_text, int width, int face_id,
21990 int x, int y, int submenu)
21992 struct it it;
21993 struct frame *f = SELECTED_FRAME ();
21994 struct window *w = XWINDOW (f->selected_window);
21995 int saved_used, saved_truncated, saved_width, saved_reversed;
21996 struct glyph_row *row;
21997 size_t item_len = strlen (item_text);
21999 eassert (FRAME_TERMCAP_P (f));
22001 /* Don't write beyond the matrix's last row. This can happen for
22002 TTY screens that are not high enough to show the entire menu.
22003 (This is actually a bit of defensive programming, as
22004 tty_menu_display already limits the number of menu items to one
22005 less than the number of screen lines.) */
22006 if (y >= f->desired_matrix->nrows)
22007 return;
22009 init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
22010 it.first_visible_x = 0;
22011 it.last_visible_x = FRAME_COLS (f) - 1;
22012 row = it.glyph_row;
22013 /* Start with the row contents from the current matrix. */
22014 deep_copy_glyph_row (row, f->current_matrix->rows + y);
22015 saved_width = row->full_width_p;
22016 row->full_width_p = 1;
22017 saved_reversed = row->reversed_p;
22018 row->reversed_p = 0;
22019 row->enabled_p = true;
22021 /* Arrange for the menu item glyphs to start at (X,Y) and have the
22022 desired face. */
22023 eassert (x < f->desired_matrix->matrix_w);
22024 it.current_x = it.hpos = x;
22025 it.current_y = it.vpos = y;
22026 saved_used = row->used[TEXT_AREA];
22027 saved_truncated = row->truncated_on_right_p;
22028 row->used[TEXT_AREA] = x;
22029 it.face_id = face_id;
22030 it.line_wrap = TRUNCATE;
22032 /* FIXME: This should be controlled by a user option. See the
22033 comments in redisplay_tool_bar and display_mode_line about this.
22034 Also, if paragraph_embedding could ever be R2L, changes will be
22035 needed to avoid shifting to the right the row characters in
22036 term.c:append_glyph. */
22037 it.paragraph_embedding = L2R;
22039 /* Pad with a space on the left. */
22040 display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
22041 width--;
22042 /* Display the menu item, pad with spaces to WIDTH. */
22043 if (submenu)
22045 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22046 item_len, 0, FRAME_COLS (f) - 1, -1);
22047 width -= item_len;
22048 /* Indicate with " >" that there's a submenu. */
22049 display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
22050 FRAME_COLS (f) - 1, -1);
22052 else
22053 display_string (item_text, Qnil, Qnil, 0, 0, &it,
22054 width, 0, FRAME_COLS (f) - 1, -1);
22056 row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
22057 row->truncated_on_right_p = saved_truncated;
22058 row->hash = row_hash (row);
22059 row->full_width_p = saved_width;
22060 row->reversed_p = saved_reversed;
22063 /***********************************************************************
22064 Mode Line
22065 ***********************************************************************/
22067 /* Redisplay mode lines in the window tree whose root is WINDOW. If
22068 FORCE is non-zero, redisplay mode lines unconditionally.
22069 Otherwise, redisplay only mode lines that are garbaged. Value is
22070 the number of windows whose mode lines were redisplayed. */
22072 static int
22073 redisplay_mode_lines (Lisp_Object window, bool force)
22075 int nwindows = 0;
22077 while (!NILP (window))
22079 struct window *w = XWINDOW (window);
22081 if (WINDOWP (w->contents))
22082 nwindows += redisplay_mode_lines (w->contents, force);
22083 else if (force
22084 || FRAME_GARBAGED_P (XFRAME (w->frame))
22085 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
22087 struct text_pos lpoint;
22088 struct buffer *old = current_buffer;
22090 /* Set the window's buffer for the mode line display. */
22091 SET_TEXT_POS (lpoint, PT, PT_BYTE);
22092 set_buffer_internal_1 (XBUFFER (w->contents));
22094 /* Point refers normally to the selected window. For any
22095 other window, set up appropriate value. */
22096 if (!EQ (window, selected_window))
22098 struct text_pos pt;
22100 CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
22101 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
22104 /* Display mode lines. */
22105 clear_glyph_matrix (w->desired_matrix);
22106 if (display_mode_lines (w))
22107 ++nwindows;
22109 /* Restore old settings. */
22110 set_buffer_internal_1 (old);
22111 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
22114 window = w->next;
22117 return nwindows;
22121 /* Display the mode and/or header line of window W. Value is the
22122 sum number of mode lines and header lines displayed. */
22124 static int
22125 display_mode_lines (struct window *w)
22127 Lisp_Object old_selected_window = selected_window;
22128 Lisp_Object old_selected_frame = selected_frame;
22129 Lisp_Object new_frame = w->frame;
22130 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
22131 int n = 0;
22133 selected_frame = new_frame;
22134 /* FIXME: If we were to allow the mode-line's computation changing the buffer
22135 or window's point, then we'd need select_window_1 here as well. */
22136 XSETWINDOW (selected_window, w);
22137 XFRAME (new_frame)->selected_window = selected_window;
22139 /* These will be set while the mode line specs are processed. */
22140 line_number_displayed = 0;
22141 w->column_number_displayed = -1;
22143 if (WINDOW_WANTS_MODELINE_P (w))
22145 struct window *sel_w = XWINDOW (old_selected_window);
22147 /* Select mode line face based on the real selected window. */
22148 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
22149 BVAR (current_buffer, mode_line_format));
22150 ++n;
22153 if (WINDOW_WANTS_HEADER_LINE_P (w))
22155 display_mode_line (w, HEADER_LINE_FACE_ID,
22156 BVAR (current_buffer, header_line_format));
22157 ++n;
22160 XFRAME (new_frame)->selected_window = old_frame_selected_window;
22161 selected_frame = old_selected_frame;
22162 selected_window = old_selected_window;
22163 if (n > 0)
22164 w->must_be_updated_p = true;
22165 return n;
22169 /* Display mode or header line of window W. FACE_ID specifies which
22170 line to display; it is either MODE_LINE_FACE_ID or
22171 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
22172 display. Value is the pixel height of the mode/header line
22173 displayed. */
22175 static int
22176 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
22178 struct it it;
22179 struct face *face;
22180 ptrdiff_t count = SPECPDL_INDEX ();
22182 init_iterator (&it, w, -1, -1, NULL, face_id);
22183 /* Don't extend on a previously drawn mode-line.
22184 This may happen if called from pos_visible_p. */
22185 it.glyph_row->enabled_p = false;
22186 prepare_desired_row (w, it.glyph_row, true);
22188 it.glyph_row->mode_line_p = 1;
22190 /* FIXME: This should be controlled by a user option. But
22191 supporting such an option is not trivial, since the mode line is
22192 made up of many separate strings. */
22193 it.paragraph_embedding = L2R;
22195 record_unwind_protect (unwind_format_mode_line,
22196 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
22198 mode_line_target = MODE_LINE_DISPLAY;
22200 /* Temporarily make frame's keyboard the current kboard so that
22201 kboard-local variables in the mode_line_format will get the right
22202 values. */
22203 push_kboard (FRAME_KBOARD (it.f));
22204 record_unwind_save_match_data ();
22205 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22206 pop_kboard ();
22208 unbind_to (count, Qnil);
22210 /* Fill up with spaces. */
22211 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
22213 compute_line_metrics (&it);
22214 it.glyph_row->full_width_p = 1;
22215 it.glyph_row->continued_p = 0;
22216 it.glyph_row->truncated_on_left_p = 0;
22217 it.glyph_row->truncated_on_right_p = 0;
22219 /* Make a 3D mode-line have a shadow at its right end. */
22220 face = FACE_FROM_ID (it.f, face_id);
22221 extend_face_to_end_of_line (&it);
22222 if (face->box != FACE_NO_BOX)
22224 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
22225 + it.glyph_row->used[TEXT_AREA] - 1);
22226 last->right_box_line_p = 1;
22229 return it.glyph_row->height;
22232 /* Move element ELT in LIST to the front of LIST.
22233 Return the updated list. */
22235 static Lisp_Object
22236 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
22238 register Lisp_Object tail, prev;
22239 register Lisp_Object tem;
22241 tail = list;
22242 prev = Qnil;
22243 while (CONSP (tail))
22245 tem = XCAR (tail);
22247 if (EQ (elt, tem))
22249 /* Splice out the link TAIL. */
22250 if (NILP (prev))
22251 list = XCDR (tail);
22252 else
22253 Fsetcdr (prev, XCDR (tail));
22255 /* Now make it the first. */
22256 Fsetcdr (tail, list);
22257 return tail;
22259 else
22260 prev = tail;
22261 tail = XCDR (tail);
22262 QUIT;
22265 /* Not found--return unchanged LIST. */
22266 return list;
22269 /* Contribute ELT to the mode line for window IT->w. How it
22270 translates into text depends on its data type.
22272 IT describes the display environment in which we display, as usual.
22274 DEPTH is the depth in recursion. It is used to prevent
22275 infinite recursion here.
22277 FIELD_WIDTH is the number of characters the display of ELT should
22278 occupy in the mode line, and PRECISION is the maximum number of
22279 characters to display from ELT's representation. See
22280 display_string for details.
22282 Returns the hpos of the end of the text generated by ELT.
22284 PROPS is a property list to add to any string we encounter.
22286 If RISKY is nonzero, remove (disregard) any properties in any string
22287 we encounter, and ignore :eval and :propertize.
22289 The global variable `mode_line_target' determines whether the
22290 output is passed to `store_mode_line_noprop',
22291 `store_mode_line_string', or `display_string'. */
22293 static int
22294 display_mode_element (struct it *it, int depth, int field_width, int precision,
22295 Lisp_Object elt, Lisp_Object props, int risky)
22297 int n = 0, field, prec;
22298 int literal = 0;
22300 tail_recurse:
22301 if (depth > 100)
22302 elt = build_string ("*too-deep*");
22304 depth++;
22306 switch (XTYPE (elt))
22308 case Lisp_String:
22310 /* A string: output it and check for %-constructs within it. */
22311 unsigned char c;
22312 ptrdiff_t offset = 0;
22314 if (SCHARS (elt) > 0
22315 && (!NILP (props) || risky))
22317 Lisp_Object oprops, aelt;
22318 oprops = Ftext_properties_at (make_number (0), elt);
22320 /* If the starting string's properties are not what
22321 we want, translate the string. Also, if the string
22322 is risky, do that anyway. */
22324 if (NILP (Fequal (props, oprops)) || risky)
22326 /* If the starting string has properties,
22327 merge the specified ones onto the existing ones. */
22328 if (! NILP (oprops) && !risky)
22330 Lisp_Object tem;
22332 oprops = Fcopy_sequence (oprops);
22333 tem = props;
22334 while (CONSP (tem))
22336 oprops = Fplist_put (oprops, XCAR (tem),
22337 XCAR (XCDR (tem)));
22338 tem = XCDR (XCDR (tem));
22340 props = oprops;
22343 aelt = Fassoc (elt, mode_line_proptrans_alist);
22344 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
22346 /* AELT is what we want. Move it to the front
22347 without consing. */
22348 elt = XCAR (aelt);
22349 mode_line_proptrans_alist
22350 = move_elt_to_front (aelt, mode_line_proptrans_alist);
22352 else
22354 Lisp_Object tem;
22356 /* If AELT has the wrong props, it is useless.
22357 so get rid of it. */
22358 if (! NILP (aelt))
22359 mode_line_proptrans_alist
22360 = Fdelq (aelt, mode_line_proptrans_alist);
22362 elt = Fcopy_sequence (elt);
22363 Fset_text_properties (make_number (0), Flength (elt),
22364 props, elt);
22365 /* Add this item to mode_line_proptrans_alist. */
22366 mode_line_proptrans_alist
22367 = Fcons (Fcons (elt, props),
22368 mode_line_proptrans_alist);
22369 /* Truncate mode_line_proptrans_alist
22370 to at most 50 elements. */
22371 tem = Fnthcdr (make_number (50),
22372 mode_line_proptrans_alist);
22373 if (! NILP (tem))
22374 XSETCDR (tem, Qnil);
22379 offset = 0;
22381 if (literal)
22383 prec = precision - n;
22384 switch (mode_line_target)
22386 case MODE_LINE_NOPROP:
22387 case MODE_LINE_TITLE:
22388 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
22389 break;
22390 case MODE_LINE_STRING:
22391 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
22392 break;
22393 case MODE_LINE_DISPLAY:
22394 n += display_string (NULL, elt, Qnil, 0, 0, it,
22395 0, prec, 0, STRING_MULTIBYTE (elt));
22396 break;
22399 break;
22402 /* Handle the non-literal case. */
22404 while ((precision <= 0 || n < precision)
22405 && SREF (elt, offset) != 0
22406 && (mode_line_target != MODE_LINE_DISPLAY
22407 || it->current_x < it->last_visible_x))
22409 ptrdiff_t last_offset = offset;
22411 /* Advance to end of string or next format specifier. */
22412 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
22415 if (offset - 1 != last_offset)
22417 ptrdiff_t nchars, nbytes;
22419 /* Output to end of string or up to '%'. Field width
22420 is length of string. Don't output more than
22421 PRECISION allows us. */
22422 offset--;
22424 prec = c_string_width (SDATA (elt) + last_offset,
22425 offset - last_offset, precision - n,
22426 &nchars, &nbytes);
22428 switch (mode_line_target)
22430 case MODE_LINE_NOPROP:
22431 case MODE_LINE_TITLE:
22432 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
22433 break;
22434 case MODE_LINE_STRING:
22436 ptrdiff_t bytepos = last_offset;
22437 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22438 ptrdiff_t endpos = (precision <= 0
22439 ? string_byte_to_char (elt, offset)
22440 : charpos + nchars);
22442 n += store_mode_line_string (NULL,
22443 Fsubstring (elt, make_number (charpos),
22444 make_number (endpos)),
22445 0, 0, 0, Qnil);
22447 break;
22448 case MODE_LINE_DISPLAY:
22450 ptrdiff_t bytepos = last_offset;
22451 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
22453 if (precision <= 0)
22454 nchars = string_byte_to_char (elt, offset) - charpos;
22455 n += display_string (NULL, elt, Qnil, 0, charpos,
22456 it, 0, nchars, 0,
22457 STRING_MULTIBYTE (elt));
22459 break;
22462 else /* c == '%' */
22464 ptrdiff_t percent_position = offset;
22466 /* Get the specified minimum width. Zero means
22467 don't pad. */
22468 field = 0;
22469 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
22470 field = field * 10 + c - '0';
22472 /* Don't pad beyond the total padding allowed. */
22473 if (field_width - n > 0 && field > field_width - n)
22474 field = field_width - n;
22476 /* Note that either PRECISION <= 0 or N < PRECISION. */
22477 prec = precision - n;
22479 if (c == 'M')
22480 n += display_mode_element (it, depth, field, prec,
22481 Vglobal_mode_string, props,
22482 risky);
22483 else if (c != 0)
22485 bool multibyte;
22486 ptrdiff_t bytepos, charpos;
22487 const char *spec;
22488 Lisp_Object string;
22490 bytepos = percent_position;
22491 charpos = (STRING_MULTIBYTE (elt)
22492 ? string_byte_to_char (elt, bytepos)
22493 : bytepos);
22494 spec = decode_mode_spec (it->w, c, field, &string);
22495 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
22497 switch (mode_line_target)
22499 case MODE_LINE_NOPROP:
22500 case MODE_LINE_TITLE:
22501 n += store_mode_line_noprop (spec, field, prec);
22502 break;
22503 case MODE_LINE_STRING:
22505 Lisp_Object tem = build_string (spec);
22506 props = Ftext_properties_at (make_number (charpos), elt);
22507 /* Should only keep face property in props */
22508 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
22510 break;
22511 case MODE_LINE_DISPLAY:
22513 int nglyphs_before, nwritten;
22515 nglyphs_before = it->glyph_row->used[TEXT_AREA];
22516 nwritten = display_string (spec, string, elt,
22517 charpos, 0, it,
22518 field, prec, 0,
22519 multibyte);
22521 /* Assign to the glyphs written above the
22522 string where the `%x' came from, position
22523 of the `%'. */
22524 if (nwritten > 0)
22526 struct glyph *glyph
22527 = (it->glyph_row->glyphs[TEXT_AREA]
22528 + nglyphs_before);
22529 int i;
22531 for (i = 0; i < nwritten; ++i)
22533 glyph[i].object = elt;
22534 glyph[i].charpos = charpos;
22537 n += nwritten;
22540 break;
22543 else /* c == 0 */
22544 break;
22548 break;
22550 case Lisp_Symbol:
22551 /* A symbol: process the value of the symbol recursively
22552 as if it appeared here directly. Avoid error if symbol void.
22553 Special case: if value of symbol is a string, output the string
22554 literally. */
22556 register Lisp_Object tem;
22558 /* If the variable is not marked as risky to set
22559 then its contents are risky to use. */
22560 if (NILP (Fget (elt, Qrisky_local_variable)))
22561 risky = 1;
22563 tem = Fboundp (elt);
22564 if (!NILP (tem))
22566 tem = Fsymbol_value (elt);
22567 /* If value is a string, output that string literally:
22568 don't check for % within it. */
22569 if (STRINGP (tem))
22570 literal = 1;
22572 if (!EQ (tem, elt))
22574 /* Give up right away for nil or t. */
22575 elt = tem;
22576 goto tail_recurse;
22580 break;
22582 case Lisp_Cons:
22584 register Lisp_Object car, tem;
22586 /* A cons cell: five distinct cases.
22587 If first element is :eval or :propertize, do something special.
22588 If first element is a string or a cons, process all the elements
22589 and effectively concatenate them.
22590 If first element is a negative number, truncate displaying cdr to
22591 at most that many characters. If positive, pad (with spaces)
22592 to at least that many characters.
22593 If first element is a symbol, process the cadr or caddr recursively
22594 according to whether the symbol's value is non-nil or nil. */
22595 car = XCAR (elt);
22596 if (EQ (car, QCeval))
22598 /* An element of the form (:eval FORM) means evaluate FORM
22599 and use the result as mode line elements. */
22601 if (risky)
22602 break;
22604 if (CONSP (XCDR (elt)))
22606 Lisp_Object spec;
22607 spec = safe__eval (true, XCAR (XCDR (elt)));
22608 n += display_mode_element (it, depth, field_width - n,
22609 precision - n, spec, props,
22610 risky);
22613 else if (EQ (car, QCpropertize))
22615 /* An element of the form (:propertize ELT PROPS...)
22616 means display ELT but applying properties PROPS. */
22618 if (risky)
22619 break;
22621 if (CONSP (XCDR (elt)))
22622 n += display_mode_element (it, depth, field_width - n,
22623 precision - n, XCAR (XCDR (elt)),
22624 XCDR (XCDR (elt)), risky);
22626 else if (SYMBOLP (car))
22628 tem = Fboundp (car);
22629 elt = XCDR (elt);
22630 if (!CONSP (elt))
22631 goto invalid;
22632 /* elt is now the cdr, and we know it is a cons cell.
22633 Use its car if CAR has a non-nil value. */
22634 if (!NILP (tem))
22636 tem = Fsymbol_value (car);
22637 if (!NILP (tem))
22639 elt = XCAR (elt);
22640 goto tail_recurse;
22643 /* Symbol's value is nil (or symbol is unbound)
22644 Get the cddr of the original list
22645 and if possible find the caddr and use that. */
22646 elt = XCDR (elt);
22647 if (NILP (elt))
22648 break;
22649 else if (!CONSP (elt))
22650 goto invalid;
22651 elt = XCAR (elt);
22652 goto tail_recurse;
22654 else if (INTEGERP (car))
22656 register int lim = XINT (car);
22657 elt = XCDR (elt);
22658 if (lim < 0)
22660 /* Negative int means reduce maximum width. */
22661 if (precision <= 0)
22662 precision = -lim;
22663 else
22664 precision = min (precision, -lim);
22666 else if (lim > 0)
22668 /* Padding specified. Don't let it be more than
22669 current maximum. */
22670 if (precision > 0)
22671 lim = min (precision, lim);
22673 /* If that's more padding than already wanted, queue it.
22674 But don't reduce padding already specified even if
22675 that is beyond the current truncation point. */
22676 field_width = max (lim, field_width);
22678 goto tail_recurse;
22680 else if (STRINGP (car) || CONSP (car))
22682 Lisp_Object halftail = elt;
22683 int len = 0;
22685 while (CONSP (elt)
22686 && (precision <= 0 || n < precision))
22688 n += display_mode_element (it, depth,
22689 /* Do padding only after the last
22690 element in the list. */
22691 (! CONSP (XCDR (elt))
22692 ? field_width - n
22693 : 0),
22694 precision - n, XCAR (elt),
22695 props, risky);
22696 elt = XCDR (elt);
22697 len++;
22698 if ((len & 1) == 0)
22699 halftail = XCDR (halftail);
22700 /* Check for cycle. */
22701 if (EQ (halftail, elt))
22702 break;
22706 break;
22708 default:
22709 invalid:
22710 elt = build_string ("*invalid*");
22711 goto tail_recurse;
22714 /* Pad to FIELD_WIDTH. */
22715 if (field_width > 0 && n < field_width)
22717 switch (mode_line_target)
22719 case MODE_LINE_NOPROP:
22720 case MODE_LINE_TITLE:
22721 n += store_mode_line_noprop ("", field_width - n, 0);
22722 break;
22723 case MODE_LINE_STRING:
22724 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
22725 break;
22726 case MODE_LINE_DISPLAY:
22727 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
22728 0, 0, 0);
22729 break;
22733 return n;
22736 /* Store a mode-line string element in mode_line_string_list.
22738 If STRING is non-null, display that C string. Otherwise, the Lisp
22739 string LISP_STRING is displayed.
22741 FIELD_WIDTH is the minimum number of output glyphs to produce.
22742 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22743 with spaces. FIELD_WIDTH <= 0 means don't pad.
22745 PRECISION is the maximum number of characters to output from
22746 STRING. PRECISION <= 0 means don't truncate the string.
22748 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
22749 properties to the string.
22751 PROPS are the properties to add to the string.
22752 The mode_line_string_face face property is always added to the string.
22755 static int
22756 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
22757 int field_width, int precision, Lisp_Object props)
22759 ptrdiff_t len;
22760 int n = 0;
22762 if (string != NULL)
22764 len = strlen (string);
22765 if (precision > 0 && len > precision)
22766 len = precision;
22767 lisp_string = make_string (string, len);
22768 if (NILP (props))
22769 props = mode_line_string_face_prop;
22770 else if (!NILP (mode_line_string_face))
22772 Lisp_Object face = Fplist_get (props, Qface);
22773 props = Fcopy_sequence (props);
22774 if (NILP (face))
22775 face = mode_line_string_face;
22776 else
22777 face = list2 (face, mode_line_string_face);
22778 props = Fplist_put (props, Qface, face);
22780 Fadd_text_properties (make_number (0), make_number (len),
22781 props, lisp_string);
22783 else
22785 len = XFASTINT (Flength (lisp_string));
22786 if (precision > 0 && len > precision)
22788 len = precision;
22789 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
22790 precision = -1;
22792 if (!NILP (mode_line_string_face))
22794 Lisp_Object face;
22795 if (NILP (props))
22796 props = Ftext_properties_at (make_number (0), lisp_string);
22797 face = Fplist_get (props, Qface);
22798 if (NILP (face))
22799 face = mode_line_string_face;
22800 else
22801 face = list2 (face, mode_line_string_face);
22802 props = list2 (Qface, face);
22803 if (copy_string)
22804 lisp_string = Fcopy_sequence (lisp_string);
22806 if (!NILP (props))
22807 Fadd_text_properties (make_number (0), make_number (len),
22808 props, lisp_string);
22811 if (len > 0)
22813 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22814 n += len;
22817 if (field_width > len)
22819 field_width -= len;
22820 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
22821 if (!NILP (props))
22822 Fadd_text_properties (make_number (0), make_number (field_width),
22823 props, lisp_string);
22824 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
22825 n += field_width;
22828 return n;
22832 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
22833 1, 4, 0,
22834 doc: /* Format a string out of a mode line format specification.
22835 First arg FORMAT specifies the mode line format (see `mode-line-format'
22836 for details) to use.
22838 By default, the format is evaluated for the currently selected window.
22840 Optional second arg FACE specifies the face property to put on all
22841 characters for which no face is specified. The value nil means the
22842 default face. The value t means whatever face the window's mode line
22843 currently uses (either `mode-line' or `mode-line-inactive',
22844 depending on whether the window is the selected window or not).
22845 An integer value means the value string has no text
22846 properties.
22848 Optional third and fourth args WINDOW and BUFFER specify the window
22849 and buffer to use as the context for the formatting (defaults
22850 are the selected window and the WINDOW's buffer). */)
22851 (Lisp_Object format, Lisp_Object face,
22852 Lisp_Object window, Lisp_Object buffer)
22854 struct it it;
22855 int len;
22856 struct window *w;
22857 struct buffer *old_buffer = NULL;
22858 int face_id;
22859 int no_props = INTEGERP (face);
22860 ptrdiff_t count = SPECPDL_INDEX ();
22861 Lisp_Object str;
22862 int string_start = 0;
22864 w = decode_any_window (window);
22865 XSETWINDOW (window, w);
22867 if (NILP (buffer))
22868 buffer = w->contents;
22869 CHECK_BUFFER (buffer);
22871 /* Make formatting the modeline a non-op when noninteractive, otherwise
22872 there will be problems later caused by a partially initialized frame. */
22873 if (NILP (format) || noninteractive)
22874 return empty_unibyte_string;
22876 if (no_props)
22877 face = Qnil;
22879 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
22880 : EQ (face, Qt) ? (EQ (window, selected_window)
22881 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
22882 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
22883 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
22884 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
22885 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
22886 : DEFAULT_FACE_ID;
22888 old_buffer = current_buffer;
22890 /* Save things including mode_line_proptrans_alist,
22891 and set that to nil so that we don't alter the outer value. */
22892 record_unwind_protect (unwind_format_mode_line,
22893 format_mode_line_unwind_data
22894 (XFRAME (WINDOW_FRAME (w)),
22895 old_buffer, selected_window, 1));
22896 mode_line_proptrans_alist = Qnil;
22898 Fselect_window (window, Qt);
22899 set_buffer_internal_1 (XBUFFER (buffer));
22901 init_iterator (&it, w, -1, -1, NULL, face_id);
22903 if (no_props)
22905 mode_line_target = MODE_LINE_NOPROP;
22906 mode_line_string_face_prop = Qnil;
22907 mode_line_string_list = Qnil;
22908 string_start = MODE_LINE_NOPROP_LEN (0);
22910 else
22912 mode_line_target = MODE_LINE_STRING;
22913 mode_line_string_list = Qnil;
22914 mode_line_string_face = face;
22915 mode_line_string_face_prop
22916 = NILP (face) ? Qnil : list2 (Qface, face);
22919 push_kboard (FRAME_KBOARD (it.f));
22920 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
22921 pop_kboard ();
22923 if (no_props)
22925 len = MODE_LINE_NOPROP_LEN (string_start);
22926 str = make_string (mode_line_noprop_buf + string_start, len);
22928 else
22930 mode_line_string_list = Fnreverse (mode_line_string_list);
22931 str = Fmapconcat (intern ("identity"), mode_line_string_list,
22932 empty_unibyte_string);
22935 unbind_to (count, Qnil);
22936 return str;
22939 /* Write a null-terminated, right justified decimal representation of
22940 the positive integer D to BUF using a minimal field width WIDTH. */
22942 static void
22943 pint2str (register char *buf, register int width, register ptrdiff_t d)
22945 register char *p = buf;
22947 if (d <= 0)
22948 *p++ = '0';
22949 else
22951 while (d > 0)
22953 *p++ = d % 10 + '0';
22954 d /= 10;
22958 for (width -= (int) (p - buf); width > 0; --width)
22959 *p++ = ' ';
22960 *p-- = '\0';
22961 while (p > buf)
22963 d = *buf;
22964 *buf++ = *p;
22965 *p-- = d;
22969 /* Write a null-terminated, right justified decimal and "human
22970 readable" representation of the nonnegative integer D to BUF using
22971 a minimal field width WIDTH. D should be smaller than 999.5e24. */
22973 static const char power_letter[] =
22975 0, /* no letter */
22976 'k', /* kilo */
22977 'M', /* mega */
22978 'G', /* giga */
22979 'T', /* tera */
22980 'P', /* peta */
22981 'E', /* exa */
22982 'Z', /* zetta */
22983 'Y' /* yotta */
22986 static void
22987 pint2hrstr (char *buf, int width, ptrdiff_t d)
22989 /* We aim to represent the nonnegative integer D as
22990 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
22991 ptrdiff_t quotient = d;
22992 int remainder = 0;
22993 /* -1 means: do not use TENTHS. */
22994 int tenths = -1;
22995 int exponent = 0;
22997 /* Length of QUOTIENT.TENTHS as a string. */
22998 int length;
23000 char * psuffix;
23001 char * p;
23003 if (quotient >= 1000)
23005 /* Scale to the appropriate EXPONENT. */
23008 remainder = quotient % 1000;
23009 quotient /= 1000;
23010 exponent++;
23012 while (quotient >= 1000);
23014 /* Round to nearest and decide whether to use TENTHS or not. */
23015 if (quotient <= 9)
23017 tenths = remainder / 100;
23018 if (remainder % 100 >= 50)
23020 if (tenths < 9)
23021 tenths++;
23022 else
23024 quotient++;
23025 if (quotient == 10)
23026 tenths = -1;
23027 else
23028 tenths = 0;
23032 else
23033 if (remainder >= 500)
23035 if (quotient < 999)
23036 quotient++;
23037 else
23039 quotient = 1;
23040 exponent++;
23041 tenths = 0;
23046 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
23047 if (tenths == -1 && quotient <= 99)
23048 if (quotient <= 9)
23049 length = 1;
23050 else
23051 length = 2;
23052 else
23053 length = 3;
23054 p = psuffix = buf + max (width, length);
23056 /* Print EXPONENT. */
23057 *psuffix++ = power_letter[exponent];
23058 *psuffix = '\0';
23060 /* Print TENTHS. */
23061 if (tenths >= 0)
23063 *--p = '0' + tenths;
23064 *--p = '.';
23067 /* Print QUOTIENT. */
23070 int digit = quotient % 10;
23071 *--p = '0' + digit;
23073 while ((quotient /= 10) != 0);
23075 /* Print leading spaces. */
23076 while (buf < p)
23077 *--p = ' ';
23080 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
23081 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
23082 type of CODING_SYSTEM. Return updated pointer into BUF. */
23084 static unsigned char invalid_eol_type[] = "(*invalid*)";
23086 static char *
23087 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
23089 Lisp_Object val;
23090 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
23091 const unsigned char *eol_str;
23092 int eol_str_len;
23093 /* The EOL conversion we are using. */
23094 Lisp_Object eoltype;
23096 val = CODING_SYSTEM_SPEC (coding_system);
23097 eoltype = Qnil;
23099 if (!VECTORP (val)) /* Not yet decided. */
23101 *buf++ = multibyte ? '-' : ' ';
23102 if (eol_flag)
23103 eoltype = eol_mnemonic_undecided;
23104 /* Don't mention EOL conversion if it isn't decided. */
23106 else
23108 Lisp_Object attrs;
23109 Lisp_Object eolvalue;
23111 attrs = AREF (val, 0);
23112 eolvalue = AREF (val, 2);
23114 *buf++ = multibyte
23115 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
23116 : ' ';
23118 if (eol_flag)
23120 /* The EOL conversion that is normal on this system. */
23122 if (NILP (eolvalue)) /* Not yet decided. */
23123 eoltype = eol_mnemonic_undecided;
23124 else if (VECTORP (eolvalue)) /* Not yet decided. */
23125 eoltype = eol_mnemonic_undecided;
23126 else /* eolvalue is Qunix, Qdos, or Qmac. */
23127 eoltype = (EQ (eolvalue, Qunix)
23128 ? eol_mnemonic_unix
23129 : (EQ (eolvalue, Qdos) == 1
23130 ? eol_mnemonic_dos : eol_mnemonic_mac));
23134 if (eol_flag)
23136 /* Mention the EOL conversion if it is not the usual one. */
23137 if (STRINGP (eoltype))
23139 eol_str = SDATA (eoltype);
23140 eol_str_len = SBYTES (eoltype);
23142 else if (CHARACTERP (eoltype))
23144 int c = XFASTINT (eoltype);
23145 return buf + CHAR_STRING (c, (unsigned char *) buf);
23147 else
23149 eol_str = invalid_eol_type;
23150 eol_str_len = sizeof (invalid_eol_type) - 1;
23152 memcpy (buf, eol_str, eol_str_len);
23153 buf += eol_str_len;
23156 return buf;
23159 /* Return a string for the output of a mode line %-spec for window W,
23160 generated by character C. FIELD_WIDTH > 0 means pad the string
23161 returned with spaces to that value. Return a Lisp string in
23162 *STRING if the resulting string is taken from that Lisp string.
23164 Note we operate on the current buffer for most purposes. */
23166 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
23168 static const char *
23169 decode_mode_spec (struct window *w, register int c, int field_width,
23170 Lisp_Object *string)
23172 Lisp_Object obj;
23173 struct frame *f = XFRAME (WINDOW_FRAME (w));
23174 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
23175 /* We are going to use f->decode_mode_spec_buffer as the buffer to
23176 produce strings from numerical values, so limit preposterously
23177 large values of FIELD_WIDTH to avoid overrunning the buffer's
23178 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
23179 bytes plus the terminating null. */
23180 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
23181 struct buffer *b = current_buffer;
23183 obj = Qnil;
23184 *string = Qnil;
23186 switch (c)
23188 case '*':
23189 if (!NILP (BVAR (b, read_only)))
23190 return "%";
23191 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23192 return "*";
23193 return "-";
23195 case '+':
23196 /* This differs from %* only for a modified read-only buffer. */
23197 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23198 return "*";
23199 if (!NILP (BVAR (b, read_only)))
23200 return "%";
23201 return "-";
23203 case '&':
23204 /* This differs from %* in ignoring read-only-ness. */
23205 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
23206 return "*";
23207 return "-";
23209 case '%':
23210 return "%";
23212 case '[':
23214 int i;
23215 char *p;
23217 if (command_loop_level > 5)
23218 return "[[[... ";
23219 p = decode_mode_spec_buf;
23220 for (i = 0; i < command_loop_level; i++)
23221 *p++ = '[';
23222 *p = 0;
23223 return decode_mode_spec_buf;
23226 case ']':
23228 int i;
23229 char *p;
23231 if (command_loop_level > 5)
23232 return " ...]]]";
23233 p = decode_mode_spec_buf;
23234 for (i = 0; i < command_loop_level; i++)
23235 *p++ = ']';
23236 *p = 0;
23237 return decode_mode_spec_buf;
23240 case '-':
23242 register int i;
23244 /* Let lots_of_dashes be a string of infinite length. */
23245 if (mode_line_target == MODE_LINE_NOPROP
23246 || mode_line_target == MODE_LINE_STRING)
23247 return "--";
23248 if (field_width <= 0
23249 || field_width > sizeof (lots_of_dashes))
23251 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
23252 decode_mode_spec_buf[i] = '-';
23253 decode_mode_spec_buf[i] = '\0';
23254 return decode_mode_spec_buf;
23256 else
23257 return lots_of_dashes;
23260 case 'b':
23261 obj = BVAR (b, name);
23262 break;
23264 case 'c':
23265 /* %c and %l are ignored in `frame-title-format'.
23266 (In redisplay_internal, the frame title is drawn _before_ the
23267 windows are updated, so the stuff which depends on actual
23268 window contents (such as %l) may fail to render properly, or
23269 even crash emacs.) */
23270 if (mode_line_target == MODE_LINE_TITLE)
23271 return "";
23272 else
23274 ptrdiff_t col = current_column ();
23275 w->column_number_displayed = col;
23276 pint2str (decode_mode_spec_buf, width, col);
23277 return decode_mode_spec_buf;
23280 case 'e':
23281 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
23283 if (NILP (Vmemory_full))
23284 return "";
23285 else
23286 return "!MEM FULL! ";
23288 #else
23289 return "";
23290 #endif
23292 case 'F':
23293 /* %F displays the frame name. */
23294 if (!NILP (f->title))
23295 return SSDATA (f->title);
23296 if (f->explicit_name || ! FRAME_WINDOW_P (f))
23297 return SSDATA (f->name);
23298 return "Emacs";
23300 case 'f':
23301 obj = BVAR (b, filename);
23302 break;
23304 case 'i':
23306 ptrdiff_t size = ZV - BEGV;
23307 pint2str (decode_mode_spec_buf, width, size);
23308 return decode_mode_spec_buf;
23311 case 'I':
23313 ptrdiff_t size = ZV - BEGV;
23314 pint2hrstr (decode_mode_spec_buf, width, size);
23315 return decode_mode_spec_buf;
23318 case 'l':
23320 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
23321 ptrdiff_t topline, nlines, height;
23322 ptrdiff_t junk;
23324 /* %c and %l are ignored in `frame-title-format'. */
23325 if (mode_line_target == MODE_LINE_TITLE)
23326 return "";
23328 startpos = marker_position (w->start);
23329 startpos_byte = marker_byte_position (w->start);
23330 height = WINDOW_TOTAL_LINES (w);
23332 /* If we decided that this buffer isn't suitable for line numbers,
23333 don't forget that too fast. */
23334 if (w->base_line_pos == -1)
23335 goto no_value;
23337 /* If the buffer is very big, don't waste time. */
23338 if (INTEGERP (Vline_number_display_limit)
23339 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
23341 w->base_line_pos = 0;
23342 w->base_line_number = 0;
23343 goto no_value;
23346 if (w->base_line_number > 0
23347 && w->base_line_pos > 0
23348 && w->base_line_pos <= startpos)
23350 line = w->base_line_number;
23351 linepos = w->base_line_pos;
23352 linepos_byte = buf_charpos_to_bytepos (b, linepos);
23354 else
23356 line = 1;
23357 linepos = BUF_BEGV (b);
23358 linepos_byte = BUF_BEGV_BYTE (b);
23361 /* Count lines from base line to window start position. */
23362 nlines = display_count_lines (linepos_byte,
23363 startpos_byte,
23364 startpos, &junk);
23366 topline = nlines + line;
23368 /* Determine a new base line, if the old one is too close
23369 or too far away, or if we did not have one.
23370 "Too close" means it's plausible a scroll-down would
23371 go back past it. */
23372 if (startpos == BUF_BEGV (b))
23374 w->base_line_number = topline;
23375 w->base_line_pos = BUF_BEGV (b);
23377 else if (nlines < height + 25 || nlines > height * 3 + 50
23378 || linepos == BUF_BEGV (b))
23380 ptrdiff_t limit = BUF_BEGV (b);
23381 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
23382 ptrdiff_t position;
23383 ptrdiff_t distance =
23384 (height * 2 + 30) * line_number_display_limit_width;
23386 if (startpos - distance > limit)
23388 limit = startpos - distance;
23389 limit_byte = CHAR_TO_BYTE (limit);
23392 nlines = display_count_lines (startpos_byte,
23393 limit_byte,
23394 - (height * 2 + 30),
23395 &position);
23396 /* If we couldn't find the lines we wanted within
23397 line_number_display_limit_width chars per line,
23398 give up on line numbers for this window. */
23399 if (position == limit_byte && limit == startpos - distance)
23401 w->base_line_pos = -1;
23402 w->base_line_number = 0;
23403 goto no_value;
23406 w->base_line_number = topline - nlines;
23407 w->base_line_pos = BYTE_TO_CHAR (position);
23410 /* Now count lines from the start pos to point. */
23411 nlines = display_count_lines (startpos_byte,
23412 PT_BYTE, PT, &junk);
23414 /* Record that we did display the line number. */
23415 line_number_displayed = 1;
23417 /* Make the string to show. */
23418 pint2str (decode_mode_spec_buf, width, topline + nlines);
23419 return decode_mode_spec_buf;
23420 no_value:
23422 char *p = decode_mode_spec_buf;
23423 int pad = width - 2;
23424 while (pad-- > 0)
23425 *p++ = ' ';
23426 *p++ = '?';
23427 *p++ = '?';
23428 *p = '\0';
23429 return decode_mode_spec_buf;
23432 break;
23434 case 'm':
23435 obj = BVAR (b, mode_name);
23436 break;
23438 case 'n':
23439 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
23440 return " Narrow";
23441 break;
23443 case 'p':
23445 ptrdiff_t pos = marker_position (w->start);
23446 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23448 if (w->window_end_pos <= BUF_Z (b) - BUF_ZV (b))
23450 if (pos <= BUF_BEGV (b))
23451 return "All";
23452 else
23453 return "Bottom";
23455 else if (pos <= BUF_BEGV (b))
23456 return "Top";
23457 else
23459 if (total > 1000000)
23460 /* Do it differently for a large value, to avoid overflow. */
23461 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23462 else
23463 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
23464 /* We can't normally display a 3-digit number,
23465 so get us a 2-digit number that is close. */
23466 if (total == 100)
23467 total = 99;
23468 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23469 return decode_mode_spec_buf;
23473 /* Display percentage of size above the bottom of the screen. */
23474 case 'P':
23476 ptrdiff_t toppos = marker_position (w->start);
23477 ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
23478 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
23480 if (botpos >= BUF_ZV (b))
23482 if (toppos <= BUF_BEGV (b))
23483 return "All";
23484 else
23485 return "Bottom";
23487 else
23489 if (total > 1000000)
23490 /* Do it differently for a large value, to avoid overflow. */
23491 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
23492 else
23493 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
23494 /* We can't normally display a 3-digit number,
23495 so get us a 2-digit number that is close. */
23496 if (total == 100)
23497 total = 99;
23498 if (toppos <= BUF_BEGV (b))
23499 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
23500 else
23501 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
23502 return decode_mode_spec_buf;
23506 case 's':
23507 /* status of process */
23508 obj = Fget_buffer_process (Fcurrent_buffer ());
23509 if (NILP (obj))
23510 return "no process";
23511 #ifndef MSDOS
23512 obj = Fsymbol_name (Fprocess_status (obj));
23513 #endif
23514 break;
23516 case '@':
23518 ptrdiff_t count = inhibit_garbage_collection ();
23519 Lisp_Object curdir = BVAR (current_buffer, directory);
23520 Lisp_Object val = Qnil;
23522 if (STRINGP (curdir))
23523 val = call1 (intern ("file-remote-p"), curdir);
23525 unbind_to (count, Qnil);
23527 if (NILP (val))
23528 return "-";
23529 else
23530 return "@";
23533 case 'z':
23534 /* coding-system (not including end-of-line format) */
23535 case 'Z':
23536 /* coding-system (including end-of-line type) */
23538 int eol_flag = (c == 'Z');
23539 char *p = decode_mode_spec_buf;
23541 if (! FRAME_WINDOW_P (f))
23543 /* No need to mention EOL here--the terminal never needs
23544 to do EOL conversion. */
23545 p = decode_mode_spec_coding (CODING_ID_NAME
23546 (FRAME_KEYBOARD_CODING (f)->id),
23547 p, 0);
23548 p = decode_mode_spec_coding (CODING_ID_NAME
23549 (FRAME_TERMINAL_CODING (f)->id),
23550 p, 0);
23552 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
23553 p, eol_flag);
23555 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
23556 #ifdef subprocesses
23557 obj = Fget_buffer_process (Fcurrent_buffer ());
23558 if (PROCESSP (obj))
23560 p = decode_mode_spec_coding
23561 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
23562 p = decode_mode_spec_coding
23563 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
23565 #endif /* subprocesses */
23566 #endif /* 0 */
23567 *p = 0;
23568 return decode_mode_spec_buf;
23572 if (STRINGP (obj))
23574 *string = obj;
23575 return SSDATA (obj);
23577 else
23578 return "";
23582 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
23583 means count lines back from START_BYTE. But don't go beyond
23584 LIMIT_BYTE. Return the number of lines thus found (always
23585 nonnegative).
23587 Set *BYTE_POS_PTR to the byte position where we stopped. This is
23588 either the position COUNT lines after/before START_BYTE, if we
23589 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
23590 COUNT lines. */
23592 static ptrdiff_t
23593 display_count_lines (ptrdiff_t start_byte,
23594 ptrdiff_t limit_byte, ptrdiff_t count,
23595 ptrdiff_t *byte_pos_ptr)
23597 register unsigned char *cursor;
23598 unsigned char *base;
23600 register ptrdiff_t ceiling;
23601 register unsigned char *ceiling_addr;
23602 ptrdiff_t orig_count = count;
23604 /* If we are not in selective display mode,
23605 check only for newlines. */
23606 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
23607 && !INTEGERP (BVAR (current_buffer, selective_display)));
23609 if (count > 0)
23611 while (start_byte < limit_byte)
23613 ceiling = BUFFER_CEILING_OF (start_byte);
23614 ceiling = min (limit_byte - 1, ceiling);
23615 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
23616 base = (cursor = BYTE_POS_ADDR (start_byte));
23620 if (selective_display)
23622 while (*cursor != '\n' && *cursor != 015
23623 && ++cursor != ceiling_addr)
23624 continue;
23625 if (cursor == ceiling_addr)
23626 break;
23628 else
23630 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
23631 if (! cursor)
23632 break;
23635 cursor++;
23637 if (--count == 0)
23639 start_byte += cursor - base;
23640 *byte_pos_ptr = start_byte;
23641 return orig_count;
23644 while (cursor < ceiling_addr);
23646 start_byte += ceiling_addr - base;
23649 else
23651 while (start_byte > limit_byte)
23653 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
23654 ceiling = max (limit_byte, ceiling);
23655 ceiling_addr = BYTE_POS_ADDR (ceiling);
23656 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
23657 while (1)
23659 if (selective_display)
23661 while (--cursor >= ceiling_addr
23662 && *cursor != '\n' && *cursor != 015)
23663 continue;
23664 if (cursor < ceiling_addr)
23665 break;
23667 else
23669 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
23670 if (! cursor)
23671 break;
23674 if (++count == 0)
23676 start_byte += cursor - base + 1;
23677 *byte_pos_ptr = start_byte;
23678 /* When scanning backwards, we should
23679 not count the newline posterior to which we stop. */
23680 return - orig_count - 1;
23683 start_byte += ceiling_addr - base;
23687 *byte_pos_ptr = limit_byte;
23689 if (count < 0)
23690 return - orig_count + count;
23691 return orig_count - count;
23697 /***********************************************************************
23698 Displaying strings
23699 ***********************************************************************/
23701 /* Display a NUL-terminated string, starting with index START.
23703 If STRING is non-null, display that C string. Otherwise, the Lisp
23704 string LISP_STRING is displayed. There's a case that STRING is
23705 non-null and LISP_STRING is not nil. It means STRING is a string
23706 data of LISP_STRING. In that case, we display LISP_STRING while
23707 ignoring its text properties.
23709 If FACE_STRING is not nil, FACE_STRING_POS is a position in
23710 FACE_STRING. Display STRING or LISP_STRING with the face at
23711 FACE_STRING_POS in FACE_STRING:
23713 Display the string in the environment given by IT, but use the
23714 standard display table, temporarily.
23716 FIELD_WIDTH is the minimum number of output glyphs to produce.
23717 If STRING has fewer characters than FIELD_WIDTH, pad to the right
23718 with spaces. If STRING has more characters, more than FIELD_WIDTH
23719 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
23721 PRECISION is the maximum number of characters to output from
23722 STRING. PRECISION < 0 means don't truncate the string.
23724 This is roughly equivalent to printf format specifiers:
23726 FIELD_WIDTH PRECISION PRINTF
23727 ----------------------------------------
23728 -1 -1 %s
23729 -1 10 %.10s
23730 10 -1 %10s
23731 20 10 %20.10s
23733 MULTIBYTE zero means do not display multibyte chars, > 0 means do
23734 display them, and < 0 means obey the current buffer's value of
23735 enable_multibyte_characters.
23737 Value is the number of columns displayed. */
23739 static int
23740 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
23741 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
23742 int field_width, int precision, int max_x, int multibyte)
23744 int hpos_at_start = it->hpos;
23745 int saved_face_id = it->face_id;
23746 struct glyph_row *row = it->glyph_row;
23747 ptrdiff_t it_charpos;
23749 /* Initialize the iterator IT for iteration over STRING beginning
23750 with index START. */
23751 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
23752 precision, field_width, multibyte);
23753 if (string && STRINGP (lisp_string))
23754 /* LISP_STRING is the one returned by decode_mode_spec. We should
23755 ignore its text properties. */
23756 it->stop_charpos = it->end_charpos;
23758 /* If displaying STRING, set up the face of the iterator from
23759 FACE_STRING, if that's given. */
23760 if (STRINGP (face_string))
23762 ptrdiff_t endptr;
23763 struct face *face;
23765 it->face_id
23766 = face_at_string_position (it->w, face_string, face_string_pos,
23767 0, &endptr, it->base_face_id, 0);
23768 face = FACE_FROM_ID (it->f, it->face_id);
23769 it->face_box_p = face->box != FACE_NO_BOX;
23772 /* Set max_x to the maximum allowed X position. Don't let it go
23773 beyond the right edge of the window. */
23774 if (max_x <= 0)
23775 max_x = it->last_visible_x;
23776 else
23777 max_x = min (max_x, it->last_visible_x);
23779 /* Skip over display elements that are not visible. because IT->w is
23780 hscrolled. */
23781 if (it->current_x < it->first_visible_x)
23782 move_it_in_display_line_to (it, 100000, it->first_visible_x,
23783 MOVE_TO_POS | MOVE_TO_X);
23785 row->ascent = it->max_ascent;
23786 row->height = it->max_ascent + it->max_descent;
23787 row->phys_ascent = it->max_phys_ascent;
23788 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
23789 row->extra_line_spacing = it->max_extra_line_spacing;
23791 if (STRINGP (it->string))
23792 it_charpos = IT_STRING_CHARPOS (*it);
23793 else
23794 it_charpos = IT_CHARPOS (*it);
23796 /* This condition is for the case that we are called with current_x
23797 past last_visible_x. */
23798 while (it->current_x < max_x)
23800 int x_before, x, n_glyphs_before, i, nglyphs;
23802 /* Get the next display element. */
23803 if (!get_next_display_element (it))
23804 break;
23806 /* Produce glyphs. */
23807 x_before = it->current_x;
23808 n_glyphs_before = row->used[TEXT_AREA];
23809 PRODUCE_GLYPHS (it);
23811 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
23812 i = 0;
23813 x = x_before;
23814 while (i < nglyphs)
23816 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
23818 if (it->line_wrap != TRUNCATE
23819 && x + glyph->pixel_width > max_x)
23821 /* End of continued line or max_x reached. */
23822 if (CHAR_GLYPH_PADDING_P (*glyph))
23824 /* A wide character is unbreakable. */
23825 if (row->reversed_p)
23826 unproduce_glyphs (it, row->used[TEXT_AREA]
23827 - n_glyphs_before);
23828 row->used[TEXT_AREA] = n_glyphs_before;
23829 it->current_x = x_before;
23831 else
23833 if (row->reversed_p)
23834 unproduce_glyphs (it, row->used[TEXT_AREA]
23835 - (n_glyphs_before + i));
23836 row->used[TEXT_AREA] = n_glyphs_before + i;
23837 it->current_x = x;
23839 break;
23841 else if (x + glyph->pixel_width >= it->first_visible_x)
23843 /* Glyph is at least partially visible. */
23844 ++it->hpos;
23845 if (x < it->first_visible_x)
23846 row->x = x - it->first_visible_x;
23848 else
23850 /* Glyph is off the left margin of the display area.
23851 Should not happen. */
23852 emacs_abort ();
23855 row->ascent = max (row->ascent, it->max_ascent);
23856 row->height = max (row->height, it->max_ascent + it->max_descent);
23857 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
23858 row->phys_height = max (row->phys_height,
23859 it->max_phys_ascent + it->max_phys_descent);
23860 row->extra_line_spacing = max (row->extra_line_spacing,
23861 it->max_extra_line_spacing);
23862 x += glyph->pixel_width;
23863 ++i;
23866 /* Stop if max_x reached. */
23867 if (i < nglyphs)
23868 break;
23870 /* Stop at line ends. */
23871 if (ITERATOR_AT_END_OF_LINE_P (it))
23873 it->continuation_lines_width = 0;
23874 break;
23877 set_iterator_to_next (it, 1);
23878 if (STRINGP (it->string))
23879 it_charpos = IT_STRING_CHARPOS (*it);
23880 else
23881 it_charpos = IT_CHARPOS (*it);
23883 /* Stop if truncating at the right edge. */
23884 if (it->line_wrap == TRUNCATE
23885 && it->current_x >= it->last_visible_x)
23887 /* Add truncation mark, but don't do it if the line is
23888 truncated at a padding space. */
23889 if (it_charpos < it->string_nchars)
23891 if (!FRAME_WINDOW_P (it->f))
23893 int ii, n;
23895 if (it->current_x > it->last_visible_x)
23897 if (!row->reversed_p)
23899 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
23900 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23901 break;
23903 else
23905 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
23906 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
23907 break;
23908 unproduce_glyphs (it, ii + 1);
23909 ii = row->used[TEXT_AREA] - (ii + 1);
23911 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
23913 row->used[TEXT_AREA] = ii;
23914 produce_special_glyphs (it, IT_TRUNCATION);
23917 produce_special_glyphs (it, IT_TRUNCATION);
23919 row->truncated_on_right_p = 1;
23921 break;
23925 /* Maybe insert a truncation at the left. */
23926 if (it->first_visible_x
23927 && it_charpos > 0)
23929 if (!FRAME_WINDOW_P (it->f)
23930 || (row->reversed_p
23931 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
23932 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
23933 insert_left_trunc_glyphs (it);
23934 row->truncated_on_left_p = 1;
23937 it->face_id = saved_face_id;
23939 /* Value is number of columns displayed. */
23940 return it->hpos - hpos_at_start;
23945 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
23946 appears as an element of LIST or as the car of an element of LIST.
23947 If PROPVAL is a list, compare each element against LIST in that
23948 way, and return 1/2 if any element of PROPVAL is found in LIST.
23949 Otherwise return 0. This function cannot quit.
23950 The return value is 2 if the text is invisible but with an ellipsis
23951 and 1 if it's invisible and without an ellipsis. */
23954 invisible_p (register Lisp_Object propval, Lisp_Object list)
23956 register Lisp_Object tail, proptail;
23958 for (tail = list; CONSP (tail); tail = XCDR (tail))
23960 register Lisp_Object tem;
23961 tem = XCAR (tail);
23962 if (EQ (propval, tem))
23963 return 1;
23964 if (CONSP (tem) && EQ (propval, XCAR (tem)))
23965 return NILP (XCDR (tem)) ? 1 : 2;
23968 if (CONSP (propval))
23970 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
23972 Lisp_Object propelt;
23973 propelt = XCAR (proptail);
23974 for (tail = list; CONSP (tail); tail = XCDR (tail))
23976 register Lisp_Object tem;
23977 tem = XCAR (tail);
23978 if (EQ (propelt, tem))
23979 return 1;
23980 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
23981 return NILP (XCDR (tem)) ? 1 : 2;
23986 return 0;
23989 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
23990 doc: /* Non-nil if the property makes the text invisible.
23991 POS-OR-PROP can be a marker or number, in which case it is taken to be
23992 a position in the current buffer and the value of the `invisible' property
23993 is checked; or it can be some other value, which is then presumed to be the
23994 value of the `invisible' property of the text of interest.
23995 The non-nil value returned can be t for truly invisible text or something
23996 else if the text is replaced by an ellipsis. */)
23997 (Lisp_Object pos_or_prop)
23999 Lisp_Object prop
24000 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
24001 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
24002 : pos_or_prop);
24003 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
24004 return (invis == 0 ? Qnil
24005 : invis == 1 ? Qt
24006 : make_number (invis));
24009 /* Calculate a width or height in pixels from a specification using
24010 the following elements:
24012 SPEC ::=
24013 NUM - a (fractional) multiple of the default font width/height
24014 (NUM) - specifies exactly NUM pixels
24015 UNIT - a fixed number of pixels, see below.
24016 ELEMENT - size of a display element in pixels, see below.
24017 (NUM . SPEC) - equals NUM * SPEC
24018 (+ SPEC SPEC ...) - add pixel values
24019 (- SPEC SPEC ...) - subtract pixel values
24020 (- SPEC) - negate pixel value
24022 NUM ::=
24023 INT or FLOAT - a number constant
24024 SYMBOL - use symbol's (buffer local) variable binding.
24026 UNIT ::=
24027 in - pixels per inch *)
24028 mm - pixels per 1/1000 meter *)
24029 cm - pixels per 1/100 meter *)
24030 width - width of current font in pixels.
24031 height - height of current font in pixels.
24033 *) using the ratio(s) defined in display-pixels-per-inch.
24035 ELEMENT ::=
24037 left-fringe - left fringe width in pixels
24038 right-fringe - right fringe width in pixels
24040 left-margin - left margin width in pixels
24041 right-margin - right margin width in pixels
24043 scroll-bar - scroll-bar area width in pixels
24045 Examples:
24047 Pixels corresponding to 5 inches:
24048 (5 . in)
24050 Total width of non-text areas on left side of window (if scroll-bar is on left):
24051 '(space :width (+ left-fringe left-margin scroll-bar))
24053 Align to first text column (in header line):
24054 '(space :align-to 0)
24056 Align to middle of text area minus half the width of variable `my-image'
24057 containing a loaded image:
24058 '(space :align-to (0.5 . (- text my-image)))
24060 Width of left margin minus width of 1 character in the default font:
24061 '(space :width (- left-margin 1))
24063 Width of left margin minus width of 2 characters in the current font:
24064 '(space :width (- left-margin (2 . width)))
24066 Center 1 character over left-margin (in header line):
24067 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
24069 Different ways to express width of left fringe plus left margin minus one pixel:
24070 '(space :width (- (+ left-fringe left-margin) (1)))
24071 '(space :width (+ left-fringe left-margin (- (1))))
24072 '(space :width (+ left-fringe left-margin (-1)))
24076 static int
24077 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
24078 struct font *font, int width_p, int *align_to)
24080 double pixels;
24082 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
24083 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
24085 if (NILP (prop))
24086 return OK_PIXELS (0);
24088 eassert (FRAME_LIVE_P (it->f));
24090 if (SYMBOLP (prop))
24092 if (SCHARS (SYMBOL_NAME (prop)) == 2)
24094 char *unit = SSDATA (SYMBOL_NAME (prop));
24096 if (unit[0] == 'i' && unit[1] == 'n')
24097 pixels = 1.0;
24098 else if (unit[0] == 'm' && unit[1] == 'm')
24099 pixels = 25.4;
24100 else if (unit[0] == 'c' && unit[1] == 'm')
24101 pixels = 2.54;
24102 else
24103 pixels = 0;
24104 if (pixels > 0)
24106 double ppi = (width_p ? FRAME_RES_X (it->f)
24107 : FRAME_RES_Y (it->f));
24109 if (ppi > 0)
24110 return OK_PIXELS (ppi / pixels);
24111 return 0;
24115 #ifdef HAVE_WINDOW_SYSTEM
24116 if (EQ (prop, Qheight))
24117 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
24118 if (EQ (prop, Qwidth))
24119 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
24120 #else
24121 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
24122 return OK_PIXELS (1);
24123 #endif
24125 if (EQ (prop, Qtext))
24126 return OK_PIXELS (width_p
24127 ? window_box_width (it->w, TEXT_AREA)
24128 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
24130 if (align_to && *align_to < 0)
24132 *res = 0;
24133 if (EQ (prop, Qleft))
24134 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
24135 if (EQ (prop, Qright))
24136 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
24137 if (EQ (prop, Qcenter))
24138 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
24139 + window_box_width (it->w, TEXT_AREA) / 2);
24140 if (EQ (prop, Qleft_fringe))
24141 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24142 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
24143 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
24144 if (EQ (prop, Qright_fringe))
24145 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24146 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24147 : window_box_right_offset (it->w, TEXT_AREA));
24148 if (EQ (prop, Qleft_margin))
24149 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
24150 if (EQ (prop, Qright_margin))
24151 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
24152 if (EQ (prop, Qscroll_bar))
24153 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
24155 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
24156 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
24157 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
24158 : 0)));
24160 else
24162 if (EQ (prop, Qleft_fringe))
24163 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
24164 if (EQ (prop, Qright_fringe))
24165 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
24166 if (EQ (prop, Qleft_margin))
24167 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
24168 if (EQ (prop, Qright_margin))
24169 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
24170 if (EQ (prop, Qscroll_bar))
24171 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
24174 prop = buffer_local_value (prop, it->w->contents);
24175 if (EQ (prop, Qunbound))
24176 prop = Qnil;
24179 if (INTEGERP (prop) || FLOATP (prop))
24181 int base_unit = (width_p
24182 ? FRAME_COLUMN_WIDTH (it->f)
24183 : FRAME_LINE_HEIGHT (it->f));
24184 return OK_PIXELS (XFLOATINT (prop) * base_unit);
24187 if (CONSP (prop))
24189 Lisp_Object car = XCAR (prop);
24190 Lisp_Object cdr = XCDR (prop);
24192 if (SYMBOLP (car))
24194 #ifdef HAVE_WINDOW_SYSTEM
24195 if (FRAME_WINDOW_P (it->f)
24196 && valid_image_p (prop))
24198 ptrdiff_t id = lookup_image (it->f, prop);
24199 struct image *img = IMAGE_FROM_ID (it->f, id);
24201 return OK_PIXELS (width_p ? img->width : img->height);
24203 #ifdef HAVE_XWIDGETS
24204 if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
24206 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
24207 return OK_PIXELS (width_p ? 100 : 100);
24209 #endif
24210 #endif
24211 if (EQ (car, Qplus) || EQ (car, Qminus))
24213 int first = 1;
24214 double px;
24216 pixels = 0;
24217 while (CONSP (cdr))
24219 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
24220 font, width_p, align_to))
24221 return 0;
24222 if (first)
24223 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
24224 else
24225 pixels += px;
24226 cdr = XCDR (cdr);
24228 if (EQ (car, Qminus))
24229 pixels = -pixels;
24230 return OK_PIXELS (pixels);
24233 car = buffer_local_value (car, it->w->contents);
24234 if (EQ (car, Qunbound))
24235 car = Qnil;
24238 if (INTEGERP (car) || FLOATP (car))
24240 double fact;
24241 pixels = XFLOATINT (car);
24242 if (NILP (cdr))
24243 return OK_PIXELS (pixels);
24244 if (calc_pixel_width_or_height (&fact, it, cdr,
24245 font, width_p, align_to))
24246 return OK_PIXELS (pixels * fact);
24247 return 0;
24250 return 0;
24253 return 0;
24257 /***********************************************************************
24258 Glyph Display
24259 ***********************************************************************/
24261 #ifdef HAVE_WINDOW_SYSTEM
24263 #ifdef GLYPH_DEBUG
24265 void
24266 dump_glyph_string (struct glyph_string *s)
24268 fprintf (stderr, "glyph string\n");
24269 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
24270 s->x, s->y, s->width, s->height);
24271 fprintf (stderr, " ybase = %d\n", s->ybase);
24272 fprintf (stderr, " hl = %d\n", s->hl);
24273 fprintf (stderr, " left overhang = %d, right = %d\n",
24274 s->left_overhang, s->right_overhang);
24275 fprintf (stderr, " nchars = %d\n", s->nchars);
24276 fprintf (stderr, " extends to end of line = %d\n",
24277 s->extends_to_end_of_line_p);
24278 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
24279 fprintf (stderr, " bg width = %d\n", s->background_width);
24282 #endif /* GLYPH_DEBUG */
24284 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
24285 of XChar2b structures for S; it can't be allocated in
24286 init_glyph_string because it must be allocated via `alloca'. W
24287 is the window on which S is drawn. ROW and AREA are the glyph row
24288 and area within the row from which S is constructed. START is the
24289 index of the first glyph structure covered by S. HL is a
24290 face-override for drawing S. */
24292 #ifdef HAVE_NTGUI
24293 #define OPTIONAL_HDC(hdc) HDC hdc,
24294 #define DECLARE_HDC(hdc) HDC hdc;
24295 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
24296 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
24297 #endif
24299 #ifndef OPTIONAL_HDC
24300 #define OPTIONAL_HDC(hdc)
24301 #define DECLARE_HDC(hdc)
24302 #define ALLOCATE_HDC(hdc, f)
24303 #define RELEASE_HDC(hdc, f)
24304 #endif
24306 static void
24307 init_glyph_string (struct glyph_string *s,
24308 OPTIONAL_HDC (hdc)
24309 XChar2b *char2b, struct window *w, struct glyph_row *row,
24310 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
24312 memset (s, 0, sizeof *s);
24313 s->w = w;
24314 s->f = XFRAME (w->frame);
24315 #ifdef HAVE_NTGUI
24316 s->hdc = hdc;
24317 #endif
24318 s->display = FRAME_X_DISPLAY (s->f);
24319 s->window = FRAME_X_WINDOW (s->f);
24320 s->char2b = char2b;
24321 s->hl = hl;
24322 s->row = row;
24323 s->area = area;
24324 s->first_glyph = row->glyphs[area] + start;
24325 s->height = row->height;
24326 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
24327 s->ybase = s->y + row->ascent;
24331 /* Append the list of glyph strings with head H and tail T to the list
24332 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
24334 static void
24335 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24336 struct glyph_string *h, struct glyph_string *t)
24338 if (h)
24340 if (*head)
24341 (*tail)->next = h;
24342 else
24343 *head = h;
24344 h->prev = *tail;
24345 *tail = t;
24350 /* Prepend the list of glyph strings with head H and tail T to the
24351 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
24352 result. */
24354 static void
24355 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
24356 struct glyph_string *h, struct glyph_string *t)
24358 if (h)
24360 if (*head)
24361 (*head)->prev = t;
24362 else
24363 *tail = t;
24364 t->next = *head;
24365 *head = h;
24370 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
24371 Set *HEAD and *TAIL to the resulting list. */
24373 static void
24374 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
24375 struct glyph_string *s)
24377 s->next = s->prev = NULL;
24378 append_glyph_string_lists (head, tail, s, s);
24382 /* Get face and two-byte form of character C in face FACE_ID on frame F.
24383 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
24384 make sure that X resources for the face returned are allocated.
24385 Value is a pointer to a realized face that is ready for display if
24386 DISPLAY_P is non-zero. */
24388 static struct face *
24389 get_char_face_and_encoding (struct frame *f, int c, int face_id,
24390 XChar2b *char2b, int display_p)
24392 struct face *face = FACE_FROM_ID (f, face_id);
24393 unsigned code = 0;
24395 if (face->font)
24397 code = face->font->driver->encode_char (face->font, c);
24399 if (code == FONT_INVALID_CODE)
24400 code = 0;
24402 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24404 /* Make sure X resources of the face are allocated. */
24405 #ifdef HAVE_X_WINDOWS
24406 if (display_p)
24407 #endif
24409 eassert (face != NULL);
24410 prepare_face_for_display (f, face);
24413 return face;
24417 /* Get face and two-byte form of character glyph GLYPH on frame F.
24418 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
24419 a pointer to a realized face that is ready for display. */
24421 static struct face *
24422 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
24423 XChar2b *char2b, int *two_byte_p)
24425 struct face *face;
24426 unsigned code = 0;
24428 eassert (glyph->type == CHAR_GLYPH);
24429 face = FACE_FROM_ID (f, glyph->face_id);
24431 /* Make sure X resources of the face are allocated. */
24432 eassert (face != NULL);
24433 prepare_face_for_display (f, face);
24435 if (two_byte_p)
24436 *two_byte_p = 0;
24438 if (face->font)
24440 if (CHAR_BYTE8_P (glyph->u.ch))
24441 code = CHAR_TO_BYTE8 (glyph->u.ch);
24442 else
24443 code = face->font->driver->encode_char (face->font, glyph->u.ch);
24445 if (code == FONT_INVALID_CODE)
24446 code = 0;
24449 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24450 return face;
24454 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
24455 Return 1 if FONT has a glyph for C, otherwise return 0. */
24457 static int
24458 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
24460 unsigned code;
24462 if (CHAR_BYTE8_P (c))
24463 code = CHAR_TO_BYTE8 (c);
24464 else
24465 code = font->driver->encode_char (font, c);
24467 if (code == FONT_INVALID_CODE)
24468 return 0;
24469 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
24470 return 1;
24474 /* Fill glyph string S with composition components specified by S->cmp.
24476 BASE_FACE is the base face of the composition.
24477 S->cmp_from is the index of the first component for S.
24479 OVERLAPS non-zero means S should draw the foreground only, and use
24480 its physical height for clipping. See also draw_glyphs.
24482 Value is the index of a component not in S. */
24484 static int
24485 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
24486 int overlaps)
24488 int i;
24489 /* For all glyphs of this composition, starting at the offset
24490 S->cmp_from, until we reach the end of the definition or encounter a
24491 glyph that requires the different face, add it to S. */
24492 struct face *face;
24494 eassert (s);
24496 s->for_overlaps = overlaps;
24497 s->face = NULL;
24498 s->font = NULL;
24499 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
24501 int c = COMPOSITION_GLYPH (s->cmp, i);
24503 /* TAB in a composition means display glyphs with padding space
24504 on the left or right. */
24505 if (c != '\t')
24507 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
24508 -1, Qnil);
24510 face = get_char_face_and_encoding (s->f, c, face_id,
24511 s->char2b + i, 1);
24512 if (face)
24514 if (! s->face)
24516 s->face = face;
24517 s->font = s->face->font;
24519 else if (s->face != face)
24520 break;
24523 ++s->nchars;
24525 s->cmp_to = i;
24527 if (s->face == NULL)
24529 s->face = base_face->ascii_face;
24530 s->font = s->face->font;
24533 /* All glyph strings for the same composition has the same width,
24534 i.e. the width set for the first component of the composition. */
24535 s->width = s->first_glyph->pixel_width;
24537 /* If the specified font could not be loaded, use the frame's
24538 default font, but record the fact that we couldn't load it in
24539 the glyph string so that we can draw rectangles for the
24540 characters of the glyph string. */
24541 if (s->font == NULL)
24543 s->font_not_found_p = 1;
24544 s->font = FRAME_FONT (s->f);
24547 /* Adjust base line for subscript/superscript text. */
24548 s->ybase += s->first_glyph->voffset;
24550 /* This glyph string must always be drawn with 16-bit functions. */
24551 s->two_byte_p = 1;
24553 return s->cmp_to;
24556 static int
24557 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
24558 int start, int end, int overlaps)
24560 struct glyph *glyph, *last;
24561 Lisp_Object lgstring;
24562 int i;
24564 s->for_overlaps = overlaps;
24565 glyph = s->row->glyphs[s->area] + start;
24566 last = s->row->glyphs[s->area] + end;
24567 s->cmp_id = glyph->u.cmp.id;
24568 s->cmp_from = glyph->slice.cmp.from;
24569 s->cmp_to = glyph->slice.cmp.to + 1;
24570 s->face = FACE_FROM_ID (s->f, face_id);
24571 lgstring = composition_gstring_from_id (s->cmp_id);
24572 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
24573 glyph++;
24574 while (glyph < last
24575 && glyph->u.cmp.automatic
24576 && glyph->u.cmp.id == s->cmp_id
24577 && s->cmp_to == glyph->slice.cmp.from)
24578 s->cmp_to = (glyph++)->slice.cmp.to + 1;
24580 for (i = s->cmp_from; i < s->cmp_to; i++)
24582 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
24583 unsigned code = LGLYPH_CODE (lglyph);
24585 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
24587 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
24588 return glyph - s->row->glyphs[s->area];
24592 /* Fill glyph string S from a sequence glyphs for glyphless characters.
24593 See the comment of fill_glyph_string for arguments.
24594 Value is the index of the first glyph not in S. */
24597 static int
24598 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
24599 int start, int end, int overlaps)
24601 struct glyph *glyph, *last;
24602 int voffset;
24604 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
24605 s->for_overlaps = overlaps;
24606 glyph = s->row->glyphs[s->area] + start;
24607 last = s->row->glyphs[s->area] + end;
24608 voffset = glyph->voffset;
24609 s->face = FACE_FROM_ID (s->f, face_id);
24610 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
24611 s->nchars = 1;
24612 s->width = glyph->pixel_width;
24613 glyph++;
24614 while (glyph < last
24615 && glyph->type == GLYPHLESS_GLYPH
24616 && glyph->voffset == voffset
24617 && glyph->face_id == face_id)
24619 s->nchars++;
24620 s->width += glyph->pixel_width;
24621 glyph++;
24623 s->ybase += voffset;
24624 return glyph - s->row->glyphs[s->area];
24628 /* Fill glyph string S from a sequence of character glyphs.
24630 FACE_ID is the face id of the string. START is the index of the
24631 first glyph to consider, END is the index of the last + 1.
24632 OVERLAPS non-zero means S should draw the foreground only, and use
24633 its physical height for clipping. See also draw_glyphs.
24635 Value is the index of the first glyph not in S. */
24637 static int
24638 fill_glyph_string (struct glyph_string *s, int face_id,
24639 int start, int end, int overlaps)
24641 struct glyph *glyph, *last;
24642 int voffset;
24643 int glyph_not_available_p;
24645 eassert (s->f == XFRAME (s->w->frame));
24646 eassert (s->nchars == 0);
24647 eassert (start >= 0 && end > start);
24649 s->for_overlaps = overlaps;
24650 glyph = s->row->glyphs[s->area] + start;
24651 last = s->row->glyphs[s->area] + end;
24652 voffset = glyph->voffset;
24653 s->padding_p = glyph->padding_p;
24654 glyph_not_available_p = glyph->glyph_not_available_p;
24656 while (glyph < last
24657 && glyph->type == CHAR_GLYPH
24658 && glyph->voffset == voffset
24659 /* Same face id implies same font, nowadays. */
24660 && glyph->face_id == face_id
24661 && glyph->glyph_not_available_p == glyph_not_available_p)
24663 int two_byte_p;
24665 s->face = get_glyph_face_and_encoding (s->f, glyph,
24666 s->char2b + s->nchars,
24667 &two_byte_p);
24668 s->two_byte_p = two_byte_p;
24669 ++s->nchars;
24670 eassert (s->nchars <= end - start);
24671 s->width += glyph->pixel_width;
24672 if (glyph++->padding_p != s->padding_p)
24673 break;
24676 s->font = s->face->font;
24678 /* If the specified font could not be loaded, use the frame's font,
24679 but record the fact that we couldn't load it in
24680 S->font_not_found_p so that we can draw rectangles for the
24681 characters of the glyph string. */
24682 if (s->font == NULL || glyph_not_available_p)
24684 s->font_not_found_p = 1;
24685 s->font = FRAME_FONT (s->f);
24688 /* Adjust base line for subscript/superscript text. */
24689 s->ybase += voffset;
24691 eassert (s->face && s->face->gc);
24692 return glyph - s->row->glyphs[s->area];
24696 /* Fill glyph string S from image glyph S->first_glyph. */
24698 static void
24699 fill_image_glyph_string (struct glyph_string *s)
24701 eassert (s->first_glyph->type == IMAGE_GLYPH);
24702 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
24703 eassert (s->img);
24704 s->slice = s->first_glyph->slice.img;
24705 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24706 s->font = s->face->font;
24707 s->width = s->first_glyph->pixel_width;
24709 /* Adjust base line for subscript/superscript text. */
24710 s->ybase += s->first_glyph->voffset;
24713 #ifdef HAVE_XWIDGETS
24714 static void
24715 fill_xwidget_glyph_string (struct glyph_string *s)
24717 eassert (s->first_glyph->type == XWIDGET_GLYPH);
24718 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
24719 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
24720 s->font = s->face->font;
24721 s->width = s->first_glyph->pixel_width;
24722 s->ybase += s->first_glyph->voffset;
24723 s->xwidget = s->first_glyph->u.xwidget;
24724 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
24726 #endif
24727 /* Fill glyph string S from a sequence of stretch glyphs.
24729 START is the index of the first glyph to consider,
24730 END is the index of the last + 1.
24732 Value is the index of the first glyph not in S. */
24734 static int
24735 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
24737 struct glyph *glyph, *last;
24738 int voffset, face_id;
24740 eassert (s->first_glyph->type == STRETCH_GLYPH);
24742 glyph = s->row->glyphs[s->area] + start;
24743 last = s->row->glyphs[s->area] + end;
24744 face_id = glyph->face_id;
24745 s->face = FACE_FROM_ID (s->f, face_id);
24746 s->font = s->face->font;
24747 s->width = glyph->pixel_width;
24748 s->nchars = 1;
24749 voffset = glyph->voffset;
24751 for (++glyph;
24752 (glyph < last
24753 && glyph->type == STRETCH_GLYPH
24754 && glyph->voffset == voffset
24755 && glyph->face_id == face_id);
24756 ++glyph)
24757 s->width += glyph->pixel_width;
24759 /* Adjust base line for subscript/superscript text. */
24760 s->ybase += voffset;
24762 /* The case that face->gc == 0 is handled when drawing the glyph
24763 string by calling prepare_face_for_display. */
24764 eassert (s->face);
24765 return glyph - s->row->glyphs[s->area];
24768 static struct font_metrics *
24769 get_per_char_metric (struct font *font, XChar2b *char2b)
24771 static struct font_metrics metrics;
24772 unsigned code;
24774 if (! font)
24775 return NULL;
24776 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
24777 if (code == FONT_INVALID_CODE)
24778 return NULL;
24779 font->driver->text_extents (font, &code, 1, &metrics);
24780 return &metrics;
24783 /* EXPORT for RIF:
24784 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
24785 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
24786 assumed to be zero. */
24788 void
24789 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
24791 *left = *right = 0;
24793 if (glyph->type == CHAR_GLYPH)
24795 struct face *face;
24796 XChar2b char2b;
24797 struct font_metrics *pcm;
24799 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
24800 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
24802 if (pcm->rbearing > pcm->width)
24803 *right = pcm->rbearing - pcm->width;
24804 if (pcm->lbearing < 0)
24805 *left = -pcm->lbearing;
24808 else if (glyph->type == COMPOSITE_GLYPH)
24810 if (! glyph->u.cmp.automatic)
24812 struct composition *cmp = composition_table[glyph->u.cmp.id];
24814 if (cmp->rbearing > cmp->pixel_width)
24815 *right = cmp->rbearing - cmp->pixel_width;
24816 if (cmp->lbearing < 0)
24817 *left = - cmp->lbearing;
24819 else
24821 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
24822 struct font_metrics metrics;
24824 composition_gstring_width (gstring, glyph->slice.cmp.from,
24825 glyph->slice.cmp.to + 1, &metrics);
24826 if (metrics.rbearing > metrics.width)
24827 *right = metrics.rbearing - metrics.width;
24828 if (metrics.lbearing < 0)
24829 *left = - metrics.lbearing;
24835 /* Return the index of the first glyph preceding glyph string S that
24836 is overwritten by S because of S's left overhang. Value is -1
24837 if no glyphs are overwritten. */
24839 static int
24840 left_overwritten (struct glyph_string *s)
24842 int k;
24844 if (s->left_overhang)
24846 int x = 0, i;
24847 struct glyph *glyphs = s->row->glyphs[s->area];
24848 int first = s->first_glyph - glyphs;
24850 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
24851 x -= glyphs[i].pixel_width;
24853 k = i + 1;
24855 else
24856 k = -1;
24858 return k;
24862 /* Return the index of the first glyph preceding glyph string S that
24863 is overwriting S because of its right overhang. Value is -1 if no
24864 glyph in front of S overwrites S. */
24866 static int
24867 left_overwriting (struct glyph_string *s)
24869 int i, k, x;
24870 struct glyph *glyphs = s->row->glyphs[s->area];
24871 int first = s->first_glyph - glyphs;
24873 k = -1;
24874 x = 0;
24875 for (i = first - 1; i >= 0; --i)
24877 int left, right;
24878 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24879 if (x + right > 0)
24880 k = i;
24881 x -= glyphs[i].pixel_width;
24884 return k;
24888 /* Return the index of the last glyph following glyph string S that is
24889 overwritten by S because of S's right overhang. Value is -1 if
24890 no such glyph is found. */
24892 static int
24893 right_overwritten (struct glyph_string *s)
24895 int k = -1;
24897 if (s->right_overhang)
24899 int x = 0, i;
24900 struct glyph *glyphs = s->row->glyphs[s->area];
24901 int first = (s->first_glyph - glyphs
24902 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24903 int end = s->row->used[s->area];
24905 for (i = first; i < end && s->right_overhang > x; ++i)
24906 x += glyphs[i].pixel_width;
24908 k = i;
24911 return k;
24915 /* Return the index of the last glyph following glyph string S that
24916 overwrites S because of its left overhang. Value is negative
24917 if no such glyph is found. */
24919 static int
24920 right_overwriting (struct glyph_string *s)
24922 int i, k, x;
24923 int end = s->row->used[s->area];
24924 struct glyph *glyphs = s->row->glyphs[s->area];
24925 int first = (s->first_glyph - glyphs
24926 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
24928 k = -1;
24929 x = 0;
24930 for (i = first; i < end; ++i)
24932 int left, right;
24933 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
24934 if (x - left < 0)
24935 k = i;
24936 x += glyphs[i].pixel_width;
24939 return k;
24943 /* Set background width of glyph string S. START is the index of the
24944 first glyph following S. LAST_X is the right-most x-position + 1
24945 in the drawing area. */
24947 static void
24948 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
24950 /* If the face of this glyph string has to be drawn to the end of
24951 the drawing area, set S->extends_to_end_of_line_p. */
24953 if (start == s->row->used[s->area]
24954 && ((s->row->fill_line_p
24955 && (s->hl == DRAW_NORMAL_TEXT
24956 || s->hl == DRAW_IMAGE_RAISED
24957 || s->hl == DRAW_IMAGE_SUNKEN))
24958 || s->hl == DRAW_MOUSE_FACE))
24959 s->extends_to_end_of_line_p = 1;
24961 /* If S extends its face to the end of the line, set its
24962 background_width to the distance to the right edge of the drawing
24963 area. */
24964 if (s->extends_to_end_of_line_p)
24965 s->background_width = last_x - s->x + 1;
24966 else
24967 s->background_width = s->width;
24971 /* Compute overhangs and x-positions for glyph string S and its
24972 predecessors, or successors. X is the starting x-position for S.
24973 BACKWARD_P non-zero means process predecessors. */
24975 static void
24976 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
24978 if (backward_p)
24980 while (s)
24982 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24983 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24984 x -= s->width;
24985 s->x = x;
24986 s = s->prev;
24989 else
24991 while (s)
24993 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
24994 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
24995 s->x = x;
24996 x += s->width;
24997 s = s->next;
25004 /* The following macros are only called from draw_glyphs below.
25005 They reference the following parameters of that function directly:
25006 `w', `row', `area', and `overlap_p'
25007 as well as the following local variables:
25008 `s', `f', and `hdc' (in W32) */
25010 #ifdef HAVE_NTGUI
25011 /* On W32, silently add local `hdc' variable to argument list of
25012 init_glyph_string. */
25013 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25014 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
25015 #else
25016 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
25017 init_glyph_string (s, char2b, w, row, area, start, hl)
25018 #endif
25020 /* Add a glyph string for a stretch glyph to the list of strings
25021 between HEAD and TAIL. START is the index of the stretch glyph in
25022 row area AREA of glyph row ROW. END is the index of the last glyph
25023 in that glyph row area. X is the current output position assigned
25024 to the new glyph string constructed. HL overrides that face of the
25025 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25026 is the right-most x-position of the drawing area. */
25028 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
25029 and below -- keep them on one line. */
25030 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25031 do \
25033 s = alloca (sizeof *s); \
25034 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25035 START = fill_stretch_glyph_string (s, START, END); \
25036 append_glyph_string (&HEAD, &TAIL, s); \
25037 s->x = (X); \
25039 while (0)
25042 /* Add a glyph string for an image glyph to the list of strings
25043 between HEAD and TAIL. START is the index of the image glyph in
25044 row area AREA of glyph row ROW. END is the index of the last glyph
25045 in that glyph row area. X is the current output position assigned
25046 to the new glyph string constructed. HL overrides that face of the
25047 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
25048 is the right-most x-position of the drawing area. */
25050 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25051 do \
25053 s = alloca (sizeof *s); \
25054 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25055 fill_image_glyph_string (s); \
25056 append_glyph_string (&HEAD, &TAIL, s); \
25057 ++START; \
25058 s->x = (X); \
25060 while (0)
25062 #ifdef HAVE_XWIDGETS
25063 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25064 do \
25066 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
25067 s = (struct glyph_string *) alloca (sizeof *s); \
25068 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25069 fill_xwidget_glyph_string (s); \
25070 append_glyph_string (&HEAD, &TAIL, s); \
25071 ++START; \
25072 s->x = (X); \
25074 while (0)
25075 #endif
25078 /* Add a glyph string for a sequence of character glyphs to the list
25079 of strings between HEAD and TAIL. START is the index of the first
25080 glyph in row area AREA of glyph row ROW that is part of the new
25081 glyph string. END is the index of the last glyph in that glyph row
25082 area. X is the current output position assigned to the new glyph
25083 string constructed. HL overrides that face of the glyph; e.g. it
25084 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
25085 right-most x-position of the drawing area. */
25087 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25088 do \
25090 int face_id; \
25091 XChar2b *char2b; \
25093 face_id = (row)->glyphs[area][START].face_id; \
25095 s = alloca (sizeof *s); \
25096 SAFE_NALLOCA (char2b, 1, (END) - (START)); \
25097 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25098 append_glyph_string (&HEAD, &TAIL, s); \
25099 s->x = (X); \
25100 START = fill_glyph_string (s, face_id, START, END, overlaps); \
25102 while (0)
25105 /* Add a glyph string for a composite sequence to the list of strings
25106 between HEAD and TAIL. START is the index of the first glyph in
25107 row area AREA of glyph row ROW that is part of the new glyph
25108 string. END is the index of the last glyph in that glyph row area.
25109 X is the current output position assigned to the new glyph string
25110 constructed. HL overrides that face of the glyph; e.g. it is
25111 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
25112 x-position of the drawing area. */
25114 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25115 do { \
25116 int face_id = (row)->glyphs[area][START].face_id; \
25117 struct face *base_face = FACE_FROM_ID (f, face_id); \
25118 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
25119 struct composition *cmp = composition_table[cmp_id]; \
25120 XChar2b *char2b; \
25121 struct glyph_string *first_s = NULL; \
25122 int n; \
25124 SAFE_NALLOCA (char2b, 1, cmp->glyph_len); \
25126 /* Make glyph_strings for each glyph sequence that is drawable by \
25127 the same face, and append them to HEAD/TAIL. */ \
25128 for (n = 0; n < cmp->glyph_len;) \
25130 s = alloca (sizeof *s); \
25131 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25132 append_glyph_string (&(HEAD), &(TAIL), s); \
25133 s->cmp = cmp; \
25134 s->cmp_from = n; \
25135 s->x = (X); \
25136 if (n == 0) \
25137 first_s = s; \
25138 n = fill_composite_glyph_string (s, base_face, overlaps); \
25141 ++START; \
25142 s = first_s; \
25143 } while (0)
25146 /* Add a glyph string for a glyph-string sequence to the list of strings
25147 between HEAD and TAIL. */
25149 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25150 do { \
25151 int face_id; \
25152 XChar2b *char2b; \
25153 Lisp_Object gstring; \
25155 face_id = (row)->glyphs[area][START].face_id; \
25156 gstring = (composition_gstring_from_id \
25157 ((row)->glyphs[area][START].u.cmp.id)); \
25158 s = alloca (sizeof *s); \
25159 SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring)); \
25160 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
25161 append_glyph_string (&(HEAD), &(TAIL), s); \
25162 s->x = (X); \
25163 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
25164 } while (0)
25167 /* Add a glyph string for a sequence of glyphless character's glyphs
25168 to the list of strings between HEAD and TAIL. The meanings of
25169 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
25171 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
25172 do \
25174 int face_id; \
25176 face_id = (row)->glyphs[area][START].face_id; \
25178 s = alloca (sizeof *s); \
25179 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
25180 append_glyph_string (&HEAD, &TAIL, s); \
25181 s->x = (X); \
25182 START = fill_glyphless_glyph_string (s, face_id, START, END, \
25183 overlaps); \
25185 while (0)
25188 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
25189 of AREA of glyph row ROW on window W between indices START and END.
25190 HL overrides the face for drawing glyph strings, e.g. it is
25191 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
25192 x-positions of the drawing area.
25194 This is an ugly monster macro construct because we must use alloca
25195 to allocate glyph strings (because draw_glyphs can be called
25196 asynchronously). */
25198 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25199 do \
25201 HEAD = TAIL = NULL; \
25202 while (START < END) \
25204 struct glyph *first_glyph = (row)->glyphs[area] + START; \
25205 switch (first_glyph->type) \
25207 case CHAR_GLYPH: \
25208 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
25209 HL, X, LAST_X); \
25210 break; \
25212 case COMPOSITE_GLYPH: \
25213 if (first_glyph->u.cmp.automatic) \
25214 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
25215 HL, X, LAST_X); \
25216 else \
25217 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
25218 HL, X, LAST_X); \
25219 break; \
25221 case STRETCH_GLYPH: \
25222 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
25223 HL, X, LAST_X); \
25224 break; \
25226 case IMAGE_GLYPH: \
25227 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
25228 HL, X, LAST_X); \
25229 break;
25231 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25232 case XWIDGET_GLYPH: \
25233 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
25234 HL, X, LAST_X); \
25235 break;
25237 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
25238 case GLYPHLESS_GLYPH: \
25239 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
25240 HL, X, LAST_X); \
25241 break; \
25243 default: \
25244 emacs_abort (); \
25247 if (s) \
25249 set_glyph_string_background_width (s, START, LAST_X); \
25250 (X) += s->width; \
25253 } while (0)
25256 #ifdef HAVE_XWIDGETS
25257 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25258 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25259 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
25260 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25261 #else
25262 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
25263 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
25264 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
25265 #endif
25268 /* Draw glyphs between START and END in AREA of ROW on window W,
25269 starting at x-position X. X is relative to AREA in W. HL is a
25270 face-override with the following meaning:
25272 DRAW_NORMAL_TEXT draw normally
25273 DRAW_CURSOR draw in cursor face
25274 DRAW_MOUSE_FACE draw in mouse face.
25275 DRAW_INVERSE_VIDEO draw in mode line face
25276 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
25277 DRAW_IMAGE_RAISED draw an image with a raised relief around it
25279 If OVERLAPS is non-zero, draw only the foreground of characters and
25280 clip to the physical height of ROW. Non-zero value also defines
25281 the overlapping part to be drawn:
25283 OVERLAPS_PRED overlap with preceding rows
25284 OVERLAPS_SUCC overlap with succeeding rows
25285 OVERLAPS_BOTH overlap with both preceding/succeeding rows
25286 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
25288 Value is the x-position reached, relative to AREA of W. */
25290 static int
25291 draw_glyphs (struct window *w, int x, struct glyph_row *row,
25292 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
25293 enum draw_glyphs_face hl, int overlaps)
25295 struct glyph_string *head, *tail;
25296 struct glyph_string *s;
25297 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
25298 int i, j, x_reached, last_x, area_left = 0;
25299 struct frame *f = XFRAME (WINDOW_FRAME (w));
25300 DECLARE_HDC (hdc);
25302 ALLOCATE_HDC (hdc, f);
25304 /* Let's rather be paranoid than getting a SEGV. */
25305 end = min (end, row->used[area]);
25306 start = clip_to_bounds (0, start, end);
25308 /* Translate X to frame coordinates. Set last_x to the right
25309 end of the drawing area. */
25310 if (row->full_width_p)
25312 /* X is relative to the left edge of W, without scroll bars
25313 or fringes. */
25314 area_left = WINDOW_LEFT_EDGE_X (w);
25315 last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
25316 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
25318 else
25320 area_left = window_box_left (w, area);
25321 last_x = area_left + window_box_width (w, area);
25323 x += area_left;
25325 /* Build a doubly-linked list of glyph_string structures between
25326 head and tail from what we have to draw. Note that the macro
25327 BUILD_GLYPH_STRINGS will modify its start parameter. That's
25328 the reason we use a separate variable `i'. */
25329 i = start;
25330 USE_SAFE_ALLOCA;
25331 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
25332 if (tail)
25333 x_reached = tail->x + tail->background_width;
25334 else
25335 x_reached = x;
25337 /* If there are any glyphs with lbearing < 0 or rbearing > width in
25338 the row, redraw some glyphs in front or following the glyph
25339 strings built above. */
25340 if (head && !overlaps && row->contains_overlapping_glyphs_p)
25342 struct glyph_string *h, *t;
25343 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25344 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
25345 int check_mouse_face = 0;
25346 int dummy_x = 0;
25348 /* If mouse highlighting is on, we may need to draw adjacent
25349 glyphs using mouse-face highlighting. */
25350 if (area == TEXT_AREA && row->mouse_face_p
25351 && hlinfo->mouse_face_beg_row >= 0
25352 && hlinfo->mouse_face_end_row >= 0)
25354 ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
25356 if (row_vpos >= hlinfo->mouse_face_beg_row
25357 && row_vpos <= hlinfo->mouse_face_end_row)
25359 check_mouse_face = 1;
25360 mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
25361 ? hlinfo->mouse_face_beg_col : 0;
25362 mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
25363 ? hlinfo->mouse_face_end_col
25364 : row->used[TEXT_AREA];
25368 /* Compute overhangs for all glyph strings. */
25369 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
25370 for (s = head; s; s = s->next)
25371 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
25373 /* Prepend glyph strings for glyphs in front of the first glyph
25374 string that are overwritten because of the first glyph
25375 string's left overhang. The background of all strings
25376 prepended must be drawn because the first glyph string
25377 draws over it. */
25378 i = left_overwritten (head);
25379 if (i >= 0)
25381 enum draw_glyphs_face overlap_hl;
25383 /* If this row contains mouse highlighting, attempt to draw
25384 the overlapped glyphs with the correct highlight. This
25385 code fails if the overlap encompasses more than one glyph
25386 and mouse-highlight spans only some of these glyphs.
25387 However, making it work perfectly involves a lot more
25388 code, and I don't know if the pathological case occurs in
25389 practice, so we'll stick to this for now. --- cyd */
25390 if (check_mouse_face
25391 && mouse_beg_col < start && mouse_end_col > i)
25392 overlap_hl = DRAW_MOUSE_FACE;
25393 else
25394 overlap_hl = DRAW_NORMAL_TEXT;
25396 if (hl != overlap_hl)
25397 clip_head = head;
25398 j = i;
25399 BUILD_GLYPH_STRINGS (j, start, h, t,
25400 overlap_hl, dummy_x, last_x);
25401 start = i;
25402 compute_overhangs_and_x (t, head->x, 1);
25403 prepend_glyph_string_lists (&head, &tail, h, t);
25404 if (clip_head == NULL)
25405 clip_head = head;
25408 /* Prepend glyph strings for glyphs in front of the first glyph
25409 string that overwrite that glyph string because of their
25410 right overhang. For these strings, only the foreground must
25411 be drawn, because it draws over the glyph string at `head'.
25412 The background must not be drawn because this would overwrite
25413 right overhangs of preceding glyphs for which no glyph
25414 strings exist. */
25415 i = left_overwriting (head);
25416 if (i >= 0)
25418 enum draw_glyphs_face overlap_hl;
25420 if (check_mouse_face
25421 && mouse_beg_col < start && mouse_end_col > i)
25422 overlap_hl = DRAW_MOUSE_FACE;
25423 else
25424 overlap_hl = DRAW_NORMAL_TEXT;
25426 if (hl == overlap_hl || clip_head == NULL)
25427 clip_head = head;
25428 BUILD_GLYPH_STRINGS (i, start, h, t,
25429 overlap_hl, dummy_x, last_x);
25430 for (s = h; s; s = s->next)
25431 s->background_filled_p = 1;
25432 compute_overhangs_and_x (t, head->x, 1);
25433 prepend_glyph_string_lists (&head, &tail, h, t);
25436 /* Append glyphs strings for glyphs following the last glyph
25437 string tail that are overwritten by tail. The background of
25438 these strings has to be drawn because tail's foreground draws
25439 over it. */
25440 i = right_overwritten (tail);
25441 if (i >= 0)
25443 enum draw_glyphs_face overlap_hl;
25445 if (check_mouse_face
25446 && mouse_beg_col < i && mouse_end_col > end)
25447 overlap_hl = DRAW_MOUSE_FACE;
25448 else
25449 overlap_hl = DRAW_NORMAL_TEXT;
25451 if (hl != overlap_hl)
25452 clip_tail = tail;
25453 BUILD_GLYPH_STRINGS (end, i, h, t,
25454 overlap_hl, x, last_x);
25455 /* Because BUILD_GLYPH_STRINGS updates the first argument,
25456 we don't have `end = i;' here. */
25457 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25458 append_glyph_string_lists (&head, &tail, h, t);
25459 if (clip_tail == NULL)
25460 clip_tail = tail;
25463 /* Append glyph strings for glyphs following the last glyph
25464 string tail that overwrite tail. The foreground of such
25465 glyphs has to be drawn because it writes into the background
25466 of tail. The background must not be drawn because it could
25467 paint over the foreground of following glyphs. */
25468 i = right_overwriting (tail);
25469 if (i >= 0)
25471 enum draw_glyphs_face overlap_hl;
25472 if (check_mouse_face
25473 && mouse_beg_col < i && mouse_end_col > end)
25474 overlap_hl = DRAW_MOUSE_FACE;
25475 else
25476 overlap_hl = DRAW_NORMAL_TEXT;
25478 if (hl == overlap_hl || clip_tail == NULL)
25479 clip_tail = tail;
25480 i++; /* We must include the Ith glyph. */
25481 BUILD_GLYPH_STRINGS (end, i, h, t,
25482 overlap_hl, x, last_x);
25483 for (s = h; s; s = s->next)
25484 s->background_filled_p = 1;
25485 compute_overhangs_and_x (h, tail->x + tail->width, 0);
25486 append_glyph_string_lists (&head, &tail, h, t);
25488 if (clip_head || clip_tail)
25489 for (s = head; s; s = s->next)
25491 s->clip_head = clip_head;
25492 s->clip_tail = clip_tail;
25496 /* Draw all strings. */
25497 for (s = head; s; s = s->next)
25498 FRAME_RIF (f)->draw_glyph_string (s);
25500 #ifndef HAVE_NS
25501 /* When focus a sole frame and move horizontally, this sets on_p to 0
25502 causing a failure to erase prev cursor position. */
25503 if (area == TEXT_AREA
25504 && !row->full_width_p
25505 /* When drawing overlapping rows, only the glyph strings'
25506 foreground is drawn, which doesn't erase a cursor
25507 completely. */
25508 && !overlaps)
25510 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
25511 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
25512 : (tail ? tail->x + tail->background_width : x));
25513 x0 -= area_left;
25514 x1 -= area_left;
25516 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
25517 row->y, MATRIX_ROW_BOTTOM_Y (row));
25519 #endif
25521 /* Value is the x-position up to which drawn, relative to AREA of W.
25522 This doesn't include parts drawn because of overhangs. */
25523 if (row->full_width_p)
25524 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
25525 else
25526 x_reached -= area_left;
25528 RELEASE_HDC (hdc, f);
25530 SAFE_FREE ();
25531 return x_reached;
25534 /* Expand row matrix if too narrow. Don't expand if area
25535 is not present. */
25537 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
25539 if (!it->f->fonts_changed \
25540 && (it->glyph_row->glyphs[area] \
25541 < it->glyph_row->glyphs[area + 1])) \
25543 it->w->ncols_scale_factor++; \
25544 it->f->fonts_changed = 1; \
25548 /* Store one glyph for IT->char_to_display in IT->glyph_row.
25549 Called from x_produce_glyphs when IT->glyph_row is non-null. */
25551 static void
25552 append_glyph (struct it *it)
25554 struct glyph *glyph;
25555 enum glyph_row_area area = it->area;
25557 eassert (it->glyph_row);
25558 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
25560 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25561 if (glyph < it->glyph_row->glyphs[area + 1])
25563 /* If the glyph row is reversed, we need to prepend the glyph
25564 rather than append it. */
25565 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25567 struct glyph *g;
25569 /* Make room for the additional glyph. */
25570 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
25571 g[1] = *g;
25572 glyph = it->glyph_row->glyphs[area];
25574 glyph->charpos = CHARPOS (it->position);
25575 glyph->object = it->object;
25576 if (it->pixel_width > 0)
25578 glyph->pixel_width = it->pixel_width;
25579 glyph->padding_p = 0;
25581 else
25583 /* Assure at least 1-pixel width. Otherwise, cursor can't
25584 be displayed correctly. */
25585 glyph->pixel_width = 1;
25586 glyph->padding_p = 1;
25588 glyph->ascent = it->ascent;
25589 glyph->descent = it->descent;
25590 glyph->voffset = it->voffset;
25591 glyph->type = CHAR_GLYPH;
25592 glyph->avoid_cursor_p = it->avoid_cursor_p;
25593 glyph->multibyte_p = it->multibyte_p;
25594 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25596 /* In R2L rows, the left and the right box edges need to be
25597 drawn in reverse direction. */
25598 glyph->right_box_line_p = it->start_of_box_run_p;
25599 glyph->left_box_line_p = it->end_of_box_run_p;
25601 else
25603 glyph->left_box_line_p = it->start_of_box_run_p;
25604 glyph->right_box_line_p = it->end_of_box_run_p;
25606 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25607 || it->phys_descent > it->descent);
25608 glyph->glyph_not_available_p = it->glyph_not_available_p;
25609 glyph->face_id = it->face_id;
25610 glyph->u.ch = it->char_to_display;
25611 glyph->slice.img = null_glyph_slice;
25612 glyph->font_type = FONT_TYPE_UNKNOWN;
25613 if (it->bidi_p)
25615 glyph->resolved_level = it->bidi_it.resolved_level;
25616 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25617 glyph->bidi_type = it->bidi_it.type;
25619 else
25621 glyph->resolved_level = 0;
25622 glyph->bidi_type = UNKNOWN_BT;
25624 ++it->glyph_row->used[area];
25626 else
25627 IT_EXPAND_MATRIX_WIDTH (it, area);
25630 /* Store one glyph for the composition IT->cmp_it.id in
25631 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
25632 non-null. */
25634 static void
25635 append_composite_glyph (struct it *it)
25637 struct glyph *glyph;
25638 enum glyph_row_area area = it->area;
25640 eassert (it->glyph_row);
25642 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25643 if (glyph < it->glyph_row->glyphs[area + 1])
25645 /* If the glyph row is reversed, we need to prepend the glyph
25646 rather than append it. */
25647 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
25649 struct glyph *g;
25651 /* Make room for the new glyph. */
25652 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
25653 g[1] = *g;
25654 glyph = it->glyph_row->glyphs[it->area];
25656 glyph->charpos = it->cmp_it.charpos;
25657 glyph->object = it->object;
25658 glyph->pixel_width = it->pixel_width;
25659 glyph->ascent = it->ascent;
25660 glyph->descent = it->descent;
25661 glyph->voffset = it->voffset;
25662 glyph->type = COMPOSITE_GLYPH;
25663 if (it->cmp_it.ch < 0)
25665 glyph->u.cmp.automatic = 0;
25666 glyph->u.cmp.id = it->cmp_it.id;
25667 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
25669 else
25671 glyph->u.cmp.automatic = 1;
25672 glyph->u.cmp.id = it->cmp_it.id;
25673 glyph->slice.cmp.from = it->cmp_it.from;
25674 glyph->slice.cmp.to = it->cmp_it.to - 1;
25676 glyph->avoid_cursor_p = it->avoid_cursor_p;
25677 glyph->multibyte_p = it->multibyte_p;
25678 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25680 /* In R2L rows, the left and the right box edges need to be
25681 drawn in reverse direction. */
25682 glyph->right_box_line_p = it->start_of_box_run_p;
25683 glyph->left_box_line_p = it->end_of_box_run_p;
25685 else
25687 glyph->left_box_line_p = it->start_of_box_run_p;
25688 glyph->right_box_line_p = it->end_of_box_run_p;
25690 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
25691 || it->phys_descent > it->descent);
25692 glyph->padding_p = 0;
25693 glyph->glyph_not_available_p = 0;
25694 glyph->face_id = it->face_id;
25695 glyph->font_type = FONT_TYPE_UNKNOWN;
25696 if (it->bidi_p)
25698 glyph->resolved_level = it->bidi_it.resolved_level;
25699 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25700 glyph->bidi_type = it->bidi_it.type;
25702 ++it->glyph_row->used[area];
25704 else
25705 IT_EXPAND_MATRIX_WIDTH (it, area);
25709 /* Change IT->ascent and IT->height according to the setting of
25710 IT->voffset. */
25712 static void
25713 take_vertical_position_into_account (struct it *it)
25715 if (it->voffset)
25717 if (it->voffset < 0)
25718 /* Increase the ascent so that we can display the text higher
25719 in the line. */
25720 it->ascent -= it->voffset;
25721 else
25722 /* Increase the descent so that we can display the text lower
25723 in the line. */
25724 it->descent += it->voffset;
25729 /* Produce glyphs/get display metrics for the image IT is loaded with.
25730 See the description of struct display_iterator in dispextern.h for
25731 an overview of struct display_iterator. */
25733 static void
25734 produce_image_glyph (struct it *it)
25736 struct image *img;
25737 struct face *face;
25738 int glyph_ascent, crop;
25739 struct glyph_slice slice;
25741 eassert (it->what == IT_IMAGE);
25743 face = FACE_FROM_ID (it->f, it->face_id);
25744 eassert (face);
25745 /* Make sure X resources of the face is loaded. */
25746 prepare_face_for_display (it->f, face);
25748 if (it->image_id < 0)
25750 /* Fringe bitmap. */
25751 it->ascent = it->phys_ascent = 0;
25752 it->descent = it->phys_descent = 0;
25753 it->pixel_width = 0;
25754 it->nglyphs = 0;
25755 return;
25758 img = IMAGE_FROM_ID (it->f, it->image_id);
25759 eassert (img);
25760 /* Make sure X resources of the image is loaded. */
25761 prepare_image_for_display (it->f, img);
25763 slice.x = slice.y = 0;
25764 slice.width = img->width;
25765 slice.height = img->height;
25767 if (INTEGERP (it->slice.x))
25768 slice.x = XINT (it->slice.x);
25769 else if (FLOATP (it->slice.x))
25770 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
25772 if (INTEGERP (it->slice.y))
25773 slice.y = XINT (it->slice.y);
25774 else if (FLOATP (it->slice.y))
25775 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
25777 if (INTEGERP (it->slice.width))
25778 slice.width = XINT (it->slice.width);
25779 else if (FLOATP (it->slice.width))
25780 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
25782 if (INTEGERP (it->slice.height))
25783 slice.height = XINT (it->slice.height);
25784 else if (FLOATP (it->slice.height))
25785 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
25787 if (slice.x >= img->width)
25788 slice.x = img->width;
25789 if (slice.y >= img->height)
25790 slice.y = img->height;
25791 if (slice.x + slice.width >= img->width)
25792 slice.width = img->width - slice.x;
25793 if (slice.y + slice.height > img->height)
25794 slice.height = img->height - slice.y;
25796 if (slice.width == 0 || slice.height == 0)
25797 return;
25799 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
25801 it->descent = slice.height - glyph_ascent;
25802 if (slice.y == 0)
25803 it->descent += img->vmargin;
25804 if (slice.y + slice.height == img->height)
25805 it->descent += img->vmargin;
25806 it->phys_descent = it->descent;
25808 it->pixel_width = slice.width;
25809 if (slice.x == 0)
25810 it->pixel_width += img->hmargin;
25811 if (slice.x + slice.width == img->width)
25812 it->pixel_width += img->hmargin;
25814 /* It's quite possible for images to have an ascent greater than
25815 their height, so don't get confused in that case. */
25816 if (it->descent < 0)
25817 it->descent = 0;
25819 it->nglyphs = 1;
25821 if (face->box != FACE_NO_BOX)
25823 if (face->box_line_width > 0)
25825 if (slice.y == 0)
25826 it->ascent += face->box_line_width;
25827 if (slice.y + slice.height == img->height)
25828 it->descent += face->box_line_width;
25831 if (it->start_of_box_run_p && slice.x == 0)
25832 it->pixel_width += eabs (face->box_line_width);
25833 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
25834 it->pixel_width += eabs (face->box_line_width);
25837 take_vertical_position_into_account (it);
25839 /* Automatically crop wide image glyphs at right edge so we can
25840 draw the cursor on same display row. */
25841 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25842 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25844 it->pixel_width -= crop;
25845 slice.width -= crop;
25848 if (it->glyph_row)
25850 struct glyph *glyph;
25851 enum glyph_row_area area = it->area;
25853 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25854 if (glyph < it->glyph_row->glyphs[area + 1])
25856 glyph->charpos = CHARPOS (it->position);
25857 glyph->object = it->object;
25858 glyph->pixel_width = it->pixel_width;
25859 glyph->ascent = glyph_ascent;
25860 glyph->descent = it->descent;
25861 glyph->voffset = it->voffset;
25862 glyph->type = IMAGE_GLYPH;
25863 glyph->avoid_cursor_p = it->avoid_cursor_p;
25864 glyph->multibyte_p = it->multibyte_p;
25865 if (it->glyph_row->reversed_p && area == TEXT_AREA)
25867 /* In R2L rows, the left and the right box edges need to be
25868 drawn in reverse direction. */
25869 glyph->right_box_line_p = it->start_of_box_run_p;
25870 glyph->left_box_line_p = it->end_of_box_run_p;
25872 else
25874 glyph->left_box_line_p = it->start_of_box_run_p;
25875 glyph->right_box_line_p = it->end_of_box_run_p;
25877 glyph->overlaps_vertically_p = 0;
25878 glyph->padding_p = 0;
25879 glyph->glyph_not_available_p = 0;
25880 glyph->face_id = it->face_id;
25881 glyph->u.img_id = img->id;
25882 glyph->slice.img = slice;
25883 glyph->font_type = FONT_TYPE_UNKNOWN;
25884 if (it->bidi_p)
25886 glyph->resolved_level = it->bidi_it.resolved_level;
25887 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
25888 glyph->bidi_type = it->bidi_it.type;
25890 ++it->glyph_row->used[area];
25892 else
25893 IT_EXPAND_MATRIX_WIDTH (it, area);
25897 #ifdef HAVE_XWIDGETS
25898 static void
25899 produce_xwidget_glyph (struct it *it)
25901 struct xwidget* xw;
25902 struct face *face;
25903 int glyph_ascent, crop;
25904 printf("produce_xwidget_glyph:\n");
25905 eassert (it->what == IT_XWIDGET);
25907 face = FACE_FROM_ID (it->f, it->face_id);
25908 eassert (face);
25909 /* Make sure X resources of the face is loaded. */
25910 prepare_face_for_display (it->f, face);
25912 xw = it->xwidget;
25913 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
25914 it->descent = xw->height/2;
25915 it->phys_descent = it->descent;
25916 it->pixel_width = xw->width;
25917 /* It's quite possible for images to have an ascent greater than
25918 their height, so don't get confused in that case. */
25919 if (it->descent < 0)
25920 it->descent = 0;
25922 it->nglyphs = 1;
25924 if (face->box != FACE_NO_BOX)
25926 if (face->box_line_width > 0)
25928 it->ascent += face->box_line_width;
25929 it->descent += face->box_line_width;
25932 if (it->start_of_box_run_p)
25933 it->pixel_width += eabs (face->box_line_width);
25934 it->pixel_width += eabs (face->box_line_width);
25937 take_vertical_position_into_account (it);
25939 /* Automatically crop wide image glyphs at right edge so we can
25940 draw the cursor on same display row. */
25941 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
25942 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
25944 it->pixel_width -= crop;
25947 if (it->glyph_row)
25949 struct glyph *glyph;
25950 enum glyph_row_area area = it->area;
25952 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25953 if (glyph < it->glyph_row->glyphs[area + 1])
25955 glyph->charpos = CHARPOS (it->position);
25956 glyph->object = it->object;
25957 glyph->pixel_width = it->pixel_width;
25958 glyph->ascent = glyph_ascent;
25959 glyph->descent = it->descent;
25960 glyph->voffset = it->voffset;
25961 glyph->type = XWIDGET_GLYPH;
25963 glyph->multibyte_p = it->multibyte_p;
25964 glyph->left_box_line_p = it->start_of_box_run_p;
25965 glyph->right_box_line_p = it->end_of_box_run_p;
25966 glyph->overlaps_vertically_p = 0;
25967 glyph->padding_p = 0;
25968 glyph->glyph_not_available_p = 0;
25969 glyph->face_id = it->face_id;
25970 glyph->u.xwidget = it->xwidget;
25971 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
25972 glyph->font_type = FONT_TYPE_UNKNOWN;
25973 ++it->glyph_row->used[area];
25975 else
25976 IT_EXPAND_MATRIX_WIDTH (it, area);
25979 #endif
25981 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
25982 of the glyph, WIDTH and HEIGHT are the width and height of the
25983 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
25985 static void
25986 append_stretch_glyph (struct it *it, Lisp_Object object,
25987 int width, int height, int ascent)
25989 struct glyph *glyph;
25990 enum glyph_row_area area = it->area;
25992 eassert (ascent >= 0 && ascent <= height);
25994 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
25995 if (glyph < it->glyph_row->glyphs[area + 1])
25997 /* If the glyph row is reversed, we need to prepend the glyph
25998 rather than append it. */
25999 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26001 struct glyph *g;
26003 /* Make room for the additional glyph. */
26004 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26005 g[1] = *g;
26006 glyph = it->glyph_row->glyphs[area];
26008 /* Decrease the width of the first glyph of the row that
26009 begins before first_visible_x (e.g., due to hscroll).
26010 This is so the overall width of the row becomes smaller
26011 by the scroll amount, and the stretch glyph appended by
26012 extend_face_to_end_of_line will be wider, to shift the
26013 row glyphs to the right. (In L2R rows, the corresponding
26014 left-shift effect is accomplished by setting row->x to a
26015 negative value, which won't work with R2L rows.)
26017 This must leave us with a positive value of WIDTH, since
26018 otherwise the call to move_it_in_display_line_to at the
26019 beginning of display_line would have got past the entire
26020 first glyph, and then it->current_x would have been
26021 greater or equal to it->first_visible_x. */
26022 if (it->current_x < it->first_visible_x)
26023 width -= it->first_visible_x - it->current_x;
26024 eassert (width > 0);
26026 glyph->charpos = CHARPOS (it->position);
26027 glyph->object = object;
26028 glyph->pixel_width = width;
26029 glyph->ascent = ascent;
26030 glyph->descent = height - ascent;
26031 glyph->voffset = it->voffset;
26032 glyph->type = STRETCH_GLYPH;
26033 glyph->avoid_cursor_p = it->avoid_cursor_p;
26034 glyph->multibyte_p = it->multibyte_p;
26035 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26037 /* In R2L rows, the left and the right box edges need to be
26038 drawn in reverse direction. */
26039 glyph->right_box_line_p = it->start_of_box_run_p;
26040 glyph->left_box_line_p = it->end_of_box_run_p;
26042 else
26044 glyph->left_box_line_p = it->start_of_box_run_p;
26045 glyph->right_box_line_p = it->end_of_box_run_p;
26047 glyph->overlaps_vertically_p = 0;
26048 glyph->padding_p = 0;
26049 glyph->glyph_not_available_p = 0;
26050 glyph->face_id = it->face_id;
26051 glyph->u.stretch.ascent = ascent;
26052 glyph->u.stretch.height = height;
26053 glyph->slice.img = null_glyph_slice;
26054 glyph->font_type = FONT_TYPE_UNKNOWN;
26055 if (it->bidi_p)
26057 glyph->resolved_level = it->bidi_it.resolved_level;
26058 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26059 glyph->bidi_type = it->bidi_it.type;
26061 else
26063 glyph->resolved_level = 0;
26064 glyph->bidi_type = UNKNOWN_BT;
26066 ++it->glyph_row->used[area];
26068 else
26069 IT_EXPAND_MATRIX_WIDTH (it, area);
26072 #endif /* HAVE_WINDOW_SYSTEM */
26074 /* Produce a stretch glyph for iterator IT. IT->object is the value
26075 of the glyph property displayed. The value must be a list
26076 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
26077 being recognized:
26079 1. `:width WIDTH' specifies that the space should be WIDTH *
26080 canonical char width wide. WIDTH may be an integer or floating
26081 point number.
26083 2. `:relative-width FACTOR' specifies that the width of the stretch
26084 should be computed from the width of the first character having the
26085 `glyph' property, and should be FACTOR times that width.
26087 3. `:align-to HPOS' specifies that the space should be wide enough
26088 to reach HPOS, a value in canonical character units.
26090 Exactly one of the above pairs must be present.
26092 4. `:height HEIGHT' specifies that the height of the stretch produced
26093 should be HEIGHT, measured in canonical character units.
26095 5. `:relative-height FACTOR' specifies that the height of the
26096 stretch should be FACTOR times the height of the characters having
26097 the glyph property.
26099 Either none or exactly one of 4 or 5 must be present.
26101 6. `:ascent ASCENT' specifies that ASCENT percent of the height
26102 of the stretch should be used for the ascent of the stretch.
26103 ASCENT must be in the range 0 <= ASCENT <= 100. */
26105 void
26106 produce_stretch_glyph (struct it *it)
26108 /* (space :width WIDTH :height HEIGHT ...) */
26109 Lisp_Object prop, plist;
26110 int width = 0, height = 0, align_to = -1;
26111 int zero_width_ok_p = 0;
26112 double tem;
26113 struct font *font = NULL;
26115 #ifdef HAVE_WINDOW_SYSTEM
26116 int ascent = 0;
26117 int zero_height_ok_p = 0;
26119 if (FRAME_WINDOW_P (it->f))
26121 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26122 font = face->font ? face->font : FRAME_FONT (it->f);
26123 prepare_face_for_display (it->f, face);
26125 #endif
26127 /* List should start with `space'. */
26128 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
26129 plist = XCDR (it->object);
26131 /* Compute the width of the stretch. */
26132 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
26133 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
26135 /* Absolute width `:width WIDTH' specified and valid. */
26136 zero_width_ok_p = 1;
26137 width = (int)tem;
26139 #ifdef HAVE_WINDOW_SYSTEM
26140 else if (FRAME_WINDOW_P (it->f)
26141 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
26143 /* Relative width `:relative-width FACTOR' specified and valid.
26144 Compute the width of the characters having the `glyph'
26145 property. */
26146 struct it it2;
26147 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
26149 it2 = *it;
26150 if (it->multibyte_p)
26151 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
26152 else
26154 it2.c = it2.char_to_display = *p, it2.len = 1;
26155 if (! ASCII_CHAR_P (it2.c))
26156 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
26159 it2.glyph_row = NULL;
26160 it2.what = IT_CHARACTER;
26161 x_produce_glyphs (&it2);
26162 width = NUMVAL (prop) * it2.pixel_width;
26164 #endif /* HAVE_WINDOW_SYSTEM */
26165 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
26166 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
26168 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
26169 align_to = (align_to < 0
26171 : align_to - window_box_left_offset (it->w, TEXT_AREA));
26172 else if (align_to < 0)
26173 align_to = window_box_left_offset (it->w, TEXT_AREA);
26174 width = max (0, (int)tem + align_to - it->current_x);
26175 zero_width_ok_p = 1;
26177 else
26178 /* Nothing specified -> width defaults to canonical char width. */
26179 width = FRAME_COLUMN_WIDTH (it->f);
26181 if (width <= 0 && (width < 0 || !zero_width_ok_p))
26182 width = 1;
26184 #ifdef HAVE_WINDOW_SYSTEM
26185 /* Compute height. */
26186 if (FRAME_WINDOW_P (it->f))
26188 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
26189 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
26191 height = (int)tem;
26192 zero_height_ok_p = 1;
26194 else if (prop = Fplist_get (plist, QCrelative_height),
26195 NUMVAL (prop) > 0)
26196 height = FONT_HEIGHT (font) * NUMVAL (prop);
26197 else
26198 height = FONT_HEIGHT (font);
26200 if (height <= 0 && (height < 0 || !zero_height_ok_p))
26201 height = 1;
26203 /* Compute percentage of height used for ascent. If
26204 `:ascent ASCENT' is present and valid, use that. Otherwise,
26205 derive the ascent from the font in use. */
26206 if (prop = Fplist_get (plist, QCascent),
26207 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
26208 ascent = height * NUMVAL (prop) / 100.0;
26209 else if (!NILP (prop)
26210 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
26211 ascent = min (max (0, (int)tem), height);
26212 else
26213 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
26215 else
26216 #endif /* HAVE_WINDOW_SYSTEM */
26217 height = 1;
26219 if (width > 0 && it->line_wrap != TRUNCATE
26220 && it->current_x + width > it->last_visible_x)
26222 width = it->last_visible_x - it->current_x;
26223 #ifdef HAVE_WINDOW_SYSTEM
26224 /* Subtract one more pixel from the stretch width, but only on
26225 GUI frames, since on a TTY each glyph is one "pixel" wide. */
26226 width -= FRAME_WINDOW_P (it->f);
26227 #endif
26230 if (width > 0 && height > 0 && it->glyph_row)
26232 Lisp_Object o_object = it->object;
26233 Lisp_Object object = it->stack[it->sp - 1].string;
26234 int n = width;
26236 if (!STRINGP (object))
26237 object = it->w->contents;
26238 #ifdef HAVE_WINDOW_SYSTEM
26239 if (FRAME_WINDOW_P (it->f))
26240 append_stretch_glyph (it, object, width, height, ascent);
26241 else
26242 #endif
26244 it->object = object;
26245 it->char_to_display = ' ';
26246 it->pixel_width = it->len = 1;
26247 while (n--)
26248 tty_append_glyph (it);
26249 it->object = o_object;
26253 it->pixel_width = width;
26254 #ifdef HAVE_WINDOW_SYSTEM
26255 if (FRAME_WINDOW_P (it->f))
26257 it->ascent = it->phys_ascent = ascent;
26258 it->descent = it->phys_descent = height - it->ascent;
26259 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
26260 take_vertical_position_into_account (it);
26262 else
26263 #endif
26264 it->nglyphs = width;
26267 /* Get information about special display element WHAT in an
26268 environment described by IT. WHAT is one of IT_TRUNCATION or
26269 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
26270 non-null glyph_row member. This function ensures that fields like
26271 face_id, c, len of IT are left untouched. */
26273 static void
26274 produce_special_glyphs (struct it *it, enum display_element_type what)
26276 struct it temp_it;
26277 Lisp_Object gc;
26278 GLYPH glyph;
26280 temp_it = *it;
26281 temp_it.object = make_number (0);
26282 memset (&temp_it.current, 0, sizeof temp_it.current);
26284 if (what == IT_CONTINUATION)
26286 /* Continuation glyph. For R2L lines, we mirror it by hand. */
26287 if (it->bidi_it.paragraph_dir == R2L)
26288 SET_GLYPH_FROM_CHAR (glyph, '/');
26289 else
26290 SET_GLYPH_FROM_CHAR (glyph, '\\');
26291 if (it->dp
26292 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26294 /* FIXME: Should we mirror GC for R2L lines? */
26295 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26296 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26299 else if (what == IT_TRUNCATION)
26301 /* Truncation glyph. */
26302 SET_GLYPH_FROM_CHAR (glyph, '$');
26303 if (it->dp
26304 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
26306 /* FIXME: Should we mirror GC for R2L lines? */
26307 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
26308 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
26311 else
26312 emacs_abort ();
26314 #ifdef HAVE_WINDOW_SYSTEM
26315 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
26316 is turned off, we precede the truncation/continuation glyphs by a
26317 stretch glyph whose width is computed such that these special
26318 glyphs are aligned at the window margin, even when very different
26319 fonts are used in different glyph rows. */
26320 if (FRAME_WINDOW_P (temp_it.f)
26321 /* init_iterator calls this with it->glyph_row == NULL, and it
26322 wants only the pixel width of the truncation/continuation
26323 glyphs. */
26324 && temp_it.glyph_row
26325 /* insert_left_trunc_glyphs calls us at the beginning of the
26326 row, and it has its own calculation of the stretch glyph
26327 width. */
26328 && temp_it.glyph_row->used[TEXT_AREA] > 0
26329 && (temp_it.glyph_row->reversed_p
26330 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
26331 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
26333 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
26335 if (stretch_width > 0)
26337 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
26338 struct font *font =
26339 face->font ? face->font : FRAME_FONT (temp_it.f);
26340 int stretch_ascent =
26341 (((temp_it.ascent + temp_it.descent)
26342 * FONT_BASE (font)) / FONT_HEIGHT (font));
26344 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
26345 temp_it.ascent + temp_it.descent,
26346 stretch_ascent);
26349 #endif
26351 temp_it.dp = NULL;
26352 temp_it.what = IT_CHARACTER;
26353 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
26354 temp_it.face_id = GLYPH_FACE (glyph);
26355 temp_it.len = CHAR_BYTES (temp_it.c);
26357 PRODUCE_GLYPHS (&temp_it);
26358 it->pixel_width = temp_it.pixel_width;
26359 it->nglyphs = temp_it.nglyphs;
26362 #ifdef HAVE_WINDOW_SYSTEM
26364 /* Calculate line-height and line-spacing properties.
26365 An integer value specifies explicit pixel value.
26366 A float value specifies relative value to current face height.
26367 A cons (float . face-name) specifies relative value to
26368 height of specified face font.
26370 Returns height in pixels, or nil. */
26373 static Lisp_Object
26374 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
26375 int boff, int override)
26377 Lisp_Object face_name = Qnil;
26378 int ascent, descent, height;
26380 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
26381 return val;
26383 if (CONSP (val))
26385 face_name = XCAR (val);
26386 val = XCDR (val);
26387 if (!NUMBERP (val))
26388 val = make_number (1);
26389 if (NILP (face_name))
26391 height = it->ascent + it->descent;
26392 goto scale;
26396 if (NILP (face_name))
26398 font = FRAME_FONT (it->f);
26399 boff = FRAME_BASELINE_OFFSET (it->f);
26401 else if (EQ (face_name, Qt))
26403 override = 0;
26405 else
26407 int face_id;
26408 struct face *face;
26410 face_id = lookup_named_face (it->f, face_name, 0);
26411 if (face_id < 0)
26412 return make_number (-1);
26414 face = FACE_FROM_ID (it->f, face_id);
26415 font = face->font;
26416 if (font == NULL)
26417 return make_number (-1);
26418 boff = font->baseline_offset;
26419 if (font->vertical_centering)
26420 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26423 ascent = FONT_BASE (font) + boff;
26424 descent = FONT_DESCENT (font) - boff;
26426 if (override)
26428 it->override_ascent = ascent;
26429 it->override_descent = descent;
26430 it->override_boff = boff;
26433 height = ascent + descent;
26435 scale:
26436 if (FLOATP (val))
26437 height = (int)(XFLOAT_DATA (val) * height);
26438 else if (INTEGERP (val))
26439 height *= XINT (val);
26441 return make_number (height);
26445 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
26446 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
26447 and only if this is for a character for which no font was found.
26449 If the display method (it->glyphless_method) is
26450 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
26451 length of the acronym or the hexadecimal string, UPPER_XOFF and
26452 UPPER_YOFF are pixel offsets for the upper part of the string,
26453 LOWER_XOFF and LOWER_YOFF are for the lower part.
26455 For the other display methods, LEN through LOWER_YOFF are zero. */
26457 static void
26458 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
26459 short upper_xoff, short upper_yoff,
26460 short lower_xoff, short lower_yoff)
26462 struct glyph *glyph;
26463 enum glyph_row_area area = it->area;
26465 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
26466 if (glyph < it->glyph_row->glyphs[area + 1])
26468 /* If the glyph row is reversed, we need to prepend the glyph
26469 rather than append it. */
26470 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26472 struct glyph *g;
26474 /* Make room for the additional glyph. */
26475 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
26476 g[1] = *g;
26477 glyph = it->glyph_row->glyphs[area];
26479 glyph->charpos = CHARPOS (it->position);
26480 glyph->object = it->object;
26481 glyph->pixel_width = it->pixel_width;
26482 glyph->ascent = it->ascent;
26483 glyph->descent = it->descent;
26484 glyph->voffset = it->voffset;
26485 glyph->type = GLYPHLESS_GLYPH;
26486 glyph->u.glyphless.method = it->glyphless_method;
26487 glyph->u.glyphless.for_no_font = for_no_font;
26488 glyph->u.glyphless.len = len;
26489 glyph->u.glyphless.ch = it->c;
26490 glyph->slice.glyphless.upper_xoff = upper_xoff;
26491 glyph->slice.glyphless.upper_yoff = upper_yoff;
26492 glyph->slice.glyphless.lower_xoff = lower_xoff;
26493 glyph->slice.glyphless.lower_yoff = lower_yoff;
26494 glyph->avoid_cursor_p = it->avoid_cursor_p;
26495 glyph->multibyte_p = it->multibyte_p;
26496 if (it->glyph_row->reversed_p && area == TEXT_AREA)
26498 /* In R2L rows, the left and the right box edges need to be
26499 drawn in reverse direction. */
26500 glyph->right_box_line_p = it->start_of_box_run_p;
26501 glyph->left_box_line_p = it->end_of_box_run_p;
26503 else
26505 glyph->left_box_line_p = it->start_of_box_run_p;
26506 glyph->right_box_line_p = it->end_of_box_run_p;
26508 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
26509 || it->phys_descent > it->descent);
26510 glyph->padding_p = 0;
26511 glyph->glyph_not_available_p = 0;
26512 glyph->face_id = face_id;
26513 glyph->font_type = FONT_TYPE_UNKNOWN;
26514 if (it->bidi_p)
26516 glyph->resolved_level = it->bidi_it.resolved_level;
26517 eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
26518 glyph->bidi_type = it->bidi_it.type;
26520 ++it->glyph_row->used[area];
26522 else
26523 IT_EXPAND_MATRIX_WIDTH (it, area);
26527 /* Produce a glyph for a glyphless character for iterator IT.
26528 IT->glyphless_method specifies which method to use for displaying
26529 the character. See the description of enum
26530 glyphless_display_method in dispextern.h for the detail.
26532 FOR_NO_FONT is nonzero if and only if this is for a character for
26533 which no font was found. ACRONYM, if non-nil, is an acronym string
26534 for the character. */
26536 static void
26537 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
26539 int face_id;
26540 struct face *face;
26541 struct font *font;
26542 int base_width, base_height, width, height;
26543 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
26544 int len;
26546 /* Get the metrics of the base font. We always refer to the current
26547 ASCII face. */
26548 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
26549 font = face->font ? face->font : FRAME_FONT (it->f);
26550 it->ascent = FONT_BASE (font) + font->baseline_offset;
26551 it->descent = FONT_DESCENT (font) - font->baseline_offset;
26552 base_height = it->ascent + it->descent;
26553 base_width = font->average_width;
26555 face_id = merge_glyphless_glyph_face (it);
26557 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
26559 it->pixel_width = THIN_SPACE_WIDTH;
26560 len = 0;
26561 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26563 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
26565 width = CHAR_WIDTH (it->c);
26566 if (width == 0)
26567 width = 1;
26568 else if (width > 4)
26569 width = 4;
26570 it->pixel_width = base_width * width;
26571 len = 0;
26572 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
26574 else
26576 char buf[7];
26577 const char *str;
26578 unsigned int code[6];
26579 int upper_len;
26580 int ascent, descent;
26581 struct font_metrics metrics_upper, metrics_lower;
26583 face = FACE_FROM_ID (it->f, face_id);
26584 font = face->font ? face->font : FRAME_FONT (it->f);
26585 prepare_face_for_display (it->f, face);
26587 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
26589 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
26590 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
26591 if (CONSP (acronym))
26592 acronym = XCAR (acronym);
26593 str = STRINGP (acronym) ? SSDATA (acronym) : "";
26595 else
26597 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
26598 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
26599 str = buf;
26601 for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
26602 code[len] = font->driver->encode_char (font, str[len]);
26603 upper_len = (len + 1) / 2;
26604 font->driver->text_extents (font, code, upper_len,
26605 &metrics_upper);
26606 font->driver->text_extents (font, code + upper_len, len - upper_len,
26607 &metrics_lower);
26611 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
26612 width = max (metrics_upper.width, metrics_lower.width) + 4;
26613 upper_xoff = upper_yoff = 2; /* the typical case */
26614 if (base_width >= width)
26616 /* Align the upper to the left, the lower to the right. */
26617 it->pixel_width = base_width;
26618 lower_xoff = base_width - 2 - metrics_lower.width;
26620 else
26622 /* Center the shorter one. */
26623 it->pixel_width = width;
26624 if (metrics_upper.width >= metrics_lower.width)
26625 lower_xoff = (width - metrics_lower.width) / 2;
26626 else
26628 /* FIXME: This code doesn't look right. It formerly was
26629 missing the "lower_xoff = 0;", which couldn't have
26630 been right since it left lower_xoff uninitialized. */
26631 lower_xoff = 0;
26632 upper_xoff = (width - metrics_upper.width) / 2;
26636 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
26637 top, bottom, and between upper and lower strings. */
26638 height = (metrics_upper.ascent + metrics_upper.descent
26639 + metrics_lower.ascent + metrics_lower.descent) + 5;
26640 /* Center vertically.
26641 H:base_height, D:base_descent
26642 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
26644 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
26645 descent = D - H/2 + h/2;
26646 lower_yoff = descent - 2 - ld;
26647 upper_yoff = lower_yoff - la - 1 - ud; */
26648 ascent = - (it->descent - (base_height + height + 1) / 2);
26649 descent = it->descent - (base_height - height) / 2;
26650 lower_yoff = descent - 2 - metrics_lower.descent;
26651 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
26652 - metrics_upper.descent);
26653 /* Don't make the height shorter than the base height. */
26654 if (height > base_height)
26656 it->ascent = ascent;
26657 it->descent = descent;
26661 it->phys_ascent = it->ascent;
26662 it->phys_descent = it->descent;
26663 if (it->glyph_row)
26664 append_glyphless_glyph (it, face_id, for_no_font, len,
26665 upper_xoff, upper_yoff,
26666 lower_xoff, lower_yoff);
26667 it->nglyphs = 1;
26668 take_vertical_position_into_account (it);
26672 /* RIF:
26673 Produce glyphs/get display metrics for the display element IT is
26674 loaded with. See the description of struct it in dispextern.h
26675 for an overview of struct it. */
26677 void
26678 x_produce_glyphs (struct it *it)
26680 int extra_line_spacing = it->extra_line_spacing;
26682 it->glyph_not_available_p = 0;
26684 if (it->what == IT_CHARACTER)
26686 XChar2b char2b;
26687 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26688 struct font *font = face->font;
26689 struct font_metrics *pcm = NULL;
26690 int boff; /* Baseline offset. */
26692 if (font == NULL)
26694 /* When no suitable font is found, display this character by
26695 the method specified in the first extra slot of
26696 Vglyphless_char_display. */
26697 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
26699 eassert (it->what == IT_GLYPHLESS);
26700 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
26701 goto done;
26704 boff = font->baseline_offset;
26705 if (font->vertical_centering)
26706 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
26708 if (it->char_to_display != '\n' && it->char_to_display != '\t')
26710 int stretched_p;
26712 it->nglyphs = 1;
26714 if (it->override_ascent >= 0)
26716 it->ascent = it->override_ascent;
26717 it->descent = it->override_descent;
26718 boff = it->override_boff;
26720 else
26722 it->ascent = FONT_BASE (font) + boff;
26723 it->descent = FONT_DESCENT (font) - boff;
26726 if (get_char_glyph_code (it->char_to_display, font, &char2b))
26728 pcm = get_per_char_metric (font, &char2b);
26729 if (pcm->width == 0
26730 && pcm->rbearing == 0 && pcm->lbearing == 0)
26731 pcm = NULL;
26734 if (pcm)
26736 it->phys_ascent = pcm->ascent + boff;
26737 it->phys_descent = pcm->descent - boff;
26738 it->pixel_width = pcm->width;
26740 else
26742 it->glyph_not_available_p = 1;
26743 it->phys_ascent = it->ascent;
26744 it->phys_descent = it->descent;
26745 it->pixel_width = font->space_width;
26748 if (it->constrain_row_ascent_descent_p)
26750 if (it->descent > it->max_descent)
26752 it->ascent += it->descent - it->max_descent;
26753 it->descent = it->max_descent;
26755 if (it->ascent > it->max_ascent)
26757 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26758 it->ascent = it->max_ascent;
26760 it->phys_ascent = min (it->phys_ascent, it->ascent);
26761 it->phys_descent = min (it->phys_descent, it->descent);
26762 extra_line_spacing = 0;
26765 /* If this is a space inside a region of text with
26766 `space-width' property, change its width. */
26767 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
26768 if (stretched_p)
26769 it->pixel_width *= XFLOATINT (it->space_width);
26771 /* If face has a box, add the box thickness to the character
26772 height. If character has a box line to the left and/or
26773 right, add the box line width to the character's width. */
26774 if (face->box != FACE_NO_BOX)
26776 int thick = face->box_line_width;
26778 if (thick > 0)
26780 it->ascent += thick;
26781 it->descent += thick;
26783 else
26784 thick = -thick;
26786 if (it->start_of_box_run_p)
26787 it->pixel_width += thick;
26788 if (it->end_of_box_run_p)
26789 it->pixel_width += thick;
26792 /* If face has an overline, add the height of the overline
26793 (1 pixel) and a 1 pixel margin to the character height. */
26794 if (face->overline_p)
26795 it->ascent += overline_margin;
26797 if (it->constrain_row_ascent_descent_p)
26799 if (it->ascent > it->max_ascent)
26800 it->ascent = it->max_ascent;
26801 if (it->descent > it->max_descent)
26802 it->descent = it->max_descent;
26805 take_vertical_position_into_account (it);
26807 /* If we have to actually produce glyphs, do it. */
26808 if (it->glyph_row)
26810 if (stretched_p)
26812 /* Translate a space with a `space-width' property
26813 into a stretch glyph. */
26814 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
26815 / FONT_HEIGHT (font));
26816 append_stretch_glyph (it, it->object, it->pixel_width,
26817 it->ascent + it->descent, ascent);
26819 else
26820 append_glyph (it);
26822 /* If characters with lbearing or rbearing are displayed
26823 in this line, record that fact in a flag of the
26824 glyph row. This is used to optimize X output code. */
26825 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
26826 it->glyph_row->contains_overlapping_glyphs_p = 1;
26828 if (! stretched_p && it->pixel_width == 0)
26829 /* We assure that all visible glyphs have at least 1-pixel
26830 width. */
26831 it->pixel_width = 1;
26833 else if (it->char_to_display == '\n')
26835 /* A newline has no width, but we need the height of the
26836 line. But if previous part of the line sets a height,
26837 don't increase that height. */
26839 Lisp_Object height;
26840 Lisp_Object total_height = Qnil;
26842 it->override_ascent = -1;
26843 it->pixel_width = 0;
26844 it->nglyphs = 0;
26846 height = get_it_property (it, Qline_height);
26847 /* Split (line-height total-height) list. */
26848 if (CONSP (height)
26849 && CONSP (XCDR (height))
26850 && NILP (XCDR (XCDR (height))))
26852 total_height = XCAR (XCDR (height));
26853 height = XCAR (height);
26855 height = calc_line_height_property (it, height, font, boff, 1);
26857 if (it->override_ascent >= 0)
26859 it->ascent = it->override_ascent;
26860 it->descent = it->override_descent;
26861 boff = it->override_boff;
26863 else
26865 it->ascent = FONT_BASE (font) + boff;
26866 it->descent = FONT_DESCENT (font) - boff;
26869 if (EQ (height, Qt))
26871 if (it->descent > it->max_descent)
26873 it->ascent += it->descent - it->max_descent;
26874 it->descent = it->max_descent;
26876 if (it->ascent > it->max_ascent)
26878 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
26879 it->ascent = it->max_ascent;
26881 it->phys_ascent = min (it->phys_ascent, it->ascent);
26882 it->phys_descent = min (it->phys_descent, it->descent);
26883 it->constrain_row_ascent_descent_p = 1;
26884 extra_line_spacing = 0;
26886 else
26888 Lisp_Object spacing;
26890 it->phys_ascent = it->ascent;
26891 it->phys_descent = it->descent;
26893 if ((it->max_ascent > 0 || it->max_descent > 0)
26894 && face->box != FACE_NO_BOX
26895 && face->box_line_width > 0)
26897 it->ascent += face->box_line_width;
26898 it->descent += face->box_line_width;
26900 if (!NILP (height)
26901 && XINT (height) > it->ascent + it->descent)
26902 it->ascent = XINT (height) - it->descent;
26904 if (!NILP (total_height))
26905 spacing = calc_line_height_property (it, total_height, font, boff, 0);
26906 else
26908 spacing = get_it_property (it, Qline_spacing);
26909 spacing = calc_line_height_property (it, spacing, font, boff, 0);
26911 if (INTEGERP (spacing))
26913 extra_line_spacing = XINT (spacing);
26914 if (!NILP (total_height))
26915 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
26919 else /* i.e. (it->char_to_display == '\t') */
26921 if (font->space_width > 0)
26923 int tab_width = it->tab_width * font->space_width;
26924 int x = it->current_x + it->continuation_lines_width;
26925 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
26927 /* If the distance from the current position to the next tab
26928 stop is less than a space character width, use the
26929 tab stop after that. */
26930 if (next_tab_x - x < font->space_width)
26931 next_tab_x += tab_width;
26933 it->pixel_width = next_tab_x - x;
26934 it->nglyphs = 1;
26935 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
26936 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
26938 if (it->glyph_row)
26940 append_stretch_glyph (it, it->object, it->pixel_width,
26941 it->ascent + it->descent, it->ascent);
26944 else
26946 it->pixel_width = 0;
26947 it->nglyphs = 1;
26951 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
26953 /* A static composition.
26955 Note: A composition is represented as one glyph in the
26956 glyph matrix. There are no padding glyphs.
26958 Important note: pixel_width, ascent, and descent are the
26959 values of what is drawn by draw_glyphs (i.e. the values of
26960 the overall glyphs composed). */
26961 struct face *face = FACE_FROM_ID (it->f, it->face_id);
26962 int boff; /* baseline offset */
26963 struct composition *cmp = composition_table[it->cmp_it.id];
26964 int glyph_len = cmp->glyph_len;
26965 struct font *font = face->font;
26967 it->nglyphs = 1;
26969 /* If we have not yet calculated pixel size data of glyphs of
26970 the composition for the current face font, calculate them
26971 now. Theoretically, we have to check all fonts for the
26972 glyphs, but that requires much time and memory space. So,
26973 here we check only the font of the first glyph. This may
26974 lead to incorrect display, but it's very rare, and C-l
26975 (recenter-top-bottom) can correct the display anyway. */
26976 if (! cmp->font || cmp->font != font)
26978 /* Ascent and descent of the font of the first character
26979 of this composition (adjusted by baseline offset).
26980 Ascent and descent of overall glyphs should not be less
26981 than these, respectively. */
26982 int font_ascent, font_descent, font_height;
26983 /* Bounding box of the overall glyphs. */
26984 int leftmost, rightmost, lowest, highest;
26985 int lbearing, rbearing;
26986 int i, width, ascent, descent;
26987 int left_padded = 0, right_padded = 0;
26988 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
26989 XChar2b char2b;
26990 struct font_metrics *pcm;
26991 int font_not_found_p;
26992 ptrdiff_t pos;
26994 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
26995 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
26996 break;
26997 if (glyph_len < cmp->glyph_len)
26998 right_padded = 1;
26999 for (i = 0; i < glyph_len; i++)
27001 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
27002 break;
27003 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27005 if (i > 0)
27006 left_padded = 1;
27008 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
27009 : IT_CHARPOS (*it));
27010 /* If no suitable font is found, use the default font. */
27011 font_not_found_p = font == NULL;
27012 if (font_not_found_p)
27014 face = face->ascii_face;
27015 font = face->font;
27017 boff = font->baseline_offset;
27018 if (font->vertical_centering)
27019 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
27020 font_ascent = FONT_BASE (font) + boff;
27021 font_descent = FONT_DESCENT (font) - boff;
27022 font_height = FONT_HEIGHT (font);
27024 cmp->font = font;
27026 pcm = NULL;
27027 if (! font_not_found_p)
27029 get_char_face_and_encoding (it->f, c, it->face_id,
27030 &char2b, 0);
27031 pcm = get_per_char_metric (font, &char2b);
27034 /* Initialize the bounding box. */
27035 if (pcm)
27037 width = cmp->glyph_len > 0 ? pcm->width : 0;
27038 ascent = pcm->ascent;
27039 descent = pcm->descent;
27040 lbearing = pcm->lbearing;
27041 rbearing = pcm->rbearing;
27043 else
27045 width = cmp->glyph_len > 0 ? font->space_width : 0;
27046 ascent = FONT_BASE (font);
27047 descent = FONT_DESCENT (font);
27048 lbearing = 0;
27049 rbearing = width;
27052 rightmost = width;
27053 leftmost = 0;
27054 lowest = - descent + boff;
27055 highest = ascent + boff;
27057 if (! font_not_found_p
27058 && font->default_ascent
27059 && CHAR_TABLE_P (Vuse_default_ascent)
27060 && !NILP (Faref (Vuse_default_ascent,
27061 make_number (it->char_to_display))))
27062 highest = font->default_ascent + boff;
27064 /* Draw the first glyph at the normal position. It may be
27065 shifted to right later if some other glyphs are drawn
27066 at the left. */
27067 cmp->offsets[i * 2] = 0;
27068 cmp->offsets[i * 2 + 1] = boff;
27069 cmp->lbearing = lbearing;
27070 cmp->rbearing = rbearing;
27072 /* Set cmp->offsets for the remaining glyphs. */
27073 for (i++; i < glyph_len; i++)
27075 int left, right, btm, top;
27076 int ch = COMPOSITION_GLYPH (cmp, i);
27077 int face_id;
27078 struct face *this_face;
27080 if (ch == '\t')
27081 ch = ' ';
27082 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
27083 this_face = FACE_FROM_ID (it->f, face_id);
27084 font = this_face->font;
27086 if (font == NULL)
27087 pcm = NULL;
27088 else
27090 get_char_face_and_encoding (it->f, ch, face_id,
27091 &char2b, 0);
27092 pcm = get_per_char_metric (font, &char2b);
27094 if (! pcm)
27095 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
27096 else
27098 width = pcm->width;
27099 ascent = pcm->ascent;
27100 descent = pcm->descent;
27101 lbearing = pcm->lbearing;
27102 rbearing = pcm->rbearing;
27103 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
27105 /* Relative composition with or without
27106 alternate chars. */
27107 left = (leftmost + rightmost - width) / 2;
27108 btm = - descent + boff;
27109 if (font->relative_compose
27110 && (! CHAR_TABLE_P (Vignore_relative_composition)
27111 || NILP (Faref (Vignore_relative_composition,
27112 make_number (ch)))))
27115 if (- descent >= font->relative_compose)
27116 /* One extra pixel between two glyphs. */
27117 btm = highest + 1;
27118 else if (ascent <= 0)
27119 /* One extra pixel between two glyphs. */
27120 btm = lowest - 1 - ascent - descent;
27123 else
27125 /* A composition rule is specified by an integer
27126 value that encodes global and new reference
27127 points (GREF and NREF). GREF and NREF are
27128 specified by numbers as below:
27130 0---1---2 -- ascent
27134 9--10--11 -- center
27136 ---3---4---5--- baseline
27138 6---7---8 -- descent
27140 int rule = COMPOSITION_RULE (cmp, i);
27141 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
27143 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
27144 grefx = gref % 3, nrefx = nref % 3;
27145 grefy = gref / 3, nrefy = nref / 3;
27146 if (xoff)
27147 xoff = font_height * (xoff - 128) / 256;
27148 if (yoff)
27149 yoff = font_height * (yoff - 128) / 256;
27151 left = (leftmost
27152 + grefx * (rightmost - leftmost) / 2
27153 - nrefx * width / 2
27154 + xoff);
27156 btm = ((grefy == 0 ? highest
27157 : grefy == 1 ? 0
27158 : grefy == 2 ? lowest
27159 : (highest + lowest) / 2)
27160 - (nrefy == 0 ? ascent + descent
27161 : nrefy == 1 ? descent - boff
27162 : nrefy == 2 ? 0
27163 : (ascent + descent) / 2)
27164 + yoff);
27167 cmp->offsets[i * 2] = left;
27168 cmp->offsets[i * 2 + 1] = btm + descent;
27170 /* Update the bounding box of the overall glyphs. */
27171 if (width > 0)
27173 right = left + width;
27174 if (left < leftmost)
27175 leftmost = left;
27176 if (right > rightmost)
27177 rightmost = right;
27179 top = btm + descent + ascent;
27180 if (top > highest)
27181 highest = top;
27182 if (btm < lowest)
27183 lowest = btm;
27185 if (cmp->lbearing > left + lbearing)
27186 cmp->lbearing = left + lbearing;
27187 if (cmp->rbearing < left + rbearing)
27188 cmp->rbearing = left + rbearing;
27192 /* If there are glyphs whose x-offsets are negative,
27193 shift all glyphs to the right and make all x-offsets
27194 non-negative. */
27195 if (leftmost < 0)
27197 for (i = 0; i < cmp->glyph_len; i++)
27198 cmp->offsets[i * 2] -= leftmost;
27199 rightmost -= leftmost;
27200 cmp->lbearing -= leftmost;
27201 cmp->rbearing -= leftmost;
27204 if (left_padded && cmp->lbearing < 0)
27206 for (i = 0; i < cmp->glyph_len; i++)
27207 cmp->offsets[i * 2] -= cmp->lbearing;
27208 rightmost -= cmp->lbearing;
27209 cmp->rbearing -= cmp->lbearing;
27210 cmp->lbearing = 0;
27212 if (right_padded && rightmost < cmp->rbearing)
27214 rightmost = cmp->rbearing;
27217 cmp->pixel_width = rightmost;
27218 cmp->ascent = highest;
27219 cmp->descent = - lowest;
27220 if (cmp->ascent < font_ascent)
27221 cmp->ascent = font_ascent;
27222 if (cmp->descent < font_descent)
27223 cmp->descent = font_descent;
27226 if (it->glyph_row
27227 && (cmp->lbearing < 0
27228 || cmp->rbearing > cmp->pixel_width))
27229 it->glyph_row->contains_overlapping_glyphs_p = 1;
27231 it->pixel_width = cmp->pixel_width;
27232 it->ascent = it->phys_ascent = cmp->ascent;
27233 it->descent = it->phys_descent = cmp->descent;
27234 if (face->box != FACE_NO_BOX)
27236 int thick = face->box_line_width;
27238 if (thick > 0)
27240 it->ascent += thick;
27241 it->descent += thick;
27243 else
27244 thick = - thick;
27246 if (it->start_of_box_run_p)
27247 it->pixel_width += thick;
27248 if (it->end_of_box_run_p)
27249 it->pixel_width += thick;
27252 /* If face has an overline, add the height of the overline
27253 (1 pixel) and a 1 pixel margin to the character height. */
27254 if (face->overline_p)
27255 it->ascent += overline_margin;
27257 take_vertical_position_into_account (it);
27258 if (it->ascent < 0)
27259 it->ascent = 0;
27260 if (it->descent < 0)
27261 it->descent = 0;
27263 if (it->glyph_row && cmp->glyph_len > 0)
27264 append_composite_glyph (it);
27266 else if (it->what == IT_COMPOSITION)
27268 /* A dynamic (automatic) composition. */
27269 struct face *face = FACE_FROM_ID (it->f, it->face_id);
27270 Lisp_Object gstring;
27271 struct font_metrics metrics;
27273 it->nglyphs = 1;
27275 gstring = composition_gstring_from_id (it->cmp_it.id);
27276 it->pixel_width
27277 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
27278 &metrics);
27279 if (it->glyph_row
27280 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
27281 it->glyph_row->contains_overlapping_glyphs_p = 1;
27282 it->ascent = it->phys_ascent = metrics.ascent;
27283 it->descent = it->phys_descent = metrics.descent;
27284 if (face->box != FACE_NO_BOX)
27286 int thick = face->box_line_width;
27288 if (thick > 0)
27290 it->ascent += thick;
27291 it->descent += thick;
27293 else
27294 thick = - thick;
27296 if (it->start_of_box_run_p)
27297 it->pixel_width += thick;
27298 if (it->end_of_box_run_p)
27299 it->pixel_width += thick;
27301 /* If face has an overline, add the height of the overline
27302 (1 pixel) and a 1 pixel margin to the character height. */
27303 if (face->overline_p)
27304 it->ascent += overline_margin;
27305 take_vertical_position_into_account (it);
27306 if (it->ascent < 0)
27307 it->ascent = 0;
27308 if (it->descent < 0)
27309 it->descent = 0;
27311 if (it->glyph_row)
27312 append_composite_glyph (it);
27314 else if (it->what == IT_GLYPHLESS)
27315 produce_glyphless_glyph (it, 0, Qnil);
27316 else if (it->what == IT_IMAGE)
27317 produce_image_glyph (it);
27318 else if (it->what == IT_STRETCH)
27319 produce_stretch_glyph (it);
27320 #ifdef HAVE_XWIDGETS
27321 else if (it->what == IT_XWIDGET)
27322 produce_xwidget_glyph (it);
27323 #endif
27324 done:
27325 /* Accumulate dimensions. Note: can't assume that it->descent > 0
27326 because this isn't true for images with `:ascent 100'. */
27327 eassert (it->ascent >= 0 && it->descent >= 0);
27328 if (it->area == TEXT_AREA)
27329 it->current_x += it->pixel_width;
27331 if (extra_line_spacing > 0)
27333 it->descent += extra_line_spacing;
27334 if (extra_line_spacing > it->max_extra_line_spacing)
27335 it->max_extra_line_spacing = extra_line_spacing;
27338 it->max_ascent = max (it->max_ascent, it->ascent);
27339 it->max_descent = max (it->max_descent, it->descent);
27340 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
27341 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
27344 /* EXPORT for RIF:
27345 Output LEN glyphs starting at START at the nominal cursor position.
27346 Advance the nominal cursor over the text. UPDATED_ROW is the glyph row
27347 being updated, and UPDATED_AREA is the area of that row being updated. */
27349 void
27350 x_write_glyphs (struct window *w, struct glyph_row *updated_row,
27351 struct glyph *start, enum glyph_row_area updated_area, int len)
27353 int x, hpos, chpos = w->phys_cursor.hpos;
27355 eassert (updated_row);
27356 /* When the window is hscrolled, cursor hpos can legitimately be out
27357 of bounds, but we draw the cursor at the corresponding window
27358 margin in that case. */
27359 if (!updated_row->reversed_p && chpos < 0)
27360 chpos = 0;
27361 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
27362 chpos = updated_row->used[TEXT_AREA] - 1;
27364 block_input ();
27366 /* Write glyphs. */
27368 hpos = start - updated_row->glyphs[updated_area];
27369 x = draw_glyphs (w, w->output_cursor.x,
27370 updated_row, updated_area,
27371 hpos, hpos + len,
27372 DRAW_NORMAL_TEXT, 0);
27374 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
27375 if (updated_area == TEXT_AREA
27376 && w->phys_cursor_on_p
27377 && w->phys_cursor.vpos == w->output_cursor.vpos
27378 && chpos >= hpos
27379 && chpos < hpos + len)
27380 w->phys_cursor_on_p = 0;
27382 unblock_input ();
27384 /* Advance the output cursor. */
27385 w->output_cursor.hpos += len;
27386 w->output_cursor.x = x;
27390 /* EXPORT for RIF:
27391 Insert LEN glyphs from START at the nominal cursor position. */
27393 void
27394 x_insert_glyphs (struct window *w, struct glyph_row *updated_row,
27395 struct glyph *start, enum glyph_row_area updated_area, int len)
27397 struct frame *f;
27398 int line_height, shift_by_width, shifted_region_width;
27399 struct glyph_row *row;
27400 struct glyph *glyph;
27401 int frame_x, frame_y;
27402 ptrdiff_t hpos;
27404 eassert (updated_row);
27405 block_input ();
27406 f = XFRAME (WINDOW_FRAME (w));
27408 /* Get the height of the line we are in. */
27409 row = updated_row;
27410 line_height = row->height;
27412 /* Get the width of the glyphs to insert. */
27413 shift_by_width = 0;
27414 for (glyph = start; glyph < start + len; ++glyph)
27415 shift_by_width += glyph->pixel_width;
27417 /* Get the width of the region to shift right. */
27418 shifted_region_width = (window_box_width (w, updated_area)
27419 - w->output_cursor.x
27420 - shift_by_width);
27422 /* Shift right. */
27423 frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
27424 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
27426 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
27427 line_height, shift_by_width);
27429 /* Write the glyphs. */
27430 hpos = start - row->glyphs[updated_area];
27431 draw_glyphs (w, w->output_cursor.x, row, updated_area,
27432 hpos, hpos + len,
27433 DRAW_NORMAL_TEXT, 0);
27435 /* Advance the output cursor. */
27436 w->output_cursor.hpos += len;
27437 w->output_cursor.x += shift_by_width;
27438 unblock_input ();
27442 /* EXPORT for RIF:
27443 Erase the current text line from the nominal cursor position
27444 (inclusive) to pixel column TO_X (exclusive). The idea is that
27445 everything from TO_X onward is already erased.
27447 TO_X is a pixel position relative to UPDATED_AREA of currently
27448 updated window W. TO_X == -1 means clear to the end of this area. */
27450 void
27451 x_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
27452 enum glyph_row_area updated_area, int to_x)
27454 struct frame *f;
27455 int max_x, min_y, max_y;
27456 int from_x, from_y, to_y;
27458 eassert (updated_row);
27459 f = XFRAME (w->frame);
27461 if (updated_row->full_width_p)
27462 max_x = (WINDOW_PIXEL_WIDTH (w)
27463 - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
27464 else
27465 max_x = window_box_width (w, updated_area);
27466 max_y = window_text_bottom_y (w);
27468 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
27469 of window. For TO_X > 0, truncate to end of drawing area. */
27470 if (to_x == 0)
27471 return;
27472 else if (to_x < 0)
27473 to_x = max_x;
27474 else
27475 to_x = min (to_x, max_x);
27477 to_y = min (max_y, w->output_cursor.y + updated_row->height);
27479 /* Notice if the cursor will be cleared by this operation. */
27480 if (!updated_row->full_width_p)
27481 notice_overwritten_cursor (w, updated_area,
27482 w->output_cursor.x, -1,
27483 updated_row->y,
27484 MATRIX_ROW_BOTTOM_Y (updated_row));
27486 from_x = w->output_cursor.x;
27488 /* Translate to frame coordinates. */
27489 if (updated_row->full_width_p)
27491 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
27492 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
27494 else
27496 int area_left = window_box_left (w, updated_area);
27497 from_x += area_left;
27498 to_x += area_left;
27501 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
27502 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
27503 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
27505 /* Prevent inadvertently clearing to end of the X window. */
27506 if (to_x > from_x && to_y > from_y)
27508 block_input ();
27509 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
27510 to_x - from_x, to_y - from_y);
27511 unblock_input ();
27515 #endif /* HAVE_WINDOW_SYSTEM */
27519 /***********************************************************************
27520 Cursor types
27521 ***********************************************************************/
27523 /* Value is the internal representation of the specified cursor type
27524 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
27525 of the bar cursor. */
27527 static enum text_cursor_kinds
27528 get_specified_cursor_type (Lisp_Object arg, int *width)
27530 enum text_cursor_kinds type;
27532 if (NILP (arg))
27533 return NO_CURSOR;
27535 if (EQ (arg, Qbox))
27536 return FILLED_BOX_CURSOR;
27538 if (EQ (arg, Qhollow))
27539 return HOLLOW_BOX_CURSOR;
27541 if (EQ (arg, Qbar))
27543 *width = 2;
27544 return BAR_CURSOR;
27547 if (CONSP (arg)
27548 && EQ (XCAR (arg), Qbar)
27549 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27551 *width = XINT (XCDR (arg));
27552 return BAR_CURSOR;
27555 if (EQ (arg, Qhbar))
27557 *width = 2;
27558 return HBAR_CURSOR;
27561 if (CONSP (arg)
27562 && EQ (XCAR (arg), Qhbar)
27563 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
27565 *width = XINT (XCDR (arg));
27566 return HBAR_CURSOR;
27569 /* Treat anything unknown as "hollow box cursor".
27570 It was bad to signal an error; people have trouble fixing
27571 .Xdefaults with Emacs, when it has something bad in it. */
27572 type = HOLLOW_BOX_CURSOR;
27574 return type;
27577 /* Set the default cursor types for specified frame. */
27578 void
27579 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
27581 int width = 1;
27582 Lisp_Object tem;
27584 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
27585 FRAME_CURSOR_WIDTH (f) = width;
27587 /* By default, set up the blink-off state depending on the on-state. */
27589 tem = Fassoc (arg, Vblink_cursor_alist);
27590 if (!NILP (tem))
27592 FRAME_BLINK_OFF_CURSOR (f)
27593 = get_specified_cursor_type (XCDR (tem), &width);
27594 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
27596 else
27597 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
27599 /* Make sure the cursor gets redrawn. */
27600 f->cursor_type_changed = 1;
27604 #ifdef HAVE_WINDOW_SYSTEM
27606 /* Return the cursor we want to be displayed in window W. Return
27607 width of bar/hbar cursor through WIDTH arg. Return with
27608 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
27609 (i.e. if the `system caret' should track this cursor).
27611 In a mini-buffer window, we want the cursor only to appear if we
27612 are reading input from this window. For the selected window, we
27613 want the cursor type given by the frame parameter or buffer local
27614 setting of cursor-type. If explicitly marked off, draw no cursor.
27615 In all other cases, we want a hollow box cursor. */
27617 static enum text_cursor_kinds
27618 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
27619 int *active_cursor)
27621 struct frame *f = XFRAME (w->frame);
27622 struct buffer *b = XBUFFER (w->contents);
27623 int cursor_type = DEFAULT_CURSOR;
27624 Lisp_Object alt_cursor;
27625 int non_selected = 0;
27627 *active_cursor = 1;
27629 /* Echo area */
27630 if (cursor_in_echo_area
27631 && FRAME_HAS_MINIBUF_P (f)
27632 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
27634 if (w == XWINDOW (echo_area_window))
27636 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
27638 *width = FRAME_CURSOR_WIDTH (f);
27639 return FRAME_DESIRED_CURSOR (f);
27641 else
27642 return get_specified_cursor_type (BVAR (b, cursor_type), width);
27645 *active_cursor = 0;
27646 non_selected = 1;
27649 /* Detect a nonselected window or nonselected frame. */
27650 else if (w != XWINDOW (f->selected_window)
27651 || f != FRAME_DISPLAY_INFO (f)->x_highlight_frame)
27653 *active_cursor = 0;
27655 if (MINI_WINDOW_P (w) && minibuf_level == 0)
27656 return NO_CURSOR;
27658 non_selected = 1;
27661 /* Never display a cursor in a window in which cursor-type is nil. */
27662 if (NILP (BVAR (b, cursor_type)))
27663 return NO_CURSOR;
27665 /* Get the normal cursor type for this window. */
27666 if (EQ (BVAR (b, cursor_type), Qt))
27668 cursor_type = FRAME_DESIRED_CURSOR (f);
27669 *width = FRAME_CURSOR_WIDTH (f);
27671 else
27672 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
27674 /* Use cursor-in-non-selected-windows instead
27675 for non-selected window or frame. */
27676 if (non_selected)
27678 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
27679 if (!EQ (Qt, alt_cursor))
27680 return get_specified_cursor_type (alt_cursor, width);
27681 /* t means modify the normal cursor type. */
27682 if (cursor_type == FILLED_BOX_CURSOR)
27683 cursor_type = HOLLOW_BOX_CURSOR;
27684 else if (cursor_type == BAR_CURSOR && *width > 1)
27685 --*width;
27686 return cursor_type;
27689 /* Use normal cursor if not blinked off. */
27690 if (!w->cursor_off_p)
27693 #ifdef HAVE_XWIDGETS
27694 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
27695 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
27696 return NO_CURSOR;
27698 #endif
27699 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27701 if (cursor_type == FILLED_BOX_CURSOR)
27703 /* Using a block cursor on large images can be very annoying.
27704 So use a hollow cursor for "large" images.
27705 If image is not transparent (no mask), also use hollow cursor. */
27706 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27707 if (img != NULL && IMAGEP (img->spec))
27709 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
27710 where N = size of default frame font size.
27711 This should cover most of the "tiny" icons people may use. */
27712 if (!img->mask
27713 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
27714 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
27715 cursor_type = HOLLOW_BOX_CURSOR;
27718 else if (cursor_type != NO_CURSOR)
27720 /* Display current only supports BOX and HOLLOW cursors for images.
27721 So for now, unconditionally use a HOLLOW cursor when cursor is
27722 not a solid box cursor. */
27723 cursor_type = HOLLOW_BOX_CURSOR;
27726 return cursor_type;
27729 /* Cursor is blinked off, so determine how to "toggle" it. */
27731 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
27732 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
27733 return get_specified_cursor_type (XCDR (alt_cursor), width);
27735 /* Then see if frame has specified a specific blink off cursor type. */
27736 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
27738 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
27739 return FRAME_BLINK_OFF_CURSOR (f);
27742 #if 0
27743 /* Some people liked having a permanently visible blinking cursor,
27744 while others had very strong opinions against it. So it was
27745 decided to remove it. KFS 2003-09-03 */
27747 /* Finally perform built-in cursor blinking:
27748 filled box <-> hollow box
27749 wide [h]bar <-> narrow [h]bar
27750 narrow [h]bar <-> no cursor
27751 other type <-> no cursor */
27753 if (cursor_type == FILLED_BOX_CURSOR)
27754 return HOLLOW_BOX_CURSOR;
27756 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
27758 *width = 1;
27759 return cursor_type;
27761 #endif
27763 return NO_CURSOR;
27767 /* Notice when the text cursor of window W has been completely
27768 overwritten by a drawing operation that outputs glyphs in AREA
27769 starting at X0 and ending at X1 in the line starting at Y0 and
27770 ending at Y1. X coordinates are area-relative. X1 < 0 means all
27771 the rest of the line after X0 has been written. Y coordinates
27772 are window-relative. */
27774 static void
27775 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
27776 int x0, int x1, int y0, int y1)
27778 int cx0, cx1, cy0, cy1;
27779 struct glyph_row *row;
27781 if (!w->phys_cursor_on_p)
27782 return;
27783 if (area != TEXT_AREA)
27784 return;
27786 if (w->phys_cursor.vpos < 0
27787 || w->phys_cursor.vpos >= w->current_matrix->nrows
27788 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
27789 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
27790 return;
27792 if (row->cursor_in_fringe_p)
27794 row->cursor_in_fringe_p = 0;
27795 draw_fringe_bitmap (w, row, row->reversed_p);
27796 w->phys_cursor_on_p = 0;
27797 return;
27800 cx0 = w->phys_cursor.x;
27801 cx1 = cx0 + w->phys_cursor_width;
27802 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
27803 return;
27805 /* The cursor image will be completely removed from the
27806 screen if the output area intersects the cursor area in
27807 y-direction. When we draw in [y0 y1[, and some part of
27808 the cursor is at y < y0, that part must have been drawn
27809 before. When scrolling, the cursor is erased before
27810 actually scrolling, so we don't come here. When not
27811 scrolling, the rows above the old cursor row must have
27812 changed, and in this case these rows must have written
27813 over the cursor image.
27815 Likewise if part of the cursor is below y1, with the
27816 exception of the cursor being in the first blank row at
27817 the buffer and window end because update_text_area
27818 doesn't draw that row. (Except when it does, but
27819 that's handled in update_text_area.) */
27821 cy0 = w->phys_cursor.y;
27822 cy1 = cy0 + w->phys_cursor_height;
27823 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
27824 return;
27826 w->phys_cursor_on_p = 0;
27829 #endif /* HAVE_WINDOW_SYSTEM */
27832 /************************************************************************
27833 Mouse Face
27834 ************************************************************************/
27836 #ifdef HAVE_WINDOW_SYSTEM
27838 /* EXPORT for RIF:
27839 Fix the display of area AREA of overlapping row ROW in window W
27840 with respect to the overlapping part OVERLAPS. */
27842 void
27843 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
27844 enum glyph_row_area area, int overlaps)
27846 int i, x;
27848 block_input ();
27850 x = 0;
27851 for (i = 0; i < row->used[area];)
27853 if (row->glyphs[area][i].overlaps_vertically_p)
27855 int start = i, start_x = x;
27859 x += row->glyphs[area][i].pixel_width;
27860 ++i;
27862 while (i < row->used[area]
27863 && row->glyphs[area][i].overlaps_vertically_p);
27865 draw_glyphs (w, start_x, row, area,
27866 start, i,
27867 DRAW_NORMAL_TEXT, overlaps);
27869 else
27871 x += row->glyphs[area][i].pixel_width;
27872 ++i;
27876 unblock_input ();
27880 /* EXPORT:
27881 Draw the cursor glyph of window W in glyph row ROW. See the
27882 comment of draw_glyphs for the meaning of HL. */
27884 void
27885 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
27886 enum draw_glyphs_face hl)
27888 /* If cursor hpos is out of bounds, don't draw garbage. This can
27889 happen in mini-buffer windows when switching between echo area
27890 glyphs and mini-buffer. */
27891 if ((row->reversed_p
27892 ? (w->phys_cursor.hpos >= 0)
27893 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
27895 int on_p = w->phys_cursor_on_p;
27896 int x1;
27897 int hpos = w->phys_cursor.hpos;
27899 /* When the window is hscrolled, cursor hpos can legitimately be
27900 out of bounds, but we draw the cursor at the corresponding
27901 window margin in that case. */
27902 if (!row->reversed_p && hpos < 0)
27903 hpos = 0;
27904 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
27905 hpos = row->used[TEXT_AREA] - 1;
27907 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
27908 hl, 0);
27909 w->phys_cursor_on_p = on_p;
27911 if (hl == DRAW_CURSOR)
27912 w->phys_cursor_width = x1 - w->phys_cursor.x;
27913 /* When we erase the cursor, and ROW is overlapped by other
27914 rows, make sure that these overlapping parts of other rows
27915 are redrawn. */
27916 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
27918 w->phys_cursor_width = x1 - w->phys_cursor.x;
27920 if (row > w->current_matrix->rows
27921 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
27922 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
27923 OVERLAPS_ERASED_CURSOR);
27925 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
27926 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
27927 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
27928 OVERLAPS_ERASED_CURSOR);
27934 /* Erase the image of a cursor of window W from the screen. */
27936 void
27937 erase_phys_cursor (struct window *w)
27939 struct frame *f = XFRAME (w->frame);
27940 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27941 int hpos = w->phys_cursor.hpos;
27942 int vpos = w->phys_cursor.vpos;
27943 int mouse_face_here_p = 0;
27944 struct glyph_matrix *active_glyphs = w->current_matrix;
27945 struct glyph_row *cursor_row;
27946 struct glyph *cursor_glyph;
27947 enum draw_glyphs_face hl;
27949 /* No cursor displayed or row invalidated => nothing to do on the
27950 screen. */
27951 if (w->phys_cursor_type == NO_CURSOR)
27952 goto mark_cursor_off;
27954 /* VPOS >= active_glyphs->nrows means that window has been resized.
27955 Don't bother to erase the cursor. */
27956 if (vpos >= active_glyphs->nrows)
27957 goto mark_cursor_off;
27959 /* If row containing cursor is marked invalid, there is nothing we
27960 can do. */
27961 cursor_row = MATRIX_ROW (active_glyphs, vpos);
27962 if (!cursor_row->enabled_p)
27963 goto mark_cursor_off;
27965 /* If line spacing is > 0, old cursor may only be partially visible in
27966 window after split-window. So adjust visible height. */
27967 cursor_row->visible_height = min (cursor_row->visible_height,
27968 window_text_bottom_y (w) - cursor_row->y);
27970 /* If row is completely invisible, don't attempt to delete a cursor which
27971 isn't there. This can happen if cursor is at top of a window, and
27972 we switch to a buffer with a header line in that window. */
27973 if (cursor_row->visible_height <= 0)
27974 goto mark_cursor_off;
27976 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
27977 if (cursor_row->cursor_in_fringe_p)
27979 cursor_row->cursor_in_fringe_p = 0;
27980 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
27981 goto mark_cursor_off;
27984 /* This can happen when the new row is shorter than the old one.
27985 In this case, either draw_glyphs or clear_end_of_line
27986 should have cleared the cursor. Note that we wouldn't be
27987 able to erase the cursor in this case because we don't have a
27988 cursor glyph at hand. */
27989 if ((cursor_row->reversed_p
27990 ? (w->phys_cursor.hpos < 0)
27991 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
27992 goto mark_cursor_off;
27994 /* When the window is hscrolled, cursor hpos can legitimately be out
27995 of bounds, but we draw the cursor at the corresponding window
27996 margin in that case. */
27997 if (!cursor_row->reversed_p && hpos < 0)
27998 hpos = 0;
27999 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
28000 hpos = cursor_row->used[TEXT_AREA] - 1;
28002 /* If the cursor is in the mouse face area, redisplay that when
28003 we clear the cursor. */
28004 if (! NILP (hlinfo->mouse_face_window)
28005 && coords_in_mouse_face_p (w, hpos, vpos)
28006 /* Don't redraw the cursor's spot in mouse face if it is at the
28007 end of a line (on a newline). The cursor appears there, but
28008 mouse highlighting does not. */
28009 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
28010 mouse_face_here_p = 1;
28012 /* Maybe clear the display under the cursor. */
28013 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
28015 int x, y;
28016 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
28017 int width;
28019 cursor_glyph = get_phys_cursor_glyph (w);
28020 if (cursor_glyph == NULL)
28021 goto mark_cursor_off;
28023 width = cursor_glyph->pixel_width;
28024 x = w->phys_cursor.x;
28025 if (x < 0)
28027 width += x;
28028 x = 0;
28030 width = min (width, window_box_width (w, TEXT_AREA) - x);
28031 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
28032 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
28034 if (width > 0)
28035 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
28038 /* Erase the cursor by redrawing the character underneath it. */
28039 if (mouse_face_here_p)
28040 hl = DRAW_MOUSE_FACE;
28041 else
28042 hl = DRAW_NORMAL_TEXT;
28043 draw_phys_cursor_glyph (w, cursor_row, hl);
28045 mark_cursor_off:
28046 w->phys_cursor_on_p = 0;
28047 w->phys_cursor_type = NO_CURSOR;
28051 /* EXPORT:
28052 Display or clear cursor of window W. If ON is zero, clear the
28053 cursor. If it is non-zero, display the cursor. If ON is nonzero,
28054 where to put the cursor is specified by HPOS, VPOS, X and Y. */
28056 void
28057 display_and_set_cursor (struct window *w, bool on,
28058 int hpos, int vpos, int x, int y)
28060 struct frame *f = XFRAME (w->frame);
28061 int new_cursor_type;
28062 int new_cursor_width;
28063 int active_cursor;
28064 struct glyph_row *glyph_row;
28065 struct glyph *glyph;
28067 /* This is pointless on invisible frames, and dangerous on garbaged
28068 windows and frames; in the latter case, the frame or window may
28069 be in the midst of changing its size, and x and y may be off the
28070 window. */
28071 if (! FRAME_VISIBLE_P (f)
28072 || FRAME_GARBAGED_P (f)
28073 || vpos >= w->current_matrix->nrows
28074 || hpos >= w->current_matrix->matrix_w)
28075 return;
28077 /* If cursor is off and we want it off, return quickly. */
28078 if (!on && !w->phys_cursor_on_p)
28079 return;
28081 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
28082 /* If cursor row is not enabled, we don't really know where to
28083 display the cursor. */
28084 if (!glyph_row->enabled_p)
28086 w->phys_cursor_on_p = 0;
28087 return;
28090 glyph = NULL;
28091 if (!glyph_row->exact_window_width_line_p
28092 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
28093 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
28095 eassert (input_blocked_p ());
28097 /* Set new_cursor_type to the cursor we want to be displayed. */
28098 new_cursor_type = get_window_cursor_type (w, glyph,
28099 &new_cursor_width, &active_cursor);
28101 /* If cursor is currently being shown and we don't want it to be or
28102 it is in the wrong place, or the cursor type is not what we want,
28103 erase it. */
28104 if (w->phys_cursor_on_p
28105 && (!on
28106 || w->phys_cursor.x != x
28107 || w->phys_cursor.y != y
28108 /* HPOS can be negative in R2L rows whose
28109 exact_window_width_line_p flag is set (i.e. their newline
28110 would "overflow into the fringe"). */
28111 || hpos < 0
28112 || new_cursor_type != w->phys_cursor_type
28113 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
28114 && new_cursor_width != w->phys_cursor_width)))
28115 erase_phys_cursor (w);
28117 /* Don't check phys_cursor_on_p here because that flag is only set
28118 to zero in some cases where we know that the cursor has been
28119 completely erased, to avoid the extra work of erasing the cursor
28120 twice. In other words, phys_cursor_on_p can be 1 and the cursor
28121 still not be visible, or it has only been partly erased. */
28122 if (on)
28124 w->phys_cursor_ascent = glyph_row->ascent;
28125 w->phys_cursor_height = glyph_row->height;
28127 /* Set phys_cursor_.* before x_draw_.* is called because some
28128 of them may need the information. */
28129 w->phys_cursor.x = x;
28130 w->phys_cursor.y = glyph_row->y;
28131 w->phys_cursor.hpos = hpos;
28132 w->phys_cursor.vpos = vpos;
28135 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
28136 new_cursor_type, new_cursor_width,
28137 on, active_cursor);
28141 /* Switch the display of W's cursor on or off, according to the value
28142 of ON. */
28144 static void
28145 update_window_cursor (struct window *w, bool on)
28147 /* Don't update cursor in windows whose frame is in the process
28148 of being deleted. */
28149 if (w->current_matrix)
28151 int hpos = w->phys_cursor.hpos;
28152 int vpos = w->phys_cursor.vpos;
28153 struct glyph_row *row;
28155 if (vpos >= w->current_matrix->nrows
28156 || hpos >= w->current_matrix->matrix_w)
28157 return;
28159 row = MATRIX_ROW (w->current_matrix, vpos);
28161 /* When the window is hscrolled, cursor hpos can legitimately be
28162 out of bounds, but we draw the cursor at the corresponding
28163 window margin in that case. */
28164 if (!row->reversed_p && hpos < 0)
28165 hpos = 0;
28166 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28167 hpos = row->used[TEXT_AREA] - 1;
28169 block_input ();
28170 display_and_set_cursor (w, on, hpos, vpos,
28171 w->phys_cursor.x, w->phys_cursor.y);
28172 unblock_input ();
28177 /* Call update_window_cursor with parameter ON_P on all leaf windows
28178 in the window tree rooted at W. */
28180 static void
28181 update_cursor_in_window_tree (struct window *w, bool on_p)
28183 while (w)
28185 if (WINDOWP (w->contents))
28186 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
28187 else
28188 update_window_cursor (w, on_p);
28190 w = NILP (w->next) ? 0 : XWINDOW (w->next);
28195 /* EXPORT:
28196 Display the cursor on window W, or clear it, according to ON_P.
28197 Don't change the cursor's position. */
28199 void
28200 x_update_cursor (struct frame *f, bool on_p)
28202 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
28206 /* EXPORT:
28207 Clear the cursor of window W to background color, and mark the
28208 cursor as not shown. This is used when the text where the cursor
28209 is about to be rewritten. */
28211 void
28212 x_clear_cursor (struct window *w)
28214 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
28215 update_window_cursor (w, 0);
28218 #endif /* HAVE_WINDOW_SYSTEM */
28220 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
28221 and MSDOS. */
28222 static void
28223 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
28224 int start_hpos, int end_hpos,
28225 enum draw_glyphs_face draw)
28227 #ifdef HAVE_WINDOW_SYSTEM
28228 if (FRAME_WINDOW_P (XFRAME (w->frame)))
28230 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
28231 return;
28233 #endif
28234 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
28235 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
28236 #endif
28239 /* Display the active region described by mouse_face_* according to DRAW. */
28241 static void
28242 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
28244 struct window *w = XWINDOW (hlinfo->mouse_face_window);
28245 struct frame *f = XFRAME (WINDOW_FRAME (w));
28247 if (/* If window is in the process of being destroyed, don't bother
28248 to do anything. */
28249 w->current_matrix != NULL
28250 /* Don't update mouse highlight if hidden. */
28251 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
28252 /* Recognize when we are called to operate on rows that don't exist
28253 anymore. This can happen when a window is split. */
28254 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
28256 int phys_cursor_on_p = w->phys_cursor_on_p;
28257 struct glyph_row *row, *first, *last;
28259 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
28260 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
28262 for (row = first; row <= last && row->enabled_p; ++row)
28264 int start_hpos, end_hpos, start_x;
28266 /* For all but the first row, the highlight starts at column 0. */
28267 if (row == first)
28269 /* R2L rows have BEG and END in reversed order, but the
28270 screen drawing geometry is always left to right. So
28271 we need to mirror the beginning and end of the
28272 highlighted area in R2L rows. */
28273 if (!row->reversed_p)
28275 start_hpos = hlinfo->mouse_face_beg_col;
28276 start_x = hlinfo->mouse_face_beg_x;
28278 else if (row == last)
28280 start_hpos = hlinfo->mouse_face_end_col;
28281 start_x = hlinfo->mouse_face_end_x;
28283 else
28285 start_hpos = 0;
28286 start_x = 0;
28289 else if (row->reversed_p && row == last)
28291 start_hpos = hlinfo->mouse_face_end_col;
28292 start_x = hlinfo->mouse_face_end_x;
28294 else
28296 start_hpos = 0;
28297 start_x = 0;
28300 if (row == last)
28302 if (!row->reversed_p)
28303 end_hpos = hlinfo->mouse_face_end_col;
28304 else if (row == first)
28305 end_hpos = hlinfo->mouse_face_beg_col;
28306 else
28308 end_hpos = row->used[TEXT_AREA];
28309 if (draw == DRAW_NORMAL_TEXT)
28310 row->fill_line_p = 1; /* Clear to end of line */
28313 else if (row->reversed_p && row == first)
28314 end_hpos = hlinfo->mouse_face_beg_col;
28315 else
28317 end_hpos = row->used[TEXT_AREA];
28318 if (draw == DRAW_NORMAL_TEXT)
28319 row->fill_line_p = 1; /* Clear to end of line */
28322 if (end_hpos > start_hpos)
28324 draw_row_with_mouse_face (w, start_x, row,
28325 start_hpos, end_hpos, draw);
28327 row->mouse_face_p
28328 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
28332 #ifdef HAVE_WINDOW_SYSTEM
28333 /* When we've written over the cursor, arrange for it to
28334 be displayed again. */
28335 if (FRAME_WINDOW_P (f)
28336 && phys_cursor_on_p && !w->phys_cursor_on_p)
28338 int hpos = w->phys_cursor.hpos;
28340 /* When the window is hscrolled, cursor hpos can legitimately be
28341 out of bounds, but we draw the cursor at the corresponding
28342 window margin in that case. */
28343 if (!row->reversed_p && hpos < 0)
28344 hpos = 0;
28345 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28346 hpos = row->used[TEXT_AREA] - 1;
28348 block_input ();
28349 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
28350 w->phys_cursor.x, w->phys_cursor.y);
28351 unblock_input ();
28353 #endif /* HAVE_WINDOW_SYSTEM */
28356 #ifdef HAVE_WINDOW_SYSTEM
28357 /* Change the mouse cursor. */
28358 if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
28360 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
28361 if (draw == DRAW_NORMAL_TEXT
28362 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
28363 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
28364 else
28365 #endif
28366 if (draw == DRAW_MOUSE_FACE)
28367 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
28368 else
28369 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
28371 #endif /* HAVE_WINDOW_SYSTEM */
28374 /* EXPORT:
28375 Clear out the mouse-highlighted active region.
28376 Redraw it un-highlighted first. Value is non-zero if mouse
28377 face was actually drawn unhighlighted. */
28380 clear_mouse_face (Mouse_HLInfo *hlinfo)
28382 int cleared = 0;
28384 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
28386 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
28387 cleared = 1;
28390 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28391 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28392 hlinfo->mouse_face_window = Qnil;
28393 hlinfo->mouse_face_overlay = Qnil;
28394 return cleared;
28397 /* Return true if the coordinates HPOS and VPOS on windows W are
28398 within the mouse face on that window. */
28399 static bool
28400 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
28402 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28404 /* Quickly resolve the easy cases. */
28405 if (!(WINDOWP (hlinfo->mouse_face_window)
28406 && XWINDOW (hlinfo->mouse_face_window) == w))
28407 return false;
28408 if (vpos < hlinfo->mouse_face_beg_row
28409 || vpos > hlinfo->mouse_face_end_row)
28410 return false;
28411 if (vpos > hlinfo->mouse_face_beg_row
28412 && vpos < hlinfo->mouse_face_end_row)
28413 return true;
28415 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
28417 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28419 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
28420 return true;
28422 else if ((vpos == hlinfo->mouse_face_beg_row
28423 && hpos >= hlinfo->mouse_face_beg_col)
28424 || (vpos == hlinfo->mouse_face_end_row
28425 && hpos < hlinfo->mouse_face_end_col))
28426 return true;
28428 else
28430 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
28432 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
28433 return true;
28435 else if ((vpos == hlinfo->mouse_face_beg_row
28436 && hpos <= hlinfo->mouse_face_beg_col)
28437 || (vpos == hlinfo->mouse_face_end_row
28438 && hpos > hlinfo->mouse_face_end_col))
28439 return true;
28441 return false;
28445 /* EXPORT:
28446 True if physical cursor of window W is within mouse face. */
28448 bool
28449 cursor_in_mouse_face_p (struct window *w)
28451 int hpos = w->phys_cursor.hpos;
28452 int vpos = w->phys_cursor.vpos;
28453 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
28455 /* When the window is hscrolled, cursor hpos can legitimately be out
28456 of bounds, but we draw the cursor at the corresponding window
28457 margin in that case. */
28458 if (!row->reversed_p && hpos < 0)
28459 hpos = 0;
28460 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
28461 hpos = row->used[TEXT_AREA] - 1;
28463 return coords_in_mouse_face_p (w, hpos, vpos);
28468 /* Find the glyph rows START_ROW and END_ROW of window W that display
28469 characters between buffer positions START_CHARPOS and END_CHARPOS
28470 (excluding END_CHARPOS). DISP_STRING is a display string that
28471 covers these buffer positions. This is similar to
28472 row_containing_pos, but is more accurate when bidi reordering makes
28473 buffer positions change non-linearly with glyph rows. */
28474 static void
28475 rows_from_pos_range (struct window *w,
28476 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
28477 Lisp_Object disp_string,
28478 struct glyph_row **start, struct glyph_row **end)
28480 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28481 int last_y = window_text_bottom_y (w);
28482 struct glyph_row *row;
28484 *start = NULL;
28485 *end = NULL;
28487 while (!first->enabled_p
28488 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
28489 first++;
28491 /* Find the START row. */
28492 for (row = first;
28493 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
28494 row++)
28496 /* A row can potentially be the START row if the range of the
28497 characters it displays intersects the range
28498 [START_CHARPOS..END_CHARPOS). */
28499 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
28500 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
28501 /* See the commentary in row_containing_pos, for the
28502 explanation of the complicated way to check whether
28503 some position is beyond the end of the characters
28504 displayed by a row. */
28505 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
28506 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
28507 && !row->ends_at_zv_p
28508 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
28509 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
28510 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
28511 && !row->ends_at_zv_p
28512 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
28514 /* Found a candidate row. Now make sure at least one of the
28515 glyphs it displays has a charpos from the range
28516 [START_CHARPOS..END_CHARPOS).
28518 This is not obvious because bidi reordering could make
28519 buffer positions of a row be 1,2,3,102,101,100, and if we
28520 want to highlight characters in [50..60), we don't want
28521 this row, even though [50..60) does intersect [1..103),
28522 the range of character positions given by the row's start
28523 and end positions. */
28524 struct glyph *g = row->glyphs[TEXT_AREA];
28525 struct glyph *e = g + row->used[TEXT_AREA];
28527 while (g < e)
28529 if (((BUFFERP (g->object) || INTEGERP (g->object))
28530 && start_charpos <= g->charpos && g->charpos < end_charpos)
28531 /* A glyph that comes from DISP_STRING is by
28532 definition to be highlighted. */
28533 || EQ (g->object, disp_string))
28534 *start = row;
28535 g++;
28537 if (*start)
28538 break;
28542 /* Find the END row. */
28543 if (!*start
28544 /* If the last row is partially visible, start looking for END
28545 from that row, instead of starting from FIRST. */
28546 && !(row->enabled_p
28547 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
28548 row = first;
28549 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
28551 struct glyph_row *next = row + 1;
28552 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
28554 if (!next->enabled_p
28555 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
28556 /* The first row >= START whose range of displayed characters
28557 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
28558 is the row END + 1. */
28559 || (start_charpos < next_start
28560 && end_charpos < next_start)
28561 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
28562 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
28563 && !next->ends_at_zv_p
28564 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
28565 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
28566 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
28567 && !next->ends_at_zv_p
28568 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
28570 *end = row;
28571 break;
28573 else
28575 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
28576 but none of the characters it displays are in the range, it is
28577 also END + 1. */
28578 struct glyph *g = next->glyphs[TEXT_AREA];
28579 struct glyph *s = g;
28580 struct glyph *e = g + next->used[TEXT_AREA];
28582 while (g < e)
28584 if (((BUFFERP (g->object) || INTEGERP (g->object))
28585 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
28586 /* If the buffer position of the first glyph in
28587 the row is equal to END_CHARPOS, it means
28588 the last character to be highlighted is the
28589 newline of ROW, and we must consider NEXT as
28590 END, not END+1. */
28591 || (((!next->reversed_p && g == s)
28592 || (next->reversed_p && g == e - 1))
28593 && (g->charpos == end_charpos
28594 /* Special case for when NEXT is an
28595 empty line at ZV. */
28596 || (g->charpos == -1
28597 && !row->ends_at_zv_p
28598 && next_start == end_charpos)))))
28599 /* A glyph that comes from DISP_STRING is by
28600 definition to be highlighted. */
28601 || EQ (g->object, disp_string))
28602 break;
28603 g++;
28605 if (g == e)
28607 *end = row;
28608 break;
28610 /* The first row that ends at ZV must be the last to be
28611 highlighted. */
28612 else if (next->ends_at_zv_p)
28614 *end = next;
28615 break;
28621 /* This function sets the mouse_face_* elements of HLINFO, assuming
28622 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
28623 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
28624 for the overlay or run of text properties specifying the mouse
28625 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
28626 before-string and after-string that must also be highlighted.
28627 DISP_STRING, if non-nil, is a display string that may cover some
28628 or all of the highlighted text. */
28630 static void
28631 mouse_face_from_buffer_pos (Lisp_Object window,
28632 Mouse_HLInfo *hlinfo,
28633 ptrdiff_t mouse_charpos,
28634 ptrdiff_t start_charpos,
28635 ptrdiff_t end_charpos,
28636 Lisp_Object before_string,
28637 Lisp_Object after_string,
28638 Lisp_Object disp_string)
28640 struct window *w = XWINDOW (window);
28641 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28642 struct glyph_row *r1, *r2;
28643 struct glyph *glyph, *end;
28644 ptrdiff_t ignore, pos;
28645 int x;
28647 eassert (NILP (disp_string) || STRINGP (disp_string));
28648 eassert (NILP (before_string) || STRINGP (before_string));
28649 eassert (NILP (after_string) || STRINGP (after_string));
28651 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
28652 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
28653 if (r1 == NULL)
28654 r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28655 /* If the before-string or display-string contains newlines,
28656 rows_from_pos_range skips to its last row. Move back. */
28657 if (!NILP (before_string) || !NILP (disp_string))
28659 struct glyph_row *prev;
28660 while ((prev = r1 - 1, prev >= first)
28661 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
28662 && prev->used[TEXT_AREA] > 0)
28664 struct glyph *beg = prev->glyphs[TEXT_AREA];
28665 glyph = beg + prev->used[TEXT_AREA];
28666 while (--glyph >= beg && INTEGERP (glyph->object));
28667 if (glyph < beg
28668 || !(EQ (glyph->object, before_string)
28669 || EQ (glyph->object, disp_string)))
28670 break;
28671 r1 = prev;
28674 if (r2 == NULL)
28676 r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28677 hlinfo->mouse_face_past_end = 1;
28679 else if (!NILP (after_string))
28681 /* If the after-string has newlines, advance to its last row. */
28682 struct glyph_row *next;
28683 struct glyph_row *last
28684 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
28686 for (next = r2 + 1;
28687 next <= last
28688 && next->used[TEXT_AREA] > 0
28689 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
28690 ++next)
28691 r2 = next;
28693 /* The rest of the display engine assumes that mouse_face_beg_row is
28694 either above mouse_face_end_row or identical to it. But with
28695 bidi-reordered continued lines, the row for START_CHARPOS could
28696 be below the row for END_CHARPOS. If so, swap the rows and store
28697 them in correct order. */
28698 if (r1->y > r2->y)
28700 struct glyph_row *tem = r2;
28702 r2 = r1;
28703 r1 = tem;
28706 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
28707 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
28709 /* For a bidi-reordered row, the positions of BEFORE_STRING,
28710 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
28711 could be anywhere in the row and in any order. The strategy
28712 below is to find the leftmost and the rightmost glyph that
28713 belongs to either of these 3 strings, or whose position is
28714 between START_CHARPOS and END_CHARPOS, and highlight all the
28715 glyphs between those two. This may cover more than just the text
28716 between START_CHARPOS and END_CHARPOS if the range of characters
28717 strides the bidi level boundary, e.g. if the beginning is in R2L
28718 text while the end is in L2R text or vice versa. */
28719 if (!r1->reversed_p)
28721 /* This row is in a left to right paragraph. Scan it left to
28722 right. */
28723 glyph = r1->glyphs[TEXT_AREA];
28724 end = glyph + r1->used[TEXT_AREA];
28725 x = r1->x;
28727 /* Skip truncation glyphs at the start of the glyph row. */
28728 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28729 for (; glyph < end
28730 && INTEGERP (glyph->object)
28731 && glyph->charpos < 0;
28732 ++glyph)
28733 x += glyph->pixel_width;
28735 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28736 or DISP_STRING, and the first glyph from buffer whose
28737 position is between START_CHARPOS and END_CHARPOS. */
28738 for (; glyph < end
28739 && !INTEGERP (glyph->object)
28740 && !EQ (glyph->object, disp_string)
28741 && !(BUFFERP (glyph->object)
28742 && (glyph->charpos >= start_charpos
28743 && glyph->charpos < end_charpos));
28744 ++glyph)
28746 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28747 are present at buffer positions between START_CHARPOS and
28748 END_CHARPOS, or if they come from an overlay. */
28749 if (EQ (glyph->object, before_string))
28751 pos = string_buffer_position (before_string,
28752 start_charpos);
28753 /* If pos == 0, it means before_string came from an
28754 overlay, not from a buffer position. */
28755 if (!pos || (pos >= start_charpos && pos < end_charpos))
28756 break;
28758 else if (EQ (glyph->object, after_string))
28760 pos = string_buffer_position (after_string, end_charpos);
28761 if (!pos || (pos >= start_charpos && pos < end_charpos))
28762 break;
28764 x += glyph->pixel_width;
28766 hlinfo->mouse_face_beg_x = x;
28767 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28769 else
28771 /* This row is in a right to left paragraph. Scan it right to
28772 left. */
28773 struct glyph *g;
28775 end = r1->glyphs[TEXT_AREA] - 1;
28776 glyph = end + r1->used[TEXT_AREA];
28778 /* Skip truncation glyphs at the start of the glyph row. */
28779 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
28780 for (; glyph > end
28781 && INTEGERP (glyph->object)
28782 && glyph->charpos < 0;
28783 --glyph)
28786 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
28787 or DISP_STRING, and the first glyph from buffer whose
28788 position is between START_CHARPOS and END_CHARPOS. */
28789 for (; glyph > end
28790 && !INTEGERP (glyph->object)
28791 && !EQ (glyph->object, disp_string)
28792 && !(BUFFERP (glyph->object)
28793 && (glyph->charpos >= start_charpos
28794 && glyph->charpos < end_charpos));
28795 --glyph)
28797 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28798 are present at buffer positions between START_CHARPOS and
28799 END_CHARPOS, or if they come from an overlay. */
28800 if (EQ (glyph->object, before_string))
28802 pos = string_buffer_position (before_string, start_charpos);
28803 /* If pos == 0, it means before_string came from an
28804 overlay, not from a buffer position. */
28805 if (!pos || (pos >= start_charpos && pos < end_charpos))
28806 break;
28808 else if (EQ (glyph->object, after_string))
28810 pos = string_buffer_position (after_string, end_charpos);
28811 if (!pos || (pos >= start_charpos && pos < end_charpos))
28812 break;
28816 glyph++; /* first glyph to the right of the highlighted area */
28817 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
28818 x += g->pixel_width;
28819 hlinfo->mouse_face_beg_x = x;
28820 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
28823 /* If the highlight ends in a different row, compute GLYPH and END
28824 for the end row. Otherwise, reuse the values computed above for
28825 the row where the highlight begins. */
28826 if (r2 != r1)
28828 if (!r2->reversed_p)
28830 glyph = r2->glyphs[TEXT_AREA];
28831 end = glyph + r2->used[TEXT_AREA];
28832 x = r2->x;
28834 else
28836 end = r2->glyphs[TEXT_AREA] - 1;
28837 glyph = end + r2->used[TEXT_AREA];
28841 if (!r2->reversed_p)
28843 /* Skip truncation and continuation glyphs near the end of the
28844 row, and also blanks and stretch glyphs inserted by
28845 extend_face_to_end_of_line. */
28846 while (end > glyph
28847 && INTEGERP ((end - 1)->object))
28848 --end;
28849 /* Scan the rest of the glyph row from the end, looking for the
28850 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28851 DISP_STRING, or whose position is between START_CHARPOS
28852 and END_CHARPOS */
28853 for (--end;
28854 end > glyph
28855 && !INTEGERP (end->object)
28856 && !EQ (end->object, disp_string)
28857 && !(BUFFERP (end->object)
28858 && (end->charpos >= start_charpos
28859 && end->charpos < end_charpos));
28860 --end)
28862 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28863 are present at buffer positions between START_CHARPOS and
28864 END_CHARPOS, or if they come from an overlay. */
28865 if (EQ (end->object, before_string))
28867 pos = string_buffer_position (before_string, start_charpos);
28868 if (!pos || (pos >= start_charpos && pos < end_charpos))
28869 break;
28871 else if (EQ (end->object, after_string))
28873 pos = string_buffer_position (after_string, end_charpos);
28874 if (!pos || (pos >= start_charpos && pos < end_charpos))
28875 break;
28878 /* Find the X coordinate of the last glyph to be highlighted. */
28879 for (; glyph <= end; ++glyph)
28880 x += glyph->pixel_width;
28882 hlinfo->mouse_face_end_x = x;
28883 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
28885 else
28887 /* Skip truncation and continuation glyphs near the end of the
28888 row, and also blanks and stretch glyphs inserted by
28889 extend_face_to_end_of_line. */
28890 x = r2->x;
28891 end++;
28892 while (end < glyph
28893 && INTEGERP (end->object))
28895 x += end->pixel_width;
28896 ++end;
28898 /* Scan the rest of the glyph row from the end, looking for the
28899 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
28900 DISP_STRING, or whose position is between START_CHARPOS
28901 and END_CHARPOS */
28902 for ( ;
28903 end < glyph
28904 && !INTEGERP (end->object)
28905 && !EQ (end->object, disp_string)
28906 && !(BUFFERP (end->object)
28907 && (end->charpos >= start_charpos
28908 && end->charpos < end_charpos));
28909 ++end)
28911 /* BEFORE_STRING or AFTER_STRING are only relevant if they
28912 are present at buffer positions between START_CHARPOS and
28913 END_CHARPOS, or if they come from an overlay. */
28914 if (EQ (end->object, before_string))
28916 pos = string_buffer_position (before_string, start_charpos);
28917 if (!pos || (pos >= start_charpos && pos < end_charpos))
28918 break;
28920 else if (EQ (end->object, after_string))
28922 pos = string_buffer_position (after_string, end_charpos);
28923 if (!pos || (pos >= start_charpos && pos < end_charpos))
28924 break;
28926 x += end->pixel_width;
28928 /* If we exited the above loop because we arrived at the last
28929 glyph of the row, and its buffer position is still not in
28930 range, it means the last character in range is the preceding
28931 newline. Bump the end column and x values to get past the
28932 last glyph. */
28933 if (end == glyph
28934 && BUFFERP (end->object)
28935 && (end->charpos < start_charpos
28936 || end->charpos >= end_charpos))
28938 x += end->pixel_width;
28939 ++end;
28941 hlinfo->mouse_face_end_x = x;
28942 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
28945 hlinfo->mouse_face_window = window;
28946 hlinfo->mouse_face_face_id
28947 = face_at_buffer_position (w, mouse_charpos, &ignore,
28948 mouse_charpos + 1,
28949 !hlinfo->mouse_face_hidden, -1);
28950 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28953 /* The following function is not used anymore (replaced with
28954 mouse_face_from_string_pos), but I leave it here for the time
28955 being, in case someone would. */
28957 #if 0 /* not used */
28959 /* Find the position of the glyph for position POS in OBJECT in
28960 window W's current matrix, and return in *X, *Y the pixel
28961 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
28963 RIGHT_P non-zero means return the position of the right edge of the
28964 glyph, RIGHT_P zero means return the left edge position.
28966 If no glyph for POS exists in the matrix, return the position of
28967 the glyph with the next smaller position that is in the matrix, if
28968 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
28969 exists in the matrix, return the position of the glyph with the
28970 next larger position in OBJECT.
28972 Value is non-zero if a glyph was found. */
28974 static int
28975 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
28976 int *hpos, int *vpos, int *x, int *y, int right_p)
28978 int yb = window_text_bottom_y (w);
28979 struct glyph_row *r;
28980 struct glyph *best_glyph = NULL;
28981 struct glyph_row *best_row = NULL;
28982 int best_x = 0;
28984 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
28985 r->enabled_p && r->y < yb;
28986 ++r)
28988 struct glyph *g = r->glyphs[TEXT_AREA];
28989 struct glyph *e = g + r->used[TEXT_AREA];
28990 int gx;
28992 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
28993 if (EQ (g->object, object))
28995 if (g->charpos == pos)
28997 best_glyph = g;
28998 best_x = gx;
28999 best_row = r;
29000 goto found;
29002 else if (best_glyph == NULL
29003 || ((eabs (g->charpos - pos)
29004 < eabs (best_glyph->charpos - pos))
29005 && (right_p
29006 ? g->charpos < pos
29007 : g->charpos > pos)))
29009 best_glyph = g;
29010 best_x = gx;
29011 best_row = r;
29016 found:
29018 if (best_glyph)
29020 *x = best_x;
29021 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
29023 if (right_p)
29025 *x += best_glyph->pixel_width;
29026 ++*hpos;
29029 *y = best_row->y;
29030 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
29033 return best_glyph != NULL;
29035 #endif /* not used */
29037 /* Find the positions of the first and the last glyphs in window W's
29038 current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
29039 (assumed to be a string), and return in HLINFO's mouse_face_*
29040 members the pixel and column/row coordinates of those glyphs. */
29042 static void
29043 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
29044 Lisp_Object object,
29045 ptrdiff_t startpos, ptrdiff_t endpos)
29047 int yb = window_text_bottom_y (w);
29048 struct glyph_row *r;
29049 struct glyph *g, *e;
29050 int gx;
29051 int found = 0;
29053 /* Find the glyph row with at least one position in the range
29054 [STARTPOS..ENDPOS), and the first glyph in that row whose
29055 position belongs to that range. */
29056 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
29057 r->enabled_p && r->y < yb;
29058 ++r)
29060 if (!r->reversed_p)
29062 g = r->glyphs[TEXT_AREA];
29063 e = g + r->used[TEXT_AREA];
29064 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
29065 if (EQ (g->object, object)
29066 && startpos <= g->charpos && g->charpos < endpos)
29068 hlinfo->mouse_face_beg_row
29069 = MATRIX_ROW_VPOS (r, w->current_matrix);
29070 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29071 hlinfo->mouse_face_beg_x = gx;
29072 found = 1;
29073 break;
29076 else
29078 struct glyph *g1;
29080 e = r->glyphs[TEXT_AREA];
29081 g = e + r->used[TEXT_AREA];
29082 for ( ; g > e; --g)
29083 if (EQ ((g-1)->object, object)
29084 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
29086 hlinfo->mouse_face_beg_row
29087 = MATRIX_ROW_VPOS (r, w->current_matrix);
29088 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
29089 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
29090 gx += g1->pixel_width;
29091 hlinfo->mouse_face_beg_x = gx;
29092 found = 1;
29093 break;
29096 if (found)
29097 break;
29100 if (!found)
29101 return;
29103 /* Starting with the next row, look for the first row which does NOT
29104 include any glyphs whose positions are in the range. */
29105 for (++r; r->enabled_p && r->y < yb; ++r)
29107 g = r->glyphs[TEXT_AREA];
29108 e = g + r->used[TEXT_AREA];
29109 found = 0;
29110 for ( ; g < e; ++g)
29111 if (EQ (g->object, object)
29112 && startpos <= g->charpos && g->charpos < endpos)
29114 found = 1;
29115 break;
29117 if (!found)
29118 break;
29121 /* The highlighted region ends on the previous row. */
29122 r--;
29124 /* Set the end row. */
29125 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
29127 /* Compute and set the end column and the end column's horizontal
29128 pixel coordinate. */
29129 if (!r->reversed_p)
29131 g = r->glyphs[TEXT_AREA];
29132 e = g + r->used[TEXT_AREA];
29133 for ( ; e > g; --e)
29134 if (EQ ((e-1)->object, object)
29135 && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
29136 break;
29137 hlinfo->mouse_face_end_col = e - g;
29139 for (gx = r->x; g < e; ++g)
29140 gx += g->pixel_width;
29141 hlinfo->mouse_face_end_x = gx;
29143 else
29145 e = r->glyphs[TEXT_AREA];
29146 g = e + r->used[TEXT_AREA];
29147 for (gx = r->x ; e < g; ++e)
29149 if (EQ (e->object, object)
29150 && startpos <= e->charpos && e->charpos < endpos)
29151 break;
29152 gx += e->pixel_width;
29154 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
29155 hlinfo->mouse_face_end_x = gx;
29159 #ifdef HAVE_WINDOW_SYSTEM
29161 /* See if position X, Y is within a hot-spot of an image. */
29163 static int
29164 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
29166 if (!CONSP (hot_spot))
29167 return 0;
29169 if (EQ (XCAR (hot_spot), Qrect))
29171 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
29172 Lisp_Object rect = XCDR (hot_spot);
29173 Lisp_Object tem;
29174 if (!CONSP (rect))
29175 return 0;
29176 if (!CONSP (XCAR (rect)))
29177 return 0;
29178 if (!CONSP (XCDR (rect)))
29179 return 0;
29180 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
29181 return 0;
29182 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
29183 return 0;
29184 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
29185 return 0;
29186 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
29187 return 0;
29188 return 1;
29190 else if (EQ (XCAR (hot_spot), Qcircle))
29192 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
29193 Lisp_Object circ = XCDR (hot_spot);
29194 Lisp_Object lr, lx0, ly0;
29195 if (CONSP (circ)
29196 && CONSP (XCAR (circ))
29197 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
29198 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
29199 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
29201 double r = XFLOATINT (lr);
29202 double dx = XINT (lx0) - x;
29203 double dy = XINT (ly0) - y;
29204 return (dx * dx + dy * dy <= r * r);
29207 else if (EQ (XCAR (hot_spot), Qpoly))
29209 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
29210 if (VECTORP (XCDR (hot_spot)))
29212 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
29213 Lisp_Object *poly = v->contents;
29214 ptrdiff_t n = v->header.size;
29215 ptrdiff_t i;
29216 int inside = 0;
29217 Lisp_Object lx, ly;
29218 int x0, y0;
29220 /* Need an even number of coordinates, and at least 3 edges. */
29221 if (n < 6 || n & 1)
29222 return 0;
29224 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
29225 If count is odd, we are inside polygon. Pixels on edges
29226 may or may not be included depending on actual geometry of the
29227 polygon. */
29228 if ((lx = poly[n-2], !INTEGERP (lx))
29229 || (ly = poly[n-1], !INTEGERP (lx)))
29230 return 0;
29231 x0 = XINT (lx), y0 = XINT (ly);
29232 for (i = 0; i < n; i += 2)
29234 int x1 = x0, y1 = y0;
29235 if ((lx = poly[i], !INTEGERP (lx))
29236 || (ly = poly[i+1], !INTEGERP (ly)))
29237 return 0;
29238 x0 = XINT (lx), y0 = XINT (ly);
29240 /* Does this segment cross the X line? */
29241 if (x0 >= x)
29243 if (x1 >= x)
29244 continue;
29246 else if (x1 < x)
29247 continue;
29248 if (y > y0 && y > y1)
29249 continue;
29250 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
29251 inside = !inside;
29253 return inside;
29256 return 0;
29259 Lisp_Object
29260 find_hot_spot (Lisp_Object map, int x, int y)
29262 while (CONSP (map))
29264 if (CONSP (XCAR (map))
29265 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
29266 return XCAR (map);
29267 map = XCDR (map);
29270 return Qnil;
29273 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
29274 3, 3, 0,
29275 doc: /* Lookup in image map MAP coordinates X and Y.
29276 An image map is an alist where each element has the format (AREA ID PLIST).
29277 An AREA is specified as either a rectangle, a circle, or a polygon:
29278 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
29279 pixel coordinates of the upper left and bottom right corners.
29280 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
29281 and the radius of the circle; r may be a float or integer.
29282 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
29283 vector describes one corner in the polygon.
29284 Returns the alist element for the first matching AREA in MAP. */)
29285 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
29287 if (NILP (map))
29288 return Qnil;
29290 CHECK_NUMBER (x);
29291 CHECK_NUMBER (y);
29293 return find_hot_spot (map,
29294 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
29295 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
29299 /* Display frame CURSOR, optionally using shape defined by POINTER. */
29300 static void
29301 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
29303 /* Do not change cursor shape while dragging mouse. */
29304 if (!NILP (do_mouse_tracking))
29305 return;
29307 if (!NILP (pointer))
29309 if (EQ (pointer, Qarrow))
29310 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29311 else if (EQ (pointer, Qhand))
29312 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
29313 else if (EQ (pointer, Qtext))
29314 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29315 else if (EQ (pointer, intern ("hdrag")))
29316 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29317 else if (EQ (pointer, intern ("nhdrag")))
29318 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29319 #ifdef HAVE_X_WINDOWS
29320 else if (EQ (pointer, intern ("vdrag")))
29321 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29322 #endif
29323 else if (EQ (pointer, intern ("hourglass")))
29324 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
29325 else if (EQ (pointer, Qmodeline))
29326 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
29327 else
29328 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29331 if (cursor != No_Cursor)
29332 FRAME_RIF (f)->define_frame_cursor (f, cursor);
29335 #endif /* HAVE_WINDOW_SYSTEM */
29337 /* Take proper action when mouse has moved to the mode or header line
29338 or marginal area AREA of window W, x-position X and y-position Y.
29339 X is relative to the start of the text display area of W, so the
29340 width of bitmap areas and scroll bars must be subtracted to get a
29341 position relative to the start of the mode line. */
29343 static void
29344 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
29345 enum window_part area)
29347 struct window *w = XWINDOW (window);
29348 struct frame *f = XFRAME (w->frame);
29349 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29350 #ifdef HAVE_WINDOW_SYSTEM
29351 Display_Info *dpyinfo;
29352 #endif
29353 Cursor cursor = No_Cursor;
29354 Lisp_Object pointer = Qnil;
29355 int dx, dy, width, height;
29356 ptrdiff_t charpos;
29357 Lisp_Object string, object = Qnil;
29358 Lisp_Object pos IF_LINT (= Qnil), help;
29360 Lisp_Object mouse_face;
29361 int original_x_pixel = x;
29362 struct glyph * glyph = NULL, * row_start_glyph = NULL;
29363 struct glyph_row *row IF_LINT (= 0);
29365 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
29367 int x0;
29368 struct glyph *end;
29370 /* Kludge alert: mode_line_string takes X/Y in pixels, but
29371 returns them in row/column units! */
29372 string = mode_line_string (w, area, &x, &y, &charpos,
29373 &object, &dx, &dy, &width, &height);
29375 row = (area == ON_MODE_LINE
29376 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
29377 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
29379 /* Find the glyph under the mouse pointer. */
29380 if (row->mode_line_p && row->enabled_p)
29382 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
29383 end = glyph + row->used[TEXT_AREA];
29385 for (x0 = original_x_pixel;
29386 glyph < end && x0 >= glyph->pixel_width;
29387 ++glyph)
29388 x0 -= glyph->pixel_width;
29390 if (glyph >= end)
29391 glyph = NULL;
29394 else
29396 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
29397 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
29398 returns them in row/column units! */
29399 string = marginal_area_string (w, area, &x, &y, &charpos,
29400 &object, &dx, &dy, &width, &height);
29403 help = Qnil;
29405 #ifdef HAVE_WINDOW_SYSTEM
29406 if (IMAGEP (object))
29408 Lisp_Object image_map, hotspot;
29409 if ((image_map = Fplist_get (XCDR (object), QCmap),
29410 !NILP (image_map))
29411 && (hotspot = find_hot_spot (image_map, dx, dy),
29412 CONSP (hotspot))
29413 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29415 Lisp_Object plist;
29417 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
29418 If so, we could look for mouse-enter, mouse-leave
29419 properties in PLIST (and do something...). */
29420 hotspot = XCDR (hotspot);
29421 if (CONSP (hotspot)
29422 && (plist = XCAR (hotspot), CONSP (plist)))
29424 pointer = Fplist_get (plist, Qpointer);
29425 if (NILP (pointer))
29426 pointer = Qhand;
29427 help = Fplist_get (plist, Qhelp_echo);
29428 if (!NILP (help))
29430 help_echo_string = help;
29431 XSETWINDOW (help_echo_window, w);
29432 help_echo_object = w->contents;
29433 help_echo_pos = charpos;
29437 if (NILP (pointer))
29438 pointer = Fplist_get (XCDR (object), QCpointer);
29440 #endif /* HAVE_WINDOW_SYSTEM */
29442 if (STRINGP (string))
29443 pos = make_number (charpos);
29445 /* Set the help text and mouse pointer. If the mouse is on a part
29446 of the mode line without any text (e.g. past the right edge of
29447 the mode line text), use the default help text and pointer. */
29448 if (STRINGP (string) || area == ON_MODE_LINE)
29450 /* Arrange to display the help by setting the global variables
29451 help_echo_string, help_echo_object, and help_echo_pos. */
29452 if (NILP (help))
29454 if (STRINGP (string))
29455 help = Fget_text_property (pos, Qhelp_echo, string);
29457 if (!NILP (help))
29459 help_echo_string = help;
29460 XSETWINDOW (help_echo_window, w);
29461 help_echo_object = string;
29462 help_echo_pos = charpos;
29464 else if (area == ON_MODE_LINE)
29466 Lisp_Object default_help
29467 = buffer_local_value (Qmode_line_default_help_echo,
29468 w->contents);
29470 if (STRINGP (default_help))
29472 help_echo_string = default_help;
29473 XSETWINDOW (help_echo_window, w);
29474 help_echo_object = Qnil;
29475 help_echo_pos = -1;
29480 #ifdef HAVE_WINDOW_SYSTEM
29481 /* Change the mouse pointer according to what is under it. */
29482 if (FRAME_WINDOW_P (f))
29484 bool draggable = (! WINDOW_BOTTOMMOST_P (w)
29485 || minibuf_level
29486 || NILP (Vresize_mini_windows));
29488 dpyinfo = FRAME_DISPLAY_INFO (f);
29489 if (STRINGP (string))
29491 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29493 if (NILP (pointer))
29494 pointer = Fget_text_property (pos, Qpointer, string);
29496 /* Change the mouse pointer according to what is under X/Y. */
29497 if (NILP (pointer)
29498 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
29500 Lisp_Object map;
29501 map = Fget_text_property (pos, Qlocal_map, string);
29502 if (!KEYMAPP (map))
29503 map = Fget_text_property (pos, Qkeymap, string);
29504 if (!KEYMAPP (map) && draggable)
29505 cursor = dpyinfo->vertical_scroll_bar_cursor;
29508 else if (draggable)
29509 /* Default mode-line pointer. */
29510 cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
29512 #endif
29515 /* Change the mouse face according to what is under X/Y. */
29516 if (STRINGP (string))
29518 mouse_face = Fget_text_property (pos, Qmouse_face, string);
29519 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
29520 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29521 && glyph)
29523 Lisp_Object b, e;
29525 struct glyph * tmp_glyph;
29527 int gpos;
29528 int gseq_length;
29529 int total_pixel_width;
29530 ptrdiff_t begpos, endpos, ignore;
29532 int vpos, hpos;
29534 b = Fprevious_single_property_change (make_number (charpos + 1),
29535 Qmouse_face, string, Qnil);
29536 if (NILP (b))
29537 begpos = 0;
29538 else
29539 begpos = XINT (b);
29541 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
29542 if (NILP (e))
29543 endpos = SCHARS (string);
29544 else
29545 endpos = XINT (e);
29547 /* Calculate the glyph position GPOS of GLYPH in the
29548 displayed string, relative to the beginning of the
29549 highlighted part of the string.
29551 Note: GPOS is different from CHARPOS. CHARPOS is the
29552 position of GLYPH in the internal string object. A mode
29553 line string format has structures which are converted to
29554 a flattened string by the Emacs Lisp interpreter. The
29555 internal string is an element of those structures. The
29556 displayed string is the flattened string. */
29557 tmp_glyph = row_start_glyph;
29558 while (tmp_glyph < glyph
29559 && (!(EQ (tmp_glyph->object, glyph->object)
29560 && begpos <= tmp_glyph->charpos
29561 && tmp_glyph->charpos < endpos)))
29562 tmp_glyph++;
29563 gpos = glyph - tmp_glyph;
29565 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
29566 the highlighted part of the displayed string to which
29567 GLYPH belongs. Note: GSEQ_LENGTH is different from
29568 SCHARS (STRING), because the latter returns the length of
29569 the internal string. */
29570 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
29571 tmp_glyph > glyph
29572 && (!(EQ (tmp_glyph->object, glyph->object)
29573 && begpos <= tmp_glyph->charpos
29574 && tmp_glyph->charpos < endpos));
29575 tmp_glyph--)
29577 gseq_length = gpos + (tmp_glyph - glyph) + 1;
29579 /* Calculate the total pixel width of all the glyphs between
29580 the beginning of the highlighted area and GLYPH. */
29581 total_pixel_width = 0;
29582 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
29583 total_pixel_width += tmp_glyph->pixel_width;
29585 /* Pre calculation of re-rendering position. Note: X is in
29586 column units here, after the call to mode_line_string or
29587 marginal_area_string. */
29588 hpos = x - gpos;
29589 vpos = (area == ON_MODE_LINE
29590 ? (w->current_matrix)->nrows - 1
29591 : 0);
29593 /* If GLYPH's position is included in the region that is
29594 already drawn in mouse face, we have nothing to do. */
29595 if ( EQ (window, hlinfo->mouse_face_window)
29596 && (!row->reversed_p
29597 ? (hlinfo->mouse_face_beg_col <= hpos
29598 && hpos < hlinfo->mouse_face_end_col)
29599 /* In R2L rows we swap BEG and END, see below. */
29600 : (hlinfo->mouse_face_end_col <= hpos
29601 && hpos < hlinfo->mouse_face_beg_col))
29602 && hlinfo->mouse_face_beg_row == vpos )
29603 return;
29605 if (clear_mouse_face (hlinfo))
29606 cursor = No_Cursor;
29608 if (!row->reversed_p)
29610 hlinfo->mouse_face_beg_col = hpos;
29611 hlinfo->mouse_face_beg_x = original_x_pixel
29612 - (total_pixel_width + dx);
29613 hlinfo->mouse_face_end_col = hpos + gseq_length;
29614 hlinfo->mouse_face_end_x = 0;
29616 else
29618 /* In R2L rows, show_mouse_face expects BEG and END
29619 coordinates to be swapped. */
29620 hlinfo->mouse_face_end_col = hpos;
29621 hlinfo->mouse_face_end_x = original_x_pixel
29622 - (total_pixel_width + dx);
29623 hlinfo->mouse_face_beg_col = hpos + gseq_length;
29624 hlinfo->mouse_face_beg_x = 0;
29627 hlinfo->mouse_face_beg_row = vpos;
29628 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
29629 hlinfo->mouse_face_past_end = 0;
29630 hlinfo->mouse_face_window = window;
29632 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
29633 charpos,
29634 0, &ignore,
29635 glyph->face_id,
29637 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29639 if (NILP (pointer))
29640 pointer = Qhand;
29642 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
29643 clear_mouse_face (hlinfo);
29645 #ifdef HAVE_WINDOW_SYSTEM
29646 if (FRAME_WINDOW_P (f))
29647 define_frame_cursor1 (f, cursor, pointer);
29648 #endif
29652 /* EXPORT:
29653 Take proper action when the mouse has moved to position X, Y on
29654 frame F with regards to highlighting portions of display that have
29655 mouse-face properties. Also de-highlight portions of display where
29656 the mouse was before, set the mouse pointer shape as appropriate
29657 for the mouse coordinates, and activate help echo (tooltips).
29658 X and Y can be negative or out of range. */
29660 void
29661 note_mouse_highlight (struct frame *f, int x, int y)
29663 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29664 enum window_part part = ON_NOTHING;
29665 Lisp_Object window;
29666 struct window *w;
29667 Cursor cursor = No_Cursor;
29668 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
29669 struct buffer *b;
29671 /* When a menu is active, don't highlight because this looks odd. */
29672 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
29673 if (popup_activated ())
29674 return;
29675 #endif
29677 if (!f->glyphs_initialized_p
29678 || f->pointer_invisible)
29679 return;
29681 hlinfo->mouse_face_mouse_x = x;
29682 hlinfo->mouse_face_mouse_y = y;
29683 hlinfo->mouse_face_mouse_frame = f;
29685 if (hlinfo->mouse_face_defer)
29686 return;
29688 /* Which window is that in? */
29689 window = window_from_coordinates (f, x, y, &part, 1);
29691 /* If displaying active text in another window, clear that. */
29692 if (! EQ (window, hlinfo->mouse_face_window)
29693 /* Also clear if we move out of text area in same window. */
29694 || (!NILP (hlinfo->mouse_face_window)
29695 && !NILP (window)
29696 && part != ON_TEXT
29697 && part != ON_MODE_LINE
29698 && part != ON_HEADER_LINE))
29699 clear_mouse_face (hlinfo);
29701 /* Not on a window -> return. */
29702 if (!WINDOWP (window))
29703 return;
29705 /* Reset help_echo_string. It will get recomputed below. */
29706 help_echo_string = Qnil;
29708 /* Convert to window-relative pixel coordinates. */
29709 w = XWINDOW (window);
29710 frame_to_window_pixel_xy (w, &x, &y);
29712 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (USE_GTK) && ! defined (HAVE_NS)
29713 /* Handle tool-bar window differently since it doesn't display a
29714 buffer. */
29715 if (EQ (window, f->tool_bar_window))
29717 note_tool_bar_highlight (f, x, y);
29718 return;
29720 #endif
29722 /* Mouse is on the mode, header line or margin? */
29723 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
29724 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29726 note_mode_line_or_margin_highlight (window, x, y, part);
29728 #ifdef HAVE_WINDOW_SYSTEM
29729 if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
29731 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29732 /* Show non-text cursor (Bug#16647). */
29733 goto set_cursor;
29735 else
29736 #endif
29737 return;
29740 #ifdef HAVE_WINDOW_SYSTEM
29741 if (part == ON_VERTICAL_BORDER)
29743 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29744 help_echo_string = build_string ("drag-mouse-1: resize");
29746 else if (part == ON_RIGHT_DIVIDER)
29748 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
29749 help_echo_string = build_string ("drag-mouse-1: resize");
29751 else if (part == ON_BOTTOM_DIVIDER)
29752 if (! WINDOW_BOTTOMMOST_P (w)
29753 || minibuf_level
29754 || NILP (Vresize_mini_windows))
29756 cursor = FRAME_X_OUTPUT (f)->vertical_drag_cursor;
29757 help_echo_string = build_string ("drag-mouse-1: resize");
29759 else
29760 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29761 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
29762 || part == ON_VERTICAL_SCROLL_BAR
29763 || part == ON_HORIZONTAL_SCROLL_BAR)
29764 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29765 else
29766 cursor = FRAME_X_OUTPUT (f)->text_cursor;
29767 #endif
29769 /* Are we in a window whose display is up to date?
29770 And verify the buffer's text has not changed. */
29771 b = XBUFFER (w->contents);
29772 if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
29774 int hpos, vpos, dx, dy, area = LAST_AREA;
29775 ptrdiff_t pos;
29776 struct glyph *glyph;
29777 Lisp_Object object;
29778 Lisp_Object mouse_face = Qnil, position;
29779 Lisp_Object *overlay_vec = NULL;
29780 ptrdiff_t i, noverlays;
29781 struct buffer *obuf;
29782 ptrdiff_t obegv, ozv;
29783 int same_region;
29785 /* Find the glyph under X/Y. */
29786 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
29788 #ifdef HAVE_WINDOW_SYSTEM
29789 /* Look for :pointer property on image. */
29790 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
29792 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
29793 if (img != NULL && IMAGEP (img->spec))
29795 Lisp_Object image_map, hotspot;
29796 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
29797 !NILP (image_map))
29798 && (hotspot = find_hot_spot (image_map,
29799 glyph->slice.img.x + dx,
29800 glyph->slice.img.y + dy),
29801 CONSP (hotspot))
29802 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
29804 Lisp_Object plist;
29806 /* Could check XCAR (hotspot) to see if we enter/leave
29807 this hot-spot.
29808 If so, we could look for mouse-enter, mouse-leave
29809 properties in PLIST (and do something...). */
29810 hotspot = XCDR (hotspot);
29811 if (CONSP (hotspot)
29812 && (plist = XCAR (hotspot), CONSP (plist)))
29814 pointer = Fplist_get (plist, Qpointer);
29815 if (NILP (pointer))
29816 pointer = Qhand;
29817 help_echo_string = Fplist_get (plist, Qhelp_echo);
29818 if (!NILP (help_echo_string))
29820 help_echo_window = window;
29821 help_echo_object = glyph->object;
29822 help_echo_pos = glyph->charpos;
29826 if (NILP (pointer))
29827 pointer = Fplist_get (XCDR (img->spec), QCpointer);
29830 #endif /* HAVE_WINDOW_SYSTEM */
29832 /* Clear mouse face if X/Y not over text. */
29833 if (glyph == NULL
29834 || area != TEXT_AREA
29835 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
29836 /* Glyph's OBJECT is an integer for glyphs inserted by the
29837 display engine for its internal purposes, like truncation
29838 and continuation glyphs and blanks beyond the end of
29839 line's text on text terminals. If we are over such a
29840 glyph, we are not over any text. */
29841 || INTEGERP (glyph->object)
29842 /* R2L rows have a stretch glyph at their front, which
29843 stands for no text, whereas L2R rows have no glyphs at
29844 all beyond the end of text. Treat such stretch glyphs
29845 like we do with NULL glyphs in L2R rows. */
29846 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
29847 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
29848 && glyph->type == STRETCH_GLYPH
29849 && glyph->avoid_cursor_p))
29851 if (clear_mouse_face (hlinfo))
29852 cursor = No_Cursor;
29853 #ifdef HAVE_WINDOW_SYSTEM
29854 if (FRAME_WINDOW_P (f) && NILP (pointer))
29856 if (area != TEXT_AREA)
29857 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
29858 else
29859 pointer = Vvoid_text_area_pointer;
29861 #endif
29862 goto set_cursor;
29865 pos = glyph->charpos;
29866 object = glyph->object;
29867 if (!STRINGP (object) && !BUFFERP (object))
29868 goto set_cursor;
29870 /* If we get an out-of-range value, return now; avoid an error. */
29871 if (BUFFERP (object) && pos > BUF_Z (b))
29872 goto set_cursor;
29874 /* Make the window's buffer temporarily current for
29875 overlays_at and compute_char_face. */
29876 obuf = current_buffer;
29877 current_buffer = b;
29878 obegv = BEGV;
29879 ozv = ZV;
29880 BEGV = BEG;
29881 ZV = Z;
29883 /* Is this char mouse-active or does it have help-echo? */
29884 position = make_number (pos);
29886 USE_SAFE_ALLOCA;
29888 if (BUFFERP (object))
29890 /* Put all the overlays we want in a vector in overlay_vec. */
29891 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
29892 /* Sort overlays into increasing priority order. */
29893 noverlays = sort_overlays (overlay_vec, noverlays, w);
29895 else
29896 noverlays = 0;
29898 if (NILP (Vmouse_highlight))
29900 clear_mouse_face (hlinfo);
29901 goto check_help_echo;
29904 same_region = coords_in_mouse_face_p (w, hpos, vpos);
29906 if (same_region)
29907 cursor = No_Cursor;
29909 /* Check mouse-face highlighting. */
29910 if (! same_region
29911 /* If there exists an overlay with mouse-face overlapping
29912 the one we are currently highlighting, we have to
29913 check if we enter the overlapping overlay, and then
29914 highlight only that. */
29915 || (OVERLAYP (hlinfo->mouse_face_overlay)
29916 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
29918 /* Find the highest priority overlay with a mouse-face. */
29919 Lisp_Object overlay = Qnil;
29920 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
29922 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
29923 if (!NILP (mouse_face))
29924 overlay = overlay_vec[i];
29927 /* If we're highlighting the same overlay as before, there's
29928 no need to do that again. */
29929 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
29930 goto check_help_echo;
29931 hlinfo->mouse_face_overlay = overlay;
29933 /* Clear the display of the old active region, if any. */
29934 if (clear_mouse_face (hlinfo))
29935 cursor = No_Cursor;
29937 /* If no overlay applies, get a text property. */
29938 if (NILP (overlay))
29939 mouse_face = Fget_text_property (position, Qmouse_face, object);
29941 /* Next, compute the bounds of the mouse highlighting and
29942 display it. */
29943 if (!NILP (mouse_face) && STRINGP (object))
29945 /* The mouse-highlighting comes from a display string
29946 with a mouse-face. */
29947 Lisp_Object s, e;
29948 ptrdiff_t ignore;
29950 s = Fprevious_single_property_change
29951 (make_number (pos + 1), Qmouse_face, object, Qnil);
29952 e = Fnext_single_property_change
29953 (position, Qmouse_face, object, Qnil);
29954 if (NILP (s))
29955 s = make_number (0);
29956 if (NILP (e))
29957 e = make_number (SCHARS (object));
29958 mouse_face_from_string_pos (w, hlinfo, object,
29959 XINT (s), XINT (e));
29960 hlinfo->mouse_face_past_end = 0;
29961 hlinfo->mouse_face_window = window;
29962 hlinfo->mouse_face_face_id
29963 = face_at_string_position (w, object, pos, 0, &ignore,
29964 glyph->face_id, 1);
29965 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
29966 cursor = No_Cursor;
29968 else
29970 /* The mouse-highlighting, if any, comes from an overlay
29971 or text property in the buffer. */
29972 Lisp_Object buffer IF_LINT (= Qnil);
29973 Lisp_Object disp_string IF_LINT (= Qnil);
29975 if (STRINGP (object))
29977 /* If we are on a display string with no mouse-face,
29978 check if the text under it has one. */
29979 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
29980 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
29981 pos = string_buffer_position (object, start);
29982 if (pos > 0)
29984 mouse_face = get_char_property_and_overlay
29985 (make_number (pos), Qmouse_face, w->contents, &overlay);
29986 buffer = w->contents;
29987 disp_string = object;
29990 else
29992 buffer = object;
29993 disp_string = Qnil;
29996 if (!NILP (mouse_face))
29998 Lisp_Object before, after;
29999 Lisp_Object before_string, after_string;
30000 /* To correctly find the limits of mouse highlight
30001 in a bidi-reordered buffer, we must not use the
30002 optimization of limiting the search in
30003 previous-single-property-change and
30004 next-single-property-change, because
30005 rows_from_pos_range needs the real start and end
30006 positions to DTRT in this case. That's because
30007 the first row visible in a window does not
30008 necessarily display the character whose position
30009 is the smallest. */
30010 Lisp_Object lim1
30011 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30012 ? Fmarker_position (w->start)
30013 : Qnil;
30014 Lisp_Object lim2
30015 = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
30016 ? make_number (BUF_Z (XBUFFER (buffer))
30017 - w->window_end_pos)
30018 : Qnil;
30020 if (NILP (overlay))
30022 /* Handle the text property case. */
30023 before = Fprevious_single_property_change
30024 (make_number (pos + 1), Qmouse_face, buffer, lim1);
30025 after = Fnext_single_property_change
30026 (make_number (pos), Qmouse_face, buffer, lim2);
30027 before_string = after_string = Qnil;
30029 else
30031 /* Handle the overlay case. */
30032 before = Foverlay_start (overlay);
30033 after = Foverlay_end (overlay);
30034 before_string = Foverlay_get (overlay, Qbefore_string);
30035 after_string = Foverlay_get (overlay, Qafter_string);
30037 if (!STRINGP (before_string)) before_string = Qnil;
30038 if (!STRINGP (after_string)) after_string = Qnil;
30041 mouse_face_from_buffer_pos (window, hlinfo, pos,
30042 NILP (before)
30044 : XFASTINT (before),
30045 NILP (after)
30046 ? BUF_Z (XBUFFER (buffer))
30047 : XFASTINT (after),
30048 before_string, after_string,
30049 disp_string);
30050 cursor = No_Cursor;
30055 check_help_echo:
30057 /* Look for a `help-echo' property. */
30058 if (NILP (help_echo_string)) {
30059 Lisp_Object help, overlay;
30061 /* Check overlays first. */
30062 help = overlay = Qnil;
30063 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
30065 overlay = overlay_vec[i];
30066 help = Foverlay_get (overlay, Qhelp_echo);
30069 if (!NILP (help))
30071 help_echo_string = help;
30072 help_echo_window = window;
30073 help_echo_object = overlay;
30074 help_echo_pos = pos;
30076 else
30078 Lisp_Object obj = glyph->object;
30079 ptrdiff_t charpos = glyph->charpos;
30081 /* Try text properties. */
30082 if (STRINGP (obj)
30083 && charpos >= 0
30084 && charpos < SCHARS (obj))
30086 help = Fget_text_property (make_number (charpos),
30087 Qhelp_echo, obj);
30088 if (NILP (help))
30090 /* If the string itself doesn't specify a help-echo,
30091 see if the buffer text ``under'' it does. */
30092 struct glyph_row *r
30093 = MATRIX_ROW (w->current_matrix, vpos);
30094 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30095 ptrdiff_t p = string_buffer_position (obj, start);
30096 if (p > 0)
30098 help = Fget_char_property (make_number (p),
30099 Qhelp_echo, w->contents);
30100 if (!NILP (help))
30102 charpos = p;
30103 obj = w->contents;
30108 else if (BUFFERP (obj)
30109 && charpos >= BEGV
30110 && charpos < ZV)
30111 help = Fget_text_property (make_number (charpos), Qhelp_echo,
30112 obj);
30114 if (!NILP (help))
30116 help_echo_string = help;
30117 help_echo_window = window;
30118 help_echo_object = obj;
30119 help_echo_pos = charpos;
30124 #ifdef HAVE_WINDOW_SYSTEM
30125 /* Look for a `pointer' property. */
30126 if (FRAME_WINDOW_P (f) && NILP (pointer))
30128 /* Check overlays first. */
30129 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
30130 pointer = Foverlay_get (overlay_vec[i], Qpointer);
30132 if (NILP (pointer))
30134 Lisp_Object obj = glyph->object;
30135 ptrdiff_t charpos = glyph->charpos;
30137 /* Try text properties. */
30138 if (STRINGP (obj)
30139 && charpos >= 0
30140 && charpos < SCHARS (obj))
30142 pointer = Fget_text_property (make_number (charpos),
30143 Qpointer, obj);
30144 if (NILP (pointer))
30146 /* If the string itself doesn't specify a pointer,
30147 see if the buffer text ``under'' it does. */
30148 struct glyph_row *r
30149 = MATRIX_ROW (w->current_matrix, vpos);
30150 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
30151 ptrdiff_t p = string_buffer_position (obj, start);
30152 if (p > 0)
30153 pointer = Fget_char_property (make_number (p),
30154 Qpointer, w->contents);
30157 else if (BUFFERP (obj)
30158 && charpos >= BEGV
30159 && charpos < ZV)
30160 pointer = Fget_text_property (make_number (charpos),
30161 Qpointer, obj);
30164 #endif /* HAVE_WINDOW_SYSTEM */
30166 BEGV = obegv;
30167 ZV = ozv;
30168 current_buffer = obuf;
30169 SAFE_FREE ();
30172 set_cursor:
30174 #ifdef HAVE_WINDOW_SYSTEM
30175 if (FRAME_WINDOW_P (f))
30176 define_frame_cursor1 (f, cursor, pointer);
30177 #else
30178 /* This is here to prevent a compiler error, about "label at end of
30179 compound statement". */
30180 return;
30181 #endif
30185 /* EXPORT for RIF:
30186 Clear any mouse-face on window W. This function is part of the
30187 redisplay interface, and is called from try_window_id and similar
30188 functions to ensure the mouse-highlight is off. */
30190 void
30191 x_clear_window_mouse_face (struct window *w)
30193 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
30194 Lisp_Object window;
30196 block_input ();
30197 XSETWINDOW (window, w);
30198 if (EQ (window, hlinfo->mouse_face_window))
30199 clear_mouse_face (hlinfo);
30200 unblock_input ();
30204 /* EXPORT:
30205 Just discard the mouse face information for frame F, if any.
30206 This is used when the size of F is changed. */
30208 void
30209 cancel_mouse_face (struct frame *f)
30211 Lisp_Object window;
30212 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30214 window = hlinfo->mouse_face_window;
30215 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
30216 reset_mouse_highlight (hlinfo);
30221 /***********************************************************************
30222 Exposure Events
30223 ***********************************************************************/
30225 #ifdef HAVE_WINDOW_SYSTEM
30227 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
30228 which intersects rectangle R. R is in window-relative coordinates. */
30230 static void
30231 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
30232 enum glyph_row_area area)
30234 struct glyph *first = row->glyphs[area];
30235 struct glyph *end = row->glyphs[area] + row->used[area];
30236 struct glyph *last;
30237 int first_x, start_x, x;
30239 if (area == TEXT_AREA && row->fill_line_p)
30240 /* If row extends face to end of line write the whole line. */
30241 draw_glyphs (w, 0, row, area,
30242 0, row->used[area],
30243 DRAW_NORMAL_TEXT, 0);
30244 else
30246 /* Set START_X to the window-relative start position for drawing glyphs of
30247 AREA. The first glyph of the text area can be partially visible.
30248 The first glyphs of other areas cannot. */
30249 start_x = window_box_left_offset (w, area);
30250 x = start_x;
30251 if (area == TEXT_AREA)
30252 x += row->x;
30254 /* Find the first glyph that must be redrawn. */
30255 while (first < end
30256 && x + first->pixel_width < r->x)
30258 x += first->pixel_width;
30259 ++first;
30262 /* Find the last one. */
30263 last = first;
30264 first_x = x;
30265 while (last < end
30266 && x < r->x + r->width)
30268 x += last->pixel_width;
30269 ++last;
30272 /* Repaint. */
30273 if (last > first)
30274 draw_glyphs (w, first_x - start_x, row, area,
30275 first - row->glyphs[area], last - row->glyphs[area],
30276 DRAW_NORMAL_TEXT, 0);
30281 /* Redraw the parts of the glyph row ROW on window W intersecting
30282 rectangle R. R is in window-relative coordinates. Value is
30283 non-zero if mouse-face was overwritten. */
30285 static int
30286 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
30288 eassert (row->enabled_p);
30290 if (row->mode_line_p || w->pseudo_window_p)
30291 draw_glyphs (w, 0, row, TEXT_AREA,
30292 0, row->used[TEXT_AREA],
30293 DRAW_NORMAL_TEXT, 0);
30294 else
30296 if (row->used[LEFT_MARGIN_AREA])
30297 expose_area (w, row, r, LEFT_MARGIN_AREA);
30298 if (row->used[TEXT_AREA])
30299 expose_area (w, row, r, TEXT_AREA);
30300 if (row->used[RIGHT_MARGIN_AREA])
30301 expose_area (w, row, r, RIGHT_MARGIN_AREA);
30302 draw_row_fringe_bitmaps (w, row);
30305 return row->mouse_face_p;
30309 /* Redraw those parts of glyphs rows during expose event handling that
30310 overlap other rows. Redrawing of an exposed line writes over parts
30311 of lines overlapping that exposed line; this function fixes that.
30313 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
30314 row in W's current matrix that is exposed and overlaps other rows.
30315 LAST_OVERLAPPING_ROW is the last such row. */
30317 static void
30318 expose_overlaps (struct window *w,
30319 struct glyph_row *first_overlapping_row,
30320 struct glyph_row *last_overlapping_row,
30321 XRectangle *r)
30323 struct glyph_row *row;
30325 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
30326 if (row->overlapping_p)
30328 eassert (row->enabled_p && !row->mode_line_p);
30330 row->clip = r;
30331 if (row->used[LEFT_MARGIN_AREA])
30332 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
30334 if (row->used[TEXT_AREA])
30335 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
30337 if (row->used[RIGHT_MARGIN_AREA])
30338 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
30339 row->clip = NULL;
30344 /* Return non-zero if W's cursor intersects rectangle R. */
30346 static int
30347 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
30349 XRectangle cr, result;
30350 struct glyph *cursor_glyph;
30351 struct glyph_row *row;
30353 if (w->phys_cursor.vpos >= 0
30354 && w->phys_cursor.vpos < w->current_matrix->nrows
30355 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
30356 row->enabled_p)
30357 && row->cursor_in_fringe_p)
30359 /* Cursor is in the fringe. */
30360 cr.x = window_box_right_offset (w,
30361 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
30362 ? RIGHT_MARGIN_AREA
30363 : TEXT_AREA));
30364 cr.y = row->y;
30365 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
30366 cr.height = row->height;
30367 return x_intersect_rectangles (&cr, r, &result);
30370 cursor_glyph = get_phys_cursor_glyph (w);
30371 if (cursor_glyph)
30373 /* r is relative to W's box, but w->phys_cursor.x is relative
30374 to left edge of W's TEXT area. Adjust it. */
30375 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
30376 cr.y = w->phys_cursor.y;
30377 cr.width = cursor_glyph->pixel_width;
30378 cr.height = w->phys_cursor_height;
30379 /* ++KFS: W32 version used W32-specific IntersectRect here, but
30380 I assume the effect is the same -- and this is portable. */
30381 return x_intersect_rectangles (&cr, r, &result);
30383 /* If we don't understand the format, pretend we're not in the hot-spot. */
30384 return 0;
30388 /* EXPORT:
30389 Draw a vertical window border to the right of window W if W doesn't
30390 have vertical scroll bars. */
30392 void
30393 x_draw_vertical_border (struct window *w)
30395 struct frame *f = XFRAME (WINDOW_FRAME (w));
30397 /* We could do better, if we knew what type of scroll-bar the adjacent
30398 windows (on either side) have... But we don't :-(
30399 However, I think this works ok. ++KFS 2003-04-25 */
30401 /* Redraw borders between horizontally adjacent windows. Don't
30402 do it for frames with vertical scroll bars because either the
30403 right scroll bar of a window, or the left scroll bar of its
30404 neighbor will suffice as a border. */
30405 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
30406 return;
30408 /* Note: It is necessary to redraw both the left and the right
30409 borders, for when only this single window W is being
30410 redisplayed. */
30411 if (!WINDOW_RIGHTMOST_P (w)
30412 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
30414 int x0, x1, y0, y1;
30416 window_box_edges (w, &x0, &y0, &x1, &y1);
30417 y1 -= 1;
30419 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30420 x1 -= 1;
30422 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
30425 if (!WINDOW_LEFTMOST_P (w)
30426 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
30428 int x0, x1, y0, y1;
30430 window_box_edges (w, &x0, &y0, &x1, &y1);
30431 y1 -= 1;
30433 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
30434 x0 -= 1;
30436 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
30441 /* Draw window dividers for window W. */
30443 void
30444 x_draw_right_divider (struct window *w)
30446 struct frame *f = WINDOW_XFRAME (w);
30448 if (w->mini || w->pseudo_window_p)
30449 return;
30450 else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30452 int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
30453 int x1 = WINDOW_RIGHT_EDGE_X (w);
30454 int y0 = WINDOW_TOP_EDGE_Y (w);
30455 /* The bottom divider prevails. */
30456 int y1 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30458 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30462 static void
30463 x_draw_bottom_divider (struct window *w)
30465 struct frame *f = XFRAME (WINDOW_FRAME (w));
30467 if (w->mini || w->pseudo_window_p)
30468 return;
30469 else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30471 int x0 = WINDOW_LEFT_EDGE_X (w);
30472 int x1 = WINDOW_RIGHT_EDGE_X (w);
30473 int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
30474 int y1 = WINDOW_BOTTOM_EDGE_Y (w);
30476 FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
30480 /* Redraw the part of window W intersection rectangle FR. Pixel
30481 coordinates in FR are frame-relative. Call this function with
30482 input blocked. Value is non-zero if the exposure overwrites
30483 mouse-face. */
30485 static int
30486 expose_window (struct window *w, XRectangle *fr)
30488 struct frame *f = XFRAME (w->frame);
30489 XRectangle wr, r;
30490 int mouse_face_overwritten_p = 0;
30492 /* If window is not yet fully initialized, do nothing. This can
30493 happen when toolkit scroll bars are used and a window is split.
30494 Reconfiguring the scroll bar will generate an expose for a newly
30495 created window. */
30496 if (w->current_matrix == NULL)
30497 return 0;
30499 /* When we're currently updating the window, display and current
30500 matrix usually don't agree. Arrange for a thorough display
30501 later. */
30502 if (w->must_be_updated_p)
30504 SET_FRAME_GARBAGED (f);
30505 return 0;
30508 /* Frame-relative pixel rectangle of W. */
30509 wr.x = WINDOW_LEFT_EDGE_X (w);
30510 wr.y = WINDOW_TOP_EDGE_Y (w);
30511 wr.width = WINDOW_PIXEL_WIDTH (w);
30512 wr.height = WINDOW_PIXEL_HEIGHT (w);
30514 if (x_intersect_rectangles (fr, &wr, &r))
30516 int yb = window_text_bottom_y (w);
30517 struct glyph_row *row;
30518 int cursor_cleared_p, phys_cursor_on_p;
30519 struct glyph_row *first_overlapping_row, *last_overlapping_row;
30521 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
30522 r.x, r.y, r.width, r.height));
30524 /* Convert to window coordinates. */
30525 r.x -= WINDOW_LEFT_EDGE_X (w);
30526 r.y -= WINDOW_TOP_EDGE_Y (w);
30528 /* Turn off the cursor. */
30529 if (!w->pseudo_window_p
30530 && phys_cursor_in_rect_p (w, &r))
30532 x_clear_cursor (w);
30533 cursor_cleared_p = 1;
30535 else
30536 cursor_cleared_p = 0;
30538 /* If the row containing the cursor extends face to end of line,
30539 then expose_area might overwrite the cursor outside the
30540 rectangle and thus notice_overwritten_cursor might clear
30541 w->phys_cursor_on_p. We remember the original value and
30542 check later if it is changed. */
30543 phys_cursor_on_p = w->phys_cursor_on_p;
30545 /* Update lines intersecting rectangle R. */
30546 first_overlapping_row = last_overlapping_row = NULL;
30547 for (row = w->current_matrix->rows;
30548 row->enabled_p;
30549 ++row)
30551 int y0 = row->y;
30552 int y1 = MATRIX_ROW_BOTTOM_Y (row);
30554 if ((y0 >= r.y && y0 < r.y + r.height)
30555 || (y1 > r.y && y1 < r.y + r.height)
30556 || (r.y >= y0 && r.y < y1)
30557 || (r.y + r.height > y0 && r.y + r.height < y1))
30559 /* A header line may be overlapping, but there is no need
30560 to fix overlapping areas for them. KFS 2005-02-12 */
30561 if (row->overlapping_p && !row->mode_line_p)
30563 if (first_overlapping_row == NULL)
30564 first_overlapping_row = row;
30565 last_overlapping_row = row;
30568 row->clip = fr;
30569 if (expose_line (w, row, &r))
30570 mouse_face_overwritten_p = 1;
30571 row->clip = NULL;
30573 else if (row->overlapping_p)
30575 /* We must redraw a row overlapping the exposed area. */
30576 if (y0 < r.y
30577 ? y0 + row->phys_height > r.y
30578 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
30580 if (first_overlapping_row == NULL)
30581 first_overlapping_row = row;
30582 last_overlapping_row = row;
30586 if (y1 >= yb)
30587 break;
30590 /* Display the mode line if there is one. */
30591 if (WINDOW_WANTS_MODELINE_P (w)
30592 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
30593 row->enabled_p)
30594 && row->y < r.y + r.height)
30596 if (expose_line (w, row, &r))
30597 mouse_face_overwritten_p = 1;
30600 if (!w->pseudo_window_p)
30602 /* Fix the display of overlapping rows. */
30603 if (first_overlapping_row)
30604 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
30605 fr);
30607 /* Draw border between windows. */
30608 if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
30609 x_draw_right_divider (w);
30610 else
30611 x_draw_vertical_border (w);
30613 if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
30614 x_draw_bottom_divider (w);
30616 /* Turn the cursor on again. */
30617 if (cursor_cleared_p
30618 || (phys_cursor_on_p && !w->phys_cursor_on_p))
30619 update_window_cursor (w, 1);
30623 return mouse_face_overwritten_p;
30628 /* Redraw (parts) of all windows in the window tree rooted at W that
30629 intersect R. R contains frame pixel coordinates. Value is
30630 non-zero if the exposure overwrites mouse-face. */
30632 static int
30633 expose_window_tree (struct window *w, XRectangle *r)
30635 struct frame *f = XFRAME (w->frame);
30636 int mouse_face_overwritten_p = 0;
30638 while (w && !FRAME_GARBAGED_P (f))
30640 if (WINDOWP (w->contents))
30641 mouse_face_overwritten_p
30642 |= expose_window_tree (XWINDOW (w->contents), r);
30643 else
30644 mouse_face_overwritten_p |= expose_window (w, r);
30646 w = NILP (w->next) ? NULL : XWINDOW (w->next);
30649 return mouse_face_overwritten_p;
30653 /* EXPORT:
30654 Redisplay an exposed area of frame F. X and Y are the upper-left
30655 corner of the exposed rectangle. W and H are width and height of
30656 the exposed area. All are pixel values. W or H zero means redraw
30657 the entire frame. */
30659 void
30660 expose_frame (struct frame *f, int x, int y, int w, int h)
30662 XRectangle r;
30663 int mouse_face_overwritten_p = 0;
30665 TRACE ((stderr, "expose_frame "));
30667 /* No need to redraw if frame will be redrawn soon. */
30668 if (FRAME_GARBAGED_P (f))
30670 TRACE ((stderr, " garbaged\n"));
30671 return;
30674 /* If basic faces haven't been realized yet, there is no point in
30675 trying to redraw anything. This can happen when we get an expose
30676 event while Emacs is starting, e.g. by moving another window. */
30677 if (FRAME_FACE_CACHE (f) == NULL
30678 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
30680 TRACE ((stderr, " no faces\n"));
30681 return;
30684 if (w == 0 || h == 0)
30686 r.x = r.y = 0;
30687 r.width = FRAME_TEXT_WIDTH (f);
30688 r.height = FRAME_TEXT_HEIGHT (f);
30690 else
30692 r.x = x;
30693 r.y = y;
30694 r.width = w;
30695 r.height = h;
30698 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
30699 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
30701 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
30702 if (WINDOWP (f->tool_bar_window))
30703 mouse_face_overwritten_p
30704 |= expose_window (XWINDOW (f->tool_bar_window), &r);
30705 #endif
30707 #ifdef HAVE_X_WINDOWS
30708 #ifndef MSDOS
30709 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
30710 if (WINDOWP (f->menu_bar_window))
30711 mouse_face_overwritten_p
30712 |= expose_window (XWINDOW (f->menu_bar_window), &r);
30713 #endif /* not USE_X_TOOLKIT and not USE_GTK */
30714 #endif
30715 #endif
30717 /* Some window managers support a focus-follows-mouse style with
30718 delayed raising of frames. Imagine a partially obscured frame,
30719 and moving the mouse into partially obscured mouse-face on that
30720 frame. The visible part of the mouse-face will be highlighted,
30721 then the WM raises the obscured frame. With at least one WM, KDE
30722 2.1, Emacs is not getting any event for the raising of the frame
30723 (even tried with SubstructureRedirectMask), only Expose events.
30724 These expose events will draw text normally, i.e. not
30725 highlighted. Which means we must redo the highlight here.
30726 Subsume it under ``we love X''. --gerd 2001-08-15 */
30727 /* Included in Windows version because Windows most likely does not
30728 do the right thing if any third party tool offers
30729 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
30730 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
30732 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
30733 if (f == hlinfo->mouse_face_mouse_frame)
30735 int mouse_x = hlinfo->mouse_face_mouse_x;
30736 int mouse_y = hlinfo->mouse_face_mouse_y;
30737 clear_mouse_face (hlinfo);
30738 note_mouse_highlight (f, mouse_x, mouse_y);
30744 /* EXPORT:
30745 Determine the intersection of two rectangles R1 and R2. Return
30746 the intersection in *RESULT. Value is non-zero if RESULT is not
30747 empty. */
30750 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
30752 XRectangle *left, *right;
30753 XRectangle *upper, *lower;
30754 int intersection_p = 0;
30756 /* Rearrange so that R1 is the left-most rectangle. */
30757 if (r1->x < r2->x)
30758 left = r1, right = r2;
30759 else
30760 left = r2, right = r1;
30762 /* X0 of the intersection is right.x0, if this is inside R1,
30763 otherwise there is no intersection. */
30764 if (right->x <= left->x + left->width)
30766 result->x = right->x;
30768 /* The right end of the intersection is the minimum of
30769 the right ends of left and right. */
30770 result->width = (min (left->x + left->width, right->x + right->width)
30771 - result->x);
30773 /* Same game for Y. */
30774 if (r1->y < r2->y)
30775 upper = r1, lower = r2;
30776 else
30777 upper = r2, lower = r1;
30779 /* The upper end of the intersection is lower.y0, if this is inside
30780 of upper. Otherwise, there is no intersection. */
30781 if (lower->y <= upper->y + upper->height)
30783 result->y = lower->y;
30785 /* The lower end of the intersection is the minimum of the lower
30786 ends of upper and lower. */
30787 result->height = (min (lower->y + lower->height,
30788 upper->y + upper->height)
30789 - result->y);
30790 intersection_p = 1;
30794 return intersection_p;
30797 #endif /* HAVE_WINDOW_SYSTEM */
30800 /***********************************************************************
30801 Initialization
30802 ***********************************************************************/
30804 void
30805 syms_of_xdisp (void)
30807 Vwith_echo_area_save_vector = Qnil;
30808 staticpro (&Vwith_echo_area_save_vector);
30810 Vmessage_stack = Qnil;
30811 staticpro (&Vmessage_stack);
30813 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
30814 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
30816 message_dolog_marker1 = Fmake_marker ();
30817 staticpro (&message_dolog_marker1);
30818 message_dolog_marker2 = Fmake_marker ();
30819 staticpro (&message_dolog_marker2);
30820 message_dolog_marker3 = Fmake_marker ();
30821 staticpro (&message_dolog_marker3);
30823 #ifdef GLYPH_DEBUG
30824 defsubr (&Sdump_frame_glyph_matrix);
30825 defsubr (&Sdump_glyph_matrix);
30826 defsubr (&Sdump_glyph_row);
30827 defsubr (&Sdump_tool_bar_row);
30828 defsubr (&Strace_redisplay);
30829 defsubr (&Strace_to_stderr);
30830 #endif
30831 #ifdef HAVE_WINDOW_SYSTEM
30832 defsubr (&Stool_bar_height);
30833 defsubr (&Slookup_image_map);
30834 #endif
30835 defsubr (&Sline_pixel_height);
30836 defsubr (&Sformat_mode_line);
30837 defsubr (&Sinvisible_p);
30838 defsubr (&Scurrent_bidi_paragraph_direction);
30839 defsubr (&Swindow_text_pixel_size);
30840 defsubr (&Smove_point_visually);
30841 defsubr (&Sbidi_find_overridden_directionality);
30843 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
30844 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
30845 DEFSYM (Qoverriding_local_map, "overriding-local-map");
30846 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
30847 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
30848 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
30849 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
30850 DEFSYM (Qeval, "eval");
30851 DEFSYM (QCdata, ":data");
30852 DEFSYM (Qdisplay, "display");
30853 DEFSYM (Qspace_width, "space-width");
30854 DEFSYM (Qraise, "raise");
30855 DEFSYM (Qslice, "slice");
30856 DEFSYM (Qspace, "space");
30857 DEFSYM (Qmargin, "margin");
30858 DEFSYM (Qpointer, "pointer");
30859 DEFSYM (Qleft_margin, "left-margin");
30860 DEFSYM (Qright_margin, "right-margin");
30861 DEFSYM (Qcenter, "center");
30862 DEFSYM (Qline_height, "line-height");
30863 DEFSYM (QCalign_to, ":align-to");
30864 DEFSYM (QCrelative_width, ":relative-width");
30865 DEFSYM (QCrelative_height, ":relative-height");
30866 DEFSYM (QCeval, ":eval");
30867 DEFSYM (QCpropertize, ":propertize");
30868 DEFSYM (QCfile, ":file");
30869 DEFSYM (Qfontified, "fontified");
30870 DEFSYM (Qfontification_functions, "fontification-functions");
30871 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
30872 DEFSYM (Qescape_glyph, "escape-glyph");
30873 DEFSYM (Qnobreak_space, "nobreak-space");
30874 DEFSYM (Qimage, "image");
30875 DEFSYM (Qtext, "text");
30876 DEFSYM (Qboth, "both");
30877 DEFSYM (Qboth_horiz, "both-horiz");
30878 DEFSYM (Qtext_image_horiz, "text-image-horiz");
30879 DEFSYM (QCmap, ":map");
30880 DEFSYM (QCpointer, ":pointer");
30881 DEFSYM (Qrect, "rect");
30882 DEFSYM (Qcircle, "circle");
30883 DEFSYM (Qpoly, "poly");
30884 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
30885 DEFSYM (Qgrow_only, "grow-only");
30886 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
30887 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
30888 DEFSYM (Qposition, "position");
30889 DEFSYM (Qbuffer_position, "buffer-position");
30890 DEFSYM (Qobject, "object");
30891 DEFSYM (Qbar, "bar");
30892 DEFSYM (Qhbar, "hbar");
30893 DEFSYM (Qbox, "box");
30894 DEFSYM (Qhollow, "hollow");
30895 DEFSYM (Qhand, "hand");
30896 DEFSYM (Qarrow, "arrow");
30897 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
30899 list_of_error = list1 (list2 (intern_c_string ("error"),
30900 intern_c_string ("void-variable")));
30901 staticpro (&list_of_error);
30903 DEFSYM (Qlast_arrow_position, "last-arrow-position");
30904 DEFSYM (Qlast_arrow_string, "last-arrow-string");
30905 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
30906 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
30908 echo_buffer[0] = echo_buffer[1] = Qnil;
30909 staticpro (&echo_buffer[0]);
30910 staticpro (&echo_buffer[1]);
30912 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
30913 staticpro (&echo_area_buffer[0]);
30914 staticpro (&echo_area_buffer[1]);
30916 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
30917 staticpro (&Vmessages_buffer_name);
30919 mode_line_proptrans_alist = Qnil;
30920 staticpro (&mode_line_proptrans_alist);
30921 mode_line_string_list = Qnil;
30922 staticpro (&mode_line_string_list);
30923 mode_line_string_face = Qnil;
30924 staticpro (&mode_line_string_face);
30925 mode_line_string_face_prop = Qnil;
30926 staticpro (&mode_line_string_face_prop);
30927 Vmode_line_unwind_vector = Qnil;
30928 staticpro (&Vmode_line_unwind_vector);
30930 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
30932 help_echo_string = Qnil;
30933 staticpro (&help_echo_string);
30934 help_echo_object = Qnil;
30935 staticpro (&help_echo_object);
30936 help_echo_window = Qnil;
30937 staticpro (&help_echo_window);
30938 previous_help_echo_string = Qnil;
30939 staticpro (&previous_help_echo_string);
30940 help_echo_pos = -1;
30942 DEFSYM (Qright_to_left, "right-to-left");
30943 DEFSYM (Qleft_to_right, "left-to-right");
30944 defsubr (&Sbidi_resolved_levels);
30946 #ifdef HAVE_WINDOW_SYSTEM
30947 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
30948 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
30949 For example, if a block cursor is over a tab, it will be drawn as
30950 wide as that tab on the display. */);
30951 x_stretch_cursor_p = 0;
30952 #endif
30954 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
30955 doc: /* Non-nil means highlight trailing whitespace.
30956 The face used for trailing whitespace is `trailing-whitespace'. */);
30957 Vshow_trailing_whitespace = Qnil;
30959 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
30960 doc: /* Control highlighting of non-ASCII space and hyphen chars.
30961 If the value is t, Emacs highlights non-ASCII chars which have the
30962 same appearance as an ASCII space or hyphen, using the `nobreak-space'
30963 or `escape-glyph' face respectively.
30965 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
30966 U+2011 (non-breaking hyphen) are affected.
30968 Any other non-nil value means to display these characters as a escape
30969 glyph followed by an ordinary space or hyphen.
30971 A value of nil means no special handling of these characters. */);
30972 Vnobreak_char_display = Qt;
30974 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
30975 doc: /* The pointer shape to show in void text areas.
30976 A value of nil means to show the text pointer. Other options are
30977 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
30978 `hourglass'. */);
30979 Vvoid_text_area_pointer = Qarrow;
30981 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
30982 doc: /* Non-nil means don't actually do any redisplay.
30983 This is used for internal purposes. */);
30984 Vinhibit_redisplay = Qnil;
30986 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
30987 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
30988 Vglobal_mode_string = Qnil;
30990 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
30991 doc: /* Marker for where to display an arrow on top of the buffer text.
30992 This must be the beginning of a line in order to work.
30993 See also `overlay-arrow-string'. */);
30994 Voverlay_arrow_position = Qnil;
30996 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
30997 doc: /* String to display as an arrow in non-window frames.
30998 See also `overlay-arrow-position'. */);
30999 Voverlay_arrow_string = build_pure_c_string ("=>");
31001 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
31002 doc: /* List of variables (symbols) which hold markers for overlay arrows.
31003 The symbols on this list are examined during redisplay to determine
31004 where to display overlay arrows. */);
31005 Voverlay_arrow_variable_list
31006 = list1 (intern_c_string ("overlay-arrow-position"));
31008 DEFVAR_INT ("scroll-step", emacs_scroll_step,
31009 doc: /* The number of lines to try scrolling a window by when point moves out.
31010 If that fails to bring point back on frame, point is centered instead.
31011 If this is zero, point is always centered after it moves off frame.
31012 If you want scrolling to always be a line at a time, you should set
31013 `scroll-conservatively' to a large value rather than set this to 1. */);
31015 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
31016 doc: /* Scroll up to this many lines, to bring point back on screen.
31017 If point moves off-screen, redisplay will scroll by up to
31018 `scroll-conservatively' lines in order to bring point just barely
31019 onto the screen again. If that cannot be done, then redisplay
31020 recenters point as usual.
31022 If the value is greater than 100, redisplay will never recenter point,
31023 but will always scroll just enough text to bring point into view, even
31024 if you move far away.
31026 A value of zero means always recenter point if it moves off screen. */);
31027 scroll_conservatively = 0;
31029 DEFVAR_INT ("scroll-margin", scroll_margin,
31030 doc: /* Number of lines of margin at the top and bottom of a window.
31031 Recenter the window whenever point gets within this many lines
31032 of the top or bottom of the window. */);
31033 scroll_margin = 0;
31035 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
31036 doc: /* Pixels per inch value for non-window system displays.
31037 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
31038 Vdisplay_pixels_per_inch = make_float (72.0);
31040 #ifdef GLYPH_DEBUG
31041 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
31042 #endif
31044 DEFVAR_LISP ("truncate-partial-width-windows",
31045 Vtruncate_partial_width_windows,
31046 doc: /* Non-nil means truncate lines in windows narrower than the frame.
31047 For an integer value, truncate lines in each window narrower than the
31048 full frame width, provided the window width is less than that integer;
31049 otherwise, respect the value of `truncate-lines'.
31051 For any other non-nil value, truncate lines in all windows that do
31052 not span the full frame width.
31054 A value of nil means to respect the value of `truncate-lines'.
31056 If `word-wrap' is enabled, you might want to reduce this. */);
31057 Vtruncate_partial_width_windows = make_number (50);
31059 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
31060 doc: /* Maximum buffer size for which line number should be displayed.
31061 If the buffer is bigger than this, the line number does not appear
31062 in the mode line. A value of nil means no limit. */);
31063 Vline_number_display_limit = Qnil;
31065 DEFVAR_INT ("line-number-display-limit-width",
31066 line_number_display_limit_width,
31067 doc: /* Maximum line width (in characters) for line number display.
31068 If the average length of the lines near point is bigger than this, then the
31069 line number may be omitted from the mode line. */);
31070 line_number_display_limit_width = 200;
31072 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
31073 doc: /* Non-nil means highlight region even in nonselected windows. */);
31074 highlight_nonselected_windows = 0;
31076 DEFVAR_BOOL ("multiple-frames", multiple_frames,
31077 doc: /* Non-nil if more than one frame is visible on this display.
31078 Minibuffer-only frames don't count, but iconified frames do.
31079 This variable is not guaranteed to be accurate except while processing
31080 `frame-title-format' and `icon-title-format'. */);
31082 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
31083 doc: /* Template for displaying the title bar of visible frames.
31084 \(Assuming the window manager supports this feature.)
31086 This variable has the same structure as `mode-line-format', except that
31087 the %c and %l constructs are ignored. It is used only on frames for
31088 which no explicit name has been set \(see `modify-frame-parameters'). */);
31090 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
31091 doc: /* Template for displaying the title bar of an iconified frame.
31092 \(Assuming the window manager supports this feature.)
31093 This variable has the same structure as `mode-line-format' (which see),
31094 and is used only on frames for which no explicit name has been set
31095 \(see `modify-frame-parameters'). */);
31096 Vicon_title_format
31097 = Vframe_title_format
31098 = listn (CONSTYPE_PURE, 3,
31099 intern_c_string ("multiple-frames"),
31100 build_pure_c_string ("%b"),
31101 listn (CONSTYPE_PURE, 4,
31102 empty_unibyte_string,
31103 intern_c_string ("invocation-name"),
31104 build_pure_c_string ("@"),
31105 intern_c_string ("system-name")));
31107 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
31108 doc: /* Maximum number of lines to keep in the message log buffer.
31109 If nil, disable message logging. If t, log messages but don't truncate
31110 the buffer when it becomes large. */);
31111 Vmessage_log_max = make_number (1000);
31113 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
31114 doc: /* Functions called before redisplay, if window sizes have changed.
31115 The value should be a list of functions that take one argument.
31116 Just before redisplay, for each frame, if any of its windows have changed
31117 size since the last redisplay, or have been split or deleted,
31118 all the functions in the list are called, with the frame as argument. */);
31119 Vwindow_size_change_functions = Qnil;
31121 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
31122 doc: /* List of functions to call before redisplaying a window with scrolling.
31123 Each function is called with two arguments, the window and its new
31124 display-start position.
31125 These functions are called whenever the `window-start' marker is modified,
31126 either to point into another buffer (e.g. via `set-window-buffer') or another
31127 place in the same buffer.
31128 Note that the value of `window-end' is not valid when these functions are
31129 called.
31131 Warning: Do not use this feature to alter the way the window
31132 is scrolled. It is not designed for that, and such use probably won't
31133 work. */);
31134 Vwindow_scroll_functions = Qnil;
31136 DEFVAR_LISP ("window-text-change-functions",
31137 Vwindow_text_change_functions,
31138 doc: /* Functions to call in redisplay when text in the window might change. */);
31139 Vwindow_text_change_functions = Qnil;
31141 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
31142 doc: /* Functions called when redisplay of a window reaches the end trigger.
31143 Each function is called with two arguments, the window and the end trigger value.
31144 See `set-window-redisplay-end-trigger'. */);
31145 Vredisplay_end_trigger_functions = Qnil;
31147 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
31148 doc: /* Non-nil means autoselect window with mouse pointer.
31149 If nil, do not autoselect windows.
31150 A positive number means delay autoselection by that many seconds: a
31151 window is autoselected only after the mouse has remained in that
31152 window for the duration of the delay.
31153 A negative number has a similar effect, but causes windows to be
31154 autoselected only after the mouse has stopped moving. \(Because of
31155 the way Emacs compares mouse events, you will occasionally wait twice
31156 that time before the window gets selected.\)
31157 Any other value means to autoselect window instantaneously when the
31158 mouse pointer enters it.
31160 Autoselection selects the minibuffer only if it is active, and never
31161 unselects the minibuffer if it is active.
31163 When customizing this variable make sure that the actual value of
31164 `focus-follows-mouse' matches the behavior of your window manager. */);
31165 Vmouse_autoselect_window = Qnil;
31167 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
31168 doc: /* Non-nil means automatically resize tool-bars.
31169 This dynamically changes the tool-bar's height to the minimum height
31170 that is needed to make all tool-bar items visible.
31171 If value is `grow-only', the tool-bar's height is only increased
31172 automatically; to decrease the tool-bar height, use \\[recenter]. */);
31173 Vauto_resize_tool_bars = Qt;
31175 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
31176 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
31177 auto_raise_tool_bar_buttons_p = 1;
31179 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
31180 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
31181 make_cursor_line_fully_visible_p = 1;
31183 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
31184 doc: /* Border below tool-bar in pixels.
31185 If an integer, use it as the height of the border.
31186 If it is one of `internal-border-width' or `border-width', use the
31187 value of the corresponding frame parameter.
31188 Otherwise, no border is added below the tool-bar. */);
31189 Vtool_bar_border = Qinternal_border_width;
31191 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
31192 doc: /* Margin around tool-bar buttons in pixels.
31193 If an integer, use that for both horizontal and vertical margins.
31194 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
31195 HORZ specifying the horizontal margin, and VERT specifying the
31196 vertical margin. */);
31197 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
31199 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
31200 doc: /* Relief thickness of tool-bar buttons. */);
31201 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
31203 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
31204 doc: /* Tool bar style to use.
31205 It can be one of
31206 image - show images only
31207 text - show text only
31208 both - show both, text below image
31209 both-horiz - show text to the right of the image
31210 text-image-horiz - show text to the left of the image
31211 any other - use system default or image if no system default.
31213 This variable only affects the GTK+ toolkit version of Emacs. */);
31214 Vtool_bar_style = Qnil;
31216 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
31217 doc: /* Maximum number of characters a label can have to be shown.
31218 The tool bar style must also show labels for this to have any effect, see
31219 `tool-bar-style'. */);
31220 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
31222 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
31223 doc: /* List of functions to call to fontify regions of text.
31224 Each function is called with one argument POS. Functions must
31225 fontify a region starting at POS in the current buffer, and give
31226 fontified regions the property `fontified'. */);
31227 Vfontification_functions = Qnil;
31228 Fmake_variable_buffer_local (Qfontification_functions);
31230 DEFVAR_BOOL ("unibyte-display-via-language-environment",
31231 unibyte_display_via_language_environment,
31232 doc: /* Non-nil means display unibyte text according to language environment.
31233 Specifically, this means that raw bytes in the range 160-255 decimal
31234 are displayed by converting them to the equivalent multibyte characters
31235 according to the current language environment. As a result, they are
31236 displayed according to the current fontset.
31238 Note that this variable affects only how these bytes are displayed,
31239 but does not change the fact they are interpreted as raw bytes. */);
31240 unibyte_display_via_language_environment = 0;
31242 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
31243 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
31244 If a float, it specifies a fraction of the mini-window frame's height.
31245 If an integer, it specifies a number of lines. */);
31246 Vmax_mini_window_height = make_float (0.25);
31248 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
31249 doc: /* How to resize mini-windows (the minibuffer and the echo area).
31250 A value of nil means don't automatically resize mini-windows.
31251 A value of t means resize them to fit the text displayed in them.
31252 A value of `grow-only', the default, means let mini-windows grow only;
31253 they return to their normal size when the minibuffer is closed, or the
31254 echo area becomes empty. */);
31255 Vresize_mini_windows = Qgrow_only;
31257 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
31258 doc: /* Alist specifying how to blink the cursor off.
31259 Each element has the form (ON-STATE . OFF-STATE). Whenever the
31260 `cursor-type' frame-parameter or variable equals ON-STATE,
31261 comparing using `equal', Emacs uses OFF-STATE to specify
31262 how to blink it off. ON-STATE and OFF-STATE are values for
31263 the `cursor-type' frame parameter.
31265 If a frame's ON-STATE has no entry in this list,
31266 the frame's other specifications determine how to blink the cursor off. */);
31267 Vblink_cursor_alist = Qnil;
31269 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
31270 doc: /* Allow or disallow automatic horizontal scrolling of windows.
31271 If non-nil, windows are automatically scrolled horizontally to make
31272 point visible. */);
31273 automatic_hscrolling_p = 1;
31274 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
31276 DEFVAR_INT ("hscroll-margin", hscroll_margin,
31277 doc: /* How many columns away from the window edge point is allowed to get
31278 before automatic hscrolling will horizontally scroll the window. */);
31279 hscroll_margin = 5;
31281 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
31282 doc: /* How many columns to scroll the window when point gets too close to the edge.
31283 When point is less than `hscroll-margin' columns from the window
31284 edge, automatic hscrolling will scroll the window by the amount of columns
31285 determined by this variable. If its value is a positive integer, scroll that
31286 many columns. If it's a positive floating-point number, it specifies the
31287 fraction of the window's width to scroll. If it's nil or zero, point will be
31288 centered horizontally after the scroll. Any other value, including negative
31289 numbers, are treated as if the value were zero.
31291 Automatic hscrolling always moves point outside the scroll margin, so if
31292 point was more than scroll step columns inside the margin, the window will
31293 scroll more than the value given by the scroll step.
31295 Note that the lower bound for automatic hscrolling specified by `scroll-left'
31296 and `scroll-right' overrides this variable's effect. */);
31297 Vhscroll_step = make_number (0);
31299 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
31300 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
31301 Bind this around calls to `message' to let it take effect. */);
31302 message_truncate_lines = 0;
31304 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
31305 doc: /* Normal hook run to update the menu bar definitions.
31306 Redisplay runs this hook before it redisplays the menu bar.
31307 This is used to update menus such as Buffers, whose contents depend on
31308 various data. */);
31309 Vmenu_bar_update_hook = Qnil;
31311 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
31312 doc: /* Frame for which we are updating a menu.
31313 The enable predicate for a menu binding should check this variable. */);
31314 Vmenu_updating_frame = Qnil;
31316 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
31317 doc: /* Non-nil means don't update menu bars. Internal use only. */);
31318 inhibit_menubar_update = 0;
31320 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
31321 doc: /* Prefix prepended to all continuation lines at display time.
31322 The value may be a string, an image, or a stretch-glyph; it is
31323 interpreted in the same way as the value of a `display' text property.
31325 This variable is overridden by any `wrap-prefix' text or overlay
31326 property.
31328 To add a prefix to non-continuation lines, use `line-prefix'. */);
31329 Vwrap_prefix = Qnil;
31330 DEFSYM (Qwrap_prefix, "wrap-prefix");
31331 Fmake_variable_buffer_local (Qwrap_prefix);
31333 DEFVAR_LISP ("line-prefix", Vline_prefix,
31334 doc: /* Prefix prepended to all non-continuation lines at display time.
31335 The value may be a string, an image, or a stretch-glyph; it is
31336 interpreted in the same way as the value of a `display' text property.
31338 This variable is overridden by any `line-prefix' text or overlay
31339 property.
31341 To add a prefix to continuation lines, use `wrap-prefix'. */);
31342 Vline_prefix = Qnil;
31343 DEFSYM (Qline_prefix, "line-prefix");
31344 Fmake_variable_buffer_local (Qline_prefix);
31346 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
31347 doc: /* Non-nil means don't eval Lisp during redisplay. */);
31348 inhibit_eval_during_redisplay = 0;
31350 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
31351 doc: /* Non-nil means don't free realized faces. Internal use only. */);
31352 inhibit_free_realized_faces = 0;
31354 DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
31355 doc: /* Non-nil means don't mirror characters even when bidi context requires that.
31356 Intended for use during debugging and for testing bidi display;
31357 see biditest.el in the test suite. */);
31358 inhibit_bidi_mirroring = 0;
31360 #ifdef GLYPH_DEBUG
31361 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
31362 doc: /* Inhibit try_window_id display optimization. */);
31363 inhibit_try_window_id = 0;
31365 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
31366 doc: /* Inhibit try_window_reusing display optimization. */);
31367 inhibit_try_window_reusing = 0;
31369 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
31370 doc: /* Inhibit try_cursor_movement display optimization. */);
31371 inhibit_try_cursor_movement = 0;
31372 #endif /* GLYPH_DEBUG */
31374 DEFVAR_INT ("overline-margin", overline_margin,
31375 doc: /* Space between overline and text, in pixels.
31376 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
31377 margin to the character height. */);
31378 overline_margin = 2;
31380 DEFVAR_INT ("underline-minimum-offset",
31381 underline_minimum_offset,
31382 doc: /* Minimum distance between baseline and underline.
31383 This can improve legibility of underlined text at small font sizes,
31384 particularly when using variable `x-use-underline-position-properties'
31385 with fonts that specify an UNDERLINE_POSITION relatively close to the
31386 baseline. The default value is 1. */);
31387 underline_minimum_offset = 1;
31389 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
31390 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
31391 This feature only works when on a window system that can change
31392 cursor shapes. */);
31393 display_hourglass_p = 1;
31395 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
31396 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
31397 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
31399 #ifdef HAVE_WINDOW_SYSTEM
31400 hourglass_atimer = NULL;
31401 hourglass_shown_p = 0;
31402 #endif /* HAVE_WINDOW_SYSTEM */
31404 DEFSYM (Qglyphless_char, "glyphless-char");
31405 DEFSYM (Qhex_code, "hex-code");
31406 DEFSYM (Qempty_box, "empty-box");
31407 DEFSYM (Qthin_space, "thin-space");
31408 DEFSYM (Qzero_width, "zero-width");
31410 DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
31411 doc: /* Function run just before redisplay.
31412 It is called with one argument, which is the set of windows that are to
31413 be redisplayed. This set can be nil (meaning, only the selected window),
31414 or t (meaning all windows). */);
31415 Vpre_redisplay_function = intern ("ignore");
31417 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
31418 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
31420 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
31421 doc: /* Char-table defining glyphless characters.
31422 Each element, if non-nil, should be one of the following:
31423 an ASCII acronym string: display this string in a box
31424 `hex-code': display the hexadecimal code of a character in a box
31425 `empty-box': display as an empty box
31426 `thin-space': display as 1-pixel width space
31427 `zero-width': don't display
31428 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
31429 display method for graphical terminals and text terminals respectively.
31430 GRAPHICAL and TEXT should each have one of the values listed above.
31432 The char-table has one extra slot to control the display of a character for
31433 which no font is found. This slot only takes effect on graphical terminals.
31434 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
31435 `thin-space'. The default is `empty-box'.
31437 If a character has a non-nil entry in an active display table, the
31438 display table takes effect; in this case, Emacs does not consult
31439 `glyphless-char-display' at all. */);
31440 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
31441 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
31442 Qempty_box);
31444 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
31445 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
31446 Vdebug_on_message = Qnil;
31448 DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
31449 doc: /* */);
31450 Vredisplay__all_windows_cause
31451 = Fmake_vector (make_number (100), make_number (0));
31453 DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
31454 doc: /* */);
31455 Vredisplay__mode_lines_cause
31456 = Fmake_vector (make_number (100), make_number (0));
31460 /* Initialize this module when Emacs starts. */
31462 void
31463 init_xdisp (void)
31465 CHARPOS (this_line_start_pos) = 0;
31467 if (!noninteractive)
31469 struct window *m = XWINDOW (minibuf_window);
31470 Lisp_Object frame = m->frame;
31471 struct frame *f = XFRAME (frame);
31472 Lisp_Object root = FRAME_ROOT_WINDOW (f);
31473 struct window *r = XWINDOW (root);
31474 int i;
31476 echo_area_window = minibuf_window;
31478 r->top_line = FRAME_TOP_MARGIN (f);
31479 r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
31480 r->total_cols = FRAME_COLS (f);
31481 r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
31482 r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
31483 r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
31485 m->top_line = FRAME_TOTAL_LINES (f) - 1;
31486 m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
31487 m->total_cols = FRAME_COLS (f);
31488 m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
31489 m->total_lines = 1;
31490 m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
31492 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
31493 scratch_glyph_row.glyphs[TEXT_AREA + 1]
31494 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
31496 /* The default ellipsis glyphs `...'. */
31497 for (i = 0; i < 3; ++i)
31498 default_invis_vector[i] = make_number ('.');
31502 /* Allocate the buffer for frame titles.
31503 Also used for `format-mode-line'. */
31504 int size = 100;
31505 mode_line_noprop_buf = xmalloc (size);
31506 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
31507 mode_line_noprop_ptr = mode_line_noprop_buf;
31508 mode_line_target = MODE_LINE_DISPLAY;
31511 help_echo_showing_p = 0;
31514 #ifdef HAVE_WINDOW_SYSTEM
31516 /* Platform-independent portion of hourglass implementation. */
31518 /* Timer function of hourglass_atimer. */
31520 static void
31521 show_hourglass (struct atimer *timer)
31523 /* The timer implementation will cancel this timer automatically
31524 after this function has run. Set hourglass_atimer to null
31525 so that we know the timer doesn't have to be canceled. */
31526 hourglass_atimer = NULL;
31528 if (!hourglass_shown_p)
31530 Lisp_Object tail, frame;
31532 block_input ();
31534 FOR_EACH_FRAME (tail, frame)
31536 struct frame *f = XFRAME (frame);
31538 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31539 && FRAME_RIF (f)->show_hourglass)
31540 FRAME_RIF (f)->show_hourglass (f);
31543 hourglass_shown_p = 1;
31544 unblock_input ();
31548 /* Cancel a currently active hourglass timer, and start a new one. */
31550 void
31551 start_hourglass (void)
31553 struct timespec delay;
31555 cancel_hourglass ();
31557 if (INTEGERP (Vhourglass_delay)
31558 && XINT (Vhourglass_delay) > 0)
31559 delay = make_timespec (min (XINT (Vhourglass_delay),
31560 TYPE_MAXIMUM (time_t)),
31562 else if (FLOATP (Vhourglass_delay)
31563 && XFLOAT_DATA (Vhourglass_delay) > 0)
31564 delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
31565 else
31566 delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
31568 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
31569 show_hourglass, NULL);
31572 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
31573 shown. */
31575 void
31576 cancel_hourglass (void)
31578 if (hourglass_atimer)
31580 cancel_atimer (hourglass_atimer);
31581 hourglass_atimer = NULL;
31584 if (hourglass_shown_p)
31586 Lisp_Object tail, frame;
31588 block_input ();
31590 FOR_EACH_FRAME (tail, frame)
31592 struct frame *f = XFRAME (frame);
31594 if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
31595 && FRAME_RIF (f)->hide_hourglass)
31596 FRAME_RIF (f)->hide_hourglass (f);
31597 #ifdef HAVE_NTGUI
31598 /* No cursors on non GUI frames - restore to stock arrow cursor. */
31599 else if (!FRAME_W32_P (f))
31600 w32_arrow_cursor ();
31601 #endif
31604 hourglass_shown_p = 0;
31605 unblock_input ();
31609 #endif /* HAVE_WINDOW_SYSTEM */