Fix a typo in a comment.
[emacs.git] / src / xdisp.c
blob55d1272faafd902e4a92b2c6802d53d5a1ff59cf
1 /* Display generation from window structure and buffer text.
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 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.
103 . try_window
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
116 Desired matrices.
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
161 Frame matrices.
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
182 Bidirectional display.
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
225 Bidirectional display and character compositions
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
252 Moving an iterator in bidirectional text
253 without producing glyphs
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
316 #include "font.h"
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
322 #define INFINITY 10000000
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
347 static Lisp_Object Qfontification_functions;
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
353 /* Non-nil means don't actually do any redisplay. */
355 Lisp_Object Qinhibit_redisplay;
357 /* Names of text properties relevant for redisplay. */
359 Lisp_Object Qdisplay;
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
370 /* These setters are used only in this file, so they can be private. */
371 static void
372 wset_base_line_number (struct window *w, Lisp_Object val)
374 w->base_line_number = val;
376 static void
377 wset_base_line_pos (struct window *w, Lisp_Object val)
379 w->base_line_pos = val;
381 static void
382 wset_column_number_displayed (struct window *w, Lisp_Object val)
384 w->column_number_displayed = val;
386 static void
387 wset_region_showing (struct window *w, Lisp_Object val)
389 w->region_showing = val;
392 #ifdef HAVE_WINDOW_SYSTEM
394 /* Test if overflow newline into fringe. Called with iterator IT
395 at or past right window margin, and with IT->current_x set. */
397 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
398 (!NILP (Voverflow_newline_into_fringe) \
399 && FRAME_WINDOW_P ((IT)->f) \
400 && ((IT)->bidi_it.paragraph_dir == R2L \
401 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
402 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
403 && (IT)->current_x == (IT)->last_visible_x \
404 && (IT)->line_wrap != WORD_WRAP)
406 #else /* !HAVE_WINDOW_SYSTEM */
407 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
408 #endif /* HAVE_WINDOW_SYSTEM */
410 /* Test if the display element loaded in IT, or the underlying buffer
411 or string character, is a space or a TAB character. This is used
412 to determine where word wrapping can occur. */
414 #define IT_DISPLAYING_WHITESPACE(it) \
415 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
416 || ((STRINGP (it->string) \
417 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
418 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
419 || (it->s \
420 && (it->s[IT_BYTEPOS (*it)] == ' ' \
421 || it->s[IT_BYTEPOS (*it)] == '\t')) \
422 || (IT_BYTEPOS (*it) < ZV_BYTE \
423 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
424 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
426 /* Name of the face used to highlight trailing whitespace. */
428 static Lisp_Object Qtrailing_whitespace;
430 /* Name and number of the face used to highlight escape glyphs. */
432 static Lisp_Object Qescape_glyph;
434 /* Name and number of the face used to highlight non-breaking spaces. */
436 static Lisp_Object Qnobreak_space;
438 /* The symbol `image' which is the car of the lists used to represent
439 images in Lisp. Also a tool bar style. */
441 Lisp_Object Qimage;
443 /* The image map types. */
444 Lisp_Object QCmap;
445 static Lisp_Object QCpointer;
446 static Lisp_Object Qrect, Qcircle, Qpoly;
448 /* Tool bar styles */
449 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
451 /* Non-zero means print newline to stdout before next mini-buffer
452 message. */
454 int noninteractive_need_newline;
456 /* Non-zero means print newline to message log before next message. */
458 static int message_log_need_newline;
460 /* Three markers that message_dolog uses.
461 It could allocate them itself, but that causes trouble
462 in handling memory-full errors. */
463 static Lisp_Object message_dolog_marker1;
464 static Lisp_Object message_dolog_marker2;
465 static Lisp_Object message_dolog_marker3;
467 /* The buffer position of the first character appearing entirely or
468 partially on the line of the selected window which contains the
469 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
470 redisplay optimization in redisplay_internal. */
472 static struct text_pos this_line_start_pos;
474 /* Number of characters past the end of the line above, including the
475 terminating newline. */
477 static struct text_pos this_line_end_pos;
479 /* The vertical positions and the height of this line. */
481 static int this_line_vpos;
482 static int this_line_y;
483 static int this_line_pixel_height;
485 /* X position at which this display line starts. Usually zero;
486 negative if first character is partially visible. */
488 static int this_line_start_x;
490 /* The smallest character position seen by move_it_* functions as they
491 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
492 hscrolled lines, see display_line. */
494 static struct text_pos this_line_min_pos;
496 /* Buffer that this_line_.* variables are referring to. */
498 static struct buffer *this_line_buffer;
501 /* Values of those variables at last redisplay are stored as
502 properties on `overlay-arrow-position' symbol. However, if
503 Voverlay_arrow_position is a marker, last-arrow-position is its
504 numerical position. */
506 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
508 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
509 properties on a symbol in overlay-arrow-variable-list. */
511 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
513 Lisp_Object Qmenu_bar_update_hook;
515 /* Nonzero if an overlay arrow has been displayed in this window. */
517 static int overlay_arrow_seen;
519 /* Number of windows showing the buffer of the selected window (or
520 another buffer with the same base buffer). keyboard.c refers to
521 this. */
523 int buffer_shared;
525 /* Vector containing glyphs for an ellipsis `...'. */
527 static Lisp_Object default_invis_vector[3];
529 /* This is the window where the echo area message was displayed. It
530 is always a mini-buffer window, but it may not be the same window
531 currently active as a mini-buffer. */
533 Lisp_Object echo_area_window;
535 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
536 pushes the current message and the value of
537 message_enable_multibyte on the stack, the function restore_message
538 pops the stack and displays MESSAGE again. */
540 static Lisp_Object Vmessage_stack;
542 /* Nonzero means multibyte characters were enabled when the echo area
543 message was specified. */
545 static int message_enable_multibyte;
547 /* Nonzero if we should redraw the mode lines on the next redisplay. */
549 int update_mode_lines;
551 /* Nonzero if window sizes or contents have changed since last
552 redisplay that finished. */
554 int windows_or_buffers_changed;
556 /* Nonzero means a frame's cursor type has been changed. */
558 int cursor_type_changed;
560 /* Nonzero after display_mode_line if %l was used and it displayed a
561 line number. */
563 static int line_number_displayed;
565 /* The name of the *Messages* buffer, a string. */
567 static Lisp_Object Vmessages_buffer_name;
569 /* Current, index 0, and last displayed echo area message. Either
570 buffers from echo_buffers, or nil to indicate no message. */
572 Lisp_Object echo_area_buffer[2];
574 /* The buffers referenced from echo_area_buffer. */
576 static Lisp_Object echo_buffer[2];
578 /* A vector saved used in with_area_buffer to reduce consing. */
580 static Lisp_Object Vwith_echo_area_save_vector;
582 /* Non-zero means display_echo_area should display the last echo area
583 message again. Set by redisplay_preserve_echo_area. */
585 static int display_last_displayed_message_p;
587 /* Nonzero if echo area is being used by print; zero if being used by
588 message. */
590 static int message_buf_print;
592 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
594 static Lisp_Object Qinhibit_menubar_update;
595 static Lisp_Object Qmessage_truncate_lines;
597 /* Set to 1 in clear_message to make redisplay_internal aware
598 of an emptied echo area. */
600 static int message_cleared_p;
602 /* A scratch glyph row with contents used for generating truncation
603 glyphs. Also used in direct_output_for_insert. */
605 #define MAX_SCRATCH_GLYPHS 100
606 static struct glyph_row scratch_glyph_row;
607 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
609 /* Ascent and height of the last line processed by move_it_to. */
611 static int last_max_ascent, last_height;
613 /* Non-zero if there's a help-echo in the echo area. */
615 int help_echo_showing_p;
617 /* If >= 0, computed, exact values of mode-line and header-line height
618 to use in the macros CURRENT_MODE_LINE_HEIGHT and
619 CURRENT_HEADER_LINE_HEIGHT. */
621 int current_mode_line_height, current_header_line_height;
623 /* The maximum distance to look ahead for text properties. Values
624 that are too small let us call compute_char_face and similar
625 functions too often which is expensive. Values that are too large
626 let us call compute_char_face and alike too often because we
627 might not be interested in text properties that far away. */
629 #define TEXT_PROP_DISTANCE_LIMIT 100
631 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
632 iterator state and later restore it. This is needed because the
633 bidi iterator on bidi.c keeps a stacked cache of its states, which
634 is really a singleton. When we use scratch iterator objects to
635 move around the buffer, we can cause the bidi cache to be pushed or
636 popped, and therefore we need to restore the cache state when we
637 return to the original iterator. */
638 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
639 do { \
640 if (CACHE) \
641 bidi_unshelve_cache (CACHE, 1); \
642 ITCOPY = ITORIG; \
643 CACHE = bidi_shelve_cache (); \
644 } while (0)
646 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
647 do { \
648 if (pITORIG != pITCOPY) \
649 *(pITORIG) = *(pITCOPY); \
650 bidi_unshelve_cache (CACHE, 0); \
651 CACHE = NULL; \
652 } while (0)
654 #ifdef GLYPH_DEBUG
656 /* Non-zero means print traces of redisplay if compiled with
657 GLYPH_DEBUG defined. */
659 int trace_redisplay_p;
661 #endif /* GLYPH_DEBUG */
663 #ifdef DEBUG_TRACE_MOVE
664 /* Non-zero means trace with TRACE_MOVE to stderr. */
665 int trace_move;
667 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
668 #else
669 #define TRACE_MOVE(x) (void) 0
670 #endif
672 static Lisp_Object Qauto_hscroll_mode;
674 /* Buffer being redisplayed -- for redisplay_window_error. */
676 static struct buffer *displayed_buffer;
678 /* Value returned from text property handlers (see below). */
680 enum prop_handled
682 HANDLED_NORMALLY,
683 HANDLED_RECOMPUTE_PROPS,
684 HANDLED_OVERLAY_STRING_CONSUMED,
685 HANDLED_RETURN
688 /* A description of text properties that redisplay is interested
689 in. */
691 struct props
693 /* The name of the property. */
694 Lisp_Object *name;
696 /* A unique index for the property. */
697 enum prop_idx idx;
699 /* A handler function called to set up iterator IT from the property
700 at IT's current position. Value is used to steer handle_stop. */
701 enum prop_handled (*handler) (struct it *it);
704 static enum prop_handled handle_face_prop (struct it *);
705 static enum prop_handled handle_invisible_prop (struct it *);
706 static enum prop_handled handle_display_prop (struct it *);
707 static enum prop_handled handle_composition_prop (struct it *);
708 static enum prop_handled handle_overlay_change (struct it *);
709 static enum prop_handled handle_fontified_prop (struct it *);
711 /* Properties handled by iterators. */
713 static struct props it_props[] =
715 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
716 /* Handle `face' before `display' because some sub-properties of
717 `display' need to know the face. */
718 {&Qface, FACE_PROP_IDX, handle_face_prop},
719 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
720 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
721 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
722 {NULL, 0, NULL}
725 /* Value is the position described by X. If X is a marker, value is
726 the marker_position of X. Otherwise, value is X. */
728 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
730 /* Enumeration returned by some move_it_.* functions internally. */
732 enum move_it_result
734 /* Not used. Undefined value. */
735 MOVE_UNDEFINED,
737 /* Move ended at the requested buffer position or ZV. */
738 MOVE_POS_MATCH_OR_ZV,
740 /* Move ended at the requested X pixel position. */
741 MOVE_X_REACHED,
743 /* Move within a line ended at the end of a line that must be
744 continued. */
745 MOVE_LINE_CONTINUED,
747 /* Move within a line ended at the end of a line that would
748 be displayed truncated. */
749 MOVE_LINE_TRUNCATED,
751 /* Move within a line ended at a line end. */
752 MOVE_NEWLINE_OR_CR
755 /* This counter is used to clear the face cache every once in a while
756 in redisplay_internal. It is incremented for each redisplay.
757 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
758 cleared. */
760 #define CLEAR_FACE_CACHE_COUNT 500
761 static int clear_face_cache_count;
763 /* Similarly for the image cache. */
765 #ifdef HAVE_WINDOW_SYSTEM
766 #define CLEAR_IMAGE_CACHE_COUNT 101
767 static int clear_image_cache_count;
769 /* Null glyph slice */
770 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
771 #endif
773 /* True while redisplay_internal is in progress. */
775 bool redisplaying_p;
777 static Lisp_Object Qinhibit_free_realized_faces;
778 static Lisp_Object Qmode_line_default_help_echo;
780 /* If a string, XTread_socket generates an event to display that string.
781 (The display is done in read_char.) */
783 Lisp_Object help_echo_string;
784 Lisp_Object help_echo_window;
785 Lisp_Object help_echo_object;
786 ptrdiff_t help_echo_pos;
788 /* Temporary variable for XTread_socket. */
790 Lisp_Object previous_help_echo_string;
792 /* Platform-independent portion of hourglass implementation. */
794 /* Non-zero means an hourglass cursor is currently shown. */
795 int hourglass_shown_p;
797 /* If non-null, an asynchronous timer that, when it expires, displays
798 an hourglass cursor on all frames. */
799 struct atimer *hourglass_atimer;
801 /* Name of the face used to display glyphless characters. */
802 Lisp_Object Qglyphless_char;
804 /* Symbol for the purpose of Vglyphless_char_display. */
805 static Lisp_Object Qglyphless_char_display;
807 /* Method symbols for Vglyphless_char_display. */
808 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
810 /* Default pixel width of `thin-space' display method. */
811 #define THIN_SPACE_WIDTH 1
813 /* Default number of seconds to wait before displaying an hourglass
814 cursor. */
815 #define DEFAULT_HOURGLASS_DELAY 1
818 /* Function prototypes. */
820 static void setup_for_ellipsis (struct it *, int);
821 static void set_iterator_to_next (struct it *, int);
822 static void mark_window_display_accurate_1 (struct window *, int);
823 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
824 static int display_prop_string_p (Lisp_Object, Lisp_Object);
825 static int cursor_row_p (struct glyph_row *);
826 static int redisplay_mode_lines (Lisp_Object, int);
827 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
829 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
831 static void handle_line_prefix (struct it *);
833 static void pint2str (char *, int, ptrdiff_t);
834 static void pint2hrstr (char *, int, ptrdiff_t);
835 static struct text_pos run_window_scroll_functions (Lisp_Object,
836 struct text_pos);
837 static void reconsider_clip_changes (struct window *, struct buffer *);
838 static int text_outside_line_unchanged_p (struct window *,
839 ptrdiff_t, ptrdiff_t);
840 static void store_mode_line_noprop_char (char);
841 static int store_mode_line_noprop (const char *, int, int);
842 static void handle_stop (struct it *);
843 static void handle_stop_backwards (struct it *, ptrdiff_t);
844 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
845 static void ensure_echo_area_buffers (void);
846 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
847 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
848 static int with_echo_area_buffer (struct window *, int,
849 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
850 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
851 static void clear_garbaged_frames (void);
852 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static void pop_message (void);
854 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
855 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
856 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
857 static int display_echo_area (struct window *);
858 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
859 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
860 static Lisp_Object unwind_redisplay (Lisp_Object);
861 static int string_char_and_length (const unsigned char *, int *);
862 static struct text_pos display_prop_end (struct it *, Lisp_Object,
863 struct text_pos);
864 static int compute_window_start_on_continuation_line (struct window *);
865 static void insert_left_trunc_glyphs (struct it *);
866 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
867 Lisp_Object);
868 static void extend_face_to_end_of_line (struct it *);
869 static int append_space_for_newline (struct it *, int);
870 static int cursor_row_fully_visible_p (struct window *, int, int);
871 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
872 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
873 static int trailing_whitespace_p (ptrdiff_t);
874 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
875 static void push_it (struct it *, struct text_pos *);
876 static void iterate_out_of_display_property (struct it *);
877 static void pop_it (struct it *);
878 static void sync_frame_with_window_matrix_rows (struct window *);
879 static void select_frame_for_redisplay (Lisp_Object);
880 static void redisplay_internal (void);
881 static int echo_area_display (int);
882 static void redisplay_windows (Lisp_Object);
883 static void redisplay_window (Lisp_Object, int);
884 static Lisp_Object redisplay_window_error (Lisp_Object);
885 static Lisp_Object redisplay_window_0 (Lisp_Object);
886 static Lisp_Object redisplay_window_1 (Lisp_Object);
887 static int set_cursor_from_row (struct window *, struct glyph_row *,
888 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
889 int, int);
890 static int update_menu_bar (struct frame *, int, int);
891 static int try_window_reusing_current_matrix (struct window *);
892 static int try_window_id (struct window *);
893 static int display_line (struct it *);
894 static int display_mode_lines (struct window *);
895 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
896 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
897 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
898 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
899 static void display_menu_bar (struct window *);
900 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
901 ptrdiff_t *);
902 static int display_string (const char *, Lisp_Object, Lisp_Object,
903 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
904 static void compute_line_metrics (struct it *);
905 static void run_redisplay_end_trigger_hook (struct it *);
906 static int get_overlay_strings (struct it *, ptrdiff_t);
907 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
908 static void next_overlay_string (struct it *);
909 static void reseat (struct it *, struct text_pos, int);
910 static void reseat_1 (struct it *, struct text_pos, int);
911 static void back_to_previous_visible_line_start (struct it *);
912 void reseat_at_previous_visible_line_start (struct it *);
913 static void reseat_at_next_visible_line_start (struct it *, int);
914 static int next_element_from_ellipsis (struct it *);
915 static int next_element_from_display_vector (struct it *);
916 static int next_element_from_string (struct it *);
917 static int next_element_from_c_string (struct it *);
918 static int next_element_from_buffer (struct it *);
919 static int next_element_from_composition (struct it *);
920 static int next_element_from_image (struct it *);
921 static int next_element_from_stretch (struct it *);
922 static void load_overlay_strings (struct it *, ptrdiff_t);
923 static int init_from_display_pos (struct it *, struct window *,
924 struct display_pos *);
925 static void reseat_to_string (struct it *, const char *,
926 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
927 static int get_next_display_element (struct it *);
928 static enum move_it_result
929 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
930 enum move_operation_enum);
931 void move_it_vertically_backward (struct it *, int);
932 static void get_visually_first_element (struct it *);
933 static void init_to_row_start (struct it *, struct window *,
934 struct glyph_row *);
935 static int init_to_row_end (struct it *, struct window *,
936 struct glyph_row *);
937 static void back_to_previous_line_start (struct it *);
938 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
939 static struct text_pos string_pos_nchars_ahead (struct text_pos,
940 Lisp_Object, ptrdiff_t);
941 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
942 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
943 static ptrdiff_t number_of_chars (const char *, int);
944 static void compute_stop_pos (struct it *);
945 static void compute_string_pos (struct text_pos *, struct text_pos,
946 Lisp_Object);
947 static int face_before_or_after_it_pos (struct it *, int);
948 static ptrdiff_t next_overlay_change (ptrdiff_t);
949 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
950 Lisp_Object, struct text_pos *, ptrdiff_t, int);
951 static int handle_single_display_spec (struct it *, Lisp_Object,
952 Lisp_Object, Lisp_Object,
953 struct text_pos *, ptrdiff_t, int, int);
954 static int underlying_face_id (struct it *);
955 static int in_ellipses_for_invisible_text_p (struct display_pos *,
956 struct window *);
958 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
959 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
961 #ifdef HAVE_WINDOW_SYSTEM
963 static void x_consider_frame_title (Lisp_Object);
964 static int tool_bar_lines_needed (struct frame *, int *);
965 static void update_tool_bar (struct frame *, int);
966 static void build_desired_tool_bar_string (struct frame *f);
967 static int redisplay_tool_bar (struct frame *);
968 static void display_tool_bar_line (struct it *, int);
969 static void notice_overwritten_cursor (struct window *,
970 enum glyph_row_area,
971 int, int, int, int);
972 static void append_stretch_glyph (struct it *, Lisp_Object,
973 int, int, int);
976 #endif /* HAVE_WINDOW_SYSTEM */
978 static void produce_special_glyphs (struct it *, enum display_element_type);
979 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
980 static int coords_in_mouse_face_p (struct window *, int, int);
984 /***********************************************************************
985 Window display dimensions
986 ***********************************************************************/
988 /* Return the bottom boundary y-position for text lines in window W.
989 This is the first y position at which a line cannot start.
990 It is relative to the top of the window.
992 This is the height of W minus the height of a mode line, if any. */
995 window_text_bottom_y (struct window *w)
997 int height = WINDOW_TOTAL_HEIGHT (w);
999 if (WINDOW_WANTS_MODELINE_P (w))
1000 height -= CURRENT_MODE_LINE_HEIGHT (w);
1001 return height;
1004 /* Return the pixel width of display area AREA of window W. AREA < 0
1005 means return the total width of W, not including fringes to
1006 the left and right of the window. */
1009 window_box_width (struct window *w, int area)
1011 int cols = XFASTINT (w->total_cols);
1012 int pixels = 0;
1014 if (!w->pseudo_window_p)
1016 cols -= WINDOW_SCROLL_BAR_COLS (w);
1018 if (area == TEXT_AREA)
1020 if (INTEGERP (w->left_margin_cols))
1021 cols -= XFASTINT (w->left_margin_cols);
1022 if (INTEGERP (w->right_margin_cols))
1023 cols -= XFASTINT (w->right_margin_cols);
1024 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1026 else if (area == LEFT_MARGIN_AREA)
1028 cols = (INTEGERP (w->left_margin_cols)
1029 ? XFASTINT (w->left_margin_cols) : 0);
1030 pixels = 0;
1032 else if (area == RIGHT_MARGIN_AREA)
1034 cols = (INTEGERP (w->right_margin_cols)
1035 ? XFASTINT (w->right_margin_cols) : 0);
1036 pixels = 0;
1040 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1044 /* Return the pixel height of the display area of window W, not
1045 including mode lines of W, if any. */
1048 window_box_height (struct window *w)
1050 struct frame *f = XFRAME (w->frame);
1051 int height = WINDOW_TOTAL_HEIGHT (w);
1053 eassert (height >= 0);
1055 /* Note: the code below that determines the mode-line/header-line
1056 height is essentially the same as that contained in the macro
1057 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1058 the appropriate glyph row has its `mode_line_p' flag set,
1059 and if it doesn't, uses estimate_mode_line_height instead. */
1061 if (WINDOW_WANTS_MODELINE_P (w))
1063 struct glyph_row *ml_row
1064 = (w->current_matrix && w->current_matrix->rows
1065 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1066 : 0);
1067 if (ml_row && ml_row->mode_line_p)
1068 height -= ml_row->height;
1069 else
1070 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1073 if (WINDOW_WANTS_HEADER_LINE_P (w))
1075 struct glyph_row *hl_row
1076 = (w->current_matrix && w->current_matrix->rows
1077 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1078 : 0);
1079 if (hl_row && hl_row->mode_line_p)
1080 height -= hl_row->height;
1081 else
1082 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1085 /* With a very small font and a mode-line that's taller than
1086 default, we might end up with a negative height. */
1087 return max (0, height);
1090 /* Return the window-relative coordinate of the left edge of display
1091 area AREA of window W. AREA < 0 means return the left edge of the
1092 whole window, to the right of the left fringe of W. */
1095 window_box_left_offset (struct window *w, int area)
1097 int x;
1099 if (w->pseudo_window_p)
1100 return 0;
1102 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1104 if (area == TEXT_AREA)
1105 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1106 + window_box_width (w, LEFT_MARGIN_AREA));
1107 else if (area == RIGHT_MARGIN_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA)
1110 + window_box_width (w, TEXT_AREA)
1111 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1113 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1114 else if (area == LEFT_MARGIN_AREA
1115 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1116 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1118 return x;
1122 /* Return the window-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1127 window_box_right_offset (struct window *w, int area)
1129 return window_box_left_offset (w, area) + window_box_width (w, area);
1132 /* Return the frame-relative coordinate of the left edge of display
1133 area AREA of window W. AREA < 0 means return the left edge of the
1134 whole window, to the right of the left fringe of W. */
1137 window_box_left (struct window *w, int area)
1139 struct frame *f = XFRAME (w->frame);
1140 int x;
1142 if (w->pseudo_window_p)
1143 return FRAME_INTERNAL_BORDER_WIDTH (f);
1145 x = (WINDOW_LEFT_EDGE_X (w)
1146 + window_box_left_offset (w, area));
1148 return x;
1152 /* Return the frame-relative coordinate of the right edge of display
1153 area AREA of window W. AREA < 0 means return the right edge of the
1154 whole window, to the left of the right fringe of W. */
1157 window_box_right (struct window *w, int area)
1159 return window_box_left (w, area) + window_box_width (w, area);
1162 /* Get the bounding box of the display area AREA of window W, without
1163 mode lines, in frame-relative coordinates. AREA < 0 means the
1164 whole window, not including the left and right fringes of
1165 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1166 coordinates of the upper-left corner of the box. Return in
1167 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1169 void
1170 window_box (struct window *w, int area, int *box_x, int *box_y,
1171 int *box_width, int *box_height)
1173 if (box_width)
1174 *box_width = window_box_width (w, area);
1175 if (box_height)
1176 *box_height = window_box_height (w);
1177 if (box_x)
1178 *box_x = window_box_left (w, area);
1179 if (box_y)
1181 *box_y = WINDOW_TOP_EDGE_Y (w);
1182 if (WINDOW_WANTS_HEADER_LINE_P (w))
1183 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1188 /* Get the bounding box of the display area AREA of window W, without
1189 mode lines. AREA < 0 means the whole window, not including the
1190 left and right fringe of the window. Return in *TOP_LEFT_X
1191 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1192 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1193 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1194 box. */
1196 static void
1197 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1198 int *bottom_right_x, int *bottom_right_y)
1200 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1201 bottom_right_y);
1202 *bottom_right_x += *top_left_x;
1203 *bottom_right_y += *top_left_y;
1208 /***********************************************************************
1209 Utilities
1210 ***********************************************************************/
1212 /* Return the bottom y-position of the line the iterator IT is in.
1213 This can modify IT's settings. */
1216 line_bottom_y (struct it *it)
1218 int line_height = it->max_ascent + it->max_descent;
1219 int line_top_y = it->current_y;
1221 if (line_height == 0)
1223 if (last_height)
1224 line_height = last_height;
1225 else if (IT_CHARPOS (*it) < ZV)
1227 move_it_by_lines (it, 1);
1228 line_height = (it->max_ascent || it->max_descent
1229 ? it->max_ascent + it->max_descent
1230 : last_height);
1232 else
1234 struct glyph_row *row = it->glyph_row;
1236 /* Use the default character height. */
1237 it->glyph_row = NULL;
1238 it->what = IT_CHARACTER;
1239 it->c = ' ';
1240 it->len = 1;
1241 PRODUCE_GLYPHS (it);
1242 line_height = it->ascent + it->descent;
1243 it->glyph_row = row;
1247 return line_top_y + line_height;
1250 /* Subroutine of pos_visible_p below. Extracts a display string, if
1251 any, from the display spec given as its argument. */
1252 static Lisp_Object
1253 string_from_display_spec (Lisp_Object spec)
1255 if (CONSP (spec))
1257 while (CONSP (spec))
1259 if (STRINGP (XCAR (spec)))
1260 return XCAR (spec);
1261 spec = XCDR (spec);
1264 else if (VECTORP (spec))
1266 ptrdiff_t i;
1268 for (i = 0; i < ASIZE (spec); i++)
1270 if (STRINGP (AREF (spec, i)))
1271 return AREF (spec, i);
1273 return Qnil;
1276 return spec;
1280 /* Limit insanely large values of W->hscroll on frame F to the largest
1281 value that will still prevent first_visible_x and last_visible_x of
1282 'struct it' from overflowing an int. */
1283 static int
1284 window_hscroll_limited (struct window *w, struct frame *f)
1286 ptrdiff_t window_hscroll = w->hscroll;
1287 int window_text_width = window_box_width (w, TEXT_AREA);
1288 int colwidth = FRAME_COLUMN_WIDTH (f);
1290 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1291 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1293 return window_hscroll;
1296 /* Return 1 if position CHARPOS is visible in window W.
1297 CHARPOS < 0 means return info about WINDOW_END position.
1298 If visible, set *X and *Y to pixel coordinates of top left corner.
1299 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1300 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1303 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1304 int *rtop, int *rbot, int *rowh, int *vpos)
1306 struct it it;
1307 void *itdata = bidi_shelve_cache ();
1308 struct text_pos top;
1309 int visible_p = 0;
1310 struct buffer *old_buffer = NULL;
1312 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1313 return visible_p;
1315 if (XBUFFER (w->buffer) != current_buffer)
1317 old_buffer = current_buffer;
1318 set_buffer_internal_1 (XBUFFER (w->buffer));
1321 SET_TEXT_POS_FROM_MARKER (top, w->start);
1322 /* Scrolling a minibuffer window via scroll bar when the echo area
1323 shows long text sometimes resets the minibuffer contents behind
1324 our backs. */
1325 if (CHARPOS (top) > ZV)
1326 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1328 /* Compute exact mode line heights. */
1329 if (WINDOW_WANTS_MODELINE_P (w))
1330 current_mode_line_height
1331 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1332 BVAR (current_buffer, mode_line_format));
1334 if (WINDOW_WANTS_HEADER_LINE_P (w))
1335 current_header_line_height
1336 = display_mode_line (w, HEADER_LINE_FACE_ID,
1337 BVAR (current_buffer, header_line_format));
1339 start_display (&it, w, top);
1340 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1341 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1343 if (charpos >= 0
1344 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1345 && IT_CHARPOS (it) >= charpos)
1346 /* When scanning backwards under bidi iteration, move_it_to
1347 stops at or _before_ CHARPOS, because it stops at or to
1348 the _right_ of the character at CHARPOS. */
1349 || (it.bidi_p && it.bidi_it.scan_dir == -1
1350 && IT_CHARPOS (it) <= charpos)))
1352 /* We have reached CHARPOS, or passed it. How the call to
1353 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1354 or covered by a display property, move_it_to stops at the end
1355 of the invisible text, to the right of CHARPOS. (ii) If
1356 CHARPOS is in a display vector, move_it_to stops on its last
1357 glyph. */
1358 int top_x = it.current_x;
1359 int top_y = it.current_y;
1360 /* Calling line_bottom_y may change it.method, it.position, etc. */
1361 enum it_method it_method = it.method;
1362 int bottom_y = (last_height = 0, line_bottom_y (&it));
1363 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1365 if (top_y < window_top_y)
1366 visible_p = bottom_y > window_top_y;
1367 else if (top_y < it.last_visible_y)
1368 visible_p = 1;
1369 if (bottom_y >= it.last_visible_y
1370 && it.bidi_p && it.bidi_it.scan_dir == -1
1371 && IT_CHARPOS (it) < charpos)
1373 /* When the last line of the window is scanned backwards
1374 under bidi iteration, we could be duped into thinking
1375 that we have passed CHARPOS, when in fact move_it_to
1376 simply stopped short of CHARPOS because it reached
1377 last_visible_y. To see if that's what happened, we call
1378 move_it_to again with a slightly larger vertical limit,
1379 and see if it actually moved vertically; if it did, we
1380 didn't really reach CHARPOS, which is beyond window end. */
1381 struct it save_it = it;
1382 /* Why 10? because we don't know how many canonical lines
1383 will the height of the next line(s) be. So we guess. */
1384 int ten_more_lines =
1385 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1387 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1388 MOVE_TO_POS | MOVE_TO_Y);
1389 if (it.current_y > top_y)
1390 visible_p = 0;
1392 it = save_it;
1394 if (visible_p)
1396 if (it_method == GET_FROM_DISPLAY_VECTOR)
1398 /* We stopped on the last glyph of a display vector.
1399 Try and recompute. Hack alert! */
1400 if (charpos < 2 || top.charpos >= charpos)
1401 top_x = it.glyph_row->x;
1402 else
1404 struct it it2;
1405 start_display (&it2, w, top);
1406 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1407 get_next_display_element (&it2);
1408 PRODUCE_GLYPHS (&it2);
1409 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1410 || it2.current_x > it2.last_visible_x)
1411 top_x = it.glyph_row->x;
1412 else
1414 top_x = it2.current_x;
1415 top_y = it2.current_y;
1419 else if (IT_CHARPOS (it) != charpos)
1421 Lisp_Object cpos = make_number (charpos);
1422 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1423 Lisp_Object string = string_from_display_spec (spec);
1424 int newline_in_string = 0;
1426 if (STRINGP (string))
1428 const char *s = SSDATA (string);
1429 const char *e = s + SBYTES (string);
1430 while (s < e)
1432 if (*s++ == '\n')
1434 newline_in_string = 1;
1435 break;
1439 /* The tricky code below is needed because there's a
1440 discrepancy between move_it_to and how we set cursor
1441 when the display line ends in a newline from a
1442 display string. move_it_to will stop _after_ such
1443 display strings, whereas set_cursor_from_row
1444 conspires with cursor_row_p to place the cursor on
1445 the first glyph produced from the display string. */
1447 /* We have overshoot PT because it is covered by a
1448 display property whose value is a string. If the
1449 string includes embedded newlines, we are also in the
1450 wrong display line. Backtrack to the correct line,
1451 where the display string begins. */
1452 if (newline_in_string)
1454 Lisp_Object startpos, endpos;
1455 EMACS_INT start, end;
1456 struct it it3;
1457 int it3_moved;
1459 /* Find the first and the last buffer positions
1460 covered by the display string. */
1461 endpos =
1462 Fnext_single_char_property_change (cpos, Qdisplay,
1463 Qnil, Qnil);
1464 startpos =
1465 Fprevious_single_char_property_change (endpos, Qdisplay,
1466 Qnil, Qnil);
1467 start = XFASTINT (startpos);
1468 end = XFASTINT (endpos);
1469 /* Move to the last buffer position before the
1470 display property. */
1471 start_display (&it3, w, top);
1472 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1473 /* Move forward one more line if the position before
1474 the display string is a newline or if it is the
1475 rightmost character on a line that is
1476 continued or word-wrapped. */
1477 if (it3.method == GET_FROM_BUFFER
1478 && it3.c == '\n')
1479 move_it_by_lines (&it3, 1);
1480 else if (move_it_in_display_line_to (&it3, -1,
1481 it3.current_x
1482 + it3.pixel_width,
1483 MOVE_TO_X)
1484 == MOVE_LINE_CONTINUED)
1486 move_it_by_lines (&it3, 1);
1487 /* When we are under word-wrap, the #$@%!
1488 move_it_by_lines moves 2 lines, so we need to
1489 fix that up. */
1490 if (it3.line_wrap == WORD_WRAP)
1491 move_it_by_lines (&it3, -1);
1494 /* Record the vertical coordinate of the display
1495 line where we wound up. */
1496 top_y = it3.current_y;
1497 if (it3.bidi_p)
1499 /* When characters are reordered for display,
1500 the character displayed to the left of the
1501 display string could be _after_ the display
1502 property in the logical order. Use the
1503 smallest vertical position of these two. */
1504 start_display (&it3, w, top);
1505 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1506 if (it3.current_y < top_y)
1507 top_y = it3.current_y;
1509 /* Move from the top of the window to the beginning
1510 of the display line where the display string
1511 begins. */
1512 start_display (&it3, w, top);
1513 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1514 /* If it3_moved stays zero after the 'while' loop
1515 below, that means we already were at a newline
1516 before the loop (e.g., the display string begins
1517 with a newline), so we don't need to (and cannot)
1518 inspect the glyphs of it3.glyph_row, because
1519 PRODUCE_GLYPHS will not produce anything for a
1520 newline, and thus it3.glyph_row stays at its
1521 stale content it got at top of the window. */
1522 it3_moved = 0;
1523 /* Finally, advance the iterator until we hit the
1524 first display element whose character position is
1525 CHARPOS, or until the first newline from the
1526 display string, which signals the end of the
1527 display line. */
1528 while (get_next_display_element (&it3))
1530 PRODUCE_GLYPHS (&it3);
1531 if (IT_CHARPOS (it3) == charpos
1532 || ITERATOR_AT_END_OF_LINE_P (&it3))
1533 break;
1534 it3_moved = 1;
1535 set_iterator_to_next (&it3, 0);
1537 top_x = it3.current_x - it3.pixel_width;
1538 /* Normally, we would exit the above loop because we
1539 found the display element whose character
1540 position is CHARPOS. For the contingency that we
1541 didn't, and stopped at the first newline from the
1542 display string, move back over the glyphs
1543 produced from the string, until we find the
1544 rightmost glyph not from the string. */
1545 if (it3_moved
1546 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1548 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1549 + it3.glyph_row->used[TEXT_AREA];
1551 while (EQ ((g - 1)->object, string))
1553 --g;
1554 top_x -= g->pixel_width;
1556 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1557 + it3.glyph_row->used[TEXT_AREA]);
1562 *x = top_x;
1563 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1564 *rtop = max (0, window_top_y - top_y);
1565 *rbot = max (0, bottom_y - it.last_visible_y);
1566 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1567 - max (top_y, window_top_y)));
1568 *vpos = it.vpos;
1571 else
1573 /* We were asked to provide info about WINDOW_END. */
1574 struct it it2;
1575 void *it2data = NULL;
1577 SAVE_IT (it2, it, it2data);
1578 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1579 move_it_by_lines (&it, 1);
1580 if (charpos < IT_CHARPOS (it)
1581 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1583 visible_p = 1;
1584 RESTORE_IT (&it2, &it2, it2data);
1585 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1586 *x = it2.current_x;
1587 *y = it2.current_y + it2.max_ascent - it2.ascent;
1588 *rtop = max (0, -it2.current_y);
1589 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1590 - it.last_visible_y));
1591 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1592 it.last_visible_y)
1593 - max (it2.current_y,
1594 WINDOW_HEADER_LINE_HEIGHT (w))));
1595 *vpos = it2.vpos;
1597 else
1598 bidi_unshelve_cache (it2data, 1);
1600 bidi_unshelve_cache (itdata, 0);
1602 if (old_buffer)
1603 set_buffer_internal_1 (old_buffer);
1605 current_header_line_height = current_mode_line_height = -1;
1607 if (visible_p && w->hscroll > 0)
1608 *x -=
1609 window_hscroll_limited (w, WINDOW_XFRAME (w))
1610 * WINDOW_FRAME_COLUMN_WIDTH (w);
1612 #if 0
1613 /* Debugging code. */
1614 if (visible_p)
1615 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1616 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1617 else
1618 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1619 #endif
1621 return visible_p;
1625 /* Return the next character from STR. Return in *LEN the length of
1626 the character. This is like STRING_CHAR_AND_LENGTH but never
1627 returns an invalid character. If we find one, we return a `?', but
1628 with the length of the invalid character. */
1630 static int
1631 string_char_and_length (const unsigned char *str, int *len)
1633 int c;
1635 c = STRING_CHAR_AND_LENGTH (str, *len);
1636 if (!CHAR_VALID_P (c))
1637 /* We may not change the length here because other places in Emacs
1638 don't use this function, i.e. they silently accept invalid
1639 characters. */
1640 c = '?';
1642 return c;
1647 /* Given a position POS containing a valid character and byte position
1648 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1650 static struct text_pos
1651 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1653 eassert (STRINGP (string) && nchars >= 0);
1655 if (STRING_MULTIBYTE (string))
1657 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1658 int len;
1660 while (nchars--)
1662 string_char_and_length (p, &len);
1663 p += len;
1664 CHARPOS (pos) += 1;
1665 BYTEPOS (pos) += len;
1668 else
1669 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1671 return pos;
1675 /* Value is the text position, i.e. character and byte position,
1676 for character position CHARPOS in STRING. */
1678 static struct text_pos
1679 string_pos (ptrdiff_t charpos, Lisp_Object string)
1681 struct text_pos pos;
1682 eassert (STRINGP (string));
1683 eassert (charpos >= 0);
1684 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1685 return pos;
1689 /* Value is a text position, i.e. character and byte position, for
1690 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1691 means recognize multibyte characters. */
1693 static struct text_pos
1694 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1696 struct text_pos pos;
1698 eassert (s != NULL);
1699 eassert (charpos >= 0);
1701 if (multibyte_p)
1703 int len;
1705 SET_TEXT_POS (pos, 0, 0);
1706 while (charpos--)
1708 string_char_and_length ((const unsigned char *) s, &len);
1709 s += len;
1710 CHARPOS (pos) += 1;
1711 BYTEPOS (pos) += len;
1714 else
1715 SET_TEXT_POS (pos, charpos, charpos);
1717 return pos;
1721 /* Value is the number of characters in C string S. MULTIBYTE_P
1722 non-zero means recognize multibyte characters. */
1724 static ptrdiff_t
1725 number_of_chars (const char *s, int multibyte_p)
1727 ptrdiff_t nchars;
1729 if (multibyte_p)
1731 ptrdiff_t rest = strlen (s);
1732 int len;
1733 const unsigned char *p = (const unsigned char *) s;
1735 for (nchars = 0; rest > 0; ++nchars)
1737 string_char_and_length (p, &len);
1738 rest -= len, p += len;
1741 else
1742 nchars = strlen (s);
1744 return nchars;
1748 /* Compute byte position NEWPOS->bytepos corresponding to
1749 NEWPOS->charpos. POS is a known position in string STRING.
1750 NEWPOS->charpos must be >= POS.charpos. */
1752 static void
1753 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1755 eassert (STRINGP (string));
1756 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1758 if (STRING_MULTIBYTE (string))
1759 *newpos = string_pos_nchars_ahead (pos, string,
1760 CHARPOS (*newpos) - CHARPOS (pos));
1761 else
1762 BYTEPOS (*newpos) = CHARPOS (*newpos);
1765 /* EXPORT:
1766 Return an estimation of the pixel height of mode or header lines on
1767 frame F. FACE_ID specifies what line's height to estimate. */
1770 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1772 #ifdef HAVE_WINDOW_SYSTEM
1773 if (FRAME_WINDOW_P (f))
1775 int height = FONT_HEIGHT (FRAME_FONT (f));
1777 /* This function is called so early when Emacs starts that the face
1778 cache and mode line face are not yet initialized. */
1779 if (FRAME_FACE_CACHE (f))
1781 struct face *face = FACE_FROM_ID (f, face_id);
1782 if (face)
1784 if (face->font)
1785 height = FONT_HEIGHT (face->font);
1786 if (face->box_line_width > 0)
1787 height += 2 * face->box_line_width;
1791 return height;
1793 #endif
1795 return 1;
1798 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1799 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1800 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1801 not force the value into range. */
1803 void
1804 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1805 int *x, int *y, NativeRectangle *bounds, int noclip)
1808 #ifdef HAVE_WINDOW_SYSTEM
1809 if (FRAME_WINDOW_P (f))
1811 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1812 even for negative values. */
1813 if (pix_x < 0)
1814 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1815 if (pix_y < 0)
1816 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1818 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1819 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1821 if (bounds)
1822 STORE_NATIVE_RECT (*bounds,
1823 FRAME_COL_TO_PIXEL_X (f, pix_x),
1824 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1825 FRAME_COLUMN_WIDTH (f) - 1,
1826 FRAME_LINE_HEIGHT (f) - 1);
1828 if (!noclip)
1830 if (pix_x < 0)
1831 pix_x = 0;
1832 else if (pix_x > FRAME_TOTAL_COLS (f))
1833 pix_x = FRAME_TOTAL_COLS (f);
1835 if (pix_y < 0)
1836 pix_y = 0;
1837 else if (pix_y > FRAME_LINES (f))
1838 pix_y = FRAME_LINES (f);
1841 #endif
1843 *x = pix_x;
1844 *y = pix_y;
1848 /* Find the glyph under window-relative coordinates X/Y in window W.
1849 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1850 strings. Return in *HPOS and *VPOS the row and column number of
1851 the glyph found. Return in *AREA the glyph area containing X.
1852 Value is a pointer to the glyph found or null if X/Y is not on
1853 text, or we can't tell because W's current matrix is not up to
1854 date. */
1856 static
1857 struct glyph *
1858 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1859 int *dx, int *dy, int *area)
1861 struct glyph *glyph, *end;
1862 struct glyph_row *row = NULL;
1863 int x0, i;
1865 /* Find row containing Y. Give up if some row is not enabled. */
1866 for (i = 0; i < w->current_matrix->nrows; ++i)
1868 row = MATRIX_ROW (w->current_matrix, i);
1869 if (!row->enabled_p)
1870 return NULL;
1871 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1872 break;
1875 *vpos = i;
1876 *hpos = 0;
1878 /* Give up if Y is not in the window. */
1879 if (i == w->current_matrix->nrows)
1880 return NULL;
1882 /* Get the glyph area containing X. */
1883 if (w->pseudo_window_p)
1885 *area = TEXT_AREA;
1886 x0 = 0;
1888 else
1890 if (x < window_box_left_offset (w, TEXT_AREA))
1892 *area = LEFT_MARGIN_AREA;
1893 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1895 else if (x < window_box_right_offset (w, TEXT_AREA))
1897 *area = TEXT_AREA;
1898 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1900 else
1902 *area = RIGHT_MARGIN_AREA;
1903 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1907 /* Find glyph containing X. */
1908 glyph = row->glyphs[*area];
1909 end = glyph + row->used[*area];
1910 x -= x0;
1911 while (glyph < end && x >= glyph->pixel_width)
1913 x -= glyph->pixel_width;
1914 ++glyph;
1917 if (glyph == end)
1918 return NULL;
1920 if (dx)
1922 *dx = x;
1923 *dy = y - (row->y + row->ascent - glyph->ascent);
1926 *hpos = glyph - row->glyphs[*area];
1927 return glyph;
1930 /* Convert frame-relative x/y to coordinates relative to window W.
1931 Takes pseudo-windows into account. */
1933 static void
1934 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1936 if (w->pseudo_window_p)
1938 /* A pseudo-window is always full-width, and starts at the
1939 left edge of the frame, plus a frame border. */
1940 struct frame *f = XFRAME (w->frame);
1941 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1942 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1944 else
1946 *x -= WINDOW_LEFT_EDGE_X (w);
1947 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1951 #ifdef HAVE_WINDOW_SYSTEM
1953 /* EXPORT:
1954 Return in RECTS[] at most N clipping rectangles for glyph string S.
1955 Return the number of stored rectangles. */
1958 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1960 XRectangle r;
1962 if (n <= 0)
1963 return 0;
1965 if (s->row->full_width_p)
1967 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1968 r.x = WINDOW_LEFT_EDGE_X (s->w);
1969 r.width = WINDOW_TOTAL_WIDTH (s->w);
1971 /* Unless displaying a mode or menu bar line, which are always
1972 fully visible, clip to the visible part of the row. */
1973 if (s->w->pseudo_window_p)
1974 r.height = s->row->visible_height;
1975 else
1976 r.height = s->height;
1978 else
1980 /* This is a text line that may be partially visible. */
1981 r.x = window_box_left (s->w, s->area);
1982 r.width = window_box_width (s->w, s->area);
1983 r.height = s->row->visible_height;
1986 if (s->clip_head)
1987 if (r.x < s->clip_head->x)
1989 if (r.width >= s->clip_head->x - r.x)
1990 r.width -= s->clip_head->x - r.x;
1991 else
1992 r.width = 0;
1993 r.x = s->clip_head->x;
1995 if (s->clip_tail)
1996 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1998 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1999 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2000 else
2001 r.width = 0;
2004 /* If S draws overlapping rows, it's sufficient to use the top and
2005 bottom of the window for clipping because this glyph string
2006 intentionally draws over other lines. */
2007 if (s->for_overlaps)
2009 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2010 r.height = window_text_bottom_y (s->w) - r.y;
2012 /* Alas, the above simple strategy does not work for the
2013 environments with anti-aliased text: if the same text is
2014 drawn onto the same place multiple times, it gets thicker.
2015 If the overlap we are processing is for the erased cursor, we
2016 take the intersection with the rectangle of the cursor. */
2017 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2019 XRectangle rc, r_save = r;
2021 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2022 rc.y = s->w->phys_cursor.y;
2023 rc.width = s->w->phys_cursor_width;
2024 rc.height = s->w->phys_cursor_height;
2026 x_intersect_rectangles (&r_save, &rc, &r);
2029 else
2031 /* Don't use S->y for clipping because it doesn't take partially
2032 visible lines into account. For example, it can be negative for
2033 partially visible lines at the top of a window. */
2034 if (!s->row->full_width_p
2035 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2036 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2037 else
2038 r.y = max (0, s->row->y);
2041 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2043 /* If drawing the cursor, don't let glyph draw outside its
2044 advertised boundaries. Cleartype does this under some circumstances. */
2045 if (s->hl == DRAW_CURSOR)
2047 struct glyph *glyph = s->first_glyph;
2048 int height, max_y;
2050 if (s->x > r.x)
2052 r.width -= s->x - r.x;
2053 r.x = s->x;
2055 r.width = min (r.width, glyph->pixel_width);
2057 /* If r.y is below window bottom, ensure that we still see a cursor. */
2058 height = min (glyph->ascent + glyph->descent,
2059 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2060 max_y = window_text_bottom_y (s->w) - height;
2061 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2062 if (s->ybase - glyph->ascent > max_y)
2064 r.y = max_y;
2065 r.height = height;
2067 else
2069 /* Don't draw cursor glyph taller than our actual glyph. */
2070 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2071 if (height < r.height)
2073 max_y = r.y + r.height;
2074 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2075 r.height = min (max_y - r.y, height);
2080 if (s->row->clip)
2082 XRectangle r_save = r;
2084 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2085 r.width = 0;
2088 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2089 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2091 #ifdef CONVERT_FROM_XRECT
2092 CONVERT_FROM_XRECT (r, *rects);
2093 #else
2094 *rects = r;
2095 #endif
2096 return 1;
2098 else
2100 /* If we are processing overlapping and allowed to return
2101 multiple clipping rectangles, we exclude the row of the glyph
2102 string from the clipping rectangle. This is to avoid drawing
2103 the same text on the environment with anti-aliasing. */
2104 #ifdef CONVERT_FROM_XRECT
2105 XRectangle rs[2];
2106 #else
2107 XRectangle *rs = rects;
2108 #endif
2109 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2111 if (s->for_overlaps & OVERLAPS_PRED)
2113 rs[i] = r;
2114 if (r.y + r.height > row_y)
2116 if (r.y < row_y)
2117 rs[i].height = row_y - r.y;
2118 else
2119 rs[i].height = 0;
2121 i++;
2123 if (s->for_overlaps & OVERLAPS_SUCC)
2125 rs[i] = r;
2126 if (r.y < row_y + s->row->visible_height)
2128 if (r.y + r.height > row_y + s->row->visible_height)
2130 rs[i].y = row_y + s->row->visible_height;
2131 rs[i].height = r.y + r.height - rs[i].y;
2133 else
2134 rs[i].height = 0;
2136 i++;
2139 n = i;
2140 #ifdef CONVERT_FROM_XRECT
2141 for (i = 0; i < n; i++)
2142 CONVERT_FROM_XRECT (rs[i], rects[i]);
2143 #endif
2144 return n;
2148 /* EXPORT:
2149 Return in *NR the clipping rectangle for glyph string S. */
2151 void
2152 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2154 get_glyph_string_clip_rects (s, nr, 1);
2158 /* EXPORT:
2159 Return the position and height of the phys cursor in window W.
2160 Set w->phys_cursor_width to width of phys cursor.
2163 void
2164 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2165 struct glyph *glyph, int *xp, int *yp, int *heightp)
2167 struct frame *f = XFRAME (WINDOW_FRAME (w));
2168 int x, y, wd, h, h0, y0;
2170 /* Compute the width of the rectangle to draw. If on a stretch
2171 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2172 rectangle as wide as the glyph, but use a canonical character
2173 width instead. */
2174 wd = glyph->pixel_width - 1;
2175 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2176 wd++; /* Why? */
2177 #endif
2179 x = w->phys_cursor.x;
2180 if (x < 0)
2182 wd += x;
2183 x = 0;
2186 if (glyph->type == STRETCH_GLYPH
2187 && !x_stretch_cursor_p)
2188 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2189 w->phys_cursor_width = wd;
2191 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2193 /* If y is below window bottom, ensure that we still see a cursor. */
2194 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2196 h = max (h0, glyph->ascent + glyph->descent);
2197 h0 = min (h0, glyph->ascent + glyph->descent);
2199 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2200 if (y < y0)
2202 h = max (h - (y0 - y) + 1, h0);
2203 y = y0 - 1;
2205 else
2207 y0 = window_text_bottom_y (w) - h0;
2208 if (y > y0)
2210 h += y - y0;
2211 y = y0;
2215 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2216 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2217 *heightp = h;
2221 * Remember which glyph the mouse is over.
2224 void
2225 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2227 Lisp_Object window;
2228 struct window *w;
2229 struct glyph_row *r, *gr, *end_row;
2230 enum window_part part;
2231 enum glyph_row_area area;
2232 int x, y, width, height;
2234 /* Try to determine frame pixel position and size of the glyph under
2235 frame pixel coordinates X/Y on frame F. */
2237 if (!f->glyphs_initialized_p
2238 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2239 NILP (window)))
2241 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2242 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2243 goto virtual_glyph;
2246 w = XWINDOW (window);
2247 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2248 height = WINDOW_FRAME_LINE_HEIGHT (w);
2250 x = window_relative_x_coord (w, part, gx);
2251 y = gy - WINDOW_TOP_EDGE_Y (w);
2253 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2254 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2256 if (w->pseudo_window_p)
2258 area = TEXT_AREA;
2259 part = ON_MODE_LINE; /* Don't adjust margin. */
2260 goto text_glyph;
2263 switch (part)
2265 case ON_LEFT_MARGIN:
2266 area = LEFT_MARGIN_AREA;
2267 goto text_glyph;
2269 case ON_RIGHT_MARGIN:
2270 area = RIGHT_MARGIN_AREA;
2271 goto text_glyph;
2273 case ON_HEADER_LINE:
2274 case ON_MODE_LINE:
2275 gr = (part == ON_HEADER_LINE
2276 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2277 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2278 gy = gr->y;
2279 area = TEXT_AREA;
2280 goto text_glyph_row_found;
2282 case ON_TEXT:
2283 area = TEXT_AREA;
2285 text_glyph:
2286 gr = 0; gy = 0;
2287 for (; r <= end_row && r->enabled_p; ++r)
2288 if (r->y + r->height > y)
2290 gr = r; gy = r->y;
2291 break;
2294 text_glyph_row_found:
2295 if (gr && gy <= y)
2297 struct glyph *g = gr->glyphs[area];
2298 struct glyph *end = g + gr->used[area];
2300 height = gr->height;
2301 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2302 if (gx + g->pixel_width > x)
2303 break;
2305 if (g < end)
2307 if (g->type == IMAGE_GLYPH)
2309 /* Don't remember when mouse is over image, as
2310 image may have hot-spots. */
2311 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2312 return;
2314 width = g->pixel_width;
2316 else
2318 /* Use nominal char spacing at end of line. */
2319 x -= gx;
2320 gx += (x / width) * width;
2323 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2324 gx += window_box_left_offset (w, area);
2326 else
2328 /* Use nominal line height at end of window. */
2329 gx = (x / width) * width;
2330 y -= gy;
2331 gy += (y / height) * height;
2333 break;
2335 case ON_LEFT_FRINGE:
2336 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2338 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2339 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2340 goto row_glyph;
2342 case ON_RIGHT_FRINGE:
2343 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2344 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2345 : window_box_right_offset (w, TEXT_AREA));
2346 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2347 goto row_glyph;
2349 case ON_SCROLL_BAR:
2350 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2352 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2353 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2354 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2355 : 0)));
2356 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2358 row_glyph:
2359 gr = 0, gy = 0;
2360 for (; r <= end_row && r->enabled_p; ++r)
2361 if (r->y + r->height > y)
2363 gr = r; gy = r->y;
2364 break;
2367 if (gr && gy <= y)
2368 height = gr->height;
2369 else
2371 /* Use nominal line height at end of window. */
2372 y -= gy;
2373 gy += (y / height) * height;
2375 break;
2377 default:
2379 virtual_glyph:
2380 /* If there is no glyph under the mouse, then we divide the screen
2381 into a grid of the smallest glyph in the frame, and use that
2382 as our "glyph". */
2384 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2385 round down even for negative values. */
2386 if (gx < 0)
2387 gx -= width - 1;
2388 if (gy < 0)
2389 gy -= height - 1;
2391 gx = (gx / width) * width;
2392 gy = (gy / height) * height;
2394 goto store_rect;
2397 gx += WINDOW_LEFT_EDGE_X (w);
2398 gy += WINDOW_TOP_EDGE_Y (w);
2400 store_rect:
2401 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2403 /* Visible feedback for debugging. */
2404 #if 0
2405 #if HAVE_X_WINDOWS
2406 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2407 f->output_data.x->normal_gc,
2408 gx, gy, width, height);
2409 #endif
2410 #endif
2414 #endif /* HAVE_WINDOW_SYSTEM */
2417 /***********************************************************************
2418 Lisp form evaluation
2419 ***********************************************************************/
2421 /* Error handler for safe_eval and safe_call. */
2423 static Lisp_Object
2424 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2426 add_to_log ("Error during redisplay: %S signaled %S",
2427 Flist (nargs, args), arg);
2428 return Qnil;
2431 /* Call function FUNC with the rest of NARGS - 1 arguments
2432 following. Return the result, or nil if something went
2433 wrong. Prevent redisplay during the evaluation. */
2435 Lisp_Object
2436 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2438 Lisp_Object val;
2440 if (inhibit_eval_during_redisplay)
2441 val = Qnil;
2442 else
2444 va_list ap;
2445 ptrdiff_t i;
2446 ptrdiff_t count = SPECPDL_INDEX ();
2447 struct gcpro gcpro1;
2448 Lisp_Object *args = alloca (nargs * word_size);
2450 args[0] = func;
2451 va_start (ap, func);
2452 for (i = 1; i < nargs; i++)
2453 args[i] = va_arg (ap, Lisp_Object);
2454 va_end (ap);
2456 GCPRO1 (args[0]);
2457 gcpro1.nvars = nargs;
2458 specbind (Qinhibit_redisplay, Qt);
2459 /* Use Qt to ensure debugger does not run,
2460 so there is no possibility of wanting to redisplay. */
2461 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2462 safe_eval_handler);
2463 UNGCPRO;
2464 val = unbind_to (count, val);
2467 return val;
2471 /* Call function FN with one argument ARG.
2472 Return the result, or nil if something went wrong. */
2474 Lisp_Object
2475 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2477 return safe_call (2, fn, arg);
2480 static Lisp_Object Qeval;
2482 Lisp_Object
2483 safe_eval (Lisp_Object sexpr)
2485 return safe_call1 (Qeval, sexpr);
2488 /* Call function FN with two arguments ARG1 and ARG2.
2489 Return the result, or nil if something went wrong. */
2491 Lisp_Object
2492 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2494 return safe_call (3, fn, arg1, arg2);
2499 /***********************************************************************
2500 Debugging
2501 ***********************************************************************/
2503 #if 0
2505 /* Define CHECK_IT to perform sanity checks on iterators.
2506 This is for debugging. It is too slow to do unconditionally. */
2508 static void
2509 check_it (struct it *it)
2511 if (it->method == GET_FROM_STRING)
2513 eassert (STRINGP (it->string));
2514 eassert (IT_STRING_CHARPOS (*it) >= 0);
2516 else
2518 eassert (IT_STRING_CHARPOS (*it) < 0);
2519 if (it->method == GET_FROM_BUFFER)
2521 /* Check that character and byte positions agree. */
2522 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2526 if (it->dpvec)
2527 eassert (it->current.dpvec_index >= 0);
2528 else
2529 eassert (it->current.dpvec_index < 0);
2532 #define CHECK_IT(IT) check_it ((IT))
2534 #else /* not 0 */
2536 #define CHECK_IT(IT) (void) 0
2538 #endif /* not 0 */
2541 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2543 /* Check that the window end of window W is what we expect it
2544 to be---the last row in the current matrix displaying text. */
2546 static void
2547 check_window_end (struct window *w)
2549 if (!MINI_WINDOW_P (w)
2550 && !NILP (w->window_end_valid))
2552 struct glyph_row *row;
2553 eassert ((row = MATRIX_ROW (w->current_matrix,
2554 XFASTINT (w->window_end_vpos)),
2555 !row->enabled_p
2556 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2557 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2561 #define CHECK_WINDOW_END(W) check_window_end ((W))
2563 #else
2565 #define CHECK_WINDOW_END(W) (void) 0
2567 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2571 /***********************************************************************
2572 Iterator initialization
2573 ***********************************************************************/
2575 /* Initialize IT for displaying current_buffer in window W, starting
2576 at character position CHARPOS. CHARPOS < 0 means that no buffer
2577 position is specified which is useful when the iterator is assigned
2578 a position later. BYTEPOS is the byte position corresponding to
2579 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2581 If ROW is not null, calls to produce_glyphs with IT as parameter
2582 will produce glyphs in that row.
2584 BASE_FACE_ID is the id of a base face to use. It must be one of
2585 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2586 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2587 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2589 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2590 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2591 will be initialized to use the corresponding mode line glyph row of
2592 the desired matrix of W. */
2594 void
2595 init_iterator (struct it *it, struct window *w,
2596 ptrdiff_t charpos, ptrdiff_t bytepos,
2597 struct glyph_row *row, enum face_id base_face_id)
2599 int highlight_region_p;
2600 enum face_id remapped_base_face_id = base_face_id;
2602 /* Some precondition checks. */
2603 eassert (w != NULL && it != NULL);
2604 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2605 && charpos <= ZV));
2607 /* If face attributes have been changed since the last redisplay,
2608 free realized faces now because they depend on face definitions
2609 that might have changed. Don't free faces while there might be
2610 desired matrices pending which reference these faces. */
2611 if (face_change_count && !inhibit_free_realized_faces)
2613 face_change_count = 0;
2614 free_all_realized_faces (Qnil);
2617 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2618 if (! NILP (Vface_remapping_alist))
2619 remapped_base_face_id
2620 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2622 /* Use one of the mode line rows of W's desired matrix if
2623 appropriate. */
2624 if (row == NULL)
2626 if (base_face_id == MODE_LINE_FACE_ID
2627 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2628 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2629 else if (base_face_id == HEADER_LINE_FACE_ID)
2630 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2633 /* Clear IT. */
2634 memset (it, 0, sizeof *it);
2635 it->current.overlay_string_index = -1;
2636 it->current.dpvec_index = -1;
2637 it->base_face_id = remapped_base_face_id;
2638 it->string = Qnil;
2639 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2640 it->paragraph_embedding = L2R;
2641 it->bidi_it.string.lstring = Qnil;
2642 it->bidi_it.string.s = NULL;
2643 it->bidi_it.string.bufpos = 0;
2645 /* The window in which we iterate over current_buffer: */
2646 XSETWINDOW (it->window, w);
2647 it->w = w;
2648 it->f = XFRAME (w->frame);
2650 it->cmp_it.id = -1;
2652 /* Extra space between lines (on window systems only). */
2653 if (base_face_id == DEFAULT_FACE_ID
2654 && FRAME_WINDOW_P (it->f))
2656 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2657 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2658 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2659 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2660 * FRAME_LINE_HEIGHT (it->f));
2661 else if (it->f->extra_line_spacing > 0)
2662 it->extra_line_spacing = it->f->extra_line_spacing;
2663 it->max_extra_line_spacing = 0;
2666 /* If realized faces have been removed, e.g. because of face
2667 attribute changes of named faces, recompute them. When running
2668 in batch mode, the face cache of the initial frame is null. If
2669 we happen to get called, make a dummy face cache. */
2670 if (FRAME_FACE_CACHE (it->f) == NULL)
2671 init_frame_faces (it->f);
2672 if (FRAME_FACE_CACHE (it->f)->used == 0)
2673 recompute_basic_faces (it->f);
2675 /* Current value of the `slice', `space-width', and 'height' properties. */
2676 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2677 it->space_width = Qnil;
2678 it->font_height = Qnil;
2679 it->override_ascent = -1;
2681 /* Are control characters displayed as `^C'? */
2682 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2684 /* -1 means everything between a CR and the following line end
2685 is invisible. >0 means lines indented more than this value are
2686 invisible. */
2687 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2688 ? (clip_to_bounds
2689 (-1, XINT (BVAR (current_buffer, selective_display)),
2690 PTRDIFF_MAX))
2691 : (!NILP (BVAR (current_buffer, selective_display))
2692 ? -1 : 0));
2693 it->selective_display_ellipsis_p
2694 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2696 /* Display table to use. */
2697 it->dp = window_display_table (w);
2699 /* Are multibyte characters enabled in current_buffer? */
2700 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2702 /* Non-zero if we should highlight the region. */
2703 highlight_region_p
2704 = (!NILP (Vtransient_mark_mode)
2705 && !NILP (BVAR (current_buffer, mark_active))
2706 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2708 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2709 start and end of a visible region in window IT->w. Set both to
2710 -1 to indicate no region. */
2711 if (highlight_region_p
2712 /* Maybe highlight only in selected window. */
2713 && (/* Either show region everywhere. */
2714 highlight_nonselected_windows
2715 /* Or show region in the selected window. */
2716 || w == XWINDOW (selected_window)
2717 /* Or show the region if we are in the mini-buffer and W is
2718 the window the mini-buffer refers to. */
2719 || (MINI_WINDOW_P (XWINDOW (selected_window))
2720 && WINDOWP (minibuf_selected_window)
2721 && w == XWINDOW (minibuf_selected_window))))
2723 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2724 it->region_beg_charpos = min (PT, markpos);
2725 it->region_end_charpos = max (PT, markpos);
2727 else
2728 it->region_beg_charpos = it->region_end_charpos = -1;
2730 /* Get the position at which the redisplay_end_trigger hook should
2731 be run, if it is to be run at all. */
2732 if (MARKERP (w->redisplay_end_trigger)
2733 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2734 it->redisplay_end_trigger_charpos
2735 = marker_position (w->redisplay_end_trigger);
2736 else if (INTEGERP (w->redisplay_end_trigger))
2737 it->redisplay_end_trigger_charpos =
2738 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2740 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2742 /* Are lines in the display truncated? */
2743 if (base_face_id != DEFAULT_FACE_ID
2744 || it->w->hscroll
2745 || (! WINDOW_FULL_WIDTH_P (it->w)
2746 && ((!NILP (Vtruncate_partial_width_windows)
2747 && !INTEGERP (Vtruncate_partial_width_windows))
2748 || (INTEGERP (Vtruncate_partial_width_windows)
2749 && (WINDOW_TOTAL_COLS (it->w)
2750 < XINT (Vtruncate_partial_width_windows))))))
2751 it->line_wrap = TRUNCATE;
2752 else if (NILP (BVAR (current_buffer, truncate_lines)))
2753 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2754 ? WINDOW_WRAP : WORD_WRAP;
2755 else
2756 it->line_wrap = TRUNCATE;
2758 /* Get dimensions of truncation and continuation glyphs. These are
2759 displayed as fringe bitmaps under X, but we need them for such
2760 frames when the fringes are turned off. But leave the dimensions
2761 zero for tooltip frames, as these glyphs look ugly there and also
2762 sabotage calculations of tooltip dimensions in x-show-tip. */
2763 #ifdef HAVE_WINDOW_SYSTEM
2764 if (!(FRAME_WINDOW_P (it->f)
2765 && FRAMEP (tip_frame)
2766 && it->f == XFRAME (tip_frame)))
2767 #endif
2769 if (it->line_wrap == TRUNCATE)
2771 /* We will need the truncation glyph. */
2772 eassert (it->glyph_row == NULL);
2773 produce_special_glyphs (it, IT_TRUNCATION);
2774 it->truncation_pixel_width = it->pixel_width;
2776 else
2778 /* We will need the continuation glyph. */
2779 eassert (it->glyph_row == NULL);
2780 produce_special_glyphs (it, IT_CONTINUATION);
2781 it->continuation_pixel_width = it->pixel_width;
2785 /* Reset these values to zero because the produce_special_glyphs
2786 above has changed them. */
2787 it->pixel_width = it->ascent = it->descent = 0;
2788 it->phys_ascent = it->phys_descent = 0;
2790 /* Set this after getting the dimensions of truncation and
2791 continuation glyphs, so that we don't produce glyphs when calling
2792 produce_special_glyphs, above. */
2793 it->glyph_row = row;
2794 it->area = TEXT_AREA;
2796 /* Forget any previous info about this row being reversed. */
2797 if (it->glyph_row)
2798 it->glyph_row->reversed_p = 0;
2800 /* Get the dimensions of the display area. The display area
2801 consists of the visible window area plus a horizontally scrolled
2802 part to the left of the window. All x-values are relative to the
2803 start of this total display area. */
2804 if (base_face_id != DEFAULT_FACE_ID)
2806 /* Mode lines, menu bar in terminal frames. */
2807 it->first_visible_x = 0;
2808 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2810 else
2812 it->first_visible_x =
2813 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2814 it->last_visible_x = (it->first_visible_x
2815 + window_box_width (w, TEXT_AREA));
2817 /* If we truncate lines, leave room for the truncation glyph(s) at
2818 the right margin. Otherwise, leave room for the continuation
2819 glyph(s). Done only if the window has no fringes. Since we
2820 don't know at this point whether there will be any R2L lines in
2821 the window, we reserve space for truncation/continuation glyphs
2822 even if only one of the fringes is absent. */
2823 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2824 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2826 if (it->line_wrap == TRUNCATE)
2827 it->last_visible_x -= it->truncation_pixel_width;
2828 else
2829 it->last_visible_x -= it->continuation_pixel_width;
2832 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2833 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2836 /* Leave room for a border glyph. */
2837 if (!FRAME_WINDOW_P (it->f)
2838 && !WINDOW_RIGHTMOST_P (it->w))
2839 it->last_visible_x -= 1;
2841 it->last_visible_y = window_text_bottom_y (w);
2843 /* For mode lines and alike, arrange for the first glyph having a
2844 left box line if the face specifies a box. */
2845 if (base_face_id != DEFAULT_FACE_ID)
2847 struct face *face;
2849 it->face_id = remapped_base_face_id;
2851 /* If we have a boxed mode line, make the first character appear
2852 with a left box line. */
2853 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2854 if (face->box != FACE_NO_BOX)
2855 it->start_of_box_run_p = 1;
2858 /* If a buffer position was specified, set the iterator there,
2859 getting overlays and face properties from that position. */
2860 if (charpos >= BUF_BEG (current_buffer))
2862 it->end_charpos = ZV;
2863 IT_CHARPOS (*it) = charpos;
2865 /* We will rely on `reseat' to set this up properly, via
2866 handle_face_prop. */
2867 it->face_id = it->base_face_id;
2869 /* Compute byte position if not specified. */
2870 if (bytepos < charpos)
2871 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2872 else
2873 IT_BYTEPOS (*it) = bytepos;
2875 it->start = it->current;
2876 /* Do we need to reorder bidirectional text? Not if this is a
2877 unibyte buffer: by definition, none of the single-byte
2878 characters are strong R2L, so no reordering is needed. And
2879 bidi.c doesn't support unibyte buffers anyway. Also, don't
2880 reorder while we are loading loadup.el, since the tables of
2881 character properties needed for reordering are not yet
2882 available. */
2883 it->bidi_p =
2884 NILP (Vpurify_flag)
2885 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2886 && it->multibyte_p;
2888 /* If we are to reorder bidirectional text, init the bidi
2889 iterator. */
2890 if (it->bidi_p)
2892 /* Note the paragraph direction that this buffer wants to
2893 use. */
2894 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2895 Qleft_to_right))
2896 it->paragraph_embedding = L2R;
2897 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qright_to_left))
2899 it->paragraph_embedding = R2L;
2900 else
2901 it->paragraph_embedding = NEUTRAL_DIR;
2902 bidi_unshelve_cache (NULL, 0);
2903 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2904 &it->bidi_it);
2907 /* Compute faces etc. */
2908 reseat (it, it->current.pos, 1);
2911 CHECK_IT (it);
2915 /* Initialize IT for the display of window W with window start POS. */
2917 void
2918 start_display (struct it *it, struct window *w, struct text_pos pos)
2920 struct glyph_row *row;
2921 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2923 row = w->desired_matrix->rows + first_vpos;
2924 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2925 it->first_vpos = first_vpos;
2927 /* Don't reseat to previous visible line start if current start
2928 position is in a string or image. */
2929 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2931 int start_at_line_beg_p;
2932 int first_y = it->current_y;
2934 /* If window start is not at a line start, skip forward to POS to
2935 get the correct continuation lines width. */
2936 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2937 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2938 if (!start_at_line_beg_p)
2940 int new_x;
2942 reseat_at_previous_visible_line_start (it);
2943 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2945 new_x = it->current_x + it->pixel_width;
2947 /* If lines are continued, this line may end in the middle
2948 of a multi-glyph character (e.g. a control character
2949 displayed as \003, or in the middle of an overlay
2950 string). In this case move_it_to above will not have
2951 taken us to the start of the continuation line but to the
2952 end of the continued line. */
2953 if (it->current_x > 0
2954 && it->line_wrap != TRUNCATE /* Lines are continued. */
2955 && (/* And glyph doesn't fit on the line. */
2956 new_x > it->last_visible_x
2957 /* Or it fits exactly and we're on a window
2958 system frame. */
2959 || (new_x == it->last_visible_x
2960 && FRAME_WINDOW_P (it->f)
2961 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2962 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2963 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2965 if ((it->current.dpvec_index >= 0
2966 || it->current.overlay_string_index >= 0)
2967 /* If we are on a newline from a display vector or
2968 overlay string, then we are already at the end of
2969 a screen line; no need to go to the next line in
2970 that case, as this line is not really continued.
2971 (If we do go to the next line, C-e will not DTRT.) */
2972 && it->c != '\n')
2974 set_iterator_to_next (it, 1);
2975 move_it_in_display_line_to (it, -1, -1, 0);
2978 it->continuation_lines_width += it->current_x;
2980 /* If the character at POS is displayed via a display
2981 vector, move_it_to above stops at the final glyph of
2982 IT->dpvec. To make the caller redisplay that character
2983 again (a.k.a. start at POS), we need to reset the
2984 dpvec_index to the beginning of IT->dpvec. */
2985 else if (it->current.dpvec_index >= 0)
2986 it->current.dpvec_index = 0;
2988 /* We're starting a new display line, not affected by the
2989 height of the continued line, so clear the appropriate
2990 fields in the iterator structure. */
2991 it->max_ascent = it->max_descent = 0;
2992 it->max_phys_ascent = it->max_phys_descent = 0;
2994 it->current_y = first_y;
2995 it->vpos = 0;
2996 it->current_x = it->hpos = 0;
3002 /* Return 1 if POS is a position in ellipses displayed for invisible
3003 text. W is the window we display, for text property lookup. */
3005 static int
3006 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3008 Lisp_Object prop, window;
3009 int ellipses_p = 0;
3010 ptrdiff_t charpos = CHARPOS (pos->pos);
3012 /* If POS specifies a position in a display vector, this might
3013 be for an ellipsis displayed for invisible text. We won't
3014 get the iterator set up for delivering that ellipsis unless
3015 we make sure that it gets aware of the invisible text. */
3016 if (pos->dpvec_index >= 0
3017 && pos->overlay_string_index < 0
3018 && CHARPOS (pos->string_pos) < 0
3019 && charpos > BEGV
3020 && (XSETWINDOW (window, w),
3021 prop = Fget_char_property (make_number (charpos),
3022 Qinvisible, window),
3023 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3025 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3026 window);
3027 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3030 return ellipses_p;
3034 /* Initialize IT for stepping through current_buffer in window W,
3035 starting at position POS that includes overlay string and display
3036 vector/ control character translation position information. Value
3037 is zero if there are overlay strings with newlines at POS. */
3039 static int
3040 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3042 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3043 int i, overlay_strings_with_newlines = 0;
3045 /* If POS specifies a position in a display vector, this might
3046 be for an ellipsis displayed for invisible text. We won't
3047 get the iterator set up for delivering that ellipsis unless
3048 we make sure that it gets aware of the invisible text. */
3049 if (in_ellipses_for_invisible_text_p (pos, w))
3051 --charpos;
3052 bytepos = 0;
3055 /* Keep in mind: the call to reseat in init_iterator skips invisible
3056 text, so we might end up at a position different from POS. This
3057 is only a problem when POS is a row start after a newline and an
3058 overlay starts there with an after-string, and the overlay has an
3059 invisible property. Since we don't skip invisible text in
3060 display_line and elsewhere immediately after consuming the
3061 newline before the row start, such a POS will not be in a string,
3062 but the call to init_iterator below will move us to the
3063 after-string. */
3064 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3066 /* This only scans the current chunk -- it should scan all chunks.
3067 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3068 to 16 in 22.1 to make this a lesser problem. */
3069 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3071 const char *s = SSDATA (it->overlay_strings[i]);
3072 const char *e = s + SBYTES (it->overlay_strings[i]);
3074 while (s < e && *s != '\n')
3075 ++s;
3077 if (s < e)
3079 overlay_strings_with_newlines = 1;
3080 break;
3084 /* If position is within an overlay string, set up IT to the right
3085 overlay string. */
3086 if (pos->overlay_string_index >= 0)
3088 int relative_index;
3090 /* If the first overlay string happens to have a `display'
3091 property for an image, the iterator will be set up for that
3092 image, and we have to undo that setup first before we can
3093 correct the overlay string index. */
3094 if (it->method == GET_FROM_IMAGE)
3095 pop_it (it);
3097 /* We already have the first chunk of overlay strings in
3098 IT->overlay_strings. Load more until the one for
3099 pos->overlay_string_index is in IT->overlay_strings. */
3100 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3102 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3103 it->current.overlay_string_index = 0;
3104 while (n--)
3106 load_overlay_strings (it, 0);
3107 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3111 it->current.overlay_string_index = pos->overlay_string_index;
3112 relative_index = (it->current.overlay_string_index
3113 % OVERLAY_STRING_CHUNK_SIZE);
3114 it->string = it->overlay_strings[relative_index];
3115 eassert (STRINGP (it->string));
3116 it->current.string_pos = pos->string_pos;
3117 it->method = GET_FROM_STRING;
3118 it->end_charpos = SCHARS (it->string);
3119 /* Set up the bidi iterator for this overlay string. */
3120 if (it->bidi_p)
3122 it->bidi_it.string.lstring = it->string;
3123 it->bidi_it.string.s = NULL;
3124 it->bidi_it.string.schars = SCHARS (it->string);
3125 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3126 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3127 it->bidi_it.string.unibyte = !it->multibyte_p;
3128 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3129 FRAME_WINDOW_P (it->f), &it->bidi_it);
3131 /* Synchronize the state of the bidi iterator with
3132 pos->string_pos. For any string position other than
3133 zero, this will be done automagically when we resume
3134 iteration over the string and get_visually_first_element
3135 is called. But if string_pos is zero, and the string is
3136 to be reordered for display, we need to resync manually,
3137 since it could be that the iteration state recorded in
3138 pos ended at string_pos of 0 moving backwards in string. */
3139 if (CHARPOS (pos->string_pos) == 0)
3141 get_visually_first_element (it);
3142 if (IT_STRING_CHARPOS (*it) != 0)
3143 do {
3144 /* Paranoia. */
3145 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3146 bidi_move_to_visually_next (&it->bidi_it);
3147 } while (it->bidi_it.charpos != 0);
3149 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3150 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3154 if (CHARPOS (pos->string_pos) >= 0)
3156 /* Recorded position is not in an overlay string, but in another
3157 string. This can only be a string from a `display' property.
3158 IT should already be filled with that string. */
3159 it->current.string_pos = pos->string_pos;
3160 eassert (STRINGP (it->string));
3161 if (it->bidi_p)
3162 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3163 FRAME_WINDOW_P (it->f), &it->bidi_it);
3166 /* Restore position in display vector translations, control
3167 character translations or ellipses. */
3168 if (pos->dpvec_index >= 0)
3170 if (it->dpvec == NULL)
3171 get_next_display_element (it);
3172 eassert (it->dpvec && it->current.dpvec_index == 0);
3173 it->current.dpvec_index = pos->dpvec_index;
3176 CHECK_IT (it);
3177 return !overlay_strings_with_newlines;
3181 /* Initialize IT for stepping through current_buffer in window W
3182 starting at ROW->start. */
3184 static void
3185 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3187 init_from_display_pos (it, w, &row->start);
3188 it->start = row->start;
3189 it->continuation_lines_width = row->continuation_lines_width;
3190 CHECK_IT (it);
3194 /* Initialize IT for stepping through current_buffer in window W
3195 starting in the line following ROW, i.e. starting at ROW->end.
3196 Value is zero if there are overlay strings with newlines at ROW's
3197 end position. */
3199 static int
3200 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3202 int success = 0;
3204 if (init_from_display_pos (it, w, &row->end))
3206 if (row->continued_p)
3207 it->continuation_lines_width
3208 = row->continuation_lines_width + row->pixel_width;
3209 CHECK_IT (it);
3210 success = 1;
3213 return success;
3219 /***********************************************************************
3220 Text properties
3221 ***********************************************************************/
3223 /* Called when IT reaches IT->stop_charpos. Handle text property and
3224 overlay changes. Set IT->stop_charpos to the next position where
3225 to stop. */
3227 static void
3228 handle_stop (struct it *it)
3230 enum prop_handled handled;
3231 int handle_overlay_change_p;
3232 struct props *p;
3234 it->dpvec = NULL;
3235 it->current.dpvec_index = -1;
3236 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3237 it->ignore_overlay_strings_at_pos_p = 0;
3238 it->ellipsis_p = 0;
3240 /* Use face of preceding text for ellipsis (if invisible) */
3241 if (it->selective_display_ellipsis_p)
3242 it->saved_face_id = it->face_id;
3246 handled = HANDLED_NORMALLY;
3248 /* Call text property handlers. */
3249 for (p = it_props; p->handler; ++p)
3251 handled = p->handler (it);
3253 if (handled == HANDLED_RECOMPUTE_PROPS)
3254 break;
3255 else if (handled == HANDLED_RETURN)
3257 /* We still want to show before and after strings from
3258 overlays even if the actual buffer text is replaced. */
3259 if (!handle_overlay_change_p
3260 || it->sp > 1
3261 /* Don't call get_overlay_strings_1 if we already
3262 have overlay strings loaded, because doing so
3263 will load them again and push the iterator state
3264 onto the stack one more time, which is not
3265 expected by the rest of the code that processes
3266 overlay strings. */
3267 || (it->current.overlay_string_index < 0
3268 ? !get_overlay_strings_1 (it, 0, 0)
3269 : 0))
3271 if (it->ellipsis_p)
3272 setup_for_ellipsis (it, 0);
3273 /* When handling a display spec, we might load an
3274 empty string. In that case, discard it here. We
3275 used to discard it in handle_single_display_spec,
3276 but that causes get_overlay_strings_1, above, to
3277 ignore overlay strings that we must check. */
3278 if (STRINGP (it->string) && !SCHARS (it->string))
3279 pop_it (it);
3280 return;
3282 else if (STRINGP (it->string) && !SCHARS (it->string))
3283 pop_it (it);
3284 else
3286 it->ignore_overlay_strings_at_pos_p = 1;
3287 it->string_from_display_prop_p = 0;
3288 it->from_disp_prop_p = 0;
3289 handle_overlay_change_p = 0;
3291 handled = HANDLED_RECOMPUTE_PROPS;
3292 break;
3294 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3295 handle_overlay_change_p = 0;
3298 if (handled != HANDLED_RECOMPUTE_PROPS)
3300 /* Don't check for overlay strings below when set to deliver
3301 characters from a display vector. */
3302 if (it->method == GET_FROM_DISPLAY_VECTOR)
3303 handle_overlay_change_p = 0;
3305 /* Handle overlay changes.
3306 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3307 if it finds overlays. */
3308 if (handle_overlay_change_p)
3309 handled = handle_overlay_change (it);
3312 if (it->ellipsis_p)
3314 setup_for_ellipsis (it, 0);
3315 break;
3318 while (handled == HANDLED_RECOMPUTE_PROPS);
3320 /* Determine where to stop next. */
3321 if (handled == HANDLED_NORMALLY)
3322 compute_stop_pos (it);
3326 /* Compute IT->stop_charpos from text property and overlay change
3327 information for IT's current position. */
3329 static void
3330 compute_stop_pos (struct it *it)
3332 register INTERVAL iv, next_iv;
3333 Lisp_Object object, limit, position;
3334 ptrdiff_t charpos, bytepos;
3336 if (STRINGP (it->string))
3338 /* Strings are usually short, so don't limit the search for
3339 properties. */
3340 it->stop_charpos = it->end_charpos;
3341 object = it->string;
3342 limit = Qnil;
3343 charpos = IT_STRING_CHARPOS (*it);
3344 bytepos = IT_STRING_BYTEPOS (*it);
3346 else
3348 ptrdiff_t pos;
3350 /* If end_charpos is out of range for some reason, such as a
3351 misbehaving display function, rationalize it (Bug#5984). */
3352 if (it->end_charpos > ZV)
3353 it->end_charpos = ZV;
3354 it->stop_charpos = it->end_charpos;
3356 /* If next overlay change is in front of the current stop pos
3357 (which is IT->end_charpos), stop there. Note: value of
3358 next_overlay_change is point-max if no overlay change
3359 follows. */
3360 charpos = IT_CHARPOS (*it);
3361 bytepos = IT_BYTEPOS (*it);
3362 pos = next_overlay_change (charpos);
3363 if (pos < it->stop_charpos)
3364 it->stop_charpos = pos;
3366 /* If showing the region, we have to stop at the region
3367 start or end because the face might change there. */
3368 if (it->region_beg_charpos > 0)
3370 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3371 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3372 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3373 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3376 /* Set up variables for computing the stop position from text
3377 property changes. */
3378 XSETBUFFER (object, current_buffer);
3379 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3382 /* Get the interval containing IT's position. Value is a null
3383 interval if there isn't such an interval. */
3384 position = make_number (charpos);
3385 iv = validate_interval_range (object, &position, &position, 0);
3386 if (iv)
3388 Lisp_Object values_here[LAST_PROP_IDX];
3389 struct props *p;
3391 /* Get properties here. */
3392 for (p = it_props; p->handler; ++p)
3393 values_here[p->idx] = textget (iv->plist, *p->name);
3395 /* Look for an interval following iv that has different
3396 properties. */
3397 for (next_iv = next_interval (iv);
3398 (next_iv
3399 && (NILP (limit)
3400 || XFASTINT (limit) > next_iv->position));
3401 next_iv = next_interval (next_iv))
3403 for (p = it_props; p->handler; ++p)
3405 Lisp_Object new_value;
3407 new_value = textget (next_iv->plist, *p->name);
3408 if (!EQ (values_here[p->idx], new_value))
3409 break;
3412 if (p->handler)
3413 break;
3416 if (next_iv)
3418 if (INTEGERP (limit)
3419 && next_iv->position >= XFASTINT (limit))
3420 /* No text property change up to limit. */
3421 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3422 else
3423 /* Text properties change in next_iv. */
3424 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3428 if (it->cmp_it.id < 0)
3430 ptrdiff_t stoppos = it->end_charpos;
3432 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3433 stoppos = -1;
3434 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3435 stoppos, it->string);
3438 eassert (STRINGP (it->string)
3439 || (it->stop_charpos >= BEGV
3440 && it->stop_charpos >= IT_CHARPOS (*it)));
3444 /* Return the position of the next overlay change after POS in
3445 current_buffer. Value is point-max if no overlay change
3446 follows. This is like `next-overlay-change' but doesn't use
3447 xmalloc. */
3449 static ptrdiff_t
3450 next_overlay_change (ptrdiff_t pos)
3452 ptrdiff_t i, noverlays;
3453 ptrdiff_t endpos;
3454 Lisp_Object *overlays;
3456 /* Get all overlays at the given position. */
3457 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3459 /* If any of these overlays ends before endpos,
3460 use its ending point instead. */
3461 for (i = 0; i < noverlays; ++i)
3463 Lisp_Object oend;
3464 ptrdiff_t oendpos;
3466 oend = OVERLAY_END (overlays[i]);
3467 oendpos = OVERLAY_POSITION (oend);
3468 endpos = min (endpos, oendpos);
3471 return endpos;
3474 /* How many characters forward to search for a display property or
3475 display string. Searching too far forward makes the bidi display
3476 sluggish, especially in small windows. */
3477 #define MAX_DISP_SCAN 250
3479 /* Return the character position of a display string at or after
3480 position specified by POSITION. If no display string exists at or
3481 after POSITION, return ZV. A display string is either an overlay
3482 with `display' property whose value is a string, or a `display'
3483 text property whose value is a string. STRING is data about the
3484 string to iterate; if STRING->lstring is nil, we are iterating a
3485 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3486 on a GUI frame. DISP_PROP is set to zero if we searched
3487 MAX_DISP_SCAN characters forward without finding any display
3488 strings, non-zero otherwise. It is set to 2 if the display string
3489 uses any kind of `(space ...)' spec that will produce a stretch of
3490 white space in the text area. */
3491 ptrdiff_t
3492 compute_display_string_pos (struct text_pos *position,
3493 struct bidi_string_data *string,
3494 int frame_window_p, int *disp_prop)
3496 /* OBJECT = nil means current buffer. */
3497 Lisp_Object object =
3498 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3499 Lisp_Object pos, spec, limpos;
3500 int string_p = (string && (STRINGP (string->lstring) || string->s));
3501 ptrdiff_t eob = string_p ? string->schars : ZV;
3502 ptrdiff_t begb = string_p ? 0 : BEGV;
3503 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3504 ptrdiff_t lim =
3505 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3506 struct text_pos tpos;
3507 int rv = 0;
3509 *disp_prop = 1;
3511 if (charpos >= eob
3512 /* We don't support display properties whose values are strings
3513 that have display string properties. */
3514 || string->from_disp_str
3515 /* C strings cannot have display properties. */
3516 || (string->s && !STRINGP (object)))
3518 *disp_prop = 0;
3519 return eob;
3522 /* If the character at CHARPOS is where the display string begins,
3523 return CHARPOS. */
3524 pos = make_number (charpos);
3525 if (STRINGP (object))
3526 bufpos = string->bufpos;
3527 else
3528 bufpos = charpos;
3529 tpos = *position;
3530 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3531 && (charpos <= begb
3532 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3533 object),
3534 spec))
3535 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3536 frame_window_p)))
3538 if (rv == 2)
3539 *disp_prop = 2;
3540 return charpos;
3543 /* Look forward for the first character with a `display' property
3544 that will replace the underlying text when displayed. */
3545 limpos = make_number (lim);
3546 do {
3547 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3548 CHARPOS (tpos) = XFASTINT (pos);
3549 if (CHARPOS (tpos) >= lim)
3551 *disp_prop = 0;
3552 break;
3554 if (STRINGP (object))
3555 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3556 else
3557 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3558 spec = Fget_char_property (pos, Qdisplay, object);
3559 if (!STRINGP (object))
3560 bufpos = CHARPOS (tpos);
3561 } while (NILP (spec)
3562 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3563 bufpos, frame_window_p)));
3564 if (rv == 2)
3565 *disp_prop = 2;
3567 return CHARPOS (tpos);
3570 /* Return the character position of the end of the display string that
3571 started at CHARPOS. If there's no display string at CHARPOS,
3572 return -1. A display string is either an overlay with `display'
3573 property whose value is a string or a `display' text property whose
3574 value is a string. */
3575 ptrdiff_t
3576 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3578 /* OBJECT = nil means current buffer. */
3579 Lisp_Object object =
3580 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3581 Lisp_Object pos = make_number (charpos);
3582 ptrdiff_t eob =
3583 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3585 if (charpos >= eob || (string->s && !STRINGP (object)))
3586 return eob;
3588 /* It could happen that the display property or overlay was removed
3589 since we found it in compute_display_string_pos above. One way
3590 this can happen is if JIT font-lock was called (through
3591 handle_fontified_prop), and jit-lock-functions remove text
3592 properties or overlays from the portion of buffer that includes
3593 CHARPOS. Muse mode is known to do that, for example. In this
3594 case, we return -1 to the caller, to signal that no display
3595 string is actually present at CHARPOS. See bidi_fetch_char for
3596 how this is handled.
3598 An alternative would be to never look for display properties past
3599 it->stop_charpos. But neither compute_display_string_pos nor
3600 bidi_fetch_char that calls it know or care where the next
3601 stop_charpos is. */
3602 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3603 return -1;
3605 /* Look forward for the first character where the `display' property
3606 changes. */
3607 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3609 return XFASTINT (pos);
3614 /***********************************************************************
3615 Fontification
3616 ***********************************************************************/
3618 /* Handle changes in the `fontified' property of the current buffer by
3619 calling hook functions from Qfontification_functions to fontify
3620 regions of text. */
3622 static enum prop_handled
3623 handle_fontified_prop (struct it *it)
3625 Lisp_Object prop, pos;
3626 enum prop_handled handled = HANDLED_NORMALLY;
3628 if (!NILP (Vmemory_full))
3629 return handled;
3631 /* Get the value of the `fontified' property at IT's current buffer
3632 position. (The `fontified' property doesn't have a special
3633 meaning in strings.) If the value is nil, call functions from
3634 Qfontification_functions. */
3635 if (!STRINGP (it->string)
3636 && it->s == NULL
3637 && !NILP (Vfontification_functions)
3638 && !NILP (Vrun_hooks)
3639 && (pos = make_number (IT_CHARPOS (*it)),
3640 prop = Fget_char_property (pos, Qfontified, Qnil),
3641 /* Ignore the special cased nil value always present at EOB since
3642 no amount of fontifying will be able to change it. */
3643 NILP (prop) && IT_CHARPOS (*it) < Z))
3645 ptrdiff_t count = SPECPDL_INDEX ();
3646 Lisp_Object val;
3647 struct buffer *obuf = current_buffer;
3648 int begv = BEGV, zv = ZV;
3649 int old_clip_changed = current_buffer->clip_changed;
3651 val = Vfontification_functions;
3652 specbind (Qfontification_functions, Qnil);
3654 eassert (it->end_charpos == ZV);
3656 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3657 safe_call1 (val, pos);
3658 else
3660 Lisp_Object fns, fn;
3661 struct gcpro gcpro1, gcpro2;
3663 fns = Qnil;
3664 GCPRO2 (val, fns);
3666 for (; CONSP (val); val = XCDR (val))
3668 fn = XCAR (val);
3670 if (EQ (fn, Qt))
3672 /* A value of t indicates this hook has a local
3673 binding; it means to run the global binding too.
3674 In a global value, t should not occur. If it
3675 does, we must ignore it to avoid an endless
3676 loop. */
3677 for (fns = Fdefault_value (Qfontification_functions);
3678 CONSP (fns);
3679 fns = XCDR (fns))
3681 fn = XCAR (fns);
3682 if (!EQ (fn, Qt))
3683 safe_call1 (fn, pos);
3686 else
3687 safe_call1 (fn, pos);
3690 UNGCPRO;
3693 unbind_to (count, Qnil);
3695 /* Fontification functions routinely call `save-restriction'.
3696 Normally, this tags clip_changed, which can confuse redisplay
3697 (see discussion in Bug#6671). Since we don't perform any
3698 special handling of fontification changes in the case where
3699 `save-restriction' isn't called, there's no point doing so in
3700 this case either. So, if the buffer's restrictions are
3701 actually left unchanged, reset clip_changed. */
3702 if (obuf == current_buffer)
3704 if (begv == BEGV && zv == ZV)
3705 current_buffer->clip_changed = old_clip_changed;
3707 /* There isn't much we can reasonably do to protect against
3708 misbehaving fontification, but here's a fig leaf. */
3709 else if (BUFFER_LIVE_P (obuf))
3710 set_buffer_internal_1 (obuf);
3712 /* The fontification code may have added/removed text.
3713 It could do even a lot worse, but let's at least protect against
3714 the most obvious case where only the text past `pos' gets changed',
3715 as is/was done in grep.el where some escapes sequences are turned
3716 into face properties (bug#7876). */
3717 it->end_charpos = ZV;
3719 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3720 something. This avoids an endless loop if they failed to
3721 fontify the text for which reason ever. */
3722 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3723 handled = HANDLED_RECOMPUTE_PROPS;
3726 return handled;
3731 /***********************************************************************
3732 Faces
3733 ***********************************************************************/
3735 /* Set up iterator IT from face properties at its current position.
3736 Called from handle_stop. */
3738 static enum prop_handled
3739 handle_face_prop (struct it *it)
3741 int new_face_id;
3742 ptrdiff_t next_stop;
3744 if (!STRINGP (it->string))
3746 new_face_id
3747 = face_at_buffer_position (it->w,
3748 IT_CHARPOS (*it),
3749 it->region_beg_charpos,
3750 it->region_end_charpos,
3751 &next_stop,
3752 (IT_CHARPOS (*it)
3753 + TEXT_PROP_DISTANCE_LIMIT),
3754 0, it->base_face_id);
3756 /* Is this a start of a run of characters with box face?
3757 Caveat: this can be called for a freshly initialized
3758 iterator; face_id is -1 in this case. We know that the new
3759 face will not change until limit, i.e. if the new face has a
3760 box, all characters up to limit will have one. But, as
3761 usual, we don't know whether limit is really the end. */
3762 if (new_face_id != it->face_id)
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3766 /* If new face has a box but old face has not, this is
3767 the start of a run of characters with box, i.e. it has
3768 a shadow on the left side. The value of face_id of the
3769 iterator will be -1 if this is the initial call that gets
3770 the face. In this case, we have to look in front of IT's
3771 position and see whether there is a face != new_face_id. */
3772 it->start_of_box_run_p
3773 = (new_face->box != FACE_NO_BOX
3774 && (it->face_id >= 0
3775 || IT_CHARPOS (*it) == BEG
3776 || new_face_id != face_before_it_pos (it)));
3777 it->face_box_p = new_face->box != FACE_NO_BOX;
3780 else
3782 int base_face_id;
3783 ptrdiff_t bufpos;
3784 int i;
3785 Lisp_Object from_overlay
3786 = (it->current.overlay_string_index >= 0
3787 ? it->string_overlays[it->current.overlay_string_index
3788 % OVERLAY_STRING_CHUNK_SIZE]
3789 : Qnil);
3791 /* See if we got to this string directly or indirectly from
3792 an overlay property. That includes the before-string or
3793 after-string of an overlay, strings in display properties
3794 provided by an overlay, their text properties, etc.
3796 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3797 if (! NILP (from_overlay))
3798 for (i = it->sp - 1; i >= 0; i--)
3800 if (it->stack[i].current.overlay_string_index >= 0)
3801 from_overlay
3802 = it->string_overlays[it->stack[i].current.overlay_string_index
3803 % OVERLAY_STRING_CHUNK_SIZE];
3804 else if (! NILP (it->stack[i].from_overlay))
3805 from_overlay = it->stack[i].from_overlay;
3807 if (!NILP (from_overlay))
3808 break;
3811 if (! NILP (from_overlay))
3813 bufpos = IT_CHARPOS (*it);
3814 /* For a string from an overlay, the base face depends
3815 only on text properties and ignores overlays. */
3816 base_face_id
3817 = face_for_overlay_string (it->w,
3818 IT_CHARPOS (*it),
3819 it->region_beg_charpos,
3820 it->region_end_charpos,
3821 &next_stop,
3822 (IT_CHARPOS (*it)
3823 + TEXT_PROP_DISTANCE_LIMIT),
3825 from_overlay);
3827 else
3829 bufpos = 0;
3831 /* For strings from a `display' property, use the face at
3832 IT's current buffer position as the base face to merge
3833 with, so that overlay strings appear in the same face as
3834 surrounding text, unless they specify their own
3835 faces. */
3836 base_face_id = it->string_from_prefix_prop_p
3837 ? DEFAULT_FACE_ID
3838 : underlying_face_id (it);
3841 new_face_id = face_at_string_position (it->w,
3842 it->string,
3843 IT_STRING_CHARPOS (*it),
3844 bufpos,
3845 it->region_beg_charpos,
3846 it->region_end_charpos,
3847 &next_stop,
3848 base_face_id, 0);
3850 /* Is this a start of a run of characters with box? Caveat:
3851 this can be called for a freshly allocated iterator; face_id
3852 is -1 is this case. We know that the new face will not
3853 change until the next check pos, i.e. if the new face has a
3854 box, all characters up to that position will have a
3855 box. But, as usual, we don't know whether that position
3856 is really the end. */
3857 if (new_face_id != it->face_id)
3859 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3860 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3862 /* If new face has a box but old face hasn't, this is the
3863 start of a run of characters with box, i.e. it has a
3864 shadow on the left side. */
3865 it->start_of_box_run_p
3866 = new_face->box && (old_face == NULL || !old_face->box);
3867 it->face_box_p = new_face->box != FACE_NO_BOX;
3871 it->face_id = new_face_id;
3872 return HANDLED_NORMALLY;
3876 /* Return the ID of the face ``underlying'' IT's current position,
3877 which is in a string. If the iterator is associated with a
3878 buffer, return the face at IT's current buffer position.
3879 Otherwise, use the iterator's base_face_id. */
3881 static int
3882 underlying_face_id (struct it *it)
3884 int face_id = it->base_face_id, i;
3886 eassert (STRINGP (it->string));
3888 for (i = it->sp - 1; i >= 0; --i)
3889 if (NILP (it->stack[i].string))
3890 face_id = it->stack[i].face_id;
3892 return face_id;
3896 /* Compute the face one character before or after the current position
3897 of IT, in the visual order. BEFORE_P non-zero means get the face
3898 in front (to the left in L2R paragraphs, to the right in R2L
3899 paragraphs) of IT's screen position. Value is the ID of the face. */
3901 static int
3902 face_before_or_after_it_pos (struct it *it, int before_p)
3904 int face_id, limit;
3905 ptrdiff_t next_check_charpos;
3906 struct it it_copy;
3907 void *it_copy_data = NULL;
3909 eassert (it->s == NULL);
3911 if (STRINGP (it->string))
3913 ptrdiff_t bufpos, charpos;
3914 int base_face_id;
3916 /* No face change past the end of the string (for the case
3917 we are padding with spaces). No face change before the
3918 string start. */
3919 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3920 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3921 return it->face_id;
3923 if (!it->bidi_p)
3925 /* Set charpos to the position before or after IT's current
3926 position, in the logical order, which in the non-bidi
3927 case is the same as the visual order. */
3928 if (before_p)
3929 charpos = IT_STRING_CHARPOS (*it) - 1;
3930 else if (it->what == IT_COMPOSITION)
3931 /* For composition, we must check the character after the
3932 composition. */
3933 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3934 else
3935 charpos = IT_STRING_CHARPOS (*it) + 1;
3937 else
3939 if (before_p)
3941 /* With bidi iteration, the character before the current
3942 in the visual order cannot be found by simple
3943 iteration, because "reverse" reordering is not
3944 supported. Instead, we need to use the move_it_*
3945 family of functions. */
3946 /* Ignore face changes before the first visible
3947 character on this display line. */
3948 if (it->current_x <= it->first_visible_x)
3949 return it->face_id;
3950 SAVE_IT (it_copy, *it, it_copy_data);
3951 /* Implementation note: Since move_it_in_display_line
3952 works in the iterator geometry, and thinks the first
3953 character is always the leftmost, even in R2L lines,
3954 we don't need to distinguish between the R2L and L2R
3955 cases here. */
3956 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3957 it_copy.current_x - 1, MOVE_TO_X);
3958 charpos = IT_STRING_CHARPOS (it_copy);
3959 RESTORE_IT (it, it, it_copy_data);
3961 else
3963 /* Set charpos to the string position of the character
3964 that comes after IT's current position in the visual
3965 order. */
3966 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3968 it_copy = *it;
3969 while (n--)
3970 bidi_move_to_visually_next (&it_copy.bidi_it);
3972 charpos = it_copy.bidi_it.charpos;
3975 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3977 if (it->current.overlay_string_index >= 0)
3978 bufpos = IT_CHARPOS (*it);
3979 else
3980 bufpos = 0;
3982 base_face_id = underlying_face_id (it);
3984 /* Get the face for ASCII, or unibyte. */
3985 face_id = face_at_string_position (it->w,
3986 it->string,
3987 charpos,
3988 bufpos,
3989 it->region_beg_charpos,
3990 it->region_end_charpos,
3991 &next_check_charpos,
3992 base_face_id, 0);
3994 /* Correct the face for charsets different from ASCII. Do it
3995 for the multibyte case only. The face returned above is
3996 suitable for unibyte text if IT->string is unibyte. */
3997 if (STRING_MULTIBYTE (it->string))
3999 struct text_pos pos1 = string_pos (charpos, it->string);
4000 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4001 int c, len;
4002 struct face *face = FACE_FROM_ID (it->f, face_id);
4004 c = string_char_and_length (p, &len);
4005 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4008 else
4010 struct text_pos pos;
4012 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4013 || (IT_CHARPOS (*it) <= BEGV && before_p))
4014 return it->face_id;
4016 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4017 pos = it->current.pos;
4019 if (!it->bidi_p)
4021 if (before_p)
4022 DEC_TEXT_POS (pos, it->multibyte_p);
4023 else
4025 if (it->what == IT_COMPOSITION)
4027 /* For composition, we must check the position after
4028 the composition. */
4029 pos.charpos += it->cmp_it.nchars;
4030 pos.bytepos += it->len;
4032 else
4033 INC_TEXT_POS (pos, it->multibyte_p);
4036 else
4038 if (before_p)
4040 /* With bidi iteration, the character before the current
4041 in the visual order cannot be found by simple
4042 iteration, because "reverse" reordering is not
4043 supported. Instead, we need to use the move_it_*
4044 family of functions. */
4045 /* Ignore face changes before the first visible
4046 character on this display line. */
4047 if (it->current_x <= it->first_visible_x)
4048 return it->face_id;
4049 SAVE_IT (it_copy, *it, it_copy_data);
4050 /* Implementation note: Since move_it_in_display_line
4051 works in the iterator geometry, and thinks the first
4052 character is always the leftmost, even in R2L lines,
4053 we don't need to distinguish between the R2L and L2R
4054 cases here. */
4055 move_it_in_display_line (&it_copy, ZV,
4056 it_copy.current_x - 1, MOVE_TO_X);
4057 pos = it_copy.current.pos;
4058 RESTORE_IT (it, it, it_copy_data);
4060 else
4062 /* Set charpos to the buffer position of the character
4063 that comes after IT's current position in the visual
4064 order. */
4065 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4067 it_copy = *it;
4068 while (n--)
4069 bidi_move_to_visually_next (&it_copy.bidi_it);
4071 SET_TEXT_POS (pos,
4072 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4075 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4077 /* Determine face for CHARSET_ASCII, or unibyte. */
4078 face_id = face_at_buffer_position (it->w,
4079 CHARPOS (pos),
4080 it->region_beg_charpos,
4081 it->region_end_charpos,
4082 &next_check_charpos,
4083 limit, 0, -1);
4085 /* Correct the face for charsets different from ASCII. Do it
4086 for the multibyte case only. The face returned above is
4087 suitable for unibyte text if current_buffer is unibyte. */
4088 if (it->multibyte_p)
4090 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4091 struct face *face = FACE_FROM_ID (it->f, face_id);
4092 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4096 return face_id;
4101 /***********************************************************************
4102 Invisible text
4103 ***********************************************************************/
4105 /* Set up iterator IT from invisible properties at its current
4106 position. Called from handle_stop. */
4108 static enum prop_handled
4109 handle_invisible_prop (struct it *it)
4111 enum prop_handled handled = HANDLED_NORMALLY;
4112 int invis_p;
4113 Lisp_Object prop;
4115 if (STRINGP (it->string))
4117 Lisp_Object end_charpos, limit, charpos;
4119 /* Get the value of the invisible text property at the
4120 current position. Value will be nil if there is no such
4121 property. */
4122 charpos = make_number (IT_STRING_CHARPOS (*it));
4123 prop = Fget_text_property (charpos, Qinvisible, it->string);
4124 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4126 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4128 /* Record whether we have to display an ellipsis for the
4129 invisible text. */
4130 int display_ellipsis_p = (invis_p == 2);
4131 ptrdiff_t len, endpos;
4133 handled = HANDLED_RECOMPUTE_PROPS;
4135 /* Get the position at which the next visible text can be
4136 found in IT->string, if any. */
4137 endpos = len = SCHARS (it->string);
4138 XSETINT (limit, len);
4141 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4142 it->string, limit);
4143 if (INTEGERP (end_charpos))
4145 endpos = XFASTINT (end_charpos);
4146 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4147 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4148 if (invis_p == 2)
4149 display_ellipsis_p = 1;
4152 while (invis_p && endpos < len);
4154 if (display_ellipsis_p)
4155 it->ellipsis_p = 1;
4157 if (endpos < len)
4159 /* Text at END_CHARPOS is visible. Move IT there. */
4160 struct text_pos old;
4161 ptrdiff_t oldpos;
4163 old = it->current.string_pos;
4164 oldpos = CHARPOS (old);
4165 if (it->bidi_p)
4167 if (it->bidi_it.first_elt
4168 && it->bidi_it.charpos < SCHARS (it->string))
4169 bidi_paragraph_init (it->paragraph_embedding,
4170 &it->bidi_it, 1);
4171 /* Bidi-iterate out of the invisible text. */
4174 bidi_move_to_visually_next (&it->bidi_it);
4176 while (oldpos <= it->bidi_it.charpos
4177 && it->bidi_it.charpos < endpos);
4179 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4180 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4181 if (IT_CHARPOS (*it) >= endpos)
4182 it->prev_stop = endpos;
4184 else
4186 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4187 compute_string_pos (&it->current.string_pos, old, it->string);
4190 else
4192 /* The rest of the string is invisible. If this is an
4193 overlay string, proceed with the next overlay string
4194 or whatever comes and return a character from there. */
4195 if (it->current.overlay_string_index >= 0
4196 && !display_ellipsis_p)
4198 next_overlay_string (it);
4199 /* Don't check for overlay strings when we just
4200 finished processing them. */
4201 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4203 else
4205 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4206 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4211 else
4213 ptrdiff_t newpos, next_stop, start_charpos, tem;
4214 Lisp_Object pos, overlay;
4216 /* First of all, is there invisible text at this position? */
4217 tem = start_charpos = IT_CHARPOS (*it);
4218 pos = make_number (tem);
4219 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4220 &overlay);
4221 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4223 /* If we are on invisible text, skip over it. */
4224 if (invis_p && start_charpos < it->end_charpos)
4226 /* Record whether we have to display an ellipsis for the
4227 invisible text. */
4228 int display_ellipsis_p = invis_p == 2;
4230 handled = HANDLED_RECOMPUTE_PROPS;
4232 /* Loop skipping over invisible text. The loop is left at
4233 ZV or with IT on the first char being visible again. */
4236 /* Try to skip some invisible text. Return value is the
4237 position reached which can be equal to where we start
4238 if there is nothing invisible there. This skips both
4239 over invisible text properties and overlays with
4240 invisible property. */
4241 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4243 /* If we skipped nothing at all we weren't at invisible
4244 text in the first place. If everything to the end of
4245 the buffer was skipped, end the loop. */
4246 if (newpos == tem || newpos >= ZV)
4247 invis_p = 0;
4248 else
4250 /* We skipped some characters but not necessarily
4251 all there are. Check if we ended up on visible
4252 text. Fget_char_property returns the property of
4253 the char before the given position, i.e. if we
4254 get invis_p = 0, this means that the char at
4255 newpos is visible. */
4256 pos = make_number (newpos);
4257 prop = Fget_char_property (pos, Qinvisible, it->window);
4258 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4261 /* If we ended up on invisible text, proceed to
4262 skip starting with next_stop. */
4263 if (invis_p)
4264 tem = next_stop;
4266 /* If there are adjacent invisible texts, don't lose the
4267 second one's ellipsis. */
4268 if (invis_p == 2)
4269 display_ellipsis_p = 1;
4271 while (invis_p);
4273 /* The position newpos is now either ZV or on visible text. */
4274 if (it->bidi_p)
4276 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4277 int on_newline =
4278 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4279 int after_newline =
4280 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4282 /* If the invisible text ends on a newline or on a
4283 character after a newline, we can avoid the costly,
4284 character by character, bidi iteration to NEWPOS, and
4285 instead simply reseat the iterator there. That's
4286 because all bidi reordering information is tossed at
4287 the newline. This is a big win for modes that hide
4288 complete lines, like Outline, Org, etc. */
4289 if (on_newline || after_newline)
4291 struct text_pos tpos;
4292 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4294 SET_TEXT_POS (tpos, newpos, bpos);
4295 reseat_1 (it, tpos, 0);
4296 /* If we reseat on a newline/ZV, we need to prep the
4297 bidi iterator for advancing to the next character
4298 after the newline/EOB, keeping the current paragraph
4299 direction (so that PRODUCE_GLYPHS does TRT wrt
4300 prepending/appending glyphs to a glyph row). */
4301 if (on_newline)
4303 it->bidi_it.first_elt = 0;
4304 it->bidi_it.paragraph_dir = pdir;
4305 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4306 it->bidi_it.nchars = 1;
4307 it->bidi_it.ch_len = 1;
4310 else /* Must use the slow method. */
4312 /* With bidi iteration, the region of invisible text
4313 could start and/or end in the middle of a
4314 non-base embedding level. Therefore, we need to
4315 skip invisible text using the bidi iterator,
4316 starting at IT's current position, until we find
4317 ourselves outside of the invisible text.
4318 Skipping invisible text _after_ bidi iteration
4319 avoids affecting the visual order of the
4320 displayed text when invisible properties are
4321 added or removed. */
4322 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4324 /* If we were `reseat'ed to a new paragraph,
4325 determine the paragraph base direction. We
4326 need to do it now because
4327 next_element_from_buffer may not have a
4328 chance to do it, if we are going to skip any
4329 text at the beginning, which resets the
4330 FIRST_ELT flag. */
4331 bidi_paragraph_init (it->paragraph_embedding,
4332 &it->bidi_it, 1);
4336 bidi_move_to_visually_next (&it->bidi_it);
4338 while (it->stop_charpos <= it->bidi_it.charpos
4339 && it->bidi_it.charpos < newpos);
4340 IT_CHARPOS (*it) = it->bidi_it.charpos;
4341 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4342 /* If we overstepped NEWPOS, record its position in
4343 the iterator, so that we skip invisible text if
4344 later the bidi iteration lands us in the
4345 invisible region again. */
4346 if (IT_CHARPOS (*it) >= newpos)
4347 it->prev_stop = newpos;
4350 else
4352 IT_CHARPOS (*it) = newpos;
4353 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4356 /* If there are before-strings at the start of invisible
4357 text, and the text is invisible because of a text
4358 property, arrange to show before-strings because 20.x did
4359 it that way. (If the text is invisible because of an
4360 overlay property instead of a text property, this is
4361 already handled in the overlay code.) */
4362 if (NILP (overlay)
4363 && get_overlay_strings (it, it->stop_charpos))
4365 handled = HANDLED_RECOMPUTE_PROPS;
4366 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4368 else if (display_ellipsis_p)
4370 /* Make sure that the glyphs of the ellipsis will get
4371 correct `charpos' values. If we would not update
4372 it->position here, the glyphs would belong to the
4373 last visible character _before_ the invisible
4374 text, which confuses `set_cursor_from_row'.
4376 We use the last invisible position instead of the
4377 first because this way the cursor is always drawn on
4378 the first "." of the ellipsis, whenever PT is inside
4379 the invisible text. Otherwise the cursor would be
4380 placed _after_ the ellipsis when the point is after the
4381 first invisible character. */
4382 if (!STRINGP (it->object))
4384 it->position.charpos = newpos - 1;
4385 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4387 it->ellipsis_p = 1;
4388 /* Let the ellipsis display before
4389 considering any properties of the following char.
4390 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4391 handled = HANDLED_RETURN;
4396 return handled;
4400 /* Make iterator IT return `...' next.
4401 Replaces LEN characters from buffer. */
4403 static void
4404 setup_for_ellipsis (struct it *it, int len)
4406 /* Use the display table definition for `...'. Invalid glyphs
4407 will be handled by the method returning elements from dpvec. */
4408 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4410 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4411 it->dpvec = v->contents;
4412 it->dpend = v->contents + v->header.size;
4414 else
4416 /* Default `...'. */
4417 it->dpvec = default_invis_vector;
4418 it->dpend = default_invis_vector + 3;
4421 it->dpvec_char_len = len;
4422 it->current.dpvec_index = 0;
4423 it->dpvec_face_id = -1;
4425 /* Remember the current face id in case glyphs specify faces.
4426 IT's face is restored in set_iterator_to_next.
4427 saved_face_id was set to preceding char's face in handle_stop. */
4428 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4429 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4431 it->method = GET_FROM_DISPLAY_VECTOR;
4432 it->ellipsis_p = 1;
4437 /***********************************************************************
4438 'display' property
4439 ***********************************************************************/
4441 /* Set up iterator IT from `display' property at its current position.
4442 Called from handle_stop.
4443 We return HANDLED_RETURN if some part of the display property
4444 overrides the display of the buffer text itself.
4445 Otherwise we return HANDLED_NORMALLY. */
4447 static enum prop_handled
4448 handle_display_prop (struct it *it)
4450 Lisp_Object propval, object, overlay;
4451 struct text_pos *position;
4452 ptrdiff_t bufpos;
4453 /* Nonzero if some property replaces the display of the text itself. */
4454 int display_replaced_p = 0;
4456 if (STRINGP (it->string))
4458 object = it->string;
4459 position = &it->current.string_pos;
4460 bufpos = CHARPOS (it->current.pos);
4462 else
4464 XSETWINDOW (object, it->w);
4465 position = &it->current.pos;
4466 bufpos = CHARPOS (*position);
4469 /* Reset those iterator values set from display property values. */
4470 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4471 it->space_width = Qnil;
4472 it->font_height = Qnil;
4473 it->voffset = 0;
4475 /* We don't support recursive `display' properties, i.e. string
4476 values that have a string `display' property, that have a string
4477 `display' property etc. */
4478 if (!it->string_from_display_prop_p)
4479 it->area = TEXT_AREA;
4481 propval = get_char_property_and_overlay (make_number (position->charpos),
4482 Qdisplay, object, &overlay);
4483 if (NILP (propval))
4484 return HANDLED_NORMALLY;
4485 /* Now OVERLAY is the overlay that gave us this property, or nil
4486 if it was a text property. */
4488 if (!STRINGP (it->string))
4489 object = it->w->buffer;
4491 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4492 position, bufpos,
4493 FRAME_WINDOW_P (it->f));
4495 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4498 /* Subroutine of handle_display_prop. Returns non-zero if the display
4499 specification in SPEC is a replacing specification, i.e. it would
4500 replace the text covered by `display' property with something else,
4501 such as an image or a display string. If SPEC includes any kind or
4502 `(space ...) specification, the value is 2; this is used by
4503 compute_display_string_pos, which see.
4505 See handle_single_display_spec for documentation of arguments.
4506 frame_window_p is non-zero if the window being redisplayed is on a
4507 GUI frame; this argument is used only if IT is NULL, see below.
4509 IT can be NULL, if this is called by the bidi reordering code
4510 through compute_display_string_pos, which see. In that case, this
4511 function only examines SPEC, but does not otherwise "handle" it, in
4512 the sense that it doesn't set up members of IT from the display
4513 spec. */
4514 static int
4515 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4516 Lisp_Object overlay, struct text_pos *position,
4517 ptrdiff_t bufpos, int frame_window_p)
4519 int replacing_p = 0;
4520 int rv;
4522 if (CONSP (spec)
4523 /* Simple specifications. */
4524 && !EQ (XCAR (spec), Qimage)
4525 && !EQ (XCAR (spec), Qspace)
4526 && !EQ (XCAR (spec), Qwhen)
4527 && !EQ (XCAR (spec), Qslice)
4528 && !EQ (XCAR (spec), Qspace_width)
4529 && !EQ (XCAR (spec), Qheight)
4530 && !EQ (XCAR (spec), Qraise)
4531 /* Marginal area specifications. */
4532 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4533 && !EQ (XCAR (spec), Qleft_fringe)
4534 && !EQ (XCAR (spec), Qright_fringe)
4535 && !NILP (XCAR (spec)))
4537 for (; CONSP (spec); spec = XCDR (spec))
4539 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4540 overlay, position, bufpos,
4541 replacing_p, frame_window_p)))
4543 replacing_p = rv;
4544 /* If some text in a string is replaced, `position' no
4545 longer points to the position of `object'. */
4546 if (!it || STRINGP (object))
4547 break;
4551 else if (VECTORP (spec))
4553 ptrdiff_t i;
4554 for (i = 0; i < ASIZE (spec); ++i)
4555 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4556 overlay, position, bufpos,
4557 replacing_p, frame_window_p)))
4559 replacing_p = rv;
4560 /* If some text in a string is replaced, `position' no
4561 longer points to the position of `object'. */
4562 if (!it || STRINGP (object))
4563 break;
4566 else
4568 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4569 position, bufpos, 0,
4570 frame_window_p)))
4571 replacing_p = rv;
4574 return replacing_p;
4577 /* Value is the position of the end of the `display' property starting
4578 at START_POS in OBJECT. */
4580 static struct text_pos
4581 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4583 Lisp_Object end;
4584 struct text_pos end_pos;
4586 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4587 Qdisplay, object, Qnil);
4588 CHARPOS (end_pos) = XFASTINT (end);
4589 if (STRINGP (object))
4590 compute_string_pos (&end_pos, start_pos, it->string);
4591 else
4592 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4594 return end_pos;
4598 /* Set up IT from a single `display' property specification SPEC. OBJECT
4599 is the object in which the `display' property was found. *POSITION
4600 is the position in OBJECT at which the `display' property was found.
4601 BUFPOS is the buffer position of OBJECT (different from POSITION if
4602 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4603 previously saw a display specification which already replaced text
4604 display with something else, for example an image; we ignore such
4605 properties after the first one has been processed.
4607 OVERLAY is the overlay this `display' property came from,
4608 or nil if it was a text property.
4610 If SPEC is a `space' or `image' specification, and in some other
4611 cases too, set *POSITION to the position where the `display'
4612 property ends.
4614 If IT is NULL, only examine the property specification in SPEC, but
4615 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4616 is intended to be displayed in a window on a GUI frame.
4618 Value is non-zero if something was found which replaces the display
4619 of buffer or string text. */
4621 static int
4622 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4623 Lisp_Object overlay, struct text_pos *position,
4624 ptrdiff_t bufpos, int display_replaced_p,
4625 int frame_window_p)
4627 Lisp_Object form;
4628 Lisp_Object location, value;
4629 struct text_pos start_pos = *position;
4630 int valid_p;
4632 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4633 If the result is non-nil, use VALUE instead of SPEC. */
4634 form = Qt;
4635 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4637 spec = XCDR (spec);
4638 if (!CONSP (spec))
4639 return 0;
4640 form = XCAR (spec);
4641 spec = XCDR (spec);
4644 if (!NILP (form) && !EQ (form, Qt))
4646 ptrdiff_t count = SPECPDL_INDEX ();
4647 struct gcpro gcpro1;
4649 /* Bind `object' to the object having the `display' property, a
4650 buffer or string. Bind `position' to the position in the
4651 object where the property was found, and `buffer-position'
4652 to the current position in the buffer. */
4654 if (NILP (object))
4655 XSETBUFFER (object, current_buffer);
4656 specbind (Qobject, object);
4657 specbind (Qposition, make_number (CHARPOS (*position)));
4658 specbind (Qbuffer_position, make_number (bufpos));
4659 GCPRO1 (form);
4660 form = safe_eval (form);
4661 UNGCPRO;
4662 unbind_to (count, Qnil);
4665 if (NILP (form))
4666 return 0;
4668 /* Handle `(height HEIGHT)' specifications. */
4669 if (CONSP (spec)
4670 && EQ (XCAR (spec), Qheight)
4671 && CONSP (XCDR (spec)))
4673 if (it)
4675 if (!FRAME_WINDOW_P (it->f))
4676 return 0;
4678 it->font_height = XCAR (XCDR (spec));
4679 if (!NILP (it->font_height))
4681 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4682 int new_height = -1;
4684 if (CONSP (it->font_height)
4685 && (EQ (XCAR (it->font_height), Qplus)
4686 || EQ (XCAR (it->font_height), Qminus))
4687 && CONSP (XCDR (it->font_height))
4688 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4690 /* `(+ N)' or `(- N)' where N is an integer. */
4691 int steps = XINT (XCAR (XCDR (it->font_height)));
4692 if (EQ (XCAR (it->font_height), Qplus))
4693 steps = - steps;
4694 it->face_id = smaller_face (it->f, it->face_id, steps);
4696 else if (FUNCTIONP (it->font_height))
4698 /* Call function with current height as argument.
4699 Value is the new height. */
4700 Lisp_Object height;
4701 height = safe_call1 (it->font_height,
4702 face->lface[LFACE_HEIGHT_INDEX]);
4703 if (NUMBERP (height))
4704 new_height = XFLOATINT (height);
4706 else if (NUMBERP (it->font_height))
4708 /* Value is a multiple of the canonical char height. */
4709 struct face *f;
4711 f = FACE_FROM_ID (it->f,
4712 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4713 new_height = (XFLOATINT (it->font_height)
4714 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4716 else
4718 /* Evaluate IT->font_height with `height' bound to the
4719 current specified height to get the new height. */
4720 ptrdiff_t count = SPECPDL_INDEX ();
4722 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4723 value = safe_eval (it->font_height);
4724 unbind_to (count, Qnil);
4726 if (NUMBERP (value))
4727 new_height = XFLOATINT (value);
4730 if (new_height > 0)
4731 it->face_id = face_with_height (it->f, it->face_id, new_height);
4735 return 0;
4738 /* Handle `(space-width WIDTH)'. */
4739 if (CONSP (spec)
4740 && EQ (XCAR (spec), Qspace_width)
4741 && CONSP (XCDR (spec)))
4743 if (it)
4745 if (!FRAME_WINDOW_P (it->f))
4746 return 0;
4748 value = XCAR (XCDR (spec));
4749 if (NUMBERP (value) && XFLOATINT (value) > 0)
4750 it->space_width = value;
4753 return 0;
4756 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4757 if (CONSP (spec)
4758 && EQ (XCAR (spec), Qslice))
4760 Lisp_Object tem;
4762 if (it)
4764 if (!FRAME_WINDOW_P (it->f))
4765 return 0;
4767 if (tem = XCDR (spec), CONSP (tem))
4769 it->slice.x = XCAR (tem);
4770 if (tem = XCDR (tem), CONSP (tem))
4772 it->slice.y = XCAR (tem);
4773 if (tem = XCDR (tem), CONSP (tem))
4775 it->slice.width = XCAR (tem);
4776 if (tem = XCDR (tem), CONSP (tem))
4777 it->slice.height = XCAR (tem);
4783 return 0;
4786 /* Handle `(raise FACTOR)'. */
4787 if (CONSP (spec)
4788 && EQ (XCAR (spec), Qraise)
4789 && CONSP (XCDR (spec)))
4791 if (it)
4793 if (!FRAME_WINDOW_P (it->f))
4794 return 0;
4796 #ifdef HAVE_WINDOW_SYSTEM
4797 value = XCAR (XCDR (spec));
4798 if (NUMBERP (value))
4800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4801 it->voffset = - (XFLOATINT (value)
4802 * (FONT_HEIGHT (face->font)));
4804 #endif /* HAVE_WINDOW_SYSTEM */
4807 return 0;
4810 /* Don't handle the other kinds of display specifications
4811 inside a string that we got from a `display' property. */
4812 if (it && it->string_from_display_prop_p)
4813 return 0;
4815 /* Characters having this form of property are not displayed, so
4816 we have to find the end of the property. */
4817 if (it)
4819 start_pos = *position;
4820 *position = display_prop_end (it, object, start_pos);
4822 value = Qnil;
4824 /* Stop the scan at that end position--we assume that all
4825 text properties change there. */
4826 if (it)
4827 it->stop_charpos = position->charpos;
4829 /* Handle `(left-fringe BITMAP [FACE])'
4830 and `(right-fringe BITMAP [FACE])'. */
4831 if (CONSP (spec)
4832 && (EQ (XCAR (spec), Qleft_fringe)
4833 || EQ (XCAR (spec), Qright_fringe))
4834 && CONSP (XCDR (spec)))
4836 int fringe_bitmap;
4838 if (it)
4840 if (!FRAME_WINDOW_P (it->f))
4841 /* If we return here, POSITION has been advanced
4842 across the text with this property. */
4844 /* Synchronize the bidi iterator with POSITION. This is
4845 needed because we are not going to push the iterator
4846 on behalf of this display property, so there will be
4847 no pop_it call to do this synchronization for us. */
4848 if (it->bidi_p)
4850 it->position = *position;
4851 iterate_out_of_display_property (it);
4852 *position = it->position;
4854 return 1;
4857 else if (!frame_window_p)
4858 return 1;
4860 #ifdef HAVE_WINDOW_SYSTEM
4861 value = XCAR (XCDR (spec));
4862 if (!SYMBOLP (value)
4863 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4864 /* If we return here, POSITION has been advanced
4865 across the text with this property. */
4867 if (it && it->bidi_p)
4869 it->position = *position;
4870 iterate_out_of_display_property (it);
4871 *position = it->position;
4873 return 1;
4876 if (it)
4878 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4880 if (CONSP (XCDR (XCDR (spec))))
4882 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4883 int face_id2 = lookup_derived_face (it->f, face_name,
4884 FRINGE_FACE_ID, 0);
4885 if (face_id2 >= 0)
4886 face_id = face_id2;
4889 /* Save current settings of IT so that we can restore them
4890 when we are finished with the glyph property value. */
4891 push_it (it, position);
4893 it->area = TEXT_AREA;
4894 it->what = IT_IMAGE;
4895 it->image_id = -1; /* no image */
4896 it->position = start_pos;
4897 it->object = NILP (object) ? it->w->buffer : object;
4898 it->method = GET_FROM_IMAGE;
4899 it->from_overlay = Qnil;
4900 it->face_id = face_id;
4901 it->from_disp_prop_p = 1;
4903 /* Say that we haven't consumed the characters with
4904 `display' property yet. The call to pop_it in
4905 set_iterator_to_next will clean this up. */
4906 *position = start_pos;
4908 if (EQ (XCAR (spec), Qleft_fringe))
4910 it->left_user_fringe_bitmap = fringe_bitmap;
4911 it->left_user_fringe_face_id = face_id;
4913 else
4915 it->right_user_fringe_bitmap = fringe_bitmap;
4916 it->right_user_fringe_face_id = face_id;
4919 #endif /* HAVE_WINDOW_SYSTEM */
4920 return 1;
4923 /* Prepare to handle `((margin left-margin) ...)',
4924 `((margin right-margin) ...)' and `((margin nil) ...)'
4925 prefixes for display specifications. */
4926 location = Qunbound;
4927 if (CONSP (spec) && CONSP (XCAR (spec)))
4929 Lisp_Object tem;
4931 value = XCDR (spec);
4932 if (CONSP (value))
4933 value = XCAR (value);
4935 tem = XCAR (spec);
4936 if (EQ (XCAR (tem), Qmargin)
4937 && (tem = XCDR (tem),
4938 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4939 (NILP (tem)
4940 || EQ (tem, Qleft_margin)
4941 || EQ (tem, Qright_margin))))
4942 location = tem;
4945 if (EQ (location, Qunbound))
4947 location = Qnil;
4948 value = spec;
4951 /* After this point, VALUE is the property after any
4952 margin prefix has been stripped. It must be a string,
4953 an image specification, or `(space ...)'.
4955 LOCATION specifies where to display: `left-margin',
4956 `right-margin' or nil. */
4958 valid_p = (STRINGP (value)
4959 #ifdef HAVE_WINDOW_SYSTEM
4960 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4961 && valid_image_p (value))
4962 #endif /* not HAVE_WINDOW_SYSTEM */
4963 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4965 if (valid_p && !display_replaced_p)
4967 int retval = 1;
4969 if (!it)
4971 /* Callers need to know whether the display spec is any kind
4972 of `(space ...)' spec that is about to affect text-area
4973 display. */
4974 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4975 retval = 2;
4976 return retval;
4979 /* Save current settings of IT so that we can restore them
4980 when we are finished with the glyph property value. */
4981 push_it (it, position);
4982 it->from_overlay = overlay;
4983 it->from_disp_prop_p = 1;
4985 if (NILP (location))
4986 it->area = TEXT_AREA;
4987 else if (EQ (location, Qleft_margin))
4988 it->area = LEFT_MARGIN_AREA;
4989 else
4990 it->area = RIGHT_MARGIN_AREA;
4992 if (STRINGP (value))
4994 it->string = value;
4995 it->multibyte_p = STRING_MULTIBYTE (it->string);
4996 it->current.overlay_string_index = -1;
4997 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4998 it->end_charpos = it->string_nchars = SCHARS (it->string);
4999 it->method = GET_FROM_STRING;
5000 it->stop_charpos = 0;
5001 it->prev_stop = 0;
5002 it->base_level_stop = 0;
5003 it->string_from_display_prop_p = 1;
5004 /* Say that we haven't consumed the characters with
5005 `display' property yet. The call to pop_it in
5006 set_iterator_to_next will clean this up. */
5007 if (BUFFERP (object))
5008 *position = start_pos;
5010 /* Force paragraph direction to be that of the parent
5011 object. If the parent object's paragraph direction is
5012 not yet determined, default to L2R. */
5013 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5014 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5015 else
5016 it->paragraph_embedding = L2R;
5018 /* Set up the bidi iterator for this display string. */
5019 if (it->bidi_p)
5021 it->bidi_it.string.lstring = it->string;
5022 it->bidi_it.string.s = NULL;
5023 it->bidi_it.string.schars = it->end_charpos;
5024 it->bidi_it.string.bufpos = bufpos;
5025 it->bidi_it.string.from_disp_str = 1;
5026 it->bidi_it.string.unibyte = !it->multibyte_p;
5027 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5030 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5032 it->method = GET_FROM_STRETCH;
5033 it->object = value;
5034 *position = it->position = start_pos;
5035 retval = 1 + (it->area == TEXT_AREA);
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 else
5040 it->what = IT_IMAGE;
5041 it->image_id = lookup_image (it->f, value);
5042 it->position = start_pos;
5043 it->object = NILP (object) ? it->w->buffer : object;
5044 it->method = GET_FROM_IMAGE;
5046 /* Say that we haven't consumed the characters with
5047 `display' property yet. The call to pop_it in
5048 set_iterator_to_next will clean this up. */
5049 *position = start_pos;
5051 #endif /* HAVE_WINDOW_SYSTEM */
5053 return retval;
5056 /* Invalid property or property not supported. Restore
5057 POSITION to what it was before. */
5058 *position = start_pos;
5059 return 0;
5062 /* Check if PROP is a display property value whose text should be
5063 treated as intangible. OVERLAY is the overlay from which PROP
5064 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5065 specify the buffer position covered by PROP. */
5068 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5069 ptrdiff_t charpos, ptrdiff_t bytepos)
5071 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5072 struct text_pos position;
5074 SET_TEXT_POS (position, charpos, bytepos);
5075 return handle_display_spec (NULL, prop, Qnil, overlay,
5076 &position, charpos, frame_window_p);
5080 /* Return 1 if PROP is a display sub-property value containing STRING.
5082 Implementation note: this and the following function are really
5083 special cases of handle_display_spec and
5084 handle_single_display_spec, and should ideally use the same code.
5085 Until they do, these two pairs must be consistent and must be
5086 modified in sync. */
5088 static int
5089 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5091 if (EQ (string, prop))
5092 return 1;
5094 /* Skip over `when FORM'. */
5095 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5097 prop = XCDR (prop);
5098 if (!CONSP (prop))
5099 return 0;
5100 /* Actually, the condition following `when' should be eval'ed,
5101 like handle_single_display_spec does, and we should return
5102 zero if it evaluates to nil. However, this function is
5103 called only when the buffer was already displayed and some
5104 glyph in the glyph matrix was found to come from a display
5105 string. Therefore, the condition was already evaluated, and
5106 the result was non-nil, otherwise the display string wouldn't
5107 have been displayed and we would have never been called for
5108 this property. Thus, we can skip the evaluation and assume
5109 its result is non-nil. */
5110 prop = XCDR (prop);
5113 if (CONSP (prop))
5114 /* Skip over `margin LOCATION'. */
5115 if (EQ (XCAR (prop), Qmargin))
5117 prop = XCDR (prop);
5118 if (!CONSP (prop))
5119 return 0;
5121 prop = XCDR (prop);
5122 if (!CONSP (prop))
5123 return 0;
5126 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5130 /* Return 1 if STRING appears in the `display' property PROP. */
5132 static int
5133 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5135 if (CONSP (prop)
5136 && !EQ (XCAR (prop), Qwhen)
5137 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5139 /* A list of sub-properties. */
5140 while (CONSP (prop))
5142 if (single_display_spec_string_p (XCAR (prop), string))
5143 return 1;
5144 prop = XCDR (prop);
5147 else if (VECTORP (prop))
5149 /* A vector of sub-properties. */
5150 ptrdiff_t i;
5151 for (i = 0; i < ASIZE (prop); ++i)
5152 if (single_display_spec_string_p (AREF (prop, i), string))
5153 return 1;
5155 else
5156 return single_display_spec_string_p (prop, string);
5158 return 0;
5161 /* Look for STRING in overlays and text properties in the current
5162 buffer, between character positions FROM and TO (excluding TO).
5163 BACK_P non-zero means look back (in this case, TO is supposed to be
5164 less than FROM).
5165 Value is the first character position where STRING was found, or
5166 zero if it wasn't found before hitting TO.
5168 This function may only use code that doesn't eval because it is
5169 called asynchronously from note_mouse_highlight. */
5171 static ptrdiff_t
5172 string_buffer_position_lim (Lisp_Object string,
5173 ptrdiff_t from, ptrdiff_t to, int back_p)
5175 Lisp_Object limit, prop, pos;
5176 int found = 0;
5178 pos = make_number (max (from, BEGV));
5180 if (!back_p) /* looking forward */
5182 limit = make_number (min (to, ZV));
5183 while (!found && !EQ (pos, limit))
5185 prop = Fget_char_property (pos, Qdisplay, Qnil);
5186 if (!NILP (prop) && display_prop_string_p (prop, string))
5187 found = 1;
5188 else
5189 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5190 limit);
5193 else /* looking back */
5195 limit = make_number (max (to, BEGV));
5196 while (!found && !EQ (pos, limit))
5198 prop = Fget_char_property (pos, Qdisplay, Qnil);
5199 if (!NILP (prop) && display_prop_string_p (prop, string))
5200 found = 1;
5201 else
5202 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5203 limit);
5207 return found ? XINT (pos) : 0;
5210 /* Determine which buffer position in current buffer STRING comes from.
5211 AROUND_CHARPOS is an approximate position where it could come from.
5212 Value is the buffer position or 0 if it couldn't be determined.
5214 This function is necessary because we don't record buffer positions
5215 in glyphs generated from strings (to keep struct glyph small).
5216 This function may only use code that doesn't eval because it is
5217 called asynchronously from note_mouse_highlight. */
5219 static ptrdiff_t
5220 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5222 const int MAX_DISTANCE = 1000;
5223 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5224 around_charpos + MAX_DISTANCE,
5227 if (!found)
5228 found = string_buffer_position_lim (string, around_charpos,
5229 around_charpos - MAX_DISTANCE, 1);
5230 return found;
5235 /***********************************************************************
5236 `composition' property
5237 ***********************************************************************/
5239 /* Set up iterator IT from `composition' property at its current
5240 position. Called from handle_stop. */
5242 static enum prop_handled
5243 handle_composition_prop (struct it *it)
5245 Lisp_Object prop, string;
5246 ptrdiff_t pos, pos_byte, start, end;
5248 if (STRINGP (it->string))
5250 unsigned char *s;
5252 pos = IT_STRING_CHARPOS (*it);
5253 pos_byte = IT_STRING_BYTEPOS (*it);
5254 string = it->string;
5255 s = SDATA (string) + pos_byte;
5256 it->c = STRING_CHAR (s);
5258 else
5260 pos = IT_CHARPOS (*it);
5261 pos_byte = IT_BYTEPOS (*it);
5262 string = Qnil;
5263 it->c = FETCH_CHAR (pos_byte);
5266 /* If there's a valid composition and point is not inside of the
5267 composition (in the case that the composition is from the current
5268 buffer), draw a glyph composed from the composition components. */
5269 if (find_composition (pos, -1, &start, &end, &prop, string)
5270 && COMPOSITION_VALID_P (start, end, prop)
5271 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5273 if (start < pos)
5274 /* As we can't handle this situation (perhaps font-lock added
5275 a new composition), we just return here hoping that next
5276 redisplay will detect this composition much earlier. */
5277 return HANDLED_NORMALLY;
5278 if (start != pos)
5280 if (STRINGP (it->string))
5281 pos_byte = string_char_to_byte (it->string, start);
5282 else
5283 pos_byte = CHAR_TO_BYTE (start);
5285 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5286 prop, string);
5288 if (it->cmp_it.id >= 0)
5290 it->cmp_it.ch = -1;
5291 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5292 it->cmp_it.nglyphs = -1;
5296 return HANDLED_NORMALLY;
5301 /***********************************************************************
5302 Overlay strings
5303 ***********************************************************************/
5305 /* The following structure is used to record overlay strings for
5306 later sorting in load_overlay_strings. */
5308 struct overlay_entry
5310 Lisp_Object overlay;
5311 Lisp_Object string;
5312 EMACS_INT priority;
5313 int after_string_p;
5317 /* Set up iterator IT from overlay strings at its current position.
5318 Called from handle_stop. */
5320 static enum prop_handled
5321 handle_overlay_change (struct it *it)
5323 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5324 return HANDLED_RECOMPUTE_PROPS;
5325 else
5326 return HANDLED_NORMALLY;
5330 /* Set up the next overlay string for delivery by IT, if there is an
5331 overlay string to deliver. Called by set_iterator_to_next when the
5332 end of the current overlay string is reached. If there are more
5333 overlay strings to display, IT->string and
5334 IT->current.overlay_string_index are set appropriately here.
5335 Otherwise IT->string is set to nil. */
5337 static void
5338 next_overlay_string (struct it *it)
5340 ++it->current.overlay_string_index;
5341 if (it->current.overlay_string_index == it->n_overlay_strings)
5343 /* No more overlay strings. Restore IT's settings to what
5344 they were before overlay strings were processed, and
5345 continue to deliver from current_buffer. */
5347 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5348 pop_it (it);
5349 eassert (it->sp > 0
5350 || (NILP (it->string)
5351 && it->method == GET_FROM_BUFFER
5352 && it->stop_charpos >= BEGV
5353 && it->stop_charpos <= it->end_charpos));
5354 it->current.overlay_string_index = -1;
5355 it->n_overlay_strings = 0;
5356 it->overlay_strings_charpos = -1;
5357 /* If there's an empty display string on the stack, pop the
5358 stack, to resync the bidi iterator with IT's position. Such
5359 empty strings are pushed onto the stack in
5360 get_overlay_strings_1. */
5361 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5362 pop_it (it);
5364 /* If we're at the end of the buffer, record that we have
5365 processed the overlay strings there already, so that
5366 next_element_from_buffer doesn't try it again. */
5367 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5368 it->overlay_strings_at_end_processed_p = 1;
5370 else
5372 /* There are more overlay strings to process. If
5373 IT->current.overlay_string_index has advanced to a position
5374 where we must load IT->overlay_strings with more strings, do
5375 it. We must load at the IT->overlay_strings_charpos where
5376 IT->n_overlay_strings was originally computed; when invisible
5377 text is present, this might not be IT_CHARPOS (Bug#7016). */
5378 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5380 if (it->current.overlay_string_index && i == 0)
5381 load_overlay_strings (it, it->overlay_strings_charpos);
5383 /* Initialize IT to deliver display elements from the overlay
5384 string. */
5385 it->string = it->overlay_strings[i];
5386 it->multibyte_p = STRING_MULTIBYTE (it->string);
5387 SET_TEXT_POS (it->current.string_pos, 0, 0);
5388 it->method = GET_FROM_STRING;
5389 it->stop_charpos = 0;
5390 it->end_charpos = SCHARS (it->string);
5391 if (it->cmp_it.stop_pos >= 0)
5392 it->cmp_it.stop_pos = 0;
5393 it->prev_stop = 0;
5394 it->base_level_stop = 0;
5396 /* Set up the bidi iterator for this overlay string. */
5397 if (it->bidi_p)
5399 it->bidi_it.string.lstring = it->string;
5400 it->bidi_it.string.s = NULL;
5401 it->bidi_it.string.schars = SCHARS (it->string);
5402 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5403 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5404 it->bidi_it.string.unibyte = !it->multibyte_p;
5405 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5409 CHECK_IT (it);
5413 /* Compare two overlay_entry structures E1 and E2. Used as a
5414 comparison function for qsort in load_overlay_strings. Overlay
5415 strings for the same position are sorted so that
5417 1. All after-strings come in front of before-strings, except
5418 when they come from the same overlay.
5420 2. Within after-strings, strings are sorted so that overlay strings
5421 from overlays with higher priorities come first.
5423 2. Within before-strings, strings are sorted so that overlay
5424 strings from overlays with higher priorities come last.
5426 Value is analogous to strcmp. */
5429 static int
5430 compare_overlay_entries (const void *e1, const void *e2)
5432 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5433 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5434 int result;
5436 if (entry1->after_string_p != entry2->after_string_p)
5438 /* Let after-strings appear in front of before-strings if
5439 they come from different overlays. */
5440 if (EQ (entry1->overlay, entry2->overlay))
5441 result = entry1->after_string_p ? 1 : -1;
5442 else
5443 result = entry1->after_string_p ? -1 : 1;
5445 else if (entry1->priority != entry2->priority)
5447 if (entry1->after_string_p)
5448 /* After-strings sorted in order of decreasing priority. */
5449 result = entry2->priority < entry1->priority ? -1 : 1;
5450 else
5451 /* Before-strings sorted in order of increasing priority. */
5452 result = entry1->priority < entry2->priority ? -1 : 1;
5454 else
5455 result = 0;
5457 return result;
5461 /* Load the vector IT->overlay_strings with overlay strings from IT's
5462 current buffer position, or from CHARPOS if that is > 0. Set
5463 IT->n_overlays to the total number of overlay strings found.
5465 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5466 a time. On entry into load_overlay_strings,
5467 IT->current.overlay_string_index gives the number of overlay
5468 strings that have already been loaded by previous calls to this
5469 function.
5471 IT->add_overlay_start contains an additional overlay start
5472 position to consider for taking overlay strings from, if non-zero.
5473 This position comes into play when the overlay has an `invisible'
5474 property, and both before and after-strings. When we've skipped to
5475 the end of the overlay, because of its `invisible' property, we
5476 nevertheless want its before-string to appear.
5477 IT->add_overlay_start will contain the overlay start position
5478 in this case.
5480 Overlay strings are sorted so that after-string strings come in
5481 front of before-string strings. Within before and after-strings,
5482 strings are sorted by overlay priority. See also function
5483 compare_overlay_entries. */
5485 static void
5486 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5488 Lisp_Object overlay, window, str, invisible;
5489 struct Lisp_Overlay *ov;
5490 ptrdiff_t start, end;
5491 ptrdiff_t size = 20;
5492 ptrdiff_t n = 0, i, j;
5493 int invis_p;
5494 struct overlay_entry *entries = alloca (size * sizeof *entries);
5495 USE_SAFE_ALLOCA;
5497 if (charpos <= 0)
5498 charpos = IT_CHARPOS (*it);
5500 /* Append the overlay string STRING of overlay OVERLAY to vector
5501 `entries' which has size `size' and currently contains `n'
5502 elements. AFTER_P non-zero means STRING is an after-string of
5503 OVERLAY. */
5504 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5505 do \
5507 Lisp_Object priority; \
5509 if (n == size) \
5511 struct overlay_entry *old = entries; \
5512 SAFE_NALLOCA (entries, 2, size); \
5513 memcpy (entries, old, size * sizeof *entries); \
5514 size *= 2; \
5517 entries[n].string = (STRING); \
5518 entries[n].overlay = (OVERLAY); \
5519 priority = Foverlay_get ((OVERLAY), Qpriority); \
5520 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5521 entries[n].after_string_p = (AFTER_P); \
5522 ++n; \
5524 while (0)
5526 /* Process overlay before the overlay center. */
5527 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5529 XSETMISC (overlay, ov);
5530 eassert (OVERLAYP (overlay));
5531 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5532 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5534 if (end < charpos)
5535 break;
5537 /* Skip this overlay if it doesn't start or end at IT's current
5538 position. */
5539 if (end != charpos && start != charpos)
5540 continue;
5542 /* Skip this overlay if it doesn't apply to IT->w. */
5543 window = Foverlay_get (overlay, Qwindow);
5544 if (WINDOWP (window) && XWINDOW (window) != it->w)
5545 continue;
5547 /* If the text ``under'' the overlay is invisible, both before-
5548 and after-strings from this overlay are visible; start and
5549 end position are indistinguishable. */
5550 invisible = Foverlay_get (overlay, Qinvisible);
5551 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5553 /* If overlay has a non-empty before-string, record it. */
5554 if ((start == charpos || (end == charpos && invis_p))
5555 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5556 && SCHARS (str))
5557 RECORD_OVERLAY_STRING (overlay, str, 0);
5559 /* If overlay has a non-empty after-string, record it. */
5560 if ((end == charpos || (start == charpos && invis_p))
5561 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5562 && SCHARS (str))
5563 RECORD_OVERLAY_STRING (overlay, str, 1);
5566 /* Process overlays after the overlay center. */
5567 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5569 XSETMISC (overlay, ov);
5570 eassert (OVERLAYP (overlay));
5571 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5572 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5574 if (start > charpos)
5575 break;
5577 /* Skip this overlay if it doesn't start or end at IT's current
5578 position. */
5579 if (end != charpos && start != charpos)
5580 continue;
5582 /* Skip this overlay if it doesn't apply to IT->w. */
5583 window = Foverlay_get (overlay, Qwindow);
5584 if (WINDOWP (window) && XWINDOW (window) != it->w)
5585 continue;
5587 /* If the text ``under'' the overlay is invisible, it has a zero
5588 dimension, and both before- and after-strings apply. */
5589 invisible = Foverlay_get (overlay, Qinvisible);
5590 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5592 /* If overlay has a non-empty before-string, record it. */
5593 if ((start == charpos || (end == charpos && invis_p))
5594 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5595 && SCHARS (str))
5596 RECORD_OVERLAY_STRING (overlay, str, 0);
5598 /* If overlay has a non-empty after-string, record it. */
5599 if ((end == charpos || (start == charpos && invis_p))
5600 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5601 && SCHARS (str))
5602 RECORD_OVERLAY_STRING (overlay, str, 1);
5605 #undef RECORD_OVERLAY_STRING
5607 /* Sort entries. */
5608 if (n > 1)
5609 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5611 /* Record number of overlay strings, and where we computed it. */
5612 it->n_overlay_strings = n;
5613 it->overlay_strings_charpos = charpos;
5615 /* IT->current.overlay_string_index is the number of overlay strings
5616 that have already been consumed by IT. Copy some of the
5617 remaining overlay strings to IT->overlay_strings. */
5618 i = 0;
5619 j = it->current.overlay_string_index;
5620 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5622 it->overlay_strings[i] = entries[j].string;
5623 it->string_overlays[i++] = entries[j++].overlay;
5626 CHECK_IT (it);
5627 SAFE_FREE ();
5631 /* Get the first chunk of overlay strings at IT's current buffer
5632 position, or at CHARPOS if that is > 0. Value is non-zero if at
5633 least one overlay string was found. */
5635 static int
5636 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5638 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5639 process. This fills IT->overlay_strings with strings, and sets
5640 IT->n_overlay_strings to the total number of strings to process.
5641 IT->pos.overlay_string_index has to be set temporarily to zero
5642 because load_overlay_strings needs this; it must be set to -1
5643 when no overlay strings are found because a zero value would
5644 indicate a position in the first overlay string. */
5645 it->current.overlay_string_index = 0;
5646 load_overlay_strings (it, charpos);
5648 /* If we found overlay strings, set up IT to deliver display
5649 elements from the first one. Otherwise set up IT to deliver
5650 from current_buffer. */
5651 if (it->n_overlay_strings)
5653 /* Make sure we know settings in current_buffer, so that we can
5654 restore meaningful values when we're done with the overlay
5655 strings. */
5656 if (compute_stop_p)
5657 compute_stop_pos (it);
5658 eassert (it->face_id >= 0);
5660 /* Save IT's settings. They are restored after all overlay
5661 strings have been processed. */
5662 eassert (!compute_stop_p || it->sp == 0);
5664 /* When called from handle_stop, there might be an empty display
5665 string loaded. In that case, don't bother saving it. But
5666 don't use this optimization with the bidi iterator, since we
5667 need the corresponding pop_it call to resync the bidi
5668 iterator's position with IT's position, after we are done
5669 with the overlay strings. (The corresponding call to pop_it
5670 in case of an empty display string is in
5671 next_overlay_string.) */
5672 if (!(!it->bidi_p
5673 && STRINGP (it->string) && !SCHARS (it->string)))
5674 push_it (it, NULL);
5676 /* Set up IT to deliver display elements from the first overlay
5677 string. */
5678 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5679 it->string = it->overlay_strings[0];
5680 it->from_overlay = Qnil;
5681 it->stop_charpos = 0;
5682 eassert (STRINGP (it->string));
5683 it->end_charpos = SCHARS (it->string);
5684 it->prev_stop = 0;
5685 it->base_level_stop = 0;
5686 it->multibyte_p = STRING_MULTIBYTE (it->string);
5687 it->method = GET_FROM_STRING;
5688 it->from_disp_prop_p = 0;
5690 /* Force paragraph direction to be that of the parent
5691 buffer. */
5692 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5693 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5694 else
5695 it->paragraph_embedding = L2R;
5697 /* Set up the bidi iterator for this overlay string. */
5698 if (it->bidi_p)
5700 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5702 it->bidi_it.string.lstring = it->string;
5703 it->bidi_it.string.s = NULL;
5704 it->bidi_it.string.schars = SCHARS (it->string);
5705 it->bidi_it.string.bufpos = pos;
5706 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5707 it->bidi_it.string.unibyte = !it->multibyte_p;
5708 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5710 return 1;
5713 it->current.overlay_string_index = -1;
5714 return 0;
5717 static int
5718 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5720 it->string = Qnil;
5721 it->method = GET_FROM_BUFFER;
5723 (void) get_overlay_strings_1 (it, charpos, 1);
5725 CHECK_IT (it);
5727 /* Value is non-zero if we found at least one overlay string. */
5728 return STRINGP (it->string);
5733 /***********************************************************************
5734 Saving and restoring state
5735 ***********************************************************************/
5737 /* Save current settings of IT on IT->stack. Called, for example,
5738 before setting up IT for an overlay string, to be able to restore
5739 IT's settings to what they were after the overlay string has been
5740 processed. If POSITION is non-NULL, it is the position to save on
5741 the stack instead of IT->position. */
5743 static void
5744 push_it (struct it *it, struct text_pos *position)
5746 struct iterator_stack_entry *p;
5748 eassert (it->sp < IT_STACK_SIZE);
5749 p = it->stack + it->sp;
5751 p->stop_charpos = it->stop_charpos;
5752 p->prev_stop = it->prev_stop;
5753 p->base_level_stop = it->base_level_stop;
5754 p->cmp_it = it->cmp_it;
5755 eassert (it->face_id >= 0);
5756 p->face_id = it->face_id;
5757 p->string = it->string;
5758 p->method = it->method;
5759 p->from_overlay = it->from_overlay;
5760 switch (p->method)
5762 case GET_FROM_IMAGE:
5763 p->u.image.object = it->object;
5764 p->u.image.image_id = it->image_id;
5765 p->u.image.slice = it->slice;
5766 break;
5767 case GET_FROM_STRETCH:
5768 p->u.stretch.object = it->object;
5769 break;
5771 p->position = position ? *position : it->position;
5772 p->current = it->current;
5773 p->end_charpos = it->end_charpos;
5774 p->string_nchars = it->string_nchars;
5775 p->area = it->area;
5776 p->multibyte_p = it->multibyte_p;
5777 p->avoid_cursor_p = it->avoid_cursor_p;
5778 p->space_width = it->space_width;
5779 p->font_height = it->font_height;
5780 p->voffset = it->voffset;
5781 p->string_from_display_prop_p = it->string_from_display_prop_p;
5782 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5783 p->display_ellipsis_p = 0;
5784 p->line_wrap = it->line_wrap;
5785 p->bidi_p = it->bidi_p;
5786 p->paragraph_embedding = it->paragraph_embedding;
5787 p->from_disp_prop_p = it->from_disp_prop_p;
5788 ++it->sp;
5790 /* Save the state of the bidi iterator as well. */
5791 if (it->bidi_p)
5792 bidi_push_it (&it->bidi_it);
5795 static void
5796 iterate_out_of_display_property (struct it *it)
5798 int buffer_p = !STRINGP (it->string);
5799 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5800 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5802 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5804 /* Maybe initialize paragraph direction. If we are at the beginning
5805 of a new paragraph, next_element_from_buffer may not have a
5806 chance to do that. */
5807 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5808 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5809 /* prev_stop can be zero, so check against BEGV as well. */
5810 while (it->bidi_it.charpos >= bob
5811 && it->prev_stop <= it->bidi_it.charpos
5812 && it->bidi_it.charpos < CHARPOS (it->position)
5813 && it->bidi_it.charpos < eob)
5814 bidi_move_to_visually_next (&it->bidi_it);
5815 /* Record the stop_pos we just crossed, for when we cross it
5816 back, maybe. */
5817 if (it->bidi_it.charpos > CHARPOS (it->position))
5818 it->prev_stop = CHARPOS (it->position);
5819 /* If we ended up not where pop_it put us, resync IT's
5820 positional members with the bidi iterator. */
5821 if (it->bidi_it.charpos != CHARPOS (it->position))
5822 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5823 if (buffer_p)
5824 it->current.pos = it->position;
5825 else
5826 it->current.string_pos = it->position;
5829 /* Restore IT's settings from IT->stack. Called, for example, when no
5830 more overlay strings must be processed, and we return to delivering
5831 display elements from a buffer, or when the end of a string from a
5832 `display' property is reached and we return to delivering display
5833 elements from an overlay string, or from a buffer. */
5835 static void
5836 pop_it (struct it *it)
5838 struct iterator_stack_entry *p;
5839 int from_display_prop = it->from_disp_prop_p;
5841 eassert (it->sp > 0);
5842 --it->sp;
5843 p = it->stack + it->sp;
5844 it->stop_charpos = p->stop_charpos;
5845 it->prev_stop = p->prev_stop;
5846 it->base_level_stop = p->base_level_stop;
5847 it->cmp_it = p->cmp_it;
5848 it->face_id = p->face_id;
5849 it->current = p->current;
5850 it->position = p->position;
5851 it->string = p->string;
5852 it->from_overlay = p->from_overlay;
5853 if (NILP (it->string))
5854 SET_TEXT_POS (it->current.string_pos, -1, -1);
5855 it->method = p->method;
5856 switch (it->method)
5858 case GET_FROM_IMAGE:
5859 it->image_id = p->u.image.image_id;
5860 it->object = p->u.image.object;
5861 it->slice = p->u.image.slice;
5862 break;
5863 case GET_FROM_STRETCH:
5864 it->object = p->u.stretch.object;
5865 break;
5866 case GET_FROM_BUFFER:
5867 it->object = it->w->buffer;
5868 break;
5869 case GET_FROM_STRING:
5870 it->object = it->string;
5871 break;
5872 case GET_FROM_DISPLAY_VECTOR:
5873 if (it->s)
5874 it->method = GET_FROM_C_STRING;
5875 else if (STRINGP (it->string))
5876 it->method = GET_FROM_STRING;
5877 else
5879 it->method = GET_FROM_BUFFER;
5880 it->object = it->w->buffer;
5883 it->end_charpos = p->end_charpos;
5884 it->string_nchars = p->string_nchars;
5885 it->area = p->area;
5886 it->multibyte_p = p->multibyte_p;
5887 it->avoid_cursor_p = p->avoid_cursor_p;
5888 it->space_width = p->space_width;
5889 it->font_height = p->font_height;
5890 it->voffset = p->voffset;
5891 it->string_from_display_prop_p = p->string_from_display_prop_p;
5892 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5893 it->line_wrap = p->line_wrap;
5894 it->bidi_p = p->bidi_p;
5895 it->paragraph_embedding = p->paragraph_embedding;
5896 it->from_disp_prop_p = p->from_disp_prop_p;
5897 if (it->bidi_p)
5899 bidi_pop_it (&it->bidi_it);
5900 /* Bidi-iterate until we get out of the portion of text, if any,
5901 covered by a `display' text property or by an overlay with
5902 `display' property. (We cannot just jump there, because the
5903 internal coherency of the bidi iterator state can not be
5904 preserved across such jumps.) We also must determine the
5905 paragraph base direction if the overlay we just processed is
5906 at the beginning of a new paragraph. */
5907 if (from_display_prop
5908 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5909 iterate_out_of_display_property (it);
5911 eassert ((BUFFERP (it->object)
5912 && IT_CHARPOS (*it) == it->bidi_it.charpos
5913 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5914 || (STRINGP (it->object)
5915 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5916 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5917 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5923 /***********************************************************************
5924 Moving over lines
5925 ***********************************************************************/
5927 /* Set IT's current position to the previous line start. */
5929 static void
5930 back_to_previous_line_start (struct it *it)
5932 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5933 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5937 /* Move IT to the next line start.
5939 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5940 we skipped over part of the text (as opposed to moving the iterator
5941 continuously over the text). Otherwise, don't change the value
5942 of *SKIPPED_P.
5944 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5945 iterator on the newline, if it was found.
5947 Newlines may come from buffer text, overlay strings, or strings
5948 displayed via the `display' property. That's the reason we can't
5949 simply use find_next_newline_no_quit.
5951 Note that this function may not skip over invisible text that is so
5952 because of text properties and immediately follows a newline. If
5953 it would, function reseat_at_next_visible_line_start, when called
5954 from set_iterator_to_next, would effectively make invisible
5955 characters following a newline part of the wrong glyph row, which
5956 leads to wrong cursor motion. */
5958 static int
5959 forward_to_next_line_start (struct it *it, int *skipped_p,
5960 struct bidi_it *bidi_it_prev)
5962 ptrdiff_t old_selective;
5963 int newline_found_p, n;
5964 const int MAX_NEWLINE_DISTANCE = 500;
5966 /* If already on a newline, just consume it to avoid unintended
5967 skipping over invisible text below. */
5968 if (it->what == IT_CHARACTER
5969 && it->c == '\n'
5970 && CHARPOS (it->position) == IT_CHARPOS (*it))
5972 if (it->bidi_p && bidi_it_prev)
5973 *bidi_it_prev = it->bidi_it;
5974 set_iterator_to_next (it, 0);
5975 it->c = 0;
5976 return 1;
5979 /* Don't handle selective display in the following. It's (a)
5980 unnecessary because it's done by the caller, and (b) leads to an
5981 infinite recursion because next_element_from_ellipsis indirectly
5982 calls this function. */
5983 old_selective = it->selective;
5984 it->selective = 0;
5986 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5987 from buffer text. */
5988 for (n = newline_found_p = 0;
5989 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5990 n += STRINGP (it->string) ? 0 : 1)
5992 if (!get_next_display_element (it))
5993 return 0;
5994 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5995 if (newline_found_p && it->bidi_p && bidi_it_prev)
5996 *bidi_it_prev = it->bidi_it;
5997 set_iterator_to_next (it, 0);
6000 /* If we didn't find a newline near enough, see if we can use a
6001 short-cut. */
6002 if (!newline_found_p)
6004 ptrdiff_t start = IT_CHARPOS (*it);
6005 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6006 Lisp_Object pos;
6008 eassert (!STRINGP (it->string));
6010 /* If there isn't any `display' property in sight, and no
6011 overlays, we can just use the position of the newline in
6012 buffer text. */
6013 if (it->stop_charpos >= limit
6014 || ((pos = Fnext_single_property_change (make_number (start),
6015 Qdisplay, Qnil,
6016 make_number (limit)),
6017 NILP (pos))
6018 && next_overlay_change (start) == ZV))
6020 if (!it->bidi_p)
6022 IT_CHARPOS (*it) = limit;
6023 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6025 else
6027 struct bidi_it bprev;
6029 /* Help bidi.c avoid expensive searches for display
6030 properties and overlays, by telling it that there are
6031 none up to `limit'. */
6032 if (it->bidi_it.disp_pos < limit)
6034 it->bidi_it.disp_pos = limit;
6035 it->bidi_it.disp_prop = 0;
6037 do {
6038 bprev = it->bidi_it;
6039 bidi_move_to_visually_next (&it->bidi_it);
6040 } while (it->bidi_it.charpos != limit);
6041 IT_CHARPOS (*it) = limit;
6042 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6043 if (bidi_it_prev)
6044 *bidi_it_prev = bprev;
6046 *skipped_p = newline_found_p = 1;
6048 else
6050 while (get_next_display_element (it)
6051 && !newline_found_p)
6053 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6054 if (newline_found_p && it->bidi_p && bidi_it_prev)
6055 *bidi_it_prev = it->bidi_it;
6056 set_iterator_to_next (it, 0);
6061 it->selective = old_selective;
6062 return newline_found_p;
6066 /* Set IT's current position to the previous visible line start. Skip
6067 invisible text that is so either due to text properties or due to
6068 selective display. Caution: this does not change IT->current_x and
6069 IT->hpos. */
6071 static void
6072 back_to_previous_visible_line_start (struct it *it)
6074 while (IT_CHARPOS (*it) > BEGV)
6076 back_to_previous_line_start (it);
6078 if (IT_CHARPOS (*it) <= BEGV)
6079 break;
6081 /* If selective > 0, then lines indented more than its value are
6082 invisible. */
6083 if (it->selective > 0
6084 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6085 it->selective))
6086 continue;
6088 /* Check the newline before point for invisibility. */
6090 Lisp_Object prop;
6091 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6092 Qinvisible, it->window);
6093 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6094 continue;
6097 if (IT_CHARPOS (*it) <= BEGV)
6098 break;
6101 struct it it2;
6102 void *it2data = NULL;
6103 ptrdiff_t pos;
6104 ptrdiff_t beg, end;
6105 Lisp_Object val, overlay;
6107 SAVE_IT (it2, *it, it2data);
6109 /* If newline is part of a composition, continue from start of composition */
6110 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6111 && beg < IT_CHARPOS (*it))
6112 goto replaced;
6114 /* If newline is replaced by a display property, find start of overlay
6115 or interval and continue search from that point. */
6116 pos = --IT_CHARPOS (it2);
6117 --IT_BYTEPOS (it2);
6118 it2.sp = 0;
6119 bidi_unshelve_cache (NULL, 0);
6120 it2.string_from_display_prop_p = 0;
6121 it2.from_disp_prop_p = 0;
6122 if (handle_display_prop (&it2) == HANDLED_RETURN
6123 && !NILP (val = get_char_property_and_overlay
6124 (make_number (pos), Qdisplay, Qnil, &overlay))
6125 && (OVERLAYP (overlay)
6126 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6127 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6129 RESTORE_IT (it, it, it2data);
6130 goto replaced;
6133 /* Newline is not replaced by anything -- so we are done. */
6134 RESTORE_IT (it, it, it2data);
6135 break;
6137 replaced:
6138 if (beg < BEGV)
6139 beg = BEGV;
6140 IT_CHARPOS (*it) = beg;
6141 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6145 it->continuation_lines_width = 0;
6147 eassert (IT_CHARPOS (*it) >= BEGV);
6148 eassert (IT_CHARPOS (*it) == BEGV
6149 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6150 CHECK_IT (it);
6154 /* Reseat iterator IT at the previous visible line start. Skip
6155 invisible text that is so either due to text properties or due to
6156 selective display. At the end, update IT's overlay information,
6157 face information etc. */
6159 void
6160 reseat_at_previous_visible_line_start (struct it *it)
6162 back_to_previous_visible_line_start (it);
6163 reseat (it, it->current.pos, 1);
6164 CHECK_IT (it);
6168 /* Reseat iterator IT on the next visible line start in the current
6169 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6170 preceding the line start. Skip over invisible text that is so
6171 because of selective display. Compute faces, overlays etc at the
6172 new position. Note that this function does not skip over text that
6173 is invisible because of text properties. */
6175 static void
6176 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6178 int newline_found_p, skipped_p = 0;
6179 struct bidi_it bidi_it_prev;
6181 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6183 /* Skip over lines that are invisible because they are indented
6184 more than the value of IT->selective. */
6185 if (it->selective > 0)
6186 while (IT_CHARPOS (*it) < ZV
6187 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6188 it->selective))
6190 eassert (IT_BYTEPOS (*it) == BEGV
6191 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6192 newline_found_p =
6193 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6196 /* Position on the newline if that's what's requested. */
6197 if (on_newline_p && newline_found_p)
6199 if (STRINGP (it->string))
6201 if (IT_STRING_CHARPOS (*it) > 0)
6203 if (!it->bidi_p)
6205 --IT_STRING_CHARPOS (*it);
6206 --IT_STRING_BYTEPOS (*it);
6208 else
6210 /* We need to restore the bidi iterator to the state
6211 it had on the newline, and resync the IT's
6212 position with that. */
6213 it->bidi_it = bidi_it_prev;
6214 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6215 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6219 else if (IT_CHARPOS (*it) > BEGV)
6221 if (!it->bidi_p)
6223 --IT_CHARPOS (*it);
6224 --IT_BYTEPOS (*it);
6226 else
6228 /* We need to restore the bidi iterator to the state it
6229 had on the newline and resync IT with that. */
6230 it->bidi_it = bidi_it_prev;
6231 IT_CHARPOS (*it) = it->bidi_it.charpos;
6232 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6234 reseat (it, it->current.pos, 0);
6237 else if (skipped_p)
6238 reseat (it, it->current.pos, 0);
6240 CHECK_IT (it);
6245 /***********************************************************************
6246 Changing an iterator's position
6247 ***********************************************************************/
6249 /* Change IT's current position to POS in current_buffer. If FORCE_P
6250 is non-zero, always check for text properties at the new position.
6251 Otherwise, text properties are only looked up if POS >=
6252 IT->check_charpos of a property. */
6254 static void
6255 reseat (struct it *it, struct text_pos pos, int force_p)
6257 ptrdiff_t original_pos = IT_CHARPOS (*it);
6259 reseat_1 (it, pos, 0);
6261 /* Determine where to check text properties. Avoid doing it
6262 where possible because text property lookup is very expensive. */
6263 if (force_p
6264 || CHARPOS (pos) > it->stop_charpos
6265 || CHARPOS (pos) < original_pos)
6267 if (it->bidi_p)
6269 /* For bidi iteration, we need to prime prev_stop and
6270 base_level_stop with our best estimations. */
6271 /* Implementation note: Of course, POS is not necessarily a
6272 stop position, so assigning prev_pos to it is a lie; we
6273 should have called compute_stop_backwards. However, if
6274 the current buffer does not include any R2L characters,
6275 that call would be a waste of cycles, because the
6276 iterator will never move back, and thus never cross this
6277 "fake" stop position. So we delay that backward search
6278 until the time we really need it, in next_element_from_buffer. */
6279 if (CHARPOS (pos) != it->prev_stop)
6280 it->prev_stop = CHARPOS (pos);
6281 if (CHARPOS (pos) < it->base_level_stop)
6282 it->base_level_stop = 0; /* meaning it's unknown */
6283 handle_stop (it);
6285 else
6287 handle_stop (it);
6288 it->prev_stop = it->base_level_stop = 0;
6293 CHECK_IT (it);
6297 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6298 IT->stop_pos to POS, also. */
6300 static void
6301 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6303 /* Don't call this function when scanning a C string. */
6304 eassert (it->s == NULL);
6306 /* POS must be a reasonable value. */
6307 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6309 it->current.pos = it->position = pos;
6310 it->end_charpos = ZV;
6311 it->dpvec = NULL;
6312 it->current.dpvec_index = -1;
6313 it->current.overlay_string_index = -1;
6314 IT_STRING_CHARPOS (*it) = -1;
6315 IT_STRING_BYTEPOS (*it) = -1;
6316 it->string = Qnil;
6317 it->method = GET_FROM_BUFFER;
6318 it->object = it->w->buffer;
6319 it->area = TEXT_AREA;
6320 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6321 it->sp = 0;
6322 it->string_from_display_prop_p = 0;
6323 it->string_from_prefix_prop_p = 0;
6325 it->from_disp_prop_p = 0;
6326 it->face_before_selective_p = 0;
6327 if (it->bidi_p)
6329 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6330 &it->bidi_it);
6331 bidi_unshelve_cache (NULL, 0);
6332 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6333 it->bidi_it.string.s = NULL;
6334 it->bidi_it.string.lstring = Qnil;
6335 it->bidi_it.string.bufpos = 0;
6336 it->bidi_it.string.unibyte = 0;
6339 if (set_stop_p)
6341 it->stop_charpos = CHARPOS (pos);
6342 it->base_level_stop = CHARPOS (pos);
6344 /* This make the information stored in it->cmp_it invalidate. */
6345 it->cmp_it.id = -1;
6349 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6350 If S is non-null, it is a C string to iterate over. Otherwise,
6351 STRING gives a Lisp string to iterate over.
6353 If PRECISION > 0, don't return more then PRECISION number of
6354 characters from the string.
6356 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6357 characters have been returned. FIELD_WIDTH < 0 means an infinite
6358 field width.
6360 MULTIBYTE = 0 means disable processing of multibyte characters,
6361 MULTIBYTE > 0 means enable it,
6362 MULTIBYTE < 0 means use IT->multibyte_p.
6364 IT must be initialized via a prior call to init_iterator before
6365 calling this function. */
6367 static void
6368 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6369 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6370 int multibyte)
6372 /* No region in strings. */
6373 it->region_beg_charpos = it->region_end_charpos = -1;
6375 /* No text property checks performed by default, but see below. */
6376 it->stop_charpos = -1;
6378 /* Set iterator position and end position. */
6379 memset (&it->current, 0, sizeof it->current);
6380 it->current.overlay_string_index = -1;
6381 it->current.dpvec_index = -1;
6382 eassert (charpos >= 0);
6384 /* If STRING is specified, use its multibyteness, otherwise use the
6385 setting of MULTIBYTE, if specified. */
6386 if (multibyte >= 0)
6387 it->multibyte_p = multibyte > 0;
6389 /* Bidirectional reordering of strings is controlled by the default
6390 value of bidi-display-reordering. Don't try to reorder while
6391 loading loadup.el, as the necessary character property tables are
6392 not yet available. */
6393 it->bidi_p =
6394 NILP (Vpurify_flag)
6395 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6397 if (s == NULL)
6399 eassert (STRINGP (string));
6400 it->string = string;
6401 it->s = NULL;
6402 it->end_charpos = it->string_nchars = SCHARS (string);
6403 it->method = GET_FROM_STRING;
6404 it->current.string_pos = string_pos (charpos, string);
6406 if (it->bidi_p)
6408 it->bidi_it.string.lstring = string;
6409 it->bidi_it.string.s = NULL;
6410 it->bidi_it.string.schars = it->end_charpos;
6411 it->bidi_it.string.bufpos = 0;
6412 it->bidi_it.string.from_disp_str = 0;
6413 it->bidi_it.string.unibyte = !it->multibyte_p;
6414 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6415 FRAME_WINDOW_P (it->f), &it->bidi_it);
6418 else
6420 it->s = (const unsigned char *) s;
6421 it->string = Qnil;
6423 /* Note that we use IT->current.pos, not it->current.string_pos,
6424 for displaying C strings. */
6425 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6426 if (it->multibyte_p)
6428 it->current.pos = c_string_pos (charpos, s, 1);
6429 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6431 else
6433 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6434 it->end_charpos = it->string_nchars = strlen (s);
6437 if (it->bidi_p)
6439 it->bidi_it.string.lstring = Qnil;
6440 it->bidi_it.string.s = (const unsigned char *) s;
6441 it->bidi_it.string.schars = it->end_charpos;
6442 it->bidi_it.string.bufpos = 0;
6443 it->bidi_it.string.from_disp_str = 0;
6444 it->bidi_it.string.unibyte = !it->multibyte_p;
6445 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6446 &it->bidi_it);
6448 it->method = GET_FROM_C_STRING;
6451 /* PRECISION > 0 means don't return more than PRECISION characters
6452 from the string. */
6453 if (precision > 0 && it->end_charpos - charpos > precision)
6455 it->end_charpos = it->string_nchars = charpos + precision;
6456 if (it->bidi_p)
6457 it->bidi_it.string.schars = it->end_charpos;
6460 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6461 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6462 FIELD_WIDTH < 0 means infinite field width. This is useful for
6463 padding with `-' at the end of a mode line. */
6464 if (field_width < 0)
6465 field_width = INFINITY;
6466 /* Implementation note: We deliberately don't enlarge
6467 it->bidi_it.string.schars here to fit it->end_charpos, because
6468 the bidi iterator cannot produce characters out of thin air. */
6469 if (field_width > it->end_charpos - charpos)
6470 it->end_charpos = charpos + field_width;
6472 /* Use the standard display table for displaying strings. */
6473 if (DISP_TABLE_P (Vstandard_display_table))
6474 it->dp = XCHAR_TABLE (Vstandard_display_table);
6476 it->stop_charpos = charpos;
6477 it->prev_stop = charpos;
6478 it->base_level_stop = 0;
6479 if (it->bidi_p)
6481 it->bidi_it.first_elt = 1;
6482 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6483 it->bidi_it.disp_pos = -1;
6485 if (s == NULL && it->multibyte_p)
6487 ptrdiff_t endpos = SCHARS (it->string);
6488 if (endpos > it->end_charpos)
6489 endpos = it->end_charpos;
6490 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6491 it->string);
6493 CHECK_IT (it);
6498 /***********************************************************************
6499 Iteration
6500 ***********************************************************************/
6502 /* Map enum it_method value to corresponding next_element_from_* function. */
6504 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6506 next_element_from_buffer,
6507 next_element_from_display_vector,
6508 next_element_from_string,
6509 next_element_from_c_string,
6510 next_element_from_image,
6511 next_element_from_stretch
6514 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6517 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6518 (possibly with the following characters). */
6520 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6521 ((IT)->cmp_it.id >= 0 \
6522 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6523 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6524 END_CHARPOS, (IT)->w, \
6525 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6526 (IT)->string)))
6529 /* Lookup the char-table Vglyphless_char_display for character C (-1
6530 if we want information for no-font case), and return the display
6531 method symbol. By side-effect, update it->what and
6532 it->glyphless_method. This function is called from
6533 get_next_display_element for each character element, and from
6534 x_produce_glyphs when no suitable font was found. */
6536 Lisp_Object
6537 lookup_glyphless_char_display (int c, struct it *it)
6539 Lisp_Object glyphless_method = Qnil;
6541 if (CHAR_TABLE_P (Vglyphless_char_display)
6542 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6544 if (c >= 0)
6546 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6547 if (CONSP (glyphless_method))
6548 glyphless_method = FRAME_WINDOW_P (it->f)
6549 ? XCAR (glyphless_method)
6550 : XCDR (glyphless_method);
6552 else
6553 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6556 retry:
6557 if (NILP (glyphless_method))
6559 if (c >= 0)
6560 /* The default is to display the character by a proper font. */
6561 return Qnil;
6562 /* The default for the no-font case is to display an empty box. */
6563 glyphless_method = Qempty_box;
6565 if (EQ (glyphless_method, Qzero_width))
6567 if (c >= 0)
6568 return glyphless_method;
6569 /* This method can't be used for the no-font case. */
6570 glyphless_method = Qempty_box;
6572 if (EQ (glyphless_method, Qthin_space))
6573 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6574 else if (EQ (glyphless_method, Qempty_box))
6575 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6576 else if (EQ (glyphless_method, Qhex_code))
6577 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6578 else if (STRINGP (glyphless_method))
6579 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6580 else
6582 /* Invalid value. We use the default method. */
6583 glyphless_method = Qnil;
6584 goto retry;
6586 it->what = IT_GLYPHLESS;
6587 return glyphless_method;
6590 /* Load IT's display element fields with information about the next
6591 display element from the current position of IT. Value is zero if
6592 end of buffer (or C string) is reached. */
6594 static struct frame *last_escape_glyph_frame = NULL;
6595 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6596 static int last_escape_glyph_merged_face_id = 0;
6598 struct frame *last_glyphless_glyph_frame = NULL;
6599 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6600 int last_glyphless_glyph_merged_face_id = 0;
6602 static int
6603 get_next_display_element (struct it *it)
6605 /* Non-zero means that we found a display element. Zero means that
6606 we hit the end of what we iterate over. Performance note: the
6607 function pointer `method' used here turns out to be faster than
6608 using a sequence of if-statements. */
6609 int success_p;
6611 get_next:
6612 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6614 if (it->what == IT_CHARACTER)
6616 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6617 and only if (a) the resolved directionality of that character
6618 is R..." */
6619 /* FIXME: Do we need an exception for characters from display
6620 tables? */
6621 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6622 it->c = bidi_mirror_char (it->c);
6623 /* Map via display table or translate control characters.
6624 IT->c, IT->len etc. have been set to the next character by
6625 the function call above. If we have a display table, and it
6626 contains an entry for IT->c, translate it. Don't do this if
6627 IT->c itself comes from a display table, otherwise we could
6628 end up in an infinite recursion. (An alternative could be to
6629 count the recursion depth of this function and signal an
6630 error when a certain maximum depth is reached.) Is it worth
6631 it? */
6632 if (success_p && it->dpvec == NULL)
6634 Lisp_Object dv;
6635 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6636 int nonascii_space_p = 0;
6637 int nonascii_hyphen_p = 0;
6638 int c = it->c; /* This is the character to display. */
6640 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6642 eassert (SINGLE_BYTE_CHAR_P (c));
6643 if (unibyte_display_via_language_environment)
6645 c = DECODE_CHAR (unibyte, c);
6646 if (c < 0)
6647 c = BYTE8_TO_CHAR (it->c);
6649 else
6650 c = BYTE8_TO_CHAR (it->c);
6653 if (it->dp
6654 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6655 VECTORP (dv)))
6657 struct Lisp_Vector *v = XVECTOR (dv);
6659 /* Return the first character from the display table
6660 entry, if not empty. If empty, don't display the
6661 current character. */
6662 if (v->header.size)
6664 it->dpvec_char_len = it->len;
6665 it->dpvec = v->contents;
6666 it->dpend = v->contents + v->header.size;
6667 it->current.dpvec_index = 0;
6668 it->dpvec_face_id = -1;
6669 it->saved_face_id = it->face_id;
6670 it->method = GET_FROM_DISPLAY_VECTOR;
6671 it->ellipsis_p = 0;
6673 else
6675 set_iterator_to_next (it, 0);
6677 goto get_next;
6680 if (! NILP (lookup_glyphless_char_display (c, it)))
6682 if (it->what == IT_GLYPHLESS)
6683 goto done;
6684 /* Don't display this character. */
6685 set_iterator_to_next (it, 0);
6686 goto get_next;
6689 /* If `nobreak-char-display' is non-nil, we display
6690 non-ASCII spaces and hyphens specially. */
6691 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6693 if (c == 0xA0)
6694 nonascii_space_p = 1;
6695 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6696 nonascii_hyphen_p = 1;
6699 /* Translate control characters into `\003' or `^C' form.
6700 Control characters coming from a display table entry are
6701 currently not translated because we use IT->dpvec to hold
6702 the translation. This could easily be changed but I
6703 don't believe that it is worth doing.
6705 The characters handled by `nobreak-char-display' must be
6706 translated too.
6708 Non-printable characters and raw-byte characters are also
6709 translated to octal form. */
6710 if (((c < ' ' || c == 127) /* ASCII control chars */
6711 ? (it->area != TEXT_AREA
6712 /* In mode line, treat \n, \t like other crl chars. */
6713 || (c != '\t'
6714 && it->glyph_row
6715 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6716 || (c != '\n' && c != '\t'))
6717 : (nonascii_space_p
6718 || nonascii_hyphen_p
6719 || CHAR_BYTE8_P (c)
6720 || ! CHAR_PRINTABLE_P (c))))
6722 /* C is a control character, non-ASCII space/hyphen,
6723 raw-byte, or a non-printable character which must be
6724 displayed either as '\003' or as `^C' where the '\\'
6725 and '^' can be defined in the display table. Fill
6726 IT->ctl_chars with glyphs for what we have to
6727 display. Then, set IT->dpvec to these glyphs. */
6728 Lisp_Object gc;
6729 int ctl_len;
6730 int face_id;
6731 int lface_id = 0;
6732 int escape_glyph;
6734 /* Handle control characters with ^. */
6736 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6738 int g;
6740 g = '^'; /* default glyph for Control */
6741 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6742 if (it->dp
6743 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6745 g = GLYPH_CODE_CHAR (gc);
6746 lface_id = GLYPH_CODE_FACE (gc);
6748 if (lface_id)
6750 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6752 else if (it->f == last_escape_glyph_frame
6753 && it->face_id == last_escape_glyph_face_id)
6755 face_id = last_escape_glyph_merged_face_id;
6757 else
6759 /* Merge the escape-glyph face into the current face. */
6760 face_id = merge_faces (it->f, Qescape_glyph, 0,
6761 it->face_id);
6762 last_escape_glyph_frame = it->f;
6763 last_escape_glyph_face_id = it->face_id;
6764 last_escape_glyph_merged_face_id = face_id;
6767 XSETINT (it->ctl_chars[0], g);
6768 XSETINT (it->ctl_chars[1], c ^ 0100);
6769 ctl_len = 2;
6770 goto display_control;
6773 /* Handle non-ascii space in the mode where it only gets
6774 highlighting. */
6776 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6778 /* Merge `nobreak-space' into the current face. */
6779 face_id = merge_faces (it->f, Qnobreak_space, 0,
6780 it->face_id);
6781 XSETINT (it->ctl_chars[0], ' ');
6782 ctl_len = 1;
6783 goto display_control;
6786 /* Handle sequences that start with the "escape glyph". */
6788 /* the default escape glyph is \. */
6789 escape_glyph = '\\';
6791 if (it->dp
6792 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6794 escape_glyph = GLYPH_CODE_CHAR (gc);
6795 lface_id = GLYPH_CODE_FACE (gc);
6797 if (lface_id)
6799 /* The display table specified a face.
6800 Merge it into face_id and also into escape_glyph. */
6801 face_id = merge_faces (it->f, Qt, lface_id,
6802 it->face_id);
6804 else if (it->f == last_escape_glyph_frame
6805 && it->face_id == last_escape_glyph_face_id)
6807 face_id = last_escape_glyph_merged_face_id;
6809 else
6811 /* Merge the escape-glyph face into the current face. */
6812 face_id = merge_faces (it->f, Qescape_glyph, 0,
6813 it->face_id);
6814 last_escape_glyph_frame = it->f;
6815 last_escape_glyph_face_id = it->face_id;
6816 last_escape_glyph_merged_face_id = face_id;
6819 /* Draw non-ASCII hyphen with just highlighting: */
6821 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6823 XSETINT (it->ctl_chars[0], '-');
6824 ctl_len = 1;
6825 goto display_control;
6828 /* Draw non-ASCII space/hyphen with escape glyph: */
6830 if (nonascii_space_p || nonascii_hyphen_p)
6832 XSETINT (it->ctl_chars[0], escape_glyph);
6833 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6834 ctl_len = 2;
6835 goto display_control;
6839 char str[10];
6840 int len, i;
6842 if (CHAR_BYTE8_P (c))
6843 /* Display \200 instead of \17777600. */
6844 c = CHAR_TO_BYTE8 (c);
6845 len = sprintf (str, "%03o", c);
6847 XSETINT (it->ctl_chars[0], escape_glyph);
6848 for (i = 0; i < len; i++)
6849 XSETINT (it->ctl_chars[i + 1], str[i]);
6850 ctl_len = len + 1;
6853 display_control:
6854 /* Set up IT->dpvec and return first character from it. */
6855 it->dpvec_char_len = it->len;
6856 it->dpvec = it->ctl_chars;
6857 it->dpend = it->dpvec + ctl_len;
6858 it->current.dpvec_index = 0;
6859 it->dpvec_face_id = face_id;
6860 it->saved_face_id = it->face_id;
6861 it->method = GET_FROM_DISPLAY_VECTOR;
6862 it->ellipsis_p = 0;
6863 goto get_next;
6865 it->char_to_display = c;
6867 else if (success_p)
6869 it->char_to_display = it->c;
6873 /* Adjust face id for a multibyte character. There are no multibyte
6874 character in unibyte text. */
6875 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6876 && it->multibyte_p
6877 && success_p
6878 && FRAME_WINDOW_P (it->f))
6880 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6882 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6884 /* Automatic composition with glyph-string. */
6885 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6887 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6889 else
6891 ptrdiff_t pos = (it->s ? -1
6892 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6893 : IT_CHARPOS (*it));
6894 int c;
6896 if (it->what == IT_CHARACTER)
6897 c = it->char_to_display;
6898 else
6900 struct composition *cmp = composition_table[it->cmp_it.id];
6901 int i;
6903 c = ' ';
6904 for (i = 0; i < cmp->glyph_len; i++)
6905 /* TAB in a composition means display glyphs with
6906 padding space on the left or right. */
6907 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6908 break;
6910 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6914 done:
6915 /* Is this character the last one of a run of characters with
6916 box? If yes, set IT->end_of_box_run_p to 1. */
6917 if (it->face_box_p
6918 && it->s == NULL)
6920 if (it->method == GET_FROM_STRING && it->sp)
6922 int face_id = underlying_face_id (it);
6923 struct face *face = FACE_FROM_ID (it->f, face_id);
6925 if (face)
6927 if (face->box == FACE_NO_BOX)
6929 /* If the box comes from face properties in a
6930 display string, check faces in that string. */
6931 int string_face_id = face_after_it_pos (it);
6932 it->end_of_box_run_p
6933 = (FACE_FROM_ID (it->f, string_face_id)->box
6934 == FACE_NO_BOX);
6936 /* Otherwise, the box comes from the underlying face.
6937 If this is the last string character displayed, check
6938 the next buffer location. */
6939 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6940 && (it->current.overlay_string_index
6941 == it->n_overlay_strings - 1))
6943 ptrdiff_t ignore;
6944 int next_face_id;
6945 struct text_pos pos = it->current.pos;
6946 INC_TEXT_POS (pos, it->multibyte_p);
6948 next_face_id = face_at_buffer_position
6949 (it->w, CHARPOS (pos), it->region_beg_charpos,
6950 it->region_end_charpos, &ignore,
6951 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6952 -1);
6953 it->end_of_box_run_p
6954 = (FACE_FROM_ID (it->f, next_face_id)->box
6955 == FACE_NO_BOX);
6959 else
6961 int face_id = face_after_it_pos (it);
6962 it->end_of_box_run_p
6963 = (face_id != it->face_id
6964 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6967 /* If we reached the end of the object we've been iterating (e.g., a
6968 display string or an overlay string), and there's something on
6969 IT->stack, proceed with what's on the stack. It doesn't make
6970 sense to return zero if there's unprocessed stuff on the stack,
6971 because otherwise that stuff will never be displayed. */
6972 if (!success_p && it->sp > 0)
6974 set_iterator_to_next (it, 0);
6975 success_p = get_next_display_element (it);
6978 /* Value is 0 if end of buffer or string reached. */
6979 return success_p;
6983 /* Move IT to the next display element.
6985 RESEAT_P non-zero means if called on a newline in buffer text,
6986 skip to the next visible line start.
6988 Functions get_next_display_element and set_iterator_to_next are
6989 separate because I find this arrangement easier to handle than a
6990 get_next_display_element function that also increments IT's
6991 position. The way it is we can first look at an iterator's current
6992 display element, decide whether it fits on a line, and if it does,
6993 increment the iterator position. The other way around we probably
6994 would either need a flag indicating whether the iterator has to be
6995 incremented the next time, or we would have to implement a
6996 decrement position function which would not be easy to write. */
6998 void
6999 set_iterator_to_next (struct it *it, int reseat_p)
7001 /* Reset flags indicating start and end of a sequence of characters
7002 with box. Reset them at the start of this function because
7003 moving the iterator to a new position might set them. */
7004 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7006 switch (it->method)
7008 case GET_FROM_BUFFER:
7009 /* The current display element of IT is a character from
7010 current_buffer. Advance in the buffer, and maybe skip over
7011 invisible lines that are so because of selective display. */
7012 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7013 reseat_at_next_visible_line_start (it, 0);
7014 else if (it->cmp_it.id >= 0)
7016 /* We are currently getting glyphs from a composition. */
7017 int i;
7019 if (! it->bidi_p)
7021 IT_CHARPOS (*it) += it->cmp_it.nchars;
7022 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7023 if (it->cmp_it.to < it->cmp_it.nglyphs)
7025 it->cmp_it.from = it->cmp_it.to;
7027 else
7029 it->cmp_it.id = -1;
7030 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7031 IT_BYTEPOS (*it),
7032 it->end_charpos, Qnil);
7035 else if (! it->cmp_it.reversed_p)
7037 /* Composition created while scanning forward. */
7038 /* Update IT's char/byte positions to point to the first
7039 character of the next grapheme cluster, or to the
7040 character visually after the current composition. */
7041 for (i = 0; i < it->cmp_it.nchars; i++)
7042 bidi_move_to_visually_next (&it->bidi_it);
7043 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7044 IT_CHARPOS (*it) = it->bidi_it.charpos;
7046 if (it->cmp_it.to < it->cmp_it.nglyphs)
7048 /* Proceed to the next grapheme cluster. */
7049 it->cmp_it.from = it->cmp_it.to;
7051 else
7053 /* No more grapheme clusters in this composition.
7054 Find the next stop position. */
7055 ptrdiff_t stop = it->end_charpos;
7056 if (it->bidi_it.scan_dir < 0)
7057 /* Now we are scanning backward and don't know
7058 where to stop. */
7059 stop = -1;
7060 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7061 IT_BYTEPOS (*it), stop, Qnil);
7064 else
7066 /* Composition created while scanning backward. */
7067 /* Update IT's char/byte positions to point to the last
7068 character of the previous grapheme cluster, or the
7069 character visually after the current composition. */
7070 for (i = 0; i < it->cmp_it.nchars; i++)
7071 bidi_move_to_visually_next (&it->bidi_it);
7072 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7073 IT_CHARPOS (*it) = it->bidi_it.charpos;
7074 if (it->cmp_it.from > 0)
7076 /* Proceed to the previous grapheme cluster. */
7077 it->cmp_it.to = it->cmp_it.from;
7079 else
7081 /* No more grapheme clusters in this composition.
7082 Find the next stop position. */
7083 ptrdiff_t stop = it->end_charpos;
7084 if (it->bidi_it.scan_dir < 0)
7085 /* Now we are scanning backward and don't know
7086 where to stop. */
7087 stop = -1;
7088 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7089 IT_BYTEPOS (*it), stop, Qnil);
7093 else
7095 eassert (it->len != 0);
7097 if (!it->bidi_p)
7099 IT_BYTEPOS (*it) += it->len;
7100 IT_CHARPOS (*it) += 1;
7102 else
7104 int prev_scan_dir = it->bidi_it.scan_dir;
7105 /* If this is a new paragraph, determine its base
7106 direction (a.k.a. its base embedding level). */
7107 if (it->bidi_it.new_paragraph)
7108 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7109 bidi_move_to_visually_next (&it->bidi_it);
7110 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7111 IT_CHARPOS (*it) = it->bidi_it.charpos;
7112 if (prev_scan_dir != it->bidi_it.scan_dir)
7114 /* As the scan direction was changed, we must
7115 re-compute the stop position for composition. */
7116 ptrdiff_t stop = it->end_charpos;
7117 if (it->bidi_it.scan_dir < 0)
7118 stop = -1;
7119 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7120 IT_BYTEPOS (*it), stop, Qnil);
7123 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7125 break;
7127 case GET_FROM_C_STRING:
7128 /* Current display element of IT is from a C string. */
7129 if (!it->bidi_p
7130 /* If the string position is beyond string's end, it means
7131 next_element_from_c_string is padding the string with
7132 blanks, in which case we bypass the bidi iterator,
7133 because it cannot deal with such virtual characters. */
7134 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7136 IT_BYTEPOS (*it) += it->len;
7137 IT_CHARPOS (*it) += 1;
7139 else
7141 bidi_move_to_visually_next (&it->bidi_it);
7142 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7143 IT_CHARPOS (*it) = it->bidi_it.charpos;
7145 break;
7147 case GET_FROM_DISPLAY_VECTOR:
7148 /* Current display element of IT is from a display table entry.
7149 Advance in the display table definition. Reset it to null if
7150 end reached, and continue with characters from buffers/
7151 strings. */
7152 ++it->current.dpvec_index;
7154 /* Restore face of the iterator to what they were before the
7155 display vector entry (these entries may contain faces). */
7156 it->face_id = it->saved_face_id;
7158 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7160 int recheck_faces = it->ellipsis_p;
7162 if (it->s)
7163 it->method = GET_FROM_C_STRING;
7164 else if (STRINGP (it->string))
7165 it->method = GET_FROM_STRING;
7166 else
7168 it->method = GET_FROM_BUFFER;
7169 it->object = it->w->buffer;
7172 it->dpvec = NULL;
7173 it->current.dpvec_index = -1;
7175 /* Skip over characters which were displayed via IT->dpvec. */
7176 if (it->dpvec_char_len < 0)
7177 reseat_at_next_visible_line_start (it, 1);
7178 else if (it->dpvec_char_len > 0)
7180 if (it->method == GET_FROM_STRING
7181 && it->n_overlay_strings > 0)
7182 it->ignore_overlay_strings_at_pos_p = 1;
7183 it->len = it->dpvec_char_len;
7184 set_iterator_to_next (it, reseat_p);
7187 /* Maybe recheck faces after display vector */
7188 if (recheck_faces)
7189 it->stop_charpos = IT_CHARPOS (*it);
7191 break;
7193 case GET_FROM_STRING:
7194 /* Current display element is a character from a Lisp string. */
7195 eassert (it->s == NULL && STRINGP (it->string));
7196 /* Don't advance past string end. These conditions are true
7197 when set_iterator_to_next is called at the end of
7198 get_next_display_element, in which case the Lisp string is
7199 already exhausted, and all we want is pop the iterator
7200 stack. */
7201 if (it->current.overlay_string_index >= 0)
7203 /* This is an overlay string, so there's no padding with
7204 spaces, and the number of characters in the string is
7205 where the string ends. */
7206 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7207 goto consider_string_end;
7209 else
7211 /* Not an overlay string. There could be padding, so test
7212 against it->end_charpos . */
7213 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7214 goto consider_string_end;
7216 if (it->cmp_it.id >= 0)
7218 int i;
7220 if (! it->bidi_p)
7222 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7223 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7224 if (it->cmp_it.to < it->cmp_it.nglyphs)
7225 it->cmp_it.from = it->cmp_it.to;
7226 else
7228 it->cmp_it.id = -1;
7229 composition_compute_stop_pos (&it->cmp_it,
7230 IT_STRING_CHARPOS (*it),
7231 IT_STRING_BYTEPOS (*it),
7232 it->end_charpos, it->string);
7235 else if (! it->cmp_it.reversed_p)
7237 for (i = 0; i < it->cmp_it.nchars; i++)
7238 bidi_move_to_visually_next (&it->bidi_it);
7239 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7240 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7242 if (it->cmp_it.to < it->cmp_it.nglyphs)
7243 it->cmp_it.from = it->cmp_it.to;
7244 else
7246 ptrdiff_t stop = it->end_charpos;
7247 if (it->bidi_it.scan_dir < 0)
7248 stop = -1;
7249 composition_compute_stop_pos (&it->cmp_it,
7250 IT_STRING_CHARPOS (*it),
7251 IT_STRING_BYTEPOS (*it), stop,
7252 it->string);
7255 else
7257 for (i = 0; i < it->cmp_it.nchars; i++)
7258 bidi_move_to_visually_next (&it->bidi_it);
7259 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7260 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7261 if (it->cmp_it.from > 0)
7262 it->cmp_it.to = it->cmp_it.from;
7263 else
7265 ptrdiff_t stop = it->end_charpos;
7266 if (it->bidi_it.scan_dir < 0)
7267 stop = -1;
7268 composition_compute_stop_pos (&it->cmp_it,
7269 IT_STRING_CHARPOS (*it),
7270 IT_STRING_BYTEPOS (*it), stop,
7271 it->string);
7275 else
7277 if (!it->bidi_p
7278 /* If the string position is beyond string's end, it
7279 means next_element_from_string is padding the string
7280 with blanks, in which case we bypass the bidi
7281 iterator, because it cannot deal with such virtual
7282 characters. */
7283 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7285 IT_STRING_BYTEPOS (*it) += it->len;
7286 IT_STRING_CHARPOS (*it) += 1;
7288 else
7290 int prev_scan_dir = it->bidi_it.scan_dir;
7292 bidi_move_to_visually_next (&it->bidi_it);
7293 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7294 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7295 if (prev_scan_dir != it->bidi_it.scan_dir)
7297 ptrdiff_t stop = it->end_charpos;
7299 if (it->bidi_it.scan_dir < 0)
7300 stop = -1;
7301 composition_compute_stop_pos (&it->cmp_it,
7302 IT_STRING_CHARPOS (*it),
7303 IT_STRING_BYTEPOS (*it), stop,
7304 it->string);
7309 consider_string_end:
7311 if (it->current.overlay_string_index >= 0)
7313 /* IT->string is an overlay string. Advance to the
7314 next, if there is one. */
7315 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7317 it->ellipsis_p = 0;
7318 next_overlay_string (it);
7319 if (it->ellipsis_p)
7320 setup_for_ellipsis (it, 0);
7323 else
7325 /* IT->string is not an overlay string. If we reached
7326 its end, and there is something on IT->stack, proceed
7327 with what is on the stack. This can be either another
7328 string, this time an overlay string, or a buffer. */
7329 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7330 && it->sp > 0)
7332 pop_it (it);
7333 if (it->method == GET_FROM_STRING)
7334 goto consider_string_end;
7337 break;
7339 case GET_FROM_IMAGE:
7340 case GET_FROM_STRETCH:
7341 /* The position etc with which we have to proceed are on
7342 the stack. The position may be at the end of a string,
7343 if the `display' property takes up the whole string. */
7344 eassert (it->sp > 0);
7345 pop_it (it);
7346 if (it->method == GET_FROM_STRING)
7347 goto consider_string_end;
7348 break;
7350 default:
7351 /* There are no other methods defined, so this should be a bug. */
7352 emacs_abort ();
7355 eassert (it->method != GET_FROM_STRING
7356 || (STRINGP (it->string)
7357 && IT_STRING_CHARPOS (*it) >= 0));
7360 /* Load IT's display element fields with information about the next
7361 display element which comes from a display table entry or from the
7362 result of translating a control character to one of the forms `^C'
7363 or `\003'.
7365 IT->dpvec holds the glyphs to return as characters.
7366 IT->saved_face_id holds the face id before the display vector--it
7367 is restored into IT->face_id in set_iterator_to_next. */
7369 static int
7370 next_element_from_display_vector (struct it *it)
7372 Lisp_Object gc;
7374 /* Precondition. */
7375 eassert (it->dpvec && it->current.dpvec_index >= 0);
7377 it->face_id = it->saved_face_id;
7379 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7380 That seemed totally bogus - so I changed it... */
7381 gc = it->dpvec[it->current.dpvec_index];
7383 if (GLYPH_CODE_P (gc))
7385 it->c = GLYPH_CODE_CHAR (gc);
7386 it->len = CHAR_BYTES (it->c);
7388 /* The entry may contain a face id to use. Such a face id is
7389 the id of a Lisp face, not a realized face. A face id of
7390 zero means no face is specified. */
7391 if (it->dpvec_face_id >= 0)
7392 it->face_id = it->dpvec_face_id;
7393 else
7395 int lface_id = GLYPH_CODE_FACE (gc);
7396 if (lface_id > 0)
7397 it->face_id = merge_faces (it->f, Qt, lface_id,
7398 it->saved_face_id);
7401 else
7402 /* Display table entry is invalid. Return a space. */
7403 it->c = ' ', it->len = 1;
7405 /* Don't change position and object of the iterator here. They are
7406 still the values of the character that had this display table
7407 entry or was translated, and that's what we want. */
7408 it->what = IT_CHARACTER;
7409 return 1;
7412 /* Get the first element of string/buffer in the visual order, after
7413 being reseated to a new position in a string or a buffer. */
7414 static void
7415 get_visually_first_element (struct it *it)
7417 int string_p = STRINGP (it->string) || it->s;
7418 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7419 ptrdiff_t bob = (string_p ? 0 : BEGV);
7421 if (STRINGP (it->string))
7423 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7424 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7426 else
7428 it->bidi_it.charpos = IT_CHARPOS (*it);
7429 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7432 if (it->bidi_it.charpos == eob)
7434 /* Nothing to do, but reset the FIRST_ELT flag, like
7435 bidi_paragraph_init does, because we are not going to
7436 call it. */
7437 it->bidi_it.first_elt = 0;
7439 else if (it->bidi_it.charpos == bob
7440 || (!string_p
7441 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7442 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7444 /* If we are at the beginning of a line/string, we can produce
7445 the next element right away. */
7446 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7447 bidi_move_to_visually_next (&it->bidi_it);
7449 else
7451 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7453 /* We need to prime the bidi iterator starting at the line's or
7454 string's beginning, before we will be able to produce the
7455 next element. */
7456 if (string_p)
7457 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7458 else
7460 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7461 -1);
7462 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7464 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7467 /* Now return to buffer/string position where we were asked
7468 to get the next display element, and produce that. */
7469 bidi_move_to_visually_next (&it->bidi_it);
7471 while (it->bidi_it.bytepos != orig_bytepos
7472 && it->bidi_it.charpos < eob);
7475 /* Adjust IT's position information to where we ended up. */
7476 if (STRINGP (it->string))
7478 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7479 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7481 else
7483 IT_CHARPOS (*it) = it->bidi_it.charpos;
7484 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7487 if (STRINGP (it->string) || !it->s)
7489 ptrdiff_t stop, charpos, bytepos;
7491 if (STRINGP (it->string))
7493 eassert (!it->s);
7494 stop = SCHARS (it->string);
7495 if (stop > it->end_charpos)
7496 stop = it->end_charpos;
7497 charpos = IT_STRING_CHARPOS (*it);
7498 bytepos = IT_STRING_BYTEPOS (*it);
7500 else
7502 stop = it->end_charpos;
7503 charpos = IT_CHARPOS (*it);
7504 bytepos = IT_BYTEPOS (*it);
7506 if (it->bidi_it.scan_dir < 0)
7507 stop = -1;
7508 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7509 it->string);
7513 /* Load IT with the next display element from Lisp string IT->string.
7514 IT->current.string_pos is the current position within the string.
7515 If IT->current.overlay_string_index >= 0, the Lisp string is an
7516 overlay string. */
7518 static int
7519 next_element_from_string (struct it *it)
7521 struct text_pos position;
7523 eassert (STRINGP (it->string));
7524 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7525 eassert (IT_STRING_CHARPOS (*it) >= 0);
7526 position = it->current.string_pos;
7528 /* With bidi reordering, the character to display might not be the
7529 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7530 that we were reseat()ed to a new string, whose paragraph
7531 direction is not known. */
7532 if (it->bidi_p && it->bidi_it.first_elt)
7534 get_visually_first_element (it);
7535 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7538 /* Time to check for invisible text? */
7539 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7541 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7543 if (!(!it->bidi_p
7544 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7545 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7547 /* With bidi non-linear iteration, we could find
7548 ourselves far beyond the last computed stop_charpos,
7549 with several other stop positions in between that we
7550 missed. Scan them all now, in buffer's logical
7551 order, until we find and handle the last stop_charpos
7552 that precedes our current position. */
7553 handle_stop_backwards (it, it->stop_charpos);
7554 return GET_NEXT_DISPLAY_ELEMENT (it);
7556 else
7558 if (it->bidi_p)
7560 /* Take note of the stop position we just moved
7561 across, for when we will move back across it. */
7562 it->prev_stop = it->stop_charpos;
7563 /* If we are at base paragraph embedding level, take
7564 note of the last stop position seen at this
7565 level. */
7566 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7567 it->base_level_stop = it->stop_charpos;
7569 handle_stop (it);
7571 /* Since a handler may have changed IT->method, we must
7572 recurse here. */
7573 return GET_NEXT_DISPLAY_ELEMENT (it);
7576 else if (it->bidi_p
7577 /* If we are before prev_stop, we may have overstepped
7578 on our way backwards a stop_pos, and if so, we need
7579 to handle that stop_pos. */
7580 && IT_STRING_CHARPOS (*it) < it->prev_stop
7581 /* We can sometimes back up for reasons that have nothing
7582 to do with bidi reordering. E.g., compositions. The
7583 code below is only needed when we are above the base
7584 embedding level, so test for that explicitly. */
7585 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7587 /* If we lost track of base_level_stop, we have no better
7588 place for handle_stop_backwards to start from than string
7589 beginning. This happens, e.g., when we were reseated to
7590 the previous screenful of text by vertical-motion. */
7591 if (it->base_level_stop <= 0
7592 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7593 it->base_level_stop = 0;
7594 handle_stop_backwards (it, it->base_level_stop);
7595 return GET_NEXT_DISPLAY_ELEMENT (it);
7599 if (it->current.overlay_string_index >= 0)
7601 /* Get the next character from an overlay string. In overlay
7602 strings, there is no field width or padding with spaces to
7603 do. */
7604 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7606 it->what = IT_EOB;
7607 return 0;
7609 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7610 IT_STRING_BYTEPOS (*it),
7611 it->bidi_it.scan_dir < 0
7612 ? -1
7613 : SCHARS (it->string))
7614 && next_element_from_composition (it))
7616 return 1;
7618 else if (STRING_MULTIBYTE (it->string))
7620 const unsigned char *s = (SDATA (it->string)
7621 + IT_STRING_BYTEPOS (*it));
7622 it->c = string_char_and_length (s, &it->len);
7624 else
7626 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7627 it->len = 1;
7630 else
7632 /* Get the next character from a Lisp string that is not an
7633 overlay string. Such strings come from the mode line, for
7634 example. We may have to pad with spaces, or truncate the
7635 string. See also next_element_from_c_string. */
7636 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7638 it->what = IT_EOB;
7639 return 0;
7641 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7643 /* Pad with spaces. */
7644 it->c = ' ', it->len = 1;
7645 CHARPOS (position) = BYTEPOS (position) = -1;
7647 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7648 IT_STRING_BYTEPOS (*it),
7649 it->bidi_it.scan_dir < 0
7650 ? -1
7651 : it->string_nchars)
7652 && next_element_from_composition (it))
7654 return 1;
7656 else if (STRING_MULTIBYTE (it->string))
7658 const unsigned char *s = (SDATA (it->string)
7659 + IT_STRING_BYTEPOS (*it));
7660 it->c = string_char_and_length (s, &it->len);
7662 else
7664 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7665 it->len = 1;
7669 /* Record what we have and where it came from. */
7670 it->what = IT_CHARACTER;
7671 it->object = it->string;
7672 it->position = position;
7673 return 1;
7677 /* Load IT with next display element from C string IT->s.
7678 IT->string_nchars is the maximum number of characters to return
7679 from the string. IT->end_charpos may be greater than
7680 IT->string_nchars when this function is called, in which case we
7681 may have to return padding spaces. Value is zero if end of string
7682 reached, including padding spaces. */
7684 static int
7685 next_element_from_c_string (struct it *it)
7687 int success_p = 1;
7689 eassert (it->s);
7690 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7691 it->what = IT_CHARACTER;
7692 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7693 it->object = Qnil;
7695 /* With bidi reordering, the character to display might not be the
7696 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7697 we were reseated to a new string, whose paragraph direction is
7698 not known. */
7699 if (it->bidi_p && it->bidi_it.first_elt)
7700 get_visually_first_element (it);
7702 /* IT's position can be greater than IT->string_nchars in case a
7703 field width or precision has been specified when the iterator was
7704 initialized. */
7705 if (IT_CHARPOS (*it) >= it->end_charpos)
7707 /* End of the game. */
7708 it->what = IT_EOB;
7709 success_p = 0;
7711 else if (IT_CHARPOS (*it) >= it->string_nchars)
7713 /* Pad with spaces. */
7714 it->c = ' ', it->len = 1;
7715 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7717 else if (it->multibyte_p)
7718 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7719 else
7720 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7722 return success_p;
7726 /* Set up IT to return characters from an ellipsis, if appropriate.
7727 The definition of the ellipsis glyphs may come from a display table
7728 entry. This function fills IT with the first glyph from the
7729 ellipsis if an ellipsis is to be displayed. */
7731 static int
7732 next_element_from_ellipsis (struct it *it)
7734 if (it->selective_display_ellipsis_p)
7735 setup_for_ellipsis (it, it->len);
7736 else
7738 /* The face at the current position may be different from the
7739 face we find after the invisible text. Remember what it
7740 was in IT->saved_face_id, and signal that it's there by
7741 setting face_before_selective_p. */
7742 it->saved_face_id = it->face_id;
7743 it->method = GET_FROM_BUFFER;
7744 it->object = it->w->buffer;
7745 reseat_at_next_visible_line_start (it, 1);
7746 it->face_before_selective_p = 1;
7749 return GET_NEXT_DISPLAY_ELEMENT (it);
7753 /* Deliver an image display element. The iterator IT is already
7754 filled with image information (done in handle_display_prop). Value
7755 is always 1. */
7758 static int
7759 next_element_from_image (struct it *it)
7761 it->what = IT_IMAGE;
7762 it->ignore_overlay_strings_at_pos_p = 0;
7763 return 1;
7767 /* Fill iterator IT with next display element from a stretch glyph
7768 property. IT->object is the value of the text property. Value is
7769 always 1. */
7771 static int
7772 next_element_from_stretch (struct it *it)
7774 it->what = IT_STRETCH;
7775 return 1;
7778 /* Scan backwards from IT's current position until we find a stop
7779 position, or until BEGV. This is called when we find ourself
7780 before both the last known prev_stop and base_level_stop while
7781 reordering bidirectional text. */
7783 static void
7784 compute_stop_pos_backwards (struct it *it)
7786 const int SCAN_BACK_LIMIT = 1000;
7787 struct text_pos pos;
7788 struct display_pos save_current = it->current;
7789 struct text_pos save_position = it->position;
7790 ptrdiff_t charpos = IT_CHARPOS (*it);
7791 ptrdiff_t where_we_are = charpos;
7792 ptrdiff_t save_stop_pos = it->stop_charpos;
7793 ptrdiff_t save_end_pos = it->end_charpos;
7795 eassert (NILP (it->string) && !it->s);
7796 eassert (it->bidi_p);
7797 it->bidi_p = 0;
7800 it->end_charpos = min (charpos + 1, ZV);
7801 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7802 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7803 reseat_1 (it, pos, 0);
7804 compute_stop_pos (it);
7805 /* We must advance forward, right? */
7806 if (it->stop_charpos <= charpos)
7807 emacs_abort ();
7809 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7811 if (it->stop_charpos <= where_we_are)
7812 it->prev_stop = it->stop_charpos;
7813 else
7814 it->prev_stop = BEGV;
7815 it->bidi_p = 1;
7816 it->current = save_current;
7817 it->position = save_position;
7818 it->stop_charpos = save_stop_pos;
7819 it->end_charpos = save_end_pos;
7822 /* Scan forward from CHARPOS in the current buffer/string, until we
7823 find a stop position > current IT's position. Then handle the stop
7824 position before that. This is called when we bump into a stop
7825 position while reordering bidirectional text. CHARPOS should be
7826 the last previously processed stop_pos (or BEGV/0, if none were
7827 processed yet) whose position is less that IT's current
7828 position. */
7830 static void
7831 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7833 int bufp = !STRINGP (it->string);
7834 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7835 struct display_pos save_current = it->current;
7836 struct text_pos save_position = it->position;
7837 struct text_pos pos1;
7838 ptrdiff_t next_stop;
7840 /* Scan in strict logical order. */
7841 eassert (it->bidi_p);
7842 it->bidi_p = 0;
7845 it->prev_stop = charpos;
7846 if (bufp)
7848 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7849 reseat_1 (it, pos1, 0);
7851 else
7852 it->current.string_pos = string_pos (charpos, it->string);
7853 compute_stop_pos (it);
7854 /* We must advance forward, right? */
7855 if (it->stop_charpos <= it->prev_stop)
7856 emacs_abort ();
7857 charpos = it->stop_charpos;
7859 while (charpos <= where_we_are);
7861 it->bidi_p = 1;
7862 it->current = save_current;
7863 it->position = save_position;
7864 next_stop = it->stop_charpos;
7865 it->stop_charpos = it->prev_stop;
7866 handle_stop (it);
7867 it->stop_charpos = next_stop;
7870 /* Load IT with the next display element from current_buffer. Value
7871 is zero if end of buffer reached. IT->stop_charpos is the next
7872 position at which to stop and check for text properties or buffer
7873 end. */
7875 static int
7876 next_element_from_buffer (struct it *it)
7878 int success_p = 1;
7880 eassert (IT_CHARPOS (*it) >= BEGV);
7881 eassert (NILP (it->string) && !it->s);
7882 eassert (!it->bidi_p
7883 || (EQ (it->bidi_it.string.lstring, Qnil)
7884 && it->bidi_it.string.s == NULL));
7886 /* With bidi reordering, the character to display might not be the
7887 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7888 we were reseat()ed to a new buffer position, which is potentially
7889 a different paragraph. */
7890 if (it->bidi_p && it->bidi_it.first_elt)
7892 get_visually_first_element (it);
7893 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7896 if (IT_CHARPOS (*it) >= it->stop_charpos)
7898 if (IT_CHARPOS (*it) >= it->end_charpos)
7900 int overlay_strings_follow_p;
7902 /* End of the game, except when overlay strings follow that
7903 haven't been returned yet. */
7904 if (it->overlay_strings_at_end_processed_p)
7905 overlay_strings_follow_p = 0;
7906 else
7908 it->overlay_strings_at_end_processed_p = 1;
7909 overlay_strings_follow_p = get_overlay_strings (it, 0);
7912 if (overlay_strings_follow_p)
7913 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7914 else
7916 it->what = IT_EOB;
7917 it->position = it->current.pos;
7918 success_p = 0;
7921 else if (!(!it->bidi_p
7922 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7923 || IT_CHARPOS (*it) == it->stop_charpos))
7925 /* With bidi non-linear iteration, we could find ourselves
7926 far beyond the last computed stop_charpos, with several
7927 other stop positions in between that we missed. Scan
7928 them all now, in buffer's logical order, until we find
7929 and handle the last stop_charpos that precedes our
7930 current position. */
7931 handle_stop_backwards (it, it->stop_charpos);
7932 return GET_NEXT_DISPLAY_ELEMENT (it);
7934 else
7936 if (it->bidi_p)
7938 /* Take note of the stop position we just moved across,
7939 for when we will move back across it. */
7940 it->prev_stop = it->stop_charpos;
7941 /* If we are at base paragraph embedding level, take
7942 note of the last stop position seen at this
7943 level. */
7944 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7945 it->base_level_stop = it->stop_charpos;
7947 handle_stop (it);
7948 return GET_NEXT_DISPLAY_ELEMENT (it);
7951 else if (it->bidi_p
7952 /* If we are before prev_stop, we may have overstepped on
7953 our way backwards a stop_pos, and if so, we need to
7954 handle that stop_pos. */
7955 && IT_CHARPOS (*it) < it->prev_stop
7956 /* We can sometimes back up for reasons that have nothing
7957 to do with bidi reordering. E.g., compositions. The
7958 code below is only needed when we are above the base
7959 embedding level, so test for that explicitly. */
7960 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7962 if (it->base_level_stop <= 0
7963 || IT_CHARPOS (*it) < it->base_level_stop)
7965 /* If we lost track of base_level_stop, we need to find
7966 prev_stop by looking backwards. This happens, e.g., when
7967 we were reseated to the previous screenful of text by
7968 vertical-motion. */
7969 it->base_level_stop = BEGV;
7970 compute_stop_pos_backwards (it);
7971 handle_stop_backwards (it, it->prev_stop);
7973 else
7974 handle_stop_backwards (it, it->base_level_stop);
7975 return GET_NEXT_DISPLAY_ELEMENT (it);
7977 else
7979 /* No face changes, overlays etc. in sight, so just return a
7980 character from current_buffer. */
7981 unsigned char *p;
7982 ptrdiff_t stop;
7984 /* Maybe run the redisplay end trigger hook. Performance note:
7985 This doesn't seem to cost measurable time. */
7986 if (it->redisplay_end_trigger_charpos
7987 && it->glyph_row
7988 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7989 run_redisplay_end_trigger_hook (it);
7991 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7992 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7993 stop)
7994 && next_element_from_composition (it))
7996 return 1;
7999 /* Get the next character, maybe multibyte. */
8000 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8001 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8002 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8003 else
8004 it->c = *p, it->len = 1;
8006 /* Record what we have and where it came from. */
8007 it->what = IT_CHARACTER;
8008 it->object = it->w->buffer;
8009 it->position = it->current.pos;
8011 /* Normally we return the character found above, except when we
8012 really want to return an ellipsis for selective display. */
8013 if (it->selective)
8015 if (it->c == '\n')
8017 /* A value of selective > 0 means hide lines indented more
8018 than that number of columns. */
8019 if (it->selective > 0
8020 && IT_CHARPOS (*it) + 1 < ZV
8021 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8022 IT_BYTEPOS (*it) + 1,
8023 it->selective))
8025 success_p = next_element_from_ellipsis (it);
8026 it->dpvec_char_len = -1;
8029 else if (it->c == '\r' && it->selective == -1)
8031 /* A value of selective == -1 means that everything from the
8032 CR to the end of the line is invisible, with maybe an
8033 ellipsis displayed for it. */
8034 success_p = next_element_from_ellipsis (it);
8035 it->dpvec_char_len = -1;
8040 /* Value is zero if end of buffer reached. */
8041 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8042 return success_p;
8046 /* Run the redisplay end trigger hook for IT. */
8048 static void
8049 run_redisplay_end_trigger_hook (struct it *it)
8051 Lisp_Object args[3];
8053 /* IT->glyph_row should be non-null, i.e. we should be actually
8054 displaying something, or otherwise we should not run the hook. */
8055 eassert (it->glyph_row);
8057 /* Set up hook arguments. */
8058 args[0] = Qredisplay_end_trigger_functions;
8059 args[1] = it->window;
8060 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8061 it->redisplay_end_trigger_charpos = 0;
8063 /* Since we are *trying* to run these functions, don't try to run
8064 them again, even if they get an error. */
8065 wset_redisplay_end_trigger (it->w, Qnil);
8066 Frun_hook_with_args (3, args);
8068 /* Notice if it changed the face of the character we are on. */
8069 handle_face_prop (it);
8073 /* Deliver a composition display element. Unlike the other
8074 next_element_from_XXX, this function is not registered in the array
8075 get_next_element[]. It is called from next_element_from_buffer and
8076 next_element_from_string when necessary. */
8078 static int
8079 next_element_from_composition (struct it *it)
8081 it->what = IT_COMPOSITION;
8082 it->len = it->cmp_it.nbytes;
8083 if (STRINGP (it->string))
8085 if (it->c < 0)
8087 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8088 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8089 return 0;
8091 it->position = it->current.string_pos;
8092 it->object = it->string;
8093 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8094 IT_STRING_BYTEPOS (*it), it->string);
8096 else
8098 if (it->c < 0)
8100 IT_CHARPOS (*it) += it->cmp_it.nchars;
8101 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8102 if (it->bidi_p)
8104 if (it->bidi_it.new_paragraph)
8105 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8106 /* Resync the bidi iterator with IT's new position.
8107 FIXME: this doesn't support bidirectional text. */
8108 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8109 bidi_move_to_visually_next (&it->bidi_it);
8111 return 0;
8113 it->position = it->current.pos;
8114 it->object = it->w->buffer;
8115 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8116 IT_BYTEPOS (*it), Qnil);
8118 return 1;
8123 /***********************************************************************
8124 Moving an iterator without producing glyphs
8125 ***********************************************************************/
8127 /* Check if iterator is at a position corresponding to a valid buffer
8128 position after some move_it_ call. */
8130 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8131 ((it)->method == GET_FROM_STRING \
8132 ? IT_STRING_CHARPOS (*it) == 0 \
8133 : 1)
8136 /* Move iterator IT to a specified buffer or X position within one
8137 line on the display without producing glyphs.
8139 OP should be a bit mask including some or all of these bits:
8140 MOVE_TO_X: Stop upon reaching x-position TO_X.
8141 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8142 Regardless of OP's value, stop upon reaching the end of the display line.
8144 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8145 This means, in particular, that TO_X includes window's horizontal
8146 scroll amount.
8148 The return value has several possible values that
8149 say what condition caused the scan to stop:
8151 MOVE_POS_MATCH_OR_ZV
8152 - when TO_POS or ZV was reached.
8154 MOVE_X_REACHED
8155 -when TO_X was reached before TO_POS or ZV were reached.
8157 MOVE_LINE_CONTINUED
8158 - when we reached the end of the display area and the line must
8159 be continued.
8161 MOVE_LINE_TRUNCATED
8162 - when we reached the end of the display area and the line is
8163 truncated.
8165 MOVE_NEWLINE_OR_CR
8166 - when we stopped at a line end, i.e. a newline or a CR and selective
8167 display is on. */
8169 static enum move_it_result
8170 move_it_in_display_line_to (struct it *it,
8171 ptrdiff_t to_charpos, int to_x,
8172 enum move_operation_enum op)
8174 enum move_it_result result = MOVE_UNDEFINED;
8175 struct glyph_row *saved_glyph_row;
8176 struct it wrap_it, atpos_it, atx_it, ppos_it;
8177 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8178 void *ppos_data = NULL;
8179 int may_wrap = 0;
8180 enum it_method prev_method = it->method;
8181 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8182 int saw_smaller_pos = prev_pos < to_charpos;
8184 /* Don't produce glyphs in produce_glyphs. */
8185 saved_glyph_row = it->glyph_row;
8186 it->glyph_row = NULL;
8188 /* Use wrap_it to save a copy of IT wherever a word wrap could
8189 occur. Use atpos_it to save a copy of IT at the desired buffer
8190 position, if found, so that we can scan ahead and check if the
8191 word later overshoots the window edge. Use atx_it similarly, for
8192 pixel positions. */
8193 wrap_it.sp = -1;
8194 atpos_it.sp = -1;
8195 atx_it.sp = -1;
8197 /* Use ppos_it under bidi reordering to save a copy of IT for the
8198 position > CHARPOS that is the closest to CHARPOS. We restore
8199 that position in IT when we have scanned the entire display line
8200 without finding a match for CHARPOS and all the character
8201 positions are greater than CHARPOS. */
8202 if (it->bidi_p)
8204 SAVE_IT (ppos_it, *it, ppos_data);
8205 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8206 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8207 SAVE_IT (ppos_it, *it, ppos_data);
8210 #define BUFFER_POS_REACHED_P() \
8211 ((op & MOVE_TO_POS) != 0 \
8212 && BUFFERP (it->object) \
8213 && (IT_CHARPOS (*it) == to_charpos \
8214 || ((!it->bidi_p \
8215 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8216 && IT_CHARPOS (*it) > to_charpos) \
8217 || (it->what == IT_COMPOSITION \
8218 && ((IT_CHARPOS (*it) > to_charpos \
8219 && to_charpos >= it->cmp_it.charpos) \
8220 || (IT_CHARPOS (*it) < to_charpos \
8221 && to_charpos <= it->cmp_it.charpos)))) \
8222 && (it->method == GET_FROM_BUFFER \
8223 || (it->method == GET_FROM_DISPLAY_VECTOR \
8224 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8226 /* If there's a line-/wrap-prefix, handle it. */
8227 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8228 && it->current_y < it->last_visible_y)
8229 handle_line_prefix (it);
8231 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8232 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8234 while (1)
8236 int x, i, ascent = 0, descent = 0;
8238 /* Utility macro to reset an iterator with x, ascent, and descent. */
8239 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8240 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8241 (IT)->max_descent = descent)
8243 /* Stop if we move beyond TO_CHARPOS (after an image or a
8244 display string or stretch glyph). */
8245 if ((op & MOVE_TO_POS) != 0
8246 && BUFFERP (it->object)
8247 && it->method == GET_FROM_BUFFER
8248 && (((!it->bidi_p
8249 /* When the iterator is at base embedding level, we
8250 are guaranteed that characters are delivered for
8251 display in strictly increasing order of their
8252 buffer positions. */
8253 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8254 && IT_CHARPOS (*it) > to_charpos)
8255 || (it->bidi_p
8256 && (prev_method == GET_FROM_IMAGE
8257 || prev_method == GET_FROM_STRETCH
8258 || prev_method == GET_FROM_STRING)
8259 /* Passed TO_CHARPOS from left to right. */
8260 && ((prev_pos < to_charpos
8261 && IT_CHARPOS (*it) > to_charpos)
8262 /* Passed TO_CHARPOS from right to left. */
8263 || (prev_pos > to_charpos
8264 && IT_CHARPOS (*it) < to_charpos)))))
8266 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8268 result = MOVE_POS_MATCH_OR_ZV;
8269 break;
8271 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8272 /* If wrap_it is valid, the current position might be in a
8273 word that is wrapped. So, save the iterator in
8274 atpos_it and continue to see if wrapping happens. */
8275 SAVE_IT (atpos_it, *it, atpos_data);
8278 /* Stop when ZV reached.
8279 We used to stop here when TO_CHARPOS reached as well, but that is
8280 too soon if this glyph does not fit on this line. So we handle it
8281 explicitly below. */
8282 if (!get_next_display_element (it))
8284 result = MOVE_POS_MATCH_OR_ZV;
8285 break;
8288 if (it->line_wrap == TRUNCATE)
8290 if (BUFFER_POS_REACHED_P ())
8292 result = MOVE_POS_MATCH_OR_ZV;
8293 break;
8296 else
8298 if (it->line_wrap == WORD_WRAP)
8300 if (IT_DISPLAYING_WHITESPACE (it))
8301 may_wrap = 1;
8302 else if (may_wrap)
8304 /* We have reached a glyph that follows one or more
8305 whitespace characters. If the position is
8306 already found, we are done. */
8307 if (atpos_it.sp >= 0)
8309 RESTORE_IT (it, &atpos_it, atpos_data);
8310 result = MOVE_POS_MATCH_OR_ZV;
8311 goto done;
8313 if (atx_it.sp >= 0)
8315 RESTORE_IT (it, &atx_it, atx_data);
8316 result = MOVE_X_REACHED;
8317 goto done;
8319 /* Otherwise, we can wrap here. */
8320 SAVE_IT (wrap_it, *it, wrap_data);
8321 may_wrap = 0;
8326 /* Remember the line height for the current line, in case
8327 the next element doesn't fit on the line. */
8328 ascent = it->max_ascent;
8329 descent = it->max_descent;
8331 /* The call to produce_glyphs will get the metrics of the
8332 display element IT is loaded with. Record the x-position
8333 before this display element, in case it doesn't fit on the
8334 line. */
8335 x = it->current_x;
8337 PRODUCE_GLYPHS (it);
8339 if (it->area != TEXT_AREA)
8341 prev_method = it->method;
8342 if (it->method == GET_FROM_BUFFER)
8343 prev_pos = IT_CHARPOS (*it);
8344 set_iterator_to_next (it, 1);
8345 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8346 SET_TEXT_POS (this_line_min_pos,
8347 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8348 if (it->bidi_p
8349 && (op & MOVE_TO_POS)
8350 && IT_CHARPOS (*it) > to_charpos
8351 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8352 SAVE_IT (ppos_it, *it, ppos_data);
8353 continue;
8356 /* The number of glyphs we get back in IT->nglyphs will normally
8357 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8358 character on a terminal frame, or (iii) a line end. For the
8359 second case, IT->nglyphs - 1 padding glyphs will be present.
8360 (On X frames, there is only one glyph produced for a
8361 composite character.)
8363 The behavior implemented below means, for continuation lines,
8364 that as many spaces of a TAB as fit on the current line are
8365 displayed there. For terminal frames, as many glyphs of a
8366 multi-glyph character are displayed in the current line, too.
8367 This is what the old redisplay code did, and we keep it that
8368 way. Under X, the whole shape of a complex character must
8369 fit on the line or it will be completely displayed in the
8370 next line.
8372 Note that both for tabs and padding glyphs, all glyphs have
8373 the same width. */
8374 if (it->nglyphs)
8376 /* More than one glyph or glyph doesn't fit on line. All
8377 glyphs have the same width. */
8378 int single_glyph_width = it->pixel_width / it->nglyphs;
8379 int new_x;
8380 int x_before_this_char = x;
8381 int hpos_before_this_char = it->hpos;
8383 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8385 new_x = x + single_glyph_width;
8387 /* We want to leave anything reaching TO_X to the caller. */
8388 if ((op & MOVE_TO_X) && new_x > to_x)
8390 if (BUFFER_POS_REACHED_P ())
8392 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8393 goto buffer_pos_reached;
8394 if (atpos_it.sp < 0)
8396 SAVE_IT (atpos_it, *it, atpos_data);
8397 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8400 else
8402 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8404 it->current_x = x;
8405 result = MOVE_X_REACHED;
8406 break;
8408 if (atx_it.sp < 0)
8410 SAVE_IT (atx_it, *it, atx_data);
8411 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8416 if (/* Lines are continued. */
8417 it->line_wrap != TRUNCATE
8418 && (/* And glyph doesn't fit on the line. */
8419 new_x > it->last_visible_x
8420 /* Or it fits exactly and we're on a window
8421 system frame. */
8422 || (new_x == it->last_visible_x
8423 && FRAME_WINDOW_P (it->f)
8424 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8425 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8426 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8428 if (/* IT->hpos == 0 means the very first glyph
8429 doesn't fit on the line, e.g. a wide image. */
8430 it->hpos == 0
8431 || (new_x == it->last_visible_x
8432 && FRAME_WINDOW_P (it->f)))
8434 ++it->hpos;
8435 it->current_x = new_x;
8437 /* The character's last glyph just barely fits
8438 in this row. */
8439 if (i == it->nglyphs - 1)
8441 /* If this is the destination position,
8442 return a position *before* it in this row,
8443 now that we know it fits in this row. */
8444 if (BUFFER_POS_REACHED_P ())
8446 if (it->line_wrap != WORD_WRAP
8447 || wrap_it.sp < 0)
8449 it->hpos = hpos_before_this_char;
8450 it->current_x = x_before_this_char;
8451 result = MOVE_POS_MATCH_OR_ZV;
8452 break;
8454 if (it->line_wrap == WORD_WRAP
8455 && atpos_it.sp < 0)
8457 SAVE_IT (atpos_it, *it, atpos_data);
8458 atpos_it.current_x = x_before_this_char;
8459 atpos_it.hpos = hpos_before_this_char;
8463 prev_method = it->method;
8464 if (it->method == GET_FROM_BUFFER)
8465 prev_pos = IT_CHARPOS (*it);
8466 set_iterator_to_next (it, 1);
8467 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8468 SET_TEXT_POS (this_line_min_pos,
8469 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8470 /* On graphical terminals, newlines may
8471 "overflow" into the fringe if
8472 overflow-newline-into-fringe is non-nil.
8473 On text terminals, and on graphical
8474 terminals with no right margin, newlines
8475 may overflow into the last glyph on the
8476 display line.*/
8477 if (!FRAME_WINDOW_P (it->f)
8478 || ((it->bidi_p
8479 && it->bidi_it.paragraph_dir == R2L)
8480 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8481 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8482 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8484 if (!get_next_display_element (it))
8486 result = MOVE_POS_MATCH_OR_ZV;
8487 break;
8489 if (BUFFER_POS_REACHED_P ())
8491 if (ITERATOR_AT_END_OF_LINE_P (it))
8492 result = MOVE_POS_MATCH_OR_ZV;
8493 else
8494 result = MOVE_LINE_CONTINUED;
8495 break;
8497 if (ITERATOR_AT_END_OF_LINE_P (it))
8499 result = MOVE_NEWLINE_OR_CR;
8500 break;
8505 else
8506 IT_RESET_X_ASCENT_DESCENT (it);
8508 if (wrap_it.sp >= 0)
8510 RESTORE_IT (it, &wrap_it, wrap_data);
8511 atpos_it.sp = -1;
8512 atx_it.sp = -1;
8515 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8516 IT_CHARPOS (*it)));
8517 result = MOVE_LINE_CONTINUED;
8518 break;
8521 if (BUFFER_POS_REACHED_P ())
8523 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8524 goto buffer_pos_reached;
8525 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8527 SAVE_IT (atpos_it, *it, atpos_data);
8528 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8532 if (new_x > it->first_visible_x)
8534 /* Glyph is visible. Increment number of glyphs that
8535 would be displayed. */
8536 ++it->hpos;
8540 if (result != MOVE_UNDEFINED)
8541 break;
8543 else if (BUFFER_POS_REACHED_P ())
8545 buffer_pos_reached:
8546 IT_RESET_X_ASCENT_DESCENT (it);
8547 result = MOVE_POS_MATCH_OR_ZV;
8548 break;
8550 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8552 /* Stop when TO_X specified and reached. This check is
8553 necessary here because of lines consisting of a line end,
8554 only. The line end will not produce any glyphs and we
8555 would never get MOVE_X_REACHED. */
8556 eassert (it->nglyphs == 0);
8557 result = MOVE_X_REACHED;
8558 break;
8561 /* Is this a line end? If yes, we're done. */
8562 if (ITERATOR_AT_END_OF_LINE_P (it))
8564 /* If we are past TO_CHARPOS, but never saw any character
8565 positions smaller than TO_CHARPOS, return
8566 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8567 did. */
8568 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8570 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8572 if (IT_CHARPOS (ppos_it) < ZV)
8574 RESTORE_IT (it, &ppos_it, ppos_data);
8575 result = MOVE_POS_MATCH_OR_ZV;
8577 else
8578 goto buffer_pos_reached;
8580 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8581 && IT_CHARPOS (*it) > to_charpos)
8582 goto buffer_pos_reached;
8583 else
8584 result = MOVE_NEWLINE_OR_CR;
8586 else
8587 result = MOVE_NEWLINE_OR_CR;
8588 break;
8591 prev_method = it->method;
8592 if (it->method == GET_FROM_BUFFER)
8593 prev_pos = IT_CHARPOS (*it);
8594 /* The current display element has been consumed. Advance
8595 to the next. */
8596 set_iterator_to_next (it, 1);
8597 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8598 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8599 if (IT_CHARPOS (*it) < to_charpos)
8600 saw_smaller_pos = 1;
8601 if (it->bidi_p
8602 && (op & MOVE_TO_POS)
8603 && IT_CHARPOS (*it) >= to_charpos
8604 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8605 SAVE_IT (ppos_it, *it, ppos_data);
8607 /* Stop if lines are truncated and IT's current x-position is
8608 past the right edge of the window now. */
8609 if (it->line_wrap == TRUNCATE
8610 && it->current_x >= it->last_visible_x)
8612 if (!FRAME_WINDOW_P (it->f)
8613 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8614 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8615 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8616 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8618 int at_eob_p = 0;
8620 if ((at_eob_p = !get_next_display_element (it))
8621 || BUFFER_POS_REACHED_P ()
8622 /* If we are past TO_CHARPOS, but never saw any
8623 character positions smaller than TO_CHARPOS,
8624 return MOVE_POS_MATCH_OR_ZV, like the
8625 unidirectional display did. */
8626 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8627 && !saw_smaller_pos
8628 && IT_CHARPOS (*it) > to_charpos))
8630 if (it->bidi_p
8631 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8632 RESTORE_IT (it, &ppos_it, ppos_data);
8633 result = MOVE_POS_MATCH_OR_ZV;
8634 break;
8636 if (ITERATOR_AT_END_OF_LINE_P (it))
8638 result = MOVE_NEWLINE_OR_CR;
8639 break;
8642 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8643 && !saw_smaller_pos
8644 && IT_CHARPOS (*it) > to_charpos)
8646 if (IT_CHARPOS (ppos_it) < ZV)
8647 RESTORE_IT (it, &ppos_it, ppos_data);
8648 result = MOVE_POS_MATCH_OR_ZV;
8649 break;
8651 result = MOVE_LINE_TRUNCATED;
8652 break;
8654 #undef IT_RESET_X_ASCENT_DESCENT
8657 #undef BUFFER_POS_REACHED_P
8659 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8660 restore the saved iterator. */
8661 if (atpos_it.sp >= 0)
8662 RESTORE_IT (it, &atpos_it, atpos_data);
8663 else if (atx_it.sp >= 0)
8664 RESTORE_IT (it, &atx_it, atx_data);
8666 done:
8668 if (atpos_data)
8669 bidi_unshelve_cache (atpos_data, 1);
8670 if (atx_data)
8671 bidi_unshelve_cache (atx_data, 1);
8672 if (wrap_data)
8673 bidi_unshelve_cache (wrap_data, 1);
8674 if (ppos_data)
8675 bidi_unshelve_cache (ppos_data, 1);
8677 /* Restore the iterator settings altered at the beginning of this
8678 function. */
8679 it->glyph_row = saved_glyph_row;
8680 return result;
8683 /* For external use. */
8684 void
8685 move_it_in_display_line (struct it *it,
8686 ptrdiff_t to_charpos, int to_x,
8687 enum move_operation_enum op)
8689 if (it->line_wrap == WORD_WRAP
8690 && (op & MOVE_TO_X))
8692 struct it save_it;
8693 void *save_data = NULL;
8694 int skip;
8696 SAVE_IT (save_it, *it, save_data);
8697 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8698 /* When word-wrap is on, TO_X may lie past the end
8699 of a wrapped line. Then it->current is the
8700 character on the next line, so backtrack to the
8701 space before the wrap point. */
8702 if (skip == MOVE_LINE_CONTINUED)
8704 int prev_x = max (it->current_x - 1, 0);
8705 RESTORE_IT (it, &save_it, save_data);
8706 move_it_in_display_line_to
8707 (it, -1, prev_x, MOVE_TO_X);
8709 else
8710 bidi_unshelve_cache (save_data, 1);
8712 else
8713 move_it_in_display_line_to (it, to_charpos, to_x, op);
8717 /* Move IT forward until it satisfies one or more of the criteria in
8718 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8720 OP is a bit-mask that specifies where to stop, and in particular,
8721 which of those four position arguments makes a difference. See the
8722 description of enum move_operation_enum.
8724 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8725 screen line, this function will set IT to the next position that is
8726 displayed to the right of TO_CHARPOS on the screen. */
8728 void
8729 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8731 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8732 int line_height, line_start_x = 0, reached = 0;
8733 void *backup_data = NULL;
8735 for (;;)
8737 if (op & MOVE_TO_VPOS)
8739 /* If no TO_CHARPOS and no TO_X specified, stop at the
8740 start of the line TO_VPOS. */
8741 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8743 if (it->vpos == to_vpos)
8745 reached = 1;
8746 break;
8748 else
8749 skip = move_it_in_display_line_to (it, -1, -1, 0);
8751 else
8753 /* TO_VPOS >= 0 means stop at TO_X in the line at
8754 TO_VPOS, or at TO_POS, whichever comes first. */
8755 if (it->vpos == to_vpos)
8757 reached = 2;
8758 break;
8761 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8763 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8765 reached = 3;
8766 break;
8768 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8770 /* We have reached TO_X but not in the line we want. */
8771 skip = move_it_in_display_line_to (it, to_charpos,
8772 -1, MOVE_TO_POS);
8773 if (skip == MOVE_POS_MATCH_OR_ZV)
8775 reached = 4;
8776 break;
8781 else if (op & MOVE_TO_Y)
8783 struct it it_backup;
8785 if (it->line_wrap == WORD_WRAP)
8786 SAVE_IT (it_backup, *it, backup_data);
8788 /* TO_Y specified means stop at TO_X in the line containing
8789 TO_Y---or at TO_CHARPOS if this is reached first. The
8790 problem is that we can't really tell whether the line
8791 contains TO_Y before we have completely scanned it, and
8792 this may skip past TO_X. What we do is to first scan to
8793 TO_X.
8795 If TO_X is not specified, use a TO_X of zero. The reason
8796 is to make the outcome of this function more predictable.
8797 If we didn't use TO_X == 0, we would stop at the end of
8798 the line which is probably not what a caller would expect
8799 to happen. */
8800 skip = move_it_in_display_line_to
8801 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8802 (MOVE_TO_X | (op & MOVE_TO_POS)));
8804 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8805 if (skip == MOVE_POS_MATCH_OR_ZV)
8806 reached = 5;
8807 else if (skip == MOVE_X_REACHED)
8809 /* If TO_X was reached, we want to know whether TO_Y is
8810 in the line. We know this is the case if the already
8811 scanned glyphs make the line tall enough. Otherwise,
8812 we must check by scanning the rest of the line. */
8813 line_height = it->max_ascent + it->max_descent;
8814 if (to_y >= it->current_y
8815 && to_y < it->current_y + line_height)
8817 reached = 6;
8818 break;
8820 SAVE_IT (it_backup, *it, backup_data);
8821 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8822 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8823 op & MOVE_TO_POS);
8824 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8825 line_height = it->max_ascent + it->max_descent;
8826 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8828 if (to_y >= it->current_y
8829 && to_y < it->current_y + line_height)
8831 /* If TO_Y is in this line and TO_X was reached
8832 above, we scanned too far. We have to restore
8833 IT's settings to the ones before skipping. But
8834 keep the more accurate values of max_ascent and
8835 max_descent we've found while skipping the rest
8836 of the line, for the sake of callers, such as
8837 pos_visible_p, that need to know the line
8838 height. */
8839 int max_ascent = it->max_ascent;
8840 int max_descent = it->max_descent;
8842 RESTORE_IT (it, &it_backup, backup_data);
8843 it->max_ascent = max_ascent;
8844 it->max_descent = max_descent;
8845 reached = 6;
8847 else
8849 skip = skip2;
8850 if (skip == MOVE_POS_MATCH_OR_ZV)
8851 reached = 7;
8854 else
8856 /* Check whether TO_Y is in this line. */
8857 line_height = it->max_ascent + it->max_descent;
8858 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8860 if (to_y >= it->current_y
8861 && to_y < it->current_y + line_height)
8863 /* When word-wrap is on, TO_X may lie past the end
8864 of a wrapped line. Then it->current is the
8865 character on the next line, so backtrack to the
8866 space before the wrap point. */
8867 if (skip == MOVE_LINE_CONTINUED
8868 && it->line_wrap == WORD_WRAP)
8870 int prev_x = max (it->current_x - 1, 0);
8871 RESTORE_IT (it, &it_backup, backup_data);
8872 skip = move_it_in_display_line_to
8873 (it, -1, prev_x, MOVE_TO_X);
8875 reached = 6;
8879 if (reached)
8880 break;
8882 else if (BUFFERP (it->object)
8883 && (it->method == GET_FROM_BUFFER
8884 || it->method == GET_FROM_STRETCH)
8885 && IT_CHARPOS (*it) >= to_charpos
8886 /* Under bidi iteration, a call to set_iterator_to_next
8887 can scan far beyond to_charpos if the initial
8888 portion of the next line needs to be reordered. In
8889 that case, give move_it_in_display_line_to another
8890 chance below. */
8891 && !(it->bidi_p
8892 && it->bidi_it.scan_dir == -1))
8893 skip = MOVE_POS_MATCH_OR_ZV;
8894 else
8895 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8897 switch (skip)
8899 case MOVE_POS_MATCH_OR_ZV:
8900 reached = 8;
8901 goto out;
8903 case MOVE_NEWLINE_OR_CR:
8904 set_iterator_to_next (it, 1);
8905 it->continuation_lines_width = 0;
8906 break;
8908 case MOVE_LINE_TRUNCATED:
8909 it->continuation_lines_width = 0;
8910 reseat_at_next_visible_line_start (it, 0);
8911 if ((op & MOVE_TO_POS) != 0
8912 && IT_CHARPOS (*it) > to_charpos)
8914 reached = 9;
8915 goto out;
8917 break;
8919 case MOVE_LINE_CONTINUED:
8920 /* For continued lines ending in a tab, some of the glyphs
8921 associated with the tab are displayed on the current
8922 line. Since it->current_x does not include these glyphs,
8923 we use it->last_visible_x instead. */
8924 if (it->c == '\t')
8926 it->continuation_lines_width += it->last_visible_x;
8927 /* When moving by vpos, ensure that the iterator really
8928 advances to the next line (bug#847, bug#969). Fixme:
8929 do we need to do this in other circumstances? */
8930 if (it->current_x != it->last_visible_x
8931 && (op & MOVE_TO_VPOS)
8932 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8934 line_start_x = it->current_x + it->pixel_width
8935 - it->last_visible_x;
8936 set_iterator_to_next (it, 0);
8939 else
8940 it->continuation_lines_width += it->current_x;
8941 break;
8943 default:
8944 emacs_abort ();
8947 /* Reset/increment for the next run. */
8948 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8949 it->current_x = line_start_x;
8950 line_start_x = 0;
8951 it->hpos = 0;
8952 it->current_y += it->max_ascent + it->max_descent;
8953 ++it->vpos;
8954 last_height = it->max_ascent + it->max_descent;
8955 last_max_ascent = it->max_ascent;
8956 it->max_ascent = it->max_descent = 0;
8959 out:
8961 /* On text terminals, we may stop at the end of a line in the middle
8962 of a multi-character glyph. If the glyph itself is continued,
8963 i.e. it is actually displayed on the next line, don't treat this
8964 stopping point as valid; move to the next line instead (unless
8965 that brings us offscreen). */
8966 if (!FRAME_WINDOW_P (it->f)
8967 && op & MOVE_TO_POS
8968 && IT_CHARPOS (*it) == to_charpos
8969 && it->what == IT_CHARACTER
8970 && it->nglyphs > 1
8971 && it->line_wrap == WINDOW_WRAP
8972 && it->current_x == it->last_visible_x - 1
8973 && it->c != '\n'
8974 && it->c != '\t'
8975 && it->vpos < XFASTINT (it->w->window_end_vpos))
8977 it->continuation_lines_width += it->current_x;
8978 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8979 it->current_y += it->max_ascent + it->max_descent;
8980 ++it->vpos;
8981 last_height = it->max_ascent + it->max_descent;
8982 last_max_ascent = it->max_ascent;
8985 if (backup_data)
8986 bidi_unshelve_cache (backup_data, 1);
8988 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8992 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8994 If DY > 0, move IT backward at least that many pixels. DY = 0
8995 means move IT backward to the preceding line start or BEGV. This
8996 function may move over more than DY pixels if IT->current_y - DY
8997 ends up in the middle of a line; in this case IT->current_y will be
8998 set to the top of the line moved to. */
9000 void
9001 move_it_vertically_backward (struct it *it, int dy)
9003 int nlines, h;
9004 struct it it2, it3;
9005 void *it2data = NULL, *it3data = NULL;
9006 ptrdiff_t start_pos;
9008 move_further_back:
9009 eassert (dy >= 0);
9011 start_pos = IT_CHARPOS (*it);
9013 /* Estimate how many newlines we must move back. */
9014 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9016 /* Set the iterator's position that many lines back. */
9017 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9018 back_to_previous_visible_line_start (it);
9020 /* Reseat the iterator here. When moving backward, we don't want
9021 reseat to skip forward over invisible text, set up the iterator
9022 to deliver from overlay strings at the new position etc. So,
9023 use reseat_1 here. */
9024 reseat_1 (it, it->current.pos, 1);
9026 /* We are now surely at a line start. */
9027 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9028 reordering is in effect. */
9029 it->continuation_lines_width = 0;
9031 /* Move forward and see what y-distance we moved. First move to the
9032 start of the next line so that we get its height. We need this
9033 height to be able to tell whether we reached the specified
9034 y-distance. */
9035 SAVE_IT (it2, *it, it2data);
9036 it2.max_ascent = it2.max_descent = 0;
9039 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9040 MOVE_TO_POS | MOVE_TO_VPOS);
9042 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9043 /* If we are in a display string which starts at START_POS,
9044 and that display string includes a newline, and we are
9045 right after that newline (i.e. at the beginning of a
9046 display line), exit the loop, because otherwise we will
9047 infloop, since move_it_to will see that it is already at
9048 START_POS and will not move. */
9049 || (it2.method == GET_FROM_STRING
9050 && IT_CHARPOS (it2) == start_pos
9051 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9052 eassert (IT_CHARPOS (*it) >= BEGV);
9053 SAVE_IT (it3, it2, it3data);
9055 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9056 eassert (IT_CHARPOS (*it) >= BEGV);
9057 /* H is the actual vertical distance from the position in *IT
9058 and the starting position. */
9059 h = it2.current_y - it->current_y;
9060 /* NLINES is the distance in number of lines. */
9061 nlines = it2.vpos - it->vpos;
9063 /* Correct IT's y and vpos position
9064 so that they are relative to the starting point. */
9065 it->vpos -= nlines;
9066 it->current_y -= h;
9068 if (dy == 0)
9070 /* DY == 0 means move to the start of the screen line. The
9071 value of nlines is > 0 if continuation lines were involved,
9072 or if the original IT position was at start of a line. */
9073 RESTORE_IT (it, it, it2data);
9074 if (nlines > 0)
9075 move_it_by_lines (it, nlines);
9076 /* The above code moves us to some position NLINES down,
9077 usually to its first glyph (leftmost in an L2R line), but
9078 that's not necessarily the start of the line, under bidi
9079 reordering. We want to get to the character position
9080 that is immediately after the newline of the previous
9081 line. */
9082 if (it->bidi_p
9083 && !it->continuation_lines_width
9084 && !STRINGP (it->string)
9085 && IT_CHARPOS (*it) > BEGV
9086 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9088 ptrdiff_t nl_pos =
9089 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9091 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9093 bidi_unshelve_cache (it3data, 1);
9095 else
9097 /* The y-position we try to reach, relative to *IT.
9098 Note that H has been subtracted in front of the if-statement. */
9099 int target_y = it->current_y + h - dy;
9100 int y0 = it3.current_y;
9101 int y1;
9102 int line_height;
9104 RESTORE_IT (&it3, &it3, it3data);
9105 y1 = line_bottom_y (&it3);
9106 line_height = y1 - y0;
9107 RESTORE_IT (it, it, it2data);
9108 /* If we did not reach target_y, try to move further backward if
9109 we can. If we moved too far backward, try to move forward. */
9110 if (target_y < it->current_y
9111 /* This is heuristic. In a window that's 3 lines high, with
9112 a line height of 13 pixels each, recentering with point
9113 on the bottom line will try to move -39/2 = 19 pixels
9114 backward. Try to avoid moving into the first line. */
9115 && (it->current_y - target_y
9116 > min (window_box_height (it->w), line_height * 2 / 3))
9117 && IT_CHARPOS (*it) > BEGV)
9119 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9120 target_y - it->current_y));
9121 dy = it->current_y - target_y;
9122 goto move_further_back;
9124 else if (target_y >= it->current_y + line_height
9125 && IT_CHARPOS (*it) < ZV)
9127 /* Should move forward by at least one line, maybe more.
9129 Note: Calling move_it_by_lines can be expensive on
9130 terminal frames, where compute_motion is used (via
9131 vmotion) to do the job, when there are very long lines
9132 and truncate-lines is nil. That's the reason for
9133 treating terminal frames specially here. */
9135 if (!FRAME_WINDOW_P (it->f))
9136 move_it_vertically (it, target_y - (it->current_y + line_height));
9137 else
9141 move_it_by_lines (it, 1);
9143 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9150 /* Move IT by a specified amount of pixel lines DY. DY negative means
9151 move backwards. DY = 0 means move to start of screen line. At the
9152 end, IT will be on the start of a screen line. */
9154 void
9155 move_it_vertically (struct it *it, int dy)
9157 if (dy <= 0)
9158 move_it_vertically_backward (it, -dy);
9159 else
9161 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9162 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9163 MOVE_TO_POS | MOVE_TO_Y);
9164 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9166 /* If buffer ends in ZV without a newline, move to the start of
9167 the line to satisfy the post-condition. */
9168 if (IT_CHARPOS (*it) == ZV
9169 && ZV > BEGV
9170 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9171 move_it_by_lines (it, 0);
9176 /* Move iterator IT past the end of the text line it is in. */
9178 void
9179 move_it_past_eol (struct it *it)
9181 enum move_it_result rc;
9183 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9184 if (rc == MOVE_NEWLINE_OR_CR)
9185 set_iterator_to_next (it, 0);
9189 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9190 negative means move up. DVPOS == 0 means move to the start of the
9191 screen line.
9193 Optimization idea: If we would know that IT->f doesn't use
9194 a face with proportional font, we could be faster for
9195 truncate-lines nil. */
9197 void
9198 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9201 /* The commented-out optimization uses vmotion on terminals. This
9202 gives bad results, because elements like it->what, on which
9203 callers such as pos_visible_p rely, aren't updated. */
9204 /* struct position pos;
9205 if (!FRAME_WINDOW_P (it->f))
9207 struct text_pos textpos;
9209 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9210 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9211 reseat (it, textpos, 1);
9212 it->vpos += pos.vpos;
9213 it->current_y += pos.vpos;
9215 else */
9217 if (dvpos == 0)
9219 /* DVPOS == 0 means move to the start of the screen line. */
9220 move_it_vertically_backward (it, 0);
9221 /* Let next call to line_bottom_y calculate real line height */
9222 last_height = 0;
9224 else if (dvpos > 0)
9226 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9227 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9229 /* Only move to the next buffer position if we ended up in a
9230 string from display property, not in an overlay string
9231 (before-string or after-string). That is because the
9232 latter don't conceal the underlying buffer position, so
9233 we can ask to move the iterator to the exact position we
9234 are interested in. Note that, even if we are already at
9235 IT_CHARPOS (*it), the call below is not a no-op, as it
9236 will detect that we are at the end of the string, pop the
9237 iterator, and compute it->current_x and it->hpos
9238 correctly. */
9239 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9240 -1, -1, -1, MOVE_TO_POS);
9243 else
9245 struct it it2;
9246 void *it2data = NULL;
9247 ptrdiff_t start_charpos, i;
9249 /* Start at the beginning of the screen line containing IT's
9250 position. This may actually move vertically backwards,
9251 in case of overlays, so adjust dvpos accordingly. */
9252 dvpos += it->vpos;
9253 move_it_vertically_backward (it, 0);
9254 dvpos -= it->vpos;
9256 /* Go back -DVPOS visible lines and reseat the iterator there. */
9257 start_charpos = IT_CHARPOS (*it);
9258 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9259 back_to_previous_visible_line_start (it);
9260 reseat (it, it->current.pos, 1);
9262 /* Move further back if we end up in a string or an image. */
9263 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9265 /* First try to move to start of display line. */
9266 dvpos += it->vpos;
9267 move_it_vertically_backward (it, 0);
9268 dvpos -= it->vpos;
9269 if (IT_POS_VALID_AFTER_MOVE_P (it))
9270 break;
9271 /* If start of line is still in string or image,
9272 move further back. */
9273 back_to_previous_visible_line_start (it);
9274 reseat (it, it->current.pos, 1);
9275 dvpos--;
9278 it->current_x = it->hpos = 0;
9280 /* Above call may have moved too far if continuation lines
9281 are involved. Scan forward and see if it did. */
9282 SAVE_IT (it2, *it, it2data);
9283 it2.vpos = it2.current_y = 0;
9284 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9285 it->vpos -= it2.vpos;
9286 it->current_y -= it2.current_y;
9287 it->current_x = it->hpos = 0;
9289 /* If we moved too far back, move IT some lines forward. */
9290 if (it2.vpos > -dvpos)
9292 int delta = it2.vpos + dvpos;
9294 RESTORE_IT (&it2, &it2, it2data);
9295 SAVE_IT (it2, *it, it2data);
9296 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9297 /* Move back again if we got too far ahead. */
9298 if (IT_CHARPOS (*it) >= start_charpos)
9299 RESTORE_IT (it, &it2, it2data);
9300 else
9301 bidi_unshelve_cache (it2data, 1);
9303 else
9304 RESTORE_IT (it, it, it2data);
9308 /* Return 1 if IT points into the middle of a display vector. */
9311 in_display_vector_p (struct it *it)
9313 return (it->method == GET_FROM_DISPLAY_VECTOR
9314 && it->current.dpvec_index > 0
9315 && it->dpvec + it->current.dpvec_index != it->dpend);
9319 /***********************************************************************
9320 Messages
9321 ***********************************************************************/
9324 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9325 to *Messages*. */
9327 void
9328 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9330 Lisp_Object args[3];
9331 Lisp_Object msg, fmt;
9332 char *buffer;
9333 ptrdiff_t len;
9334 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9335 USE_SAFE_ALLOCA;
9337 fmt = msg = Qnil;
9338 GCPRO4 (fmt, msg, arg1, arg2);
9340 args[0] = fmt = build_string (format);
9341 args[1] = arg1;
9342 args[2] = arg2;
9343 msg = Fformat (3, args);
9345 len = SBYTES (msg) + 1;
9346 buffer = SAFE_ALLOCA (len);
9347 memcpy (buffer, SDATA (msg), len);
9349 message_dolog (buffer, len - 1, 1, 0);
9350 SAFE_FREE ();
9352 UNGCPRO;
9356 /* Output a newline in the *Messages* buffer if "needs" one. */
9358 void
9359 message_log_maybe_newline (void)
9361 if (message_log_need_newline)
9362 message_dolog ("", 0, 1, 0);
9366 /* Add a string M of length NBYTES to the message log, optionally
9367 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9368 nonzero, means interpret the contents of M as multibyte. This
9369 function calls low-level routines in order to bypass text property
9370 hooks, etc. which might not be safe to run.
9372 This may GC (insert may run before/after change hooks),
9373 so the buffer M must NOT point to a Lisp string. */
9375 void
9376 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9378 const unsigned char *msg = (const unsigned char *) m;
9380 if (!NILP (Vmemory_full))
9381 return;
9383 if (!NILP (Vmessage_log_max))
9385 struct buffer *oldbuf;
9386 Lisp_Object oldpoint, oldbegv, oldzv;
9387 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9388 ptrdiff_t point_at_end = 0;
9389 ptrdiff_t zv_at_end = 0;
9390 Lisp_Object old_deactivate_mark, tem;
9391 struct gcpro gcpro1;
9393 old_deactivate_mark = Vdeactivate_mark;
9394 oldbuf = current_buffer;
9395 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9396 bset_undo_list (current_buffer, Qt);
9398 oldpoint = message_dolog_marker1;
9399 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9400 oldbegv = message_dolog_marker2;
9401 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9402 oldzv = message_dolog_marker3;
9403 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9404 GCPRO1 (old_deactivate_mark);
9406 if (PT == Z)
9407 point_at_end = 1;
9408 if (ZV == Z)
9409 zv_at_end = 1;
9411 BEGV = BEG;
9412 BEGV_BYTE = BEG_BYTE;
9413 ZV = Z;
9414 ZV_BYTE = Z_BYTE;
9415 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9417 /* Insert the string--maybe converting multibyte to single byte
9418 or vice versa, so that all the text fits the buffer. */
9419 if (multibyte
9420 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9422 ptrdiff_t i;
9423 int c, char_bytes;
9424 char work[1];
9426 /* Convert a multibyte string to single-byte
9427 for the *Message* buffer. */
9428 for (i = 0; i < nbytes; i += char_bytes)
9430 c = string_char_and_length (msg + i, &char_bytes);
9431 work[0] = (ASCII_CHAR_P (c)
9433 : multibyte_char_to_unibyte (c));
9434 insert_1_both (work, 1, 1, 1, 0, 0);
9437 else if (! multibyte
9438 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9440 ptrdiff_t i;
9441 int c, char_bytes;
9442 unsigned char str[MAX_MULTIBYTE_LENGTH];
9443 /* Convert a single-byte string to multibyte
9444 for the *Message* buffer. */
9445 for (i = 0; i < nbytes; i++)
9447 c = msg[i];
9448 MAKE_CHAR_MULTIBYTE (c);
9449 char_bytes = CHAR_STRING (c, str);
9450 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9453 else if (nbytes)
9454 insert_1 (m, nbytes, 1, 0, 0);
9456 if (nlflag)
9458 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9459 printmax_t dups;
9460 insert_1 ("\n", 1, 1, 0, 0);
9462 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9463 this_bol = PT;
9464 this_bol_byte = PT_BYTE;
9466 /* See if this line duplicates the previous one.
9467 If so, combine duplicates. */
9468 if (this_bol > BEG)
9470 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9471 prev_bol = PT;
9472 prev_bol_byte = PT_BYTE;
9474 dups = message_log_check_duplicate (prev_bol_byte,
9475 this_bol_byte);
9476 if (dups)
9478 del_range_both (prev_bol, prev_bol_byte,
9479 this_bol, this_bol_byte, 0);
9480 if (dups > 1)
9482 char dupstr[sizeof " [ times]"
9483 + INT_STRLEN_BOUND (printmax_t)];
9485 /* If you change this format, don't forget to also
9486 change message_log_check_duplicate. */
9487 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9488 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9489 insert_1 (dupstr, duplen, 1, 0, 1);
9494 /* If we have more than the desired maximum number of lines
9495 in the *Messages* buffer now, delete the oldest ones.
9496 This is safe because we don't have undo in this buffer. */
9498 if (NATNUMP (Vmessage_log_max))
9500 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9501 -XFASTINT (Vmessage_log_max) - 1, 0);
9502 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9505 BEGV = XMARKER (oldbegv)->charpos;
9506 BEGV_BYTE = marker_byte_position (oldbegv);
9508 if (zv_at_end)
9510 ZV = Z;
9511 ZV_BYTE = Z_BYTE;
9513 else
9515 ZV = XMARKER (oldzv)->charpos;
9516 ZV_BYTE = marker_byte_position (oldzv);
9519 if (point_at_end)
9520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9521 else
9522 /* We can't do Fgoto_char (oldpoint) because it will run some
9523 Lisp code. */
9524 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9525 XMARKER (oldpoint)->bytepos);
9527 UNGCPRO;
9528 unchain_marker (XMARKER (oldpoint));
9529 unchain_marker (XMARKER (oldbegv));
9530 unchain_marker (XMARKER (oldzv));
9532 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9533 set_buffer_internal (oldbuf);
9534 if (NILP (tem))
9535 windows_or_buffers_changed = old_windows_or_buffers_changed;
9536 message_log_need_newline = !nlflag;
9537 Vdeactivate_mark = old_deactivate_mark;
9542 /* We are at the end of the buffer after just having inserted a newline.
9543 (Note: We depend on the fact we won't be crossing the gap.)
9544 Check to see if the most recent message looks a lot like the previous one.
9545 Return 0 if different, 1 if the new one should just replace it, or a
9546 value N > 1 if we should also append " [N times]". */
9548 static intmax_t
9549 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9551 ptrdiff_t i;
9552 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9553 int seen_dots = 0;
9554 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9555 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9557 for (i = 0; i < len; i++)
9559 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9560 seen_dots = 1;
9561 if (p1[i] != p2[i])
9562 return seen_dots;
9564 p1 += len;
9565 if (*p1 == '\n')
9566 return 2;
9567 if (*p1++ == ' ' && *p1++ == '[')
9569 char *pend;
9570 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9571 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9572 return n+1;
9574 return 0;
9578 /* Display an echo area message M with a specified length of NBYTES
9579 bytes. The string may include null characters. If M is 0, clear
9580 out any existing message, and let the mini-buffer text show
9581 through.
9583 This may GC, so the buffer M must NOT point to a Lisp string. */
9585 void
9586 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9588 /* First flush out any partial line written with print. */
9589 message_log_maybe_newline ();
9590 if (m)
9591 message_dolog (m, nbytes, 1, multibyte);
9592 message2_nolog (m, nbytes, multibyte);
9596 /* The non-logging counterpart of message2. */
9598 void
9599 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9601 struct frame *sf = SELECTED_FRAME ();
9602 message_enable_multibyte = multibyte;
9604 if (FRAME_INITIAL_P (sf))
9606 if (noninteractive_need_newline)
9607 putc ('\n', stderr);
9608 noninteractive_need_newline = 0;
9609 if (m)
9610 fwrite (m, nbytes, 1, stderr);
9611 if (cursor_in_echo_area == 0)
9612 fprintf (stderr, "\n");
9613 fflush (stderr);
9615 /* A null message buffer means that the frame hasn't really been
9616 initialized yet. Error messages get reported properly by
9617 cmd_error, so this must be just an informative message; toss it. */
9618 else if (INTERACTIVE
9619 && sf->glyphs_initialized_p
9620 && FRAME_MESSAGE_BUF (sf))
9622 Lisp_Object mini_window;
9623 struct frame *f;
9625 /* Get the frame containing the mini-buffer
9626 that the selected frame is using. */
9627 mini_window = FRAME_MINIBUF_WINDOW (sf);
9628 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9630 FRAME_SAMPLE_VISIBILITY (f);
9631 if (FRAME_VISIBLE_P (sf)
9632 && ! FRAME_VISIBLE_P (f))
9633 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9635 if (m)
9637 set_message (m, Qnil, nbytes, multibyte);
9638 if (minibuffer_auto_raise)
9639 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9641 else
9642 clear_message (1, 1);
9644 do_pending_window_change (0);
9645 echo_area_display (1);
9646 do_pending_window_change (0);
9647 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9648 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9653 /* Display an echo area message M with a specified length of NBYTES
9654 bytes. The string may include null characters. If M is not a
9655 string, clear out any existing message, and let the mini-buffer
9656 text show through.
9658 This function cancels echoing. */
9660 void
9661 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9663 struct gcpro gcpro1;
9665 GCPRO1 (m);
9666 clear_message (1,1);
9667 cancel_echoing ();
9669 /* First flush out any partial line written with print. */
9670 message_log_maybe_newline ();
9671 if (STRINGP (m))
9673 USE_SAFE_ALLOCA;
9674 char *buffer = SAFE_ALLOCA (nbytes);
9675 memcpy (buffer, SDATA (m), nbytes);
9676 message_dolog (buffer, nbytes, 1, multibyte);
9677 SAFE_FREE ();
9679 message3_nolog (m, nbytes, multibyte);
9681 UNGCPRO;
9685 /* The non-logging version of message3.
9686 This does not cancel echoing, because it is used for echoing.
9687 Perhaps we need to make a separate function for echoing
9688 and make this cancel echoing. */
9690 void
9691 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9693 struct frame *sf = SELECTED_FRAME ();
9694 message_enable_multibyte = multibyte;
9696 if (FRAME_INITIAL_P (sf))
9698 if (noninteractive_need_newline)
9699 putc ('\n', stderr);
9700 noninteractive_need_newline = 0;
9701 if (STRINGP (m))
9702 fwrite (SDATA (m), nbytes, 1, stderr);
9703 if (cursor_in_echo_area == 0)
9704 fprintf (stderr, "\n");
9705 fflush (stderr);
9707 /* A null message buffer means that the frame hasn't really been
9708 initialized yet. Error messages get reported properly by
9709 cmd_error, so this must be just an informative message; toss it. */
9710 else if (INTERACTIVE
9711 && sf->glyphs_initialized_p
9712 && FRAME_MESSAGE_BUF (sf))
9714 Lisp_Object mini_window;
9715 Lisp_Object frame;
9716 struct frame *f;
9718 /* Get the frame containing the mini-buffer
9719 that the selected frame is using. */
9720 mini_window = FRAME_MINIBUF_WINDOW (sf);
9721 frame = XWINDOW (mini_window)->frame;
9722 f = XFRAME (frame);
9724 FRAME_SAMPLE_VISIBILITY (f);
9725 if (FRAME_VISIBLE_P (sf)
9726 && !FRAME_VISIBLE_P (f))
9727 Fmake_frame_visible (frame);
9729 if (STRINGP (m) && SCHARS (m) > 0)
9731 set_message (NULL, m, nbytes, multibyte);
9732 if (minibuffer_auto_raise)
9733 Fraise_frame (frame);
9734 /* Assume we are not echoing.
9735 (If we are, echo_now will override this.) */
9736 echo_message_buffer = Qnil;
9738 else
9739 clear_message (1, 1);
9741 do_pending_window_change (0);
9742 echo_area_display (1);
9743 do_pending_window_change (0);
9744 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9745 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9750 /* Display a null-terminated echo area message M. If M is 0, clear
9751 out any existing message, and let the mini-buffer text show through.
9753 The buffer M must continue to exist until after the echo area gets
9754 cleared or some other message gets displayed there. Do not pass
9755 text that is stored in a Lisp string. Do not pass text in a buffer
9756 that was alloca'd. */
9758 void
9759 message1 (const char *m)
9761 message2 (m, (m ? strlen (m) : 0), 0);
9765 /* The non-logging counterpart of message1. */
9767 void
9768 message1_nolog (const char *m)
9770 message2_nolog (m, (m ? strlen (m) : 0), 0);
9773 /* Display a message M which contains a single %s
9774 which gets replaced with STRING. */
9776 void
9777 message_with_string (const char *m, Lisp_Object string, int log)
9779 CHECK_STRING (string);
9781 if (noninteractive)
9783 if (m)
9785 if (noninteractive_need_newline)
9786 putc ('\n', stderr);
9787 noninteractive_need_newline = 0;
9788 fprintf (stderr, m, SDATA (string));
9789 if (!cursor_in_echo_area)
9790 fprintf (stderr, "\n");
9791 fflush (stderr);
9794 else if (INTERACTIVE)
9796 /* The frame whose minibuffer we're going to display the message on.
9797 It may be larger than the selected frame, so we need
9798 to use its buffer, not the selected frame's buffer. */
9799 Lisp_Object mini_window;
9800 struct frame *f, *sf = SELECTED_FRAME ();
9802 /* Get the frame containing the minibuffer
9803 that the selected frame is using. */
9804 mini_window = FRAME_MINIBUF_WINDOW (sf);
9805 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9807 /* A null message buffer means that the frame hasn't really been
9808 initialized yet. Error messages get reported properly by
9809 cmd_error, so this must be just an informative message; toss it. */
9810 if (FRAME_MESSAGE_BUF (f))
9812 Lisp_Object args[2], msg;
9813 struct gcpro gcpro1, gcpro2;
9815 args[0] = build_string (m);
9816 args[1] = msg = string;
9817 GCPRO2 (args[0], msg);
9818 gcpro1.nvars = 2;
9820 msg = Fformat (2, args);
9822 if (log)
9823 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9824 else
9825 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9827 UNGCPRO;
9829 /* Print should start at the beginning of the message
9830 buffer next time. */
9831 message_buf_print = 0;
9837 /* Dump an informative message to the minibuf. If M is 0, clear out
9838 any existing message, and let the mini-buffer text show through. */
9840 static void
9841 vmessage (const char *m, va_list ap)
9843 if (noninteractive)
9845 if (m)
9847 if (noninteractive_need_newline)
9848 putc ('\n', stderr);
9849 noninteractive_need_newline = 0;
9850 vfprintf (stderr, m, ap);
9851 if (cursor_in_echo_area == 0)
9852 fprintf (stderr, "\n");
9853 fflush (stderr);
9856 else if (INTERACTIVE)
9858 /* The frame whose mini-buffer we're going to display the message
9859 on. It may be larger than the selected frame, so we need to
9860 use its buffer, not the selected frame's buffer. */
9861 Lisp_Object mini_window;
9862 struct frame *f, *sf = SELECTED_FRAME ();
9864 /* Get the frame containing the mini-buffer
9865 that the selected frame is using. */
9866 mini_window = FRAME_MINIBUF_WINDOW (sf);
9867 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9869 /* A null message buffer means that the frame hasn't really been
9870 initialized yet. Error messages get reported properly by
9871 cmd_error, so this must be just an informative message; toss
9872 it. */
9873 if (FRAME_MESSAGE_BUF (f))
9875 if (m)
9877 ptrdiff_t len;
9879 len = doprnt (FRAME_MESSAGE_BUF (f),
9880 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9882 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9884 else
9885 message1 (0);
9887 /* Print should start at the beginning of the message
9888 buffer next time. */
9889 message_buf_print = 0;
9894 void
9895 message (const char *m, ...)
9897 va_list ap;
9898 va_start (ap, m);
9899 vmessage (m, ap);
9900 va_end (ap);
9904 #if 0
9905 /* The non-logging version of message. */
9907 void
9908 message_nolog (const char *m, ...)
9910 Lisp_Object old_log_max;
9911 va_list ap;
9912 va_start (ap, m);
9913 old_log_max = Vmessage_log_max;
9914 Vmessage_log_max = Qnil;
9915 vmessage (m, ap);
9916 Vmessage_log_max = old_log_max;
9917 va_end (ap);
9919 #endif
9922 /* Display the current message in the current mini-buffer. This is
9923 only called from error handlers in process.c, and is not time
9924 critical. */
9926 void
9927 update_echo_area (void)
9929 if (!NILP (echo_area_buffer[0]))
9931 Lisp_Object string;
9932 string = Fcurrent_message ();
9933 message3 (string, SBYTES (string),
9934 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9939 /* Make sure echo area buffers in `echo_buffers' are live.
9940 If they aren't, make new ones. */
9942 static void
9943 ensure_echo_area_buffers (void)
9945 int i;
9947 for (i = 0; i < 2; ++i)
9948 if (!BUFFERP (echo_buffer[i])
9949 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9951 char name[30];
9952 Lisp_Object old_buffer;
9953 int j;
9955 old_buffer = echo_buffer[i];
9956 echo_buffer[i] = Fget_buffer_create
9957 (make_formatted_string (name, " *Echo Area %d*", i));
9958 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9959 /* to force word wrap in echo area -
9960 it was decided to postpone this*/
9961 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9963 for (j = 0; j < 2; ++j)
9964 if (EQ (old_buffer, echo_area_buffer[j]))
9965 echo_area_buffer[j] = echo_buffer[i];
9970 /* Call FN with args A1..A4 with either the current or last displayed
9971 echo_area_buffer as current buffer.
9973 WHICH zero means use the current message buffer
9974 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9975 from echo_buffer[] and clear it.
9977 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9978 suitable buffer from echo_buffer[] and clear it.
9980 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9981 that the current message becomes the last displayed one, make
9982 choose a suitable buffer for echo_area_buffer[0], and clear it.
9984 Value is what FN returns. */
9986 static int
9987 with_echo_area_buffer (struct window *w, int which,
9988 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9989 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9991 Lisp_Object buffer;
9992 int this_one, the_other, clear_buffer_p, rc;
9993 ptrdiff_t count = SPECPDL_INDEX ();
9995 /* If buffers aren't live, make new ones. */
9996 ensure_echo_area_buffers ();
9998 clear_buffer_p = 0;
10000 if (which == 0)
10001 this_one = 0, the_other = 1;
10002 else if (which > 0)
10003 this_one = 1, the_other = 0;
10004 else
10006 this_one = 0, the_other = 1;
10007 clear_buffer_p = 1;
10009 /* We need a fresh one in case the current echo buffer equals
10010 the one containing the last displayed echo area message. */
10011 if (!NILP (echo_area_buffer[this_one])
10012 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10013 echo_area_buffer[this_one] = Qnil;
10016 /* Choose a suitable buffer from echo_buffer[] is we don't
10017 have one. */
10018 if (NILP (echo_area_buffer[this_one]))
10020 echo_area_buffer[this_one]
10021 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10022 ? echo_buffer[the_other]
10023 : echo_buffer[this_one]);
10024 clear_buffer_p = 1;
10027 buffer = echo_area_buffer[this_one];
10029 /* Don't get confused by reusing the buffer used for echoing
10030 for a different purpose. */
10031 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10032 cancel_echoing ();
10034 record_unwind_protect (unwind_with_echo_area_buffer,
10035 with_echo_area_buffer_unwind_data (w));
10037 /* Make the echo area buffer current. Note that for display
10038 purposes, it is not necessary that the displayed window's buffer
10039 == current_buffer, except for text property lookup. So, let's
10040 only set that buffer temporarily here without doing a full
10041 Fset_window_buffer. We must also change w->pointm, though,
10042 because otherwise an assertions in unshow_buffer fails, and Emacs
10043 aborts. */
10044 set_buffer_internal_1 (XBUFFER (buffer));
10045 if (w)
10047 wset_buffer (w, buffer);
10048 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10051 bset_undo_list (current_buffer, Qt);
10052 bset_read_only (current_buffer, Qnil);
10053 specbind (Qinhibit_read_only, Qt);
10054 specbind (Qinhibit_modification_hooks, Qt);
10056 if (clear_buffer_p && Z > BEG)
10057 del_range (BEG, Z);
10059 eassert (BEGV >= BEG);
10060 eassert (ZV <= Z && ZV >= BEGV);
10062 rc = fn (a1, a2, a3, a4);
10064 eassert (BEGV >= BEG);
10065 eassert (ZV <= Z && ZV >= BEGV);
10067 unbind_to (count, Qnil);
10068 return rc;
10072 /* Save state that should be preserved around the call to the function
10073 FN called in with_echo_area_buffer. */
10075 static Lisp_Object
10076 with_echo_area_buffer_unwind_data (struct window *w)
10078 int i = 0;
10079 Lisp_Object vector, tmp;
10081 /* Reduce consing by keeping one vector in
10082 Vwith_echo_area_save_vector. */
10083 vector = Vwith_echo_area_save_vector;
10084 Vwith_echo_area_save_vector = Qnil;
10086 if (NILP (vector))
10087 vector = Fmake_vector (make_number (7), Qnil);
10089 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10090 ASET (vector, i, Vdeactivate_mark); ++i;
10091 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10093 if (w)
10095 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10096 ASET (vector, i, w->buffer); ++i;
10097 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10098 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10100 else
10102 int end = i + 4;
10103 for (; i < end; ++i)
10104 ASET (vector, i, Qnil);
10107 eassert (i == ASIZE (vector));
10108 return vector;
10112 /* Restore global state from VECTOR which was created by
10113 with_echo_area_buffer_unwind_data. */
10115 static Lisp_Object
10116 unwind_with_echo_area_buffer (Lisp_Object vector)
10118 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10119 Vdeactivate_mark = AREF (vector, 1);
10120 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10122 if (WINDOWP (AREF (vector, 3)))
10124 struct window *w;
10125 Lisp_Object buffer, charpos, bytepos;
10127 w = XWINDOW (AREF (vector, 3));
10128 buffer = AREF (vector, 4);
10129 charpos = AREF (vector, 5);
10130 bytepos = AREF (vector, 6);
10132 wset_buffer (w, buffer);
10133 set_marker_both (w->pointm, buffer,
10134 XFASTINT (charpos), XFASTINT (bytepos));
10137 Vwith_echo_area_save_vector = vector;
10138 return Qnil;
10142 /* Set up the echo area for use by print functions. MULTIBYTE_P
10143 non-zero means we will print multibyte. */
10145 void
10146 setup_echo_area_for_printing (int multibyte_p)
10148 /* If we can't find an echo area any more, exit. */
10149 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10150 Fkill_emacs (Qnil);
10152 ensure_echo_area_buffers ();
10154 if (!message_buf_print)
10156 /* A message has been output since the last time we printed.
10157 Choose a fresh echo area buffer. */
10158 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10159 echo_area_buffer[0] = echo_buffer[1];
10160 else
10161 echo_area_buffer[0] = echo_buffer[0];
10163 /* Switch to that buffer and clear it. */
10164 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10165 bset_truncate_lines (current_buffer, Qnil);
10167 if (Z > BEG)
10169 ptrdiff_t count = SPECPDL_INDEX ();
10170 specbind (Qinhibit_read_only, Qt);
10171 /* Note that undo recording is always disabled. */
10172 del_range (BEG, Z);
10173 unbind_to (count, Qnil);
10175 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10177 /* Set up the buffer for the multibyteness we need. */
10178 if (multibyte_p
10179 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10180 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10182 /* Raise the frame containing the echo area. */
10183 if (minibuffer_auto_raise)
10185 struct frame *sf = SELECTED_FRAME ();
10186 Lisp_Object mini_window;
10187 mini_window = FRAME_MINIBUF_WINDOW (sf);
10188 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10191 message_log_maybe_newline ();
10192 message_buf_print = 1;
10194 else
10196 if (NILP (echo_area_buffer[0]))
10198 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10199 echo_area_buffer[0] = echo_buffer[1];
10200 else
10201 echo_area_buffer[0] = echo_buffer[0];
10204 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10206 /* Someone switched buffers between print requests. */
10207 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10208 bset_truncate_lines (current_buffer, Qnil);
10214 /* Display an echo area message in window W. Value is non-zero if W's
10215 height is changed. If display_last_displayed_message_p is
10216 non-zero, display the message that was last displayed, otherwise
10217 display the current message. */
10219 static int
10220 display_echo_area (struct window *w)
10222 int i, no_message_p, window_height_changed_p;
10224 /* Temporarily disable garbage collections while displaying the echo
10225 area. This is done because a GC can print a message itself.
10226 That message would modify the echo area buffer's contents while a
10227 redisplay of the buffer is going on, and seriously confuse
10228 redisplay. */
10229 ptrdiff_t count = inhibit_garbage_collection ();
10231 /* If there is no message, we must call display_echo_area_1
10232 nevertheless because it resizes the window. But we will have to
10233 reset the echo_area_buffer in question to nil at the end because
10234 with_echo_area_buffer will sets it to an empty buffer. */
10235 i = display_last_displayed_message_p ? 1 : 0;
10236 no_message_p = NILP (echo_area_buffer[i]);
10238 window_height_changed_p
10239 = with_echo_area_buffer (w, display_last_displayed_message_p,
10240 display_echo_area_1,
10241 (intptr_t) w, Qnil, 0, 0);
10243 if (no_message_p)
10244 echo_area_buffer[i] = Qnil;
10246 unbind_to (count, Qnil);
10247 return window_height_changed_p;
10251 /* Helper for display_echo_area. Display the current buffer which
10252 contains the current echo area message in window W, a mini-window,
10253 a pointer to which is passed in A1. A2..A4 are currently not used.
10254 Change the height of W so that all of the message is displayed.
10255 Value is non-zero if height of W was changed. */
10257 static int
10258 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10260 intptr_t i1 = a1;
10261 struct window *w = (struct window *) i1;
10262 Lisp_Object window;
10263 struct text_pos start;
10264 int window_height_changed_p = 0;
10266 /* Do this before displaying, so that we have a large enough glyph
10267 matrix for the display. If we can't get enough space for the
10268 whole text, display the last N lines. That works by setting w->start. */
10269 window_height_changed_p = resize_mini_window (w, 0);
10271 /* Use the starting position chosen by resize_mini_window. */
10272 SET_TEXT_POS_FROM_MARKER (start, w->start);
10274 /* Display. */
10275 clear_glyph_matrix (w->desired_matrix);
10276 XSETWINDOW (window, w);
10277 try_window (window, start, 0);
10279 return window_height_changed_p;
10283 /* Resize the echo area window to exactly the size needed for the
10284 currently displayed message, if there is one. If a mini-buffer
10285 is active, don't shrink it. */
10287 void
10288 resize_echo_area_exactly (void)
10290 if (BUFFERP (echo_area_buffer[0])
10291 && WINDOWP (echo_area_window))
10293 struct window *w = XWINDOW (echo_area_window);
10294 int resized_p;
10295 Lisp_Object resize_exactly;
10297 if (minibuf_level == 0)
10298 resize_exactly = Qt;
10299 else
10300 resize_exactly = Qnil;
10302 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10303 (intptr_t) w, resize_exactly,
10304 0, 0);
10305 if (resized_p)
10307 ++windows_or_buffers_changed;
10308 ++update_mode_lines;
10309 redisplay_internal ();
10315 /* Callback function for with_echo_area_buffer, when used from
10316 resize_echo_area_exactly. A1 contains a pointer to the window to
10317 resize, EXACTLY non-nil means resize the mini-window exactly to the
10318 size of the text displayed. A3 and A4 are not used. Value is what
10319 resize_mini_window returns. */
10321 static int
10322 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10324 intptr_t i1 = a1;
10325 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10329 /* Resize mini-window W to fit the size of its contents. EXACT_P
10330 means size the window exactly to the size needed. Otherwise, it's
10331 only enlarged until W's buffer is empty.
10333 Set W->start to the right place to begin display. If the whole
10334 contents fit, start at the beginning. Otherwise, start so as
10335 to make the end of the contents appear. This is particularly
10336 important for y-or-n-p, but seems desirable generally.
10338 Value is non-zero if the window height has been changed. */
10341 resize_mini_window (struct window *w, int exact_p)
10343 struct frame *f = XFRAME (w->frame);
10344 int window_height_changed_p = 0;
10346 eassert (MINI_WINDOW_P (w));
10348 /* By default, start display at the beginning. */
10349 set_marker_both (w->start, w->buffer,
10350 BUF_BEGV (XBUFFER (w->buffer)),
10351 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10353 /* Don't resize windows while redisplaying a window; it would
10354 confuse redisplay functions when the size of the window they are
10355 displaying changes from under them. Such a resizing can happen,
10356 for instance, when which-func prints a long message while
10357 we are running fontification-functions. We're running these
10358 functions with safe_call which binds inhibit-redisplay to t. */
10359 if (!NILP (Vinhibit_redisplay))
10360 return 0;
10362 /* Nil means don't try to resize. */
10363 if (NILP (Vresize_mini_windows)
10364 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10365 return 0;
10367 if (!FRAME_MINIBUF_ONLY_P (f))
10369 struct it it;
10370 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10371 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10372 int height;
10373 EMACS_INT max_height;
10374 int unit = FRAME_LINE_HEIGHT (f);
10375 struct text_pos start;
10376 struct buffer *old_current_buffer = NULL;
10378 if (current_buffer != XBUFFER (w->buffer))
10380 old_current_buffer = current_buffer;
10381 set_buffer_internal (XBUFFER (w->buffer));
10384 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10386 /* Compute the max. number of lines specified by the user. */
10387 if (FLOATP (Vmax_mini_window_height))
10388 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10389 else if (INTEGERP (Vmax_mini_window_height))
10390 max_height = XINT (Vmax_mini_window_height);
10391 else
10392 max_height = total_height / 4;
10394 /* Correct that max. height if it's bogus. */
10395 max_height = max (1, max_height);
10396 max_height = min (total_height, max_height);
10398 /* Find out the height of the text in the window. */
10399 if (it.line_wrap == TRUNCATE)
10400 height = 1;
10401 else
10403 last_height = 0;
10404 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10405 if (it.max_ascent == 0 && it.max_descent == 0)
10406 height = it.current_y + last_height;
10407 else
10408 height = it.current_y + it.max_ascent + it.max_descent;
10409 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10410 height = (height + unit - 1) / unit;
10413 /* Compute a suitable window start. */
10414 if (height > max_height)
10416 height = max_height;
10417 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10418 move_it_vertically_backward (&it, (height - 1) * unit);
10419 start = it.current.pos;
10421 else
10422 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10423 SET_MARKER_FROM_TEXT_POS (w->start, start);
10425 if (EQ (Vresize_mini_windows, Qgrow_only))
10427 /* Let it grow only, until we display an empty message, in which
10428 case the window shrinks again. */
10429 if (height > WINDOW_TOTAL_LINES (w))
10431 int old_height = WINDOW_TOTAL_LINES (w);
10432 freeze_window_starts (f, 1);
10433 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10434 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10436 else if (height < WINDOW_TOTAL_LINES (w)
10437 && (exact_p || BEGV == ZV))
10439 int old_height = WINDOW_TOTAL_LINES (w);
10440 freeze_window_starts (f, 0);
10441 shrink_mini_window (w);
10442 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10445 else
10447 /* Always resize to exact size needed. */
10448 if (height > WINDOW_TOTAL_LINES (w))
10450 int old_height = WINDOW_TOTAL_LINES (w);
10451 freeze_window_starts (f, 1);
10452 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10453 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10455 else if (height < WINDOW_TOTAL_LINES (w))
10457 int old_height = WINDOW_TOTAL_LINES (w);
10458 freeze_window_starts (f, 0);
10459 shrink_mini_window (w);
10461 if (height)
10463 freeze_window_starts (f, 1);
10464 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10467 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10471 if (old_current_buffer)
10472 set_buffer_internal (old_current_buffer);
10475 return window_height_changed_p;
10479 /* Value is the current message, a string, or nil if there is no
10480 current message. */
10482 Lisp_Object
10483 current_message (void)
10485 Lisp_Object msg;
10487 if (!BUFFERP (echo_area_buffer[0]))
10488 msg = Qnil;
10489 else
10491 with_echo_area_buffer (0, 0, current_message_1,
10492 (intptr_t) &msg, Qnil, 0, 0);
10493 if (NILP (msg))
10494 echo_area_buffer[0] = Qnil;
10497 return msg;
10501 static int
10502 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10504 intptr_t i1 = a1;
10505 Lisp_Object *msg = (Lisp_Object *) i1;
10507 if (Z > BEG)
10508 *msg = make_buffer_string (BEG, Z, 1);
10509 else
10510 *msg = Qnil;
10511 return 0;
10515 /* Push the current message on Vmessage_stack for later restoration
10516 by restore_message. Value is non-zero if the current message isn't
10517 empty. This is a relatively infrequent operation, so it's not
10518 worth optimizing. */
10520 bool
10521 push_message (void)
10523 Lisp_Object msg = current_message ();
10524 Vmessage_stack = Fcons (msg, Vmessage_stack);
10525 return STRINGP (msg);
10529 /* Restore message display from the top of Vmessage_stack. */
10531 void
10532 restore_message (void)
10534 Lisp_Object msg;
10536 eassert (CONSP (Vmessage_stack));
10537 msg = XCAR (Vmessage_stack);
10538 if (STRINGP (msg))
10539 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10540 else
10541 message3_nolog (msg, 0, 0);
10545 /* Handler for record_unwind_protect calling pop_message. */
10547 Lisp_Object
10548 pop_message_unwind (Lisp_Object dummy)
10550 pop_message ();
10551 return Qnil;
10554 /* Pop the top-most entry off Vmessage_stack. */
10556 static void
10557 pop_message (void)
10559 eassert (CONSP (Vmessage_stack));
10560 Vmessage_stack = XCDR (Vmessage_stack);
10564 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10565 exits. If the stack is not empty, we have a missing pop_message
10566 somewhere. */
10568 void
10569 check_message_stack (void)
10571 if (!NILP (Vmessage_stack))
10572 emacs_abort ();
10576 /* Truncate to NCHARS what will be displayed in the echo area the next
10577 time we display it---but don't redisplay it now. */
10579 void
10580 truncate_echo_area (ptrdiff_t nchars)
10582 if (nchars == 0)
10583 echo_area_buffer[0] = Qnil;
10584 /* A null message buffer means that the frame hasn't really been
10585 initialized yet. Error messages get reported properly by
10586 cmd_error, so this must be just an informative message; toss it. */
10587 else if (!noninteractive
10588 && INTERACTIVE
10589 && !NILP (echo_area_buffer[0]))
10591 struct frame *sf = SELECTED_FRAME ();
10592 if (FRAME_MESSAGE_BUF (sf))
10593 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10598 /* Helper function for truncate_echo_area. Truncate the current
10599 message to at most NCHARS characters. */
10601 static int
10602 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10604 if (BEG + nchars < Z)
10605 del_range (BEG + nchars, Z);
10606 if (Z == BEG)
10607 echo_area_buffer[0] = Qnil;
10608 return 0;
10611 /* Set the current message to a substring of S or STRING.
10613 If STRING is a Lisp string, set the message to the first NBYTES
10614 bytes from STRING. NBYTES zero means use the whole string. If
10615 STRING is multibyte, the message will be displayed multibyte.
10617 If S is not null, set the message to the first LEN bytes of S. LEN
10618 zero means use the whole string. MULTIBYTE_P non-zero means S is
10619 multibyte. Display the message multibyte in that case.
10621 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10622 to t before calling set_message_1 (which calls insert).
10625 static void
10626 set_message (const char *s, Lisp_Object string,
10627 ptrdiff_t nbytes, int multibyte_p)
10629 message_enable_multibyte
10630 = ((s && multibyte_p)
10631 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10633 with_echo_area_buffer (0, -1, set_message_1,
10634 (intptr_t) s, string, nbytes, multibyte_p);
10635 message_buf_print = 0;
10636 help_echo_showing_p = 0;
10638 if (STRINGP (Vdebug_on_message)
10639 && fast_string_match (Vdebug_on_message, string) >= 0)
10640 call_debugger (list2 (Qerror, string));
10644 /* Helper function for set_message. Arguments have the same meaning
10645 as there, with A1 corresponding to S and A2 corresponding to STRING
10646 This function is called with the echo area buffer being
10647 current. */
10649 static int
10650 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10652 intptr_t i1 = a1;
10653 const char *s = (const char *) i1;
10654 const unsigned char *msg = (const unsigned char *) s;
10655 Lisp_Object string = a2;
10657 /* Change multibyteness of the echo buffer appropriately. */
10658 if (message_enable_multibyte
10659 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10660 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10662 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10663 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10664 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10666 /* Insert new message at BEG. */
10667 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10669 if (STRINGP (string))
10671 ptrdiff_t nchars;
10673 if (nbytes == 0)
10674 nbytes = SBYTES (string);
10675 nchars = string_byte_to_char (string, nbytes);
10677 /* This function takes care of single/multibyte conversion. We
10678 just have to ensure that the echo area buffer has the right
10679 setting of enable_multibyte_characters. */
10680 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10682 else if (s)
10684 if (nbytes == 0)
10685 nbytes = strlen (s);
10687 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10689 /* Convert from multi-byte to single-byte. */
10690 ptrdiff_t i;
10691 int c, n;
10692 char work[1];
10694 /* Convert a multibyte string to single-byte. */
10695 for (i = 0; i < nbytes; i += n)
10697 c = string_char_and_length (msg + i, &n);
10698 work[0] = (ASCII_CHAR_P (c)
10700 : multibyte_char_to_unibyte (c));
10701 insert_1_both (work, 1, 1, 1, 0, 0);
10704 else if (!multibyte_p
10705 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10707 /* Convert from single-byte to multi-byte. */
10708 ptrdiff_t i;
10709 int c, n;
10710 unsigned char str[MAX_MULTIBYTE_LENGTH];
10712 /* Convert a single-byte string to multibyte. */
10713 for (i = 0; i < nbytes; i++)
10715 c = msg[i];
10716 MAKE_CHAR_MULTIBYTE (c);
10717 n = CHAR_STRING (c, str);
10718 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10721 else
10722 insert_1 (s, nbytes, 1, 0, 0);
10725 return 0;
10729 /* Clear messages. CURRENT_P non-zero means clear the current
10730 message. LAST_DISPLAYED_P non-zero means clear the message
10731 last displayed. */
10733 void
10734 clear_message (int current_p, int last_displayed_p)
10736 if (current_p)
10738 echo_area_buffer[0] = Qnil;
10739 message_cleared_p = 1;
10742 if (last_displayed_p)
10743 echo_area_buffer[1] = Qnil;
10745 message_buf_print = 0;
10748 /* Clear garbaged frames.
10750 This function is used where the old redisplay called
10751 redraw_garbaged_frames which in turn called redraw_frame which in
10752 turn called clear_frame. The call to clear_frame was a source of
10753 flickering. I believe a clear_frame is not necessary. It should
10754 suffice in the new redisplay to invalidate all current matrices,
10755 and ensure a complete redisplay of all windows. */
10757 static void
10758 clear_garbaged_frames (void)
10760 if (frame_garbaged)
10762 Lisp_Object tail, frame;
10763 int changed_count = 0;
10765 FOR_EACH_FRAME (tail, frame)
10767 struct frame *f = XFRAME (frame);
10769 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10771 if (f->resized_p)
10773 Fredraw_frame (frame);
10774 f->force_flush_display_p = 1;
10776 clear_current_matrices (f);
10777 changed_count++;
10778 f->garbaged = 0;
10779 f->resized_p = 0;
10783 frame_garbaged = 0;
10784 if (changed_count)
10785 ++windows_or_buffers_changed;
10790 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10791 is non-zero update selected_frame. Value is non-zero if the
10792 mini-windows height has been changed. */
10794 static int
10795 echo_area_display (int update_frame_p)
10797 Lisp_Object mini_window;
10798 struct window *w;
10799 struct frame *f;
10800 int window_height_changed_p = 0;
10801 struct frame *sf = SELECTED_FRAME ();
10803 mini_window = FRAME_MINIBUF_WINDOW (sf);
10804 w = XWINDOW (mini_window);
10805 f = XFRAME (WINDOW_FRAME (w));
10807 /* Don't display if frame is invisible or not yet initialized. */
10808 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10809 return 0;
10811 #ifdef HAVE_WINDOW_SYSTEM
10812 /* When Emacs starts, selected_frame may be the initial terminal
10813 frame. If we let this through, a message would be displayed on
10814 the terminal. */
10815 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10816 return 0;
10817 #endif /* HAVE_WINDOW_SYSTEM */
10819 /* Redraw garbaged frames. */
10820 if (frame_garbaged)
10821 clear_garbaged_frames ();
10823 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10825 echo_area_window = mini_window;
10826 window_height_changed_p = display_echo_area (w);
10827 w->must_be_updated_p = 1;
10829 /* Update the display, unless called from redisplay_internal.
10830 Also don't update the screen during redisplay itself. The
10831 update will happen at the end of redisplay, and an update
10832 here could cause confusion. */
10833 if (update_frame_p && !redisplaying_p)
10835 int n = 0;
10837 /* If the display update has been interrupted by pending
10838 input, update mode lines in the frame. Due to the
10839 pending input, it might have been that redisplay hasn't
10840 been called, so that mode lines above the echo area are
10841 garbaged. This looks odd, so we prevent it here. */
10842 if (!display_completed)
10843 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10845 if (window_height_changed_p
10846 /* Don't do this if Emacs is shutting down. Redisplay
10847 needs to run hooks. */
10848 && !NILP (Vrun_hooks))
10850 /* Must update other windows. Likewise as in other
10851 cases, don't let this update be interrupted by
10852 pending input. */
10853 ptrdiff_t count = SPECPDL_INDEX ();
10854 specbind (Qredisplay_dont_pause, Qt);
10855 windows_or_buffers_changed = 1;
10856 redisplay_internal ();
10857 unbind_to (count, Qnil);
10859 else if (FRAME_WINDOW_P (f) && n == 0)
10861 /* Window configuration is the same as before.
10862 Can do with a display update of the echo area,
10863 unless we displayed some mode lines. */
10864 update_single_window (w, 1);
10865 FRAME_RIF (f)->flush_display (f);
10867 else
10868 update_frame (f, 1, 1);
10870 /* If cursor is in the echo area, make sure that the next
10871 redisplay displays the minibuffer, so that the cursor will
10872 be replaced with what the minibuffer wants. */
10873 if (cursor_in_echo_area)
10874 ++windows_or_buffers_changed;
10877 else if (!EQ (mini_window, selected_window))
10878 windows_or_buffers_changed++;
10880 /* Last displayed message is now the current message. */
10881 echo_area_buffer[1] = echo_area_buffer[0];
10882 /* Inform read_char that we're not echoing. */
10883 echo_message_buffer = Qnil;
10885 /* Prevent redisplay optimization in redisplay_internal by resetting
10886 this_line_start_pos. This is done because the mini-buffer now
10887 displays the message instead of its buffer text. */
10888 if (EQ (mini_window, selected_window))
10889 CHARPOS (this_line_start_pos) = 0;
10891 return window_height_changed_p;
10896 /***********************************************************************
10897 Mode Lines and Frame Titles
10898 ***********************************************************************/
10900 /* A buffer for constructing non-propertized mode-line strings and
10901 frame titles in it; allocated from the heap in init_xdisp and
10902 resized as needed in store_mode_line_noprop_char. */
10904 static char *mode_line_noprop_buf;
10906 /* The buffer's end, and a current output position in it. */
10908 static char *mode_line_noprop_buf_end;
10909 static char *mode_line_noprop_ptr;
10911 #define MODE_LINE_NOPROP_LEN(start) \
10912 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10914 static enum {
10915 MODE_LINE_DISPLAY = 0,
10916 MODE_LINE_TITLE,
10917 MODE_LINE_NOPROP,
10918 MODE_LINE_STRING
10919 } mode_line_target;
10921 /* Alist that caches the results of :propertize.
10922 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10923 static Lisp_Object mode_line_proptrans_alist;
10925 /* List of strings making up the mode-line. */
10926 static Lisp_Object mode_line_string_list;
10928 /* Base face property when building propertized mode line string. */
10929 static Lisp_Object mode_line_string_face;
10930 static Lisp_Object mode_line_string_face_prop;
10933 /* Unwind data for mode line strings */
10935 static Lisp_Object Vmode_line_unwind_vector;
10937 static Lisp_Object
10938 format_mode_line_unwind_data (struct frame *target_frame,
10939 struct buffer *obuf,
10940 Lisp_Object owin,
10941 int save_proptrans)
10943 Lisp_Object vector, tmp;
10945 /* Reduce consing by keeping one vector in
10946 Vwith_echo_area_save_vector. */
10947 vector = Vmode_line_unwind_vector;
10948 Vmode_line_unwind_vector = Qnil;
10950 if (NILP (vector))
10951 vector = Fmake_vector (make_number (10), Qnil);
10953 ASET (vector, 0, make_number (mode_line_target));
10954 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10955 ASET (vector, 2, mode_line_string_list);
10956 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10957 ASET (vector, 4, mode_line_string_face);
10958 ASET (vector, 5, mode_line_string_face_prop);
10960 if (obuf)
10961 XSETBUFFER (tmp, obuf);
10962 else
10963 tmp = Qnil;
10964 ASET (vector, 6, tmp);
10965 ASET (vector, 7, owin);
10966 if (target_frame)
10968 /* Similarly to `with-selected-window', if the operation selects
10969 a window on another frame, we must restore that frame's
10970 selected window, and (for a tty) the top-frame. */
10971 ASET (vector, 8, target_frame->selected_window);
10972 if (FRAME_TERMCAP_P (target_frame))
10973 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10976 return vector;
10979 static Lisp_Object
10980 unwind_format_mode_line (Lisp_Object vector)
10982 Lisp_Object old_window = AREF (vector, 7);
10983 Lisp_Object target_frame_window = AREF (vector, 8);
10984 Lisp_Object old_top_frame = AREF (vector, 9);
10986 mode_line_target = XINT (AREF (vector, 0));
10987 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10988 mode_line_string_list = AREF (vector, 2);
10989 if (! EQ (AREF (vector, 3), Qt))
10990 mode_line_proptrans_alist = AREF (vector, 3);
10991 mode_line_string_face = AREF (vector, 4);
10992 mode_line_string_face_prop = AREF (vector, 5);
10994 /* Select window before buffer, since it may change the buffer. */
10995 if (!NILP (old_window))
10997 /* If the operation that we are unwinding had selected a window
10998 on a different frame, reset its frame-selected-window. For a
10999 text terminal, reset its top-frame if necessary. */
11000 if (!NILP (target_frame_window))
11002 Lisp_Object frame
11003 = WINDOW_FRAME (XWINDOW (target_frame_window));
11005 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11006 Fselect_window (target_frame_window, Qt);
11008 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11009 Fselect_frame (old_top_frame, Qt);
11012 Fselect_window (old_window, Qt);
11015 if (!NILP (AREF (vector, 6)))
11017 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11018 ASET (vector, 6, Qnil);
11021 Vmode_line_unwind_vector = vector;
11022 return Qnil;
11026 /* Store a single character C for the frame title in mode_line_noprop_buf.
11027 Re-allocate mode_line_noprop_buf if necessary. */
11029 static void
11030 store_mode_line_noprop_char (char c)
11032 /* If output position has reached the end of the allocated buffer,
11033 increase the buffer's size. */
11034 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11036 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11037 ptrdiff_t size = len;
11038 mode_line_noprop_buf =
11039 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11040 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11041 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11044 *mode_line_noprop_ptr++ = c;
11048 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11049 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11050 characters that yield more columns than PRECISION; PRECISION <= 0
11051 means copy the whole string. Pad with spaces until FIELD_WIDTH
11052 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11053 pad. Called from display_mode_element when it is used to build a
11054 frame title. */
11056 static int
11057 store_mode_line_noprop (const char *string, int field_width, int precision)
11059 const unsigned char *str = (const unsigned char *) string;
11060 int n = 0;
11061 ptrdiff_t dummy, nbytes;
11063 /* Copy at most PRECISION chars from STR. */
11064 nbytes = strlen (string);
11065 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11066 while (nbytes--)
11067 store_mode_line_noprop_char (*str++);
11069 /* Fill up with spaces until FIELD_WIDTH reached. */
11070 while (field_width > 0
11071 && n < field_width)
11073 store_mode_line_noprop_char (' ');
11074 ++n;
11077 return n;
11080 /***********************************************************************
11081 Frame Titles
11082 ***********************************************************************/
11084 #ifdef HAVE_WINDOW_SYSTEM
11086 /* Set the title of FRAME, if it has changed. The title format is
11087 Vicon_title_format if FRAME is iconified, otherwise it is
11088 frame_title_format. */
11090 static void
11091 x_consider_frame_title (Lisp_Object frame)
11093 struct frame *f = XFRAME (frame);
11095 if (FRAME_WINDOW_P (f)
11096 || FRAME_MINIBUF_ONLY_P (f)
11097 || f->explicit_name)
11099 /* Do we have more than one visible frame on this X display? */
11100 Lisp_Object tail;
11101 Lisp_Object fmt;
11102 ptrdiff_t title_start;
11103 char *title;
11104 ptrdiff_t len;
11105 struct it it;
11106 ptrdiff_t count = SPECPDL_INDEX ();
11108 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
11110 Lisp_Object other_frame = XCAR (tail);
11111 struct frame *tf = XFRAME (other_frame);
11113 if (tf != f
11114 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11115 && !FRAME_MINIBUF_ONLY_P (tf)
11116 && !EQ (other_frame, tip_frame)
11117 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11118 break;
11121 /* Set global variable indicating that multiple frames exist. */
11122 multiple_frames = CONSP (tail);
11124 /* Switch to the buffer of selected window of the frame. Set up
11125 mode_line_target so that display_mode_element will output into
11126 mode_line_noprop_buf; then display the title. */
11127 record_unwind_protect (unwind_format_mode_line,
11128 format_mode_line_unwind_data
11129 (f, current_buffer, selected_window, 0));
11131 Fselect_window (f->selected_window, Qt);
11132 set_buffer_internal_1
11133 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11134 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11136 mode_line_target = MODE_LINE_TITLE;
11137 title_start = MODE_LINE_NOPROP_LEN (0);
11138 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11139 NULL, DEFAULT_FACE_ID);
11140 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11141 len = MODE_LINE_NOPROP_LEN (title_start);
11142 title = mode_line_noprop_buf + title_start;
11143 unbind_to (count, Qnil);
11145 /* Set the title only if it's changed. This avoids consing in
11146 the common case where it hasn't. (If it turns out that we've
11147 already wasted too much time by walking through the list with
11148 display_mode_element, then we might need to optimize at a
11149 higher level than this.) */
11150 if (! STRINGP (f->name)
11151 || SBYTES (f->name) != len
11152 || memcmp (title, SDATA (f->name), len) != 0)
11153 x_implicitly_set_name (f, make_string (title, len), Qnil);
11157 #endif /* not HAVE_WINDOW_SYSTEM */
11160 /***********************************************************************
11161 Menu Bars
11162 ***********************************************************************/
11165 /* Prepare for redisplay by updating menu-bar item lists when
11166 appropriate. This can call eval. */
11168 void
11169 prepare_menu_bars (void)
11171 int all_windows;
11172 struct gcpro gcpro1, gcpro2;
11173 struct frame *f;
11174 Lisp_Object tooltip_frame;
11176 #ifdef HAVE_WINDOW_SYSTEM
11177 tooltip_frame = tip_frame;
11178 #else
11179 tooltip_frame = Qnil;
11180 #endif
11182 /* Update all frame titles based on their buffer names, etc. We do
11183 this before the menu bars so that the buffer-menu will show the
11184 up-to-date frame titles. */
11185 #ifdef HAVE_WINDOW_SYSTEM
11186 if (windows_or_buffers_changed || update_mode_lines)
11188 Lisp_Object tail, frame;
11190 FOR_EACH_FRAME (tail, frame)
11192 f = XFRAME (frame);
11193 if (!EQ (frame, tooltip_frame)
11194 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11195 x_consider_frame_title (frame);
11198 #endif /* HAVE_WINDOW_SYSTEM */
11200 /* Update the menu bar item lists, if appropriate. This has to be
11201 done before any actual redisplay or generation of display lines. */
11202 all_windows = (update_mode_lines
11203 || buffer_shared > 1
11204 || windows_or_buffers_changed);
11205 if (all_windows)
11207 Lisp_Object tail, frame;
11208 ptrdiff_t count = SPECPDL_INDEX ();
11209 /* 1 means that update_menu_bar has run its hooks
11210 so any further calls to update_menu_bar shouldn't do so again. */
11211 int menu_bar_hooks_run = 0;
11213 record_unwind_save_match_data ();
11215 FOR_EACH_FRAME (tail, frame)
11217 f = XFRAME (frame);
11219 /* Ignore tooltip frame. */
11220 if (EQ (frame, tooltip_frame))
11221 continue;
11223 /* If a window on this frame changed size, report that to
11224 the user and clear the size-change flag. */
11225 if (FRAME_WINDOW_SIZES_CHANGED (f))
11227 Lisp_Object functions;
11229 /* Clear flag first in case we get an error below. */
11230 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11231 functions = Vwindow_size_change_functions;
11232 GCPRO2 (tail, functions);
11234 while (CONSP (functions))
11236 if (!EQ (XCAR (functions), Qt))
11237 call1 (XCAR (functions), frame);
11238 functions = XCDR (functions);
11240 UNGCPRO;
11243 GCPRO1 (tail);
11244 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11245 #ifdef HAVE_WINDOW_SYSTEM
11246 update_tool_bar (f, 0);
11247 #endif
11248 #ifdef HAVE_NS
11249 if (windows_or_buffers_changed
11250 && FRAME_NS_P (f))
11251 ns_set_doc_edited
11252 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11253 #endif
11254 UNGCPRO;
11257 unbind_to (count, Qnil);
11259 else
11261 struct frame *sf = SELECTED_FRAME ();
11262 update_menu_bar (sf, 1, 0);
11263 #ifdef HAVE_WINDOW_SYSTEM
11264 update_tool_bar (sf, 1);
11265 #endif
11270 /* Update the menu bar item list for frame F. This has to be done
11271 before we start to fill in any display lines, because it can call
11272 eval.
11274 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11276 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11277 already ran the menu bar hooks for this redisplay, so there
11278 is no need to run them again. The return value is the
11279 updated value of this flag, to pass to the next call. */
11281 static int
11282 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11284 Lisp_Object window;
11285 register struct window *w;
11287 /* If called recursively during a menu update, do nothing. This can
11288 happen when, for instance, an activate-menubar-hook causes a
11289 redisplay. */
11290 if (inhibit_menubar_update)
11291 return hooks_run;
11293 window = FRAME_SELECTED_WINDOW (f);
11294 w = XWINDOW (window);
11296 if (FRAME_WINDOW_P (f)
11298 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11299 || defined (HAVE_NS) || defined (USE_GTK)
11300 FRAME_EXTERNAL_MENU_BAR (f)
11301 #else
11302 FRAME_MENU_BAR_LINES (f) > 0
11303 #endif
11304 : FRAME_MENU_BAR_LINES (f) > 0)
11306 /* If the user has switched buffers or windows, we need to
11307 recompute to reflect the new bindings. But we'll
11308 recompute when update_mode_lines is set too; that means
11309 that people can use force-mode-line-update to request
11310 that the menu bar be recomputed. The adverse effect on
11311 the rest of the redisplay algorithm is about the same as
11312 windows_or_buffers_changed anyway. */
11313 if (windows_or_buffers_changed
11314 /* This used to test w->update_mode_line, but we believe
11315 there is no need to recompute the menu in that case. */
11316 || update_mode_lines
11317 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11318 < BUF_MODIFF (XBUFFER (w->buffer)))
11319 != w->last_had_star)
11320 || ((!NILP (Vtransient_mark_mode)
11321 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11322 != !NILP (w->region_showing)))
11324 struct buffer *prev = current_buffer;
11325 ptrdiff_t count = SPECPDL_INDEX ();
11327 specbind (Qinhibit_menubar_update, Qt);
11329 set_buffer_internal_1 (XBUFFER (w->buffer));
11330 if (save_match_data)
11331 record_unwind_save_match_data ();
11332 if (NILP (Voverriding_local_map_menu_flag))
11334 specbind (Qoverriding_terminal_local_map, Qnil);
11335 specbind (Qoverriding_local_map, Qnil);
11338 if (!hooks_run)
11340 /* Run the Lucid hook. */
11341 safe_run_hooks (Qactivate_menubar_hook);
11343 /* If it has changed current-menubar from previous value,
11344 really recompute the menu-bar from the value. */
11345 if (! NILP (Vlucid_menu_bar_dirty_flag))
11346 call0 (Qrecompute_lucid_menubar);
11348 safe_run_hooks (Qmenu_bar_update_hook);
11350 hooks_run = 1;
11353 XSETFRAME (Vmenu_updating_frame, f);
11354 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11356 /* Redisplay the menu bar in case we changed it. */
11357 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11358 || defined (HAVE_NS) || defined (USE_GTK)
11359 if (FRAME_WINDOW_P (f))
11361 #if defined (HAVE_NS)
11362 /* All frames on Mac OS share the same menubar. So only
11363 the selected frame should be allowed to set it. */
11364 if (f == SELECTED_FRAME ())
11365 #endif
11366 set_frame_menubar (f, 0, 0);
11368 else
11369 /* On a terminal screen, the menu bar is an ordinary screen
11370 line, and this makes it get updated. */
11371 w->update_mode_line = 1;
11372 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11373 /* In the non-toolkit version, the menu bar is an ordinary screen
11374 line, and this makes it get updated. */
11375 w->update_mode_line = 1;
11376 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11378 unbind_to (count, Qnil);
11379 set_buffer_internal_1 (prev);
11383 return hooks_run;
11388 /***********************************************************************
11389 Output Cursor
11390 ***********************************************************************/
11392 #ifdef HAVE_WINDOW_SYSTEM
11394 /* EXPORT:
11395 Nominal cursor position -- where to draw output.
11396 HPOS and VPOS are window relative glyph matrix coordinates.
11397 X and Y are window relative pixel coordinates. */
11399 struct cursor_pos output_cursor;
11402 /* EXPORT:
11403 Set the global variable output_cursor to CURSOR. All cursor
11404 positions are relative to updated_window. */
11406 void
11407 set_output_cursor (struct cursor_pos *cursor)
11409 output_cursor.hpos = cursor->hpos;
11410 output_cursor.vpos = cursor->vpos;
11411 output_cursor.x = cursor->x;
11412 output_cursor.y = cursor->y;
11416 /* EXPORT for RIF:
11417 Set a nominal cursor position.
11419 HPOS and VPOS are column/row positions in a window glyph matrix. X
11420 and Y are window text area relative pixel positions.
11422 If this is done during an update, updated_window will contain the
11423 window that is being updated and the position is the future output
11424 cursor position for that window. If updated_window is null, use
11425 selected_window and display the cursor at the given position. */
11427 void
11428 x_cursor_to (int vpos, int hpos, int y, int x)
11430 struct window *w;
11432 /* If updated_window is not set, work on selected_window. */
11433 if (updated_window)
11434 w = updated_window;
11435 else
11436 w = XWINDOW (selected_window);
11438 /* Set the output cursor. */
11439 output_cursor.hpos = hpos;
11440 output_cursor.vpos = vpos;
11441 output_cursor.x = x;
11442 output_cursor.y = y;
11444 /* If not called as part of an update, really display the cursor.
11445 This will also set the cursor position of W. */
11446 if (updated_window == NULL)
11448 block_input ();
11449 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11450 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11451 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11452 unblock_input ();
11456 #endif /* HAVE_WINDOW_SYSTEM */
11459 /***********************************************************************
11460 Tool-bars
11461 ***********************************************************************/
11463 #ifdef HAVE_WINDOW_SYSTEM
11465 /* Where the mouse was last time we reported a mouse event. */
11467 FRAME_PTR last_mouse_frame;
11469 /* Tool-bar item index of the item on which a mouse button was pressed
11470 or -1. */
11472 int last_tool_bar_item;
11475 static Lisp_Object
11476 update_tool_bar_unwind (Lisp_Object frame)
11478 selected_frame = frame;
11479 return Qnil;
11482 /* Update the tool-bar item list for frame F. This has to be done
11483 before we start to fill in any display lines. Called from
11484 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11485 and restore it here. */
11487 static void
11488 update_tool_bar (struct frame *f, int save_match_data)
11490 #if defined (USE_GTK) || defined (HAVE_NS)
11491 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11492 #else
11493 int do_update = WINDOWP (f->tool_bar_window)
11494 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11495 #endif
11497 if (do_update)
11499 Lisp_Object window;
11500 struct window *w;
11502 window = FRAME_SELECTED_WINDOW (f);
11503 w = XWINDOW (window);
11505 /* If the user has switched buffers or windows, we need to
11506 recompute to reflect the new bindings. But we'll
11507 recompute when update_mode_lines is set too; that means
11508 that people can use force-mode-line-update to request
11509 that the menu bar be recomputed. The adverse effect on
11510 the rest of the redisplay algorithm is about the same as
11511 windows_or_buffers_changed anyway. */
11512 if (windows_or_buffers_changed
11513 || w->update_mode_line
11514 || update_mode_lines
11515 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11516 < BUF_MODIFF (XBUFFER (w->buffer)))
11517 != w->last_had_star)
11518 || ((!NILP (Vtransient_mark_mode)
11519 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11520 != !NILP (w->region_showing)))
11522 struct buffer *prev = current_buffer;
11523 ptrdiff_t count = SPECPDL_INDEX ();
11524 Lisp_Object frame, new_tool_bar;
11525 int new_n_tool_bar;
11526 struct gcpro gcpro1;
11528 /* Set current_buffer to the buffer of the selected
11529 window of the frame, so that we get the right local
11530 keymaps. */
11531 set_buffer_internal_1 (XBUFFER (w->buffer));
11533 /* Save match data, if we must. */
11534 if (save_match_data)
11535 record_unwind_save_match_data ();
11537 /* Make sure that we don't accidentally use bogus keymaps. */
11538 if (NILP (Voverriding_local_map_menu_flag))
11540 specbind (Qoverriding_terminal_local_map, Qnil);
11541 specbind (Qoverriding_local_map, Qnil);
11544 GCPRO1 (new_tool_bar);
11546 /* We must temporarily set the selected frame to this frame
11547 before calling tool_bar_items, because the calculation of
11548 the tool-bar keymap uses the selected frame (see
11549 `tool-bar-make-keymap' in tool-bar.el). */
11550 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11551 XSETFRAME (frame, f);
11552 selected_frame = frame;
11554 /* Build desired tool-bar items from keymaps. */
11555 new_tool_bar
11556 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11557 &new_n_tool_bar);
11559 /* Redisplay the tool-bar if we changed it. */
11560 if (new_n_tool_bar != f->n_tool_bar_items
11561 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11563 /* Redisplay that happens asynchronously due to an expose event
11564 may access f->tool_bar_items. Make sure we update both
11565 variables within BLOCK_INPUT so no such event interrupts. */
11566 block_input ();
11567 fset_tool_bar_items (f, new_tool_bar);
11568 f->n_tool_bar_items = new_n_tool_bar;
11569 w->update_mode_line = 1;
11570 unblock_input ();
11573 UNGCPRO;
11575 unbind_to (count, Qnil);
11576 set_buffer_internal_1 (prev);
11582 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11583 F's desired tool-bar contents. F->tool_bar_items must have
11584 been set up previously by calling prepare_menu_bars. */
11586 static void
11587 build_desired_tool_bar_string (struct frame *f)
11589 int i, size, size_needed;
11590 struct gcpro gcpro1, gcpro2, gcpro3;
11591 Lisp_Object image, plist, props;
11593 image = plist = props = Qnil;
11594 GCPRO3 (image, plist, props);
11596 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11597 Otherwise, make a new string. */
11599 /* The size of the string we might be able to reuse. */
11600 size = (STRINGP (f->desired_tool_bar_string)
11601 ? SCHARS (f->desired_tool_bar_string)
11602 : 0);
11604 /* We need one space in the string for each image. */
11605 size_needed = f->n_tool_bar_items;
11607 /* Reuse f->desired_tool_bar_string, if possible. */
11608 if (size < size_needed || NILP (f->desired_tool_bar_string))
11609 fset_desired_tool_bar_string
11610 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11611 else
11613 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11614 Fremove_text_properties (make_number (0), make_number (size),
11615 props, f->desired_tool_bar_string);
11618 /* Put a `display' property on the string for the images to display,
11619 put a `menu_item' property on tool-bar items with a value that
11620 is the index of the item in F's tool-bar item vector. */
11621 for (i = 0; i < f->n_tool_bar_items; ++i)
11623 #define PROP(IDX) \
11624 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11626 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11627 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11628 int hmargin, vmargin, relief, idx, end;
11630 /* If image is a vector, choose the image according to the
11631 button state. */
11632 image = PROP (TOOL_BAR_ITEM_IMAGES);
11633 if (VECTORP (image))
11635 if (enabled_p)
11636 idx = (selected_p
11637 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11638 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11639 else
11640 idx = (selected_p
11641 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11642 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11644 eassert (ASIZE (image) >= idx);
11645 image = AREF (image, idx);
11647 else
11648 idx = -1;
11650 /* Ignore invalid image specifications. */
11651 if (!valid_image_p (image))
11652 continue;
11654 /* Display the tool-bar button pressed, or depressed. */
11655 plist = Fcopy_sequence (XCDR (image));
11657 /* Compute margin and relief to draw. */
11658 relief = (tool_bar_button_relief >= 0
11659 ? tool_bar_button_relief
11660 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11661 hmargin = vmargin = relief;
11663 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11664 INT_MAX - max (hmargin, vmargin)))
11666 hmargin += XFASTINT (Vtool_bar_button_margin);
11667 vmargin += XFASTINT (Vtool_bar_button_margin);
11669 else if (CONSP (Vtool_bar_button_margin))
11671 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11672 INT_MAX - hmargin))
11673 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11675 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11676 INT_MAX - vmargin))
11677 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11680 if (auto_raise_tool_bar_buttons_p)
11682 /* Add a `:relief' property to the image spec if the item is
11683 selected. */
11684 if (selected_p)
11686 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11687 hmargin -= relief;
11688 vmargin -= relief;
11691 else
11693 /* If image is selected, display it pressed, i.e. with a
11694 negative relief. If it's not selected, display it with a
11695 raised relief. */
11696 plist = Fplist_put (plist, QCrelief,
11697 (selected_p
11698 ? make_number (-relief)
11699 : make_number (relief)));
11700 hmargin -= relief;
11701 vmargin -= relief;
11704 /* Put a margin around the image. */
11705 if (hmargin || vmargin)
11707 if (hmargin == vmargin)
11708 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11709 else
11710 plist = Fplist_put (plist, QCmargin,
11711 Fcons (make_number (hmargin),
11712 make_number (vmargin)));
11715 /* If button is not enabled, and we don't have special images
11716 for the disabled state, make the image appear disabled by
11717 applying an appropriate algorithm to it. */
11718 if (!enabled_p && idx < 0)
11719 plist = Fplist_put (plist, QCconversion, Qdisabled);
11721 /* Put a `display' text property on the string for the image to
11722 display. Put a `menu-item' property on the string that gives
11723 the start of this item's properties in the tool-bar items
11724 vector. */
11725 image = Fcons (Qimage, plist);
11726 props = list4 (Qdisplay, image,
11727 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11729 /* Let the last image hide all remaining spaces in the tool bar
11730 string. The string can be longer than needed when we reuse a
11731 previous string. */
11732 if (i + 1 == f->n_tool_bar_items)
11733 end = SCHARS (f->desired_tool_bar_string);
11734 else
11735 end = i + 1;
11736 Fadd_text_properties (make_number (i), make_number (end),
11737 props, f->desired_tool_bar_string);
11738 #undef PROP
11741 UNGCPRO;
11745 /* Display one line of the tool-bar of frame IT->f.
11747 HEIGHT specifies the desired height of the tool-bar line.
11748 If the actual height of the glyph row is less than HEIGHT, the
11749 row's height is increased to HEIGHT, and the icons are centered
11750 vertically in the new height.
11752 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11753 count a final empty row in case the tool-bar width exactly matches
11754 the window width.
11757 static void
11758 display_tool_bar_line (struct it *it, int height)
11760 struct glyph_row *row = it->glyph_row;
11761 int max_x = it->last_visible_x;
11762 struct glyph *last;
11764 prepare_desired_row (row);
11765 row->y = it->current_y;
11767 /* Note that this isn't made use of if the face hasn't a box,
11768 so there's no need to check the face here. */
11769 it->start_of_box_run_p = 1;
11771 while (it->current_x < max_x)
11773 int x, n_glyphs_before, i, nglyphs;
11774 struct it it_before;
11776 /* Get the next display element. */
11777 if (!get_next_display_element (it))
11779 /* Don't count empty row if we are counting needed tool-bar lines. */
11780 if (height < 0 && !it->hpos)
11781 return;
11782 break;
11785 /* Produce glyphs. */
11786 n_glyphs_before = row->used[TEXT_AREA];
11787 it_before = *it;
11789 PRODUCE_GLYPHS (it);
11791 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11792 i = 0;
11793 x = it_before.current_x;
11794 while (i < nglyphs)
11796 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11798 if (x + glyph->pixel_width > max_x)
11800 /* Glyph doesn't fit on line. Backtrack. */
11801 row->used[TEXT_AREA] = n_glyphs_before;
11802 *it = it_before;
11803 /* If this is the only glyph on this line, it will never fit on the
11804 tool-bar, so skip it. But ensure there is at least one glyph,
11805 so we don't accidentally disable the tool-bar. */
11806 if (n_glyphs_before == 0
11807 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11808 break;
11809 goto out;
11812 ++it->hpos;
11813 x += glyph->pixel_width;
11814 ++i;
11817 /* Stop at line end. */
11818 if (ITERATOR_AT_END_OF_LINE_P (it))
11819 break;
11821 set_iterator_to_next (it, 1);
11824 out:;
11826 row->displays_text_p = row->used[TEXT_AREA] != 0;
11828 /* Use default face for the border below the tool bar.
11830 FIXME: When auto-resize-tool-bars is grow-only, there is
11831 no additional border below the possibly empty tool-bar lines.
11832 So to make the extra empty lines look "normal", we have to
11833 use the tool-bar face for the border too. */
11834 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11835 it->face_id = DEFAULT_FACE_ID;
11837 extend_face_to_end_of_line (it);
11838 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11839 last->right_box_line_p = 1;
11840 if (last == row->glyphs[TEXT_AREA])
11841 last->left_box_line_p = 1;
11843 /* Make line the desired height and center it vertically. */
11844 if ((height -= it->max_ascent + it->max_descent) > 0)
11846 /* Don't add more than one line height. */
11847 height %= FRAME_LINE_HEIGHT (it->f);
11848 it->max_ascent += height / 2;
11849 it->max_descent += (height + 1) / 2;
11852 compute_line_metrics (it);
11854 /* If line is empty, make it occupy the rest of the tool-bar. */
11855 if (!row->displays_text_p)
11857 row->height = row->phys_height = it->last_visible_y - row->y;
11858 row->visible_height = row->height;
11859 row->ascent = row->phys_ascent = 0;
11860 row->extra_line_spacing = 0;
11863 row->full_width_p = 1;
11864 row->continued_p = 0;
11865 row->truncated_on_left_p = 0;
11866 row->truncated_on_right_p = 0;
11868 it->current_x = it->hpos = 0;
11869 it->current_y += row->height;
11870 ++it->vpos;
11871 ++it->glyph_row;
11875 /* Max tool-bar height. */
11877 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11878 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11880 /* Value is the number of screen lines needed to make all tool-bar
11881 items of frame F visible. The number of actual rows needed is
11882 returned in *N_ROWS if non-NULL. */
11884 static int
11885 tool_bar_lines_needed (struct frame *f, int *n_rows)
11887 struct window *w = XWINDOW (f->tool_bar_window);
11888 struct it it;
11889 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11890 the desired matrix, so use (unused) mode-line row as temporary row to
11891 avoid destroying the first tool-bar row. */
11892 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11894 /* Initialize an iterator for iteration over
11895 F->desired_tool_bar_string in the tool-bar window of frame F. */
11896 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11897 it.first_visible_x = 0;
11898 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11899 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11900 it.paragraph_embedding = L2R;
11902 while (!ITERATOR_AT_END_P (&it))
11904 clear_glyph_row (temp_row);
11905 it.glyph_row = temp_row;
11906 display_tool_bar_line (&it, -1);
11908 clear_glyph_row (temp_row);
11910 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11911 if (n_rows)
11912 *n_rows = it.vpos > 0 ? it.vpos : -1;
11914 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11918 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11919 0, 1, 0,
11920 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11921 (Lisp_Object frame)
11923 struct frame *f;
11924 struct window *w;
11925 int nlines = 0;
11927 if (NILP (frame))
11928 frame = selected_frame;
11929 else
11930 CHECK_FRAME (frame);
11931 f = XFRAME (frame);
11933 if (WINDOWP (f->tool_bar_window)
11934 && (w = XWINDOW (f->tool_bar_window),
11935 WINDOW_TOTAL_LINES (w) > 0))
11937 update_tool_bar (f, 1);
11938 if (f->n_tool_bar_items)
11940 build_desired_tool_bar_string (f);
11941 nlines = tool_bar_lines_needed (f, NULL);
11945 return make_number (nlines);
11949 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11950 height should be changed. */
11952 static int
11953 redisplay_tool_bar (struct frame *f)
11955 struct window *w;
11956 struct it it;
11957 struct glyph_row *row;
11959 #if defined (USE_GTK) || defined (HAVE_NS)
11960 if (FRAME_EXTERNAL_TOOL_BAR (f))
11961 update_frame_tool_bar (f);
11962 return 0;
11963 #endif
11965 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11966 do anything. This means you must start with tool-bar-lines
11967 non-zero to get the auto-sizing effect. Or in other words, you
11968 can turn off tool-bars by specifying tool-bar-lines zero. */
11969 if (!WINDOWP (f->tool_bar_window)
11970 || (w = XWINDOW (f->tool_bar_window),
11971 WINDOW_TOTAL_LINES (w) == 0))
11972 return 0;
11974 /* Set up an iterator for the tool-bar window. */
11975 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11976 it.first_visible_x = 0;
11977 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11978 row = it.glyph_row;
11980 /* Build a string that represents the contents of the tool-bar. */
11981 build_desired_tool_bar_string (f);
11982 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11983 /* FIXME: This should be controlled by a user option. But it
11984 doesn't make sense to have an R2L tool bar if the menu bar cannot
11985 be drawn also R2L, and making the menu bar R2L is tricky due
11986 toolkit-specific code that implements it. If an R2L tool bar is
11987 ever supported, display_tool_bar_line should also be augmented to
11988 call unproduce_glyphs like display_line and display_string
11989 do. */
11990 it.paragraph_embedding = L2R;
11992 if (f->n_tool_bar_rows == 0)
11994 int nlines;
11996 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11997 nlines != WINDOW_TOTAL_LINES (w)))
11999 Lisp_Object frame;
12000 int old_height = WINDOW_TOTAL_LINES (w);
12002 XSETFRAME (frame, f);
12003 Fmodify_frame_parameters (frame,
12004 Fcons (Fcons (Qtool_bar_lines,
12005 make_number (nlines)),
12006 Qnil));
12007 if (WINDOW_TOTAL_LINES (w) != old_height)
12009 clear_glyph_matrix (w->desired_matrix);
12010 fonts_changed_p = 1;
12011 return 1;
12016 /* Display as many lines as needed to display all tool-bar items. */
12018 if (f->n_tool_bar_rows > 0)
12020 int border, rows, height, extra;
12022 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12023 border = XINT (Vtool_bar_border);
12024 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12025 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12026 else if (EQ (Vtool_bar_border, Qborder_width))
12027 border = f->border_width;
12028 else
12029 border = 0;
12030 if (border < 0)
12031 border = 0;
12033 rows = f->n_tool_bar_rows;
12034 height = max (1, (it.last_visible_y - border) / rows);
12035 extra = it.last_visible_y - border - height * rows;
12037 while (it.current_y < it.last_visible_y)
12039 int h = 0;
12040 if (extra > 0 && rows-- > 0)
12042 h = (extra + rows - 1) / rows;
12043 extra -= h;
12045 display_tool_bar_line (&it, height + h);
12048 else
12050 while (it.current_y < it.last_visible_y)
12051 display_tool_bar_line (&it, 0);
12054 /* It doesn't make much sense to try scrolling in the tool-bar
12055 window, so don't do it. */
12056 w->desired_matrix->no_scrolling_p = 1;
12057 w->must_be_updated_p = 1;
12059 if (!NILP (Vauto_resize_tool_bars))
12061 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12062 int change_height_p = 0;
12064 /* If we couldn't display everything, change the tool-bar's
12065 height if there is room for more. */
12066 if (IT_STRING_CHARPOS (it) < it.end_charpos
12067 && it.current_y < max_tool_bar_height)
12068 change_height_p = 1;
12070 row = it.glyph_row - 1;
12072 /* If there are blank lines at the end, except for a partially
12073 visible blank line at the end that is smaller than
12074 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12075 if (!row->displays_text_p
12076 && row->height >= FRAME_LINE_HEIGHT (f))
12077 change_height_p = 1;
12079 /* If row displays tool-bar items, but is partially visible,
12080 change the tool-bar's height. */
12081 if (row->displays_text_p
12082 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12083 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12084 change_height_p = 1;
12086 /* Resize windows as needed by changing the `tool-bar-lines'
12087 frame parameter. */
12088 if (change_height_p)
12090 Lisp_Object frame;
12091 int old_height = WINDOW_TOTAL_LINES (w);
12092 int nrows;
12093 int nlines = tool_bar_lines_needed (f, &nrows);
12095 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12096 && !f->minimize_tool_bar_window_p)
12097 ? (nlines > old_height)
12098 : (nlines != old_height));
12099 f->minimize_tool_bar_window_p = 0;
12101 if (change_height_p)
12103 XSETFRAME (frame, f);
12104 Fmodify_frame_parameters (frame,
12105 Fcons (Fcons (Qtool_bar_lines,
12106 make_number (nlines)),
12107 Qnil));
12108 if (WINDOW_TOTAL_LINES (w) != old_height)
12110 clear_glyph_matrix (w->desired_matrix);
12111 f->n_tool_bar_rows = nrows;
12112 fonts_changed_p = 1;
12113 return 1;
12119 f->minimize_tool_bar_window_p = 0;
12120 return 0;
12124 /* Get information about the tool-bar item which is displayed in GLYPH
12125 on frame F. Return in *PROP_IDX the index where tool-bar item
12126 properties start in F->tool_bar_items. Value is zero if
12127 GLYPH doesn't display a tool-bar item. */
12129 static int
12130 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12132 Lisp_Object prop;
12133 int success_p;
12134 int charpos;
12136 /* This function can be called asynchronously, which means we must
12137 exclude any possibility that Fget_text_property signals an
12138 error. */
12139 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12140 charpos = max (0, charpos);
12142 /* Get the text property `menu-item' at pos. The value of that
12143 property is the start index of this item's properties in
12144 F->tool_bar_items. */
12145 prop = Fget_text_property (make_number (charpos),
12146 Qmenu_item, f->current_tool_bar_string);
12147 if (INTEGERP (prop))
12149 *prop_idx = XINT (prop);
12150 success_p = 1;
12152 else
12153 success_p = 0;
12155 return success_p;
12159 /* Get information about the tool-bar item at position X/Y on frame F.
12160 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12161 the current matrix of the tool-bar window of F, or NULL if not
12162 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12163 item in F->tool_bar_items. Value is
12165 -1 if X/Y is not on a tool-bar item
12166 0 if X/Y is on the same item that was highlighted before.
12167 1 otherwise. */
12169 static int
12170 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12171 int *hpos, int *vpos, int *prop_idx)
12173 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12174 struct window *w = XWINDOW (f->tool_bar_window);
12175 int area;
12177 /* Find the glyph under X/Y. */
12178 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12179 if (*glyph == NULL)
12180 return -1;
12182 /* Get the start of this tool-bar item's properties in
12183 f->tool_bar_items. */
12184 if (!tool_bar_item_info (f, *glyph, prop_idx))
12185 return -1;
12187 /* Is mouse on the highlighted item? */
12188 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12189 && *vpos >= hlinfo->mouse_face_beg_row
12190 && *vpos <= hlinfo->mouse_face_end_row
12191 && (*vpos > hlinfo->mouse_face_beg_row
12192 || *hpos >= hlinfo->mouse_face_beg_col)
12193 && (*vpos < hlinfo->mouse_face_end_row
12194 || *hpos < hlinfo->mouse_face_end_col
12195 || hlinfo->mouse_face_past_end))
12196 return 0;
12198 return 1;
12202 /* EXPORT:
12203 Handle mouse button event on the tool-bar of frame F, at
12204 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12205 0 for button release. MODIFIERS is event modifiers for button
12206 release. */
12208 void
12209 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12210 int modifiers)
12212 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12213 struct window *w = XWINDOW (f->tool_bar_window);
12214 int hpos, vpos, prop_idx;
12215 struct glyph *glyph;
12216 Lisp_Object enabled_p;
12218 /* If not on the highlighted tool-bar item, return. */
12219 frame_to_window_pixel_xy (w, &x, &y);
12220 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12221 return;
12223 /* If item is disabled, do nothing. */
12224 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12225 if (NILP (enabled_p))
12226 return;
12228 if (down_p)
12230 /* Show item in pressed state. */
12231 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12232 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12233 last_tool_bar_item = prop_idx;
12235 else
12237 Lisp_Object key, frame;
12238 struct input_event event;
12239 EVENT_INIT (event);
12241 /* Show item in released state. */
12242 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12243 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12245 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12247 XSETFRAME (frame, f);
12248 event.kind = TOOL_BAR_EVENT;
12249 event.frame_or_window = frame;
12250 event.arg = frame;
12251 kbd_buffer_store_event (&event);
12253 event.kind = TOOL_BAR_EVENT;
12254 event.frame_or_window = frame;
12255 event.arg = key;
12256 event.modifiers = modifiers;
12257 kbd_buffer_store_event (&event);
12258 last_tool_bar_item = -1;
12263 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12264 tool-bar window-relative coordinates X/Y. Called from
12265 note_mouse_highlight. */
12267 static void
12268 note_tool_bar_highlight (struct frame *f, int x, int y)
12270 Lisp_Object window = f->tool_bar_window;
12271 struct window *w = XWINDOW (window);
12272 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12273 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12274 int hpos, vpos;
12275 struct glyph *glyph;
12276 struct glyph_row *row;
12277 int i;
12278 Lisp_Object enabled_p;
12279 int prop_idx;
12280 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12281 int mouse_down_p, rc;
12283 /* Function note_mouse_highlight is called with negative X/Y
12284 values when mouse moves outside of the frame. */
12285 if (x <= 0 || y <= 0)
12287 clear_mouse_face (hlinfo);
12288 return;
12291 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12292 if (rc < 0)
12294 /* Not on tool-bar item. */
12295 clear_mouse_face (hlinfo);
12296 return;
12298 else if (rc == 0)
12299 /* On same tool-bar item as before. */
12300 goto set_help_echo;
12302 clear_mouse_face (hlinfo);
12304 /* Mouse is down, but on different tool-bar item? */
12305 mouse_down_p = (dpyinfo->grabbed
12306 && f == last_mouse_frame
12307 && FRAME_LIVE_P (f));
12308 if (mouse_down_p
12309 && last_tool_bar_item != prop_idx)
12310 return;
12312 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12313 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12315 /* If tool-bar item is not enabled, don't highlight it. */
12316 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12317 if (!NILP (enabled_p))
12319 /* Compute the x-position of the glyph. In front and past the
12320 image is a space. We include this in the highlighted area. */
12321 row = MATRIX_ROW (w->current_matrix, vpos);
12322 for (i = x = 0; i < hpos; ++i)
12323 x += row->glyphs[TEXT_AREA][i].pixel_width;
12325 /* Record this as the current active region. */
12326 hlinfo->mouse_face_beg_col = hpos;
12327 hlinfo->mouse_face_beg_row = vpos;
12328 hlinfo->mouse_face_beg_x = x;
12329 hlinfo->mouse_face_beg_y = row->y;
12330 hlinfo->mouse_face_past_end = 0;
12332 hlinfo->mouse_face_end_col = hpos + 1;
12333 hlinfo->mouse_face_end_row = vpos;
12334 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12335 hlinfo->mouse_face_end_y = row->y;
12336 hlinfo->mouse_face_window = window;
12337 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12339 /* Display it as active. */
12340 show_mouse_face (hlinfo, draw);
12341 hlinfo->mouse_face_image_state = draw;
12344 set_help_echo:
12346 /* Set help_echo_string to a help string to display for this tool-bar item.
12347 XTread_socket does the rest. */
12348 help_echo_object = help_echo_window = Qnil;
12349 help_echo_pos = -1;
12350 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12351 if (NILP (help_echo_string))
12352 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12355 #endif /* HAVE_WINDOW_SYSTEM */
12359 /************************************************************************
12360 Horizontal scrolling
12361 ************************************************************************/
12363 static int hscroll_window_tree (Lisp_Object);
12364 static int hscroll_windows (Lisp_Object);
12366 /* For all leaf windows in the window tree rooted at WINDOW, set their
12367 hscroll value so that PT is (i) visible in the window, and (ii) so
12368 that it is not within a certain margin at the window's left and
12369 right border. Value is non-zero if any window's hscroll has been
12370 changed. */
12372 static int
12373 hscroll_window_tree (Lisp_Object window)
12375 int hscrolled_p = 0;
12376 int hscroll_relative_p = FLOATP (Vhscroll_step);
12377 int hscroll_step_abs = 0;
12378 double hscroll_step_rel = 0;
12380 if (hscroll_relative_p)
12382 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12383 if (hscroll_step_rel < 0)
12385 hscroll_relative_p = 0;
12386 hscroll_step_abs = 0;
12389 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12391 hscroll_step_abs = XINT (Vhscroll_step);
12392 if (hscroll_step_abs < 0)
12393 hscroll_step_abs = 0;
12395 else
12396 hscroll_step_abs = 0;
12398 while (WINDOWP (window))
12400 struct window *w = XWINDOW (window);
12402 if (WINDOWP (w->hchild))
12403 hscrolled_p |= hscroll_window_tree (w->hchild);
12404 else if (WINDOWP (w->vchild))
12405 hscrolled_p |= hscroll_window_tree (w->vchild);
12406 else if (w->cursor.vpos >= 0)
12408 int h_margin;
12409 int text_area_width;
12410 struct glyph_row *current_cursor_row
12411 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12412 struct glyph_row *desired_cursor_row
12413 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12414 struct glyph_row *cursor_row
12415 = (desired_cursor_row->enabled_p
12416 ? desired_cursor_row
12417 : current_cursor_row);
12418 int row_r2l_p = cursor_row->reversed_p;
12420 text_area_width = window_box_width (w, TEXT_AREA);
12422 /* Scroll when cursor is inside this scroll margin. */
12423 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12425 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12426 /* For left-to-right rows, hscroll when cursor is either
12427 (i) inside the right hscroll margin, or (ii) if it is
12428 inside the left margin and the window is already
12429 hscrolled. */
12430 && ((!row_r2l_p
12431 && ((w->hscroll
12432 && w->cursor.x <= h_margin)
12433 || (cursor_row->enabled_p
12434 && cursor_row->truncated_on_right_p
12435 && (w->cursor.x >= text_area_width - h_margin))))
12436 /* For right-to-left rows, the logic is similar,
12437 except that rules for scrolling to left and right
12438 are reversed. E.g., if cursor.x <= h_margin, we
12439 need to hscroll "to the right" unconditionally,
12440 and that will scroll the screen to the left so as
12441 to reveal the next portion of the row. */
12442 || (row_r2l_p
12443 && ((cursor_row->enabled_p
12444 /* FIXME: It is confusing to set the
12445 truncated_on_right_p flag when R2L rows
12446 are actually truncated on the left. */
12447 && cursor_row->truncated_on_right_p
12448 && w->cursor.x <= h_margin)
12449 || (w->hscroll
12450 && (w->cursor.x >= text_area_width - h_margin))))))
12452 struct it it;
12453 ptrdiff_t hscroll;
12454 struct buffer *saved_current_buffer;
12455 ptrdiff_t pt;
12456 int wanted_x;
12458 /* Find point in a display of infinite width. */
12459 saved_current_buffer = current_buffer;
12460 current_buffer = XBUFFER (w->buffer);
12462 if (w == XWINDOW (selected_window))
12463 pt = PT;
12464 else
12466 pt = marker_position (w->pointm);
12467 pt = max (BEGV, pt);
12468 pt = min (ZV, pt);
12471 /* Move iterator to pt starting at cursor_row->start in
12472 a line with infinite width. */
12473 init_to_row_start (&it, w, cursor_row);
12474 it.last_visible_x = INFINITY;
12475 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12476 current_buffer = saved_current_buffer;
12478 /* Position cursor in window. */
12479 if (!hscroll_relative_p && hscroll_step_abs == 0)
12480 hscroll = max (0, (it.current_x
12481 - (ITERATOR_AT_END_OF_LINE_P (&it)
12482 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12483 : (text_area_width / 2))))
12484 / FRAME_COLUMN_WIDTH (it.f);
12485 else if ((!row_r2l_p
12486 && w->cursor.x >= text_area_width - h_margin)
12487 || (row_r2l_p && w->cursor.x <= h_margin))
12489 if (hscroll_relative_p)
12490 wanted_x = text_area_width * (1 - hscroll_step_rel)
12491 - h_margin;
12492 else
12493 wanted_x = text_area_width
12494 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12495 - h_margin;
12496 hscroll
12497 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12499 else
12501 if (hscroll_relative_p)
12502 wanted_x = text_area_width * hscroll_step_rel
12503 + h_margin;
12504 else
12505 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12506 + h_margin;
12507 hscroll
12508 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12510 hscroll = max (hscroll, w->min_hscroll);
12512 /* Don't prevent redisplay optimizations if hscroll
12513 hasn't changed, as it will unnecessarily slow down
12514 redisplay. */
12515 if (w->hscroll != hscroll)
12517 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12518 w->hscroll = hscroll;
12519 hscrolled_p = 1;
12524 window = w->next;
12527 /* Value is non-zero if hscroll of any leaf window has been changed. */
12528 return hscrolled_p;
12532 /* Set hscroll so that cursor is visible and not inside horizontal
12533 scroll margins for all windows in the tree rooted at WINDOW. See
12534 also hscroll_window_tree above. Value is non-zero if any window's
12535 hscroll has been changed. If it has, desired matrices on the frame
12536 of WINDOW are cleared. */
12538 static int
12539 hscroll_windows (Lisp_Object window)
12541 int hscrolled_p = hscroll_window_tree (window);
12542 if (hscrolled_p)
12543 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12544 return hscrolled_p;
12549 /************************************************************************
12550 Redisplay
12551 ************************************************************************/
12553 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12554 to a non-zero value. This is sometimes handy to have in a debugger
12555 session. */
12557 #ifdef GLYPH_DEBUG
12559 /* First and last unchanged row for try_window_id. */
12561 static int debug_first_unchanged_at_end_vpos;
12562 static int debug_last_unchanged_at_beg_vpos;
12564 /* Delta vpos and y. */
12566 static int debug_dvpos, debug_dy;
12568 /* Delta in characters and bytes for try_window_id. */
12570 static ptrdiff_t debug_delta, debug_delta_bytes;
12572 /* Values of window_end_pos and window_end_vpos at the end of
12573 try_window_id. */
12575 static ptrdiff_t debug_end_vpos;
12577 /* Append a string to W->desired_matrix->method. FMT is a printf
12578 format string. If trace_redisplay_p is non-zero also printf the
12579 resulting string to stderr. */
12581 static void debug_method_add (struct window *, char const *, ...)
12582 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12584 static void
12585 debug_method_add (struct window *w, char const *fmt, ...)
12587 char *method = w->desired_matrix->method;
12588 int len = strlen (method);
12589 int size = sizeof w->desired_matrix->method;
12590 int remaining = size - len - 1;
12591 va_list ap;
12593 if (len && remaining)
12595 method[len] = '|';
12596 --remaining, ++len;
12599 va_start (ap, fmt);
12600 vsnprintf (method + len, remaining + 1, fmt, ap);
12601 va_end (ap);
12603 if (trace_redisplay_p)
12604 fprintf (stderr, "%p (%s): %s\n",
12606 ((BUFFERP (w->buffer)
12607 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12608 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12609 : "no buffer"),
12610 method + len);
12613 #endif /* GLYPH_DEBUG */
12616 /* Value is non-zero if all changes in window W, which displays
12617 current_buffer, are in the text between START and END. START is a
12618 buffer position, END is given as a distance from Z. Used in
12619 redisplay_internal for display optimization. */
12621 static int
12622 text_outside_line_unchanged_p (struct window *w,
12623 ptrdiff_t start, ptrdiff_t end)
12625 int unchanged_p = 1;
12627 /* If text or overlays have changed, see where. */
12628 if (w->last_modified < MODIFF
12629 || w->last_overlay_modified < OVERLAY_MODIFF)
12631 /* Gap in the line? */
12632 if (GPT < start || Z - GPT < end)
12633 unchanged_p = 0;
12635 /* Changes start in front of the line, or end after it? */
12636 if (unchanged_p
12637 && (BEG_UNCHANGED < start - 1
12638 || END_UNCHANGED < end))
12639 unchanged_p = 0;
12641 /* If selective display, can't optimize if changes start at the
12642 beginning of the line. */
12643 if (unchanged_p
12644 && INTEGERP (BVAR (current_buffer, selective_display))
12645 && XINT (BVAR (current_buffer, selective_display)) > 0
12646 && (BEG_UNCHANGED < start || GPT <= start))
12647 unchanged_p = 0;
12649 /* If there are overlays at the start or end of the line, these
12650 may have overlay strings with newlines in them. A change at
12651 START, for instance, may actually concern the display of such
12652 overlay strings as well, and they are displayed on different
12653 lines. So, quickly rule out this case. (For the future, it
12654 might be desirable to implement something more telling than
12655 just BEG/END_UNCHANGED.) */
12656 if (unchanged_p)
12658 if (BEG + BEG_UNCHANGED == start
12659 && overlay_touches_p (start))
12660 unchanged_p = 0;
12661 if (END_UNCHANGED == end
12662 && overlay_touches_p (Z - end))
12663 unchanged_p = 0;
12666 /* Under bidi reordering, adding or deleting a character in the
12667 beginning of a paragraph, before the first strong directional
12668 character, can change the base direction of the paragraph (unless
12669 the buffer specifies a fixed paragraph direction), which will
12670 require to redisplay the whole paragraph. It might be worthwhile
12671 to find the paragraph limits and widen the range of redisplayed
12672 lines to that, but for now just give up this optimization. */
12673 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12674 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12675 unchanged_p = 0;
12678 return unchanged_p;
12682 /* Do a frame update, taking possible shortcuts into account. This is
12683 the main external entry point for redisplay.
12685 If the last redisplay displayed an echo area message and that message
12686 is no longer requested, we clear the echo area or bring back the
12687 mini-buffer if that is in use. */
12689 void
12690 redisplay (void)
12692 redisplay_internal ();
12696 static Lisp_Object
12697 overlay_arrow_string_or_property (Lisp_Object var)
12699 Lisp_Object val;
12701 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12702 return val;
12704 return Voverlay_arrow_string;
12707 /* Return 1 if there are any overlay-arrows in current_buffer. */
12708 static int
12709 overlay_arrow_in_current_buffer_p (void)
12711 Lisp_Object vlist;
12713 for (vlist = Voverlay_arrow_variable_list;
12714 CONSP (vlist);
12715 vlist = XCDR (vlist))
12717 Lisp_Object var = XCAR (vlist);
12718 Lisp_Object val;
12720 if (!SYMBOLP (var))
12721 continue;
12722 val = find_symbol_value (var);
12723 if (MARKERP (val)
12724 && current_buffer == XMARKER (val)->buffer)
12725 return 1;
12727 return 0;
12731 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12732 has changed. */
12734 static int
12735 overlay_arrows_changed_p (void)
12737 Lisp_Object vlist;
12739 for (vlist = Voverlay_arrow_variable_list;
12740 CONSP (vlist);
12741 vlist = XCDR (vlist))
12743 Lisp_Object var = XCAR (vlist);
12744 Lisp_Object val, pstr;
12746 if (!SYMBOLP (var))
12747 continue;
12748 val = find_symbol_value (var);
12749 if (!MARKERP (val))
12750 continue;
12751 if (! EQ (COERCE_MARKER (val),
12752 Fget (var, Qlast_arrow_position))
12753 || ! (pstr = overlay_arrow_string_or_property (var),
12754 EQ (pstr, Fget (var, Qlast_arrow_string))))
12755 return 1;
12757 return 0;
12760 /* Mark overlay arrows to be updated on next redisplay. */
12762 static void
12763 update_overlay_arrows (int up_to_date)
12765 Lisp_Object vlist;
12767 for (vlist = Voverlay_arrow_variable_list;
12768 CONSP (vlist);
12769 vlist = XCDR (vlist))
12771 Lisp_Object var = XCAR (vlist);
12773 if (!SYMBOLP (var))
12774 continue;
12776 if (up_to_date > 0)
12778 Lisp_Object val = find_symbol_value (var);
12779 Fput (var, Qlast_arrow_position,
12780 COERCE_MARKER (val));
12781 Fput (var, Qlast_arrow_string,
12782 overlay_arrow_string_or_property (var));
12784 else if (up_to_date < 0
12785 || !NILP (Fget (var, Qlast_arrow_position)))
12787 Fput (var, Qlast_arrow_position, Qt);
12788 Fput (var, Qlast_arrow_string, Qt);
12794 /* Return overlay arrow string to display at row.
12795 Return integer (bitmap number) for arrow bitmap in left fringe.
12796 Return nil if no overlay arrow. */
12798 static Lisp_Object
12799 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12801 Lisp_Object vlist;
12803 for (vlist = Voverlay_arrow_variable_list;
12804 CONSP (vlist);
12805 vlist = XCDR (vlist))
12807 Lisp_Object var = XCAR (vlist);
12808 Lisp_Object val;
12810 if (!SYMBOLP (var))
12811 continue;
12813 val = find_symbol_value (var);
12815 if (MARKERP (val)
12816 && current_buffer == XMARKER (val)->buffer
12817 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12819 if (FRAME_WINDOW_P (it->f)
12820 /* FIXME: if ROW->reversed_p is set, this should test
12821 the right fringe, not the left one. */
12822 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12824 #ifdef HAVE_WINDOW_SYSTEM
12825 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12827 int fringe_bitmap;
12828 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12829 return make_number (fringe_bitmap);
12831 #endif
12832 return make_number (-1); /* Use default arrow bitmap. */
12834 return overlay_arrow_string_or_property (var);
12838 return Qnil;
12841 /* Return 1 if point moved out of or into a composition. Otherwise
12842 return 0. PREV_BUF and PREV_PT are the last point buffer and
12843 position. BUF and PT are the current point buffer and position. */
12845 static int
12846 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12847 struct buffer *buf, ptrdiff_t pt)
12849 ptrdiff_t start, end;
12850 Lisp_Object prop;
12851 Lisp_Object buffer;
12853 XSETBUFFER (buffer, buf);
12854 /* Check a composition at the last point if point moved within the
12855 same buffer. */
12856 if (prev_buf == buf)
12858 if (prev_pt == pt)
12859 /* Point didn't move. */
12860 return 0;
12862 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12863 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12864 && COMPOSITION_VALID_P (start, end, prop)
12865 && start < prev_pt && end > prev_pt)
12866 /* The last point was within the composition. Return 1 iff
12867 point moved out of the composition. */
12868 return (pt <= start || pt >= end);
12871 /* Check a composition at the current point. */
12872 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12873 && find_composition (pt, -1, &start, &end, &prop, buffer)
12874 && COMPOSITION_VALID_P (start, end, prop)
12875 && start < pt && end > pt);
12879 /* Reconsider the setting of B->clip_changed which is displayed
12880 in window W. */
12882 static void
12883 reconsider_clip_changes (struct window *w, struct buffer *b)
12885 if (b->clip_changed
12886 && !NILP (w->window_end_valid)
12887 && w->current_matrix->buffer == b
12888 && w->current_matrix->zv == BUF_ZV (b)
12889 && w->current_matrix->begv == BUF_BEGV (b))
12890 b->clip_changed = 0;
12892 /* If display wasn't paused, and W is not a tool bar window, see if
12893 point has been moved into or out of a composition. In that case,
12894 we set b->clip_changed to 1 to force updating the screen. If
12895 b->clip_changed has already been set to 1, we can skip this
12896 check. */
12897 if (!b->clip_changed
12898 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12900 ptrdiff_t pt;
12902 if (w == XWINDOW (selected_window))
12903 pt = PT;
12904 else
12905 pt = marker_position (w->pointm);
12907 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12908 || pt != w->last_point)
12909 && check_point_in_composition (w->current_matrix->buffer,
12910 w->last_point,
12911 XBUFFER (w->buffer), pt))
12912 b->clip_changed = 1;
12917 /* Select FRAME to forward the values of frame-local variables into C
12918 variables so that the redisplay routines can access those values
12919 directly. */
12921 static void
12922 select_frame_for_redisplay (Lisp_Object frame)
12924 Lisp_Object tail, tem;
12925 Lisp_Object old = selected_frame;
12926 struct Lisp_Symbol *sym;
12928 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12930 selected_frame = frame;
12932 do {
12933 for (tail = XFRAME (frame)->param_alist;
12934 CONSP (tail); tail = XCDR (tail))
12935 if (CONSP (XCAR (tail))
12936 && (tem = XCAR (XCAR (tail)),
12937 SYMBOLP (tem))
12938 && (sym = indirect_variable (XSYMBOL (tem)),
12939 sym->redirect == SYMBOL_LOCALIZED)
12940 && sym->val.blv->frame_local)
12941 /* Use find_symbol_value rather than Fsymbol_value
12942 to avoid an error if it is void. */
12943 find_symbol_value (tem);
12944 } while (!EQ (frame, old) && (frame = old, 1));
12948 #define STOP_POLLING \
12949 do { if (! polling_stopped_here) stop_polling (); \
12950 polling_stopped_here = 1; } while (0)
12952 #define RESUME_POLLING \
12953 do { if (polling_stopped_here) start_polling (); \
12954 polling_stopped_here = 0; } while (0)
12957 /* Perhaps in the future avoid recentering windows if it
12958 is not necessary; currently that causes some problems. */
12960 static void
12961 redisplay_internal (void)
12963 struct window *w = XWINDOW (selected_window);
12964 struct window *sw;
12965 struct frame *fr;
12966 int pending;
12967 int must_finish = 0;
12968 struct text_pos tlbufpos, tlendpos;
12969 int number_of_visible_frames;
12970 ptrdiff_t count, count1;
12971 struct frame *sf;
12972 int polling_stopped_here = 0;
12973 Lisp_Object old_frame = selected_frame;
12974 struct backtrace backtrace;
12976 /* Non-zero means redisplay has to consider all windows on all
12977 frames. Zero means, only selected_window is considered. */
12978 int consider_all_windows_p;
12980 /* Non-zero means redisplay has to redisplay the miniwindow. */
12981 int update_miniwindow_p = 0;
12983 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12985 /* No redisplay if running in batch mode or frame is not yet fully
12986 initialized, or redisplay is explicitly turned off by setting
12987 Vinhibit_redisplay. */
12988 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12989 || !NILP (Vinhibit_redisplay))
12990 return;
12992 /* Don't examine these until after testing Vinhibit_redisplay.
12993 When Emacs is shutting down, perhaps because its connection to
12994 X has dropped, we should not look at them at all. */
12995 fr = XFRAME (w->frame);
12996 sf = SELECTED_FRAME ();
12998 if (!fr->glyphs_initialized_p)
12999 return;
13001 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13002 if (popup_activated ())
13003 return;
13004 #endif
13006 /* I don't think this happens but let's be paranoid. */
13007 if (redisplaying_p)
13008 return;
13010 /* Record a function that clears redisplaying_p
13011 when we leave this function. */
13012 count = SPECPDL_INDEX ();
13013 record_unwind_protect (unwind_redisplay, selected_frame);
13014 redisplaying_p = 1;
13015 specbind (Qinhibit_free_realized_faces, Qnil);
13017 /* Record this function, so it appears on the profiler's backtraces. */
13018 backtrace.next = backtrace_list;
13019 backtrace.function = Qredisplay_internal;
13020 backtrace.args = &Qnil;
13021 backtrace.nargs = 0;
13022 backtrace.debug_on_exit = 0;
13023 backtrace_list = &backtrace;
13026 Lisp_Object tail, frame;
13028 FOR_EACH_FRAME (tail, frame)
13030 struct frame *f = XFRAME (frame);
13031 f->already_hscrolled_p = 0;
13035 retry:
13036 /* Remember the currently selected window. */
13037 sw = w;
13039 if (!EQ (old_frame, selected_frame)
13040 && FRAME_LIVE_P (XFRAME (old_frame)))
13041 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13042 selected_frame and selected_window to be temporarily out-of-sync so
13043 when we come back here via `goto retry', we need to resync because we
13044 may need to run Elisp code (via prepare_menu_bars). */
13045 select_frame_for_redisplay (old_frame);
13047 pending = 0;
13048 reconsider_clip_changes (w, current_buffer);
13049 last_escape_glyph_frame = NULL;
13050 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13051 last_glyphless_glyph_frame = NULL;
13052 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13054 /* If new fonts have been loaded that make a glyph matrix adjustment
13055 necessary, do it. */
13056 if (fonts_changed_p)
13058 adjust_glyphs (NULL);
13059 ++windows_or_buffers_changed;
13060 fonts_changed_p = 0;
13063 /* If face_change_count is non-zero, init_iterator will free all
13064 realized faces, which includes the faces referenced from current
13065 matrices. So, we can't reuse current matrices in this case. */
13066 if (face_change_count)
13067 ++windows_or_buffers_changed;
13069 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13070 && FRAME_TTY (sf)->previous_frame != sf)
13072 /* Since frames on a single ASCII terminal share the same
13073 display area, displaying a different frame means redisplay
13074 the whole thing. */
13075 windows_or_buffers_changed++;
13076 SET_FRAME_GARBAGED (sf);
13077 #ifndef DOS_NT
13078 set_tty_color_mode (FRAME_TTY (sf), sf);
13079 #endif
13080 FRAME_TTY (sf)->previous_frame = sf;
13083 /* Set the visible flags for all frames. Do this before checking
13084 for resized or garbaged frames; they want to know if their frames
13085 are visible. See the comment in frame.h for
13086 FRAME_SAMPLE_VISIBILITY. */
13088 Lisp_Object tail, frame;
13090 number_of_visible_frames = 0;
13092 FOR_EACH_FRAME (tail, frame)
13094 struct frame *f = XFRAME (frame);
13096 FRAME_SAMPLE_VISIBILITY (f);
13097 if (FRAME_VISIBLE_P (f))
13098 ++number_of_visible_frames;
13099 clear_desired_matrices (f);
13103 /* Notice any pending interrupt request to change frame size. */
13104 do_pending_window_change (1);
13106 /* do_pending_window_change could change the selected_window due to
13107 frame resizing which makes the selected window too small. */
13108 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13110 sw = w;
13111 reconsider_clip_changes (w, current_buffer);
13114 /* Clear frames marked as garbaged. */
13115 if (frame_garbaged)
13116 clear_garbaged_frames ();
13118 /* Build menubar and tool-bar items. */
13119 if (NILP (Vmemory_full))
13120 prepare_menu_bars ();
13122 if (windows_or_buffers_changed)
13123 update_mode_lines++;
13125 /* Detect case that we need to write or remove a star in the mode line. */
13126 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13128 w->update_mode_line = 1;
13129 if (buffer_shared > 1)
13130 update_mode_lines++;
13133 /* Avoid invocation of point motion hooks by `current_column' below. */
13134 count1 = SPECPDL_INDEX ();
13135 specbind (Qinhibit_point_motion_hooks, Qt);
13137 /* If %c is in the mode line, update it if needed. */
13138 if (!NILP (w->column_number_displayed)
13139 /* This alternative quickly identifies a common case
13140 where no change is needed. */
13141 && !(PT == w->last_point
13142 && w->last_modified >= MODIFF
13143 && w->last_overlay_modified >= OVERLAY_MODIFF)
13144 && (XFASTINT (w->column_number_displayed) != current_column ()))
13145 w->update_mode_line = 1;
13147 unbind_to (count1, Qnil);
13149 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13151 /* The variable buffer_shared is set in redisplay_window and
13152 indicates that we redisplay a buffer in different windows. See
13153 there. */
13154 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13155 || cursor_type_changed);
13157 /* If specs for an arrow have changed, do thorough redisplay
13158 to ensure we remove any arrow that should no longer exist. */
13159 if (overlay_arrows_changed_p ())
13160 consider_all_windows_p = windows_or_buffers_changed = 1;
13162 /* Normally the message* functions will have already displayed and
13163 updated the echo area, but the frame may have been trashed, or
13164 the update may have been preempted, so display the echo area
13165 again here. Checking message_cleared_p captures the case that
13166 the echo area should be cleared. */
13167 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13168 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13169 || (message_cleared_p
13170 && minibuf_level == 0
13171 /* If the mini-window is currently selected, this means the
13172 echo-area doesn't show through. */
13173 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13175 int window_height_changed_p = echo_area_display (0);
13177 if (message_cleared_p)
13178 update_miniwindow_p = 1;
13180 must_finish = 1;
13182 /* If we don't display the current message, don't clear the
13183 message_cleared_p flag, because, if we did, we wouldn't clear
13184 the echo area in the next redisplay which doesn't preserve
13185 the echo area. */
13186 if (!display_last_displayed_message_p)
13187 message_cleared_p = 0;
13189 if (fonts_changed_p)
13190 goto retry;
13191 else if (window_height_changed_p)
13193 consider_all_windows_p = 1;
13194 ++update_mode_lines;
13195 ++windows_or_buffers_changed;
13197 /* If window configuration was changed, frames may have been
13198 marked garbaged. Clear them or we will experience
13199 surprises wrt scrolling. */
13200 if (frame_garbaged)
13201 clear_garbaged_frames ();
13204 else if (EQ (selected_window, minibuf_window)
13205 && (current_buffer->clip_changed
13206 || w->last_modified < MODIFF
13207 || w->last_overlay_modified < OVERLAY_MODIFF)
13208 && resize_mini_window (w, 0))
13210 /* Resized active mini-window to fit the size of what it is
13211 showing if its contents might have changed. */
13212 must_finish = 1;
13213 /* FIXME: this causes all frames to be updated, which seems unnecessary
13214 since only the current frame needs to be considered. This function needs
13215 to be rewritten with two variables, consider_all_windows and
13216 consider_all_frames. */
13217 consider_all_windows_p = 1;
13218 ++windows_or_buffers_changed;
13219 ++update_mode_lines;
13221 /* If window configuration was changed, frames may have been
13222 marked garbaged. Clear them or we will experience
13223 surprises wrt scrolling. */
13224 if (frame_garbaged)
13225 clear_garbaged_frames ();
13229 /* If showing the region, and mark has changed, we must redisplay
13230 the whole window. The assignment to this_line_start_pos prevents
13231 the optimization directly below this if-statement. */
13232 if (((!NILP (Vtransient_mark_mode)
13233 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13234 != !NILP (w->region_showing))
13235 || (!NILP (w->region_showing)
13236 && !EQ (w->region_showing,
13237 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13238 CHARPOS (this_line_start_pos) = 0;
13240 /* Optimize the case that only the line containing the cursor in the
13241 selected window has changed. Variables starting with this_ are
13242 set in display_line and record information about the line
13243 containing the cursor. */
13244 tlbufpos = this_line_start_pos;
13245 tlendpos = this_line_end_pos;
13246 if (!consider_all_windows_p
13247 && CHARPOS (tlbufpos) > 0
13248 && !w->update_mode_line
13249 && !current_buffer->clip_changed
13250 && !current_buffer->prevent_redisplay_optimizations_p
13251 && FRAME_VISIBLE_P (XFRAME (w->frame))
13252 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13253 /* Make sure recorded data applies to current buffer, etc. */
13254 && this_line_buffer == current_buffer
13255 && current_buffer == XBUFFER (w->buffer)
13256 && !w->force_start
13257 && !w->optional_new_start
13258 /* Point must be on the line that we have info recorded about. */
13259 && PT >= CHARPOS (tlbufpos)
13260 && PT <= Z - CHARPOS (tlendpos)
13261 /* All text outside that line, including its final newline,
13262 must be unchanged. */
13263 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13264 CHARPOS (tlendpos)))
13266 if (CHARPOS (tlbufpos) > BEGV
13267 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13268 && (CHARPOS (tlbufpos) == ZV
13269 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13270 /* Former continuation line has disappeared by becoming empty. */
13271 goto cancel;
13272 else if (w->last_modified < MODIFF
13273 || w->last_overlay_modified < OVERLAY_MODIFF
13274 || MINI_WINDOW_P (w))
13276 /* We have to handle the case of continuation around a
13277 wide-column character (see the comment in indent.c around
13278 line 1340).
13280 For instance, in the following case:
13282 -------- Insert --------
13283 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13284 J_I_ ==> J_I_ `^^' are cursors.
13285 ^^ ^^
13286 -------- --------
13288 As we have to redraw the line above, we cannot use this
13289 optimization. */
13291 struct it it;
13292 int line_height_before = this_line_pixel_height;
13294 /* Note that start_display will handle the case that the
13295 line starting at tlbufpos is a continuation line. */
13296 start_display (&it, w, tlbufpos);
13298 /* Implementation note: It this still necessary? */
13299 if (it.current_x != this_line_start_x)
13300 goto cancel;
13302 TRACE ((stderr, "trying display optimization 1\n"));
13303 w->cursor.vpos = -1;
13304 overlay_arrow_seen = 0;
13305 it.vpos = this_line_vpos;
13306 it.current_y = this_line_y;
13307 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13308 display_line (&it);
13310 /* If line contains point, is not continued,
13311 and ends at same distance from eob as before, we win. */
13312 if (w->cursor.vpos >= 0
13313 /* Line is not continued, otherwise this_line_start_pos
13314 would have been set to 0 in display_line. */
13315 && CHARPOS (this_line_start_pos)
13316 /* Line ends as before. */
13317 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13318 /* Line has same height as before. Otherwise other lines
13319 would have to be shifted up or down. */
13320 && this_line_pixel_height == line_height_before)
13322 /* If this is not the window's last line, we must adjust
13323 the charstarts of the lines below. */
13324 if (it.current_y < it.last_visible_y)
13326 struct glyph_row *row
13327 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13328 ptrdiff_t delta, delta_bytes;
13330 /* We used to distinguish between two cases here,
13331 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13332 when the line ends in a newline or the end of the
13333 buffer's accessible portion. But both cases did
13334 the same, so they were collapsed. */
13335 delta = (Z
13336 - CHARPOS (tlendpos)
13337 - MATRIX_ROW_START_CHARPOS (row));
13338 delta_bytes = (Z_BYTE
13339 - BYTEPOS (tlendpos)
13340 - MATRIX_ROW_START_BYTEPOS (row));
13342 increment_matrix_positions (w->current_matrix,
13343 this_line_vpos + 1,
13344 w->current_matrix->nrows,
13345 delta, delta_bytes);
13348 /* If this row displays text now but previously didn't,
13349 or vice versa, w->window_end_vpos may have to be
13350 adjusted. */
13351 if ((it.glyph_row - 1)->displays_text_p)
13353 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13354 wset_window_end_vpos (w, make_number (this_line_vpos));
13356 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13357 && this_line_vpos > 0)
13358 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13359 wset_window_end_valid (w, Qnil);
13361 /* Update hint: No need to try to scroll in update_window. */
13362 w->desired_matrix->no_scrolling_p = 1;
13364 #ifdef GLYPH_DEBUG
13365 *w->desired_matrix->method = 0;
13366 debug_method_add (w, "optimization 1");
13367 #endif
13368 #ifdef HAVE_WINDOW_SYSTEM
13369 update_window_fringes (w, 0);
13370 #endif
13371 goto update;
13373 else
13374 goto cancel;
13376 else if (/* Cursor position hasn't changed. */
13377 PT == w->last_point
13378 /* Make sure the cursor was last displayed
13379 in this window. Otherwise we have to reposition it. */
13380 && 0 <= w->cursor.vpos
13381 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13383 if (!must_finish)
13385 do_pending_window_change (1);
13386 /* If selected_window changed, redisplay again. */
13387 if (WINDOWP (selected_window)
13388 && (w = XWINDOW (selected_window)) != sw)
13389 goto retry;
13391 /* We used to always goto end_of_redisplay here, but this
13392 isn't enough if we have a blinking cursor. */
13393 if (w->cursor_off_p == w->last_cursor_off_p)
13394 goto end_of_redisplay;
13396 goto update;
13398 /* If highlighting the region, or if the cursor is in the echo area,
13399 then we can't just move the cursor. */
13400 else if (! (!NILP (Vtransient_mark_mode)
13401 && !NILP (BVAR (current_buffer, mark_active)))
13402 && (EQ (selected_window,
13403 BVAR (current_buffer, last_selected_window))
13404 || highlight_nonselected_windows)
13405 && NILP (w->region_showing)
13406 && NILP (Vshow_trailing_whitespace)
13407 && !cursor_in_echo_area)
13409 struct it it;
13410 struct glyph_row *row;
13412 /* Skip from tlbufpos to PT and see where it is. Note that
13413 PT may be in invisible text. If so, we will end at the
13414 next visible position. */
13415 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13416 NULL, DEFAULT_FACE_ID);
13417 it.current_x = this_line_start_x;
13418 it.current_y = this_line_y;
13419 it.vpos = this_line_vpos;
13421 /* The call to move_it_to stops in front of PT, but
13422 moves over before-strings. */
13423 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13425 if (it.vpos == this_line_vpos
13426 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13427 row->enabled_p))
13429 eassert (this_line_vpos == it.vpos);
13430 eassert (this_line_y == it.current_y);
13431 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13432 #ifdef GLYPH_DEBUG
13433 *w->desired_matrix->method = 0;
13434 debug_method_add (w, "optimization 3");
13435 #endif
13436 goto update;
13438 else
13439 goto cancel;
13442 cancel:
13443 /* Text changed drastically or point moved off of line. */
13444 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13447 CHARPOS (this_line_start_pos) = 0;
13448 consider_all_windows_p |= buffer_shared > 1;
13449 ++clear_face_cache_count;
13450 #ifdef HAVE_WINDOW_SYSTEM
13451 ++clear_image_cache_count;
13452 #endif
13454 /* Build desired matrices, and update the display. If
13455 consider_all_windows_p is non-zero, do it for all windows on all
13456 frames. Otherwise do it for selected_window, only. */
13458 if (consider_all_windows_p)
13460 Lisp_Object tail, frame;
13462 FOR_EACH_FRAME (tail, frame)
13463 XFRAME (frame)->updated_p = 0;
13465 /* Recompute # windows showing selected buffer. This will be
13466 incremented each time such a window is displayed. */
13467 buffer_shared = 0;
13469 FOR_EACH_FRAME (tail, frame)
13471 struct frame *f = XFRAME (frame);
13473 /* We don't have to do anything for unselected terminal
13474 frames. */
13475 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13476 && !EQ (FRAME_TTY (f)->top_frame, frame))
13477 continue;
13479 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13481 if (! EQ (frame, selected_frame))
13482 /* Select the frame, for the sake of frame-local
13483 variables. */
13484 select_frame_for_redisplay (frame);
13486 /* Mark all the scroll bars to be removed; we'll redeem
13487 the ones we want when we redisplay their windows. */
13488 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13489 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13491 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13492 redisplay_windows (FRAME_ROOT_WINDOW (f));
13494 /* The X error handler may have deleted that frame. */
13495 if (!FRAME_LIVE_P (f))
13496 continue;
13498 /* Any scroll bars which redisplay_windows should have
13499 nuked should now go away. */
13500 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13501 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13503 /* If fonts changed, display again. */
13504 /* ??? rms: I suspect it is a mistake to jump all the way
13505 back to retry here. It should just retry this frame. */
13506 if (fonts_changed_p)
13507 goto retry;
13509 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13511 /* See if we have to hscroll. */
13512 if (!f->already_hscrolled_p)
13514 f->already_hscrolled_p = 1;
13515 if (hscroll_windows (f->root_window))
13516 goto retry;
13519 /* Prevent various kinds of signals during display
13520 update. stdio is not robust about handling
13521 signals, which can cause an apparent I/O
13522 error. */
13523 if (interrupt_input)
13524 unrequest_sigio ();
13525 STOP_POLLING;
13527 /* Update the display. */
13528 set_window_update_flags (XWINDOW (f->root_window), 1);
13529 pending |= update_frame (f, 0, 0);
13530 f->updated_p = 1;
13535 if (!EQ (old_frame, selected_frame)
13536 && FRAME_LIVE_P (XFRAME (old_frame)))
13537 /* We played a bit fast-and-loose above and allowed selected_frame
13538 and selected_window to be temporarily out-of-sync but let's make
13539 sure this stays contained. */
13540 select_frame_for_redisplay (old_frame);
13541 eassert (EQ (XFRAME (selected_frame)->selected_window,
13542 selected_window));
13544 if (!pending)
13546 /* Do the mark_window_display_accurate after all windows have
13547 been redisplayed because this call resets flags in buffers
13548 which are needed for proper redisplay. */
13549 FOR_EACH_FRAME (tail, frame)
13551 struct frame *f = XFRAME (frame);
13552 if (f->updated_p)
13554 mark_window_display_accurate (f->root_window, 1);
13555 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13556 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13561 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13563 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13564 struct frame *mini_frame;
13566 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13567 /* Use list_of_error, not Qerror, so that
13568 we catch only errors and don't run the debugger. */
13569 internal_condition_case_1 (redisplay_window_1, selected_window,
13570 list_of_error,
13571 redisplay_window_error);
13572 if (update_miniwindow_p)
13573 internal_condition_case_1 (redisplay_window_1, mini_window,
13574 list_of_error,
13575 redisplay_window_error);
13577 /* Compare desired and current matrices, perform output. */
13579 update:
13580 /* If fonts changed, display again. */
13581 if (fonts_changed_p)
13582 goto retry;
13584 /* Prevent various kinds of signals during display update.
13585 stdio is not robust about handling signals,
13586 which can cause an apparent I/O error. */
13587 if (interrupt_input)
13588 unrequest_sigio ();
13589 STOP_POLLING;
13591 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13593 if (hscroll_windows (selected_window))
13594 goto retry;
13596 XWINDOW (selected_window)->must_be_updated_p = 1;
13597 pending = update_frame (sf, 0, 0);
13600 /* We may have called echo_area_display at the top of this
13601 function. If the echo area is on another frame, that may
13602 have put text on a frame other than the selected one, so the
13603 above call to update_frame would not have caught it. Catch
13604 it here. */
13605 mini_window = FRAME_MINIBUF_WINDOW (sf);
13606 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13608 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13610 XWINDOW (mini_window)->must_be_updated_p = 1;
13611 pending |= update_frame (mini_frame, 0, 0);
13612 if (!pending && hscroll_windows (mini_window))
13613 goto retry;
13617 /* If display was paused because of pending input, make sure we do a
13618 thorough update the next time. */
13619 if (pending)
13621 /* Prevent the optimization at the beginning of
13622 redisplay_internal that tries a single-line update of the
13623 line containing the cursor in the selected window. */
13624 CHARPOS (this_line_start_pos) = 0;
13626 /* Let the overlay arrow be updated the next time. */
13627 update_overlay_arrows (0);
13629 /* If we pause after scrolling, some rows in the current
13630 matrices of some windows are not valid. */
13631 if (!WINDOW_FULL_WIDTH_P (w)
13632 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13633 update_mode_lines = 1;
13635 else
13637 if (!consider_all_windows_p)
13639 /* This has already been done above if
13640 consider_all_windows_p is set. */
13641 mark_window_display_accurate_1 (w, 1);
13643 /* Say overlay arrows are up to date. */
13644 update_overlay_arrows (1);
13646 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13647 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13650 update_mode_lines = 0;
13651 windows_or_buffers_changed = 0;
13652 cursor_type_changed = 0;
13655 /* Start SIGIO interrupts coming again. Having them off during the
13656 code above makes it less likely one will discard output, but not
13657 impossible, since there might be stuff in the system buffer here.
13658 But it is much hairier to try to do anything about that. */
13659 if (interrupt_input)
13660 request_sigio ();
13661 RESUME_POLLING;
13663 /* If a frame has become visible which was not before, redisplay
13664 again, so that we display it. Expose events for such a frame
13665 (which it gets when becoming visible) don't call the parts of
13666 redisplay constructing glyphs, so simply exposing a frame won't
13667 display anything in this case. So, we have to display these
13668 frames here explicitly. */
13669 if (!pending)
13671 Lisp_Object tail, frame;
13672 int new_count = 0;
13674 FOR_EACH_FRAME (tail, frame)
13676 int this_is_visible = 0;
13678 if (XFRAME (frame)->visible)
13679 this_is_visible = 1;
13680 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13681 if (XFRAME (frame)->visible)
13682 this_is_visible = 1;
13684 if (this_is_visible)
13685 new_count++;
13688 if (new_count != number_of_visible_frames)
13689 windows_or_buffers_changed++;
13692 /* Change frame size now if a change is pending. */
13693 do_pending_window_change (1);
13695 /* If we just did a pending size change, or have additional
13696 visible frames, or selected_window changed, redisplay again. */
13697 if ((windows_or_buffers_changed && !pending)
13698 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13699 goto retry;
13701 /* Clear the face and image caches.
13703 We used to do this only if consider_all_windows_p. But the cache
13704 needs to be cleared if a timer creates images in the current
13705 buffer (e.g. the test case in Bug#6230). */
13707 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13709 clear_face_cache (0);
13710 clear_face_cache_count = 0;
13713 #ifdef HAVE_WINDOW_SYSTEM
13714 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13716 clear_image_caches (Qnil);
13717 clear_image_cache_count = 0;
13719 #endif /* HAVE_WINDOW_SYSTEM */
13721 end_of_redisplay:
13722 backtrace_list = backtrace.next;
13723 unbind_to (count, Qnil);
13724 RESUME_POLLING;
13728 /* Redisplay, but leave alone any recent echo area message unless
13729 another message has been requested in its place.
13731 This is useful in situations where you need to redisplay but no
13732 user action has occurred, making it inappropriate for the message
13733 area to be cleared. See tracking_off and
13734 wait_reading_process_output for examples of these situations.
13736 FROM_WHERE is an integer saying from where this function was
13737 called. This is useful for debugging. */
13739 void
13740 redisplay_preserve_echo_area (int from_where)
13742 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13744 if (!NILP (echo_area_buffer[1]))
13746 /* We have a previously displayed message, but no current
13747 message. Redisplay the previous message. */
13748 display_last_displayed_message_p = 1;
13749 redisplay_internal ();
13750 display_last_displayed_message_p = 0;
13752 else
13753 redisplay_internal ();
13755 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13756 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13757 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13761 /* Function registered with record_unwind_protect in redisplay_internal.
13762 Clear redisplaying_p. Also, select the previously
13763 selected frame, unless it has been deleted (by an X connection
13764 failure during redisplay, for example). */
13766 static Lisp_Object
13767 unwind_redisplay (Lisp_Object old_frame)
13769 redisplaying_p = 0;
13770 if (! EQ (old_frame, selected_frame)
13771 && FRAME_LIVE_P (XFRAME (old_frame)))
13772 select_frame_for_redisplay (old_frame);
13773 return Qnil;
13777 /* Mark the display of window W as accurate or inaccurate. If
13778 ACCURATE_P is non-zero mark display of W as accurate. If
13779 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13780 redisplay_internal is called. */
13782 static void
13783 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13785 if (BUFFERP (w->buffer))
13787 struct buffer *b = XBUFFER (w->buffer);
13789 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13790 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13791 w->last_had_star
13792 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13794 if (accurate_p)
13796 b->clip_changed = 0;
13797 b->prevent_redisplay_optimizations_p = 0;
13799 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13800 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13801 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13802 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13804 w->current_matrix->buffer = b;
13805 w->current_matrix->begv = BUF_BEGV (b);
13806 w->current_matrix->zv = BUF_ZV (b);
13808 w->last_cursor = w->cursor;
13809 w->last_cursor_off_p = w->cursor_off_p;
13811 if (w == XWINDOW (selected_window))
13812 w->last_point = BUF_PT (b);
13813 else
13814 w->last_point = XMARKER (w->pointm)->charpos;
13818 if (accurate_p)
13820 wset_window_end_valid (w, w->buffer);
13821 w->update_mode_line = 0;
13826 /* Mark the display of windows in the window tree rooted at WINDOW as
13827 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13828 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13829 be redisplayed the next time redisplay_internal is called. */
13831 void
13832 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13834 struct window *w;
13836 for (; !NILP (window); window = w->next)
13838 w = XWINDOW (window);
13839 mark_window_display_accurate_1 (w, accurate_p);
13841 if (!NILP (w->vchild))
13842 mark_window_display_accurate (w->vchild, accurate_p);
13843 if (!NILP (w->hchild))
13844 mark_window_display_accurate (w->hchild, accurate_p);
13847 if (accurate_p)
13849 update_overlay_arrows (1);
13851 else
13853 /* Force a thorough redisplay the next time by setting
13854 last_arrow_position and last_arrow_string to t, which is
13855 unequal to any useful value of Voverlay_arrow_... */
13856 update_overlay_arrows (-1);
13861 /* Return value in display table DP (Lisp_Char_Table *) for character
13862 C. Since a display table doesn't have any parent, we don't have to
13863 follow parent. Do not call this function directly but use the
13864 macro DISP_CHAR_VECTOR. */
13866 Lisp_Object
13867 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13869 Lisp_Object val;
13871 if (ASCII_CHAR_P (c))
13873 val = dp->ascii;
13874 if (SUB_CHAR_TABLE_P (val))
13875 val = XSUB_CHAR_TABLE (val)->contents[c];
13877 else
13879 Lisp_Object table;
13881 XSETCHAR_TABLE (table, dp);
13882 val = char_table_ref (table, c);
13884 if (NILP (val))
13885 val = dp->defalt;
13886 return val;
13891 /***********************************************************************
13892 Window Redisplay
13893 ***********************************************************************/
13895 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13897 static void
13898 redisplay_windows (Lisp_Object window)
13900 while (!NILP (window))
13902 struct window *w = XWINDOW (window);
13904 if (!NILP (w->hchild))
13905 redisplay_windows (w->hchild);
13906 else if (!NILP (w->vchild))
13907 redisplay_windows (w->vchild);
13908 else if (!NILP (w->buffer))
13910 displayed_buffer = XBUFFER (w->buffer);
13911 /* Use list_of_error, not Qerror, so that
13912 we catch only errors and don't run the debugger. */
13913 internal_condition_case_1 (redisplay_window_0, window,
13914 list_of_error,
13915 redisplay_window_error);
13918 window = w->next;
13922 static Lisp_Object
13923 redisplay_window_error (Lisp_Object ignore)
13925 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13926 return Qnil;
13929 static Lisp_Object
13930 redisplay_window_0 (Lisp_Object window)
13932 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13933 redisplay_window (window, 0);
13934 return Qnil;
13937 static Lisp_Object
13938 redisplay_window_1 (Lisp_Object window)
13940 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13941 redisplay_window (window, 1);
13942 return Qnil;
13946 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13947 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13948 which positions recorded in ROW differ from current buffer
13949 positions.
13951 Return 0 if cursor is not on this row, 1 otherwise. */
13953 static int
13954 set_cursor_from_row (struct window *w, struct glyph_row *row,
13955 struct glyph_matrix *matrix,
13956 ptrdiff_t delta, ptrdiff_t delta_bytes,
13957 int dy, int dvpos)
13959 struct glyph *glyph = row->glyphs[TEXT_AREA];
13960 struct glyph *end = glyph + row->used[TEXT_AREA];
13961 struct glyph *cursor = NULL;
13962 /* The last known character position in row. */
13963 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13964 int x = row->x;
13965 ptrdiff_t pt_old = PT - delta;
13966 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13967 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13968 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13969 /* A glyph beyond the edge of TEXT_AREA which we should never
13970 touch. */
13971 struct glyph *glyphs_end = end;
13972 /* Non-zero means we've found a match for cursor position, but that
13973 glyph has the avoid_cursor_p flag set. */
13974 int match_with_avoid_cursor = 0;
13975 /* Non-zero means we've seen at least one glyph that came from a
13976 display string. */
13977 int string_seen = 0;
13978 /* Largest and smallest buffer positions seen so far during scan of
13979 glyph row. */
13980 ptrdiff_t bpos_max = pos_before;
13981 ptrdiff_t bpos_min = pos_after;
13982 /* Last buffer position covered by an overlay string with an integer
13983 `cursor' property. */
13984 ptrdiff_t bpos_covered = 0;
13985 /* Non-zero means the display string on which to display the cursor
13986 comes from a text property, not from an overlay. */
13987 int string_from_text_prop = 0;
13989 /* Don't even try doing anything if called for a mode-line or
13990 header-line row, since the rest of the code isn't prepared to
13991 deal with such calamities. */
13992 eassert (!row->mode_line_p);
13993 if (row->mode_line_p)
13994 return 0;
13996 /* Skip over glyphs not having an object at the start and the end of
13997 the row. These are special glyphs like truncation marks on
13998 terminal frames. */
13999 if (row->displays_text_p)
14001 if (!row->reversed_p)
14003 while (glyph < end
14004 && INTEGERP (glyph->object)
14005 && glyph->charpos < 0)
14007 x += glyph->pixel_width;
14008 ++glyph;
14010 while (end > glyph
14011 && INTEGERP ((end - 1)->object)
14012 /* CHARPOS is zero for blanks and stretch glyphs
14013 inserted by extend_face_to_end_of_line. */
14014 && (end - 1)->charpos <= 0)
14015 --end;
14016 glyph_before = glyph - 1;
14017 glyph_after = end;
14019 else
14021 struct glyph *g;
14023 /* If the glyph row is reversed, we need to process it from back
14024 to front, so swap the edge pointers. */
14025 glyphs_end = end = glyph - 1;
14026 glyph += row->used[TEXT_AREA] - 1;
14028 while (glyph > end + 1
14029 && INTEGERP (glyph->object)
14030 && glyph->charpos < 0)
14032 --glyph;
14033 x -= glyph->pixel_width;
14035 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14036 --glyph;
14037 /* By default, in reversed rows we put the cursor on the
14038 rightmost (first in the reading order) glyph. */
14039 for (g = end + 1; g < glyph; g++)
14040 x += g->pixel_width;
14041 while (end < glyph
14042 && INTEGERP ((end + 1)->object)
14043 && (end + 1)->charpos <= 0)
14044 ++end;
14045 glyph_before = glyph + 1;
14046 glyph_after = end;
14049 else if (row->reversed_p)
14051 /* In R2L rows that don't display text, put the cursor on the
14052 rightmost glyph. Case in point: an empty last line that is
14053 part of an R2L paragraph. */
14054 cursor = end - 1;
14055 /* Avoid placing the cursor on the last glyph of the row, where
14056 on terminal frames we hold the vertical border between
14057 adjacent windows. */
14058 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14059 && !WINDOW_RIGHTMOST_P (w)
14060 && cursor == row->glyphs[LAST_AREA] - 1)
14061 cursor--;
14062 x = -1; /* will be computed below, at label compute_x */
14065 /* Step 1: Try to find the glyph whose character position
14066 corresponds to point. If that's not possible, find 2 glyphs
14067 whose character positions are the closest to point, one before
14068 point, the other after it. */
14069 if (!row->reversed_p)
14070 while (/* not marched to end of glyph row */
14071 glyph < end
14072 /* glyph was not inserted by redisplay for internal purposes */
14073 && !INTEGERP (glyph->object))
14075 if (BUFFERP (glyph->object))
14077 ptrdiff_t dpos = glyph->charpos - pt_old;
14079 if (glyph->charpos > bpos_max)
14080 bpos_max = glyph->charpos;
14081 if (glyph->charpos < bpos_min)
14082 bpos_min = glyph->charpos;
14083 if (!glyph->avoid_cursor_p)
14085 /* If we hit point, we've found the glyph on which to
14086 display the cursor. */
14087 if (dpos == 0)
14089 match_with_avoid_cursor = 0;
14090 break;
14092 /* See if we've found a better approximation to
14093 POS_BEFORE or to POS_AFTER. */
14094 if (0 > dpos && dpos > pos_before - pt_old)
14096 pos_before = glyph->charpos;
14097 glyph_before = glyph;
14099 else if (0 < dpos && dpos < pos_after - pt_old)
14101 pos_after = glyph->charpos;
14102 glyph_after = glyph;
14105 else if (dpos == 0)
14106 match_with_avoid_cursor = 1;
14108 else if (STRINGP (glyph->object))
14110 Lisp_Object chprop;
14111 ptrdiff_t glyph_pos = glyph->charpos;
14113 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14114 glyph->object);
14115 if (!NILP (chprop))
14117 /* If the string came from a `display' text property,
14118 look up the buffer position of that property and
14119 use that position to update bpos_max, as if we
14120 actually saw such a position in one of the row's
14121 glyphs. This helps with supporting integer values
14122 of `cursor' property on the display string in
14123 situations where most or all of the row's buffer
14124 text is completely covered by display properties,
14125 so that no glyph with valid buffer positions is
14126 ever seen in the row. */
14127 ptrdiff_t prop_pos =
14128 string_buffer_position_lim (glyph->object, pos_before,
14129 pos_after, 0);
14131 if (prop_pos >= pos_before)
14132 bpos_max = prop_pos - 1;
14134 if (INTEGERP (chprop))
14136 bpos_covered = bpos_max + XINT (chprop);
14137 /* If the `cursor' property covers buffer positions up
14138 to and including point, we should display cursor on
14139 this glyph. Note that, if a `cursor' property on one
14140 of the string's characters has an integer value, we
14141 will break out of the loop below _before_ we get to
14142 the position match above. IOW, integer values of
14143 the `cursor' property override the "exact match for
14144 point" strategy of positioning the cursor. */
14145 /* Implementation note: bpos_max == pt_old when, e.g.,
14146 we are in an empty line, where bpos_max is set to
14147 MATRIX_ROW_START_CHARPOS, see above. */
14148 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14150 cursor = glyph;
14151 break;
14155 string_seen = 1;
14157 x += glyph->pixel_width;
14158 ++glyph;
14160 else if (glyph > end) /* row is reversed */
14161 while (!INTEGERP (glyph->object))
14163 if (BUFFERP (glyph->object))
14165 ptrdiff_t dpos = glyph->charpos - pt_old;
14167 if (glyph->charpos > bpos_max)
14168 bpos_max = glyph->charpos;
14169 if (glyph->charpos < bpos_min)
14170 bpos_min = glyph->charpos;
14171 if (!glyph->avoid_cursor_p)
14173 if (dpos == 0)
14175 match_with_avoid_cursor = 0;
14176 break;
14178 if (0 > dpos && dpos > pos_before - pt_old)
14180 pos_before = glyph->charpos;
14181 glyph_before = glyph;
14183 else if (0 < dpos && dpos < pos_after - pt_old)
14185 pos_after = glyph->charpos;
14186 glyph_after = glyph;
14189 else if (dpos == 0)
14190 match_with_avoid_cursor = 1;
14192 else if (STRINGP (glyph->object))
14194 Lisp_Object chprop;
14195 ptrdiff_t glyph_pos = glyph->charpos;
14197 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14198 glyph->object);
14199 if (!NILP (chprop))
14201 ptrdiff_t prop_pos =
14202 string_buffer_position_lim (glyph->object, pos_before,
14203 pos_after, 0);
14205 if (prop_pos >= pos_before)
14206 bpos_max = prop_pos - 1;
14208 if (INTEGERP (chprop))
14210 bpos_covered = bpos_max + XINT (chprop);
14211 /* If the `cursor' property covers buffer positions up
14212 to and including point, we should display cursor on
14213 this glyph. */
14214 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14216 cursor = glyph;
14217 break;
14220 string_seen = 1;
14222 --glyph;
14223 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14225 x--; /* can't use any pixel_width */
14226 break;
14228 x -= glyph->pixel_width;
14231 /* Step 2: If we didn't find an exact match for point, we need to
14232 look for a proper place to put the cursor among glyphs between
14233 GLYPH_BEFORE and GLYPH_AFTER. */
14234 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14235 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14236 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14238 /* An empty line has a single glyph whose OBJECT is zero and
14239 whose CHARPOS is the position of a newline on that line.
14240 Note that on a TTY, there are more glyphs after that, which
14241 were produced by extend_face_to_end_of_line, but their
14242 CHARPOS is zero or negative. */
14243 int empty_line_p =
14244 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14245 && INTEGERP (glyph->object) && glyph->charpos > 0
14246 /* On a TTY, continued and truncated rows also have a glyph at
14247 their end whose OBJECT is zero and whose CHARPOS is
14248 positive (the continuation and truncation glyphs), but such
14249 rows are obviously not "empty". */
14250 && !(row->continued_p || row->truncated_on_right_p);
14252 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14254 ptrdiff_t ellipsis_pos;
14256 /* Scan back over the ellipsis glyphs. */
14257 if (!row->reversed_p)
14259 ellipsis_pos = (glyph - 1)->charpos;
14260 while (glyph > row->glyphs[TEXT_AREA]
14261 && (glyph - 1)->charpos == ellipsis_pos)
14262 glyph--, x -= glyph->pixel_width;
14263 /* That loop always goes one position too far, including
14264 the glyph before the ellipsis. So scan forward over
14265 that one. */
14266 x += glyph->pixel_width;
14267 glyph++;
14269 else /* row is reversed */
14271 ellipsis_pos = (glyph + 1)->charpos;
14272 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14273 && (glyph + 1)->charpos == ellipsis_pos)
14274 glyph++, x += glyph->pixel_width;
14275 x -= glyph->pixel_width;
14276 glyph--;
14279 else if (match_with_avoid_cursor)
14281 cursor = glyph_after;
14282 x = -1;
14284 else if (string_seen)
14286 int incr = row->reversed_p ? -1 : +1;
14288 /* Need to find the glyph that came out of a string which is
14289 present at point. That glyph is somewhere between
14290 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14291 positioned between POS_BEFORE and POS_AFTER in the
14292 buffer. */
14293 struct glyph *start, *stop;
14294 ptrdiff_t pos = pos_before;
14296 x = -1;
14298 /* If the row ends in a newline from a display string,
14299 reordering could have moved the glyphs belonging to the
14300 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14301 in this case we extend the search to the last glyph in
14302 the row that was not inserted by redisplay. */
14303 if (row->ends_in_newline_from_string_p)
14305 glyph_after = end;
14306 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14309 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14310 correspond to POS_BEFORE and POS_AFTER, respectively. We
14311 need START and STOP in the order that corresponds to the
14312 row's direction as given by its reversed_p flag. If the
14313 directionality of characters between POS_BEFORE and
14314 POS_AFTER is the opposite of the row's base direction,
14315 these characters will have been reordered for display,
14316 and we need to reverse START and STOP. */
14317 if (!row->reversed_p)
14319 start = min (glyph_before, glyph_after);
14320 stop = max (glyph_before, glyph_after);
14322 else
14324 start = max (glyph_before, glyph_after);
14325 stop = min (glyph_before, glyph_after);
14327 for (glyph = start + incr;
14328 row->reversed_p ? glyph > stop : glyph < stop; )
14331 /* Any glyphs that come from the buffer are here because
14332 of bidi reordering. Skip them, and only pay
14333 attention to glyphs that came from some string. */
14334 if (STRINGP (glyph->object))
14336 Lisp_Object str;
14337 ptrdiff_t tem;
14338 /* If the display property covers the newline, we
14339 need to search for it one position farther. */
14340 ptrdiff_t lim = pos_after
14341 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14343 string_from_text_prop = 0;
14344 str = glyph->object;
14345 tem = string_buffer_position_lim (str, pos, lim, 0);
14346 if (tem == 0 /* from overlay */
14347 || pos <= tem)
14349 /* If the string from which this glyph came is
14350 found in the buffer at point, or at position
14351 that is closer to point than pos_after, then
14352 we've found the glyph we've been looking for.
14353 If it comes from an overlay (tem == 0), and
14354 it has the `cursor' property on one of its
14355 glyphs, record that glyph as a candidate for
14356 displaying the cursor. (As in the
14357 unidirectional version, we will display the
14358 cursor on the last candidate we find.) */
14359 if (tem == 0
14360 || tem == pt_old
14361 || (tem - pt_old > 0 && tem < pos_after))
14363 /* The glyphs from this string could have
14364 been reordered. Find the one with the
14365 smallest string position. Or there could
14366 be a character in the string with the
14367 `cursor' property, which means display
14368 cursor on that character's glyph. */
14369 ptrdiff_t strpos = glyph->charpos;
14371 if (tem)
14373 cursor = glyph;
14374 string_from_text_prop = 1;
14376 for ( ;
14377 (row->reversed_p ? glyph > stop : glyph < stop)
14378 && EQ (glyph->object, str);
14379 glyph += incr)
14381 Lisp_Object cprop;
14382 ptrdiff_t gpos = glyph->charpos;
14384 cprop = Fget_char_property (make_number (gpos),
14385 Qcursor,
14386 glyph->object);
14387 if (!NILP (cprop))
14389 cursor = glyph;
14390 break;
14392 if (tem && glyph->charpos < strpos)
14394 strpos = glyph->charpos;
14395 cursor = glyph;
14399 if (tem == pt_old
14400 || (tem - pt_old > 0 && tem < pos_after))
14401 goto compute_x;
14403 if (tem)
14404 pos = tem + 1; /* don't find previous instances */
14406 /* This string is not what we want; skip all of the
14407 glyphs that came from it. */
14408 while ((row->reversed_p ? glyph > stop : glyph < stop)
14409 && EQ (glyph->object, str))
14410 glyph += incr;
14412 else
14413 glyph += incr;
14416 /* If we reached the end of the line, and END was from a string,
14417 the cursor is not on this line. */
14418 if (cursor == NULL
14419 && (row->reversed_p ? glyph <= end : glyph >= end)
14420 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14421 && STRINGP (end->object)
14422 && row->continued_p)
14423 return 0;
14425 /* A truncated row may not include PT among its character positions.
14426 Setting the cursor inside the scroll margin will trigger
14427 recalculation of hscroll in hscroll_window_tree. But if a
14428 display string covers point, defer to the string-handling
14429 code below to figure this out. */
14430 else if (row->truncated_on_left_p && pt_old < bpos_min)
14432 cursor = glyph_before;
14433 x = -1;
14435 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14436 /* Zero-width characters produce no glyphs. */
14437 || (!empty_line_p
14438 && (row->reversed_p
14439 ? glyph_after > glyphs_end
14440 : glyph_after < glyphs_end)))
14442 cursor = glyph_after;
14443 x = -1;
14447 compute_x:
14448 if (cursor != NULL)
14449 glyph = cursor;
14450 else if (glyph == glyphs_end
14451 && pos_before == pos_after
14452 && STRINGP ((row->reversed_p
14453 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14454 : row->glyphs[TEXT_AREA])->object))
14456 /* If all the glyphs of this row came from strings, put the
14457 cursor on the first glyph of the row. This avoids having the
14458 cursor outside of the text area in this very rare and hard
14459 use case. */
14460 glyph =
14461 row->reversed_p
14462 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14463 : row->glyphs[TEXT_AREA];
14465 if (x < 0)
14467 struct glyph *g;
14469 /* Need to compute x that corresponds to GLYPH. */
14470 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14472 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14473 emacs_abort ();
14474 x += g->pixel_width;
14478 /* ROW could be part of a continued line, which, under bidi
14479 reordering, might have other rows whose start and end charpos
14480 occlude point. Only set w->cursor if we found a better
14481 approximation to the cursor position than we have from previously
14482 examined candidate rows belonging to the same continued line. */
14483 if (/* we already have a candidate row */
14484 w->cursor.vpos >= 0
14485 /* that candidate is not the row we are processing */
14486 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14487 /* Make sure cursor.vpos specifies a row whose start and end
14488 charpos occlude point, and it is valid candidate for being a
14489 cursor-row. This is because some callers of this function
14490 leave cursor.vpos at the row where the cursor was displayed
14491 during the last redisplay cycle. */
14492 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14493 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14494 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14496 struct glyph *g1 =
14497 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14499 /* Don't consider glyphs that are outside TEXT_AREA. */
14500 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14501 return 0;
14502 /* Keep the candidate whose buffer position is the closest to
14503 point or has the `cursor' property. */
14504 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14505 w->cursor.hpos >= 0
14506 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14507 && ((BUFFERP (g1->object)
14508 && (g1->charpos == pt_old /* an exact match always wins */
14509 || (BUFFERP (glyph->object)
14510 && eabs (g1->charpos - pt_old)
14511 < eabs (glyph->charpos - pt_old))))
14512 /* previous candidate is a glyph from a string that has
14513 a non-nil `cursor' property */
14514 || (STRINGP (g1->object)
14515 && (!NILP (Fget_char_property (make_number (g1->charpos),
14516 Qcursor, g1->object))
14517 /* previous candidate is from the same display
14518 string as this one, and the display string
14519 came from a text property */
14520 || (EQ (g1->object, glyph->object)
14521 && string_from_text_prop)
14522 /* this candidate is from newline and its
14523 position is not an exact match */
14524 || (INTEGERP (glyph->object)
14525 && glyph->charpos != pt_old)))))
14526 return 0;
14527 /* If this candidate gives an exact match, use that. */
14528 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14529 /* If this candidate is a glyph created for the
14530 terminating newline of a line, and point is on that
14531 newline, it wins because it's an exact match. */
14532 || (!row->continued_p
14533 && INTEGERP (glyph->object)
14534 && glyph->charpos == 0
14535 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14536 /* Otherwise, keep the candidate that comes from a row
14537 spanning less buffer positions. This may win when one or
14538 both candidate positions are on glyphs that came from
14539 display strings, for which we cannot compare buffer
14540 positions. */
14541 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14542 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14543 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14544 return 0;
14546 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14547 w->cursor.x = x;
14548 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14549 w->cursor.y = row->y + dy;
14551 if (w == XWINDOW (selected_window))
14553 if (!row->continued_p
14554 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14555 && row->x == 0)
14557 this_line_buffer = XBUFFER (w->buffer);
14559 CHARPOS (this_line_start_pos)
14560 = MATRIX_ROW_START_CHARPOS (row) + delta;
14561 BYTEPOS (this_line_start_pos)
14562 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14564 CHARPOS (this_line_end_pos)
14565 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14566 BYTEPOS (this_line_end_pos)
14567 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14569 this_line_y = w->cursor.y;
14570 this_line_pixel_height = row->height;
14571 this_line_vpos = w->cursor.vpos;
14572 this_line_start_x = row->x;
14574 else
14575 CHARPOS (this_line_start_pos) = 0;
14578 return 1;
14582 /* Run window scroll functions, if any, for WINDOW with new window
14583 start STARTP. Sets the window start of WINDOW to that position.
14585 We assume that the window's buffer is really current. */
14587 static struct text_pos
14588 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14590 struct window *w = XWINDOW (window);
14591 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14593 if (current_buffer != XBUFFER (w->buffer))
14594 emacs_abort ();
14596 if (!NILP (Vwindow_scroll_functions))
14598 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14599 make_number (CHARPOS (startp)));
14600 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14601 /* In case the hook functions switch buffers. */
14602 set_buffer_internal (XBUFFER (w->buffer));
14605 return startp;
14609 /* Make sure the line containing the cursor is fully visible.
14610 A value of 1 means there is nothing to be done.
14611 (Either the line is fully visible, or it cannot be made so,
14612 or we cannot tell.)
14614 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14615 is higher than window.
14617 A value of 0 means the caller should do scrolling
14618 as if point had gone off the screen. */
14620 static int
14621 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14623 struct glyph_matrix *matrix;
14624 struct glyph_row *row;
14625 int window_height;
14627 if (!make_cursor_line_fully_visible_p)
14628 return 1;
14630 /* It's not always possible to find the cursor, e.g, when a window
14631 is full of overlay strings. Don't do anything in that case. */
14632 if (w->cursor.vpos < 0)
14633 return 1;
14635 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14636 row = MATRIX_ROW (matrix, w->cursor.vpos);
14638 /* If the cursor row is not partially visible, there's nothing to do. */
14639 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14640 return 1;
14642 /* If the row the cursor is in is taller than the window's height,
14643 it's not clear what to do, so do nothing. */
14644 window_height = window_box_height (w);
14645 if (row->height >= window_height)
14647 if (!force_p || MINI_WINDOW_P (w)
14648 || w->vscroll || w->cursor.vpos == 0)
14649 return 1;
14651 return 0;
14655 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14656 non-zero means only WINDOW is redisplayed in redisplay_internal.
14657 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14658 in redisplay_window to bring a partially visible line into view in
14659 the case that only the cursor has moved.
14661 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14662 last screen line's vertical height extends past the end of the screen.
14664 Value is
14666 1 if scrolling succeeded
14668 0 if scrolling didn't find point.
14670 -1 if new fonts have been loaded so that we must interrupt
14671 redisplay, adjust glyph matrices, and try again. */
14673 enum
14675 SCROLLING_SUCCESS,
14676 SCROLLING_FAILED,
14677 SCROLLING_NEED_LARGER_MATRICES
14680 /* If scroll-conservatively is more than this, never recenter.
14682 If you change this, don't forget to update the doc string of
14683 `scroll-conservatively' and the Emacs manual. */
14684 #define SCROLL_LIMIT 100
14686 static int
14687 try_scrolling (Lisp_Object window, int just_this_one_p,
14688 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14689 int temp_scroll_step, int last_line_misfit)
14691 struct window *w = XWINDOW (window);
14692 struct frame *f = XFRAME (w->frame);
14693 struct text_pos pos, startp;
14694 struct it it;
14695 int this_scroll_margin, scroll_max, rc, height;
14696 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14697 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14698 Lisp_Object aggressive;
14699 /* We will never try scrolling more than this number of lines. */
14700 int scroll_limit = SCROLL_LIMIT;
14702 #ifdef GLYPH_DEBUG
14703 debug_method_add (w, "try_scrolling");
14704 #endif
14706 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14708 /* Compute scroll margin height in pixels. We scroll when point is
14709 within this distance from the top or bottom of the window. */
14710 if (scroll_margin > 0)
14711 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14712 * FRAME_LINE_HEIGHT (f);
14713 else
14714 this_scroll_margin = 0;
14716 /* Force arg_scroll_conservatively to have a reasonable value, to
14717 avoid scrolling too far away with slow move_it_* functions. Note
14718 that the user can supply scroll-conservatively equal to
14719 `most-positive-fixnum', which can be larger than INT_MAX. */
14720 if (arg_scroll_conservatively > scroll_limit)
14722 arg_scroll_conservatively = scroll_limit + 1;
14723 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14725 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14726 /* Compute how much we should try to scroll maximally to bring
14727 point into view. */
14728 scroll_max = (max (scroll_step,
14729 max (arg_scroll_conservatively, temp_scroll_step))
14730 * FRAME_LINE_HEIGHT (f));
14731 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14732 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14733 /* We're trying to scroll because of aggressive scrolling but no
14734 scroll_step is set. Choose an arbitrary one. */
14735 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14736 else
14737 scroll_max = 0;
14739 too_near_end:
14741 /* Decide whether to scroll down. */
14742 if (PT > CHARPOS (startp))
14744 int scroll_margin_y;
14746 /* Compute the pixel ypos of the scroll margin, then move IT to
14747 either that ypos or PT, whichever comes first. */
14748 start_display (&it, w, startp);
14749 scroll_margin_y = it.last_visible_y - this_scroll_margin
14750 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14751 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14752 (MOVE_TO_POS | MOVE_TO_Y));
14754 if (PT > CHARPOS (it.current.pos))
14756 int y0 = line_bottom_y (&it);
14757 /* Compute how many pixels below window bottom to stop searching
14758 for PT. This avoids costly search for PT that is far away if
14759 the user limited scrolling by a small number of lines, but
14760 always finds PT if scroll_conservatively is set to a large
14761 number, such as most-positive-fixnum. */
14762 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14763 int y_to_move = it.last_visible_y + slack;
14765 /* Compute the distance from the scroll margin to PT or to
14766 the scroll limit, whichever comes first. This should
14767 include the height of the cursor line, to make that line
14768 fully visible. */
14769 move_it_to (&it, PT, -1, y_to_move,
14770 -1, MOVE_TO_POS | MOVE_TO_Y);
14771 dy = line_bottom_y (&it) - y0;
14773 if (dy > scroll_max)
14774 return SCROLLING_FAILED;
14776 if (dy > 0)
14777 scroll_down_p = 1;
14781 if (scroll_down_p)
14783 /* Point is in or below the bottom scroll margin, so move the
14784 window start down. If scrolling conservatively, move it just
14785 enough down to make point visible. If scroll_step is set,
14786 move it down by scroll_step. */
14787 if (arg_scroll_conservatively)
14788 amount_to_scroll
14789 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14790 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14791 else if (scroll_step || temp_scroll_step)
14792 amount_to_scroll = scroll_max;
14793 else
14795 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14796 height = WINDOW_BOX_TEXT_HEIGHT (w);
14797 if (NUMBERP (aggressive))
14799 double float_amount = XFLOATINT (aggressive) * height;
14800 int aggressive_scroll = float_amount;
14801 if (aggressive_scroll == 0 && float_amount > 0)
14802 aggressive_scroll = 1;
14803 /* Don't let point enter the scroll margin near top of
14804 the window. This could happen if the value of
14805 scroll_up_aggressively is too large and there are
14806 non-zero margins, because scroll_up_aggressively
14807 means put point that fraction of window height
14808 _from_the_bottom_margin_. */
14809 if (aggressive_scroll + 2*this_scroll_margin > height)
14810 aggressive_scroll = height - 2*this_scroll_margin;
14811 amount_to_scroll = dy + aggressive_scroll;
14815 if (amount_to_scroll <= 0)
14816 return SCROLLING_FAILED;
14818 start_display (&it, w, startp);
14819 if (arg_scroll_conservatively <= scroll_limit)
14820 move_it_vertically (&it, amount_to_scroll);
14821 else
14823 /* Extra precision for users who set scroll-conservatively
14824 to a large number: make sure the amount we scroll
14825 the window start is never less than amount_to_scroll,
14826 which was computed as distance from window bottom to
14827 point. This matters when lines at window top and lines
14828 below window bottom have different height. */
14829 struct it it1;
14830 void *it1data = NULL;
14831 /* We use a temporary it1 because line_bottom_y can modify
14832 its argument, if it moves one line down; see there. */
14833 int start_y;
14835 SAVE_IT (it1, it, it1data);
14836 start_y = line_bottom_y (&it1);
14837 do {
14838 RESTORE_IT (&it, &it, it1data);
14839 move_it_by_lines (&it, 1);
14840 SAVE_IT (it1, it, it1data);
14841 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14844 /* If STARTP is unchanged, move it down another screen line. */
14845 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14846 move_it_by_lines (&it, 1);
14847 startp = it.current.pos;
14849 else
14851 struct text_pos scroll_margin_pos = startp;
14853 /* See if point is inside the scroll margin at the top of the
14854 window. */
14855 if (this_scroll_margin)
14857 start_display (&it, w, startp);
14858 move_it_vertically (&it, this_scroll_margin);
14859 scroll_margin_pos = it.current.pos;
14862 if (PT < CHARPOS (scroll_margin_pos))
14864 /* Point is in the scroll margin at the top of the window or
14865 above what is displayed in the window. */
14866 int y0, y_to_move;
14868 /* Compute the vertical distance from PT to the scroll
14869 margin position. Move as far as scroll_max allows, or
14870 one screenful, or 10 screen lines, whichever is largest.
14871 Give up if distance is greater than scroll_max or if we
14872 didn't reach the scroll margin position. */
14873 SET_TEXT_POS (pos, PT, PT_BYTE);
14874 start_display (&it, w, pos);
14875 y0 = it.current_y;
14876 y_to_move = max (it.last_visible_y,
14877 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14878 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14879 y_to_move, -1,
14880 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14881 dy = it.current_y - y0;
14882 if (dy > scroll_max
14883 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14884 return SCROLLING_FAILED;
14886 /* Compute new window start. */
14887 start_display (&it, w, startp);
14889 if (arg_scroll_conservatively)
14890 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14891 max (scroll_step, temp_scroll_step));
14892 else if (scroll_step || temp_scroll_step)
14893 amount_to_scroll = scroll_max;
14894 else
14896 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14897 height = WINDOW_BOX_TEXT_HEIGHT (w);
14898 if (NUMBERP (aggressive))
14900 double float_amount = XFLOATINT (aggressive) * height;
14901 int aggressive_scroll = float_amount;
14902 if (aggressive_scroll == 0 && float_amount > 0)
14903 aggressive_scroll = 1;
14904 /* Don't let point enter the scroll margin near
14905 bottom of the window, if the value of
14906 scroll_down_aggressively happens to be too
14907 large. */
14908 if (aggressive_scroll + 2*this_scroll_margin > height)
14909 aggressive_scroll = height - 2*this_scroll_margin;
14910 amount_to_scroll = dy + aggressive_scroll;
14914 if (amount_to_scroll <= 0)
14915 return SCROLLING_FAILED;
14917 move_it_vertically_backward (&it, amount_to_scroll);
14918 startp = it.current.pos;
14922 /* Run window scroll functions. */
14923 startp = run_window_scroll_functions (window, startp);
14925 /* Display the window. Give up if new fonts are loaded, or if point
14926 doesn't appear. */
14927 if (!try_window (window, startp, 0))
14928 rc = SCROLLING_NEED_LARGER_MATRICES;
14929 else if (w->cursor.vpos < 0)
14931 clear_glyph_matrix (w->desired_matrix);
14932 rc = SCROLLING_FAILED;
14934 else
14936 /* Maybe forget recorded base line for line number display. */
14937 if (!just_this_one_p
14938 || current_buffer->clip_changed
14939 || BEG_UNCHANGED < CHARPOS (startp))
14940 wset_base_line_number (w, Qnil);
14942 /* If cursor ends up on a partially visible line,
14943 treat that as being off the bottom of the screen. */
14944 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14945 /* It's possible that the cursor is on the first line of the
14946 buffer, which is partially obscured due to a vscroll
14947 (Bug#7537). In that case, avoid looping forever . */
14948 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14950 clear_glyph_matrix (w->desired_matrix);
14951 ++extra_scroll_margin_lines;
14952 goto too_near_end;
14954 rc = SCROLLING_SUCCESS;
14957 return rc;
14961 /* Compute a suitable window start for window W if display of W starts
14962 on a continuation line. Value is non-zero if a new window start
14963 was computed.
14965 The new window start will be computed, based on W's width, starting
14966 from the start of the continued line. It is the start of the
14967 screen line with the minimum distance from the old start W->start. */
14969 static int
14970 compute_window_start_on_continuation_line (struct window *w)
14972 struct text_pos pos, start_pos;
14973 int window_start_changed_p = 0;
14975 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14977 /* If window start is on a continuation line... Window start may be
14978 < BEGV in case there's invisible text at the start of the
14979 buffer (M-x rmail, for example). */
14980 if (CHARPOS (start_pos) > BEGV
14981 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14983 struct it it;
14984 struct glyph_row *row;
14986 /* Handle the case that the window start is out of range. */
14987 if (CHARPOS (start_pos) < BEGV)
14988 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14989 else if (CHARPOS (start_pos) > ZV)
14990 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14992 /* Find the start of the continued line. This should be fast
14993 because scan_buffer is fast (newline cache). */
14994 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14995 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14996 row, DEFAULT_FACE_ID);
14997 reseat_at_previous_visible_line_start (&it);
14999 /* If the line start is "too far" away from the window start,
15000 say it takes too much time to compute a new window start. */
15001 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15002 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15004 int min_distance, distance;
15006 /* Move forward by display lines to find the new window
15007 start. If window width was enlarged, the new start can
15008 be expected to be > the old start. If window width was
15009 decreased, the new window start will be < the old start.
15010 So, we're looking for the display line start with the
15011 minimum distance from the old window start. */
15012 pos = it.current.pos;
15013 min_distance = INFINITY;
15014 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15015 distance < min_distance)
15017 min_distance = distance;
15018 pos = it.current.pos;
15019 move_it_by_lines (&it, 1);
15022 /* Set the window start there. */
15023 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15024 window_start_changed_p = 1;
15028 return window_start_changed_p;
15032 /* Try cursor movement in case text has not changed in window WINDOW,
15033 with window start STARTP. Value is
15035 CURSOR_MOVEMENT_SUCCESS if successful
15037 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15039 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15040 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15041 we want to scroll as if scroll-step were set to 1. See the code.
15043 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15044 which case we have to abort this redisplay, and adjust matrices
15045 first. */
15047 enum
15049 CURSOR_MOVEMENT_SUCCESS,
15050 CURSOR_MOVEMENT_CANNOT_BE_USED,
15051 CURSOR_MOVEMENT_MUST_SCROLL,
15052 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15055 static int
15056 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15058 struct window *w = XWINDOW (window);
15059 struct frame *f = XFRAME (w->frame);
15060 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15062 #ifdef GLYPH_DEBUG
15063 if (inhibit_try_cursor_movement)
15064 return rc;
15065 #endif
15067 /* Previously, there was a check for Lisp integer in the
15068 if-statement below. Now, this field is converted to
15069 ptrdiff_t, thus zero means invalid position in a buffer. */
15070 eassert (w->last_point > 0);
15072 /* Handle case where text has not changed, only point, and it has
15073 not moved off the frame. */
15074 if (/* Point may be in this window. */
15075 PT >= CHARPOS (startp)
15076 /* Selective display hasn't changed. */
15077 && !current_buffer->clip_changed
15078 /* Function force-mode-line-update is used to force a thorough
15079 redisplay. It sets either windows_or_buffers_changed or
15080 update_mode_lines. So don't take a shortcut here for these
15081 cases. */
15082 && !update_mode_lines
15083 && !windows_or_buffers_changed
15084 && !cursor_type_changed
15085 /* Can't use this case if highlighting a region. When a
15086 region exists, cursor movement has to do more than just
15087 set the cursor. */
15088 && !(!NILP (Vtransient_mark_mode)
15089 && !NILP (BVAR (current_buffer, mark_active)))
15090 && NILP (w->region_showing)
15091 && NILP (Vshow_trailing_whitespace)
15092 /* This code is not used for mini-buffer for the sake of the case
15093 of redisplaying to replace an echo area message; since in
15094 that case the mini-buffer contents per se are usually
15095 unchanged. This code is of no real use in the mini-buffer
15096 since the handling of this_line_start_pos, etc., in redisplay
15097 handles the same cases. */
15098 && !EQ (window, minibuf_window)
15099 /* When splitting windows or for new windows, it happens that
15100 redisplay is called with a nil window_end_vpos or one being
15101 larger than the window. This should really be fixed in
15102 window.c. I don't have this on my list, now, so we do
15103 approximately the same as the old redisplay code. --gerd. */
15104 && INTEGERP (w->window_end_vpos)
15105 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15106 && (FRAME_WINDOW_P (f)
15107 || !overlay_arrow_in_current_buffer_p ()))
15109 int this_scroll_margin, top_scroll_margin;
15110 struct glyph_row *row = NULL;
15112 #ifdef GLYPH_DEBUG
15113 debug_method_add (w, "cursor movement");
15114 #endif
15116 /* Scroll if point within this distance from the top or bottom
15117 of the window. This is a pixel value. */
15118 if (scroll_margin > 0)
15120 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15121 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15123 else
15124 this_scroll_margin = 0;
15126 top_scroll_margin = this_scroll_margin;
15127 if (WINDOW_WANTS_HEADER_LINE_P (w))
15128 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15130 /* Start with the row the cursor was displayed during the last
15131 not paused redisplay. Give up if that row is not valid. */
15132 if (w->last_cursor.vpos < 0
15133 || w->last_cursor.vpos >= w->current_matrix->nrows)
15134 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15135 else
15137 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15138 if (row->mode_line_p)
15139 ++row;
15140 if (!row->enabled_p)
15141 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15144 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15146 int scroll_p = 0, must_scroll = 0;
15147 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15149 if (PT > w->last_point)
15151 /* Point has moved forward. */
15152 while (MATRIX_ROW_END_CHARPOS (row) < PT
15153 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15155 eassert (row->enabled_p);
15156 ++row;
15159 /* If the end position of a row equals the start
15160 position of the next row, and PT is at that position,
15161 we would rather display cursor in the next line. */
15162 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15163 && MATRIX_ROW_END_CHARPOS (row) == PT
15164 && row < w->current_matrix->rows
15165 + w->current_matrix->nrows - 1
15166 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15167 && !cursor_row_p (row))
15168 ++row;
15170 /* If within the scroll margin, scroll. Note that
15171 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15172 the next line would be drawn, and that
15173 this_scroll_margin can be zero. */
15174 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15175 || PT > MATRIX_ROW_END_CHARPOS (row)
15176 /* Line is completely visible last line in window
15177 and PT is to be set in the next line. */
15178 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15179 && PT == MATRIX_ROW_END_CHARPOS (row)
15180 && !row->ends_at_zv_p
15181 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15182 scroll_p = 1;
15184 else if (PT < w->last_point)
15186 /* Cursor has to be moved backward. Note that PT >=
15187 CHARPOS (startp) because of the outer if-statement. */
15188 while (!row->mode_line_p
15189 && (MATRIX_ROW_START_CHARPOS (row) > PT
15190 || (MATRIX_ROW_START_CHARPOS (row) == PT
15191 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15192 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15193 row > w->current_matrix->rows
15194 && (row-1)->ends_in_newline_from_string_p))))
15195 && (row->y > top_scroll_margin
15196 || CHARPOS (startp) == BEGV))
15198 eassert (row->enabled_p);
15199 --row;
15202 /* Consider the following case: Window starts at BEGV,
15203 there is invisible, intangible text at BEGV, so that
15204 display starts at some point START > BEGV. It can
15205 happen that we are called with PT somewhere between
15206 BEGV and START. Try to handle that case. */
15207 if (row < w->current_matrix->rows
15208 || row->mode_line_p)
15210 row = w->current_matrix->rows;
15211 if (row->mode_line_p)
15212 ++row;
15215 /* Due to newlines in overlay strings, we may have to
15216 skip forward over overlay strings. */
15217 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15218 && MATRIX_ROW_END_CHARPOS (row) == PT
15219 && !cursor_row_p (row))
15220 ++row;
15222 /* If within the scroll margin, scroll. */
15223 if (row->y < top_scroll_margin
15224 && CHARPOS (startp) != BEGV)
15225 scroll_p = 1;
15227 else
15229 /* Cursor did not move. So don't scroll even if cursor line
15230 is partially visible, as it was so before. */
15231 rc = CURSOR_MOVEMENT_SUCCESS;
15234 if (PT < MATRIX_ROW_START_CHARPOS (row)
15235 || PT > MATRIX_ROW_END_CHARPOS (row))
15237 /* if PT is not in the glyph row, give up. */
15238 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15239 must_scroll = 1;
15241 else if (rc != CURSOR_MOVEMENT_SUCCESS
15242 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15244 struct glyph_row *row1;
15246 /* If rows are bidi-reordered and point moved, back up
15247 until we find a row that does not belong to a
15248 continuation line. This is because we must consider
15249 all rows of a continued line as candidates for the
15250 new cursor positioning, since row start and end
15251 positions change non-linearly with vertical position
15252 in such rows. */
15253 /* FIXME: Revisit this when glyph ``spilling'' in
15254 continuation lines' rows is implemented for
15255 bidi-reordered rows. */
15256 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15257 MATRIX_ROW_CONTINUATION_LINE_P (row);
15258 --row)
15260 /* If we hit the beginning of the displayed portion
15261 without finding the first row of a continued
15262 line, give up. */
15263 if (row <= row1)
15265 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15266 break;
15268 eassert (row->enabled_p);
15271 if (must_scroll)
15273 else if (rc != CURSOR_MOVEMENT_SUCCESS
15274 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15275 /* Make sure this isn't a header line by any chance, since
15276 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15277 && !row->mode_line_p
15278 && make_cursor_line_fully_visible_p)
15280 if (PT == MATRIX_ROW_END_CHARPOS (row)
15281 && !row->ends_at_zv_p
15282 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15283 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15284 else if (row->height > window_box_height (w))
15286 /* If we end up in a partially visible line, let's
15287 make it fully visible, except when it's taller
15288 than the window, in which case we can't do much
15289 about it. */
15290 *scroll_step = 1;
15291 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15293 else
15295 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15296 if (!cursor_row_fully_visible_p (w, 0, 1))
15297 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15298 else
15299 rc = CURSOR_MOVEMENT_SUCCESS;
15302 else if (scroll_p)
15303 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15304 else if (rc != CURSOR_MOVEMENT_SUCCESS
15305 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15307 /* With bidi-reordered rows, there could be more than
15308 one candidate row whose start and end positions
15309 occlude point. We need to let set_cursor_from_row
15310 find the best candidate. */
15311 /* FIXME: Revisit this when glyph ``spilling'' in
15312 continuation lines' rows is implemented for
15313 bidi-reordered rows. */
15314 int rv = 0;
15318 int at_zv_p = 0, exact_match_p = 0;
15320 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15321 && PT <= MATRIX_ROW_END_CHARPOS (row)
15322 && cursor_row_p (row))
15323 rv |= set_cursor_from_row (w, row, w->current_matrix,
15324 0, 0, 0, 0);
15325 /* As soon as we've found the exact match for point,
15326 or the first suitable row whose ends_at_zv_p flag
15327 is set, we are done. */
15328 at_zv_p =
15329 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15330 if (rv && !at_zv_p
15331 && w->cursor.hpos >= 0
15332 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15333 w->cursor.vpos))
15335 struct glyph_row *candidate =
15336 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15337 struct glyph *g =
15338 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15339 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15341 exact_match_p =
15342 (BUFFERP (g->object) && g->charpos == PT)
15343 || (INTEGERP (g->object)
15344 && (g->charpos == PT
15345 || (g->charpos == 0 && endpos - 1 == PT)));
15347 if (rv && (at_zv_p || exact_match_p))
15349 rc = CURSOR_MOVEMENT_SUCCESS;
15350 break;
15352 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15353 break;
15354 ++row;
15356 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15357 || row->continued_p)
15358 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15359 || (MATRIX_ROW_START_CHARPOS (row) == PT
15360 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15361 /* If we didn't find any candidate rows, or exited the
15362 loop before all the candidates were examined, signal
15363 to the caller that this method failed. */
15364 if (rc != CURSOR_MOVEMENT_SUCCESS
15365 && !(rv
15366 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15367 && !row->continued_p))
15368 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15369 else if (rv)
15370 rc = CURSOR_MOVEMENT_SUCCESS;
15372 else
15376 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15378 rc = CURSOR_MOVEMENT_SUCCESS;
15379 break;
15381 ++row;
15383 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15384 && MATRIX_ROW_START_CHARPOS (row) == PT
15385 && cursor_row_p (row));
15390 return rc;
15393 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15394 static
15395 #endif
15396 void
15397 set_vertical_scroll_bar (struct window *w)
15399 ptrdiff_t start, end, whole;
15401 /* Calculate the start and end positions for the current window.
15402 At some point, it would be nice to choose between scrollbars
15403 which reflect the whole buffer size, with special markers
15404 indicating narrowing, and scrollbars which reflect only the
15405 visible region.
15407 Note that mini-buffers sometimes aren't displaying any text. */
15408 if (!MINI_WINDOW_P (w)
15409 || (w == XWINDOW (minibuf_window)
15410 && NILP (echo_area_buffer[0])))
15412 struct buffer *buf = XBUFFER (w->buffer);
15413 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15414 start = marker_position (w->start) - BUF_BEGV (buf);
15415 /* I don't think this is guaranteed to be right. For the
15416 moment, we'll pretend it is. */
15417 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15419 if (end < start)
15420 end = start;
15421 if (whole < (end - start))
15422 whole = end - start;
15424 else
15425 start = end = whole = 0;
15427 /* Indicate what this scroll bar ought to be displaying now. */
15428 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15429 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15430 (w, end - start, whole, start);
15434 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15435 selected_window is redisplayed.
15437 We can return without actually redisplaying the window if
15438 fonts_changed_p. In that case, redisplay_internal will
15439 retry. */
15441 static void
15442 redisplay_window (Lisp_Object window, int just_this_one_p)
15444 struct window *w = XWINDOW (window);
15445 struct frame *f = XFRAME (w->frame);
15446 struct buffer *buffer = XBUFFER (w->buffer);
15447 struct buffer *old = current_buffer;
15448 struct text_pos lpoint, opoint, startp;
15449 int update_mode_line;
15450 int tem;
15451 struct it it;
15452 /* Record it now because it's overwritten. */
15453 int current_matrix_up_to_date_p = 0;
15454 int used_current_matrix_p = 0;
15455 /* This is less strict than current_matrix_up_to_date_p.
15456 It indicates that the buffer contents and narrowing are unchanged. */
15457 int buffer_unchanged_p = 0;
15458 int temp_scroll_step = 0;
15459 ptrdiff_t count = SPECPDL_INDEX ();
15460 int rc;
15461 int centering_position = -1;
15462 int last_line_misfit = 0;
15463 ptrdiff_t beg_unchanged, end_unchanged;
15465 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15466 opoint = lpoint;
15468 /* W must be a leaf window here. */
15469 eassert (!NILP (w->buffer));
15470 #ifdef GLYPH_DEBUG
15471 *w->desired_matrix->method = 0;
15472 #endif
15474 restart:
15475 reconsider_clip_changes (w, buffer);
15477 /* Has the mode line to be updated? */
15478 update_mode_line = (w->update_mode_line
15479 || update_mode_lines
15480 || buffer->clip_changed
15481 || buffer->prevent_redisplay_optimizations_p);
15483 if (MINI_WINDOW_P (w))
15485 if (w == XWINDOW (echo_area_window)
15486 && !NILP (echo_area_buffer[0]))
15488 if (update_mode_line)
15489 /* We may have to update a tty frame's menu bar or a
15490 tool-bar. Example `M-x C-h C-h C-g'. */
15491 goto finish_menu_bars;
15492 else
15493 /* We've already displayed the echo area glyphs in this window. */
15494 goto finish_scroll_bars;
15496 else if ((w != XWINDOW (minibuf_window)
15497 || minibuf_level == 0)
15498 /* When buffer is nonempty, redisplay window normally. */
15499 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15500 /* Quail displays non-mini buffers in minibuffer window.
15501 In that case, redisplay the window normally. */
15502 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15504 /* W is a mini-buffer window, but it's not active, so clear
15505 it. */
15506 int yb = window_text_bottom_y (w);
15507 struct glyph_row *row;
15508 int y;
15510 for (y = 0, row = w->desired_matrix->rows;
15511 y < yb;
15512 y += row->height, ++row)
15513 blank_row (w, row, y);
15514 goto finish_scroll_bars;
15517 clear_glyph_matrix (w->desired_matrix);
15520 /* Otherwise set up data on this window; select its buffer and point
15521 value. */
15522 /* Really select the buffer, for the sake of buffer-local
15523 variables. */
15524 set_buffer_internal_1 (XBUFFER (w->buffer));
15526 current_matrix_up_to_date_p
15527 = (!NILP (w->window_end_valid)
15528 && !current_buffer->clip_changed
15529 && !current_buffer->prevent_redisplay_optimizations_p
15530 && w->last_modified >= MODIFF
15531 && w->last_overlay_modified >= OVERLAY_MODIFF);
15533 /* Run the window-bottom-change-functions
15534 if it is possible that the text on the screen has changed
15535 (either due to modification of the text, or any other reason). */
15536 if (!current_matrix_up_to_date_p
15537 && !NILP (Vwindow_text_change_functions))
15539 safe_run_hooks (Qwindow_text_change_functions);
15540 goto restart;
15543 beg_unchanged = BEG_UNCHANGED;
15544 end_unchanged = END_UNCHANGED;
15546 SET_TEXT_POS (opoint, PT, PT_BYTE);
15548 specbind (Qinhibit_point_motion_hooks, Qt);
15550 buffer_unchanged_p
15551 = (!NILP (w->window_end_valid)
15552 && !current_buffer->clip_changed
15553 && w->last_modified >= MODIFF
15554 && w->last_overlay_modified >= OVERLAY_MODIFF);
15556 /* When windows_or_buffers_changed is non-zero, we can't rely on
15557 the window end being valid, so set it to nil there. */
15558 if (windows_or_buffers_changed)
15560 /* If window starts on a continuation line, maybe adjust the
15561 window start in case the window's width changed. */
15562 if (XMARKER (w->start)->buffer == current_buffer)
15563 compute_window_start_on_continuation_line (w);
15565 wset_window_end_valid (w, Qnil);
15568 /* Some sanity checks. */
15569 CHECK_WINDOW_END (w);
15570 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15571 emacs_abort ();
15572 if (BYTEPOS (opoint) < CHARPOS (opoint))
15573 emacs_abort ();
15575 /* If %c is in mode line, update it if needed. */
15576 if (!NILP (w->column_number_displayed)
15577 /* This alternative quickly identifies a common case
15578 where no change is needed. */
15579 && !(PT == w->last_point
15580 && w->last_modified >= MODIFF
15581 && w->last_overlay_modified >= OVERLAY_MODIFF)
15582 && (XFASTINT (w->column_number_displayed) != current_column ()))
15583 update_mode_line = 1;
15585 /* Count number of windows showing the selected buffer. An indirect
15586 buffer counts as its base buffer. */
15587 if (!just_this_one_p)
15589 struct buffer *current_base, *window_base;
15590 current_base = current_buffer;
15591 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15592 if (current_base->base_buffer)
15593 current_base = current_base->base_buffer;
15594 if (window_base->base_buffer)
15595 window_base = window_base->base_buffer;
15596 if (current_base == window_base)
15597 buffer_shared++;
15600 /* Point refers normally to the selected window. For any other
15601 window, set up appropriate value. */
15602 if (!EQ (window, selected_window))
15604 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15605 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15606 if (new_pt < BEGV)
15608 new_pt = BEGV;
15609 new_pt_byte = BEGV_BYTE;
15610 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15612 else if (new_pt > (ZV - 1))
15614 new_pt = ZV;
15615 new_pt_byte = ZV_BYTE;
15616 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15619 /* We don't use SET_PT so that the point-motion hooks don't run. */
15620 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15623 /* If any of the character widths specified in the display table
15624 have changed, invalidate the width run cache. It's true that
15625 this may be a bit late to catch such changes, but the rest of
15626 redisplay goes (non-fatally) haywire when the display table is
15627 changed, so why should we worry about doing any better? */
15628 if (current_buffer->width_run_cache)
15630 struct Lisp_Char_Table *disptab = buffer_display_table ();
15632 if (! disptab_matches_widthtab
15633 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15635 invalidate_region_cache (current_buffer,
15636 current_buffer->width_run_cache,
15637 BEG, Z);
15638 recompute_width_table (current_buffer, disptab);
15642 /* If window-start is screwed up, choose a new one. */
15643 if (XMARKER (w->start)->buffer != current_buffer)
15644 goto recenter;
15646 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15648 /* If someone specified a new starting point but did not insist,
15649 check whether it can be used. */
15650 if (w->optional_new_start
15651 && CHARPOS (startp) >= BEGV
15652 && CHARPOS (startp) <= ZV)
15654 w->optional_new_start = 0;
15655 start_display (&it, w, startp);
15656 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15657 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15658 if (IT_CHARPOS (it) == PT)
15659 w->force_start = 1;
15660 /* IT may overshoot PT if text at PT is invisible. */
15661 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15662 w->force_start = 1;
15665 force_start:
15667 /* Handle case where place to start displaying has been specified,
15668 unless the specified location is outside the accessible range. */
15669 if (w->force_start || w->frozen_window_start_p)
15671 /* We set this later on if we have to adjust point. */
15672 int new_vpos = -1;
15674 w->force_start = 0;
15675 w->vscroll = 0;
15676 wset_window_end_valid (w, Qnil);
15678 /* Forget any recorded base line for line number display. */
15679 if (!buffer_unchanged_p)
15680 wset_base_line_number (w, Qnil);
15682 /* Redisplay the mode line. Select the buffer properly for that.
15683 Also, run the hook window-scroll-functions
15684 because we have scrolled. */
15685 /* Note, we do this after clearing force_start because
15686 if there's an error, it is better to forget about force_start
15687 than to get into an infinite loop calling the hook functions
15688 and having them get more errors. */
15689 if (!update_mode_line
15690 || ! NILP (Vwindow_scroll_functions))
15692 update_mode_line = 1;
15693 w->update_mode_line = 1;
15694 startp = run_window_scroll_functions (window, startp);
15697 w->last_modified = 0;
15698 w->last_overlay_modified = 0;
15699 if (CHARPOS (startp) < BEGV)
15700 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15701 else if (CHARPOS (startp) > ZV)
15702 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15704 /* Redisplay, then check if cursor has been set during the
15705 redisplay. Give up if new fonts were loaded. */
15706 /* We used to issue a CHECK_MARGINS argument to try_window here,
15707 but this causes scrolling to fail when point begins inside
15708 the scroll margin (bug#148) -- cyd */
15709 if (!try_window (window, startp, 0))
15711 w->force_start = 1;
15712 clear_glyph_matrix (w->desired_matrix);
15713 goto need_larger_matrices;
15716 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15718 /* If point does not appear, try to move point so it does
15719 appear. The desired matrix has been built above, so we
15720 can use it here. */
15721 new_vpos = window_box_height (w) / 2;
15724 if (!cursor_row_fully_visible_p (w, 0, 0))
15726 /* Point does appear, but on a line partly visible at end of window.
15727 Move it back to a fully-visible line. */
15728 new_vpos = window_box_height (w);
15731 /* If we need to move point for either of the above reasons,
15732 now actually do it. */
15733 if (new_vpos >= 0)
15735 struct glyph_row *row;
15737 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15738 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15739 ++row;
15741 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15742 MATRIX_ROW_START_BYTEPOS (row));
15744 if (w != XWINDOW (selected_window))
15745 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15746 else if (current_buffer == old)
15747 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15749 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15751 /* If we are highlighting the region, then we just changed
15752 the region, so redisplay to show it. */
15753 if (!NILP (Vtransient_mark_mode)
15754 && !NILP (BVAR (current_buffer, mark_active)))
15756 clear_glyph_matrix (w->desired_matrix);
15757 if (!try_window (window, startp, 0))
15758 goto need_larger_matrices;
15762 #ifdef GLYPH_DEBUG
15763 debug_method_add (w, "forced window start");
15764 #endif
15765 goto done;
15768 /* Handle case where text has not changed, only point, and it has
15769 not moved off the frame, and we are not retrying after hscroll.
15770 (current_matrix_up_to_date_p is nonzero when retrying.) */
15771 if (current_matrix_up_to_date_p
15772 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15773 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15775 switch (rc)
15777 case CURSOR_MOVEMENT_SUCCESS:
15778 used_current_matrix_p = 1;
15779 goto done;
15781 case CURSOR_MOVEMENT_MUST_SCROLL:
15782 goto try_to_scroll;
15784 default:
15785 emacs_abort ();
15788 /* If current starting point was originally the beginning of a line
15789 but no longer is, find a new starting point. */
15790 else if (w->start_at_line_beg
15791 && !(CHARPOS (startp) <= BEGV
15792 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15794 #ifdef GLYPH_DEBUG
15795 debug_method_add (w, "recenter 1");
15796 #endif
15797 goto recenter;
15800 /* Try scrolling with try_window_id. Value is > 0 if update has
15801 been done, it is -1 if we know that the same window start will
15802 not work. It is 0 if unsuccessful for some other reason. */
15803 else if ((tem = try_window_id (w)) != 0)
15805 #ifdef GLYPH_DEBUG
15806 debug_method_add (w, "try_window_id %d", tem);
15807 #endif
15809 if (fonts_changed_p)
15810 goto need_larger_matrices;
15811 if (tem > 0)
15812 goto done;
15814 /* Otherwise try_window_id has returned -1 which means that we
15815 don't want the alternative below this comment to execute. */
15817 else if (CHARPOS (startp) >= BEGV
15818 && CHARPOS (startp) <= ZV
15819 && PT >= CHARPOS (startp)
15820 && (CHARPOS (startp) < ZV
15821 /* Avoid starting at end of buffer. */
15822 || CHARPOS (startp) == BEGV
15823 || (w->last_modified >= MODIFF
15824 && w->last_overlay_modified >= OVERLAY_MODIFF)))
15826 int d1, d2, d3, d4, d5, d6;
15828 /* If first window line is a continuation line, and window start
15829 is inside the modified region, but the first change is before
15830 current window start, we must select a new window start.
15832 However, if this is the result of a down-mouse event (e.g. by
15833 extending the mouse-drag-overlay), we don't want to select a
15834 new window start, since that would change the position under
15835 the mouse, resulting in an unwanted mouse-movement rather
15836 than a simple mouse-click. */
15837 if (!w->start_at_line_beg
15838 && NILP (do_mouse_tracking)
15839 && CHARPOS (startp) > BEGV
15840 && CHARPOS (startp) > BEG + beg_unchanged
15841 && CHARPOS (startp) <= Z - end_unchanged
15842 /* Even if w->start_at_line_beg is nil, a new window may
15843 start at a line_beg, since that's how set_buffer_window
15844 sets it. So, we need to check the return value of
15845 compute_window_start_on_continuation_line. (See also
15846 bug#197). */
15847 && XMARKER (w->start)->buffer == current_buffer
15848 && compute_window_start_on_continuation_line (w)
15849 /* It doesn't make sense to force the window start like we
15850 do at label force_start if it is already known that point
15851 will not be visible in the resulting window, because
15852 doing so will move point from its correct position
15853 instead of scrolling the window to bring point into view.
15854 See bug#9324. */
15855 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15857 w->force_start = 1;
15858 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15859 goto force_start;
15862 #ifdef GLYPH_DEBUG
15863 debug_method_add (w, "same window start");
15864 #endif
15866 /* Try to redisplay starting at same place as before.
15867 If point has not moved off frame, accept the results. */
15868 if (!current_matrix_up_to_date_p
15869 /* Don't use try_window_reusing_current_matrix in this case
15870 because a window scroll function can have changed the
15871 buffer. */
15872 || !NILP (Vwindow_scroll_functions)
15873 || MINI_WINDOW_P (w)
15874 || !(used_current_matrix_p
15875 = try_window_reusing_current_matrix (w)))
15877 IF_DEBUG (debug_method_add (w, "1"));
15878 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15879 /* -1 means we need to scroll.
15880 0 means we need new matrices, but fonts_changed_p
15881 is set in that case, so we will detect it below. */
15882 goto try_to_scroll;
15885 if (fonts_changed_p)
15886 goto need_larger_matrices;
15888 if (w->cursor.vpos >= 0)
15890 if (!just_this_one_p
15891 || current_buffer->clip_changed
15892 || BEG_UNCHANGED < CHARPOS (startp))
15893 /* Forget any recorded base line for line number display. */
15894 wset_base_line_number (w, Qnil);
15896 if (!cursor_row_fully_visible_p (w, 1, 0))
15898 clear_glyph_matrix (w->desired_matrix);
15899 last_line_misfit = 1;
15901 /* Drop through and scroll. */
15902 else
15903 goto done;
15905 else
15906 clear_glyph_matrix (w->desired_matrix);
15909 try_to_scroll:
15911 w->last_modified = 0;
15912 w->last_overlay_modified = 0;
15914 /* Redisplay the mode line. Select the buffer properly for that. */
15915 if (!update_mode_line)
15917 update_mode_line = 1;
15918 w->update_mode_line = 1;
15921 /* Try to scroll by specified few lines. */
15922 if ((scroll_conservatively
15923 || emacs_scroll_step
15924 || temp_scroll_step
15925 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15926 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15927 && CHARPOS (startp) >= BEGV
15928 && CHARPOS (startp) <= ZV)
15930 /* The function returns -1 if new fonts were loaded, 1 if
15931 successful, 0 if not successful. */
15932 int ss = try_scrolling (window, just_this_one_p,
15933 scroll_conservatively,
15934 emacs_scroll_step,
15935 temp_scroll_step, last_line_misfit);
15936 switch (ss)
15938 case SCROLLING_SUCCESS:
15939 goto done;
15941 case SCROLLING_NEED_LARGER_MATRICES:
15942 goto need_larger_matrices;
15944 case SCROLLING_FAILED:
15945 break;
15947 default:
15948 emacs_abort ();
15952 /* Finally, just choose a place to start which positions point
15953 according to user preferences. */
15955 recenter:
15957 #ifdef GLYPH_DEBUG
15958 debug_method_add (w, "recenter");
15959 #endif
15961 /* w->vscroll = 0; */
15963 /* Forget any previously recorded base line for line number display. */
15964 if (!buffer_unchanged_p)
15965 wset_base_line_number (w, Qnil);
15967 /* Determine the window start relative to point. */
15968 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15969 it.current_y = it.last_visible_y;
15970 if (centering_position < 0)
15972 int margin =
15973 scroll_margin > 0
15974 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15975 : 0;
15976 ptrdiff_t margin_pos = CHARPOS (startp);
15977 Lisp_Object aggressive;
15978 int scrolling_up;
15980 /* If there is a scroll margin at the top of the window, find
15981 its character position. */
15982 if (margin
15983 /* Cannot call start_display if startp is not in the
15984 accessible region of the buffer. This can happen when we
15985 have just switched to a different buffer and/or changed
15986 its restriction. In that case, startp is initialized to
15987 the character position 1 (BEGV) because we did not yet
15988 have chance to display the buffer even once. */
15989 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15991 struct it it1;
15992 void *it1data = NULL;
15994 SAVE_IT (it1, it, it1data);
15995 start_display (&it1, w, startp);
15996 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15997 margin_pos = IT_CHARPOS (it1);
15998 RESTORE_IT (&it, &it, it1data);
16000 scrolling_up = PT > margin_pos;
16001 aggressive =
16002 scrolling_up
16003 ? BVAR (current_buffer, scroll_up_aggressively)
16004 : BVAR (current_buffer, scroll_down_aggressively);
16006 if (!MINI_WINDOW_P (w)
16007 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16009 int pt_offset = 0;
16011 /* Setting scroll-conservatively overrides
16012 scroll-*-aggressively. */
16013 if (!scroll_conservatively && NUMBERP (aggressive))
16015 double float_amount = XFLOATINT (aggressive);
16017 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16018 if (pt_offset == 0 && float_amount > 0)
16019 pt_offset = 1;
16020 if (pt_offset && margin > 0)
16021 margin -= 1;
16023 /* Compute how much to move the window start backward from
16024 point so that point will be displayed where the user
16025 wants it. */
16026 if (scrolling_up)
16028 centering_position = it.last_visible_y;
16029 if (pt_offset)
16030 centering_position -= pt_offset;
16031 centering_position -=
16032 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16033 + WINDOW_HEADER_LINE_HEIGHT (w);
16034 /* Don't let point enter the scroll margin near top of
16035 the window. */
16036 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16037 centering_position = margin * FRAME_LINE_HEIGHT (f);
16039 else
16040 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16042 else
16043 /* Set the window start half the height of the window backward
16044 from point. */
16045 centering_position = window_box_height (w) / 2;
16047 move_it_vertically_backward (&it, centering_position);
16049 eassert (IT_CHARPOS (it) >= BEGV);
16051 /* The function move_it_vertically_backward may move over more
16052 than the specified y-distance. If it->w is small, e.g. a
16053 mini-buffer window, we may end up in front of the window's
16054 display area. Start displaying at the start of the line
16055 containing PT in this case. */
16056 if (it.current_y <= 0)
16058 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16059 move_it_vertically_backward (&it, 0);
16060 it.current_y = 0;
16063 it.current_x = it.hpos = 0;
16065 /* Set the window start position here explicitly, to avoid an
16066 infinite loop in case the functions in window-scroll-functions
16067 get errors. */
16068 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16070 /* Run scroll hooks. */
16071 startp = run_window_scroll_functions (window, it.current.pos);
16073 /* Redisplay the window. */
16074 if (!current_matrix_up_to_date_p
16075 || windows_or_buffers_changed
16076 || cursor_type_changed
16077 /* Don't use try_window_reusing_current_matrix in this case
16078 because it can have changed the buffer. */
16079 || !NILP (Vwindow_scroll_functions)
16080 || !just_this_one_p
16081 || MINI_WINDOW_P (w)
16082 || !(used_current_matrix_p
16083 = try_window_reusing_current_matrix (w)))
16084 try_window (window, startp, 0);
16086 /* If new fonts have been loaded (due to fontsets), give up. We
16087 have to start a new redisplay since we need to re-adjust glyph
16088 matrices. */
16089 if (fonts_changed_p)
16090 goto need_larger_matrices;
16092 /* If cursor did not appear assume that the middle of the window is
16093 in the first line of the window. Do it again with the next line.
16094 (Imagine a window of height 100, displaying two lines of height
16095 60. Moving back 50 from it->last_visible_y will end in the first
16096 line.) */
16097 if (w->cursor.vpos < 0)
16099 if (!NILP (w->window_end_valid)
16100 && PT >= Z - XFASTINT (w->window_end_pos))
16102 clear_glyph_matrix (w->desired_matrix);
16103 move_it_by_lines (&it, 1);
16104 try_window (window, it.current.pos, 0);
16106 else if (PT < IT_CHARPOS (it))
16108 clear_glyph_matrix (w->desired_matrix);
16109 move_it_by_lines (&it, -1);
16110 try_window (window, it.current.pos, 0);
16112 else
16114 /* Not much we can do about it. */
16118 /* Consider the following case: Window starts at BEGV, there is
16119 invisible, intangible text at BEGV, so that display starts at
16120 some point START > BEGV. It can happen that we are called with
16121 PT somewhere between BEGV and START. Try to handle that case. */
16122 if (w->cursor.vpos < 0)
16124 struct glyph_row *row = w->current_matrix->rows;
16125 if (row->mode_line_p)
16126 ++row;
16127 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16130 if (!cursor_row_fully_visible_p (w, 0, 0))
16132 /* If vscroll is enabled, disable it and try again. */
16133 if (w->vscroll)
16135 w->vscroll = 0;
16136 clear_glyph_matrix (w->desired_matrix);
16137 goto recenter;
16140 /* Users who set scroll-conservatively to a large number want
16141 point just above/below the scroll margin. If we ended up
16142 with point's row partially visible, move the window start to
16143 make that row fully visible and out of the margin. */
16144 if (scroll_conservatively > SCROLL_LIMIT)
16146 int margin =
16147 scroll_margin > 0
16148 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16149 : 0;
16150 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16152 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16153 clear_glyph_matrix (w->desired_matrix);
16154 if (1 == try_window (window, it.current.pos,
16155 TRY_WINDOW_CHECK_MARGINS))
16156 goto done;
16159 /* If centering point failed to make the whole line visible,
16160 put point at the top instead. That has to make the whole line
16161 visible, if it can be done. */
16162 if (centering_position == 0)
16163 goto done;
16165 clear_glyph_matrix (w->desired_matrix);
16166 centering_position = 0;
16167 goto recenter;
16170 done:
16172 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16173 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16174 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16176 /* Display the mode line, if we must. */
16177 if ((update_mode_line
16178 /* If window not full width, must redo its mode line
16179 if (a) the window to its side is being redone and
16180 (b) we do a frame-based redisplay. This is a consequence
16181 of how inverted lines are drawn in frame-based redisplay. */
16182 || (!just_this_one_p
16183 && !FRAME_WINDOW_P (f)
16184 && !WINDOW_FULL_WIDTH_P (w))
16185 /* Line number to display. */
16186 || INTEGERP (w->base_line_pos)
16187 /* Column number is displayed and different from the one displayed. */
16188 || (!NILP (w->column_number_displayed)
16189 && (XFASTINT (w->column_number_displayed) != current_column ())))
16190 /* This means that the window has a mode line. */
16191 && (WINDOW_WANTS_MODELINE_P (w)
16192 || WINDOW_WANTS_HEADER_LINE_P (w)))
16194 display_mode_lines (w);
16196 /* If mode line height has changed, arrange for a thorough
16197 immediate redisplay using the correct mode line height. */
16198 if (WINDOW_WANTS_MODELINE_P (w)
16199 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16201 fonts_changed_p = 1;
16202 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16203 = DESIRED_MODE_LINE_HEIGHT (w);
16206 /* If header line height has changed, arrange for a thorough
16207 immediate redisplay using the correct header line height. */
16208 if (WINDOW_WANTS_HEADER_LINE_P (w)
16209 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16211 fonts_changed_p = 1;
16212 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16213 = DESIRED_HEADER_LINE_HEIGHT (w);
16216 if (fonts_changed_p)
16217 goto need_larger_matrices;
16220 if (!line_number_displayed
16221 && !BUFFERP (w->base_line_pos))
16223 wset_base_line_pos (w, Qnil);
16224 wset_base_line_number (w, Qnil);
16227 finish_menu_bars:
16229 /* When we reach a frame's selected window, redo the frame's menu bar. */
16230 if (update_mode_line
16231 && EQ (FRAME_SELECTED_WINDOW (f), window))
16233 int redisplay_menu_p = 0;
16235 if (FRAME_WINDOW_P (f))
16237 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16238 || defined (HAVE_NS) || defined (USE_GTK)
16239 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16240 #else
16241 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16242 #endif
16244 else
16245 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16247 if (redisplay_menu_p)
16248 display_menu_bar (w);
16250 #ifdef HAVE_WINDOW_SYSTEM
16251 if (FRAME_WINDOW_P (f))
16253 #if defined (USE_GTK) || defined (HAVE_NS)
16254 if (FRAME_EXTERNAL_TOOL_BAR (f))
16255 redisplay_tool_bar (f);
16256 #else
16257 if (WINDOWP (f->tool_bar_window)
16258 && (FRAME_TOOL_BAR_LINES (f) > 0
16259 || !NILP (Vauto_resize_tool_bars))
16260 && redisplay_tool_bar (f))
16261 ignore_mouse_drag_p = 1;
16262 #endif
16264 #endif
16267 #ifdef HAVE_WINDOW_SYSTEM
16268 if (FRAME_WINDOW_P (f)
16269 && update_window_fringes (w, (just_this_one_p
16270 || (!used_current_matrix_p && !overlay_arrow_seen)
16271 || w->pseudo_window_p)))
16273 update_begin (f);
16274 block_input ();
16275 if (draw_window_fringes (w, 1))
16276 x_draw_vertical_border (w);
16277 unblock_input ();
16278 update_end (f);
16280 #endif /* HAVE_WINDOW_SYSTEM */
16282 /* We go to this label, with fonts_changed_p set,
16283 if it is necessary to try again using larger glyph matrices.
16284 We have to redeem the scroll bar even in this case,
16285 because the loop in redisplay_internal expects that. */
16286 need_larger_matrices:
16288 finish_scroll_bars:
16290 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16292 /* Set the thumb's position and size. */
16293 set_vertical_scroll_bar (w);
16295 /* Note that we actually used the scroll bar attached to this
16296 window, so it shouldn't be deleted at the end of redisplay. */
16297 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16298 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16301 /* Restore current_buffer and value of point in it. The window
16302 update may have changed the buffer, so first make sure `opoint'
16303 is still valid (Bug#6177). */
16304 if (CHARPOS (opoint) < BEGV)
16305 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16306 else if (CHARPOS (opoint) > ZV)
16307 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16308 else
16309 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16311 set_buffer_internal_1 (old);
16312 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16313 shorter. This can be caused by log truncation in *Messages*. */
16314 if (CHARPOS (lpoint) <= ZV)
16315 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16317 unbind_to (count, Qnil);
16321 /* Build the complete desired matrix of WINDOW with a window start
16322 buffer position POS.
16324 Value is 1 if successful. It is zero if fonts were loaded during
16325 redisplay which makes re-adjusting glyph matrices necessary, and -1
16326 if point would appear in the scroll margins.
16327 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16328 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16329 set in FLAGS.) */
16332 try_window (Lisp_Object window, struct text_pos pos, int flags)
16334 struct window *w = XWINDOW (window);
16335 struct it it;
16336 struct glyph_row *last_text_row = NULL;
16337 struct frame *f = XFRAME (w->frame);
16339 /* Make POS the new window start. */
16340 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16342 /* Mark cursor position as unknown. No overlay arrow seen. */
16343 w->cursor.vpos = -1;
16344 overlay_arrow_seen = 0;
16346 /* Initialize iterator and info to start at POS. */
16347 start_display (&it, w, pos);
16349 /* Display all lines of W. */
16350 while (it.current_y < it.last_visible_y)
16352 if (display_line (&it))
16353 last_text_row = it.glyph_row - 1;
16354 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16355 return 0;
16358 /* Don't let the cursor end in the scroll margins. */
16359 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16360 && !MINI_WINDOW_P (w))
16362 int this_scroll_margin;
16364 if (scroll_margin > 0)
16366 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16367 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16369 else
16370 this_scroll_margin = 0;
16372 if ((w->cursor.y >= 0 /* not vscrolled */
16373 && w->cursor.y < this_scroll_margin
16374 && CHARPOS (pos) > BEGV
16375 && IT_CHARPOS (it) < ZV)
16376 /* rms: considering make_cursor_line_fully_visible_p here
16377 seems to give wrong results. We don't want to recenter
16378 when the last line is partly visible, we want to allow
16379 that case to be handled in the usual way. */
16380 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16382 w->cursor.vpos = -1;
16383 clear_glyph_matrix (w->desired_matrix);
16384 return -1;
16388 /* If bottom moved off end of frame, change mode line percentage. */
16389 if (XFASTINT (w->window_end_pos) <= 0
16390 && Z != IT_CHARPOS (it))
16391 w->update_mode_line = 1;
16393 /* Set window_end_pos to the offset of the last character displayed
16394 on the window from the end of current_buffer. Set
16395 window_end_vpos to its row number. */
16396 if (last_text_row)
16398 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16399 w->window_end_bytepos
16400 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16401 wset_window_end_pos
16402 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16403 wset_window_end_vpos
16404 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16405 eassert
16406 (MATRIX_ROW (w->desired_matrix,
16407 XFASTINT (w->window_end_vpos))->displays_text_p);
16409 else
16411 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16412 wset_window_end_pos (w, make_number (Z - ZV));
16413 wset_window_end_vpos (w, make_number (0));
16416 /* But that is not valid info until redisplay finishes. */
16417 wset_window_end_valid (w, Qnil);
16418 return 1;
16423 /************************************************************************
16424 Window redisplay reusing current matrix when buffer has not changed
16425 ************************************************************************/
16427 /* Try redisplay of window W showing an unchanged buffer with a
16428 different window start than the last time it was displayed by
16429 reusing its current matrix. Value is non-zero if successful.
16430 W->start is the new window start. */
16432 static int
16433 try_window_reusing_current_matrix (struct window *w)
16435 struct frame *f = XFRAME (w->frame);
16436 struct glyph_row *bottom_row;
16437 struct it it;
16438 struct run run;
16439 struct text_pos start, new_start;
16440 int nrows_scrolled, i;
16441 struct glyph_row *last_text_row;
16442 struct glyph_row *last_reused_text_row;
16443 struct glyph_row *start_row;
16444 int start_vpos, min_y, max_y;
16446 #ifdef GLYPH_DEBUG
16447 if (inhibit_try_window_reusing)
16448 return 0;
16449 #endif
16451 if (/* This function doesn't handle terminal frames. */
16452 !FRAME_WINDOW_P (f)
16453 /* Don't try to reuse the display if windows have been split
16454 or such. */
16455 || windows_or_buffers_changed
16456 || cursor_type_changed)
16457 return 0;
16459 /* Can't do this if region may have changed. */
16460 if ((!NILP (Vtransient_mark_mode)
16461 && !NILP (BVAR (current_buffer, mark_active)))
16462 || !NILP (w->region_showing)
16463 || !NILP (Vshow_trailing_whitespace))
16464 return 0;
16466 /* If top-line visibility has changed, give up. */
16467 if (WINDOW_WANTS_HEADER_LINE_P (w)
16468 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16469 return 0;
16471 /* Give up if old or new display is scrolled vertically. We could
16472 make this function handle this, but right now it doesn't. */
16473 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16474 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16475 return 0;
16477 /* The variable new_start now holds the new window start. The old
16478 start `start' can be determined from the current matrix. */
16479 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16480 start = start_row->minpos;
16481 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16483 /* Clear the desired matrix for the display below. */
16484 clear_glyph_matrix (w->desired_matrix);
16486 if (CHARPOS (new_start) <= CHARPOS (start))
16488 /* Don't use this method if the display starts with an ellipsis
16489 displayed for invisible text. It's not easy to handle that case
16490 below, and it's certainly not worth the effort since this is
16491 not a frequent case. */
16492 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16493 return 0;
16495 IF_DEBUG (debug_method_add (w, "twu1"));
16497 /* Display up to a row that can be reused. The variable
16498 last_text_row is set to the last row displayed that displays
16499 text. Note that it.vpos == 0 if or if not there is a
16500 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16501 start_display (&it, w, new_start);
16502 w->cursor.vpos = -1;
16503 last_text_row = last_reused_text_row = NULL;
16505 while (it.current_y < it.last_visible_y
16506 && !fonts_changed_p)
16508 /* If we have reached into the characters in the START row,
16509 that means the line boundaries have changed. So we
16510 can't start copying with the row START. Maybe it will
16511 work to start copying with the following row. */
16512 while (IT_CHARPOS (it) > CHARPOS (start))
16514 /* Advance to the next row as the "start". */
16515 start_row++;
16516 start = start_row->minpos;
16517 /* If there are no more rows to try, or just one, give up. */
16518 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16519 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16520 || CHARPOS (start) == ZV)
16522 clear_glyph_matrix (w->desired_matrix);
16523 return 0;
16526 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16528 /* If we have reached alignment, we can copy the rest of the
16529 rows. */
16530 if (IT_CHARPOS (it) == CHARPOS (start)
16531 /* Don't accept "alignment" inside a display vector,
16532 since start_row could have started in the middle of
16533 that same display vector (thus their character
16534 positions match), and we have no way of telling if
16535 that is the case. */
16536 && it.current.dpvec_index < 0)
16537 break;
16539 if (display_line (&it))
16540 last_text_row = it.glyph_row - 1;
16544 /* A value of current_y < last_visible_y means that we stopped
16545 at the previous window start, which in turn means that we
16546 have at least one reusable row. */
16547 if (it.current_y < it.last_visible_y)
16549 struct glyph_row *row;
16551 /* IT.vpos always starts from 0; it counts text lines. */
16552 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16554 /* Find PT if not already found in the lines displayed. */
16555 if (w->cursor.vpos < 0)
16557 int dy = it.current_y - start_row->y;
16559 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16560 row = row_containing_pos (w, PT, row, NULL, dy);
16561 if (row)
16562 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16563 dy, nrows_scrolled);
16564 else
16566 clear_glyph_matrix (w->desired_matrix);
16567 return 0;
16571 /* Scroll the display. Do it before the current matrix is
16572 changed. The problem here is that update has not yet
16573 run, i.e. part of the current matrix is not up to date.
16574 scroll_run_hook will clear the cursor, and use the
16575 current matrix to get the height of the row the cursor is
16576 in. */
16577 run.current_y = start_row->y;
16578 run.desired_y = it.current_y;
16579 run.height = it.last_visible_y - it.current_y;
16581 if (run.height > 0 && run.current_y != run.desired_y)
16583 update_begin (f);
16584 FRAME_RIF (f)->update_window_begin_hook (w);
16585 FRAME_RIF (f)->clear_window_mouse_face (w);
16586 FRAME_RIF (f)->scroll_run_hook (w, &run);
16587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16588 update_end (f);
16591 /* Shift current matrix down by nrows_scrolled lines. */
16592 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16593 rotate_matrix (w->current_matrix,
16594 start_vpos,
16595 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16596 nrows_scrolled);
16598 /* Disable lines that must be updated. */
16599 for (i = 0; i < nrows_scrolled; ++i)
16600 (start_row + i)->enabled_p = 0;
16602 /* Re-compute Y positions. */
16603 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16604 max_y = it.last_visible_y;
16605 for (row = start_row + nrows_scrolled;
16606 row < bottom_row;
16607 ++row)
16609 row->y = it.current_y;
16610 row->visible_height = row->height;
16612 if (row->y < min_y)
16613 row->visible_height -= min_y - row->y;
16614 if (row->y + row->height > max_y)
16615 row->visible_height -= row->y + row->height - max_y;
16616 if (row->fringe_bitmap_periodic_p)
16617 row->redraw_fringe_bitmaps_p = 1;
16619 it.current_y += row->height;
16621 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16622 last_reused_text_row = row;
16623 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16624 break;
16627 /* Disable lines in the current matrix which are now
16628 below the window. */
16629 for (++row; row < bottom_row; ++row)
16630 row->enabled_p = row->mode_line_p = 0;
16633 /* Update window_end_pos etc.; last_reused_text_row is the last
16634 reused row from the current matrix containing text, if any.
16635 The value of last_text_row is the last displayed line
16636 containing text. */
16637 if (last_reused_text_row)
16639 w->window_end_bytepos
16640 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16641 wset_window_end_pos
16642 (w, make_number (Z
16643 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16644 wset_window_end_vpos
16645 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16646 w->current_matrix)));
16648 else if (last_text_row)
16650 w->window_end_bytepos
16651 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16652 wset_window_end_pos
16653 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16654 wset_window_end_vpos
16655 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16656 w->desired_matrix)));
16658 else
16660 /* This window must be completely empty. */
16661 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16662 wset_window_end_pos (w, make_number (Z - ZV));
16663 wset_window_end_vpos (w, make_number (0));
16665 wset_window_end_valid (w, Qnil);
16667 /* Update hint: don't try scrolling again in update_window. */
16668 w->desired_matrix->no_scrolling_p = 1;
16670 #ifdef GLYPH_DEBUG
16671 debug_method_add (w, "try_window_reusing_current_matrix 1");
16672 #endif
16673 return 1;
16675 else if (CHARPOS (new_start) > CHARPOS (start))
16677 struct glyph_row *pt_row, *row;
16678 struct glyph_row *first_reusable_row;
16679 struct glyph_row *first_row_to_display;
16680 int dy;
16681 int yb = window_text_bottom_y (w);
16683 /* Find the row starting at new_start, if there is one. Don't
16684 reuse a partially visible line at the end. */
16685 first_reusable_row = start_row;
16686 while (first_reusable_row->enabled_p
16687 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16688 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16689 < CHARPOS (new_start)))
16690 ++first_reusable_row;
16692 /* Give up if there is no row to reuse. */
16693 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16694 || !first_reusable_row->enabled_p
16695 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16696 != CHARPOS (new_start)))
16697 return 0;
16699 /* We can reuse fully visible rows beginning with
16700 first_reusable_row to the end of the window. Set
16701 first_row_to_display to the first row that cannot be reused.
16702 Set pt_row to the row containing point, if there is any. */
16703 pt_row = NULL;
16704 for (first_row_to_display = first_reusable_row;
16705 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16706 ++first_row_to_display)
16708 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16709 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16710 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16711 && first_row_to_display->ends_at_zv_p
16712 && pt_row == NULL)))
16713 pt_row = first_row_to_display;
16716 /* Start displaying at the start of first_row_to_display. */
16717 eassert (first_row_to_display->y < yb);
16718 init_to_row_start (&it, w, first_row_to_display);
16720 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16721 - start_vpos);
16722 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16723 - nrows_scrolled);
16724 it.current_y = (first_row_to_display->y - first_reusable_row->y
16725 + WINDOW_HEADER_LINE_HEIGHT (w));
16727 /* Display lines beginning with first_row_to_display in the
16728 desired matrix. Set last_text_row to the last row displayed
16729 that displays text. */
16730 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16731 if (pt_row == NULL)
16732 w->cursor.vpos = -1;
16733 last_text_row = NULL;
16734 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16735 if (display_line (&it))
16736 last_text_row = it.glyph_row - 1;
16738 /* If point is in a reused row, adjust y and vpos of the cursor
16739 position. */
16740 if (pt_row)
16742 w->cursor.vpos -= nrows_scrolled;
16743 w->cursor.y -= first_reusable_row->y - start_row->y;
16746 /* Give up if point isn't in a row displayed or reused. (This
16747 also handles the case where w->cursor.vpos < nrows_scrolled
16748 after the calls to display_line, which can happen with scroll
16749 margins. See bug#1295.) */
16750 if (w->cursor.vpos < 0)
16752 clear_glyph_matrix (w->desired_matrix);
16753 return 0;
16756 /* Scroll the display. */
16757 run.current_y = first_reusable_row->y;
16758 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16759 run.height = it.last_visible_y - run.current_y;
16760 dy = run.current_y - run.desired_y;
16762 if (run.height)
16764 update_begin (f);
16765 FRAME_RIF (f)->update_window_begin_hook (w);
16766 FRAME_RIF (f)->clear_window_mouse_face (w);
16767 FRAME_RIF (f)->scroll_run_hook (w, &run);
16768 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16769 update_end (f);
16772 /* Adjust Y positions of reused rows. */
16773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16774 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16775 max_y = it.last_visible_y;
16776 for (row = first_reusable_row; row < first_row_to_display; ++row)
16778 row->y -= dy;
16779 row->visible_height = row->height;
16780 if (row->y < min_y)
16781 row->visible_height -= min_y - row->y;
16782 if (row->y + row->height > max_y)
16783 row->visible_height -= row->y + row->height - max_y;
16784 if (row->fringe_bitmap_periodic_p)
16785 row->redraw_fringe_bitmaps_p = 1;
16788 /* Scroll the current matrix. */
16789 eassert (nrows_scrolled > 0);
16790 rotate_matrix (w->current_matrix,
16791 start_vpos,
16792 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16793 -nrows_scrolled);
16795 /* Disable rows not reused. */
16796 for (row -= nrows_scrolled; row < bottom_row; ++row)
16797 row->enabled_p = 0;
16799 /* Point may have moved to a different line, so we cannot assume that
16800 the previous cursor position is valid; locate the correct row. */
16801 if (pt_row)
16803 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16804 row < bottom_row
16805 && PT >= MATRIX_ROW_END_CHARPOS (row)
16806 && !row->ends_at_zv_p;
16807 row++)
16809 w->cursor.vpos++;
16810 w->cursor.y = row->y;
16812 if (row < bottom_row)
16814 /* Can't simply scan the row for point with
16815 bidi-reordered glyph rows. Let set_cursor_from_row
16816 figure out where to put the cursor, and if it fails,
16817 give up. */
16818 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16820 if (!set_cursor_from_row (w, row, w->current_matrix,
16821 0, 0, 0, 0))
16823 clear_glyph_matrix (w->desired_matrix);
16824 return 0;
16827 else
16829 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16830 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16832 for (; glyph < end
16833 && (!BUFFERP (glyph->object)
16834 || glyph->charpos < PT);
16835 glyph++)
16837 w->cursor.hpos++;
16838 w->cursor.x += glyph->pixel_width;
16844 /* Adjust window end. A null value of last_text_row means that
16845 the window end is in reused rows which in turn means that
16846 only its vpos can have changed. */
16847 if (last_text_row)
16849 w->window_end_bytepos
16850 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16851 wset_window_end_pos
16852 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16853 wset_window_end_vpos
16854 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16855 w->desired_matrix)));
16857 else
16859 wset_window_end_vpos
16860 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16863 wset_window_end_valid (w, Qnil);
16864 w->desired_matrix->no_scrolling_p = 1;
16866 #ifdef GLYPH_DEBUG
16867 debug_method_add (w, "try_window_reusing_current_matrix 2");
16868 #endif
16869 return 1;
16872 return 0;
16877 /************************************************************************
16878 Window redisplay reusing current matrix when buffer has changed
16879 ************************************************************************/
16881 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16882 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16883 ptrdiff_t *, ptrdiff_t *);
16884 static struct glyph_row *
16885 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16886 struct glyph_row *);
16889 /* Return the last row in MATRIX displaying text. If row START is
16890 non-null, start searching with that row. IT gives the dimensions
16891 of the display. Value is null if matrix is empty; otherwise it is
16892 a pointer to the row found. */
16894 static struct glyph_row *
16895 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16896 struct glyph_row *start)
16898 struct glyph_row *row, *row_found;
16900 /* Set row_found to the last row in IT->w's current matrix
16901 displaying text. The loop looks funny but think of partially
16902 visible lines. */
16903 row_found = NULL;
16904 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16905 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16907 eassert (row->enabled_p);
16908 row_found = row;
16909 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16910 break;
16911 ++row;
16914 return row_found;
16918 /* Return the last row in the current matrix of W that is not affected
16919 by changes at the start of current_buffer that occurred since W's
16920 current matrix was built. Value is null if no such row exists.
16922 BEG_UNCHANGED us the number of characters unchanged at the start of
16923 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16924 first changed character in current_buffer. Characters at positions <
16925 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16926 when the current matrix was built. */
16928 static struct glyph_row *
16929 find_last_unchanged_at_beg_row (struct window *w)
16931 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16932 struct glyph_row *row;
16933 struct glyph_row *row_found = NULL;
16934 int yb = window_text_bottom_y (w);
16936 /* Find the last row displaying unchanged text. */
16937 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16938 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16939 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16940 ++row)
16942 if (/* If row ends before first_changed_pos, it is unchanged,
16943 except in some case. */
16944 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16945 /* When row ends in ZV and we write at ZV it is not
16946 unchanged. */
16947 && !row->ends_at_zv_p
16948 /* When first_changed_pos is the end of a continued line,
16949 row is not unchanged because it may be no longer
16950 continued. */
16951 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16952 && (row->continued_p
16953 || row->exact_window_width_line_p))
16954 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16955 needs to be recomputed, so don't consider this row as
16956 unchanged. This happens when the last line was
16957 bidi-reordered and was killed immediately before this
16958 redisplay cycle. In that case, ROW->end stores the
16959 buffer position of the first visual-order character of
16960 the killed text, which is now beyond ZV. */
16961 && CHARPOS (row->end.pos) <= ZV)
16962 row_found = row;
16964 /* Stop if last visible row. */
16965 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16966 break;
16969 return row_found;
16973 /* Find the first glyph row in the current matrix of W that is not
16974 affected by changes at the end of current_buffer since the
16975 time W's current matrix was built.
16977 Return in *DELTA the number of chars by which buffer positions in
16978 unchanged text at the end of current_buffer must be adjusted.
16980 Return in *DELTA_BYTES the corresponding number of bytes.
16982 Value is null if no such row exists, i.e. all rows are affected by
16983 changes. */
16985 static struct glyph_row *
16986 find_first_unchanged_at_end_row (struct window *w,
16987 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16989 struct glyph_row *row;
16990 struct glyph_row *row_found = NULL;
16992 *delta = *delta_bytes = 0;
16994 /* Display must not have been paused, otherwise the current matrix
16995 is not up to date. */
16996 eassert (!NILP (w->window_end_valid));
16998 /* A value of window_end_pos >= END_UNCHANGED means that the window
16999 end is in the range of changed text. If so, there is no
17000 unchanged row at the end of W's current matrix. */
17001 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
17002 return NULL;
17004 /* Set row to the last row in W's current matrix displaying text. */
17005 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17007 /* If matrix is entirely empty, no unchanged row exists. */
17008 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17010 /* The value of row is the last glyph row in the matrix having a
17011 meaningful buffer position in it. The end position of row
17012 corresponds to window_end_pos. This allows us to translate
17013 buffer positions in the current matrix to current buffer
17014 positions for characters not in changed text. */
17015 ptrdiff_t Z_old =
17016 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17017 ptrdiff_t Z_BYTE_old =
17018 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17019 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17020 struct glyph_row *first_text_row
17021 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17023 *delta = Z - Z_old;
17024 *delta_bytes = Z_BYTE - Z_BYTE_old;
17026 /* Set last_unchanged_pos to the buffer position of the last
17027 character in the buffer that has not been changed. Z is the
17028 index + 1 of the last character in current_buffer, i.e. by
17029 subtracting END_UNCHANGED we get the index of the last
17030 unchanged character, and we have to add BEG to get its buffer
17031 position. */
17032 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17033 last_unchanged_pos_old = last_unchanged_pos - *delta;
17035 /* Search backward from ROW for a row displaying a line that
17036 starts at a minimum position >= last_unchanged_pos_old. */
17037 for (; row > first_text_row; --row)
17039 /* This used to abort, but it can happen.
17040 It is ok to just stop the search instead here. KFS. */
17041 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17042 break;
17044 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17045 row_found = row;
17049 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17051 return row_found;
17055 /* Make sure that glyph rows in the current matrix of window W
17056 reference the same glyph memory as corresponding rows in the
17057 frame's frame matrix. This function is called after scrolling W's
17058 current matrix on a terminal frame in try_window_id and
17059 try_window_reusing_current_matrix. */
17061 static void
17062 sync_frame_with_window_matrix_rows (struct window *w)
17064 struct frame *f = XFRAME (w->frame);
17065 struct glyph_row *window_row, *window_row_end, *frame_row;
17067 /* Preconditions: W must be a leaf window and full-width. Its frame
17068 must have a frame matrix. */
17069 eassert (NILP (w->hchild) && NILP (w->vchild));
17070 eassert (WINDOW_FULL_WIDTH_P (w));
17071 eassert (!FRAME_WINDOW_P (f));
17073 /* If W is a full-width window, glyph pointers in W's current matrix
17074 have, by definition, to be the same as glyph pointers in the
17075 corresponding frame matrix. Note that frame matrices have no
17076 marginal areas (see build_frame_matrix). */
17077 window_row = w->current_matrix->rows;
17078 window_row_end = window_row + w->current_matrix->nrows;
17079 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17080 while (window_row < window_row_end)
17082 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17083 struct glyph *end = window_row->glyphs[LAST_AREA];
17085 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17086 frame_row->glyphs[TEXT_AREA] = start;
17087 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17088 frame_row->glyphs[LAST_AREA] = end;
17090 /* Disable frame rows whose corresponding window rows have
17091 been disabled in try_window_id. */
17092 if (!window_row->enabled_p)
17093 frame_row->enabled_p = 0;
17095 ++window_row, ++frame_row;
17100 /* Find the glyph row in window W containing CHARPOS. Consider all
17101 rows between START and END (not inclusive). END null means search
17102 all rows to the end of the display area of W. Value is the row
17103 containing CHARPOS or null. */
17105 struct glyph_row *
17106 row_containing_pos (struct window *w, ptrdiff_t charpos,
17107 struct glyph_row *start, struct glyph_row *end, int dy)
17109 struct glyph_row *row = start;
17110 struct glyph_row *best_row = NULL;
17111 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17112 int last_y;
17114 /* If we happen to start on a header-line, skip that. */
17115 if (row->mode_line_p)
17116 ++row;
17118 if ((end && row >= end) || !row->enabled_p)
17119 return NULL;
17121 last_y = window_text_bottom_y (w) - dy;
17123 while (1)
17125 /* Give up if we have gone too far. */
17126 if (end && row >= end)
17127 return NULL;
17128 /* This formerly returned if they were equal.
17129 I think that both quantities are of a "last plus one" type;
17130 if so, when they are equal, the row is within the screen. -- rms. */
17131 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17132 return NULL;
17134 /* If it is in this row, return this row. */
17135 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17136 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17137 /* The end position of a row equals the start
17138 position of the next row. If CHARPOS is there, we
17139 would rather display it in the next line, except
17140 when this line ends in ZV. */
17141 && !row->ends_at_zv_p
17142 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17143 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17145 struct glyph *g;
17147 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17148 || (!best_row && !row->continued_p))
17149 return row;
17150 /* In bidi-reordered rows, there could be several rows
17151 occluding point, all of them belonging to the same
17152 continued line. We need to find the row which fits
17153 CHARPOS the best. */
17154 for (g = row->glyphs[TEXT_AREA];
17155 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17156 g++)
17158 if (!STRINGP (g->object))
17160 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17162 mindif = eabs (g->charpos - charpos);
17163 best_row = row;
17164 /* Exact match always wins. */
17165 if (mindif == 0)
17166 return best_row;
17171 else if (best_row && !row->continued_p)
17172 return best_row;
17173 ++row;
17178 /* Try to redisplay window W by reusing its existing display. W's
17179 current matrix must be up to date when this function is called,
17180 i.e. window_end_valid must not be nil.
17182 Value is
17184 1 if display has been updated
17185 0 if otherwise unsuccessful
17186 -1 if redisplay with same window start is known not to succeed
17188 The following steps are performed:
17190 1. Find the last row in the current matrix of W that is not
17191 affected by changes at the start of current_buffer. If no such row
17192 is found, give up.
17194 2. Find the first row in W's current matrix that is not affected by
17195 changes at the end of current_buffer. Maybe there is no such row.
17197 3. Display lines beginning with the row + 1 found in step 1 to the
17198 row found in step 2 or, if step 2 didn't find a row, to the end of
17199 the window.
17201 4. If cursor is not known to appear on the window, give up.
17203 5. If display stopped at the row found in step 2, scroll the
17204 display and current matrix as needed.
17206 6. Maybe display some lines at the end of W, if we must. This can
17207 happen under various circumstances, like a partially visible line
17208 becoming fully visible, or because newly displayed lines are displayed
17209 in smaller font sizes.
17211 7. Update W's window end information. */
17213 static int
17214 try_window_id (struct window *w)
17216 struct frame *f = XFRAME (w->frame);
17217 struct glyph_matrix *current_matrix = w->current_matrix;
17218 struct glyph_matrix *desired_matrix = w->desired_matrix;
17219 struct glyph_row *last_unchanged_at_beg_row;
17220 struct glyph_row *first_unchanged_at_end_row;
17221 struct glyph_row *row;
17222 struct glyph_row *bottom_row;
17223 int bottom_vpos;
17224 struct it it;
17225 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17226 int dvpos, dy;
17227 struct text_pos start_pos;
17228 struct run run;
17229 int first_unchanged_at_end_vpos = 0;
17230 struct glyph_row *last_text_row, *last_text_row_at_end;
17231 struct text_pos start;
17232 ptrdiff_t first_changed_charpos, last_changed_charpos;
17234 #ifdef GLYPH_DEBUG
17235 if (inhibit_try_window_id)
17236 return 0;
17237 #endif
17239 /* This is handy for debugging. */
17240 #if 0
17241 #define GIVE_UP(X) \
17242 do { \
17243 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17244 return 0; \
17245 } while (0)
17246 #else
17247 #define GIVE_UP(X) return 0
17248 #endif
17250 SET_TEXT_POS_FROM_MARKER (start, w->start);
17252 /* Don't use this for mini-windows because these can show
17253 messages and mini-buffers, and we don't handle that here. */
17254 if (MINI_WINDOW_P (w))
17255 GIVE_UP (1);
17257 /* This flag is used to prevent redisplay optimizations. */
17258 if (windows_or_buffers_changed || cursor_type_changed)
17259 GIVE_UP (2);
17261 /* Verify that narrowing has not changed.
17262 Also verify that we were not told to prevent redisplay optimizations.
17263 It would be nice to further
17264 reduce the number of cases where this prevents try_window_id. */
17265 if (current_buffer->clip_changed
17266 || current_buffer->prevent_redisplay_optimizations_p)
17267 GIVE_UP (3);
17269 /* Window must either use window-based redisplay or be full width. */
17270 if (!FRAME_WINDOW_P (f)
17271 && (!FRAME_LINE_INS_DEL_OK (f)
17272 || !WINDOW_FULL_WIDTH_P (w)))
17273 GIVE_UP (4);
17275 /* Give up if point is known NOT to appear in W. */
17276 if (PT < CHARPOS (start))
17277 GIVE_UP (5);
17279 /* Another way to prevent redisplay optimizations. */
17280 if (w->last_modified == 0)
17281 GIVE_UP (6);
17283 /* Verify that window is not hscrolled. */
17284 if (w->hscroll != 0)
17285 GIVE_UP (7);
17287 /* Verify that display wasn't paused. */
17288 if (NILP (w->window_end_valid))
17289 GIVE_UP (8);
17291 /* Can't use this if highlighting a region because a cursor movement
17292 will do more than just set the cursor. */
17293 if (!NILP (Vtransient_mark_mode)
17294 && !NILP (BVAR (current_buffer, mark_active)))
17295 GIVE_UP (9);
17297 /* Likewise if highlighting trailing whitespace. */
17298 if (!NILP (Vshow_trailing_whitespace))
17299 GIVE_UP (11);
17301 /* Likewise if showing a region. */
17302 if (!NILP (w->region_showing))
17303 GIVE_UP (10);
17305 /* Can't use this if overlay arrow position and/or string have
17306 changed. */
17307 if (overlay_arrows_changed_p ())
17308 GIVE_UP (12);
17310 /* When word-wrap is on, adding a space to the first word of a
17311 wrapped line can change the wrap position, altering the line
17312 above it. It might be worthwhile to handle this more
17313 intelligently, but for now just redisplay from scratch. */
17314 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17315 GIVE_UP (21);
17317 /* Under bidi reordering, adding or deleting a character in the
17318 beginning of a paragraph, before the first strong directional
17319 character, can change the base direction of the paragraph (unless
17320 the buffer specifies a fixed paragraph direction), which will
17321 require to redisplay the whole paragraph. It might be worthwhile
17322 to find the paragraph limits and widen the range of redisplayed
17323 lines to that, but for now just give up this optimization and
17324 redisplay from scratch. */
17325 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17326 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17327 GIVE_UP (22);
17329 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17330 only if buffer has really changed. The reason is that the gap is
17331 initially at Z for freshly visited files. The code below would
17332 set end_unchanged to 0 in that case. */
17333 if (MODIFF > SAVE_MODIFF
17334 /* This seems to happen sometimes after saving a buffer. */
17335 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17337 if (GPT - BEG < BEG_UNCHANGED)
17338 BEG_UNCHANGED = GPT - BEG;
17339 if (Z - GPT < END_UNCHANGED)
17340 END_UNCHANGED = Z - GPT;
17343 /* The position of the first and last character that has been changed. */
17344 first_changed_charpos = BEG + BEG_UNCHANGED;
17345 last_changed_charpos = Z - END_UNCHANGED;
17347 /* If window starts after a line end, and the last change is in
17348 front of that newline, then changes don't affect the display.
17349 This case happens with stealth-fontification. Note that although
17350 the display is unchanged, glyph positions in the matrix have to
17351 be adjusted, of course. */
17352 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17353 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17354 && ((last_changed_charpos < CHARPOS (start)
17355 && CHARPOS (start) == BEGV)
17356 || (last_changed_charpos < CHARPOS (start) - 1
17357 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17359 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17360 struct glyph_row *r0;
17362 /* Compute how many chars/bytes have been added to or removed
17363 from the buffer. */
17364 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17365 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17366 Z_delta = Z - Z_old;
17367 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17369 /* Give up if PT is not in the window. Note that it already has
17370 been checked at the start of try_window_id that PT is not in
17371 front of the window start. */
17372 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17373 GIVE_UP (13);
17375 /* If window start is unchanged, we can reuse the whole matrix
17376 as is, after adjusting glyph positions. No need to compute
17377 the window end again, since its offset from Z hasn't changed. */
17378 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17379 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17380 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17381 /* PT must not be in a partially visible line. */
17382 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17383 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17385 /* Adjust positions in the glyph matrix. */
17386 if (Z_delta || Z_delta_bytes)
17388 struct glyph_row *r1
17389 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17390 increment_matrix_positions (w->current_matrix,
17391 MATRIX_ROW_VPOS (r0, current_matrix),
17392 MATRIX_ROW_VPOS (r1, current_matrix),
17393 Z_delta, Z_delta_bytes);
17396 /* Set the cursor. */
17397 row = row_containing_pos (w, PT, r0, NULL, 0);
17398 if (row)
17399 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17400 else
17401 emacs_abort ();
17402 return 1;
17406 /* Handle the case that changes are all below what is displayed in
17407 the window, and that PT is in the window. This shortcut cannot
17408 be taken if ZV is visible in the window, and text has been added
17409 there that is visible in the window. */
17410 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17411 /* ZV is not visible in the window, or there are no
17412 changes at ZV, actually. */
17413 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17414 || first_changed_charpos == last_changed_charpos))
17416 struct glyph_row *r0;
17418 /* Give up if PT is not in the window. Note that it already has
17419 been checked at the start of try_window_id that PT is not in
17420 front of the window start. */
17421 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17422 GIVE_UP (14);
17424 /* If window start is unchanged, we can reuse the whole matrix
17425 as is, without changing glyph positions since no text has
17426 been added/removed in front of the window end. */
17427 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17428 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17429 /* PT must not be in a partially visible line. */
17430 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17431 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17433 /* We have to compute the window end anew since text
17434 could have been added/removed after it. */
17435 wset_window_end_pos
17436 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17437 w->window_end_bytepos
17438 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17440 /* Set the cursor. */
17441 row = row_containing_pos (w, PT, r0, NULL, 0);
17442 if (row)
17443 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17444 else
17445 emacs_abort ();
17446 return 2;
17450 /* Give up if window start is in the changed area.
17452 The condition used to read
17454 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17456 but why that was tested escapes me at the moment. */
17457 if (CHARPOS (start) >= first_changed_charpos
17458 && CHARPOS (start) <= last_changed_charpos)
17459 GIVE_UP (15);
17461 /* Check that window start agrees with the start of the first glyph
17462 row in its current matrix. Check this after we know the window
17463 start is not in changed text, otherwise positions would not be
17464 comparable. */
17465 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17466 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17467 GIVE_UP (16);
17469 /* Give up if the window ends in strings. Overlay strings
17470 at the end are difficult to handle, so don't try. */
17471 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17472 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17473 GIVE_UP (20);
17475 /* Compute the position at which we have to start displaying new
17476 lines. Some of the lines at the top of the window might be
17477 reusable because they are not displaying changed text. Find the
17478 last row in W's current matrix not affected by changes at the
17479 start of current_buffer. Value is null if changes start in the
17480 first line of window. */
17481 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17482 if (last_unchanged_at_beg_row)
17484 /* Avoid starting to display in the middle of a character, a TAB
17485 for instance. This is easier than to set up the iterator
17486 exactly, and it's not a frequent case, so the additional
17487 effort wouldn't really pay off. */
17488 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17489 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17490 && last_unchanged_at_beg_row > w->current_matrix->rows)
17491 --last_unchanged_at_beg_row;
17493 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17494 GIVE_UP (17);
17496 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17497 GIVE_UP (18);
17498 start_pos = it.current.pos;
17500 /* Start displaying new lines in the desired matrix at the same
17501 vpos we would use in the current matrix, i.e. below
17502 last_unchanged_at_beg_row. */
17503 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17504 current_matrix);
17505 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17506 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17508 eassert (it.hpos == 0 && it.current_x == 0);
17510 else
17512 /* There are no reusable lines at the start of the window.
17513 Start displaying in the first text line. */
17514 start_display (&it, w, start);
17515 it.vpos = it.first_vpos;
17516 start_pos = it.current.pos;
17519 /* Find the first row that is not affected by changes at the end of
17520 the buffer. Value will be null if there is no unchanged row, in
17521 which case we must redisplay to the end of the window. delta
17522 will be set to the value by which buffer positions beginning with
17523 first_unchanged_at_end_row have to be adjusted due to text
17524 changes. */
17525 first_unchanged_at_end_row
17526 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17527 IF_DEBUG (debug_delta = delta);
17528 IF_DEBUG (debug_delta_bytes = delta_bytes);
17530 /* Set stop_pos to the buffer position up to which we will have to
17531 display new lines. If first_unchanged_at_end_row != NULL, this
17532 is the buffer position of the start of the line displayed in that
17533 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17534 that we don't stop at a buffer position. */
17535 stop_pos = 0;
17536 if (first_unchanged_at_end_row)
17538 eassert (last_unchanged_at_beg_row == NULL
17539 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17541 /* If this is a continuation line, move forward to the next one
17542 that isn't. Changes in lines above affect this line.
17543 Caution: this may move first_unchanged_at_end_row to a row
17544 not displaying text. */
17545 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17546 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17547 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17548 < it.last_visible_y))
17549 ++first_unchanged_at_end_row;
17551 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17552 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17553 >= it.last_visible_y))
17554 first_unchanged_at_end_row = NULL;
17555 else
17557 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17558 + delta);
17559 first_unchanged_at_end_vpos
17560 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17561 eassert (stop_pos >= Z - END_UNCHANGED);
17564 else if (last_unchanged_at_beg_row == NULL)
17565 GIVE_UP (19);
17568 #ifdef GLYPH_DEBUG
17570 /* Either there is no unchanged row at the end, or the one we have
17571 now displays text. This is a necessary condition for the window
17572 end pos calculation at the end of this function. */
17573 eassert (first_unchanged_at_end_row == NULL
17574 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17576 debug_last_unchanged_at_beg_vpos
17577 = (last_unchanged_at_beg_row
17578 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17579 : -1);
17580 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17582 #endif /* GLYPH_DEBUG */
17585 /* Display new lines. Set last_text_row to the last new line
17586 displayed which has text on it, i.e. might end up as being the
17587 line where the window_end_vpos is. */
17588 w->cursor.vpos = -1;
17589 last_text_row = NULL;
17590 overlay_arrow_seen = 0;
17591 while (it.current_y < it.last_visible_y
17592 && !fonts_changed_p
17593 && (first_unchanged_at_end_row == NULL
17594 || IT_CHARPOS (it) < stop_pos))
17596 if (display_line (&it))
17597 last_text_row = it.glyph_row - 1;
17600 if (fonts_changed_p)
17601 return -1;
17604 /* Compute differences in buffer positions, y-positions etc. for
17605 lines reused at the bottom of the window. Compute what we can
17606 scroll. */
17607 if (first_unchanged_at_end_row
17608 /* No lines reused because we displayed everything up to the
17609 bottom of the window. */
17610 && it.current_y < it.last_visible_y)
17612 dvpos = (it.vpos
17613 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17614 current_matrix));
17615 dy = it.current_y - first_unchanged_at_end_row->y;
17616 run.current_y = first_unchanged_at_end_row->y;
17617 run.desired_y = run.current_y + dy;
17618 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17620 else
17622 delta = delta_bytes = dvpos = dy
17623 = run.current_y = run.desired_y = run.height = 0;
17624 first_unchanged_at_end_row = NULL;
17626 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17629 /* Find the cursor if not already found. We have to decide whether
17630 PT will appear on this window (it sometimes doesn't, but this is
17631 not a very frequent case.) This decision has to be made before
17632 the current matrix is altered. A value of cursor.vpos < 0 means
17633 that PT is either in one of the lines beginning at
17634 first_unchanged_at_end_row or below the window. Don't care for
17635 lines that might be displayed later at the window end; as
17636 mentioned, this is not a frequent case. */
17637 if (w->cursor.vpos < 0)
17639 /* Cursor in unchanged rows at the top? */
17640 if (PT < CHARPOS (start_pos)
17641 && last_unchanged_at_beg_row)
17643 row = row_containing_pos (w, PT,
17644 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17645 last_unchanged_at_beg_row + 1, 0);
17646 if (row)
17647 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17650 /* Start from first_unchanged_at_end_row looking for PT. */
17651 else if (first_unchanged_at_end_row)
17653 row = row_containing_pos (w, PT - delta,
17654 first_unchanged_at_end_row, NULL, 0);
17655 if (row)
17656 set_cursor_from_row (w, row, w->current_matrix, delta,
17657 delta_bytes, dy, dvpos);
17660 /* Give up if cursor was not found. */
17661 if (w->cursor.vpos < 0)
17663 clear_glyph_matrix (w->desired_matrix);
17664 return -1;
17668 /* Don't let the cursor end in the scroll margins. */
17670 int this_scroll_margin, cursor_height;
17672 this_scroll_margin =
17673 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17674 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17675 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17677 if ((w->cursor.y < this_scroll_margin
17678 && CHARPOS (start) > BEGV)
17679 /* Old redisplay didn't take scroll margin into account at the bottom,
17680 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17681 || (w->cursor.y + (make_cursor_line_fully_visible_p
17682 ? cursor_height + this_scroll_margin
17683 : 1)) > it.last_visible_y)
17685 w->cursor.vpos = -1;
17686 clear_glyph_matrix (w->desired_matrix);
17687 return -1;
17691 /* Scroll the display. Do it before changing the current matrix so
17692 that xterm.c doesn't get confused about where the cursor glyph is
17693 found. */
17694 if (dy && run.height)
17696 update_begin (f);
17698 if (FRAME_WINDOW_P (f))
17700 FRAME_RIF (f)->update_window_begin_hook (w);
17701 FRAME_RIF (f)->clear_window_mouse_face (w);
17702 FRAME_RIF (f)->scroll_run_hook (w, &run);
17703 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17705 else
17707 /* Terminal frame. In this case, dvpos gives the number of
17708 lines to scroll by; dvpos < 0 means scroll up. */
17709 int from_vpos
17710 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17711 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17712 int end = (WINDOW_TOP_EDGE_LINE (w)
17713 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17714 + window_internal_height (w));
17716 #if defined (HAVE_GPM) || defined (MSDOS)
17717 x_clear_window_mouse_face (w);
17718 #endif
17719 /* Perform the operation on the screen. */
17720 if (dvpos > 0)
17722 /* Scroll last_unchanged_at_beg_row to the end of the
17723 window down dvpos lines. */
17724 set_terminal_window (f, end);
17726 /* On dumb terminals delete dvpos lines at the end
17727 before inserting dvpos empty lines. */
17728 if (!FRAME_SCROLL_REGION_OK (f))
17729 ins_del_lines (f, end - dvpos, -dvpos);
17731 /* Insert dvpos empty lines in front of
17732 last_unchanged_at_beg_row. */
17733 ins_del_lines (f, from, dvpos);
17735 else if (dvpos < 0)
17737 /* Scroll up last_unchanged_at_beg_vpos to the end of
17738 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17739 set_terminal_window (f, end);
17741 /* Delete dvpos lines in front of
17742 last_unchanged_at_beg_vpos. ins_del_lines will set
17743 the cursor to the given vpos and emit |dvpos| delete
17744 line sequences. */
17745 ins_del_lines (f, from + dvpos, dvpos);
17747 /* On a dumb terminal insert dvpos empty lines at the
17748 end. */
17749 if (!FRAME_SCROLL_REGION_OK (f))
17750 ins_del_lines (f, end + dvpos, -dvpos);
17753 set_terminal_window (f, 0);
17756 update_end (f);
17759 /* Shift reused rows of the current matrix to the right position.
17760 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17761 text. */
17762 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17763 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17764 if (dvpos < 0)
17766 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17767 bottom_vpos, dvpos);
17768 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17769 bottom_vpos);
17771 else if (dvpos > 0)
17773 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17774 bottom_vpos, dvpos);
17775 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17776 first_unchanged_at_end_vpos + dvpos);
17779 /* For frame-based redisplay, make sure that current frame and window
17780 matrix are in sync with respect to glyph memory. */
17781 if (!FRAME_WINDOW_P (f))
17782 sync_frame_with_window_matrix_rows (w);
17784 /* Adjust buffer positions in reused rows. */
17785 if (delta || delta_bytes)
17786 increment_matrix_positions (current_matrix,
17787 first_unchanged_at_end_vpos + dvpos,
17788 bottom_vpos, delta, delta_bytes);
17790 /* Adjust Y positions. */
17791 if (dy)
17792 shift_glyph_matrix (w, current_matrix,
17793 first_unchanged_at_end_vpos + dvpos,
17794 bottom_vpos, dy);
17796 if (first_unchanged_at_end_row)
17798 first_unchanged_at_end_row += dvpos;
17799 if (first_unchanged_at_end_row->y >= it.last_visible_y
17800 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17801 first_unchanged_at_end_row = NULL;
17804 /* If scrolling up, there may be some lines to display at the end of
17805 the window. */
17806 last_text_row_at_end = NULL;
17807 if (dy < 0)
17809 /* Scrolling up can leave for example a partially visible line
17810 at the end of the window to be redisplayed. */
17811 /* Set last_row to the glyph row in the current matrix where the
17812 window end line is found. It has been moved up or down in
17813 the matrix by dvpos. */
17814 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17815 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17817 /* If last_row is the window end line, it should display text. */
17818 eassert (last_row->displays_text_p);
17820 /* If window end line was partially visible before, begin
17821 displaying at that line. Otherwise begin displaying with the
17822 line following it. */
17823 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17825 init_to_row_start (&it, w, last_row);
17826 it.vpos = last_vpos;
17827 it.current_y = last_row->y;
17829 else
17831 init_to_row_end (&it, w, last_row);
17832 it.vpos = 1 + last_vpos;
17833 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17834 ++last_row;
17837 /* We may start in a continuation line. If so, we have to
17838 get the right continuation_lines_width and current_x. */
17839 it.continuation_lines_width = last_row->continuation_lines_width;
17840 it.hpos = it.current_x = 0;
17842 /* Display the rest of the lines at the window end. */
17843 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17844 while (it.current_y < it.last_visible_y
17845 && !fonts_changed_p)
17847 /* Is it always sure that the display agrees with lines in
17848 the current matrix? I don't think so, so we mark rows
17849 displayed invalid in the current matrix by setting their
17850 enabled_p flag to zero. */
17851 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17852 if (display_line (&it))
17853 last_text_row_at_end = it.glyph_row - 1;
17857 /* Update window_end_pos and window_end_vpos. */
17858 if (first_unchanged_at_end_row
17859 && !last_text_row_at_end)
17861 /* Window end line if one of the preserved rows from the current
17862 matrix. Set row to the last row displaying text in current
17863 matrix starting at first_unchanged_at_end_row, after
17864 scrolling. */
17865 eassert (first_unchanged_at_end_row->displays_text_p);
17866 row = find_last_row_displaying_text (w->current_matrix, &it,
17867 first_unchanged_at_end_row);
17868 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17870 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17871 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17872 wset_window_end_vpos
17873 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17874 eassert (w->window_end_bytepos >= 0);
17875 IF_DEBUG (debug_method_add (w, "A"));
17877 else if (last_text_row_at_end)
17879 wset_window_end_pos
17880 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17881 w->window_end_bytepos
17882 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17883 wset_window_end_vpos
17884 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17885 desired_matrix)));
17886 eassert (w->window_end_bytepos >= 0);
17887 IF_DEBUG (debug_method_add (w, "B"));
17889 else if (last_text_row)
17891 /* We have displayed either to the end of the window or at the
17892 end of the window, i.e. the last row with text is to be found
17893 in the desired matrix. */
17894 wset_window_end_pos
17895 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17896 w->window_end_bytepos
17897 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17898 wset_window_end_vpos
17899 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17900 eassert (w->window_end_bytepos >= 0);
17902 else if (first_unchanged_at_end_row == NULL
17903 && last_text_row == NULL
17904 && last_text_row_at_end == NULL)
17906 /* Displayed to end of window, but no line containing text was
17907 displayed. Lines were deleted at the end of the window. */
17908 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17909 int vpos = XFASTINT (w->window_end_vpos);
17910 struct glyph_row *current_row = current_matrix->rows + vpos;
17911 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17913 for (row = NULL;
17914 row == NULL && vpos >= first_vpos;
17915 --vpos, --current_row, --desired_row)
17917 if (desired_row->enabled_p)
17919 if (desired_row->displays_text_p)
17920 row = desired_row;
17922 else if (current_row->displays_text_p)
17923 row = current_row;
17926 eassert (row != NULL);
17927 wset_window_end_vpos (w, make_number (vpos + 1));
17928 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17929 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17930 eassert (w->window_end_bytepos >= 0);
17931 IF_DEBUG (debug_method_add (w, "C"));
17933 else
17934 emacs_abort ();
17936 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17937 debug_end_vpos = XFASTINT (w->window_end_vpos));
17939 /* Record that display has not been completed. */
17940 wset_window_end_valid (w, Qnil);
17941 w->desired_matrix->no_scrolling_p = 1;
17942 return 3;
17944 #undef GIVE_UP
17949 /***********************************************************************
17950 More debugging support
17951 ***********************************************************************/
17953 #ifdef GLYPH_DEBUG
17955 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17956 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17957 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17960 /* Dump the contents of glyph matrix MATRIX on stderr.
17962 GLYPHS 0 means don't show glyph contents.
17963 GLYPHS 1 means show glyphs in short form
17964 GLYPHS > 1 means show glyphs in long form. */
17966 void
17967 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17969 int i;
17970 for (i = 0; i < matrix->nrows; ++i)
17971 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17975 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17976 the glyph row and area where the glyph comes from. */
17978 void
17979 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17981 if (glyph->type == CHAR_GLYPH
17982 || glyph->type == GLYPHLESS_GLYPH)
17984 fprintf (stderr,
17985 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17986 glyph - row->glyphs[TEXT_AREA],
17987 (glyph->type == CHAR_GLYPH
17988 ? 'C'
17989 : 'G'),
17990 glyph->charpos,
17991 (BUFFERP (glyph->object)
17992 ? 'B'
17993 : (STRINGP (glyph->object)
17994 ? 'S'
17995 : (INTEGERP (glyph->object)
17996 ? '0'
17997 : '-'))),
17998 glyph->pixel_width,
17999 glyph->u.ch,
18000 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18001 ? glyph->u.ch
18002 : '.'),
18003 glyph->face_id,
18004 glyph->left_box_line_p,
18005 glyph->right_box_line_p);
18007 else if (glyph->type == STRETCH_GLYPH)
18009 fprintf (stderr,
18010 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18011 glyph - row->glyphs[TEXT_AREA],
18012 'S',
18013 glyph->charpos,
18014 (BUFFERP (glyph->object)
18015 ? 'B'
18016 : (STRINGP (glyph->object)
18017 ? 'S'
18018 : (INTEGERP (glyph->object)
18019 ? '0'
18020 : '-'))),
18021 glyph->pixel_width,
18023 ' ',
18024 glyph->face_id,
18025 glyph->left_box_line_p,
18026 glyph->right_box_line_p);
18028 else if (glyph->type == IMAGE_GLYPH)
18030 fprintf (stderr,
18031 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
18032 glyph - row->glyphs[TEXT_AREA],
18033 'I',
18034 glyph->charpos,
18035 (BUFFERP (glyph->object)
18036 ? 'B'
18037 : (STRINGP (glyph->object)
18038 ? 'S'
18039 : (INTEGERP (glyph->object)
18040 ? '0'
18041 : '-'))),
18042 glyph->pixel_width,
18043 glyph->u.img_id,
18044 '.',
18045 glyph->face_id,
18046 glyph->left_box_line_p,
18047 glyph->right_box_line_p);
18049 else if (glyph->type == COMPOSITE_GLYPH)
18051 fprintf (stderr,
18052 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
18053 glyph - row->glyphs[TEXT_AREA],
18054 '+',
18055 glyph->charpos,
18056 (BUFFERP (glyph->object)
18057 ? 'B'
18058 : (STRINGP (glyph->object)
18059 ? 'S'
18060 : (INTEGERP (glyph->object)
18061 ? '0'
18062 : '-'))),
18063 glyph->pixel_width,
18064 glyph->u.cmp.id);
18065 if (glyph->u.cmp.automatic)
18066 fprintf (stderr,
18067 "[%d-%d]",
18068 glyph->slice.cmp.from, glyph->slice.cmp.to);
18069 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18070 glyph->face_id,
18071 glyph->left_box_line_p,
18072 glyph->right_box_line_p);
18077 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18078 GLYPHS 0 means don't show glyph contents.
18079 GLYPHS 1 means show glyphs in short form
18080 GLYPHS > 1 means show glyphs in long form. */
18082 void
18083 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18085 if (glyphs != 1)
18087 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18088 fprintf (stderr, "==============================================================================\n");
18090 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18091 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18092 vpos,
18093 MATRIX_ROW_START_CHARPOS (row),
18094 MATRIX_ROW_END_CHARPOS (row),
18095 row->used[TEXT_AREA],
18096 row->contains_overlapping_glyphs_p,
18097 row->enabled_p,
18098 row->truncated_on_left_p,
18099 row->truncated_on_right_p,
18100 row->continued_p,
18101 MATRIX_ROW_CONTINUATION_LINE_P (row),
18102 row->displays_text_p,
18103 row->ends_at_zv_p,
18104 row->fill_line_p,
18105 row->ends_in_middle_of_char_p,
18106 row->starts_in_middle_of_char_p,
18107 row->mouse_face_p,
18108 row->x,
18109 row->y,
18110 row->pixel_width,
18111 row->height,
18112 row->visible_height,
18113 row->ascent,
18114 row->phys_ascent);
18115 /* The next 3 lines should align to "Start" in the header. */
18116 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18117 row->end.overlay_string_index,
18118 row->continuation_lines_width);
18119 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18120 CHARPOS (row->start.string_pos),
18121 CHARPOS (row->end.string_pos));
18122 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18123 row->end.dpvec_index);
18126 if (glyphs > 1)
18128 int area;
18130 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18132 struct glyph *glyph = row->glyphs[area];
18133 struct glyph *glyph_end = glyph + row->used[area];
18135 /* Glyph for a line end in text. */
18136 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18137 ++glyph_end;
18139 if (glyph < glyph_end)
18140 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18142 for (; glyph < glyph_end; ++glyph)
18143 dump_glyph (row, glyph, area);
18146 else if (glyphs == 1)
18148 int area;
18150 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18152 char *s = alloca (row->used[area] + 4);
18153 int i;
18155 for (i = 0; i < row->used[area]; ++i)
18157 struct glyph *glyph = row->glyphs[area] + i;
18158 if (i == row->used[area] - 1
18159 && area == TEXT_AREA
18160 && INTEGERP (glyph->object)
18161 && glyph->type == CHAR_GLYPH
18162 && glyph->u.ch == ' ')
18164 strcpy (&s[i], "[\\n]");
18165 i += 4;
18167 else if (glyph->type == CHAR_GLYPH
18168 && glyph->u.ch < 0x80
18169 && glyph->u.ch >= ' ')
18170 s[i] = glyph->u.ch;
18171 else
18172 s[i] = '.';
18175 s[i] = '\0';
18176 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18182 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18183 Sdump_glyph_matrix, 0, 1, "p",
18184 doc: /* Dump the current matrix of the selected window to stderr.
18185 Shows contents of glyph row structures. With non-nil
18186 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18187 glyphs in short form, otherwise show glyphs in long form. */)
18188 (Lisp_Object glyphs)
18190 struct window *w = XWINDOW (selected_window);
18191 struct buffer *buffer = XBUFFER (w->buffer);
18193 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18194 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18195 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18196 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18197 fprintf (stderr, "=============================================\n");
18198 dump_glyph_matrix (w->current_matrix,
18199 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18200 return Qnil;
18204 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18205 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18206 (void)
18208 struct frame *f = XFRAME (selected_frame);
18209 dump_glyph_matrix (f->current_matrix, 1);
18210 return Qnil;
18214 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18215 doc: /* Dump glyph row ROW to stderr.
18216 GLYPH 0 means don't dump glyphs.
18217 GLYPH 1 means dump glyphs in short form.
18218 GLYPH > 1 or omitted means dump glyphs in long form. */)
18219 (Lisp_Object row, Lisp_Object glyphs)
18221 struct glyph_matrix *matrix;
18222 EMACS_INT vpos;
18224 CHECK_NUMBER (row);
18225 matrix = XWINDOW (selected_window)->current_matrix;
18226 vpos = XINT (row);
18227 if (vpos >= 0 && vpos < matrix->nrows)
18228 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18229 vpos,
18230 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18231 return Qnil;
18235 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18236 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18237 GLYPH 0 means don't dump glyphs.
18238 GLYPH 1 means dump glyphs in short form.
18239 GLYPH > 1 or omitted means dump glyphs in long form. */)
18240 (Lisp_Object row, Lisp_Object glyphs)
18242 struct frame *sf = SELECTED_FRAME ();
18243 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18244 EMACS_INT vpos;
18246 CHECK_NUMBER (row);
18247 vpos = XINT (row);
18248 if (vpos >= 0 && vpos < m->nrows)
18249 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18250 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18251 return Qnil;
18255 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18256 doc: /* Toggle tracing of redisplay.
18257 With ARG, turn tracing on if and only if ARG is positive. */)
18258 (Lisp_Object arg)
18260 if (NILP (arg))
18261 trace_redisplay_p = !trace_redisplay_p;
18262 else
18264 arg = Fprefix_numeric_value (arg);
18265 trace_redisplay_p = XINT (arg) > 0;
18268 return Qnil;
18272 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18273 doc: /* Like `format', but print result to stderr.
18274 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18275 (ptrdiff_t nargs, Lisp_Object *args)
18277 Lisp_Object s = Fformat (nargs, args);
18278 fprintf (stderr, "%s", SDATA (s));
18279 return Qnil;
18282 #endif /* GLYPH_DEBUG */
18286 /***********************************************************************
18287 Building Desired Matrix Rows
18288 ***********************************************************************/
18290 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18291 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18293 static struct glyph_row *
18294 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18296 struct frame *f = XFRAME (WINDOW_FRAME (w));
18297 struct buffer *buffer = XBUFFER (w->buffer);
18298 struct buffer *old = current_buffer;
18299 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18300 int arrow_len = SCHARS (overlay_arrow_string);
18301 const unsigned char *arrow_end = arrow_string + arrow_len;
18302 const unsigned char *p;
18303 struct it it;
18304 int multibyte_p;
18305 int n_glyphs_before;
18307 set_buffer_temp (buffer);
18308 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18309 it.glyph_row->used[TEXT_AREA] = 0;
18310 SET_TEXT_POS (it.position, 0, 0);
18312 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18313 p = arrow_string;
18314 while (p < arrow_end)
18316 Lisp_Object face, ilisp;
18318 /* Get the next character. */
18319 if (multibyte_p)
18320 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18321 else
18323 it.c = it.char_to_display = *p, it.len = 1;
18324 if (! ASCII_CHAR_P (it.c))
18325 it.char_to_display = BYTE8_TO_CHAR (it.c);
18327 p += it.len;
18329 /* Get its face. */
18330 ilisp = make_number (p - arrow_string);
18331 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18332 it.face_id = compute_char_face (f, it.char_to_display, face);
18334 /* Compute its width, get its glyphs. */
18335 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18336 SET_TEXT_POS (it.position, -1, -1);
18337 PRODUCE_GLYPHS (&it);
18339 /* If this character doesn't fit any more in the line, we have
18340 to remove some glyphs. */
18341 if (it.current_x > it.last_visible_x)
18343 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18344 break;
18348 set_buffer_temp (old);
18349 return it.glyph_row;
18353 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18354 glyphs to insert is determined by produce_special_glyphs. */
18356 static void
18357 insert_left_trunc_glyphs (struct it *it)
18359 struct it truncate_it;
18360 struct glyph *from, *end, *to, *toend;
18362 eassert (!FRAME_WINDOW_P (it->f)
18363 || (!it->glyph_row->reversed_p
18364 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18365 || (it->glyph_row->reversed_p
18366 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18368 /* Get the truncation glyphs. */
18369 truncate_it = *it;
18370 truncate_it.current_x = 0;
18371 truncate_it.face_id = DEFAULT_FACE_ID;
18372 truncate_it.glyph_row = &scratch_glyph_row;
18373 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18374 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18375 truncate_it.object = make_number (0);
18376 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18378 /* Overwrite glyphs from IT with truncation glyphs. */
18379 if (!it->glyph_row->reversed_p)
18381 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18383 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18384 end = from + tused;
18385 to = it->glyph_row->glyphs[TEXT_AREA];
18386 toend = to + it->glyph_row->used[TEXT_AREA];
18387 if (FRAME_WINDOW_P (it->f))
18389 /* On GUI frames, when variable-size fonts are displayed,
18390 the truncation glyphs may need more pixels than the row's
18391 glyphs they overwrite. We overwrite more glyphs to free
18392 enough screen real estate, and enlarge the stretch glyph
18393 on the right (see display_line), if there is one, to
18394 preserve the screen position of the truncation glyphs on
18395 the right. */
18396 int w = 0;
18397 struct glyph *g = to;
18398 short used;
18400 /* The first glyph could be partially visible, in which case
18401 it->glyph_row->x will be negative. But we want the left
18402 truncation glyphs to be aligned at the left margin of the
18403 window, so we override the x coordinate at which the row
18404 will begin. */
18405 it->glyph_row->x = 0;
18406 while (g < toend && w < it->truncation_pixel_width)
18408 w += g->pixel_width;
18409 ++g;
18411 if (g - to - tused > 0)
18413 memmove (to + tused, g, (toend - g) * sizeof(*g));
18414 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18416 used = it->glyph_row->used[TEXT_AREA];
18417 if (it->glyph_row->truncated_on_right_p
18418 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18419 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18420 == STRETCH_GLYPH)
18422 int extra = w - it->truncation_pixel_width;
18424 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18428 while (from < end)
18429 *to++ = *from++;
18431 /* There may be padding glyphs left over. Overwrite them too. */
18432 if (!FRAME_WINDOW_P (it->f))
18434 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18436 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18437 while (from < end)
18438 *to++ = *from++;
18442 if (to > toend)
18443 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18445 else
18447 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18449 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18450 that back to front. */
18451 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18452 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18453 toend = it->glyph_row->glyphs[TEXT_AREA];
18454 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18455 if (FRAME_WINDOW_P (it->f))
18457 int w = 0;
18458 struct glyph *g = to;
18460 while (g >= toend && w < it->truncation_pixel_width)
18462 w += g->pixel_width;
18463 --g;
18465 if (to - g - tused > 0)
18466 to = g + tused;
18467 if (it->glyph_row->truncated_on_right_p
18468 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18469 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18471 int extra = w - it->truncation_pixel_width;
18473 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18477 while (from >= end && to >= toend)
18478 *to-- = *from--;
18479 if (!FRAME_WINDOW_P (it->f))
18481 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18483 from =
18484 truncate_it.glyph_row->glyphs[TEXT_AREA]
18485 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18486 while (from >= end && to >= toend)
18487 *to-- = *from--;
18490 if (from >= end)
18492 /* Need to free some room before prepending additional
18493 glyphs. */
18494 int move_by = from - end + 1;
18495 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18496 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18498 for ( ; g >= g0; g--)
18499 g[move_by] = *g;
18500 while (from >= end)
18501 *to-- = *from--;
18502 it->glyph_row->used[TEXT_AREA] += move_by;
18507 /* Compute the hash code for ROW. */
18508 unsigned
18509 row_hash (struct glyph_row *row)
18511 int area, k;
18512 unsigned hashval = 0;
18514 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18515 for (k = 0; k < row->used[area]; ++k)
18516 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18517 + row->glyphs[area][k].u.val
18518 + row->glyphs[area][k].face_id
18519 + row->glyphs[area][k].padding_p
18520 + (row->glyphs[area][k].type << 2));
18522 return hashval;
18525 /* Compute the pixel height and width of IT->glyph_row.
18527 Most of the time, ascent and height of a display line will be equal
18528 to the max_ascent and max_height values of the display iterator
18529 structure. This is not the case if
18531 1. We hit ZV without displaying anything. In this case, max_ascent
18532 and max_height will be zero.
18534 2. We have some glyphs that don't contribute to the line height.
18535 (The glyph row flag contributes_to_line_height_p is for future
18536 pixmap extensions).
18538 The first case is easily covered by using default values because in
18539 these cases, the line height does not really matter, except that it
18540 must not be zero. */
18542 static void
18543 compute_line_metrics (struct it *it)
18545 struct glyph_row *row = it->glyph_row;
18547 if (FRAME_WINDOW_P (it->f))
18549 int i, min_y, max_y;
18551 /* The line may consist of one space only, that was added to
18552 place the cursor on it. If so, the row's height hasn't been
18553 computed yet. */
18554 if (row->height == 0)
18556 if (it->max_ascent + it->max_descent == 0)
18557 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18558 row->ascent = it->max_ascent;
18559 row->height = it->max_ascent + it->max_descent;
18560 row->phys_ascent = it->max_phys_ascent;
18561 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18562 row->extra_line_spacing = it->max_extra_line_spacing;
18565 /* Compute the width of this line. */
18566 row->pixel_width = row->x;
18567 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18568 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18570 eassert (row->pixel_width >= 0);
18571 eassert (row->ascent >= 0 && row->height > 0);
18573 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18574 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18576 /* If first line's physical ascent is larger than its logical
18577 ascent, use the physical ascent, and make the row taller.
18578 This makes accented characters fully visible. */
18579 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18580 && row->phys_ascent > row->ascent)
18582 row->height += row->phys_ascent - row->ascent;
18583 row->ascent = row->phys_ascent;
18586 /* Compute how much of the line is visible. */
18587 row->visible_height = row->height;
18589 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18590 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18592 if (row->y < min_y)
18593 row->visible_height -= min_y - row->y;
18594 if (row->y + row->height > max_y)
18595 row->visible_height -= row->y + row->height - max_y;
18597 else
18599 row->pixel_width = row->used[TEXT_AREA];
18600 if (row->continued_p)
18601 row->pixel_width -= it->continuation_pixel_width;
18602 else if (row->truncated_on_right_p)
18603 row->pixel_width -= it->truncation_pixel_width;
18604 row->ascent = row->phys_ascent = 0;
18605 row->height = row->phys_height = row->visible_height = 1;
18606 row->extra_line_spacing = 0;
18609 /* Compute a hash code for this row. */
18610 row->hash = row_hash (row);
18612 it->max_ascent = it->max_descent = 0;
18613 it->max_phys_ascent = it->max_phys_descent = 0;
18617 /* Append one space to the glyph row of iterator IT if doing a
18618 window-based redisplay. The space has the same face as
18619 IT->face_id. Value is non-zero if a space was added.
18621 This function is called to make sure that there is always one glyph
18622 at the end of a glyph row that the cursor can be set on under
18623 window-systems. (If there weren't such a glyph we would not know
18624 how wide and tall a box cursor should be displayed).
18626 At the same time this space let's a nicely handle clearing to the
18627 end of the line if the row ends in italic text. */
18629 static int
18630 append_space_for_newline (struct it *it, int default_face_p)
18632 if (FRAME_WINDOW_P (it->f))
18634 int n = it->glyph_row->used[TEXT_AREA];
18636 if (it->glyph_row->glyphs[TEXT_AREA] + n
18637 < it->glyph_row->glyphs[1 + TEXT_AREA])
18639 /* Save some values that must not be changed.
18640 Must save IT->c and IT->len because otherwise
18641 ITERATOR_AT_END_P wouldn't work anymore after
18642 append_space_for_newline has been called. */
18643 enum display_element_type saved_what = it->what;
18644 int saved_c = it->c, saved_len = it->len;
18645 int saved_char_to_display = it->char_to_display;
18646 int saved_x = it->current_x;
18647 int saved_face_id = it->face_id;
18648 struct text_pos saved_pos;
18649 Lisp_Object saved_object;
18650 struct face *face;
18652 saved_object = it->object;
18653 saved_pos = it->position;
18655 it->what = IT_CHARACTER;
18656 memset (&it->position, 0, sizeof it->position);
18657 it->object = make_number (0);
18658 it->c = it->char_to_display = ' ';
18659 it->len = 1;
18661 /* If the default face was remapped, be sure to use the
18662 remapped face for the appended newline. */
18663 if (default_face_p)
18664 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18665 else if (it->face_before_selective_p)
18666 it->face_id = it->saved_face_id;
18667 face = FACE_FROM_ID (it->f, it->face_id);
18668 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18670 PRODUCE_GLYPHS (it);
18672 it->override_ascent = -1;
18673 it->constrain_row_ascent_descent_p = 0;
18674 it->current_x = saved_x;
18675 it->object = saved_object;
18676 it->position = saved_pos;
18677 it->what = saved_what;
18678 it->face_id = saved_face_id;
18679 it->len = saved_len;
18680 it->c = saved_c;
18681 it->char_to_display = saved_char_to_display;
18682 return 1;
18686 return 0;
18690 /* Extend the face of the last glyph in the text area of IT->glyph_row
18691 to the end of the display line. Called from display_line. If the
18692 glyph row is empty, add a space glyph to it so that we know the
18693 face to draw. Set the glyph row flag fill_line_p. If the glyph
18694 row is R2L, prepend a stretch glyph to cover the empty space to the
18695 left of the leftmost glyph. */
18697 static void
18698 extend_face_to_end_of_line (struct it *it)
18700 struct face *face, *default_face;
18701 struct frame *f = it->f;
18703 /* If line is already filled, do nothing. Non window-system frames
18704 get a grace of one more ``pixel'' because their characters are
18705 1-``pixel'' wide, so they hit the equality too early. This grace
18706 is needed only for R2L rows that are not continued, to produce
18707 one extra blank where we could display the cursor. */
18708 if (it->current_x >= it->last_visible_x
18709 + (!FRAME_WINDOW_P (f)
18710 && it->glyph_row->reversed_p
18711 && !it->glyph_row->continued_p))
18712 return;
18714 /* The default face, possibly remapped. */
18715 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18717 /* Face extension extends the background and box of IT->face_id
18718 to the end of the line. If the background equals the background
18719 of the frame, we don't have to do anything. */
18720 if (it->face_before_selective_p)
18721 face = FACE_FROM_ID (f, it->saved_face_id);
18722 else
18723 face = FACE_FROM_ID (f, it->face_id);
18725 if (FRAME_WINDOW_P (f)
18726 && it->glyph_row->displays_text_p
18727 && face->box == FACE_NO_BOX
18728 && face->background == FRAME_BACKGROUND_PIXEL (f)
18729 && !face->stipple
18730 && !it->glyph_row->reversed_p)
18731 return;
18733 /* Set the glyph row flag indicating that the face of the last glyph
18734 in the text area has to be drawn to the end of the text area. */
18735 it->glyph_row->fill_line_p = 1;
18737 /* If current character of IT is not ASCII, make sure we have the
18738 ASCII face. This will be automatically undone the next time
18739 get_next_display_element returns a multibyte character. Note
18740 that the character will always be single byte in unibyte
18741 text. */
18742 if (!ASCII_CHAR_P (it->c))
18744 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18747 if (FRAME_WINDOW_P (f))
18749 /* If the row is empty, add a space with the current face of IT,
18750 so that we know which face to draw. */
18751 if (it->glyph_row->used[TEXT_AREA] == 0)
18753 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18754 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18755 it->glyph_row->used[TEXT_AREA] = 1;
18757 #ifdef HAVE_WINDOW_SYSTEM
18758 if (it->glyph_row->reversed_p)
18760 /* Prepend a stretch glyph to the row, such that the
18761 rightmost glyph will be drawn flushed all the way to the
18762 right margin of the window. The stretch glyph that will
18763 occupy the empty space, if any, to the left of the
18764 glyphs. */
18765 struct font *font = face->font ? face->font : FRAME_FONT (f);
18766 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18767 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18768 struct glyph *g;
18769 int row_width, stretch_ascent, stretch_width;
18770 struct text_pos saved_pos;
18771 int saved_face_id, saved_avoid_cursor;
18773 for (row_width = 0, g = row_start; g < row_end; g++)
18774 row_width += g->pixel_width;
18775 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18776 if (stretch_width > 0)
18778 stretch_ascent =
18779 (((it->ascent + it->descent)
18780 * FONT_BASE (font)) / FONT_HEIGHT (font));
18781 saved_pos = it->position;
18782 memset (&it->position, 0, sizeof it->position);
18783 saved_avoid_cursor = it->avoid_cursor_p;
18784 it->avoid_cursor_p = 1;
18785 saved_face_id = it->face_id;
18786 /* The last row's stretch glyph should get the default
18787 face, to avoid painting the rest of the window with
18788 the region face, if the region ends at ZV. */
18789 if (it->glyph_row->ends_at_zv_p)
18790 it->face_id = default_face->id;
18791 else
18792 it->face_id = face->id;
18793 append_stretch_glyph (it, make_number (0), stretch_width,
18794 it->ascent + it->descent, stretch_ascent);
18795 it->position = saved_pos;
18796 it->avoid_cursor_p = saved_avoid_cursor;
18797 it->face_id = saved_face_id;
18800 #endif /* HAVE_WINDOW_SYSTEM */
18802 else
18804 /* Save some values that must not be changed. */
18805 int saved_x = it->current_x;
18806 struct text_pos saved_pos;
18807 Lisp_Object saved_object;
18808 enum display_element_type saved_what = it->what;
18809 int saved_face_id = it->face_id;
18811 saved_object = it->object;
18812 saved_pos = it->position;
18814 it->what = IT_CHARACTER;
18815 memset (&it->position, 0, sizeof it->position);
18816 it->object = make_number (0);
18817 it->c = it->char_to_display = ' ';
18818 it->len = 1;
18819 /* The last row's blank glyphs should get the default face, to
18820 avoid painting the rest of the window with the region face,
18821 if the region ends at ZV. */
18822 if (it->glyph_row->ends_at_zv_p)
18823 it->face_id = default_face->id;
18824 else
18825 it->face_id = face->id;
18827 PRODUCE_GLYPHS (it);
18829 while (it->current_x <= it->last_visible_x)
18830 PRODUCE_GLYPHS (it);
18832 /* Don't count these blanks really. It would let us insert a left
18833 truncation glyph below and make us set the cursor on them, maybe. */
18834 it->current_x = saved_x;
18835 it->object = saved_object;
18836 it->position = saved_pos;
18837 it->what = saved_what;
18838 it->face_id = saved_face_id;
18843 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18844 trailing whitespace. */
18846 static int
18847 trailing_whitespace_p (ptrdiff_t charpos)
18849 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18850 int c = 0;
18852 while (bytepos < ZV_BYTE
18853 && (c = FETCH_CHAR (bytepos),
18854 c == ' ' || c == '\t'))
18855 ++bytepos;
18857 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18859 if (bytepos != PT_BYTE)
18860 return 1;
18862 return 0;
18866 /* Highlight trailing whitespace, if any, in ROW. */
18868 static void
18869 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18871 int used = row->used[TEXT_AREA];
18873 if (used)
18875 struct glyph *start = row->glyphs[TEXT_AREA];
18876 struct glyph *glyph = start + used - 1;
18878 if (row->reversed_p)
18880 /* Right-to-left rows need to be processed in the opposite
18881 direction, so swap the edge pointers. */
18882 glyph = start;
18883 start = row->glyphs[TEXT_AREA] + used - 1;
18886 /* Skip over glyphs inserted to display the cursor at the
18887 end of a line, for extending the face of the last glyph
18888 to the end of the line on terminals, and for truncation
18889 and continuation glyphs. */
18890 if (!row->reversed_p)
18892 while (glyph >= start
18893 && glyph->type == CHAR_GLYPH
18894 && INTEGERP (glyph->object))
18895 --glyph;
18897 else
18899 while (glyph <= start
18900 && glyph->type == CHAR_GLYPH
18901 && INTEGERP (glyph->object))
18902 ++glyph;
18905 /* If last glyph is a space or stretch, and it's trailing
18906 whitespace, set the face of all trailing whitespace glyphs in
18907 IT->glyph_row to `trailing-whitespace'. */
18908 if ((row->reversed_p ? glyph <= start : glyph >= start)
18909 && BUFFERP (glyph->object)
18910 && (glyph->type == STRETCH_GLYPH
18911 || (glyph->type == CHAR_GLYPH
18912 && glyph->u.ch == ' '))
18913 && trailing_whitespace_p (glyph->charpos))
18915 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18916 if (face_id < 0)
18917 return;
18919 if (!row->reversed_p)
18921 while (glyph >= start
18922 && BUFFERP (glyph->object)
18923 && (glyph->type == STRETCH_GLYPH
18924 || (glyph->type == CHAR_GLYPH
18925 && glyph->u.ch == ' ')))
18926 (glyph--)->face_id = face_id;
18928 else
18930 while (glyph <= start
18931 && BUFFERP (glyph->object)
18932 && (glyph->type == STRETCH_GLYPH
18933 || (glyph->type == CHAR_GLYPH
18934 && glyph->u.ch == ' ')))
18935 (glyph++)->face_id = face_id;
18942 /* Value is non-zero if glyph row ROW should be
18943 used to hold the cursor. */
18945 static int
18946 cursor_row_p (struct glyph_row *row)
18948 int result = 1;
18950 if (PT == CHARPOS (row->end.pos)
18951 || PT == MATRIX_ROW_END_CHARPOS (row))
18953 /* Suppose the row ends on a string.
18954 Unless the row is continued, that means it ends on a newline
18955 in the string. If it's anything other than a display string
18956 (e.g., a before-string from an overlay), we don't want the
18957 cursor there. (This heuristic seems to give the optimal
18958 behavior for the various types of multi-line strings.)
18959 One exception: if the string has `cursor' property on one of
18960 its characters, we _do_ want the cursor there. */
18961 if (CHARPOS (row->end.string_pos) >= 0)
18963 if (row->continued_p)
18964 result = 1;
18965 else
18967 /* Check for `display' property. */
18968 struct glyph *beg = row->glyphs[TEXT_AREA];
18969 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18970 struct glyph *glyph;
18972 result = 0;
18973 for (glyph = end; glyph >= beg; --glyph)
18974 if (STRINGP (glyph->object))
18976 Lisp_Object prop
18977 = Fget_char_property (make_number (PT),
18978 Qdisplay, Qnil);
18979 result =
18980 (!NILP (prop)
18981 && display_prop_string_p (prop, glyph->object));
18982 /* If there's a `cursor' property on one of the
18983 string's characters, this row is a cursor row,
18984 even though this is not a display string. */
18985 if (!result)
18987 Lisp_Object s = glyph->object;
18989 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18991 ptrdiff_t gpos = glyph->charpos;
18993 if (!NILP (Fget_char_property (make_number (gpos),
18994 Qcursor, s)))
18996 result = 1;
18997 break;
19001 break;
19005 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19007 /* If the row ends in middle of a real character,
19008 and the line is continued, we want the cursor here.
19009 That's because CHARPOS (ROW->end.pos) would equal
19010 PT if PT is before the character. */
19011 if (!row->ends_in_ellipsis_p)
19012 result = row->continued_p;
19013 else
19014 /* If the row ends in an ellipsis, then
19015 CHARPOS (ROW->end.pos) will equal point after the
19016 invisible text. We want that position to be displayed
19017 after the ellipsis. */
19018 result = 0;
19020 /* If the row ends at ZV, display the cursor at the end of that
19021 row instead of at the start of the row below. */
19022 else if (row->ends_at_zv_p)
19023 result = 1;
19024 else
19025 result = 0;
19028 return result;
19033 /* Push the property PROP so that it will be rendered at the current
19034 position in IT. Return 1 if PROP was successfully pushed, 0
19035 otherwise. Called from handle_line_prefix to handle the
19036 `line-prefix' and `wrap-prefix' properties. */
19038 static int
19039 push_prefix_prop (struct it *it, Lisp_Object prop)
19041 struct text_pos pos =
19042 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19044 eassert (it->method == GET_FROM_BUFFER
19045 || it->method == GET_FROM_DISPLAY_VECTOR
19046 || it->method == GET_FROM_STRING);
19048 /* We need to save the current buffer/string position, so it will be
19049 restored by pop_it, because iterate_out_of_display_property
19050 depends on that being set correctly, but some situations leave
19051 it->position not yet set when this function is called. */
19052 push_it (it, &pos);
19054 if (STRINGP (prop))
19056 if (SCHARS (prop) == 0)
19058 pop_it (it);
19059 return 0;
19062 it->string = prop;
19063 it->string_from_prefix_prop_p = 1;
19064 it->multibyte_p = STRING_MULTIBYTE (it->string);
19065 it->current.overlay_string_index = -1;
19066 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19067 it->end_charpos = it->string_nchars = SCHARS (it->string);
19068 it->method = GET_FROM_STRING;
19069 it->stop_charpos = 0;
19070 it->prev_stop = 0;
19071 it->base_level_stop = 0;
19073 /* Force paragraph direction to be that of the parent
19074 buffer/string. */
19075 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19076 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19077 else
19078 it->paragraph_embedding = L2R;
19080 /* Set up the bidi iterator for this display string. */
19081 if (it->bidi_p)
19083 it->bidi_it.string.lstring = it->string;
19084 it->bidi_it.string.s = NULL;
19085 it->bidi_it.string.schars = it->end_charpos;
19086 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19087 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19088 it->bidi_it.string.unibyte = !it->multibyte_p;
19089 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19092 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19094 it->method = GET_FROM_STRETCH;
19095 it->object = prop;
19097 #ifdef HAVE_WINDOW_SYSTEM
19098 else if (IMAGEP (prop))
19100 it->what = IT_IMAGE;
19101 it->image_id = lookup_image (it->f, prop);
19102 it->method = GET_FROM_IMAGE;
19104 #endif /* HAVE_WINDOW_SYSTEM */
19105 else
19107 pop_it (it); /* bogus display property, give up */
19108 return 0;
19111 return 1;
19114 /* Return the character-property PROP at the current position in IT. */
19116 static Lisp_Object
19117 get_it_property (struct it *it, Lisp_Object prop)
19119 Lisp_Object position;
19121 if (STRINGP (it->object))
19122 position = make_number (IT_STRING_CHARPOS (*it));
19123 else if (BUFFERP (it->object))
19124 position = make_number (IT_CHARPOS (*it));
19125 else
19126 return Qnil;
19128 return Fget_char_property (position, prop, it->object);
19131 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19133 static void
19134 handle_line_prefix (struct it *it)
19136 Lisp_Object prefix;
19138 if (it->continuation_lines_width > 0)
19140 prefix = get_it_property (it, Qwrap_prefix);
19141 if (NILP (prefix))
19142 prefix = Vwrap_prefix;
19144 else
19146 prefix = get_it_property (it, Qline_prefix);
19147 if (NILP (prefix))
19148 prefix = Vline_prefix;
19150 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19152 /* If the prefix is wider than the window, and we try to wrap
19153 it, it would acquire its own wrap prefix, and so on till the
19154 iterator stack overflows. So, don't wrap the prefix. */
19155 it->line_wrap = TRUNCATE;
19156 it->avoid_cursor_p = 1;
19162 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19163 only for R2L lines from display_line and display_string, when they
19164 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19165 the line/string needs to be continued on the next glyph row. */
19166 static void
19167 unproduce_glyphs (struct it *it, int n)
19169 struct glyph *glyph, *end;
19171 eassert (it->glyph_row);
19172 eassert (it->glyph_row->reversed_p);
19173 eassert (it->area == TEXT_AREA);
19174 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19176 if (n > it->glyph_row->used[TEXT_AREA])
19177 n = it->glyph_row->used[TEXT_AREA];
19178 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19179 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19180 for ( ; glyph < end; glyph++)
19181 glyph[-n] = *glyph;
19184 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19185 and ROW->maxpos. */
19186 static void
19187 find_row_edges (struct it *it, struct glyph_row *row,
19188 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19189 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19191 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19192 lines' rows is implemented for bidi-reordered rows. */
19194 /* ROW->minpos is the value of min_pos, the minimal buffer position
19195 we have in ROW, or ROW->start.pos if that is smaller. */
19196 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19197 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19198 else
19199 /* We didn't find buffer positions smaller than ROW->start, or
19200 didn't find _any_ valid buffer positions in any of the glyphs,
19201 so we must trust the iterator's computed positions. */
19202 row->minpos = row->start.pos;
19203 if (max_pos <= 0)
19205 max_pos = CHARPOS (it->current.pos);
19206 max_bpos = BYTEPOS (it->current.pos);
19209 /* Here are the various use-cases for ending the row, and the
19210 corresponding values for ROW->maxpos:
19212 Line ends in a newline from buffer eol_pos + 1
19213 Line is continued from buffer max_pos + 1
19214 Line is truncated on right it->current.pos
19215 Line ends in a newline from string max_pos + 1(*)
19216 (*) + 1 only when line ends in a forward scan
19217 Line is continued from string max_pos
19218 Line is continued from display vector max_pos
19219 Line is entirely from a string min_pos == max_pos
19220 Line is entirely from a display vector min_pos == max_pos
19221 Line that ends at ZV ZV
19223 If you discover other use-cases, please add them here as
19224 appropriate. */
19225 if (row->ends_at_zv_p)
19226 row->maxpos = it->current.pos;
19227 else if (row->used[TEXT_AREA])
19229 int seen_this_string = 0;
19230 struct glyph_row *r1 = row - 1;
19232 /* Did we see the same display string on the previous row? */
19233 if (STRINGP (it->object)
19234 /* this is not the first row */
19235 && row > it->w->desired_matrix->rows
19236 /* previous row is not the header line */
19237 && !r1->mode_line_p
19238 /* previous row also ends in a newline from a string */
19239 && r1->ends_in_newline_from_string_p)
19241 struct glyph *start, *end;
19243 /* Search for the last glyph of the previous row that came
19244 from buffer or string. Depending on whether the row is
19245 L2R or R2L, we need to process it front to back or the
19246 other way round. */
19247 if (!r1->reversed_p)
19249 start = r1->glyphs[TEXT_AREA];
19250 end = start + r1->used[TEXT_AREA];
19251 /* Glyphs inserted by redisplay have an integer (zero)
19252 as their object. */
19253 while (end > start
19254 && INTEGERP ((end - 1)->object)
19255 && (end - 1)->charpos <= 0)
19256 --end;
19257 if (end > start)
19259 if (EQ ((end - 1)->object, it->object))
19260 seen_this_string = 1;
19262 else
19263 /* If all the glyphs of the previous row were inserted
19264 by redisplay, it means the previous row was
19265 produced from a single newline, which is only
19266 possible if that newline came from the same string
19267 as the one which produced this ROW. */
19268 seen_this_string = 1;
19270 else
19272 end = r1->glyphs[TEXT_AREA] - 1;
19273 start = end + r1->used[TEXT_AREA];
19274 while (end < start
19275 && INTEGERP ((end + 1)->object)
19276 && (end + 1)->charpos <= 0)
19277 ++end;
19278 if (end < start)
19280 if (EQ ((end + 1)->object, it->object))
19281 seen_this_string = 1;
19283 else
19284 seen_this_string = 1;
19287 /* Take note of each display string that covers a newline only
19288 once, the first time we see it. This is for when a display
19289 string includes more than one newline in it. */
19290 if (row->ends_in_newline_from_string_p && !seen_this_string)
19292 /* If we were scanning the buffer forward when we displayed
19293 the string, we want to account for at least one buffer
19294 position that belongs to this row (position covered by
19295 the display string), so that cursor positioning will
19296 consider this row as a candidate when point is at the end
19297 of the visual line represented by this row. This is not
19298 required when scanning back, because max_pos will already
19299 have a much larger value. */
19300 if (CHARPOS (row->end.pos) > max_pos)
19301 INC_BOTH (max_pos, max_bpos);
19302 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19304 else if (CHARPOS (it->eol_pos) > 0)
19305 SET_TEXT_POS (row->maxpos,
19306 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19307 else if (row->continued_p)
19309 /* If max_pos is different from IT's current position, it
19310 means IT->method does not belong to the display element
19311 at max_pos. However, it also means that the display
19312 element at max_pos was displayed in its entirety on this
19313 line, which is equivalent to saying that the next line
19314 starts at the next buffer position. */
19315 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19316 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19317 else
19319 INC_BOTH (max_pos, max_bpos);
19320 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19323 else if (row->truncated_on_right_p)
19324 /* display_line already called reseat_at_next_visible_line_start,
19325 which puts the iterator at the beginning of the next line, in
19326 the logical order. */
19327 row->maxpos = it->current.pos;
19328 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19329 /* A line that is entirely from a string/image/stretch... */
19330 row->maxpos = row->minpos;
19331 else
19332 emacs_abort ();
19334 else
19335 row->maxpos = it->current.pos;
19338 /* Construct the glyph row IT->glyph_row in the desired matrix of
19339 IT->w from text at the current position of IT. See dispextern.h
19340 for an overview of struct it. Value is non-zero if
19341 IT->glyph_row displays text, as opposed to a line displaying ZV
19342 only. */
19344 static int
19345 display_line (struct it *it)
19347 struct glyph_row *row = it->glyph_row;
19348 Lisp_Object overlay_arrow_string;
19349 struct it wrap_it;
19350 void *wrap_data = NULL;
19351 int may_wrap = 0, wrap_x IF_LINT (= 0);
19352 int wrap_row_used = -1;
19353 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19354 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19355 int wrap_row_extra_line_spacing IF_LINT (= 0);
19356 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19357 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19358 int cvpos;
19359 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19360 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19362 /* We always start displaying at hpos zero even if hscrolled. */
19363 eassert (it->hpos == 0 && it->current_x == 0);
19365 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19366 >= it->w->desired_matrix->nrows)
19368 it->w->nrows_scale_factor++;
19369 fonts_changed_p = 1;
19370 return 0;
19373 /* Is IT->w showing the region? */
19374 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19376 /* Clear the result glyph row and enable it. */
19377 prepare_desired_row (row);
19379 row->y = it->current_y;
19380 row->start = it->start;
19381 row->continuation_lines_width = it->continuation_lines_width;
19382 row->displays_text_p = 1;
19383 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19384 it->starts_in_middle_of_char_p = 0;
19386 /* Arrange the overlays nicely for our purposes. Usually, we call
19387 display_line on only one line at a time, in which case this
19388 can't really hurt too much, or we call it on lines which appear
19389 one after another in the buffer, in which case all calls to
19390 recenter_overlay_lists but the first will be pretty cheap. */
19391 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19393 /* Move over display elements that are not visible because we are
19394 hscrolled. This may stop at an x-position < IT->first_visible_x
19395 if the first glyph is partially visible or if we hit a line end. */
19396 if (it->current_x < it->first_visible_x)
19398 enum move_it_result move_result;
19400 this_line_min_pos = row->start.pos;
19401 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19402 MOVE_TO_POS | MOVE_TO_X);
19403 /* If we are under a large hscroll, move_it_in_display_line_to
19404 could hit the end of the line without reaching
19405 it->first_visible_x. Pretend that we did reach it. This is
19406 especially important on a TTY, where we will call
19407 extend_face_to_end_of_line, which needs to know how many
19408 blank glyphs to produce. */
19409 if (it->current_x < it->first_visible_x
19410 && (move_result == MOVE_NEWLINE_OR_CR
19411 || move_result == MOVE_POS_MATCH_OR_ZV))
19412 it->current_x = it->first_visible_x;
19414 /* Record the smallest positions seen while we moved over
19415 display elements that are not visible. This is needed by
19416 redisplay_internal for optimizing the case where the cursor
19417 stays inside the same line. The rest of this function only
19418 considers positions that are actually displayed, so
19419 RECORD_MAX_MIN_POS will not otherwise record positions that
19420 are hscrolled to the left of the left edge of the window. */
19421 min_pos = CHARPOS (this_line_min_pos);
19422 min_bpos = BYTEPOS (this_line_min_pos);
19424 else
19426 /* We only do this when not calling `move_it_in_display_line_to'
19427 above, because move_it_in_display_line_to calls
19428 handle_line_prefix itself. */
19429 handle_line_prefix (it);
19432 /* Get the initial row height. This is either the height of the
19433 text hscrolled, if there is any, or zero. */
19434 row->ascent = it->max_ascent;
19435 row->height = it->max_ascent + it->max_descent;
19436 row->phys_ascent = it->max_phys_ascent;
19437 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19438 row->extra_line_spacing = it->max_extra_line_spacing;
19440 /* Utility macro to record max and min buffer positions seen until now. */
19441 #define RECORD_MAX_MIN_POS(IT) \
19442 do \
19444 int composition_p = !STRINGP ((IT)->string) \
19445 && ((IT)->what == IT_COMPOSITION); \
19446 ptrdiff_t current_pos = \
19447 composition_p ? (IT)->cmp_it.charpos \
19448 : IT_CHARPOS (*(IT)); \
19449 ptrdiff_t current_bpos = \
19450 composition_p ? CHAR_TO_BYTE (current_pos) \
19451 : IT_BYTEPOS (*(IT)); \
19452 if (current_pos < min_pos) \
19454 min_pos = current_pos; \
19455 min_bpos = current_bpos; \
19457 if (IT_CHARPOS (*it) > max_pos) \
19459 max_pos = IT_CHARPOS (*it); \
19460 max_bpos = IT_BYTEPOS (*it); \
19463 while (0)
19465 /* Loop generating characters. The loop is left with IT on the next
19466 character to display. */
19467 while (1)
19469 int n_glyphs_before, hpos_before, x_before;
19470 int x, nglyphs;
19471 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19473 /* Retrieve the next thing to display. Value is zero if end of
19474 buffer reached. */
19475 if (!get_next_display_element (it))
19477 /* Maybe add a space at the end of this line that is used to
19478 display the cursor there under X. Set the charpos of the
19479 first glyph of blank lines not corresponding to any text
19480 to -1. */
19481 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19482 row->exact_window_width_line_p = 1;
19483 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19484 || row->used[TEXT_AREA] == 0)
19486 row->glyphs[TEXT_AREA]->charpos = -1;
19487 row->displays_text_p = 0;
19489 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19490 && (!MINI_WINDOW_P (it->w)
19491 || (minibuf_level && EQ (it->window, minibuf_window))))
19492 row->indicate_empty_line_p = 1;
19495 it->continuation_lines_width = 0;
19496 row->ends_at_zv_p = 1;
19497 /* A row that displays right-to-left text must always have
19498 its last face extended all the way to the end of line,
19499 even if this row ends in ZV, because we still write to
19500 the screen left to right. We also need to extend the
19501 last face if the default face is remapped to some
19502 different face, otherwise the functions that clear
19503 portions of the screen will clear with the default face's
19504 background color. */
19505 if (row->reversed_p
19506 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19507 extend_face_to_end_of_line (it);
19508 break;
19511 /* Now, get the metrics of what we want to display. This also
19512 generates glyphs in `row' (which is IT->glyph_row). */
19513 n_glyphs_before = row->used[TEXT_AREA];
19514 x = it->current_x;
19516 /* Remember the line height so far in case the next element doesn't
19517 fit on the line. */
19518 if (it->line_wrap != TRUNCATE)
19520 ascent = it->max_ascent;
19521 descent = it->max_descent;
19522 phys_ascent = it->max_phys_ascent;
19523 phys_descent = it->max_phys_descent;
19525 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19527 if (IT_DISPLAYING_WHITESPACE (it))
19528 may_wrap = 1;
19529 else if (may_wrap)
19531 SAVE_IT (wrap_it, *it, wrap_data);
19532 wrap_x = x;
19533 wrap_row_used = row->used[TEXT_AREA];
19534 wrap_row_ascent = row->ascent;
19535 wrap_row_height = row->height;
19536 wrap_row_phys_ascent = row->phys_ascent;
19537 wrap_row_phys_height = row->phys_height;
19538 wrap_row_extra_line_spacing = row->extra_line_spacing;
19539 wrap_row_min_pos = min_pos;
19540 wrap_row_min_bpos = min_bpos;
19541 wrap_row_max_pos = max_pos;
19542 wrap_row_max_bpos = max_bpos;
19543 may_wrap = 0;
19548 PRODUCE_GLYPHS (it);
19550 /* If this display element was in marginal areas, continue with
19551 the next one. */
19552 if (it->area != TEXT_AREA)
19554 row->ascent = max (row->ascent, it->max_ascent);
19555 row->height = max (row->height, it->max_ascent + it->max_descent);
19556 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19557 row->phys_height = max (row->phys_height,
19558 it->max_phys_ascent + it->max_phys_descent);
19559 row->extra_line_spacing = max (row->extra_line_spacing,
19560 it->max_extra_line_spacing);
19561 set_iterator_to_next (it, 1);
19562 continue;
19565 /* Does the display element fit on the line? If we truncate
19566 lines, we should draw past the right edge of the window. If
19567 we don't truncate, we want to stop so that we can display the
19568 continuation glyph before the right margin. If lines are
19569 continued, there are two possible strategies for characters
19570 resulting in more than 1 glyph (e.g. tabs): Display as many
19571 glyphs as possible in this line and leave the rest for the
19572 continuation line, or display the whole element in the next
19573 line. Original redisplay did the former, so we do it also. */
19574 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19575 hpos_before = it->hpos;
19576 x_before = x;
19578 if (/* Not a newline. */
19579 nglyphs > 0
19580 /* Glyphs produced fit entirely in the line. */
19581 && it->current_x < it->last_visible_x)
19583 it->hpos += nglyphs;
19584 row->ascent = max (row->ascent, it->max_ascent);
19585 row->height = max (row->height, it->max_ascent + it->max_descent);
19586 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19587 row->phys_height = max (row->phys_height,
19588 it->max_phys_ascent + it->max_phys_descent);
19589 row->extra_line_spacing = max (row->extra_line_spacing,
19590 it->max_extra_line_spacing);
19591 if (it->current_x - it->pixel_width < it->first_visible_x)
19592 row->x = x - it->first_visible_x;
19593 /* Record the maximum and minimum buffer positions seen so
19594 far in glyphs that will be displayed by this row. */
19595 if (it->bidi_p)
19596 RECORD_MAX_MIN_POS (it);
19598 else
19600 int i, new_x;
19601 struct glyph *glyph;
19603 for (i = 0; i < nglyphs; ++i, x = new_x)
19605 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19606 new_x = x + glyph->pixel_width;
19608 if (/* Lines are continued. */
19609 it->line_wrap != TRUNCATE
19610 && (/* Glyph doesn't fit on the line. */
19611 new_x > it->last_visible_x
19612 /* Or it fits exactly on a window system frame. */
19613 || (new_x == it->last_visible_x
19614 && FRAME_WINDOW_P (it->f)
19615 && (row->reversed_p
19616 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19617 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19619 /* End of a continued line. */
19621 if (it->hpos == 0
19622 || (new_x == it->last_visible_x
19623 && FRAME_WINDOW_P (it->f)
19624 && (row->reversed_p
19625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19628 /* Current glyph is the only one on the line or
19629 fits exactly on the line. We must continue
19630 the line because we can't draw the cursor
19631 after the glyph. */
19632 row->continued_p = 1;
19633 it->current_x = new_x;
19634 it->continuation_lines_width += new_x;
19635 ++it->hpos;
19636 if (i == nglyphs - 1)
19638 /* If line-wrap is on, check if a previous
19639 wrap point was found. */
19640 if (wrap_row_used > 0
19641 /* Even if there is a previous wrap
19642 point, continue the line here as
19643 usual, if (i) the previous character
19644 was a space or tab AND (ii) the
19645 current character is not. */
19646 && (!may_wrap
19647 || IT_DISPLAYING_WHITESPACE (it)))
19648 goto back_to_wrap;
19650 /* Record the maximum and minimum buffer
19651 positions seen so far in glyphs that will be
19652 displayed by this row. */
19653 if (it->bidi_p)
19654 RECORD_MAX_MIN_POS (it);
19655 set_iterator_to_next (it, 1);
19656 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19658 if (!get_next_display_element (it))
19660 row->exact_window_width_line_p = 1;
19661 it->continuation_lines_width = 0;
19662 row->continued_p = 0;
19663 row->ends_at_zv_p = 1;
19665 else if (ITERATOR_AT_END_OF_LINE_P (it))
19667 row->continued_p = 0;
19668 row->exact_window_width_line_p = 1;
19672 else if (it->bidi_p)
19673 RECORD_MAX_MIN_POS (it);
19675 else if (CHAR_GLYPH_PADDING_P (*glyph)
19676 && !FRAME_WINDOW_P (it->f))
19678 /* A padding glyph that doesn't fit on this line.
19679 This means the whole character doesn't fit
19680 on the line. */
19681 if (row->reversed_p)
19682 unproduce_glyphs (it, row->used[TEXT_AREA]
19683 - n_glyphs_before);
19684 row->used[TEXT_AREA] = n_glyphs_before;
19686 /* Fill the rest of the row with continuation
19687 glyphs like in 20.x. */
19688 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19689 < row->glyphs[1 + TEXT_AREA])
19690 produce_special_glyphs (it, IT_CONTINUATION);
19692 row->continued_p = 1;
19693 it->current_x = x_before;
19694 it->continuation_lines_width += x_before;
19696 /* Restore the height to what it was before the
19697 element not fitting on the line. */
19698 it->max_ascent = ascent;
19699 it->max_descent = descent;
19700 it->max_phys_ascent = phys_ascent;
19701 it->max_phys_descent = phys_descent;
19703 else if (wrap_row_used > 0)
19705 back_to_wrap:
19706 if (row->reversed_p)
19707 unproduce_glyphs (it,
19708 row->used[TEXT_AREA] - wrap_row_used);
19709 RESTORE_IT (it, &wrap_it, wrap_data);
19710 it->continuation_lines_width += wrap_x;
19711 row->used[TEXT_AREA] = wrap_row_used;
19712 row->ascent = wrap_row_ascent;
19713 row->height = wrap_row_height;
19714 row->phys_ascent = wrap_row_phys_ascent;
19715 row->phys_height = wrap_row_phys_height;
19716 row->extra_line_spacing = wrap_row_extra_line_spacing;
19717 min_pos = wrap_row_min_pos;
19718 min_bpos = wrap_row_min_bpos;
19719 max_pos = wrap_row_max_pos;
19720 max_bpos = wrap_row_max_bpos;
19721 row->continued_p = 1;
19722 row->ends_at_zv_p = 0;
19723 row->exact_window_width_line_p = 0;
19724 it->continuation_lines_width += x;
19726 /* Make sure that a non-default face is extended
19727 up to the right margin of the window. */
19728 extend_face_to_end_of_line (it);
19730 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19732 /* A TAB that extends past the right edge of the
19733 window. This produces a single glyph on
19734 window system frames. We leave the glyph in
19735 this row and let it fill the row, but don't
19736 consume the TAB. */
19737 if ((row->reversed_p
19738 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19739 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19740 produce_special_glyphs (it, IT_CONTINUATION);
19741 it->continuation_lines_width += it->last_visible_x;
19742 row->ends_in_middle_of_char_p = 1;
19743 row->continued_p = 1;
19744 glyph->pixel_width = it->last_visible_x - x;
19745 it->starts_in_middle_of_char_p = 1;
19747 else
19749 /* Something other than a TAB that draws past
19750 the right edge of the window. Restore
19751 positions to values before the element. */
19752 if (row->reversed_p)
19753 unproduce_glyphs (it, row->used[TEXT_AREA]
19754 - (n_glyphs_before + i));
19755 row->used[TEXT_AREA] = n_glyphs_before + i;
19757 /* Display continuation glyphs. */
19758 it->current_x = x_before;
19759 it->continuation_lines_width += x;
19760 if (!FRAME_WINDOW_P (it->f)
19761 || (row->reversed_p
19762 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19763 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19764 produce_special_glyphs (it, IT_CONTINUATION);
19765 row->continued_p = 1;
19767 extend_face_to_end_of_line (it);
19769 if (nglyphs > 1 && i > 0)
19771 row->ends_in_middle_of_char_p = 1;
19772 it->starts_in_middle_of_char_p = 1;
19775 /* Restore the height to what it was before the
19776 element not fitting on the line. */
19777 it->max_ascent = ascent;
19778 it->max_descent = descent;
19779 it->max_phys_ascent = phys_ascent;
19780 it->max_phys_descent = phys_descent;
19783 break;
19785 else if (new_x > it->first_visible_x)
19787 /* Increment number of glyphs actually displayed. */
19788 ++it->hpos;
19790 /* Record the maximum and minimum buffer positions
19791 seen so far in glyphs that will be displayed by
19792 this row. */
19793 if (it->bidi_p)
19794 RECORD_MAX_MIN_POS (it);
19796 if (x < it->first_visible_x)
19797 /* Glyph is partially visible, i.e. row starts at
19798 negative X position. */
19799 row->x = x - it->first_visible_x;
19801 else
19803 /* Glyph is completely off the left margin of the
19804 window. This should not happen because of the
19805 move_it_in_display_line at the start of this
19806 function, unless the text display area of the
19807 window is empty. */
19808 eassert (it->first_visible_x <= it->last_visible_x);
19811 /* Even if this display element produced no glyphs at all,
19812 we want to record its position. */
19813 if (it->bidi_p && nglyphs == 0)
19814 RECORD_MAX_MIN_POS (it);
19816 row->ascent = max (row->ascent, it->max_ascent);
19817 row->height = max (row->height, it->max_ascent + it->max_descent);
19818 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19819 row->phys_height = max (row->phys_height,
19820 it->max_phys_ascent + it->max_phys_descent);
19821 row->extra_line_spacing = max (row->extra_line_spacing,
19822 it->max_extra_line_spacing);
19824 /* End of this display line if row is continued. */
19825 if (row->continued_p || row->ends_at_zv_p)
19826 break;
19829 at_end_of_line:
19830 /* Is this a line end? If yes, we're also done, after making
19831 sure that a non-default face is extended up to the right
19832 margin of the window. */
19833 if (ITERATOR_AT_END_OF_LINE_P (it))
19835 int used_before = row->used[TEXT_AREA];
19837 row->ends_in_newline_from_string_p = STRINGP (it->object);
19839 /* Add a space at the end of the line that is used to
19840 display the cursor there. */
19841 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19842 append_space_for_newline (it, 0);
19844 /* Extend the face to the end of the line. */
19845 extend_face_to_end_of_line (it);
19847 /* Make sure we have the position. */
19848 if (used_before == 0)
19849 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19851 /* Record the position of the newline, for use in
19852 find_row_edges. */
19853 it->eol_pos = it->current.pos;
19855 /* Consume the line end. This skips over invisible lines. */
19856 set_iterator_to_next (it, 1);
19857 it->continuation_lines_width = 0;
19858 break;
19861 /* Proceed with next display element. Note that this skips
19862 over lines invisible because of selective display. */
19863 set_iterator_to_next (it, 1);
19865 /* If we truncate lines, we are done when the last displayed
19866 glyphs reach past the right margin of the window. */
19867 if (it->line_wrap == TRUNCATE
19868 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19869 ? (it->current_x >= it->last_visible_x)
19870 : (it->current_x > it->last_visible_x)))
19872 /* Maybe add truncation glyphs. */
19873 if (!FRAME_WINDOW_P (it->f)
19874 || (row->reversed_p
19875 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19876 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19878 int i, n;
19880 if (!row->reversed_p)
19882 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19883 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19884 break;
19886 else
19888 for (i = 0; i < row->used[TEXT_AREA]; i++)
19889 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19890 break;
19891 /* Remove any padding glyphs at the front of ROW, to
19892 make room for the truncation glyphs we will be
19893 adding below. The loop below always inserts at
19894 least one truncation glyph, so also remove the
19895 last glyph added to ROW. */
19896 unproduce_glyphs (it, i + 1);
19897 /* Adjust i for the loop below. */
19898 i = row->used[TEXT_AREA] - (i + 1);
19901 it->current_x = x_before;
19902 if (!FRAME_WINDOW_P (it->f))
19904 for (n = row->used[TEXT_AREA]; i < n; ++i)
19906 row->used[TEXT_AREA] = i;
19907 produce_special_glyphs (it, IT_TRUNCATION);
19910 else
19912 row->used[TEXT_AREA] = i;
19913 produce_special_glyphs (it, IT_TRUNCATION);
19916 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19918 /* Don't truncate if we can overflow newline into fringe. */
19919 if (!get_next_display_element (it))
19921 it->continuation_lines_width = 0;
19922 row->ends_at_zv_p = 1;
19923 row->exact_window_width_line_p = 1;
19924 break;
19926 if (ITERATOR_AT_END_OF_LINE_P (it))
19928 row->exact_window_width_line_p = 1;
19929 goto at_end_of_line;
19931 it->current_x = x_before;
19934 row->truncated_on_right_p = 1;
19935 it->continuation_lines_width = 0;
19936 reseat_at_next_visible_line_start (it, 0);
19937 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19938 it->hpos = hpos_before;
19939 break;
19943 if (wrap_data)
19944 bidi_unshelve_cache (wrap_data, 1);
19946 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19947 at the left window margin. */
19948 if (it->first_visible_x
19949 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19951 if (!FRAME_WINDOW_P (it->f)
19952 || (row->reversed_p
19953 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19954 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19955 insert_left_trunc_glyphs (it);
19956 row->truncated_on_left_p = 1;
19959 /* Remember the position at which this line ends.
19961 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19962 cannot be before the call to find_row_edges below, since that is
19963 where these positions are determined. */
19964 row->end = it->current;
19965 if (!it->bidi_p)
19967 row->minpos = row->start.pos;
19968 row->maxpos = row->end.pos;
19970 else
19972 /* ROW->minpos and ROW->maxpos must be the smallest and
19973 `1 + the largest' buffer positions in ROW. But if ROW was
19974 bidi-reordered, these two positions can be anywhere in the
19975 row, so we must determine them now. */
19976 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19979 /* If the start of this line is the overlay arrow-position, then
19980 mark this glyph row as the one containing the overlay arrow.
19981 This is clearly a mess with variable size fonts. It would be
19982 better to let it be displayed like cursors under X. */
19983 if ((row->displays_text_p || !overlay_arrow_seen)
19984 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19985 !NILP (overlay_arrow_string)))
19987 /* Overlay arrow in window redisplay is a fringe bitmap. */
19988 if (STRINGP (overlay_arrow_string))
19990 struct glyph_row *arrow_row
19991 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19992 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19993 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19994 struct glyph *p = row->glyphs[TEXT_AREA];
19995 struct glyph *p2, *end;
19997 /* Copy the arrow glyphs. */
19998 while (glyph < arrow_end)
19999 *p++ = *glyph++;
20001 /* Throw away padding glyphs. */
20002 p2 = p;
20003 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20004 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20005 ++p2;
20006 if (p2 > p)
20008 while (p2 < end)
20009 *p++ = *p2++;
20010 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20013 else
20015 eassert (INTEGERP (overlay_arrow_string));
20016 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20018 overlay_arrow_seen = 1;
20021 /* Highlight trailing whitespace. */
20022 if (!NILP (Vshow_trailing_whitespace))
20023 highlight_trailing_whitespace (it->f, it->glyph_row);
20025 /* Compute pixel dimensions of this line. */
20026 compute_line_metrics (it);
20028 /* Implementation note: No changes in the glyphs of ROW or in their
20029 faces can be done past this point, because compute_line_metrics
20030 computes ROW's hash value and stores it within the glyph_row
20031 structure. */
20033 /* Record whether this row ends inside an ellipsis. */
20034 row->ends_in_ellipsis_p
20035 = (it->method == GET_FROM_DISPLAY_VECTOR
20036 && it->ellipsis_p);
20038 /* Save fringe bitmaps in this row. */
20039 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20040 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20041 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20042 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20044 it->left_user_fringe_bitmap = 0;
20045 it->left_user_fringe_face_id = 0;
20046 it->right_user_fringe_bitmap = 0;
20047 it->right_user_fringe_face_id = 0;
20049 /* Maybe set the cursor. */
20050 cvpos = it->w->cursor.vpos;
20051 if ((cvpos < 0
20052 /* In bidi-reordered rows, keep checking for proper cursor
20053 position even if one has been found already, because buffer
20054 positions in such rows change non-linearly with ROW->VPOS,
20055 when a line is continued. One exception: when we are at ZV,
20056 display cursor on the first suitable glyph row, since all
20057 the empty rows after that also have their position set to ZV. */
20058 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20059 lines' rows is implemented for bidi-reordered rows. */
20060 || (it->bidi_p
20061 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20062 && PT >= MATRIX_ROW_START_CHARPOS (row)
20063 && PT <= MATRIX_ROW_END_CHARPOS (row)
20064 && cursor_row_p (row))
20065 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20067 /* Prepare for the next line. This line starts horizontally at (X
20068 HPOS) = (0 0). Vertical positions are incremented. As a
20069 convenience for the caller, IT->glyph_row is set to the next
20070 row to be used. */
20071 it->current_x = it->hpos = 0;
20072 it->current_y += row->height;
20073 SET_TEXT_POS (it->eol_pos, 0, 0);
20074 ++it->vpos;
20075 ++it->glyph_row;
20076 /* The next row should by default use the same value of the
20077 reversed_p flag as this one. set_iterator_to_next decides when
20078 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20079 the flag accordingly. */
20080 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20081 it->glyph_row->reversed_p = row->reversed_p;
20082 it->start = row->end;
20083 return row->displays_text_p;
20085 #undef RECORD_MAX_MIN_POS
20088 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20089 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20090 doc: /* Return paragraph direction at point in BUFFER.
20091 Value is either `left-to-right' or `right-to-left'.
20092 If BUFFER is omitted or nil, it defaults to the current buffer.
20094 Paragraph direction determines how the text in the paragraph is displayed.
20095 In left-to-right paragraphs, text begins at the left margin of the window
20096 and the reading direction is generally left to right. In right-to-left
20097 paragraphs, text begins at the right margin and is read from right to left.
20099 See also `bidi-paragraph-direction'. */)
20100 (Lisp_Object buffer)
20102 struct buffer *buf = current_buffer;
20103 struct buffer *old = buf;
20105 if (! NILP (buffer))
20107 CHECK_BUFFER (buffer);
20108 buf = XBUFFER (buffer);
20111 if (NILP (BVAR (buf, bidi_display_reordering))
20112 || NILP (BVAR (buf, enable_multibyte_characters))
20113 /* When we are loading loadup.el, the character property tables
20114 needed for bidi iteration are not yet available. */
20115 || !NILP (Vpurify_flag))
20116 return Qleft_to_right;
20117 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20118 return BVAR (buf, bidi_paragraph_direction);
20119 else
20121 /* Determine the direction from buffer text. We could try to
20122 use current_matrix if it is up to date, but this seems fast
20123 enough as it is. */
20124 struct bidi_it itb;
20125 ptrdiff_t pos = BUF_PT (buf);
20126 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20127 int c;
20128 void *itb_data = bidi_shelve_cache ();
20130 set_buffer_temp (buf);
20131 /* bidi_paragraph_init finds the base direction of the paragraph
20132 by searching forward from paragraph start. We need the base
20133 direction of the current or _previous_ paragraph, so we need
20134 to make sure we are within that paragraph. To that end, find
20135 the previous non-empty line. */
20136 if (pos >= ZV && pos > BEGV)
20138 pos--;
20139 bytepos = CHAR_TO_BYTE (pos);
20141 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20142 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20144 while ((c = FETCH_BYTE (bytepos)) == '\n'
20145 || c == ' ' || c == '\t' || c == '\f')
20147 if (bytepos <= BEGV_BYTE)
20148 break;
20149 bytepos--;
20150 pos--;
20152 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20153 bytepos--;
20155 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20156 itb.paragraph_dir = NEUTRAL_DIR;
20157 itb.string.s = NULL;
20158 itb.string.lstring = Qnil;
20159 itb.string.bufpos = 0;
20160 itb.string.unibyte = 0;
20161 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20162 bidi_unshelve_cache (itb_data, 0);
20163 set_buffer_temp (old);
20164 switch (itb.paragraph_dir)
20166 case L2R:
20167 return Qleft_to_right;
20168 break;
20169 case R2L:
20170 return Qright_to_left;
20171 break;
20172 default:
20173 emacs_abort ();
20180 /***********************************************************************
20181 Menu Bar
20182 ***********************************************************************/
20184 /* Redisplay the menu bar in the frame for window W.
20186 The menu bar of X frames that don't have X toolkit support is
20187 displayed in a special window W->frame->menu_bar_window.
20189 The menu bar of terminal frames is treated specially as far as
20190 glyph matrices are concerned. Menu bar lines are not part of
20191 windows, so the update is done directly on the frame matrix rows
20192 for the menu bar. */
20194 static void
20195 display_menu_bar (struct window *w)
20197 struct frame *f = XFRAME (WINDOW_FRAME (w));
20198 struct it it;
20199 Lisp_Object items;
20200 int i;
20202 /* Don't do all this for graphical frames. */
20203 #ifdef HAVE_NTGUI
20204 if (FRAME_W32_P (f))
20205 return;
20206 #endif
20207 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20208 if (FRAME_X_P (f))
20209 return;
20210 #endif
20212 #ifdef HAVE_NS
20213 if (FRAME_NS_P (f))
20214 return;
20215 #endif /* HAVE_NS */
20217 #ifdef USE_X_TOOLKIT
20218 eassert (!FRAME_WINDOW_P (f));
20219 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20220 it.first_visible_x = 0;
20221 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20222 #else /* not USE_X_TOOLKIT */
20223 if (FRAME_WINDOW_P (f))
20225 /* Menu bar lines are displayed in the desired matrix of the
20226 dummy window menu_bar_window. */
20227 struct window *menu_w;
20228 eassert (WINDOWP (f->menu_bar_window));
20229 menu_w = XWINDOW (f->menu_bar_window);
20230 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20231 MENU_FACE_ID);
20232 it.first_visible_x = 0;
20233 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20235 else
20237 /* This is a TTY frame, i.e. character hpos/vpos are used as
20238 pixel x/y. */
20239 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20240 MENU_FACE_ID);
20241 it.first_visible_x = 0;
20242 it.last_visible_x = FRAME_COLS (f);
20244 #endif /* not USE_X_TOOLKIT */
20246 /* FIXME: This should be controlled by a user option. See the
20247 comments in redisplay_tool_bar and display_mode_line about
20248 this. */
20249 it.paragraph_embedding = L2R;
20251 /* Clear all rows of the menu bar. */
20252 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20254 struct glyph_row *row = it.glyph_row + i;
20255 clear_glyph_row (row);
20256 row->enabled_p = 1;
20257 row->full_width_p = 1;
20260 /* Display all items of the menu bar. */
20261 items = FRAME_MENU_BAR_ITEMS (it.f);
20262 for (i = 0; i < ASIZE (items); i += 4)
20264 Lisp_Object string;
20266 /* Stop at nil string. */
20267 string = AREF (items, i + 1);
20268 if (NILP (string))
20269 break;
20271 /* Remember where item was displayed. */
20272 ASET (items, i + 3, make_number (it.hpos));
20274 /* Display the item, pad with one space. */
20275 if (it.current_x < it.last_visible_x)
20276 display_string (NULL, string, Qnil, 0, 0, &it,
20277 SCHARS (string) + 1, 0, 0, -1);
20280 /* Fill out the line with spaces. */
20281 if (it.current_x < it.last_visible_x)
20282 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20284 /* Compute the total height of the lines. */
20285 compute_line_metrics (&it);
20290 /***********************************************************************
20291 Mode Line
20292 ***********************************************************************/
20294 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20295 FORCE is non-zero, redisplay mode lines unconditionally.
20296 Otherwise, redisplay only mode lines that are garbaged. Value is
20297 the number of windows whose mode lines were redisplayed. */
20299 static int
20300 redisplay_mode_lines (Lisp_Object window, int force)
20302 int nwindows = 0;
20304 while (!NILP (window))
20306 struct window *w = XWINDOW (window);
20308 if (WINDOWP (w->hchild))
20309 nwindows += redisplay_mode_lines (w->hchild, force);
20310 else if (WINDOWP (w->vchild))
20311 nwindows += redisplay_mode_lines (w->vchild, force);
20312 else if (force
20313 || FRAME_GARBAGED_P (XFRAME (w->frame))
20314 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20316 struct text_pos lpoint;
20317 struct buffer *old = current_buffer;
20319 /* Set the window's buffer for the mode line display. */
20320 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20321 set_buffer_internal_1 (XBUFFER (w->buffer));
20323 /* Point refers normally to the selected window. For any
20324 other window, set up appropriate value. */
20325 if (!EQ (window, selected_window))
20327 struct text_pos pt;
20329 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20330 if (CHARPOS (pt) < BEGV)
20331 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20332 else if (CHARPOS (pt) > (ZV - 1))
20333 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20334 else
20335 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20338 /* Display mode lines. */
20339 clear_glyph_matrix (w->desired_matrix);
20340 if (display_mode_lines (w))
20342 ++nwindows;
20343 w->must_be_updated_p = 1;
20346 /* Restore old settings. */
20347 set_buffer_internal_1 (old);
20348 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20351 window = w->next;
20354 return nwindows;
20358 /* Display the mode and/or header line of window W. Value is the
20359 sum number of mode lines and header lines displayed. */
20361 static int
20362 display_mode_lines (struct window *w)
20364 Lisp_Object old_selected_window, old_selected_frame;
20365 int n = 0;
20367 old_selected_frame = selected_frame;
20368 selected_frame = w->frame;
20369 old_selected_window = selected_window;
20370 XSETWINDOW (selected_window, w);
20372 /* These will be set while the mode line specs are processed. */
20373 line_number_displayed = 0;
20374 wset_column_number_displayed (w, Qnil);
20376 if (WINDOW_WANTS_MODELINE_P (w))
20378 struct window *sel_w = XWINDOW (old_selected_window);
20380 /* Select mode line face based on the real selected window. */
20381 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20382 BVAR (current_buffer, mode_line_format));
20383 ++n;
20386 if (WINDOW_WANTS_HEADER_LINE_P (w))
20388 display_mode_line (w, HEADER_LINE_FACE_ID,
20389 BVAR (current_buffer, header_line_format));
20390 ++n;
20393 selected_frame = old_selected_frame;
20394 selected_window = old_selected_window;
20395 return n;
20399 /* Display mode or header line of window W. FACE_ID specifies which
20400 line to display; it is either MODE_LINE_FACE_ID or
20401 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20402 display. Value is the pixel height of the mode/header line
20403 displayed. */
20405 static int
20406 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20408 struct it it;
20409 struct face *face;
20410 ptrdiff_t count = SPECPDL_INDEX ();
20412 init_iterator (&it, w, -1, -1, NULL, face_id);
20413 /* Don't extend on a previously drawn mode-line.
20414 This may happen if called from pos_visible_p. */
20415 it.glyph_row->enabled_p = 0;
20416 prepare_desired_row (it.glyph_row);
20418 it.glyph_row->mode_line_p = 1;
20420 /* FIXME: This should be controlled by a user option. But
20421 supporting such an option is not trivial, since the mode line is
20422 made up of many separate strings. */
20423 it.paragraph_embedding = L2R;
20425 record_unwind_protect (unwind_format_mode_line,
20426 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20428 mode_line_target = MODE_LINE_DISPLAY;
20430 /* Temporarily make frame's keyboard the current kboard so that
20431 kboard-local variables in the mode_line_format will get the right
20432 values. */
20433 push_kboard (FRAME_KBOARD (it.f));
20434 record_unwind_save_match_data ();
20435 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20436 pop_kboard ();
20438 unbind_to (count, Qnil);
20440 /* Fill up with spaces. */
20441 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20443 compute_line_metrics (&it);
20444 it.glyph_row->full_width_p = 1;
20445 it.glyph_row->continued_p = 0;
20446 it.glyph_row->truncated_on_left_p = 0;
20447 it.glyph_row->truncated_on_right_p = 0;
20449 /* Make a 3D mode-line have a shadow at its right end. */
20450 face = FACE_FROM_ID (it.f, face_id);
20451 extend_face_to_end_of_line (&it);
20452 if (face->box != FACE_NO_BOX)
20454 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20455 + it.glyph_row->used[TEXT_AREA] - 1);
20456 last->right_box_line_p = 1;
20459 return it.glyph_row->height;
20462 /* Move element ELT in LIST to the front of LIST.
20463 Return the updated list. */
20465 static Lisp_Object
20466 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20468 register Lisp_Object tail, prev;
20469 register Lisp_Object tem;
20471 tail = list;
20472 prev = Qnil;
20473 while (CONSP (tail))
20475 tem = XCAR (tail);
20477 if (EQ (elt, tem))
20479 /* Splice out the link TAIL. */
20480 if (NILP (prev))
20481 list = XCDR (tail);
20482 else
20483 Fsetcdr (prev, XCDR (tail));
20485 /* Now make it the first. */
20486 Fsetcdr (tail, list);
20487 return tail;
20489 else
20490 prev = tail;
20491 tail = XCDR (tail);
20492 QUIT;
20495 /* Not found--return unchanged LIST. */
20496 return list;
20499 /* Contribute ELT to the mode line for window IT->w. How it
20500 translates into text depends on its data type.
20502 IT describes the display environment in which we display, as usual.
20504 DEPTH is the depth in recursion. It is used to prevent
20505 infinite recursion here.
20507 FIELD_WIDTH is the number of characters the display of ELT should
20508 occupy in the mode line, and PRECISION is the maximum number of
20509 characters to display from ELT's representation. See
20510 display_string for details.
20512 Returns the hpos of the end of the text generated by ELT.
20514 PROPS is a property list to add to any string we encounter.
20516 If RISKY is nonzero, remove (disregard) any properties in any string
20517 we encounter, and ignore :eval and :propertize.
20519 The global variable `mode_line_target' determines whether the
20520 output is passed to `store_mode_line_noprop',
20521 `store_mode_line_string', or `display_string'. */
20523 static int
20524 display_mode_element (struct it *it, int depth, int field_width, int precision,
20525 Lisp_Object elt, Lisp_Object props, int risky)
20527 int n = 0, field, prec;
20528 int literal = 0;
20530 tail_recurse:
20531 if (depth > 100)
20532 elt = build_string ("*too-deep*");
20534 depth++;
20536 switch (XTYPE (elt))
20538 case Lisp_String:
20540 /* A string: output it and check for %-constructs within it. */
20541 unsigned char c;
20542 ptrdiff_t offset = 0;
20544 if (SCHARS (elt) > 0
20545 && (!NILP (props) || risky))
20547 Lisp_Object oprops, aelt;
20548 oprops = Ftext_properties_at (make_number (0), elt);
20550 /* If the starting string's properties are not what
20551 we want, translate the string. Also, if the string
20552 is risky, do that anyway. */
20554 if (NILP (Fequal (props, oprops)) || risky)
20556 /* If the starting string has properties,
20557 merge the specified ones onto the existing ones. */
20558 if (! NILP (oprops) && !risky)
20560 Lisp_Object tem;
20562 oprops = Fcopy_sequence (oprops);
20563 tem = props;
20564 while (CONSP (tem))
20566 oprops = Fplist_put (oprops, XCAR (tem),
20567 XCAR (XCDR (tem)));
20568 tem = XCDR (XCDR (tem));
20570 props = oprops;
20573 aelt = Fassoc (elt, mode_line_proptrans_alist);
20574 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20576 /* AELT is what we want. Move it to the front
20577 without consing. */
20578 elt = XCAR (aelt);
20579 mode_line_proptrans_alist
20580 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20582 else
20584 Lisp_Object tem;
20586 /* If AELT has the wrong props, it is useless.
20587 so get rid of it. */
20588 if (! NILP (aelt))
20589 mode_line_proptrans_alist
20590 = Fdelq (aelt, mode_line_proptrans_alist);
20592 elt = Fcopy_sequence (elt);
20593 Fset_text_properties (make_number (0), Flength (elt),
20594 props, elt);
20595 /* Add this item to mode_line_proptrans_alist. */
20596 mode_line_proptrans_alist
20597 = Fcons (Fcons (elt, props),
20598 mode_line_proptrans_alist);
20599 /* Truncate mode_line_proptrans_alist
20600 to at most 50 elements. */
20601 tem = Fnthcdr (make_number (50),
20602 mode_line_proptrans_alist);
20603 if (! NILP (tem))
20604 XSETCDR (tem, Qnil);
20609 offset = 0;
20611 if (literal)
20613 prec = precision - n;
20614 switch (mode_line_target)
20616 case MODE_LINE_NOPROP:
20617 case MODE_LINE_TITLE:
20618 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20619 break;
20620 case MODE_LINE_STRING:
20621 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20622 break;
20623 case MODE_LINE_DISPLAY:
20624 n += display_string (NULL, elt, Qnil, 0, 0, it,
20625 0, prec, 0, STRING_MULTIBYTE (elt));
20626 break;
20629 break;
20632 /* Handle the non-literal case. */
20634 while ((precision <= 0 || n < precision)
20635 && SREF (elt, offset) != 0
20636 && (mode_line_target != MODE_LINE_DISPLAY
20637 || it->current_x < it->last_visible_x))
20639 ptrdiff_t last_offset = offset;
20641 /* Advance to end of string or next format specifier. */
20642 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20645 if (offset - 1 != last_offset)
20647 ptrdiff_t nchars, nbytes;
20649 /* Output to end of string or up to '%'. Field width
20650 is length of string. Don't output more than
20651 PRECISION allows us. */
20652 offset--;
20654 prec = c_string_width (SDATA (elt) + last_offset,
20655 offset - last_offset, precision - n,
20656 &nchars, &nbytes);
20658 switch (mode_line_target)
20660 case MODE_LINE_NOPROP:
20661 case MODE_LINE_TITLE:
20662 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20663 break;
20664 case MODE_LINE_STRING:
20666 ptrdiff_t bytepos = last_offset;
20667 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20668 ptrdiff_t endpos = (precision <= 0
20669 ? string_byte_to_char (elt, offset)
20670 : charpos + nchars);
20672 n += store_mode_line_string (NULL,
20673 Fsubstring (elt, make_number (charpos),
20674 make_number (endpos)),
20675 0, 0, 0, Qnil);
20677 break;
20678 case MODE_LINE_DISPLAY:
20680 ptrdiff_t bytepos = last_offset;
20681 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20683 if (precision <= 0)
20684 nchars = string_byte_to_char (elt, offset) - charpos;
20685 n += display_string (NULL, elt, Qnil, 0, charpos,
20686 it, 0, nchars, 0,
20687 STRING_MULTIBYTE (elt));
20689 break;
20692 else /* c == '%' */
20694 ptrdiff_t percent_position = offset;
20696 /* Get the specified minimum width. Zero means
20697 don't pad. */
20698 field = 0;
20699 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20700 field = field * 10 + c - '0';
20702 /* Don't pad beyond the total padding allowed. */
20703 if (field_width - n > 0 && field > field_width - n)
20704 field = field_width - n;
20706 /* Note that either PRECISION <= 0 or N < PRECISION. */
20707 prec = precision - n;
20709 if (c == 'M')
20710 n += display_mode_element (it, depth, field, prec,
20711 Vglobal_mode_string, props,
20712 risky);
20713 else if (c != 0)
20715 int multibyte;
20716 ptrdiff_t bytepos, charpos;
20717 const char *spec;
20718 Lisp_Object string;
20720 bytepos = percent_position;
20721 charpos = (STRING_MULTIBYTE (elt)
20722 ? string_byte_to_char (elt, bytepos)
20723 : bytepos);
20724 spec = decode_mode_spec (it->w, c, field, &string);
20725 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20727 switch (mode_line_target)
20729 case MODE_LINE_NOPROP:
20730 case MODE_LINE_TITLE:
20731 n += store_mode_line_noprop (spec, field, prec);
20732 break;
20733 case MODE_LINE_STRING:
20735 Lisp_Object tem = build_string (spec);
20736 props = Ftext_properties_at (make_number (charpos), elt);
20737 /* Should only keep face property in props */
20738 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20740 break;
20741 case MODE_LINE_DISPLAY:
20743 int nglyphs_before, nwritten;
20745 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20746 nwritten = display_string (spec, string, elt,
20747 charpos, 0, it,
20748 field, prec, 0,
20749 multibyte);
20751 /* Assign to the glyphs written above the
20752 string where the `%x' came from, position
20753 of the `%'. */
20754 if (nwritten > 0)
20756 struct glyph *glyph
20757 = (it->glyph_row->glyphs[TEXT_AREA]
20758 + nglyphs_before);
20759 int i;
20761 for (i = 0; i < nwritten; ++i)
20763 glyph[i].object = elt;
20764 glyph[i].charpos = charpos;
20767 n += nwritten;
20770 break;
20773 else /* c == 0 */
20774 break;
20778 break;
20780 case Lisp_Symbol:
20781 /* A symbol: process the value of the symbol recursively
20782 as if it appeared here directly. Avoid error if symbol void.
20783 Special case: if value of symbol is a string, output the string
20784 literally. */
20786 register Lisp_Object tem;
20788 /* If the variable is not marked as risky to set
20789 then its contents are risky to use. */
20790 if (NILP (Fget (elt, Qrisky_local_variable)))
20791 risky = 1;
20793 tem = Fboundp (elt);
20794 if (!NILP (tem))
20796 tem = Fsymbol_value (elt);
20797 /* If value is a string, output that string literally:
20798 don't check for % within it. */
20799 if (STRINGP (tem))
20800 literal = 1;
20802 if (!EQ (tem, elt))
20804 /* Give up right away for nil or t. */
20805 elt = tem;
20806 goto tail_recurse;
20810 break;
20812 case Lisp_Cons:
20814 register Lisp_Object car, tem;
20816 /* A cons cell: five distinct cases.
20817 If first element is :eval or :propertize, do something special.
20818 If first element is a string or a cons, process all the elements
20819 and effectively concatenate them.
20820 If first element is a negative number, truncate displaying cdr to
20821 at most that many characters. If positive, pad (with spaces)
20822 to at least that many characters.
20823 If first element is a symbol, process the cadr or caddr recursively
20824 according to whether the symbol's value is non-nil or nil. */
20825 car = XCAR (elt);
20826 if (EQ (car, QCeval))
20828 /* An element of the form (:eval FORM) means evaluate FORM
20829 and use the result as mode line elements. */
20831 if (risky)
20832 break;
20834 if (CONSP (XCDR (elt)))
20836 Lisp_Object spec;
20837 spec = safe_eval (XCAR (XCDR (elt)));
20838 n += display_mode_element (it, depth, field_width - n,
20839 precision - n, spec, props,
20840 risky);
20843 else if (EQ (car, QCpropertize))
20845 /* An element of the form (:propertize ELT PROPS...)
20846 means display ELT but applying properties PROPS. */
20848 if (risky)
20849 break;
20851 if (CONSP (XCDR (elt)))
20852 n += display_mode_element (it, depth, field_width - n,
20853 precision - n, XCAR (XCDR (elt)),
20854 XCDR (XCDR (elt)), risky);
20856 else if (SYMBOLP (car))
20858 tem = Fboundp (car);
20859 elt = XCDR (elt);
20860 if (!CONSP (elt))
20861 goto invalid;
20862 /* elt is now the cdr, and we know it is a cons cell.
20863 Use its car if CAR has a non-nil value. */
20864 if (!NILP (tem))
20866 tem = Fsymbol_value (car);
20867 if (!NILP (tem))
20869 elt = XCAR (elt);
20870 goto tail_recurse;
20873 /* Symbol's value is nil (or symbol is unbound)
20874 Get the cddr of the original list
20875 and if possible find the caddr and use that. */
20876 elt = XCDR (elt);
20877 if (NILP (elt))
20878 break;
20879 else if (!CONSP (elt))
20880 goto invalid;
20881 elt = XCAR (elt);
20882 goto tail_recurse;
20884 else if (INTEGERP (car))
20886 register int lim = XINT (car);
20887 elt = XCDR (elt);
20888 if (lim < 0)
20890 /* Negative int means reduce maximum width. */
20891 if (precision <= 0)
20892 precision = -lim;
20893 else
20894 precision = min (precision, -lim);
20896 else if (lim > 0)
20898 /* Padding specified. Don't let it be more than
20899 current maximum. */
20900 if (precision > 0)
20901 lim = min (precision, lim);
20903 /* If that's more padding than already wanted, queue it.
20904 But don't reduce padding already specified even if
20905 that is beyond the current truncation point. */
20906 field_width = max (lim, field_width);
20908 goto tail_recurse;
20910 else if (STRINGP (car) || CONSP (car))
20912 Lisp_Object halftail = elt;
20913 int len = 0;
20915 while (CONSP (elt)
20916 && (precision <= 0 || n < precision))
20918 n += display_mode_element (it, depth,
20919 /* Do padding only after the last
20920 element in the list. */
20921 (! CONSP (XCDR (elt))
20922 ? field_width - n
20923 : 0),
20924 precision - n, XCAR (elt),
20925 props, risky);
20926 elt = XCDR (elt);
20927 len++;
20928 if ((len & 1) == 0)
20929 halftail = XCDR (halftail);
20930 /* Check for cycle. */
20931 if (EQ (halftail, elt))
20932 break;
20936 break;
20938 default:
20939 invalid:
20940 elt = build_string ("*invalid*");
20941 goto tail_recurse;
20944 /* Pad to FIELD_WIDTH. */
20945 if (field_width > 0 && n < field_width)
20947 switch (mode_line_target)
20949 case MODE_LINE_NOPROP:
20950 case MODE_LINE_TITLE:
20951 n += store_mode_line_noprop ("", field_width - n, 0);
20952 break;
20953 case MODE_LINE_STRING:
20954 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20955 break;
20956 case MODE_LINE_DISPLAY:
20957 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20958 0, 0, 0);
20959 break;
20963 return n;
20966 /* Store a mode-line string element in mode_line_string_list.
20968 If STRING is non-null, display that C string. Otherwise, the Lisp
20969 string LISP_STRING is displayed.
20971 FIELD_WIDTH is the minimum number of output glyphs to produce.
20972 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20973 with spaces. FIELD_WIDTH <= 0 means don't pad.
20975 PRECISION is the maximum number of characters to output from
20976 STRING. PRECISION <= 0 means don't truncate the string.
20978 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20979 properties to the string.
20981 PROPS are the properties to add to the string.
20982 The mode_line_string_face face property is always added to the string.
20985 static int
20986 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20987 int field_width, int precision, Lisp_Object props)
20989 ptrdiff_t len;
20990 int n = 0;
20992 if (string != NULL)
20994 len = strlen (string);
20995 if (precision > 0 && len > precision)
20996 len = precision;
20997 lisp_string = make_string (string, len);
20998 if (NILP (props))
20999 props = mode_line_string_face_prop;
21000 else if (!NILP (mode_line_string_face))
21002 Lisp_Object face = Fplist_get (props, Qface);
21003 props = Fcopy_sequence (props);
21004 if (NILP (face))
21005 face = mode_line_string_face;
21006 else
21007 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21008 props = Fplist_put (props, Qface, face);
21010 Fadd_text_properties (make_number (0), make_number (len),
21011 props, lisp_string);
21013 else
21015 len = XFASTINT (Flength (lisp_string));
21016 if (precision > 0 && len > precision)
21018 len = precision;
21019 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21020 precision = -1;
21022 if (!NILP (mode_line_string_face))
21024 Lisp_Object face;
21025 if (NILP (props))
21026 props = Ftext_properties_at (make_number (0), lisp_string);
21027 face = Fplist_get (props, Qface);
21028 if (NILP (face))
21029 face = mode_line_string_face;
21030 else
21031 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21032 props = Fcons (Qface, Fcons (face, Qnil));
21033 if (copy_string)
21034 lisp_string = Fcopy_sequence (lisp_string);
21036 if (!NILP (props))
21037 Fadd_text_properties (make_number (0), make_number (len),
21038 props, lisp_string);
21041 if (len > 0)
21043 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21044 n += len;
21047 if (field_width > len)
21049 field_width -= len;
21050 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21051 if (!NILP (props))
21052 Fadd_text_properties (make_number (0), make_number (field_width),
21053 props, lisp_string);
21054 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21055 n += field_width;
21058 return n;
21062 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21063 1, 4, 0,
21064 doc: /* Format a string out of a mode line format specification.
21065 First arg FORMAT specifies the mode line format (see `mode-line-format'
21066 for details) to use.
21068 By default, the format is evaluated for the currently selected window.
21070 Optional second arg FACE specifies the face property to put on all
21071 characters for which no face is specified. The value nil means the
21072 default face. The value t means whatever face the window's mode line
21073 currently uses (either `mode-line' or `mode-line-inactive',
21074 depending on whether the window is the selected window or not).
21075 An integer value means the value string has no text
21076 properties.
21078 Optional third and fourth args WINDOW and BUFFER specify the window
21079 and buffer to use as the context for the formatting (defaults
21080 are the selected window and the WINDOW's buffer). */)
21081 (Lisp_Object format, Lisp_Object face,
21082 Lisp_Object window, Lisp_Object buffer)
21084 struct it it;
21085 int len;
21086 struct window *w;
21087 struct buffer *old_buffer = NULL;
21088 int face_id;
21089 int no_props = INTEGERP (face);
21090 ptrdiff_t count = SPECPDL_INDEX ();
21091 Lisp_Object str;
21092 int string_start = 0;
21094 if (NILP (window))
21095 window = selected_window;
21096 CHECK_WINDOW (window);
21097 w = XWINDOW (window);
21099 if (NILP (buffer))
21100 buffer = w->buffer;
21101 CHECK_BUFFER (buffer);
21103 /* Make formatting the modeline a non-op when noninteractive, otherwise
21104 there will be problems later caused by a partially initialized frame. */
21105 if (NILP (format) || noninteractive)
21106 return empty_unibyte_string;
21108 if (no_props)
21109 face = Qnil;
21111 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21112 : EQ (face, Qt) ? (EQ (window, selected_window)
21113 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21114 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21115 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21116 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21117 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21118 : DEFAULT_FACE_ID;
21120 old_buffer = current_buffer;
21122 /* Save things including mode_line_proptrans_alist,
21123 and set that to nil so that we don't alter the outer value. */
21124 record_unwind_protect (unwind_format_mode_line,
21125 format_mode_line_unwind_data
21126 (XFRAME (WINDOW_FRAME (XWINDOW (window))),
21127 old_buffer, selected_window, 1));
21128 mode_line_proptrans_alist = Qnil;
21130 Fselect_window (window, Qt);
21131 set_buffer_internal_1 (XBUFFER (buffer));
21133 init_iterator (&it, w, -1, -1, NULL, face_id);
21135 if (no_props)
21137 mode_line_target = MODE_LINE_NOPROP;
21138 mode_line_string_face_prop = Qnil;
21139 mode_line_string_list = Qnil;
21140 string_start = MODE_LINE_NOPROP_LEN (0);
21142 else
21144 mode_line_target = MODE_LINE_STRING;
21145 mode_line_string_list = Qnil;
21146 mode_line_string_face = face;
21147 mode_line_string_face_prop
21148 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21151 push_kboard (FRAME_KBOARD (it.f));
21152 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21153 pop_kboard ();
21155 if (no_props)
21157 len = MODE_LINE_NOPROP_LEN (string_start);
21158 str = make_string (mode_line_noprop_buf + string_start, len);
21160 else
21162 mode_line_string_list = Fnreverse (mode_line_string_list);
21163 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21164 empty_unibyte_string);
21167 unbind_to (count, Qnil);
21168 return str;
21171 /* Write a null-terminated, right justified decimal representation of
21172 the positive integer D to BUF using a minimal field width WIDTH. */
21174 static void
21175 pint2str (register char *buf, register int width, register ptrdiff_t d)
21177 register char *p = buf;
21179 if (d <= 0)
21180 *p++ = '0';
21181 else
21183 while (d > 0)
21185 *p++ = d % 10 + '0';
21186 d /= 10;
21190 for (width -= (int) (p - buf); width > 0; --width)
21191 *p++ = ' ';
21192 *p-- = '\0';
21193 while (p > buf)
21195 d = *buf;
21196 *buf++ = *p;
21197 *p-- = d;
21201 /* Write a null-terminated, right justified decimal and "human
21202 readable" representation of the nonnegative integer D to BUF using
21203 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21205 static const char power_letter[] =
21207 0, /* no letter */
21208 'k', /* kilo */
21209 'M', /* mega */
21210 'G', /* giga */
21211 'T', /* tera */
21212 'P', /* peta */
21213 'E', /* exa */
21214 'Z', /* zetta */
21215 'Y' /* yotta */
21218 static void
21219 pint2hrstr (char *buf, int width, ptrdiff_t d)
21221 /* We aim to represent the nonnegative integer D as
21222 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21223 ptrdiff_t quotient = d;
21224 int remainder = 0;
21225 /* -1 means: do not use TENTHS. */
21226 int tenths = -1;
21227 int exponent = 0;
21229 /* Length of QUOTIENT.TENTHS as a string. */
21230 int length;
21232 char * psuffix;
21233 char * p;
21235 if (1000 <= quotient)
21237 /* Scale to the appropriate EXPONENT. */
21240 remainder = quotient % 1000;
21241 quotient /= 1000;
21242 exponent++;
21244 while (1000 <= quotient);
21246 /* Round to nearest and decide whether to use TENTHS or not. */
21247 if (quotient <= 9)
21249 tenths = remainder / 100;
21250 if (50 <= remainder % 100)
21252 if (tenths < 9)
21253 tenths++;
21254 else
21256 quotient++;
21257 if (quotient == 10)
21258 tenths = -1;
21259 else
21260 tenths = 0;
21264 else
21265 if (500 <= remainder)
21267 if (quotient < 999)
21268 quotient++;
21269 else
21271 quotient = 1;
21272 exponent++;
21273 tenths = 0;
21278 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21279 if (tenths == -1 && quotient <= 99)
21280 if (quotient <= 9)
21281 length = 1;
21282 else
21283 length = 2;
21284 else
21285 length = 3;
21286 p = psuffix = buf + max (width, length);
21288 /* Print EXPONENT. */
21289 *psuffix++ = power_letter[exponent];
21290 *psuffix = '\0';
21292 /* Print TENTHS. */
21293 if (tenths >= 0)
21295 *--p = '0' + tenths;
21296 *--p = '.';
21299 /* Print QUOTIENT. */
21302 int digit = quotient % 10;
21303 *--p = '0' + digit;
21305 while ((quotient /= 10) != 0);
21307 /* Print leading spaces. */
21308 while (buf < p)
21309 *--p = ' ';
21312 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21313 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21314 type of CODING_SYSTEM. Return updated pointer into BUF. */
21316 static unsigned char invalid_eol_type[] = "(*invalid*)";
21318 static char *
21319 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21321 Lisp_Object val;
21322 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21323 const unsigned char *eol_str;
21324 int eol_str_len;
21325 /* The EOL conversion we are using. */
21326 Lisp_Object eoltype;
21328 val = CODING_SYSTEM_SPEC (coding_system);
21329 eoltype = Qnil;
21331 if (!VECTORP (val)) /* Not yet decided. */
21333 *buf++ = multibyte ? '-' : ' ';
21334 if (eol_flag)
21335 eoltype = eol_mnemonic_undecided;
21336 /* Don't mention EOL conversion if it isn't decided. */
21338 else
21340 Lisp_Object attrs;
21341 Lisp_Object eolvalue;
21343 attrs = AREF (val, 0);
21344 eolvalue = AREF (val, 2);
21346 *buf++ = multibyte
21347 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21348 : ' ';
21350 if (eol_flag)
21352 /* The EOL conversion that is normal on this system. */
21354 if (NILP (eolvalue)) /* Not yet decided. */
21355 eoltype = eol_mnemonic_undecided;
21356 else if (VECTORP (eolvalue)) /* Not yet decided. */
21357 eoltype = eol_mnemonic_undecided;
21358 else /* eolvalue is Qunix, Qdos, or Qmac. */
21359 eoltype = (EQ (eolvalue, Qunix)
21360 ? eol_mnemonic_unix
21361 : (EQ (eolvalue, Qdos) == 1
21362 ? eol_mnemonic_dos : eol_mnemonic_mac));
21366 if (eol_flag)
21368 /* Mention the EOL conversion if it is not the usual one. */
21369 if (STRINGP (eoltype))
21371 eol_str = SDATA (eoltype);
21372 eol_str_len = SBYTES (eoltype);
21374 else if (CHARACTERP (eoltype))
21376 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21377 int c = XFASTINT (eoltype);
21378 eol_str_len = CHAR_STRING (c, tmp);
21379 eol_str = tmp;
21381 else
21383 eol_str = invalid_eol_type;
21384 eol_str_len = sizeof (invalid_eol_type) - 1;
21386 memcpy (buf, eol_str, eol_str_len);
21387 buf += eol_str_len;
21390 return buf;
21393 /* Return a string for the output of a mode line %-spec for window W,
21394 generated by character C. FIELD_WIDTH > 0 means pad the string
21395 returned with spaces to that value. Return a Lisp string in
21396 *STRING if the resulting string is taken from that Lisp string.
21398 Note we operate on the current buffer for most purposes,
21399 the exception being w->base_line_pos. */
21401 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21403 static const char *
21404 decode_mode_spec (struct window *w, register int c, int field_width,
21405 Lisp_Object *string)
21407 Lisp_Object obj;
21408 struct frame *f = XFRAME (WINDOW_FRAME (w));
21409 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21410 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21411 produce strings from numerical values, so limit preposterously
21412 large values of FIELD_WIDTH to avoid overrunning the buffer's
21413 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21414 bytes plus the terminating null. */
21415 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21416 struct buffer *b = current_buffer;
21418 obj = Qnil;
21419 *string = Qnil;
21421 switch (c)
21423 case '*':
21424 if (!NILP (BVAR (b, read_only)))
21425 return "%";
21426 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21427 return "*";
21428 return "-";
21430 case '+':
21431 /* This differs from %* only for a modified read-only buffer. */
21432 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21433 return "*";
21434 if (!NILP (BVAR (b, read_only)))
21435 return "%";
21436 return "-";
21438 case '&':
21439 /* This differs from %* in ignoring read-only-ness. */
21440 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21441 return "*";
21442 return "-";
21444 case '%':
21445 return "%";
21447 case '[':
21449 int i;
21450 char *p;
21452 if (command_loop_level > 5)
21453 return "[[[... ";
21454 p = decode_mode_spec_buf;
21455 for (i = 0; i < command_loop_level; i++)
21456 *p++ = '[';
21457 *p = 0;
21458 return decode_mode_spec_buf;
21461 case ']':
21463 int i;
21464 char *p;
21466 if (command_loop_level > 5)
21467 return " ...]]]";
21468 p = decode_mode_spec_buf;
21469 for (i = 0; i < command_loop_level; i++)
21470 *p++ = ']';
21471 *p = 0;
21472 return decode_mode_spec_buf;
21475 case '-':
21477 register int i;
21479 /* Let lots_of_dashes be a string of infinite length. */
21480 if (mode_line_target == MODE_LINE_NOPROP ||
21481 mode_line_target == MODE_LINE_STRING)
21482 return "--";
21483 if (field_width <= 0
21484 || field_width > sizeof (lots_of_dashes))
21486 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21487 decode_mode_spec_buf[i] = '-';
21488 decode_mode_spec_buf[i] = '\0';
21489 return decode_mode_spec_buf;
21491 else
21492 return lots_of_dashes;
21495 case 'b':
21496 obj = BVAR (b, name);
21497 break;
21499 case 'c':
21500 /* %c and %l are ignored in `frame-title-format'.
21501 (In redisplay_internal, the frame title is drawn _before_ the
21502 windows are updated, so the stuff which depends on actual
21503 window contents (such as %l) may fail to render properly, or
21504 even crash emacs.) */
21505 if (mode_line_target == MODE_LINE_TITLE)
21506 return "";
21507 else
21509 ptrdiff_t col = current_column ();
21510 wset_column_number_displayed (w, make_number (col));
21511 pint2str (decode_mode_spec_buf, width, col);
21512 return decode_mode_spec_buf;
21515 case 'e':
21516 #ifndef SYSTEM_MALLOC
21518 if (NILP (Vmemory_full))
21519 return "";
21520 else
21521 return "!MEM FULL! ";
21523 #else
21524 return "";
21525 #endif
21527 case 'F':
21528 /* %F displays the frame name. */
21529 if (!NILP (f->title))
21530 return SSDATA (f->title);
21531 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21532 return SSDATA (f->name);
21533 return "Emacs";
21535 case 'f':
21536 obj = BVAR (b, filename);
21537 break;
21539 case 'i':
21541 ptrdiff_t size = ZV - BEGV;
21542 pint2str (decode_mode_spec_buf, width, size);
21543 return decode_mode_spec_buf;
21546 case 'I':
21548 ptrdiff_t size = ZV - BEGV;
21549 pint2hrstr (decode_mode_spec_buf, width, size);
21550 return decode_mode_spec_buf;
21553 case 'l':
21555 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21556 ptrdiff_t topline, nlines, height;
21557 ptrdiff_t junk;
21559 /* %c and %l are ignored in `frame-title-format'. */
21560 if (mode_line_target == MODE_LINE_TITLE)
21561 return "";
21563 startpos = XMARKER (w->start)->charpos;
21564 startpos_byte = marker_byte_position (w->start);
21565 height = WINDOW_TOTAL_LINES (w);
21567 /* If we decided that this buffer isn't suitable for line numbers,
21568 don't forget that too fast. */
21569 if (EQ (w->base_line_pos, w->buffer))
21570 goto no_value;
21571 /* But do forget it, if the window shows a different buffer now. */
21572 else if (BUFFERP (w->base_line_pos))
21573 wset_base_line_pos (w, Qnil);
21575 /* If the buffer is very big, don't waste time. */
21576 if (INTEGERP (Vline_number_display_limit)
21577 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21579 wset_base_line_pos (w, Qnil);
21580 wset_base_line_number (w, Qnil);
21581 goto no_value;
21584 if (INTEGERP (w->base_line_number)
21585 && INTEGERP (w->base_line_pos)
21586 && XFASTINT (w->base_line_pos) <= startpos)
21588 line = XFASTINT (w->base_line_number);
21589 linepos = XFASTINT (w->base_line_pos);
21590 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21592 else
21594 line = 1;
21595 linepos = BUF_BEGV (b);
21596 linepos_byte = BUF_BEGV_BYTE (b);
21599 /* Count lines from base line to window start position. */
21600 nlines = display_count_lines (linepos_byte,
21601 startpos_byte,
21602 startpos, &junk);
21604 topline = nlines + line;
21606 /* Determine a new base line, if the old one is too close
21607 or too far away, or if we did not have one.
21608 "Too close" means it's plausible a scroll-down would
21609 go back past it. */
21610 if (startpos == BUF_BEGV (b))
21612 wset_base_line_number (w, make_number (topline));
21613 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21615 else if (nlines < height + 25 || nlines > height * 3 + 50
21616 || linepos == BUF_BEGV (b))
21618 ptrdiff_t limit = BUF_BEGV (b);
21619 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21620 ptrdiff_t position;
21621 ptrdiff_t distance =
21622 (height * 2 + 30) * line_number_display_limit_width;
21624 if (startpos - distance > limit)
21626 limit = startpos - distance;
21627 limit_byte = CHAR_TO_BYTE (limit);
21630 nlines = display_count_lines (startpos_byte,
21631 limit_byte,
21632 - (height * 2 + 30),
21633 &position);
21634 /* If we couldn't find the lines we wanted within
21635 line_number_display_limit_width chars per line,
21636 give up on line numbers for this window. */
21637 if (position == limit_byte && limit == startpos - distance)
21639 wset_base_line_pos (w, w->buffer);
21640 wset_base_line_number (w, Qnil);
21641 goto no_value;
21644 wset_base_line_number (w, make_number (topline - nlines));
21645 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21648 /* Now count lines from the start pos to point. */
21649 nlines = display_count_lines (startpos_byte,
21650 PT_BYTE, PT, &junk);
21652 /* Record that we did display the line number. */
21653 line_number_displayed = 1;
21655 /* Make the string to show. */
21656 pint2str (decode_mode_spec_buf, width, topline + nlines);
21657 return decode_mode_spec_buf;
21658 no_value:
21660 char* p = decode_mode_spec_buf;
21661 int pad = width - 2;
21662 while (pad-- > 0)
21663 *p++ = ' ';
21664 *p++ = '?';
21665 *p++ = '?';
21666 *p = '\0';
21667 return decode_mode_spec_buf;
21670 break;
21672 case 'm':
21673 obj = BVAR (b, mode_name);
21674 break;
21676 case 'n':
21677 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21678 return " Narrow";
21679 break;
21681 case 'p':
21683 ptrdiff_t pos = marker_position (w->start);
21684 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21686 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21688 if (pos <= BUF_BEGV (b))
21689 return "All";
21690 else
21691 return "Bottom";
21693 else if (pos <= BUF_BEGV (b))
21694 return "Top";
21695 else
21697 if (total > 1000000)
21698 /* Do it differently for a large value, to avoid overflow. */
21699 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21700 else
21701 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21702 /* We can't normally display a 3-digit number,
21703 so get us a 2-digit number that is close. */
21704 if (total == 100)
21705 total = 99;
21706 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21707 return decode_mode_spec_buf;
21711 /* Display percentage of size above the bottom of the screen. */
21712 case 'P':
21714 ptrdiff_t toppos = marker_position (w->start);
21715 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21716 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21718 if (botpos >= BUF_ZV (b))
21720 if (toppos <= BUF_BEGV (b))
21721 return "All";
21722 else
21723 return "Bottom";
21725 else
21727 if (total > 1000000)
21728 /* Do it differently for a large value, to avoid overflow. */
21729 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21730 else
21731 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21732 /* We can't normally display a 3-digit number,
21733 so get us a 2-digit number that is close. */
21734 if (total == 100)
21735 total = 99;
21736 if (toppos <= BUF_BEGV (b))
21737 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21738 else
21739 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21740 return decode_mode_spec_buf;
21744 case 's':
21745 /* status of process */
21746 obj = Fget_buffer_process (Fcurrent_buffer ());
21747 if (NILP (obj))
21748 return "no process";
21749 #ifndef MSDOS
21750 obj = Fsymbol_name (Fprocess_status (obj));
21751 #endif
21752 break;
21754 case '@':
21756 ptrdiff_t count = inhibit_garbage_collection ();
21757 Lisp_Object val = call1 (intern ("file-remote-p"),
21758 BVAR (current_buffer, directory));
21759 unbind_to (count, Qnil);
21761 if (NILP (val))
21762 return "-";
21763 else
21764 return "@";
21767 case 't': /* indicate TEXT or BINARY */
21768 return "T";
21770 case 'z':
21771 /* coding-system (not including end-of-line format) */
21772 case 'Z':
21773 /* coding-system (including end-of-line type) */
21775 int eol_flag = (c == 'Z');
21776 char *p = decode_mode_spec_buf;
21778 if (! FRAME_WINDOW_P (f))
21780 /* No need to mention EOL here--the terminal never needs
21781 to do EOL conversion. */
21782 p = decode_mode_spec_coding (CODING_ID_NAME
21783 (FRAME_KEYBOARD_CODING (f)->id),
21784 p, 0);
21785 p = decode_mode_spec_coding (CODING_ID_NAME
21786 (FRAME_TERMINAL_CODING (f)->id),
21787 p, 0);
21789 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21790 p, eol_flag);
21792 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21793 #ifdef subprocesses
21794 obj = Fget_buffer_process (Fcurrent_buffer ());
21795 if (PROCESSP (obj))
21797 p = decode_mode_spec_coding
21798 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21799 p = decode_mode_spec_coding
21800 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21802 #endif /* subprocesses */
21803 #endif /* 0 */
21804 *p = 0;
21805 return decode_mode_spec_buf;
21809 if (STRINGP (obj))
21811 *string = obj;
21812 return SSDATA (obj);
21814 else
21815 return "";
21819 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21820 means count lines back from START_BYTE. But don't go beyond
21821 LIMIT_BYTE. Return the number of lines thus found (always
21822 nonnegative).
21824 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21825 either the position COUNT lines after/before START_BYTE, if we
21826 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21827 COUNT lines. */
21829 static ptrdiff_t
21830 display_count_lines (ptrdiff_t start_byte,
21831 ptrdiff_t limit_byte, ptrdiff_t count,
21832 ptrdiff_t *byte_pos_ptr)
21834 register unsigned char *cursor;
21835 unsigned char *base;
21837 register ptrdiff_t ceiling;
21838 register unsigned char *ceiling_addr;
21839 ptrdiff_t orig_count = count;
21841 /* If we are not in selective display mode,
21842 check only for newlines. */
21843 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21844 && !INTEGERP (BVAR (current_buffer, selective_display)));
21846 if (count > 0)
21848 while (start_byte < limit_byte)
21850 ceiling = BUFFER_CEILING_OF (start_byte);
21851 ceiling = min (limit_byte - 1, ceiling);
21852 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21853 base = (cursor = BYTE_POS_ADDR (start_byte));
21854 while (1)
21856 if (selective_display)
21857 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21859 else
21860 while (*cursor != '\n' && ++cursor != ceiling_addr)
21863 if (cursor != ceiling_addr)
21865 if (--count == 0)
21867 start_byte += cursor - base + 1;
21868 *byte_pos_ptr = start_byte;
21869 return orig_count;
21871 else
21872 if (++cursor == ceiling_addr)
21873 break;
21875 else
21876 break;
21878 start_byte += cursor - base;
21881 else
21883 while (start_byte > limit_byte)
21885 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21886 ceiling = max (limit_byte, ceiling);
21887 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21888 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21889 while (1)
21891 if (selective_display)
21892 while (--cursor != ceiling_addr
21893 && *cursor != '\n' && *cursor != 015)
21895 else
21896 while (--cursor != ceiling_addr && *cursor != '\n')
21899 if (cursor != ceiling_addr)
21901 if (++count == 0)
21903 start_byte += cursor - base + 1;
21904 *byte_pos_ptr = start_byte;
21905 /* When scanning backwards, we should
21906 not count the newline posterior to which we stop. */
21907 return - orig_count - 1;
21910 else
21911 break;
21913 /* Here we add 1 to compensate for the last decrement
21914 of CURSOR, which took it past the valid range. */
21915 start_byte += cursor - base + 1;
21919 *byte_pos_ptr = limit_byte;
21921 if (count < 0)
21922 return - orig_count + count;
21923 return orig_count - count;
21929 /***********************************************************************
21930 Displaying strings
21931 ***********************************************************************/
21933 /* Display a NUL-terminated string, starting with index START.
21935 If STRING is non-null, display that C string. Otherwise, the Lisp
21936 string LISP_STRING is displayed. There's a case that STRING is
21937 non-null and LISP_STRING is not nil. It means STRING is a string
21938 data of LISP_STRING. In that case, we display LISP_STRING while
21939 ignoring its text properties.
21941 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21942 FACE_STRING. Display STRING or LISP_STRING with the face at
21943 FACE_STRING_POS in FACE_STRING:
21945 Display the string in the environment given by IT, but use the
21946 standard display table, temporarily.
21948 FIELD_WIDTH is the minimum number of output glyphs to produce.
21949 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21950 with spaces. If STRING has more characters, more than FIELD_WIDTH
21951 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21953 PRECISION is the maximum number of characters to output from
21954 STRING. PRECISION < 0 means don't truncate the string.
21956 This is roughly equivalent to printf format specifiers:
21958 FIELD_WIDTH PRECISION PRINTF
21959 ----------------------------------------
21960 -1 -1 %s
21961 -1 10 %.10s
21962 10 -1 %10s
21963 20 10 %20.10s
21965 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21966 display them, and < 0 means obey the current buffer's value of
21967 enable_multibyte_characters.
21969 Value is the number of columns displayed. */
21971 static int
21972 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21973 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21974 int field_width, int precision, int max_x, int multibyte)
21976 int hpos_at_start = it->hpos;
21977 int saved_face_id = it->face_id;
21978 struct glyph_row *row = it->glyph_row;
21979 ptrdiff_t it_charpos;
21981 /* Initialize the iterator IT for iteration over STRING beginning
21982 with index START. */
21983 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21984 precision, field_width, multibyte);
21985 if (string && STRINGP (lisp_string))
21986 /* LISP_STRING is the one returned by decode_mode_spec. We should
21987 ignore its text properties. */
21988 it->stop_charpos = it->end_charpos;
21990 /* If displaying STRING, set up the face of the iterator from
21991 FACE_STRING, if that's given. */
21992 if (STRINGP (face_string))
21994 ptrdiff_t endptr;
21995 struct face *face;
21997 it->face_id
21998 = face_at_string_position (it->w, face_string, face_string_pos,
21999 0, it->region_beg_charpos,
22000 it->region_end_charpos,
22001 &endptr, it->base_face_id, 0);
22002 face = FACE_FROM_ID (it->f, it->face_id);
22003 it->face_box_p = face->box != FACE_NO_BOX;
22006 /* Set max_x to the maximum allowed X position. Don't let it go
22007 beyond the right edge of the window. */
22008 if (max_x <= 0)
22009 max_x = it->last_visible_x;
22010 else
22011 max_x = min (max_x, it->last_visible_x);
22013 /* Skip over display elements that are not visible. because IT->w is
22014 hscrolled. */
22015 if (it->current_x < it->first_visible_x)
22016 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22017 MOVE_TO_POS | MOVE_TO_X);
22019 row->ascent = it->max_ascent;
22020 row->height = it->max_ascent + it->max_descent;
22021 row->phys_ascent = it->max_phys_ascent;
22022 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22023 row->extra_line_spacing = it->max_extra_line_spacing;
22025 if (STRINGP (it->string))
22026 it_charpos = IT_STRING_CHARPOS (*it);
22027 else
22028 it_charpos = IT_CHARPOS (*it);
22030 /* This condition is for the case that we are called with current_x
22031 past last_visible_x. */
22032 while (it->current_x < max_x)
22034 int x_before, x, n_glyphs_before, i, nglyphs;
22036 /* Get the next display element. */
22037 if (!get_next_display_element (it))
22038 break;
22040 /* Produce glyphs. */
22041 x_before = it->current_x;
22042 n_glyphs_before = row->used[TEXT_AREA];
22043 PRODUCE_GLYPHS (it);
22045 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22046 i = 0;
22047 x = x_before;
22048 while (i < nglyphs)
22050 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22052 if (it->line_wrap != TRUNCATE
22053 && x + glyph->pixel_width > max_x)
22055 /* End of continued line or max_x reached. */
22056 if (CHAR_GLYPH_PADDING_P (*glyph))
22058 /* A wide character is unbreakable. */
22059 if (row->reversed_p)
22060 unproduce_glyphs (it, row->used[TEXT_AREA]
22061 - n_glyphs_before);
22062 row->used[TEXT_AREA] = n_glyphs_before;
22063 it->current_x = x_before;
22065 else
22067 if (row->reversed_p)
22068 unproduce_glyphs (it, row->used[TEXT_AREA]
22069 - (n_glyphs_before + i));
22070 row->used[TEXT_AREA] = n_glyphs_before + i;
22071 it->current_x = x;
22073 break;
22075 else if (x + glyph->pixel_width >= it->first_visible_x)
22077 /* Glyph is at least partially visible. */
22078 ++it->hpos;
22079 if (x < it->first_visible_x)
22080 row->x = x - it->first_visible_x;
22082 else
22084 /* Glyph is off the left margin of the display area.
22085 Should not happen. */
22086 emacs_abort ();
22089 row->ascent = max (row->ascent, it->max_ascent);
22090 row->height = max (row->height, it->max_ascent + it->max_descent);
22091 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22092 row->phys_height = max (row->phys_height,
22093 it->max_phys_ascent + it->max_phys_descent);
22094 row->extra_line_spacing = max (row->extra_line_spacing,
22095 it->max_extra_line_spacing);
22096 x += glyph->pixel_width;
22097 ++i;
22100 /* Stop if max_x reached. */
22101 if (i < nglyphs)
22102 break;
22104 /* Stop at line ends. */
22105 if (ITERATOR_AT_END_OF_LINE_P (it))
22107 it->continuation_lines_width = 0;
22108 break;
22111 set_iterator_to_next (it, 1);
22112 if (STRINGP (it->string))
22113 it_charpos = IT_STRING_CHARPOS (*it);
22114 else
22115 it_charpos = IT_CHARPOS (*it);
22117 /* Stop if truncating at the right edge. */
22118 if (it->line_wrap == TRUNCATE
22119 && it->current_x >= it->last_visible_x)
22121 /* Add truncation mark, but don't do it if the line is
22122 truncated at a padding space. */
22123 if (it_charpos < it->string_nchars)
22125 if (!FRAME_WINDOW_P (it->f))
22127 int ii, n;
22129 if (it->current_x > it->last_visible_x)
22131 if (!row->reversed_p)
22133 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22134 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22135 break;
22137 else
22139 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22140 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22141 break;
22142 unproduce_glyphs (it, ii + 1);
22143 ii = row->used[TEXT_AREA] - (ii + 1);
22145 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22147 row->used[TEXT_AREA] = ii;
22148 produce_special_glyphs (it, IT_TRUNCATION);
22151 produce_special_glyphs (it, IT_TRUNCATION);
22153 row->truncated_on_right_p = 1;
22155 break;
22159 /* Maybe insert a truncation at the left. */
22160 if (it->first_visible_x
22161 && it_charpos > 0)
22163 if (!FRAME_WINDOW_P (it->f)
22164 || (row->reversed_p
22165 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22166 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22167 insert_left_trunc_glyphs (it);
22168 row->truncated_on_left_p = 1;
22171 it->face_id = saved_face_id;
22173 /* Value is number of columns displayed. */
22174 return it->hpos - hpos_at_start;
22179 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22180 appears as an element of LIST or as the car of an element of LIST.
22181 If PROPVAL is a list, compare each element against LIST in that
22182 way, and return 1/2 if any element of PROPVAL is found in LIST.
22183 Otherwise return 0. This function cannot quit.
22184 The return value is 2 if the text is invisible but with an ellipsis
22185 and 1 if it's invisible and without an ellipsis. */
22188 invisible_p (register Lisp_Object propval, Lisp_Object list)
22190 register Lisp_Object tail, proptail;
22192 for (tail = list; CONSP (tail); tail = XCDR (tail))
22194 register Lisp_Object tem;
22195 tem = XCAR (tail);
22196 if (EQ (propval, tem))
22197 return 1;
22198 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22199 return NILP (XCDR (tem)) ? 1 : 2;
22202 if (CONSP (propval))
22204 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22206 Lisp_Object propelt;
22207 propelt = XCAR (proptail);
22208 for (tail = list; CONSP (tail); tail = XCDR (tail))
22210 register Lisp_Object tem;
22211 tem = XCAR (tail);
22212 if (EQ (propelt, tem))
22213 return 1;
22214 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22215 return NILP (XCDR (tem)) ? 1 : 2;
22220 return 0;
22223 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22224 doc: /* Non-nil if the property makes the text invisible.
22225 POS-OR-PROP can be a marker or number, in which case it is taken to be
22226 a position in the current buffer and the value of the `invisible' property
22227 is checked; or it can be some other value, which is then presumed to be the
22228 value of the `invisible' property of the text of interest.
22229 The non-nil value returned can be t for truly invisible text or something
22230 else if the text is replaced by an ellipsis. */)
22231 (Lisp_Object pos_or_prop)
22233 Lisp_Object prop
22234 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22235 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22236 : pos_or_prop);
22237 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22238 return (invis == 0 ? Qnil
22239 : invis == 1 ? Qt
22240 : make_number (invis));
22243 /* Calculate a width or height in pixels from a specification using
22244 the following elements:
22246 SPEC ::=
22247 NUM - a (fractional) multiple of the default font width/height
22248 (NUM) - specifies exactly NUM pixels
22249 UNIT - a fixed number of pixels, see below.
22250 ELEMENT - size of a display element in pixels, see below.
22251 (NUM . SPEC) - equals NUM * SPEC
22252 (+ SPEC SPEC ...) - add pixel values
22253 (- SPEC SPEC ...) - subtract pixel values
22254 (- SPEC) - negate pixel value
22256 NUM ::=
22257 INT or FLOAT - a number constant
22258 SYMBOL - use symbol's (buffer local) variable binding.
22260 UNIT ::=
22261 in - pixels per inch *)
22262 mm - pixels per 1/1000 meter *)
22263 cm - pixels per 1/100 meter *)
22264 width - width of current font in pixels.
22265 height - height of current font in pixels.
22267 *) using the ratio(s) defined in display-pixels-per-inch.
22269 ELEMENT ::=
22271 left-fringe - left fringe width in pixels
22272 right-fringe - right fringe width in pixels
22274 left-margin - left margin width in pixels
22275 right-margin - right margin width in pixels
22277 scroll-bar - scroll-bar area width in pixels
22279 Examples:
22281 Pixels corresponding to 5 inches:
22282 (5 . in)
22284 Total width of non-text areas on left side of window (if scroll-bar is on left):
22285 '(space :width (+ left-fringe left-margin scroll-bar))
22287 Align to first text column (in header line):
22288 '(space :align-to 0)
22290 Align to middle of text area minus half the width of variable `my-image'
22291 containing a loaded image:
22292 '(space :align-to (0.5 . (- text my-image)))
22294 Width of left margin minus width of 1 character in the default font:
22295 '(space :width (- left-margin 1))
22297 Width of left margin minus width of 2 characters in the current font:
22298 '(space :width (- left-margin (2 . width)))
22300 Center 1 character over left-margin (in header line):
22301 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22303 Different ways to express width of left fringe plus left margin minus one pixel:
22304 '(space :width (- (+ left-fringe left-margin) (1)))
22305 '(space :width (+ left-fringe left-margin (- (1))))
22306 '(space :width (+ left-fringe left-margin (-1)))
22310 #define NUMVAL(X) \
22311 ((INTEGERP (X) || FLOATP (X)) \
22312 ? XFLOATINT (X) \
22313 : - 1)
22315 static int
22316 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22317 struct font *font, int width_p, int *align_to)
22319 double pixels;
22321 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22322 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22324 if (NILP (prop))
22325 return OK_PIXELS (0);
22327 eassert (FRAME_LIVE_P (it->f));
22329 if (SYMBOLP (prop))
22331 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22333 char *unit = SSDATA (SYMBOL_NAME (prop));
22335 if (unit[0] == 'i' && unit[1] == 'n')
22336 pixels = 1.0;
22337 else if (unit[0] == 'm' && unit[1] == 'm')
22338 pixels = 25.4;
22339 else if (unit[0] == 'c' && unit[1] == 'm')
22340 pixels = 2.54;
22341 else
22342 pixels = 0;
22343 if (pixels > 0)
22345 double ppi;
22346 #ifdef HAVE_WINDOW_SYSTEM
22347 if (FRAME_WINDOW_P (it->f)
22348 && (ppi = (width_p
22349 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22350 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22351 ppi > 0))
22352 return OK_PIXELS (ppi / pixels);
22353 #endif
22355 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22356 || (CONSP (Vdisplay_pixels_per_inch)
22357 && (ppi = (width_p
22358 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22359 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22360 ppi > 0)))
22361 return OK_PIXELS (ppi / pixels);
22363 return 0;
22367 #ifdef HAVE_WINDOW_SYSTEM
22368 if (EQ (prop, Qheight))
22369 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22370 if (EQ (prop, Qwidth))
22371 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22372 #else
22373 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22374 return OK_PIXELS (1);
22375 #endif
22377 if (EQ (prop, Qtext))
22378 return OK_PIXELS (width_p
22379 ? window_box_width (it->w, TEXT_AREA)
22380 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22382 if (align_to && *align_to < 0)
22384 *res = 0;
22385 if (EQ (prop, Qleft))
22386 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22387 if (EQ (prop, Qright))
22388 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22389 if (EQ (prop, Qcenter))
22390 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22391 + window_box_width (it->w, TEXT_AREA) / 2);
22392 if (EQ (prop, Qleft_fringe))
22393 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22394 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22395 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22396 if (EQ (prop, Qright_fringe))
22397 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22398 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22399 : window_box_right_offset (it->w, TEXT_AREA));
22400 if (EQ (prop, Qleft_margin))
22401 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22402 if (EQ (prop, Qright_margin))
22403 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22404 if (EQ (prop, Qscroll_bar))
22405 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22407 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22408 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22409 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22410 : 0)));
22412 else
22414 if (EQ (prop, Qleft_fringe))
22415 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22416 if (EQ (prop, Qright_fringe))
22417 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22418 if (EQ (prop, Qleft_margin))
22419 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22420 if (EQ (prop, Qright_margin))
22421 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22422 if (EQ (prop, Qscroll_bar))
22423 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22426 prop = buffer_local_value_1 (prop, it->w->buffer);
22427 if (EQ (prop, Qunbound))
22428 prop = Qnil;
22431 if (INTEGERP (prop) || FLOATP (prop))
22433 int base_unit = (width_p
22434 ? FRAME_COLUMN_WIDTH (it->f)
22435 : FRAME_LINE_HEIGHT (it->f));
22436 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22439 if (CONSP (prop))
22441 Lisp_Object car = XCAR (prop);
22442 Lisp_Object cdr = XCDR (prop);
22444 if (SYMBOLP (car))
22446 #ifdef HAVE_WINDOW_SYSTEM
22447 if (FRAME_WINDOW_P (it->f)
22448 && valid_image_p (prop))
22450 ptrdiff_t id = lookup_image (it->f, prop);
22451 struct image *img = IMAGE_FROM_ID (it->f, id);
22453 return OK_PIXELS (width_p ? img->width : img->height);
22455 #endif
22456 if (EQ (car, Qplus) || EQ (car, Qminus))
22458 int first = 1;
22459 double px;
22461 pixels = 0;
22462 while (CONSP (cdr))
22464 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22465 font, width_p, align_to))
22466 return 0;
22467 if (first)
22468 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22469 else
22470 pixels += px;
22471 cdr = XCDR (cdr);
22473 if (EQ (car, Qminus))
22474 pixels = -pixels;
22475 return OK_PIXELS (pixels);
22478 car = buffer_local_value_1 (car, it->w->buffer);
22479 if (EQ (car, Qunbound))
22480 car = Qnil;
22483 if (INTEGERP (car) || FLOATP (car))
22485 double fact;
22486 pixels = XFLOATINT (car);
22487 if (NILP (cdr))
22488 return OK_PIXELS (pixels);
22489 if (calc_pixel_width_or_height (&fact, it, cdr,
22490 font, width_p, align_to))
22491 return OK_PIXELS (pixels * fact);
22492 return 0;
22495 return 0;
22498 return 0;
22502 /***********************************************************************
22503 Glyph Display
22504 ***********************************************************************/
22506 #ifdef HAVE_WINDOW_SYSTEM
22508 #ifdef GLYPH_DEBUG
22510 void
22511 dump_glyph_string (struct glyph_string *s)
22513 fprintf (stderr, "glyph string\n");
22514 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22515 s->x, s->y, s->width, s->height);
22516 fprintf (stderr, " ybase = %d\n", s->ybase);
22517 fprintf (stderr, " hl = %d\n", s->hl);
22518 fprintf (stderr, " left overhang = %d, right = %d\n",
22519 s->left_overhang, s->right_overhang);
22520 fprintf (stderr, " nchars = %d\n", s->nchars);
22521 fprintf (stderr, " extends to end of line = %d\n",
22522 s->extends_to_end_of_line_p);
22523 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22524 fprintf (stderr, " bg width = %d\n", s->background_width);
22527 #endif /* GLYPH_DEBUG */
22529 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22530 of XChar2b structures for S; it can't be allocated in
22531 init_glyph_string because it must be allocated via `alloca'. W
22532 is the window on which S is drawn. ROW and AREA are the glyph row
22533 and area within the row from which S is constructed. START is the
22534 index of the first glyph structure covered by S. HL is a
22535 face-override for drawing S. */
22537 #ifdef HAVE_NTGUI
22538 #define OPTIONAL_HDC(hdc) HDC hdc,
22539 #define DECLARE_HDC(hdc) HDC hdc;
22540 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22541 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22542 #endif
22544 #ifndef OPTIONAL_HDC
22545 #define OPTIONAL_HDC(hdc)
22546 #define DECLARE_HDC(hdc)
22547 #define ALLOCATE_HDC(hdc, f)
22548 #define RELEASE_HDC(hdc, f)
22549 #endif
22551 static void
22552 init_glyph_string (struct glyph_string *s,
22553 OPTIONAL_HDC (hdc)
22554 XChar2b *char2b, struct window *w, struct glyph_row *row,
22555 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22557 memset (s, 0, sizeof *s);
22558 s->w = w;
22559 s->f = XFRAME (w->frame);
22560 #ifdef HAVE_NTGUI
22561 s->hdc = hdc;
22562 #endif
22563 s->display = FRAME_X_DISPLAY (s->f);
22564 s->window = FRAME_X_WINDOW (s->f);
22565 s->char2b = char2b;
22566 s->hl = hl;
22567 s->row = row;
22568 s->area = area;
22569 s->first_glyph = row->glyphs[area] + start;
22570 s->height = row->height;
22571 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22572 s->ybase = s->y + row->ascent;
22576 /* Append the list of glyph strings with head H and tail T to the list
22577 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22579 static void
22580 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22581 struct glyph_string *h, struct glyph_string *t)
22583 if (h)
22585 if (*head)
22586 (*tail)->next = h;
22587 else
22588 *head = h;
22589 h->prev = *tail;
22590 *tail = t;
22595 /* Prepend the list of glyph strings with head H and tail T to the
22596 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22597 result. */
22599 static void
22600 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22601 struct glyph_string *h, struct glyph_string *t)
22603 if (h)
22605 if (*head)
22606 (*head)->prev = t;
22607 else
22608 *tail = t;
22609 t->next = *head;
22610 *head = h;
22615 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22616 Set *HEAD and *TAIL to the resulting list. */
22618 static void
22619 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22620 struct glyph_string *s)
22622 s->next = s->prev = NULL;
22623 append_glyph_string_lists (head, tail, s, s);
22627 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22628 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22629 make sure that X resources for the face returned are allocated.
22630 Value is a pointer to a realized face that is ready for display if
22631 DISPLAY_P is non-zero. */
22633 static struct face *
22634 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22635 XChar2b *char2b, int display_p)
22637 struct face *face = FACE_FROM_ID (f, face_id);
22639 if (face->font)
22641 unsigned code = face->font->driver->encode_char (face->font, c);
22643 if (code != FONT_INVALID_CODE)
22644 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22645 else
22646 STORE_XCHAR2B (char2b, 0, 0);
22649 /* Make sure X resources of the face are allocated. */
22650 #ifdef HAVE_X_WINDOWS
22651 if (display_p)
22652 #endif
22654 eassert (face != NULL);
22655 PREPARE_FACE_FOR_DISPLAY (f, face);
22658 return face;
22662 /* Get face and two-byte form of character glyph GLYPH on frame F.
22663 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22664 a pointer to a realized face that is ready for display. */
22666 static struct face *
22667 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22668 XChar2b *char2b, int *two_byte_p)
22670 struct face *face;
22672 eassert (glyph->type == CHAR_GLYPH);
22673 face = FACE_FROM_ID (f, glyph->face_id);
22675 if (two_byte_p)
22676 *two_byte_p = 0;
22678 if (face->font)
22680 unsigned code;
22682 if (CHAR_BYTE8_P (glyph->u.ch))
22683 code = CHAR_TO_BYTE8 (glyph->u.ch);
22684 else
22685 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22687 if (code != FONT_INVALID_CODE)
22688 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22689 else
22690 STORE_XCHAR2B (char2b, 0, 0);
22693 /* Make sure X resources of the face are allocated. */
22694 eassert (face != NULL);
22695 PREPARE_FACE_FOR_DISPLAY (f, face);
22696 return face;
22700 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22701 Return 1 if FONT has a glyph for C, otherwise return 0. */
22703 static int
22704 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22706 unsigned code;
22708 if (CHAR_BYTE8_P (c))
22709 code = CHAR_TO_BYTE8 (c);
22710 else
22711 code = font->driver->encode_char (font, c);
22713 if (code == FONT_INVALID_CODE)
22714 return 0;
22715 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22716 return 1;
22720 /* Fill glyph string S with composition components specified by S->cmp.
22722 BASE_FACE is the base face of the composition.
22723 S->cmp_from is the index of the first component for S.
22725 OVERLAPS non-zero means S should draw the foreground only, and use
22726 its physical height for clipping. See also draw_glyphs.
22728 Value is the index of a component not in S. */
22730 static int
22731 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22732 int overlaps)
22734 int i;
22735 /* For all glyphs of this composition, starting at the offset
22736 S->cmp_from, until we reach the end of the definition or encounter a
22737 glyph that requires the different face, add it to S. */
22738 struct face *face;
22740 eassert (s);
22742 s->for_overlaps = overlaps;
22743 s->face = NULL;
22744 s->font = NULL;
22745 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22747 int c = COMPOSITION_GLYPH (s->cmp, i);
22749 /* TAB in a composition means display glyphs with padding space
22750 on the left or right. */
22751 if (c != '\t')
22753 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22754 -1, Qnil);
22756 face = get_char_face_and_encoding (s->f, c, face_id,
22757 s->char2b + i, 1);
22758 if (face)
22760 if (! s->face)
22762 s->face = face;
22763 s->font = s->face->font;
22765 else if (s->face != face)
22766 break;
22769 ++s->nchars;
22771 s->cmp_to = i;
22773 if (s->face == NULL)
22775 s->face = base_face->ascii_face;
22776 s->font = s->face->font;
22779 /* All glyph strings for the same composition has the same width,
22780 i.e. the width set for the first component of the composition. */
22781 s->width = s->first_glyph->pixel_width;
22783 /* If the specified font could not be loaded, use the frame's
22784 default font, but record the fact that we couldn't load it in
22785 the glyph string so that we can draw rectangles for the
22786 characters of the glyph string. */
22787 if (s->font == NULL)
22789 s->font_not_found_p = 1;
22790 s->font = FRAME_FONT (s->f);
22793 /* Adjust base line for subscript/superscript text. */
22794 s->ybase += s->first_glyph->voffset;
22796 /* This glyph string must always be drawn with 16-bit functions. */
22797 s->two_byte_p = 1;
22799 return s->cmp_to;
22802 static int
22803 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22804 int start, int end, int overlaps)
22806 struct glyph *glyph, *last;
22807 Lisp_Object lgstring;
22808 int i;
22810 s->for_overlaps = overlaps;
22811 glyph = s->row->glyphs[s->area] + start;
22812 last = s->row->glyphs[s->area] + end;
22813 s->cmp_id = glyph->u.cmp.id;
22814 s->cmp_from = glyph->slice.cmp.from;
22815 s->cmp_to = glyph->slice.cmp.to + 1;
22816 s->face = FACE_FROM_ID (s->f, face_id);
22817 lgstring = composition_gstring_from_id (s->cmp_id);
22818 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22819 glyph++;
22820 while (glyph < last
22821 && glyph->u.cmp.automatic
22822 && glyph->u.cmp.id == s->cmp_id
22823 && s->cmp_to == glyph->slice.cmp.from)
22824 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22826 for (i = s->cmp_from; i < s->cmp_to; i++)
22828 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22829 unsigned code = LGLYPH_CODE (lglyph);
22831 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22833 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22834 return glyph - s->row->glyphs[s->area];
22838 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22839 See the comment of fill_glyph_string for arguments.
22840 Value is the index of the first glyph not in S. */
22843 static int
22844 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22845 int start, int end, int overlaps)
22847 struct glyph *glyph, *last;
22848 int voffset;
22850 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22851 s->for_overlaps = overlaps;
22852 glyph = s->row->glyphs[s->area] + start;
22853 last = s->row->glyphs[s->area] + end;
22854 voffset = glyph->voffset;
22855 s->face = FACE_FROM_ID (s->f, face_id);
22856 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22857 s->nchars = 1;
22858 s->width = glyph->pixel_width;
22859 glyph++;
22860 while (glyph < last
22861 && glyph->type == GLYPHLESS_GLYPH
22862 && glyph->voffset == voffset
22863 && glyph->face_id == face_id)
22865 s->nchars++;
22866 s->width += glyph->pixel_width;
22867 glyph++;
22869 s->ybase += voffset;
22870 return glyph - s->row->glyphs[s->area];
22874 /* Fill glyph string S from a sequence of character glyphs.
22876 FACE_ID is the face id of the string. START is the index of the
22877 first glyph to consider, END is the index of the last + 1.
22878 OVERLAPS non-zero means S should draw the foreground only, and use
22879 its physical height for clipping. See also draw_glyphs.
22881 Value is the index of the first glyph not in S. */
22883 static int
22884 fill_glyph_string (struct glyph_string *s, int face_id,
22885 int start, int end, int overlaps)
22887 struct glyph *glyph, *last;
22888 int voffset;
22889 int glyph_not_available_p;
22891 eassert (s->f == XFRAME (s->w->frame));
22892 eassert (s->nchars == 0);
22893 eassert (start >= 0 && end > start);
22895 s->for_overlaps = overlaps;
22896 glyph = s->row->glyphs[s->area] + start;
22897 last = s->row->glyphs[s->area] + end;
22898 voffset = glyph->voffset;
22899 s->padding_p = glyph->padding_p;
22900 glyph_not_available_p = glyph->glyph_not_available_p;
22902 while (glyph < last
22903 && glyph->type == CHAR_GLYPH
22904 && glyph->voffset == voffset
22905 /* Same face id implies same font, nowadays. */
22906 && glyph->face_id == face_id
22907 && glyph->glyph_not_available_p == glyph_not_available_p)
22909 int two_byte_p;
22911 s->face = get_glyph_face_and_encoding (s->f, glyph,
22912 s->char2b + s->nchars,
22913 &two_byte_p);
22914 s->two_byte_p = two_byte_p;
22915 ++s->nchars;
22916 eassert (s->nchars <= end - start);
22917 s->width += glyph->pixel_width;
22918 if (glyph++->padding_p != s->padding_p)
22919 break;
22922 s->font = s->face->font;
22924 /* If the specified font could not be loaded, use the frame's font,
22925 but record the fact that we couldn't load it in
22926 S->font_not_found_p so that we can draw rectangles for the
22927 characters of the glyph string. */
22928 if (s->font == NULL || glyph_not_available_p)
22930 s->font_not_found_p = 1;
22931 s->font = FRAME_FONT (s->f);
22934 /* Adjust base line for subscript/superscript text. */
22935 s->ybase += voffset;
22937 eassert (s->face && s->face->gc);
22938 return glyph - s->row->glyphs[s->area];
22942 /* Fill glyph string S from image glyph S->first_glyph. */
22944 static void
22945 fill_image_glyph_string (struct glyph_string *s)
22947 eassert (s->first_glyph->type == IMAGE_GLYPH);
22948 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22949 eassert (s->img);
22950 s->slice = s->first_glyph->slice.img;
22951 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22952 s->font = s->face->font;
22953 s->width = s->first_glyph->pixel_width;
22955 /* Adjust base line for subscript/superscript text. */
22956 s->ybase += s->first_glyph->voffset;
22960 /* Fill glyph string S from a sequence of stretch glyphs.
22962 START is the index of the first glyph to consider,
22963 END is the index of the last + 1.
22965 Value is the index of the first glyph not in S. */
22967 static int
22968 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22970 struct glyph *glyph, *last;
22971 int voffset, face_id;
22973 eassert (s->first_glyph->type == STRETCH_GLYPH);
22975 glyph = s->row->glyphs[s->area] + start;
22976 last = s->row->glyphs[s->area] + end;
22977 face_id = glyph->face_id;
22978 s->face = FACE_FROM_ID (s->f, face_id);
22979 s->font = s->face->font;
22980 s->width = glyph->pixel_width;
22981 s->nchars = 1;
22982 voffset = glyph->voffset;
22984 for (++glyph;
22985 (glyph < last
22986 && glyph->type == STRETCH_GLYPH
22987 && glyph->voffset == voffset
22988 && glyph->face_id == face_id);
22989 ++glyph)
22990 s->width += glyph->pixel_width;
22992 /* Adjust base line for subscript/superscript text. */
22993 s->ybase += voffset;
22995 /* The case that face->gc == 0 is handled when drawing the glyph
22996 string by calling PREPARE_FACE_FOR_DISPLAY. */
22997 eassert (s->face);
22998 return glyph - s->row->glyphs[s->area];
23001 static struct font_metrics *
23002 get_per_char_metric (struct font *font, XChar2b *char2b)
23004 static struct font_metrics metrics;
23005 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23007 if (! font || code == FONT_INVALID_CODE)
23008 return NULL;
23009 font->driver->text_extents (font, &code, 1, &metrics);
23010 return &metrics;
23013 /* EXPORT for RIF:
23014 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23015 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23016 assumed to be zero. */
23018 void
23019 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23021 *left = *right = 0;
23023 if (glyph->type == CHAR_GLYPH)
23025 struct face *face;
23026 XChar2b char2b;
23027 struct font_metrics *pcm;
23029 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23030 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23032 if (pcm->rbearing > pcm->width)
23033 *right = pcm->rbearing - pcm->width;
23034 if (pcm->lbearing < 0)
23035 *left = -pcm->lbearing;
23038 else if (glyph->type == COMPOSITE_GLYPH)
23040 if (! glyph->u.cmp.automatic)
23042 struct composition *cmp = composition_table[glyph->u.cmp.id];
23044 if (cmp->rbearing > cmp->pixel_width)
23045 *right = cmp->rbearing - cmp->pixel_width;
23046 if (cmp->lbearing < 0)
23047 *left = - cmp->lbearing;
23049 else
23051 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23052 struct font_metrics metrics;
23054 composition_gstring_width (gstring, glyph->slice.cmp.from,
23055 glyph->slice.cmp.to + 1, &metrics);
23056 if (metrics.rbearing > metrics.width)
23057 *right = metrics.rbearing - metrics.width;
23058 if (metrics.lbearing < 0)
23059 *left = - metrics.lbearing;
23065 /* Return the index of the first glyph preceding glyph string S that
23066 is overwritten by S because of S's left overhang. Value is -1
23067 if no glyphs are overwritten. */
23069 static int
23070 left_overwritten (struct glyph_string *s)
23072 int k;
23074 if (s->left_overhang)
23076 int x = 0, i;
23077 struct glyph *glyphs = s->row->glyphs[s->area];
23078 int first = s->first_glyph - glyphs;
23080 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23081 x -= glyphs[i].pixel_width;
23083 k = i + 1;
23085 else
23086 k = -1;
23088 return k;
23092 /* Return the index of the first glyph preceding glyph string S that
23093 is overwriting S because of its right overhang. Value is -1 if no
23094 glyph in front of S overwrites S. */
23096 static int
23097 left_overwriting (struct glyph_string *s)
23099 int i, k, x;
23100 struct glyph *glyphs = s->row->glyphs[s->area];
23101 int first = s->first_glyph - glyphs;
23103 k = -1;
23104 x = 0;
23105 for (i = first - 1; i >= 0; --i)
23107 int left, right;
23108 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23109 if (x + right > 0)
23110 k = i;
23111 x -= glyphs[i].pixel_width;
23114 return k;
23118 /* Return the index of the last glyph following glyph string S that is
23119 overwritten by S because of S's right overhang. Value is -1 if
23120 no such glyph is found. */
23122 static int
23123 right_overwritten (struct glyph_string *s)
23125 int k = -1;
23127 if (s->right_overhang)
23129 int x = 0, i;
23130 struct glyph *glyphs = s->row->glyphs[s->area];
23131 int first = (s->first_glyph - glyphs
23132 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23133 int end = s->row->used[s->area];
23135 for (i = first; i < end && s->right_overhang > x; ++i)
23136 x += glyphs[i].pixel_width;
23138 k = i;
23141 return k;
23145 /* Return the index of the last glyph following glyph string S that
23146 overwrites S because of its left overhang. Value is negative
23147 if no such glyph is found. */
23149 static int
23150 right_overwriting (struct glyph_string *s)
23152 int i, k, x;
23153 int end = s->row->used[s->area];
23154 struct glyph *glyphs = s->row->glyphs[s->area];
23155 int first = (s->first_glyph - glyphs
23156 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23158 k = -1;
23159 x = 0;
23160 for (i = first; i < end; ++i)
23162 int left, right;
23163 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23164 if (x - left < 0)
23165 k = i;
23166 x += glyphs[i].pixel_width;
23169 return k;
23173 /* Set background width of glyph string S. START is the index of the
23174 first glyph following S. LAST_X is the right-most x-position + 1
23175 in the drawing area. */
23177 static void
23178 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23180 /* If the face of this glyph string has to be drawn to the end of
23181 the drawing area, set S->extends_to_end_of_line_p. */
23183 if (start == s->row->used[s->area]
23184 && s->area == TEXT_AREA
23185 && ((s->row->fill_line_p
23186 && (s->hl == DRAW_NORMAL_TEXT
23187 || s->hl == DRAW_IMAGE_RAISED
23188 || s->hl == DRAW_IMAGE_SUNKEN))
23189 || s->hl == DRAW_MOUSE_FACE))
23190 s->extends_to_end_of_line_p = 1;
23192 /* If S extends its face to the end of the line, set its
23193 background_width to the distance to the right edge of the drawing
23194 area. */
23195 if (s->extends_to_end_of_line_p)
23196 s->background_width = last_x - s->x + 1;
23197 else
23198 s->background_width = s->width;
23202 /* Compute overhangs and x-positions for glyph string S and its
23203 predecessors, or successors. X is the starting x-position for S.
23204 BACKWARD_P non-zero means process predecessors. */
23206 static void
23207 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23209 if (backward_p)
23211 while (s)
23213 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23214 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23215 x -= s->width;
23216 s->x = x;
23217 s = s->prev;
23220 else
23222 while (s)
23224 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23225 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23226 s->x = x;
23227 x += s->width;
23228 s = s->next;
23235 /* The following macros are only called from draw_glyphs below.
23236 They reference the following parameters of that function directly:
23237 `w', `row', `area', and `overlap_p'
23238 as well as the following local variables:
23239 `s', `f', and `hdc' (in W32) */
23241 #ifdef HAVE_NTGUI
23242 /* On W32, silently add local `hdc' variable to argument list of
23243 init_glyph_string. */
23244 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23245 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23246 #else
23247 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23248 init_glyph_string (s, char2b, w, row, area, start, hl)
23249 #endif
23251 /* Add a glyph string for a stretch glyph to the list of strings
23252 between HEAD and TAIL. START is the index of the stretch glyph in
23253 row area AREA of glyph row ROW. END is the index of the last glyph
23254 in that glyph row area. X is the current output position assigned
23255 to the new glyph string constructed. HL overrides that face of the
23256 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23257 is the right-most x-position of the drawing area. */
23259 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23260 and below -- keep them on one line. */
23261 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23262 do \
23264 s = alloca (sizeof *s); \
23265 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23266 START = fill_stretch_glyph_string (s, START, END); \
23267 append_glyph_string (&HEAD, &TAIL, s); \
23268 s->x = (X); \
23270 while (0)
23273 /* Add a glyph string for an image glyph to the list of strings
23274 between HEAD and TAIL. START is the index of the image glyph in
23275 row area AREA of glyph row ROW. END is the index of the last glyph
23276 in that glyph row area. X is the current output position assigned
23277 to the new glyph string constructed. HL overrides that face of the
23278 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23279 is the right-most x-position of the drawing area. */
23281 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23282 do \
23284 s = alloca (sizeof *s); \
23285 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23286 fill_image_glyph_string (s); \
23287 append_glyph_string (&HEAD, &TAIL, s); \
23288 ++START; \
23289 s->x = (X); \
23291 while (0)
23294 /* Add a glyph string for a sequence of character glyphs to the list
23295 of strings between HEAD and TAIL. START is the index of the first
23296 glyph in row area AREA of glyph row ROW that is part of the new
23297 glyph string. END is the index of the last glyph in that glyph row
23298 area. X is the current output position assigned to the new glyph
23299 string constructed. HL overrides that face of the glyph; e.g. it
23300 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23301 right-most x-position of the drawing area. */
23303 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23304 do \
23306 int face_id; \
23307 XChar2b *char2b; \
23309 face_id = (row)->glyphs[area][START].face_id; \
23311 s = alloca (sizeof *s); \
23312 char2b = alloca ((END - START) * sizeof *char2b); \
23313 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23314 append_glyph_string (&HEAD, &TAIL, s); \
23315 s->x = (X); \
23316 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23318 while (0)
23321 /* Add a glyph string for a composite sequence to the list of strings
23322 between HEAD and TAIL. START is the index of the first glyph in
23323 row area AREA of glyph row ROW that is part of the new glyph
23324 string. END is the index of the last glyph in that glyph row area.
23325 X is the current output position assigned to the new glyph string
23326 constructed. HL overrides that face of the glyph; e.g. it is
23327 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23328 x-position of the drawing area. */
23330 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23331 do { \
23332 int face_id = (row)->glyphs[area][START].face_id; \
23333 struct face *base_face = FACE_FROM_ID (f, face_id); \
23334 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23335 struct composition *cmp = composition_table[cmp_id]; \
23336 XChar2b *char2b; \
23337 struct glyph_string *first_s = NULL; \
23338 int n; \
23340 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23342 /* Make glyph_strings for each glyph sequence that is drawable by \
23343 the same face, and append them to HEAD/TAIL. */ \
23344 for (n = 0; n < cmp->glyph_len;) \
23346 s = alloca (sizeof *s); \
23347 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23348 append_glyph_string (&(HEAD), &(TAIL), s); \
23349 s->cmp = cmp; \
23350 s->cmp_from = n; \
23351 s->x = (X); \
23352 if (n == 0) \
23353 first_s = s; \
23354 n = fill_composite_glyph_string (s, base_face, overlaps); \
23357 ++START; \
23358 s = first_s; \
23359 } while (0)
23362 /* Add a glyph string for a glyph-string sequence to the list of strings
23363 between HEAD and TAIL. */
23365 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23366 do { \
23367 int face_id; \
23368 XChar2b *char2b; \
23369 Lisp_Object gstring; \
23371 face_id = (row)->glyphs[area][START].face_id; \
23372 gstring = (composition_gstring_from_id \
23373 ((row)->glyphs[area][START].u.cmp.id)); \
23374 s = alloca (sizeof *s); \
23375 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23376 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23377 append_glyph_string (&(HEAD), &(TAIL), s); \
23378 s->x = (X); \
23379 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23380 } while (0)
23383 /* Add a glyph string for a sequence of glyphless character's glyphs
23384 to the list of strings between HEAD and TAIL. The meanings of
23385 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23387 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23388 do \
23390 int face_id; \
23392 face_id = (row)->glyphs[area][START].face_id; \
23394 s = alloca (sizeof *s); \
23395 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23396 append_glyph_string (&HEAD, &TAIL, s); \
23397 s->x = (X); \
23398 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23399 overlaps); \
23401 while (0)
23404 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23405 of AREA of glyph row ROW on window W between indices START and END.
23406 HL overrides the face for drawing glyph strings, e.g. it is
23407 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23408 x-positions of the drawing area.
23410 This is an ugly monster macro construct because we must use alloca
23411 to allocate glyph strings (because draw_glyphs can be called
23412 asynchronously). */
23414 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23415 do \
23417 HEAD = TAIL = NULL; \
23418 while (START < END) \
23420 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23421 switch (first_glyph->type) \
23423 case CHAR_GLYPH: \
23424 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23425 HL, X, LAST_X); \
23426 break; \
23428 case COMPOSITE_GLYPH: \
23429 if (first_glyph->u.cmp.automatic) \
23430 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23431 HL, X, LAST_X); \
23432 else \
23433 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23434 HL, X, LAST_X); \
23435 break; \
23437 case STRETCH_GLYPH: \
23438 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23439 HL, X, LAST_X); \
23440 break; \
23442 case IMAGE_GLYPH: \
23443 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23444 HL, X, LAST_X); \
23445 break; \
23447 case GLYPHLESS_GLYPH: \
23448 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23449 HL, X, LAST_X); \
23450 break; \
23452 default: \
23453 emacs_abort (); \
23456 if (s) \
23458 set_glyph_string_background_width (s, START, LAST_X); \
23459 (X) += s->width; \
23462 } while (0)
23465 /* Draw glyphs between START and END in AREA of ROW on window W,
23466 starting at x-position X. X is relative to AREA in W. HL is a
23467 face-override with the following meaning:
23469 DRAW_NORMAL_TEXT draw normally
23470 DRAW_CURSOR draw in cursor face
23471 DRAW_MOUSE_FACE draw in mouse face.
23472 DRAW_INVERSE_VIDEO draw in mode line face
23473 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23474 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23476 If OVERLAPS is non-zero, draw only the foreground of characters and
23477 clip to the physical height of ROW. Non-zero value also defines
23478 the overlapping part to be drawn:
23480 OVERLAPS_PRED overlap with preceding rows
23481 OVERLAPS_SUCC overlap with succeeding rows
23482 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23483 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23485 Value is the x-position reached, relative to AREA of W. */
23487 static int
23488 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23489 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23490 enum draw_glyphs_face hl, int overlaps)
23492 struct glyph_string *head, *tail;
23493 struct glyph_string *s;
23494 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23495 int i, j, x_reached, last_x, area_left = 0;
23496 struct frame *f = XFRAME (WINDOW_FRAME (w));
23497 DECLARE_HDC (hdc);
23499 ALLOCATE_HDC (hdc, f);
23501 /* Let's rather be paranoid than getting a SEGV. */
23502 end = min (end, row->used[area]);
23503 start = max (0, start);
23504 start = min (end, start);
23506 /* Translate X to frame coordinates. Set last_x to the right
23507 end of the drawing area. */
23508 if (row->full_width_p)
23510 /* X is relative to the left edge of W, without scroll bars
23511 or fringes. */
23512 area_left = WINDOW_LEFT_EDGE_X (w);
23513 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23515 else
23517 area_left = window_box_left (w, area);
23518 last_x = area_left + window_box_width (w, area);
23520 x += area_left;
23522 /* Build a doubly-linked list of glyph_string structures between
23523 head and tail from what we have to draw. Note that the macro
23524 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23525 the reason we use a separate variable `i'. */
23526 i = start;
23527 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23528 if (tail)
23529 x_reached = tail->x + tail->background_width;
23530 else
23531 x_reached = x;
23533 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23534 the row, redraw some glyphs in front or following the glyph
23535 strings built above. */
23536 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23538 struct glyph_string *h, *t;
23539 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23540 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23541 int check_mouse_face = 0;
23542 int dummy_x = 0;
23544 /* If mouse highlighting is on, we may need to draw adjacent
23545 glyphs using mouse-face highlighting. */
23546 if (area == TEXT_AREA && row->mouse_face_p
23547 && hlinfo->mouse_face_beg_row >= 0
23548 && hlinfo->mouse_face_end_row >= 0)
23550 struct glyph_row *mouse_beg_row, *mouse_end_row;
23552 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23553 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23555 if (row >= mouse_beg_row && row <= mouse_end_row)
23557 check_mouse_face = 1;
23558 mouse_beg_col = (row == mouse_beg_row)
23559 ? hlinfo->mouse_face_beg_col : 0;
23560 mouse_end_col = (row == mouse_end_row)
23561 ? hlinfo->mouse_face_end_col
23562 : row->used[TEXT_AREA];
23566 /* Compute overhangs for all glyph strings. */
23567 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23568 for (s = head; s; s = s->next)
23569 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23571 /* Prepend glyph strings for glyphs in front of the first glyph
23572 string that are overwritten because of the first glyph
23573 string's left overhang. The background of all strings
23574 prepended must be drawn because the first glyph string
23575 draws over it. */
23576 i = left_overwritten (head);
23577 if (i >= 0)
23579 enum draw_glyphs_face overlap_hl;
23581 /* If this row contains mouse highlighting, attempt to draw
23582 the overlapped glyphs with the correct highlight. This
23583 code fails if the overlap encompasses more than one glyph
23584 and mouse-highlight spans only some of these glyphs.
23585 However, making it work perfectly involves a lot more
23586 code, and I don't know if the pathological case occurs in
23587 practice, so we'll stick to this for now. --- cyd */
23588 if (check_mouse_face
23589 && mouse_beg_col < start && mouse_end_col > i)
23590 overlap_hl = DRAW_MOUSE_FACE;
23591 else
23592 overlap_hl = DRAW_NORMAL_TEXT;
23594 j = i;
23595 BUILD_GLYPH_STRINGS (j, start, h, t,
23596 overlap_hl, dummy_x, last_x);
23597 start = i;
23598 compute_overhangs_and_x (t, head->x, 1);
23599 prepend_glyph_string_lists (&head, &tail, h, t);
23600 clip_head = head;
23603 /* Prepend glyph strings for glyphs in front of the first glyph
23604 string that overwrite that glyph string because of their
23605 right overhang. For these strings, only the foreground must
23606 be drawn, because it draws over the glyph string at `head'.
23607 The background must not be drawn because this would overwrite
23608 right overhangs of preceding glyphs for which no glyph
23609 strings exist. */
23610 i = left_overwriting (head);
23611 if (i >= 0)
23613 enum draw_glyphs_face overlap_hl;
23615 if (check_mouse_face
23616 && mouse_beg_col < start && mouse_end_col > i)
23617 overlap_hl = DRAW_MOUSE_FACE;
23618 else
23619 overlap_hl = DRAW_NORMAL_TEXT;
23621 clip_head = head;
23622 BUILD_GLYPH_STRINGS (i, start, h, t,
23623 overlap_hl, dummy_x, last_x);
23624 for (s = h; s; s = s->next)
23625 s->background_filled_p = 1;
23626 compute_overhangs_and_x (t, head->x, 1);
23627 prepend_glyph_string_lists (&head, &tail, h, t);
23630 /* Append glyphs strings for glyphs following the last glyph
23631 string tail that are overwritten by tail. The background of
23632 these strings has to be drawn because tail's foreground draws
23633 over it. */
23634 i = right_overwritten (tail);
23635 if (i >= 0)
23637 enum draw_glyphs_face overlap_hl;
23639 if (check_mouse_face
23640 && mouse_beg_col < i && mouse_end_col > end)
23641 overlap_hl = DRAW_MOUSE_FACE;
23642 else
23643 overlap_hl = DRAW_NORMAL_TEXT;
23645 BUILD_GLYPH_STRINGS (end, i, h, t,
23646 overlap_hl, x, last_x);
23647 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23648 we don't have `end = i;' here. */
23649 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23650 append_glyph_string_lists (&head, &tail, h, t);
23651 clip_tail = tail;
23654 /* Append glyph strings for glyphs following the last glyph
23655 string tail that overwrite tail. The foreground of such
23656 glyphs has to be drawn because it writes into the background
23657 of tail. The background must not be drawn because it could
23658 paint over the foreground of following glyphs. */
23659 i = right_overwriting (tail);
23660 if (i >= 0)
23662 enum draw_glyphs_face overlap_hl;
23663 if (check_mouse_face
23664 && mouse_beg_col < i && mouse_end_col > end)
23665 overlap_hl = DRAW_MOUSE_FACE;
23666 else
23667 overlap_hl = DRAW_NORMAL_TEXT;
23669 clip_tail = tail;
23670 i++; /* We must include the Ith glyph. */
23671 BUILD_GLYPH_STRINGS (end, i, h, t,
23672 overlap_hl, x, last_x);
23673 for (s = h; s; s = s->next)
23674 s->background_filled_p = 1;
23675 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23676 append_glyph_string_lists (&head, &tail, h, t);
23678 if (clip_head || clip_tail)
23679 for (s = head; s; s = s->next)
23681 s->clip_head = clip_head;
23682 s->clip_tail = clip_tail;
23686 /* Draw all strings. */
23687 for (s = head; s; s = s->next)
23688 FRAME_RIF (f)->draw_glyph_string (s);
23690 #ifndef HAVE_NS
23691 /* When focus a sole frame and move horizontally, this sets on_p to 0
23692 causing a failure to erase prev cursor position. */
23693 if (area == TEXT_AREA
23694 && !row->full_width_p
23695 /* When drawing overlapping rows, only the glyph strings'
23696 foreground is drawn, which doesn't erase a cursor
23697 completely. */
23698 && !overlaps)
23700 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23701 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23702 : (tail ? tail->x + tail->background_width : x));
23703 x0 -= area_left;
23704 x1 -= area_left;
23706 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23707 row->y, MATRIX_ROW_BOTTOM_Y (row));
23709 #endif
23711 /* Value is the x-position up to which drawn, relative to AREA of W.
23712 This doesn't include parts drawn because of overhangs. */
23713 if (row->full_width_p)
23714 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23715 else
23716 x_reached -= area_left;
23718 RELEASE_HDC (hdc, f);
23720 return x_reached;
23723 /* Expand row matrix if too narrow. Don't expand if area
23724 is not present. */
23726 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23728 if (!fonts_changed_p \
23729 && (it->glyph_row->glyphs[area] \
23730 < it->glyph_row->glyphs[area + 1])) \
23732 it->w->ncols_scale_factor++; \
23733 fonts_changed_p = 1; \
23737 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23738 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23740 static void
23741 append_glyph (struct it *it)
23743 struct glyph *glyph;
23744 enum glyph_row_area area = it->area;
23746 eassert (it->glyph_row);
23747 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23749 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23750 if (glyph < it->glyph_row->glyphs[area + 1])
23752 /* If the glyph row is reversed, we need to prepend the glyph
23753 rather than append it. */
23754 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23756 struct glyph *g;
23758 /* Make room for the additional glyph. */
23759 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23760 g[1] = *g;
23761 glyph = it->glyph_row->glyphs[area];
23763 glyph->charpos = CHARPOS (it->position);
23764 glyph->object = it->object;
23765 if (it->pixel_width > 0)
23767 glyph->pixel_width = it->pixel_width;
23768 glyph->padding_p = 0;
23770 else
23772 /* Assure at least 1-pixel width. Otherwise, cursor can't
23773 be displayed correctly. */
23774 glyph->pixel_width = 1;
23775 glyph->padding_p = 1;
23777 glyph->ascent = it->ascent;
23778 glyph->descent = it->descent;
23779 glyph->voffset = it->voffset;
23780 glyph->type = CHAR_GLYPH;
23781 glyph->avoid_cursor_p = it->avoid_cursor_p;
23782 glyph->multibyte_p = it->multibyte_p;
23783 glyph->left_box_line_p = it->start_of_box_run_p;
23784 glyph->right_box_line_p = it->end_of_box_run_p;
23785 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23786 || it->phys_descent > it->descent);
23787 glyph->glyph_not_available_p = it->glyph_not_available_p;
23788 glyph->face_id = it->face_id;
23789 glyph->u.ch = it->char_to_display;
23790 glyph->slice.img = null_glyph_slice;
23791 glyph->font_type = FONT_TYPE_UNKNOWN;
23792 if (it->bidi_p)
23794 glyph->resolved_level = it->bidi_it.resolved_level;
23795 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23796 emacs_abort ();
23797 glyph->bidi_type = it->bidi_it.type;
23799 else
23801 glyph->resolved_level = 0;
23802 glyph->bidi_type = UNKNOWN_BT;
23804 ++it->glyph_row->used[area];
23806 else
23807 IT_EXPAND_MATRIX_WIDTH (it, area);
23810 /* Store one glyph for the composition IT->cmp_it.id in
23811 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23812 non-null. */
23814 static void
23815 append_composite_glyph (struct it *it)
23817 struct glyph *glyph;
23818 enum glyph_row_area area = it->area;
23820 eassert (it->glyph_row);
23822 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23823 if (glyph < it->glyph_row->glyphs[area + 1])
23825 /* If the glyph row is reversed, we need to prepend the glyph
23826 rather than append it. */
23827 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23829 struct glyph *g;
23831 /* Make room for the new glyph. */
23832 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23833 g[1] = *g;
23834 glyph = it->glyph_row->glyphs[it->area];
23836 glyph->charpos = it->cmp_it.charpos;
23837 glyph->object = it->object;
23838 glyph->pixel_width = it->pixel_width;
23839 glyph->ascent = it->ascent;
23840 glyph->descent = it->descent;
23841 glyph->voffset = it->voffset;
23842 glyph->type = COMPOSITE_GLYPH;
23843 if (it->cmp_it.ch < 0)
23845 glyph->u.cmp.automatic = 0;
23846 glyph->u.cmp.id = it->cmp_it.id;
23847 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23849 else
23851 glyph->u.cmp.automatic = 1;
23852 glyph->u.cmp.id = it->cmp_it.id;
23853 glyph->slice.cmp.from = it->cmp_it.from;
23854 glyph->slice.cmp.to = it->cmp_it.to - 1;
23856 glyph->avoid_cursor_p = it->avoid_cursor_p;
23857 glyph->multibyte_p = it->multibyte_p;
23858 glyph->left_box_line_p = it->start_of_box_run_p;
23859 glyph->right_box_line_p = it->end_of_box_run_p;
23860 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23861 || it->phys_descent > it->descent);
23862 glyph->padding_p = 0;
23863 glyph->glyph_not_available_p = 0;
23864 glyph->face_id = it->face_id;
23865 glyph->font_type = FONT_TYPE_UNKNOWN;
23866 if (it->bidi_p)
23868 glyph->resolved_level = it->bidi_it.resolved_level;
23869 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23870 emacs_abort ();
23871 glyph->bidi_type = it->bidi_it.type;
23873 ++it->glyph_row->used[area];
23875 else
23876 IT_EXPAND_MATRIX_WIDTH (it, area);
23880 /* Change IT->ascent and IT->height according to the setting of
23881 IT->voffset. */
23883 static void
23884 take_vertical_position_into_account (struct it *it)
23886 if (it->voffset)
23888 if (it->voffset < 0)
23889 /* Increase the ascent so that we can display the text higher
23890 in the line. */
23891 it->ascent -= it->voffset;
23892 else
23893 /* Increase the descent so that we can display the text lower
23894 in the line. */
23895 it->descent += it->voffset;
23900 /* Produce glyphs/get display metrics for the image IT is loaded with.
23901 See the description of struct display_iterator in dispextern.h for
23902 an overview of struct display_iterator. */
23904 static void
23905 produce_image_glyph (struct it *it)
23907 struct image *img;
23908 struct face *face;
23909 int glyph_ascent, crop;
23910 struct glyph_slice slice;
23912 eassert (it->what == IT_IMAGE);
23914 face = FACE_FROM_ID (it->f, it->face_id);
23915 eassert (face);
23916 /* Make sure X resources of the face is loaded. */
23917 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23919 if (it->image_id < 0)
23921 /* Fringe bitmap. */
23922 it->ascent = it->phys_ascent = 0;
23923 it->descent = it->phys_descent = 0;
23924 it->pixel_width = 0;
23925 it->nglyphs = 0;
23926 return;
23929 img = IMAGE_FROM_ID (it->f, it->image_id);
23930 eassert (img);
23931 /* Make sure X resources of the image is loaded. */
23932 prepare_image_for_display (it->f, img);
23934 slice.x = slice.y = 0;
23935 slice.width = img->width;
23936 slice.height = img->height;
23938 if (INTEGERP (it->slice.x))
23939 slice.x = XINT (it->slice.x);
23940 else if (FLOATP (it->slice.x))
23941 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23943 if (INTEGERP (it->slice.y))
23944 slice.y = XINT (it->slice.y);
23945 else if (FLOATP (it->slice.y))
23946 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23948 if (INTEGERP (it->slice.width))
23949 slice.width = XINT (it->slice.width);
23950 else if (FLOATP (it->slice.width))
23951 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23953 if (INTEGERP (it->slice.height))
23954 slice.height = XINT (it->slice.height);
23955 else if (FLOATP (it->slice.height))
23956 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23958 if (slice.x >= img->width)
23959 slice.x = img->width;
23960 if (slice.y >= img->height)
23961 slice.y = img->height;
23962 if (slice.x + slice.width >= img->width)
23963 slice.width = img->width - slice.x;
23964 if (slice.y + slice.height > img->height)
23965 slice.height = img->height - slice.y;
23967 if (slice.width == 0 || slice.height == 0)
23968 return;
23970 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23972 it->descent = slice.height - glyph_ascent;
23973 if (slice.y == 0)
23974 it->descent += img->vmargin;
23975 if (slice.y + slice.height == img->height)
23976 it->descent += img->vmargin;
23977 it->phys_descent = it->descent;
23979 it->pixel_width = slice.width;
23980 if (slice.x == 0)
23981 it->pixel_width += img->hmargin;
23982 if (slice.x + slice.width == img->width)
23983 it->pixel_width += img->hmargin;
23985 /* It's quite possible for images to have an ascent greater than
23986 their height, so don't get confused in that case. */
23987 if (it->descent < 0)
23988 it->descent = 0;
23990 it->nglyphs = 1;
23992 if (face->box != FACE_NO_BOX)
23994 if (face->box_line_width > 0)
23996 if (slice.y == 0)
23997 it->ascent += face->box_line_width;
23998 if (slice.y + slice.height == img->height)
23999 it->descent += face->box_line_width;
24002 if (it->start_of_box_run_p && slice.x == 0)
24003 it->pixel_width += eabs (face->box_line_width);
24004 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24005 it->pixel_width += eabs (face->box_line_width);
24008 take_vertical_position_into_account (it);
24010 /* Automatically crop wide image glyphs at right edge so we can
24011 draw the cursor on same display row. */
24012 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24013 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24015 it->pixel_width -= crop;
24016 slice.width -= crop;
24019 if (it->glyph_row)
24021 struct glyph *glyph;
24022 enum glyph_row_area area = it->area;
24024 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24025 if (glyph < it->glyph_row->glyphs[area + 1])
24027 glyph->charpos = CHARPOS (it->position);
24028 glyph->object = it->object;
24029 glyph->pixel_width = it->pixel_width;
24030 glyph->ascent = glyph_ascent;
24031 glyph->descent = it->descent;
24032 glyph->voffset = it->voffset;
24033 glyph->type = IMAGE_GLYPH;
24034 glyph->avoid_cursor_p = it->avoid_cursor_p;
24035 glyph->multibyte_p = it->multibyte_p;
24036 glyph->left_box_line_p = it->start_of_box_run_p;
24037 glyph->right_box_line_p = it->end_of_box_run_p;
24038 glyph->overlaps_vertically_p = 0;
24039 glyph->padding_p = 0;
24040 glyph->glyph_not_available_p = 0;
24041 glyph->face_id = it->face_id;
24042 glyph->u.img_id = img->id;
24043 glyph->slice.img = slice;
24044 glyph->font_type = FONT_TYPE_UNKNOWN;
24045 if (it->bidi_p)
24047 glyph->resolved_level = it->bidi_it.resolved_level;
24048 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24049 emacs_abort ();
24050 glyph->bidi_type = it->bidi_it.type;
24052 ++it->glyph_row->used[area];
24054 else
24055 IT_EXPAND_MATRIX_WIDTH (it, area);
24060 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24061 of the glyph, WIDTH and HEIGHT are the width and height of the
24062 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24064 static void
24065 append_stretch_glyph (struct it *it, Lisp_Object object,
24066 int width, int height, int ascent)
24068 struct glyph *glyph;
24069 enum glyph_row_area area = it->area;
24071 eassert (ascent >= 0 && ascent <= height);
24073 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24074 if (glyph < it->glyph_row->glyphs[area + 1])
24076 /* If the glyph row is reversed, we need to prepend the glyph
24077 rather than append it. */
24078 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24080 struct glyph *g;
24082 /* Make room for the additional glyph. */
24083 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24084 g[1] = *g;
24085 glyph = it->glyph_row->glyphs[area];
24087 glyph->charpos = CHARPOS (it->position);
24088 glyph->object = object;
24089 glyph->pixel_width = width;
24090 glyph->ascent = ascent;
24091 glyph->descent = height - ascent;
24092 glyph->voffset = it->voffset;
24093 glyph->type = STRETCH_GLYPH;
24094 glyph->avoid_cursor_p = it->avoid_cursor_p;
24095 glyph->multibyte_p = it->multibyte_p;
24096 glyph->left_box_line_p = it->start_of_box_run_p;
24097 glyph->right_box_line_p = it->end_of_box_run_p;
24098 glyph->overlaps_vertically_p = 0;
24099 glyph->padding_p = 0;
24100 glyph->glyph_not_available_p = 0;
24101 glyph->face_id = it->face_id;
24102 glyph->u.stretch.ascent = ascent;
24103 glyph->u.stretch.height = height;
24104 glyph->slice.img = null_glyph_slice;
24105 glyph->font_type = FONT_TYPE_UNKNOWN;
24106 if (it->bidi_p)
24108 glyph->resolved_level = it->bidi_it.resolved_level;
24109 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24110 emacs_abort ();
24111 glyph->bidi_type = it->bidi_it.type;
24113 else
24115 glyph->resolved_level = 0;
24116 glyph->bidi_type = UNKNOWN_BT;
24118 ++it->glyph_row->used[area];
24120 else
24121 IT_EXPAND_MATRIX_WIDTH (it, area);
24124 #endif /* HAVE_WINDOW_SYSTEM */
24126 /* Produce a stretch glyph for iterator IT. IT->object is the value
24127 of the glyph property displayed. The value must be a list
24128 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24129 being recognized:
24131 1. `:width WIDTH' specifies that the space should be WIDTH *
24132 canonical char width wide. WIDTH may be an integer or floating
24133 point number.
24135 2. `:relative-width FACTOR' specifies that the width of the stretch
24136 should be computed from the width of the first character having the
24137 `glyph' property, and should be FACTOR times that width.
24139 3. `:align-to HPOS' specifies that the space should be wide enough
24140 to reach HPOS, a value in canonical character units.
24142 Exactly one of the above pairs must be present.
24144 4. `:height HEIGHT' specifies that the height of the stretch produced
24145 should be HEIGHT, measured in canonical character units.
24147 5. `:relative-height FACTOR' specifies that the height of the
24148 stretch should be FACTOR times the height of the characters having
24149 the glyph property.
24151 Either none or exactly one of 4 or 5 must be present.
24153 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24154 of the stretch should be used for the ascent of the stretch.
24155 ASCENT must be in the range 0 <= ASCENT <= 100. */
24157 void
24158 produce_stretch_glyph (struct it *it)
24160 /* (space :width WIDTH :height HEIGHT ...) */
24161 Lisp_Object prop, plist;
24162 int width = 0, height = 0, align_to = -1;
24163 int zero_width_ok_p = 0;
24164 double tem;
24165 struct font *font = NULL;
24167 #ifdef HAVE_WINDOW_SYSTEM
24168 int ascent = 0;
24169 int zero_height_ok_p = 0;
24171 if (FRAME_WINDOW_P (it->f))
24173 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24174 font = face->font ? face->font : FRAME_FONT (it->f);
24175 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24177 #endif
24179 /* List should start with `space'. */
24180 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24181 plist = XCDR (it->object);
24183 /* Compute the width of the stretch. */
24184 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24185 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24187 /* Absolute width `:width WIDTH' specified and valid. */
24188 zero_width_ok_p = 1;
24189 width = (int)tem;
24191 #ifdef HAVE_WINDOW_SYSTEM
24192 else if (FRAME_WINDOW_P (it->f)
24193 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24195 /* Relative width `:relative-width FACTOR' specified and valid.
24196 Compute the width of the characters having the `glyph'
24197 property. */
24198 struct it it2;
24199 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24201 it2 = *it;
24202 if (it->multibyte_p)
24203 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24204 else
24206 it2.c = it2.char_to_display = *p, it2.len = 1;
24207 if (! ASCII_CHAR_P (it2.c))
24208 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24211 it2.glyph_row = NULL;
24212 it2.what = IT_CHARACTER;
24213 x_produce_glyphs (&it2);
24214 width = NUMVAL (prop) * it2.pixel_width;
24216 #endif /* HAVE_WINDOW_SYSTEM */
24217 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24218 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24220 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24221 align_to = (align_to < 0
24223 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24224 else if (align_to < 0)
24225 align_to = window_box_left_offset (it->w, TEXT_AREA);
24226 width = max (0, (int)tem + align_to - it->current_x);
24227 zero_width_ok_p = 1;
24229 else
24230 /* Nothing specified -> width defaults to canonical char width. */
24231 width = FRAME_COLUMN_WIDTH (it->f);
24233 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24234 width = 1;
24236 #ifdef HAVE_WINDOW_SYSTEM
24237 /* Compute height. */
24238 if (FRAME_WINDOW_P (it->f))
24240 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24241 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24243 height = (int)tem;
24244 zero_height_ok_p = 1;
24246 else if (prop = Fplist_get (plist, QCrelative_height),
24247 NUMVAL (prop) > 0)
24248 height = FONT_HEIGHT (font) * NUMVAL (prop);
24249 else
24250 height = FONT_HEIGHT (font);
24252 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24253 height = 1;
24255 /* Compute percentage of height used for ascent. If
24256 `:ascent ASCENT' is present and valid, use that. Otherwise,
24257 derive the ascent from the font in use. */
24258 if (prop = Fplist_get (plist, QCascent),
24259 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24260 ascent = height * NUMVAL (prop) / 100.0;
24261 else if (!NILP (prop)
24262 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24263 ascent = min (max (0, (int)tem), height);
24264 else
24265 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24267 else
24268 #endif /* HAVE_WINDOW_SYSTEM */
24269 height = 1;
24271 if (width > 0 && it->line_wrap != TRUNCATE
24272 && it->current_x + width > it->last_visible_x)
24274 width = it->last_visible_x - it->current_x;
24275 #ifdef HAVE_WINDOW_SYSTEM
24276 /* Subtract one more pixel from the stretch width, but only on
24277 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24278 width -= FRAME_WINDOW_P (it->f);
24279 #endif
24282 if (width > 0 && height > 0 && it->glyph_row)
24284 Lisp_Object o_object = it->object;
24285 Lisp_Object object = it->stack[it->sp - 1].string;
24286 int n = width;
24288 if (!STRINGP (object))
24289 object = it->w->buffer;
24290 #ifdef HAVE_WINDOW_SYSTEM
24291 if (FRAME_WINDOW_P (it->f))
24292 append_stretch_glyph (it, object, width, height, ascent);
24293 else
24294 #endif
24296 it->object = object;
24297 it->char_to_display = ' ';
24298 it->pixel_width = it->len = 1;
24299 while (n--)
24300 tty_append_glyph (it);
24301 it->object = o_object;
24305 it->pixel_width = width;
24306 #ifdef HAVE_WINDOW_SYSTEM
24307 if (FRAME_WINDOW_P (it->f))
24309 it->ascent = it->phys_ascent = ascent;
24310 it->descent = it->phys_descent = height - it->ascent;
24311 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24312 take_vertical_position_into_account (it);
24314 else
24315 #endif
24316 it->nglyphs = width;
24319 /* Get information about special display element WHAT in an
24320 environment described by IT. WHAT is one of IT_TRUNCATION or
24321 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24322 non-null glyph_row member. This function ensures that fields like
24323 face_id, c, len of IT are left untouched. */
24325 static void
24326 produce_special_glyphs (struct it *it, enum display_element_type what)
24328 struct it temp_it;
24329 Lisp_Object gc;
24330 GLYPH glyph;
24332 temp_it = *it;
24333 temp_it.object = make_number (0);
24334 memset (&temp_it.current, 0, sizeof temp_it.current);
24336 if (what == IT_CONTINUATION)
24338 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24339 if (it->bidi_it.paragraph_dir == R2L)
24340 SET_GLYPH_FROM_CHAR (glyph, '/');
24341 else
24342 SET_GLYPH_FROM_CHAR (glyph, '\\');
24343 if (it->dp
24344 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24346 /* FIXME: Should we mirror GC for R2L lines? */
24347 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24348 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24351 else if (what == IT_TRUNCATION)
24353 /* Truncation glyph. */
24354 SET_GLYPH_FROM_CHAR (glyph, '$');
24355 if (it->dp
24356 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24358 /* FIXME: Should we mirror GC for R2L lines? */
24359 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24360 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24363 else
24364 emacs_abort ();
24366 #ifdef HAVE_WINDOW_SYSTEM
24367 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24368 is turned off, we precede the truncation/continuation glyphs by a
24369 stretch glyph whose width is computed such that these special
24370 glyphs are aligned at the window margin, even when very different
24371 fonts are used in different glyph rows. */
24372 if (FRAME_WINDOW_P (temp_it.f)
24373 /* init_iterator calls this with it->glyph_row == NULL, and it
24374 wants only the pixel width of the truncation/continuation
24375 glyphs. */
24376 && temp_it.glyph_row
24377 /* insert_left_trunc_glyphs calls us at the beginning of the
24378 row, and it has its own calculation of the stretch glyph
24379 width. */
24380 && temp_it.glyph_row->used[TEXT_AREA] > 0
24381 && (temp_it.glyph_row->reversed_p
24382 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24383 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24385 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24387 if (stretch_width > 0)
24389 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24390 struct font *font =
24391 face->font ? face->font : FRAME_FONT (temp_it.f);
24392 int stretch_ascent =
24393 (((temp_it.ascent + temp_it.descent)
24394 * FONT_BASE (font)) / FONT_HEIGHT (font));
24396 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24397 temp_it.ascent + temp_it.descent,
24398 stretch_ascent);
24401 #endif
24403 temp_it.dp = NULL;
24404 temp_it.what = IT_CHARACTER;
24405 temp_it.len = 1;
24406 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24407 temp_it.face_id = GLYPH_FACE (glyph);
24408 temp_it.len = CHAR_BYTES (temp_it.c);
24410 PRODUCE_GLYPHS (&temp_it);
24411 it->pixel_width = temp_it.pixel_width;
24412 it->nglyphs = temp_it.pixel_width;
24415 #ifdef HAVE_WINDOW_SYSTEM
24417 /* Calculate line-height and line-spacing properties.
24418 An integer value specifies explicit pixel value.
24419 A float value specifies relative value to current face height.
24420 A cons (float . face-name) specifies relative value to
24421 height of specified face font.
24423 Returns height in pixels, or nil. */
24426 static Lisp_Object
24427 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24428 int boff, int override)
24430 Lisp_Object face_name = Qnil;
24431 int ascent, descent, height;
24433 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24434 return val;
24436 if (CONSP (val))
24438 face_name = XCAR (val);
24439 val = XCDR (val);
24440 if (!NUMBERP (val))
24441 val = make_number (1);
24442 if (NILP (face_name))
24444 height = it->ascent + it->descent;
24445 goto scale;
24449 if (NILP (face_name))
24451 font = FRAME_FONT (it->f);
24452 boff = FRAME_BASELINE_OFFSET (it->f);
24454 else if (EQ (face_name, Qt))
24456 override = 0;
24458 else
24460 int face_id;
24461 struct face *face;
24463 face_id = lookup_named_face (it->f, face_name, 0);
24464 if (face_id < 0)
24465 return make_number (-1);
24467 face = FACE_FROM_ID (it->f, face_id);
24468 font = face->font;
24469 if (font == NULL)
24470 return make_number (-1);
24471 boff = font->baseline_offset;
24472 if (font->vertical_centering)
24473 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24476 ascent = FONT_BASE (font) + boff;
24477 descent = FONT_DESCENT (font) - boff;
24479 if (override)
24481 it->override_ascent = ascent;
24482 it->override_descent = descent;
24483 it->override_boff = boff;
24486 height = ascent + descent;
24488 scale:
24489 if (FLOATP (val))
24490 height = (int)(XFLOAT_DATA (val) * height);
24491 else if (INTEGERP (val))
24492 height *= XINT (val);
24494 return make_number (height);
24498 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24499 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24500 and only if this is for a character for which no font was found.
24502 If the display method (it->glyphless_method) is
24503 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24504 length of the acronym or the hexadecimal string, UPPER_XOFF and
24505 UPPER_YOFF are pixel offsets for the upper part of the string,
24506 LOWER_XOFF and LOWER_YOFF are for the lower part.
24508 For the other display methods, LEN through LOWER_YOFF are zero. */
24510 static void
24511 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24512 short upper_xoff, short upper_yoff,
24513 short lower_xoff, short lower_yoff)
24515 struct glyph *glyph;
24516 enum glyph_row_area area = it->area;
24518 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24519 if (glyph < it->glyph_row->glyphs[area + 1])
24521 /* If the glyph row is reversed, we need to prepend the glyph
24522 rather than append it. */
24523 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24525 struct glyph *g;
24527 /* Make room for the additional glyph. */
24528 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24529 g[1] = *g;
24530 glyph = it->glyph_row->glyphs[area];
24532 glyph->charpos = CHARPOS (it->position);
24533 glyph->object = it->object;
24534 glyph->pixel_width = it->pixel_width;
24535 glyph->ascent = it->ascent;
24536 glyph->descent = it->descent;
24537 glyph->voffset = it->voffset;
24538 glyph->type = GLYPHLESS_GLYPH;
24539 glyph->u.glyphless.method = it->glyphless_method;
24540 glyph->u.glyphless.for_no_font = for_no_font;
24541 glyph->u.glyphless.len = len;
24542 glyph->u.glyphless.ch = it->c;
24543 glyph->slice.glyphless.upper_xoff = upper_xoff;
24544 glyph->slice.glyphless.upper_yoff = upper_yoff;
24545 glyph->slice.glyphless.lower_xoff = lower_xoff;
24546 glyph->slice.glyphless.lower_yoff = lower_yoff;
24547 glyph->avoid_cursor_p = it->avoid_cursor_p;
24548 glyph->multibyte_p = it->multibyte_p;
24549 glyph->left_box_line_p = it->start_of_box_run_p;
24550 glyph->right_box_line_p = it->end_of_box_run_p;
24551 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24552 || it->phys_descent > it->descent);
24553 glyph->padding_p = 0;
24554 glyph->glyph_not_available_p = 0;
24555 glyph->face_id = face_id;
24556 glyph->font_type = FONT_TYPE_UNKNOWN;
24557 if (it->bidi_p)
24559 glyph->resolved_level = it->bidi_it.resolved_level;
24560 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24561 emacs_abort ();
24562 glyph->bidi_type = it->bidi_it.type;
24564 ++it->glyph_row->used[area];
24566 else
24567 IT_EXPAND_MATRIX_WIDTH (it, area);
24571 /* Produce a glyph for a glyphless character for iterator IT.
24572 IT->glyphless_method specifies which method to use for displaying
24573 the character. See the description of enum
24574 glyphless_display_method in dispextern.h for the detail.
24576 FOR_NO_FONT is nonzero if and only if this is for a character for
24577 which no font was found. ACRONYM, if non-nil, is an acronym string
24578 for the character. */
24580 static void
24581 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24583 int face_id;
24584 struct face *face;
24585 struct font *font;
24586 int base_width, base_height, width, height;
24587 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24588 int len;
24590 /* Get the metrics of the base font. We always refer to the current
24591 ASCII face. */
24592 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24593 font = face->font ? face->font : FRAME_FONT (it->f);
24594 it->ascent = FONT_BASE (font) + font->baseline_offset;
24595 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24596 base_height = it->ascent + it->descent;
24597 base_width = font->average_width;
24599 /* Get a face ID for the glyph by utilizing a cache (the same way as
24600 done for `escape-glyph' in get_next_display_element). */
24601 if (it->f == last_glyphless_glyph_frame
24602 && it->face_id == last_glyphless_glyph_face_id)
24604 face_id = last_glyphless_glyph_merged_face_id;
24606 else
24608 /* Merge the `glyphless-char' face into the current face. */
24609 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24610 last_glyphless_glyph_frame = it->f;
24611 last_glyphless_glyph_face_id = it->face_id;
24612 last_glyphless_glyph_merged_face_id = face_id;
24615 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24617 it->pixel_width = THIN_SPACE_WIDTH;
24618 len = 0;
24619 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24621 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24623 width = CHAR_WIDTH (it->c);
24624 if (width == 0)
24625 width = 1;
24626 else if (width > 4)
24627 width = 4;
24628 it->pixel_width = base_width * width;
24629 len = 0;
24630 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24632 else
24634 char buf[7];
24635 const char *str;
24636 unsigned int code[6];
24637 int upper_len;
24638 int ascent, descent;
24639 struct font_metrics metrics_upper, metrics_lower;
24641 face = FACE_FROM_ID (it->f, face_id);
24642 font = face->font ? face->font : FRAME_FONT (it->f);
24643 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24645 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24647 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24648 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24649 if (CONSP (acronym))
24650 acronym = XCAR (acronym);
24651 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24653 else
24655 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24656 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24657 str = buf;
24659 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24660 code[len] = font->driver->encode_char (font, str[len]);
24661 upper_len = (len + 1) / 2;
24662 font->driver->text_extents (font, code, upper_len,
24663 &metrics_upper);
24664 font->driver->text_extents (font, code + upper_len, len - upper_len,
24665 &metrics_lower);
24669 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24670 width = max (metrics_upper.width, metrics_lower.width) + 4;
24671 upper_xoff = upper_yoff = 2; /* the typical case */
24672 if (base_width >= width)
24674 /* Align the upper to the left, the lower to the right. */
24675 it->pixel_width = base_width;
24676 lower_xoff = base_width - 2 - metrics_lower.width;
24678 else
24680 /* Center the shorter one. */
24681 it->pixel_width = width;
24682 if (metrics_upper.width >= metrics_lower.width)
24683 lower_xoff = (width - metrics_lower.width) / 2;
24684 else
24686 /* FIXME: This code doesn't look right. It formerly was
24687 missing the "lower_xoff = 0;", which couldn't have
24688 been right since it left lower_xoff uninitialized. */
24689 lower_xoff = 0;
24690 upper_xoff = (width - metrics_upper.width) / 2;
24694 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24695 top, bottom, and between upper and lower strings. */
24696 height = (metrics_upper.ascent + metrics_upper.descent
24697 + metrics_lower.ascent + metrics_lower.descent) + 5;
24698 /* Center vertically.
24699 H:base_height, D:base_descent
24700 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24702 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24703 descent = D - H/2 + h/2;
24704 lower_yoff = descent - 2 - ld;
24705 upper_yoff = lower_yoff - la - 1 - ud; */
24706 ascent = - (it->descent - (base_height + height + 1) / 2);
24707 descent = it->descent - (base_height - height) / 2;
24708 lower_yoff = descent - 2 - metrics_lower.descent;
24709 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24710 - metrics_upper.descent);
24711 /* Don't make the height shorter than the base height. */
24712 if (height > base_height)
24714 it->ascent = ascent;
24715 it->descent = descent;
24719 it->phys_ascent = it->ascent;
24720 it->phys_descent = it->descent;
24721 if (it->glyph_row)
24722 append_glyphless_glyph (it, face_id, for_no_font, len,
24723 upper_xoff, upper_yoff,
24724 lower_xoff, lower_yoff);
24725 it->nglyphs = 1;
24726 take_vertical_position_into_account (it);
24730 /* RIF:
24731 Produce glyphs/get display metrics for the display element IT is
24732 loaded with. See the description of struct it in dispextern.h
24733 for an overview of struct it. */
24735 void
24736 x_produce_glyphs (struct it *it)
24738 int extra_line_spacing = it->extra_line_spacing;
24740 it->glyph_not_available_p = 0;
24742 if (it->what == IT_CHARACTER)
24744 XChar2b char2b;
24745 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24746 struct font *font = face->font;
24747 struct font_metrics *pcm = NULL;
24748 int boff; /* baseline offset */
24750 if (font == NULL)
24752 /* When no suitable font is found, display this character by
24753 the method specified in the first extra slot of
24754 Vglyphless_char_display. */
24755 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24757 eassert (it->what == IT_GLYPHLESS);
24758 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24759 goto done;
24762 boff = font->baseline_offset;
24763 if (font->vertical_centering)
24764 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24766 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24768 int stretched_p;
24770 it->nglyphs = 1;
24772 if (it->override_ascent >= 0)
24774 it->ascent = it->override_ascent;
24775 it->descent = it->override_descent;
24776 boff = it->override_boff;
24778 else
24780 it->ascent = FONT_BASE (font) + boff;
24781 it->descent = FONT_DESCENT (font) - boff;
24784 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24786 pcm = get_per_char_metric (font, &char2b);
24787 if (pcm->width == 0
24788 && pcm->rbearing == 0 && pcm->lbearing == 0)
24789 pcm = NULL;
24792 if (pcm)
24794 it->phys_ascent = pcm->ascent + boff;
24795 it->phys_descent = pcm->descent - boff;
24796 it->pixel_width = pcm->width;
24798 else
24800 it->glyph_not_available_p = 1;
24801 it->phys_ascent = it->ascent;
24802 it->phys_descent = it->descent;
24803 it->pixel_width = font->space_width;
24806 if (it->constrain_row_ascent_descent_p)
24808 if (it->descent > it->max_descent)
24810 it->ascent += it->descent - it->max_descent;
24811 it->descent = it->max_descent;
24813 if (it->ascent > it->max_ascent)
24815 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24816 it->ascent = it->max_ascent;
24818 it->phys_ascent = min (it->phys_ascent, it->ascent);
24819 it->phys_descent = min (it->phys_descent, it->descent);
24820 extra_line_spacing = 0;
24823 /* If this is a space inside a region of text with
24824 `space-width' property, change its width. */
24825 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24826 if (stretched_p)
24827 it->pixel_width *= XFLOATINT (it->space_width);
24829 /* If face has a box, add the box thickness to the character
24830 height. If character has a box line to the left and/or
24831 right, add the box line width to the character's width. */
24832 if (face->box != FACE_NO_BOX)
24834 int thick = face->box_line_width;
24836 if (thick > 0)
24838 it->ascent += thick;
24839 it->descent += thick;
24841 else
24842 thick = -thick;
24844 if (it->start_of_box_run_p)
24845 it->pixel_width += thick;
24846 if (it->end_of_box_run_p)
24847 it->pixel_width += thick;
24850 /* If face has an overline, add the height of the overline
24851 (1 pixel) and a 1 pixel margin to the character height. */
24852 if (face->overline_p)
24853 it->ascent += overline_margin;
24855 if (it->constrain_row_ascent_descent_p)
24857 if (it->ascent > it->max_ascent)
24858 it->ascent = it->max_ascent;
24859 if (it->descent > it->max_descent)
24860 it->descent = it->max_descent;
24863 take_vertical_position_into_account (it);
24865 /* If we have to actually produce glyphs, do it. */
24866 if (it->glyph_row)
24868 if (stretched_p)
24870 /* Translate a space with a `space-width' property
24871 into a stretch glyph. */
24872 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24873 / FONT_HEIGHT (font));
24874 append_stretch_glyph (it, it->object, it->pixel_width,
24875 it->ascent + it->descent, ascent);
24877 else
24878 append_glyph (it);
24880 /* If characters with lbearing or rbearing are displayed
24881 in this line, record that fact in a flag of the
24882 glyph row. This is used to optimize X output code. */
24883 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24884 it->glyph_row->contains_overlapping_glyphs_p = 1;
24886 if (! stretched_p && it->pixel_width == 0)
24887 /* We assure that all visible glyphs have at least 1-pixel
24888 width. */
24889 it->pixel_width = 1;
24891 else if (it->char_to_display == '\n')
24893 /* A newline has no width, but we need the height of the
24894 line. But if previous part of the line sets a height,
24895 don't increase that height */
24897 Lisp_Object height;
24898 Lisp_Object total_height = Qnil;
24900 it->override_ascent = -1;
24901 it->pixel_width = 0;
24902 it->nglyphs = 0;
24904 height = get_it_property (it, Qline_height);
24905 /* Split (line-height total-height) list */
24906 if (CONSP (height)
24907 && CONSP (XCDR (height))
24908 && NILP (XCDR (XCDR (height))))
24910 total_height = XCAR (XCDR (height));
24911 height = XCAR (height);
24913 height = calc_line_height_property (it, height, font, boff, 1);
24915 if (it->override_ascent >= 0)
24917 it->ascent = it->override_ascent;
24918 it->descent = it->override_descent;
24919 boff = it->override_boff;
24921 else
24923 it->ascent = FONT_BASE (font) + boff;
24924 it->descent = FONT_DESCENT (font) - boff;
24927 if (EQ (height, Qt))
24929 if (it->descent > it->max_descent)
24931 it->ascent += it->descent - it->max_descent;
24932 it->descent = it->max_descent;
24934 if (it->ascent > it->max_ascent)
24936 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24937 it->ascent = it->max_ascent;
24939 it->phys_ascent = min (it->phys_ascent, it->ascent);
24940 it->phys_descent = min (it->phys_descent, it->descent);
24941 it->constrain_row_ascent_descent_p = 1;
24942 extra_line_spacing = 0;
24944 else
24946 Lisp_Object spacing;
24948 it->phys_ascent = it->ascent;
24949 it->phys_descent = it->descent;
24951 if ((it->max_ascent > 0 || it->max_descent > 0)
24952 && face->box != FACE_NO_BOX
24953 && face->box_line_width > 0)
24955 it->ascent += face->box_line_width;
24956 it->descent += face->box_line_width;
24958 if (!NILP (height)
24959 && XINT (height) > it->ascent + it->descent)
24960 it->ascent = XINT (height) - it->descent;
24962 if (!NILP (total_height))
24963 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24964 else
24966 spacing = get_it_property (it, Qline_spacing);
24967 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24969 if (INTEGERP (spacing))
24971 extra_line_spacing = XINT (spacing);
24972 if (!NILP (total_height))
24973 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24977 else /* i.e. (it->char_to_display == '\t') */
24979 if (font->space_width > 0)
24981 int tab_width = it->tab_width * font->space_width;
24982 int x = it->current_x + it->continuation_lines_width;
24983 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24985 /* If the distance from the current position to the next tab
24986 stop is less than a space character width, use the
24987 tab stop after that. */
24988 if (next_tab_x - x < font->space_width)
24989 next_tab_x += tab_width;
24991 it->pixel_width = next_tab_x - x;
24992 it->nglyphs = 1;
24993 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24994 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24996 if (it->glyph_row)
24998 append_stretch_glyph (it, it->object, it->pixel_width,
24999 it->ascent + it->descent, it->ascent);
25002 else
25004 it->pixel_width = 0;
25005 it->nglyphs = 1;
25009 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25011 /* A static composition.
25013 Note: A composition is represented as one glyph in the
25014 glyph matrix. There are no padding glyphs.
25016 Important note: pixel_width, ascent, and descent are the
25017 values of what is drawn by draw_glyphs (i.e. the values of
25018 the overall glyphs composed). */
25019 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25020 int boff; /* baseline offset */
25021 struct composition *cmp = composition_table[it->cmp_it.id];
25022 int glyph_len = cmp->glyph_len;
25023 struct font *font = face->font;
25025 it->nglyphs = 1;
25027 /* If we have not yet calculated pixel size data of glyphs of
25028 the composition for the current face font, calculate them
25029 now. Theoretically, we have to check all fonts for the
25030 glyphs, but that requires much time and memory space. So,
25031 here we check only the font of the first glyph. This may
25032 lead to incorrect display, but it's very rare, and C-l
25033 (recenter-top-bottom) can correct the display anyway. */
25034 if (! cmp->font || cmp->font != font)
25036 /* Ascent and descent of the font of the first character
25037 of this composition (adjusted by baseline offset).
25038 Ascent and descent of overall glyphs should not be less
25039 than these, respectively. */
25040 int font_ascent, font_descent, font_height;
25041 /* Bounding box of the overall glyphs. */
25042 int leftmost, rightmost, lowest, highest;
25043 int lbearing, rbearing;
25044 int i, width, ascent, descent;
25045 int left_padded = 0, right_padded = 0;
25046 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25047 XChar2b char2b;
25048 struct font_metrics *pcm;
25049 int font_not_found_p;
25050 ptrdiff_t pos;
25052 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25053 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25054 break;
25055 if (glyph_len < cmp->glyph_len)
25056 right_padded = 1;
25057 for (i = 0; i < glyph_len; i++)
25059 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25060 break;
25061 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25063 if (i > 0)
25064 left_padded = 1;
25066 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25067 : IT_CHARPOS (*it));
25068 /* If no suitable font is found, use the default font. */
25069 font_not_found_p = font == NULL;
25070 if (font_not_found_p)
25072 face = face->ascii_face;
25073 font = face->font;
25075 boff = font->baseline_offset;
25076 if (font->vertical_centering)
25077 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25078 font_ascent = FONT_BASE (font) + boff;
25079 font_descent = FONT_DESCENT (font) - boff;
25080 font_height = FONT_HEIGHT (font);
25082 cmp->font = font;
25084 pcm = NULL;
25085 if (! font_not_found_p)
25087 get_char_face_and_encoding (it->f, c, it->face_id,
25088 &char2b, 0);
25089 pcm = get_per_char_metric (font, &char2b);
25092 /* Initialize the bounding box. */
25093 if (pcm)
25095 width = cmp->glyph_len > 0 ? pcm->width : 0;
25096 ascent = pcm->ascent;
25097 descent = pcm->descent;
25098 lbearing = pcm->lbearing;
25099 rbearing = pcm->rbearing;
25101 else
25103 width = cmp->glyph_len > 0 ? font->space_width : 0;
25104 ascent = FONT_BASE (font);
25105 descent = FONT_DESCENT (font);
25106 lbearing = 0;
25107 rbearing = width;
25110 rightmost = width;
25111 leftmost = 0;
25112 lowest = - descent + boff;
25113 highest = ascent + boff;
25115 if (! font_not_found_p
25116 && font->default_ascent
25117 && CHAR_TABLE_P (Vuse_default_ascent)
25118 && !NILP (Faref (Vuse_default_ascent,
25119 make_number (it->char_to_display))))
25120 highest = font->default_ascent + boff;
25122 /* Draw the first glyph at the normal position. It may be
25123 shifted to right later if some other glyphs are drawn
25124 at the left. */
25125 cmp->offsets[i * 2] = 0;
25126 cmp->offsets[i * 2 + 1] = boff;
25127 cmp->lbearing = lbearing;
25128 cmp->rbearing = rbearing;
25130 /* Set cmp->offsets for the remaining glyphs. */
25131 for (i++; i < glyph_len; i++)
25133 int left, right, btm, top;
25134 int ch = COMPOSITION_GLYPH (cmp, i);
25135 int face_id;
25136 struct face *this_face;
25138 if (ch == '\t')
25139 ch = ' ';
25140 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25141 this_face = FACE_FROM_ID (it->f, face_id);
25142 font = this_face->font;
25144 if (font == NULL)
25145 pcm = NULL;
25146 else
25148 get_char_face_and_encoding (it->f, ch, face_id,
25149 &char2b, 0);
25150 pcm = get_per_char_metric (font, &char2b);
25152 if (! pcm)
25153 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25154 else
25156 width = pcm->width;
25157 ascent = pcm->ascent;
25158 descent = pcm->descent;
25159 lbearing = pcm->lbearing;
25160 rbearing = pcm->rbearing;
25161 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25163 /* Relative composition with or without
25164 alternate chars. */
25165 left = (leftmost + rightmost - width) / 2;
25166 btm = - descent + boff;
25167 if (font->relative_compose
25168 && (! CHAR_TABLE_P (Vignore_relative_composition)
25169 || NILP (Faref (Vignore_relative_composition,
25170 make_number (ch)))))
25173 if (- descent >= font->relative_compose)
25174 /* One extra pixel between two glyphs. */
25175 btm = highest + 1;
25176 else if (ascent <= 0)
25177 /* One extra pixel between two glyphs. */
25178 btm = lowest - 1 - ascent - descent;
25181 else
25183 /* A composition rule is specified by an integer
25184 value that encodes global and new reference
25185 points (GREF and NREF). GREF and NREF are
25186 specified by numbers as below:
25188 0---1---2 -- ascent
25192 9--10--11 -- center
25194 ---3---4---5--- baseline
25196 6---7---8 -- descent
25198 int rule = COMPOSITION_RULE (cmp, i);
25199 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25201 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25202 grefx = gref % 3, nrefx = nref % 3;
25203 grefy = gref / 3, nrefy = nref / 3;
25204 if (xoff)
25205 xoff = font_height * (xoff - 128) / 256;
25206 if (yoff)
25207 yoff = font_height * (yoff - 128) / 256;
25209 left = (leftmost
25210 + grefx * (rightmost - leftmost) / 2
25211 - nrefx * width / 2
25212 + xoff);
25214 btm = ((grefy == 0 ? highest
25215 : grefy == 1 ? 0
25216 : grefy == 2 ? lowest
25217 : (highest + lowest) / 2)
25218 - (nrefy == 0 ? ascent + descent
25219 : nrefy == 1 ? descent - boff
25220 : nrefy == 2 ? 0
25221 : (ascent + descent) / 2)
25222 + yoff);
25225 cmp->offsets[i * 2] = left;
25226 cmp->offsets[i * 2 + 1] = btm + descent;
25228 /* Update the bounding box of the overall glyphs. */
25229 if (width > 0)
25231 right = left + width;
25232 if (left < leftmost)
25233 leftmost = left;
25234 if (right > rightmost)
25235 rightmost = right;
25237 top = btm + descent + ascent;
25238 if (top > highest)
25239 highest = top;
25240 if (btm < lowest)
25241 lowest = btm;
25243 if (cmp->lbearing > left + lbearing)
25244 cmp->lbearing = left + lbearing;
25245 if (cmp->rbearing < left + rbearing)
25246 cmp->rbearing = left + rbearing;
25250 /* If there are glyphs whose x-offsets are negative,
25251 shift all glyphs to the right and make all x-offsets
25252 non-negative. */
25253 if (leftmost < 0)
25255 for (i = 0; i < cmp->glyph_len; i++)
25256 cmp->offsets[i * 2] -= leftmost;
25257 rightmost -= leftmost;
25258 cmp->lbearing -= leftmost;
25259 cmp->rbearing -= leftmost;
25262 if (left_padded && cmp->lbearing < 0)
25264 for (i = 0; i < cmp->glyph_len; i++)
25265 cmp->offsets[i * 2] -= cmp->lbearing;
25266 rightmost -= cmp->lbearing;
25267 cmp->rbearing -= cmp->lbearing;
25268 cmp->lbearing = 0;
25270 if (right_padded && rightmost < cmp->rbearing)
25272 rightmost = cmp->rbearing;
25275 cmp->pixel_width = rightmost;
25276 cmp->ascent = highest;
25277 cmp->descent = - lowest;
25278 if (cmp->ascent < font_ascent)
25279 cmp->ascent = font_ascent;
25280 if (cmp->descent < font_descent)
25281 cmp->descent = font_descent;
25284 if (it->glyph_row
25285 && (cmp->lbearing < 0
25286 || cmp->rbearing > cmp->pixel_width))
25287 it->glyph_row->contains_overlapping_glyphs_p = 1;
25289 it->pixel_width = cmp->pixel_width;
25290 it->ascent = it->phys_ascent = cmp->ascent;
25291 it->descent = it->phys_descent = cmp->descent;
25292 if (face->box != FACE_NO_BOX)
25294 int thick = face->box_line_width;
25296 if (thick > 0)
25298 it->ascent += thick;
25299 it->descent += thick;
25301 else
25302 thick = - thick;
25304 if (it->start_of_box_run_p)
25305 it->pixel_width += thick;
25306 if (it->end_of_box_run_p)
25307 it->pixel_width += thick;
25310 /* If face has an overline, add the height of the overline
25311 (1 pixel) and a 1 pixel margin to the character height. */
25312 if (face->overline_p)
25313 it->ascent += overline_margin;
25315 take_vertical_position_into_account (it);
25316 if (it->ascent < 0)
25317 it->ascent = 0;
25318 if (it->descent < 0)
25319 it->descent = 0;
25321 if (it->glyph_row && cmp->glyph_len > 0)
25322 append_composite_glyph (it);
25324 else if (it->what == IT_COMPOSITION)
25326 /* A dynamic (automatic) composition. */
25327 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25328 Lisp_Object gstring;
25329 struct font_metrics metrics;
25331 it->nglyphs = 1;
25333 gstring = composition_gstring_from_id (it->cmp_it.id);
25334 it->pixel_width
25335 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25336 &metrics);
25337 if (it->glyph_row
25338 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25339 it->glyph_row->contains_overlapping_glyphs_p = 1;
25340 it->ascent = it->phys_ascent = metrics.ascent;
25341 it->descent = it->phys_descent = metrics.descent;
25342 if (face->box != FACE_NO_BOX)
25344 int thick = face->box_line_width;
25346 if (thick > 0)
25348 it->ascent += thick;
25349 it->descent += thick;
25351 else
25352 thick = - thick;
25354 if (it->start_of_box_run_p)
25355 it->pixel_width += thick;
25356 if (it->end_of_box_run_p)
25357 it->pixel_width += thick;
25359 /* If face has an overline, add the height of the overline
25360 (1 pixel) and a 1 pixel margin to the character height. */
25361 if (face->overline_p)
25362 it->ascent += overline_margin;
25363 take_vertical_position_into_account (it);
25364 if (it->ascent < 0)
25365 it->ascent = 0;
25366 if (it->descent < 0)
25367 it->descent = 0;
25369 if (it->glyph_row)
25370 append_composite_glyph (it);
25372 else if (it->what == IT_GLYPHLESS)
25373 produce_glyphless_glyph (it, 0, Qnil);
25374 else if (it->what == IT_IMAGE)
25375 produce_image_glyph (it);
25376 else if (it->what == IT_STRETCH)
25377 produce_stretch_glyph (it);
25379 done:
25380 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25381 because this isn't true for images with `:ascent 100'. */
25382 eassert (it->ascent >= 0 && it->descent >= 0);
25383 if (it->area == TEXT_AREA)
25384 it->current_x += it->pixel_width;
25386 if (extra_line_spacing > 0)
25388 it->descent += extra_line_spacing;
25389 if (extra_line_spacing > it->max_extra_line_spacing)
25390 it->max_extra_line_spacing = extra_line_spacing;
25393 it->max_ascent = max (it->max_ascent, it->ascent);
25394 it->max_descent = max (it->max_descent, it->descent);
25395 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25396 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25399 /* EXPORT for RIF:
25400 Output LEN glyphs starting at START at the nominal cursor position.
25401 Advance the nominal cursor over the text. The global variable
25402 updated_window contains the window being updated, updated_row is
25403 the glyph row being updated, and updated_area is the area of that
25404 row being updated. */
25406 void
25407 x_write_glyphs (struct glyph *start, int len)
25409 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25411 eassert (updated_window && updated_row);
25412 /* When the window is hscrolled, cursor hpos can legitimately be out
25413 of bounds, but we draw the cursor at the corresponding window
25414 margin in that case. */
25415 if (!updated_row->reversed_p && chpos < 0)
25416 chpos = 0;
25417 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25418 chpos = updated_row->used[TEXT_AREA] - 1;
25420 block_input ();
25422 /* Write glyphs. */
25424 hpos = start - updated_row->glyphs[updated_area];
25425 x = draw_glyphs (updated_window, output_cursor.x,
25426 updated_row, updated_area,
25427 hpos, hpos + len,
25428 DRAW_NORMAL_TEXT, 0);
25430 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25431 if (updated_area == TEXT_AREA
25432 && updated_window->phys_cursor_on_p
25433 && updated_window->phys_cursor.vpos == output_cursor.vpos
25434 && chpos >= hpos
25435 && chpos < hpos + len)
25436 updated_window->phys_cursor_on_p = 0;
25438 unblock_input ();
25440 /* Advance the output cursor. */
25441 output_cursor.hpos += len;
25442 output_cursor.x = x;
25446 /* EXPORT for RIF:
25447 Insert LEN glyphs from START at the nominal cursor position. */
25449 void
25450 x_insert_glyphs (struct glyph *start, int len)
25452 struct frame *f;
25453 struct window *w;
25454 int line_height, shift_by_width, shifted_region_width;
25455 struct glyph_row *row;
25456 struct glyph *glyph;
25457 int frame_x, frame_y;
25458 ptrdiff_t hpos;
25460 eassert (updated_window && updated_row);
25461 block_input ();
25462 w = updated_window;
25463 f = XFRAME (WINDOW_FRAME (w));
25465 /* Get the height of the line we are in. */
25466 row = updated_row;
25467 line_height = row->height;
25469 /* Get the width of the glyphs to insert. */
25470 shift_by_width = 0;
25471 for (glyph = start; glyph < start + len; ++glyph)
25472 shift_by_width += glyph->pixel_width;
25474 /* Get the width of the region to shift right. */
25475 shifted_region_width = (window_box_width (w, updated_area)
25476 - output_cursor.x
25477 - shift_by_width);
25479 /* Shift right. */
25480 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25481 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25483 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25484 line_height, shift_by_width);
25486 /* Write the glyphs. */
25487 hpos = start - row->glyphs[updated_area];
25488 draw_glyphs (w, output_cursor.x, row, updated_area,
25489 hpos, hpos + len,
25490 DRAW_NORMAL_TEXT, 0);
25492 /* Advance the output cursor. */
25493 output_cursor.hpos += len;
25494 output_cursor.x += shift_by_width;
25495 unblock_input ();
25499 /* EXPORT for RIF:
25500 Erase the current text line from the nominal cursor position
25501 (inclusive) to pixel column TO_X (exclusive). The idea is that
25502 everything from TO_X onward is already erased.
25504 TO_X is a pixel position relative to updated_area of
25505 updated_window. TO_X == -1 means clear to the end of this area. */
25507 void
25508 x_clear_end_of_line (int to_x)
25510 struct frame *f;
25511 struct window *w = updated_window;
25512 int max_x, min_y, max_y;
25513 int from_x, from_y, to_y;
25515 eassert (updated_window && updated_row);
25516 f = XFRAME (w->frame);
25518 if (updated_row->full_width_p)
25519 max_x = WINDOW_TOTAL_WIDTH (w);
25520 else
25521 max_x = window_box_width (w, updated_area);
25522 max_y = window_text_bottom_y (w);
25524 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25525 of window. For TO_X > 0, truncate to end of drawing area. */
25526 if (to_x == 0)
25527 return;
25528 else if (to_x < 0)
25529 to_x = max_x;
25530 else
25531 to_x = min (to_x, max_x);
25533 to_y = min (max_y, output_cursor.y + updated_row->height);
25535 /* Notice if the cursor will be cleared by this operation. */
25536 if (!updated_row->full_width_p)
25537 notice_overwritten_cursor (w, updated_area,
25538 output_cursor.x, -1,
25539 updated_row->y,
25540 MATRIX_ROW_BOTTOM_Y (updated_row));
25542 from_x = output_cursor.x;
25544 /* Translate to frame coordinates. */
25545 if (updated_row->full_width_p)
25547 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25548 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25550 else
25552 int area_left = window_box_left (w, updated_area);
25553 from_x += area_left;
25554 to_x += area_left;
25557 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25558 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25559 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25561 /* Prevent inadvertently clearing to end of the X window. */
25562 if (to_x > from_x && to_y > from_y)
25564 block_input ();
25565 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25566 to_x - from_x, to_y - from_y);
25567 unblock_input ();
25571 #endif /* HAVE_WINDOW_SYSTEM */
25575 /***********************************************************************
25576 Cursor types
25577 ***********************************************************************/
25579 /* Value is the internal representation of the specified cursor type
25580 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25581 of the bar cursor. */
25583 static enum text_cursor_kinds
25584 get_specified_cursor_type (Lisp_Object arg, int *width)
25586 enum text_cursor_kinds type;
25588 if (NILP (arg))
25589 return NO_CURSOR;
25591 if (EQ (arg, Qbox))
25592 return FILLED_BOX_CURSOR;
25594 if (EQ (arg, Qhollow))
25595 return HOLLOW_BOX_CURSOR;
25597 if (EQ (arg, Qbar))
25599 *width = 2;
25600 return BAR_CURSOR;
25603 if (CONSP (arg)
25604 && EQ (XCAR (arg), Qbar)
25605 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25607 *width = XINT (XCDR (arg));
25608 return BAR_CURSOR;
25611 if (EQ (arg, Qhbar))
25613 *width = 2;
25614 return HBAR_CURSOR;
25617 if (CONSP (arg)
25618 && EQ (XCAR (arg), Qhbar)
25619 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25621 *width = XINT (XCDR (arg));
25622 return HBAR_CURSOR;
25625 /* Treat anything unknown as "hollow box cursor".
25626 It was bad to signal an error; people have trouble fixing
25627 .Xdefaults with Emacs, when it has something bad in it. */
25628 type = HOLLOW_BOX_CURSOR;
25630 return type;
25633 /* Set the default cursor types for specified frame. */
25634 void
25635 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25637 int width = 1;
25638 Lisp_Object tem;
25640 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25641 FRAME_CURSOR_WIDTH (f) = width;
25643 /* By default, set up the blink-off state depending on the on-state. */
25645 tem = Fassoc (arg, Vblink_cursor_alist);
25646 if (!NILP (tem))
25648 FRAME_BLINK_OFF_CURSOR (f)
25649 = get_specified_cursor_type (XCDR (tem), &width);
25650 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25652 else
25653 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25657 #ifdef HAVE_WINDOW_SYSTEM
25659 /* Return the cursor we want to be displayed in window W. Return
25660 width of bar/hbar cursor through WIDTH arg. Return with
25661 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25662 (i.e. if the `system caret' should track this cursor).
25664 In a mini-buffer window, we want the cursor only to appear if we
25665 are reading input from this window. For the selected window, we
25666 want the cursor type given by the frame parameter or buffer local
25667 setting of cursor-type. If explicitly marked off, draw no cursor.
25668 In all other cases, we want a hollow box cursor. */
25670 static enum text_cursor_kinds
25671 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25672 int *active_cursor)
25674 struct frame *f = XFRAME (w->frame);
25675 struct buffer *b = XBUFFER (w->buffer);
25676 int cursor_type = DEFAULT_CURSOR;
25677 Lisp_Object alt_cursor;
25678 int non_selected = 0;
25680 *active_cursor = 1;
25682 /* Echo area */
25683 if (cursor_in_echo_area
25684 && FRAME_HAS_MINIBUF_P (f)
25685 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25687 if (w == XWINDOW (echo_area_window))
25689 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25691 *width = FRAME_CURSOR_WIDTH (f);
25692 return FRAME_DESIRED_CURSOR (f);
25694 else
25695 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25698 *active_cursor = 0;
25699 non_selected = 1;
25702 /* Detect a nonselected window or nonselected frame. */
25703 else if (w != XWINDOW (f->selected_window)
25704 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25706 *active_cursor = 0;
25708 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25709 return NO_CURSOR;
25711 non_selected = 1;
25714 /* Never display a cursor in a window in which cursor-type is nil. */
25715 if (NILP (BVAR (b, cursor_type)))
25716 return NO_CURSOR;
25718 /* Get the normal cursor type for this window. */
25719 if (EQ (BVAR (b, cursor_type), Qt))
25721 cursor_type = FRAME_DESIRED_CURSOR (f);
25722 *width = FRAME_CURSOR_WIDTH (f);
25724 else
25725 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25727 /* Use cursor-in-non-selected-windows instead
25728 for non-selected window or frame. */
25729 if (non_selected)
25731 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25732 if (!EQ (Qt, alt_cursor))
25733 return get_specified_cursor_type (alt_cursor, width);
25734 /* t means modify the normal cursor type. */
25735 if (cursor_type == FILLED_BOX_CURSOR)
25736 cursor_type = HOLLOW_BOX_CURSOR;
25737 else if (cursor_type == BAR_CURSOR && *width > 1)
25738 --*width;
25739 return cursor_type;
25742 /* Use normal cursor if not blinked off. */
25743 if (!w->cursor_off_p)
25745 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25747 if (cursor_type == FILLED_BOX_CURSOR)
25749 /* Using a block cursor on large images can be very annoying.
25750 So use a hollow cursor for "large" images.
25751 If image is not transparent (no mask), also use hollow cursor. */
25752 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25753 if (img != NULL && IMAGEP (img->spec))
25755 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25756 where N = size of default frame font size.
25757 This should cover most of the "tiny" icons people may use. */
25758 if (!img->mask
25759 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25760 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25761 cursor_type = HOLLOW_BOX_CURSOR;
25764 else if (cursor_type != NO_CURSOR)
25766 /* Display current only supports BOX and HOLLOW cursors for images.
25767 So for now, unconditionally use a HOLLOW cursor when cursor is
25768 not a solid box cursor. */
25769 cursor_type = HOLLOW_BOX_CURSOR;
25772 return cursor_type;
25775 /* Cursor is blinked off, so determine how to "toggle" it. */
25777 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25778 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25779 return get_specified_cursor_type (XCDR (alt_cursor), width);
25781 /* Then see if frame has specified a specific blink off cursor type. */
25782 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25784 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25785 return FRAME_BLINK_OFF_CURSOR (f);
25788 #if 0
25789 /* Some people liked having a permanently visible blinking cursor,
25790 while others had very strong opinions against it. So it was
25791 decided to remove it. KFS 2003-09-03 */
25793 /* Finally perform built-in cursor blinking:
25794 filled box <-> hollow box
25795 wide [h]bar <-> narrow [h]bar
25796 narrow [h]bar <-> no cursor
25797 other type <-> no cursor */
25799 if (cursor_type == FILLED_BOX_CURSOR)
25800 return HOLLOW_BOX_CURSOR;
25802 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25804 *width = 1;
25805 return cursor_type;
25807 #endif
25809 return NO_CURSOR;
25813 /* Notice when the text cursor of window W has been completely
25814 overwritten by a drawing operation that outputs glyphs in AREA
25815 starting at X0 and ending at X1 in the line starting at Y0 and
25816 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25817 the rest of the line after X0 has been written. Y coordinates
25818 are window-relative. */
25820 static void
25821 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25822 int x0, int x1, int y0, int y1)
25824 int cx0, cx1, cy0, cy1;
25825 struct glyph_row *row;
25827 if (!w->phys_cursor_on_p)
25828 return;
25829 if (area != TEXT_AREA)
25830 return;
25832 if (w->phys_cursor.vpos < 0
25833 || w->phys_cursor.vpos >= w->current_matrix->nrows
25834 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25835 !(row->enabled_p && row->displays_text_p)))
25836 return;
25838 if (row->cursor_in_fringe_p)
25840 row->cursor_in_fringe_p = 0;
25841 draw_fringe_bitmap (w, row, row->reversed_p);
25842 w->phys_cursor_on_p = 0;
25843 return;
25846 cx0 = w->phys_cursor.x;
25847 cx1 = cx0 + w->phys_cursor_width;
25848 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25849 return;
25851 /* The cursor image will be completely removed from the
25852 screen if the output area intersects the cursor area in
25853 y-direction. When we draw in [y0 y1[, and some part of
25854 the cursor is at y < y0, that part must have been drawn
25855 before. When scrolling, the cursor is erased before
25856 actually scrolling, so we don't come here. When not
25857 scrolling, the rows above the old cursor row must have
25858 changed, and in this case these rows must have written
25859 over the cursor image.
25861 Likewise if part of the cursor is below y1, with the
25862 exception of the cursor being in the first blank row at
25863 the buffer and window end because update_text_area
25864 doesn't draw that row. (Except when it does, but
25865 that's handled in update_text_area.) */
25867 cy0 = w->phys_cursor.y;
25868 cy1 = cy0 + w->phys_cursor_height;
25869 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25870 return;
25872 w->phys_cursor_on_p = 0;
25875 #endif /* HAVE_WINDOW_SYSTEM */
25878 /************************************************************************
25879 Mouse Face
25880 ************************************************************************/
25882 #ifdef HAVE_WINDOW_SYSTEM
25884 /* EXPORT for RIF:
25885 Fix the display of area AREA of overlapping row ROW in window W
25886 with respect to the overlapping part OVERLAPS. */
25888 void
25889 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25890 enum glyph_row_area area, int overlaps)
25892 int i, x;
25894 block_input ();
25896 x = 0;
25897 for (i = 0; i < row->used[area];)
25899 if (row->glyphs[area][i].overlaps_vertically_p)
25901 int start = i, start_x = x;
25905 x += row->glyphs[area][i].pixel_width;
25906 ++i;
25908 while (i < row->used[area]
25909 && row->glyphs[area][i].overlaps_vertically_p);
25911 draw_glyphs (w, start_x, row, area,
25912 start, i,
25913 DRAW_NORMAL_TEXT, overlaps);
25915 else
25917 x += row->glyphs[area][i].pixel_width;
25918 ++i;
25922 unblock_input ();
25926 /* EXPORT:
25927 Draw the cursor glyph of window W in glyph row ROW. See the
25928 comment of draw_glyphs for the meaning of HL. */
25930 void
25931 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25932 enum draw_glyphs_face hl)
25934 /* If cursor hpos is out of bounds, don't draw garbage. This can
25935 happen in mini-buffer windows when switching between echo area
25936 glyphs and mini-buffer. */
25937 if ((row->reversed_p
25938 ? (w->phys_cursor.hpos >= 0)
25939 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25941 int on_p = w->phys_cursor_on_p;
25942 int x1;
25943 int hpos = w->phys_cursor.hpos;
25945 /* When the window is hscrolled, cursor hpos can legitimately be
25946 out of bounds, but we draw the cursor at the corresponding
25947 window margin in that case. */
25948 if (!row->reversed_p && hpos < 0)
25949 hpos = 0;
25950 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25951 hpos = row->used[TEXT_AREA] - 1;
25953 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25954 hl, 0);
25955 w->phys_cursor_on_p = on_p;
25957 if (hl == DRAW_CURSOR)
25958 w->phys_cursor_width = x1 - w->phys_cursor.x;
25959 /* When we erase the cursor, and ROW is overlapped by other
25960 rows, make sure that these overlapping parts of other rows
25961 are redrawn. */
25962 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25964 w->phys_cursor_width = x1 - w->phys_cursor.x;
25966 if (row > w->current_matrix->rows
25967 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25968 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25969 OVERLAPS_ERASED_CURSOR);
25971 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25972 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25973 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25974 OVERLAPS_ERASED_CURSOR);
25980 /* EXPORT:
25981 Erase the image of a cursor of window W from the screen. */
25983 void
25984 erase_phys_cursor (struct window *w)
25986 struct frame *f = XFRAME (w->frame);
25987 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25988 int hpos = w->phys_cursor.hpos;
25989 int vpos = w->phys_cursor.vpos;
25990 int mouse_face_here_p = 0;
25991 struct glyph_matrix *active_glyphs = w->current_matrix;
25992 struct glyph_row *cursor_row;
25993 struct glyph *cursor_glyph;
25994 enum draw_glyphs_face hl;
25996 /* No cursor displayed or row invalidated => nothing to do on the
25997 screen. */
25998 if (w->phys_cursor_type == NO_CURSOR)
25999 goto mark_cursor_off;
26001 /* VPOS >= active_glyphs->nrows means that window has been resized.
26002 Don't bother to erase the cursor. */
26003 if (vpos >= active_glyphs->nrows)
26004 goto mark_cursor_off;
26006 /* If row containing cursor is marked invalid, there is nothing we
26007 can do. */
26008 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26009 if (!cursor_row->enabled_p)
26010 goto mark_cursor_off;
26012 /* If line spacing is > 0, old cursor may only be partially visible in
26013 window after split-window. So adjust visible height. */
26014 cursor_row->visible_height = min (cursor_row->visible_height,
26015 window_text_bottom_y (w) - cursor_row->y);
26017 /* If row is completely invisible, don't attempt to delete a cursor which
26018 isn't there. This can happen if cursor is at top of a window, and
26019 we switch to a buffer with a header line in that window. */
26020 if (cursor_row->visible_height <= 0)
26021 goto mark_cursor_off;
26023 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26024 if (cursor_row->cursor_in_fringe_p)
26026 cursor_row->cursor_in_fringe_p = 0;
26027 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26028 goto mark_cursor_off;
26031 /* This can happen when the new row is shorter than the old one.
26032 In this case, either draw_glyphs or clear_end_of_line
26033 should have cleared the cursor. Note that we wouldn't be
26034 able to erase the cursor in this case because we don't have a
26035 cursor glyph at hand. */
26036 if ((cursor_row->reversed_p
26037 ? (w->phys_cursor.hpos < 0)
26038 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26039 goto mark_cursor_off;
26041 /* When the window is hscrolled, cursor hpos can legitimately be out
26042 of bounds, but we draw the cursor at the corresponding window
26043 margin in that case. */
26044 if (!cursor_row->reversed_p && hpos < 0)
26045 hpos = 0;
26046 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26047 hpos = cursor_row->used[TEXT_AREA] - 1;
26049 /* If the cursor is in the mouse face area, redisplay that when
26050 we clear the cursor. */
26051 if (! NILP (hlinfo->mouse_face_window)
26052 && coords_in_mouse_face_p (w, hpos, vpos)
26053 /* Don't redraw the cursor's spot in mouse face if it is at the
26054 end of a line (on a newline). The cursor appears there, but
26055 mouse highlighting does not. */
26056 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26057 mouse_face_here_p = 1;
26059 /* Maybe clear the display under the cursor. */
26060 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26062 int x, y, left_x;
26063 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26064 int width;
26066 cursor_glyph = get_phys_cursor_glyph (w);
26067 if (cursor_glyph == NULL)
26068 goto mark_cursor_off;
26070 width = cursor_glyph->pixel_width;
26071 left_x = window_box_left_offset (w, TEXT_AREA);
26072 x = w->phys_cursor.x;
26073 if (x < left_x)
26074 width -= left_x - x;
26075 width = min (width, window_box_width (w, TEXT_AREA) - x);
26076 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26077 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26079 if (width > 0)
26080 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26083 /* Erase the cursor by redrawing the character underneath it. */
26084 if (mouse_face_here_p)
26085 hl = DRAW_MOUSE_FACE;
26086 else
26087 hl = DRAW_NORMAL_TEXT;
26088 draw_phys_cursor_glyph (w, cursor_row, hl);
26090 mark_cursor_off:
26091 w->phys_cursor_on_p = 0;
26092 w->phys_cursor_type = NO_CURSOR;
26096 /* EXPORT:
26097 Display or clear cursor of window W. If ON is zero, clear the
26098 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26099 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26101 void
26102 display_and_set_cursor (struct window *w, int on,
26103 int hpos, int vpos, int x, int y)
26105 struct frame *f = XFRAME (w->frame);
26106 int new_cursor_type;
26107 int new_cursor_width;
26108 int active_cursor;
26109 struct glyph_row *glyph_row;
26110 struct glyph *glyph;
26112 /* This is pointless on invisible frames, and dangerous on garbaged
26113 windows and frames; in the latter case, the frame or window may
26114 be in the midst of changing its size, and x and y may be off the
26115 window. */
26116 if (! FRAME_VISIBLE_P (f)
26117 || FRAME_GARBAGED_P (f)
26118 || vpos >= w->current_matrix->nrows
26119 || hpos >= w->current_matrix->matrix_w)
26120 return;
26122 /* If cursor is off and we want it off, return quickly. */
26123 if (!on && !w->phys_cursor_on_p)
26124 return;
26126 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26127 /* If cursor row is not enabled, we don't really know where to
26128 display the cursor. */
26129 if (!glyph_row->enabled_p)
26131 w->phys_cursor_on_p = 0;
26132 return;
26135 glyph = NULL;
26136 if (!glyph_row->exact_window_width_line_p
26137 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26138 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26140 eassert (input_blocked_p ());
26142 /* Set new_cursor_type to the cursor we want to be displayed. */
26143 new_cursor_type = get_window_cursor_type (w, glyph,
26144 &new_cursor_width, &active_cursor);
26146 /* If cursor is currently being shown and we don't want it to be or
26147 it is in the wrong place, or the cursor type is not what we want,
26148 erase it. */
26149 if (w->phys_cursor_on_p
26150 && (!on
26151 || w->phys_cursor.x != x
26152 || w->phys_cursor.y != y
26153 || new_cursor_type != w->phys_cursor_type
26154 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26155 && new_cursor_width != w->phys_cursor_width)))
26156 erase_phys_cursor (w);
26158 /* Don't check phys_cursor_on_p here because that flag is only set
26159 to zero in some cases where we know that the cursor has been
26160 completely erased, to avoid the extra work of erasing the cursor
26161 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26162 still not be visible, or it has only been partly erased. */
26163 if (on)
26165 w->phys_cursor_ascent = glyph_row->ascent;
26166 w->phys_cursor_height = glyph_row->height;
26168 /* Set phys_cursor_.* before x_draw_.* is called because some
26169 of them may need the information. */
26170 w->phys_cursor.x = x;
26171 w->phys_cursor.y = glyph_row->y;
26172 w->phys_cursor.hpos = hpos;
26173 w->phys_cursor.vpos = vpos;
26176 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26177 new_cursor_type, new_cursor_width,
26178 on, active_cursor);
26182 /* Switch the display of W's cursor on or off, according to the value
26183 of ON. */
26185 static void
26186 update_window_cursor (struct window *w, int on)
26188 /* Don't update cursor in windows whose frame is in the process
26189 of being deleted. */
26190 if (w->current_matrix)
26192 int hpos = w->phys_cursor.hpos;
26193 int vpos = w->phys_cursor.vpos;
26194 struct glyph_row *row;
26196 if (vpos >= w->current_matrix->nrows
26197 || hpos >= w->current_matrix->matrix_w)
26198 return;
26200 row = MATRIX_ROW (w->current_matrix, vpos);
26202 /* When the window is hscrolled, cursor hpos can legitimately be
26203 out of bounds, but we draw the cursor at the corresponding
26204 window margin in that case. */
26205 if (!row->reversed_p && hpos < 0)
26206 hpos = 0;
26207 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26208 hpos = row->used[TEXT_AREA] - 1;
26210 block_input ();
26211 display_and_set_cursor (w, on, hpos, vpos,
26212 w->phys_cursor.x, w->phys_cursor.y);
26213 unblock_input ();
26218 /* Call update_window_cursor with parameter ON_P on all leaf windows
26219 in the window tree rooted at W. */
26221 static void
26222 update_cursor_in_window_tree (struct window *w, int on_p)
26224 while (w)
26226 if (!NILP (w->hchild))
26227 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26228 else if (!NILP (w->vchild))
26229 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26230 else
26231 update_window_cursor (w, on_p);
26233 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26238 /* EXPORT:
26239 Display the cursor on window W, or clear it, according to ON_P.
26240 Don't change the cursor's position. */
26242 void
26243 x_update_cursor (struct frame *f, int on_p)
26245 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26249 /* EXPORT:
26250 Clear the cursor of window W to background color, and mark the
26251 cursor as not shown. This is used when the text where the cursor
26252 is about to be rewritten. */
26254 void
26255 x_clear_cursor (struct window *w)
26257 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26258 update_window_cursor (w, 0);
26261 #endif /* HAVE_WINDOW_SYSTEM */
26263 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26264 and MSDOS. */
26265 static void
26266 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26267 int start_hpos, int end_hpos,
26268 enum draw_glyphs_face draw)
26270 #ifdef HAVE_WINDOW_SYSTEM
26271 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26273 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26274 return;
26276 #endif
26277 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26278 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26279 #endif
26282 /* Display the active region described by mouse_face_* according to DRAW. */
26284 static void
26285 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26287 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26288 struct frame *f = XFRAME (WINDOW_FRAME (w));
26290 if (/* If window is in the process of being destroyed, don't bother
26291 to do anything. */
26292 w->current_matrix != NULL
26293 /* Don't update mouse highlight if hidden */
26294 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26295 /* Recognize when we are called to operate on rows that don't exist
26296 anymore. This can happen when a window is split. */
26297 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26299 int phys_cursor_on_p = w->phys_cursor_on_p;
26300 struct glyph_row *row, *first, *last;
26302 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26303 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26305 for (row = first; row <= last && row->enabled_p; ++row)
26307 int start_hpos, end_hpos, start_x;
26309 /* For all but the first row, the highlight starts at column 0. */
26310 if (row == first)
26312 /* R2L rows have BEG and END in reversed order, but the
26313 screen drawing geometry is always left to right. So
26314 we need to mirror the beginning and end of the
26315 highlighted area in R2L rows. */
26316 if (!row->reversed_p)
26318 start_hpos = hlinfo->mouse_face_beg_col;
26319 start_x = hlinfo->mouse_face_beg_x;
26321 else if (row == last)
26323 start_hpos = hlinfo->mouse_face_end_col;
26324 start_x = hlinfo->mouse_face_end_x;
26326 else
26328 start_hpos = 0;
26329 start_x = 0;
26332 else if (row->reversed_p && row == last)
26334 start_hpos = hlinfo->mouse_face_end_col;
26335 start_x = hlinfo->mouse_face_end_x;
26337 else
26339 start_hpos = 0;
26340 start_x = 0;
26343 if (row == last)
26345 if (!row->reversed_p)
26346 end_hpos = hlinfo->mouse_face_end_col;
26347 else if (row == first)
26348 end_hpos = hlinfo->mouse_face_beg_col;
26349 else
26351 end_hpos = row->used[TEXT_AREA];
26352 if (draw == DRAW_NORMAL_TEXT)
26353 row->fill_line_p = 1; /* Clear to end of line */
26356 else if (row->reversed_p && row == first)
26357 end_hpos = hlinfo->mouse_face_beg_col;
26358 else
26360 end_hpos = row->used[TEXT_AREA];
26361 if (draw == DRAW_NORMAL_TEXT)
26362 row->fill_line_p = 1; /* Clear to end of line */
26365 if (end_hpos > start_hpos)
26367 draw_row_with_mouse_face (w, start_x, row,
26368 start_hpos, end_hpos, draw);
26370 row->mouse_face_p
26371 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26375 #ifdef HAVE_WINDOW_SYSTEM
26376 /* When we've written over the cursor, arrange for it to
26377 be displayed again. */
26378 if (FRAME_WINDOW_P (f)
26379 && phys_cursor_on_p && !w->phys_cursor_on_p)
26381 int hpos = w->phys_cursor.hpos;
26383 /* When the window is hscrolled, cursor hpos can legitimately be
26384 out of bounds, but we draw the cursor at the corresponding
26385 window margin in that case. */
26386 if (!row->reversed_p && hpos < 0)
26387 hpos = 0;
26388 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26389 hpos = row->used[TEXT_AREA] - 1;
26391 block_input ();
26392 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26393 w->phys_cursor.x, w->phys_cursor.y);
26394 unblock_input ();
26396 #endif /* HAVE_WINDOW_SYSTEM */
26399 #ifdef HAVE_WINDOW_SYSTEM
26400 /* Change the mouse cursor. */
26401 if (FRAME_WINDOW_P (f))
26403 if (draw == DRAW_NORMAL_TEXT
26404 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26405 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26406 else if (draw == DRAW_MOUSE_FACE)
26407 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26408 else
26409 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26411 #endif /* HAVE_WINDOW_SYSTEM */
26414 /* EXPORT:
26415 Clear out the mouse-highlighted active region.
26416 Redraw it un-highlighted first. Value is non-zero if mouse
26417 face was actually drawn unhighlighted. */
26420 clear_mouse_face (Mouse_HLInfo *hlinfo)
26422 int cleared = 0;
26424 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26426 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26427 cleared = 1;
26430 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26431 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26432 hlinfo->mouse_face_window = Qnil;
26433 hlinfo->mouse_face_overlay = Qnil;
26434 return cleared;
26437 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26438 within the mouse face on that window. */
26439 static int
26440 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26442 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26444 /* Quickly resolve the easy cases. */
26445 if (!(WINDOWP (hlinfo->mouse_face_window)
26446 && XWINDOW (hlinfo->mouse_face_window) == w))
26447 return 0;
26448 if (vpos < hlinfo->mouse_face_beg_row
26449 || vpos > hlinfo->mouse_face_end_row)
26450 return 0;
26451 if (vpos > hlinfo->mouse_face_beg_row
26452 && vpos < hlinfo->mouse_face_end_row)
26453 return 1;
26455 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26457 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26459 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26460 return 1;
26462 else if ((vpos == hlinfo->mouse_face_beg_row
26463 && hpos >= hlinfo->mouse_face_beg_col)
26464 || (vpos == hlinfo->mouse_face_end_row
26465 && hpos < hlinfo->mouse_face_end_col))
26466 return 1;
26468 else
26470 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26472 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26473 return 1;
26475 else if ((vpos == hlinfo->mouse_face_beg_row
26476 && hpos <= hlinfo->mouse_face_beg_col)
26477 || (vpos == hlinfo->mouse_face_end_row
26478 && hpos > hlinfo->mouse_face_end_col))
26479 return 1;
26481 return 0;
26485 /* EXPORT:
26486 Non-zero if physical cursor of window W is within mouse face. */
26489 cursor_in_mouse_face_p (struct window *w)
26491 int hpos = w->phys_cursor.hpos;
26492 int vpos = w->phys_cursor.vpos;
26493 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26495 /* When the window is hscrolled, cursor hpos can legitimately be out
26496 of bounds, but we draw the cursor at the corresponding window
26497 margin in that case. */
26498 if (!row->reversed_p && hpos < 0)
26499 hpos = 0;
26500 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26501 hpos = row->used[TEXT_AREA] - 1;
26503 return coords_in_mouse_face_p (w, hpos, vpos);
26508 /* Find the glyph rows START_ROW and END_ROW of window W that display
26509 characters between buffer positions START_CHARPOS and END_CHARPOS
26510 (excluding END_CHARPOS). DISP_STRING is a display string that
26511 covers these buffer positions. This is similar to
26512 row_containing_pos, but is more accurate when bidi reordering makes
26513 buffer positions change non-linearly with glyph rows. */
26514 static void
26515 rows_from_pos_range (struct window *w,
26516 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26517 Lisp_Object disp_string,
26518 struct glyph_row **start, struct glyph_row **end)
26520 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26521 int last_y = window_text_bottom_y (w);
26522 struct glyph_row *row;
26524 *start = NULL;
26525 *end = NULL;
26527 while (!first->enabled_p
26528 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26529 first++;
26531 /* Find the START row. */
26532 for (row = first;
26533 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26534 row++)
26536 /* A row can potentially be the START row if the range of the
26537 characters it displays intersects the range
26538 [START_CHARPOS..END_CHARPOS). */
26539 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26540 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26541 /* See the commentary in row_containing_pos, for the
26542 explanation of the complicated way to check whether
26543 some position is beyond the end of the characters
26544 displayed by a row. */
26545 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26546 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26547 && !row->ends_at_zv_p
26548 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26549 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26550 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26551 && !row->ends_at_zv_p
26552 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26554 /* Found a candidate row. Now make sure at least one of the
26555 glyphs it displays has a charpos from the range
26556 [START_CHARPOS..END_CHARPOS).
26558 This is not obvious because bidi reordering could make
26559 buffer positions of a row be 1,2,3,102,101,100, and if we
26560 want to highlight characters in [50..60), we don't want
26561 this row, even though [50..60) does intersect [1..103),
26562 the range of character positions given by the row's start
26563 and end positions. */
26564 struct glyph *g = row->glyphs[TEXT_AREA];
26565 struct glyph *e = g + row->used[TEXT_AREA];
26567 while (g < e)
26569 if (((BUFFERP (g->object) || INTEGERP (g->object))
26570 && start_charpos <= g->charpos && g->charpos < end_charpos)
26571 /* A glyph that comes from DISP_STRING is by
26572 definition to be highlighted. */
26573 || EQ (g->object, disp_string))
26574 *start = row;
26575 g++;
26577 if (*start)
26578 break;
26582 /* Find the END row. */
26583 if (!*start
26584 /* If the last row is partially visible, start looking for END
26585 from that row, instead of starting from FIRST. */
26586 && !(row->enabled_p
26587 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26588 row = first;
26589 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26591 struct glyph_row *next = row + 1;
26592 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26594 if (!next->enabled_p
26595 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26596 /* The first row >= START whose range of displayed characters
26597 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26598 is the row END + 1. */
26599 || (start_charpos < next_start
26600 && end_charpos < next_start)
26601 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26602 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26603 && !next->ends_at_zv_p
26604 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26605 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26606 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26607 && !next->ends_at_zv_p
26608 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26610 *end = row;
26611 break;
26613 else
26615 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26616 but none of the characters it displays are in the range, it is
26617 also END + 1. */
26618 struct glyph *g = next->glyphs[TEXT_AREA];
26619 struct glyph *s = g;
26620 struct glyph *e = g + next->used[TEXT_AREA];
26622 while (g < e)
26624 if (((BUFFERP (g->object) || INTEGERP (g->object))
26625 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26626 /* If the buffer position of the first glyph in
26627 the row is equal to END_CHARPOS, it means
26628 the last character to be highlighted is the
26629 newline of ROW, and we must consider NEXT as
26630 END, not END+1. */
26631 || (((!next->reversed_p && g == s)
26632 || (next->reversed_p && g == e - 1))
26633 && (g->charpos == end_charpos
26634 /* Special case for when NEXT is an
26635 empty line at ZV. */
26636 || (g->charpos == -1
26637 && !row->ends_at_zv_p
26638 && next_start == end_charpos)))))
26639 /* A glyph that comes from DISP_STRING is by
26640 definition to be highlighted. */
26641 || EQ (g->object, disp_string))
26642 break;
26643 g++;
26645 if (g == e)
26647 *end = row;
26648 break;
26650 /* The first row that ends at ZV must be the last to be
26651 highlighted. */
26652 else if (next->ends_at_zv_p)
26654 *end = next;
26655 break;
26661 /* This function sets the mouse_face_* elements of HLINFO, assuming
26662 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26663 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26664 for the overlay or run of text properties specifying the mouse
26665 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26666 before-string and after-string that must also be highlighted.
26667 DISP_STRING, if non-nil, is a display string that may cover some
26668 or all of the highlighted text. */
26670 static void
26671 mouse_face_from_buffer_pos (Lisp_Object window,
26672 Mouse_HLInfo *hlinfo,
26673 ptrdiff_t mouse_charpos,
26674 ptrdiff_t start_charpos,
26675 ptrdiff_t end_charpos,
26676 Lisp_Object before_string,
26677 Lisp_Object after_string,
26678 Lisp_Object disp_string)
26680 struct window *w = XWINDOW (window);
26681 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26682 struct glyph_row *r1, *r2;
26683 struct glyph *glyph, *end;
26684 ptrdiff_t ignore, pos;
26685 int x;
26687 eassert (NILP (disp_string) || STRINGP (disp_string));
26688 eassert (NILP (before_string) || STRINGP (before_string));
26689 eassert (NILP (after_string) || STRINGP (after_string));
26691 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26692 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26693 if (r1 == NULL)
26694 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26695 /* If the before-string or display-string contains newlines,
26696 rows_from_pos_range skips to its last row. Move back. */
26697 if (!NILP (before_string) || !NILP (disp_string))
26699 struct glyph_row *prev;
26700 while ((prev = r1 - 1, prev >= first)
26701 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26702 && prev->used[TEXT_AREA] > 0)
26704 struct glyph *beg = prev->glyphs[TEXT_AREA];
26705 glyph = beg + prev->used[TEXT_AREA];
26706 while (--glyph >= beg && INTEGERP (glyph->object));
26707 if (glyph < beg
26708 || !(EQ (glyph->object, before_string)
26709 || EQ (glyph->object, disp_string)))
26710 break;
26711 r1 = prev;
26714 if (r2 == NULL)
26716 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26717 hlinfo->mouse_face_past_end = 1;
26719 else if (!NILP (after_string))
26721 /* If the after-string has newlines, advance to its last row. */
26722 struct glyph_row *next;
26723 struct glyph_row *last
26724 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26726 for (next = r2 + 1;
26727 next <= last
26728 && next->used[TEXT_AREA] > 0
26729 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26730 ++next)
26731 r2 = next;
26733 /* The rest of the display engine assumes that mouse_face_beg_row is
26734 either above mouse_face_end_row or identical to it. But with
26735 bidi-reordered continued lines, the row for START_CHARPOS could
26736 be below the row for END_CHARPOS. If so, swap the rows and store
26737 them in correct order. */
26738 if (r1->y > r2->y)
26740 struct glyph_row *tem = r2;
26742 r2 = r1;
26743 r1 = tem;
26746 hlinfo->mouse_face_beg_y = r1->y;
26747 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26748 hlinfo->mouse_face_end_y = r2->y;
26749 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26751 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26752 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26753 could be anywhere in the row and in any order. The strategy
26754 below is to find the leftmost and the rightmost glyph that
26755 belongs to either of these 3 strings, or whose position is
26756 between START_CHARPOS and END_CHARPOS, and highlight all the
26757 glyphs between those two. This may cover more than just the text
26758 between START_CHARPOS and END_CHARPOS if the range of characters
26759 strides the bidi level boundary, e.g. if the beginning is in R2L
26760 text while the end is in L2R text or vice versa. */
26761 if (!r1->reversed_p)
26763 /* This row is in a left to right paragraph. Scan it left to
26764 right. */
26765 glyph = r1->glyphs[TEXT_AREA];
26766 end = glyph + r1->used[TEXT_AREA];
26767 x = r1->x;
26769 /* Skip truncation glyphs at the start of the glyph row. */
26770 if (r1->displays_text_p)
26771 for (; glyph < end
26772 && INTEGERP (glyph->object)
26773 && glyph->charpos < 0;
26774 ++glyph)
26775 x += glyph->pixel_width;
26777 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26778 or DISP_STRING, and the first glyph from buffer whose
26779 position is between START_CHARPOS and END_CHARPOS. */
26780 for (; glyph < end
26781 && !INTEGERP (glyph->object)
26782 && !EQ (glyph->object, disp_string)
26783 && !(BUFFERP (glyph->object)
26784 && (glyph->charpos >= start_charpos
26785 && glyph->charpos < end_charpos));
26786 ++glyph)
26788 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26789 are present at buffer positions between START_CHARPOS and
26790 END_CHARPOS, or if they come from an overlay. */
26791 if (EQ (glyph->object, before_string))
26793 pos = string_buffer_position (before_string,
26794 start_charpos);
26795 /* If pos == 0, it means before_string came from an
26796 overlay, not from a buffer position. */
26797 if (!pos || (pos >= start_charpos && pos < end_charpos))
26798 break;
26800 else if (EQ (glyph->object, after_string))
26802 pos = string_buffer_position (after_string, end_charpos);
26803 if (!pos || (pos >= start_charpos && pos < end_charpos))
26804 break;
26806 x += glyph->pixel_width;
26808 hlinfo->mouse_face_beg_x = x;
26809 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26811 else
26813 /* This row is in a right to left paragraph. Scan it right to
26814 left. */
26815 struct glyph *g;
26817 end = r1->glyphs[TEXT_AREA] - 1;
26818 glyph = end + r1->used[TEXT_AREA];
26820 /* Skip truncation glyphs at the start of the glyph row. */
26821 if (r1->displays_text_p)
26822 for (; glyph > end
26823 && INTEGERP (glyph->object)
26824 && glyph->charpos < 0;
26825 --glyph)
26828 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26829 or DISP_STRING, and the first glyph from buffer whose
26830 position is between START_CHARPOS and END_CHARPOS. */
26831 for (; glyph > end
26832 && !INTEGERP (glyph->object)
26833 && !EQ (glyph->object, disp_string)
26834 && !(BUFFERP (glyph->object)
26835 && (glyph->charpos >= start_charpos
26836 && glyph->charpos < end_charpos));
26837 --glyph)
26839 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26840 are present at buffer positions between START_CHARPOS and
26841 END_CHARPOS, or if they come from an overlay. */
26842 if (EQ (glyph->object, before_string))
26844 pos = string_buffer_position (before_string, start_charpos);
26845 /* If pos == 0, it means before_string came from an
26846 overlay, not from a buffer position. */
26847 if (!pos || (pos >= start_charpos && pos < end_charpos))
26848 break;
26850 else if (EQ (glyph->object, after_string))
26852 pos = string_buffer_position (after_string, end_charpos);
26853 if (!pos || (pos >= start_charpos && pos < end_charpos))
26854 break;
26858 glyph++; /* first glyph to the right of the highlighted area */
26859 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26860 x += g->pixel_width;
26861 hlinfo->mouse_face_beg_x = x;
26862 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26865 /* If the highlight ends in a different row, compute GLYPH and END
26866 for the end row. Otherwise, reuse the values computed above for
26867 the row where the highlight begins. */
26868 if (r2 != r1)
26870 if (!r2->reversed_p)
26872 glyph = r2->glyphs[TEXT_AREA];
26873 end = glyph + r2->used[TEXT_AREA];
26874 x = r2->x;
26876 else
26878 end = r2->glyphs[TEXT_AREA] - 1;
26879 glyph = end + r2->used[TEXT_AREA];
26883 if (!r2->reversed_p)
26885 /* Skip truncation and continuation glyphs near the end of the
26886 row, and also blanks and stretch glyphs inserted by
26887 extend_face_to_end_of_line. */
26888 while (end > glyph
26889 && INTEGERP ((end - 1)->object))
26890 --end;
26891 /* Scan the rest of the glyph row from the end, looking for the
26892 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26893 DISP_STRING, or whose position is between START_CHARPOS
26894 and END_CHARPOS */
26895 for (--end;
26896 end > glyph
26897 && !INTEGERP (end->object)
26898 && !EQ (end->object, disp_string)
26899 && !(BUFFERP (end->object)
26900 && (end->charpos >= start_charpos
26901 && end->charpos < end_charpos));
26902 --end)
26904 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26905 are present at buffer positions between START_CHARPOS and
26906 END_CHARPOS, or if they come from an overlay. */
26907 if (EQ (end->object, before_string))
26909 pos = string_buffer_position (before_string, start_charpos);
26910 if (!pos || (pos >= start_charpos && pos < end_charpos))
26911 break;
26913 else if (EQ (end->object, after_string))
26915 pos = string_buffer_position (after_string, end_charpos);
26916 if (!pos || (pos >= start_charpos && pos < end_charpos))
26917 break;
26920 /* Find the X coordinate of the last glyph to be highlighted. */
26921 for (; glyph <= end; ++glyph)
26922 x += glyph->pixel_width;
26924 hlinfo->mouse_face_end_x = x;
26925 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26927 else
26929 /* Skip truncation and continuation glyphs near the end of the
26930 row, and also blanks and stretch glyphs inserted by
26931 extend_face_to_end_of_line. */
26932 x = r2->x;
26933 end++;
26934 while (end < glyph
26935 && INTEGERP (end->object))
26937 x += end->pixel_width;
26938 ++end;
26940 /* Scan the rest of the glyph row from the end, looking for the
26941 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26942 DISP_STRING, or whose position is between START_CHARPOS
26943 and END_CHARPOS */
26944 for ( ;
26945 end < glyph
26946 && !INTEGERP (end->object)
26947 && !EQ (end->object, disp_string)
26948 && !(BUFFERP (end->object)
26949 && (end->charpos >= start_charpos
26950 && end->charpos < end_charpos));
26951 ++end)
26953 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26954 are present at buffer positions between START_CHARPOS and
26955 END_CHARPOS, or if they come from an overlay. */
26956 if (EQ (end->object, before_string))
26958 pos = string_buffer_position (before_string, start_charpos);
26959 if (!pos || (pos >= start_charpos && pos < end_charpos))
26960 break;
26962 else if (EQ (end->object, after_string))
26964 pos = string_buffer_position (after_string, end_charpos);
26965 if (!pos || (pos >= start_charpos && pos < end_charpos))
26966 break;
26968 x += end->pixel_width;
26970 /* If we exited the above loop because we arrived at the last
26971 glyph of the row, and its buffer position is still not in
26972 range, it means the last character in range is the preceding
26973 newline. Bump the end column and x values to get past the
26974 last glyph. */
26975 if (end == glyph
26976 && BUFFERP (end->object)
26977 && (end->charpos < start_charpos
26978 || end->charpos >= end_charpos))
26980 x += end->pixel_width;
26981 ++end;
26983 hlinfo->mouse_face_end_x = x;
26984 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26987 hlinfo->mouse_face_window = window;
26988 hlinfo->mouse_face_face_id
26989 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26990 mouse_charpos + 1,
26991 !hlinfo->mouse_face_hidden, -1);
26992 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26995 /* The following function is not used anymore (replaced with
26996 mouse_face_from_string_pos), but I leave it here for the time
26997 being, in case someone would. */
26999 #if 0 /* not used */
27001 /* Find the position of the glyph for position POS in OBJECT in
27002 window W's current matrix, and return in *X, *Y the pixel
27003 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27005 RIGHT_P non-zero means return the position of the right edge of the
27006 glyph, RIGHT_P zero means return the left edge position.
27008 If no glyph for POS exists in the matrix, return the position of
27009 the glyph with the next smaller position that is in the matrix, if
27010 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27011 exists in the matrix, return the position of the glyph with the
27012 next larger position in OBJECT.
27014 Value is non-zero if a glyph was found. */
27016 static int
27017 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27018 int *hpos, int *vpos, int *x, int *y, int right_p)
27020 int yb = window_text_bottom_y (w);
27021 struct glyph_row *r;
27022 struct glyph *best_glyph = NULL;
27023 struct glyph_row *best_row = NULL;
27024 int best_x = 0;
27026 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27027 r->enabled_p && r->y < yb;
27028 ++r)
27030 struct glyph *g = r->glyphs[TEXT_AREA];
27031 struct glyph *e = g + r->used[TEXT_AREA];
27032 int gx;
27034 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27035 if (EQ (g->object, object))
27037 if (g->charpos == pos)
27039 best_glyph = g;
27040 best_x = gx;
27041 best_row = r;
27042 goto found;
27044 else if (best_glyph == NULL
27045 || ((eabs (g->charpos - pos)
27046 < eabs (best_glyph->charpos - pos))
27047 && (right_p
27048 ? g->charpos < pos
27049 : g->charpos > pos)))
27051 best_glyph = g;
27052 best_x = gx;
27053 best_row = r;
27058 found:
27060 if (best_glyph)
27062 *x = best_x;
27063 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27065 if (right_p)
27067 *x += best_glyph->pixel_width;
27068 ++*hpos;
27071 *y = best_row->y;
27072 *vpos = best_row - w->current_matrix->rows;
27075 return best_glyph != NULL;
27077 #endif /* not used */
27079 /* Find the positions of the first and the last glyphs in window W's
27080 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27081 (assumed to be a string), and return in HLINFO's mouse_face_*
27082 members the pixel and column/row coordinates of those glyphs. */
27084 static void
27085 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27086 Lisp_Object object,
27087 ptrdiff_t startpos, ptrdiff_t endpos)
27089 int yb = window_text_bottom_y (w);
27090 struct glyph_row *r;
27091 struct glyph *g, *e;
27092 int gx;
27093 int found = 0;
27095 /* Find the glyph row with at least one position in the range
27096 [STARTPOS..ENDPOS], and the first glyph in that row whose
27097 position belongs to that range. */
27098 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27099 r->enabled_p && r->y < yb;
27100 ++r)
27102 if (!r->reversed_p)
27104 g = r->glyphs[TEXT_AREA];
27105 e = g + r->used[TEXT_AREA];
27106 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27107 if (EQ (g->object, object)
27108 && startpos <= g->charpos && g->charpos <= endpos)
27110 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27111 hlinfo->mouse_face_beg_y = r->y;
27112 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27113 hlinfo->mouse_face_beg_x = gx;
27114 found = 1;
27115 break;
27118 else
27120 struct glyph *g1;
27122 e = r->glyphs[TEXT_AREA];
27123 g = e + r->used[TEXT_AREA];
27124 for ( ; g > e; --g)
27125 if (EQ ((g-1)->object, object)
27126 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27128 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27129 hlinfo->mouse_face_beg_y = r->y;
27130 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27131 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27132 gx += g1->pixel_width;
27133 hlinfo->mouse_face_beg_x = gx;
27134 found = 1;
27135 break;
27138 if (found)
27139 break;
27142 if (!found)
27143 return;
27145 /* Starting with the next row, look for the first row which does NOT
27146 include any glyphs whose positions are in the range. */
27147 for (++r; r->enabled_p && r->y < yb; ++r)
27149 g = r->glyphs[TEXT_AREA];
27150 e = g + r->used[TEXT_AREA];
27151 found = 0;
27152 for ( ; g < e; ++g)
27153 if (EQ (g->object, object)
27154 && startpos <= g->charpos && g->charpos <= endpos)
27156 found = 1;
27157 break;
27159 if (!found)
27160 break;
27163 /* The highlighted region ends on the previous row. */
27164 r--;
27166 /* Set the end row and its vertical pixel coordinate. */
27167 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27168 hlinfo->mouse_face_end_y = r->y;
27170 /* Compute and set the end column and the end column's horizontal
27171 pixel coordinate. */
27172 if (!r->reversed_p)
27174 g = r->glyphs[TEXT_AREA];
27175 e = g + r->used[TEXT_AREA];
27176 for ( ; e > g; --e)
27177 if (EQ ((e-1)->object, object)
27178 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27179 break;
27180 hlinfo->mouse_face_end_col = e - g;
27182 for (gx = r->x; g < e; ++g)
27183 gx += g->pixel_width;
27184 hlinfo->mouse_face_end_x = gx;
27186 else
27188 e = r->glyphs[TEXT_AREA];
27189 g = e + r->used[TEXT_AREA];
27190 for (gx = r->x ; e < g; ++e)
27192 if (EQ (e->object, object)
27193 && startpos <= e->charpos && e->charpos <= endpos)
27194 break;
27195 gx += e->pixel_width;
27197 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27198 hlinfo->mouse_face_end_x = gx;
27202 #ifdef HAVE_WINDOW_SYSTEM
27204 /* See if position X, Y is within a hot-spot of an image. */
27206 static int
27207 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27209 if (!CONSP (hot_spot))
27210 return 0;
27212 if (EQ (XCAR (hot_spot), Qrect))
27214 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27215 Lisp_Object rect = XCDR (hot_spot);
27216 Lisp_Object tem;
27217 if (!CONSP (rect))
27218 return 0;
27219 if (!CONSP (XCAR (rect)))
27220 return 0;
27221 if (!CONSP (XCDR (rect)))
27222 return 0;
27223 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27224 return 0;
27225 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27226 return 0;
27227 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27228 return 0;
27229 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27230 return 0;
27231 return 1;
27233 else if (EQ (XCAR (hot_spot), Qcircle))
27235 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27236 Lisp_Object circ = XCDR (hot_spot);
27237 Lisp_Object lr, lx0, ly0;
27238 if (CONSP (circ)
27239 && CONSP (XCAR (circ))
27240 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27241 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27242 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27244 double r = XFLOATINT (lr);
27245 double dx = XINT (lx0) - x;
27246 double dy = XINT (ly0) - y;
27247 return (dx * dx + dy * dy <= r * r);
27250 else if (EQ (XCAR (hot_spot), Qpoly))
27252 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27253 if (VECTORP (XCDR (hot_spot)))
27255 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27256 Lisp_Object *poly = v->contents;
27257 ptrdiff_t n = v->header.size;
27258 ptrdiff_t i;
27259 int inside = 0;
27260 Lisp_Object lx, ly;
27261 int x0, y0;
27263 /* Need an even number of coordinates, and at least 3 edges. */
27264 if (n < 6 || n & 1)
27265 return 0;
27267 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27268 If count is odd, we are inside polygon. Pixels on edges
27269 may or may not be included depending on actual geometry of the
27270 polygon. */
27271 if ((lx = poly[n-2], !INTEGERP (lx))
27272 || (ly = poly[n-1], !INTEGERP (lx)))
27273 return 0;
27274 x0 = XINT (lx), y0 = XINT (ly);
27275 for (i = 0; i < n; i += 2)
27277 int x1 = x0, y1 = y0;
27278 if ((lx = poly[i], !INTEGERP (lx))
27279 || (ly = poly[i+1], !INTEGERP (ly)))
27280 return 0;
27281 x0 = XINT (lx), y0 = XINT (ly);
27283 /* Does this segment cross the X line? */
27284 if (x0 >= x)
27286 if (x1 >= x)
27287 continue;
27289 else if (x1 < x)
27290 continue;
27291 if (y > y0 && y > y1)
27292 continue;
27293 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27294 inside = !inside;
27296 return inside;
27299 return 0;
27302 Lisp_Object
27303 find_hot_spot (Lisp_Object map, int x, int y)
27305 while (CONSP (map))
27307 if (CONSP (XCAR (map))
27308 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27309 return XCAR (map);
27310 map = XCDR (map);
27313 return Qnil;
27316 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27317 3, 3, 0,
27318 doc: /* Lookup in image map MAP coordinates X and Y.
27319 An image map is an alist where each element has the format (AREA ID PLIST).
27320 An AREA is specified as either a rectangle, a circle, or a polygon:
27321 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27322 pixel coordinates of the upper left and bottom right corners.
27323 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27324 and the radius of the circle; r may be a float or integer.
27325 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27326 vector describes one corner in the polygon.
27327 Returns the alist element for the first matching AREA in MAP. */)
27328 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27330 if (NILP (map))
27331 return Qnil;
27333 CHECK_NUMBER (x);
27334 CHECK_NUMBER (y);
27336 return find_hot_spot (map,
27337 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27338 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27342 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27343 static void
27344 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27346 /* Do not change cursor shape while dragging mouse. */
27347 if (!NILP (do_mouse_tracking))
27348 return;
27350 if (!NILP (pointer))
27352 if (EQ (pointer, Qarrow))
27353 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27354 else if (EQ (pointer, Qhand))
27355 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27356 else if (EQ (pointer, Qtext))
27357 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27358 else if (EQ (pointer, intern ("hdrag")))
27359 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27360 #ifdef HAVE_X_WINDOWS
27361 else if (EQ (pointer, intern ("vdrag")))
27362 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27363 #endif
27364 else if (EQ (pointer, intern ("hourglass")))
27365 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27366 else if (EQ (pointer, Qmodeline))
27367 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27368 else
27369 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27372 if (cursor != No_Cursor)
27373 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27376 #endif /* HAVE_WINDOW_SYSTEM */
27378 /* Take proper action when mouse has moved to the mode or header line
27379 or marginal area AREA of window W, x-position X and y-position Y.
27380 X is relative to the start of the text display area of W, so the
27381 width of bitmap areas and scroll bars must be subtracted to get a
27382 position relative to the start of the mode line. */
27384 static void
27385 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27386 enum window_part area)
27388 struct window *w = XWINDOW (window);
27389 struct frame *f = XFRAME (w->frame);
27390 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27391 #ifdef HAVE_WINDOW_SYSTEM
27392 Display_Info *dpyinfo;
27393 #endif
27394 Cursor cursor = No_Cursor;
27395 Lisp_Object pointer = Qnil;
27396 int dx, dy, width, height;
27397 ptrdiff_t charpos;
27398 Lisp_Object string, object = Qnil;
27399 Lisp_Object pos IF_LINT (= Qnil), help;
27401 Lisp_Object mouse_face;
27402 int original_x_pixel = x;
27403 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27404 struct glyph_row *row IF_LINT (= 0);
27406 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27408 int x0;
27409 struct glyph *end;
27411 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27412 returns them in row/column units! */
27413 string = mode_line_string (w, area, &x, &y, &charpos,
27414 &object, &dx, &dy, &width, &height);
27416 row = (area == ON_MODE_LINE
27417 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27418 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27420 /* Find the glyph under the mouse pointer. */
27421 if (row->mode_line_p && row->enabled_p)
27423 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27424 end = glyph + row->used[TEXT_AREA];
27426 for (x0 = original_x_pixel;
27427 glyph < end && x0 >= glyph->pixel_width;
27428 ++glyph)
27429 x0 -= glyph->pixel_width;
27431 if (glyph >= end)
27432 glyph = NULL;
27435 else
27437 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27438 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27439 returns them in row/column units! */
27440 string = marginal_area_string (w, area, &x, &y, &charpos,
27441 &object, &dx, &dy, &width, &height);
27444 help = Qnil;
27446 #ifdef HAVE_WINDOW_SYSTEM
27447 if (IMAGEP (object))
27449 Lisp_Object image_map, hotspot;
27450 if ((image_map = Fplist_get (XCDR (object), QCmap),
27451 !NILP (image_map))
27452 && (hotspot = find_hot_spot (image_map, dx, dy),
27453 CONSP (hotspot))
27454 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27456 Lisp_Object plist;
27458 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27459 If so, we could look for mouse-enter, mouse-leave
27460 properties in PLIST (and do something...). */
27461 hotspot = XCDR (hotspot);
27462 if (CONSP (hotspot)
27463 && (plist = XCAR (hotspot), CONSP (plist)))
27465 pointer = Fplist_get (plist, Qpointer);
27466 if (NILP (pointer))
27467 pointer = Qhand;
27468 help = Fplist_get (plist, Qhelp_echo);
27469 if (!NILP (help))
27471 help_echo_string = help;
27472 XSETWINDOW (help_echo_window, w);
27473 help_echo_object = w->buffer;
27474 help_echo_pos = charpos;
27478 if (NILP (pointer))
27479 pointer = Fplist_get (XCDR (object), QCpointer);
27481 #endif /* HAVE_WINDOW_SYSTEM */
27483 if (STRINGP (string))
27484 pos = make_number (charpos);
27486 /* Set the help text and mouse pointer. If the mouse is on a part
27487 of the mode line without any text (e.g. past the right edge of
27488 the mode line text), use the default help text and pointer. */
27489 if (STRINGP (string) || area == ON_MODE_LINE)
27491 /* Arrange to display the help by setting the global variables
27492 help_echo_string, help_echo_object, and help_echo_pos. */
27493 if (NILP (help))
27495 if (STRINGP (string))
27496 help = Fget_text_property (pos, Qhelp_echo, string);
27498 if (!NILP (help))
27500 help_echo_string = help;
27501 XSETWINDOW (help_echo_window, w);
27502 help_echo_object = string;
27503 help_echo_pos = charpos;
27505 else if (area == ON_MODE_LINE)
27507 Lisp_Object default_help
27508 = buffer_local_value_1 (Qmode_line_default_help_echo,
27509 w->buffer);
27511 if (STRINGP (default_help))
27513 help_echo_string = default_help;
27514 XSETWINDOW (help_echo_window, w);
27515 help_echo_object = Qnil;
27516 help_echo_pos = -1;
27521 #ifdef HAVE_WINDOW_SYSTEM
27522 /* Change the mouse pointer according to what is under it. */
27523 if (FRAME_WINDOW_P (f))
27525 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27526 if (STRINGP (string))
27528 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27530 if (NILP (pointer))
27531 pointer = Fget_text_property (pos, Qpointer, string);
27533 /* Change the mouse pointer according to what is under X/Y. */
27534 if (NILP (pointer)
27535 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27537 Lisp_Object map;
27538 map = Fget_text_property (pos, Qlocal_map, string);
27539 if (!KEYMAPP (map))
27540 map = Fget_text_property (pos, Qkeymap, string);
27541 if (!KEYMAPP (map))
27542 cursor = dpyinfo->vertical_scroll_bar_cursor;
27545 else
27546 /* Default mode-line pointer. */
27547 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27549 #endif
27552 /* Change the mouse face according to what is under X/Y. */
27553 if (STRINGP (string))
27555 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27556 if (!NILP (mouse_face)
27557 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27558 && glyph)
27560 Lisp_Object b, e;
27562 struct glyph * tmp_glyph;
27564 int gpos;
27565 int gseq_length;
27566 int total_pixel_width;
27567 ptrdiff_t begpos, endpos, ignore;
27569 int vpos, hpos;
27571 b = Fprevious_single_property_change (make_number (charpos + 1),
27572 Qmouse_face, string, Qnil);
27573 if (NILP (b))
27574 begpos = 0;
27575 else
27576 begpos = XINT (b);
27578 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27579 if (NILP (e))
27580 endpos = SCHARS (string);
27581 else
27582 endpos = XINT (e);
27584 /* Calculate the glyph position GPOS of GLYPH in the
27585 displayed string, relative to the beginning of the
27586 highlighted part of the string.
27588 Note: GPOS is different from CHARPOS. CHARPOS is the
27589 position of GLYPH in the internal string object. A mode
27590 line string format has structures which are converted to
27591 a flattened string by the Emacs Lisp interpreter. The
27592 internal string is an element of those structures. The
27593 displayed string is the flattened string. */
27594 tmp_glyph = row_start_glyph;
27595 while (tmp_glyph < glyph
27596 && (!(EQ (tmp_glyph->object, glyph->object)
27597 && begpos <= tmp_glyph->charpos
27598 && tmp_glyph->charpos < endpos)))
27599 tmp_glyph++;
27600 gpos = glyph - tmp_glyph;
27602 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27603 the highlighted part of the displayed string to which
27604 GLYPH belongs. Note: GSEQ_LENGTH is different from
27605 SCHARS (STRING), because the latter returns the length of
27606 the internal string. */
27607 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27608 tmp_glyph > glyph
27609 && (!(EQ (tmp_glyph->object, glyph->object)
27610 && begpos <= tmp_glyph->charpos
27611 && tmp_glyph->charpos < endpos));
27612 tmp_glyph--)
27614 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27616 /* Calculate the total pixel width of all the glyphs between
27617 the beginning of the highlighted area and GLYPH. */
27618 total_pixel_width = 0;
27619 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27620 total_pixel_width += tmp_glyph->pixel_width;
27622 /* Pre calculation of re-rendering position. Note: X is in
27623 column units here, after the call to mode_line_string or
27624 marginal_area_string. */
27625 hpos = x - gpos;
27626 vpos = (area == ON_MODE_LINE
27627 ? (w->current_matrix)->nrows - 1
27628 : 0);
27630 /* If GLYPH's position is included in the region that is
27631 already drawn in mouse face, we have nothing to do. */
27632 if ( EQ (window, hlinfo->mouse_face_window)
27633 && (!row->reversed_p
27634 ? (hlinfo->mouse_face_beg_col <= hpos
27635 && hpos < hlinfo->mouse_face_end_col)
27636 /* In R2L rows we swap BEG and END, see below. */
27637 : (hlinfo->mouse_face_end_col <= hpos
27638 && hpos < hlinfo->mouse_face_beg_col))
27639 && hlinfo->mouse_face_beg_row == vpos )
27640 return;
27642 if (clear_mouse_face (hlinfo))
27643 cursor = No_Cursor;
27645 if (!row->reversed_p)
27647 hlinfo->mouse_face_beg_col = hpos;
27648 hlinfo->mouse_face_beg_x = original_x_pixel
27649 - (total_pixel_width + dx);
27650 hlinfo->mouse_face_end_col = hpos + gseq_length;
27651 hlinfo->mouse_face_end_x = 0;
27653 else
27655 /* In R2L rows, show_mouse_face expects BEG and END
27656 coordinates to be swapped. */
27657 hlinfo->mouse_face_end_col = hpos;
27658 hlinfo->mouse_face_end_x = original_x_pixel
27659 - (total_pixel_width + dx);
27660 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27661 hlinfo->mouse_face_beg_x = 0;
27664 hlinfo->mouse_face_beg_row = vpos;
27665 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27666 hlinfo->mouse_face_beg_y = 0;
27667 hlinfo->mouse_face_end_y = 0;
27668 hlinfo->mouse_face_past_end = 0;
27669 hlinfo->mouse_face_window = window;
27671 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27672 charpos,
27673 0, 0, 0,
27674 &ignore,
27675 glyph->face_id,
27677 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27679 if (NILP (pointer))
27680 pointer = Qhand;
27682 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27683 clear_mouse_face (hlinfo);
27685 #ifdef HAVE_WINDOW_SYSTEM
27686 if (FRAME_WINDOW_P (f))
27687 define_frame_cursor1 (f, cursor, pointer);
27688 #endif
27692 /* EXPORT:
27693 Take proper action when the mouse has moved to position X, Y on
27694 frame F as regards highlighting characters that have mouse-face
27695 properties. Also de-highlighting chars where the mouse was before.
27696 X and Y can be negative or out of range. */
27698 void
27699 note_mouse_highlight (struct frame *f, int x, int y)
27701 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27702 enum window_part part = ON_NOTHING;
27703 Lisp_Object window;
27704 struct window *w;
27705 Cursor cursor = No_Cursor;
27706 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27707 struct buffer *b;
27709 /* When a menu is active, don't highlight because this looks odd. */
27710 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27711 if (popup_activated ())
27712 return;
27713 #endif
27715 if (NILP (Vmouse_highlight)
27716 || !f->glyphs_initialized_p
27717 || f->pointer_invisible)
27718 return;
27720 hlinfo->mouse_face_mouse_x = x;
27721 hlinfo->mouse_face_mouse_y = y;
27722 hlinfo->mouse_face_mouse_frame = f;
27724 if (hlinfo->mouse_face_defer)
27725 return;
27727 if (gc_in_progress)
27729 hlinfo->mouse_face_deferred_gc = 1;
27730 return;
27733 /* Which window is that in? */
27734 window = window_from_coordinates (f, x, y, &part, 1);
27736 /* If displaying active text in another window, clear that. */
27737 if (! EQ (window, hlinfo->mouse_face_window)
27738 /* Also clear if we move out of text area in same window. */
27739 || (!NILP (hlinfo->mouse_face_window)
27740 && !NILP (window)
27741 && part != ON_TEXT
27742 && part != ON_MODE_LINE
27743 && part != ON_HEADER_LINE))
27744 clear_mouse_face (hlinfo);
27746 /* Not on a window -> return. */
27747 if (!WINDOWP (window))
27748 return;
27750 /* Reset help_echo_string. It will get recomputed below. */
27751 help_echo_string = Qnil;
27753 /* Convert to window-relative pixel coordinates. */
27754 w = XWINDOW (window);
27755 frame_to_window_pixel_xy (w, &x, &y);
27757 #ifdef HAVE_WINDOW_SYSTEM
27758 /* Handle tool-bar window differently since it doesn't display a
27759 buffer. */
27760 if (EQ (window, f->tool_bar_window))
27762 note_tool_bar_highlight (f, x, y);
27763 return;
27765 #endif
27767 /* Mouse is on the mode, header line or margin? */
27768 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27769 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27771 note_mode_line_or_margin_highlight (window, x, y, part);
27772 return;
27775 #ifdef HAVE_WINDOW_SYSTEM
27776 if (part == ON_VERTICAL_BORDER)
27778 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27779 help_echo_string = build_string ("drag-mouse-1: resize");
27781 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27782 || part == ON_SCROLL_BAR)
27783 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27784 else
27785 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27786 #endif
27788 /* Are we in a window whose display is up to date?
27789 And verify the buffer's text has not changed. */
27790 b = XBUFFER (w->buffer);
27791 if (part == ON_TEXT
27792 && EQ (w->window_end_valid, w->buffer)
27793 && w->last_modified == BUF_MODIFF (b)
27794 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27796 int hpos, vpos, dx, dy, area = LAST_AREA;
27797 ptrdiff_t pos;
27798 struct glyph *glyph;
27799 Lisp_Object object;
27800 Lisp_Object mouse_face = Qnil, position;
27801 Lisp_Object *overlay_vec = NULL;
27802 ptrdiff_t i, noverlays;
27803 struct buffer *obuf;
27804 ptrdiff_t obegv, ozv;
27805 int same_region;
27807 /* Find the glyph under X/Y. */
27808 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27810 #ifdef HAVE_WINDOW_SYSTEM
27811 /* Look for :pointer property on image. */
27812 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27814 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27815 if (img != NULL && IMAGEP (img->spec))
27817 Lisp_Object image_map, hotspot;
27818 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27819 !NILP (image_map))
27820 && (hotspot = find_hot_spot (image_map,
27821 glyph->slice.img.x + dx,
27822 glyph->slice.img.y + dy),
27823 CONSP (hotspot))
27824 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27826 Lisp_Object plist;
27828 /* Could check XCAR (hotspot) to see if we enter/leave
27829 this hot-spot.
27830 If so, we could look for mouse-enter, mouse-leave
27831 properties in PLIST (and do something...). */
27832 hotspot = XCDR (hotspot);
27833 if (CONSP (hotspot)
27834 && (plist = XCAR (hotspot), CONSP (plist)))
27836 pointer = Fplist_get (plist, Qpointer);
27837 if (NILP (pointer))
27838 pointer = Qhand;
27839 help_echo_string = Fplist_get (plist, Qhelp_echo);
27840 if (!NILP (help_echo_string))
27842 help_echo_window = window;
27843 help_echo_object = glyph->object;
27844 help_echo_pos = glyph->charpos;
27848 if (NILP (pointer))
27849 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27852 #endif /* HAVE_WINDOW_SYSTEM */
27854 /* Clear mouse face if X/Y not over text. */
27855 if (glyph == NULL
27856 || area != TEXT_AREA
27857 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27858 /* Glyph's OBJECT is an integer for glyphs inserted by the
27859 display engine for its internal purposes, like truncation
27860 and continuation glyphs and blanks beyond the end of
27861 line's text on text terminals. If we are over such a
27862 glyph, we are not over any text. */
27863 || INTEGERP (glyph->object)
27864 /* R2L rows have a stretch glyph at their front, which
27865 stands for no text, whereas L2R rows have no glyphs at
27866 all beyond the end of text. Treat such stretch glyphs
27867 like we do with NULL glyphs in L2R rows. */
27868 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27869 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27870 && glyph->type == STRETCH_GLYPH
27871 && glyph->avoid_cursor_p))
27873 if (clear_mouse_face (hlinfo))
27874 cursor = No_Cursor;
27875 #ifdef HAVE_WINDOW_SYSTEM
27876 if (FRAME_WINDOW_P (f) && NILP (pointer))
27878 if (area != TEXT_AREA)
27879 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27880 else
27881 pointer = Vvoid_text_area_pointer;
27883 #endif
27884 goto set_cursor;
27887 pos = glyph->charpos;
27888 object = glyph->object;
27889 if (!STRINGP (object) && !BUFFERP (object))
27890 goto set_cursor;
27892 /* If we get an out-of-range value, return now; avoid an error. */
27893 if (BUFFERP (object) && pos > BUF_Z (b))
27894 goto set_cursor;
27896 /* Make the window's buffer temporarily current for
27897 overlays_at and compute_char_face. */
27898 obuf = current_buffer;
27899 current_buffer = b;
27900 obegv = BEGV;
27901 ozv = ZV;
27902 BEGV = BEG;
27903 ZV = Z;
27905 /* Is this char mouse-active or does it have help-echo? */
27906 position = make_number (pos);
27908 if (BUFFERP (object))
27910 /* Put all the overlays we want in a vector in overlay_vec. */
27911 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27912 /* Sort overlays into increasing priority order. */
27913 noverlays = sort_overlays (overlay_vec, noverlays, w);
27915 else
27916 noverlays = 0;
27918 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27920 if (same_region)
27921 cursor = No_Cursor;
27923 /* Check mouse-face highlighting. */
27924 if (! same_region
27925 /* If there exists an overlay with mouse-face overlapping
27926 the one we are currently highlighting, we have to
27927 check if we enter the overlapping overlay, and then
27928 highlight only that. */
27929 || (OVERLAYP (hlinfo->mouse_face_overlay)
27930 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27932 /* Find the highest priority overlay with a mouse-face. */
27933 Lisp_Object overlay = Qnil;
27934 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27936 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27937 if (!NILP (mouse_face))
27938 overlay = overlay_vec[i];
27941 /* If we're highlighting the same overlay as before, there's
27942 no need to do that again. */
27943 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27944 goto check_help_echo;
27945 hlinfo->mouse_face_overlay = overlay;
27947 /* Clear the display of the old active region, if any. */
27948 if (clear_mouse_face (hlinfo))
27949 cursor = No_Cursor;
27951 /* If no overlay applies, get a text property. */
27952 if (NILP (overlay))
27953 mouse_face = Fget_text_property (position, Qmouse_face, object);
27955 /* Next, compute the bounds of the mouse highlighting and
27956 display it. */
27957 if (!NILP (mouse_face) && STRINGP (object))
27959 /* The mouse-highlighting comes from a display string
27960 with a mouse-face. */
27961 Lisp_Object s, e;
27962 ptrdiff_t ignore;
27964 s = Fprevious_single_property_change
27965 (make_number (pos + 1), Qmouse_face, object, Qnil);
27966 e = Fnext_single_property_change
27967 (position, Qmouse_face, object, Qnil);
27968 if (NILP (s))
27969 s = make_number (0);
27970 if (NILP (e))
27971 e = make_number (SCHARS (object) - 1);
27972 mouse_face_from_string_pos (w, hlinfo, object,
27973 XINT (s), XINT (e));
27974 hlinfo->mouse_face_past_end = 0;
27975 hlinfo->mouse_face_window = window;
27976 hlinfo->mouse_face_face_id
27977 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27978 glyph->face_id, 1);
27979 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27980 cursor = No_Cursor;
27982 else
27984 /* The mouse-highlighting, if any, comes from an overlay
27985 or text property in the buffer. */
27986 Lisp_Object buffer IF_LINT (= Qnil);
27987 Lisp_Object disp_string IF_LINT (= Qnil);
27989 if (STRINGP (object))
27991 /* If we are on a display string with no mouse-face,
27992 check if the text under it has one. */
27993 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27994 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27995 pos = string_buffer_position (object, start);
27996 if (pos > 0)
27998 mouse_face = get_char_property_and_overlay
27999 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28000 buffer = w->buffer;
28001 disp_string = object;
28004 else
28006 buffer = object;
28007 disp_string = Qnil;
28010 if (!NILP (mouse_face))
28012 Lisp_Object before, after;
28013 Lisp_Object before_string, after_string;
28014 /* To correctly find the limits of mouse highlight
28015 in a bidi-reordered buffer, we must not use the
28016 optimization of limiting the search in
28017 previous-single-property-change and
28018 next-single-property-change, because
28019 rows_from_pos_range needs the real start and end
28020 positions to DTRT in this case. That's because
28021 the first row visible in a window does not
28022 necessarily display the character whose position
28023 is the smallest. */
28024 Lisp_Object lim1 =
28025 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28026 ? Fmarker_position (w->start)
28027 : Qnil;
28028 Lisp_Object lim2 =
28029 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28030 ? make_number (BUF_Z (XBUFFER (buffer))
28031 - XFASTINT (w->window_end_pos))
28032 : Qnil;
28034 if (NILP (overlay))
28036 /* Handle the text property case. */
28037 before = Fprevious_single_property_change
28038 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28039 after = Fnext_single_property_change
28040 (make_number (pos), Qmouse_face, buffer, lim2);
28041 before_string = after_string = Qnil;
28043 else
28045 /* Handle the overlay case. */
28046 before = Foverlay_start (overlay);
28047 after = Foverlay_end (overlay);
28048 before_string = Foverlay_get (overlay, Qbefore_string);
28049 after_string = Foverlay_get (overlay, Qafter_string);
28051 if (!STRINGP (before_string)) before_string = Qnil;
28052 if (!STRINGP (after_string)) after_string = Qnil;
28055 mouse_face_from_buffer_pos (window, hlinfo, pos,
28056 NILP (before)
28058 : XFASTINT (before),
28059 NILP (after)
28060 ? BUF_Z (XBUFFER (buffer))
28061 : XFASTINT (after),
28062 before_string, after_string,
28063 disp_string);
28064 cursor = No_Cursor;
28069 check_help_echo:
28071 /* Look for a `help-echo' property. */
28072 if (NILP (help_echo_string)) {
28073 Lisp_Object help, overlay;
28075 /* Check overlays first. */
28076 help = overlay = Qnil;
28077 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28079 overlay = overlay_vec[i];
28080 help = Foverlay_get (overlay, Qhelp_echo);
28083 if (!NILP (help))
28085 help_echo_string = help;
28086 help_echo_window = window;
28087 help_echo_object = overlay;
28088 help_echo_pos = pos;
28090 else
28092 Lisp_Object obj = glyph->object;
28093 ptrdiff_t charpos = glyph->charpos;
28095 /* Try text properties. */
28096 if (STRINGP (obj)
28097 && charpos >= 0
28098 && charpos < SCHARS (obj))
28100 help = Fget_text_property (make_number (charpos),
28101 Qhelp_echo, obj);
28102 if (NILP (help))
28104 /* If the string itself doesn't specify a help-echo,
28105 see if the buffer text ``under'' it does. */
28106 struct glyph_row *r
28107 = MATRIX_ROW (w->current_matrix, vpos);
28108 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28109 ptrdiff_t p = string_buffer_position (obj, start);
28110 if (p > 0)
28112 help = Fget_char_property (make_number (p),
28113 Qhelp_echo, w->buffer);
28114 if (!NILP (help))
28116 charpos = p;
28117 obj = w->buffer;
28122 else if (BUFFERP (obj)
28123 && charpos >= BEGV
28124 && charpos < ZV)
28125 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28126 obj);
28128 if (!NILP (help))
28130 help_echo_string = help;
28131 help_echo_window = window;
28132 help_echo_object = obj;
28133 help_echo_pos = charpos;
28138 #ifdef HAVE_WINDOW_SYSTEM
28139 /* Look for a `pointer' property. */
28140 if (FRAME_WINDOW_P (f) && NILP (pointer))
28142 /* Check overlays first. */
28143 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28144 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28146 if (NILP (pointer))
28148 Lisp_Object obj = glyph->object;
28149 ptrdiff_t charpos = glyph->charpos;
28151 /* Try text properties. */
28152 if (STRINGP (obj)
28153 && charpos >= 0
28154 && charpos < SCHARS (obj))
28156 pointer = Fget_text_property (make_number (charpos),
28157 Qpointer, obj);
28158 if (NILP (pointer))
28160 /* If the string itself doesn't specify a pointer,
28161 see if the buffer text ``under'' it does. */
28162 struct glyph_row *r
28163 = MATRIX_ROW (w->current_matrix, vpos);
28164 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28165 ptrdiff_t p = string_buffer_position (obj, start);
28166 if (p > 0)
28167 pointer = Fget_char_property (make_number (p),
28168 Qpointer, w->buffer);
28171 else if (BUFFERP (obj)
28172 && charpos >= BEGV
28173 && charpos < ZV)
28174 pointer = Fget_text_property (make_number (charpos),
28175 Qpointer, obj);
28178 #endif /* HAVE_WINDOW_SYSTEM */
28180 BEGV = obegv;
28181 ZV = ozv;
28182 current_buffer = obuf;
28185 set_cursor:
28187 #ifdef HAVE_WINDOW_SYSTEM
28188 if (FRAME_WINDOW_P (f))
28189 define_frame_cursor1 (f, cursor, pointer);
28190 #else
28191 /* This is here to prevent a compiler error, about "label at end of
28192 compound statement". */
28193 return;
28194 #endif
28198 /* EXPORT for RIF:
28199 Clear any mouse-face on window W. This function is part of the
28200 redisplay interface, and is called from try_window_id and similar
28201 functions to ensure the mouse-highlight is off. */
28203 void
28204 x_clear_window_mouse_face (struct window *w)
28206 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28207 Lisp_Object window;
28209 block_input ();
28210 XSETWINDOW (window, w);
28211 if (EQ (window, hlinfo->mouse_face_window))
28212 clear_mouse_face (hlinfo);
28213 unblock_input ();
28217 /* EXPORT:
28218 Just discard the mouse face information for frame F, if any.
28219 This is used when the size of F is changed. */
28221 void
28222 cancel_mouse_face (struct frame *f)
28224 Lisp_Object window;
28225 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28227 window = hlinfo->mouse_face_window;
28228 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28230 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28231 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28232 hlinfo->mouse_face_window = Qnil;
28238 /***********************************************************************
28239 Exposure Events
28240 ***********************************************************************/
28242 #ifdef HAVE_WINDOW_SYSTEM
28244 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28245 which intersects rectangle R. R is in window-relative coordinates. */
28247 static void
28248 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28249 enum glyph_row_area area)
28251 struct glyph *first = row->glyphs[area];
28252 struct glyph *end = row->glyphs[area] + row->used[area];
28253 struct glyph *last;
28254 int first_x, start_x, x;
28256 if (area == TEXT_AREA && row->fill_line_p)
28257 /* If row extends face to end of line write the whole line. */
28258 draw_glyphs (w, 0, row, area,
28259 0, row->used[area],
28260 DRAW_NORMAL_TEXT, 0);
28261 else
28263 /* Set START_X to the window-relative start position for drawing glyphs of
28264 AREA. The first glyph of the text area can be partially visible.
28265 The first glyphs of other areas cannot. */
28266 start_x = window_box_left_offset (w, area);
28267 x = start_x;
28268 if (area == TEXT_AREA)
28269 x += row->x;
28271 /* Find the first glyph that must be redrawn. */
28272 while (first < end
28273 && x + first->pixel_width < r->x)
28275 x += first->pixel_width;
28276 ++first;
28279 /* Find the last one. */
28280 last = first;
28281 first_x = x;
28282 while (last < end
28283 && x < r->x + r->width)
28285 x += last->pixel_width;
28286 ++last;
28289 /* Repaint. */
28290 if (last > first)
28291 draw_glyphs (w, first_x - start_x, row, area,
28292 first - row->glyphs[area], last - row->glyphs[area],
28293 DRAW_NORMAL_TEXT, 0);
28298 /* Redraw the parts of the glyph row ROW on window W intersecting
28299 rectangle R. R is in window-relative coordinates. Value is
28300 non-zero if mouse-face was overwritten. */
28302 static int
28303 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28305 eassert (row->enabled_p);
28307 if (row->mode_line_p || w->pseudo_window_p)
28308 draw_glyphs (w, 0, row, TEXT_AREA,
28309 0, row->used[TEXT_AREA],
28310 DRAW_NORMAL_TEXT, 0);
28311 else
28313 if (row->used[LEFT_MARGIN_AREA])
28314 expose_area (w, row, r, LEFT_MARGIN_AREA);
28315 if (row->used[TEXT_AREA])
28316 expose_area (w, row, r, TEXT_AREA);
28317 if (row->used[RIGHT_MARGIN_AREA])
28318 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28319 draw_row_fringe_bitmaps (w, row);
28322 return row->mouse_face_p;
28326 /* Redraw those parts of glyphs rows during expose event handling that
28327 overlap other rows. Redrawing of an exposed line writes over parts
28328 of lines overlapping that exposed line; this function fixes that.
28330 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28331 row in W's current matrix that is exposed and overlaps other rows.
28332 LAST_OVERLAPPING_ROW is the last such row. */
28334 static void
28335 expose_overlaps (struct window *w,
28336 struct glyph_row *first_overlapping_row,
28337 struct glyph_row *last_overlapping_row,
28338 XRectangle *r)
28340 struct glyph_row *row;
28342 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28343 if (row->overlapping_p)
28345 eassert (row->enabled_p && !row->mode_line_p);
28347 row->clip = r;
28348 if (row->used[LEFT_MARGIN_AREA])
28349 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28351 if (row->used[TEXT_AREA])
28352 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28354 if (row->used[RIGHT_MARGIN_AREA])
28355 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28356 row->clip = NULL;
28361 /* Return non-zero if W's cursor intersects rectangle R. */
28363 static int
28364 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28366 XRectangle cr, result;
28367 struct glyph *cursor_glyph;
28368 struct glyph_row *row;
28370 if (w->phys_cursor.vpos >= 0
28371 && w->phys_cursor.vpos < w->current_matrix->nrows
28372 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28373 row->enabled_p)
28374 && row->cursor_in_fringe_p)
28376 /* Cursor is in the fringe. */
28377 cr.x = window_box_right_offset (w,
28378 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28379 ? RIGHT_MARGIN_AREA
28380 : TEXT_AREA));
28381 cr.y = row->y;
28382 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28383 cr.height = row->height;
28384 return x_intersect_rectangles (&cr, r, &result);
28387 cursor_glyph = get_phys_cursor_glyph (w);
28388 if (cursor_glyph)
28390 /* r is relative to W's box, but w->phys_cursor.x is relative
28391 to left edge of W's TEXT area. Adjust it. */
28392 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28393 cr.y = w->phys_cursor.y;
28394 cr.width = cursor_glyph->pixel_width;
28395 cr.height = w->phys_cursor_height;
28396 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28397 I assume the effect is the same -- and this is portable. */
28398 return x_intersect_rectangles (&cr, r, &result);
28400 /* If we don't understand the format, pretend we're not in the hot-spot. */
28401 return 0;
28405 /* EXPORT:
28406 Draw a vertical window border to the right of window W if W doesn't
28407 have vertical scroll bars. */
28409 void
28410 x_draw_vertical_border (struct window *w)
28412 struct frame *f = XFRAME (WINDOW_FRAME (w));
28414 /* We could do better, if we knew what type of scroll-bar the adjacent
28415 windows (on either side) have... But we don't :-(
28416 However, I think this works ok. ++KFS 2003-04-25 */
28418 /* Redraw borders between horizontally adjacent windows. Don't
28419 do it for frames with vertical scroll bars because either the
28420 right scroll bar of a window, or the left scroll bar of its
28421 neighbor will suffice as a border. */
28422 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28423 return;
28425 /* Note: It is necessary to redraw both the left and the right
28426 borders, for when only this single window W is being
28427 redisplayed. */
28428 if (!WINDOW_RIGHTMOST_P (w)
28429 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28431 int x0, x1, y0, y1;
28433 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28434 y1 -= 1;
28436 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28437 x1 -= 1;
28439 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28441 if (!WINDOW_LEFTMOST_P (w)
28442 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28444 int x0, x1, y0, y1;
28446 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28447 y1 -= 1;
28449 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28450 x0 -= 1;
28452 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28457 /* Redraw the part of window W intersection rectangle FR. Pixel
28458 coordinates in FR are frame-relative. Call this function with
28459 input blocked. Value is non-zero if the exposure overwrites
28460 mouse-face. */
28462 static int
28463 expose_window (struct window *w, XRectangle *fr)
28465 struct frame *f = XFRAME (w->frame);
28466 XRectangle wr, r;
28467 int mouse_face_overwritten_p = 0;
28469 /* If window is not yet fully initialized, do nothing. This can
28470 happen when toolkit scroll bars are used and a window is split.
28471 Reconfiguring the scroll bar will generate an expose for a newly
28472 created window. */
28473 if (w->current_matrix == NULL)
28474 return 0;
28476 /* When we're currently updating the window, display and current
28477 matrix usually don't agree. Arrange for a thorough display
28478 later. */
28479 if (w == updated_window)
28481 SET_FRAME_GARBAGED (f);
28482 return 0;
28485 /* Frame-relative pixel rectangle of W. */
28486 wr.x = WINDOW_LEFT_EDGE_X (w);
28487 wr.y = WINDOW_TOP_EDGE_Y (w);
28488 wr.width = WINDOW_TOTAL_WIDTH (w);
28489 wr.height = WINDOW_TOTAL_HEIGHT (w);
28491 if (x_intersect_rectangles (fr, &wr, &r))
28493 int yb = window_text_bottom_y (w);
28494 struct glyph_row *row;
28495 int cursor_cleared_p, phys_cursor_on_p;
28496 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28498 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28499 r.x, r.y, r.width, r.height));
28501 /* Convert to window coordinates. */
28502 r.x -= WINDOW_LEFT_EDGE_X (w);
28503 r.y -= WINDOW_TOP_EDGE_Y (w);
28505 /* Turn off the cursor. */
28506 if (!w->pseudo_window_p
28507 && phys_cursor_in_rect_p (w, &r))
28509 x_clear_cursor (w);
28510 cursor_cleared_p = 1;
28512 else
28513 cursor_cleared_p = 0;
28515 /* If the row containing the cursor extends face to end of line,
28516 then expose_area might overwrite the cursor outside the
28517 rectangle and thus notice_overwritten_cursor might clear
28518 w->phys_cursor_on_p. We remember the original value and
28519 check later if it is changed. */
28520 phys_cursor_on_p = w->phys_cursor_on_p;
28522 /* Update lines intersecting rectangle R. */
28523 first_overlapping_row = last_overlapping_row = NULL;
28524 for (row = w->current_matrix->rows;
28525 row->enabled_p;
28526 ++row)
28528 int y0 = row->y;
28529 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28531 if ((y0 >= r.y && y0 < r.y + r.height)
28532 || (y1 > r.y && y1 < r.y + r.height)
28533 || (r.y >= y0 && r.y < y1)
28534 || (r.y + r.height > y0 && r.y + r.height < y1))
28536 /* A header line may be overlapping, but there is no need
28537 to fix overlapping areas for them. KFS 2005-02-12 */
28538 if (row->overlapping_p && !row->mode_line_p)
28540 if (first_overlapping_row == NULL)
28541 first_overlapping_row = row;
28542 last_overlapping_row = row;
28545 row->clip = fr;
28546 if (expose_line (w, row, &r))
28547 mouse_face_overwritten_p = 1;
28548 row->clip = NULL;
28550 else if (row->overlapping_p)
28552 /* We must redraw a row overlapping the exposed area. */
28553 if (y0 < r.y
28554 ? y0 + row->phys_height > r.y
28555 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28557 if (first_overlapping_row == NULL)
28558 first_overlapping_row = row;
28559 last_overlapping_row = row;
28563 if (y1 >= yb)
28564 break;
28567 /* Display the mode line if there is one. */
28568 if (WINDOW_WANTS_MODELINE_P (w)
28569 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28570 row->enabled_p)
28571 && row->y < r.y + r.height)
28573 if (expose_line (w, row, &r))
28574 mouse_face_overwritten_p = 1;
28577 if (!w->pseudo_window_p)
28579 /* Fix the display of overlapping rows. */
28580 if (first_overlapping_row)
28581 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28582 fr);
28584 /* Draw border between windows. */
28585 x_draw_vertical_border (w);
28587 /* Turn the cursor on again. */
28588 if (cursor_cleared_p
28589 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28590 update_window_cursor (w, 1);
28594 return mouse_face_overwritten_p;
28599 /* Redraw (parts) of all windows in the window tree rooted at W that
28600 intersect R. R contains frame pixel coordinates. Value is
28601 non-zero if the exposure overwrites mouse-face. */
28603 static int
28604 expose_window_tree (struct window *w, XRectangle *r)
28606 struct frame *f = XFRAME (w->frame);
28607 int mouse_face_overwritten_p = 0;
28609 while (w && !FRAME_GARBAGED_P (f))
28611 if (!NILP (w->hchild))
28612 mouse_face_overwritten_p
28613 |= expose_window_tree (XWINDOW (w->hchild), r);
28614 else if (!NILP (w->vchild))
28615 mouse_face_overwritten_p
28616 |= expose_window_tree (XWINDOW (w->vchild), r);
28617 else
28618 mouse_face_overwritten_p |= expose_window (w, r);
28620 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28623 return mouse_face_overwritten_p;
28627 /* EXPORT:
28628 Redisplay an exposed area of frame F. X and Y are the upper-left
28629 corner of the exposed rectangle. W and H are width and height of
28630 the exposed area. All are pixel values. W or H zero means redraw
28631 the entire frame. */
28633 void
28634 expose_frame (struct frame *f, int x, int y, int w, int h)
28636 XRectangle r;
28637 int mouse_face_overwritten_p = 0;
28639 TRACE ((stderr, "expose_frame "));
28641 /* No need to redraw if frame will be redrawn soon. */
28642 if (FRAME_GARBAGED_P (f))
28644 TRACE ((stderr, " garbaged\n"));
28645 return;
28648 /* If basic faces haven't been realized yet, there is no point in
28649 trying to redraw anything. This can happen when we get an expose
28650 event while Emacs is starting, e.g. by moving another window. */
28651 if (FRAME_FACE_CACHE (f) == NULL
28652 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28654 TRACE ((stderr, " no faces\n"));
28655 return;
28658 if (w == 0 || h == 0)
28660 r.x = r.y = 0;
28661 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28662 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28664 else
28666 r.x = x;
28667 r.y = y;
28668 r.width = w;
28669 r.height = h;
28672 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28673 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28675 if (WINDOWP (f->tool_bar_window))
28676 mouse_face_overwritten_p
28677 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28679 #ifdef HAVE_X_WINDOWS
28680 #ifndef MSDOS
28681 #ifndef USE_X_TOOLKIT
28682 if (WINDOWP (f->menu_bar_window))
28683 mouse_face_overwritten_p
28684 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28685 #endif /* not USE_X_TOOLKIT */
28686 #endif
28687 #endif
28689 /* Some window managers support a focus-follows-mouse style with
28690 delayed raising of frames. Imagine a partially obscured frame,
28691 and moving the mouse into partially obscured mouse-face on that
28692 frame. The visible part of the mouse-face will be highlighted,
28693 then the WM raises the obscured frame. With at least one WM, KDE
28694 2.1, Emacs is not getting any event for the raising of the frame
28695 (even tried with SubstructureRedirectMask), only Expose events.
28696 These expose events will draw text normally, i.e. not
28697 highlighted. Which means we must redo the highlight here.
28698 Subsume it under ``we love X''. --gerd 2001-08-15 */
28699 /* Included in Windows version because Windows most likely does not
28700 do the right thing if any third party tool offers
28701 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28702 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28704 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28705 if (f == hlinfo->mouse_face_mouse_frame)
28707 int mouse_x = hlinfo->mouse_face_mouse_x;
28708 int mouse_y = hlinfo->mouse_face_mouse_y;
28709 clear_mouse_face (hlinfo);
28710 note_mouse_highlight (f, mouse_x, mouse_y);
28716 /* EXPORT:
28717 Determine the intersection of two rectangles R1 and R2. Return
28718 the intersection in *RESULT. Value is non-zero if RESULT is not
28719 empty. */
28722 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28724 XRectangle *left, *right;
28725 XRectangle *upper, *lower;
28726 int intersection_p = 0;
28728 /* Rearrange so that R1 is the left-most rectangle. */
28729 if (r1->x < r2->x)
28730 left = r1, right = r2;
28731 else
28732 left = r2, right = r1;
28734 /* X0 of the intersection is right.x0, if this is inside R1,
28735 otherwise there is no intersection. */
28736 if (right->x <= left->x + left->width)
28738 result->x = right->x;
28740 /* The right end of the intersection is the minimum of
28741 the right ends of left and right. */
28742 result->width = (min (left->x + left->width, right->x + right->width)
28743 - result->x);
28745 /* Same game for Y. */
28746 if (r1->y < r2->y)
28747 upper = r1, lower = r2;
28748 else
28749 upper = r2, lower = r1;
28751 /* The upper end of the intersection is lower.y0, if this is inside
28752 of upper. Otherwise, there is no intersection. */
28753 if (lower->y <= upper->y + upper->height)
28755 result->y = lower->y;
28757 /* The lower end of the intersection is the minimum of the lower
28758 ends of upper and lower. */
28759 result->height = (min (lower->y + lower->height,
28760 upper->y + upper->height)
28761 - result->y);
28762 intersection_p = 1;
28766 return intersection_p;
28769 #endif /* HAVE_WINDOW_SYSTEM */
28772 /***********************************************************************
28773 Initialization
28774 ***********************************************************************/
28776 void
28777 syms_of_xdisp (void)
28779 Vwith_echo_area_save_vector = Qnil;
28780 staticpro (&Vwith_echo_area_save_vector);
28782 Vmessage_stack = Qnil;
28783 staticpro (&Vmessage_stack);
28785 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28786 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28788 message_dolog_marker1 = Fmake_marker ();
28789 staticpro (&message_dolog_marker1);
28790 message_dolog_marker2 = Fmake_marker ();
28791 staticpro (&message_dolog_marker2);
28792 message_dolog_marker3 = Fmake_marker ();
28793 staticpro (&message_dolog_marker3);
28795 #ifdef GLYPH_DEBUG
28796 defsubr (&Sdump_frame_glyph_matrix);
28797 defsubr (&Sdump_glyph_matrix);
28798 defsubr (&Sdump_glyph_row);
28799 defsubr (&Sdump_tool_bar_row);
28800 defsubr (&Strace_redisplay);
28801 defsubr (&Strace_to_stderr);
28802 #endif
28803 #ifdef HAVE_WINDOW_SYSTEM
28804 defsubr (&Stool_bar_lines_needed);
28805 defsubr (&Slookup_image_map);
28806 #endif
28807 defsubr (&Sformat_mode_line);
28808 defsubr (&Sinvisible_p);
28809 defsubr (&Scurrent_bidi_paragraph_direction);
28811 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28812 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28813 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28814 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28815 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28816 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28817 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28818 DEFSYM (Qeval, "eval");
28819 DEFSYM (QCdata, ":data");
28820 DEFSYM (Qdisplay, "display");
28821 DEFSYM (Qspace_width, "space-width");
28822 DEFSYM (Qraise, "raise");
28823 DEFSYM (Qslice, "slice");
28824 DEFSYM (Qspace, "space");
28825 DEFSYM (Qmargin, "margin");
28826 DEFSYM (Qpointer, "pointer");
28827 DEFSYM (Qleft_margin, "left-margin");
28828 DEFSYM (Qright_margin, "right-margin");
28829 DEFSYM (Qcenter, "center");
28830 DEFSYM (Qline_height, "line-height");
28831 DEFSYM (QCalign_to, ":align-to");
28832 DEFSYM (QCrelative_width, ":relative-width");
28833 DEFSYM (QCrelative_height, ":relative-height");
28834 DEFSYM (QCeval, ":eval");
28835 DEFSYM (QCpropertize, ":propertize");
28836 DEFSYM (QCfile, ":file");
28837 DEFSYM (Qfontified, "fontified");
28838 DEFSYM (Qfontification_functions, "fontification-functions");
28839 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28840 DEFSYM (Qescape_glyph, "escape-glyph");
28841 DEFSYM (Qnobreak_space, "nobreak-space");
28842 DEFSYM (Qimage, "image");
28843 DEFSYM (Qtext, "text");
28844 DEFSYM (Qboth, "both");
28845 DEFSYM (Qboth_horiz, "both-horiz");
28846 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28847 DEFSYM (QCmap, ":map");
28848 DEFSYM (QCpointer, ":pointer");
28849 DEFSYM (Qrect, "rect");
28850 DEFSYM (Qcircle, "circle");
28851 DEFSYM (Qpoly, "poly");
28852 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28853 DEFSYM (Qgrow_only, "grow-only");
28854 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28855 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28856 DEFSYM (Qposition, "position");
28857 DEFSYM (Qbuffer_position, "buffer-position");
28858 DEFSYM (Qobject, "object");
28859 DEFSYM (Qbar, "bar");
28860 DEFSYM (Qhbar, "hbar");
28861 DEFSYM (Qbox, "box");
28862 DEFSYM (Qhollow, "hollow");
28863 DEFSYM (Qhand, "hand");
28864 DEFSYM (Qarrow, "arrow");
28865 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28867 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28868 Fcons (intern_c_string ("void-variable"), Qnil)),
28869 Qnil);
28870 staticpro (&list_of_error);
28872 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28873 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28874 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28875 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28877 echo_buffer[0] = echo_buffer[1] = Qnil;
28878 staticpro (&echo_buffer[0]);
28879 staticpro (&echo_buffer[1]);
28881 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28882 staticpro (&echo_area_buffer[0]);
28883 staticpro (&echo_area_buffer[1]);
28885 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28886 staticpro (&Vmessages_buffer_name);
28888 mode_line_proptrans_alist = Qnil;
28889 staticpro (&mode_line_proptrans_alist);
28890 mode_line_string_list = Qnil;
28891 staticpro (&mode_line_string_list);
28892 mode_line_string_face = Qnil;
28893 staticpro (&mode_line_string_face);
28894 mode_line_string_face_prop = Qnil;
28895 staticpro (&mode_line_string_face_prop);
28896 Vmode_line_unwind_vector = Qnil;
28897 staticpro (&Vmode_line_unwind_vector);
28899 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28901 help_echo_string = Qnil;
28902 staticpro (&help_echo_string);
28903 help_echo_object = Qnil;
28904 staticpro (&help_echo_object);
28905 help_echo_window = Qnil;
28906 staticpro (&help_echo_window);
28907 previous_help_echo_string = Qnil;
28908 staticpro (&previous_help_echo_string);
28909 help_echo_pos = -1;
28911 DEFSYM (Qright_to_left, "right-to-left");
28912 DEFSYM (Qleft_to_right, "left-to-right");
28914 #ifdef HAVE_WINDOW_SYSTEM
28915 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28916 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28917 For example, if a block cursor is over a tab, it will be drawn as
28918 wide as that tab on the display. */);
28919 x_stretch_cursor_p = 0;
28920 #endif
28922 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28923 doc: /* Non-nil means highlight trailing whitespace.
28924 The face used for trailing whitespace is `trailing-whitespace'. */);
28925 Vshow_trailing_whitespace = Qnil;
28927 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28928 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28929 If the value is t, Emacs highlights non-ASCII chars which have the
28930 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28931 or `escape-glyph' face respectively.
28933 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28934 U+2011 (non-breaking hyphen) are affected.
28936 Any other non-nil value means to display these characters as a escape
28937 glyph followed by an ordinary space or hyphen.
28939 A value of nil means no special handling of these characters. */);
28940 Vnobreak_char_display = Qt;
28942 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28943 doc: /* The pointer shape to show in void text areas.
28944 A value of nil means to show the text pointer. Other options are `arrow',
28945 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28946 Vvoid_text_area_pointer = Qarrow;
28948 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28949 doc: /* Non-nil means don't actually do any redisplay.
28950 This is used for internal purposes. */);
28951 Vinhibit_redisplay = Qnil;
28953 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28954 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28955 Vglobal_mode_string = Qnil;
28957 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28958 doc: /* Marker for where to display an arrow on top of the buffer text.
28959 This must be the beginning of a line in order to work.
28960 See also `overlay-arrow-string'. */);
28961 Voverlay_arrow_position = Qnil;
28963 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28964 doc: /* String to display as an arrow in non-window frames.
28965 See also `overlay-arrow-position'. */);
28966 Voverlay_arrow_string = build_pure_c_string ("=>");
28968 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28969 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28970 The symbols on this list are examined during redisplay to determine
28971 where to display overlay arrows. */);
28972 Voverlay_arrow_variable_list
28973 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28975 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28976 doc: /* The number of lines to try scrolling a window by when point moves out.
28977 If that fails to bring point back on frame, point is centered instead.
28978 If this is zero, point is always centered after it moves off frame.
28979 If you want scrolling to always be a line at a time, you should set
28980 `scroll-conservatively' to a large value rather than set this to 1. */);
28982 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28983 doc: /* Scroll up to this many lines, to bring point back on screen.
28984 If point moves off-screen, redisplay will scroll by up to
28985 `scroll-conservatively' lines in order to bring point just barely
28986 onto the screen again. If that cannot be done, then redisplay
28987 recenters point as usual.
28989 If the value is greater than 100, redisplay will never recenter point,
28990 but will always scroll just enough text to bring point into view, even
28991 if you move far away.
28993 A value of zero means always recenter point if it moves off screen. */);
28994 scroll_conservatively = 0;
28996 DEFVAR_INT ("scroll-margin", scroll_margin,
28997 doc: /* Number of lines of margin at the top and bottom of a window.
28998 Recenter the window whenever point gets within this many lines
28999 of the top or bottom of the window. */);
29000 scroll_margin = 0;
29002 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29003 doc: /* Pixels per inch value for non-window system displays.
29004 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29005 Vdisplay_pixels_per_inch = make_float (72.0);
29007 #ifdef GLYPH_DEBUG
29008 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29009 #endif
29011 DEFVAR_LISP ("truncate-partial-width-windows",
29012 Vtruncate_partial_width_windows,
29013 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29014 For an integer value, truncate lines in each window narrower than the
29015 full frame width, provided the window width is less than that integer;
29016 otherwise, respect the value of `truncate-lines'.
29018 For any other non-nil value, truncate lines in all windows that do
29019 not span the full frame width.
29021 A value of nil means to respect the value of `truncate-lines'.
29023 If `word-wrap' is enabled, you might want to reduce this. */);
29024 Vtruncate_partial_width_windows = make_number (50);
29026 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29027 doc: /* Maximum buffer size for which line number should be displayed.
29028 If the buffer is bigger than this, the line number does not appear
29029 in the mode line. A value of nil means no limit. */);
29030 Vline_number_display_limit = Qnil;
29032 DEFVAR_INT ("line-number-display-limit-width",
29033 line_number_display_limit_width,
29034 doc: /* Maximum line width (in characters) for line number display.
29035 If the average length of the lines near point is bigger than this, then the
29036 line number may be omitted from the mode line. */);
29037 line_number_display_limit_width = 200;
29039 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29040 doc: /* Non-nil means highlight region even in nonselected windows. */);
29041 highlight_nonselected_windows = 0;
29043 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29044 doc: /* Non-nil if more than one frame is visible on this display.
29045 Minibuffer-only frames don't count, but iconified frames do.
29046 This variable is not guaranteed to be accurate except while processing
29047 `frame-title-format' and `icon-title-format'. */);
29049 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29050 doc: /* Template for displaying the title bar of visible frames.
29051 \(Assuming the window manager supports this feature.)
29053 This variable has the same structure as `mode-line-format', except that
29054 the %c and %l constructs are ignored. It is used only on frames for
29055 which no explicit name has been set \(see `modify-frame-parameters'). */);
29057 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29058 doc: /* Template for displaying the title bar of an iconified frame.
29059 \(Assuming the window manager supports this feature.)
29060 This variable has the same structure as `mode-line-format' (which see),
29061 and is used only on frames for which no explicit name has been set
29062 \(see `modify-frame-parameters'). */);
29063 Vicon_title_format
29064 = Vframe_title_format
29065 = listn (CONSTYPE_PURE, 3,
29066 intern_c_string ("multiple-frames"),
29067 build_pure_c_string ("%b"),
29068 listn (CONSTYPE_PURE, 4,
29069 empty_unibyte_string,
29070 intern_c_string ("invocation-name"),
29071 build_pure_c_string ("@"),
29072 intern_c_string ("system-name")));
29074 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29075 doc: /* Maximum number of lines to keep in the message log buffer.
29076 If nil, disable message logging. If t, log messages but don't truncate
29077 the buffer when it becomes large. */);
29078 Vmessage_log_max = make_number (1000);
29080 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29081 doc: /* Functions called before redisplay, if window sizes have changed.
29082 The value should be a list of functions that take one argument.
29083 Just before redisplay, for each frame, if any of its windows have changed
29084 size since the last redisplay, or have been split or deleted,
29085 all the functions in the list are called, with the frame as argument. */);
29086 Vwindow_size_change_functions = Qnil;
29088 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29089 doc: /* List of functions to call before redisplaying a window with scrolling.
29090 Each function is called with two arguments, the window and its new
29091 display-start position. Note that these functions are also called by
29092 `set-window-buffer'. Also note that the value of `window-end' is not
29093 valid when these functions are called.
29095 Warning: Do not use this feature to alter the way the window
29096 is scrolled. It is not designed for that, and such use probably won't
29097 work. */);
29098 Vwindow_scroll_functions = Qnil;
29100 DEFVAR_LISP ("window-text-change-functions",
29101 Vwindow_text_change_functions,
29102 doc: /* Functions to call in redisplay when text in the window might change. */);
29103 Vwindow_text_change_functions = Qnil;
29105 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29106 doc: /* Functions called when redisplay of a window reaches the end trigger.
29107 Each function is called with two arguments, the window and the end trigger value.
29108 See `set-window-redisplay-end-trigger'. */);
29109 Vredisplay_end_trigger_functions = Qnil;
29111 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29112 doc: /* Non-nil means autoselect window with mouse pointer.
29113 If nil, do not autoselect windows.
29114 A positive number means delay autoselection by that many seconds: a
29115 window is autoselected only after the mouse has remained in that
29116 window for the duration of the delay.
29117 A negative number has a similar effect, but causes windows to be
29118 autoselected only after the mouse has stopped moving. \(Because of
29119 the way Emacs compares mouse events, you will occasionally wait twice
29120 that time before the window gets selected.\)
29121 Any other value means to autoselect window instantaneously when the
29122 mouse pointer enters it.
29124 Autoselection selects the minibuffer only if it is active, and never
29125 unselects the minibuffer if it is active.
29127 When customizing this variable make sure that the actual value of
29128 `focus-follows-mouse' matches the behavior of your window manager. */);
29129 Vmouse_autoselect_window = Qnil;
29131 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29132 doc: /* Non-nil means automatically resize tool-bars.
29133 This dynamically changes the tool-bar's height to the minimum height
29134 that is needed to make all tool-bar items visible.
29135 If value is `grow-only', the tool-bar's height is only increased
29136 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29137 Vauto_resize_tool_bars = Qt;
29139 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29140 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29141 auto_raise_tool_bar_buttons_p = 1;
29143 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29144 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29145 make_cursor_line_fully_visible_p = 1;
29147 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29148 doc: /* Border below tool-bar in pixels.
29149 If an integer, use it as the height of the border.
29150 If it is one of `internal-border-width' or `border-width', use the
29151 value of the corresponding frame parameter.
29152 Otherwise, no border is added below the tool-bar. */);
29153 Vtool_bar_border = Qinternal_border_width;
29155 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29156 doc: /* Margin around tool-bar buttons in pixels.
29157 If an integer, use that for both horizontal and vertical margins.
29158 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29159 HORZ specifying the horizontal margin, and VERT specifying the
29160 vertical margin. */);
29161 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29163 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29164 doc: /* Relief thickness of tool-bar buttons. */);
29165 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29167 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29168 doc: /* Tool bar style to use.
29169 It can be one of
29170 image - show images only
29171 text - show text only
29172 both - show both, text below image
29173 both-horiz - show text to the right of the image
29174 text-image-horiz - show text to the left of the image
29175 any other - use system default or image if no system default.
29177 This variable only affects the GTK+ toolkit version of Emacs. */);
29178 Vtool_bar_style = Qnil;
29180 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29181 doc: /* Maximum number of characters a label can have to be shown.
29182 The tool bar style must also show labels for this to have any effect, see
29183 `tool-bar-style'. */);
29184 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29186 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29187 doc: /* List of functions to call to fontify regions of text.
29188 Each function is called with one argument POS. Functions must
29189 fontify a region starting at POS in the current buffer, and give
29190 fontified regions the property `fontified'. */);
29191 Vfontification_functions = Qnil;
29192 Fmake_variable_buffer_local (Qfontification_functions);
29194 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29195 unibyte_display_via_language_environment,
29196 doc: /* Non-nil means display unibyte text according to language environment.
29197 Specifically, this means that raw bytes in the range 160-255 decimal
29198 are displayed by converting them to the equivalent multibyte characters
29199 according to the current language environment. As a result, they are
29200 displayed according to the current fontset.
29202 Note that this variable affects only how these bytes are displayed,
29203 but does not change the fact they are interpreted as raw bytes. */);
29204 unibyte_display_via_language_environment = 0;
29206 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29207 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29208 If a float, it specifies a fraction of the mini-window frame's height.
29209 If an integer, it specifies a number of lines. */);
29210 Vmax_mini_window_height = make_float (0.25);
29212 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29213 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29214 A value of nil means don't automatically resize mini-windows.
29215 A value of t means resize them to fit the text displayed in them.
29216 A value of `grow-only', the default, means let mini-windows grow only;
29217 they return to their normal size when the minibuffer is closed, or the
29218 echo area becomes empty. */);
29219 Vresize_mini_windows = Qgrow_only;
29221 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29222 doc: /* Alist specifying how to blink the cursor off.
29223 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29224 `cursor-type' frame-parameter or variable equals ON-STATE,
29225 comparing using `equal', Emacs uses OFF-STATE to specify
29226 how to blink it off. ON-STATE and OFF-STATE are values for
29227 the `cursor-type' frame parameter.
29229 If a frame's ON-STATE has no entry in this list,
29230 the frame's other specifications determine how to blink the cursor off. */);
29231 Vblink_cursor_alist = Qnil;
29233 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29234 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29235 If non-nil, windows are automatically scrolled horizontally to make
29236 point visible. */);
29237 automatic_hscrolling_p = 1;
29238 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29240 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29241 doc: /* How many columns away from the window edge point is allowed to get
29242 before automatic hscrolling will horizontally scroll the window. */);
29243 hscroll_margin = 5;
29245 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29246 doc: /* How many columns to scroll the window when point gets too close to the edge.
29247 When point is less than `hscroll-margin' columns from the window
29248 edge, automatic hscrolling will scroll the window by the amount of columns
29249 determined by this variable. If its value is a positive integer, scroll that
29250 many columns. If it's a positive floating-point number, it specifies the
29251 fraction of the window's width to scroll. If it's nil or zero, point will be
29252 centered horizontally after the scroll. Any other value, including negative
29253 numbers, are treated as if the value were zero.
29255 Automatic hscrolling always moves point outside the scroll margin, so if
29256 point was more than scroll step columns inside the margin, the window will
29257 scroll more than the value given by the scroll step.
29259 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29260 and `scroll-right' overrides this variable's effect. */);
29261 Vhscroll_step = make_number (0);
29263 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29264 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29265 Bind this around calls to `message' to let it take effect. */);
29266 message_truncate_lines = 0;
29268 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29269 doc: /* Normal hook run to update the menu bar definitions.
29270 Redisplay runs this hook before it redisplays the menu bar.
29271 This is used to update submenus such as Buffers,
29272 whose contents depend on various data. */);
29273 Vmenu_bar_update_hook = Qnil;
29275 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29276 doc: /* Frame for which we are updating a menu.
29277 The enable predicate for a menu binding should check this variable. */);
29278 Vmenu_updating_frame = Qnil;
29280 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29281 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29282 inhibit_menubar_update = 0;
29284 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29285 doc: /* Prefix prepended to all continuation lines at display time.
29286 The value may be a string, an image, or a stretch-glyph; it is
29287 interpreted in the same way as the value of a `display' text property.
29289 This variable is overridden by any `wrap-prefix' text or overlay
29290 property.
29292 To add a prefix to non-continuation lines, use `line-prefix'. */);
29293 Vwrap_prefix = Qnil;
29294 DEFSYM (Qwrap_prefix, "wrap-prefix");
29295 Fmake_variable_buffer_local (Qwrap_prefix);
29297 DEFVAR_LISP ("line-prefix", Vline_prefix,
29298 doc: /* Prefix prepended to all non-continuation lines at display time.
29299 The value may be a string, an image, or a stretch-glyph; it is
29300 interpreted in the same way as the value of a `display' text property.
29302 This variable is overridden by any `line-prefix' text or overlay
29303 property.
29305 To add a prefix to continuation lines, use `wrap-prefix'. */);
29306 Vline_prefix = Qnil;
29307 DEFSYM (Qline_prefix, "line-prefix");
29308 Fmake_variable_buffer_local (Qline_prefix);
29310 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29311 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29312 inhibit_eval_during_redisplay = 0;
29314 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29315 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29316 inhibit_free_realized_faces = 0;
29318 #ifdef GLYPH_DEBUG
29319 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29320 doc: /* Inhibit try_window_id display optimization. */);
29321 inhibit_try_window_id = 0;
29323 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29324 doc: /* Inhibit try_window_reusing display optimization. */);
29325 inhibit_try_window_reusing = 0;
29327 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29328 doc: /* Inhibit try_cursor_movement display optimization. */);
29329 inhibit_try_cursor_movement = 0;
29330 #endif /* GLYPH_DEBUG */
29332 DEFVAR_INT ("overline-margin", overline_margin,
29333 doc: /* Space between overline and text, in pixels.
29334 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29335 margin to the character height. */);
29336 overline_margin = 2;
29338 DEFVAR_INT ("underline-minimum-offset",
29339 underline_minimum_offset,
29340 doc: /* Minimum distance between baseline and underline.
29341 This can improve legibility of underlined text at small font sizes,
29342 particularly when using variable `x-use-underline-position-properties'
29343 with fonts that specify an UNDERLINE_POSITION relatively close to the
29344 baseline. The default value is 1. */);
29345 underline_minimum_offset = 1;
29347 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29348 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29349 This feature only works when on a window system that can change
29350 cursor shapes. */);
29351 display_hourglass_p = 1;
29353 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29354 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29355 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29357 hourglass_atimer = NULL;
29358 hourglass_shown_p = 0;
29360 DEFSYM (Qglyphless_char, "glyphless-char");
29361 DEFSYM (Qhex_code, "hex-code");
29362 DEFSYM (Qempty_box, "empty-box");
29363 DEFSYM (Qthin_space, "thin-space");
29364 DEFSYM (Qzero_width, "zero-width");
29366 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29367 /* Intern this now in case it isn't already done.
29368 Setting this variable twice is harmless.
29369 But don't staticpro it here--that is done in alloc.c. */
29370 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29371 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29373 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29374 doc: /* Char-table defining glyphless characters.
29375 Each element, if non-nil, should be one of the following:
29376 an ASCII acronym string: display this string in a box
29377 `hex-code': display the hexadecimal code of a character in a box
29378 `empty-box': display as an empty box
29379 `thin-space': display as 1-pixel width space
29380 `zero-width': don't display
29381 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29382 display method for graphical terminals and text terminals respectively.
29383 GRAPHICAL and TEXT should each have one of the values listed above.
29385 The char-table has one extra slot to control the display of a character for
29386 which no font is found. This slot only takes effect on graphical terminals.
29387 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29388 `thin-space'. The default is `empty-box'. */);
29389 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29390 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29391 Qempty_box);
29393 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29394 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29395 Vdebug_on_message = Qnil;
29399 /* Initialize this module when Emacs starts. */
29401 void
29402 init_xdisp (void)
29404 current_header_line_height = current_mode_line_height = -1;
29406 CHARPOS (this_line_start_pos) = 0;
29408 if (!noninteractive)
29410 struct window *m = XWINDOW (minibuf_window);
29411 Lisp_Object frame = m->frame;
29412 struct frame *f = XFRAME (frame);
29413 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29414 struct window *r = XWINDOW (root);
29415 int i;
29417 echo_area_window = minibuf_window;
29419 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29420 wset_total_lines
29421 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29422 wset_total_cols (r, make_number (FRAME_COLS (f)));
29423 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29424 wset_total_lines (m, make_number (1));
29425 wset_total_cols (m, make_number (FRAME_COLS (f)));
29427 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29428 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29429 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29431 /* The default ellipsis glyphs `...'. */
29432 for (i = 0; i < 3; ++i)
29433 default_invis_vector[i] = make_number ('.');
29437 /* Allocate the buffer for frame titles.
29438 Also used for `format-mode-line'. */
29439 int size = 100;
29440 mode_line_noprop_buf = xmalloc (size);
29441 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29442 mode_line_noprop_ptr = mode_line_noprop_buf;
29443 mode_line_target = MODE_LINE_DISPLAY;
29446 help_echo_showing_p = 0;
29449 /* Platform-independent portion of hourglass implementation. */
29451 /* Cancel a currently active hourglass timer, and start a new one. */
29452 void
29453 start_hourglass (void)
29455 #if defined (HAVE_WINDOW_SYSTEM)
29456 EMACS_TIME delay;
29458 cancel_hourglass ();
29460 if (INTEGERP (Vhourglass_delay)
29461 && XINT (Vhourglass_delay) > 0)
29462 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29463 TYPE_MAXIMUM (time_t)),
29465 else if (FLOATP (Vhourglass_delay)
29466 && XFLOAT_DATA (Vhourglass_delay) > 0)
29467 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29468 else
29469 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29471 #ifdef HAVE_NTGUI
29473 extern void w32_note_current_window (void);
29474 w32_note_current_window ();
29476 #endif /* HAVE_NTGUI */
29478 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29479 show_hourglass, NULL);
29480 #endif
29484 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29485 shown. */
29486 void
29487 cancel_hourglass (void)
29489 #if defined (HAVE_WINDOW_SYSTEM)
29490 if (hourglass_atimer)
29492 cancel_atimer (hourglass_atimer);
29493 hourglass_atimer = NULL;
29496 if (hourglass_shown_p)
29497 hide_hourglass ();
29498 #endif